The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Leon Romanovsky <leon@kernel.org>
To: Zhu Yanjun <yanjun.zhu@linux.dev>
Cc: WenTao Liang <vulab@iscas.ac.cn>, Jason Gunthorpe <jgg@ziepe.ca>,
	linux-rdma@vger.kernel.org, stable@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] fix: infiniband/rxe: check_rkey: fix refcount underflow due to unchecked rxe_get return value
Date: Sun, 5 Jul 2026 15:26:23 +0300	[thread overview]
Message-ID: <20260705122623.GE15188@unreal> (raw)
In-Reply-To: <f3a8dd38-7f9e-42d4-8e53-a5d1a973979d@linux.dev>

On Sat, Jun 27, 2026 at 07:09:07PM -0700, Zhu Yanjun wrote:
> 
> 在 2026/6/27 4:45, WenTao Liang 写道:
> > 
> > 
> > > 2026年6月27日 09:42,yanjun.zhu <yanjun.zhu@linux.dev> 写道:
> > > 
> > > On 6/26/26 8:05 AM, WenTao Liang wrote:
> > > > rxe_get is a conditional get (kref_get_unless_zero) that returns 0 when
> > > >   the object's refcount is already zero. In check_rkey, the
> > > > return value of
> > > >   rxe_get(mr) is ignored. If rxe_get fails (returns 0), the code
> > > > continues
> > > >   to use mr without a valid reference, and error paths will call
> > > >   rxe_put(mr) on an unheld reference, causing a refcount underflow.
> > > > Check the return value of rxe_get and bail out with an error
> > > > when it fails.
> > > > Cc: stable@vger.kernel.org
> > > > Fixes: 290c4a902b79 ("RDMA/rxe: Fix \"Replace mr by rkey in
> > > > responder resources\"")
> > > > Signed-off-by: WenTao Liang <vulab@iscas.ac.cn>
> > > > ---
> > > >  drivers/infiniband/sw/rxe/rxe_resp.c | 7 ++++++-
> > > >  1 file changed, 6 insertions(+), 1 deletion(-)
> > > > diff --git a/drivers/infiniband/sw/rxe/rxe_resp.c
> > > > b/drivers/infiniband/sw/rxe/rxe_resp.c
> > > > index 9cb2f6fbf2dd..0c3f3930b494 100644
> > > > --- a/drivers/infiniband/sw/rxe/rxe_resp.c
> > > > +++ b/drivers/infiniband/sw/rxe/rxe_resp.c
> > > > @@ -514,7 +514,12 @@ static enum resp_states check_rkey(struct
> > > > rxe_qp *qp,
> > > > if (mw->access & IB_ZERO_BASED)
> > > > qp->resp.offset = mw->addr;
> > > >  -rxe_get(mr);
> > > > +if (!rxe_get(mr)) {
> > > 
> > > Can you reproduce this (rxe_get(mr) = 0)?
> > > 
> > > Thanks a lot.
> > > 
> > > Zhu Yanjun
> > > 
> > > > +rxe_put(mw);
> > > > +mw = NULL;
> > > > +state = get_rkey_violation_state(pkt);
> > > > +goto err;
> > > > +}
> > > > rxe_put(mw);
> > > > mw = NULL;
> > > > } else {
> > 
> > Hi Zhu Yanjun,
> > 
> > Thank you for reviewing the patch.
> > 
> > I haven't been able to reproduce the exact scenario where rxe_get(mr)
> > returns 0 in testing, because the race window is extremely narrow.
> > 
> > However, this patch is a defensive fix based on code analysis:
> > 
> > 1. rxe_get() is a wrapper around kref_get_unless_zero and explicitly
> >    returns a success/failure indication. Ignoring the return value
> >    violates the API contract.
> static inline __must_check
> bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp)
> {
>     int old = refcount_read(r);
> 
>     do {
>         if (!old)
>             break;
>     } while (!atomic_try_cmpxchg_relaxed(&r->refs, &old, old + i));
>     if (oldp)
>         *oldp = old;
> 
>     if (unlikely(old < 0 || old + i < 0))
> 
>         refcount_warn_saturate(r, REFCOUNT_ADD_NOT_ZERO_OVF);
> 
>     return old;
> }
> 
> 
> rxe_get will call the above function finally. Only mr->ref_cnt is zero, this
> rxe_get will return false.
> 
> When mr->ref_cnt is zero, mr will be destroyed in rxe_put. But in the
> function
> 
> 
>  444 static enum resp_states check_rkey(struct rxe_qp *qp,
>  445                    struct rxe_pkt_info *pkt)
> ...
>  507         mr = mw->mr;
>  508         if (!mr) {
>  509             rxe_dbg_qp(qp, "MW doesn't have an MR\n");
>  510             state = get_rkey_violation_state(pkt);
>  511             goto err;
>  512         }
>  513
>  514         if (mw->access & IB_ZERO_BASED)
>  515             qp->resp.offset = mw->addr;
>  516
>  517         rxe_get(mr);
>  518         rxe_put(mw);
>  519         mw = NULL;
> ...
> }
> 
> mr is guaranteed to be non-NULL by the checks above, and its reference count
> should be valid at this point.
> 
> It should be impossible for rxe_get() to fail here under normal conditions.

I came to the same conclusion. This rxe_get() is useless.

Thanks

      parent reply	other threads:[~2026-07-05 12:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-26 15:05 [PATCH] fix: infiniband/rxe: check_rkey: fix refcount underflow due to unchecked rxe_get return value WenTao Liang
2026-06-27  1:42 ` yanjun.zhu
     [not found]   ` <6597C9CE-970B-4650-97BD-651F8226E3D9@iscas.ac.cn>
     [not found]     ` <f3a8dd38-7f9e-42d4-8e53-a5d1a973979d@linux.dev>
2026-07-05 12:26       ` 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=20260705122623.GE15188@unreal \
    --to=leon@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=vulab@iscas.ac.cn \
    --cc=yanjun.zhu@linux.dev \
    /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