Skip to contentSkip to conversation

dmaengine: idxd: Fix deadlock on interrupt handle revocation

PATCH Superseded Ziyu Zhang 2 participants 2 messages Nov 25

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ziyu Zhang

author
#0 lore raw

PATCH 1/1 dmaengine: idxd: Fix deadlock on interrupt handle revocation

idxd_misc_thread() is the threaded handler of the misc MSI-X vector and the only context that completes idxd device commands: it calls complete(idxd->cmd_done) for IDXD_INTC_CMD. On an IDXD_INTC_INT_HANDLE_REVOKED interrupt it allocated a work item with GFP_ATOMIC and, if that allocation failed, called idxd_wqs_quiesce() inline to fall back. The quiesce takes wq_lock of every enabled kernel work queue, so the misc thread can block there behind a command submitter that holds wq_lock and waits for cmd_done, which only the misc thread can complete. Nothing bounds that wait.

This potential deadlock was found by our experimental static analysis tool, which analyzes lock usage and completion-based synchronization.

Embed the work item in struct idxd_device and queue it unconditionally, removing the allocation and its synchronous quiesce fallback.

Revokes received while the work is pending are coalesced; the queued work refreshes all valid interrupt handles. A revoke received while the work is running can queue another execution to refresh handles that have already been processed. Reusing the same work item on the same workqueue also serialises revoke handling.

Fixes 56fc39f5a367 ("dmaengine: idxd: handle interrupt handle revoked event")
---
 drivers/dma/idxd/idxd.h |  2 ++
 drivers/dma/idxd/init.c |  1 +
 drivers/dma/idxd/irq.c  | 27 ++++-----------------------
 3 files changed, 7 insertions(+), 23 deletions(-)
drivers/dma/idxd/idxd.h +2 -0
index ce78b9a7c641..03a73da22596 100644
@@ -366,6 +366,7 @@ struct idxd_device {
 	struct idxd_dma_dev *idxd_dma;
 	struct workqueue_struct *wq;
 	struct work_struct work;
+	struct work_struct int_handle_revoke_work;
 
 	struct idxd_pmu *idxd_pmu;
 
@@ -762,6 +763,7 @@ int idxd_load_iaa_device_defaults(struct idxd_device *idxd);
 
 /* device interrupt control */
 irqreturn_t idxd_misc_thread(int vec, void *data);
+void idxd_int_handle_revoke(struct work_struct *work);
 irqreturn_t idxd_wq_thread(int irq, void *data);
 void idxd_mask_error_interrupts(struct idxd_device *idxd);
 void idxd_unmask_error_interrupts(struct idxd_device *idxd);
drivers/dma/idxd/init.c +1 -0
index 4b827a329756..f3ade8036ddb 100644
@@ -108,6 +108,7 @@ static int idxd_setup_interrupts(struct idxd_device *idxd)
 	}
 	dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
 
+	INIT_WORK(&idxd->int_handle_revoke_work, idxd_int_handle_revoke);
 
 	ie = idxd_get_ie(idxd, 0);
 	ie->vector = pci_irq_vector(pdev, 0);
drivers/dma/idxd/irq.c +4 -23
index 6a25e1fd0e62..6d3355ce88f7 100644
@@ -24,11 +24,6 @@ struct idxd_resubmit {
 	struct idxd_desc *desc;
 };
 
-struct idxd_int_handle_revoke {
-	struct work_struct work;
-	struct idxd_device *idxd;
-};
-
 static void idxd_device_reinit(struct work_struct *work)
 {
 	struct idxd_device *idxd = container_of(work, struct idxd_device, work);
@@ -127,17 +122,15 @@ static void idxd_abort_invalid_int_handle_descs(struct idxd_irq_entry *ie)
 	}
 }
 
-static void idxd_int_handle_revoke(struct work_struct *work)
+void idxd_int_handle_revoke(struct work_struct *work)
 {
-	struct idxd_int_handle_revoke *revoke =
-		container_of(work, struct idxd_int_handle_revoke, work);
-	struct idxd_device *idxd = revoke->idxd;
+	struct idxd_device *idxd =
+		container_of(work, struct idxd_device, int_handle_revoke_work);
 	struct pci_dev *pdev = idxd->pdev;
 	struct device *dev = &pdev->dev;
 	int i, new_handle, rc;
 
 	if (!idxd->request_int_handles) {
-		kfree(revoke);
 		dev_warn(dev, "Unexpected int handle refresh interrupt.\n");
 		return;
 	}
@@ -216,7 +209,6 @@ static void idxd_int_handle_revoke(struct work_struct *work)
 			udelay(100);
 		idxd_int_handle_revoke_drain(ie);
 	}
