Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

net: reduce indentation level in sk_clone_lock()

Rework initial test to jump over init code
if memory allocation has failed.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Link: https://lore.kernel.org/r/20210127152731.748663-1-eric.dumazet@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Eric Dumazet and committed by
Jakub Kicinski
bbc20b70 d1f3bdd4

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