Skip to contentSkip to conversation

PCI: pciehp: Report surprise removal during safe removal

RFC PATCH In review Abhin Parekadan Jose 3 participants 16 messages Sep 27 – Oct 12

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 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 serialize disconnect_work_enable with a per-device spinlock.

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 v3 [5]:

- 5/5: Reinitialize the completion in remove() before requesting the delayed interrupt, so that an earlier completion cannot let remove() return while the interrupt is still pending. (Sashiko)
- 1/5 to 4/5: No changes.

Changes since RFC v2 [4]:

- 1/5: Protect disconnect_work_enable with a per-device spinlock, held while testing it and scheduling the work, instead of lockless accesses and barriers. pci_dev_set_disconnected() could otherwise test the flag, get preempted, and queue the work while a newly bound driver re-initializes it. With the lock, cancel_work_sync() is sufficient again, so go back to it from disable_work_sync(). (Sashiko)
- 2/5: Test PCI_LINK_CHANGING with test_bit_acquire(), so that the caller's subsequent accesses are ordered after the end of the code section even if wait_event() returns without sleeping. (Sashiko)
- 3/5, 4/5, 5/5: No changes.

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
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/

Assisted-by LLM

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               | 172 +++++++++++++++++++++++++
 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                      |  10 ++
 drivers/pci/pcie/dpc.c                 |  54 ++++++--
 drivers/pci/probe.c                    |   1 +
 include/linux/pci.h                    |  57 ++++++++
 10 files changed, 381 insertions(+), 14 deletions(-)
 create mode 100644 drivers/misc/edu_srpoc.c

base-commit: fd179f8a05be3ccae366b9b96e176b51fbe54aab --
2.51.1

#1 lore raw

RFC PATCH v4 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 v2:
- Protect disconnect_work_enable with a per-device spinlock, held while testing it and scheduling the work, instead of lockless accesses and barriers. pci_dev_set_disconnected() could otherwise test the flag, get preempted, and queue the work while a newly bound driver re-initializes it. With the lock, cancel_work_sync() is sufficient again, so go back to it from disable_work_sync(). (Sashiko)

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 v2: https://lore.kernel.org/all/[email protected]/
Sashiko review of v2: https://lore.kernel.org/all/[email protected]/
RFC v1: https://lore.kernel.org/all/[email protected]/
Sashiko review of v1: https://lore.kernel.org/all/[email protected]/

---
 drivers/pci/pci.h   |  7 ++++++
 drivers/pci/probe.c |  1 +
 include/linux/pci.h | 57 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 65 insertions(+)
drivers/pci/pci.h +7 -0
index ba3c3fddddc23..175273756c183 100644
@@ -802,9 +802,16 @@ static inline bool pci_dev_set_io_state(struct pci_dev *dev,
 
 static inline int pci_dev_set_disconnected(struct pci_dev *dev, void *unused)
 {
+	unsigned long flags;
+
 	pci_dev_set_io_state(dev, pci_channel_io_perm_failure);
 	pci_doe_disconnected(dev);
 
+	spin_lock_irqsave(&dev->disconnect_lock, flags);
+	if (dev->disconnect_work_enable)
+		schedule_work(&dev->disconnect_work);
+	spin_unlock_irqrestore(&dev->disconnect_lock, flags);
+
 	return 0;
 }
 
drivers/pci/probe.c +1 -0
index 27008e2ea5afc..3a05ad32bfd54 100644
@@ -2515,6 +2515,7 @@ struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
 	};
 
 	spin_lock_init(&dev->pcie_cap_lock);
+	spin_lock_init(&dev->disconnect_lock);
 #ifdef CONFIG_PCI_MSI
 	raw_spin_lock_init(&dev->msi_lock);
 #endif
include/linux/pci.h +57 -0

69 lines not on this page · raw message

Signature · 1 line
2.51.1
#3 lore raw

RFC PATCH v4 2/5 PCI: pciehp: Add pci_hp_wait_link_change()

In reply to Abhin Parekadan Jose

pci_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. Test PCI_LINK_CHANGING with test_bit_acquire(), so that the caller's subsequent accesses are ordered after the end of the code section even if wait_event() returns without sleeping. No functional change intended.

Assisted-by LLM

---
Changes since RFC v2:
- Test PCI_LINK_CHANGING with test_bit_acquire(). (Sashiko)

RFC v2: https://lore.kernel.org/all/[email protected]/
Sashiko review of v2: https://lore.kernel.org/all/[email protected]/

---
 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
index fadcf98a8a660..75524fe2bec6e 100644
@@ -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_acquire(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
index 175273756c183..cfa0202bf610b 100644
@@ -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
#5 lore raw

RFC PATCH v4 3/5 PCI/DPC: Add pci_dpc_wait_recovery()

In reply to Abhin Parekadan Jose

pci_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.

Assisted-by LLM
---
 drivers/pci/pci.h      |  2 ++
 drivers/pci/pcie/dpc.c | 54 ++++++++++++++++++++++++++++++++----------
 2 files changed, 44 insertions(+), 12 deletions(-)
drivers/pci/pci.h +2 -0
index cfa0202bf610b..b1d9df904c625 100644
@@ -941,12 +941,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
#7 lore raw

RFC PATCH v4 4/5 PCI: pciehp: Report surprise removal from pciehp_isr()

In reply to Abhin Parekadan Jose

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 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
index debc79b0adfb2..c8ceb9320e2e9 100644
@@ -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
#9 lore raw

RFC PATCH v4 5/5 misc: Add edu_srpoc surprise removal POC driver

In reply to Abhin Parekadan Jose

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 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.

Assisted-by LLM

---
Changes since RFC v3:
- Reinitialize the completion in remove() before requesting the delayed interrupt, so that an earlier completion cannot let remove() return while the interrupt is still pending. (Sashiko)

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]/
RFC v3: https://lore.kernel.org/all/[email protected]/
Sashiko review of v3: https://lore.kernel.org/all/[email protected]/

---
 drivers/misc/Kconfig     |  11 +++
 drivers/misc/Makefile    |   1 +
 drivers/misc/edu_srpoc.c | 172 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 184 insertions(+)
 create mode 100644 drivers/misc/edu_srpoc.c
drivers/misc/Kconfig +11 -0
index 7364931dad3a1..99457e53d21cf 100644
@@ -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
index e8d8d5d88c0df..9ebcc6ce60f34 100644
@@ -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 +172 -0

172 lines not on this page · raw message

Signature · 1 line
2.51.1
#6 lore raw

Re: [PATCH RFC v4 3/5] PCI/DPC: Add pci_dpc_wait_recovery()

In reply to Abhin Parekadan Jose
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, 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().

Quoted 4 lines

Add pci_dpc_wait_recovery(), which does the check and the wait but leaves PCI_DPC_RECOVERED alone. No functional change intended.

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
#4 lore raw

Re: [PATCH RFC v4 2/5] PCI: pciehp: Add pci_hp_wait_link_change()

In reply to Abhin Parekadan Jose
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

pci_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.

Quoted 8 lines

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. Test PCI_LINK_CHANGING with test_bit_acquire(), so that the caller's subsequent accesses are ordered after the end of the code section even if wait_event() returns without sleeping. No functional change intended.

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=2
#10 lore raw

Re: [PATCH RFC v4 5/5] misc: Add edu_srpoc surprise removal POC driver

In reply to Abhin Parekadan Jose
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] misc: edu_srpoc: lost disconnect_work completion causes remove() deadlock --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] misc: edu_srpoc: lost disconnect_work completion causes remove() deadlock --

commit bc425826c0abe82189b4fc145666a7bddb680acf
Author: Abhin Parekadan Jose <[email protected]>

misc: Add edu_srpoc surprise removal POC driver

A test driver for the QEMU edu device that reproduces a surprise removal hang. It blocks in remove() until a delayed interrupt arrives or the device is surprise removed and its disconnect work runs.

Quoted 5 lines
drivers/misc/edu_srpoc.c new file +0 -0
new file mode 100644
index 0000000000000..1f395b2c31ee3

[ ... ]

drivers/misc/edu_srpoc.c line 139

Quoted 5 lines
drivers/misc/edu_srpoc.c new file +5 -0
+static void edu_remove(struct pci_dev *pdev)
+{
+	struct edu_dev *edu = pci_get_drvdata(pdev);
+
+	reinit_completion(&edu->irq_done);

[Severity: Medium]
Can this cause a deadlock if the device is surprise-removed before edu_remove() starts executing?

If the PCI core schedules and executes edu_disconnect() first, it clears the disconnect_work_enable flag and signals complete() on edu->irq_done:

drivers/misc/edu_srpoc.c:edu_disconnect() {
    ...
	if (!pci_test_and_clear_disconnect_enable(pdev))
		return;
    ...
	complete(&edu->irq_done);
}

When edu_remove() then runs, it unconditionally calls reinit_completion(), which wipes out the unblock signal from edu_disconnect(). Because the disconnect flag was already cleared, edu_disconnect() will not run again.

drivers/misc/edu_srpoc.c line 144

Quoted 5 lines
drivers/misc/edu_srpoc.c new file +5 -0
+	iowrite32(EDU_STATUS_IRQFACT, edu->regs + EDU_REG_STATUS);
+	iowrite32(600000, edu->regs + EDU_REG_DELAYED_IRQ);
+
+	dev_info(&pdev->dev, "Waiting for IRQ in remove()\n");
+	wait_for_completion(&edu->irq_done);

Since the device is gone and no hardware interrupts will arrive, will wait_for_completion() block indefinitely here, defeating the test driver's stated purpose of unblocking on surprise removal?

Quoted 10 lines
drivers/misc/edu_srpoc.c new file +10 -0
+	dev_info(&pdev->dev, "Unblocked, cleaning up\n");
+
+	pci_clear_disconnect_work(pdev);
+	free_irq(pci_irq_vector(pdev, 0), edu);
+	pci_free_irq_vectors(pdev);
+	pci_clear_master(pdev);
+	pci_iounmap(pdev, edu->regs);
+	pci_release_regions(pdev);
+	pci_disable_device(pdev);
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#8 lore raw

Re: [PATCH RFC v4 4/5] PCI: pciehp: Report surprise removal from pciehp_isr()

In reply to Abhin Parekadan Jose
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 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.

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
#2 lore raw

Re: [PATCH RFC v4 1/5] PCI: Report surprise removal event

In reply to Abhin Parekadan Jose
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines

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:

Quoted 7 lines

1. the graceful removal is ongoing in the remove() callback,
[ ... ]

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=1
#11 lore raw

Re: [PATCH RFC v4 5/5] misc: Add edu_srpoc surprise removal POC driver

In reply to [email protected]
Quoted 51 lines · On Sun, Sep 27, 2026 at 06:27:54PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] misc: edu_srpoc: lost disconnect_work completion causes remove() deadlock --

commit bc425826c0abe82189b4fc145666a7bddb680acf
Author: Abhin Parekadan Jose <[email protected]>

misc: Add edu_srpoc surprise removal POC driver

A test driver for the QEMU edu device that reproduces a surprise removal hang. It blocks in remove() until a delayed interrupt arrives or the device is surprise removed and its disconnect work runs.

Quoted 5 lines
drivers/misc/edu_srpoc.c new file +0 -0
new file mode 100644
index 0000000000000..1f395b2c31ee3

[ ... ]

drivers/misc/edu_srpoc.c line 139

