The 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. Signed-off-by: Mahanta Jambigi --- 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(-) diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c index 7fd79bbba..912da20f5 100644 --- a/net/smc/af_smc.c +++ b/net/smc/af_smc.c @@ -373,6 +373,14 @@ int smc_release(struct socket *sock) 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) diff --git a/net/smc/smc.h b/net/smc/smc.h index 616bb3245..c6bfe63a9 100644 --- a/net/smc/smc.h +++ b/net/smc/smc.h @@ -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); diff --git a/net/smc/smc_close.c b/net/smc/smc_close.c index e71245dbb..d359436e5 100644 --- a/net/smc/smc_close.c +++ b/net/smc/smc_close.c @@ -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; diff --git a/net/smc/smc_diag.c b/net/smc/smc_diag.c index 15485a08d..42828ffe9 100644 --- a/net/smc/smc_diag.c +++ b/net/smc/smc_diag.c @@ -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); -- 2.50.1 (Apple Git-155)