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 --- 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 7415829da..7fd79bbba 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 = 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; diff --git a/net/smc/smc.h b/net/smc/smc.h index 36b243b34..616bb3245 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 cf1add1ff..e71245dbb 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 520b666fd..76cde321a 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)