Skip to contentSkip to conversation

net/smc: prevent device removal during connection setup

PATCH In review Qi Zhang 5 participants 5 messages Aug 23 – Aug 27

Subsystems (6)
lore
Share & tools
lore

Qi Zhang

author
#0 lore raw

PATCH v2 1/1 net/smc: prevent device removal during connection setup

From: Chengfeng Ye <[email protected]>

smc_pnet_determine_gid() stores an smc_ib_device pointer in connection initialization data while holding smc_ib_devices.mutex. Connection setup keeps using the pointer after releasing the mutex, but device removal only waits for links which have already incremented lnk_cnt.

This permits the following interleaving:

connection setup device removal
---------------- --------------
select smcibdev
store ini->smcrv2.ib_dev_v2
mutex_unlock()
remove smcibdev from the device list observe lnk_cnt == 0 return from the remove callback kfree(smcibdev)
copy smcibdev->pnetid

KASAN reported:

  BUG: KASAN: slab-use-after-free in smc_conn_create+0x1330/0x2680
  Read of size 16 at addr ffff888112dbd934 by task smc_bug86/97
  Call Trace:
   __asan_memcpy
   smc_conn_create
   __smc_connect
   smc_connect
   __sys_connect
  Allocated by task 93:
   smc_ib_add_dev
   add_client_context
   ib_register_device
   nldev_newlink
  Freed by task 98:
   kfree
   remove_client_context
   __ib_unregister_device
   nldev_dellink

Hold a lifetime reference on every SMCR device selected for connection setup. The reference also pins the underlying ib_device, but does not make device removal wait for the peer-controlled CLC handshake.

Before turning the selection into a link, check under smc_ib_devices.mutex that the device is still registered. Track only the short local interval between that check and publishing the new link group. Device removal waits for this interval before terminating links, ensuring that it cannot miss a link being created concurrently. Keep the lifetime reference until after the final counter decrement and wakeup, and serialize begin/end with device delisting so no thread touches a freed smcibdev.

This change is limited to the reported SMCR lifetime race. SMC-D device selection has a similar pre-existing borrowed-pointer lifetime issue and requires a separate fix.

Fixes a046d57da19f ("smc: CLC handshake (incl. preparation steps)")

---
Changes in v2:
- Replace the handshake-long removal wait with a lifetime reference that defers object release without blocking device removal.
- Revalidate the selected device and wait only while a new link is being published, so the removal-side termination pass cannot miss it.
- Keep a lifetime reference across the final publication-counter decrement and wakeup, and serialize begin/end with device delisting to close the reviewed wake-after-free window.
- Explicitly scope this patch to SMC-R; the analogous pre-existing SMC-D borrowed-pointer lifetime issue requires a separate fix.
- Rebase onto net/main at 7cbfb180945c and preserve the intervening LLC qentry lifetime fixes.

v1: https://lore.kernel.org/r/[email protected]
Review: https://lore.kernel.org/r/[email protected]

 net/smc/af_smc.c   | 11 +++++++----
 net/smc/smc_core.c | 29 ++++++++++++++++++++++++----
 net/smc/smc_core.h |  3 +++
 net/smc/smc_ib.c   | 47 +++++++++++++++++++++++++++++++++++++++++++++-
 net/smc/smc_ib.h   |  9 +++++++++
 net/smc/smc_llc.c  |  8 +++++---
 net/smc/smc_pnet.c |  8 ++++++++
 7 files changed, 103 insertions(+), 12 deletions(-)
net/smc/af_smc.c +7 -4
index e9f93b3ab435..d781dee1b329 100644
@@ -1225,7 +1225,7 @@ void smc_fill_gid_list(struct smc_link_group *lgr,
 	       SMC_GID_SIZE);
 
 out:
