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. 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 it releases the clcsock: smc_conn_kill() and smc_close_passive_work() check conn->freed and return early once the connection has been freed. 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 bfc0c7e71a12..1d8a602729c1 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 736abfbd2aaf..cf1add1ffda6 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 @@ static void smc_close_passive_work(struct work_struct *work) (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)