mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/4] dmaengine: fixes for channels registered and removed at run time
@ 2026-11-30 19:51 Daniel Byrne
  2026-11-30 19:52 ` [PATCH v2 1/4] dmaengine: put a channel device whose registration failed Daniel Byrne
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Daniel Byrne @ 2026-11-30 19:51 UTC (permalink / raw)
  To: Vinod Koul, dmaengine
  Cc: Vinicius Costa Gomes, Dave Jiang, Frank Li, Dan Williams,
	Ruoyu Wang, Zhejun Huang, Changyul Lee, linux-kernel,
	Daniel Byrne

These four patches change the dmaengine core. Patches 2 and 3 add core
helpers for a channel that is registered or removed at run time and
convert idxd to them. One of the four was patch 4 of v1 of "dmaengine:
idxd: fixes found by an in-kernel polling client", and the other three
are new. The other idxd fixes do not depend on them, so they go as a
series of their own. They apply to v7.4-rc2 on top of two fixes from
the list that are not applied yet, see "Patches on the list" below.
The patches meant for stable carry a Cc: stable line with the first
affected release. Patches 1 and 4 have a Fixes tag and no stable line.

A dmaengine channel can be registered and removed while its device
stays registered. idxd does that for every work queue that is bound to
its dmaengine driver or unbound from it.

In short, one patch each:
 1. a channel device whose registration failed is put, not freed with
    kfree();
 2. a new dma_async_device_channel_add() lists a channel only once it
    is registered, and idxd uses it;
 3. a new dma_async_device_channel_remove() unlinks a channel under
    dma_list_mutex before it is unregistered, and idxd uses it;
 4. the rebalance of a dynamically registered channel is done under
    dma_list_mutex.

v1 was one series of 17 patches. v2 is posted as three series:
 - "dmaengine: idxd: event log, cdev and prep fixes", ten fixes that
   stand alone, posted with this one;
 - "dmaengine: fixes for channels registered and removed at run time",
   this series;
 - "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 the first and is not posted yet. Two
   of its patches overlap patches 7 and 8 of Vinicius Costa Gomes'
   "dmaengine: idxd: More error handling fixes" v2, and it follows
   when that is resolved. This series applies with that one or
   without it.
   https://lore.kernel.org/r/20261125-wt-idxd-fixes-v2-0-25a4691dfb9e@intel.com

The channel lifetime series, posted after the three, builds on this
one. It makes dma_async_device_channel_remove() detach a channel that
clients still hold.

Link to v1: https://lore.kernel.org/dmaengine/20261019150630.4026341-1-daniel1.byrne@intel.com/

Changes since v1:

v1's 17 patches were reviewed by Frank Li, by sashiko.dev and by a local
run of the same reviewer. v1's patch 4 is patch 2 here, and the other
three 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 patch 4.
 - The local run found the other three in its review of that patch,
   all in code that v7.3 has already: a channel unlinked after its
   unregistration and without dma_list_mutex (3), the rebalance and
   the channel count without the mutex (4), and a channel device freed
   with kfree() after device_register() had initialized it (1). It
   also found the channel id freed while the channel's name is still
   in sysfs, which a patch from the list fixes now, see below.
 - The v2 patches were reviewed by a local run of the same reviewer as
   well. Its last run reports no issue on the four. In earlier runs it
   noted on patch 3 what the changelog of that patch says too: a
   client that took the channel before the unlink still holds it
   across the kfree(). The patch does not introduce that, and the
   channel lifetime series closes it.

Patches on the list:

Two fixes by other authors are reviewed and not applied yet. Each makes
a change that a patch of this series made too, so the series is now
based on them:
 - "dmaengine: idxd: Remove channel from list on registration failure",
   Ruoyu Wang, two Reviewed-by. It unlinks a channel whose registration
   failed. Patch 2 never links such a channel and removes that unlink
   again.
   https://lore.kernel.org/r/20260709062303.4167624-1-ruoyuw560@gmail.com
 - "dmaengine: Free channel ID after device_unregister()", Zhejun Huang,
   two Reviewed-by. It was the main change of patch 1, which keeps its
   other change. Changyul Lee's "dmaengine: Free channel and device IDs
   correctly" makes the same change and also frees the ids on an error
   path of dma_async_device_register().
   https://lore.kernel.org/r/20261110101607.13784-1-zhejun_huang@linux.alibaba.com
   https://lore.kernel.org/r/20261106005154.1669214-1-lcy8047@gmail.com

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 registration and the removal of a
channel on every bind and unbind of a work queue, with dmatest's and
the test module's channel requests and releases around them, and
dmatest started and stopped against the unbind in both orders. The
failed registration of patch 1 was not provoked 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 (4):
  dmaengine: put a channel device whose registration failed
  dmaengine: idxd: list a new channel only once it is complete
  dmaengine: idxd: unlink a channel under dma_list_mutex before removing
    it
  dmaengine: rebalance a dynamically registered channel under
    dma_list_mutex

 drivers/dma/dmaengine.c | 87 +++++++++++++++++++++++++++++++++++++++--
 drivers/dma/dmaengine.h |  4 ++
 drivers/dma/idxd/dma.c  | 16 +++++---
 3 files changed, 97 insertions(+), 10 deletions(-)


base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
prerequisite-patch-id: 97748f72eb2738963faecb0a1b611532641acd94
prerequisite-patch-id: 5c96dea1a1c642b3c619d6291cf7741eebd0977d
-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 1/4] dmaengine: put a channel device whose registration failed
  2026-11-30 19:51 [PATCH v2 0/4] dmaengine: fixes for channels registered and removed at run time Daniel Byrne
@ 2026-11-30 19:52 ` Daniel Byrne
  2026-11-30 21:13   ` Frank Li
  2026-11-30 19:52 ` [PATCH v2 2/4] dmaengine: idxd: list a new channel only once it is complete Daniel Byrne
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Daniel Byrne @ 2026-11-30 19:52 UTC (permalink / raw)
  To: Vinod Koul, dmaengine
  Cc: Vinicius Costa Gomes, Dave Jiang, Frank Li, Dan Williams,
	Ruoyu Wang, Zhejun Huang, Changyul Lee, linux-kernel,
	Daniel Byrne

__dma_async_device_channel_register() frees a channel's device with
kfree() when device_register() fails. device_register() has initialized
the device by then, so its reference has to be dropped with
put_device(), and the name set by dev_set_name() leaks otherwise.

Drop the reference, and let the release free the device together with
its name. The channel's id is freed first, since the release does not
free it, and the ida label that no longer has a user goes away.

The registration of a channel failed with -EEXIST when the channel was
handed an id whose sysfs device still existed, which "dmaengine: Free
channel ID after device_unregister()" [1] fixes. The registration can
still fail for want of memory.

Fixes: 257b17ca0303 ("dmaengine: fail device registration if channel registration fails")
Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
Link: https://lore.kernel.org/r/20261110101607.13784-1-zhejun_huang@linux.alibaba.com [1]
---
New in v2.

 drivers/dma/dmaengine.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
index 2642abf105d7..b1cb466f376f 100644
--- a/drivers/dma/dmaengine.c
+++ b/drivers/dma/dmaengine.c
@@ -1126,15 +1126,21 @@ static int __dma_async_device_channel_register(struct dma_device *device,
 	else
 		dev_set_name(dmaengine_chan_dev(chan), "%s", name);
 	rc = device_register(dmaengine_chan_dev(chan));
-	if (rc)
-		goto err_out_ida;
+	if (rc) {
+		/*
+		 * device_register() initialized the device. Its release
+		 * frees it, together with the name set above.
+		 */
+		ida_free(&device->chan_ida, chan->chan_id);
+		put_device(dmaengine_chan_dev(chan));
+		chan->chan_dev = NULL;
+		goto err_free_local;
+	}
 	chan->client_count = 0;
 	device->chancnt++;
 
 	return 0;
 
- err_out_ida:
-	ida_free(&device->chan_ida, chan->chan_id);
  err_free_dev:
 	kfree(chan->chan_dev);
  err_free_local:
-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 2/4] dmaengine: idxd: list a new channel only once it is complete
  2026-11-30 19:51 [PATCH v2 0/4] dmaengine: fixes for channels registered and removed at run time Daniel Byrne
  2026-11-30 19:52 ` [PATCH v2 1/4] dmaengine: put a channel device whose registration failed Daniel Byrne
@ 2026-11-30 19:52 ` Daniel Byrne
  2026-11-30 21:30   ` Frank Li
  2026-11-30 19:52 ` [PATCH v2 3/4] dmaengine: idxd: unlink a channel under dma_list_mutex before removing it Daniel Byrne
  2026-11-30 19:52 ` [PATCH v2 4/4] dmaengine: rebalance a dynamically registered channel under dma_list_mutex Daniel Byrne
  3 siblings, 1 reply; 9+ messages in thread
From: Daniel Byrne @ 2026-11-30 19:52 UTC (permalink / raw)
  To: Vinod Koul, dmaengine
  Cc: Vinicius Costa Gomes, Dave Jiang, Frank Li, Dan Williams,
	Ruoyu Wang, Zhejun Huang, Changyul Lee, linux-kernel,
	Daniel Byrne, stable

idxd_register_dma_channel() links a new channel into the dma_device's
channel list before it registers the channel, and sets the channel's
work queue only after that. The dma_device is registered when the device
is enabled, before any of its work queues, and a channel request walks
the channel list of every registered device under dma_list_mutex and may
hand out any listed channel without a client. A request that runs while
a work queue is being enabled can therefore take the half-built channel,
and the allocation callback dereferences its NULL work queue with
dma_list_mutex held. After that every channel request and release hangs.
The link runs without that mutex as well, so it races the walkers.

When the registration fails, the channel is unlinked again before it is
freed, since "dmaengine: idxd: Remove channel from list on registration
failure" [1]. Until then it is on the list like any other, where a
request can take the channel that is about to be freed, and the unlink
runs without the mutex as the link does.

Add dma_async_device_channel_add(), which registers a channel and only
then links it into its device's list, under dma_list_mutex, and use it.
The driver sets the channel's work queue and initializes its descriptors
before the call, so a request finds the channel complete or not at all,
and a channel that failed to register is never listed. An enable whose
registration fails still fails, and has nothing to unlink, so drop the
list_del() that [1] added on the error path.

This is a new helper rather than a change to
dma_async_device_channel_register(), whose contract requires the caller
to have linked the channel into the device's list already. virt-dma's
vchan_init() does that for stm32-dma3, the only other caller. That
driver has the same window between the link and the registration and is
left for a follow-up.

The helper serves DMA_PRIVATE devices such as idxd's only. The channel
list of a public device is also walked under RCU alone, and its channels
hold references for the dmaengine_get() users, which the helper does not
take. The unbind still unlinks the channel without the mutex, which
needs a core helper that unlinks under it.

The hang is reachable since the dma_device registration moved to the
device enable in v5.13, so this is for stable too.

Fixes: 8f47d1a5e545 ("dmaengine: idxd: connect idxd to dmaengine subsystem")
Fixes: 397862855619 ("dmaengine: idxd: fix dma device lifetime")
Cc: stable@vger.kernel.org # 5.13+
Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
Link: https://lore.kernel.org/r/20260709062303.4167624-1-ruoyuw560@gmail.com [1]
---
v2: dma_async_device_channel_add() is declared in
drivers/dma/dmaengine.h, next to dma_async_device_channel_register() on
the new base, instead of include/linux/dmaengine.h; the helper itself is
unchanged. idxd_register_dma_channel() drops the list_del() that
"dmaengine: idxd: Remove channel from list on registration failure"
added to its error path, since a channel that failed to register is
never listed. The message describes the failed registration as it is
after that patch, with a Link to it, and no longer describes the -EEXIST
id reuse; of the unbind's unlink without the mutex it now says that this
needs a core helper, where v1 pointed at a later rework of the unbind.

 drivers/dma/dmaengine.c | 41 +++++++++++++++++++++++++++++++++++++++++
 drivers/dma/dmaengine.h |  2 ++
 drivers/dma/idxd/dma.c  | 13 +++++++++----
 3 files changed, 52 insertions(+), 4 deletions(-)

diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
index b1cb466f376f..1d8d2949a810 100644
--- a/drivers/dma/dmaengine.c
+++ b/drivers/dma/dmaengine.c
@@ -1164,6 +1164,47 @@ int dma_async_device_channel_register(struct dma_device *device,
 }
 EXPORT_SYMBOL_GPL(dma_async_device_channel_register);
 
+/**
+ * dma_async_device_channel_add - register a channel and make it available
+ * @device: the dmaengine device, DMA_PRIVATE and registered already
+ * @chan: the channel, not on @device's channel list
+ * @name: the channel's name, or NULL for the default one
+ *
+ * Channel requests walk the channel list of every registered device under
+ * dma_list_mutex and may hand out any listed channel that has no client.
+ * A channel added to a registered device is therefore linked to the list
+ * only once it is registered, and under that mutex, so that a request
+ * never finds it half initialized. The driver's own state behind the
+ * channel must be complete before the call. A channel that fails to
+ * register is not linked.
+ *
+ * Only a DMA_PRIVATE device may add channels this way. The channel list
+ * of a public device is also walked under RCU alone, and its channels
+ * hold references for the dmaengine_get() users, which this function
+ * does not take.
+ *
+ * Return: 0 on success or a negative errno.
+ */
+int dma_async_device_channel_add(struct dma_device *device,
+				 struct dma_chan *chan, const char *name)
+{
+	int rc;
+
+	if (WARN_ON_ONCE(!dma_has_cap(DMA_PRIVATE, device->cap_mask)))
+		return -EINVAL;
+
+	rc = __dma_async_device_channel_register(device, chan, name);
+	if (rc < 0)
+		return rc;
+
+	mutex_lock(&dma_list_mutex);
+	list_add_tail(&chan->device_node, &device->channels);
+	dma_channel_rebalance();
+	mutex_unlock(&dma_list_mutex);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(dma_async_device_channel_add);
+
 static void __dma_async_device_channel_unregister(struct dma_device *device,
 						  struct dma_chan *chan)
 {
diff --git a/drivers/dma/dmaengine.h b/drivers/dma/dmaengine.h
index 42a3cfa7e597..371509b26e6b 100644
--- a/drivers/dma/dmaengine.h
+++ b/drivers/dma/dmaengine.h
@@ -208,6 +208,8 @@ static inline bool is_slave_direction(enum dma_transfer_direction direction)
 int dma_async_device_register(struct dma_device *device);
 int dmaenginem_async_device_register(struct dma_device *device);
 void dma_async_device_unregister(struct dma_device *device);
+int dma_async_device_channel_add(struct dma_device *device,
+				 struct dma_chan *chan, const char *name);
 int dma_async_device_channel_register(struct dma_device *device,
 				      struct dma_chan *chan,
 				      const char *name);
diff --git a/drivers/dma/idxd/dma.c b/drivers/dma/idxd/dma.c
index f2c03f3cf192..6293842689fb 100644
--- a/drivers/dma/idxd/dma.c
+++ b/drivers/dma/idxd/dma.c
@@ -278,7 +278,7 @@ static int idxd_register_dma_channel(struct idxd_wq *wq)
 
 	chan = &idxd_chan->chan;
 	chan->device = dma;
-	list_add_tail(&chan->device_node, &dma->channels);
+	idxd_chan->wq = wq;
 
 	for (i = 0; i < wq->num_descs; i++) {
 		struct idxd_desc *desc = wq->descs[i];
@@ -287,15 +287,20 @@ static int idxd_register_dma_channel(struct idxd_wq *wq)
 		desc->txd.tx_submit = idxd_dma_tx_submit;
 	}
 
-	rc = dma_async_device_channel_register(dma, chan, NULL);
+	/*
+	 * The dma_device is registered already, so a channel request may
+	 * take the channel as soon as it is on the device's list. The core
+	 * links it only once it is registered, and everything a client
+	 * reaches through it is set up by then. What follows serves the
+	 * unbind, which cannot run before this probe returns.
+	 */
+	rc = dma_async_device_channel_add(dma, chan, NULL);
 	if (rc < 0) {
-		list_del(&chan->device_node);
 		kfree(idxd_chan);
 		return rc;
 	}
 
 	wq->idxd_chan = idxd_chan;
-	idxd_chan->wq = wq;
 	get_device(wq_confdev(wq));
 
 	return 0;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 3/4] dmaengine: idxd: unlink a channel under dma_list_mutex before removing it
  2026-11-30 19:51 [PATCH v2 0/4] dmaengine: fixes for channels registered and removed at run time Daniel Byrne
  2026-11-30 19:52 ` [PATCH v2 1/4] dmaengine: put a channel device whose registration failed Daniel Byrne
  2026-11-30 19:52 ` [PATCH v2 2/4] dmaengine: idxd: list a new channel only once it is complete Daniel Byrne
@ 2026-11-30 19:52 ` Daniel Byrne
  2026-11-30 21:32   ` Frank Li
  2026-11-30 19:52 ` [PATCH v2 4/4] dmaengine: rebalance a dynamically registered channel under dma_list_mutex Daniel Byrne
  3 siblings, 1 reply; 9+ messages in thread
