forked from mirrors/linux
		
	 04efcee6ef
			
		
	
	
		04efcee6ef
		
	
	
	
	
		
			
			Cosmin reports an issue with ipv6_add_dev being called from
NETDEV_CHANGE notifier:
[ 3455.008776]  ? ipv6_add_dev+0x370/0x620
[ 3455.010097]  ipv6_find_idev+0x96/0xe0
[ 3455.010725]  addrconf_add_dev+0x1e/0xa0
[ 3455.011382]  addrconf_init_auto_addrs+0xb0/0x720
[ 3455.013537]  addrconf_notify+0x35f/0x8d0
[ 3455.014214]  notifier_call_chain+0x38/0xf0
[ 3455.014903]  netdev_state_change+0x65/0x90
[ 3455.015586]  linkwatch_do_dev+0x5a/0x70
[ 3455.016238]  rtnl_getlink+0x241/0x3e0
[ 3455.019046]  rtnetlink_rcv_msg+0x177/0x5e0
Similarly, linkwatch might get to ipv6_add_dev without ops lock:
[ 3456.656261]  ? ipv6_add_dev+0x370/0x620
[ 3456.660039]  ipv6_find_idev+0x96/0xe0
[ 3456.660445]  addrconf_add_dev+0x1e/0xa0
[ 3456.660861]  addrconf_init_auto_addrs+0xb0/0x720
[ 3456.661803]  addrconf_notify+0x35f/0x8d0
[ 3456.662236]  notifier_call_chain+0x38/0xf0
[ 3456.662676]  netdev_state_change+0x65/0x90
[ 3456.663112]  linkwatch_do_dev+0x5a/0x70
[ 3456.663529]  __linkwatch_run_queue+0xeb/0x200
[ 3456.663990]  linkwatch_event+0x21/0x30
[ 3456.664399]  process_one_work+0x211/0x610
[ 3456.664828]  worker_thread+0x1cc/0x380
[ 3456.665691]  kthread+0xf4/0x210
Reclassify NETDEV_CHANGE as a notifier that consistently runs under the
instance lock.
Link: https://lore.kernel.org/netdev/aac073de8beec3e531c86c101b274d434741c28e.camel@nvidia.com/
Reported-by: Cosmin Ratiu <cratiu@nvidia.com>
Tested-by: Cosmin Ratiu <cratiu@nvidia.com>
Fixes: ad7c7b2172 ("net: hold netdev instance lock during sysfs operations")
Signed-off-by: Stanislav Fomichev <sdf@fomichev.me>
Link: https://patch.msgid.link/20250404161122.3907628-1-sdf@fomichev.me
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
		
	
			
		
			
				
	
	
		
			120 lines
		
	
	
	
		
			3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			120 lines
		
	
	
	
		
			3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| // SPDX-License-Identifier: GPL-2.0-or-later
 | |
| /* Copyright Amazon.com Inc. or its affiliates. */
 | |
| 
 | |
| #include <linux/init.h>
 | |
| #include <linux/netdevice.h>
 | |
| #include <linux/notifier.h>
 | |
| #include <linux/rtnetlink.h>
 | |
| #include <net/net_namespace.h>
 | |
| #include <net/netdev_lock.h>
 | |
| #include <net/netns/generic.h>
 | |
| 
 | |
| int netdev_debug_event(struct notifier_block *nb, unsigned long event,
 | |
| 		       void *ptr)
 | |
