From: Leon Romanovsky <leon@kernel.org>
To: Edward Srouji <edwards@nvidia.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>,
Doug Ledford <dledford@redhat.com>,
Haggai Eran <haggaie@mellanox.com>,
linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org,
Maher Sanalla <msanalla@nvidia.com>
Subject: Re: [PATCH rdma-next] RDMA/mlx5: Free software WCs left on wc_list at CQ destroy
Date: Thu, 5 Nov 2026 21:42:22 +0200 [thread overview]
Message-ID: <20261105194222.GK8298@unreal> (raw)
In-Reply-To: <4b02dc55-7362-438c-9595-19818a60cbf4@nvidia.com>
On Thu, Nov 05, 2026 at 12:05:47PM +0200, Edward Srouji wrote:
>
>
> On 11/4/2026 4:01 PM, Leon Romanovsky wrote:
> > External email: Use caution opening links or attachments
> >
> >
> > On Tue, Nov 03, 2026 at 06:55:19PM +0200, Edward Srouji wrote:
> > > Software WCs generated by mlx5_ib_generate_wc() are allocated one at a
> > > time and linked onto cq->wc_list. The only thing that ever frees them is
> > > poll_soft_wc(), i.e. a consumer actually reaping them. If the CQ is
> > > destroyed while entries are still linked, those allocations are leaked.
> >
> > Is this real issue? Just put WARN_ON(!list_empty(...)) instead.
> >
> > Thanks
>
> Yes, it's a real issue reachable on a normal device removal.
> E.g. ib_mad destroys the GSI QP and frees the CQ without polling it (while
> it may have unpolled completions).
"may"? Does it have unpolled completions or not?
> I'd say that this is a complementary fix to
> https://patch.msgid.link/20261008141209.6923-1-prathameshdeshpande7@gmail.com
> Before it, a pending notify_work still ran and polled from freed memory,
> which was the UAF it fixed.
Do you have dmesg stack trace?
Thanks
>
> >
> > >
> > > Drain the list in destroy_cq_kernel(). Nothing can be walking wc_list by
> > > then: the poll context is the only walker, notify_work the only thing
> > > that can re-arm it, and both are stopped before post_destroy_cq() runs.
> > >
> > > destroy_cq_kernel() is also the error unwind for mlx5_ib_create_cq(), so
> > > move the INIT_LIST_HEAD() of wc_list up next to the other list
> > > initialisations, ahead of anything that can jump to err_cqb. The driver
> > > object is zero-allocated, and list_for_each_entry_safe() evaluates the
> > > next linkage before testing the loop condition, so draining a wc_list
> > > that is still all zeroes would oops.
> > >
> > > Fixes: 25361e02c448 ("IB/mlx5: Generate completions in software")
> > > Reviewed-by: Maher Sanalla <msanalla@nvidia.com>
> > > Signed-off-by: Edward Srouji <edwards@nvidia.com>
> > > ---
> > > drivers/infiniband/hw/mlx5/cq.c | 13 ++++++++++++-
> > > 1 file changed, 12 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/infiniband/hw/mlx5/cq.c b/drivers/infiniband/hw/mlx5/cq.c
> > > index 55ffd7d4de517e6ce3f04c98b4edcd94b12e43b3..2f7ac350c3d8f179ad1d4d8c1181b93f3151c7ce 100644
> > > --- a/drivers/infiniband/hw/mlx5/cq.c
> > > +++ b/drivers/infiniband/hw/mlx5/cq.c
> > > @@ -933,8 +933,19 @@ static int create_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
> > > return err;
> > > }
> > >
> > > +static void drain_soft_wc(struct mlx5_ib_cq *cq)
> > > +{
> > > + struct mlx5_ib_wc *soft_wc, *next;
> > > +
> > > + list_for_each_entry_safe(soft_wc, next, &cq->wc_list, list) {
> > > + list_del(&soft_wc->list);
> > > + kfree(soft_wc);
> > > + }
> > > +}
> > > +
> > > static void destroy_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
> > > {
> > > + drain_soft_wc(cq);
> > > free_cq_buf(dev, &cq->buf);
> > > mlx5_db_free(dev->mdev, &cq->db);
> > > }
> > > @@ -1067,6 +1078,7 @@ int mlx5_ib_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
> > > spin_lock_init(&cq->lock);
> > > INIT_LIST_HEAD(&cq->list_send_qp);
> > > INIT_LIST_HEAD(&cq->list_recv_qp);
> > > + INIT_LIST_HEAD(&cq->wc_list);
> > >
> > > cqe_size = cache_line_size() == 128 ? 128 : 64;
> > > err = create_cq_kernel(dev, cq, entries, cqe_size, &cqb, &index,
> > > @@ -1102,7 +1114,6 @@ int mlx5_ib_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
> > > mlx5_ib_dbg(dev, "cqn 0x%x\n", cq->mcq.cqn);
> > > cq->mcq.event = mlx5_ib_cq_event;
> > >
> > > - INIT_LIST_HEAD(&cq->wc_list);
> > > kvfree(cqb);
> > > return 0;
> > >
> > >
> > > ---
> > > base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
> > > change-id: 20261103-free-sw-wcs-8cf40aa66fd8
> > >
> > > Best regards,
> > > --
> > > Edward Srouji <edwards@nvidia.com>
> > >
>
prev parent reply other threads:[~2026-11-05 19:42 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-03 16:55 Edward Srouji
2026-11-04 14:01 ` Leon Romanovsky
2026-11-05 10:05 ` Edward Srouji
2026-11-05 19:42 ` Leon Romanovsky [this message]
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=20261105194222.GK8298@unreal \
--to=leon@kernel.org \
--cc=dledford@redhat.com \
--cc=edwards@nvidia.com \
--cc=haggaie@mellanox.com \
--cc=jgg@ziepe.ca \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rdma@vger.kernel.org \
--cc=msanalla@nvidia.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
all inboxes | Powered by JetHome®