PCI: pciehp: Report surprise removal during safe removal
v2 cover PCI: pciehp: Report surprise removal during safe removal
If a safe removal is already in progress when the device is surprise removed, pciehp cannot report the disconnect [1]. The removal blocks waiting on a device interrupt or status read, and pciehp's IRQ thread is single-threaded and is itself executing that removal, so it never runs again to report the device gone. The removal hangs indefinitely.
pciehp_isr() does run while the IRQ thread is blocked, but pciehp_ist() must ignore link and presence changes caused by Secondary Bus Reset or DPC, and telling those apart takes seconds which cannot be spent in hardirq.
Patch 4 does not do that work in hardirq. pciehp_isr() only checks whether the card is present or the link is active, and defers the rest to a work item in process context. The work item does not need to know why the link changed, only whether the card is gone: it waits for DPC recovery or a Secondary Bus Reset in progress to complete, then checks again under reset_lock, and if the card is absent and the link is down marks the devices below disconnected, which schedules the driver's disconnect work from patch 1. pciehp_ist() is unchanged and remains the only consumer of the one-shot flags PCI_DPC_RECOVERED and PCI_LINK_CHANGED, which tell it whether a link change can be ignored.
Patches:
1/5 Michael's "PCI: Report surprise removal event" from his RFC v5,
which adds disconnect_work_enable and pdev->disconnect_work. Changed to use disable_work_sync() on teardown.
2/5 Add pci_hp_wait_link_change(), which awaits a Secondary Bus Reset
in progress without consuming PCI_LINK_CHANGED.
3/5 Add pci_dpc_wait_recovery(), which awaits DPC recovery without
consuming PCI_DPC_RECOVERED.
4/5 The pciehp change. Adds disconnect_work to struct controller,
scheduled from pciehp_isr() on PDC or DLLSC when neither Presence Detect State nor Data Link Layer Link Active indicates a card.
5/5 A POC driver for the QEMU edu device that blocks in remove()
waiting for an interrupt, standing in for del_gendisk() stuck in blk_mq_freeze_queue_wait(). Not for merge -- included so the hang can be reproduced.
Changes since RFC v1 [2]:
- 1/5: Use disable_work_sync() instead of cancel_work_sync() in pci_clear_disconnect_work(), so that a racing schedule_work() cannot queue the work after remove() has returned. (Sashiko)
- 2/5, 3/5: New.
- 4/5: Drop schedule_notification_work(); pci_dev_set_disconnected() schedules the driver's disconnect work for all callers again. (Michael)
- 4/5: Don't consume PCI_DPC_RECOVERED and PCI_LINK_CHANGED in the work item. In v1 it ran the same spurious link change test as pciehp_ist(), and whichever ran first took the flags, so pciehp_ist() could tear down a device that was only reset. (Sashiko)
- 4/5: Check presence with pciehp_card_present_or_link_active() in both pciehp_isr() and the work item, as pciehp_ist() does, so that a port with Presence Detect State hardwired to zero is not mistaken for an empty slot.
- 4/5: Return early from the work item if pciehp_ist() has already taken the pending events, and treat a read error of the presence check as "not present". (Sashiko)
- 4/5: In pciehp_isr(), check presence before dropping the runtime PM reference on the port's parent. In the work item, take a runtime PM reference and check presence under reset_lock, since a slot reset may make Presence Detect State and Link Active flap.
- 5/5: Build only with CONFIG_EDU_SRPOC, which depends on PCI; don't claim the interrupt when the device reads all ones; clear bus mastering on teardown. (Sashiko)
Testing
Reproducing this needs QEMU changes, since neither device_del nor the attention button produces a true surprise removal. A branch with both is here [3]:
- a delayed-IRQ register on the edu device (BAR0 0x30, write N ms)
- a pcie_surprise_del monitor command that drops the device and
generates PDC=1, DLLSC=1, PDS=0
Both tests use:
./qemu-system-aarch64 -machine virt,gic-version=3 -cpu cortex-a57 \
-m 512 -smp 2 -kernel Image -initrd initramfs.cpio.gz \
-device pcie-root-port,id=rp1,chassis=1,slot=1 \
-device edu,bus=rp1,id=edu0 -append "console=ttyAMA0 rdinit=/init" \
-nographic -monitor unix:/tmp/qemu-mon.sock,server,nowait and need a guest kernel with CONFIG_EDU_SRPOC=y.
Test 1 (Hang in remove() on a user thread, then surprise removal):
guest# echo 1 > /sys/bus/pci/devices/0000:01:00.0/remove &
host$ echo "pcie_surprise_del edu0" | socat - unix-connect:/tmp/qemu-mon.sock
This is the case patch 1 solves on its own: pciehp's IRQ thread is free and handles the removal.
Test 2 (Hang in remove() on pciehp's IRQ thread, then surprise removal):
guest# echo 0 > /sys/bus/pci/slots/1/power &
host$ echo "pcie_surprise_del edu0" | socat - unix-connect:/tmp/qemu-mon.sock
Without patch 4 the safe removal does not return until the delayed interrupt fires 600 s later. With it,
pciehp_isr() schedules ctrl->disconnect_work, which marks the device disconnected; the POC driver's disconnect work completes the wait and remove() proceeds.
Open questions
- Is this a viable approach?
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://gitlab.com/abhinkop/qemu/-/commits/suprise-removal
Abhin Parekadan Jose (4):
PCI: pciehp: Add pci_hp_wait_link_change() PCI/DPC: Add pci_dpc_wait_recovery()
PCI: pciehp: Report surprise removal from pciehp_isr()
misc: Add edu_srpoc surprise removal POC driver
Michael S. Tsirkin (1):
PCI: Report surprise removal event
drivers/misc/Kconfig | 11 ++ drivers/misc/Makefile | 1 + drivers/misc/edu_srpoc.c | 171 +++++++++++++++++++++++++ drivers/pci/hotplug/pci_hotplug_core.c | 21 ++- drivers/pci/hotplug/pciehp.h | 1 + drivers/pci/hotplug/pciehp_hpc.c | 67 ++++++++++ drivers/pci/pci.h | 9 ++ drivers/pci/pcie/dpc.c | 54 ++++++-- include/linux/pci.h | 46 +++++++ 9 files changed, 367 insertions(+), 14 deletions(-) create mode 100644 drivers/misc/edu_srpoc.c
base-commit: fd179f8a05be3ccae366b9b96e176b51fbe54aab --
2.51.1
RFC PATCH v2 1/5 PCI: Report surprise removal event
From: "Michael S. Tsirkin" <[email protected]>
At the moment, in case of a surprise removal, the regular remove callback is invoked, exclusively. This works well, because mostly, the cleanup would be the same.
However, there's a race: imagine device removal was initiated by a user action, such as driver unbind, and it in turn initiated some cleanup and is now waiting for an interrupt from the device. If the device is now surprise-removed, that never arrives and the remove callback hangs forever.
For example, this was reported for virtio-blk:
1. the graceful removal is ongoing in the remove() callback, where disk deletion del_gendisk() is ongoing, which waits for the requests to complete,
2. Now few requests are yet to complete, and surprise removal started.
At this point, virtio block driver will not get notified by
the driver core layer, because it is likely serializing remove() happening by +user/driver unload and PCI hotplug driver-initiated device removal. So vblk driver doesn't know that device is removed, block layer is waiting for requests completions to arrive which it never gets. So del_gendisk() gets stuck.
Drivers can artificially add timeouts to handle that, but it can be flaky.
Instead, let's add a way for the driver to be notified about the disconnect. It can then do any necessary cleanup, knowing that the device is inactive.
Since cleanups can take a long time, this takes an approach of a work struct that the driver initiates and enables on probe, and tears down on remove.
---
Changes since RFC v1:
- Use disable_work_sync() instead of cancel_work_sync() in pci_clear_disconnect_work() as schedule_work() on a disabled work item is a no-op. (Sashiko)
RFC v1: https://lore.kernel.org/all/[email protected]/
Sashiko review: https://lore.kernel.org/all/[email protected]/
--- drivers/pci/pci.h | 6 ++++++ include/linux/pci.h | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+)
drivers/pci/pci.h +6 -0
@@ -805,6 +805,12 @@ static inline int pci_dev_set_disconnected(struct pci_dev *dev, void *unused)pci_dev_set_io_state (dev, pci_channel_io_perm_failure);pci_doe_disconnected (dev); +if (READ_ONCE (dev->disconnect_work_enable)) { +/* Make sure work is up to date. */ +smp_rmb (); +schedule_work (&dev->disconnect_work); + } +return 0 ; }
include/linux/pci.h +46 -0
58 lines not on this page · raw message
Signature · 1 line
2.51.1
[email protected]
AI reviewRe: [PATCH RFC v2 1/5] PCI: Report surprise removal event
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] pci: workqueue list corruption via TOCTTOU in pci_dev_set_disconnected --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] pci: workqueue list corruption via TOCTTOU in pci_dev_set_disconnected --
commit 95014e29e88ff3b1020861e3c7351ac0913e2410
Author: Michael S. Tsirkin <[email protected]>
PCI: Report surprise removal event
This commit introduces a mechanism to notify drivers about surprise device disconnects via a work struct. It allows drivers to perform necessary cleanup knowing the device is inactive, preventing the remove callback from hanging forever when waiting for interrupts.
drivers/pci/pci.h static inline int pci_dev_set_disconnected(struct pci_dev *dev, void *unused) · line 812
Quoted 13 lines
drivers/pci/pci.h +5
-0@@ -805,6 +805,12 @@ static inline int pci_dev_set_disconnected(struct pci_dev *dev, void *unused)pci_dev_set_io_state (dev, pci_channel_io_perm_failure);pci_doe_disconnected (dev); +if (READ_ONCE (dev->disconnect_work_enable)) { +/* Make sure work is up to date. */ +smp_rmb (); +schedule_work (&dev->disconnect_work); + }
[Severity: High]
Can this lockless check cause a race condition if the CPU is preempted right after the READ_ONCE(), but before schedule_work() executes?
If preemption occurs here, the driver could be concurrently unbound, calling pci_clear_disconnect_work().
If a new driver then binds and initializes the work item via INIT_WORK(&dev->disconnect_work, ...) during its probe, the delayed schedule_work() from the preempted CPU might execute concurrently with the new initialization.
[ ... ]
Quoted 4 lines
include/linux/pci.h +0
-0
[ ... ]
Quoted 1 line
include/linux/pci.h +0
-0@@ -2123,6 +2126,49 @@ pci_release_mem_regions(struct pci_dev *pdev)
[ ... ]
include/linux/pci.h pci_release_mem_regions(struct pci_dev *pdev) · line 2170
Quoted 10 lines
include/linux/pci.h +10
-0+static inline void pci_clear_disconnect_work (struct pci_dev *pdev) +{ +WRITE_ONCE (pdev->disconnect_work_enable,0x0 ); + +/* Make sure to stop using work from now on. */ +smp_wmb (); + +/* Leaves the work disabled, so a racing schedule_work() is a no-op. */ +disable_work_sync (&pdev->disconnect_work); +}
[Severity: High]
Does this function provide enough protection against the delayed schedule_work() scenario mentioned above?
While disable_work_sync() prevents immediate races by setting disable bits on the work item, a subsequent driver probe calling INIT_WORK() will non-atomically reset work->data.
This reset clears all flags, including the WORK_OFFQ_DISABLE_BITS. If INIT_WORK() executes INIT_LIST_HEAD() concurrently with the delayed schedule_work() insertion, couldn't this overwrite the next and prev pointers of the queued work item and corrupt the workqueue linked list?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RFC PATCH v2 2/5 PCI: pciehp: Add pci_hp_wait_link_change()
In reply to Abhin Parekadan Josepci_hp_spurious_link_change() awaits the end of a code section causing spurious link changes, such as a Secondary Bus Reset, and then tells whether such a section has executed, using test_and_clear_bit() on PCI_LINK_CHANGED. The answer is one-shot: only the first caller sees true.
Split the wait out into pci_hp_wait_link_change(), which leaves PCI_LINK_CHANGED alone, and implement pci_hp_spurious_link_change() on top of it. No functional change intended.
--- drivers/pci/hotplug/pci_hotplug_core.c | 21 +++++++++++++++++++-- drivers/pci/pci.h | 1 + 2 files changed, 20 insertions(+), 2 deletions(-)
drivers/pci/hotplug/pci_hotplug_core.c +19 -2
@@ -528,6 +528,24 @@ void pci_hp_unignore_link_change(struct pci_dev *pdev)wake_up_all (&pci_hp_link_change_wq); } +/** +* pci_hp_wait_link_change - await end of code section causing spurious link changes +* @pdev: PCI hotplug bridge +* +* Await the end of a concurrently executing code section which is causing +* spurious link changes on the Secondary Bus of @pdev, if there is one. +* +* Unlike pci_hp_spurious_link_change(), leave the record that such a code +* section has executed in place. May be called by hotplug drivers which need +* the link to have settled, but not the cause of a link change, so that they +* don't take the answer away from the caller of pci_hp_spurious_link_change(). +*/ +void pci_hp_wait_link_change (struct pci_dev *pdev) +{ +wait_event (pci_hp_link_change_wq, + !test_bit (PCI_LINK_CHANGING , &pdev->priv_flags)); +} +/** * pci_hp_spurious_link_change - check for spurious link changes * @pdev: PCI hotplug bridge
@@ -551,8 +569,7 @@ void pci_hp_unignore_link_change(struct pci_dev *pdev)*/ bool pci_hp_spurious_link_change (struct pci_dev *pdev) { -wait_event (pci_hp_link_change_wq, - !test_bit (PCI_LINK_CHANGING , &pdev->priv_flags)); +pci_hp_wait_link_change (pdev);return test_and_clear_bit (PCI_LINK_CHANGED , &pdev->priv_flags); }
drivers/pci/pci.h +1 -0
@@ -360,6 +360,7 @@ static inline int pci_proc_detach_bus(struct pci_bus *bus) { return 0; }/* Functions for PCI Hotplug drivers to use */ int pci_hp_add_bridge (struct pci_dev *dev); +void pci_hp_wait_link_change (struct pci_dev *pdev);bool pci_hp_spurious_link_change (struct pci_dev *pdev);/* Lock for read/write access to pci device and bus lists */
Signature · 1 line
2.51.1
[email protected]
AI reviewRe: [PATCH RFC v2 2/5] PCI: pciehp: Add pci_hp_wait_link_change()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] pci: missing acquire barrier in pci_hp_wait_link_change() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] pci: missing acquire barrier in pci_hp_wait_link_change()
--
commit 4a9f36e746ab6c768961f1d34f724895b17c9def
Author: Abhin Parekadan Jose <[email protected]>
PCI: pciehp: Add pci_hp_wait_link_change()
pci_hp_spurious_link_change() awaits the end of a code section causing spurious link changes, such as a Secondary Bus Reset. This patch splits the wait out into a new function pci_hp_wait_link_change() to allow hotplug drivers to wait for the link to settle without clearing the changed state.
Quoted 4 lines
drivers/pci/hotplug/pci_hotplug_core.c +0
-0
[ ... ]
Quoted 12 lines
drivers/pci/hotplug/pci_hotplug_core.c +8
-0@@ -528,6 +528,24 @@ void pci_hp_unignore_link_change(struct pci_dev *pdev)wake_up_all (&pci_hp_link_change_wq); } +/** +* pci_hp_wait_link_change - await end of code section causing spurious link changes +* @pdev: PCI hotplug bridge +* +* Await the end of a concurrently executing code section which is causing +* spurious link changes on the Secondary Bus of @pdev, if there is one. +* +* Unlike pci_hp_spurious_link_change(), leave the record that such a code
drivers/pci/hotplug/pci_hotplug_core.c void pci_hp_unignore_link_change(struct pci_dev *pdev) · line 548
Quoted 10 lines
drivers/pci/hotplug/pci_hotplug_core.c +10
-0+* section has executed in place. May be called by hotplug drivers which need +* the link to have settled, but not the cause of a link change, so that they +* don't take the answer away from the caller of pci_hp_spurious_link_change(). +*/ +void pci_hp_wait_link_change (struct pci_dev *pdev) +{ +wait_event (pci_hp_link_change_wq, + !test_bit (PCI_LINK_CHANGING , &pdev->priv_flags)); +} +
[Severity: High]
Does this wait_event() condition check need an explicit acquire barrier?
If !test_bit(PCI_LINK_CHANGING, ...) evaluates to true immediately, wait_event() returns without executing any memory barriers.
On weakly ordered architectures like ARM64, could this allow the CPU or compiler to reorder the caller's subsequent state reads before the test_bit() load?
For instance, if thread A clears PCI_LINK_CHANGING and wakes waiters, and thread B calls pci_hp_wait_link_change() and returns immediately without a barrier, could thread B's subsequent PCIe config reads execute before observing the link state change, even if inside an acquire lock like down_read_nested()?
Would using test_bit_acquire() or an explicit smp_mb() prevent stale reads here?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
RFC PATCH v2 3/5 PCI/DPC: Add pci_dpc_wait_recovery()
In reply to Abhin Parekadan Josepci_dpc_recovered() awaits completion of DPC recovery and then tells whether DPC recovered successfully, using test_and_clear_bit() on PCI_DPC_RECOVERED. The answer is one-shot: only the first caller sees true.
Split pci_dpc_recovered() into:
- pci_dpc_hp_sync_supported(), the check whether hotplug can synchronize with DPC at all.
- dpc_wait_completed(), the wait for recovery with its 4 second timeout;
- the final test_and_clear_bit().
Add pci_dpc_wait_recovery(), which does the check and the wait but leaves PCI_DPC_RECOVERED alone. No functional change intended.
--- drivers/pci/pci.h | 2 ++ drivers/pci/pcie/dpc.c | 54 ++++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 12 deletions(-)
drivers/pci/pci.h +2 -0
@@ -940,12 +940,14 @@ void pci_restore_dpc_state(struct pci_dev *dev);void pci_dpc_init (struct pci_dev *pdev);void dpc_process_error (struct pci_dev *pdev);pci_ers_result_t dpc_reset_link (struct pci_dev *pdev); +void pci_dpc_wait_recovery (struct pci_dev *pdev);bool pci_dpc_recovered (struct pci_dev *pdev);unsigned int dpc_tlp_log_len (struct pci_dev *dev);#else static inline void pci_save_dpc_state (struct pci_dev *dev) { }static inline void pci_restore_dpc_state (struct pci_dev *dev) { }static inline void pci_dpc_init (struct pci_dev *pdev) { } +static inline void pci_dpc_wait_recovery (struct pci_dev *pdev) { }static inline bool pci_dpc_recovered (struct pci_dev *pdev) {return false; }#endif
drivers/pci/pcie/dpc.c +42 -12
76 lines not on this page · raw message
Signature · 1 line
2.51.1
[email protected]
AI reviewRe: [PATCH RFC v2 3/5] PCI/DPC: Add pci_dpc_wait_recovery()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
pci_dpc_recovered()awaits completion of DPC recovery and then tells whether DPC recovered successfully, usingtest_and_clear_bit()onPCI_DPC_RECOVERED. The answer is one-shot: only the first caller sees true.Split
pci_dpc_recovered()into:-
pci_dpc_hp_sync_supported(), the check whether hotplug can synchronize with DPC at all.
-dpc_wait_completed(), the wait for recovery with its 4 second timeout;
- the finaltest_and_clear_bit().
Quoted 4 lines
Add
pci_dpc_wait_recovery(), which does the check and the wait but leavesPCI_DPC_RECOVEREDalone. No functional change intended.Signed-off-by Abhin Parekadan Jose <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
RFC PATCH v2 4/5 PCI: pciehp: Report surprise removal from pciehp_isr()
In reply to Abhin Parekadan JoseA surprise removal during a safe removal cannot be reported: the removal blocks waiting on a device interrupt or status read, and the single-threaded IRQ thread is itself executing that removal, so it cannot report that the device is gone. The removal hangs.
The hardirq handler pciehp_isr() still runs while the IRQ thread is blocked, so it can report the disconnect. However, pciehp_ist() deliberately ignores link and presence changes caused by a Secondary Bus Reset or Downstream Port Containment, where the device is only temporarily inaccessible. Distinguishing those normally requires waiting for the SBR or DPC to conclude, which takes seconds and is not possible in hardirq context.
Schedule a work item from pciehp_isr() when a PDC or DLLSC event arrives and neither Presence Detect State nor Data Link Layer Link Active indicates that a card is present.
This provides us a pathway to wait/block/sleep as we will not be in pciehp_isr().
In pciehp_disconnect_work(), wait for the DPC recovery or SBR to complete before checking presence and not consume the link change flags, so that pciehp_ist() can still see them and ignore the link change if it was caused by a DPC or SBR. Even after the DPC or SBR has completed, if the device is really gone then pciehp_disconnect_work() will see that and report the surprise removal. Like pciehp_ist(), it holds a runtime PM reference on the port and checks presence under reset_lock, since a slot reset may make Presence Detect State and Link Active flap.
---
Changes since RFC v1:
- Drop schedule_notification_work(). pci_dev_set_disconnected() schedules the driver's disconnect_work again, as in patch 1, for all callers, and pciehp_disconnect_work() calls it through pci_walk_bus(). (Michael)
- Return early from pciehp_disconnect_work() if pending_events is zero. pciehp_ist() has then already taken the events and handles them itself. (Sashiko)
- Treat a read error of the presence check as "not present", both when scheduling the work in pciehp_isr() and when checking presence in pciehp_disconnect_work(). (Sashiko)
- Check presence with pciehp_card_present_or_link_active(), as pciehp_ist() does, so that a port with Presence Detect State hardwired to zero is not mistaken for an empty slot.
- In pciehp_isr(), check presence before dropping the runtime PM reference on the port's parent. In pciehp_disconnect_work(), take a runtime PM reference and check presence under reset_lock.
- Don't call the spurious link change test from pciehp_disconnect_work(). It consumes the one-shot flags PCI_DPC_RECOVERED and PCI_LINK_CHANGED, so pciehp_ist() could miss them and tear down a device that was only reset. Await DPC recovery and Secondary Bus Reset with the new pci_dpc_wait_recovery() and pci_hp_wait_link_change() (patches 2 and 3) instead, without consuming the flags. pciehp_is_spurious_link_change() is dropped and pciehp_ist() is unchanged. (Sashiko)
RFC v1: https://lore.kernel.org/all/[email protected]/
Michael's review: https://lore.kernel.org/all/[email protected]/
Sashiko review: https://lore.kernel.org/all/[email protected]/
--- drivers/pci/hotplug/pciehp.h | 1 + drivers/pci/hotplug/pciehp_hpc.c | 67 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+)
drivers/pci/hotplug/pciehp.h +1 -0
@@ -116,6 +116,7 @@ struct controller {unsigned int ist_running;int request_result;wait_queue_head_t requester; +struct work_struct disconnect_work; };/**
drivers/pci/hotplug/pciehp_hpc.c +67 -0
103 lines not on this page · raw message
Signature · 1 line
2.51.1
[email protected]
AI reviewRe: [PATCH RFC v2 4/5] PCI: pciehp: Report surprise removal from pciehp_isr()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 28 lines
A surprise removal during a safe removal cannot be reported: the removal blocks waiting on a device interrupt or status read, and the single-threaded IRQ thread is itself executing that removal, so it cannot report that the device is gone. The removal hangs.
The hardirq handler
pciehp_isr()still runs while the IRQ thread is blocked, so it can report the disconnect. However,pciehp_ist()deliberately ignores link and presence changes caused by a Secondary Bus Reset or Downstream Port Containment, where the device is only temporarily inaccessible. Distinguishing those normally requires waiting for the SBR or DPC to conclude, which takes seconds and is not possible in hardirq context.Schedule a work item from
pciehp_isr()when a PDC or DLLSC event arrives and neither Presence Detect State nor Data Link Layer Link Active indicates that a card is present.
This provides us a pathway to wait/block/sleep as we will not be inpciehp_isr().In
pciehp_disconnect_work(), wait for the DPC recovery or SBR to complete before checking presence and not consume the link change flags, so thatpciehp_ist()can still see them and ignore the link change if it was caused by a DPC or SBR. Even after the DPC or SBR has completed, if the device is really gone thenpciehp_disconnect_work()will see that and report the surprise removal. Likepciehp_ist(), it holds a runtime PM reference on the port and checks presence underreset_lock, since a slot reset may make Presence Detect State and Link Active flap.
Quoted 2 lines
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
RFC PATCH v2 5/5 misc: Add edu_srpoc surprise removal POC driver
In reply to Abhin Parekadan JoseA test driver for the QEMU edu device that reproduces the surprise removal hang described in MST's RFC v5 thread.
- hacked in a reg to the edu device on qemu to raise a delayed irq
- This driver writes to that reg in remove and waits for the irq to be handled. This simulates del_gendisk() blocked in blk_mq_freeze_queue_wait()
remove() blocks until the delayed interrupt arrives, 600 seconds after it is requested, or until the device is surprise removed and its disconnect work runs. That is the purpose of the driver, so a normal unbind takes 600 seconds, and on a QEMU without the delayed interrupt register it never returns. It is only built with CONFIG_EDU_SRPOC.
---
Changes since RFC v1:
- Build only with CONFIG_EDU_SRPOC, which depends on PCI, instead of unconditionally. (Sashiko)
- Don't claim the interrupt if the status register reads all ones, i.e. the device is gone. (Sashiko)
- Clear bus mastering in the probe error path and in remove(). (Sashiko)
- Document that remove() blocks by design. (Sashiko)
- Fixed a checkpatch warning about braced if() blocks.
RFC v1: https://lore.kernel.org/all/[email protected]/
Sashiko review: https://lore.kernel.org/all/[email protected]/
--- drivers/misc/Kconfig | 11 +++ drivers/misc/Makefile | 1 + drivers/misc/edu_srpoc.c | 171 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 drivers/misc/edu_srpoc.c
drivers/misc/Kconfig +11 -0
@@ -57,6 +57,17 @@ config DUMMY_IRQ The sole purpose of this module is to help with debugging of systems on which spurious IRQs would happen on disabled IRQ vector. +config EDU_SRPOC + tristate "QEMU edu surprise removal POC driver" + depends on PCI + help + Test driver for the QEMU edu device. Its remove() callback blocks + until the device raises a delayed interrupt or is surprise removed, + to reproduce a hang in remove() during surprise removal. Needs an + edu device with the delayed interrupt register at BAR0 0x30. + + If unsure, say N. + config IBMVMC tristate "IBM Virtual Management Channel support" depends on PPC_PSERIES
drivers/misc/Makefile +1 -0
@@ -9,6 +9,7 @@ obj-$(CONFIG_AD525X_DPOT_I2C) += ad525x_dpot-i2c.o obj-$(CONFIG_AD525X_DPOT_SPI) += ad525x_dpot-spi.o obj-$(CONFIG_ATMEL_SSC) += atmel-ssc.o obj-$(CONFIG_DUMMY_IRQ) += dummy-irq.o +obj-$(CONFIG_EDU_SRPOC) += edu_srpoc.o obj-$(CONFIG_ICS932S401) += ics932s401.o obj-$(CONFIG_LKDTM) += lkdtm/ obj-$(CONFIG_TI_FPC202) += ti_fpc202.o
drivers/misc/edu_srpoc.c new file +171 -0
171 lines not on this page · raw message
Signature · 1 line
2.51.1
[email protected]
AI reviewRe: [PATCH RFC v2 5/5] misc: Add edu_srpoc surprise removal POC driver
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
A test driver for the QEMU edu device that reproduces the surprise removal hang described in MST's RFC v5 thread.
- hacked in a reg to the edu device on qemu to raise a delayed irq
- This driver writes to that reg in remove and waits for the irq to be handled. This simulatesdel_gendisk()blocked inblk_mq_freeze_queue_wait()
Quoted 8 lines
remove()blocks until the delayed interrupt arrives, 600 seconds after it is requested, or until the device is surprise removed and its disconnect work runs. That is the purpose of the driver, so a normal unbind takes 600 seconds, and on a QEMU without the delayed interrupt register it never returns. It is only built withCONFIG_EDU_SRPOC.Assisted-by LLMSigned-off-by Abhin Parekadan Jose <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5