Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH rdma-next] RDMA/mlx5: Read the CQ consumer index under cq->lock when arming
@ 2026-11-25  8:40 Mahanta Jambigi
  2026-11-25  8:56 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Mahanta Jambigi @ 2026-11-25  8:40 UTC (permalink / raw)
  To: linux-rdma
  Cc: Leon Romanovsky, Jason Gunthorpe, linux-kernel, Mahanta Jambigi

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>
---
Note: mlx5_cq_arm() also reads cq->arm_sn, which eq.c increments without
cq->lock. That pair is pre-existing and separate from the race fixed here;
this patch does not change it, and I have not analyzed what a stale sn
does to the arm protocol.

 drivers/infiniband/hw/mlx5/cq.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/infiniband/hw/mlx5/cq.c b/drivers/infiniband/hw/mlx5/cq.c
index 55ffd7d4de51..8f4389b4eb6f 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;
 }
--
2.39.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH rdma-next] RDMA/mlx5: Read the CQ consumer index under cq->lock when arming
  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
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-25  8:56 UTC (permalink / raw)
  To: Mahanta Jambigi, linux-rdma

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-25  8:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox