Skip to content

Commit

Permalink
netns: Fix device renaming for sysfs
Browse files Browse the repository at this point in the history
When a netdev is moved across namespaces with the
'dev_change_net_namespace' function, the 'device_rename' function is
used to fixup kobject and refresh the sysfs tree. The device_rename
function will call kobject_rename and this one will check if there is
an object with the same name and this is the case because we are
renaming the object with the same name.

The use of 'device_rename' seems for me wrong because we usually don't
rename it but just move it across namespaces. As we just want to do a
mini "netdev_[un]register", IMO the functions
'netdev_[un]register_kobject' should be used instead, like an usual
network device [un]registering.

This patch replace device_rename by netdev_unregister_kobject,
followed by netdev_register_kobject.

The netdev_register_kobject will call device_initialize and will raise
a warning indicating the device was already initialized. In order to
fix that, I split the device initialization into a separate function
and use it together with 'netdev_register_kobject' into
register_netdevice. So we can safely call 'netdev_register_kobject' in
'dev_change_net_namespace'.

This fix will allow to properly use the sysfs per namespace which is
coming from -mm tree.

Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
Acked-by: Benjamin Thery <benjamin.thery@bull.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Daniel Lezcano authored and David S. Miller committed May 3, 2008
1 parent 705d209 commit aaf8cdc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion net/core/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -3776,6 +3776,7 @@ int register_netdevice(struct net_device *dev)
}
}

netdev_initialize_kobject(dev);
ret = netdev_register_kobject(dev);
if (ret)
goto err_uninit;
Expand Down Expand Up @@ -4208,7 +4209,8 @@ int dev_change_net_namespace(struct net_device *dev, struct net *net, const char
}

/* Fixup kobjects */
err = device_rename(&dev->dev, dev->name);
netdev_unregister_kobject(dev);
err = netdev_register_kobject(dev);
WARN_ON(err);

/* Add the device back in the hashes */
Expand Down
7 changes: 6 additions & 1 deletion net/core/net-sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ int netdev_register_kobject(struct net_device *net)
struct device *dev = &(net->dev);
struct attribute_group **groups = net->sysfs_groups;

device_initialize(dev);
dev->class = &net_class;
dev->platform_data = net;
dev->groups = groups;
Expand All @@ -470,6 +469,12 @@ int netdev_register_kobject(struct net_device *net)
return device_add(dev);
}

void netdev_initialize_kobject(struct net_device *net)
{
struct device *device = &(net->dev);
device_initialize(device);
}

int netdev_kobject_init(void)
{
return class_register(&net_class);
Expand Down
2 changes: 1 addition & 1 deletion net/core/net-sysfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
int netdev_kobject_init(void);
int netdev_register_kobject(struct net_device *);
void netdev_unregister_kobject(struct net_device *);

void netdev_initialize_kobject(struct net_device *);
#endif

0 comments on commit aaf8cdc

Please sign in to comment.