Skip to content

Commit f118487

Browse files
committed
PCI: hv: Add a per-bus mutex state_lock
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2182619 commit 067d6ec Author: Dexuan Cui <decui@microsoft.com> Date: Wed Jun 14 21:44:51 2023 -0700 PCI: hv: Add a per-bus mutex state_lock In the case of fast device addition/removal, it's possible that hv_eject_device_work() can start to run before create_root_hv_pci_bus() starts to run; as a result, the pci_get_domain_bus_and_slot() in hv_eject_device_work() can return a 'pdev' of NULL, and hv_eject_device_work() can remove the 'hpdev', and immediately send a message PCI_EJECTION_COMPLETE to the host, and the host immediately unassigns the PCI device from the guest; meanwhile, create_root_hv_pci_bus() and the PCI device driver can be probing the dead PCI device and reporting timeout errors. Fix the issue by adding a per-bus mutex 'state_lock' and grabbing the mutex before powering on the PCI bus in hv_pci_enter_d0(): when hv_eject_device_work() starts to run, it's able to find the 'pdev' and call pci_stop_and_remove_bus_device(pdev): if the PCI device driver has loaded, the PCI device driver's probe() function is already called in create_root_hv_pci_bus() -> pci_bus_add_devices(), and now hv_eject_device_work() -> pci_stop_and_remove_bus_device() is able to call the PCI device driver's remove() function and remove the device reliably; if the PCI device driver hasn't loaded yet, the function call hv_eject_device_work() -> pci_stop_and_remove_bus_device() is able to remove the PCI device reliably and the PCI device driver's probe() function won't be called; if the PCI device driver's probe() is already running (e.g., systemd-udev is loading the PCI device driver), it must be holding the per-device lock, and after the probe() finishes and releases the lock, hv_eject_device_work() -> pci_stop_and_remove_bus_device() is able to proceed to remove the device reliably. Fixes: 4daace0 ("PCI: hv: Add paravirtual PCI front-end for Microsoft Hyper-V VMs") Signed-off-by: Dexuan Cui <decui@microsoft.com> Reviewed-by: Michael Kelley <mikelley@microsoft.com> Acked-by: Lorenzo Pieralisi <lpieralisi@kernel.org> Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20230615044451.5580-6-decui@microsoft.com Signed-off-by: Wei Liu <wei.liu@kernel.org> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
1 parent ab1b2ab commit f118487

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

drivers/pci/controller/pci-hyperv.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,10 @@ struct hv_pcibus_device {
489489
struct fwnode_handle *fwnode;
490490
/* Protocol version negotiated with the host */
491491
enum pci_protocol_version_t protocol_version;
492+
493+
struct mutex state_lock;
492494
enum hv_pcibus_state state;
495+
493496
struct hv_device *hdev;
494497
resource_size_t low_mmio_space;
495498
resource_size_t high_mmio_space;
@@ -2512,6 +2515,8 @@ static void pci_devices_present_work(struct work_struct *work)
25122515
if (!dr)
25132516
return;
25142517

2518+
mutex_lock(&hbus->state_lock);
2519+
25152520
/* First, mark all existing children as reported missing. */
25162521
spin_lock_irqsave(&hbus->device_list_lock, flags);
25172522
list_for_each_entry(hpdev, &hbus->children, list_entry) {
@@ -2593,6 +2598,8 @@ static void pci_devices_present_work(struct work_struct *work)
25932598
break;
25942599
}
25952600

2601+
mutex_unlock(&hbus->state_lock);
2602+
25962603
kfree(dr);
25972604
}
25982605

@@ -2741,6 +2748,8 @@ static void hv_eject_device_work(struct work_struct *work)
27412748
hpdev = container_of(work, struct hv_pci_dev, wrk);
27422749
hbus = hpdev->hbus;
27432750

2751+
mutex_lock(&hbus->state_lock);
2752+
27442753
/*
27452754
* Ejection can come before or after the PCI bus has been set up, so
27462755
* attempt to find it and tear down the bus state, if it exists. This
@@ -2777,6 +2786,8 @@ static void hv_eject_device_work(struct work_struct *work)
27772786
put_pcichild(hpdev);
27782787
put_pcichild(hpdev);
27792788
/* hpdev has been freed. Do not use it any more. */
2789+
2790+
mutex_unlock(&hbus->state_lock);
27802791
}
27812792

27822793
/**
@@ -3567,6 +3578,7 @@ static int hv_pci_probe(struct hv_device *hdev,
35673578
return -ENOMEM;
35683579

35693580
hbus->bridge = bridge;
3581+
mutex_init(&hbus->state_lock);
35703582
hbus->state = hv_pcibus_init;
35713583
hbus->wslot_res_allocated = -1;
35723584

@@ -3675,9 +3687,11 @@ static int hv_pci_probe(struct hv_device *hdev,
36753687
if (ret)
36763688
goto free_irq_domain;
36773689

3690+
mutex_lock(&hbus->state_lock);
3691+
36783692
ret = hv_pci_enter_d0(hdev);
36793693
if (ret)
3680-
goto free_irq_domain;
3694+
goto release_state_lock;
36813695

36823696
ret = hv_pci_allocate_bridge_windows(hbus);
36833697
if (ret)
@@ -3695,12 +3709,15 @@ static int hv_pci_probe(struct hv_device *hdev,
36953709
if (ret)
36963710
goto free_windows;
36973711

3712+
mutex_unlock(&hbus->state_lock);
36983713
return 0;
36993714

37003715
free_windows:
37013716
hv_pci_free_bridge_windows(hbus);
37023717
exit_d0:
37033718
(void) hv_pci_bus_exit(hdev, true);
3719+
release_state_lock:
3720+
mutex_unlock(&hbus->state_lock);
37043721
free_irq_domain:
37053722
irq_domain_remove(hbus->irq_domain);
37063723
free_fwnode:
@@ -3950,20 +3967,26 @@ static int hv_pci_resume(struct hv_device *hdev)
39503967
if (ret)
39513968
goto out;
39523969

3970+
mutex_lock(&hbus->state_lock);
3971+
39533972
ret = hv_pci_enter_d0(hdev);
39543973
if (ret)
3955-
goto out;
3974+
goto release_state_lock;
39563975

39573976
ret = hv_send_resources_allocated(hdev);
39583977
if (ret)
3959-
goto out;
3978+
goto release_state_lock;
39603979

39613980
prepopulate_bars(hbus);
39623981

39633982
hv_pci_restore_msi_state(hbus);
39643983

39653984
hbus->state = hv_pcibus_installed;
3985+
mutex_unlock(&hbus->state_lock);
39663986
return 0;
3987+
3988+
release_state_lock:
3989+
mutex_unlock(&hbus->state_lock);
39673990
out:
39683991
vmbus_close(hdev->channel);
39693992
return ret;

0 commit comments

Comments
 (0)