net: add per_cpu_fw_alloc field to struct proto
authorEric Dumazet <edumazet@google.com>
Thu, 9 Jun 2022 06:34:08 +0000 (23:34 -0700)
committerJakub Kicinski <kuba@kernel.org>
Fri, 10 Jun 2022 23:21:26 +0000 (16:21 -0700)
Each protocol having a ->memory_allocated pointer gets a corresponding
per-cpu reserve, that following patches will use.

Instead of having reserved bytes per socket,
we want to have per-cpu reserves.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Soheil Hassas Yeganeh <soheil@google.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
14 files changed:
include/net/sock.h
include/net/tcp.h
include/net/udp.h
net/core/sock.c
net/decnet/af_decnet.c
net/ipv4/tcp.c
net/ipv4/tcp_ipv4.c
net/ipv4/udp.c
net/ipv4/udplite.c
net/ipv6/tcp_ipv6.c
net/ipv6/udp.c
net/ipv6/udplite.c
net/mptcp/protocol.c
net/sctp/socket.c

index 298897b..825f8cb 100644 (file)
@@ -1254,6 +1254,7 @@ struct proto {
        void                    (*enter_memory_pressure)(struct sock *sk);
        void                    (*leave_memory_pressure)(struct sock *sk);
        atomic_long_t           *memory_allocated;      /* Current allocated memory. */
+       int  __percpu           *per_cpu_fw_alloc;
        struct percpu_counter   *sockets_allocated;     /* Current number of sockets. */
 
        /*
index 1e99f5c..4794cae 100644 (file)
@@ -253,6 +253,8 @@ extern long sysctl_tcp_mem[3];
 #define TCP_RACK_NO_DUPTHRESH    0x4 /* Do not use DUPACK threshold in RACK */
 
 extern atomic_long_t tcp_memory_allocated;
+DECLARE_PER_CPU(int, tcp_memory_per_cpu_fw_alloc);
+
 extern struct percpu_counter tcp_sockets_allocated;
 extern unsigned long tcp_memory_pressure;
 
index b83a003..b60eea2 100644 (file)
@@ -95,6 +95,7 @@ static inline struct udp_hslot *udp_hashslot2(struct udp_table *table,
 extern struct proto udp_prot;
 
 extern atomic_long_t udp_memory_allocated;
+DECLARE_PER_CPU(int, udp_memory_per_cpu_fw_alloc);
 
 /* sysctl variables for udp */
 extern long sysctl_udp_mem[3];
index 063fd76..f96efc9 100644 (file)
@@ -3798,6 +3798,10 @@ int proto_register(struct proto *prot, int alloc_slab)
                pr_err("%s: missing sysctl_mem\n", prot->name);
                return -EINVAL;
        }
+       if (prot->memory_allocated && !prot->per_cpu_fw_alloc) {
+               pr_err("%s: missing per_cpu_fw_alloc\n", prot->name);
+               return -EINVAL;
+       }
        if (alloc_slab) {
                prot->slab = kmem_cache_create_usercopy(prot->name,
                                        prot->obj_size, 0,
index dc92a67..aa4f43f 100644 (file)
@@ -149,6 +149,7 @@ static DEFINE_RWLOCK(dn_hash_lock);
 static struct hlist_head dn_sk_hash[DN_SK_HASH_SIZE];
 static struct hlist_head dn_wild_sk;
 static atomic_long_t decnet_memory_allocated;
+static DEFINE_PER_CPU(int, decnet_memory_per_cpu_fw_alloc);
 
 static int __dn_setsockopt(struct socket *sock, int level, int optname,
                sockptr_t optval, unsigned int optlen, int flags);
@@ -454,7 +455,10 @@ static struct proto dn_proto = {
        .owner                  = THIS_MODULE,
        .enter_memory_pressure  = dn_enter_memory_pressure,
        .memory_pressure        = &dn_memory_pressure,
+
        .memory_allocated       = &decnet_memory_allocated,
+       .per_cpu_fw_alloc       = &decnet_memory_per_cpu_fw_alloc,
+
        .sysctl_mem             = sysctl_decnet_mem,
        .sysctl_wmem            = sysctl_decnet_wmem,
        .sysctl_rmem            = sysctl_decnet_rmem,
index 9e69675..e6bdf8e 100644 (file)
@@ -294,6 +294,8 @@ EXPORT_SYMBOL(sysctl_tcp_mem);
 
 atomic_long_t tcp_memory_allocated ____cacheline_aligned_in_smp;       /* Current allocated memory. */
 EXPORT_SYMBOL(tcp_memory_allocated);
+DEFINE_PER_CPU(int, tcp_memory_per_cpu_fw_alloc);
+EXPORT_PER_CPU_SYMBOL_GPL(tcp_memory_per_cpu_fw_alloc);
 
 #if IS_ENABLED(CONFIG_SMC)
 DEFINE_STATIC_KEY_FALSE(tcp_have_smc);
index fe8f23b..fda811a 100644 (file)
@@ -3045,7 +3045,10 @@ struct proto tcp_prot = {
        .stream_memory_free     = tcp_stream_memory_free,
        .sockets_allocated      = &tcp_sockets_allocated,
        .orphan_count           = &tcp_orphan_count,
+
        .memory_allocated       = &tcp_memory_allocated,
+       .per_cpu_fw_alloc       = &tcp_memory_per_cpu_fw_alloc,
+
        .memory_pressure        = &tcp_memory_pressure,
        .sysctl_mem             = sysctl_tcp_mem,
        .sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_tcp_wmem),
index bbc9970..6172b47 100644 (file)
@@ -125,6 +125,8 @@ EXPORT_SYMBOL(sysctl_udp_mem);
 
 atomic_long_t udp_memory_allocated ____cacheline_aligned_in_smp;
 EXPORT_SYMBOL(udp_memory_allocated);
+DEFINE_PER_CPU(int, udp_memory_per_cpu_fw_alloc);
+EXPORT_PER_CPU_SYMBOL_GPL(udp_memory_per_cpu_fw_alloc);
 
 #define MAX_UDP_PORTS 65536
 #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
@@ -2946,6 +2948,8 @@ struct proto udp_prot = {
        .psock_update_sk_prot   = udp_bpf_update_proto,
 #endif
        .memory_allocated       = &udp_memory_allocated,
+       .per_cpu_fw_alloc       = &udp_memory_per_cpu_fw_alloc,
+
        .sysctl_mem             = sysctl_udp_mem,
        .sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
        .sysctl_rmem_offset     = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
index cd1cd68..6e08a76 100644 (file)
@@ -51,7 +51,10 @@ struct proto         udplite_prot = {
        .unhash            = udp_lib_unhash,
        .rehash            = udp_v4_rehash,
        .get_port          = udp_v4_get_port,
+
        .memory_allocated  = &udp_memory_allocated,
+       .per_cpu_fw_alloc  = &udp_memory_per_cpu_fw_alloc,
+
        .sysctl_mem        = sysctl_udp_mem,
        .obj_size          = sizeof(struct udp_sock),
        .h.udp_table       = &udplite_table,
index f37dd4a..c72448b 100644 (file)
@@ -2159,7 +2159,10 @@ struct proto tcpv6_prot = {
        .leave_memory_pressure  = tcp_leave_memory_pressure,
        .stream_memory_free     = tcp_stream_memory_free,
        .sockets_allocated      = &tcp_sockets_allocated,
+
        .memory_allocated       = &tcp_memory_allocated,
+       .per_cpu_fw_alloc       = &tcp_memory_per_cpu_fw_alloc,
+
        .memory_pressure        = &tcp_memory_pressure,
        .orphan_count           = &tcp_orphan_count,
        .sysctl_mem             = sysctl_tcp_mem,
index 55afd7f..be074f0 100644 (file)
@@ -1740,7 +1740,10 @@ struct proto udpv6_prot = {
 #ifdef CONFIG_BPF_SYSCALL
        .psock_update_sk_prot   = udp_bpf_update_proto,
 #endif
+
        .memory_allocated       = &udp_memory_allocated,
+       .per_cpu_fw_alloc       = &udp_memory_per_cpu_fw_alloc,
+
        .sysctl_mem             = sysctl_udp_mem,
        .sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
        .sysctl_rmem_offset     = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
index fbb700d..b707258 100644 (file)
@@ -48,7 +48,10 @@ struct proto udplitev6_prot = {
        .unhash            = udp_lib_unhash,
        .rehash            = udp_v6_rehash,
        .get_port          = udp_v6_get_port,
+
        .memory_allocated  = &udp_memory_allocated,
+       .per_cpu_fw_alloc  = &udp_memory_per_cpu_fw_alloc,
+
        .sysctl_mem        = sysctl_udp_mem,
        .obj_size          = sizeof(struct udp6_sock),
        .h.udp_table       = &udplite_table,
index 080a630..9563124 100644 (file)
@@ -3437,7 +3437,10 @@ static struct proto mptcp_prot = {
        .get_port       = mptcp_get_port,
        .forward_alloc_get      = mptcp_forward_alloc_get,
        .sockets_allocated      = &mptcp_sockets_allocated,
+
        .memory_allocated       = &tcp_memory_allocated,
+       .per_cpu_fw_alloc       = &tcp_memory_per_cpu_fw_alloc,
+
        .memory_pressure        = &tcp_memory_pressure,
        .sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_tcp_wmem),
        .sysctl_rmem_offset     = offsetof(struct net, ipv4.sysctl_tcp_rmem),
index 6d37d2d..05174ac 100644 (file)
@@ -93,6 +93,7 @@ static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
 
 static unsigned long sctp_memory_pressure;
 static atomic_long_t sctp_memory_allocated;
+static DEFINE_PER_CPU(int, sctp_memory_per_cpu_fw_alloc);
 struct percpu_counter sctp_sockets_allocated;
 
 static void sctp_enter_memory_pressure(struct sock *sk)
@@ -9657,7 +9658,10 @@ struct proto sctp_prot = {
        .sysctl_wmem =  sysctl_sctp_wmem,
        .memory_pressure = &sctp_memory_pressure,
        .enter_memory_pressure = sctp_enter_memory_pressure,
+
        .memory_allocated = &sctp_memory_allocated,
+       .per_cpu_fw_alloc = &sctp_memory_per_cpu_fw_alloc,
+
        .sockets_allocated = &sctp_sockets_allocated,
 };
 
@@ -9700,7 +9704,10 @@ struct proto sctpv6_prot = {
        .sysctl_wmem    = sysctl_sctp_wmem,
        .memory_pressure = &sctp_memory_pressure,
        .enter_memory_pressure = sctp_enter_memory_pressure,
+
        .memory_allocated = &sctp_memory_allocated,
+       .per_cpu_fw_alloc = &sctp_memory_per_cpu_fw_alloc,
+
        .sockets_allocated = &sctp_sockets_allocated,
 };
 #endif /* IS_ENABLED(CONFIG_IPV6) */