All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 1/2] sock: Change the netns_core member name.
@ 2017-11-15 15:36 Tonghao Zhang
  2017-11-15 15:36 ` [PATCH v3 2/2] sock: Move the socket inuse to namespace Tonghao Zhang
  0 siblings, 1 reply; 4+ messages in thread
From: Tonghao Zhang @ 2017-11-15 15:36 UTC (permalink / raw)
  To: netdev; +Cc: xiyou.wangcong, Tonghao Zhang, Martin Zhang, Tonghao Zhang

From: Tonghao Zhang <xiangxia.m.yue@gmail.com>

Change the member name will make the code more readable.
This patch will be used in next patch.

Signed-off-by: Martin Zhang <zhangjunweimartin@didichuxing.com>
Signed-off-by: Tonghao Zhang <zhangtonghao@didichuxing.com>
---
 include/net/netns/core.h |  2 +-
 net/core/sock.c          | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/include/net/netns/core.h b/include/net/netns/core.h
index 78eb1ff75475..6490b79881d2 100644
--- a/include/net/netns/core.h
+++ b/include/net/netns/core.h
@@ -10,7 +10,7 @@ struct netns_core {
 
 	int	sysctl_somaxconn;
 
-	struct prot_inuse __percpu *inuse;
+	struct prot_inuse __percpu *prot_inuse;
 };
 
 #endif
diff --git a/net/core/sock.c b/net/core/sock.c
index 03e1b1ec0d5b..b899d8669388 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -3044,7 +3044,7 @@ 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.inuse->val[prot->inuse_idx], val);
+	__this_cpu_add(net->core.prot_inuse->val[prot->inuse_idx], val);
 }
 EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
 
@@ -3054,7 +3054,7 @@ int sock_prot_inuse_get(struct net *net, struct proto *prot)
 	int res = 0;
 
 	for_each_possible_cpu(cpu)
-		res += per_cpu_ptr(net->core.inuse, cpu)->val[idx];
+		res += per_cpu_ptr(net->core.prot_inuse, cpu)->val[idx];
 
 	return res >= 0 ? res : 0;
 }
@@ -3062,13 +3062,13 @@ EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
 
 static int __net_init sock_inuse_init_net(struct net *net)
 {
-	net->core.inuse = alloc_percpu(struct prot_inuse);
-	return net->core.inuse ? 0 : -ENOMEM;
+	net->core.prot_inuse = alloc_percpu(struct prot_inuse);
+	return net->core.prot_inuse ? 0 : -ENOMEM;
 }
 
 static void __net_exit sock_inuse_exit_net(struct net *net)
 {
-	free_percpu(net->core.inuse);
+	free_percpu(net->core.prot_inuse);
 }
 
 static struct pernet_operations net_inuse_ops = {
-- 
2.13.6

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v3 2/2] sock: Move the socket inuse to namespace.
  2017-11-15 15:36 [PATCH v3 1/2] sock: Change the netns_core member name Tonghao Zhang
@ 2017-11-15 15:36 ` Tonghao Zhang
  2017-11-16 20:20   ` Cong Wang
  0 siblings, 1 reply; 4+ messages in thread
From: Tonghao Zhang @ 2017-11-15 15:36 UTC (permalink / raw)
  To: netdev; +Cc: xiyou.wangcong, Tonghao Zhang, Martin Zhang, Tonghao Zhang

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(-)

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 */
 
-- 
2.13.6

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3 2/2] sock: Move the socket inuse to namespace.
  2017-11-15 15:36 ` [PATCH v3 2/2] sock: Move the socket inuse to namespace Tonghao Zhang
@ 2017-11-16 20:20   ` Cong Wang
  2017-11-16 22:06     ` Tonghao Zhang
  0 siblings, 1 reply; 4+ messages in thread
From: Cong Wang @ 2017-11-16 20:20 UTC (permalink / raw)
  To: Tonghao Zhang
  Cc: Linux Kernel Network Developers, Martin Zhang, Tonghao Zhang

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?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3 2/2] sock: Move the socket inuse to namespace.
  2017-11-16 20:20   ` Cong Wang
@ 2017-11-16 22:06     ` Tonghao Zhang
  0 siblings, 0 replies; 4+ messages in thread
From: Tonghao Zhang @ 2017-11-16 22:06 UTC (permalink / raw)
  To: Cong Wang; +Cc: Linux Kernel Network Developers, Martin Zhang, Tonghao Zhang

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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-11-16 22:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-15 15:36 [PATCH v3 1/2] sock: Change the netns_core member name Tonghao Zhang
2017-11-15 15:36 ` [PATCH v3 2/2] sock: Move the socket inuse to namespace Tonghao Zhang
2017-11-16 20:20   ` Cong Wang
2017-11-16 22:06     ` Tonghao Zhang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.