From: Daniel Byrne @ 2026-11-30 19:52 UTC (permalink / raw)
  To: Vinod Koul, dmaengine
  Cc: Vinicius Costa Gomes, Dave Jiang, Frank Li, Dan Williams,
	Ruoyu Wang, Zhejun Huang, Changyul Lee, linux-kernel,
	Daniel Byrne, stable

The unbind of a work queue unlinks its channel from the dma_device's
channel list after the core has unregistered the channel, and without
dma_list_mutex. A channel request walks that list under the mutex and
may hand out any listed channel without a client, so it can still take a
channel whose sysfs device is gone and whose idxd_chan the unbind is
about to free, and the unlocked list_del() races the locked link of
another queue's channel. The quiesce that precedes the unregistration
kills the queue's submissions, not the channel's presence on the list.

Add dma_async_device_channel_remove(), the counterpart of
dma_async_device_channel_add(). It unlinks the channel under the mutex
first and unregisters it afterwards, which the core can do and the
driver cannot, since the mutex is private to it. Use it in the unbind.
After the locked unlink no walker under the mutex finds the channel.

A client that took the channel between the quiesce and the unlink still
holds idxd_chan across the kfree(). This is a broader lifetime problem
of a channel unbound under its clients, and is left for the change that
reworks the unbind for channels that clients still hold.

