mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Byrne <daniel1.byrne@intel.com>
To: Vinod Koul <vkoul@kernel.org>, dmaengine@vger.kernel.org
Cc: Vinicius Costa Gomes <vinicius.gomes@intel.com>,
	Dave Jiang <dave.jiang@intel.com>, Frank Li <Frank.Li@kernel.org>,
	Dan Williams <djbw@kernel.org>, Ruoyu Wang <ruoyuw560@gmail.com>,
	Zhejun Huang <zhejun_huang@linux.alibaba.com>,
	Changyul Lee <lcy8047@gmail.com>,
	linux-kernel@vger.kernel.org,
	Daniel Byrne <daniel1.byrne@intel.com>,
	stable@vger.kernel.org
Subject: [PATCH v2 3/4] dmaengine: idxd: unlink a channel under dma_list_mutex before removing it
Date: Mon, 30 Nov 2026 11:52:02 -0800	[thread overview]
Message-ID: <20261130195203.1901143-4-daniel1.byrne@intel.com> (raw)
In-Reply-To: <20261130195203.1901143-1-daniel1.byrne@intel.com>

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


  parent reply	other threads:[~2026-11-30 19:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-11-30 19:52 ` [PATCH v2 4/4] dmaengine: rebalance a dynamically registered channel under dma_list_mutex Daniel Byrne

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130195203.1901143-4-daniel1.byrne@intel.com \
    --to=daniel1.byrne@intel.com \
    --cc=Frank.Li@kernel.org \
    --cc=dave.jiang@intel.com \
    --cc=djbw@kernel.org \
    --cc=dmaengine@vger.kernel.org \
    --cc=lcy8047@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ruoyuw560@gmail.com \
    --cc=stable@vger.kernel.org \
    --cc=vinicius.gomes@intel.com \
    --cc=vkoul@kernel.org \
    --cc=zhejun_huang@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®