mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vinicius Costa Gomes <vinicius.gomes@intel.com>
To: Dave Jiang <dave.jiang@intel.com>, Vinod Koul <vkoul@kernel.org>,
	 Frank Li <Frank.Li@kernel.org>, Fenghua Yu <fenghuay@nvidia.com>,
	 Shuai Xue <xueshuai@linux.alibaba.com>
Cc: dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Vinicius Costa Gomes <vinicius.gomes@intel.com>
Subject: [PATCH v2 4/8] dmaengine: idxd: Fix the device registration error paths
Date: Wed, 25 Nov 2026 15:49:02 -0800	[thread overview]
Message-ID: <20261125-wt-idxd-fixes-v2-4-25a4691dfb9e@intel.com> (raw)
In-Reply-To: <20261125-wt-idxd-fixes-v2-0-25a4691dfb9e@intel.com>

idxd_setup_*() takes a reference on each wq, engine and group device,
and idxd_cleanup_internals() drops it. When registering the devices
fails, the error paths drop it first:

 idxd_pci_probe_alloc()
 |-idxd_register_devices()
 | \-idxd_register_{wq,engine,group}_devices()
 |   put_device() or device_unregister() on each device.
 |   The last reference is gone, the device is freed.
 |
 \-idxd_cleanup()
   \-idxd_cleanup_internals()
     put_device() on each device again, use-after-free.

idxd_register_devices() does the same, with device_unregister(), for
the devices registered before the failure.

The rollback in the helpers is also off by one. With j = i - 1 and
while (j--), it skips the last device added. When the first device
fails, j starts at -1 and the loop walks off the front of the array.

Only device_del() the devices that were added, and leave the references
to idxd_cleanup_internals().

Fixes: 7c5dd23e57c1 ("dmaengine: idxd: fix wq conf_dev 'struct device' lifetime")
Fixes: 75b911309060 ("dmaengine: idxd: fix engine conf_dev lifetime")
Fixes: defe49f96012 ("dmaengine: idxd: fix group conf_dev lifetime")
Fixes: ddf742d4f3f1 ("dmaengine: idxd: Add missing cleanup for early error out in probe call")
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
---
 drivers/dma/idxd/sysfs.c | 40 +++++++++++-----------------------------
 1 file changed, 11 insertions(+), 29 deletions(-)

diff --git a/drivers/dma/idxd/sysfs.c b/drivers/dma/idxd/sysfs.c
index d5ffc641c856..87fa4f43e27c 100644
--- a/drivers/dma/idxd/sysfs.c
+++ b/drivers/dma/idxd/sysfs.c
@@ -1864,7 +1864,7 @@ const struct device_type iax_device_type = {
 static int idxd_register_engine_devices(struct idxd_device *idxd)
 {
 	struct idxd_engine *engine;
-	int i, j, rc;
+	int i, rc;
 
 	for (i = 0; i < idxd->max_engines; i++) {
 		engine = idxd->engines[i];
@@ -1876,15 +1876,9 @@ static int idxd_register_engine_devices(struct idxd_device *idxd)
 	return 0;
 
 cleanup:
-	j = i - 1;
-	for (; i < idxd->max_engines; i++) {
+	while (i--) {
 		engine = idxd->engines[i];
-		put_device(engine_confdev(engine));
-	}
-
-	while (j--) {
-		engine = idxd->engines[j];
-		device_unregister(engine_confdev(engine));
+		device_del(engine_confdev(engine));
 	}
 	return rc;
 }
@@ -1892,7 +1886,7 @@ static int idxd_register_engine_devices(struct idxd_device *idxd)
 static int idxd_register_group_devices(struct idxd_device *idxd)
 {
 	struct idxd_group *group;
-	int i, j, rc;
+	int i, rc;
 
 	for (i = 0; i < idxd->max_groups; i++) {
 		group = idxd->groups[i];
@@ -1904,15 +1898,9 @@ static int idxd_register_group_devices(struct idxd_device *idxd)
 	return 0;
 
 cleanup:
-	j = i - 1;
-	for (; i < idxd->max_groups; i++) {
+	while (i--) {
 		group = idxd->groups[i];
-		put_device(group_confdev(group));
-	}
-
-	while (j--) {
-		group = idxd->groups[j];
-		device_unregister(group_confdev(group));
+		device_del(group_confdev(group));
 	}
 	return rc;
 }
@@ -1920,7 +1908,7 @@ static int idxd_register_group_devices(struct idxd_device *idxd)
 static int idxd_register_wq_devices(struct idxd_device *idxd)
 {
 	struct idxd_wq *wq;
-	int i, rc, j;
+	int i, rc;
 
 	for (i = 0; i < idxd->max_wqs; i++) {
 		wq = idxd->wqs[i];
@@ -1932,15 +1920,9 @@ static int idxd_register_wq_devices(struct idxd_device *idxd)
 	return 0;
 
 cleanup:
-	j = i - 1;
-	for (; i < idxd->max_wqs; i++) {
+	while (i--) {
 		wq = idxd->wqs[i];
-		put_device(wq_confdev(wq));
-	}
-
-	while (j--) {
-		wq = idxd->wqs[j];
-		device_unregister(wq_confdev(wq));
+		device_del(wq_confdev(wq));
 	}
 	return rc;
 }
@@ -1976,10 +1958,10 @@ int idxd_register_devices(struct idxd_device *idxd)
 
  err_group:
 	for (i = 0; i < idxd->max_engines; i++)
-		device_unregister(engine_confdev(idxd->engines[i]));
+		device_del(engine_confdev(idxd->engines[i]));
  err_engine:
 	for (i = 0; i < idxd->max_wqs; i++)
-		device_unregister(wq_confdev(idxd->wqs[i]));
+		device_del(wq_confdev(idxd->wqs[i]));
  err_wq:
 	device_del(idxd_confdev(idxd));
 	return rc;

-- 
2.56.1


  parent reply	other threads:[~2026-11-25 23:49 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 23:48 [PATCH v2 0/8] dmaengine: idxd: More error handling fixes Vinicius Costa Gomes
2026-11-25 23:48 ` [PATCH v2 1/8] dmaengine: idxd: Do not call destroy_workqueue() with null idxd->wq Vinicius Costa Gomes
2026-11-25 23:49 ` [PATCH v2 2/8] dmaengine: idxd: Fix potential double free on probe error path Vinicius Costa Gomes
2026-11-25 23:49 ` [PATCH v2 3/8] dmaengine: idxd: fix duplicate memory frees on initialization " Vinicius Costa Gomes
2026-11-25 23:49 ` Vinicius Costa Gomes [this message]
2026-11-25 23:49 ` [PATCH v2 5/8] dmaengine: idxd: Fix error handling in idxd_cdev_open() Vinicius Costa Gomes
2026-11-25 23:49 ` [PATCH v2 6/8] dmaengine: idxd: Initialize the WQ XArray before the WQ can be released Vinicius Costa Gomes
2026-11-25 23:49 ` [PATCH v2 7/8] dmaengine: idxd: Do not release interrupt handles on terminate_all() Vinicius Costa Gomes
2026-11-25 23:49 ` [PATCH v2 8/8] dmaengine: idxd: Don't flush the descriptors " Vinicius Costa Gomes

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=20261125-wt-idxd-fixes-v2-4-25a4691dfb9e@intel.com \
    --to=vinicius.gomes@intel.com \
    --cc=Frank.Li@kernel.org \
    --cc=dave.jiang@intel.com \
    --cc=dmaengine@vger.kernel.org \
    --cc=fenghuay@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=vkoul@kernel.org \
    --cc=xueshuai@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®