net: Remove redundant calls of sk_tx_queue_clear().
[platform/kernel/linux-starfive.git] / net / core / sock.c
index bbcd4b9..648a5cb 100644 (file)
@@ -1657,6 +1657,16 @@ static void sock_copy(struct sock *nsk, const struct sock *osk)
 #ifdef CONFIG_SECURITY_NETWORK
        void *sptr = nsk->sk_security;
 #endif
+
+       /* If we move sk_tx_queue_mapping out of the private section,
+        * we must check if sk_tx_queue_clear() is called after
+        * sock_copy() in sk_clone_lock().
+        */
+       BUILD_BUG_ON(offsetof(struct sock, sk_tx_queue_mapping) <
+                    offsetof(struct sock, sk_dontcopy_begin) ||
+                    offsetof(struct sock, sk_tx_queue_mapping) >=
+                    offsetof(struct sock, sk_dontcopy_end));
+
        memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
 
        memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
@@ -1690,7 +1700,6 @@ static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
 
                if (!try_module_get(prot->owner))
                        goto out_free_sec;
-               sk_tx_queue_clear(sk);
        }
 
        return sk;
@@ -1876,123 +1885,120 @@ static void sk_init_common(struct sock *sk)
 struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
 {
        struct proto *prot = READ_ONCE(sk->sk_prot);
-       struct sock *newsk;
+       struct sk_filter *filter;
        bool is_charged = true;
+       struct sock *newsk;
 
        newsk = sk_prot_alloc(prot, priority, sk->sk_family);
-       if (newsk != NULL) {
-               struct sk_filter *filter;
+       if (!newsk)
+               goto out;
 
-               sock_copy(newsk, sk);
+       sock_copy(newsk, sk);
 
-               newsk->sk_prot_creator = prot;
+       newsk->sk_prot_creator = prot;
 
-               /* SANITY */
-               if (likely(newsk->sk_net_refcnt))
-                       get_net(sock_net(newsk));
-               sk_node_init(&newsk->sk_node);
-               sock_lock_init(newsk);
-               bh_lock_sock(newsk);
-               newsk->sk_backlog.head  = newsk->sk_backlog.tail = NULL;
-               newsk->sk_backlog.len = 0;
+       /* SANITY */
+       if (likely(newsk->sk_net_refcnt))
+               get_net(sock_net(newsk));
+       sk_node_init(&newsk->sk_node);
+       sock_lock_init(newsk);
+       bh_lock_sock(newsk);
+       newsk->sk_backlog.head  = newsk->sk_backlog.tail = NULL;
+       newsk->sk_backlog.len = 0;
 
-               atomic_set(&newsk->sk_rmem_alloc, 0);
-               /*
-                * sk_wmem_alloc set to one (see sk_free() and sock_wfree())
-                */
-               refcount_set(&newsk->sk_wmem_alloc, 1);
-               atomic_set(&newsk->sk_omem_alloc, 0);
-               sk_init_common(newsk);
+       atomic_set(&newsk->sk_rmem_alloc, 0);
 
-               newsk->sk_dst_cache     = NULL;
-               newsk->sk_dst_pending_confirm = 0;
-               newsk->sk_wmem_queued   = 0;
-               newsk->sk_forward_alloc = 0;
-               atomic_set(&newsk->sk_drops, 0);
-               newsk->sk_send_head     = NULL;
-               newsk->sk_userlocks     = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
-               atomic_set(&newsk->sk_zckey, 0);
+       /* sk_wmem_alloc set to one (see sk_free() and sock_wfree()) */
+       refcount_set(&newsk->sk_wmem_alloc, 1);
 
-               sock_reset_flag(newsk, SOCK_DONE);
+       atomic_set(&newsk->sk_omem_alloc, 0);
+       sk_init_common(newsk);
 
-               /* sk->sk_memcg will be populated at accept() time */
-               newsk->sk_memcg = NULL;
+       newsk->sk_dst_cache     = NULL;
+       newsk->sk_dst_pending_confirm = 0;
+       newsk->sk_wmem_queued   = 0;
+       newsk->sk_forward_alloc = 0;
+       atomic_set(&newsk->sk_drops, 0);
+       newsk->sk_send_head     = NULL;
+       newsk->sk_userlocks     = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
+       atomic_set(&newsk->sk_zckey, 0);
 
-               cgroup_sk_clone(&newsk->sk_cgrp_data);
+       sock_reset_flag(newsk, SOCK_DONE);
 
-               rcu_read_lock();
-               filter = rcu_dereference(sk->sk_filter);
-               if (filter != NULL)
-                       /* though it's an empty new sock, the charging may fail
-                        * if sysctl_optmem_max was changed between creation of
-                        * original socket and cloning
-                        */
-                       is_charged = sk_filter_charge(newsk, filter);
-               RCU_INIT_POINTER(newsk->sk_filter, filter);
-               rcu_read_unlock();
+       /* sk->sk_memcg will be populated at accept() time */
+       newsk->sk_memcg = NULL;
 
-               if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
-                       /* We need to make sure that we don't uncharge the new
-                        * socket if we couldn't charge it in the first place
-                        * as otherwise we uncharge the parent's filter.
-                        */
-                       if (!is_charged)
-                               RCU_INIT_POINTER(newsk->sk_filter, NULL);
-                       sk_free_unlock_clone(newsk);
-                       newsk = NULL;
-                       goto out;
-               }
-               RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
+       cgroup_sk_clone(&newsk->sk_cgrp_data);
 
-               if (bpf_sk_storage_clone(sk, newsk)) {
-                       sk_free_unlock_clone(newsk);
-                       newsk = NULL;
-                       goto out;
-               }
+       rcu_read_lock();
+       filter = rcu_dereference(sk->sk_filter);
+       if (filter != NULL)
+               /* though it's an empty new sock, the charging may fail
+                * if sysctl_optmem_max was changed between creation of
+                * original socket and cloning
+                */
+               is_charged = sk_filter_charge(newsk, filter);
+       RCU_INIT_POINTER(newsk->sk_filter, filter);
+       rcu_read_unlock();
 
-               /* Clear sk_user_data if parent had the pointer tagged
-                * as not suitable for copying when cloning.
+       if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
+               /* We need to make sure that we don't uncharge the new
+                * socket if we couldn't charge it in the first place
+                * as otherwise we uncharge the parent's filter.
                 */
-               if (sk_user_data_is_nocopy(newsk))
-                       newsk->sk_user_data = NULL;
+               if (!is_charged)
+                       RCU_INIT_POINTER(newsk->sk_filter, NULL);
+               sk_free_unlock_clone(newsk);
+               newsk = NULL;
+               goto out;
+       }
+       RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
 
