All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mahanta Jambigi <mjambigi@linux.ibm.com>
To: andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com,
	kuba@kernel.org, pabeni@redhat.com, alibuda@linux.alibaba.com,
	dust.li@linux.alibaba.com, sidraya@linux.ibm.com
Cc: hidayath@linux.ibm.com, pasic@linux.ibm.com, horms@kernel.org,
	tonylu@linux.alibaba.com, guwen@linux.alibaba.com,
	netdev@vger.kernel.org, linux-s390@vger.kernel.org,
	linux-rdma@vger.kernel.org,
	Mahanta Jambigi <mjambigi@linux.ibm.com>
Subject: [PATCH net-next 2/3] net/smc: remove the clcsock release lock
Date: Fri, 20 Nov 2026 13:45:33 +0100	[thread overview]
Message-ID: <20261120124534.430200-3-mjambigi@linux.ibm.com> (raw)
In-Reply-To: <20261120124534.430200-1-mjambigi@linux.ibm.com>

The clcsock is now freed only when no socket file can reference it, and
that decision is made under the socket lock: smc_accept_dequeue() holds
the same lock across the accept-queue state check and sock_graft(), so a
child that is being accepted cannot lose its clcsock to an asynchronous
close. A socket operation on the file is serialized with smc_release()
by the file reference.

The listen paths that held the lock are covered without it:
smc_clcsock_release() flushes smc_tcp_listen_work() before releasing a
listener's clcsock and waits for smc_listen_work() before releasing a
child's. Remove the lock and the NULL checks and error paths it needed
in getsockopt, setsockopt, getname, ioctl and accept;
smc_switch_to_fallback() can no longer fail and returns void. Detach the
clcsock with xchg() at both release sites, so a second release attempt
is a no-op.

Signed-off-by: Mahanta Jambigi <mjambigi@linux.ibm.com>
---
 net/smc/af_smc.c    | 106 +++++++++++++-------------------------------
 net/smc/smc.h       |   4 --
 net/smc/smc_close.c |  12 ++---
 net/smc/smc_inet.c  |   9 ++--
 4 files changed, 42 insertions(+), 89 deletions(-)

diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c
index 1d8a602729c1..cc7fb2d90637 100644
--- a/net/smc/af_smc.c
+++ b/net/smc/af_smc.c
@@ -409,7 +409,6 @@ void smc_sk_init(struct net *net, struct sock *sk, int protocol)
 				      "sk_lock-AF_SMC", &smc_key);
 	spin_lock_init(&smc->accept_q_lock);
 	spin_lock_init(&smc->conn.send_lock);
-	mutex_init(&smc->clcsock_release_lock);
 	smc_init_saved_callbacks(smc);
 	smc->limit_smc_hs = net->smc.limit_smc_hs;
 	smc->use_fallback = false; /* assume rdma capability first */
@@ -932,16 +931,8 @@ static void smc_fback_replace_callbacks(struct smc_sock *smc)
 	write_unlock_bh(&clcsk->sk_callback_lock);
 }

-static int smc_switch_to_fallback(struct smc_sock *smc, int reason_code)
+static void smc_switch_to_fallback(struct smc_sock *smc, int reason_code)
 {
-	int rc = 0;
-
-	mutex_lock(&smc->clcsock_release_lock);
-	if (!smc->clcsock) {
-		rc = -EBADF;
-		goto out;
-	}
-
 	smc->use_fallback = true;
 	smc->fallback_rsn = reason_code;
 	smc_stat_fallback(smc);
@@ -959,24 +950,12 @@ static int smc_switch_to_fallback(struct smc_sock *smc, int reason_code)
 		 */
 		smc_fback_replace_callbacks(smc);
 	}
