Commit c93cf61f authored by Pavel Emelyanov's avatar Pavel Emelyanov Committed by David S. Miller

[NETNS]: The net-subsys IDs generator.

To make some per-net generic pointers, we need some way to address
them, i.e. - IDs. This is simple IDA-based IDs generator for pernet

Addressing questions about potential checkpoint/restart problems: 
these IDs are "lite-offsets" within the net structure and are by no 
means supposed to be exported to the userspace.

Since it will be used in the nearest future by devices only (tun,
vlan, tunnels, bridge, etc), I make it resemble the functionality
of register_pernet_device().

The new ids is stored in the *id pointer _before_ calling the init
callback to make this id available in this callback.
Signed-off-by: default avatarPavel Emelyanov <>
Signed-off-by: default avatarDavid S. Miller <>
parent 31efdf05
......@@ -185,6 +185,8 @@ extern int register_pernet_subsys(struct pernet_operations *);
extern void unregister_pernet_subsys(struct pernet_operations *);
extern int register_pernet_device(struct pernet_operations *);
extern void unregister_pernet_device(struct pernet_operations *);
extern int register_pernet_gen_device(int *id, struct pernet_operations *);
extern void unregister_pernet_gen_device(int id, struct pernet_operations *);
struct ctl_path;
struct ctl_table;
......@@ -5,6 +5,7 @@
#include <linux/list.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/idr.h>
#include <net/net_namespace.h>
......@@ -253,6 +254,8 @@ static void unregister_pernet_operations(struct pernet_operations *ops)
static DEFINE_IDA(net_generic_ids);
* register_pernet_subsys - register a network namespace subsystem
* @ops: pernet operations structure for the subsystem
......@@ -330,6 +333,30 @@ int register_pernet_device(struct pernet_operations *ops)
int register_pernet_gen_device(int *id, struct pernet_operations *ops)
int error;
error = ida_get_new_above(&net_generic_ids, 1, id);
if (error) {
if (error == -EAGAIN) {
ida_pre_get(&net_generic_ids, GFP_KERNEL);
goto again;
goto out;
error = register_pernet_operations(&pernet_list, ops);
if (error)
ida_remove(&net_generic_ids, *id);
else if (first_device == &pernet_list)
first_device = &ops->list;
return error;
* unregister_pernet_device - unregister a network namespace netdevice
* @ops: pernet operations structure to manipulate
......@@ -348,3 +375,14 @@ void unregister_pernet_device(struct pernet_operations *ops)
void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
if (&ops->list == first_device)
first_device = first_device->next;
ida_remove(&net_generic_ids, id);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment