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
Signed-off-by: Abhin Parekadan Jose <[email protected]>

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

diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index 7364931dad3a1..99457e53d21cf 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -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
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index e8d8d5d88c0df..9ebcc6ce60f34 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-$(CONFIG_EDU_SRPOC)                += 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 0000000000000..b74a95109dba9
--- /dev/null
+++ b/drivers/misc/edu_srpoc.c
@@ -0,0 +1,171 @@
+// 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);
+       /* All ones means the device is gone; the interrupt is not ours */
+       if (!status || PCI_POSSIBLE_ERROR(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_clear_master(pdev);
+       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_clear_master(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


Reply via email to