-	kfree(alt_ini);
+	smc_init_info_free(alt_ini);
 }
 
 static int smc_connect_rdma_v2_prepare(struct smc_sock *smc,
@@ -1596,14 +1596,14 @@ static int __smc_connect(struct smc_sock *smc)
 	SMC_STAT_CLNT_SUCC_INC(sock_net(smc->clcsock->sk), aclc);
 	smc_connect_ism_vlan_cleanup(ini);
 	kfree(buf);
-	kfree(ini);
+	smc_init_info_free(ini);
 	return 0;
 
 vlan_cleanup:
 	smc_connect_ism_vlan_cleanup(ini);
 	kfree(buf);
 fallback:
-	kfree(ini);
+	smc_init_info_free(ini);
 	return smc_connect_decline_fallback(smc, rc, version);
 }
 
@@ -2344,7 +2344,10 @@ static void smc_find_rdma_v2_device_serv(struct smc_sock *new_smc,
 
 not_found:
 	ini->smcr_version &= ~SMC_V2;
+	if (ini->smcrv2.ib_dev_v2_ref)
+		smc_ibdev_put(ini->smcrv2.ib_dev_v2);
 	ini->smcrv2.ib_dev_v2 = NULL;
+	ini->smcrv2.ib_dev_v2_ref = false;
 	ini->check_smcrv2 = false;
 }
 
@@ -2588,7 +2591,7 @@ static void smc_listen_work(struct work_struct *work)
 	smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
 			   proposal_version);
 out_free:
-	kfree(ini);
+	smc_init_info_free(ini);
 	kfree(buf);
 }
 
net/smc/smc_core.c +25 -4
index 181647982490..d97bc7323106 100644
@@ -55,6 +55,17 @@ static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft);
 
 static void smc_link_down_work(struct work_struct *work);
 
+void smc_init_info_free(struct smc_init_info *ini)
+{
+	if (!ini)
+		return;
+	if (ini->ib_dev_ref)
+		smc_ibdev_put(ini->ib_dev);
+	if (ini->smcrv2.ib_dev_v2_ref)
+		smc_ibdev_put(ini->smcrv2.ib_dev_v2);
+	kfree(ini);
+}
+
 /* return head of link group list and its lock for a given link group */
 static inline struct list_head *smc_lgr_list_head(struct smc_link_group *lgr,
 						  spinlock_t **lgr_lock)
@@ -793,14 +804,21 @@ int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
 	int rc;
 
 	if (lgr->smc_version == SMC_V2) {
-		lnk->smcibdev = ini->smcrv2.ib_dev_v2;
+		smcibdev = ini->smcrv2.ib_dev_v2;
 		lnk->ibport = ini->smcrv2.ib_port_v2;
-		lnk->wr_rx_sge_cnt = lnk->smcibdev->ibdev->attrs.max_recv_sge < 2 ? 1 : 2;
+	} else {
+		smcibdev = ini->ib_dev;
+		lnk->ibport = ini->ib_port;
+	}
+	rc = smc_ibdev_init_begin(smcibdev);
+	if (rc)
+		return rc;
+	lnk->smcibdev = smcibdev;
+	if (lgr->smc_version == SMC_V2) {
+		lnk->wr_rx_sge_cnt = smcibdev->ibdev->attrs.max_recv_sge < 2 ? 1 : 2;
 		lnk->wr_rx_buflen = smc_link_shared_v2_rxbuf(lnk) ?
 			SMC_WR_BUF_SIZE : SMC_WR_BUF_V2_SIZE;
 	} else {
-		lnk->smcibdev = ini->ib_dev;
-		lnk->ibport = ini->ib_port;
 		lnk->wr_rx_sge_cnt = 1;
 		lnk->wr_rx_buflen = SMC_WR_BUF_SIZE;
 	}
@@ -882,6 +900,7 @@ int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
 	if (!atomic_dec_return(&smcibdev->lnk_cnt))
 		wake_up(&smcibdev->lnks_deleted);
 	smc_lgr_put(lgr); /* lgr_hold above */