Quoted 5 lines
drivers/misc/edu_srpoc.c new file +5 -0
+static void edu_remove(struct pci_dev *pdev)
+{
+	struct edu_dev *edu = pci_get_drvdata(pdev);
+
+	reinit_completion(&edu->irq_done);

[Severity: Medium]
Can this cause a deadlock if the device is surprise-removed before edu_remove() starts executing?

If the PCI core schedules and executes edu_disconnect() first, it clears the disconnect_work_enable flag and signals complete() on edu->irq_done:

drivers/misc/edu_srpoc.c:edu_disconnect() {
    ...
	if (!pci_test_and_clear_disconnect_enable(pdev))
		return;
    ...
	complete(&edu->irq_done);
}

When edu_remove() then runs, it unconditionally calls reinit_completion(), which wipes out the unblock signal from edu_disconnect(). Because the disconnect flag was already cleared, edu_disconnect() will not run again.

Quoted 10 lines

drivers/misc/edu_srpoc.c line 144

Quoted 5 lines
drivers/misc/edu_srpoc.c new file +5 -0
+	iowrite32(EDU_STATUS_IRQFACT, edu->regs + EDU_REG_STATUS);
+	iowrite32(600000, edu->regs + EDU_REG_DELAYED_IRQ);
+
+	dev_info(&pdev->dev, "Waiting for IRQ in remove()\n");
+	wait_for_completion(&edu->irq_done);

Since the device is gone and no hardware interrupts will arrive, will wait_for_completion() block indefinitely here, defeating the test driver's stated purpose of unblocking on surprise removal?

This driver is only meant to reproduce the two use cases in the cover letter, where remove() is already blocked when the device is surprise removed. It is not a complete driver and is not intended to be merged, so corner cases like this one are not handled.

#12 lore raw
In reply to Abhin Parekadan Jose

Gentle ping on this RFC.

Lukas, Bjorn, could you let me know whether the general direction is acceptable before I work on a non-RFC version? Any comments on the approach would help.

Thanks,
Abhin

Quoted 171 lines · Abhin Parekadan Jose

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 serialize disconnect_work_enable with a per-device spinlock.

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 v3 [5]:

- 5/5: Reinitialize the completion in remove() before requesting the delayed interrupt, so that an earlier completion cannot let remove() return while the interrupt is still pending. (Sashiko)
- 1/5 to 4/5: No changes.

Changes since RFC v2 [4]:

- 1/5: Protect disconnect_work_enable with a per-device spinlock, held while testing it and scheduling the work, instead of lockless accesses and barriers. pci_dev_set_disconnected() could otherwise test the flag, get preempted, and queue the work while a newly bound driver re-initializes it. With the lock, cancel_work_sync() is sufficient again, so go back to it from disable_work_sync(). (Sashiko)
- 2/5: Test PCI_LINK_CHANGING with test_bit_acquire(), so that the caller's subsequent accesses are ordered after the end of the code section even if wait_event() returns without sleeping. (Sashiko)
- 3/5, 4/5, 5/5: No changes.

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
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/

Assisted-by LLM

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               | 172 +++++++++++++++++++++++++
 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                      |  10 ++
 drivers/pci/pcie/dpc.c                 |  54 ++++++--
 drivers/pci/probe.c                    |   1 +
 include/linux/pci.h                    |  57 ++++++++
 10 files changed, 381 insertions(+), 14 deletions(-)
 create mode 100644 drivers/misc/edu_srpoc.c

base-commit: fd179f8a05be3ccae366b9b96e176b51fbe54aab --
2.51.1

#13 lore raw
Quoted 8 lines · Abhin Parekadan Jose

Gentle ping on this RFC.

Lukas, Bjorn, could you let me know whether the general direction is acceptable before I work on a non-RFC version? Any comments on the approach would help.

Thanks,
Abhin

I'd say, complete the picture. acpi,shpc,polling...

Maybe think about how this interacts with pci_ignore_hotplug (seems to weirdly check presence then ignore)?

And test with virtio not with edu.

Quoted 33 lines · Abhin Parekadan Jose
Quoted 32 lines

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 serialize disconnect_work_enable with a per-device spinlock.

2/5 Add pci_hp_wait_link_change(), which awaits a Secondary Bus Reset
in progress without consuming PCI_LINK_CHANGED.

Quoted 6 lines
Quoted 6 lines

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.

how about including the virtio patch? seems more relevant than fooling around with edu.

Quoted 132 lines
Quoted 132 lines

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 v3 [5]:

- 5/5: Reinitialize the completion in remove() before requesting the delayed interrupt, so that an earlier completion cannot let remove() return while the interrupt is still pending. (Sashiko)
- 1/5 to 4/5: No changes.

Changes since RFC v2 [4]:

- 1/5: Protect disconnect_work_enable with a per-device spinlock, held while testing it and scheduling the work, instead of lockless accesses and barriers. pci_dev_set_disconnected() could otherwise test the flag, get preempted, and queue the work while a newly bound driver re-initializes it. With the lock, cancel_work_sync() is sufficient again, so go back to it from disable_work_sync(). (Sashiko)
- 2/5: Test PCI_LINK_CHANGING with test_bit_acquire(), so that the caller's subsequent accesses are ordered after the end of the code section even if wait_event() returns without sleeping. (Sashiko)
- 3/5, 4/5, 5/5: No changes.

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
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/

Assisted-by LLM

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               | 172 +++++++++++++++++++++++++
 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                      |  10 ++
 drivers/pci/pcie/dpc.c                 |  54 ++++++--
 drivers/pci/probe.c                    |   1 +
 include/linux/pci.h                    |  57 ++++++++
 10 files changed, 381 insertions(+), 14 deletions(-)
 create mode 100644 drivers/misc/edu_srpoc.c

base-commit: fd179f8a05be3ccae366b9b96e176b51fbe54aab --
2.51.1

#14 lore raw
Quoted 11 lines · Michael S. Tsirkin
Quoted 8 lines · Abhin Parekadan Jose

Gentle ping on this RFC.

Lukas, Bjorn, could you let me know whether the general direction is acceptable before I work on a non-RFC version? Any comments on the approach would help.

Thanks,
Abhin

I'd say, complete the picture. acpi,shpc,polling...

Ok.

Quoted 3 lines

Maybe think about how this interacts with pci_ignore_hotplug (seems to weirdly check presence then ignore)?

Yeah, the order is wrong in v4. The presence read should come after the ignore_hotplug check. Will fix.
(Need to still find out why this was introduced. Looks like only the GPU drivers use it.)

Quoted 2 lines

And test with virtio not with edu.

Bjorn had asked me to keep this puerly PCI[1], So I had decoupled it from the virtio changes. I could post another series with those changes with a dependecy to this series.

[1] https://lore.kernel.org/all/20260826194815.GA1552818@bhelgaas/

Quoted 178 lines
Quoted 34 lines · Abhin Parekadan Jose
Quoted 32 lines

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 serialize disconnect_work_enable with a per-device spinlock.

2/5 Add pci_hp_wait_link_change(), which awaits a Secondary Bus Reset
in progress without consuming PCI_LINK_CHANGED.

Quoted 6 lines
Quoted 6 lines

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.

how about including the virtio patch? seems more relevant than fooling around with edu.

Quoted 132 lines
Quoted 132 lines

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 v3 [5]:

- 5/5: Reinitialize the completion in remove() before requesting the delayed interrupt, so that an earlier completion cannot let remove() return while the interrupt is still pending. (Sashiko)
- 1/5 to 4/5: No changes.

Changes since RFC v2 [4]:

- 1/5: Protect disconnect_work_enable with a per-device spinlock, held while testing it and scheduling the work, instead of lockless accesses and barriers. pci_dev_set_disconnected() could otherwise test the flag, get preempted, and queue the work while a newly bound driver re-initializes it. With the lock, cancel_work_sync() is sufficient again, so go back to it from disable_work_sync(). (Sashiko)
- 2/5: Test PCI_LINK_CHANGING with test_bit_acquire(), so that the caller's subsequent accesses are ordered after the end of the code section even if wait_event() returns without sleeping. (Sashiko)
- 3/5, 4/5, 5/5: No changes.

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
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/

Assisted-by LLM

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               | 172 +++++++++++++++++++++++++
 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                      |  10 ++
 drivers/pci/pcie/dpc.c                 |  54 ++++++--
 drivers/pci/probe.c                    |   1 +
 include/linux/pci.h                    |  57 ++++++++
 10 files changed, 381 insertions(+), 14 deletions(-)
 create mode 100644 drivers/misc/edu_srpoc.c

base-commit: fd179f8a05be3ccae366b9b96e176b51fbe54aab --
2.51.1

#15 lore raw
Quoted 25 lines · Abhin Parekadan Jose
Quoted 11 lines · Michael S. Tsirkin
Quoted 8 lines · Abhin Parekadan Jose

Gentle ping on this RFC.

Lukas, Bjorn, could you let me know whether the general direction is acceptable before I work on a non-RFC version? Any comments on the approach would help.

Thanks,
Abhin

I'd say, complete the picture. acpi,shpc,polling...

Ok.

Quoted 3 lines

Maybe think about how this interacts with pci_ignore_hotplug (seems to weirdly check presence then ignore)?

Yeah, the order is wrong in v4. The presence read should come after the ignore_hotplug check. Will fix.
(Need to still find out why this was introduced. Looks like only the GPU drivers use it.)

Quoted 1 line
Quoted 7 lines
Quoted 1 line

And test with virtio not with edu.

Bjorn had asked me to keep this puerly PCI[1], So I had decoupled it from the virtio changes. I could post another series with those changes with a dependecy to this series.

[1] https://lore.kernel.org/all/20260826194815.GA1552818@bhelgaas/

yea sure.

Quoted 178 lines
Quoted 178 lines
Quoted 34 lines · Abhin Parekadan Jose
Quoted 32 lines

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 serialize disconnect_work_enable with a per-device spinlock.

2/5 Add pci_hp_wait_link_change(), which awaits a Secondary Bus Reset
in progress without consuming PCI_LINK_CHANGED.

Quoted 6 lines
Quoted 6 lines

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.

how about including the virtio patch? seems more relevant than fooling around with edu.

Quoted 132 lines
Quoted 132 lines

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 v3 [5]:

- 5/5: Reinitialize the completion in remove() before requesting the delayed interrupt, so that an earlier completion cannot let remove() return while the interrupt is still pending. (Sashiko)
- 1/5 to 4/5: No changes.

Changes since RFC v2 [4]:

- 1/5: Protect disconnect_work_enable with a per-device spinlock, held while testing it and scheduling the work, instead of lockless accesses and barriers. pci_dev_set_disconnected() could otherwise test the flag, get preempted, and queue the work while a newly bound driver re-initializes it. With the lock, cancel_work_sync() is sufficient again, so go back to it from disable_work_sync(). (Sashiko)
- 2/5: Test PCI_LINK_CHANGING with test_bit_acquire(), so that the caller's subsequent accesses are ordered after the end of the code section even if wait_event() returns without sleeping. (Sashiko)
- 3/5, 4/5, 5/5: No changes.

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
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/

Assisted-by LLM

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               | 172 +++++++++++++++++++++++++
 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                      |  10 ++
 drivers/pci/pcie/dpc.c                 |  54 ++++++--
 drivers/pci/probe.c                    |   1 +
 include/linux/pci.h                    |  57 ++++++++
 10 files changed, 381 insertions(+), 14 deletions(-)
 create mode 100644 drivers/misc/edu_srpoc.c

base-commit: fd179f8a05be3ccae366b9b96e176b51fbe54aab --
2.51.1