Skip to contentSkip to conversation

net/smc: transition to RDMA core CQ pooling

PATCH In review D. Wythe 5 participants 9 messages May 28 – Jun 12

Subsystems (5)
lore
Share & tools
lore

D. Wythe

author maintainer
#0 lore raw

cover net/smc: transition to RDMA core CQ pooling

This series transitions SMC-R completion handling to RDMA core CQ pooling via the ib_cqe API. The new completion model improves scalability by allowing per-link completion processing across multiple cores and enables DIM-based interrupt moderation.

As a side effect, the increased concurrency can amplify contention for TX slots on the shared wait queue. Patch 2 addresses this by switching TX slot allocation from non-exclusive wait_event() to prepare_to_wait_exclusive(), which avoids thundering-herd wakeups under contention.

Patch 1 replaces the global per-device CQ and manual tasklet polling model with RDMA core CQ pooling.
Patch 2 reduces TX slot contention by using exclusive wait queue entries during allocation.

---
Changes v1 -> v2:
https://lore.kernel.org/netdev/[email protected]/
1. remove unnecessary inline from static CQE init helpers.
2. Use ib_drain_qp() with +1 max_send_wr;
3. Fix v2 state clearing.
4. Add re-check after schedule_timeout() to fix timeout/signal races.

D. Wythe (2):
net/smc: transition to RDMA core CQ pooling net/smc: reduce TX slot contention with exclusive wait

 net/smc/smc_core.c |   9 +-
 net/smc/smc_core.h |  28 ++--
 net/smc/smc_ib.c   | 113 +++++----------
 net/smc/smc_ib.h   |   7 -
 net/smc/smc_tx.c   |   1 -
 net/smc/smc_wr.c   | 344 ++++++++++++++++++++-------------------------
 net/smc/smc_wr.h   |  40 ++----
 7 files changed, 215 insertions(+), 327 deletions(-)
Signature · 1 line
2.45.0
author maintainer
#1 lore raw

PATCH v2 1/2 net/smc: transition to RDMA core CQ pooling

The current SMC-R implementation relies on global per-device CQs and manual polling within tasklets, which introduces severe scalability bottlenecks due to global lock contention and tasklet scheduling overhead, resulting in poor performance as concurrency increases.

Refactor the completion handling to utilize the ib_cqe API and standard RDMA core CQ pooling. This transition provides several key advantages:

1. Multi-CQ: Shift from a single shared per-device CQ to multiple link-specific CQs via the CQ pool. This allows completion processing to be parallelized across multiple CPU cores, effectively eliminating the global CQ bottleneck.

2. Leverage DIM: Utilizing the standard CQ pool with IB_POLL_SOFTIRQ enables Dynamic Interrupt Moderation from the RDMA core, optimizing interrupt frequency and reducing CPU load under high pressure.

3. O(1) Context Retrieval: Replaces the expensive wr_id based lookup logic (e.g., smc_wr_tx_find_pending_index) with direct context retrieval using container_of() on the embedded ib_cqe.

4. Code Simplification: This refactoring results in a reduction of ~150 lines of code. It removes redundant sequence tracking, complex lookup helpers, and manual CQ management, significantly improving maintainability.

Performance Test: redis-benchmark with max 32 connections per QP Data format: Requests Per Second (RPS), Percentage in brackets represents the gain/loss compared to TCP.

| Clients | TCP      | SMC (original)      | SMC (cq_pool)       |
|---------|----------|---------------------|---------------------|
| c = 1   | 24449    | 31172  (+27%)       | 34039  (+39%)       |
| c = 2   | 46420    | 53216  (+14%)       | 64391  (+38%)       |
| c = 16  | 159673   | 83668  (-48%)  <--  | 216947 (+36%)       |
| c = 32  | 164956   | 97631  (-41%)  <--  | 249376 (+51%)       |
| c = 64  | 166322   | 118192 (-29%)  <--  | 249488 (+50%)       |
| c = 128 | 167700   | 121497 (-27%)  <--  | 249480 (+48%)       |
| c = 256 | 175021   | 146109 (-16%)  <--  | 240384 (+37%)       |
| c = 512 | 168987   | 101479 (-40%)  <--  | 226634 (+34%)       |

The results demonstrate that this optimization effectively resolves the scalability bottleneck, with RPS increasing by over 110% at c=64 compared to the original implementation.

---
 net/smc/smc_core.c |   9 +-
 net/smc/smc_core.h |  28 ++--
 net/smc/smc_ib.c   | 113 +++++-----------
 net/smc/smc_ib.h   |   7 -
 net/smc/smc_tx.c   |   1 -
 net/smc/smc_wr.c   | 324 ++++++++++++++++++---------------------------
 net/smc/smc_wr.h   |  40 ++----
 7 files changed, 196 insertions(+), 326 deletions(-)
net/smc/smc_core.c +2 -7
index cf6b620fef05..1c6c1340f12b 100644
@@ -815,17 +815,11 @@ int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
 	lnk->lgr = lgr;
 	smc_lgr_hold(lgr); /* lgr_put in smcr_link_clear() */
 	lnk->link_idx = link_idx;
-	lnk->wr_rx_id_compl = 0;
 	smc_ibdev_cnt_inc(lnk);
 	smcr_copy_dev_info_to_link(lnk);
 	atomic_set(&lnk->conn_cnt, 0);
 	smc_llc_link_set_uid(lnk);
 	INIT_WORK(&lnk->link_down_wrk, smc_link_down_work);
-	if (!lnk->smcibdev->initialized) {
-		rc = (int)smc_ib_setup_per_ibdev(lnk->smcibdev);
-		if (rc)
-			goto out;
-	}
 	get_random_bytes(rndvec, sizeof(rndvec));
 	lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) +
 		(rndvec[2] << 16);
@@ -863,6 +857,7 @@ int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
 	if (rc)
 		goto free_link_mem;
 	lnk->state = SMC_LNK_ACTIVATING;
+	smc_wr_init_cqes(lnk);
 	return 0;
 
 free_link_mem:
@@ -1373,7 +1368,7 @@ void smcr_link_clear(struct smc_link *lnk, bool log)
 	smc_llc_link_clear(lnk, log);
 	smcr_buf_unmap_lgr(lnk);
 	smcr_rtoken_clear_link(lnk);
