net: Defer netdev KOBJ_ADD uevent until the device is published
Subsystems (2)
PATCH 1/1 net: Defer netdev KOBJ_ADD uevent until the device is published
netdev_register_kobject() calls device_add(), which emits KOBJ_ADD and wakes udev, but register_netdevice() only makes the device findable by name later, in list_netdevice(). A udev worker that reacts to the uevent can therefore run against a device that no lookup can find yet.
This used to be harmless because the ethtool ioctl took the rtnl_lock when looking the device up, and register_netdevice() runs under rtnl, so the worker simply blocked until registration finished. The commit in the fixes tag moved the lookup out from under rtnl for ops-locked drivers. Now there is a short window in register_netdevice() between netdev_register_kobject() until list_netdevice() when the device is not findable by name.
This was reproduced with the mlx5 driver on a kernel with KASAN enabled during devlink reload: systemd-udevd's net_driver builtin gets -ENODEV from ETHTOOL_GDRVINFO, which was preventing interface renaming.
Suppress the uevent in netdev_register_kobject() and emit it from register_netdevice() next to rtmsg_ifinfo(). This is the last point in register_netdevice() where no error can happen, so only fully registered devices are announced: the registration error paths never reach it, and the device_del() that unwinds them stays silent as well, leaving userspace with neither an add nor a remove.
--- net/core/dev.c | 1 + net/core/net-sysfs.c | 16 ++++++++++++++++ net/core/net-sysfs.h | 1 + 3 files changed, 18 insertions(+)
net/core/dev.c +1 -0
@@ -11499,6 +11499,7 @@ int register_netdevice(struct net_device *dev)* Prevent userspace races by waiting until the network * device is fully setup before sending notifications. */ +netdev_uevent_add (dev);if (!(dev->rtnl_link_ops && dev->rtnl_link_initializing))rtmsg_ifinfo (RTM_NEWLINK , dev, ~0U ,GFP_KERNEL ,0 ,NULL );
net/core/net-sysfs.c +16 -0
@@ -2334,6 +2334,11 @@ int netdev_register_kobject(struct net_device *ndev) *groups++ = &wireless_group;#endif /* CONFIG_SYSFS */ +/* Hold back the KOBJ_ADD uevent until the device is discoverable +* by name. +*/ +dev_set_uevent_suppress (dev,1 ); + error =device_add (dev);if (error)return error;
@@ -2349,6 +2354,17 @@ int netdev_register_kobject(struct net_device *ndev)return error; } +/* Announce a fully registered device to userspace. This pairs with the uevent +* suppression from netdev_register_kobject(); +*/ +void netdev_uevent_add (struct net_device *ndev) +{ +struct device *dev = &ndev->dev; + +dev_set_uevent_suppress (dev,0 ); +kobject_uevent (&dev->kobj,KOBJ_ADD ); +} +/* Change owner for sysfs entries when moving network devices across network * namespaces owned by different user namespaces. */
net/core/net-sysfs.h +1 -0
@@ -4,6 +4,7 @@int __init netdev_kobject_init (void );int netdev_register_kobject (struct net_device *); +void netdev_uevent_add (struct net_device *dev);void netdev_unregister_kobject (struct net_device *);int net_rx_queue_update_kobjects (struct net_device *,int old_num,int new_num); int netdev_queue_update_kobjects(struct net_device *net,
Signature · 1 line
2.55.0
Jakub Kicinski
maintainerQuoted 24 lines · On Tue, 4 Aug 2026 18:51:01 +0300 Dragos Tatulea wrote:
netdev_register_kobject()callsdevice_add(), which emitsKOBJ_ADDand wakes udev, butregister_netdevice()only makes the device findable by name later, inlist_netdevice(). A udev worker that reacts to the uevent can therefore run against a device that no lookup can find yet.This used to be harmless because the ethtool ioctl took the
rtnl_lockwhen looking the device up, andregister_netdevice()runs under rtnl, so the worker simply blocked until registration finished. The commit in the fixes tag moved the lookup out from under rtnl for ops-locked drivers. Now there is a short window inregister_netdevice()betweennetdev_register_kobject()untillist_netdevice()when the device is not findable by name.This was reproduced with the mlx5 driver on a kernel with KASAN enabled during devlink reload: systemd-udevd's
net_driverbuiltin gets-ENODEVfromETHTOOL_GDRVINFO, which was preventing interface renaming.Suppress the uevent in
netdev_register_kobject()and emit it fromregister_netdevice()next tortmsg_ifinfo(). This is the last point inregister_netdevice()where no error can happen, so only fully registered devices are announced: the registration error paths never reach it, and thedevice_del()that unwinds them stays silent as well, leaving userspace with neither an add nor a remove.
Quoted 4 lines
Fixes f994752b1127 ("net: ethtool: optionally skip rtnl_lock on IOCTL path")Assisted-by Claude:claude-opus-5Signed-off-by Dragos Tatulea <[email protected]>Reviewed-by Shahar Shitrit <[email protected]>
LGTM, one nit..
Quoted 22 lines
net/core/dev.c | 1 + net/core/net-sysfs.c | 16 ++++++++++++++++ net/core/net-sysfs.h | 1 + 3 files changed, 18 insertions(+)net/core/dev.c +1
-0@@ -11499,6 +11499,7 @@ int register_netdevice(struct net_device *dev)* Prevent userspace races by waiting until the network * device is fully setup before sending notifications. */ +netdev_uevent_add (dev);if (!(dev->rtnl_link_ops && dev->rtnl_link_initializing))rtmsg_ifinfo (RTM_NEWLINK , dev, ~0U ,GFP_KERNEL ,0 ,NULL );net/core/net-sysfs.c +0
-0
net/core/net-sysfs.c int netdev_register_kobject(struct net_device *ndev) · line 2338
Quoted 5 lines
net/core/net-sysfs.c +2
-0@@ -2334,6 +2334,11 @@ int netdev_register_kobject(struct net_device *ndev) *groups++ = &wireless_group;#endif /* CONFIG_SYSFS */ +/* Hold back the KOBJ_ADD uevent until the device is discoverable +* by name.
nit: maybe just say "until the device is listed" ? Or there's some less than obvious relevance of the name lookup vs ifindex lookup?
net/core/net-sysfs.c int netdev_register_kobject(struct net_device *ndev) · line 2358
Quoted 12 lines
net/core/net-sysfs.c +5
-0+*/ +dev_set_uevent_suppress (dev,1 ); + error =device_add (dev);if (error)return error;@@ -2349,6 +2354,17 @@ int netdev_register_kobject(struct net_device *ndev)return error; } +/* Announce a fully registered device to userspace. This pairs with the uevent +* suppression from netdev_register_kobject();
nit: s/;/./ ?
net/core/net-sysfs.c int netdev_register_kobject(struct net_device *ndev) · line 2366
Quoted 8 lines
net/core/net-sysfs.c +8
-0+*/ +void netdev_uevent_add (struct net_device *ndev) +{ +struct device *dev = &ndev->dev; + +dev_set_uevent_suppress (dev,0 ); +kobject_uevent (&dev->kobj,KOBJ_ADD ); +}
Please wait for Clashiko to cross-review before posting v2.
Signature · 1 line
pw-bot: cr