-	kfree(revoke);
 }
 
 static void idxd_evl_fault_work(struct work_struct *work)
@@ -504,20 +496,9 @@ irqreturn_t idxd_misc_thread(int vec, void *data)
 	}
 
 	if (cause & IDXD_INTC_INT_HANDLE_REVOKED) {
-		struct idxd_int_handle_revoke *revoke;
-
 		val |= IDXD_INTC_INT_HANDLE_REVOKED;
 
-		revoke = kzalloc_obj(*revoke, GFP_ATOMIC);
-		if (revoke) {
-			revoke->idxd = idxd;
-			INIT_WORK(&revoke->work, idxd_int_handle_revoke);
-			queue_work(idxd->wq, &revoke->work);
-
-		} else {
-			dev_err(dev, "Failed to allocate work for int handle revoke\n");
-			idxd_wqs_quiesce(idxd);
-		}
+		queue_work(idxd->wq, &idxd->int_handle_revoke_work);
 	}
 
 	if (cause & IDXD_INTC_CMD) {
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] dmaengine: idxd: active work reinit and missing cancel on interrupt setup --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] dmaengine: idxd: active work reinit and missing cancel on interrupt setup
--

commit e10f41baa968851823cafa610d6872d2435dbbf7
Author: Ziyu Zhang <[email protected]>

dmaengine: idxd: Fix deadlock on interrupt handle revocation

This commit embeds int_handle_revoke_work in struct idxd_device and queues it unconditionally on IDXD_INTC_INT_HANDLE_REVOKED, removing the dynamic work allocation and synchronous idxd_wqs_quiesce() fallback.

drivers/dma/idxd/init.c static int idxd_setup_interrupts(struct idxd_device *idxd) · line 114

Quoted 12 lines
drivers/dma/idxd/init.c +1 -0
index 4b827a3297564..f3ade8036ddb7 100644
@@ -108,6 +108,7 @@ static int idxd_setup_interrupts(struct idxd_device *idxd)
 	}
 	dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
 
+	INIT_WORK(&idxd->int_handle_revoke_work, idxd_int_handle_revoke);
 
 	ie = idxd_get_ie(idxd, 0);
 	ie->vector = pci_irq_vector(pdev, 0);

[Severity: High]
Can idxd_setup_interrupts() reinitialize idxd->int_handle_revoke_work while it is pending or running, and does idxd_cleanup_interrupts() leave it uncancelled?

idxd_setup_interrupts() also runs during FLR recovery via idxd_reset_done() -> idxd_pci_probe_alloc(idxd, NULL, NULL), which calls idxd_cleanup_interrupts() and idxd_setup_interrupts() back-to-back on an existing device without cancelling int_handle_revoke_work:

drivers/dma/idxd/init.c:idxd_pci_probe_alloc() {
    ...
	if (!alloc_idxd) {
		/* Release interrupts in the IDXD device. */
		idxd_cleanup_interrupts(idxd);

		/* Re-enable interrupts in the IDXD device. */
		rc = idxd_setup_interrupts(idxd);
    ...
}

kworker (idxd->wq) <irq thread> (idxd_misc_thread)
idxd_device_flr()

                                  cause & IDXD_INTC_INT_HANDLE_REVOKED
                                  queue_work(idxd->wq,
                                             &idxd->int_handle_revoke_work)
  pci_reset_function()
    ...
      idxd_reset_done()
        idxd_pci_probe_alloc()
          idxd_cleanup_interrupts()
            free_irq(ie->vector, ie)
          idxd_setup_interrupts()
            INIT_WORK(&idxd->int_handle_revoke_work, ...)
              <-- reinitializes queued work, corrupting pwq->worklist

Calling INIT_WORK() on a queued work item corrupts pwq->worklist and clears WORK_STRUCT_PWQ in work->data, leading to a NULL pointer dereference in process_one_work(). Because idxd_cleanup_interrupts() does not cancel the work after free_irq(), a pending or running idxd_int_handle_revoke() can also hang waiting on idxd->cmd_done after the misc IRQ is freed, or access idxd->wqs and idxd->reg_base after they are torn down in idxd_cleanup_internals() or idxd_remove().

Initializing idxd->int_handle_revoke_work once during device allocation (such as in idxd_setup_internals()) and calling cancel_work_sync(&idxd->int_handle_revoke_work) after free_irq() in idxd_cleanup_interrupts() would avoid both problems.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1