aboutsummaryrefslogtreecommitdiffstats
path: root/net/core
diff options
context:
space:
mode:
Diffstat (limited to 'net/core')
-rw-r--r--net/core/net_namespace.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
index 2197d51aef3b..763674e1e593 100644
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -7,6 +7,7 @@
7#include <linux/sched.h> 7#include <linux/sched.h>
8#include <linux/idr.h> 8#include <linux/idr.h>
9#include <net/net_namespace.h> 9#include <net/net_namespace.h>
10#include <net/netns/generic.h>
10 11
11/* 12/*
12 * Our network namespace constructor/destructor lists 13 * Our network namespace constructor/destructor lists
@@ -21,6 +22,8 @@ LIST_HEAD(net_namespace_list);
21struct net init_net; 22struct net init_net;
22EXPORT_SYMBOL(init_net); 23EXPORT_SYMBOL(init_net);
23 24
25#define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
26
24/* 27/*
25 * setup_net runs the initializers for the network namespace object. 28 * setup_net runs the initializers for the network namespace object.
26 */ 29 */
@@ -29,10 +32,21 @@ static __net_init int setup_net(struct net *net)
29 /* Must be called with net_mutex held */ 32 /* Must be called with net_mutex held */
30 struct pernet_operations *ops; 33 struct pernet_operations *ops;
31 int error; 34 int error;
35 struct net_generic *ng;
32 36
33 atomic_set(&net->count, 1); 37 atomic_set(&net->count, 1);
34 atomic_set(&net->use_count, 0); 38 atomic_set(&net->use_count, 0);
35 39
40 error = -ENOMEM;
41 ng = kzalloc(sizeof(struct net_generic) +
42 INITIAL_NET_GEN_PTRS * sizeof(void *), GFP_KERNEL);
43 if (ng == NULL)
44 goto out;
45
46 ng->len = INITIAL_NET_GEN_PTRS;
47 INIT_RCU_HEAD(&ng->rcu);
48 rcu_assign_pointer(net->gen, ng);
49
36 error = 0; 50 error = 0;
37 list_for_each_entry(ops, &pernet_list, list) { 51 list_for_each_entry(ops, &pernet_list, list) {
38 if (ops->init) { 52 if (ops->init) {
@@ -54,6 +68,7 @@ out_undo:
54 } 68 }
55 69
56 rcu_barrier(); 70 rcu_barrier();
71 kfree(ng);
57 goto out; 72 goto out;
58} 73}
59 74
@@ -386,3 +401,50 @@ void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
386 mutex_unlock(&net_mutex); 401 mutex_unlock(&net_mutex);
387} 402}
388EXPORT_SYMBOL_GPL(unregister_pernet_gen_device); 403EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);
404
405static void net_generic_release(struct rcu_head *rcu)
406{
407 struct net_generic *ng;
408
409 ng = container_of(rcu, struct net_generic, rcu);
410 kfree(ng);
411}
412
413int net_assign_generic(struct net *net, int id, void *data)
414{
415 struct net_generic *ng, *old_ng;
416
417 BUG_ON(!mutex_is_locked(&net_mutex));
418 BUG_ON(id == 0);
419
420 ng = old_ng = net->gen;
421 if (old_ng->len >= id)
422 goto assign;
423
424 ng = kzalloc(sizeof(struct net_generic) +
425 id * sizeof(void *), GFP_KERNEL);
426 if (ng == NULL)
427 return -ENOMEM;
428
429 /*
430 * Some synchronisation notes:
431 *
432 * The net_generic explores the net->gen array inside rcu
433 * read section. Besides once set the net->gen->ptr[x]
434 * pointer never changes (see rules in netns/generic.h).
435 *
436 * That said, we simply duplicate this array and schedule
437 * the old copy for kfree after a grace period.
438 */
439
440 ng->len = id;
441 INIT_RCU_HEAD(&ng->rcu);
442 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len);
443
444 rcu_assign_pointer(net->gen, ng);
445 call_rcu(&old_ng->rcu, net_generic_release);
446assign:
447 ng->ptr[id - 1] = data;
448 return 0;
449}
450EXPORT_SYMBOL_GPL(net_assign_generic);