Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: disallow to use net_assign_generic externally

Now there's no need to use this fuction directly because it's handled by
register_pernet_device. So to make this simple and easy to understand,
make this static to do not tempt potentional users.

Signed-off-by: Jiri Pirko <jpirko@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Jiri Pirko and committed by
David S. Miller
05fceb4a 47d54d65

+47 -53
+2 -7
include/net/netns/generic.h
··· 14 14 * The rules are simple: 15 15 * 1. set pernet_operations->id. After register_pernet_device you 16 16 * will have the id of your private pointer. 17 - * 2. Either set pernet_operations->size (to have the code allocate and 18 - * free a private structure pointed to from struct net ) or 19 - * call net_assign_generic() to put the private data on the struct 20 - * net (most preferably this should be done in the ->init callback 21 - * of the ops registered); 17 + * 2. set pernet_operations->size to have the code allocate and free 18 + * a private structure pointed to from struct net. 22 19 * 3. do not change this pointer while the net is alive; 23 20 * 4. do not try to have any private reference on the net_generic object. 24 21 * ··· 43 46 44 47 return ptr; 45 48 } 46 - 47 - extern int net_assign_generic(struct net *net, int id, void *data); 48 49 #endif
+45 -46
net/core/net_namespace.c
··· 27 27 28 28 #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */ 29 29 30 + static void net_generic_release(struct rcu_head *rcu) 31 + { 32 + struct net_generic *ng; 33 + 34 + ng = container_of(rcu, struct net_generic, rcu); 35 + kfree(ng); 36 + } 37 + 38 + static int net_assign_generic(struct net *net, int id, void *data) 39 + { 40 + struct net_generic *ng, *old_ng; 41 + 42 + BUG_ON(!mutex_is_locked(&net_mutex)); 43 + BUG_ON(id == 0); 44 + 45 + ng = old_ng = net->gen; 46 + if (old_ng->len >= id) 47 + goto assign; 48 + 49 + ng = kzalloc(sizeof(struct net_generic) + 50 + id * sizeof(void *), GFP_KERNEL); 51 + if (ng == NULL) 52 + return -ENOMEM; 53 + 54 + /* 55 + * Some synchronisation notes: 56 + * 57 + * The net_generic explores the net->gen array inside rcu 58 + * read section. Besides once set the net->gen->ptr[x] 59 + * pointer never changes (see rules in netns/generic.h). 60 + * 61 + * That said, we simply duplicate this array and schedule 62 + * the old copy for kfree after a grace period. 63 + */ 64 + 65 + ng->len = id; 66 + memcpy(&ng->ptr, &old_ng->ptr, old_ng->len * sizeof(void*)); 67 + 68 + rcu_assign_pointer(net->gen, ng); 69 + call_rcu(&old_ng->rcu, net_generic_release); 70 + assign: 71 + ng->ptr[id - 1] = data; 72 + return 0; 73 + } 74 + 30 75 static int ops_init(const struct pernet_operations *ops, struct net *net) 31 76 { 32 77 int err; ··· 571 526 mutex_unlock(&net_mutex); 572 527 } 573 528 EXPORT_SYMBOL_GPL(unregister_pernet_device); 574 - 575 - static void net_generic_release(struct rcu_head *rcu) 576 - { 577 - struct net_generic *ng; 578 - 579 - ng = container_of(rcu, struct net_generic, rcu); 580 - kfree(ng); 581 - } 582 - 583 - int net_assign_generic(struct net *net, int id, void *data) 584 - { 585 - struct net_generic *ng, *old_ng; 586 - 587 - BUG_ON(!mutex_is_locked(&net_mutex)); 588 - BUG_ON(id == 0); 589 - 590 - ng = old_ng = net->gen; 591 - if (old_ng->len >= id) 592 - goto assign; 593 - 594 - ng = kzalloc(sizeof(struct net_generic) + 595 - id * sizeof(void *), GFP_KERNEL); 596 - if (ng == NULL) 597 - return -ENOMEM; 598 - 599 - /* 600 - * Some synchronisation notes: 601 - * 602 - * The net_generic explores the net->gen array inside rcu 603 - * read section. Besides once set the net->gen->ptr[x] 604 - * pointer never changes (see rules in netns/generic.h). 605 - * 606 - * That said, we simply duplicate this array and schedule 607 - * the old copy for kfree after a grace period. 608 - */ 609 - 610 - ng->len = id; 611 - memcpy(&ng->ptr, &old_ng->ptr, old_ng->len * sizeof(void*)); 612 - 613 - rcu_assign_pointer(net->gen, ng); 614 - call_rcu(&old_ng->rcu, net_generic_release); 615 - assign: 616 - ng->ptr[id - 1] = data; 617 - return 0; 618 - } 619 - EXPORT_SYMBOL_GPL(net_assign_generic);