Fixes: 397862855619 ("dmaengine: idxd: fix dma device lifetime")
Cc: stable@vger.kernel.org # 5.13+
Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
---
New in v2.

 drivers/dma/dmaengine.c | 26 ++++++++++++++++++++++++++
 drivers/dma/dmaengine.h |  2 ++
 drivers/dma/idxd/dma.c  |  3 +--
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
index 1d8d2949a810..85a98aae6a97 100644
--- a/drivers/dma/dmaengine.c
+++ b/drivers/dma/dmaengine.c
@@ -1231,6 +1231,32 @@ void dma_async_device_channel_unregister(struct dma_device *device,
 }
 EXPORT_SYMBOL_GPL(dma_async_device_channel_unregister);
 
+/**
+ * dma_async_device_channel_remove - unlink and unregister a channel
+ * @device: the channel's device
+ * @chan: the channel, added with dma_async_device_channel_add()
+ *
+ * The channel is unlinked from the device's list under dma_list_mutex
+ * before it is unregistered, so that a request that walks the list under
+ * the mutex cannot hand out a channel whose sysfs device is gone and whose
+ * memory the driver is about to free, and so that the unlink does not race
+ * the locked link of another channel. The driver must have quiesced the
+ * channel. As with dma_async_device_channel_unregister(), a client that
+ * already holds the channel is not handled here.
+ */
+void dma_async_device_channel_remove(struct dma_device *device,
+				     struct dma_chan *chan)
+{
+	mutex_lock(&dma_list_mutex);
+	list_del(&chan->device_node);
+	mutex_unlock(&dma_list_mutex);
+	__dma_async_device_channel_unregister(device, chan);
+	mutex_lock(&dma_list_mutex);
+	dma_channel_rebalance();
+	mutex_unlock(&dma_list_mutex);
+}
+EXPORT_SYMBOL_GPL(dma_async_device_channel_remove);
+
 /**
  * dma_async_device_register - registers DMA devices found
  * @device:	pointer to &struct dma_device
diff --git a/drivers/dma/dmaengine.h b/drivers/dma/dmaengine.h
index 371509b26e6b..c6c03174c97a 100644
--- a/drivers/dma/dmaengine.h
+++ b/drivers/dma/dmaengine.h
@@ -215,6 +215,8 @@ int dma_async_device_channel_register(struct dma_device *device,
 				      const char *name);
 void dma_async_device_channel_unregister(struct dma_device *device,
 					 struct dma_chan *chan);
+void dma_async_device_channel_remove(struct dma_device *device,
+				     struct dma_chan *chan);
 
 #ifdef CONFIG_DEBUG_FS
 #include <linux/debugfs.h>
diff --git a/drivers/dma/idxd/dma.c b/drivers/dma/idxd/dma.c
index 6293842689fb..bed71e66eca6 100644
--- a/drivers/dma/idxd/dma.c
+++ b/drivers/dma/idxd/dma.c
@@ -312,8 +312,7 @@ static void idxd_unregister_dma_channel(struct idxd_wq *wq)
 	struct dma_chan *chan = &idxd_chan->chan;
 	struct idxd_dma_dev *idxd_dma = wq->idxd->idxd_dma;
 
-	dma_async_device_channel_unregister(&idxd_dma->dma, chan);
-	list_del(&chan->device_node);
+	dma_async_device_channel_remove(&idxd_dma->dma, chan);
 	kfree(wq->idxd_chan);
 	wq->idxd_chan = NULL;
 	put_device(wq_confdev(wq));
-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 4/4] dmaengine: rebalance a dynamically registered channel under dma_list_mutex
  2026-11-30 19:51 [PATCH v2 0/4] dmaengine: fixes for channels registered and removed at run time Daniel Byrne
                   ` (2 preceding siblings ...)
  2026-11-30 19:52 ` [PATCH v2 3/4] dmaengine: idxd: unlink a channel under dma_list_mutex before removing it Daniel Byrne
@ 2026-11-30 19:52 ` Daniel Byrne
  2026-11-30 22:09   ` Frank Li
  3 siblings, 1 reply; 9+ messages in thread
From: Daniel Byrne @ 2026-11-30 19:52 UTC (permalink / raw)
  To: Vinod Koul, dmaengine
  Cc: Vinicius Costa Gomes, Dave Jiang, Frank Li, Dan Williams,
	Ruoyu Wang, Zhejun Huang, Changyul Lee, linux-kernel,
	Daniel Byrne

dma_async_device_channel_register() and
dma_async_device_channel_unregister() call dma_channel_rebalance()
without dma_list_mutex, which the function requires. It clears every
slot of the channel table and repopulates it from a walk of the device
list, so a concurrent locked rebalance from dmaengine_get() or a device
registration interleaves with it. Public channels lose their table
entries until the next one, and a device release can delete the entry
the unlocked walk is on. The device's channel count is bumped without
the mutex on the same path and dropped under it on the other.

Take the mutex around the two rebalances and the count. Neither caller
holds it, and the helpers underneath take and drop it themselves.

Fixes: e81274cd6b52 ("dmaengine: add support to dynamic register/unregister of channels")
Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
---
New in v2.

 drivers/dma/dmaengine.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
index 85a98aae6a97..49ecbdaf6dbb 100644
--- a/drivers/dma/dmaengine.c
+++ b/drivers/dma/dmaengine.c
@@ -1137,7 +1137,9 @@ static int __dma_async_device_channel_register(struct dma_device *device,
 		goto err_free_local;
 	}
 	chan->client_count = 0;
+	mutex_lock(&dma_list_mutex);
 	device->chancnt++;
+	mutex_unlock(&dma_list_mutex);
 
 	return 0;
 
@@ -1159,7 +1161,9 @@ int dma_async_device_channel_register(struct dma_device *device,
 	if (rc < 0)
 		return rc;
 
+	mutex_lock(&dma_list_mutex);
 	dma_channel_rebalance();
+	mutex_unlock(&dma_list_mutex);
 	return 0;
 }
 EXPORT_SYMBOL_GPL(dma_async_device_channel_register);
@@ -1227,7 +1231,9 @@ void dma_async_device_channel_unregister(struct dma_device *device,
 					 struct dma_chan *chan)
 {
 	__dma_async_device_channel_unregister(device, chan);
+	mutex_lock(&dma_list_mutex);
 	dma_channel_rebalance();
+	mutex_unlock(&dma_list_mutex);
 }
 EXPORT_SYMBOL_GPL(dma_async_device_channel_unregister);
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 1/4] dmaengine: put a channel device whose registration failed
  2026-11-30 19:52 ` [PATCH v2 1/4] dmaengine: put a channel device whose registration failed Daniel Byrne
@ 2026-11-30 21:13   ` Frank Li
  0 siblings, 0 replies; 9+ messages in thread
From: Frank Li @ 2026-11-30 21:13 UTC (permalink / raw)
  To: Daniel Byrne
  Cc: Vinod Koul, dmaengine, Vinicius Costa Gomes, Dave Jiang,
	Frank Li, Dan Williams, Ruoyu Wang, Zhejun Huang, Changyul Lee,
	linux-kernel

On Mon, Nov 30, 2026 at 11:52:00AM -0800, Daniel Byrne wrote:
> __dma_async_device_channel_register() frees a channel's device with
> kfree() when device_register() fails. device_register() has initialized
> the device by then, so its reference has to be dropped with
> put_device(), and the name set by dev_set_name() leaks otherwise.
>
> Drop the reference, and let the release free the device together with
> its name. The channel's id is freed first, since the release does not
> free it, and the ida label that no longer has a user goes away.
>
> The registration of a channel failed with -EEXIST when the channel was
> handed an id whose sysfs device still existed, which "dmaengine: Free
> channel ID after device_unregister()" [1] fixes. The registration can
> still fail for want of memory.
>
> Fixes: 257b17ca0303 ("dmaengine: fail device registration if channel registration fails")
> Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
> Link: https://lore.kernel.org/r/20261110101607.13784-1-zhejun_huang@linux.alibaba.com [1]
> ---

Reviewed-by: Frank Li <Frank.Li@nxp.com>

> New in v2.
>
>  drivers/dma/dmaengine.c | 14 ++++++++++----
>  1 file changed, 10 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
> index 2642abf105d7..b1cb466f376f 100644
> --- a/drivers/dma/dmaengine.c
> +++ b/drivers/dma/dmaengine.c
> @@ -1126,15 +1126,21 @@ static int __dma_async_device_channel_register(struct dma_device *device,
>  	else
>  		dev_set_name(dmaengine_chan_dev(chan), "%s", name);
>  	rc = device_register(dmaengine_chan_dev(chan));
> -	if (rc)
> -		goto err_out_ida;
> +	if (rc) {
> +		/*
> +		 * device_register() initialized the device. Its release
> +		 * frees it, together with the name set above.
> +		 */
> +		ida_free(&device->chan_ida, chan->chan_id);
> +		put_device(dmaengine_chan_dev(chan));
> +		chan->chan_dev = NULL;
> +		goto err_free_local;
> +	}
>  	chan->client_count = 0;
>  	device->chancnt++;
>
>  	return 0;
>
> - err_out_ida:
> -	ida_free(&device->chan_ida, chan->chan_id);
>   err_free_dev:
>  	kfree(chan->chan_dev);
>   err_free_local:
> --
> 2.43.0
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 2/4] dmaengine: idxd: list a new channel only once it is complete
  2026-11-30 19:52 ` [PATCH v2 2/4] dmaengine: idxd: list a new channel only once it is complete Daniel Byrne
@ 2026-11-30 21:30   ` Frank Li
  0 siblings, 0 replies; 9+ messages in thread