-	smc_ib_modify_qp_error(lnk);
+	smc_wr_drain_qp(lnk);
 	smc_wr_free_link(lnk);
 	smc_ib_destroy_queue_pair(lnk);
 	smc_ib_dealloc_protection_domain(lnk);
net/smc/smc_core.h +20 -8
index 5c18f08a4c8a..f98c0f0cb14b 100644
@@ -89,8 +89,21 @@ struct smc_rdma_sges {				/* sges per message send */
 struct smc_rdma_wr {				/* work requests per message
 						 * send
 						 */
+	struct ib_cqe		cqe;
 	struct ib_rdma_wr	wr_tx_rdma[SMC_MAX_RDMA_WRITES];
-};
+} ____cacheline_aligned_in_smp;
+
+struct smc_ib_recv_wr {
+	struct ib_cqe		cqe;
+	struct ib_recv_wr	wr;
+	int idx;
+} ____cacheline_aligned_in_smp;
+
+struct smc_ib_send_wr {
+	struct ib_cqe		cqe;
+	struct ib_send_wr	wr;
+	int idx;
+} ____cacheline_aligned_in_smp;
 
 #define SMC_LGR_ID_SIZE		4
 
@@ -100,23 +113,24 @@ struct smc_link {
 	struct ib_pd		*roce_pd;	/* IB protection domain,
 						 * unique for every RoCE QP
 						 */
+	unsigned int		nr_cqe;		/* number of CQ entries */
+	struct ib_cq		*ib_cq;		/* IB completion queue */
 	struct ib_qp		*roce_qp;	/* IB queue pair */
 	struct ib_qp_attr	qp_attr;	/* IB queue pair attributes */
 
 	struct smc_wr_buf	*wr_tx_bufs;	/* WR send payload buffers */
-	struct ib_send_wr	*wr_tx_ibs;	/* WR send meta data */
+	struct smc_ib_send_wr	*wr_tx_ibs;	/* WR send meta data */
 	struct ib_sge		*wr_tx_sges;	/* WR send gather meta data */
 	struct smc_rdma_sges	*wr_tx_rdma_sges;/*RDMA WRITE gather meta data*/
 	struct smc_rdma_wr	*wr_tx_rdmas;	/* WR RDMA WRITE */
 	struct smc_wr_tx_pend	*wr_tx_pends;	/* WR send waiting for CQE */
 	struct completion	*wr_tx_compl;	/* WR send CQE completion */
 	/* above four vectors have wr_tx_cnt elements and use the same index */
-	struct ib_send_wr	*wr_tx_v2_ib;	/* WR send v2 meta data */
+	struct smc_ib_send_wr	*wr_tx_v2_ib;	/* WR send v2 meta data */
 	struct ib_sge		*wr_tx_v2_sge;	/* WR send v2 gather meta data*/
 	struct smc_wr_tx_pend	*wr_tx_v2_pend;	/* WR send v2 waiting for CQE */
 	dma_addr_t		wr_tx_dma_addr;	/* DMA address of wr_tx_bufs */
 	dma_addr_t		wr_tx_v2_dma_addr; /* DMA address of v2 tx buf*/
-	atomic_long_t		wr_tx_id;	/* seq # of last sent WR */
 	unsigned long		*wr_tx_mask;	/* bit mask of used indexes */
 	u32			wr_tx_cnt;	/* number of WR send buffers */
 	wait_queue_head_t	wr_tx_wait;	/* wait for free WR send buf */
@@ -126,7 +140,7 @@ struct smc_link {
 	struct completion	tx_ref_comp;
 
 	u8			*wr_rx_bufs;	/* WR recv payload buffers */
-	struct ib_recv_wr	*wr_rx_ibs;	/* WR recv meta data */
+	struct smc_ib_recv_wr	*wr_rx_ibs;	/* WR recv meta data */
 	struct ib_sge		*wr_rx_sges;	/* WR recv scatter meta data */
 	/* above three vectors have wr_rx_cnt elements and use the same index */
 	int			wr_rx_sge_cnt; /* rx sge, V1 is 1, V2 is either 2 or 1 */
@@ -135,13 +149,11 @@ struct smc_link {
 						 */
 	dma_addr_t		wr_rx_dma_addr;	/* DMA address of wr_rx_bufs */
 	dma_addr_t		wr_rx_v2_dma_addr; /* DMA address of v2 rx buf*/
-	u64			wr_rx_id;	/* seq # of last recv WR */
-	u64			wr_rx_id_compl; /* seq # of last completed WR */
 	u32			wr_rx_cnt;	/* number of WR recv buffers */
 	unsigned long		wr_rx_tstamp;	/* jiffies when last buf rx */
-	wait_queue_head_t       wr_rx_empty_wait; /* wait for RQ empty */
 
 	struct ib_reg_wr	wr_reg;		/* WR register memory region */
+	struct ib_cqe		wr_reg_cqe;	/* ib_cqe for wr_reg */
 	wait_queue_head_t	wr_reg_wait;	/* wait for wr_reg result */
 	struct {
 		struct percpu_ref	wr_reg_refs;
net/smc/smc_ib.c +32 -81

171 lines not on this page · raw message

net/smc/smc_ib.h +0 -7
index ef8ac2b7546d..a75fe8bcef3a 100644
@@ -37,17 +37,12 @@ struct smc_ib_device {				/* ib-device infos for smc */
 	struct ib_device	*ibdev;
 	struct ib_port_attr	pattr[SMC_MAX_PORTS];	/* ib dev. port attrs */
 	struct ib_event_handler	event_handler;	/* global ib_event handler */
-	struct ib_cq		*roce_cq_send;	/* send completion queue */
-	struct ib_cq		*roce_cq_recv;	/* recv completion queue */
-	struct tasklet_struct	send_tasklet;	/* called by send cq handler */
-	struct tasklet_struct	recv_tasklet;	/* called by recv cq handler */
 	char			mac[SMC_MAX_PORTS][ETH_ALEN];
 						/* mac address per port*/
 	u8			pnetid[SMC_MAX_PORTS][SMC_MAX_PNETID_LEN];
 						/* pnetid per port */
 	bool			pnetid_by_user[SMC_MAX_PORTS];
 						/* pnetid defined by user? */
-	u8			initialized : 1; /* ib dev CQ, evthdl done */
 	struct work_struct	port_event_work;
 	unsigned long		port_event_mask;
 	DECLARE_BITMAP(ports_going_away, SMC_MAX_PORTS);
@@ -96,8 +91,6 @@ void smc_ib_destroy_queue_pair(struct smc_link *lnk);
 int smc_ib_create_queue_pair(struct smc_link *lnk);
 int smc_ib_ready_link(struct smc_link *lnk);
 int smc_ib_modify_qp_rts(struct smc_link *lnk);
-int smc_ib_modify_qp_error(struct smc_link *lnk);
-long smc_ib_setup_per_ibdev(struct smc_ib_device *smcibdev);
 int smc_ib_get_memory_region(struct ib_pd *pd, int access_flags,
 			     struct smc_buf_desc *buf_slot, u8 link_idx);
 void smc_ib_put_memory_region(struct ib_mr *mr);
net/smc/smc_tx.c +0 -1
index 3144b4b1fe29..d301df9ed58b 100644
@@ -321,7 +321,6 @@ static int smc_tx_rdma_write(struct smc_connection *conn, int peer_rmbe_offset,
 	struct smc_link *link = conn->lnk;
 	int rc;
 
-	rdma_wr->wr.wr_id = smc_wr_tx_get_next_wr_id(link);
 	rdma_wr->wr.num_sge = num_sges;
 	rdma_wr->remote_addr =
 		lgr->rtokens[conn->rtoken_idx][link->link_idx].dma_addr +
net/smc/smc_wr.c +132 -192

508 lines not on this page · raw message

net/smc/smc_wr.h +10 -30
index aa4533af9122..6873b86cff90 100644
@@ -44,19 +44,6 @@ struct smc_wr_rx_handler {
 	u8			type;
 };
 
-/* Only used by RDMA write WRs.
- * All other WRs (CDC/LLC) use smc_wr_tx_send handling WR_ID implicitly
- */
-static inline long smc_wr_tx_get_next_wr_id(struct smc_link *link)
-{
-	return atomic_long_inc_return(&link->wr_tx_id);
-}
-
-static inline void smc_wr_tx_set_wr_id(atomic_long_t *wr_tx_id, long val)
-{
-	atomic_long_set(wr_tx_id, val);
-}
-
 static inline bool smc_wr_tx_link_hold(struct smc_link *link)
 {
 	if (!smc_link_sendable(link))
@@ -70,9 +57,10 @@ static inline void smc_wr_tx_link_put(struct smc_link *link)
 	percpu_ref_put(&link->wr_tx_refs);
 }
 
-static inline void smc_wr_drain_cq(struct smc_link *lnk)
+static inline void smc_wr_drain_qp(struct smc_link *lnk)
 {
-	wait_event(lnk->wr_rx_empty_wait, lnk->wr_rx_id_compl == lnk->wr_rx_id);
+	if (lnk->qp_attr.cur_qp_state != IB_QPS_RESET)
+		ib_drain_qp(lnk->roce_qp);
 }
 
 static inline void smc_wr_wakeup_tx_wait(struct smc_link *lnk)
@@ -86,18 +74,12 @@ static inline void smc_wr_wakeup_reg_wait(struct smc_link *lnk)
 }
 
 /* post a new receive work request to fill a completed old work request entry */
-static inline int smc_wr_rx_post(struct smc_link *link)
+static inline int smc_wr_rx_post(struct smc_link *link, struct ib_cqe *cqe)
 {
-	int rc;
-	u64 wr_id, temp_wr_id;
-	u32 index;
-
-	wr_id = ++link->wr_rx_id; /* tasklet context, thus not atomic */
-	temp_wr_id = wr_id;
-	index = do_div(temp_wr_id, link->wr_rx_cnt);
-	link->wr_rx_ibs[index].wr_id = wr_id;
-	rc = ib_post_recv(link->roce_qp, &link->wr_rx_ibs[index], NULL);
-	return rc;
+	struct smc_ib_recv_wr *recv_wr;
+
+	recv_wr = container_of(cqe, struct smc_ib_recv_wr, cqe);
+	return ib_post_recv(link->roce_qp, &recv_wr->wr, NULL);
 }
 
 int smc_wr_create_link(struct smc_link *lnk);
@@ -107,8 +89,6 @@ void smc_wr_free_link(struct smc_link *lnk);
 void smc_wr_free_link_mem(struct smc_link *lnk);
 void smc_wr_free_lgr_mem(struct smc_link_group *lgr);
 void smc_wr_remember_qp_attr(struct smc_link *lnk);
-void smc_wr_remove_dev(struct smc_ib_device *smcibdev);
-void smc_wr_add_dev(struct smc_ib_device *smcibdev);
 
 int smc_wr_tx_get_free_slot(struct smc_link *link, smc_wr_tx_handler handler,
 			    struct smc_wr_buf **wr_buf,
@@ -126,12 +106,12 @@ int smc_wr_tx_v2_send(struct smc_link *link,
 		      struct smc_wr_tx_pend_priv *priv, int len);
 int smc_wr_tx_send_wait(struct smc_link *link, struct smc_wr_tx_pend_priv *priv,
 			unsigned long timeout);
-void smc_wr_tx_cq_handler(struct ib_cq *ib_cq, void *cq_context);
 void smc_wr_tx_wait_no_pending_sends(struct smc_link *link);
 
 int smc_wr_rx_register_handler(struct smc_wr_rx_handler *handler);
 int smc_wr_rx_post_init(struct smc_link *link);
-void smc_wr_rx_cq_handler(struct ib_cq *ib_cq, void *cq_context);
 int smc_wr_reg_send(struct smc_link *link, struct ib_mr *mr);
 
+void smc_wr_init_cqes(struct smc_link *lnk);
+
 #endif /* SMC_WR_H */
Signature · 1 line
2.45.0
maintainer
#2 lore raw

From: AI Reviewer <[email protected]>

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev/
---
net/smc: transition to RDMA core CQ pooling

The current SMC-R implementation relies on global per-device CQs and manual polling within tasklets, which introduces severe scalability bottlenecks due to global lock contention and tasklet scheduling overhead, resulting in poor performance as concurrency increases.

Refactor the completion handling to utilize the ib_cqe API and standard RDMA core CQ pooling.

Quoted 4 lines
net/smc/smc_ib.c +0 -0
index 9bb495707445e..a6d6b81830d38 100644

[ ... ]

Quoted 10 lines
net/smc/smc_ib.c +4 -0
@@ -657,38 +645,59 @@ void smc_ib_destroy_queue_pair(struct smc_link *lnk)
 	if (lnk->roce_qp)
 		ib_destroy_qp(lnk->roce_qp);
 	lnk->roce_qp = NULL;
+	if (lnk->ib_cq) {
+		ib_cq_pool_put(lnk->ib_cq, lnk->nr_cqe);
+		lnk->ib_cq = NULL;
+	}
 }
 

net/smc/smc_ib.c void smc_ib_destroy_queue_pair(struct smc_link *lnk) · line 663

Quoted 10 lines
net/smc/smc_ib.c +7 -0
 /* create a queue pair within the protection domain for a link */
 int smc_ib_create_queue_pair(struct smc_link *lnk)
 {
+	int max_send_wr, max_recv_wr, rc;
+	struct ib_cq *cq;
+
+	/* include unsolicited rdma_writes as well,
+	 * there are max. 2 RDMA_WRITE per 1 WR_SEND.
+	 */
+	max_send_wr = 3 * lnk->lgr->max_send_wr + 1;	/* +1 for ib_drain_sq() */

[Severity: High]
Does this calculation account for all possible work requests?

To support ib_drain_sq(), this patch increases SQ capacity by exactly 1, assuming standard data transmissions consume up to 3 WRs each. However, SMC also posts FastReg WRs (smc_wr_reg_send) and SMC-Rv2 LLC control messages (smc_wr_tx_v2_send) to the same SQ.

If these independent WRs are posted while data TX slots are saturated, the total in-flight WRs will consume the remaining +1 slot reserved for the drain WR.

When teardown occurs, ib_drain_sq() will attempt to post its dummy WR, but ib_post_send() will fail with -ENOMEM because the SQ is full. ib_drain_sq() responds to this failure by returning immediately without waiting for completions, unblocking teardown and freeing link structures while the hardware is still asynchronously flushing the SQ. When the softirq polls the flushed CQEs, it will access the freed ib_cqe pointers, leading to a use-after-free.

Quoted 4 lines
net/smc/smc_ib.c +4 -0
+	max_recv_wr = lnk->lgr->max_recv_wr + 1;	/* +1 for ib_drain_rq() */
+
+	cq = ib_cq_pool_get(lnk->smcibdev->ibdev, max_send_wr + max_recv_wr, -1,
+			    IB_POLL_SOFTIRQ);

[ ... ]

Quoted 4 lines
net/smc/smc_wr.c +0 -0
index 59c92b46945c5..130bc6c26fb31 100644

[ ... ]

Quoted 1 line
net/smc/smc_wr.c +0 -0
@@ -63,109 +60,67 @@ void smc_wr_tx_wait_no_pending_sends(struct smc_link *link)

[ ... ]

net/smc/smc_wr.c void smc_wr_tx_wait_no_pending_sends(struct smc_link *link) · line 106

Quoted 11 lines
net/smc/smc_wr.c +0 -8
 	if (pnd_snd_idx == link->wr_tx_cnt) {
-		if (link->lgr->smc_version != SMC_V2 ||
-		    link->wr_tx_v2_pend->wr_id != wc->wr_id)
-			return;
-		link->wr_tx_v2_pend->wc_status = wc->status;
-		memcpy(&pnd_snd, link->wr_tx_v2_pend, sizeof(pnd_snd));
-		/* clear the full struct smc_wr_tx_pend including .priv */
-		memset(link->wr_tx_v2_pend, 0,
-		       sizeof(*link->wr_tx_v2_pend));
 		memset(link->lgr->wr_tx_buf_v2, 0,
 		       sizeof(*link->lgr->wr_tx_buf_v2));

[Severity: High]
This is a pre-existing issue, but while reviewing this code I noticed a potential memory leak and NULL pointer dereference in smc_wr_tx_put_slot().

In the error path of smc_wr_tx_put_slot(), the code attempts to clear the V2 pending slots:

net/smc/smc_wr.c:smc_wr_tx_put_slot() {
    ...
	} else if (link->lgr->smc_version == SMC_V2 &&
		   pend->idx == link->wr_tx_cnt) {
		/* Large v2 buffer */
		memset(&link->wr_tx_v2_pend, 0,
		       sizeof(link->wr_tx_v2_pend));
		memset(&link->lgr->wr_tx_buf_v2, 0,
		       sizeof(link->lgr->wr_tx_buf_v2));
    ...
}

Because these target variables are pointers, sizeof evaluates to 8 bytes. The memset() call zeroes the pointer variable itself rather than clearing the underlying structure, which leaks the allocated memory.

If smc_wr_tx_get_v2_slot() is called subsequently, it will attempt to dereference the now-NULL link->wr_tx_v2_pend, leading to a direct NULL pointer dereference.

Could this be updated to use sizeof(*link->wr_tx_v2_pend) similar to the code removed above?

[ ... ]

Quoted 2 lines
net/smc/smc_wr.c +0 -0
@@ -431,94 +367,74 @@ static inline void smc_wr_rx_demultiplex(struct ib_wc *wc)
 {

[ ... ]

Quoted 24 lines

}

net/smc/smc_wr.c +2 -18
-static inline void smc_wr_rx_process_cqes(struct ib_wc wc[], int num)
+static void smc_wr_rx_process_cqe(struct ib_cq *cq, struct ib_wc *wc)
 {
-	struct smc_link *link;
-	int i;
+	struct smc_link *link = wc->qp->qp_context;
 
-	for (i = 0; i < num; i++) {
-		link = wc[i].qp->qp_context;
-		link->wr_rx_id_compl = wc[i].wr_id;
-		if (wc[i].status == IB_WC_SUCCESS) {
-			link->wr_rx_tstamp = jiffies;
-			smc_wr_rx_demultiplex(&wc[i]);
-			smc_wr_rx_post(link); /* refill WR RX */
-		} else {
-			/* handle status errors */
-			switch (wc[i].status) {
-			case IB_WC_RETRY_EXC_ERR:
-			case IB_WC_RNR_RETRY_EXC_ERR:
-			case IB_WC_WR_FLUSH_ERR:
-				smcr_link_down_cond_sched(link);
-				if (link->wr_rx_id_compl == link->wr_rx_id)

net/smc/smc_wr.c static inline void smc_wr_rx_demultiplex(struct ib_wc *wc) · line 392

Quoted 10 lines
net/smc/smc_wr.c +4 -6
-					wake_up(&link->wr_rx_empty_wait);
-				break;
-			default:
-				smc_wr_rx_post(link); /* refill WR RX */
-				break;
-			}
+	if (wc->status == IB_WC_SUCCESS) {
+		link->wr_rx_tstamp = jiffies;
+		smc_wr_rx_demultiplex(wc);
+		smc_wr_rx_post(link, wc->wr_cqe); /* refill WR RX */

[Severity: Critical]
Can this cause a use-after-free when racing with queue teardown?

The patch replaces smc_wr_drain_cq() with ib_drain_qp(). ib_drain_rq() transitions the QP to IB_QPS_ERR and posts a dummy drain WR to wait for completions.

However, smc_wr_rx_process_cqe() unconditionally reposts a new WR when processing a successful packet completion. If a valid RX completion is polled in the softirq concurrently with ib_drain_rq(), the softirq will post a new WR after the drain WR has been posted.

Since the QP is in the ERR state, the new WR will be immediately completed with a flush error, but its CQE will be generated after the drain WR's CQE. This allows ib_drain_rq() to return and unblock link teardown, freeing link->wr_rx_ibs.

When the softirq subsequently polls the flush CQE for the newly posted WR, it will dereference wc->wr_cqe->done. Since wc->wr_cqe points to the freed smc_ib_recv_wr, this results in a use-after-free.

Quoted 14 lines
net/smc/smc_wr.c +11 -0
+	} else {
+		/* handle status errors */
+		switch (wc->status) {
+		case IB_WC_RETRY_EXC_ERR:
+		case IB_WC_RNR_RETRY_EXC_ERR:
+		case IB_WC_WR_FLUSH_ERR:
+			smcr_link_down_cond_sched(link);
+			break;
+		default:
+			smc_wr_rx_post(link, wc->wr_cqe); /* refill WR RX */
+			break;
 		}
 	}
 }
Signature · 1 line
This is an AI-generated review.
author maintainer
#3 lore raw
Quoted 57 lines · Paolo Abeni

From: AI Reviewer <[email protected]>

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev/
---
net/smc: transition to RDMA core CQ pooling

The current SMC-R implementation relies on global per-device CQs and manual polling within tasklets, which introduces severe scalability bottlenecks due to global lock contention and tasklet scheduling overhead, resulting in poor performance as concurrency increases.

Refactor the completion handling to utilize the ib_cqe API and standard RDMA core CQ pooling.

Quoted 4 lines
net/smc/smc_ib.c +0 -0
index 9bb495707445e..a6d6b81830d38 100644

[ ... ]

Quoted 10 lines
net/smc/smc_ib.c +4 -0
@@ -657,38 +645,59 @@ void smc_ib_destroy_queue_pair(struct smc_link *lnk)
 	if (lnk->roce_qp)
 		ib_destroy_qp(lnk->roce_qp);
 	lnk->roce_qp = NULL;
+	if (lnk->ib_cq) {
+		ib_cq_pool_put(lnk->ib_cq, lnk->nr_cqe);
+		lnk->ib_cq = NULL;
+	}
 }
 

net/smc/smc_ib.c void smc_ib_destroy_queue_pair(struct smc_link *lnk) · line 663

Quoted 10 lines
net/smc/smc_ib.c +7 -0
 /* create a queue pair within the protection domain for a link */
 int smc_ib_create_queue_pair(struct smc_link *lnk)
 {
+	int max_send_wr, max_recv_wr, rc;
+	struct ib_cq *cq;
+
+	/* include unsolicited rdma_writes as well,
+	 * there are max. 2 RDMA_WRITE per 1 WR_SEND.
+	 */
+	max_send_wr = 3 * lnk->lgr->max_send_wr + 1;	/* +1 for ib_drain_sq() */

[Severity: High]
Does this calculation account for all possible work requests?

To support ib_drain_sq(), this patch increases SQ capacity by exactly 1, assuming standard data transmissions consume up to 3 WRs each. However, SMC also posts FastReg WRs (smc_wr_reg_send) and SMC-Rv2 LLC control messages (smc_wr_tx_v2_send) to the same SQ.

If these independent WRs are posted while data TX slots are saturated, the total in-flight WRs will consume the remaining +1 slot reserved for the drain WR.

Quoted 8 lines

When teardown occurs, ib_drain_sq() will attempt to post its dummy WR, but ib_post_send() will fail with -ENOMEM because the SQ is full. ib_drain_sq() responds to this failure by returning immediately without waiting for completions, unblocking teardown and freeing link structures while the hardware is still asynchronously flushing the SQ. When the softirq polls the flushed CQEs, it will access the freed ib_cqe pointers, leading to a use-after-free.

Good catch. The +1 is indeed not enough.

The SQ has three independent producers but only the data path (3*N) was counted. FastReg (link->wr_reg) and the SMC-Rv2 v2 SEND (link->wr_tx_v2_ib) are both link-unique, so up to 3N + 2 WRs can be in flight when __ib_drain_sq() posts its drain WR.

With only +1 the __ib_drain_sq() may return without draining, and teardown frees the link while WRs are still in flight.

The undersizing itself is pre-existing — 3*N never reserved room for FastReg/v2, but it was harmless before. This series only turns it fatal by moving to ib_drain_qp(), which actually posts a WR. Since it was benign on its own, I'll just fix it here in the next revision rather than as a separate patch, bumping the reservation to +3 (drain + FastReg + v2).

Quoted 53 lines
Quoted 4 lines
net/smc/smc_ib.c +4 -0
+	max_recv_wr = lnk->lgr->max_recv_wr + 1;	/* +1 for ib_drain_rq() */
+
+	cq = ib_cq_pool_get(lnk->smcibdev->ibdev, max_send_wr + max_recv_wr, -1,
+			    IB_POLL_SOFTIRQ);

[ ... ]

Quoted 4 lines
net/smc/smc_wr.c +0 -0
index 59c92b46945c5..130bc6c26fb31 100644

[ ... ]

Quoted 1 line
net/smc/smc_wr.c +0 -0
@@ -63,109 +60,67 @@ void smc_wr_tx_wait_no_pending_sends(struct smc_link *link)

[ ... ]

net/smc/smc_wr.c void smc_wr_tx_wait_no_pending_sends(struct smc_link *link) · line 106

Quoted 11 lines
net/smc/smc_wr.c +0 -8
 	if (pnd_snd_idx == link->wr_tx_cnt) {
-		if (link->lgr->smc_version != SMC_V2 ||
-		    link->wr_tx_v2_pend->wr_id != wc->wr_id)
-			return;
-		link->wr_tx_v2_pend->wc_status = wc->status;
-		memcpy(&pnd_snd, link->wr_tx_v2_pend, sizeof(pnd_snd));
-		/* clear the full struct smc_wr_tx_pend including .priv */
-		memset(link->wr_tx_v2_pend, 0,
-		       sizeof(*link->wr_tx_v2_pend));
 		memset(link->lgr->wr_tx_buf_v2, 0,
 		       sizeof(*link->lgr->wr_tx_buf_v2));

[Severity: High]
This is a pre-existing issue, but while reviewing this code I noticed a potential memory leak and NULL pointer dereference in smc_wr_tx_put_slot().

In the error path of smc_wr_tx_put_slot(), the code attempts to clear the V2 pending slots:

net/smc/smc_wr.c:smc_wr_tx_put_slot() {
    ...
	} else if (link->lgr->smc_version == SMC_V2 &&
		   pend->idx == link->wr_tx_cnt) {
		/* Large v2 buffer */
		memset(&link->wr_tx_v2_pend, 0,
		       sizeof(link->wr_tx_v2_pend));
		memset(&link->lgr->wr_tx_buf_v2, 0,
		       sizeof(link->lgr->wr_tx_buf_v2));
    ...
}

Because these target variables are pointers, sizeof evaluates to 8 bytes. The memset() call zeroes the pointer variable itself rather than clearing the underlying structure, which leaks the allocated memory.

Quoted 9 lines

If smc_wr_tx_get_v2_slot() is called subsequently, it will attempt to dereference the now-NULL link->wr_tx_v2_pend, leading to a direct NULL pointer dereference.

Could this be updated to use sizeof(*link->wr_tx_v2_pend) similar to the code removed above?

[ ... ]

Makes sense. Since this is pre-existing and unrelated to this series, I'll send it as a separate patch.

Quoted 52 lines
Quoted 2 lines
net/smc/smc_wr.c +0 -0
@@ -431,94 +367,74 @@ static inline void smc_wr_rx_demultiplex(struct ib_wc *wc)
 {

[ ... ]

Quoted 24 lines

}

net/smc/smc_wr.c +2 -18
-static inline void smc_wr_rx_process_cqes(struct ib_wc wc[], int num)
+static void smc_wr_rx_process_cqe(struct ib_cq *cq, struct ib_wc *wc)
 {
-	struct smc_link *link;
-	int i;
+	struct smc_link *link = wc->qp->qp_context;
 
-	for (i = 0; i < num; i++) {
-		link = wc[i].qp->qp_context;
-		link->wr_rx_id_compl = wc[i].wr_id;
-		if (wc[i].status == IB_WC_SUCCESS) {
-			link->wr_rx_tstamp = jiffies;
-			smc_wr_rx_demultiplex(&wc[i]);
-			smc_wr_rx_post(link); /* refill WR RX */
-		} else {
-			/* handle status errors */
-			switch (wc[i].status) {
-			case IB_WC_RETRY_EXC_ERR:
-			case IB_WC_RNR_RETRY_EXC_ERR:
-			case IB_WC_WR_FLUSH_ERR:
-				smcr_link_down_cond_sched(link);
-				if (link->wr_rx_id_compl == link->wr_rx_id)

net/smc/smc_wr.c static inline void smc_wr_rx_demultiplex(struct ib_wc *wc) · line 392

Quoted 10 lines
net/smc/smc_wr.c +4 -6
-					wake_up(&link->wr_rx_empty_wait);
-				break;
-			default:
-				smc_wr_rx_post(link); /* refill WR RX */
-				break;
-			}
+	if (wc->status == IB_WC_SUCCESS) {
+		link->wr_rx_tstamp = jiffies;
+		smc_wr_rx_demultiplex(wc);
+		smc_wr_rx_post(link, wc->wr_cqe); /* refill WR RX */

[Severity: Critical]
Can this cause a use-after-free when racing with queue teardown?

The patch replaces smc_wr_drain_cq() with ib_drain_qp(). ib_drain_rq() transitions the QP to IB_QPS_ERR and posts a dummy drain WR to wait for completions.

However, smc_wr_rx_process_cqe() unconditionally reposts a new WR when processing a successful packet completion. If a valid RX completion is polled in the softirq concurrently with ib_drain_rq(), the softirq will post a new WR after the drain WR has been posted.

Quoted 9 lines

Since the QP is in the ERR state, the new WR will be immediately completed with a flush error, but its CQE will be generated after the drain WR's CQE. This allows ib_drain_rq() to return and unblock link teardown, freeing link->wr_rx_ibs.

When the softirq subsequently polls the flush CQE for the newly posted WR, it will dereference wc->wr_cqe->done. Since wc->wr_cqe points to the freed smc_ib_recv_wr, this results in a use-after-free.

The analysis is right, and unlike the other two this one is introduced by this series. The old smc_wr_drain_cq() was a pure counting wait that posted no WR, so the unconditional repost was harmless. By switching to ib_drain_qp(), which relies on the drain WR being the last WR on the queue, the same repost now breaks that invariant and opens the UAF window.

I'll fix it in the next revision by quiescing the repost path before draining, so the drain WR is guaranteed to be the last one.

D. Wythe

Quoted 16 lines
Quoted 14 lines
net/smc/smc_wr.c +11 -0
+	} else {
+		/* handle status errors */
+		switch (wc->status) {
+		case IB_WC_RETRY_EXC_ERR:
+		case IB_WC_RNR_RETRY_EXC_ERR:
+		case IB_WC_WR_FLUSH_ERR:
+			smcr_link_down_cond_sched(link);
+			break;
+		default:
+			smc_wr_rx_post(link, wc->wr_cqe); /* refill WR RX */
+			break;
 		}
 	}
 }
Signature · 1 line
This is an AI-generated review.
author maintainer
#4 lore raw

PATCH v2 2/2 net/smc: reduce TX slot contention with exclusive wait

In reply to D. Wythe

smc_wr_tx_get_free_slot() waits for a free TX slot with wait_event_interruptible_timeout(). Since the wait_event family enqueues waiters as non-exclusive, wake_up() may wake multiple waiters even though only one can use the slot, causing thundering-herd contention when slots are scarce.

Use an exclusive wait loop with prepare_to_wait_exclusive() so wake_up() wakes only one waiter per freed slot. smc_wr_wakeup_tx_wait() still uses wake_up_all() during link teardown, so teardown behavior is unchanged.

Performance measured with netperf TCP_RR (63 flows, 200B write / 1000B read, 60s duration):

+-------------------------------+---------------+---------------+
| smcr_max_conns_per_lgr        | 32            | 255           |
|-------------------------------+---------------+---------------|
| before                        | 4.85 Gb/s     | 657.95 Mb/s   |
|-------------------------------+---------------+---------------|
| after                         | 5.01 Gb/s     | 2.2 Gb/s      |
+-------------------------------+---------------+---------------+
---
 net/smc/smc_wr.c | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)
net/smc/smc_wr.c +26 -10
index 130bc6c26fb3..3cb47f77130e 100644
@@ -153,9 +153,11 @@ int smc_wr_tx_get_free_slot(struct smc_link *link,
 			    struct smc_rdma_wr **wr_rdma_buf,
 			    struct smc_wr_tx_pend_priv **wr_pend_priv)
 {
+	unsigned long timeout = SMC_WR_TX_WAIT_FREE_SLOT_TIME;
 	struct smc_link_group *lgr = smc_get_lgr(link);
 	struct smc_wr_tx_pend *wr_pend;
 	u32 idx = link->wr_tx_cnt;
+	DEFINE_WAIT(wait);
 	int rc;
 
 	*wr_buf = NULL;
@@ -165,17 +167,31 @@ int smc_wr_tx_get_free_slot(struct smc_link *link,
 		if (rc)
 			return rc;
 	} else {
-		rc = wait_event_interruptible_timeout(
-			link->wr_tx_wait,
-			!smc_link_sendable(link) ||
-			lgr->terminating ||
-			(smc_wr_tx_get_free_slot_index(link, &idx) != -EBUSY),
-			SMC_WR_TX_WAIT_FREE_SLOT_TIME);
-		if (!rc) {
-			/* timeout - terminate link */
-			smcr_link_down_cond_sched(link);
-			return -EPIPE;
+		rc = 0;
+		for (;;) {
+			prepare_to_wait_exclusive(&link->wr_tx_wait, &wait,
+						  TASK_INTERRUPTIBLE);
+			if (!smc_link_sendable(link) || lgr->terminating ||
+			    smc_wr_tx_get_free_slot_index(link, &idx) != -EBUSY)
+				break;
+			timeout = schedule_timeout(timeout);
+			/* re-check */
+			if (!smc_link_sendable(link) || lgr->terminating ||
+			    smc_wr_tx_get_free_slot_index(link, &idx) != -EBUSY)
+				break;
+			if (!timeout) {
+				/* timeout - terminate link */
+				smcr_link_down_cond_sched(link);
+				break;
+			}
+			if (signal_pending(current)) {
+				rc = -ERESTARTSYS;
+				break;
+			}
 		}
+		finish_wait(&link->wr_tx_wait, &wait);
+		if (rc)
+			return rc;
 		if (idx == link->wr_tx_cnt)
 			return -EPIPE;
 	}
Signature · 1 line
2.45.0
maintainer
#5 lore raw

Re: [PATCH net-next v2 2/2] net/smc: reduce TX slot contention with exclusive wait

Quoted 64 lines · D. Wythe

smc_wr_tx_get_free_slot() waits for a free TX slot with wait_event_interruptible_timeout(). Since the wait_event family enqueues waiters as non-exclusive, wake_up() may wake multiple waiters even though only one can use the slot, causing thundering-herd contention when slots are scarce.

Use an exclusive wait loop with prepare_to_wait_exclusive() so wake_up() wakes only one waiter per freed slot. smc_wr_wakeup_tx_wait() still uses wake_up_all() during link teardown, so teardown behavior is unchanged.

Performance measured with netperf TCP_RR (63 flows, 200B write / 1000B read, 60s duration):

+-------------------------------+---------------+---------------+
| smcr_max_conns_per_lgr        | 32            | 255           |
|-------------------------------+---------------+---------------|
| before                        | 4.85 Gb/s     | 657.95 Mb/s   |
|-------------------------------+---------------+---------------|
| after                         | 5.01 Gb/s     | 2.2 Gb/s      |
+-------------------------------+---------------+---------------+

---
net/smc/smc_wr.c | 36 ++++++++++++++++++++++++++----------

1 file changed, 26 insertions(+), 10 deletions(-)
net/smc/smc_wr.c +9 -10
index 130bc6c26fb3..3cb47f77130e 100644
@@ -153,9 +153,11 @@ int smc_wr_tx_get_free_slot(struct smc_link *link,
 			    struct smc_rdma_wr **wr_rdma_buf,
 			    struct smc_wr_tx_pend_priv **wr_pend_priv)
 {
+	unsigned long timeout = SMC_WR_TX_WAIT_FREE_SLOT_TIME;
 	struct smc_link_group *lgr = smc_get_lgr(link);
 	struct smc_wr_tx_pend *wr_pend;
 	u32 idx = link->wr_tx_cnt;
+	DEFINE_WAIT(wait);
 	int rc;
 
 	*wr_buf = NULL;
@@ -165,17 +167,31 @@ int smc_wr_tx_get_free_slot(struct smc_link *link,
 		if (rc)
 			return rc;
 	} else {
-		rc = wait_event_interruptible_timeout(
-			link->wr_tx_wait,
-			!smc_link_sendable(link) ||
-			lgr->terminating ||
-			(smc_wr_tx_get_free_slot_index(link, &idx) != -EBUSY),
-			SMC_WR_TX_WAIT_FREE_SLOT_TIME);
-		if (!rc) {
-			/* timeout - terminate link */
-			smcr_link_down_cond_sched(link);
-			return -EPIPE;
+		rc = 0;
+		for (;;) {
+			prepare_to_wait_exclusive(&link->wr_tx_wait, &wait,
+						  TASK_INTERRUPTIBLE);
+			if (!smc_link_sendable(link) || lgr->terminating ||
+			    smc_wr_tx_get_free_slot_index(link, &idx) != -EBUSY)
+				break;

net/smc/smc_wr.c int smc_wr_tx_get_free_slot(struct smc_link *link, · line 186

Quoted 10 lines
net/smc/smc_wr.c +10 -0
+			timeout = schedule_timeout(timeout);
+			/* re-check */
+			if (!smc_link_sendable(link) || lgr->terminating ||
+			    smc_wr_tx_get_free_slot_index(link, &idx) != -EBUSY)
+				break;
+			if (!timeout) {
+				/* timeout - terminate link */
+				smcr_link_down_cond_sched(link);
+				break;
+			}

The change itself looks correct to me. But I think we should probably define a wait_event_interruptible_exclusive_timeout() helper in include/linux/wait.h rather than open-coding it in smc ?

net/smc/smc_wr.c int smc_wr_tx_get_free_slot(struct smc_link *link, · line 191

Quoted 5 lines
net/smc/smc_wr.c +4 -0
+			if (signal_pending(current)) {
+				rc = -ERESTARTSYS;
+				break;
+			}
 		}

One more thing, seems we changed the signal here, it's better to add a comment or note it in the commit message.

Best regards,
Dust

maintainer
#6 lore raw
In reply to D. Wythe
Quoted 10 lines · On Thu, 28 May 2026 16:48:17 +0800 D. Wythe wrote:

This series transitions SMC-R completion handling to RDMA core CQ pooling via the ib_cqe API. The new completion model improves scalability by allowing per-link completion processing across multiple cores and enables DIM-based interrupt moderation.

As a side effect, the increased concurrency can amplify contention for TX slots on the shared wait queue. Patch 2 addresses this by switching TX slot allocation from non-exclusive wait_event() to prepare_to_wait_exclusive(), which avoids thundering-herd wakeups under contention.

Quoted 4 lines

Patch 1 replaces the global per-device CQ and manual tasklet polling model with RDMA core CQ pooling.
Patch 2 reduces TX slot contention by using exclusive wait queue entries during allocation.

Sashiko reports a couple of issues on patch 1:
https://sashiko.dev/#/patchset/[email protected]
Are these legit?

Either way - would be good to get some reviews here from (ohter) SMC maintainers.

author maintainer
#7 lore raw
Quoted 16 lines · Jakub Kicinski
Quoted 10 lines · On Thu, 28 May 2026 16:48:17 +0800 D. Wythe wrote:

This series transitions SMC-R completion handling to RDMA core CQ pooling via the ib_cqe API. The new completion model improves scalability by allowing per-link completion processing across multiple cores and enables DIM-based interrupt moderation.

As a side effect, the increased concurrency can amplify contention for TX slots on the shared wait queue. Patch 2 addresses this by switching TX slot allocation from non-exclusive wait_event() to prepare_to_wait_exclusive(), which avoids thundering-herd wakeups under contention.

Quoted 4 lines

Patch 1 replaces the global per-device CQ and manual tasklet polling model with RDMA core CQ pooling.
Patch 2 reduces TX slot contention by using exclusive wait queue entries during allocation.

Quoted 6 lines

Sashiko reports a couple of issues on patch 1:
https://sashiko.dev/#/patchset/[email protected]
Are these legit?

Either way - would be good to get some reviews here from (ohter) SMC maintainers.

Thanks for the heads up.

We’ve seen the Sashiko reports and are currently evaluating whether they’re legit. So far it looks valid, though some of the issues may be uncovering pre-existing problems. If needed, we may send extra patches to fix them before applying this patch. The SMC maintainers are already aware, and I expect more maintainers to join the review soon.

D. Wythe

maintainer
#8 lore raw
In reply to Jakub Kicinski
Quoted 16 lines · Jakub Kicinski
Quoted 10 lines · On Thu, 28 May 2026 16:48:17 +0800 D. Wythe wrote:

This series transitions SMC-R completion handling to RDMA core CQ pooling via the ib_cqe API. The new completion model improves scalability by allowing per-link completion processing across multiple cores and enables DIM-based interrupt moderation.

As a side effect, the increased concurrency can amplify contention for TX slots on the shared wait queue. Patch 2 addresses this by switching TX slot allocation from non-exclusive wait_event() to prepare_to_wait_exclusive(), which avoids thundering-herd wakeups under contention.

Quoted 4 lines

Patch 1 replaces the global per-device CQ and manual tasklet polling model with RDMA core CQ pooling.
Patch 2 reduces TX slot contention by using exclusive wait queue entries during allocation.

Quoted 6 lines

Sashiko reports a couple of issues on patch 1:
https://sashiko.dev/#/patchset/[email protected]
Are these legit?

Either way - would be good to get some reviews here from (ohter) SMC maintainers.

D Wythe - Thank you for addressing Sashiko's comments.

I've reviewed the three issues raised by Sashiko for Patch 1 and your responses:

Issue #1: Acknowledged, will be fixed in v3. I agree with your proposal of using "max_send_wr = 3 * lnk->lgr->max_send_wr + *3*;" Issue #2: I agree this is a pre-existing issue unrelated to this patch series. The potential memory leak and NULL pointer de-reference in smc_wr_tx_put_slot() should be addressed in a separate patch to keep the scope focused.
Issue #3: Acknowledged, will be fixed in v3. I agree that ib_drain_qp() introduced in this patch does open a UAF window.

I am looking forward for v3.