* [PATCH rdma-next v3] RDMA/mlx5: Read the CQ consumer index under cq->lock when arming
@ 2026-11-27 7:53 Mahanta Jambigi
2026-11-27 8:09 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Mahanta Jambigi @ 2026-11-27 7:53 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():
[ 312.234586] ==================================================================
[ 312.234707] BUG: KCSAN: data-race in mlx5_ib_arm_cq [mlx5_ib] / mlx5_poll_one [mlx5_ib]
[ 312.234933] write to 0x00000000956c9dc8 of 4 bytes by interrupt on cpu 1:
[ 312.234951] mlx5_poll_one+0xa0/0xed0 [mlx5_ib]
[ 312.235066] mlx5_ib_poll_cq+0xda/0x340 [mlx5_ib]
[ 312.235181] smc_wr_tx_tasklet_fn+0x92/0x1d0 [smc]
[ 312.235259] tasklet_action_common+0x150/0x450
[ 312.235277] handle_softirqs+0x144/0x5a0
[ 312.235294] irq_exit_rcu+0x1ba/0x1f0
[ 312.235309] do_io_irq+0xd2/0x1b0
[ 312.235326] io_int_handler+0xec/0x118
[ 312.235344] arch_cpu_idle+0xf4/0x100
[ 312.235363] arch_cpu_idle+0x48/0x100
[ 312.235382] default_idle_call+0xea/0x2d0
[ 312.235398] do_idle+0xfe/0x1e0
[ 312.235412] cpu_startup_entry+0x40/0x50
[ 312.235428] smp_start_secondary+0x218/0x250
[ 312.235447] restart_int_handler+0x72/0x88
[ 312.235475] read to 0x00000000956c9dc8 of 4 bytes by task 6316 on cpu 2:
[ 312.235492] mlx5_ib_arm_cq+0xe8/0x180 [mlx5_ib]
[ 312.235614] smc_wr_tx_send+0x6a/0x140 [smc]
[ 312.235691] smc_cdc_msg_send+0x2a4/0x360 [smc]
[ 312.235770] smcr_tx_sndbuf_nonempty+0x1f4/0x520 [smc]
[ 312.235848] smc_tx_sndbuf_nonempty+0x238/0x2e0 [smc]
[ 312.235926] smc_tx_sendmsg+0x92a/0xa80 [smc]
[ 312.236003] smc_sendmsg+0x160/0x310 [smc]
[ 312.236078] __sock_sendmsg+0x96/0x110
[ 312.236098] sock_write_iter+0x102/0x180
[ 312.236115] vfs_write+0x3fe/0x740
[ 312.236131] ksys_write+0x10e/0x150
[ 312.236147] __s390x_sys_write+0x5e/0x70
[ 312.236163] __do_syscall+0x1b4/0x580
[ 312.236181] system_call+0x72/0x90
[ 312.236206] locks held by iperf3/6316: 2, on CPU#2:
[ 312.236221] #0: 00000001018dbe58 (sk_lock-AF_SMC){+.+.}-{0:0}, at: smc_sendmsg+0x46/0x310 [smc]
[ 312.236321] #1: 00000001018dc358 (&smc->conn.send_lock){+.-.}-{2:2}, at: smcr_tx_sndbuf_nonempty+0x17c/0x520 [smc]
[ 312.236423] irq event stamp: 45730
[ 312.236435] hardirqs last enabled at (45729): [<000003a606250966>] _raw_spin_unlock_irqrestore+0x66/0x80
[ 312.236461] hardirqs last disabled at (45730): [<000003a604e5d228>] kcsan_setup_watchpoint+0x4f8/0x590
[ 312.236485] softirqs last enabled at (45723): [<000003a605d3aaae>] lock_sock_nested+0x8e/0xb0
[ 312.236509] softirqs last disabled at (45725): [<000003a5857d155c>] smcr_tx_sndbuf_nonempty+0x17c/0x520 [smc]
[ 312.236601] value changed: 0x0000063d -> 0x0000063e
[ 312.236623] Reported by Kernel Concurrency Sanitizer on:
[ 312.236636] CPU: 2 UID: 0 PID: 6316 Comm: iperf3 Kdump: loaded Not tainted 7.4.0-rc3-kcsan-00406-g65e4fd942f72 #1 PREEMPT
[ 312.236674] ==================================================================
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 v3:
- Add the full untrimmed KCSAN report to the commit message, as
requested.
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] 2+ messages in thread* Re: [PATCH rdma-next v3] RDMA/mlx5: Read the CQ consumer index under cq->lock when arming
2026-11-27 7:53 [PATCH rdma-next v3] RDMA/mlx5: Read the CQ consumer index under cq->lock when arming Mahanta Jambigi
@ 2026-11-27 8:09 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-27 8: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 cd4a8e789a4da161bb086eeea0e1e95d45a10710
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 unlocking and calling 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 snapshotting cons_index before spin_unlock_irqrestore() cause
mlx5_cq_arm() to overwrite *cq->mcq.arm_db and MLX5_CQ_DOORBELL with a
stale index?
Reading cons_index under cq->lock and dropping the lock before calling
mlx5_cq_arm() does not serialize the doorbell update against concurrent
polls and re-arms, and introduces a local-CPU race across interrupt
re-enablement when called from process context with softirqs enabled
(such as smc_wr_reg_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()
ib_poll_cq() /* cons_index -> N + k */
ib_req_notify_cq()
mlx5_cq_arm(..., N + k)
mlx5_cq_arm(..., N) <-- rewinds arm_db to N
Before this change, cons_index was read after spin_unlock_irqrestore()
returned and observed N + k on the local CPU. Across CPUs, if the first
caller also reads cq->mcq.arm_sn in mlx5_cq_arm() before
mlx5_eq_comp_int() increments it and the tasklet re-arms the CQ at
(sn + 1, N + k), the delayed caller overwrites *cq->mcq.arm_db in DMA
memory with the already-fired sequence number sn and stale index N,
which can cause spurious interrupts or a lost completion notification
during doorbell recovery.
Calling mlx5_cq_arm() while holding cq->lock before
spin_unlock_irqrestore() would keep cons_index, *cq->mcq.arm_db, and the
doorbell write serialized with the poll path.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127075323.4099367-1-mjambigi@linux.ibm.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-27 8:09 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 7:53 [PATCH rdma-next v3] RDMA/mlx5: Read the CQ consumer index under cq->lock when arming Mahanta Jambigi
2026-11-27 8:09 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox