* [PATCH RFC 1/3] PCI: Report surprise removal event
2026-09-05 18:38 [PATCH RFC 0/3] PCI: pciehp: Report surprise removal during safe removal Abhin Parekadan Jose
@ 2026-09-05 18:38 ` Abhin Parekadan Jose
2026-09-05 18:38 ` [PATCH RFC 2/3] PCI: pciehp: Report surprise removal from pciehp_isr() Abhin Parekadan Jose
2026-09-05 18:39 ` [PATCH RFC 3/3] misc: Add edu_srpoc surprise removal POC driver Abhin Parekadan Jose
2 siblings, 0 replies; 4+ messages in thread
From: Abhin Parekadan Jose @ 2026-09-05 18:38 UTC (permalink / raw)
To: bhelgaas, lukas, mst
Cc: linux-pci, linux-kernel, ilpo.jarvinen, kees, xueshuai,
Abhin Parekadan Jose
From: "Michael S. Tsirkin" <mst@redhat.com>
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.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Link: https://lore.kernel.org/all/fba3d235e38c1c6fcef2a30ed083ad9e25b20fa3.1752094439.git.mst@redhat.com/
[Abhin: adapted commit message subject]
Signed-off-by: Abhin Parekadan Jose <abhinjoses@gmail.com>
---
drivers/pci/pci.h | 6 ++++++
include/linux/pci.h | 45 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 51 insertions(+)
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index ba3c3fddddc2..23b1605e783a 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -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;
}
diff --git a/include/linux/pci.h b/include/linux/pci.h
index d31a8d107b1e..06d43f57f509 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -592,6 +592,9 @@ struct pci_dev {
u8 reset_methods[PCI_NUM_RESET_METHODS]; /* In priority order */
struct gpio_desc *wake; /* WAKE# GPIO */
+ /* Report disconnect events. 0x0 - disable, 0x1 - enable */
+ u8 disconnect_work_enable;
+ struct work_struct disconnect_work;
#ifdef CONFIG_PCIE_TPH
u16 tph_cap; /* TPH capability offset */
@@ -2123,6 +2126,48 @@ pci_release_mem_regions(struct pci_dev *pdev)
pci_select_bars(pdev, IORESOURCE_MEM));
}
+/*
+ * Run this first thing after getting a disconnect work, to prevent it from
+ * running multiple times.
+ * Returns: true if disconnect was enabled, proceed. false if disabled, abort.
+ */
+static inline bool pci_test_and_clear_disconnect_enable(struct pci_dev *pdev)
+{
+ u8 enable = 0x1;
+ u8 disable = 0x0;
+
+ return try_cmpxchg(&pdev->disconnect_work_enable, &enable, disable);
+}
+
+/*
+ * Caller must initialize @pdev->disconnect_work before invoking this.
+ * The work function must run and check pci_test_and_clear_disconnect_enable.
+ * Note that device can go away right after this call.
+ */
+static inline void pci_set_disconnect_work(struct pci_dev *pdev)
+{
+ /* Make sure WQ has been initialized already */
+ smp_wmb();
+
+ WRITE_ONCE(pdev->disconnect_work_enable, 0x1);
+
+ /* check the device did not go away meanwhile. */
+ mb();
+
+ if (!pci_device_is_present(pdev))
+ schedule_work(&pdev->disconnect_work);
+}
+
+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();
+
+ cancel_work_sync(&pdev->disconnect_work);
+}
+
bool pci_suspend_retains_context(struct pci_dev *pdev);
#else /* CONFIG_PCI is not enabled */
--
2.51.1
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH RFC 2/3] PCI: pciehp: Report surprise removal from pciehp_isr()
2026-09-05 18:38 [PATCH RFC 0/3] PCI: pciehp: Report surprise removal during safe removal Abhin Parekadan Jose
2026-09-05 18:38 ` [PATCH RFC 1/3] PCI: Report surprise removal event Abhin Parekadan Jose
@ 2026-09-05 18:38 ` Abhin Parekadan Jose
2026-09-05 18:39 ` [PATCH RFC 3/3] misc: Add edu_srpoc surprise removal POC driver Abhin Parekadan Jose
2 siblings, 0 replies; 4+ messages in thread
From: Abhin Parekadan Jose @ 2026-09-05 18:38 UTC (permalink / raw)
To: bhelgaas, lukas, mst
Cc: linux-pci, linux-kernel, ilpo.jarvinen, kees, xueshuai,
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 PDS indicates if the device is connected/disconnected.
This provides us a pathway to wait/block/sleep as we will not be
in pciehp_isr().
Move the scheduling of the driver's disconnect notification out of
pci_dev_set_disconnected() into schedule_notification_work() so it can
be invoked from the new work item. Factor the spurious link change
test out of pciehp_ist() into pciehp_is_spurious_link_change() so both
pciehp_ist() and pciehp_disconnect_work() can use it.
Link: https://lore.kernel.org/all/aHlZE18kPuHuDtTT@wunner.de/
Signed-off-by: Abhin Parekadan Jose <abhinjoses@gmail.com>
---
drivers/pci/hotplug/pciehp.h | 1 +
drivers/pci/hotplug/pciehp_hpc.c | 56 +++++++++++++++++++++++++++-----
drivers/pci/pci.h | 6 ++++
3 files changed, 55 insertions(+), 8 deletions(-)
diff --git a/drivers/pci/hotplug/pciehp.h b/drivers/pci/hotplug/pciehp.h
index debc79b0adfb..c8ceb9320e2e 100644
--- a/drivers/pci/hotplug/pciehp.h
+++ b/drivers/pci/hotplug/pciehp.h
@@ -116,6 +116,7 @@ struct controller {
unsigned int ist_running;
int request_result;
wait_queue_head_t requester;
+ struct work_struct disconnect_work;
};
/**
diff --git a/drivers/pci/hotplug/pciehp_hpc.c b/drivers/pci/hotplug/pciehp_hpc.c
index 4c62140a3cb4..235ca8a176f1 100644
--- a/drivers/pci/hotplug/pciehp_hpc.c
+++ b/drivers/pci/hotplug/pciehp_hpc.c
@@ -620,6 +620,45 @@ static void pciehp_ignore_link_change(struct controller *ctrl,
up_read(&ctrl->reset_lock);
}
+/*
+ * Link Down/Up events caused by Downstream Port Containment if recovery
+ * succeeded, or caused by Secondary Bus Reset, suspend to D3cold, firmware
+ * update, FPGA reconfiguration, etc. are spurious and should be ignored.
+ */
+static bool pciehp_is_spurious_link_change(struct controller *ctrl,
+ struct pci_dev *pdev,
+ u32 events)
+{
+ return (events & (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC)) &&
+ (pci_dpc_recovered(pdev) || pci_hp_spurious_link_change(pdev)) &&
+ ctrl->state == ON_STATE;
+}
+
+/*
+ * Workaround to not wait in the isr.
+ */
+static void pciehp_disconnect_work(struct work_struct *work)
+{
+ struct pci_bus *bus;
+ struct controller *ctrl = container_of(work, struct controller,
+ disconnect_work);
+ struct pci_dev *pdev = ctrl_dev(ctrl);
+ u32 events;
+
+ events = atomic_read(&ctrl->pending_events);
+
+ if (pciehp_is_spurious_link_change(ctrl, pdev, events))
+ return;
+
+ bus = ctrl->pcie->port->subordinate;
+
+ /* The card may have returned */
+ if (!bus || pciehp_card_present(ctrl) != 0)
+ return;
+
+ pci_walk_bus(bus, schedule_notification_work, NULL);
+}
+
static irqreturn_t pciehp_isr(int irq, void *dev_id)
{
struct controller *ctrl = (struct controller *)dev_id;
@@ -722,6 +761,12 @@ static irqreturn_t pciehp_isr(int irq, void *dev_id)
/* Save pending events for consumption by IRQ thread. */
atomic_or(events, &ctrl->pending_events);
+
+ /* presence change events */
+ if ((events & (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC)) &&
+ !pciehp_card_present(ctrl))
+ schedule_work(&ctrl->disconnect_work);
+
return IRQ_WAKE_THREAD;
}
@@ -761,14 +806,7 @@ static irqreturn_t pciehp_ist(int irq, void *dev_id)
PCI_EXP_SLTCTL_ATTN_IND_ON);
}
- /*
- * Ignore Link Down/Up events caused by Downstream Port Containment
- * if recovery succeeded, or caused by Secondary Bus Reset,
- * suspend to D3cold, firmware update, FPGA reconfiguration, etc.
- */
- if ((events & (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC)) &&
- (pci_dpc_recovered(pdev) || pci_hp_spurious_link_change(pdev)) &&
- ctrl->state == ON_STATE) {
+ if (pciehp_is_spurious_link_change(ctrl, pdev, events)) {
u16 ignored_events = PCI_EXP_SLTSTA_DLLSC;
if (!ctrl->inband_presence_disabled)
@@ -1036,6 +1074,7 @@ struct controller *pcie_init(struct pcie_device *dev)
init_waitqueue_head(&ctrl->requester);
init_waitqueue_head(&ctrl->queue);
INIT_DELAYED_WORK(&ctrl->button_work, pciehp_queue_pushbutton_work);
+ INIT_WORK(&ctrl->disconnect_work, pciehp_disconnect_work);
dbg_ctrl(ctrl);
down_read(&pci_bus_sem);
@@ -1096,6 +1135,7 @@ struct controller *pcie_init(struct pcie_device *dev)
void pciehp_release_ctrl(struct controller *ctrl)
{
cancel_delayed_work_sync(&ctrl->button_work);
+ cancel_work_sync(&ctrl->disconnect_work);
kfree(ctrl);
}
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 23b1605e783a..4e17878edeab 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -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);
+ return 0;
+}
+
+static inline int schedule_notification_work(struct pci_dev *dev, void *unused)
+{
+ pci_dev_set_disconnected(dev, NULL);
if (READ_ONCE(dev->disconnect_work_enable)) {
/* Make sure work is up to date. */
smp_rmb();
--
2.51.1
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH RFC 3/3] misc: Add edu_srpoc surprise removal POC driver
2026-09-05 18:38 [PATCH RFC 0/3] PCI: pciehp: Report surprise removal during safe removal Abhin Parekadan Jose
2026-09-05 18:38 ` [PATCH RFC 1/3] PCI: Report surprise removal event Abhin Parekadan Jose
2026-09-05 18:38 ` [PATCH RFC 2/3] PCI: pciehp: Report surprise removal from pciehp_isr() Abhin Parekadan Jose
@ 2026-09-05 18:39 ` Abhin Parekadan Jose
2 siblings, 0 replies; 4+ messages in thread
From: Abhin Parekadan Jose @ 2026-09-05 18:39 UTC (permalink / raw)
To: bhelgaas, lukas, mst
Cc: linux-pci, linux-kernel, ilpo.jarvinen, kees, xueshuai,
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()
Assisted-by: LLM
Signed-off-by: Abhin Parekadan Jose <abhinjoses@gmail.com>
---
drivers/misc/Makefile | 1 +
drivers/misc/edu_srpoc.c | 169 +++++++++++++++++++++++++++++++++++++++
2 files changed, 170 insertions(+)
create mode 100644 drivers/misc/edu_srpoc.c
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index e8d8d5d88c0d..1479bf19c646 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -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-y += edu_srpoc.o
obj-$(CONFIG_ICS932S401) += ics932s401.o
obj-$(CONFIG_LKDTM) += lkdtm/
obj-$(CONFIG_TI_FPC202) += ti_fpc202.o
diff --git a/drivers/misc/edu_srpoc.c b/drivers/misc/edu_srpoc.c
new file mode 100644
index 000000000000..f536bc4aa253
--- /dev/null
+++ b/drivers/misc/edu_srpoc.c
@@ -0,0 +1,169 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * edu_srpoc.c Surprise Removal POC driver for the QEMU edu device
+ *
+ * In remove(), schedules a delayed interrupt on the edu device and
+ * blocks waiting for it to complete. This simulates del_gendisk()
+ * blocked in blk_mq_freeze_queue_wait() on slow in-flight I/O.
+ *
+ * Surprise-remove the device during this window to reproduce the hang.
+ *
+ * edu BAR 0 registers used:
+ * 0x08 Factorial: write N to compute N! asynchronously
+ * 0x20 Status: write EDU_STATUS_IRQFACT to enable IRQ on completion
+ * 0x24 IRQ status: bit 0 = FACT_IRQ, bit 9 = DELAY_IRQ
+ * 0x30 Delayed IRQ: write N (ms). Hacked in this functionality(not upstream).
+ * 0x64 IRQ lower: write bitmask to ack
+ */
+
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/interrupt.h>
+#include <linux/completion.h>
+#include <linux/delay.h>
+
+#define PCI_VENDOR_ID_EDU 0x1234
+#define PCI_DEVICE_ID_EDU 0x11e8
+
+#define EDU_REG_FACT 0x08
+#define EDU_REG_STATUS 0x20
+#define EDU_REG_DELAYED_IRQ 0x30
+#define EDU_REG_IRQ_STATUS 0x24
+#define EDU_REG_IRQ_LOWER 0x64
+
+#define EDU_STATUS_IRQFACT 0x80
+#define EDU_FACT_IRQ BIT(0)
+#define EDU_DELAY_IRQ BIT(9)
+
+struct edu_dev {
+ struct pci_dev *pdev;
+ void __iomem *regs;
+ struct completion irq_done;
+};
+
+static irqreturn_t edu_irq_handler(int irq, void *data)
+{
+ struct edu_dev *edu = data;
+ u32 status;
+
+ status = ioread32(edu->regs + EDU_REG_IRQ_STATUS);
+ if (!status)
+ return IRQ_NONE;
+
+ iowrite32(status, edu->regs + EDU_REG_IRQ_LOWER);
+
+ if (status & (EDU_FACT_IRQ | EDU_DELAY_IRQ)) {
+ complete(&edu->irq_done);
+ }
+
+ return IRQ_HANDLED;
+}
+
+static void edu_disconnect(struct work_struct *work)
+{
+ struct pci_dev *pdev = container_of(work, struct pci_dev,
+ disconnect_work);
+ struct edu_dev *edu = pci_get_drvdata(pdev);
+
+ if (!pci_test_and_clear_disconnect_enable(pdev))
+ return;
+
+ if (!edu)
+ return;
+
+ dev_info(&pdev->dev, "disconnect_work fired — unblocking remove()\n");
+ complete(&edu->irq_done);
+}
+
+static int edu_probe(struct pci_dev *pdev, const struct pci_device_id *id)
+{
+ struct edu_dev *edu;
+ int err;
+
+ edu = devm_kzalloc(&pdev->dev, sizeof(*edu), GFP_KERNEL);
+ if (!edu)
+ return -ENOMEM;
+
+ edu->pdev = pdev;
+ init_completion(&edu->irq_done);
+
+ err = pci_enable_device(pdev);
+ if (err)
+ return err;
+
+ err = pci_request_regions(pdev, "edu_srpoc");
+ if (err)
+ goto err_disable;
+
+ edu->regs = pci_iomap(pdev, 0, 0);
+ if (!edu->regs) {
+ err = -ENOMEM;
+ goto err_release;
+ }
+
+ pci_set_master(pdev);
+
+ err = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI | PCI_IRQ_INTX);
+ if (err < 0)
+ goto err_iounmap;
+
+ err = request_irq(pci_irq_vector(pdev, 0), edu_irq_handler,
+ IRQF_SHARED, "edu_srpoc", edu);
+ if (err)
+ goto err_free_vectors;
+
+ pci_set_drvdata(pdev, edu);
+
+ INIT_WORK(&pdev->disconnect_work, edu_disconnect);
+ pci_set_disconnect_work(pdev);
+
+ dev_info(&pdev->dev, "edu_srpoc probed\n");
+ return 0;
+
+err_free_vectors:
+ pci_free_irq_vectors(pdev);
+err_iounmap:
+ pci_iounmap(pdev, edu->regs);
+err_release:
+ pci_release_regions(pdev);
+err_disable:
+ pci_disable_device(pdev);
+ return err;
+}
+
+static void edu_remove(struct pci_dev *pdev)
+{
+ struct edu_dev *edu = pci_get_drvdata(pdev);
+
+ 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);
+ 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_iounmap(pdev, edu->regs);
+ pci_release_regions(pdev);
+ pci_disable_device(pdev);
+}
+
+static const struct pci_device_id edu_ids[] = {
+ { PCI_DEVICE(PCI_VENDOR_ID_EDU, PCI_DEVICE_ID_EDU) },
+ { 0 }
+};
+MODULE_DEVICE_TABLE(pci, edu_ids);
+
+static struct pci_driver edu_driver = {
+ .name = "edu_srpoc",
+ .id_table = edu_ids,
+ .probe = edu_probe,
+ .remove = edu_remove,
+};
+
+module_pci_driver(edu_driver);
+MODULE_AUTHOR("Abhin Parekadan Jose");
+MODULE_DESCRIPTION("edu surprise removal POC driver");
+MODULE_LICENSE("GPL");
--
2.51.1
^ permalink raw reply [flat|nested] 4+ messages in thread