+	smc_ibdev_init_end(smcibdev);
 	return rc;
 }
 
@@ -997,6 +1016,8 @@ static int smc_lgr_create(struct smc_sock *smc, struct smc_init_info *ini)
 	spin_lock_bh(lgr_lock);
 	list_add_tail(&lgr->list, lgr_list);
 	spin_unlock_bh(lgr_lock);
+	if (!ini->is_smcd)
+		smc_ibdev_init_end(lnk->smcibdev);
 	return 0;
 
 free_wq:
net/smc/smc_core.h +3 -0
index 5c18f08a4c8a..9f5a69a30cb3 100644
@@ -403,6 +403,7 @@ struct smc_init_info_smcrv2 {
 
 	/* Output fields when saddr is set */
 	struct smc_ib_device	*ib_dev_v2;
+	bool			ib_dev_v2_ref; /* owns an smc_ibdev reference */
 	u8			ib_port_v2;
 	u8			ib_gid_v2[SMC_GID_SIZE];
 
@@ -438,6 +439,7 @@ struct smc_init_info {
 	u8			peer_mac[ETH_ALEN];
 	u8			peer_systemid[SMC_SYSTEMID_LEN];
 	struct smc_ib_device	*ib_dev;
+	bool			ib_dev_ref; /* owns an smc_ibdev reference */
 	u8			ib_gid[SMC_GID_SIZE];
 	u8			ib_port;
 	u32			ib_clcqpn;
@@ -594,6 +596,7 @@ void smc_rtoken_set2(struct smc_link_group *lgr, int rtok_idx, int link_id,
 void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn);
 void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn);
 int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini);
+void smc_init_info_free(struct smc_init_info *ini);
 
 void smc_conn_free(struct smc_connection *conn);
 int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini);
net/smc/smc_ib.c +46 -1

70 lines not on this page · raw message

net/smc/smc_ib.h +9 -0
index ef8ac2b7546d..d32547350e8b 100644
@@ -15,6 +15,7 @@
 #include <linux/interrupt.h>
 #include <linux/if_ether.h>
 #include <linux/mutex.h>
+#include <linux/refcount.h>
 #include <linux/wait.h>
 #include <rdma/ib_verbs.h>
 #include <net/smc.h>
