diff mbox series

[v3,2/2] sock: Move the socket inuse to namespace.

Message ID 1510760215-2403-2-git-send-email-zhangtonghao@didichuxing.com
State Changes Requested, archived
Delegated to: David Miller
Headers show
Series [v3,1/2] sock: Change the netns_core member name. | expand

Commit Message

Tonghao Zhang Nov. 15, 2017, 3:36 p.m. UTC
From: Tonghao Zhang <xiangxia.m.yue@gmail.com>

This patch add a member in struct netns_core. and this is
a counter for socket_inuse in the _net_ namespace. The patch
will add/sub counter in the sk_alloc or sk_free. Because socket and
sock is in pair. It's a easy way to maintain the code. and help
developer to review. More important, it avoids holding the _net_
namespace again.

Signed-off-by: Martin Zhang <zhangjunweimartin@didichuxing.com>
Signed-off-by: Tonghao Zhang <zhangtonghao@didichuxing.com>
---
 include/net/netns/core.h |  1 +
 net/core/sock.c          | 26 +++++++++++++++++++++++++-
 net/socket.c             | 17 +++++++++--------
 3 files changed, 35 insertions(+), 9 deletions(-)

Comments

Cong Wang Nov. 16, 2017, 8:20 p.m. UTC | #1
On Wed, Nov 15, 2017 at 7:36 AM, Tonghao Zhang <xiangxia.m.yue@gmail.com> wrote:
> diff --git a/net/core/sock.c b/net/core/sock.c
> index b899d8669388..f01ed0b41bde 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c
> @@ -145,6 +145,10 @@
>  static DEFINE_MUTEX(proto_list_mutex);
>  static LIST_HEAD(proto_list);
>
> +#ifdef CONFIG_PROC_FS
> +static void sock_inuse_add(struct net *net, int val);
> +#endif
> +
>  /**
>   * sk_ns_capable - General socket capability test
>   * @sk: Socket to use a capability on or through
> @@ -1536,6 +1540,7 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
>                 if (likely(sk->sk_net_refcnt))
>                         get_net(net);
>                 sock_net_set(sk, net);
> +               sock_inuse_add(net, 1);

You don't need to define a nop for sock_inuse_add() in
!CONFIG_PROC_FS case?
Tonghao Zhang Nov. 16, 2017, 10:06 p.m. UTC | #2
On Fri, Nov 17, 2017 at 4:20 AM, Cong Wang <xiyou.wangcong@gmail.com> wrote:
> On Wed, Nov 15, 2017 at 7:36 AM, Tonghao Zhang <xiangxia.m.yue@gmail.com> wrote:
>> diff --git a/net/core/sock.c b/net/core/sock.c
>> index b899d8669388..f01ed0b41bde 100644
>> --- a/net/core/sock.c
>> +++ b/net/core/sock.c
>> @@ -145,6 +145,10 @@
>>  static DEFINE_MUTEX(proto_list_mutex);
>>  static LIST_HEAD(proto_list);
>>
>> +#ifdef CONFIG_PROC_FS
>> +static void sock_inuse_add(struct net *net, int val);
>> +#endif
>> +
>>  /**
>>   * sk_ns_capable - General socket capability test
>>   * @sk: Socket to use a capability on or through
>> @@ -1536,6 +1540,7 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
>>                 if (likely(sk->sk_net_refcnt))
>>                         get_net(net);
>>                 sock_net_set(sk, net);
>> +               sock_inuse_add(net, 1);
>
> You don't need to define a nop for sock_inuse_add() in
> !CONFIG_PROC_FS case?
Yes, we should. But we cant config the CONFIG_PROC_FS in 'make menuconfig'
Then !CONFIG_PROC_FS is a rare event.  so I dont check it there, such
as  other counter sock_prot_inuse_add.
A patch will be sent for fixing  !CONFIG_PROC_FS. and v4 will be sent
too. Thanks a lot, cong.
diff mbox series

Patch

diff --git a/include/net/netns/core.h b/include/net/netns/core.h
index 6490b79881d2..1de41f3a72a1 100644
--- a/include/net/netns/core.h
+++ b/include/net/netns/core.h
@@ -11,6 +11,7 @@  struct netns_core {
 	int	sysctl_somaxconn;
 
 	struct prot_inuse __percpu *prot_inuse;
+	int __percpu *sock_inuse;
 };
 
 #endif
diff --git a/net/core/sock.c b/net/core/sock.c
index b899d8669388..f01ed0b41bde 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -145,6 +145,10 @@ 
 static DEFINE_MUTEX(proto_list_mutex);
 static LIST_HEAD(proto_list);
 
+#ifdef CONFIG_PROC_FS
+static void sock_inuse_add(struct net *net, int val);
+#endif
+
 /**
  * sk_ns_capable - General socket capability test
  * @sk: Socket to use a capability on or through
@@ -1536,6 +1540,7 @@  struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
 		if (likely(sk->sk_net_refcnt))
 			get_net(net);
 		sock_net_set(sk, net);
+		sock_inuse_add(net, 1);
 		refcount_set(&sk->sk_wmem_alloc, 1);
 
 		mem_cgroup_sk_alloc(sk);
@@ -1597,6 +1602,8 @@  void sk_destruct(struct sock *sk)
 
 static void __sk_free(struct sock *sk)
 {
+	sock_inuse_add(sock_net(sk), -1);
+
 	if (unlikely(sock_diag_has_destroy_listeners(sk) && sk->sk_net_refcnt))
 		sock_diag_broadcast_destroy(sk);
 	else
@@ -1665,6 +1672,7 @@  struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
 		newsk->sk_backlog.head	= newsk->sk_backlog.tail = NULL;
 		newsk->sk_backlog.len = 0;
 
+		sock_inuse_add(sock_net(newsk), 1);
 		atomic_set(&newsk->sk_rmem_alloc, 0);
 		/*
 		 * sk_wmem_alloc set to one (see sk_free() and sock_wfree())
@@ -3048,6 +3056,11 @@  void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
 }
 EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
 
+static void sock_inuse_add(struct net *net, int val)
+{
+	__this_cpu_add(*net->core.sock_inuse, val);
+}
+
 int sock_prot_inuse_get(struct net *net, struct proto *prot)
 {
 	int cpu, idx = prot->inuse_idx;
@@ -3063,12 +3076,23 @@  EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
 static int __net_init sock_inuse_init_net(struct net *net)
 {
 	net->core.prot_inuse = alloc_percpu(struct prot_inuse);
-	return net->core.prot_inuse ? 0 : -ENOMEM;
+	if (!net->core.prot_inuse)
+		return -ENOMEM;
+
+	net->core.sock_inuse = alloc_percpu(int);
+	if (!net->core.sock_inuse)
+		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 = {
diff --git a/net/socket.c b/net/socket.c
index c729625eb5d3..90767eb1731d 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -2643,19 +2643,20 @@  static int __init sock_init(void)
 core_initcall(sock_init);	/* early initcall */
 
 #ifdef CONFIG_PROC_FS
-void socket_seq_show(struct seq_file *seq)
+static int socket_inuse_get(struct net *net)
 {
-	int cpu;
-	int counter = 0;
+	int cpu, res = 0;
 
 	for_each_possible_cpu(cpu)
-	    counter += per_cpu(sockets_in_use, cpu);
+		res += *per_cpu_ptr(net->core.sock_inuse, cpu);
 
-	/* It can be negative, by the way. 8) */
-	if (counter < 0)
-		counter = 0;
+	return res >= 0 ? res : 0;
+}
 
-	seq_printf(seq, "sockets: used %d\n", counter);
+void socket_seq_show(struct seq_file *seq)
+{
+	seq_printf(seq, "sockets: used %d\n",
+		   socket_inuse_get(seq->private));
 }
 #endif				/* CONFIG_PROC_FS */