From: Frank Li @ 2026-11-30 21:30 UTC (permalink / raw)
  To: Daniel Byrne
  Cc: Vinod Koul, dmaengine, Vinicius Costa Gomes, Dave Jiang,
	Frank Li, Dan Williams, Ruoyu Wang, Zhejun Huang, Changyul Lee,
	linux-kernel, stable

On Mon, Nov 30, 2026 at 11:52:01AM -0800, Daniel Byrne wrote:
> idxd_register_dma_channel() links a new channel into the dma_device's
> channel list before it registers the channel, and sets the channel's
> work queue only after that. The dma_device is registered when the device
> is enabled, before any of its work queues, and a channel request walks
> the channel list of every registered device under dma_list_mutex and may
> hand out any listed channel without a client. A request that runs while
> a work queue is being enabled can therefore take the half-built channel,
> and the allocation callback dereferences its NULL work queue with
> dma_list_mutex held. After that every channel request and release hangs.
> The link runs without that mutex as well, so it races the walkers.
>
> When the registration fails, the channel is unlinked again before it is
> freed, since "dmaengine: idxd: Remove channel from list on registration
> failure" [1]. Until then it is on the list like any other, where a
> request can take the channel that is about to be freed, and the unlink
> runs without the mutex as the link does.
>
> Add dma_async_device_channel_add(), which registers a channel and only
> then links it into its device's list, under dma_list_mutex, and use it.
> The driver sets the channel's work queue and initializes its descriptors
> before the call, so a request finds the channel complete or not at all,
> and a channel that failed to register is never listed. An enable whose
> registration fails still fails, and has nothing to unlink, so drop the
> list_del() that [1] added on the error path.
>
> This is a new helper rather than a change to
> dma_async_device_channel_register(), whose contract requires the caller
> to have linked the channel into the device's list already. virt-dma's
> vchan_init() does that for stm32-dma3, the only other caller. That
> driver has the same window between the link and the registration and is
> left for a follow-up.
>
> The helper serves DMA_PRIVATE devices such as idxd's only. The channel
> list of a public device is also walked under RCU alone, and its channels
> hold references for the dmaengine_get() users, which the helper does not
> take. The unbind still unlinks the channel without the mutex, which
> needs a core helper that unlinks under it.
>
> The hang is reachable since the dma_device registration moved to the
> device enable in v5.13, so this is for stable too.
>
> Fixes: 8f47d1a5e545 ("dmaengine: idxd: connect idxd to dmaengine subsystem")
> Fixes: 397862855619 ("dmaengine: idxd: fix dma device lifetime")
> Cc: stable@vger.kernel.org # 5.13+
> Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
> Link: https://lore.kernel.org/r/20260709062303.4167624-1-ruoyuw560@gmail.com [1]
> ---
> v2: dma_async_device_channel_add() is declared in
> drivers/dma/dmaengine.h, next to dma_async_device_channel_register() on
> the new base, instead of include/linux/dmaengine.h; the helper itself is
> unchanged. idxd_register_dma_channel() drops the list_del() that
> "dmaengine: idxd: Remove channel from list on registration failure"
> added to its error path, since a channel that failed to register is
> never listed. The message describes the failed registration as it is
> after that patch, with a Link to it, and no longer describes the -EEXIST
> id reuse; of the unbind's unlink without the mutex it now says that this
> needs a core helper, where v1 pointed at a later rework of the unbind.
>
>  drivers/dma/dmaengine.c | 41 +++++++++++++++++++++++++++++++++++++++++
>  drivers/dma/dmaengine.h |  2 ++