@@ -51,6 +52,9 @@ struct smc_ib_device {				/* ib-device infos for smc */
 	struct work_struct	port_event_work;
 	unsigned long		port_event_mask;
 	DECLARE_BITMAP(ports_going_away, SMC_MAX_PORTS);
+	refcount_t		refcnt;		/* object lifetime */
+	atomic_t		init_cnt;	/* unpublished link initializations */
+	wait_queue_head_t	init_wait;	/* wait for links to be published */
 	atomic_t		lnk_cnt;	/* number of links on ibdev */
 	wait_queue_head_t	lnks_deleted;	/* wait 4 removal of all links*/
 	struct mutex		mutex;		/* protect dev setup+cleanup */
@@ -59,6 +63,11 @@ struct smc_ib_device {				/* ib-device infos for smc */
 	int			ndev_ifidx[SMC_MAX_PORTS]; /* ndev if indexes */
 };
 
+void smc_ibdev_get(struct smc_ib_device *smcibdev);
+void smc_ibdev_put(struct smc_ib_device *smcibdev);
+int smc_ibdev_init_begin(struct smc_ib_device *smcibdev);
+void smc_ibdev_init_end(struct smc_ib_device *smcibdev);
+
 static inline __be32 smc_ib_gid_to_ipv4(u8 gid[SMC_GID_SIZE])
 {
 	struct in6_addr *addr6 = (struct in6_addr *)gid;
net/smc/smc_llc.c +5 -3
index c9ccd8480369..e16aaa514368 100644
@@ -1108,6 +1108,7 @@ int smc_llc_cli_add_link(struct smc_link *link, struct smc_llc_qentry *qentry)
 	rc = smcr_link_init(lgr, lnk_new, lnk_idx, ini);
 	if (rc)
 		goto out_reject;
+	smc_ibdev_init_end(lnk_new->smcibdev);
 	smc_llc_save_add_link_info(lnk_new, llc);
 	lnk_new->link_id = llc->link_num;	/* SMC server assigns link id */
 	smc_llc_link_set_uid(lnk_new);
@@ -1143,7 +1144,7 @@ int smc_llc_cli_add_link(struct smc_link *link, struct smc_llc_qentry *qentry)
 out_reject:
 	smc_llc_cli_add_link_reject(qentry);
 out:
-	kfree(ini);
+	smc_init_info_free(ini);
 	kfree(qentry);
 	return rc;
 }
@@ -1217,7 +1218,7 @@ static void smc_llc_cli_add_link_invite(struct smc_link *link,
 	smc_llc_send_add_link(link, ini->ib_dev->mac[ini->ib_port - 1],
 			      ini->ib_gid, NULL, SMC_LLC_REQ);
 out:
-	kfree(ini);
+	smc_init_info_free(ini);
 	kfree(qentry);
 }
 
@@ -1487,6 +1488,7 @@ int smc_llc_srv_add_link(struct smc_link *link,
 	if (rc)
 		goto out;
 	link_new = &lgr->lnk[lnk_idx];
+	smc_ibdev_init_end(link_new->smcibdev);
 
 	rc = smcr_buf_map_lgr(link_new);
 	if (rc)
@@ -1544,7 +1546,7 @@ int smc_llc_srv_add_link(struct smc_link *link,
 	}
 out:
 	kfree(qentry);
-	kfree(ini);
+	smc_init_info_free(ini);
 	if (send_req_add_link_resp)
 		smc_llc_send_req_add_link_response(req_qentry);
 	return rc;
net/smc/smc_pnet.c +8 -0
index 63e286e2dfaa..be1f5137c8ee 100644
@@ -980,14 +980,22 @@ static int smc_pnet_determine_gid(struct smc_ib_device *ibdev, int i,
 	if (!ini->check_smcrv2 &&
 	    !smc_ib_determine_gid(ibdev, i, ini->vlan_id, ini->ib_gid, NULL,
 				  NULL)) {
+		if (ini->ib_dev_ref)
+			smc_ibdev_put(ini->ib_dev);
+		smc_ibdev_get(ibdev);
 		ini->ib_dev = ibdev;
+		ini->ib_dev_ref = true;
 		ini->ib_port = i;
 		return 0;
 	}
 	if (ini->check_smcrv2 &&
 	    !smc_ib_determine_gid(ibdev, i, ini->vlan_id, ini->smcrv2.ib_gid_v2,
 				  NULL, &ini->smcrv2)) {
+		if (ini->smcrv2.ib_dev_v2_ref)
+			smc_ibdev_put(ini->smcrv2.ib_dev_v2);
+		smc_ibdev_get(ibdev);
 		ini->smcrv2.ib_dev_v2 = ibdev;
+		ini->smcrv2.ib_dev_v2_ref = true;
 		ini->smcrv2.ib_port_v2 = i;
 		return 0;
 	}
Signature · 1 line
2.25.1
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines

From: Chengfeng Ye <[email protected]>

smc_pnet_determine_gid() stores an smc_ib_device pointer in connection initialization data while holding smc_ib_devices.mutex. Connection setup keeps using the pointer after releasing the mutex, but device removal only waits for links which have already incremented lnk_cnt.

This permits the following interleaving:

connection setup device removal
---------------- --------------
select smcibdev
store ini->smcrv2.ib_dev_v2
mutex_unlock()
remove smcibdev from the device list
[ ... ]

Quoted 4 lines
Fixes a046d57da19f ("smc: CLC handshake (incl. preparation steps)")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer Reviewed-by
#3 lore raw
Quoted 7 lines · Markus Elfring

…

Quoted 3 lines

This change is limited to the reported SMCR lifetime race. SMC-D device selection has a similar pre-existing borrowed-pointer lifetime issue and requires a separate fix.

May lock guards be applied?
https://elixir.bootlin.com/linux/v7.2/source/include/linux/mutex.h#L253

This approach seems fine to me.

Quoting the documentation:

Use of guard() is discouraged within any function longer than 20 lines, scoped_guard() is considered more readable. Using normal lock/unlock is still (weakly) preferred.

maintainer
#4 lore raw
In reply to Qi Zhang

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net/smc: prevent device removal during connection setup

This patch adds a refcount to struct smc_ib_device so that an smcibdev selected during SMC-R connection setup cannot be freed while the initialization data still points at it. It also adds an init_cnt/init_wait pair so that ib device removal waits for the interval between revalidating the selected device and publishing the resulting link group.

Quoted 9 lines
net/smc/smc_core.c +1 -0
index 181647982490..d97bc7323106 100644
@@ -55,6 +55,17 @@ static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft);
 
 static void smc_link_down_work(struct work_struct *work);
 
+void smc_init_info_free(struct smc_init_info *ini)

net/smc/smc_core.c static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft); · line 68

Quoted 10 lines
net/smc/smc_core.c +10 -0
+{
+	if (!ini)
+		return;
+	if (ini->ib_dev_ref)
+		smc_ibdev_put(ini->ib_dev);
+	if (ini->smcrv2.ib_dev_v2_ref)
+		smc_ibdev_put(ini->smcrv2.ib_dev_v2);
+	kfree(ini);
+}
+

[Severity: High]

This isn't a bug introduced by this patch, but since the commit message mentions the SMC-D case only in passing, can the smcd_dev pointer in ini->ism_dev[] be used after free in the same way?

smc_pnet_find_ism_by_pnetid() publishes the raw pointer and then drops the selection lock:

net/smc/smc_pnet.c:smc_pnet_find_ism_by_pnetid() {
	...
			ini->ism_dev[0] = ismdev;
			break;
		}
	}
	mutex_unlock(&smcd_dev_list.mutex);
}

