Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
From: Serhat Kumral <serhatkumral1@gmail.com>
To: jgg@ziepe.ca
Cc: bharat@chelsio.com, larrystevenwise@gmail.com, leon@kernel.org,
	linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org,
	rolanddreier@rivian.com, serhatkumral1@gmail.com
Subject: [PATCH v2] RDMA/cxgb4: Use dealloc_driver op to fix ioremap leak on alloc failure
Date: Thu, 26 Nov 2026 20:08:41 +0300	[thread overview]
Message-ID: <20261126170841.11048-1-serhatkumral1@gmail.com> (raw)
In-Reply-To: <20261123165128.GL179082@ziepe.ca>

In c4iw_alloc(), if c4iw_rdev_open() fails, the previously ioremapped
BAR2/on-chip memory (bar2_kva / oc_mw_kva) is leaked because
ib_dealloc_device() does not unmap it.

Refactor c4iw_dealloc() to take struct ib_device * and register it as the
.dealloc_driver callback in c4iw_dev_ops. Move ib_set_device_ops() and
the xarray/mutex/waitqueue initializations earlier in c4iw_alloc() so
that all ib_dealloc_device() error paths invoke c4iw_dealloc(). Guard
c4iw_rdev_close() with dev->rdev.free_workq so it is only called when
c4iw_rdev_open() succeeded.

Update c4iw_register_device() and c4iw_remove() to rely on
ib_dealloc_device() and ib_unregister_device() rather than calling
c4iw_dealloc() directly, and handle the case where cancel_work_sync()
cancels reg_work before registration runs.

Fixes: c6d7b26791a2 ("RDMA/cxgb4: Support on-chip SQs")
Fixes: fa658a98a2d0 ("RDMA/cxgb4: Use the BAR2/WC path for kernel QPs and T5 devices")
Signed-off-by: Serhat Kumral <serhatkumral1@gmail.com>
---
 drivers/infiniband/hw/cxgb4/device.c   | 66 +++++++++++++-------------
 drivers/infiniband/hw/cxgb4/iw_cxgb4.h |  3 +-
 drivers/infiniband/hw/cxgb4/provider.c |  7 +--
 3 files changed, 40 insertions(+), 36 deletions(-)

diff --git a/drivers/infiniband/hw/cxgb4/device.c b/drivers/infiniband/hw/cxgb4/device.c
index 24ac9871ce67..336747080abd 100644
--- a/drivers/infiniband/hw/cxgb4/device.c
+++ b/drivers/infiniband/hw/cxgb4/device.c
@@ -931,22 +931,23 @@ static void c4iw_rdev_close(struct c4iw_rdev *rdev)
 	c4iw_destroy_resource(&rdev->resource);
 }
 
-void c4iw_dealloc(struct uld_ctx *ctx)
+void c4iw_dealloc(struct ib_device *ibdev)
 {
-	debugfs_remove_recursive(ctx->dev->debugfs_root);
-	c4iw_rdev_close(&ctx->dev->rdev);
-	WARN_ON(!xa_empty(&ctx->dev->cqs));
-	WARN_ON(!xa_empty(&ctx->dev->qps));
-	WARN_ON(!xa_empty(&ctx->dev->mrs));
-	wait_event(ctx->dev->wait, xa_empty(&ctx->dev->hwtids));
-	WARN_ON(!xa_empty(&ctx->dev->stids));
-	WARN_ON(!xa_empty(&ctx->dev->atids));
-	if (ctx->dev->rdev.bar2_kva)
-		iounmap(ctx->dev->rdev.bar2_kva);
-	if (ctx->dev->rdev.oc_mw_kva)
-		iounmap(ctx->dev->rdev.oc_mw_kva);
-	ib_dealloc_device(&ctx->dev->ibdev);
-	ctx->dev = NULL;
+	struct c4iw_dev *dev = to_c4iw_dev(ibdev);
+
+	debugfs_remove_recursive(dev->debugfs_root);
+	if (dev->rdev.free_workq)
+		c4iw_rdev_close(&dev->rdev);
+	WARN_ON(!xa_empty(&dev->cqs));
+	WARN_ON(!xa_empty(&dev->qps));
+	WARN_ON(!xa_empty(&dev->mrs));
+	wait_event(dev->wait, xa_empty(&dev->hwtids));
+	WARN_ON(!xa_empty(&dev->stids));
+	WARN_ON(!xa_empty(&dev->atids));
+	if (dev->rdev.bar2_kva)
+		iounmap(dev->rdev.bar2_kva);
+	if (dev->rdev.oc_mw_kva)
+		iounmap(dev->rdev.oc_mw_kva);
 }
 
 static void c4iw_remove(struct uld_ctx *ctx)
