dmaengine: idxd: event log, cdev and prep fixes
Subsystems (3)
v2 cover dmaengine: idxd: event log, cdev and prep fixes
These are fixes for bugs in the idxd driver that an in-kernel polling client exposed while sharing DSA work queues between many users. That client, a DMA copy offload engine, is not posted yet, and these fixes do not depend on it. They apply to v7.4-rc2. Every patch has a Fixes tag and a Cc: stable line with the first affected release.
v1 was one series of 17 patches. v2 is posted as three series:
- "dmaengine: idxd: event log, cdev and prep fixes", this series:
ten fixes that stand alone. They apply in order, since some of them touch the same lines, but none needs another to work, nor either of the other two series;
- "dmaengine: fixes for channels registered and removed at run time", 4 patches to the dmaengine core and to idxd's use of it, posted with this one;
- "dmaengine: idxd: fixes found by an in-kernel polling client", on the device commands, terminate_all, the teardown, halts and resets. It applies on top of this series and is not posted yet, see "Patches on the list" below.
In short:
- the event-log fault path dereferences a NULL workqueue on a kernel WQ (1), and the user WQ's workqueue is detached under the event log lock (2);
- a memcpy longer than the work queue's transfer limit is accepted at prep and fails silently in hardware (4);
- event log: the fault work item leaked when there is no record to copy (3), a reference on the mm around the completion-record copy (5), GENCFG read before the event log enable is cleared (6);
- the prep callbacks no longer sleep for a descriptor (7); it was carried by v1 of the polled-status series and belongs here;
- three bugs in the code next to these, new in this posting: the event log is allocated by a size that is read before its lock is taken (8), the queue enable leaves the interrupt requested when its last step fails (9), and the quiesce of all kernel queues tests a queue without its lock and can kill a reference that is not there (10).
Link to v1: https://lore.kernel.org/dmaengine/[email protected]/
Changes since v1:
v1's 17 patches were reviewed by Frank Li, by sashiko.dev and by a local run of the same reviewer. Frank's Reviewed-by is on patch 4. v1's patches 1, 2 and 3 are patches 1, 2 and 4 here, and the other seven are new. The per-patch changes are under the --- line of each patch.
What the reviews found, as far as it concerns this series:
- sashiko.dev had no finding on v1's patches 1, 2 and 3. One of its findings on v1's patch 10 was a bug that v7.3 has already, the completion record copied into an mm without a users reference. Patch 5 fixes it.
- The local run found the leak that patch 3 fixes in its review of v1's patches 1 and 2, and with it the warning of patch 1, which called every queue it dropped an entry for a kernel queue. It found the GENCFG write that patch 6 fixes in its review of v1's patch 8.
- The v2 patches were reviewed by a local run of the same reviewer as well. In its reviews of the third series it reported bugs that those patches did not introduce and that v7.4-rc2 has. Patches 8, 9 and 10 fix three of them. Two more are in the error path of idxd_cdev_open(), and a patch from the list fixes those, see "Patches on the list" below. Its last complete run reports no issue on patches 1 to 9. On patch 10 it reports no bug that the patch introduces. It reports two in the code around it that v7.4-rc2 has, the second kill of a queue's reference and the handle revoke's test of a queue without its lock. The third series fixes both.
Patches on the list:
Vinicius Costa Gomes' "dmaengine: idxd: More error handling fixes" v2 is on the list and not applied yet. This series applies with it or without it.
- Its patch 5, "dmaengine: idxd: Fix error handling in idxd_cdev_open()", fixes the open that returns success with its context freed and the PASID that stays on a dedicated queue. The local reviews of this series found both, and they are left to that patch.
- Its patches 7 and 8 fix terminate_all. Two patches of the third series, which were patches 6 and 9 of v1, fix the same two problems. The third series follows when that overlap is resolved.
https://lore.kernel.org/r/[email protected]
Ziyu Zhang's "dmaengine: idxd: Fix deadlock on interrupt handle revocation" v2 removes the call of idxd_wqs_quiesce() from the handle revoke path. Patch 10 fixes that function for its other caller, the halt handler, and applies with that patch or without it.
https://lore.kernel.org/r/[email protected]
Testing:
The three series were tested as one stack, and were one series for most of the rounds. The cover letter of the third, "dmaengine: idxd: fixes found by an in-kernel polling client", will describe them: a test module's scenarios on a dmaengine channel, dmatest, iaa_crypto with zswap, user queues, resets and halts, under KASAN with lockdep and DMA-API debugging and under KCSAN, on v7.3 and on v7.4-rc2. Every scenario passes.
Of this series those rounds run the prep callbacks on an exhausted pool (7), which the scenarios that fill the queue reach, and the setup and the free of the event log on every enable and disable of a device (6, 8). They do not reach the event log fault paths (1, 2, 3, 5), which need a user process whose completion record faults, the refusal in patch 4, the write of the event log size that patch 8 refuses, the error path of patch 9, or the quiesce of patch 10, which needs a system-reset halt or a revoked interrupt handle. Those paths did not run in these rounds.
Tools:
I used Claude Opus/Fable model to review (sashiko and local reviews) the code, suggest fixes, and edit the changelogs. I also used it to run the builds and the test rounds described above. I reviewed the results and fixes it suggested and I am responsible for them.
Daniel Byrne (10):
dmaengine: idxd: don't queue EVL fault work for kernel WQs
dmaengine: idxd: detach the user fault workqueue under the event log
lock
dmaengine: idxd: free the event log fault item when no record is
copied
dmaengine: idxd: refuse a memcpy longer than the WQ max transfer size
dmaengine: idxd: take an mm_users reference for the completion record
copy
dmaengine: idxd: read GENCFG before clearing the event log enable
dmaengine: idxd: don't sleep for a descriptor in the prep callbacks
dmaengine: idxd: read the event log size under its lock in the setup
dmaengine: idxd: free the queue interrupt when the percpu_ref setup
fails
dmaengine: idxd: test a queue under its lock before quiescing it
drivers/dma/idxd/cdev.c | 60 +++++++++++++++++++++++++++++++++------ drivers/dma/idxd/device.c | 15 +++++++++- drivers/dma/idxd/dma.c | 9 ++++-- drivers/dma/idxd/init.c | 20 ++++++++++++- drivers/dma/idxd/irq.c | 25 ++++++++++++++-- drivers/dma/idxd/sysfs.c | 10 +++++++ 6 files changed, 123 insertions(+), 16 deletions(-)
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
Signature · 1 line
2.43.0
PATCH v2 1/10 dmaengine: idxd: don't queue EVL fault work for kernel WQs
The event log fault path queues work on wq->wq, the workqueue that only the user (cdev) driver creates for its work queues. A completion record fault logged for a kernel work queue, a dmaengine or iaa_crypto queue, finds that pointer NULL and queue_work() dereferences it.
The fault worker only serves user contexts. It copies the completion record through the PASID xarray that cdev opens populate. On a kernel work queue there is nothing to do, so log the entry rate-limited and drop it. Test the queue's type and not the pointer alone, since a user probe that failed leaves the pointer dangling. The warning names the queue's type, because the same test drops an entry for a queue whose user driver was unbound after the entry was logged, and for a queue retyped to user while disabled.
---
v2: the test is unchanged. The warning no longer says "kernel wq" and prints the queue's type, since the same test also drops the record of a user queue whose cdev driver is not bound; the comment says so, and the message adds a queue retyped to user while disabled. The wq declaration moves to the top of the block.
drivers/dma/idxd/irq.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
drivers/dma/idxd/irq.c +17 -2
@@ -322,6 +322,7 @@ static void process_evl_entry(struct idxd_device *idxd,if (status ==DSA_COMP_CRA_XLAT || status ==DSA_COMP_DRAIN_EVL || status ==DSA_COMP_BATCH_EVL_ERR ) { +struct idxd_wq *wq = idxd->wqs[entry_head->wq_idx];struct idxd_evl_fault *fault;int ent_size =evl_ent_size (idxd);
@@ -331,10 +332,24 @@ static void process_evl_entry(struct idxd_device *idxd,if (!entry_head->rcr && status ==DSA_COMP_DRAIN_EVL )return ; +/* +* The fault worker only does user-context work, +* copying the completion record through the wq's +* PASID xarray, which is populated by cdev opens +* alone. A kernel WQ, or a user WQ whose cdev driver +* is not bound, has no cdev workqueue and no user +* contexts, so queueing could only warn and drop the +* record. Note it and move on. +*/ +if (!is_idxd_wq_user (wq) || !wq->wq) { +dev_warn_ratelimited (dev, +"EVL fault on wq %d with no user fault workqueue (type %u), status %#x\n" , + wq->id, wq->type, status); +return ; + } + fault =kmem_cache_alloc (idxd->evl_cache,GFP_ATOMIC );if (fault) { -struct idxd_wq *wq = idxd->wqs[entry_head->wq_idx]; - fault->wq = wq; fault->status = status;memcpy (&fault->entry, entry_head, ent_size);
Signature · 1 line
2.43.0
PATCH v2 2/10 dmaengine: idxd: detach the user fault workqueue under the event log lock
In reply to Daniel ByrneThe event log walk tests wq->wq and queues fault work on it, and the drain of a closing context's PASID tests it and drains it, both under evl->lock. The user driver's remove destroyed the workqueue without that lock, so a walk could queue on a workqueue being destroyed. The probe's error path destroyed it without clearing the pointer, so a later drain found a dangling one. That happens when a context's file stays open across an unbind, a rebind's probe fails after it created the workqueue, and the file then closes.
Detach and destroy the workqueue under evl->lock, in one helper that the remove and the probe error path share, and clear the pointer in both. A walk or a drain that finds it gone knows that the fault work it would have waited for has run.
The probe attaches the workqueue and sets the type under the same lock, so that a walk sees them together.
---
v2: idxd_user_drv_probe() creates the workqueue into a local and stores wq->wq and the IDXD_WQT_USER type together under evl->lock, the lock they are detached under, so that a walk sees the two together; the message gives the sequence that leaves a drain with the dangling pointer (a file kept open across an unbind, a rebind whose probe fails after it created the workqueue, then the close) and no longer says that a walk finds it; Fixes and Cc: stable tags added.
drivers/dma/idxd/cdev.c | 42 ++++++++++++++++++++++++++++++++++------- drivers/dma/idxd/irq.c | 5 ++++- 2 files changed, 39 insertions(+), 8 deletions(-)
drivers/dma/idxd/cdev.c +35 -7
@@ -592,11 +592,33 @@ void idxd_wq_del_cdev(struct idxd_wq *wq)put_device (cdev_dev (idxd_cdev)); } +/* +* The event log walk tests the fault workqueue and queues on it, and the +* drain of a closing context's PASID tests it and drains it, both under +* the log's lock, which neither the probe nor the remove otherwise takes. +* The workqueue is therefore detached and destroyed under that lock. A +* walk or a drain that finds it gone knows that its work has run. +*/ +static void idxd_user_wq_destroy_fault_wq (struct idxd_wq *wq) +{ +struct idxd_evl *evl = wq->idxd->evl; + +if (evl) +mutex_lock (&evl->lock); + wq->type =IDXD_WQT_NONE ; +destroy_workqueue (wq->wq); + wq->wq =NULL ; +if (evl) +mutex_unlock (&evl->lock); +} +static int idxd_user_drv_probe (struct idxd_dev *idxd_dev) {struct device *dev = &idxd_dev->conf_dev;struct idxd_wq *wq =idxd_dev_to_wq (idxd_dev);struct idxd_device *idxd = wq->idxd; +struct idxd_evl *evl = idxd->evl; +struct workqueue_struct *fwq;int rc;if (idxd->state !=IDXD_DEV_ENABLED )
@@ -629,13 +651,22 @@ static int idxd_user_drv_probe(struct idxd_dev *idxd_dev) goto wq_err; } - wq->wq =create_workqueue (dev_name (wq_confdev (wq))); -if (!wq->wq) { + fwq =create_workqueue (dev_name (wq_confdev (wq))); +if (!fwq) { rc = -ENOMEM ; goto wq_err; } +/* +* Attached under the lock it is detached under, so that a walk sees +* the workqueue and the type together. +*/ +if (evl) +mutex_lock (&evl->lock); + wq->wq = fwq; wq->type =IDXD_WQT_USER ; +if (evl) +mutex_unlock (&evl->lock); rc =idxd_drv_enable_wq (wq);if (rc <0 ) goto err;
@@ -653,8 +684,7 @@ static int idxd_user_drv_probe(struct idxd_dev *idxd_dev) err_cdev:idxd_drv_disable_wq (wq); err: -destroy_workqueue (wq->wq); - wq->type =IDXD_WQT_NONE ; +idxd_user_wq_destroy_fault_wq (wq); wq_err:mutex_unlock (&wq->wq_lock);return rc;
@@ -667,9 +697,7 @@ static void idxd_user_drv_remove(struct idxd_dev *idxd_dev)mutex_lock (&wq->wq_lock);idxd_wq_del_cdev (wq);idxd_drv_disable_wq (wq); - wq->type =IDXD_WQT_NONE ; -destroy_workqueue (wq->wq); - wq->wq =NULL ; +idxd_user_wq_destroy_fault_wq (wq);mutex_unlock (&wq->wq_lock); }
drivers/dma/idxd/irq.c +4 -1
@@ -339,7 +339,10 @@ static void process_evl_entry(struct idxd_device *idxd,* alone. A kernel WQ, or a user WQ whose cdev driver * is not bound, has no cdev workqueue and no user * contexts, so queueing could only warn and drop the -* record. Note it and move on. +* record. Note it and move on. The user driver +* detaches the workqueue under evl->lock, which this +* walk holds, so the one tested here is the one +* queued on. */ if (!is_idxd_wq_user (wq) || !wq->wq) { dev_warn_ratelimited(dev,
Signature · 1 line
2.43.0
PATCH v2 3/10 dmaengine: idxd: free the event log fault item when no record is copied
In reply to Daniel Byrneidxd_evl_fault_work() returns without freeing the fault object when a batch event log error entry has no completion record to copy, because the batch descriptor did not request one and no earlier fault in the batch failed its copy. That is the only path where the worker has nothing to do, and the only free of the object is at the end of the function, so one evl_cache object the size of an event log entry leaks per such entry. A user of the work queue drives it at submission rate, and kmem_cache_destroy() complains about the live objects when the device is unbound.
Free the object on that path too.
---
New in v2.
drivers/dma/idxd/irq.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma/idxd/irq.c +2 -1
@@ -264,7 +264,7 @@ static void idxd_evl_fault_work(struct work_struct *work) }if (copy_size ==0 ) -return ; + goto out;/* * Copy completion record to fault_addr in user address space
@@ -305,6 +305,7 @@ static void idxd_evl_fault_work(struct work_struct *work)break ; } +out:kmem_cache_free (idxd->evl_cache, fault); }
Signature · 1 line
2.43.0
PATCH v2 4/10 dmaengine: idxd: refuse a memcpy longer than the WQ max transfer size
In reply to Daniel Byrneidxd_dma_submit_memcpy() checks the length only against the device limit from GENCAP. Since commit d7aad5550eca ("dmaengine: idxd: add support for configurable max wq xfer size"), WQCFG is programmed with the work queue's own max_transfer_size instead, and that is the limit the hardware enforces. Since commit 92452a72ebdf ("dmaengine: idxd: set defaults for wq configs"), that limit defaults to 2 MiB.
A copy longer than the work queue's limit is therefore accepted at prep time and then fails in hardware with DSA_COMP_XFER_ERANGE. A client that sets only tx->callback cannot see that error, so the copy looks complete. Also check the work queue's limit, so that prep returns NULL and the caller can fall back.
On Granite Rapids DSA 2.0 with the default 2 MiB work queue limit and a device limit of 2 GiB, dmatest with norandom=1 and test_buf_size=4194304 reports a data error for every copy on v7.2. The destination is never written, yet each copy completes. With this change the same test fails at prep, and copies of 1 MiB and 2 MiB pass as before.
---
v2: code unchanged; Reviewed-by from Frank Li added; the message names the test device as Granite Rapids DSA 2.0.
drivers/dma/idxd/dma.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma/idxd/dma.c +2 -1
@@ -115,7 +115,8 @@ idxd_dma_submit_memcpy(struct dma_chan *c, dma_addr_t dma_dest,if (wq->state !=IDXD_WQ_ENABLED )return NULL ; -if (len > idxd->max_xfer_bytes) +/* The work queue's configured limit is what the hardware enforces. */ +if (len > idxd->max_xfer_bytes || len > wq->max_xfer_bytes)return NULL ;op_flag_setup (flags, &desc_flags);
Signature · 1 line
2.43.0
PATCH v2 5/10 dmaengine: idxd: take an mm_users reference for the completion record copy
In reply to Daniel ByrneThe user context records the opener's mm without a users reference. On exit, exit_mm() runs before the files are closed, so the context's release, which removes it from the work queue's PASID xarray, runs after the address space is torn down, and the drain the release issues completes the context's in-flight descriptors. A completion record write of one of them faults, and the event log fault work finds the context in the xarray and calls kthread_use_mm() on an mm whose users count is zero to copy the record into it. The mm_struct itself is kept by the SVA binding, but an address space in that state must not be used. The copy normally fails, and when it races with exit_mmap() it can install page table entries that free_pgtables() abandons.
Take a users reference for the copy with mmget_not_zero(), and skip the copy when the address space is gone, which counts as a failed copy. Remove the context from the xarray before unbinding the SVA handle, so that the mm_struct is alive whenever the fault work can find the context.
---
New in v2.
drivers/dma/idxd/cdev.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/dma/idxd/cdev.c +17 -1
@@ -6,6 +6,7 @@#include <linux/pci.h> #include <linux/device.h> #include <linux/sched/task.h> +#include <linux/sched/mm.h> #include <linux/io-64-nonatomic-lo-hi.h> #include <linux/cdev.h> #include <linux/fs.h>
@@ -140,8 +141,13 @@ static void idxd_file_dev_release(struct device *dev)if (ctx->sva) {idxd_cdev_evl_drain_pasid (wq, ctx->pasid); -iommu_sva_unbind_device (ctx->sva); +/* +* Once the context is no longer in the xarray no fault work +* can find it. The SVA binding keeps its mm_struct alive +* until then. +*/ idxd_xa_pasid_remove (ctx); +iommu_sva_unbind_device (ctx->sva); }kfree (ctx);mutex_lock (&wq->wq_lock);
@@ -774,7 +780,16 @@ int idxd_copy_cr(struct idxd_wq *wq, ioasid_t pasid, unsigned long addr, goto out; } +/* +* The context records the opener's mm without a users reference of +* its own. The file's release, which removes the context from the +* xarray, runs after exit_mm() when the opener exits. Take one for +* the copy, and skip it if the address space is already gone. +*/ mm = ctx->mm; +if (!mmget_not_zero (mm)) + goto out; +/* * The completion record fault handling work is running in kernel * thread context. It temporarily switches to the mm to copy cr
@@ -805,6 +820,7 @@ int idxd_copy_cr(struct idxd_wq *wq, ioasid_t pasid, unsigned long addr, left += status_size; }kthread_unuse_mm (mm); +mmput (mm); out:mutex_unlock (&wq->uc_lock);
Signature · 1 line
2.43.0
PATCH v2 6/10 dmaengine: idxd: read GENCFG before clearing the event log enable
In reply to Daniel Byrneidxd_device_evl_free() clears the event log enable in a GENCFG value it never read. The other 31 bits it writes back are whatever the stack held, whereas the setup path and the GENCTRL update three lines below read the register first. Every unbind of the device driver, and both of its probe error paths, therefore program an arbitrary read buffer limit and arbitrary reserved bits. The limit is rewritten on the next enable only when one was configured, so with the default of none the arbitrary one stays in effect until the next device reset. The read was dropped with the evl_en test on the remove path when the log's memory leak was fixed.
Read the register before changing the bit.
---
New in v2.
drivers/dma/idxd/device.c | 1 + 1 file changed, 1 insertion(+)
drivers/dma/idxd/device.c +1 -0
@@ -835,6 +835,7 @@ static void idxd_device_evl_free(struct idxd_device *idxd)return ;mutex_lock (&evl->lock); + gencfg.bits =ioread32 (idxd->reg_base +IDXD_GENCFG_OFFSET ); gencfg.evl_en =0 ;iowrite32 (gencfg.bits, idxd->reg_base +IDXD_GENCFG_OFFSET );
Signature · 1 line
2.43.0
PATCH v2 7/10 dmaengine: idxd: don't sleep for a descriptor in the prep callbacks
In reply to Daniel ByrneThe dmaengine prep callbacks may be called from atomic context, and ntb_transport calls idxd's memcpy prep from its receive tasklet and from ndo_start_xmit. Both idxd prep callbacks allocate their descriptor with IDXD_OP_BLOCK, which sleeps on the sbitmap wait queue while every slot of the work queue is in flight, so a queue pair that outruns the work queue schedules in atomic context.
Allocate with IDXD_OP_NONBLOCK and return NULL on an exhausted pool, as ioat, plx_dma and mv_xor do when their rings are full. ntb_transport falls back to a CPU copy. A polled client needs this as well. Only its own query frees a polled descriptor's slot, so a prep that slept for a slot while that client held the whole pool would wait on itself.
Clients that can sleep and do not fall back see the change. dmatest reports a prep error, sleeps 100 ms and counts a failure when threads_per_chan exceeds the queue's descriptor count, which is the size of a dedicated queue and the threshold of a shared one. It can also do so with exactly as many threads as descriptors, when a thread that its callback woke prepares again before the interrupt thread has freed that callback's slot. ntb_perf sleeps 10 ms each time it overruns the pool, which caps its reported bandwidth at about the pool size times the chunk size per 10 ms. iaa_crypto allocates outside the prep callbacks and keeps the blocking allocation.
---
New in v2 (was carried by v1 of the polled-status series).
drivers/dma/idxd/dma.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/dma/idxd/dma.c +4 -2
@@ -93,7 +93,8 @@ idxd_dma_prep_interrupt(struct dma_chan *c, unsigned long flags)return NULL ;op_flag_setup (flags, &desc_flags); - desc =idxd_alloc_desc (wq,IDXD_OP_BLOCK ); +/* Prep may run in atomic context: fail rather than wait for a slot. */ + desc =idxd_alloc_desc (wq,IDXD_OP_NONBLOCK );if (IS_ERR (desc))return NULL ;
@@ -120,7 +121,8 @@ idxd_dma_submit_memcpy(struct dma_chan *c, dma_addr_t dma_dest,return NULL ;op_flag_setup (flags, &desc_flags); - desc =idxd_alloc_desc (wq,IDXD_OP_BLOCK ); +/* Prep may run in atomic context: fail rather than wait for a slot. */ + desc =idxd_alloc_desc (wq,IDXD_OP_NONBLOCK );if (IS_ERR (desc))return NULL ;
Signature · 1 line
2.43.0
PATCH v2 8/10 dmaengine: idxd: read the event log size under its lock in the setup
In reply to Daniel Byrneidxd_device_evl_setup() allocates the event log and its bitmap by evl_size(), which it reads before it takes the event log lock, and then gives the device evl->size, read again under the lock. event_log_size can be written until the device is enabled, which comes after the setup, and the store takes no lock. A write between the two reads gives the device a log larger than the memory behind it, which it writes past once it has logged more entries than the old size holds. A write after the setup changes the size by which the log is walked.
Take the lock before the size is read and hold it to the end of the setup, write the size under the same lock, and refuse the write with -EBUSY once there is a log. The log keeps the size it was allocated with until idxd_device_evl_free() takes it away.
---
New in v2.
drivers/dma/idxd/device.c | 8 +++++++- drivers/dma/idxd/sysfs.c | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-)
drivers/dma/idxd/device.c +7 -1
@@ -773,6 +773,12 @@ static int idxd_device_evl_setup(struct idxd_device *idxd)if (!evl)return 0 ; +/* +* event_log_size_store() writes the size under this lock, and only +* while there is no log. So the size read here is the one the log +* is allocated by and the one the device is given. +*/ +mutex_lock (&evl->lock); size =evl_size (idxd); bmap =bitmap_zalloc (size,GFP_KERNEL );
@@ -791,7 +797,6 @@ static int idxd_device_evl_setup(struct idxd_device *idxd) goto err_alloc; } -mutex_lock (&evl->lock); evl->log = addr; evl->dma = dma_addr; evl->log_size = size;
@@ -818,6 +823,7 @@ static int idxd_device_evl_setup(struct idxd_device *idxd) err_alloc:bitmap_free (bmap); err_bmap: +mutex_unlock (&evl->lock);return rc; }
drivers/dma/idxd/sysfs.c +10 -0
@@ -1708,7 +1708,17 @@ static ssize_t event_log_size_store(struct device *dev, (val * evl_ent_size(idxd) > ULONG_MAX - idxd->evl->dma)) return -EINVAL; +/* +* idxd_device_evl_setup() reads the size under this lock. Once a +* log is set up it keeps the size it was allocated with. +*/ +mutex_lock (&idxd->evl->lock); +if (idxd->evl->log) { +mutex_unlock (&idxd->evl->lock); +return -EBUSY ; + } idxd->evl->size = val; +mutex_unlock (&idxd->evl->lock);return count; }static DEVICE_ATTR_RW (event_log_size);
Signature · 1 line
2.43.0
PATCH v2 9/10 dmaengine: idxd: free the queue interrupt when the percpu_ref setup fails
In reply to Daniel ByrneWhen idxd_wq_init_percpu_ref() fails, idxd_drv_enable_wq() unwinds through idxd_wq_free_resources() and then idxd_wq_free_irq(). Since commit d9cfb5193a04 ("dmaengine: idxd: Fix memory leak when a wq is reset") the first clears the queue's type, and the second returns at once for a queue whose type is not kernel. The interrupt stays requested with the queue's interrupt entry as its cookie, and on a device that uses interrupt handles the handle is not released. The next enable of the queue fails to request the vector.
Free the interrupt first on that path.
---
New in v2.
drivers/dma/idxd/device.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/dma/idxd/device.c +6 -0
@@ -1531,7 +1531,13 @@ int idxd_drv_enable_wq(struct idxd_wq *wq)return 0 ; err_ref: +/* +* idxd_wq_free_irq() goes first: it returns at once for a queue +* whose type idxd_wq_free_resources() has cleared. +*/ +idxd_wq_free_irq (wq);idxd_wq_free_resources (wq); + goto err_irq; err_res_alloc:idxd_wq_free_irq (wq); err_irq:
Signature · 1 line
2.43.0
PATCH v2 10/10 dmaengine: idxd: test a queue under its lock before quiescing it
In reply to Daniel Byrneidxd_wqs_quiesce() tests a queue's state and type without wq_lock and takes the lock only for the quiesce itself. The enable and the disable of a queue run under that lock from end to end, so the test can pass while one of them is under way, and the quiesce then runs once it is through. An enable that failed after it had stored the queue enabled never initialized wq_active, and a disable has released it by then. Either way percpu_ref_kill() works on a reference that has no data and dereferences NULL.
The halt handler calls this for a halt that a system reset recovers from, and the interrupt thread calls it when it cannot allocate the work item for a revoked interrupt handle.
Test the queue again once the lock is held. A queue that reads enabled there has its reference. The test without the lock stays, as a filter that keeps the thread off a lock it must not wait for. Both callers run in the interrupt thread that also completes the device commands, and a queue's lock is held across such commands. The enable of a kernel queue and a cdev open issue theirs under the lock of a queue that does not read as an enabled kernel queue yet, so the filter skips that queue and the thread never waits for its lock. Taking every queue's lock here would let the thread and such a waiter wait for each other. An unbind issues its commands under the lock of a queue that does read as one, and the thread waits behind it, as it did before.
---
New in v2.
drivers/dma/idxd/init.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/dma/idxd/init.c +19 -1
@@ -1242,8 +1242,26 @@ void idxd_wqs_quiesce(struct idxd_device *idxd)for (i =0 ; i < idxd->max_wqs; i++) { wq = idxd->wqs[i]; +/* +* Skip a queue that does not read as an enabled kernel +* queue without taking its lock. This runs in the thread +* that completes the device commands, and the enable of a +* kernel queue and a cdev open hold the lock of such a +* queue while they wait for a command. An unbind holds the +* lock of an enabled kernel queue across its commands, and +* this waits behind it. +*/ +if (wq->state !=IDXD_WQ_ENABLED || wq->type !=IDXD_WQT_KERNEL ) +continue ; +/* +* Test again under the lock. The enable and the disable of +* a queue hold it from end to end, so a queue that reads +* enabled here has its reference. +*/ +mutex_lock (&wq->wq_lock);if (wq->state ==IDXD_WQ_ENABLED && wq->type ==IDXD_WQT_KERNEL ) -idxd_wq_quiesce (wq); +__idxd_wq_quiesce (wq); +mutex_unlock (&wq->wq_lock); } }
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 10/10] dmaengine: idxd: test a queue under its lock before quiescing it
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] dmaengine: idxd: deadlock on wq_lock vs cmd_done in idxd_wqs_quiesce() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] dmaengine: idxd: deadlock on wq_lock vs cmd_done in idxd_wqs_quiesce()
--
commit 36a9678a1a099380b2c088bef490159f8c968311
Author: Daniel Byrne <[email protected]>
dmaengine: idxd: test a queue under its lock before quiescing it
This commit re-checks wq->state and wq->type under wq->wq_lock in idxd_wqs_quiesce() before calling __idxd_wq_quiesce(), while keeping the unlocked check ahead of mutex_lock(&wq->wq_lock).
Quoted 21 lines
drivers/dma/idxd/init.c +13
-0@@ -1242,8 +1242,26 @@ void idxd_wqs_quiesce(struct idxd_device *idxd)for (i =0 ; i < idxd->max_wqs; i++) { wq = idxd->wqs[i]; +/* +* Skip a queue that does not read as an enabled kernel +* queue without taking its lock. This runs in the thread +* that completes the device commands, and the enable of a +* kernel queue and a cdev open hold the lock of such a +* queue while they wait for a command. An unbind holds the +* lock of an enabled kernel queue across its commands, and +* this waits behind it. +*/ +if (wq->state !=IDXD_WQ_ENABLED || wq->type !=IDXD_WQT_KERNEL ) +continue ; +/* +* Test again under the lock. The enable and the disable of
drivers/dma/idxd/init.c void idxd_wqs_quiesce(struct idxd_device *idxd) · line 1266
Quoted 10 lines
drivers/dma/idxd/init.c +6
-1+* a queue hold it from end to end, so a queue that reads +* enabled here has its reference. +*/ +mutex_lock (&wq->wq_lock);if (wq->state ==IDXD_WQ_ENABLED && wq->type ==IDXD_WQT_KERNEL ) -idxd_wq_quiesce (wq); +__idxd_wq_quiesce (wq); +mutex_unlock (&wq->wq_lock); } }
[Severity: Medium]
Can idxd_misc_thread() still deadlock on wq->wq_lock against a command waiter in idxd_drv_enable_wq() or idxd_drv_disable_wq()?
idxd_drv_enable_wq() sets wq->state = IDXD_WQ_ENABLED inside idxd_wq_enable() while wq->type is already IDXD_WQT_KERNEL, and then issues IDXD_CMD_REQUEST_INT_HANDLE via idxd_wq_request_irq() (and IDXD_CMD_DRAIN_WQ / IDXD_CMD_RESET_WQ on its err_map_portal unwind path) while still holding wq->wq_lock. Likewise, idxd_drv_disable_wq() holds wq->wq_lock on an enabled kernel queue while issuing drain and reset commands.
Because those commands wait in idxd_cmd_exec() for idxd_misc_thread() to complete idxd->cmd_done, the unlocked filter does not skip the queue and idxd_misc_thread() blocks on wq->wq_lock before reaching the IDXD_INTC_CMD check:
CPU0 CPU1 (idxd_misc_thread)idxd_drv_enable_wq()
mutex_lock(&wq->wq_lock)
idxd_wq_enable()
wq->state = IDXD_WQ_ENABLED
idxd_wq_request_irq()
...
idxd_cmd_exec()
idxd_halt()
idxd_wqs_quiesce()
mutex_lock(&wq->wq_lock)
wait_for_completion(&done) <-- deadlock
complete(idxd->cmd_done) (never reached)Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Re: [PATCH v2 10/10] dmaengine: idxd: test a queue under its lock before quiescing it
Quoted 2 lines · On Mon, 30 Nov 2026 20:09:29 +0000, [email protected] wrote:
Can
idxd_misc_thread()still deadlock onwq->wq_lockagainst a command waiter inidxd_drv_enable_wq()oridxd_drv_disable_wq()?
Yes, it can, and it could before this patch. v7.4-rc2 makes the same test without the lock and then calls idxd_wq_quiesce(), which takes wq_lock, from the same thread. The patch does not change which queues the thread waits for. It repeats the test once the lock is held.
The changelog and the comment say so for the unbind. They are wrong about the enable. Only the enable command itself runs while the queue does not read as an enabled kernel queue yet. What idxd_drv_enable_wq() issues after it, the interrupt handle request and the commands of its error path, runs under wq_lock with the queue reading enabled, and the thread waits behind those too. I'll correct both texts in the next version.
This series does not close the wait. Ziyu Zhang's patch takes the call out of the handle revoke path, which leaves the halt handler as the only caller:
https://lore.kernel.org/r/[email protected]
Thanks,
Daniel