Skip to content

Commit 70ee115

Browse files
xemuldavem330
authored andcommitted
[SOCK][NETNS]: Add the percpu prot_inuse counter in the struct net.
Such an accounting would cost us two more dereferences to get the percpu variable from the struct net, so I make sock_prot_inuse_get and _add calls work differently depending on CONFIG_NET_NS - without it old optimized routines are used. The per-cpu counter for init_net is prepared in core_initcall, so that even af_inet, that starts as fs_initcall, will already have the init_net prepared. Signed-off-by: Pavel Emelyanov <xemul@openvz.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent c29a0bc commit 70ee115

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎include/net/netns/core.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
#define __NETNS_CORE_H__
33

44
struct ctl_table_header;
5+
struct prot_inuse;
56

67
struct netns_core {
78
/* core sysctls */
89
struct ctl_table_header *sysctl_hdr;
910

1011
int sysctl_somaxconn;
12+
13+
struct prot_inuse *inuse;
1114
};
1215

1316
#endif

‎net/core/sock.c‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,53 @@ struct prot_inuse {
19471947
};
19481948

19491949
static DECLARE_BITMAP(proto_inuse_idx, PROTO_INUSE_NR);
1950+
1951+
#ifdef CONFIG_NET_NS
1952+
void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
1953+
{
1954+
int cpu = smp_processor_id();
1955+
per_cpu_ptr(net->core.inuse, cpu)->val[prot->inuse_idx] += val;
1956+
}
1957+
EXPORT_SYMBOL_GPL(sock_prot_inuse_add);
1958+
1959+
int sock_prot_inuse_get(struct net *net, struct proto *prot)
1960+
{
1961+
int cpu, idx = prot->inuse_idx;
1962+
int res = 0;
1963+
1964+
for_each_possible_cpu(cpu)
1965+
res += per_cpu_ptr(net->core.inuse, cpu)->val[idx];
1966+
1967+
return res >= 0 ? res : 0;
1968+
}
1969+
EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
1970+
1971+
static int sock_inuse_init_net(struct net *net)
1972+
{
1973+
net->core.inuse = alloc_percpu(struct prot_inuse);
1974+
return net->core.inuse ? 0 : -ENOMEM;
1975+
}
1976+
1977+
static void sock_inuse_exit_net(struct net *net)
1978+
{
1979+
free_percpu(net->core.inuse);
1980+
}
1981+
1982+
static struct pernet_operations net_inuse_ops = {
1983+
.init = sock_inuse_init_net,
1984+
.exit = sock_inuse_exit_net,
1985+
};
1986+
1987+
static __init int net_inuse_init(void)
1988+
{
1989+
if (register_pernet_subsys(&net_inuse_ops))
1990+
panic("Cannot initialize net inuse counters");
1991+
1992+
return 0;
1993+
}
1994+
1995+
core_initcall(net_inuse_init);
1996+
#else
19501997
static DEFINE_PER_CPU(struct prot_inuse, prot_inuse);
19511998

19521999
void sock_prot_inuse_add(struct net *net, struct proto *prot, int val)
@@ -1966,6 +2013,7 @@ int sock_prot_inuse_get(struct net *net, struct proto *prot)
19662013
return res >= 0 ? res : 0;
19672014
}
19682015
EXPORT_SYMBOL_GPL(sock_prot_inuse_get);
2016+
#endif
19692017

19702018
static void assign_proto_idx(struct proto *prot)
19712019
{

0 commit comments

Comments
 (0)