Skip to content

Commit 91aeb56

Browse files
brettcreeleyawilliam
authored andcommitted
vfio/pds: Fix mutex lock->magic != lock warning
The following BUG was found when running on a kernel with CONFIG_DEBUG_MUTEXES=y set: DEBUG_LOCKS_WARN_ON(lock->magic != lock) RIP: 0010:mutex_trylock+0x10d/0x120 Call Trace: <TASK> ? __warn+0x85/0x140 ? mutex_trylock+0x10d/0x120 ? report_bug+0xfc/0x1e0 ? handle_bug+0x3f/0x70 ? exc_invalid_op+0x17/0x70 ? asm_exc_invalid_op+0x1a/0x20 ? mutex_trylock+0x10d/0x120 ? mutex_trylock+0x10d/0x120 pds_vfio_reset+0x3a/0x60 [pds_vfio_pci] pci_reset_function+0x4b/0x70 reset_store+0x5b/0xa0 kernfs_fop_write_iter+0x137/0x1d0 vfs_write+0x2de/0x410 ksys_write+0x5d/0xd0 do_syscall_64+0x3b/0x90 entry_SYSCALL_64_after_hwframe+0x6e/0xd8 As shown, lock->magic != lock. This is because mutex_init(&pds_vfio->state_mutex) is called in the VFIO open path. So, if a reset is initiated before the VFIO device is opened the mutex will have never been initialized. Fix this by calling mutex_init(&pds_vfio->state_mutex) in the VFIO init path. Also, don't destroy the mutex on close because the device may be re-opened, which would cause mutex to be uninitialized. Fix this by implementing a driver specific vfio_device_ops.release callback that destroys the mutex before calling vfio_pci_core_release_dev(). Fixes: bb500db ("vfio/pds: Add VFIO live migration support") Signed-off-by: Brett Creeley <brett.creeley@amd.com> Reviewed-by: Shannon Nelson <shannon.nelson@amd.com> Link: https://lore.kernel.org/r/20231122192532.25791-2-brett.creeley@amd.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent 2cc14f5 commit 91aeb56

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

drivers/vfio/pci/pds/vfio_dev.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ static int pds_vfio_init_device(struct vfio_device *vdev)
155155

156156
pds_vfio->vf_id = vf_id;
157157

158+
mutex_init(&pds_vfio->state_mutex);
159+
158160
vdev->migration_flags = VFIO_MIGRATION_STOP_COPY | VFIO_MIGRATION_P2P;
159161
vdev->mig_ops = &pds_vfio_lm_ops;
160162
vdev->log_ops = &pds_vfio_log_ops;
@@ -168,6 +170,16 @@ static int pds_vfio_init_device(struct vfio_device *vdev)
168170
return 0;
169171
}
170172

173+
static void pds_vfio_release_device(struct vfio_device *vdev)
174+
{
175+
struct pds_vfio_pci_device *pds_vfio =
176+
container_of(vdev, struct pds_vfio_pci_device,
177+
vfio_coredev.vdev);
178+
179+
mutex_destroy(&pds_vfio->state_mutex);
180+
vfio_pci_core_release_dev(vdev);
181+
}
182+
171183
static int pds_vfio_open_device(struct vfio_device *vdev)
172184
{
173185
struct pds_vfio_pci_device *pds_vfio =
@@ -179,7 +191,6 @@ static int pds_vfio_open_device(struct vfio_device *vdev)
179191
if (err)
180192
return err;
181193

182-
mutex_init(&pds_vfio->state_mutex);
183194
pds_vfio->state = VFIO_DEVICE_STATE_RUNNING;
184195
pds_vfio->deferred_reset_state = VFIO_DEVICE_STATE_RUNNING;
185196

@@ -199,14 +210,13 @@ static void pds_vfio_close_device(struct vfio_device *vdev)
199210
pds_vfio_put_save_file(pds_vfio);
200211
pds_vfio_dirty_disable(pds_vfio, true);
201212
mutex_unlock(&pds_vfio->state_mutex);
202-
mutex_destroy(&pds_vfio->state_mutex);
203213
vfio_pci_core_close_device(vdev);
204214
}
205215

206216
static const struct vfio_device_ops pds_vfio_ops = {
207217
.name = "pds-vfio",
208218
.init = pds_vfio_init_device,
209-
.release = vfio_pci_core_release_dev,
219+
.release = pds_vfio_release_device,
210220
.open_device = pds_vfio_open_device,
211221
.close_device = pds_vfio_close_device,
212222
.ioctl = vfio_pci_core_ioctl,

0 commit comments

Comments
 (0)