pci-hyper: Fix race condition bugs for fast device hotplug
Subsystems (8)
Dexuan Cui
maintainerv2 cover pci-hyper: Fix race condition bugs for fast device hotplug
Before the guest finishes probing a device, the host may be already starting to remove the device. Currently there are multiple race condition bugs in the pci-hyperv driver, which can cause the guest to panic. The patchset fixes the crashes.
The patchset also does some cleanup work: patch 3 removes the useless hv_pcichild_state, and patch 4 reverts an old patch which is not really useful (without patch 4, it would be hard to make patch 5 clean).
Patch 6 removes the use of a global mutex lock, and enables async-probing to allow concurrent device probing for faster boot.
In v2, I dropped the "debug code" before the real patch body to avoid confusion; I fixed some minor issues pointed out by Michael Kelely:
fixed "goto release_state_lock" in patch 5, and improved the commit log of patch 6; I added Wei Hu's Acked-by to patch 4. I added the cc:stable tag to all the 6 patches.
v2 is based on v6.3-rc5.
I have been testing the patchset with
https://lwn.net/ml/linux-kernel/20230316091540.494366-1-alexander.stein%40ew.tq-group.com/
for several days, and no panic/hang is observed (earlier the kernel would panic/hang within 1 day in my long haul testing).
The patchset is also availsble in my github branch:
https://github.com/dcui/tdx/commits/decui/vpci/v6.3-rc5-v2
v1 can be found here:
https://lwn.net/ml/linux-kernel/20230328045122.25850-1-decui%40microsoft.com/
Please review. Thanks!
Dexuan Cui (6):
PCI: hv: Fix a race condition bug in hv_pci_query_relations()
PCI: hv: Fix a race condition in hv_irq_unmask() that can cause panic
PCI: hv: Remove the useless hv_pcichild_state from struct hv_pci_dev Revert "PCI: hv: Fix a timing issue which causes kdump to fail
occasionally"
PCI: hv: Add a per-bus mutex state_lock
PCI: hv: Use async probing to reduce boot time
drivers/pci/controller/pci-hyperv.c | 145 +++++++++++++++++----------- 1 file changed, 86 insertions(+), 59 deletions(-)
Signature · 1 line
2.25.1
Dexuan Cui
maintainerPATCH v2 1/6 PCI: hv: Fix a race condition bug in hv_pci_query_relations()
Fix the longstanding race between hv_pci_query_relations() and survey_child_resources() by flushing the workqueue before we exit from hv_pci_query_relations().
---
v2:
Removed the "debug code".
No change to the patch body.
Added Cc:stable
drivers/pci/controller/pci-hyperv.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/pci/controller/pci-hyperv.c +13 -0
@@ -3308,6 +3308,19 @@ static int hv_pci_query_relations(struct hv_device *hdev)if (!ret) ret =wait_for_response (hdev, &comp); +/* +* In the case of fast device addition/removal, it's possible that +* vmbus_sendpacket() or wait_for_response() returns -ENODEV but we +* already got a PCI_BUS_RELATIONS* message from the host and the +* channel callback already scheduled a work to hbus->wq, which can be +* running survey_child_resources() -> complete(&hbus->survey_event), +* even after hv_pci_query_relations() exits and the stack variable +* 'comp' is no longer valid. This can cause a strange hang issue +* or sometimes a page fault. Flush hbus->wq before we exit from +* hv_pci_query_relations() to avoid the issues. +*/ +flush_workqueue (hbus->wq); +return ret; }
Signature · 1 line
2.25.1
RE: [PATCH v2 1/6] PCI: hv: Fix a race condition bug in hv_pci_query_relations()
From: Dexuan Cui <[email protected]> Sent: Monday, April 3, 2023 7:06 PM
Quoted 44 lines
Fix the longstanding race between
hv_pci_query_relations()andsurvey_child_resources()by flushing the workqueue before we exit fromhv_pci_query_relations().Fixes 4daace0d8ce8 ("PCI: hv: Add paravirtual PCI front-end for Microsoft Hyper-VVMs")
Signed-off-by Dexuan Cui <[email protected]>---
v2:
Removed the "debug code".
No change to the patch body.
Added Cc:stabledrivers/pci/controller/pci-hyperv.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)drivers/pci/controller/pci-hyperv.c +13
-1@@ -3308,6 +3308,19 @@ static int hv_pci_query_relations(struct hv_device *hdev)if (!ret) ret =wait_for_response (hdev, &comp); +/* +* In the case of fast device addition/removal, it's possible that +* vmbus_sendpacket() or wait_for_response() returns -ENODEV but we +* already got a PCI_BUS_RELATIONS* message from the host and the +* channel callback already scheduled a work to hbus->wq, which can be +* running survey_child_resources() -> complete(&hbus->survey_event), +* even after hv_pci_query_relations() exits and the stack variable +* 'comp' is no longer valid. This can cause a strange hang issue +* or sometimes a page fault. Flush hbus->wq before we exit from +* hv_pci_query_relations() to avoid the issues. +*/ +flush_workqueue (hbus->wq); +return ret; } --
Quoted 1 line
2.25.1
Dexuan Cui
maintainerPATCH v2 2/6 PCI: hv: Fix a race condition in hv_irq_unmask() that can cause panic
In reply to Dexuan CuiWhen the host tries to remove a PCI device, the host first sends a PCI_EJECT message to the guest, and the guest is supposed to gracefully remove the PCI device and send a PCI_EJECTION_COMPLETE message to the host; the host then sends a VMBus message CHANNELMSG_RESCIND_CHANNELOFFER to the guest (when the guest receives this message, the device is already unassigned from the guest) and the guest can do some final cleanup work; if the guest fails to respond to the PCI_EJECT message within one minute, the host sends the VMBus message CHANNELMSG_RESCIND_CHANNELOFFER and removes the PCI device forcibly.
In the case of fast device addition/removal, it's possible that the PCI device driver is still configuring MSI-X interrupts when the guest receives the PCI_EJECT message; the channel callback calls hv_pci_eject_device(), which sets hpdev->state to hv_pcichild_ejecting, and schedules a work hv_eject_device_work(); if the PCI device driver is calling pci_alloc_irq_vectors() -> ... -> hv_compose_msi_msg(), we can break the while loop in hv_compose_msi_msg() due to the updated hpdev->state, and leave data->chip_data with its default value of NULL; later, when the PCI device driver calls request_irq() -> ... -> hv_irq_unmask(), the guest crashes in hv_arch_irq_unmask() due to data->chip_data being NULL.
Fix the issue by not testing hpdev->state in the while loop: when the guest receives PCI_EJECT, the device is still assigned to the guest, and the guest has one minute to finish the device removal gracefully. We don't really need to (and we should not) test hpdev->state in the loop.
---
v2:
Removed the "debug code".
No change to the patch body.
Added Cc:stable
drivers/pci/controller/pci-hyperv.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/pci/controller/pci-hyperv.c +5 -6
@@ -643,6 +643,11 @@ static void hv_arch_irq_unmask(struct irq_data *data) pbus = pdev->bus; hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); int_desc = data->chip_data; +if (!int_desc) { +dev_warn (&hbus->hdev->device,"%s() can not unmask irq %u\n" , + __func__, data->irq); +return ; + }spin_lock_irqsave (&hbus->retarget_msi_interrupt_lock, flags);
@@ -1911,12 +1916,6 @@ static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)hv_pci_onchannelcallback (hbus);spin_unlock_irqrestore (&channel->sched_lock, flags); -if (hpdev->state == hv_pcichild_ejecting) { -dev_err_once (&hbus->hdev->device, -"the device is being ejected\n" ); - goto enable_tasklet; - } -udelay (100 ); }
Signature · 1 line
2.25.1
RE: [PATCH v2 2/6] PCI: hv: Fix a race condition in hv_irq_unmask() that can cause panic
From: Dexuan Cui <[email protected]> Sent: Monday, April 3, 2023 7:06 PM
Quoted 71 lines
When the host tries to remove a PCI device, the host first sends a
PCI_EJECTmessage to the guest, and the guest is supposed to gracefully remove the PCI device and send aPCI_EJECTION_COMPLETEmessage to the host; the host then sends a VMBus messageCHANNELMSG_RESCIND_CHANNELOFFERto the guest (when the guest receives this message, the device is already unassigned from the guest) and the guest can do some final cleanup work; if the guest fails to respond to thePCI_EJECTmessage within one minute, the host sends the VMBus messageCHANNELMSG_RESCIND_CHANNELOFFERand removes the PCI device forcibly.In the case of fast device addition/removal, it's possible that the PCI device driver is still configuring MSI-X interrupts when the guest receives the
PCI_EJECTmessage; the channel callback callshv_pci_eject_device(), which setshpdev->statetohv_pcichild_ejecting, and schedules a workhv_eject_device_work(); if the PCI device driver is callingpci_alloc_irq_vectors()-> ... ->hv_compose_msi_msg(), we can break the while loop inhv_compose_msi_msg()due to the updatedhpdev->state, and leavedata->chip_datawith its default value of NULL; later, when the PCI device driver callsrequest_irq()-> ... ->hv_irq_unmask(), the guest crashes inhv_arch_irq_unmask()due todata->chip_databeing NULL.Fix the issue by not testing
hpdev->statein the while loop: when the guest receivesPCI_EJECT, the device is still assigned to the guest, and the guest has one minute to finish the device removal gracefully. We don't really need to (and we should not) testhpdev->statein the loop.Fixes de0aa7b2f97d ("PCI: hv: Fix 2 hang issues in hv_compose_msi_msg()")Signed-off-by Dexuan Cui <[email protected]>---
v2:
Removed the "debug code".
No change to the patch body.
Added Cc:stabledrivers/pci/controller/pci-hyperv.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)drivers/pci/controller/pci-hyperv.c +5
-7@@ -643,6 +643,11 @@ static void hv_arch_irq_unmask(struct irq_data *data) pbus = pdev->bus; hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata); int_desc = data->chip_data; +if (!int_desc) { +dev_warn (&hbus->hdev->device,"%s() can not unmask irq %u\n" , + __func__, data->irq); +return ; + }spin_lock_irqsave (&hbus->retarget_msi_interrupt_lock, flags);@@ -1911,12 +1916,6 @@ static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)hv_pci_onchannelcallback (hbus );spin_unlock_irqrestore (&channel->sched_lock, flags); -if (hpdev->state == hv_pcichild_ejecting) { -dev_err_once (&hbus->hdev->device, -"the device is being ejected\n" ); - goto enable_tasklet; - } -udelay (100 ); } --
Quoted 1 line
2.25.1
Dexuan Cui
maintainerPATCH v2 3/6 PCI: hv: Remove the useless hv_pcichild_state from struct hv_pci_dev
In reply to Dexuan CuiThe hpdev->state is never really useful. The only use in hv_pci_eject_device() and hv_eject_device_work() is not really necessary.
--- drivers/pci/controller/pci-hyperv.c | 12 ------------ 1 file changed, 12 deletions(-)
v2:
No change to the patch body.
Added Cc:stable
drivers/pci/controller/pci-hyperv.c +0 -12
@@ -553,19 +553,10 @@ struct hv_dr_state {struct hv_pcidev_description func[]; }; -enum hv_pcichild_state { - hv_pcichild_init =0 , - hv_pcichild_requirements, - hv_pcichild_resourced, - hv_pcichild_ejecting, - hv_pcichild_maximum -}; -struct hv_pci_dev {/* List protected by pci_rescan_remove_lock */ struct list_head list_entry;refcount_t refs; -enum hv_pcichild_state state;struct pci_slot *pci_slot;struct hv_pcidev_description desc;bool reported_missing;
@@ -2750,8 +2741,6 @@ static void hv_eject_device_work(struct work_struct *work) hpdev = container_of(work, struct hv_pci_dev, wrk); hbus = hpdev->hbus; -WARN_ON (hpdev->state != hv_pcichild_ejecting); -/* * Ejection can come before or after the PCI bus has been set up, so * attempt to find it and tear down the bus state, if it exists. This
@@ -2808,7 +2797,6 @@ static void hv_pci_eject_device(struct hv_pci_dev *hpdev)return ; } - hpdev->state = hv_pcichild_ejecting;get_pcichild (hpdev);INIT_WORK (&hpdev->wrk, hv_eject_device_work);queue_work (hbus->wq, &hpdev->wrk);
Signature · 1 line
2.25.1
RE: [PATCH v2 3/6] PCI: hv: Remove the useless hv_pcichild_state from struct hv_pci_dev
From: Dexuan Cui <[email protected]> Sent: Monday, April 3, 2023 7:06 PM
Quoted 56 lines
The
hpdev->stateis never really useful. The only use inhv_pci_eject_device()andhv_eject_device_work()is not really necessary.Signed-off-by Dexuan Cui <[email protected]>--- drivers/pci/controller/pci-hyperv.c | 12 ------------ 1 file changed, 12 deletions(-)v2:
No change to the patch body.
Added Cc:stabledrivers/pci/controller/pci-hyperv.c +0
-13@@ -553,19 +553,10 @@ struct hv_dr_state {struct hv_pcidev_description func[]; }; -enum hv_pcichild_state { - hv_pcichild_init =0 , - hv_pcichild_requirements, - hv_pcichild_resourced, - hv_pcichild_ejecting, - hv_pcichild_maximum -}; -struct hv_pci_dev {/* List protected by pci_rescan_remove_lock */ struct list_head list_entry;refcount_t refs; -enum hv_pcichild_state state;struct pci_slot *pci_slot;struct hv_pcidev_description desc;bool reported_missing;@@ -2750,8 +2741,6 @@ static void hv_eject_device_work(struct work_struct *work) hpdev = container_of(work, struct hv_pci_dev, wrk); hbus = hpdev->hbus; -WARN_ON (hpdev->state != hv_pcichild_ejecting); -/* * Ejection can come before or after the PCI bus has been set up, so * attempt to find it and tear down the bus state, if it exists. This @@ -2808,7 +2797,6 @@ static void hv_pci_eject_device(struct hv_pci_dev *hpdev)return ; } - hpdev->state = hv_pcichild_ejecting;get_pcichild (hpdev);INIT_WORK (&hpdev->wrk, hv_eject_device_work);queue_work (hbus->wq, &hpdev->wrk); --
Quoted 1 line
2.25.1
Dexuan Cui
maintainerPATCH v2 4/6 Revert "PCI: hv: Fix a timing issue which causes kdump to fail occasionally"
In reply to Dexuan CuiThis reverts commit d6af2ed29c7c1c311b96dac989dcb991e90ee195.
The statement "the hv_pci_bus_exit() call releases structures of all its child devices" in commit d6af2ed29c7c is not true: in the path hv_pci_probe() -> hv_pci_enter_d0() -> hv_pci_bus_exit(hdev, true): the parameter "keep_devs" is true, so hv_pci_bus_exit() does *not* release the child "struct hv_pci_dev *hpdev" that is created earlier in pci_devices_present_work() -> new_pcichild_device().
The commit d6af2ed29c7c was originally made in July 2020 for RHEL 7.7, where the old version of hv_pci_bus_exit() was used; when the commit was rebased and merged into the upstream, people didn't notice that it's not really necessary. The commit itself doesn't cause any issue, but it makes hv_pci_probe() more complicated. Revert it to facilitate some upcoming changes to hv_pci_probe().
---
v2:
No change to the patch body.
Added Wei Hu's Acked-by.
Added Cc:stable
drivers/pci/controller/pci-hyperv.c | 71 ++++++++++++++--------------- 1 file changed, 34 insertions(+), 37 deletions(-)
drivers/pci/controller/pci-hyperv.c +34 -37
@@ -3225,8 +3225,10 @@ static int hv_pci_enter_d0(struct hv_device *hdev)struct pci_bus_d0_entry *d0_entry;struct hv_pci_compl comp_pkt;struct pci_packet *pkt; +bool retry = true;int ret; +enter_d0_retry:/* * Tell the host that the bus is ready to use, and moved into the * powered-on state. This includes telling the host which region
@@ -3253,6 +3255,38 @@ static int hv_pci_enter_d0(struct hv_device *hdev)if (ret) goto exit; +/* +* In certain case (Kdump) the pci device of interest was +* not cleanly shut down and resource is still held on host +* side, the host could return invalid device status. +* We need to explicitly request host to release the resource +* and try to enter D0 again. +*/ +if (comp_pkt.completion_status <0 && retry) { + retry = false; + +dev_err (&hdev->device,"Retrying D0 Entry\n" ); + +/* +* Hv_pci_bus_exit() calls hv_send_resource_released() +* to free up resources of its child devices. +* In the kdump kernel we need to set the +* wslot_res_allocated to 255 so it scans all child +* devices to release resources allocated in the +* normal kernel before panic happened. +*/ + hbus->wslot_res_allocated =255 ; + + ret =hv_pci_bus_exit (hdev, true); + +if (ret ==0 ) { +kfree (pkt); + goto enter_d0_retry; + } +dev_err (&hdev->device, +"Retrying D0 failed with ret %d\n" , ret); + } +if (comp_pkt.completion_status <0 ) { dev_err(&hdev->device, "PCI Pass-through VSP failed D0 Entry with status %x\n",
@@ -3493,7 +3527,6 @@ static int hv_pci_probe(struct hv_device *hdev,struct hv_pcibus_device *hbus;u16 dom_req, dom;char *name; -bool enter_d0_retry = true;int ret;/*
@@ -3633,47 +3666,11 @@ static int hv_pci_probe(struct hv_device *hdev,if (ret) goto free_fwnode; -retry: ret =hv_pci_query_relations (hdev);if (ret) goto free_irq_domain; ret =hv_pci_enter_d0 (hdev); -/* -* In certain case (Kdump) the pci device of interest was -* not cleanly shut down and resource is still held on host -* side, the host could return invalid device status. -* We need to explicitly request host to release the resource -* and try to enter D0 again. -* Since the hv_pci_bus_exit() call releases structures -* of all its child devices, we need to start the retry from -* hv_pci_query_relations() call, requesting host to send -* the synchronous child device relations message before this -* information is needed in hv_send_resources_allocated() -* call later. -*/ -if (ret == -EPROTO && enter_d0_retry) { - enter_d0_retry = false; - -dev_err (&hdev->device,"Retrying D0 Entry\n" ); - -/* -* Hv_pci_bus_exit() calls hv_send_resources_released() -* to free up resources of its child devices. -* In the kdump kernel we need to set the -* wslot_res_allocated to 255 so it scans all child -* devices to release resources allocated in the -* normal kernel before panic happened. -*/ - hbus->wslot_res_allocated =255 ; - ret =hv_pci_bus_exit (hdev, true); - -if (ret ==0 ) - goto retry; - -dev_err (&hdev->device, -"Retrying D0 failed with ret %d\n" , ret); - }if (ret) goto free_irq_domain;
Signature · 1 line
2.25.1
RE: [PATCH v2 4/6] Revert "PCI: hv: Fix a timing issue which causes kdump to fail occasionally"
From: Dexuan Cui <[email protected]> Sent: Monday, April 3, 2023 7:06 PM
Quoted 22 lines
This reverts commit
d6af2ed29c7c1c311b96dac989dcb991e90ee195.The statement "the
hv_pci_bus_exit()call releases structures of all its child devices" in commitd6af2ed29c7cis not true: in the pathhv_pci_probe()->hv_pci_enter_d0()->hv_pci_bus_exit(hdev, true): the parameter "keep_devs" is true, sohv_pci_bus_exit()does *not* release the child "structhv_pci_dev*hpdev" that is created earlier inpci_devices_present_work()->new_pcichild_device().The commit
d6af2ed29c7cwas originally made in July 2020 for RHEL 7.7, where the old version ofhv_pci_bus_exit()was used; when the commit was rebased and merged into the upstream, people didn't notice that it's not really necessary. The commit itself doesn't cause any issue, but it makeshv_pci_probe()more complicated. Revert it to facilitate some upcoming changes tohv_pci_probe().---
Quoted 8 lines
v2:
No change to the patch body.
Added Wei Hu's Acked-by.
Added Cc:stabledrivers/pci/controller/pci-hyperv.c | 71 ++++++++++++++--------------- 1 file changed, 34 insertions(+), 37 deletions(-)
Dexuan Cui
maintainerPATCH v2 5/6 PCI: hv: Add a per-bus mutex state_lock
In reply to Dexuan CuiIn 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.
---
v2:
Removed the "debug code".
Fixed the "goto out" in hv_pci_resume() [Michael Kelley] Added Cc:stable
drivers/pci/controller/pci-hyperv.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)
drivers/pci/controller/pci-hyperv.c +26 -3
@@ -489,7 +489,10 @@ struct hv_pcibus_device {struct fwnode_handle *fwnode;/* Protocol version negotiated with the host */ enum pci_protocol_version_t protocol_version; + +struct mutex state_lock;enum hv_pcibus_state state; +struct hv_device *hdev;resource_size_t low_mmio_space;resource_size_t high_mmio_space;
@@ -2512,6 +2515,8 @@ static void pci_devices_present_work(struct work_struct *work)if (!dr)return ; +mutex_lock (&hbus->state_lock); +/* First, mark all existing children as reported missing. */ spin_lock_irqsave (&hbus->device_list_lock, flags);list_for_each_entry (hpdev, &hbus->children, list_entry) {
@@ -2593,6 +2598,8 @@ static void pci_devices_present_work(struct work_struct *work)break ; } +mutex_unlock (&hbus->state_lock); +kfree (dr); }
@@ -2741,6 +2748,8 @@ static void hv_eject_device_work(struct work_struct *work) hpdev = container_of(work, struct hv_pci_dev, wrk); hbus = hpdev->hbus; +mutex_lock (&hbus->state_lock); +/* * Ejection can come before or after the PCI bus has been set up, so * 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)put_pcichild (hpdev);put_pcichild (hpdev);/* hpdev has been freed. Do not use it any more. */ + +mutex_unlock (&hbus->state_lock); }/**
@@ -3562,6 +3573,7 @@ static int hv_pci_probe(struct hv_device *hdev,return -ENOMEM ; hbus->bridge = bridge; +mutex_init (&hbus->state_lock); hbus->state = hv_pcibus_init; hbus->wslot_res_allocated =-1 ;
@@ -3670,9 +3682,11 @@ static int hv_pci_probe(struct hv_device *hdev,if (ret) goto free_irq_domain; +mutex_lock (&hbus->state_lock); + ret =hv_pci_enter_d0 (hdev);if (ret) - goto free_irq_domain; + goto release_state_lock; ret =hv_pci_allocate_bridge_windows (hbus);if (ret)
@@ -3690,12 +3704,15 @@ static int hv_pci_probe(struct hv_device *hdev,if (ret) goto free_windows; +mutex_unlock (&hbus->state_lock);return 0 ; free_windows:hv_pci_free_bridge_windows (hbus); exit_d0: (void )hv_pci_bus_exit (hdev, true); +release_state_lock: +mutex_unlock (&hbus->state_lock); free_irq_domain:irq_domain_remove (hbus->irq_domain); free_fwnode:
@@ -3945,20 +3962,26 @@ static int hv_pci_resume(struct hv_device *hdev)if (ret) goto out; +mutex_lock (&hbus->state_lock); + ret =hv_pci_enter_d0 (hdev);if (ret) - goto out; + goto release_state_lock; ret =hv_send_resources_allocated (hdev);if (ret) - goto out; + goto release_state_lock;prepopulate_bars (hbus);hv_pci_restore_msi_state (hbus); hbus->state = hv_pcibus_installed; +mutex_unlock (&hbus->state_lock);return 0 ; + +release_state_lock: +mutex_unlock (&hbus->state_lock); out:vmbus_close (hdev->channel);return ret;
Signature · 1 line
2.25.1
RE: [PATCH v2 5/6] PCI: hv: Add a per-bus mutex state_lock
From: Dexuan Cui <[email protected]> Sent: Monday, April 3, 2023 7:06 PM
Quoted 159 lines
In the case of fast device addition/removal, it's possible that
hv_eject_device_work()can start to run beforecreate_root_hv_pci_bus()starts to run; as a result, thepci_get_domain_bus_and_slot()inhv_eject_device_work()can return a 'pdev' of NULL, andhv_eject_device_work()can remove the 'hpdev', and immediately send a messagePCI_EJECTION_COMPLETEto 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 inhv_pci_enter_d0(): whenhv_eject_device_work()starts to run, it's able to find the 'pdev' and callpci_stop_and_remove_bus_device(pdev): if the PCI device driver has loaded, the PCI device driver'sprobe()function is already called increate_root_hv_pci_bus()->pci_bus_add_devices(), and nowhv_eject_device_work()->pci_stop_and_remove_bus_device()is able to call the PCI device driver'sremove()function and remove the device reliably; if the PCI device driver hasn't loaded yet, the function callhv_eject_device_work()->pci_stop_and_remove_bus_device()is able to remove the PCI device reliably and the PCI device driver'sprobe()function won't be called; if the PCI device driver'sprobe()is already running (e.g., systemd-udev is loading the PCI device driver), it must be holding the per-device lock, and after theprobe()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 4daace0d8ce8 ("PCI: hv: Add paravirtual PCI front-end for Microsoft Hyper-V VMs")Signed-off-by Dexuan Cui <[email protected]>---
v2:
Removed the "debug code".
Fixed the "goto out" inhv_pci_resume()[Michael Kelley] Added Cc:stabledrivers/pci/controller/pci-hyperv.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)drivers/pci/controller/pci-hyperv.c +26
-4@@ -489,7 +489,10 @@ struct hv_pcibus_device {struct fwnode_handle *fwnode;/* Protocol version negotiated with the host */ enum pci_protocol_version_t protocol_version; + +struct mutex state_lock;enum hv_pcibus_state state; +struct hv_device *hdev;resource_size_t low_mmio_space;resource_size_t high_mmio_space;@@ -2512,6 +2515,8 @@ static void pci_devices_present_work(struct work_struct *work)if (!dr)return ; +mutex_lock (&hbus->state_lock); +/* First, mark all existing children as reported missing. */ spin_lock_irqsave (&hbus->device_list_lock, flags);list_for_each_entry (hpdev, &hbus->children, list_entry) {@@ -2593,6 +2598,8 @@ static void pci_devices_present_work(struct work_struct *work)break ; } +mutex_unlock (&hbus->state_lock); +kfree (dr); }@@ -2741,6 +2748,8 @@ static void hv_eject_device_work(struct work_struct *work) hpdev = container_of(work, struct hv_pci_dev, wrk); hbus = hpdev->hbus; +mutex_lock (&hbus->state_lock); +/* * Ejection can come before or after the PCI bus has been set up, so * 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)put_pcichild (hpdev);put_pcichild (hpdev);/* hpdev has been freed. Do not use it any more. */ + +mutex_unlock (&hbus->state_lock); }/** @@ -3562,6 +3573,7 @@ static int hv_pci_probe(struct hv_device *hdev,return -ENOMEM ; hbus->bridge = bridge; +mutex_init (&hbus->state_lock); hbus->state = hv_pcibus_init; hbus->wslot_res_allocated =-1 ;@@ -3670,9 +3682,11 @@ static int hv_pci_probe(struct hv_device *hdev,if (ret) goto free_irq_domain; +mutex_lock (&hbus->state_lock); + ret =hv_pci_enter_d0 (hdev);if (ret) - goto free_irq_domain; + goto release_state_lock; ret =hv_pci_allocate_bridge_windows (hbus);if (ret)@@ -3690,12 +3704,15 @@ static int hv_pci_probe(struct hv_device *hdev,if (ret) goto free_windows; +mutex_unlock (&hbus->state_lock);return 0 ; free_windows:hv_pci_free_bridge_windows (hbus); exit_d0: (void )hv_pci_bus_exit (hdev, true); +release_state_lock: +mutex_unlock (&hbus->state_lock); free_irq_domain:irq_domain_remove (hbus->irq_domain); free_fwnode:@@ -3945,20 +3962,26 @@ static int hv_pci_resume(struct hv_device *hdev)if (ret) goto out; +mutex_lock (&hbus->state_lock); + ret =hv_pci_enter_d0 (hdev);if (ret) - goto out; + goto release_state_lock; ret =hv_send_resources_allocated (hdev);if (ret) - goto out; + goto release_state_lock;prepopulate_bars (hbus);hv_pci_restore_msi_state (hbus); hbus->state = hv_pcibus_installed; +mutex_unlock (&hbus->state_lock);return 0 ; + +release_state_lock: +mutex_unlock (&hbus->state_lock); out:vmbus_close (hdev->channel);return ret; --
Quoted 1 line
2.25.1
Dexuan Cui
maintainerPATCH v2 6/6 PCI: hv: Use async probing to reduce boot time
In reply to Dexuan CuiCommit 414428c5da1c ("PCI: hv: Lock PCI bus on device eject") added pci_lock_rescan_remove() and pci_unlock_rescan_remove() in create_root_hv_pci_bus() and in hv_eject_device_work() to address the race between create_root_hv_pci_bus() and hv_eject_device_work(), but it turns that grubing the pci_rescan_remove_lock mutex is not enough:
refer to the earlier fix "PCI: hv: Add a per-bus mutex state_lock".
Now with hbus->state_lock and other fixes, the race is resolved, so remove pci_{lock,unlock}_rescan_remove() in create_root_hv_pci_bus():
this removes the serialization in hv_pci_probe() and hence allows async-probing (PROBE_PREFER_ASYNCHRONOUS) to work.
Add the async-probing flag to hv_pci_drv.
pci_{lock,unlock}_rescan_remove() in hv_eject_device_work() and in hv_pci_remove() are still kept: according to the comment before drivers/pci/probe.c: static DEFINE_MUTEX(pci_rescan_remove_lock), "PCI device removal routines should always be executed under this mutex".
---
v2:
No change to the patch body.
Improved the commit message [Michael Kelley]
Added Cc:stable
drivers/pci/controller/pci-hyperv.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/pci/controller/pci-hyperv.c +9 -2
@@ -2312,12 +2312,16 @@ static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)if (error)return error; -pci_lock_rescan_remove (); +/* +* pci_lock_rescan_remove() and pci_unlock_rescan_remove() are +* unnecessary here, because we hold the hbus->state_lock, meaning +* hv_eject_device_work() and pci_devices_present_work() can't race +* with create_root_hv_pci_bus(). +*/ hv_pci_assign_numa_node (hbus);pci_bus_assign_resources (bridge->bus);hv_pci_assign_slots (hbus);pci_bus_add_devices (bridge->bus); -pci_unlock_rescan_remove (); hbus->state = hv_pcibus_installed;return 0 ; }
@@ -4003,6 +4007,9 @@ static struct hv_driver hv_pci_drv = { .remove = hv_pci_remove, .suspend = hv_pci_suspend, .resume = hv_pci_resume, + .driver = { + .probe_type = PROBE_PREFER_ASYNCHRONOUS, + }, }; static void __exit exit_hv_pci_drv(void)
Signature · 1 line
2.25.1
RE: [PATCH v2 6/6] PCI: hv: Use async probing to reduce boot time
From: Dexuan Cui <[email protected]> Sent: Monday, April 3, 2023 7:06 PM
Quoted 67 lines
Commit
414428c5da1c("PCI: hv: Lock PCI bus on device eject") addedpci_lock_rescan_remove()andpci_unlock_rescan_remove()increate_root_hv_pci_bus()and inhv_eject_device_work()to address the race betweencreate_root_hv_pci_bus()andhv_eject_device_work(), but it turns that grubing thepci_rescan_remove_lockmutex is not enough:
refer to the earlier fix "PCI: hv: Add a per-bus mutexstate_lock".Now with
hbus->state_lockand other fixes, the race is resolved, so remove pci_{lock,unlock}_rescan_remove() increate_root_hv_pci_bus():
this removes the serialization inhv_pci_probe()and hence allows async-probing (PROBE_PREFER_ASYNCHRONOUS) to work.Add the async-probing flag to
hv_pci_drv.pci_{lock,unlock}_rescan_remove() in
hv_eject_device_work()and inhv_pci_remove()are still kept: according to the comment beforedrivers/pci/probe.c: staticDEFINE_MUTEX(pci_rescan_remove_lock), "PCI device removal routines should always be executed under this mutex".Signed-off-by Dexuan Cui <[email protected]>---
v2:
No change to the patch body.
Improved the commit message [Michael Kelley]
Added Cc:stabledrivers/pci/controller/pci-hyperv.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)drivers/pci/controller/pci-hyperv.c +6
-2@@ -2312,12 +2312,16 @@ static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus) if (error) return error; -pci_lock_rescan_remove (); + /* + * pci_lock_rescan_remove() and pci_unlock_rescan_remove() are + * unnecessary here, because we hold the hbus->state_lock, meaning + * hv_eject_device_work() and pci_devices_present_work() can't race + * with create_root_hv_pci_bus(). + */ hv_pci_assign_numa_node(hbus); pci_bus_assign_resources(bridge->bus); hv_pci_assign_slots(hbus); pci_bus_add_devices(bridge->bus); -pci_unlock_rescan_remove (); hbus->state = hv_pcibus_installed; return 0;}
drivers/pci/controller/pci-hyperv.c +3
-1@@ -4003,6 +4007,9 @@ static struct hv_driver hv_pci_drv = { .remove = hv_pci_remove, .suspend = hv_pci_suspend, .resume = hv_pci_resume, + .driver = { + .probe_type = PROBE_PREFER_ASYNCHRONOUS, + }, }; static void __exit exit_hv_pci_drv(void) --
Quoted 1 line
2.25.1
RE: [PATCH v2 6/6] PCI: hv: Use async probing to reduce boot time
From: Michael Kelley (LINUX) <[email protected]> Sent: Friday, April 7, 2023 9:12 AM
Quoted 8 lines
From: Dexuan Cui <[email protected]> Sent: Monday, April 3, 2023 7:06 PM
Quoted 6 lines
Commit
414428c5da1c("PCI: hv: Lock PCI bus on device eject") addedpci_lock_rescan_remove()andpci_unlock_rescan_remove()increate_root_hv_pci_bus()and inhv_eject_device_work()to address the race betweencreate_root_hv_pci_bus()andhv_eject_device_work(), but it turns that grubing thepci_rescan_remove_lockmutex is not enough:
There's some kind of spelling error or typo above. Should "grubing" be "grabbing"? Or did you intend something else?
Michael
Quoted 16 lines
Quoted 16 lines
refer to the earlier fix "PCI: hv: Add a per-bus mutex
state_lock".Now with
hbus->state_lockand other fixes, the race is resolved, so remove pci_{lock,unlock}_rescan_remove() increate_root_hv_pci_bus():
this removes the serialization inhv_pci_probe()and hence allows async-probing (PROBE_PREFER_ASYNCHRONOUS) to work.Add the async-probing flag to
hv_pci_drv.pci_{lock,unlock}_rescan_remove() in
hv_eject_device_work()and inhv_pci_remove()are still kept: according to the comment beforedrivers/pci/probe.c: staticDEFINE_MUTEX(pci_rescan_remove_lock), "PCI device removal routines should always be executed under this mutex".Signed-off-by Dexuan Cui <[email protected]>
Dexuan Cui
maintainerRE: [PATCH v2 6/6] PCI: hv: Use async probing to reduce boot time
Quoted 9 lines
From: Michael Kelley (LINUX) <[email protected]>
Sent: Friday, April 7, 2023 9:15 AM
...Quoted 5 lines
Quoted 5 lines
Commit
414428c5da1c("PCI: hv: Lock PCI bus on device eject") addedpci_lock_rescan_remove()andpci_unlock_rescan_remove()increate_root_hv_pci_bus()and inhv_eject_device_work()to address the race betweencreate_root_hv_pci_bus()andhv_eject_device_work(), but it turns that grubing thepci_rescan_remove_lockmutex is not enough:
Quoted 4 lines
There's some kind of spelling error or typo above. Should "grubing" be "grabbing"? Or did you intend something else?
Michael
Sorry, it's a typo. The "grubing" should be "grabbing".
I suppose the PCI maintainers can help fix this. Let me know if v3 is needed.
Long Li
Reviewed-byRE: [PATCH v2 6/6] PCI: hv: Use async probing to reduce boot time
Quoted 13 lines
Subject: RE: [PATCH v2 6/6] PCI: hv: Use async probing to reduce boot time
Quoted 9 lines
From: Michael Kelley (LINUX) <[email protected]>
Sent: Friday, April 7, 2023 9:15 AM
...Quoted 6 lines
Quoted 6 lines
Commit
414428c5da1c("PCI: hv: Lock PCI bus on device eject") addedpci_lock_rescan_remove()andpci_unlock_rescan_remove()increate_root_hv_pci_bus()and inhv_eject_device_work()to address the race betweencreate_root_hv_pci_bus()andhv_eject_device_work(), but it turns that grubing the
pci_rescan_remove_lockmutex is not enough:Quoted 1 line
Quoted 7 lines
Quoted 4 lines
There's some kind of spelling error or typo above. Should "grubing" be "grabbing"? Or did you intend something else?
Michael
Sorry, it's a typo. The "grubing" should be "grabbing".
I suppose the PCI maintainers can help fix this. Let me know if v3 is needed.
Other than the typo,