@@ -1883,123 +1883,120 @@ static void sk_init_common(struct sock *sk)
18831883struct sock * sk_clone_lock (const struct sock * sk , const gfp_t priority )
18841884{
18851885 struct proto * prot = READ_ONCE (sk -> sk_prot );
1886- struct sock * newsk ;
1886+ struct sk_filter * filter ;
18871887 bool is_charged = true;
1888+ struct sock * newsk ;
18881889
18891890 newsk = sk_prot_alloc (prot , priority , sk -> sk_family );
1890- if (newsk != NULL ) {
1891- struct sk_filter * filter ;
1891+ if (! newsk )
1892+ goto out ;
18921893
1893- sock_copy (newsk , sk );
1894+ sock_copy (newsk , sk );
18941895
1895- newsk -> sk_prot_creator = prot ;
1896+ newsk -> sk_prot_creator = prot ;
18961897
1897- /* SANITY */
1898- if (likely (newsk -> sk_net_refcnt ))
1899- get_net (sock_net (newsk ));
1900- sk_node_init (& newsk -> sk_node );
1901- sock_lock_init (newsk );
1902- bh_lock_sock (newsk );
1903- newsk -> sk_backlog .head = newsk -> sk_backlog .tail = NULL ;
1904- newsk -> sk_backlog .len = 0 ;
1898+ /* SANITY */
1899+ if (likely (newsk -> sk_net_refcnt ))
1900+ get_net (sock_net (newsk ));
1901+ sk_node_init (& newsk -> sk_node );
1902+ sock_lock_init (newsk );
1903+ bh_lock_sock (newsk );
1904+ newsk -> sk_backlog .head = newsk -> sk_backlog .tail = NULL ;
1905+ newsk -> sk_backlog .len = 0 ;
19051906
1906- atomic_set (& newsk -> sk_rmem_alloc , 0 );
1907- /*
1908- * sk_wmem_alloc set to one (see sk_free() and sock_wfree())
1909- */
1910- refcount_set (& newsk -> sk_wmem_alloc , 1 );
1911- atomic_set (& newsk -> sk_omem_alloc , 0 );
1912- sk_init_common (newsk );
1907+ atomic_set (& newsk -> sk_rmem_alloc , 0 );
19131908
1914- newsk -> sk_dst_cache = NULL ;
1915- newsk -> sk_dst_pending_confirm = 0 ;
1916- newsk -> sk_wmem_queued = 0 ;
1917- newsk -> sk_forward_alloc = 0 ;
1918- atomic_set (& newsk -> sk_drops , 0 );
1919- newsk -> sk_send_head = NULL ;
1920- newsk -> sk_userlocks = sk -> sk_userlocks & ~SOCK_BINDPORT_LOCK ;
1921- atomic_set (& newsk -> sk_zckey , 0 );
1909+ /* sk_wmem_alloc set to one (see sk_free() and sock_wfree()) */
1910+ refcount_set (& newsk -> sk_wmem_alloc , 1 );
19221911
1923- sock_reset_flag (newsk , SOCK_DONE );
1912+ atomic_set (& newsk -> sk_omem_alloc , 0 );
1913+ sk_init_common (newsk );
19241914
1925- /* sk->sk_memcg will be populated at accept() time */
1926- newsk -> sk_memcg = NULL ;
1915+ newsk -> sk_dst_cache = NULL ;
1916+ newsk -> sk_dst_pending_confirm = 0 ;
1917+ newsk -> sk_wmem_queued = 0 ;
1918+ newsk -> sk_forward_alloc = 0 ;
1919+ atomic_set (& newsk -> sk_drops , 0 );
1920+ newsk -> sk_send_head = NULL ;
1921+ newsk -> sk_userlocks = sk -> sk_userlocks & ~SOCK_BINDPORT_LOCK ;
1922+ atomic_set (& newsk -> sk_zckey , 0 );
19271923
1928- cgroup_sk_clone ( & newsk -> sk_cgrp_data );
1924+ sock_reset_flag ( newsk , SOCK_DONE );
19291925
1930- rcu_read_lock ();
1931- filter = rcu_dereference (sk -> sk_filter );
1932- if (filter != NULL )
1933- /* though it's an empty new sock, the charging may fail
1934- * if sysctl_optmem_max was changed between creation of
1935- * original socket and cloning
1936- */
1937- is_charged = sk_filter_charge (newsk , filter );
1938- RCU_INIT_POINTER (newsk -> sk_filter , filter );
1939- rcu_read_unlock ();
1926+ /* sk->sk_memcg will be populated at accept() time */
1927+ newsk -> sk_memcg = NULL ;
19401928
1941- if (unlikely (!is_charged || xfrm_sk_clone_policy (newsk , sk ))) {
1942- /* We need to make sure that we don't uncharge the new
1943- * socket if we couldn't charge it in the first place
1944- * as otherwise we uncharge the parent's filter.
1945- */
1946- if (!is_charged )
1947- RCU_INIT_POINTER (newsk -> sk_filter , NULL );
1948- sk_free_unlock_clone (newsk );
1949- newsk = NULL ;
1950- goto out ;
1951- }
1952- RCU_INIT_POINTER (newsk -> sk_reuseport_cb , NULL );
1929+ cgroup_sk_clone (& newsk -> sk_cgrp_data );
19531930
1954- if (bpf_sk_storage_clone (sk , newsk )) {
1955- sk_free_unlock_clone (newsk );
1956- newsk = NULL ;
1957- goto out ;
1958- }
1931+ rcu_read_lock ();
1932+ filter = rcu_dereference (sk -> sk_filter );
1933+ if (filter != NULL )
1934+ /* though it's an empty new sock, the charging may fail
1935+ * if sysctl_optmem_max was changed between creation of
1936+ * original socket and cloning
1937+ */
1938+ is_charged = sk_filter_charge (newsk , filter );
1939+ RCU_INIT_POINTER (newsk -> sk_filter , filter );
1940+ rcu_read_unlock ();
19591941
1960- /* Clear sk_user_data if parent had the pointer tagged
1961- * as not suitable for copying when cloning.
1942+ if (unlikely (!is_charged || xfrm_sk_clone_policy (newsk , sk ))) {
1943+ /* We need to make sure that we don't uncharge the new
1944+ * socket if we couldn't charge it in the first place
1945+ * as otherwise we uncharge the parent's filter.
19621946 */
1963- if (sk_user_data_is_nocopy (newsk ))
1964- newsk -> sk_user_data = NULL ;
1947+ if (!is_charged )
1948+ RCU_INIT_POINTER (newsk -> sk_filter , NULL );
1949+ sk_free_unlock_clone (newsk );
1950+ newsk = NULL ;
1951+ goto out ;
1952+ }
1953+ RCU_INIT_POINTER (newsk -> sk_reuseport_cb , NULL );
19651954
1966- newsk -> sk_err = 0 ;
1967- newsk -> sk_err_soft = 0 ;
1968- newsk -> sk_priority = 0 ;
1969- newsk -> sk_incoming_cpu = raw_smp_processor_id ();
1970- if (likely (newsk -> sk_net_refcnt ))
1971- sock_inuse_add (sock_net (newsk ), 1 );
1955+ if (bpf_sk_storage_clone (sk , newsk )) {
1956+ sk_free_unlock_clone (newsk );
1957+ newsk = NULL ;
1958+ goto out ;
1959+ }
19721960
1973- /*
1974- * Before updating sk_refcnt, we must commit prior changes to memory
1975- * (Documentation/RCU/rculist_nulls.rst for details)
1976- */
1977- smp_wmb ();
1978- refcount_set (& newsk -> sk_refcnt , 2 );
1961+ /* Clear sk_user_data if parent had the pointer tagged
1962+ * as not suitable for copying when cloning.
1963+ */
1964+ if (sk_user_data_is_nocopy (newsk ))
1965+ newsk -> sk_user_data = NULL ;
19791966
1980- /*
1981- * Increment the counter in the same struct proto as the master
1982- * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
1983- * is the same as sk->sk_prot->socks, as this field was copied
1984- * with memcpy).
1985- *
1986- * This _changes_ the previous behaviour, where
1987- * tcp_create_openreq_child always was incrementing the
1988- * equivalent to tcp_prot->socks (inet_sock_nr), so this have
1989- * to be taken into account in all callers. -acme
1990- */
1991- sk_refcnt_debug_inc (newsk );
1992- sk_set_socket (newsk , NULL );
1993- sk_tx_queue_clear (newsk );
1994- RCU_INIT_POINTER (newsk -> sk_wq , NULL );
1967+ newsk -> sk_err = 0 ;
1968+ newsk -> sk_err_soft = 0 ;
1969+ newsk -> sk_priority = 0 ;
1970+ newsk -> sk_incoming_cpu = raw_smp_processor_id ();
1971+ if (likely (newsk -> sk_net_refcnt ))
1972+ sock_inuse_add (sock_net (newsk ), 1 );
19951973
1996- if (newsk -> sk_prot -> sockets_allocated )
1997- sk_sockets_allocated_inc (newsk );
1974+ /* Before updating sk_refcnt, we must commit prior changes to memory
1975+ * (Documentation/RCU/rculist_nulls.rst for details)
1976+ */
1977+ smp_wmb ();
1978+ refcount_set (& newsk -> sk_refcnt , 2 );
19981979
1999- if (sock_needs_netstamp (sk ) &&
2000- newsk -> sk_flags & SK_FLAGS_TIMESTAMP )
2001- net_enable_timestamp ();
2002- }
1980+ /* Increment the counter in the same struct proto as the master
1981+ * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
1982+ * is the same as sk->sk_prot->socks, as this field was copied
1983+ * with memcpy).
1984+ *
1985+ * This _changes_ the previous behaviour, where
1986+ * tcp_create_openreq_child always was incrementing the
1987+ * equivalent to tcp_prot->socks (inet_sock_nr), so this have
1988+ * to be taken into account in all callers. -acme
1989+ */
1990+ sk_refcnt_debug_inc (newsk );
1991+ sk_set_socket (newsk , NULL );
1992+ sk_tx_queue_clear (newsk );
1993+ RCU_INIT_POINTER (newsk -> sk_wq , NULL );
1994+
1995+ if (newsk -> sk_prot -> sockets_allocated )
1996+ sk_sockets_allocated_inc (newsk );
1997+
1998+ if (sock_needs_netstamp (sk ) && newsk -> sk_flags & SK_FLAGS_TIMESTAMP )
1999+ net_enable_timestamp ();
20032000out :
20042001 return newsk ;
20052002}
0 commit comments