diff options
author | Pavel Emelyanov <xemul@openvz.org> | 2008-04-15 03:36:08 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2008-04-15 03:36:08 -0400 |
commit | dec827d174d7f76c457238800183ca864a639365 (patch) | |
tree | ca7c04e35eda9956afef94678a2a863c3e75e2ed /net/core | |
parent | c93cf61fd1d5378134f9b06703f7078067542e00 (diff) |
[NETNS]: The generic per-net pointers.
Add the elastic array of void * pointer to the struct net.
The access rules are simple:
1. register the ops with register_pernet_gen_device to get
the id of your private pointer
2. call net_assign_generic() to put the private data on the
struct net (most preferably this should be done in the
->init callback of the ops registered)
3. do not store any private reference on the net_generic array;
4. do not change this pointer while the net is alive;
5. use the net_generic() to get the pointer.
When adding a new pointer, I copy the old array, replace it
with a new one and schedule the old for kfree after an RCU
grace period.
Since the net_generic explores the net->gen array inside rcu
read section and once set the net->gen->ptr[x] pointer never
changes, this grants us a safe access to generic pointers.
Quoting Paul: "... RCU is protecting -only- the net_generic
structure that net_generic() is traversing, and the [pointer]
returned by net_generic() is protected by a reference counter
in the upper-level struct net."
Signed-off-by: Pavel Emelyanov <xemul@openvz.org>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/core')
-rw-r--r-- | net/core/net_namespace.c | 62 |
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); | |||
21 | struct net init_net; | 22 | struct net init_net; |
22 | EXPORT_SYMBOL(init_net); | 23 | EXPORT_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 | } |
388 | EXPORT_SYMBOL_GPL(unregister_pernet_gen_device); | 403 | EXPORT_SYMBOL_GPL(unregister_pernet_gen_device); |
404 | |||
405 | static 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 | |||
413 | int 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); | ||
446 | assign: | ||
447 | ng->ptr[id - 1] = data; | ||
448 | return 0; | ||
449 | } | ||
450 | EXPORT_SYMBOL_GPL(net_assign_generic); | ||