cbb9172032326ce2bfed48dc6d29423a6c4611a7
[openwrt/staging/blocktrron.git] /
1 From fe2b1226656afae56702d1d84c6900f6b67df297 Mon Sep 17 00:00:00 2001
2 From: Heiner Kallweit <hkallweit1@gmail.com>
3 Date: Fri, 1 Dec 2023 11:23:22 +0100
4 Subject: [PATCH] leds: trigger: netdev: fix RTNL handling to prevent potential
5 deadlock
6
7 When working on LED support for r8169 I got the following lockdep
8 warning. Easiest way to prevent this scenario seems to be to take
9 the RTNL lock before the trigger_data lock in set_device_name().
10
11 ======================================================
12 WARNING: possible circular locking dependency detected
13 6.7.0-rc2-next-20231124+ #2 Not tainted
14 ------------------------------------------------------
15 bash/383 is trying to acquire lock:
16 ffff888103aa1c68 (&trigger_data->lock){+.+.}-{3:3}, at: netdev_trig_notify+0xec/0x190 [ledtrig_netdev]
17
18 but task is already holding lock:
19 ffffffff8cddf808 (rtnl_mutex){+.+.}-{3:3}, at: rtnl_lock+0x12/0x20
20
21 which lock already depends on the new lock.
22
23 the existing dependency chain (in reverse order) is:
24
25 -> #1 (rtnl_mutex){+.+.}-{3:3}:
26 __mutex_lock+0x9b/0xb50
27 mutex_lock_nested+0x16/0x20
28 rtnl_lock+0x12/0x20
29 set_device_name+0xa9/0x120 [ledtrig_netdev]
30 netdev_trig_activate+0x1a1/0x230 [ledtrig_netdev]
31 led_trigger_set+0x172/0x2c0
32 led_trigger_write+0xf1/0x140
33 sysfs_kf_bin_write+0x5d/0x80
34 kernfs_fop_write_iter+0x15d/0x210
35 vfs_write+0x1f0/0x510
36 ksys_write+0x6c/0xf0
37 __x64_sys_write+0x14/0x20
38 do_syscall_64+0x3f/0xf0
39 entry_SYSCALL_64_after_hwframe+0x6c/0x74
40
41 -> #0 (&trigger_data->lock){+.+.}-{3:3}:
42 __lock_acquire+0x1459/0x25a0
43 lock_acquire+0xc8/0x2d0
44 __mutex_lock+0x9b/0xb50
45 mutex_lock_nested+0x16/0x20
46 netdev_trig_notify+0xec/0x190 [ledtrig_netdev]
47 call_netdevice_register_net_notifiers+0x5a/0x100
48 register_netdevice_notifier+0x85/0x120
49 netdev_trig_activate+0x1d4/0x230 [ledtrig_netdev]
50 led_trigger_set+0x172/0x2c0
51 led_trigger_write+0xf1/0x140
52 sysfs_kf_bin_write+0x5d/0x80
53 kernfs_fop_write_iter+0x15d/0x210
54 vfs_write+0x1f0/0x510
55 ksys_write+0x6c/0xf0
56 __x64_sys_write+0x14/0x20
57 do_syscall_64+0x3f/0xf0
58 entry_SYSCALL_64_after_hwframe+0x6c/0x74
59
60 other info that might help us debug this:
61
62 Possible unsafe locking scenario:
63
64 CPU0 CPU1
65 ---- ----
66 lock(rtnl_mutex);
67 lock(&trigger_data->lock);
68 lock(rtnl_mutex);
69 lock(&trigger_data->lock);
70
71 *** DEADLOCK ***
72
73 8 locks held by bash/383:
74 #0: ffff888103ff33f0 (sb_writers#3){.+.+}-{0:0}, at: ksys_write+0x6c/0xf0
75 #1: ffff888103aa1e88 (&of->mutex){+.+.}-{3:3}, at: kernfs_fop_write_iter+0x114/0x210
76 #2: ffff8881036f1890 (kn->active#82){.+.+}-{0:0}, at: kernfs_fop_write_iter+0x11d/0x210
77 #3: ffff888108e2c358 (&led_cdev->led_access){+.+.}-{3:3}, at: led_trigger_write+0x30/0x140
78 #4: ffffffff8cdd9e10 (triggers_list_lock){++++}-{3:3}, at: led_trigger_write+0x75/0x140
79 #5: ffff888108e2c270 (&led_cdev->trigger_lock){++++}-{3:3}, at: led_trigger_write+0xe3/0x140
80 #6: ffffffff8cdde3d0 (pernet_ops_rwsem){++++}-{3:3}, at: register_netdevice_notifier+0x1c/0x120
81 #7: ffffffff8cddf808 (rtnl_mutex){+.+.}-{3:3}, at: rtnl_lock+0x12/0x20
82
83 stack backtrace:
84 CPU: 0 PID: 383 Comm: bash Not tainted 6.7.0-rc2-next-20231124+ #2
85 Hardware name: Default string Default string/Default string, BIOS ADLN.M6.SODIMM.ZB.CY.015 08/08/2023
86 Call Trace:
87 <TASK>
88 dump_stack_lvl+0x5c/0xd0
89 dump_stack+0x10/0x20
90 print_circular_bug+0x2dd/0x410
91 check_noncircular+0x131/0x150
92 __lock_acquire+0x1459/0x25a0
93 lock_acquire+0xc8/0x2d0
94 ? netdev_trig_notify+0xec/0x190 [ledtrig_netdev]
95 __mutex_lock+0x9b/0xb50
96 ? netdev_trig_notify+0xec/0x190 [ledtrig_netdev]
97 ? __this_cpu_preempt_check+0x13/0x20
98 ? netdev_trig_notify+0xec/0x190 [ledtrig_netdev]
99 ? __cancel_work_timer+0x11c/0x1b0
100 ? __mutex_lock+0x123/0xb50
101 mutex_lock_nested+0x16/0x20
102 ? mutex_lock_nested+0x16/0x20
103 netdev_trig_notify+0xec/0x190 [ledtrig_netdev]
104 call_netdevice_register_net_notifiers+0x5a/0x100
105 register_netdevice_notifier+0x85/0x120
106 netdev_trig_activate+0x1d4/0x230 [ledtrig_netdev]
107 led_trigger_set+0x172/0x2c0
108 ? preempt_count_add+0x49/0xc0
109 led_trigger_write+0xf1/0x140
110 sysfs_kf_bin_write+0x5d/0x80
111 kernfs_fop_write_iter+0x15d/0x210
112 vfs_write+0x1f0/0x510
113 ksys_write+0x6c/0xf0
114 __x64_sys_write+0x14/0x20
115 do_syscall_64+0x3f/0xf0
116 entry_SYSCALL_64_after_hwframe+0x6c/0x74
117 RIP: 0033:0x7f269055d034
118 Code: c7 00 16 00 00 00 b8 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 f3 0f 1e fa 80 3d 35 c3 0d 00 00 74 13 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 54 c3 0f 1f 00 48 83 ec 28 48 89 54 24 18 48
119 RSP: 002b:00007ffddb7ef748 EFLAGS: 00000202 ORIG_RAX: 0000000000000001
120 RAX: ffffffffffffffda RBX: 0000000000000007 RCX: 00007f269055d034
121 RDX: 0000000000000007 RSI: 000055bf5f4af3c0 RDI: 0000000000000001
122 RBP: 000055bf5f4af3c0 R08: 0000000000000073 R09: 0000000000000001
123 R10: 0000000000000000 R11: 0000000000000202 R12: 0000000000000007
124 R13: 00007f26906325c0 R14: 00007f269062ff20 R15: 0000000000000000
125 </TASK>
126
127 Fixes: d5e01266e7f5 ("leds: trigger: netdev: add additional specific link speed mode")
128 Cc: stable@vger.kernel.org
129 Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
130 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
131 Acked-by: Lee Jones <lee@kernel.org>
132 Link: https://lore.kernel.org/r/fb5c8294-2a10-4bf5-8f10-3d2b77d2757e@gmail.com
133 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
134 ---
135 drivers/leds/trigger/ledtrig-netdev.c | 11 +++++++----
136 1 file changed, 7 insertions(+), 4 deletions(-)
137
138 --- a/drivers/leds/trigger/ledtrig-netdev.c
139 +++ b/drivers/leds/trigger/ledtrig-netdev.c
140 @@ -235,6 +235,11 @@ static int set_device_name(struct led_ne
141 {
142 cancel_delayed_work_sync(&trigger_data->work);
143
144 + /*
145 + * Take RTNL lock before trigger_data lock to prevent potential
146 + * deadlock with netdev notifier registration.
147 + */
148 + rtnl_lock();
149 mutex_lock(&trigger_data->lock);
150
151 if (trigger_data->net_dev) {
152 @@ -254,16 +259,14 @@ static int set_device_name(struct led_ne
153 trigger_data->carrier_link_up = false;
154 trigger_data->link_speed = SPEED_UNKNOWN;
155 trigger_data->duplex = DUPLEX_UNKNOWN;
156 - if (trigger_data->net_dev != NULL) {
157 - rtnl_lock();
158 + if (trigger_data->net_dev)
159 get_device_state(trigger_data);
160 - rtnl_unlock();
161 - }
162
163 trigger_data->last_activity = 0;
164
165 set_baseline_state(trigger_data);
166 mutex_unlock(&trigger_data->lock);
167 + rtnl_unlock();
168
169 return 0;
170 }