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. Signed-off-by: Mahanta Jambigi --- net/smc/af_smc.c | 4 ++-- net/smc/smc_close.c | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c index c0d9051..7415829 100644 --- a/net/smc/af_smc.c +++ b/net/smc/af_smc.c @@ -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; diff --git a/net/smc/smc_close.c b/net/smc/smc_close.c index 736abfb..cf1add1 100644 --- a/net/smc/smc_close.c +++ b/net/smc/smc_close.c @@ -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; } } -- 2.50.1 (Apple Git-155)