Skip to content

Commit 6d34594

Browse files
idoschkuba-moo
authored andcommitted
bridge: Fix sleep in atomic context in sysfs path
Since the start of the git history, brport_store() always acquired the bridge lock. Back then this decision made sense: The bridge lock protects the STP state of the bridge and its ports and at that time the function was only used by two STP related attributes (cost and priority). Nowadays, brport_store() processes a lot more attributes and most of them do not need the bridge lock: * Bridge flags: Only require RTNL. Read locklessly by the data path. Annotations can be added in net-next. * FDB port flushing: Only requires the FDB lock. * Multicast attributes: Only require the multicast lock. * Group forward mask: Only requires RTNL. Read locklessly by the data path. Annotations can be added in net-next. * Backup port: Only requires RTNL. Read locklessly by the data path. This is a problem as the bridge calls dev_set_promiscuity() when certain bridge port flags change and this function can sleep since the commit cited below, resulting in a splat such as [1]. Fix this by reducing the scope of the bridge lock and only take it when processing the two STP related attributes that require it. Remove the now stale comment from br_switchdev_set_port_flag(). The SWITCHDEV_F_DEFER flag can be removed in net-next. [1] BUG: sleeping function called from invalid context at net/core/dev_addr_lists.c:1262 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 372, name: bash preempt_count: 201, expected: 0 RCU nest depth: 0, expected: 0 5 locks held by bash/372: #0: ffff88810c51c3f0 (sb_writers#7){.+.+}-{0:0}, at: ksys_write (fs/read_write.c:740) #1: ffff888115ce9480 (&of->mutex){+.+.}-{4:4}, at: kernfs_fop_write_iter (fs/kernfs/file.c:343) #2: ffff88810b9fd330 (kn->active#37){.+.+}-{0:0}, at: kernfs_fop_write_iter (fs/kernfs/file.c:80 fs/kernfs/file.c:344) #3: ffffffffa59473a0 (rtnl_mutex){+.+.}-{4:4}, at: brport_store (net/bridge/br_sysfs_if.c:326) #4: ffff8881099d2d58 (&br->lock){+...}-{3:3}, at: brport_store (./include/linux/spinlock.h:348 net/bridge/br_sysfs_if.c:345) Preemption disabled at: 0x0 Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011 Call Trace: <TASK> dump_stack_lvl (lib/dump_stack.c:94 lib/dump_stack.c:120) __might_resched.cold (kernel/sched/core.c:9163) netif_rx_mode_run (net/core/dev_addr_lists.c:1262) netif_rx_mode_sync (net/core/dev_addr_lists.c:1428) dev_set_promiscuity (net/core/dev_api.c:289) br_manage_promisc (net/bridge/br_if.c:135 net/bridge/br_if.c:172) br_port_flags_change (net/bridge/br_if.c:242 net/bridge/br_if.c:747) store_learning (net/bridge/br_sysfs_if.c:79 net/bridge/br_sysfs_if.c:235) brport_store (net/bridge/br_sysfs_if.c:346) kernfs_fop_write_iter (fs/kernfs/file.c:352) new_sync_write (fs/read_write.c:595) vfs_write (fs/read_write.c:688) ksys_write (fs/read_write.c:740) do_syscall_64 (arch/x86/entry/syscall_64.c:63 arch/x86/entry/syscall_64.c:94) entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) Fixes: 78cd408 ("net: add missing instance lock to dev_set_promiscuity") Reviewed-by: Nikolay Aleksandrov <nikolay@nvidia.com> Signed-off-by: Ido Schimmel <idosch@nvidia.com> Link: https://patch.msgid.link/20260526064818.272516-3-idosch@nvidia.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 5eec442 commit 6d34594

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

net/bridge/br_switchdev.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ int br_switchdev_set_port_flag(struct net_bridge_port *p,
9999
attr.u.brport_flags.val = flags;
100100
attr.u.brport_flags.mask = mask;
101101

102-
/* We run from atomic context here */
103102
err = call_switchdev_notifiers(SWITCHDEV_PORT_ATTR_SET, p->dev,
104103
&info.info, extack);
105104
err = notifier_to_errno(err);

net/bridge/br_sysfs_if.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,34 @@ static ssize_t show_path_cost(struct net_bridge_port *p, char *buf)
8686
return sysfs_emit(buf, "%d\n", p->path_cost);
8787
}
8888

89-
static BRPORT_ATTR(path_cost, 0644,
90-
show_path_cost, br_stp_set_path_cost);
89+
static int store_path_cost(struct net_bridge_port *p, unsigned long v)
90+
{
91+
int ret;
92+
93+
spin_lock_bh(&p->br->lock);
94+
ret = br_stp_set_path_cost(p, v);
95+
spin_unlock_bh(&p->br->lock);
96+
return ret;
97+
}
98+
99+
static BRPORT_ATTR(path_cost, 0644, show_path_cost, store_path_cost);
91100

92101
static ssize_t show_priority(struct net_bridge_port *p, char *buf)
93102
{
94103
return sysfs_emit(buf, "%d\n", p->priority);
95104
}
96105

97-
static BRPORT_ATTR(priority, 0644,
98-
show_priority, br_stp_set_port_priority);
106+
static int store_priority(struct net_bridge_port *p, unsigned long v)
107+
{
108+
int ret;
109+
110+
spin_lock_bh(&p->br->lock);
111+
ret = br_stp_set_port_priority(p, v);
112+
spin_unlock_bh(&p->br->lock);
113+
return ret;
114+
}
115+
116+
static BRPORT_ATTR(priority, 0644, show_priority, store_priority);
99117

100118
static ssize_t show_designated_root(struct net_bridge_port *p, char *buf)
101119
{
@@ -334,17 +352,13 @@ static ssize_t brport_store(struct kobject *kobj,
334352
ret = -ENOMEM;
335353
goto out_unlock;
336354
}
337-
spin_lock_bh(&p->br->lock);
338355
ret = brport_attr->store_raw(p, buf_copy);
339-
spin_unlock_bh(&p->br->lock);
340356
kfree(buf_copy);
341357
} else if (brport_attr->store) {
342358
val = simple_strtoul(buf, &endp, 0);
343359
if (endp == buf)
344360
goto out_unlock;
345-
spin_lock_bh(&p->br->lock);
346361
ret = brport_attr->store(p, val);
347-
spin_unlock_bh(&p->br->lock);
348362
}
349363

350364
if (!ret) {

0 commit comments

Comments
 (0)