* [PATCH] RDMA/core: fix refcount leak in __ib_alloc_pd()
@ 2026-06-08 8:56 Wentao Liang
2026-06-08 18:32 ` Jason Gunthorpe
0 siblings, 1 reply; 2+ messages in thread
From: Wentao Liang @ 2026-06-08 8:56 UTC (permalink / raw)
To: jgg, leon; +Cc: linux-rdma, linux-kernel, Wentao Liang, stable
The error handling in __ib_alloc_pd() has a refcount leak. When
get_dma_mr() fails it calls ib_dealloc_pd() which invokes
ib_dealloc_pd_user(). If the driver's dealloc_pd operation returns
an error, ib_dealloc_pd_user() returns early and skips both
rdma_restrack_del() and kfree(pd). This leaves the resource tracking
kref held and the pd memory unfreed. Because ib_dealloc_pd() has a
void return, __ib_alloc_pd() cannot detect the failure, so the leak
persists.
Fix it by always calling rdma_restrack_del() and kfree(pd) in
ib_dealloc_pd_user(), even when the driver callback fails. This
ensures the software state is cleaned up regardless of the hardware
operation result.
Cc: stable@vger.kernel.org
Fixes: 91a7c58fce06 ("RDMA: Restore ability to fail on PD deallocate")
Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
---
drivers/infiniband/core/verbs.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/infiniband/core/verbs.c b/drivers/infiniband/core/verbs.c
index bac87de9cc67..6437ede11908 100644
--- a/drivers/infiniband/core/verbs.c
+++ b/drivers/infiniband/core/verbs.c
@@ -398,8 +398,11 @@ int ib_dealloc_pd_user(struct ib_pd *pd, struct ib_udata *udata)
}
ret = pd->device->ops.dealloc_pd(pd, udata);
- if (ret)
+ if (ret) {
+ rdma_restrack_del(&pd->res);
+ kfree(pd);
return ret;
+ }
rdma_restrack_del(&pd->res);
kfree(pd);
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] RDMA/core: fix refcount leak in __ib_alloc_pd()
2026-06-08 8:56 [PATCH] RDMA/core: fix refcount leak in __ib_alloc_pd() Wentao Liang
@ 2026-06-08 18:32 ` Jason Gunthorpe
0 siblings, 0 replies; 2+ messages in thread
From: Jason Gunthorpe @ 2026-06-08 18:32 UTC (permalink / raw)
To: Wentao Liang; +Cc: leon, linux-rdma, linux-kernel, stable
On Mon, Jun 08, 2026 at 08:56:25AM +0000, Wentao Liang wrote:
> The error handling in __ib_alloc_pd() has a refcount leak.
No it doesn't, the error handling is as designed.
> When get_dma_mr() fails it calls ib_dealloc_pd() which invokes
> ib_dealloc_pd_user(). If the driver's dealloc_pd operation returns
> an error, ib_dealloc_pd_user() returns early and skips both
> rdma_restrack_del() and kfree(pd). This leaves the resource
> tracking kref held and the pd memory unfreed. Because
> ib_dealloc_pd() has a void return, __ib_alloc_pd() cannot detect the
> failure, so the leak persists.
Yes, we can't handle failures on this path, the kernel verb
ib_dealloc_pd() could gain a WARN_ON to make these clearer, but the
correct thing for the user facing API is to just leave it as is
untouched.
If you mess with this you break the uverbs stuff pretty badly.
Jason
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-06-08 18:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-08 8:56 [PATCH] RDMA/core: fix refcount leak in __ib_alloc_pd() Wentao Liang
2026-06-08 18:32 ` Jason Gunthorpe
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®