@@ -954,12 +955,12 @@ static void c4iw_remove(struct uld_ctx *ctx)
 	pr_debug("c4iw_dev %p\n", ctx->dev);
 
 	/* c4iw_register_device() may still be using ctx->dev. */
-	cancel_work_sync(&ctx->reg_work);
-	if (!ctx->dev)
-		return;
+	if (cancel_work_sync(&ctx->reg_work))
+		ib_dealloc_device(&ctx->dev->ibdev);
+	else if (ctx->dev)
+		c4iw_unregister_device(ctx->dev);
 
-	c4iw_unregister_device(ctx->dev);
-	c4iw_dealloc(ctx);
+	ctx->dev = NULL;
 }
 
 static int rdma_supported(const struct cxgb4_lld_info *infop)
@@ -990,6 +991,19 @@ static struct c4iw_dev *c4iw_alloc(const struct cxgb4_lld_info *infop)
 	}
 	devp->rdev.lldi = *infop;
 
+	xa_init_flags(&devp->cqs, XA_FLAGS_LOCK_IRQ);
+	xa_init_flags(&devp->qps, XA_FLAGS_LOCK_IRQ);
+	xa_init_flags(&devp->mrs, XA_FLAGS_LOCK_IRQ);
+	xa_init_flags(&devp->hwtids, XA_FLAGS_LOCK_IRQ);
+	xa_init_flags(&devp->atids, XA_FLAGS_LOCK_IRQ);
+	xa_init_flags(&devp->stids, XA_FLAGS_LOCK_IRQ);
+	mutex_init(&devp->rdev.stats.lock);
+	mutex_init(&devp->db_mutex);
+	INIT_LIST_HEAD(&devp->db_fc_list);
+	init_waitqueue_head(&devp->wait);
+	devp->avail_ird = devp->rdev.lldi.max_ird_adapter;
+	ib_set_device_ops(&devp->ibdev, &c4iw_dev_ops);
+
 	/* init various hw-queue params based on lld info */
 	pr_debug("Ing. padding boundary is %d, egrsstatuspagesize = %d\n",
 		 devp->rdev.lldi.sge_ingpadboundary,
@@ -1050,18 +1064,6 @@ static struct c4iw_dev *c4iw_alloc(const struct cxgb4_lld_info *infop)
 		return ERR_PTR(ret);
 	}
 
-	xa_init_flags(&devp->cqs, XA_FLAGS_LOCK_IRQ);
-	xa_init_flags(&devp->qps, XA_FLAGS_LOCK_IRQ);
-	xa_init_flags(&devp->mrs, XA_FLAGS_LOCK_IRQ);
-	xa_init_flags(&devp->hwtids, XA_FLAGS_LOCK_IRQ);
-	xa_init_flags(&devp->atids, XA_FLAGS_LOCK_IRQ);
-	xa_init_flags(&devp->stids, XA_FLAGS_LOCK_IRQ);
-	mutex_init(&devp->rdev.stats.lock);
-	mutex_init(&devp->db_mutex);
-	INIT_LIST_HEAD(&devp->db_fc_list);
-	init_waitqueue_head(&devp->wait);
-	devp->avail_ird = devp->rdev.lldi.max_ird_adapter;
-
 	if (c4iw_debugfs_root) {
 		devp->debugfs_root = debugfs_create_dir(
 					pci_name(devp->rdev.lldi.pdev),
diff --git a/drivers/infiniband/hw/cxgb4/iw_cxgb4.h b/drivers/infiniband/hw/cxgb4/iw_cxgb4.h
index e17c1252536b..2ab852e611c6 100644
--- a/drivers/infiniband/hw/cxgb4/iw_cxgb4.h
+++ b/drivers/infiniband/hw/cxgb4/iw_cxgb4.h
@@ -1003,7 +1003,8 @@ struct ib_mr *c4iw_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
 			    u32 max_num_sg);
 int c4iw_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
 		   unsigned int *sg_offset);