| {
 | |
| 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
 | |
| 	struct net *net = dev_net(dev);
 | |
| 	enum netdev_cmd cmd = event;
 | |
| 
 | |
| 	/* Keep enum and don't add default to trigger -Werror=switch */
 | |
| 	switch (cmd) {
 | |
| 	case NETDEV_REGISTER:
 | |
| 	case NETDEV_UP:
 | |
| 	case NETDEV_CHANGE:
 | |
| 		netdev_ops_assert_locked(dev);
 | |
| 		fallthrough;
 | |
| 	case NETDEV_DOWN:
 | |
| 	case NETDEV_REBOOT:
 | |
| 	case NETDEV_UNREGISTER:
 | |
| 	case NETDEV_CHANGEMTU:
 | |
| 	case NETDEV_CHANGEADDR:
 | |
| 	case NETDEV_PRE_CHANGEADDR:
 | |
| 	case NETDEV_GOING_DOWN:
 | |
| 	case NETDEV_FEAT_CHANGE:
 | |
| 	case NETDEV_BONDING_FAILOVER:
 | |
| 	case NETDEV_PRE_UP:
 | |
| 	case NETDEV_PRE_TYPE_CHANGE:
 | |
| 	case NETDEV_POST_TYPE_CHANGE:
 | |
| 	case NETDEV_POST_INIT:
 | |
| 	case NETDEV_PRE_UNINIT:
 | |
| 	case NETDEV_RELEASE:
 | |
| 	case NETDEV_NOTIFY_PEERS:
 | |
| 	case NETDEV_JOIN:
 | |
| 	case NETDEV_CHANGEUPPER:
 | |
| 	case NETDEV_RESEND_IGMP:
 | |
| 	case NETDEV_PRECHANGEMTU:
 | |
| 	case NETDEV_CHANGEINFODATA:
 | |
| 	case NETDEV_BONDING_INFO:
 | |
| 	case NETDEV_PRECHANGEUPPER:
 | |
| 	case NETDEV_CHANGELOWERSTATE:
 | |
| 	case NETDEV_UDP_TUNNEL_PUSH_INFO:
 | |
| 	case NETDEV_UDP_TUNNEL_DROP_INFO:
 | |
| 	case NETDEV_CHANGE_TX_QUEUE_LEN:
 | |
| 	case NETDEV_CVLAN_FILTER_PUSH_INFO:
 | |
| 	case NETDEV_CVLAN_FILTER_DROP_INFO:
 | |
| 	case NETDEV_SVLAN_FILTER_PUSH_INFO:
 | |
| 	case NETDEV_SVLAN_FILTER_DROP_INFO:
 | |
| 	case NETDEV_OFFLOAD_XSTATS_ENABLE:
 | |
| 	case NETDEV_OFFLOAD_XSTATS_DISABLE:
 | |
| 	case NETDEV_OFFLOAD_XSTATS_REPORT_USED:
 | |
| 	case NETDEV_OFFLOAD_XSTATS_REPORT_DELTA:
 | |
| 	case NETDEV_XDP_FEAT_CHANGE:
 | |
| 		ASSERT_RTNL();
 | |
| 		break;
 | |
| 
 | |
| 	case NETDEV_CHANGENAME:
 | |
| 		ASSERT_RTNL_NET(net);
 | |
| 		break;
 | |
| 	}
 | |
| 
 | |
| 	return NOTIFY_DONE;
 | |
| }
 | |
| EXPORT_SYMBOL_NS_GPL(netdev_debug_event, "NETDEV_INTERNAL");
 | |
| 
 | |
| static int rtnl_net_debug_net_id;
 | |
| 
 | |
| static int __net_init rtnl_net_debug_net_init(struct net *net)
 | |
| {
 | |
| 	struct notifier_block *nb;
 | |
| 
 | |
| 	nb = net_generic(net, rtnl_net_debug_net_id);
 | |
| 	nb->notifier_call = netdev_debug_event;
 | |
| 
 | |
| 	return register_netdevice_notifier_net(net, nb);
 | |
| }
 | |
| 
 | |
| static void __net_exit rtnl_net_debug_net_exit(struct net *net)
 | |
| {
 | |
| 	struct notifier_block *nb;
 | |
| 
 | |
| 	nb = net_generic(net, rtnl_net_debug_net_id);
 | |
| 	unregister_netdevice_notifier_net(net, nb);
 | |
| }
 | |
| 
 | |
| static struct pernet_operations rtnl_net_debug_net_ops __net_initdata = {
 | |
| 	.init = rtnl_net_debug_net_init,
 | |
| 	.exit = rtnl_net_debug_net_exit,
 | |
| 	.id = &rtnl_net_debug_net_id,
 | |
| 	.size = sizeof(struct notifier_block),
 | |
| };
 | |
| 
 | |
| static struct notifier_block rtnl_net_debug_block = {
 | |
| 	.notifier_call = netdev_debug_event,
 | |
| };
 | |
| 
 | |
| static int __init rtnl_net_debug_init(void)
 | |
| {
 | |
| 	int ret;
 | |
| 
 | |
| 	ret = register_pernet_subsys(&rtnl_net_debug_net_ops);
 | |
| 	if (ret)
 | |
| 		return ret;
 | |
| 
 | |
| 	ret = register_netdevice_notifier(&rtnl_net_debug_block);
 | |
| 	if (ret)
 | |
| 		unregister_pernet_subsys(&rtnl_net_debug_net_ops);
 | |
| 
 | |
| 	return ret;
 | |
| }
 | |
| 
 | |
| subsys_initcall(rtnl_net_debug_init);
 |