-out:
-	mutex_unlock(&smc->clcsock_release_lock);
-	return rc;
 }

 /* fall back during connect */
 static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
 {
-	struct net *net = sock_net(&smc->sk);
-	int rc = 0;
-
-	rc = smc_switch_to_fallback(smc, reason_code);
-	if (rc) { /* fallback fails */
-		this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
-		if (smc->sk.sk_state == SMC_INIT)
-			sock_put(&smc->sk); /* passive closing */
-		return rc;
-	}
+	smc_switch_to_fallback(smc, reason_code);
 	smc_copy_sock_settings_to_clc(smc);
 	smc->connect_nonblock = 0;
 	if (smc->sk.sk_state == SMC_INIT)
@@ -1757,10 +1736,10 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
 	}
 	*new_smc = smc_sk(new_sk);

-	mutex_lock(&lsmc->clcsock_release_lock);
-	if (lsmc->clcsock)
-		rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
-	mutex_unlock(&lsmc->clcsock_release_lock);
+	/* The listener lock is dropped here. smc_clcsock_release() flushes
+	 * smc_tcp_listen_work() before it releases the clcsock.
+	 */
+	rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
 	lock_sock(lsk);
 	if  (rc < 0 && rc != -EAGAIN)
 		lsk->sk_err = -rc;
@@ -1830,18 +1809,26 @@ struct sock *smc_accept_dequeue(struct sock *parent,
 				struct socket *new_sock)
 {
 	struct smc_sock *isk, *n;
+	struct socket *tcp;
 	struct sock *new_sk;

 	list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
 		new_sk = (struct sock *)isk;

 		smc_accept_unlink(new_sk);
+		/* smc_close_active_abort() and smc_close_passive_work()
+		 * decide under the socket lock whether the clcsock may be
+		 * released. Hold it across the state check and the graft so a
+		 * child that becomes file-referenced here cannot lose its
+		 * clcsock to an asynchronous close.
+		 */
+		lock_sock(new_sk);
 		if (new_sk->sk_state == SMC_CLOSED) {
 			new_sk->sk_prot->unhash(new_sk);
-			if (isk->clcsock) {
-				sock_release(isk->clcsock);
-				isk->clcsock = NULL;
-			}
+			tcp = xchg(&isk->clcsock, NULL);
+			release_sock(new_sk);
+			if (tcp)
+				sock_release(tcp);
 			sock_put(new_sk); /* final */
 			continue;
 		}
@@ -1853,6 +1840,7 @@ struct sock *smc_accept_dequeue(struct sock *parent,
 				isk->clcsock->file->private_data = isk->clcsock;
 			}
 		}
+		release_sock(new_sk);
 		return new_sk;
 	}
 	return NULL;