Use sperate patch to add APIs

>  drivers/dma/idxd/dma.c  | 13 +++++++++----
>  3 files changed, 52 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
> index b1cb466f376f..1d8d2949a810 100644
> --- a/drivers/dma/dmaengine.c
> +++ b/drivers/dma/dmaengine.c
> @@ -1164,6 +1164,47 @@ int dma_async_device_channel_register(struct dma_device *device,
>  }
>  EXPORT_SYMBOL_GPL(dma_async_device_channel_register);
>
> +/**
> + * dma_async_device_channel_add - register a channel and make it available
> + * @device: the dmaengine device, DMA_PRIVATE and registered already
> + * @chan: the channel, not on @device's channel list
> + * @name: the channel's name, or NULL for the default one
> + *
> + * Channel requests walk the channel list of every registered device under
> + * dma_list_mutex and may hand out any listed channel that has no client.
> + * A channel added to a registered device is therefore linked to the list
> + * only once it is registered, and under that mutex, so that a request
> + * never finds it half initialized. The driver's own state behind the
> + * channel must be complete before the call. A channel that fails to
> + * register is not linked.
> + *
> + * Only a DMA_PRIVATE device may add channels this way. The channel list
> + * of a public device is also walked under RCU alone, and its channels
> + * hold references for the dmaengine_get() users, which this function
> + * does not take.
> + *
> + * Return: 0 on success or a negative errno.
> + */
> +int dma_async_device_channel_add(struct dma_device *device,
> +				 struct dma_chan *chan, const char *name)
> +{

New API should use prefix dmaengine_

Frank


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 3/4] dmaengine: idxd: unlink a channel under dma_list_mutex before removing it
  2026-11-30 19:52 ` [PATCH v2 3/4] dmaengine: idxd: unlink a channel under dma_list_mutex before removing it Daniel Byrne
@ 2026-11-30 21:32   ` Frank Li
  0 siblings, 0 replies; 9+ messages in thread
