net/smc: fix clcsock lifetime vs asynchronous paths
Subsystems (5)
Mahanta Jambigi
maintainerv2 cover net/smc: fix clcsock lifetime vs asynchronous paths
The internal TCP socket (clcsock) of an SMC socket is freed by asynchronous paths: link group termination reaches smc_close_active_abort() and peer close reaches smc_close_passive_work(), and both can run while the SMC socket is still referenced by an open file descriptor. A task that uses the fd at that moment, for example getsockopt() or a socket diag dump, then operates on a released socket.
Free the clcsock only when no socket file can reference it: from the socket release paths, after the socket has been unhashed, and from the abnormal close paths once the socket has no file left. The decision is made under the socket lock, and the accept path holds the same lock across the accept-queue state check and sock_graft(), so a connection that is being accepted cannot lose its clcsock to an asynchronous close. The clcsock_release_lock is no longer needed: a socket operation on a file is serialized with smc_release() by the file reference, and the listen work is covered by flush_work() and cancel_work_sync() in smc_clcsock_release().
Trade-off: with an open file, a clcsock now stays allocated after an abnormal close until the application closes the SMC fd. The SMC connection is already torn down and its TCP connection has been reset or shut down; only the local TCP socket remains allocated until then.
Patch 1 makes the lifetime change and is self-contained. Patch 2 removes the lock and the checks it made necessary, and serializes the accept path with the asynchronous close. Patch 3 documents the invariant and adds a destructor warning for it. The diag dump keeps its NULL check: a socket is hashed in smc_sk_init(), before its clcsock is created.
This is hardening found by code walk, not a fix for a reported regression, so the series targets net-next and carries no Fixes: tag.
---
Changes in v2:
- Rebased onto net-next (eb6d6c80d354) instead of the torvalds tree (7e1392a42ea2). The patch content is unchanged; the only differences are unrelated net-next context: patch 2 now shows the READ_ONCE() around limit_smc_hs, and hunk labels were re-derived.
- Patch 1: clarify the commit message to say the issue was found by code inspection rather than from a reported regression, and to describe the conn->freed rationale more precisely.
- No functional change to the series.
Mahanta Jambigi (3):
net/smc: keep clcsock until socket release
net/smc: remove the clcsock release lock
net/smc: document the clcsock lifetime
net/smc/af_smc.c | 123 +++++++++++++++++--------------------------- net/smc/smc.h | 8 ++- net/smc/smc_close.c | 35 +++++++++---- net/smc/smc_diag.c | 3 ++ net/smc/smc_inet.c | 9 ++-- 5 files changed, 82 insertions(+), 96 deletions(-)
base-commit: eb6d6c80d35449c27471b1874f13066c09a8964d
Signature · 1 line
2.50.1 (Apple Git-155)
Mahanta Jambigi
maintainerPATCH v2 1/3 net/smc: keep clcsock until socket release
The clcsock of an SMC socket can be freed from asynchronous contexts:
link group termination reaches smc_close_active_abort() and peer close reaches smc_close_passive_work(), and both can run while the socket is referenced by an open file. A task that uses the fd then operates on a released socket; this was found by code inspection, not from a reported regression.
Free the clcsock only when no socket file can reference it: from the release paths, and from the abnormal close paths only once the socket has no file left. __smc_release() now tears the connection down before releasing the clcsock, which is safe because smc_conn_kill() and smc_close_passive_work() return early once conn->freed is set, so a clcsock can stay allocated until the application closes the socket.
--- net/smc/af_smc.c | 4 ++-- net/smc/smc_close.c | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-)
net/smc/af_smc.c +2 -2
@@ -313,13 +313,13 @@ static int __smc_release(struct smc_sock *smc) sk->sk_prot->unhash (sk);if (sk->sk_state ==SMC_CLOSED ) { +if (!smc->use_fallback) +smc_conn_free (&smc->conn);if (smc->clcsock) {release_sock (sk);smc_clcsock_release (smc);lock_sock (sk); } -if (!smc->use_fallback) -smc_conn_free (&smc->conn); }return rc;
net/smc/smc_close.c +13 -3
@@ -36,6 +36,14 @@ void smc_clcsock_release(struct smc_sock *smc)mutex_unlock (&smc->clcsock_release_lock); } +/* The clcsock can be released once no socket file can reach it; while a +* file is still open, smc_release() releases it. +*/ +static bool smc_clcsock_can_release (struct smc_sock *smc) +{ +return smc->clcsock && !smc->sk.sk_socket; +} +static void smc_close_cleanup_listen (struct sock *parent) {struct sock *sk;
@@ -157,7 +165,8 @@ void smc_close_active_abort(struct smc_sock *smc) sk->sk_state =SMC_CLOSED ; sk->sk_prot->unhash (sk);smc_conn_free (&smc->conn); - release_clcsock = true; +if (smc_clcsock_can_release (smc)) + release_clcsock = true;sock_put (sk);/* passive closing */ break ;case SMC_PROCESSABORT :
@@ -169,7 +178,8 @@ void smc_close_active_abort(struct smc_sock *smc) sk->sk_state =SMC_CLOSED ; sk->sk_prot->unhash (sk);smc_conn_free (&smc->conn); - release_clcsock = true; +if (smc_clcsock_can_release (smc)) + release_clcsock = true;break ;case SMC_INIT :case SMC_PEERABORTWAIT :
@@ -452,7 +462,7 @@ wakeup: (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket)) { sk->sk_prot->unhash (sk);smc_conn_free (conn); -if (smc->clcsock) +if (smc_clcsock_can_release (smc)) release_clcsock = true; } }
Signature · 1 line
2.50.1 (Apple Git-155)
Mahanta Jambigi
maintainerPATCH v2 2/3 net/smc: remove the clcsock release lock
In reply to Mahanta JambigiThe 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.
--- 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(-)
net/smc/af_smc.c +32 -74
@@ -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 =READ_ONCE (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;
net/smc/smc.h +0 -4
@@ -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)
net/smc/smc_close.c +6 -6
@@ -27,13 +27,13 @@ void smc_clcsock_release(struct smc_sock *smc)if (smc->listen_smc &¤t_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
net/smc/smc_inet.c +4 -5
@@ -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));
Signature · 1 line
2.50.1 (Apple Git-155)
Mahanta Jambigi
maintainerPATCH v2 3/3 net/smc: document the clcsock lifetime
In reply to Mahanta JambigiThe clcsock is freed only when no socket file can reference it. Record that invariant where it is relied upon: at the clcsock field, at the release helper, at the diag dump, which can find a socket before its clcsock has been created, at the accept cleanup, which must not wait for the listen work with the child and listener locks held, and in the destructor, which warns if a clcsock is left.
Freeing the clcsock from the destructor is not possible: it can run from an RCU callback or a softirq.
--- net/smc/af_smc.c | 13 +++++++++++++ net/smc/smc.h | 4 +++- net/smc/smc_close.c | 7 ++++++- net/smc/smc_diag.c | 3 +++ 4 files changed, 25 insertions(+), 2 deletions(-)
net/smc/af_smc.c +13 -0
@@ -373,6 +373,14 @@ out:static void smc_destruct (struct sock *sk) { +struct smc_sock *smc =smc_sk (sk); + +/* The destructor can run from an RCU callback or a softirq, where +* sock_release() must not be called. Every path that drops an SMC +* socket therefore releases its clcsock before the final +* sock_put(). +*/ +WARN_ON_ONCE (smc->clcsock);if (sk->sk_state !=SMC_CLOSED )return ;if (!sock_flag (sk,SOCK_DEAD ))
@@ -1825,6 +1833,11 @@ struct sock *smc_accept_dequeue(struct sock *parent,lock_sock (new_sk);if (new_sk->sk_state ==SMC_CLOSED ) { new_sk->sk_prot->unhash (new_sk); +/* The child is on the accept queue, so its +* smc_listen_work() no longer uses the clcsock. Do not +* wait for that work here, the child and listener locks +* are held. +*/ tcp =xchg (&isk->clcsock,NULL );release_sock (new_sk);if (tcp)
net/smc/smc.h +3 -1
@@ -288,7 +288,9 @@ struct smc_sock { /* smc sock container */struct sock sk;struct inet_sock icsk_inet; }; -struct socket *clcsock;/* internal tcp socket */ +struct socket *clcsock;/* internal tcp socket, freed +* when no file can use it +*/ void (*clcsk_state_change)(struct sock *sk);/* original stat_change fct. */ void (*clcsk_data_ready)(struct sock *sk);
net/smc/smc_close.c +6 -1
@@ -20,7 +20,12 @@#include "smc_cdc.h" #include "smc_close.h" -/* release the clcsock that is assigned to the smc_sock */ +/* Release the clcsock. Call this only after the socket has been +* unhashed, and only when no socket file can reach the clcsock: from +* smc_release(), from the listener cleanup, and from the abnormal +* close paths once the socket has no file left. While a file is open, +* the clcsock stays in place until smc_release() runs. +*/ void smc_clcsock_release (struct smc_sock *smc) {struct socket *tcp;
net/smc/smc_diag.c +3 -0
@@ -39,6 +39,9 @@ static void smc_diag_msg_common_fill(struct smc_diag_msg *r, struct sock *sk)memset (r,0 ,sizeof (*r)); r->diag_family = sk->sk_family;sock_diag_save_cookie (sk, r->id.idiag_cookie); +/* The socket is hashed in smc_sk_init(), before its clcsock is +* created, so a dump can find it without one. +*/ if (!smc->clcsock)return ; r->id.idiag_sport =htons (smc->clcsock->sk->sk_num);
Signature · 1 line
2.50.1 (Apple Git-155)