-void c4iw_dealloc(struct uld_ctx *ctx);
+void c4iw_dealloc(struct ib_device *ibdev);
+extern const struct ib_device_ops c4iw_dev_ops;
 struct ib_mr *c4iw_reg_user_mr(struct ib_pd *pd, u64 start,
 					   u64 length, u64 virt, int acc,
 					   struct ib_dmah *dmah,
diff --git a/drivers/infiniband/hw/cxgb4/provider.c b/drivers/infiniband/hw/cxgb4/provider.c
index 904a5323a693..8fa9b88f3b2a 100644
--- a/drivers/infiniband/hw/cxgb4/provider.c
+++ b/drivers/infiniband/hw/cxgb4/provider.c
@@ -460,7 +460,7 @@ static void get_dev_fw_str(struct ib_device *dev, char *str)
 		 FW_HDR_FW_VER_BUILD_G(c4iw_dev->rdev.lldi.fw_vers));
 }
 
-static const struct ib_device_ops c4iw_dev_ops = {
+const struct ib_device_ops c4iw_dev_ops = {
 	.owner = THIS_MODULE,
 	.driver_id = RDMA_DRIVER_CXGB4,
 	.uverbs_abi_ver = C4IW_UVERBS_ABI_VERSION,
@@ -472,6 +472,7 @@ static const struct ib_device_ops c4iw_dev_ops = {
 	.create_cq = c4iw_create_cq,
 	.create_qp = c4iw_create_qp,
 	.create_srq = c4iw_create_srq,
+	.dealloc_driver = c4iw_dealloc,
 	.dealloc_pd = c4iw_deallocate_pd,
 	.dealloc_ucontext = c4iw_dealloc_ucontext,
 	.dereg_mr = c4iw_dereg_mr,
@@ -552,7 +553,6 @@ void c4iw_register_device(struct work_struct *work)
 	memcpy(dev->ibdev.iw_ifname, dev->rdev.lldi.ports[0]->name,
 	       sizeof(dev->ibdev.iw_ifname));
 
-	ib_set_device_ops(&dev->ibdev, &c4iw_dev_ops);
 	ret = set_netdevs(&dev->ibdev, &dev->rdev);
 	if (ret)
 		goto err_dealloc_ctx;
@@ -566,7 +566,8 @@ void c4iw_register_device(struct work_struct *work)
 err_dealloc_ctx:
 	pr_err("%s - Failed registering iwarp device: %d\n",
 	       pci_name(ctx->lldi.pdev), ret);
-	c4iw_dealloc(ctx);
+	ib_dealloc_device(&dev->ibdev);
+	ctx->dev = NULL;
 	return;
 }
 
-- 
2.53.0


  reply	other threads:[~2026-11-26 17:09 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 13:54 [PATCH] RDMA/cxgb4: Unmap BAR2 when c4iw_rdev_open() fails Serhat Kumral
2026-11-22 13:57 ` sashiko-bot
2026-11-23 16:51 ` Jason Gunthorpe
2026-11-26 17:08   ` Serhat Kumral [this message]
2026-11-26 17:19     ` [PATCH v2] RDMA/cxgb4: Use dealloc_driver op to fix ioremap leak on alloc failure sashiko-bot

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=20261126170841.11048-1-serhatkumral1@gmail.com \
    --to=serhatkumral1@gmail.com \
    --cc=bharat@chelsio.com \
    --cc=jgg@ziepe.ca \
    --cc=larrystevenwise@gmail.com \
    --cc=leon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=rolanddreier@rivian.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