]> www.infradead.org Git - users/hch/configfs.git/commitdiff
net/smc: refactoring initialization of smc sock
authorD. Wythe <alibuda@linux.alibaba.com>
Thu, 13 Jun 2024 18:00:28 +0000 (02:00 +0800)
committerDavid S. Miller <davem@davemloft.net>
Mon, 17 Jun 2024 12:14:09 +0000 (13:14 +0100)
This patch aims to isolate the shared components of SMC socket
allocation by introducing smc_sk_init() for sock initialization
and __smc_create_clcsk() for the initialization of clcsock.

This is in preparation for the subsequent implementation of the
AF_INET version of SMC.

Signed-off-by: D. Wythe <alibuda@linux.alibaba.com>
Reviewed-by: Tony Lu <tonylu@linux.alibaba.com>
Reviewed-by: Wenjia Zhang <wenjia@linux.ibm.com>
Reviewed-by: Dust Li <dust.li@linux.alibaba.com>
Tested-by: Niklas Schnelle <schnelle@linux.ibm.com>
Tested-by: Wenjia Zhang <wenjia@linux.ibm.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/smc/af_smc.c
net/smc/smc.h

index c5f98c6b25613f542066f8fab1bb0d630d46a77d..0508ea20b3c3bab1f5d9171d8906192c5dddabf4 100644 (file)
@@ -361,25 +361,15 @@ static void smc_destruct(struct sock *sk)
                return;
 }
 
-static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
-                                  int protocol)
+void smc_sk_init(struct net *net, struct sock *sk, int protocol)
 {
-       struct smc_sock *smc;
-       struct proto *prot;
-       struct sock *sk;
-
-       prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
-       sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
-       if (!sk)
-               return NULL;
+       struct smc_sock *smc = smc_sk(sk);
 
-       sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
        sk->sk_state = SMC_INIT;
        sk->sk_destruct = smc_destruct;
        sk->sk_protocol = protocol;
        WRITE_ONCE(sk->sk_sndbuf, 2 * READ_ONCE(net->smc.sysctl_wmem));
        WRITE_ONCE(sk->sk_rcvbuf, 2 * READ_ONCE(net->smc.sysctl_rmem));
-       smc = smc_sk(sk);
        INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
        INIT_WORK(&smc->connect_work, smc_connect_work);
        INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
@@ -389,6 +379,24 @@ static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
        sk->sk_prot->hash(sk);
        mutex_init(&smc->clcsock_release_lock);
        smc_init_saved_callbacks(smc);
+       smc->limit_smc_hs = net->smc.limit_smc_hs;
+       smc->use_fallback = false; /* assume rdma capability first */
+       smc->fallback_rsn = 0;
+}
+
+static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
+                                  int protocol)
+{
+       struct proto *prot;
+       struct sock *sk;
+
+       prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
+       sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
+       if (!sk)
+               return NULL;
+
+       sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
+       smc_sk_init(net, sk, protocol);
 
        return sk;
 }
@@ -3303,6 +3311,31 @@ static const struct proto_ops smc_sock_ops = {
        .splice_read    = smc_splice_read,
 };
 
+int smc_create_clcsk(struct net *net, struct sock *sk, int family)
+{
+       struct smc_sock *smc = smc_sk(sk);
+       int rc;
+
+       rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
+                             &smc->clcsock);
+       if (rc) {
+               sk_common_release(sk);
+               return rc;
+       }
+
+       /* smc_clcsock_release() does not wait smc->clcsock->sk's
+        * destruction;  its sk_state might not be TCP_CLOSE after
+        * smc->sk is close()d, and TCP timers can be fired later,
+        * which need net ref.
+        */
+       sk = smc->clcsock->sk;
+       __netns_tracker_free(net, &sk->ns_tracker, false);
+       sk->sk_net_refcnt = 1;
+       get_net_track(net, &sk->ns_tracker, GFP_KERNEL);
+       sock_inuse_add(net, 1);
+       return 0;
+}
+
 static int __smc_create(struct net *net, struct socket *sock, int protocol,
                        int kern, struct socket *clcsock)
 {
@@ -3328,35 +3361,12 @@ static int __smc_create(struct net *net, struct socket *sock, int protocol,
 
        /* create internal TCP socket for CLC handshake and fallback */
        smc = smc_sk(sk);
-       smc->use_fallback = false; /* assume rdma capability first */
-       smc->fallback_rsn = 0;
-
-       /* default behavior from limit_smc_hs in every net namespace */
-       smc->limit_smc_hs = net->smc.limit_smc_hs;
 
        rc = 0;
-       if (!clcsock) {
-               rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
-                                     &smc->clcsock);
-               if (rc) {
-                       sk_common_release(sk);
-                       goto out;
-               }
-
-               /* smc_clcsock_release() does not wait smc->clcsock->sk's
-                * destruction;  its sk_state might not be TCP_CLOSE after
-                * smc->sk is close()d, and TCP timers can be fired later,
-                * which need net ref.
-                */
-               sk = smc->clcsock->sk;
-               __netns_tracker_free(net, &sk->ns_tracker, false);
-               sk->sk_net_refcnt = 1;
-               get_net_track(net, &sk->ns_tracker, GFP_KERNEL);
-               sock_inuse_add(net, 1);
-       } else {
+       if (clcsock)
                smc->clcsock = clcsock;
-       }
-
+       else
+               rc = smc_create_clcsk(net, sk, family);
 out:
        return rc;
 }
index 18c8b7870198be079a7e32d87075a4f76093b140..3edec1e133d87218c586acc5b1fd1e8ba82d8385 100644 (file)
 extern struct proto smc_proto;
 extern struct proto smc_proto6;
 
+/* smc sock initialization */
+void smc_sk_init(struct net *net, struct sock *sk, int protocol);
+/* clcsock initialization */
+int smc_create_clcsk(struct net *net, struct sock *sk, int family);
+
 #ifdef ATOMIC64_INIT
 #define KERNEL_HAS_ATOMIC64
 #endif