[PATCH v2 3/4] dmaengine: idxd: unlink a channel under dma_list_mutex before removing it

From: Daniel Byrne

Date: Mon Nov 30 2026 - 14:58:29 EST


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@xxxxxxxxxxxxxxx # 5.13+
Signed-off-by: Daniel Byrne <daniel1.byrne@xxxxxxxxx>
---
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