Skip to content

Commit 62531a1

Browse files
Amit CohenPaolo Abeni
Amit Cohen
authored and
Paolo Abeni
committed
net: switchdev: Convert blocking notification chain to a raw one
A blocking notification chain uses a read-write semaphore to protect the integrity of the chain. The semaphore is acquired for writing when adding / removing notifiers to / from the chain and acquired for reading when traversing the chain and informing notifiers about an event. In case of the blocking switchdev notification chain, recursive notifications are possible which leads to the semaphore being acquired twice for reading and to lockdep warnings being generated [1]. Specifically, this can happen when the bridge driver processes a SWITCHDEV_BRPORT_UNOFFLOADED event which causes it to emit notifications about deferred events when calling switchdev_deferred_process(). Fix this by converting the notification chain to a raw notification chain in a similar fashion to the netdev notification chain. Protect the chain using the RTNL mutex by acquiring it when modifying the chain. Events are always informed under the RTNL mutex, but add an assertion in call_switchdev_blocking_notifiers() to make sure this is not violated in the future. Maintain the "blocking" prefix as events are always emitted from process context and listeners are allowed to block. [1]: WARNING: possible recursive locking detected 6.14.0-rc4-custom-g079270089484 CachyOS#1 Not tainted -------------------------------------------- ip/52731 is trying to acquire lock: ffffffff850918d8 ((switchdev_blocking_notif_chain).rwsem){++++}-{4:4}, at: blocking_notifier_call_chain+0x58/0xa0 but task is already holding lock: ffffffff850918d8 ((switchdev_blocking_notif_chain).rwsem){++++}-{4:4}, at: blocking_notifier_call_chain+0x58/0xa0 other info that might help us debug this: Possible unsafe locking scenario: CPU0 ---- lock((switchdev_blocking_notif_chain).rwsem); lock((switchdev_blocking_notif_chain).rwsem); *** DEADLOCK *** May be due to missing lock nesting notation 3 locks held by ip/52731: #0: ffffffff84f795b0 (rtnl_mutex){+.+.}-{4:4}, at: rtnl_newlink+0x727/0x1dc0 CachyOS#1: ffffffff8731f628 (&net->rtnl_mutex){+.+.}-{4:4}, at: rtnl_newlink+0x790/0x1dc0 CachyOS#2: ffffffff850918d8 ((switchdev_blocking_notif_chain).rwsem){++++}-{4:4}, at: blocking_notifier_call_chain+0x58/0xa0 stack backtrace: ... ? __pfx_down_read+0x10/0x10 ? __pfx_mark_lock+0x10/0x10 ? __pfx_switchdev_port_attr_set_deferred+0x10/0x10 blocking_notifier_call_chain+0x58/0xa0 switchdev_port_attr_notify.constprop.0+0xb3/0x1b0 ? __pfx_switchdev_port_attr_notify.constprop.0+0x10/0x10 ? mark_held_locks+0x94/0xe0 ? switchdev_deferred_process+0x11a/0x340 switchdev_port_attr_set_deferred+0x27/0xd0 switchdev_deferred_process+0x164/0x340 br_switchdev_port_unoffload+0xc8/0x100 [bridge] br_switchdev_blocking_event+0x29f/0x580 [bridge] notifier_call_chain+0xa2/0x440 blocking_notifier_call_chain+0x6e/0xa0 switchdev_bridge_port_unoffload+0xde/0x1a0 ... Fixes: f7a70d6 ("net: bridge: switchdev: Ensure deferred event delivery on unoffload") Signed-off-by: Amit Cohen <amcohen@nvidia.com> Reviewed-by: Ido Schimmel <idosch@nvidia.com> Reviewed-by: Simon Horman <horms@kernel.org> Reviewed-by: Vladimir Oltean <olteanv@gmail.com> Tested-by: Vladimir Oltean <olteanv@gmail.com> Link: https://patch.msgid.link/20250305121509.631207-1-amcohen@nvidia.com Signed-off-by: Paolo Abeni <pabeni@redhat.com>
1 parent 77b2ab3 commit 62531a1

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

net/switchdev/switchdev.c

+18-7
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ bool switchdev_port_obj_act_is_deferred(struct net_device *dev,
472472
EXPORT_SYMBOL_GPL(switchdev_port_obj_act_is_deferred);
473473

474474
static ATOMIC_NOTIFIER_HEAD(switchdev_notif_chain);
475-
static BLOCKING_NOTIFIER_HEAD(switchdev_blocking_notif_chain);
475+
static RAW_NOTIFIER_HEAD(switchdev_blocking_notif_chain);
476476

477477
/**
478478
* register_switchdev_notifier - Register notifier
@@ -518,28 +518,39 @@ EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
518518

519519
int register_switchdev_blocking_notifier(struct notifier_block *nb)
520520
{
521-
struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
521+
struct raw_notifier_head *chain = &switchdev_blocking_notif_chain;
522+
int err;
523+
524+
rtnl_lock();
525+
err = raw_notifier_chain_register(chain, nb);
526+
rtnl_unlock();
522527

523-
return blocking_notifier_chain_register(chain, nb);
528+
return err;
524529
}
525530
EXPORT_SYMBOL_GPL(register_switchdev_blocking_notifier);
526531

527532
int unregister_switchdev_blocking_notifier(struct notifier_block *nb)
528533
{
529-
struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
534+
struct raw_notifier_head *chain = &switchdev_blocking_notif_chain;
535+
int err;
530536

531-
return blocking_notifier_chain_unregister(chain, nb);
537+
rtnl_lock();
538+
err = raw_notifier_chain_unregister(chain, nb);
539+
rtnl_unlock();
540+
541+
return err;
532542
}
533543
EXPORT_SYMBOL_GPL(unregister_switchdev_blocking_notifier);
534544

535545
int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
536546
struct switchdev_notifier_info *info,
537547
struct netlink_ext_ack *extack)
538548
{
549+
ASSERT_RTNL();
539550
info->dev = dev;
540551
info->extack = extack;
541-
return blocking_notifier_call_chain(&switchdev_blocking_notif_chain,
542-
val, info);
552+
return raw_notifier_call_chain(&switchdev_blocking_notif_chain,
553+
val, info);
543554
}
544555
EXPORT_SYMBOL_GPL(call_switchdev_blocking_notifiers);
545556

0 commit comments

Comments
 (0)