Skip to contentSkip to conversation

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

PATCH New Mahanta Jambigi 2 participants 2 messages Nov 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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")

---
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(-)
drivers/infiniband/hw/mlx5/cq.c +6 -1
index 55ffd7d4de51..8f4389b4eb6f 100644
@@ -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

#1 lore raw
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…

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 <[email protected]>

Quoted 10 lines

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]
Quoted 6 lines

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")

[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").

Quoted 20 lines
drivers/infiniband/hw/mlx5/cq.c +5 -0
index 2581a7693585b..65badcb90d34a 100644
@@ -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;

drivers/infiniband/hw/mlx5/cq.c int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags) · line 675

Quoted 10 lines
drivers/infiniband/hw/mlx5/cq.c +1 -1
 	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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1