The removal side only waits for already published link groups via lgr_cnt and then frees the object:

net/smc/smc_ism.c:smcd_unregister_dev() {
	...
	smc_smcd_terminate_all(smcd);
	destroy_workqueue(smcd->event_wq);
	kfree(smcd);
}

A setup that has selected the device but has not created a link group yet is not counted anywhere, so the borrowed pointer is still dereferenced afterwards by smc_connect_ism_vlan_setup() and smc_connect_ism_vlan_cleanup():

	if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))

and by smc_lgr_create():

		smcd = ini->ism_dev[ini->ism_selected];
		get_device(&smcd->dibs->dev);

smc_ism_get_vlan() takes spin_lock_irqsave(&smcd->lock), so this is a write into the freed object as well. Since smc_init_info_free() only drops the two SMC-R references, would it be worth stating in the commit message that ini->ism_dev[] stays an unowned pointer on purpose?

net/smc/smc_core.c int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk, · line 905

Quoted 7 lines
net/smc/smc_core.c +1 -0
@@ -882,6 +900,7 @@ int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
 	if (!atomic_dec_return(&smcibdev->lnk_cnt))
 		wake_up(&smcibdev->lnks_deleted);
 	smc_lgr_put(lgr); /* lgr_hold above */
+	smc_ibdev_init_end(smcibdev);
 	return rc;
 }

[Severity: High]

This is a pre-existing issue, but now that a refcount exists, should it also cover the lnk_cnt decrement and wakeup above and the identical sequence in __smcr_link_clear()?

net/smc/smc_core.c:__smcr_link_clear() {
	...
	smcibdev = lnk->smcibdev;
	memset(lnk, 0, sizeof(struct smc_link));
	lnk->state = SMC_LNK_UNUSED;
	if (!atomic_dec_return(&smcibdev->lnk_cnt))
		wake_up(&smcibdev->lnks_deleted);
	...
}

