net: prot_inuse and sock_inuse cleanups
Subsystems (11)
cover net: prot_inuse and sock_inuse cleanups
From: Eric Dumazet <[email protected]>
Small series cleaning and optimizing sock_prot_inuse_add() and sock_inuse_add().
Eric Dumazet (4):
net: inline sock_prot_inuse_add()
net: make sock_inuse_add() available
net: merge net->core.prot_inuse and net->core.sock_inuse
net: drop nopreempt requirement on sock_prot_inuse_add()
include/net/netns/core.h | 1 - include/net/sock.h | 27 +++++++++++++++++++++++---- net/core/sock.c | 33 +-------------------------------- net/ieee802154/socket.c | 4 ++-- net/ipv4/raw.c | 2 +- net/ipv6/ipv6_sockglue.c | 8 ++++---- net/mptcp/subflow.c | 4 +--- net/netlink/af_netlink.c | 4 ---- net/packet/af_packet.c | 4 ---- net/sctp/socket.c | 5 ----- net/smc/af_smc.c | 2 +- net/unix/af_unix.c | 4 ---- net/xdp/xsk.c | 4 ---- 13 files changed, 33 insertions(+), 69 deletions(-)
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
PATCH 1/4 net: inline sock_prot_inuse_add()
From: Eric Dumazet <[email protected]>
sock_prot_inuse_add() is very small, we can inline it.
--- include/net/sock.h | 14 +++++++++++--- net/core/sock.c | 11 ----------- 2 files changed, 11 insertions(+), 14 deletions(-)
include/net/sock.h +11 -3
@@ -1419,13 +1419,21 @@ proto_memory_pressure(struct proto *prot)#ifdef CONFIG_PROC_FS +#define PROTO_INUSE_NR 64/* should be enough for the first time */ +struct prot_inuse { +int val[PROTO_INUSE_NR ]; +};/* Called with local bh disabled */ -void sock_prot_inuse_add (struct net *net,struct proto *prot,int inc); +static inline void sock_prot_inuse_add (const struct net *net, +const struct proto *prot,int val) +{ +__this_cpu_add (net->core.prot_inuse->val[prot->inuse_idx], val); +}int sock_prot_inuse_get (struct net *net,struct proto *proto);int sock_inuse_get (struct net *net);#else -static inline void sock_prot_inuse_add (struct net *net,struct proto *prot, -int inc) +static inline void sock_prot_inuse_add (const struct net *net, +const struct proto *prot,int val) { }#endif
net/core/sock.c +0 -11
@@ -3532,19 +3532,8 @@ void sk_get_meminfo(const struct sock *sk, u32 *mem) }#ifdef CONFIG_PROC_FS -#define PROTO_INUSE_NR 64/* should be enough for the first time */ -struct prot_inuse { -int val[PROTO_INUSE_NR ]; -}; - static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR); -void sock_prot_inuse_add (struct net *net,struct proto *prot,int val) -{ -__this_cpu_add (net->core.prot_inuse->val[prot->inuse_idx], val); -} -EXPORT_SYMBOL_GPL (sock_prot_inuse_add); -int sock_prot_inuse_get (struct net *net,struct proto *prot) {int cpu, idx = prot->inuse_idx;
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
PATCH 2/4 net: make sock_inuse_add() available
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
MPTCP hard codes it, let us instead provide this helper.
--- include/net/sock.h | 10 ++++++++++ net/core/sock.c | 10 ---------- net/mptcp/subflow.c | 4 +--- 3 files changed, 11 insertions(+), 13 deletions(-)
include/net/sock.h +10 -0
@@ -1429,6 +1429,12 @@ static inline void sock_prot_inuse_add(const struct net *net, {__this_cpu_add (net->core.prot_inuse->val[prot->inuse_idx], val); } + +static inline void sock_inuse_add (const struct net *net,int val) +{ +this_cpu_add (*net->core.sock_inuse, val); +} +int sock_prot_inuse_get (struct net *net,struct proto *proto);int sock_inuse_get (struct net *net);#else
@@ -1436,6 +1442,10 @@ static inline void sock_prot_inuse_add(const struct net *net, const struct proto *prot, int val) { } + +static inline void sock_inuse_add (const struct net *net,int val) +{ +}#endif
net/core/sock.c +0 -10
@@ -144,8 +144,6 @@static DEFINE_MUTEX (proto_list_mutex);static LIST_HEAD (proto_list); -static void sock_inuse_add (struct net *net,int val); -/** * sk_ns_capable - General socket capability test * @sk: Socket to use a capability on or through
@@ -3546,11 +3544,6 @@ int sock_prot_inuse_get(struct net *net, struct proto *prot) }EXPORT_SYMBOL_GPL (sock_prot_inuse_get); -static void sock_inuse_add (struct net *net,int val) -{ -this_cpu_add (*net->core.sock_inuse, val); -} -int sock_inuse_get (struct net *net) {int cpu, res =0 ;
@@ -3629,9 +3622,6 @@ static inline void release_proto_idx(struct proto *prot) { } -static void sock_inuse_add (struct net *net,int val) -{ -}#endif static void tw_prot_cleanup (struct timewait_sock_ops *twsk_prot)
net/mptcp/subflow.c +1 -3
@@ -1534,9 +1534,7 @@ int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock)*/ sf->sk->sk_net_refcnt =1 ;get_net (net); -#ifdef CONFIG_PROC_FS -this_cpu_add (*net->core.sock_inuse,1 ); -#endif +sock_inuse_add (net,1 ); err =tcp_set_ulp (sf->sk,"mptcp" );release_sock (sf->sk);
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
PATCH 3/4 net: merge net->core.prot_inuse and net->core.sock_inuse
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
net->core.sock_inuse is a per cpu variable (int), while net->core.prot_inuse is another per cpu variable of 64 integers.
per cpu allocator tend to place them in very different places.
Grouping them together makes sense, since it makes updates potentially faster, if hitting the same cache line.
--- include/net/netns/core.h | 1 - include/net/sock.h | 3 ++- net/core/sock.c | 12 +----------- 3 files changed, 3 insertions(+), 13 deletions(-)
include/net/netns/core.h +0 -1
@@ -12,7 +12,6 @@ struct netns_core {int sysctl_somaxconn;#ifdef CONFIG_PROC_FS -int __percpu *sock_inuse;struct prot_inuse __percpu *prot_inuse;#endif };
include/net/sock.h +2 -1
@@ -1421,6 +1421,7 @@ proto_memory_pressure(struct proto *prot)#ifdef CONFIG_PROC_FS #define PROTO_INUSE_NR 64/* should be enough for the first time */ struct prot_inuse { +int all;int val[PROTO_INUSE_NR ]; };/* Called with local bh disabled */
@@ -1432,7 +1433,7 @@ static inline void sock_prot_inuse_add(const struct net *net,static inline void sock_inuse_add (const struct net *net,int val) { -this_cpu_add (*net->core.sock_inuse, val); +this_cpu_add (net->core.prot_inuse->all, val); }int sock_prot_inuse_get (struct net *net,struct proto *proto);
net/core/sock.c +1 -11
@@ -3549,7 +3549,7 @@ int sock_inuse_get(struct net *net)int cpu, res =0 ;for_each_possible_cpu (cpu) - res += *per_cpu_ptr (net->core.sock_inuse, cpu); + res +=per_cpu_ptr (net->core.prot_inuse, cpu)->all;return res; }
@@ -3561,22 +3561,12 @@ static int __net_init sock_inuse_init_net(struct net *net) net->core.prot_inuse = alloc_percpu(struct prot_inuse);if (net->core.prot_inuse ==NULL )return -ENOMEM ; - - net->core.sock_inuse =alloc_percpu (int); -if (net->core.sock_inuse ==NULL ) - goto out; -return 0 ; - -out: -free_percpu (net->core.prot_inuse); -return -ENOMEM ; } static void __net_exit sock_inuse_exit_net(struct net *net) {free_percpu (net->core.prot_inuse); -free_percpu (net->core.sock_inuse); }static struct pernet_operations net_inuse_ops = {
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
PATCH 4/4 net: drop nopreempt requirement on sock_prot_inuse_add()
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
This is distracting really, let's make this simpler, because many callers had to take care of this by themselves, even if on x86 this adds more code than really needed.
--- include/net/sock.h | 4 ++-- net/ieee802154/socket.c | 4 ++-- net/ipv4/raw.c | 2 +- net/ipv6/ipv6_sockglue.c | 8 ++++---- net/netlink/af_netlink.c | 4 ---- net/packet/af_packet.c | 4 ---- net/sctp/socket.c | 5 ----- net/smc/af_smc.c | 2 +- net/unix/af_unix.c | 4 ---- net/xdp/xsk.c | 4 ---- 10 files changed, 10 insertions(+), 31 deletions(-)
include/net/sock.h +2 -2
@@ -1424,11 +1424,11 @@ struct prot_inuse {int all;int val[PROTO_INUSE_NR ]; }; -/* Called with local bh disabled */ +static inline void sock_prot_inuse_add (const struct net *net,const struct proto *prot,int val) { -__this_cpu_add (net->core.prot_inuse->val[prot->inuse_idx], val); +this_cpu_add (net->core.prot_inuse->val[prot->inuse_idx], val); }static inline void sock_inuse_add (const struct net *net,int val)
net/ieee802154/socket.c +2 -2
@@ -174,8 +174,8 @@ static int raw_hash(struct sock *sk) {write_lock_bh (&raw_lock);sk_add_node (sk, &raw_head); -sock_prot_inuse_add (sock_net (sk), sk->sk_prot,1 );write_unlock_bh (&raw_lock); +sock_prot_inuse_add (sock_net (sk), sk->sk_prot,1 );return 0 ; }
@@ -453,8 +453,8 @@ static int dgram_hash(struct sock *sk) {write_lock_bh (&dgram_lock);sk_add_node (sk, &dgram_head); -sock_prot_inuse_add (sock_net (sk), sk->sk_prot,1 );write_unlock_bh (&dgram_lock); +sock_prot_inuse_add (sock_net (sk), sk->sk_prot,1 );return 0 ; }
net/ipv4/raw.c +1 -1
@@ -99,8 +99,8 @@ int raw_hash_sk(struct sock *sk)write_lock_bh (&h->lock);sk_add_node (sk, head); +write_unlock_bh (&h->lock);sock_prot_inuse_add (sock_net (sk), sk->sk_prot,1 ); -write_unlock_bh (&h->lock);return 0 ; }
net/ipv6/ipv6_sockglue.c +4 -4
@@ -471,10 +471,10 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,if (sk->sk_protocol ==IPPROTO_TCP ) {struct inet_connection_sock *icsk =inet_csk (sk); -local_bh_disable (); +sock_prot_inuse_add (net, sk->sk_prot,-1 );sock_prot_inuse_add (net, &tcp_prot,1 ); -local_bh_enable (); + sk->sk_prot = &tcp_prot; icsk->icsk_af_ops = &ipv4_specific; sk->sk_socket->ops = &inet_stream_ops;
@@ -485,10 +485,10 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,if (sk->sk_protocol ==IPPROTO_UDPLITE ) prot = &udplite_prot; -local_bh_disable (); +sock_prot_inuse_add (net, sk->sk_prot,-1 );sock_prot_inuse_add (net, prot,1 ); -local_bh_enable (); + sk->sk_prot = prot; sk->sk_socket->ops = &inet_dgram_ops; sk->sk_family =PF_INET ;
net/netlink/af_netlink.c +0 -4
@@ -707,9 +707,7 @@ static int netlink_create(struct net *net, struct socket *sock, int protocol,if (err <0 ) goto out_module; -local_bh_disable ();sock_prot_inuse_add (net, &netlink_proto,1 ); -local_bh_enable (); nlk =nlk_sk (sock->sk); nlk->module = module;
@@ -809,9 +807,7 @@ static int netlink_release(struct socket *sock)netlink_table_ungrab (); } -local_bh_disable ();sock_prot_inuse_add (sock_net (sk), &netlink_proto,-1 ); -local_bh_enable ();call_rcu (&nlk->rcu, deferred_put_nlk_sk);return 0 ; }
net/packet/af_packet.c +0 -4
@@ -3102,9 +3102,7 @@ static int packet_release(struct socket *sock)sk_del_node_init_rcu (sk);mutex_unlock (&net->packet.sklist_lock); -preempt_disable ();sock_prot_inuse_add (net, sk->sk_prot,-1 ); -preempt_enable ();spin_lock (&po->bind_lock);unregister_prot_hook (sk, false);
@@ -3368,9 +3366,7 @@ static int packet_create(struct net *net, struct socket *sock, int protocol,sk_add_node_tail_rcu (sk, &net->packet.sklist);mutex_unlock (&net->packet.sklist_lock); -preempt_disable ();sock_prot_inuse_add (net, &packet_proto,1 ); -preempt_enable ();return 0 ; out2:
net/sctp/socket.c +0 -5
@@ -5068,12 +5068,9 @@ static int sctp_init_sock(struct sock *sk)SCTP_DBG_OBJCNT_INC (sock); -local_bh_disable ();sk_sockets_allocated_inc (sk);sock_prot_inuse_add (net, sk->sk_prot,1 ); -local_bh_enable (); -return 0 ; }
@@ -5099,10 +5096,8 @@ static void sctp_destroy_sock(struct sock *sk)list_del (&sp->auto_asconf_list); }sctp_endpoint_free (sp->ep); -local_bh_disable ();sk_sockets_allocated_dec (sk);sock_prot_inuse_add (sock_net (sk), sk->sk_prot,-1 ); -local_bh_enable (); }/* Triggered when there are no references on the socket anymore */
net/smc/af_smc.c +1 -1
@@ -89,8 +89,8 @@ int smc_hash_sk(struct sock *sk)write_lock_bh (&h->lock);sk_add_node (sk, head); +write_unlock_bh (&h->lock);sock_prot_inuse_add (sock_net (sk), sk->sk_prot,1 ); -write_unlock_bh (&h->lock);return 0 ; }
net/unix/af_unix.c +0 -4
@@ -522,9 +522,7 @@ static void unix_sock_destructor(struct sock *sk)unix_release_addr (u->addr);atomic_long_dec (&unix_nr_socks); -local_bh_disable ();sock_prot_inuse_add (sock_net (sk), sk->sk_prot,-1 ); -local_bh_enable ();#ifdef UNIX_REFCNT_DEBUG pr_debug ("UNIX %p is destroyed, %ld are still alive.\n" , sk,atomic_long_read (&unix_nr_socks));
@@ -889,9 +887,7 @@ static struct sock *unix_create1(struct net *net, struct socket *sock, int kern,memset (&u->scm_stat,0 ,sizeof (struct scm_stat ));unix_insert_socket (unix_sockets_unbound (sk), sk); -local_bh_disable ();sock_prot_inuse_add (sock_net (sk), sk->sk_prot,1 ); -local_bh_enable ();return sk;
net/xdp/xsk.c +0 -4
@@ -794,9 +794,7 @@ static int xsk_release(struct socket *sock)sk_del_node_init_rcu (sk);mutex_unlock (&net->xdp.lock); -local_bh_disable ();sock_prot_inuse_add (net, sk->sk_prot,-1 ); -local_bh_enable ();xsk_delete_from_maps (xs);mutex_lock (&xs->mutex);
@@ -1396,9 +1394,7 @@ static int xsk_create(struct net *net, struct socket *sock, int protocol,sk_add_node_rcu (sk, &net->xdp.list);mutex_unlock (&net->xdp.lock); -local_bh_disable ();sock_prot_inuse_add (net, &xsk_proto,1 ); -local_bh_enable ();return 0 ; }
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog