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] RDMA/mlx5: Read the CQ consumer index under cq->lock when arming
Date: Wed, 25 Nov 2026 08:56:37 +0000	[thread overview]
Message-ID: <sashiko-outbox-232958@kernel.org> (raw)
In-Reply-To: <20261125084054.1349677-1-mjambigi@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] RDMA/mlx5: race on arm_db and doorbell in mlx5_ib_arm_cq()
  outside cq->lock
- [Low] RDMA/mlx5: inaccurate Fixes trailer in mlx5_ib_arm_cq() commit
  message
--

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

> RDMA/mlx5: Read the CQ consumer index under cq->lock when arming
>
> mlx5_ib_arm_cq() reads cq->mcq.cons_index after dropping cq->lock, while
> the poll path advances the index under that lock. A ULP that arms the CQ
> from process context while the completion tasklet polls races on the
> index, as SMC-R does from smc_wr_tx_send():
>
>     BUG: KCSAN: data-race in mlx5_ib_arm_cq [mlx5_ib] / mlx5_poll_one
>     [mlx5_ib]
>
> Read the index under cq->lock, which every writer of it already holds,
> so the doorbell is programmed with a consistent value. A stale index can
> only cause a spurious completion event, never a lost one.
>
> Fixes: ce0f75093282 ("net/mlx5_core: Modify arm CQ in preparation for upcoming Ethernet driver")
> Signed-off-by: Mahanta Jambigi <mjambigi@linux.ibm.com>

[Severity: Low]
Is ce0f75093282 ("net/mlx5_core: Modify arm CQ in preparation for upcoming
Ethernet driver") the right commit for the Fixes: tag?  That commit only
moved the unlocked read of cons_index from inside mlx5_cq_arm() to its caller
at a time when mlx5_ib_arm_cq() did not take cq->lock at all.  The unlocked
read against mlx5_poll_one() dates back to e126ba97dba9 ("mlx5: Add driver
for Mellanox Connect-IB adapters"), and cq->lock in mlx5_ib_arm_cq() was
introduced by 25361e02c448 ("IB/mlx5: Generate completions in software").

> 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 dropping cq->lock before mlx5_cq_arm() still let a delayed caller
overwrite *cq->mcq.arm_db and MLX5_CQ_DOORBELL out of order?

In SMC-R, smc_wr_tx_send() arms roce_cq_send from process context while
smc_wr_tx_tasklet_fn() polls and re-arms the same CQ from softirq context:

CPU0 (smc_wr_tx_send)             CPU1 (smc_wr_tx_tasklet_fn)
mlx5_ib_arm_cq()
  spin_lock_irqsave(&cq->lock)
  cons_index = cq->mcq.cons_index (N)
  spin_unlock_irqrestore(&cq->lock)
  mlx5_cq_arm(..., N)
    sn = cq->arm_sn & 3
                                  <hardirq>
                                    mlx5_eq_comp_int()
                                      cq->arm_sn++
                                  mlx5_ib_poll_cq()
                                    cons_index = N + k
                                  mlx5_ib_arm_cq()
                                    mlx5_cq_arm(..., N + k)
                                      *cq->arm_db = (sn + 1) | (N + k)
                                      mlx5_write64(..., (sn + 1) | (N + k))
    *cq->arm_db = sn | N  <-- overwrites with stale sn and index
    mlx5_write64(..., sn | N)

Even without an intervening EQE, CPU0 rewinds *cq->mcq.arm_db and the MMIO
doorbell from N + k back to N; if a completion EQE also incremented
cq->arm_sn before CPU1 re-armed, CPU0 overwrites *cq->mcq.arm_db in host DMA
memory with the already-fired sn, which can lose the completion event if the
HCA reads the doorbell record.  Concurrent mlx5_ib_arm_cq() callers also
still have a data race on *cq->mcq.arm_db and unserialized 32-bit doorbell
writes in mlx5_write64() on 32-bit architectures.

Calling mlx5_cq_arm() before spin_unlock_irqrestore(&cq->lock, irq_flags)
would serialize the cons_index snapshot, the *cq->mcq.arm_db update, and the
MMIO doorbell write.

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

      reply	other threads:[~2026-11-25  8:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  8:40 [PATCH rdma-next] RDMA/mlx5: Read the CQ consumer index under cq->lock when arming Mahanta Jambigi
2026-11-25  8:56 ` sashiko-bot [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=sashiko-outbox-232958@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