Links take get_device(&lnk->smcibdev->ibdev->dev) but never smc_ibdev_get(), so nothing pins the smc_ib_device allocation across the decrement and the wake_up().

The waiter can leave the wait purely by re-evaluating the counter:

net/smc/smc_core.c:smc_smcr_terminate_all() {
	...
		if (atomic_read(&smcibdev->lnk_cnt))
			wait_event(smcibdev->lnks_deleted,
				   !atomic_read(&smcibdev->lnk_cnt));
	...
}

If the clearing task is preempted between atomic_dec_return() returning 0 and wake_up(), smc_ib_remove_dev() can proceed to smc_ibdev_put() -> smc_ibdev_release() -> kfree(smcibdev), and the clearing task then takes the waitqueue spinlock inside freed memory. Would an smc_ibdev_get() in smcr_link_init() paired with an smc_ibdev_put() after the wakeup close this?

Quoted 8 lines
net/smc/smc_ib.c +0 -0
index 9bb495707445..d858453c64d9 100644
@@ -1000,11 +1042,14 @@ static void smc_ib_remove_dev(struct ib_device *ibdev, void *client_data)
 	mutex_unlock(&smc_ib_devices.mutex);
 	pr_warn_ratelimited("smc: removing ib device %s\n",
 			    smcibdev->ibdev->name);

net/smc/smc_ib.c static void smc_ib_remove_dev(struct ib_device *ibdev, void *client_data) · line 1053

Quoted 10 lines
net/smc/smc_ib.c +4 -1
+	if (atomic_read(&smcibdev->init_cnt))
+		wait_event(smcibdev->init_wait,
+			   !atomic_read(&smcibdev->init_cnt));
 	smc_smcr_terminate_all(smcibdev);
 	smc_ib_cleanup_per_ibdev(smcibdev);
 	ib_unregister_event_handler(&smcibdev->event_handler);
 	cancel_work_sync(&smcibdev->port_event_work);
-	kfree(smcibdev);
+	smc_ibdev_put(smcibdev);
 }

[Severity: High]

This is a pre-existing behaviour of the termination scan rather than something the patch introduces, but does the new init_cnt interval really give the guarantee the commit message claims?

The commit message says:

"Device removal waits for this interval before terminating links, ensuring that it cannot miss a link being created concurrently."

smcr_link_init() returns with the new link in SMC_LNK_ACTIVATING:

	lnk->state = SMC_LNK_ACTIVATING;
	return 0;

and every caller releases init_cnt right away, for example smc_lgr_create():

	if (!ini->is_smcd)
		smc_ibdev_init_end(lnk->smcibdev);

so the interval ends long before smc_llc_link_active() promotes the link to SMC_LNK_ACTIVE. After the init_cnt wait drains, the single scan in smc_smcr_terminate_all() reaches smcr_link_down_cond_sched(), which only acts on an ACTIVE link:

net/smc/smc_core.c:smcr_link_down_cond_sched() {
	if (smc_link_downing(&lnk->state)) {
		...
}

smc_link_downing() is a cmpxchg from SMC_LNK_ACTIVE to SMC_LNK_INACTIVE, so an ACTIVATING link is skipped and the scan is not repeated. The following

		if (atomic_read(&smcibdev->lnk_cnt))
			wait_event(smcibdev->lnks_deleted,
				   !atomic_read(&smcibdev->lnk_cnt));

then has no timeout, so if the connection completes and stays up, smc_ib_remove_dev() and therefore ib_unregister_device() block until the link goes down for an unrelated reason. The ACTIVATING window spans the peer-controlled CONFIRM_LINK exchange, so a remote peer can widen it. Would it be more robust to hold init_cnt until the link reaches SMC_LNK_ACTIVE, or to have the scan also handle ACTIVATING links?

Signature · 1 line
pw-bot: cr