From: Frank Li @ 2026-11-30 21:32 UTC (permalink / raw)
  To: Daniel Byrne
  Cc: Vinod Koul, dmaengine, Vinicius Costa Gomes, Dave Jiang,
	Frank Li, Dan Williams, Ruoyu Wang, Zhejun Huang, Changyul Lee,
	linux-kernel, stable

On Mon, Nov 30, 2026 at 11:52:02AM -0800, Daniel Byrne wrote:
> The unbind of a work queue unlinks its channel from the dma_device's
> channel list after the core has unregistered the channel, and without
> dma_list_mutex. A channel request walks that list under the mutex and
> may hand out any listed channel without a client, so it can still take a
> channel whose sysfs device is gone and whose idxd_chan the unbind is
> about to free, and the unlocked list_del() races the locked link of
> another queue's channel. The quiesce that precedes the unregistration
> kills the queue's submissions, not the channel's presence on the list.
>
> Add dma_async_device_channel_remove(), the counterpart of
> dma_async_device_channel_add(). It unlinks the channel under the mutex
> first and unregisters it afterwards, which the core can do and the
> driver cannot, since the mutex is private to it. Use it in the unbind.
> After the locked unlink no walker under the mutex finds the channel.
>
> A client that took the channel between the quiesce and the unlink still
> holds idxd_chan across the kfree(). This is a broader lifetime problem
> of a channel unbound under its clients, and is left for the change that
> reworks the unbind for channels that clients still hold.
>
> Fixes: 397862855619 ("dmaengine: idxd: fix dma device lifetime")
> Cc: stable@vger.kernel.org # 5.13+
> Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
> ---
> New in v2.
>
>  drivers/dma/dmaengine.c | 26 ++++++++++++++++++++++++++
>  drivers/dma/dmaengine.h |  2 ++