@@ -1983,12 +1971,12 @@ static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
 {
 	/* RDMA setup failed, switch back to TCP */
 	smc_conn_abort(new_smc, local_first);
-	if (reason_code < 0 ||
-	    smc_switch_to_fallback(new_smc, reason_code)) {
+	if (reason_code < 0) {
 		/* error, no fallback possible */
 		smc_listen_out_err(new_smc);
 		return;
 	}
+	smc_switch_to_fallback(new_smc, reason_code);
 	if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
 		if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
 			smc_listen_out_err(new_smc);
@@ -2478,11 +2466,8 @@ static void smc_listen_work(struct work_struct *work)

 	/* check if peer is smc capable */
 	if (!tcp_sk(newclcsock->sk)->syn_smc) {
-		rc = smc_switch_to_fallback(new_smc, SMC_CLC_DECL_PEERNOSMC);
-		if (rc)
-			smc_listen_out_err(new_smc);
-		else
-			smc_listen_out_connected(new_smc);
+		smc_switch_to_fallback(new_smc, SMC_CLC_DECL_PEERNOSMC);
+		smc_listen_out_connected(new_smc);
 		return;
 	}

@@ -2729,7 +2714,8 @@ int smc_accept(struct socket *sock, struct socket *new_sock,

 	lsmc = smc_sk(sk);
 	sock_hold(sk); /* sock_put below */
-	lock_sock(sk);
+	/* smc_accept_dequeue() locks the child socket as well. */
+	lock_sock_nested(sk, SINGLE_DEPTH_NESTING);

 	if (lsmc->sk.sk_state != SMC_LISTEN) {
 		rc = -EINVAL;
@@ -2750,7 +2736,7 @@ int smc_accept(struct socket *sock, struct socket *new_sock,
 		timeo = schedule_timeout(timeo);
 		/* wakeup by sk_data_ready in smc_listen_work() */
 		sched_annotate_sleep();
-		lock_sock(sk);
+		lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
 		if (signal_pending(current)) {
 			rc = sock_intr_errno(timeo);
 			break;
@@ -2791,19 +2777,13 @@ int smc_getname(struct socket *sock, struct sockaddr *addr,
 		int peer)
 {
 	struct smc_sock *smc;
-	int rc = -EBADF;

 	if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
 	    (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
 		return -ENOTCONN;

 	smc = smc_sk(sock->sk);
-
-	mutex_lock(&smc->clcsock_release_lock);
-	if (smc->clcsock)
-		rc = smc->clcsock->ops->getname(smc->clcsock, addr, peer);
-	mutex_unlock(&smc->clcsock_release_lock);
-	return rc;
+	return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
 }

 int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
@@ -2819,9 +2799,7 @@ int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
 	if (msg->msg_flags & MSG_FASTOPEN) {
 		/* not connected yet, fallback */
 		if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
-			rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
-			if (rc)
-				goto out;
+			smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
 		} else {
 			rc = -EINVAL;
 			goto out;
@@ -3110,11 +3088,6 @@ int smc_setsockopt(struct socket *sock, int level, int optname,
 	/* generic setsockopts reaching us here always apply to the
 	 * CLC socket
 	 */
-	mutex_lock(&smc->clcsock_release_lock);
-	if (!smc->clcsock) {
-		mutex_unlock(&smc->clcsock_release_lock);
-		return -EBADF;
-	}
 	if (unlikely(!smc->clcsock->ops->setsockopt))
 		rc = -EOPNOTSUPP;
 	else
@@ -3124,7 +3097,6 @@ int smc_setsockopt(struct socket *sock, int level, int optname,
 		sk->sk_err = smc->clcsock->sk->sk_err;
 		sk_error_report(sk);
 	}
-	mutex_unlock(&smc->clcsock_release_lock);

 	if (optlen < sizeof(int))
 		return -EINVAL;
@@ -3141,7 +3113,7 @@ int smc_setsockopt(struct socket *sock, int level, int optname,
 	case TCP_FASTOPEN_NO_COOKIE:
 		/* option not supported by SMC */
 		if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
-			rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
+			smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
 		} else {
 			rc = -EINVAL;
 		}
@@ -3186,26 +3158,16 @@ int smc_getsockopt(struct socket *sock, int level, int optname,
 		   char __user *optval, int __user *optlen)
 {
 	struct smc_sock *smc;
-	int rc;

 	if (level == SOL_SMC)
 		return __smc_getsockopt(sock, level, optname, optval, optlen);

 	smc = smc_sk(sock->sk);
-	mutex_lock(&smc->clcsock_release_lock);
-	if (!smc->clcsock) {
-		mutex_unlock(&smc->clcsock_release_lock);
-		return -EBADF;
-	}
 	/* socket options apply to the CLC socket */
-	if (unlikely(!smc->clcsock->ops->getsockopt)) {
-		mutex_unlock(&smc->clcsock_release_lock);
+	if (unlikely(!smc->clcsock->ops->getsockopt))
 		return -EOPNOTSUPP;
-	}
-	rc = smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
-					   optval, optlen);
-	mutex_unlock(&smc->clcsock_release_lock);
-	return rc;
+	return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
+					     optval, optlen);
 }

 int smc_ioctl(struct socket *sock, unsigned int cmd,
@@ -3220,10 +3182,6 @@ int smc_ioctl(struct socket *sock, unsigned int cmd,
 	conn = &smc->conn;
 	lock_sock(&smc->sk);
 	if (smc->use_fallback) {
-		if (!smc->clcsock) {
-			release_sock(&smc->sk);
-			return -EBADF;
-		}
 		answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
 		release_sock(&smc->sk);
 		return answ;
diff --git a/net/smc/smc.h b/net/smc/smc.h
index 36b243b34a9a..616bb3245f7b 100644
--- a/net/smc/smc.h
+++ b/net/smc/smc.h
@@ -325,10 +325,6 @@ struct smc_sock {				/* smc sock container */
 						/* non-blocking connect in
 						 * flight
 						 */
-	struct mutex            clcsock_release_lock;
-						/* protects clcsock of a listen
-						 * socket
-						 * */
 };

 #define smc_sk(ptr) container_of_const(ptr, struct smc_sock, sk)
diff --git a/net/smc/smc_close.c b/net/smc/smc_close.c
index cf1add1ffda6..e71245dbb296 100644
--- a/net/smc/smc_close.c
+++ b/net/smc/smc_close.c
@@ -27,13 +27,13 @@ void smc_clcsock_release(struct smc_sock *smc)

 	if (smc->listen_smc && current_work() != &smc->smc_listen_work)
 		cancel_work_sync(&smc->smc_listen_work);
-	mutex_lock(&smc->clcsock_release_lock);
-	if (smc->clcsock) {
-		tcp = smc->clcsock;
-		smc->clcsock = NULL;
+	/* smc_tcp_listen_work() dereferences smc->clcsock outside the
+	 * socket lock; wait for it before the clcsock goes away.
+	 */
+	flush_work(&smc->tcp_listen_work);
+	tcp = xchg(&smc->clcsock, NULL);
+	if (tcp)
 		sock_release(tcp);
-	}
-	mutex_unlock(&smc->clcsock_release_lock);
 }

 /* The clcsock can be released once no socket file can reach it; while a
diff --git a/net/smc/smc_inet.c b/net/smc/smc_inet.c
index 520b666fdd8f..76cde321a8ea 100644
--- a/net/smc/smc_inet.c
+++ b/net/smc/smc_inet.c
@@ -122,11 +122,10 @@ static int smc_inet_init_sock(struct sock *sk)

 static void smc_inet_destroy_sock(struct sock *sk)
 {
-	/* The sock is hashed and smc_diag dumps dereference smc->clcsock
-	 * without clcsock_release_lock, while sk_common_release() calls
-	 * .destroy before .unhash. Unhash first, as __smc_release() does,
-	 * so no dump can observe the clcsock being released; the second
-	 * unhash is a no-op.
+	/* The sock is hashed and smc_diag dumps dereference smc->clcsock,
+	 * while sk_common_release() calls .destroy before .unhash. Unhash
+	 * first, as __smc_release() does, so no dump can observe the
+	 * clcsock being released; the second unhash is a no-op.
 	 */
 	sk->sk_prot->unhash(sk);
 	smc_clcsock_release(smc_sk(sk));
-- 
2.50.1 (Apple Git-155)


  parent reply	other threads:[~2026-11-20 12:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 12:45 [PATCH net-next 0/3] net/smc: fix clcsock lifetime vs asynchronous paths Mahanta Jambigi
2026-11-20 12:45 ` [PATCH net-next 1/3] net/smc: keep clcsock until socket release Mahanta Jambigi
2026-11-21 12:46   ` sashiko-bot
2026-11-20 12:45 ` Mahanta Jambigi [this message]
2026-11-21 12:46   ` [PATCH net-next 2/3] net/smc: remove the clcsock release lock sashiko-bot
2026-11-20 12:45 ` [PATCH net-next 3/3] net/smc: document the clcsock lifetime Mahanta Jambigi
2026-11-21 12:46 ` [PATCH net-next 0/3] net/smc: fix clcsock lifetime vs asynchronous paths sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120124534.430200-3-mjambigi@linux.ibm.com \
    --to=mjambigi@linux.ibm.com \
    --cc=alibuda@linux.alibaba.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=dust.li@linux.alibaba.com \
    --cc=edumazet@google.com \
    --cc=guwen@linux.alibaba.com \
    --cc=hidayath@linux.ibm.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pasic@linux.ibm.com \
    --cc=sidraya@linux.ibm.com \
    --cc=tonylu@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.