Skip to content

Commit 6a7e448

Browse files
brettcreeleyawilliam
authored andcommitted
vfio/pds: Refactor/simplify reset logic
The current logic for handling resets is more complicated than it needs to be. The deferred_reset flag is used to indicate a reset is needed and the deferred_reset_state is the requested, post-reset, state. Also, the deferred_reset logic was added to vfio migration drivers to prevent a circular locking dependency with respect to mm_lock and state mutex. This is mainly because of the copy_to/from_user() functions(which takes mm_lock) invoked under state mutex. Remove all of the deferred reset logic and just pass the requested next state to pds_vfio_reset() so it can be used for VMM and DSC initiated resets. This removes the need for pds_vfio_state_mutex_lock(), so remove that and replace its use with a simple mutex_unlock(). Also, remove the reset_mutex as it's no longer needed since the state_mutex can be the driver's primary protector. Suggested-by: Shameer Kolothum <shameerali.kolothum.thodi@huawei.com> Reviewed-by: Shannon Nelson <shannon.nelson@amd.com> Signed-off-by: Brett Creeley <brett.creeley@amd.com> Link: https://lore.kernel.org/r/20240308182149.22036-3-brett.creeley@amd.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent 457f730 commit 6a7e448

File tree

4 files changed

+19
-67
lines changed

4 files changed

+19
-67
lines changed

drivers/vfio/pci/pds/dirty.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ int pds_vfio_dma_logging_report(struct vfio_device *vdev, unsigned long iova,
607607

608608
mutex_lock(&pds_vfio->state_mutex);
609609
err = pds_vfio_dirty_sync(pds_vfio, dirty, iova, length);
610-
pds_vfio_state_mutex_unlock(pds_vfio);
610+
mutex_unlock(&pds_vfio->state_mutex);
611611

612612
return err;
613613
}
@@ -624,7 +624,7 @@ int pds_vfio_dma_logging_start(struct vfio_device *vdev,
624624
mutex_lock(&pds_vfio->state_mutex);
625625
pds_vfio_send_host_vf_lm_status_cmd(pds_vfio, PDS_LM_STA_IN_PROGRESS);
626626
err = pds_vfio_dirty_enable(pds_vfio, ranges, nnodes, page_size);
627-
pds_vfio_state_mutex_unlock(pds_vfio);
627+
mutex_unlock(&pds_vfio->state_mutex);
628628

629629
return err;
630630
}
@@ -637,7 +637,7 @@ int pds_vfio_dma_logging_stop(struct vfio_device *vdev)
637637

638638
mutex_lock(&pds_vfio->state_mutex);
639639
pds_vfio_dirty_disable(pds_vfio, true);
640-
pds_vfio_state_mutex_unlock(pds_vfio);
640+
mutex_unlock(&pds_vfio->state_mutex);
641641

642642
return 0;
643643
}

drivers/vfio/pci/pds/pci_drv.c

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@
2121

2222
static void pds_vfio_recovery(struct pds_vfio_pci_device *pds_vfio)
2323
{
24-
bool deferred_reset_needed = false;
25-
2624
/*
2725
* Documentation states that the kernel migration driver must not
2826
* generate asynchronous device state transitions outside of
2927
* manipulation by the user or the VFIO_DEVICE_RESET ioctl.
3028
*
3129
* Since recovery is an asynchronous event received from the device,
32-
* initiate a deferred reset. Issue a deferred reset in the following
33-
* situations:
30+
* initiate a reset in the following situations:
3431
* 1. Migration is in progress, which will cause the next step of
3532
* the migration to fail.
3633
* 2. If the device is in a state that will be set to
@@ -42,24 +39,8 @@ static void pds_vfio_recovery(struct pds_vfio_pci_device *pds_vfio)
4239
pds_vfio->state != VFIO_DEVICE_STATE_ERROR) ||
4340
(pds_vfio->state == VFIO_DEVICE_STATE_RUNNING &&
4441
pds_vfio_dirty_is_enabled(pds_vfio)))
45-
deferred_reset_needed = true;
42+
pds_vfio_reset(pds_vfio, VFIO_DEVICE_STATE_ERROR);
4643
mutex_unlock(&pds_vfio->state_mutex);
47-
48-
/*
49-
* On the next user initiated state transition, the device will
50-
* transition to the VFIO_DEVICE_STATE_ERROR. At this point it's the user's
51-
* responsibility to reset the device.
52-
*
53-
* If a VFIO_DEVICE_RESET is requested post recovery and before the next
54-
* state transition, then the deferred reset state will be set to
55-
* VFIO_DEVICE_STATE_RUNNING.
56-
*/
57-
if (deferred_reset_needed) {
58-
mutex_lock(&pds_vfio->reset_mutex);
59-
pds_vfio->deferred_reset = true;
60-
pds_vfio->deferred_reset_state = VFIO_DEVICE_STATE_ERROR;
61-
mutex_unlock(&pds_vfio->reset_mutex);
62-
}
6344
}
6445

6546
static int pds_vfio_pci_notify_handler(struct notifier_block *nb,
@@ -185,7 +166,9 @@ static void pds_vfio_pci_aer_reset_done(struct pci_dev *pdev)
185166
{
186167
struct pds_vfio_pci_device *pds_vfio = pds_vfio_pci_drvdata(pdev);
187168

188-
pds_vfio_reset(pds_vfio);
169+
mutex_lock(&pds_vfio->state_mutex);
170+
pds_vfio_reset(pds_vfio, VFIO_DEVICE_STATE_RUNNING);
171+
mutex_unlock(&pds_vfio->state_mutex);
189172
}
190173

191174
static const struct pci_error_handlers pds_vfio_pci_err_handlers = {

drivers/vfio/pci/pds/vfio_dev.c

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,14 @@ struct pds_vfio_pci_device *pds_vfio_pci_drvdata(struct pci_dev *pdev)
2626
vfio_coredev);
2727
}
2828

