Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mahanta Jambigi" <mjambigi@linux.ibm.com>, linux-rdma@vger.kernel.org
Subject: Re: [PATCH rdma-next v2] RDMA/mlx5: Read the CQ consumer index under cq->lock when arming
Date: Thu, 26 Nov 2026 07:09:41 +0000	[thread overview]
Message-ID: <sashiko-outbox-234933@kernel.org> (raw)
In-Reply-To: <20261126065219.1367677-1-mjambigi@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] RDMA/mlx5: stale cons_index and arm_db race in
  mlx5_ib_arm_cq()
--

commit 8599d5eedfaa0e58f45970b9ef54ea48dbceee76
Author: Mahanta Jambigi <mjambigi@linux.ibm.com>

RDMA/mlx5: Read the CQ consumer index under cq->lock when arming

This commit snapshots cq->mcq.cons_index under cq->lock in
mlx5_ib_arm_cq() before passing it to mlx5_cq_arm().

> diff --git a/drivers/infiniband/hw/mlx5/cq.c b/drivers/infiniband/hw/mlx5/cq.c
> index 2581a7693585b..65badcb90d34a 100644
> --- a/drivers/infiniband/hw/mlx5/cq.c
> +++ b/drivers/infiniband/hw/mlx5/cq.c
> @@ -651,6 +651,7 @@ int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
>  	struct mlx5_ib_cq *cq = to_mcq(ibcq);
>  	void __iomem *uar_page = mdev->priv.bfreg.up->map;
>  	unsigned long irq_flags;
> +	u32 cons_index;
>  	int ret = 0;
>  
>  	spin_lock_irqsave(&cq->lock, irq_flags);
> @@ -659,12 +660,16 @@ int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
>  
>  	if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !list_empty(&cq->wc_list))
>  		ret = 1;
> +	/* The poll path advances cons_index under cq->lock; take the
> +	 * snapshot here so the doorbell sees a consistent index.
> +	 */
> +	cons_index = cq->mcq.cons_index;
>  	spin_unlock_irqrestore(&cq->lock, irq_flags);
>  
>  	mlx5_cq_arm(&cq->mcq,
>  		    (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
>  		    MLX5_CQ_DB_REQ_NOT_SOL : MLX5_CQ_DB_REQ_NOT,
> -		    uar_page, to_mcq(ibcq)->mcq.cons_index);
> +		    uar_page, cons_index);
>  
>  	return ret;
>  }

[Severity: Medium]
Can releasing cq->lock before calling mlx5_cq_arm() overwrite *cq->mcq.arm_db
and MLX5_CQ_DOORBELL with a stale cons_index and arm_sn?

Because cq->mcq.cons_index is an aligned u32, reading it under cq->lock and
dropping the lock before mlx5_cq_arm() does not serialize the doorbell update
against concurrent polling and re-arming. Moving the read before
spin_unlock_irqrestore() also opens a window on the local CPU when a
completion interrupt arrives while IRQs are disabled under cq->lock:

smc_wr_tx_send()
  mlx5_ib_arm_cq()
    spin_lock_irqsave(&cq->lock, irq_flags)
    cons_index = cq->mcq.cons_index;  /* N */
    spin_unlock_irqrestore(&cq->lock, irq_flags)
    <hardirq>
      mlx5_eq_comp_int()
        ++cq->arm_sn;
    <softirq>
      smc_wr_tx_tasklet_fn()
        mlx5_ib_poll_cq()             /* advances cons_index to N + k */
        mlx5_ib_arm_cq()
          mlx5_cq_arm(..., N + k)     /* writes arm_db with N + k */
    mlx5_cq_arm(..., N)               <-- rewinds arm_db and doorbell to N

If the outer caller is delayed between reading cq->arm_sn in mlx5_cq_arm()
and writing *cq->arm_db while an EQE increments cq->arm_sn and the tasklet
polls and re-arms the CQ, the delayed write also overwrites *cq->arm_db with
an already-fired sequence number, which can lose a completion notification
during doorbell recovery.

Should mlx5_cq_arm() be called before spin_unlock_irqrestore(&cq->lock,
irq_flags) so that reading cons_index, updating *cq->mcq.arm_db, and ringing
MLX5_CQ_DOORBELL are serialized by cq->lock?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126065219.1367677-1-mjambigi@linux.ibm.com?part=1

  reply	other threads:[~2026-11-26  7:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  6:52 [PATCH rdma-next v2] RDMA/mlx5: Read the CQ consumer index under cq->lock when arming Mahanta Jambigi
2026-11-26  7:09 ` sashiko-bot [this message]
2026-11-26  7:33   ` Mahanta Jambigi

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=sashiko-outbox-234933@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=mjambigi@linux.ibm.com \
    --cc=sashiko-reviews@lists.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