Skip to content

Commit dec827d

Browse files
xemuldavem330
authored andcommitted
[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 <[email protected]> Acked-by: Paul E. McKenney <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent c93cf61 commit dec827d

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

include/net/net_namespace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct proc_dir_entry;
2020
struct net_device;
2121
struct sock;
2222
struct ctl_table_header;
23+
struct net_generic;
2324

2425
struct net {
2526
atomic_t count; /* To decided when the network
@@ -61,6 +62,7 @@ struct net {
6162
#ifdef CONFIG_NETFILTER
6263
struct netns_xt xt;
6364
#endif
65+
struct net_generic *gen;
6466
};
6567

6668

include/net/netns/generic.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* generic net pointers
3+
*/
4+
5+
#ifndef __NET_GENERIC_H__
6+
#define __NET_GENERIC_H__
7+
8+
#include <linux/rcupdate.h>
9+
10+
/*
11+
* Generic net pointers are to be used by modules to put some private
12+
* stuff on the struct net without explicit struct net modification
13+
*
14+
* The rules are simple:
15+
* 1. register the ops with register_pernet_gen_device to get the id
16+
* of your private pointer;
17+
* 2. call net_assign_generic() to put the private data on the struct
18+
* net (most preferably this should be done in the ->init callback
19+
* of the ops registered);
20+
* 3. do not change this pointer while the net is alive;
21+
* 4. do not try to have any private reference on the net_generic object.
22+
*
23+
* After accomplishing all of the above, the private pointer can be
24+
* accessed with the net_generic() call.
25+
*/
26+
27+
struct net_generic {
28+
unsigned int len;
29+
struct rcu_head rcu;
30+
31+
void *ptr[0];
32+
};
33+
34+
static inline void *net_generic(struct net *net, int id)
35+
{
36+
struct net_generic *ng;
37+
void *ptr;
38+
39+
rcu_read_lock();
40+
ng = rcu_dereference(net->gen);
41+
BUG_ON(id == 0 || id > ng->len);
42+
ptr = ng->ptr[id - 1];
43+
rcu_read_unlock();
44+
45+
return ptr;
46+
}
47+
48+
extern int net_assign_generic(struct net *net, int id, void *data);
49+
#endif

net/core/net_namespace.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/sched.h>
88
#include <linux/idr.h>
99
#include <net/net_namespace.h>
10+
#include <net/netns/generic.h>
1011

1112
/*
1213
* Our network namespace constructor/destructor lists
@@ -21,6 +22,8 @@ LIST_HEAD(net_namespace_list);
2122
struct net init_net;
2223
EXPORT_SYMBOL(init_net);
2324

25+
#define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
26+
2427
/*
2528
* setup_net runs the initializers for the network namespace object.
2629
*/
@@ -29,10 +32,21 @@ static __net_init int setup_net(struct net *net)
2932
/* Must be called with net_mutex held */
3033
struct pernet_operations *ops;
3134
int error;
35+
struct net_generic *ng;
3236

3337
atomic_set(&net->count, 1);
3438
atomic_set(&net->use_count, 0);
3539

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+
3650
error = 0;
3751
list_for_each_entry(ops, &pernet_list, list) {
3852
if (ops->init) {
@@ -54,6 +68,7 @@ static __net_init int setup_net(struct net *net)
5468
}
5569

5670
rcu_barrier();
71+
kfree(ng);
5772
goto out;
5873
}
5974

@@ -386,3 +401,50 @@ void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
386401
mutex_unlock(&net_mutex);
387402
}
388403
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);

0 commit comments

Comments
 (0)