29-
void pds_vfio_state_mutex_unlock(struct pds_vfio_pci_device *pds_vfio)
29+
void pds_vfio_reset(struct pds_vfio_pci_device *pds_vfio,
30+
enum vfio_device_mig_state state)
3031
{
31-
again:
32-
mutex_lock(&pds_vfio->reset_mutex);
33-
if (pds_vfio->deferred_reset) {
34-
pds_vfio->deferred_reset = false;
35-
pds_vfio_put_restore_file(pds_vfio);
36-
pds_vfio_put_save_file(pds_vfio);
37-
if (pds_vfio->state == VFIO_DEVICE_STATE_ERROR) {
38-
pds_vfio_dirty_disable(pds_vfio, false);
39-
}
40-
pds_vfio->state = pds_vfio->deferred_reset_state;
41-
pds_vfio->deferred_reset_state = VFIO_DEVICE_STATE_RUNNING;
42-
mutex_unlock(&pds_vfio->reset_mutex);
43-
goto again;
44-
}
45-
mutex_unlock(&pds_vfio->state_mutex);
46-
mutex_unlock(&pds_vfio->reset_mutex);
47-
}
48-
49-
void pds_vfio_reset(struct pds_vfio_pci_device *pds_vfio)
50-
{
51-
mutex_lock(&pds_vfio->reset_mutex);
52-
pds_vfio->deferred_reset = true;
53-
pds_vfio->deferred_reset_state = VFIO_DEVICE_STATE_RUNNING;
54-
if (!mutex_trylock(&pds_vfio->state_mutex)) {
55-
mutex_unlock(&pds_vfio->reset_mutex);
56-
return;
57-
}
58-
mutex_unlock(&pds_vfio->reset_mutex);
59-
pds_vfio_state_mutex_unlock(pds_vfio);
32+
pds_vfio_put_restore_file(pds_vfio);
33+
pds_vfio_put_save_file(pds_vfio);
34+
if (state == VFIO_DEVICE_STATE_ERROR)
35+
pds_vfio_dirty_disable(pds_vfio, false);
36+
pds_vfio->state = state;
6037
}
6138

6239
static struct file *
@@ -97,8 +74,7 @@ pds_vfio_set_device_state(struct vfio_device *vdev,
9774
break;
9875
}
9976
}
100-
pds_vfio_state_mutex_unlock(pds_vfio);
101-
/* still waiting on a deferred_reset */
77+
mutex_unlock(&pds_vfio->state_mutex);
10278
if (pds_vfio->state == VFIO_DEVICE_STATE_ERROR)
10379
res = ERR_PTR(-EIO);
10480

@@ -114,7 +90,7 @@ static int pds_vfio_get_device_state(struct vfio_device *vdev,
11490

11591
mutex_lock(&pds_vfio->state_mutex);
11692
*current_state = pds_vfio->state;
117-
pds_vfio_state_mutex_unlock(pds_vfio);
93+
mutex_unlock(&pds_vfio->state_mutex);
11894
return 0;
11995
}
12096

@@ -156,7 +132,6 @@ static int pds_vfio_init_device(struct vfio_device *vdev)
156132
pds_vfio->vf_id = vf_id;
157133

158134
mutex_init(&pds_vfio->state_mutex);
159-
mutex_init(&pds_vfio->reset_mutex);
160135

161136
vdev->migration_flags = VFIO_MIGRATION_STOP_COPY | VFIO_MIGRATION_P2P;
162137
vdev->mig_ops = &pds_vfio_lm_ops;
@@ -178,7 +153,6 @@ static void pds_vfio_release_device(struct vfio_device *vdev)
178153
vfio_coredev.vdev);
179154

180155
mutex_destroy(&pds_vfio->state_mutex);
181-
mutex_destroy(&pds_vfio->reset_mutex);
182156
vfio_pci_core_release_dev(vdev);
183157
}
184158

@@ -194,7 +168,6 @@ static int pds_vfio_open_device(struct vfio_device *vdev)
194168
return err;
195169

196170
pds_vfio->state = VFIO_DEVICE_STATE_RUNNING;
197-
pds_vfio->deferred_reset_state = VFIO_DEVICE_STATE_RUNNING;
198171

199172
vfio_pci_core_finish_enable(&pds_vfio->vfio_coredev);
200173

drivers/vfio/pci/pds/vfio_dev.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,16 @@ struct pds_vfio_pci_device {
1818
struct pds_vfio_dirty dirty;
1919
struct mutex state_mutex; /* protect migration state */
2020
enum vfio_device_mig_state state;
21-
struct mutex reset_mutex; /* protect reset_done flow */
22-
u8 deferred_reset;
23-
enum vfio_device_mig_state deferred_reset_state;
2421
struct notifier_block nb;
2522

2623
int vf_id;
2724
u16 client_id;
2825
};
2926

30-
void pds_vfio_state_mutex_unlock(struct pds_vfio_pci_device *pds_vfio);
31-
3227
const struct vfio_device_ops *pds_vfio_ops_info(void);
3328
struct pds_vfio_pci_device *pds_vfio_pci_drvdata(struct pci_dev *pdev);
34-
void pds_vfio_reset(struct pds_vfio_pci_device *pds_vfio);
29+
void pds_vfio_reset(struct pds_vfio_pci_device *pds_vfio,
30+
enum vfio_device_mig_state state);
3531

3632
struct pci_dev *pds_vfio_to_pci_dev(struct pds_vfio_pci_device *pds_vfio);
3733
struct device *pds_vfio_to_dev(struct pds_vfio_pci_device *pds_vfio);

0 commit comments

Comments
 (0)