From: "Michał Winiarski" <michal.winiarski@intel.com>
To: Jason Gunthorpe <jgg@ziepe.ca>,
Alex Williamson <alex@shazbot.org>,
"Kevin Tian" <kevin.tian@intel.com>,
Yishai Hadas <yishaih@nvidia.com>,
Longfang Liu <liulongfang@huawei.com>,
Shameer Kolothum <skolothumtho@nvidia.com>,
"Brett Creeley" <brett.creeley@amd.com>,
Giovanni Cabiddu <giovanni.cabiddu@intel.com>,
<kvm@vger.kernel.org>, <qat-linux@intel.com>,
<virtualization@lists.linux.dev>, <linux-kernel@vger.kernel.org>
Cc: "Michał Winiarski" <michal.winiarski@intel.com>
Subject: [PATCH 6/6] vfio/virtio: Use .migration_reset_state() callback
Date: Thu, 20 Nov 2025 13:36:47 +0100 [thread overview]
Message-ID: <20251120123647.3522082-7-michal.winiarski@intel.com> (raw)
In-Reply-To: <20251120123647.3522082-1-michal.winiarski@intel.com>
Move the migration device state reset code from .reset_done() to
dedicated callback.
Remove the deferred reset mechanism, as it's no longer needed.
Signed-off-by: Michał Winiarski <michal.winiarski@intel.com>
---
drivers/vfio/pci/virtio/common.h | 3 --
drivers/vfio/pci/virtio/main.c | 1 -
drivers/vfio/pci/virtio/migrate.c | 71 +++++++++----------------------
3 files changed, 21 insertions(+), 54 deletions(-)
diff --git a/drivers/vfio/pci/virtio/common.h b/drivers/vfio/pci/virtio/common.h
index c7d7e27af386e..cb27d3d2d3bb9 100644
--- a/drivers/vfio/pci/virtio/common.h
+++ b/drivers/vfio/pci/virtio/common.h
@@ -92,12 +92,9 @@ struct virtiovf_pci_core_device {
/* LM related */
u8 migrate_cap:1;
- u8 deferred_reset:1;
/* protect migration state */
struct mutex state_mutex;
enum vfio_device_mig_state mig_state;
- /* protect the reset_done flow */
- spinlock_t reset_lock;
struct virtiovf_migration_file *resuming_migf;
struct virtiovf_migration_file *saving_migf;
};
diff --git a/drivers/vfio/pci/virtio/main.c b/drivers/vfio/pci/virtio/main.c
index 8084f3e36a9f7..b80cb740f9a5d 100644
--- a/drivers/vfio/pci/virtio/main.c
+++ b/drivers/vfio/pci/virtio/main.c
@@ -203,7 +203,6 @@ static void virtiovf_pci_aer_reset_done(struct pci_dev *pdev)
#ifdef CONFIG_VIRTIO_VFIO_PCI_ADMIN_LEGACY
virtiovf_legacy_io_reset_done(pdev);
#endif
- virtiovf_migration_reset_done(pdev);
}
static const struct pci_error_handlers virtiovf_err_handlers = {
diff --git a/drivers/vfio/pci/virtio/migrate.c b/drivers/vfio/pci/virtio/migrate.c
index 7dd0ac866461d..5c7f9091d84e8 100644
--- a/drivers/vfio/pci/virtio/migrate.c
+++ b/drivers/vfio/pci/virtio/migrate.c
@@ -247,49 +247,6 @@ static void virtiovf_disable_fds(struct virtiovf_pci_core_device *virtvdev)
}
}
-/*
- * This function is called in all state_mutex unlock cases to
- * handle a 'deferred_reset' if exists.
- */
-static void virtiovf_state_mutex_unlock(struct virtiovf_pci_core_device *virtvdev)
-{
-again:
- spin_lock(&virtvdev->reset_lock);
- if (virtvdev->deferred_reset) {
- virtvdev->deferred_reset = false;
- spin_unlock(&virtvdev->reset_lock);
- virtvdev->mig_state = VFIO_DEVICE_STATE_RUNNING;
- virtiovf_disable_fds(virtvdev);
- goto again;
- }
- mutex_unlock(&virtvdev->state_mutex);
- spin_unlock(&virtvdev->reset_lock);
-}
-
-void virtiovf_migration_reset_done(struct pci_dev *pdev)
-{
- struct virtiovf_pci_core_device *virtvdev = dev_get_drvdata(&pdev->dev);
-
- if (!virtvdev->migrate_cap)
- return;
-
- /*
- * As the higher VFIO layers are holding locks across reset and using
- * those same locks with the mm_lock we need to prevent ABBA deadlock
- * with the state_mutex and mm_lock.
- * In case the state_mutex was taken already we defer the cleanup work
- * to the unlock flow of the other running context.
- */
- spin_lock(&virtvdev->reset_lock);
- virtvdev->deferred_reset = true;
- if (!mutex_trylock(&virtvdev->state_mutex)) {
- spin_unlock(&virtvdev->reset_lock);
- return;
- }
- spin_unlock(&virtvdev->reset_lock);
- virtiovf_state_mutex_unlock(virtvdev);
-}
-
static int virtiovf_release_file(struct inode *inode, struct file *filp)
{
struct virtiovf_migration_file *migf = filp->private_data;
@@ -513,7 +470,7 @@ static long virtiovf_precopy_ioctl(struct file *filp, unsigned int cmd,
goto err_state_unlock;
done:
- virtiovf_state_mutex_unlock(virtvdev);
+ mutex_unlock(&virtvdev->state_mutex);
if (copy_to_user((void __user *)arg, &info, minsz))
return -EFAULT;
return 0;
@@ -521,7 +478,7 @@ static long virtiovf_precopy_ioctl(struct file *filp, unsigned int cmd,
err_migf_unlock:
mutex_unlock(&migf->lock);
err_state_unlock:
- virtiovf_state_mutex_unlock(virtvdev);
+ mutex_unlock(&virtvdev->state_mutex);
return ret;
}
@@ -1048,7 +1005,7 @@ static ssize_t virtiovf_resume_write(struct file *filp, const char __user *buf,
if (ret)
migf->state = VIRTIOVF_MIGF_STATE_ERROR;
mutex_unlock(&migf->lock);
- virtiovf_state_mutex_unlock(migf->virtvdev);
+ mutex_unlock(&migf->virtvdev->state_mutex);
return ret ? ret : done;
}
@@ -1245,7 +1202,7 @@ virtiovf_pci_set_device_state(struct vfio_device *vdev,
break;
}
}
- virtiovf_state_mutex_unlock(virtvdev);
+ mutex_unlock(&virtvdev->state_mutex);
return res;
}
@@ -1257,10 +1214,24 @@ static int virtiovf_pci_get_device_state(struct vfio_device *vdev,
mutex_lock(&virtvdev->state_mutex);
*curr_state = virtvdev->mig_state;
- virtiovf_state_mutex_unlock(virtvdev);
+ mutex_unlock(&virtvdev->state_mutex);
return 0;
}
+static void virtiovf_pci_reset_device_state(struct vfio_device *vdev)
+{
+ struct virtiovf_pci_core_device *virtvdev = container_of(
+ vdev, struct virtiovf_pci_core_device, core_device.vdev);
+
+ if (!virtvdev->migrate_cap)
+ return;
+
+ mutex_lock(&virtvdev->state_mutex);
+ virtvdev->mig_state = VFIO_DEVICE_STATE_RUNNING;
+ virtiovf_disable_fds(virtvdev);
+ mutex_unlock(&virtvdev->state_mutex);
+}
+
static int virtiovf_pci_get_data_size(struct vfio_device *vdev,
unsigned long *stop_copy_length)
{
@@ -1297,13 +1268,14 @@ static int virtiovf_pci_get_data_size(struct vfio_device *vdev,
if (!obj_id_exists)
virtiovf_pci_free_obj_id(virtvdev, obj_id);
end:
- virtiovf_state_mutex_unlock(virtvdev);
+ mutex_unlock(&virtvdev->state_mutex);
return ret;
}
static const struct vfio_migration_ops virtvdev_pci_mig_ops = {
.migration_set_state = virtiovf_pci_set_device_state,
.migration_get_state = virtiovf_pci_get_device_state,
+ .migration_reset_state = virtiovf_pci_reset_device_state,
.migration_get_data_size = virtiovf_pci_get_data_size,
};
@@ -1311,7 +1283,6 @@ void virtiovf_set_migratable(struct virtiovf_pci_core_device *virtvdev)
{
virtvdev->migrate_cap = 1;
mutex_init(&virtvdev->state_mutex);
- spin_lock_init(&virtvdev->reset_lock);
virtvdev->core_device.vdev.migration_flags =
VFIO_MIGRATION_STOP_COPY |
VFIO_MIGRATION_P2P |
--
2.51.2
prev parent reply other threads:[~2025-11-20 12:38 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-20 12:36 [PATCH 0/6] vfio: Reset migration device state using dedicated callback Michał Winiarski
2025-11-20 12:36 ` [PATCH 1/6] vfio: Introduce .migration_reset_state() callback Michał Winiarski
2025-11-21 7:13 ` Tian, Kevin
2025-11-24 21:29 ` Winiarski, Michal
2025-11-21 18:16 ` Alex Williamson
2025-11-24 21:44 ` Michał Winiarski
2025-11-20 12:36 ` [PATCH 2/6] hisi_acc_vfio_pci: Use " Michał Winiarski
2025-11-20 12:36 ` [PATCH 3/6] vfio/pds: " Michał Winiarski
2025-11-20 12:36 ` [PATCH 4/6] vfio/qat: " Michał Winiarski
2025-11-20 12:36 ` [PATCH 5/6] vfio/mlx5: " Michał Winiarski
2025-11-20 12:36 ` Michał Winiarski [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251120123647.3522082-7-michal.winiarski@intel.com \
--to=michal.winiarski@intel.com \
--cc=alex@shazbot.org \
--cc=brett.creeley@amd.com \
--cc=giovanni.cabiddu@intel.com \
--cc=jgg@ziepe.ca \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=liulongfang@huawei.com \
--cc=qat-linux@intel.com \
--cc=skolothumtho@nvidia.com \
--cc=virtualization@lists.linux.dev \
--cc=yishaih@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®