-               newsk->sk_err      = 0;
-               newsk->sk_err_soft = 0;
-               newsk->sk_priority = 0;
-               newsk->sk_incoming_cpu = raw_smp_processor_id();
-               if (likely(newsk->sk_net_refcnt))
-                       sock_inuse_add(sock_net(newsk), 1);
+       if (bpf_sk_storage_clone(sk, newsk)) {
+               sk_free_unlock_clone(newsk);
+               newsk = NULL;
+               goto out;
+       }
 
-               /*
-                * Before updating sk_refcnt, we must commit prior changes to memory
-                * (Documentation/RCU/rculist_nulls.rst for details)
-                */
-               smp_wmb();
-               refcount_set(&newsk->sk_refcnt, 2);
+       /* Clear sk_user_data if parent had the pointer tagged
+        * as not suitable for copying when cloning.
+        */
+       if (sk_user_data_is_nocopy(newsk))
+               newsk->sk_user_data = NULL;
 
-               /*
-                * Increment the counter in the same struct proto as the master
-                * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
-                * is the same as sk->sk_prot->socks, as this field was copied
-                * with memcpy).
-                *
-                * This _changes_ the previous behaviour, where
-                * tcp_create_openreq_child always was incrementing the
-                * equivalent to tcp_prot->socks (inet_sock_nr), so this have
-                * to be taken into account in all callers. -acme
-                */
-               sk_refcnt_debug_inc(newsk);
-               sk_set_socket(newsk, NULL);
-               sk_tx_queue_clear(newsk);
-               RCU_INIT_POINTER(newsk->sk_wq, NULL);
+       newsk->sk_err      = 0;
+       newsk->sk_err_soft = 0;
+       newsk->sk_priority = 0;
+       newsk->sk_incoming_cpu = raw_smp_processor_id();
+       if (likely(newsk->sk_net_refcnt))
+               sock_inuse_add(sock_net(newsk), 1);
 
-               if (newsk->sk_prot->sockets_allocated)
-                       sk_sockets_allocated_inc(newsk);
+       /* Before updating sk_refcnt, we must commit prior changes to memory
+        * (Documentation/RCU/rculist_nulls.rst for details)
+        */
+       smp_wmb();
+       refcount_set(&newsk->sk_refcnt, 2);
 
-               if (sock_needs_netstamp(sk) &&
-                   newsk->sk_flags & SK_FLAGS_TIMESTAMP)
-                       net_enable_timestamp();
-       }
+       /* Increment the counter in the same struct proto as the master
+        * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
+        * is the same as sk->sk_prot->socks, as this field was copied
+        * with memcpy).
+        *
+        * This _changes_ the previous behaviour, where
+        * tcp_create_openreq_child always was incrementing the
+        * equivalent to tcp_prot->socks (inet_sock_nr), so this have
+        * to be taken into account in all callers. -acme
+        */
+       sk_refcnt_debug_inc(newsk);
+       sk_set_socket(newsk, NULL);
+       sk_tx_queue_clear(newsk);
+       RCU_INIT_POINTER(newsk->sk_wq, NULL);
+
+       if (newsk->sk_prot->sockets_allocated)
+               sk_sockets_allocated_inc(newsk);
+
+       if (sock_needs_netstamp(sk) && newsk->sk_flags & SK_FLAGS_TIMESTAMP)
+               net_enable_timestamp();
 out:
        return newsk;
 }