combine dma_async_device_channel_remove() and _add() to one prepare patch.

Frank

>  drivers/dma/idxd/dma.c  |  3 +--
>  3 files changed, 29 insertions(+), 2 deletions(-)


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 4/4] dmaengine: rebalance a dynamically registered channel under dma_list_mutex
  2026-11-30 19:52 ` [PATCH v2 4/4] dmaengine: rebalance a dynamically registered channel under dma_list_mutex Daniel Byrne
@ 2026-11-30 22:09   ` Frank Li
  0 siblings, 0 replies; 9+ messages in thread
From: Frank Li @ 2026-11-30 22:09 UTC (permalink / raw)
  To: Daniel Byrne
  Cc: Vinod Koul, dmaengine, Vinicius Costa Gomes, Dave Jiang,
	Frank Li, Dan Williams, Ruoyu Wang, Zhejun Huang, Changyul Lee,
	linux-kernel

On Mon, Nov 30, 2026 at 11:52:03AM -0800, Daniel Byrne wrote:
> dma_async_device_channel_register() and
> dma_async_device_channel_unregister() call dma_channel_rebalance()
> without dma_list_mutex, which the function requires. It clears every
> slot of the channel table and repopulates it from a walk of the device
> list, so a concurrent locked rebalance from dmaengine_get() or a device
> registration interleaves with it. Public channels lose their table
> entries until the next one, and a device release can delete the entry
> the unlocked walk is on. The device's channel count is bumped without
> the mutex on the same path and dropped under it on the other.
>
> Take the mutex around the two rebalances and the count. Neither caller
> holds it, and the helpers underneath take and drop it themselves.
>
> Fixes: e81274cd6b52 ("dmaengine: add support to dynamic register/unregister of channels")
> Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
> ---

Reviewed-by: Frank Li <Frank.Li@nxp.com>

> New in v2.
>
>  drivers/dma/dmaengine.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
> index 85a98aae6a97..49ecbdaf6dbb 100644
> --- a/drivers/dma/dmaengine.c
> +++ b/drivers/dma/dmaengine.c
> @@ -1137,7 +1137,9 @@ static int __dma_async_device_channel_register(struct dma_device *device,
>  		goto err_free_local;
>  	}
>  	chan->client_count = 0;
> +	mutex_lock(&dma_list_mutex);
>  	device->chancnt++;
> +	mutex_unlock(&dma_list_mutex);
>
>  	return 0;
>
> @@ -1159,7 +1161,9 @@ int dma_async_device_channel_register(struct dma_device *device,
>  	if (rc < 0)
>  		return rc;
>
> +	mutex_lock(&dma_list_mutex);
>  	dma_channel_rebalance();
> +	mutex_unlock(&dma_list_mutex);
>  	return 0;
>  }
>  EXPORT_SYMBOL_GPL(dma_async_device_channel_register);
> @@ -1227,7 +1231,9 @@ void dma_async_device_channel_unregister(struct dma_device *device,
>  					 struct dma_chan *chan)
>  {
>  	__dma_async_device_channel_unregister(device, chan);
> +	mutex_lock(&dma_list_mutex);
>  	dma_channel_rebalance();
> +	mutex_unlock(&dma_list_mutex);
>  }
>  EXPORT_SYMBOL_GPL(dma_async_device_channel_unregister);
>
> --
> 2.43.0
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-11-30 22:09 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 19:51 [PATCH v2 0/4] dmaengine: fixes for channels registered and removed at run time Daniel Byrne
2026-11-30 19:52 ` [PATCH v2 1/4] dmaengine: put a channel device whose registration failed Daniel Byrne
2026-11-30 21:13   ` Frank Li
2026-11-30 19:52 ` [PATCH v2 2/4] dmaengine: idxd: list a new channel only once it is complete Daniel Byrne
2026-11-30 21:30   ` Frank Li
2026-11-30 19:52 ` [PATCH v2 3/4] dmaengine: idxd: unlink a channel under dma_list_mutex before removing it Daniel Byrne
2026-11-30 21:32   ` Frank Li
2026-11-30 19:52 ` [PATCH v2 4/4] dmaengine: rebalance a dynamically registered channel under dma_list_mutex Daniel Byrne
2026-11-30 22:09   ` Frank Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®