Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH rdma-next v2] RDMA/mlx5: Read the CQ consumer index under cq->lock when arming
@ 2026-11-26  6:52 Mahanta Jambigi
  2026-11-26  7:09 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Mahanta Jambigi @ 2026-11-26  6:52 UTC (permalink / raw)
  To: linux-rdma; +Cc: Leon Romanovsky, Jason Gunthorpe, 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 runtime writer already holds,
so the value handed to mlx5_cq_arm() is a synchronized snapshot. A stale
index can cause a spurious completion event, never a lost one.

Signed-off-by: Mahanta Jambigi <mjambigi@linux.ibm.com>
---
Changes in v2:
- Drop the Fixes: tag. The unlocked read predates ce0f75093282; the race
  is as old as the driver and this is a KCSAN annotation rather than a
  regression fix.
- Reword the justification to cover only what the change does: the value
  handed to mlx5_cq_arm() is a synchronized snapshot. The arm record and
  the doorbell can still be overwritten by concurrent arm callers, which
  is a separate pre-existing issue and not addressed here.

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] 3+ messages in thread

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

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

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

* Re: [PATCH rdma-next v2] RDMA/mlx5: Read the CQ consumer index under cq->lock when arming
  2026-11-26  7:09 ` sashiko-bot
@ 2026-11-26  7:33   ` Mahanta Jambigi
  0 siblings, 0 replies; 3+ messages in thread
From: Mahanta Jambigi @ 2026-11-26  7:33 UTC (permalink / raw)
  To: sashiko-reviews, linux-rdma



On 26/11/26 12:39 pm, sashiko-bot@kernel.org wrote:
> 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?

That ordering is *pre-existing* and outside this change: the arm record
and the doorbell have always been written after cq->lock is dropped, and
the v2 changelog already records it as a separate issue. Calling
mlx5_cq_arm() under cq->lock would not close it either, because
cq->arm_sn is incremented by mlx5_eq_comp_int() in the EQ hardirq
without that lock, so the stale-sn case remains. A complete fix has to
serialize arm_sn, *arm_db and the doorbell write against the EQ handler;
that is mlx5 work, not part of this cons_index cleanup.


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

end of thread, other threads:[~2026-11-26  7:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2026-11-26  7:33   ` Mahanta Jambigi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox