RDMA/core: fix refcount leak in __ib_alloc_pd()
Subsystems (2)
PATCH 1/1 RDMA/core: fix refcount leak in __ib_alloc_pd()
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.
--- drivers/infiniband/core/verbs.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/infiniband/core/verbs.c +4 -1
@@ -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);
Signature · 1 line
2.34.1
Quoted 1 line · Wentao Liang
The error handling in
__ib_alloc_pd()has a refcount leak.
No it doesn't, the error handling is as designed.
Quoted 7 lines
When
get_dma_mr()fails it callsib_dealloc_pd()which invokesib_dealloc_pd_user(). If the driver'sdealloc_pdoperation returns an error,ib_dealloc_pd_user()returns early and skips bothrdma_restrack_del()and kfree(pd). This leaves the resource tracking kref held and the pd memory unfreed. Becauseib_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