@@ -11,6 +11,7 @@ struct netns_core {
int sysctl_somaxconn;
struct prot_inuse __percpu *prot_inuse;
+ int __percpu *sock_inuse;
};
#endif
@@ -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 = {
@@ -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 */