In bnxt_re_setup_qp_swqs(), which only runs for kernel QPs (!qplqp->is_user; user QPs return early at the top of the function), if rq->swq allocation fails when !qplqp->srq after sq->swq has already been allocated, the error path frees sq->swq via kfree(sq->swq) without resetting sq->swq to NULL. Both callers, bnxt_qplib_create_qp1() and bnxt_qplib_create_qp(), destroy the firmware QP and propagate the error back to bnxt_re_create_qp(), whose free_hwq unwind label calls bnxt_qplib_free_qp_res(). Because qp->sq.swq is still non-NULL, bnxt_qplib_free_qp_res() calls kfree(qp->sq.swq) a second time on the already-freed buffer: BUG: KASAN: double-free in bnxt_qplib_free_qp_res Call Trace: kfree bnxt_qplib_free_qp_res bnxt_re_create_qp Clear sq->swq to NULL after freeing it in bnxt_re_setup_qp_swqs() so subsequent cleanup via bnxt_qplib_free_qp_res() is safe. Fixes: 13f9a813eee5 ("RDMA/bnxt_re: Refactor bnxt_qplib_create_qp() function") Cc: stable@vger.kernel.org Signed-off-by: Yuansheng Yang <0xbeac@gmail.com> --- Compiled drivers/infiniband/hw/bnxt_re/qplib_fp.o with W=1 on x86_64 (0 warnings, 0 errors) and verified on v7.4-rc5 in an x86_64 KASAN QEMU guest via bnxt_qplib_create_qp() failing on rq->swq allocation followed by bnxt_qplib_free_qp_res(): before this fix, KASAN reports a double-free in bnxt_qplib_free_qp_res(); after this fix, 10/10 iterations unwind cleanly with qp->sq.swq == NULL and 0 KASAN, BUG, or WARNING diagnostics. drivers/infiniband/hw/bnxt_re/qplib_fp.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/infiniband/hw/bnxt_re/qplib_fp.c b/drivers/infiniband/hw/bnxt_re/qplib_fp.c index 5301e4f21..aee7407dc 100644 --- a/drivers/infiniband/hw/bnxt_re/qplib_fp.c +++ b/drivers/infiniband/hw/bnxt_re/qplib_fp.c @@ -832,6 +832,7 @@ static int bnxt_re_setup_qp_swqs(struct bnxt_qplib_qp *qplqp) return 0; free_sq_swq: kfree(sq->swq); + sq->swq = NULL; return rc; } -- 2.53.0