mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] RDMA/cxgb4: Unmap BAR2 when c4iw_rdev_open() fails
@ 2026-11-22 13:54 Serhat Kumral
  2026-11-23 16:51 ` Jason Gunthorpe
  0 siblings, 1 reply; 3+ messages in thread
From: Serhat Kumral @ 2026-11-22 13:54 UTC (permalink / raw)
  To: linux-rdma, Potnuri Bharat Teja
  Cc: Jason Gunthorpe, Leon Romanovsky, Roland Dreier, Steve Wise,
	linux-kernel, Serhat Kumral

c4iw_alloc() maps BAR2 on T5/T6 devices or its on-chip queue region
on T4 devices. If c4iw_rdev_open() fails, c4iw_alloc() releases the
IB device but leaves the mapping in place.

Unmap the mapped region before releasing the IB device.

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 | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/infiniband/hw/cxgb4/device.c b/drivers/infiniband/hw/cxgb4/device.c
index 24ac9871ce67..35dacf379294 100644
--- a/drivers/infiniband/hw/cxgb4/device.c
+++ b/drivers/infiniband/hw/cxgb4/device.c
@@ -1046,6 +1046,10 @@ static struct c4iw_dev *c4iw_alloc(const struct cxgb4_lld_info *infop)
 	ret = c4iw_rdev_open(&devp->rdev);
 	if (ret) {
 		pr_err("Unable to open CXIO rdev err %d\n", ret);
+		if (devp->rdev.bar2_kva)
+			iounmap(devp->rdev.bar2_kva);
+		if (devp->rdev.oc_mw_kva)
+			iounmap(devp->rdev.oc_mw_kva);
 		ib_dealloc_device(&devp->ibdev);
 		return ERR_PTR(ret);
 	}
-- 
2.53.0


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

* Re: [PATCH] RDMA/cxgb4: Unmap BAR2 when c4iw_rdev_open() fails
  2026-11-22 13:54 [PATCH] RDMA/cxgb4: Unmap BAR2 when c4iw_rdev_open() fails Serhat Kumral
@ 2026-11-23 16:51 ` Jason Gunthorpe
  2026-11-26 17:08   ` [PATCH v2] RDMA/cxgb4: Use dealloc_driver op to fix ioremap leak on alloc failure Serhat Kumral
  0 siblings, 1 reply; 3+ messages in thread
From: Jason Gunthorpe @ 2026-11-23 16:51 UTC (permalink / raw)
  To: Serhat Kumral
  Cc: linux-rdma, Potnuri Bharat Teja, Leon Romanovsky, Roland Dreier,
	Steve Wise, linux-kernel

On Sun, Nov 22, 2026 at 04:54:13PM +0300, Serhat Kumral wrote:
> c4iw_alloc() maps BAR2 on T5/T6 devices or its on-chip queue region
> on T4 devices. If c4iw_rdev_open() fails, c4iw_alloc() releases the
> IB device but leaves the mapping in place.
> 
> Unmap the mapped region before releasing the IB device.
> 
> 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 | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/infiniband/hw/cxgb4/device.c b/drivers/infiniband/hw/cxgb4/device.c
> index 24ac9871ce67..35dacf379294 100644
> --- a/drivers/infiniband/hw/cxgb4/device.c
> +++ b/drivers/infiniband/hw/cxgb4/device.c
> @@ -1046,6 +1046,10 @@ static struct c4iw_dev *c4iw_alloc(const struct cxgb4_lld_info *infop)
>  	ret = c4iw_rdev_open(&devp->rdev);
>  	if (ret) {
>  		pr_err("Unable to open CXIO rdev err %d\n", ret);
> +		if (devp->rdev.bar2_kva)
> +			iounmap(devp->rdev.bar2_kva);
> +		if (devp->rdev.oc_mw_kva)
> +			iounmap(devp->rdev.oc_mw_kva);
>  		ib_dealloc_device(&devp->ibdev);
>  		return ERR_PTR(ret);

You should refactor things so c4iw uses the 
		device->ops.dealloc_driver(device);

Call and puts stuff like this in there

Existing c4iw_dealloc() is possibly close to what should be in the op.

Jason

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

* [PATCH v2] RDMA/cxgb4: Use dealloc_driver op to fix ioremap leak on alloc failure
  2026-11-23 16:51 ` Jason Gunthorpe
@ 2026-11-26 17:08   ` Serhat Kumral
  0 siblings, 0 replies; 3+ messages in thread
From: Serhat Kumral @ 2026-11-26 17:08 UTC (permalink / raw)
  To: jgg
  Cc: bharat, larrystevenwise, leon, linux-kernel, linux-rdma,
	rolanddreier, serhatkumral1

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


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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-22 13:54 [PATCH] RDMA/cxgb4: Unmap BAR2 when c4iw_rdev_open() fails Serhat Kumral
2026-11-23 16:51 ` Jason Gunthorpe
2026-11-26 17:08   ` [PATCH v2] RDMA/cxgb4: Use dealloc_driver op to fix ioremap leak on alloc failure Serhat Kumral

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®