Commit 47cfb83
bpf: avoid hashtab deadlock with try_lock
The commit 20b6cc3 ("bpf: Avoid hashtab deadlock with map_locked"),
try to fix deadlock, but in some case, the deadlock occurs:
* CPUn in task context with K1, and taking lock.
* CPUn interrupted by NMI context, with K2.
* They are using the same bucket, but different map_locked.
| Task
|
+---v----+
| CPUn |
+---^----+
|
| NMI
Anyway, the lockdep still warn:
[ 36.092222] ================================
[ 36.092230] WARNING: inconsistent lock state
[ 36.092234] 6.1.0-rc5+ #81 Tainted: G E
[ 36.092236] --------------------------------
[ 36.092237] inconsistent {INITIAL USE} -> {IN-NMI} usage.
[ 36.092238] perf/1515 [HC1[1]:SC0[0]:HE0:SE1] takes:
[ 36.092242] ffff888341acd1a0 (&htab->lockdep_key){....}-{2:2}, at: htab_lock_bucket+0x4d/0x58
[ 36.092253] {INITIAL USE} state was registered at:
[ 36.092255] mark_usage+0x1d/0x11d
[ 36.092262] __lock_acquire+0x3c9/0x6ed
[ 36.092266] lock_acquire+0x23d/0x29a
[ 36.092270] _raw_spin_lock_irqsave+0x43/0x7f
[ 36.092274] htab_lock_bucket+0x4d/0x58
[ 36.092276] htab_map_delete_elem+0x82/0xfb
[ 36.092278] map_delete_elem+0x156/0x1ac
[ 36.092282] __sys_bpf+0x138/0xb71
[ 36.092285] __do_sys_bpf+0xd/0x15
[ 36.092288] do_syscall_64+0x6d/0x84
[ 36.092291] entry_SYSCALL_64_after_hwframe+0x63/0xcd
[ 36.092295] irq event stamp: 120346
[ 36.092296] hardirqs last enabled at (120345): [<ffffffff8180b97f>] _raw_spin_unlock_irq+0x24/0x39
[ 36.092299] hardirqs last disabled at (120346): [<ffffffff81169e85>] generic_exec_single+0x40/0xb9
[ 36.092303] softirqs last enabled at (120268): [<ffffffff81c00347>] __do_softirq+0x347/0x387
[ 36.092307] softirqs last disabled at (120133): [<ffffffff810ba4f0>] __irq_exit_rcu+0x67/0xc6
[ 36.092311]
[ 36.092311] other info that might help us debug this:
[ 36.092312] Possible unsafe locking scenario:
[ 36.092312]
[ 36.092313] CPU0
[ 36.092313] ----
[ 36.092314] lock(&htab->lockdep_key);
[ 36.092315] <Interrupt>
[ 36.092316] lock(&htab->lockdep_key);
[ 36.092318]
[ 36.092318] *** DEADLOCK ***
[ 36.092318]
[ 36.092318] 3 locks held by perf/1515:
[ 36.092320] #0: ffff8881b9805cc0 (&cpuctx_mutex){+.+.}-{4:4}, at: perf_event_ctx_lock_nested+0x8e/0xba
[ 36.092327] #1: ffff8881075ecc20 (&event->child_mutex){+.+.}-{4:4}, at: perf_event_for_each_child+0x35/0x76
[ 36.092332] #2: ffff8881b9805c20 (&cpuctx_lock){-.-.}-{2:2}, at: perf_ctx_lock+0x12/0x27
[ 36.092339]
[ 36.092339] stack backtrace:
[ 36.092341] CPU: 0 PID: 1515 Comm: perf Tainted: G E 6.1.0-rc5+ #81
[ 36.092344] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014
[ 36.092349] Call Trace:
[ 36.092351] <NMI>
[ 36.092354] dump_stack_lvl+0x57/0x81
[ 36.092359] lock_acquire+0x1f4/0x29a
[ 36.092363] ? handle_pmi_common+0x13f/0x1f0
[ 36.092366] ? htab_lock_bucket+0x4d/0x58
[ 36.092371] _raw_spin_lock_irqsave+0x43/0x7f
[ 36.092374] ? htab_lock_bucket+0x4d/0x58
[ 36.092377] htab_lock_bucket+0x4d/0x58
[ 36.092379] htab_map_update_elem+0x11e/0x220
[ 36.092386] bpf_prog_f3a535ca81a8128a_bpf_prog2+0x3e/0x42
[ 36.092392] trace_call_bpf+0x177/0x215
[ 36.092398] perf_trace_run_bpf_submit+0x52/0xaa
[ 36.092403] ? x86_pmu_stop+0x97/0x97
[ 36.092407] perf_trace_nmi_handler+0xb7/0xe0
[ 36.092415] nmi_handle+0x116/0x254
[ 36.092418] ? x86_pmu_stop+0x97/0x97
[ 36.092423] default_do_nmi+0x3d/0xf6
[ 36.092428] exc_nmi+0xa1/0x109
[ 36.092432] end_repeat_nmi+0x16/0x67
[ 36.092436] RIP: 0010:wrmsrl+0xd/0x1b
[ 36.092441] Code: 04 01 00 00 c6 84 07 48 01 00 00 01 5b e9 46 15 80 00 5b c3 cc cc cc cc c3 cc cc cc cc 48 89 f2 89 f9 89 f0 48 c1 ea 20 0f 30 <66> 90 c3 cc cc cc cc 31 d2 e9 2f 04 49 00 0f 1f 44 00 00 40 0f6
[ 36.092443] RSP: 0018:ffffc900043dfc48 EFLAGS: 00000002
[ 36.092445] RAX: 000000000000000f RBX: ffff8881b96153e0 RCX: 000000000000038f
[ 36.092447] RDX: 0000000000000007 RSI: 000000070000000f RDI: 000000000000038f
[ 36.092449] RBP: 000000070000000f R08: ffffffffffffffff R09: ffff8881053bdaa8
[ 36.092451] R10: ffff8881b9805d40 R11: 0000000000000005 R12: ffff8881b9805c00
[ 36.092452] R13: 0000000000000000 R14: 0000000000000000 R15: ffff8881075ec970
[ 36.092460] ? wrmsrl+0xd/0x1b
[ 36.092465] ? wrmsrl+0xd/0x1b
[ 36.092469] </NMI>
[ 36.092469] <TASK>
[ 36.092470] __intel_pmu_enable_all.constprop.0+0x7c/0xaf
[ 36.092475] event_function+0xb6/0xd3
[ 36.092478] ? cpu_to_node+0x1a/0x1a
[ 36.092482] ? cpu_to_node+0x1a/0x1a
[ 36.092485] remote_function+0x1e/0x4c
[ 36.092489] generic_exec_single+0x48/0xb9
[ 36.092492] ? __lock_acquire+0x666/0x6ed
[ 36.092497] smp_call_function_single+0xbf/0x106
[ 36.092499] ? cpu_to_node+0x1a/0x1a
[ 36.092504] ? kvm_sched_clock_read+0x5/0x11
[ 36.092508] ? __perf_event_task_sched_in+0x13d/0x13d
[ 36.092513] cpu_function_call+0x47/0x69
[ 36.092516] ? perf_event_update_time+0x52/0x52
[ 36.092519] event_function_call+0x89/0x117
[ 36.092521] ? __perf_event_task_sched_in+0x13d/0x13d
[ 36.092526] ? _perf_event_disable+0x4a/0x4a
[ 36.092528] perf_event_for_each_child+0x3d/0x76
[ 36.092532] ? _perf_event_disable+0x4a/0x4a
[ 36.092533] _perf_ioctl+0x564/0x590
[ 36.092537] ? __lock_release+0xd5/0x1b0
[ 36.092543] ? perf_event_ctx_lock_nested+0x8e/0xba
[ 36.092547] perf_ioctl+0x42/0x5f
[ 36.092551] vfs_ioctl+0x1e/0x2f
[ 36.092554] __do_sys_ioctl+0x66/0x89
[ 36.092559] do_syscall_64+0x6d/0x84
[ 36.092563] entry_SYSCALL_64_after_hwframe+0x63/0xcd
[ 36.092566] RIP: 0033:0x7fe7110f362b
[ 36.092569] Code: 0f 1e fa 48 8b 05 5d b8 2c 00 64 c7 00 26 00 00 00 48 c7 c0 ff ff ff ff c3 66 0f 1f 44 00 00 f3 0f 1e fa b8 10 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 2d b8 2c 00 f7 d8 64 89 018
[ 36.092570] RSP: 002b:00007ffebb8e4b08 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
[ 36.092573] RAX: ffffffffffffffda RBX: 0000000000002400 RCX: 00007fe7110f362b
[ 36.092575] RDX: 0000000000000000 RSI: 0000000000002400 RDI: 0000000000000013
[ 36.092576] RBP: 00007ffebb8e4b40 R08: 0000000000000001 R09: 000055c1db4a5b40
[ 36.092577] R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
[ 36.092579] R13: 000055c1db3b2a30 R14: 0000000000000000 R15: 0000000000000000
[ 36.092586] </TASK>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Martin KaFai Lau <martin.lau@linux.dev>
Cc: Song Liu <song@kernel.org>
Cc: Yonghong Song <yhs@fb.com>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: KP Singh <kpsingh@kernel.org>
Cc: Stanislav Fomichev <sdf@google.com>
Cc: Hao Luo <haoluo@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Tonghao Zhang <xiangxia.m.yue@gmail.com>1 parent 62c6ffb commit 47cfb83
1 file changed
+36
-60
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | 83 | | |
87 | 84 | | |
88 | 85 | | |
| |||
104 | 101 | | |
105 | 102 | | |
106 | 103 | | |
107 | | - | |
108 | 104 | | |
109 | 105 | | |
110 | 106 | | |
| |||
146 | 142 | | |
147 | 143 | | |
148 | 144 | | |
149 | | - | |
150 | | - | |
| 145 | + | |
151 | 146 | | |
152 | 147 | | |
153 | 148 | | |
154 | 149 | | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
162 | 155 | | |
163 | 156 | | |
164 | | - | |
165 | 157 | | |
166 | | - | |
167 | 158 | | |
168 | 159 | | |
169 | 160 | | |
170 | | - | |
171 | | - | |
| 161 | + | |
172 | 162 | | |
173 | 163 | | |
174 | | - | |
175 | 164 | | |
176 | | - | |
177 | | - | |
178 | 165 | | |
179 | 166 | | |
180 | 167 | | |
| |||
467 | 454 | | |
468 | 455 | | |
469 | 456 | | |
470 | | - | |
| 457 | + | |
471 | 458 | | |
472 | 459 | | |
473 | 460 | | |
| |||
512 | 499 | | |
513 | 500 | | |
514 | 501 | | |
515 | | - | |
516 | | - | |
517 | | - | |
518 | | - | |
519 | | - | |
520 | | - | |
521 | | - | |
522 | | - | |
523 | | - | |
524 | 502 | | |
525 | 503 | | |
526 | 504 | | |
| |||
548 | 526 | | |
549 | 527 | | |
550 | 528 | | |
551 | | - | |
| 529 | + | |
552 | 530 | | |
553 | 531 | | |
554 | 532 | | |
555 | 533 | | |
556 | 534 | | |
557 | | - | |
| 535 | + | |
558 | 536 | | |
559 | 537 | | |
560 | 538 | | |
| |||
567 | 545 | | |
568 | 546 | | |
569 | 547 | | |
570 | | - | |
| 548 | + | |
571 | 549 | | |
572 | 550 | | |
573 | 551 | | |
574 | 552 | | |
575 | | - | |
| 553 | + | |
576 | 554 | | |
577 | 555 | | |
578 | 556 | | |
579 | 557 | | |
580 | 558 | | |
581 | 559 | | |
582 | 560 | | |
583 | | - | |
| 561 | + | |
584 | 562 | | |
585 | 563 | | |
586 | | - | |
587 | | - | |
| 564 | + | |
588 | 565 | | |
589 | 566 | | |
590 | 567 | | |
| |||
781 | 758 | | |
782 | 759 | | |
783 | 760 | | |
784 | | - | |
| 761 | + | |
785 | 762 | | |
786 | 763 | | |
787 | 764 | | |
| |||
792 | 769 | | |
793 | 770 | | |
794 | 771 | | |
795 | | - | |
| 772 | + | |
796 | 773 | | |
797 | 774 | | |
798 | 775 | | |
| |||
1106 | 1083 | | |
1107 | 1084 | | |
1108 | 1085 | | |
1109 | | - | |
| 1086 | + | |
1110 | 1087 | | |
1111 | 1088 | | |
1112 | 1089 | | |
| |||
1151 | 1128 | | |
1152 | 1129 | | |
1153 | 1130 | | |
1154 | | - | |
| 1131 | + | |
1155 | 1132 | | |
1156 | 1133 | | |
1157 | 1134 | | |
| |||
1197 | 1174 | | |
1198 | 1175 | | |
1199 | 1176 | | |
1200 | | - | |
| 1177 | + | |
1201 | 1178 | | |
1202 | 1179 | | |
1203 | 1180 | | |
| |||
1218 | 1195 | | |
1219 | 1196 | | |
1220 | 1197 | | |
1221 | | - | |
| 1198 | + | |
1222 | 1199 | | |
1223 | 1200 | | |
1224 | 1201 | | |
| |||
1254 | 1231 | | |
1255 | 1232 | | |
1256 | 1233 | | |
1257 | | - | |
| 1234 | + | |
1258 | 1235 | | |
1259 | 1236 | | |
1260 | 1237 | | |
| |||
1279 | 1256 | | |
1280 | 1257 | | |
1281 | 1258 | | |
1282 | | - | |
| 1259 | + | |
1283 | 1260 | | |
1284 | 1261 | | |
1285 | 1262 | | |
| |||
1320 | 1297 | | |
1321 | 1298 | | |
1322 | 1299 | | |
1323 | | - | |
| 1300 | + | |
1324 | 1301 | | |
1325 | 1302 | | |
1326 | 1303 | | |
| |||
1344 | 1321 | | |
1345 | 1322 | | |
1346 | 1323 | | |
1347 | | - | |
| 1324 | + | |
1348 | 1325 | | |
1349 | 1326 | | |
1350 | 1327 | | |
| |||
1383 | 1360 | | |
1384 | 1361 | | |
1385 | 1362 | | |
1386 | | - | |
| 1363 | + | |
1387 | 1364 | | |
1388 | 1365 | | |
1389 | 1366 | | |
| |||
1396 | 1373 | | |
1397 | 1374 | | |
1398 | 1375 | | |
1399 | | - | |
| 1376 | + | |
1400 | 1377 | | |
1401 | 1378 | | |
1402 | 1379 | | |
| |||
1419 | 1396 | | |
1420 | 1397 | | |
1421 | 1398 | | |
1422 | | - | |
| 1399 | + | |
1423 | 1400 | | |
1424 | 1401 | | |
1425 | 1402 | | |
| |||
1430 | 1407 | | |
1431 | 1408 | | |
1432 | 1409 | | |
1433 | | - | |
| 1410 | + | |
1434 | 1411 | | |
1435 | 1412 | | |
1436 | 1413 | | |
| |||
1493 | 1470 | | |
1494 | 1471 | | |
1495 | 1472 | | |
1496 | | - | |
1497 | 1473 | | |
1498 | 1474 | | |
1499 | 1475 | | |
| |||
1515 | 1491 | | |
1516 | 1492 | | |
1517 | 1493 | | |
| 1494 | + | |
1518 | 1495 | | |
1519 | 1496 | | |
1520 | | - | |
1521 | | - | |
| 1497 | + | |
1522 | 1498 | | |
1523 | 1499 | | |
1524 | 1500 | | |
| |||
1562 | 1538 | | |
1563 | 1539 | | |
1564 | 1540 | | |
1565 | | - | |
| 1541 | + | |
1566 | 1542 | | |
1567 | 1543 | | |
1568 | 1544 | | |
| |||
1600 | 1576 | | |
1601 | 1577 | | |
1602 | 1578 | | |
1603 | | - | |
| 1579 | + | |
1604 | 1580 | | |
1605 | 1581 | | |
1606 | 1582 | | |
| |||
1718 | 1694 | | |
1719 | 1695 | | |
1720 | 1696 | | |
1721 | | - | |
| 1697 | + | |
1722 | 1698 | | |
1723 | 1699 | | |
1724 | 1700 | | |
| |||
1741 | 1717 | | |
1742 | 1718 | | |
1743 | 1719 | | |
1744 | | - | |
| 1720 | + | |
1745 | 1721 | | |
1746 | 1722 | | |
1747 | 1723 | | |
| |||
1752 | 1728 | | |
1753 | 1729 | | |
1754 | 1730 | | |
1755 | | - | |
| 1731 | + | |
1756 | 1732 | | |
1757 | 1733 | | |
1758 | 1734 | | |
| |||
1813 | 1789 | | |
1814 | 1790 | | |
1815 | 1791 | | |
1816 | | - | |
| 1792 | + | |
1817 | 1793 | | |
1818 | 1794 | | |
1819 | 1795 | | |
| |||
0 commit comments