| 0/0 |
2026/11/19 01:33 |
flow |
bisect-cause |
58m
Results: map[BisectReasoning:### Root Cause Analysis
The crash is a false-positive `BUG: workqueue leaked atomic, lock or RCU` check in `process_one_work()` (`kernel/workqueue.c`):
```
BUG: workqueue leaked atomic, lock or RCU: syz-executor[6626]
preempt=0x00000000 lock=4->0 RCU=2->2 workfn=usb_giveback_urb_bh
INFO: lockdep is turned off.
```
Looking at the reported values:
- `preempt=0x00000000` (and `worker->task == NULL` for a BH worker): no atomic context leak.
- `RCU=2->2`: `rcu_preempt_depth()` before and after the work item is unchanged (`2`).
- `lock=4->0` followed by `INFO: lockdep is turned off.`: `lockdep_start_depth` was `4`, and `lockdep_depth(current)` after the work item evaluated to `0` because `debug_locks` became `0`.
In `include/linux/lockdep.h:279`, `lockdep_depth()` is defined as:
```c
#define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
```
(This definition was introduced in commit `e3a55fd18deab` ("[PATCH] lockdep: lockdep_depth vs. debug_locks") specifically so that when lockdep turns itself off mid-workfn and stops decrementing `tsk->lockdep_depth` on unlock, `lockdep_depth(current)` returns `0` and did not trigger the old `lockdep_depth(current) > 0` leak check in workqueues.)
In `kernel/workqueue.c:3466-3517`, `process_one_work()` does:
```c
rcu_start_depth = rcu_preempt_depth();
lockdep_start_depth = lockdep_depth(current);
...
worker->current_func(work);
...
if (unlikely((worker->task && in_atomic()) ||
lockdep_depth(current) != lockdep_start_depth ||
rcu_preempt_depth() != rcu_start_depth)) {
pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n"
" preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n", ...);
debug_show_held_locks(current);
dump_stack();
}
```
Here, `syz-executor` is executing `rtnetlink_rcv_msg()` -> ... -> `__alloc_skb()` -> `local_bh_enable()` -> `handle_softirqs()` -> `tasklet_hi_action()` -> `bh_worker()` -> `process_one_work()` while legitimately holding 4 locks in `current` (such as `rtnl_mutex`, `rcu_lock_map`, BH lock, and `pool->cb_lock` on `PREEMPT_RT`).
1. At the start of `process_one_work()`, `debug_locks` is `1`, so `lockdep_start_depth = lockdep_depth(current)` records `4`.
2. While `usb_giveback_urb_bh` executes (or concurrently on another CPU), lockdep disables itself (`debug_locks = 0` via `debug_locks_off()`, e.g. upon hitting a non-tainting lockdep resource limit such as `MAX_LOCKDEP_*` / `MAX_LOCK_DEPTH` / `MAX_STACK_TRACE_ENTRIES`, which syzkaller ignores, or a concurrent lockdep disable on another CPU).
3. After `worker->current_func(work)` finishes, `lockdep_depth(current)` evaluates `(debug_locks ? current->lockdep_depth : 0)` -> `0`.
4. Because `process_one_work()` checks `lockdep_depth(current) != lockdep_start_depth` without checking `debug_locks`, `0 != 4` evaluates to true and triggers the bogus `BUG: workqueue leaked atomic, lock or RCU` (`lock=4->0`), immediately followed by `debug_show_held_locks(current)` printing `INFO: lockdep is turned off.`.
### Bisection Log
1. **`git blame kernel/workqueue.c` (lines 3466-3517)**:
Traced `lockdep_start_depth = lockdep_depth(current)` and `lockdep_depth(current) != lockdep_start_depth` directly to commit **`c35aea39d1e106f61fd2130f0d32a3bac8bd4570`** ("workqueue: Update lock debugging code").
- Prior to `c35aea39d1e10`, `process_one_work()` checked `lockdep_depth(current) > 0`. When `debug_locks` became `0`, `lockdep_depth(current)` returned `0`, so `0 > 0` was false and no false positive occurred (matching the design of `e3a55fd18deab` ("[PATCH] lockdep: lockdep_depth vs. debug_locks")).
- `c35aea39d1e10` changed this check to `lockdep_depth(current) != lockdep_start_depth` in preparation for BH workqueues (where `lockdep_start_depth` can be non-zero), overlooking that `lockdep_depth(current)` collapses to `0` when `debug_locks` becomes `0`, which causes `0 != lockdep_start_depth` whenever `lockdep_start_depth > 0`.
2. **Other commits examined and ruled out as secondary/enabling**:
- `e3a55fd18deab758a2970e0dfcd60a677a920426` ("[PATCH] lockdep: lockdep_depth vs. debug_locks"): Historical commit from 2007 defining `lockdep_depth(tsk)` to return `0` when `!debug_locks`; this worked with `lockdep_depth(current) > 0` until `c35aea39d1e10` changed the comparison to `!= lockdep_start_depth`.
- `4cb1ef64609f9b0254184b2947824f4b46ccab22` ("workqueue: Implement BH workqueues to eventually replace tasklets"): Follow-up commit in the same series that added `bh_worker()`, enabling `process_one_work()` to run in softirq/BH context with `lockdep_start_depth > 0`.
- `8fea0c8fda30129b4168464975505d5dc9735ac1` ("usb: core: hcd: Convert from tasklet to BH workqueue"): Converted `usb_giveback_urb_bh` to run on `system_bh[_highpri]_wq`, merely acting as the BH work item executing when `debug_locks` turned off.
- `ad7c7f4b9c6c2950778e5bd305392a333de73912` ("workqueue: Provide a handshake for canceling BH workers") and `6eca8f94d84106d3754b9df27f46a14572af9e7f` ("workqueue: Annotate cb_lock nesting when draining a dead BH pool"): Added `pool->cb_lock` acquisition in `bh_worker()` on `PREEMPT_RT`, contributing one of the held locks in `lockdep_start_depth` but not introducing the faulty `!debug_locks` check.
- Recent lockdep commits (`02c6be7d675b` "locking/lockdep: Invalidate stale class_cache entries for zapped classes", `8f2109843137` "locking/lockdep: add sequence counter to held_lock"): Inspected and confirmed they do not erroneously clear `debug_locks`.
### Conclusion
The bug was introduced by commit **`c35aea39d1e106f61fd2130f0d32a3bac8bd4570`** ("workqueue: Update lock debugging code"), which changed `process_one_work()` from `lockdep_depth(current) > 0` to `lockdep_depth(current) != lockdep_start_depth` without guarding the comparison with `debug_locks`. Commits:[map[AuthorEmail:tj@kernel.org AuthorName:Tejun Heo Date:2024-02-04T11:28:06-10:00 Hash:c35aea39d1e106f61fd2130f0d32a3bac8bd4570 Recipients:[map[Email:allen.lkml@gmail.com Name: To:true] map[Email:tj@kernel.org Name: To:true]] Title:workqueue: Update lock debugging code]] KernelCommit:20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca]
|
| 1/1 |
2026/11/19 01:33 |
action |
use-crash-commit |
0m
Results: map[KernelCommit:20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca]
|
| 2/1 |
2026/11/19 01:33 |
action |
syz-repro-to-c-repro |
0m
Results:
|
| 3/1 |
2026/11/19 01:33 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/app/workdir/cache/src/65d1e97cf6f053be32832df4bbc2692d51afc3cc/data]
|
| 4/1 |
2026/11/19 01:33 |
action |
kernel-builder |
17m
Results: map[KernelObj:/app/workdir/cache/build/9736662a13399a66f20ec42d5b77936965421306/data]
|
| 5/1 |
2026/11/19 01:50 |
action |
codesearch-prepare |
13m
Results: map[Index:codesearch-index]
|
| 6/1 |
2026/11/19 02:04 |
agent |
cause-finder |
27m
Model:
Results: map[CandidateCommits:[c35aea39d1e106f61fd2130f0d32a3bac8bd4570]]
Instruction:
You are an experienced Linux kernel developer tasked with identifying the commit
that introduced a kernel bug (cause bisection).
Your investigation strategy:
1. Establish the root cause first.
Analyze the crash report and the code it points to (stack traces, allocation/free stacks,
the involved functions and structs) until you understand the exact invariant that is
violated and the exact execution sequence that leads to the crash.
Identify the specific functions, structs, invariants, or lines of code where the buggy
logic resides.
2. Use git tools (git-blame, git-log, git-show) to trace the history
of the affected functions and lines.
3. Trace past pure refactorings, code movements, formatting changes, or variable renames
to find the commit that actually introduced the buggy logic or broke an existing invariant.
4. Do NOT blame commits that merely added debugging or sanitizer checks (WARN_ON, BUG_ON,
KASAN/KMSAN/LOCKDEP annotations) that exposed an older bug, unless the check itself is
incorrect and is the bug.
5. Do NOT return a merge commit unless the bug was introduced by the merge conflict
resolution itself; otherwise identify the underlying non-merge commit.
6. Verify your hypothesis by inspecting the full commit diff with git-show and
confirming how the change introduced the exact condition described in the root cause.
Output rules:
- Returning a SINGLE commit in CandidateCommits (a 1-element list) is the expected norm
and is strongly preferred. A single commit gives maintainers a clear, directly actionable
result, whereas returning extra commits dilutes the report and notifies additional commit
authors unnecessarily.
- Once you identify the primary commit that introduced (or fixed) the bug, return ONLY that
commit in CandidateCommits. Do NOT include secondary or "contributing" commits such as
earlier background/foundation commits, preparatory or follow-up commits in the same
series, later refactorings, incomplete fix attempts, or commits that merely exposed or
enabled the code path (e.g. new callers, default config changes, or sanitizer/debug checks).
Mention those related commits in your text reply for context instead.
- Returning more than one commit (at most 3) is the rare exception, not the norm. Only return
multiple commits when:
1. There is genuine, unresolvable ambiguity between 2-3 equally plausible candidates after
thorough investigation, OR
2. The bug was strictly and inseparably introduced (or fixed) by 2-3 commits together, with
no single primary commit.
In those rare cases, order CandidateCommits by relevance with the most important/likely
commit FIRST.
- If you cannot determine the commit with confidence, return an empty CandidateCommits list
rather than guessing.
- Your text reply must provide a clear bisection log explaining the root cause of the bug,
the git history you traced, the commits you examined (with their hashes and titles), why
you ruled out other commits, and how you reached your conclusion.
Don't make assumptions about the kernel source code (it may be different from what you assume it is).
Extensively use the provided code access tools (codesearch-*, git-*, grepper, etc)
to examine the actual source code, and confirm any assumptions.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt:
The crash is:
BUG: workqueue leaked atomic, lock or RCU: syz-executor[6626]
preempt=0x00000000 lock=4->0 RCU=2->2 workfn=usb_giveback_urb_bh
INFO: lockdep is turned off.
CPU: 0 UID: 0 PID: 6626 Comm: syz-executor Not tainted syzkaller #0 PREEMPT_{RT,(full)}
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
Call Trace:
<TASK>
dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120
process_one_work kernel/workqueue.c:3516 [inline]
process_scheduled_works+0x12c2/0x1630 kernel/workqueue.c:3578
bh_worker+0x4b3/0xbb0 kernel/workqueue.c:3885
tasklet_hi_action+0xf/0x70 kernel/softirq.c:1028
handle_softirqs+0x1d4/0x6c0 kernel/softirq.c:669
__do_softirq kernel/softirq.c:703 [inline]
__local_bh_enable_ip+0x16f/0x2b0 kernel/softirq.c:325
local_bh_enable include/linux/bottom_half.h:33 [inline]
__alloc_skb+0x1b1/0x7a0 net/core/skbuff.c:700
alloc_skb include/linux/skbuff.h:1390 [inline]
nlmsg_new include/net/netlink.h:1055 [inline]
rtmsg_fib+0xda/0x440 net/ipv4/fib_semantics.c:584
fib_table_insert+0xd54/0x1b10 net/ipv4/fib_trie.c:1381
fib_magic+0x43b/0x530 net/ipv4/fib_frontend.c:1147
fib_add_ifaddr+0x376/0x5d0 net/ipv4/fib_frontend.c:1184
fib_netdev_event+0x382/0x490 net/ipv4/fib_frontend.c:1529
notifier_call_chain+0x1a5/0x3d0 kernel/notifier.c:86
call_netdevice_notifiers_extack net/core/dev.c:2351 [inline]
call_netdevice_notifiers net/core/dev.c:2365 [inline]
__dev_notify_flags+0x31e/0x510 net/core/dev.c:10063
netif_change_flags+0xde/0x1b0 net/core/dev.c:10092
do_setlink+0xdd6/0x47a0 net/core/rtnetlink.c:3294
rtnl_changelink net/core/rtnetlink.c:3942 [inline]
__rtnl_newlink net/core/rtnetlink.c:4113 [inline]
rtnl_newlink+0x15b0/0x1c30 net/core/rtnetlink.c:4252
rtnetlink_rcv_msg+0x802/0xc00 net/core/rtnetlink.c:7169
netlink_rcv_skb+0x226/0x4a0 net/netlink/af_netlink.c:2585
netlink_unicast_kernel net/netlink/af_netlink.c:1336 [inline]
netlink_unicast+0x90c/0xac0 net/netlink/af_netlink.c:1362
netlink_sendmsg+0x7f0/0xb20 net/netlink/af_netlink.c:1921
sock_sendmsg_nosec+0x14e/0x190 net/socket.c:800
__sock_sendmsg net/socket.c:818 [inline]
__sys_sendto+0x41e/0x5d0 net/socket.c:2283
__do_sys_sendto net/socket.c:2290 [inline]
__se_sys_sendto net/socket.c:2286 [inline]
__x64_sys_sendto+0xde/0x100 net/socket.c:2286
do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
do_syscall_64+0x171/0x540 arch/x86/entry/syscall_64.c:89
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7fa34ec39d8e
Code: 08 0f 85 a5 a8 ff ff 49 89 fb 48 89 f0 48 89 d7 48 89 ce 4c 89 c2 4d 89 ca 4c 8b 44 24 08 4c 8b 4c 24 10 4c 89 5c 24 08 0f 05 <c3> 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 80 00 00 00 00 48 83 ec 08
RSP: 002b:00007fff60930748 EFLAGS: 00000246 ORIG_RAX: 000000000000002c
RAX: ffffffffffffffda RBX: 000055557a5b1500 RCX: 00007fa34ec39d8e
RDX: 000000000000002c RSI: 00007fa34fa24670 RDI: 0000000000000003
RBP: 0000000000000001 R08: 00007fff609307c4 R09: 000000000000000c
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000003
R13: 0000000000000000 R14: 00007fa34fa24670 R15: 0000000000000000
</TASK>
Search for the commit that introduced this bug.
Reply: ### Root Cause Analysis
The crash is a false-positive `BUG: workqueue leaked atomic, lock or RCU` check in `process_one_work()` (`kernel/workqueue.c`):
```
BUG: workqueue leaked atomic, lock or RCU: syz-executor[6626]
preempt=0x00000000 lock=4->0 RCU=2->2 workfn=usb_giveback_urb_bh
INFO: lockdep is turned off.
```
Looking at the reported values:
- `preempt=0x00000000` (and `worker->task == NULL` for a BH worker): no atomic context leak.
- `RCU=2->2`: `rcu_preempt_depth()` before and after the work item is unchanged (`2`).
- `lock=4->0` followed by `INFO: lockdep is turned off.`: `lockdep_start_depth` was `4`, and `lockdep_depth(current)` after the work item evaluated to `0` because `debug_locks` became `0`.
In `include/linux/lockdep.h:279`, `lockdep_depth()` is defined as:
```c
#define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
```
(This definition was introduced in commit `e3a55fd18deab` ("[PATCH] lockdep: lockdep_depth vs. debug_locks") specifically so that when lockdep turns itself off mid-workfn and stops decrementing `tsk->lockdep_depth` on unlock, `lockdep_depth(current)` returns `0` and did not trigger the old `lockdep_depth(current) > 0` leak check in workqueues.)
In `kernel/workqueue.c:3466-3517`, `process_one_work()` does:
```c
rcu_start_depth = rcu_preempt_depth();
lockdep_start_depth = lockdep_depth(current);
...
worker->current_func(work);
...
if (unlikely((worker->task && in_atomic()) ||
lockdep_depth(current) != lockdep_start_depth ||
rcu_preempt_depth() != rcu_start_depth)) {
pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n"
" preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n", ...);
debug_show_held_locks(current);
dump_stack();
}
```
Here, `syz-executor` is executing `rtnetlink_rcv_msg()` -> ... -> `__alloc_skb()` -> `local_bh_enable()` -> `handle_softirqs()` -> `tasklet_hi_action()` -> `bh_worker()` -> `process_one_work()` while legitimately holding 4 locks in `current` (such as `rtnl_mutex`, `rcu_lock_map`, BH lock, and `pool->cb_lock` on `PREEMPT_RT`).
1. At the start of `process_one_work()`, `debug_locks` is `1`, so `lockdep_start_depth = lockdep_depth(current)` records `4`.
2. While `usb_giveback_urb_bh` executes (or concurrently on another CPU), lockdep disables itself (`debug_locks = 0` via `debug_locks_off()`, e.g. upon hitting a non-tainting lockdep resource limit such as `MAX_LOCKDEP_*` / `MAX_LOCK_DEPTH` / `MAX_STACK_TRACE_ENTRIES`, which syzkaller ignores, or a concurrent lockdep disable on another CPU).
3. After `worker->current_func(work)` finishes, `lockdep_depth(current)` evaluates `(debug_locks ? current->lockdep_depth : 0)` -> `0`.
4. Because `process_one_work()` checks `lockdep_depth(current) != lockdep_start_depth` without checking `debug_locks`, `0 != 4` evaluates to true and triggers the bogus `BUG: workqueue leaked atomic, lock or RCU` (`lock=4->0`), immediately followed by `debug_show_held_locks(current)` printing `INFO: lockdep is turned off.`.
### Bisection Log
1. **`git blame kernel/workqueue.c` (lines 3466-3517)**:
Traced `lockdep_start_depth = lockdep_depth(current)` and `lockdep_depth(current) != lockdep_start_depth` directly to commit **`c35aea39d1e106f61fd2130f0d32a3bac8bd4570`** ("workqueue: Update lock debugging code").
- Prior to `c35aea39d1e10`, `process_one_work()` checked `lockdep_depth(current) > 0`. When `debug_locks` became `0`, `lockdep_depth(current)` returned `0`, so `0 > 0` was false and no false positive occurred (matching the design of `e3a55fd18deab` ("[PATCH] lockdep: lockdep_depth vs. debug_locks")).
- `c35aea39d1e10` changed this check to `lockdep_depth(current) != lockdep_start_depth` in preparation for BH workqueues (where `lockdep_start_depth` can be non-zero), overlooking that `lockdep_depth(current)` collapses to `0` when `debug_locks` becomes `0`, which causes `0 != lockdep_start_depth` whenever `lockdep_start_depth > 0`.
2. **Other commits examined and ruled out as secondary/enabling**:
- `e3a55fd18deab758a2970e0dfcd60a677a920426` ("[PATCH] lockdep: lockdep_depth vs. debug_locks"): Historical commit from 2007 defining `lockdep_depth(tsk)` to return `0` when `!debug_locks`; this worked with `lockdep_depth(current) > 0` until `c35aea39d1e10` changed the comparison to `!= lockdep_start_depth`.
- `4cb1ef64609f9b0254184b2947824f4b46ccab22` ("workqueue: Implement BH workqueues to eventually replace tasklets"): Follow-up commit in the same series that added `bh_worker()`, enabling `process_one_work()` to run in softirq/BH context with `lockdep_start_depth > 0`.
- `8fea0c8fda30129b4168464975505d5dc9735ac1` ("usb: core: hcd: Convert from tasklet to BH workqueue"): Converted `usb_giveback_urb_bh` to run on `system_bh[_highpri]_wq`, merely acting as the BH work item executing when `debug_locks` turned off.
- `ad7c7f4b9c6c2950778e5bd305392a333de73912` ("workqueue: Provide a handshake for canceling BH workers") and `6eca8f94d84106d3754b9df27f46a14572af9e7f` ("workqueue: Annotate cb_lock nesting when draining a dead BH pool"): Added `pool->cb_lock` acquisition in `bh_worker()` on `PREEMPT_RT`, contributing one of the held locks in `lockdep_start_depth` but not introducing the faulty `!debug_locks` check.
- Recent lockdep commits (`02c6be7d675b` "locking/lockdep: Invalidate stale class_cache entries for zapped classes", `8f2109843137` "locking/lockdep: add sequence counter to held_lock"): Inspected and confirmed they do not erroneously clear `debug_locks`.
### Conclusion
The bug was introduced by commit **`c35aea39d1e106f61fd2130f0d32a3bac8bd4570`** ("workqueue: Update lock debugging code"), which changed `process_one_work()` from `lockdep_depth(current) > 0` to `lockdep_depth(current) != lockdep_start_depth` without guarding the comparison with `debug_locks`.
|
| 7/2 |
2026/11/19 02:04 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 6393
output: 143
thoughts: 776
Thoughts: **Decoding a Workqueue Leak: A Deep Dive into Lock States and Interrupts**
My immediate reaction upon seeing this crash report was a surge of analytical focus. The core of the issue, `BUG: workqueue leaked atomic, lock or RCU: syz-executor[6626]`, immediately signals a critical failure in how kernel synchronization primitives are being managed. The associated state values are particularly intriguing: `preempt=0x00000000 lock=4->0 RCU=2->2 workfn=usb_giveback_urb_bh`. And then, the line `INFO: lockdep is turned off.` – that's a major red flag, an indicator that something significant has just happened, potentially obscuring the root cause.
My first thought is to interrogate the `lock=4->0` transition. The fact that the `lockdep_depth(current)` (I'm assuming `lock` refers to this given the context) was 4 *before* the `process_one_work` function, and then dropped to 0 *after*, coupled with `lockdep` being turned off, demands a thorough investigation. This isn't a simple increment/decrement; it suggests a drastic change in lock acquisition state or even a forced disabling of lock tracking.
Several possibilities immediately spring to mind:
* **Was `lockdep` disabled *during* the execution of `usb_giveback_urb_bh`?** This would explain why we don't see a lockdep warning *before* the crash. But why would it be disabled? A critical error like a lockdep warning itself can trigger `debug_locks_off()`, but then I'd expect to see that warning logged.
* **Did something else modify `lockdep_depth` directly?** This seems less likely as a standard kernel operation, but not impossible if a debugging feature or an unusual kernel path is involved.
* **Could `bh_worker` running in a softirq context have interrupted a task that was holding four locks?** This is a strong contender. If a tasklet (running in softirq context, hence `tasklet_hi_action` in the call trace) preempted a process that had acquired four locks, and then the workqueue processing that led to the leak occurred, it could explain the dramatic drop in `lockdep_depth`. The `local_bh_enable` in the call trace further supports the idea of bottom halves being enabled, which aligns with softirq processing.
The call trace itself points us to key areas: `kernel/workqueue.c` (lines 3516, 3578, 3885), `kernel/softirq.c` (lines 1028, 669, 703, 325), and `include/linux/bottom_half.h` (line 33), and crucially, `net/core/skbuff.c` (line 700) which is where `__alloc_skb` is found. The `usb_giveback_urb_bh` function is the specific work function identified.
My next steps are clear: I need to dive deep into the source code of these functions. I'll be meticulously examining the logic within `process_one_work`, the role of `bh_worker` in handling softirqs and workqueues, and the specifics of `tasklet_hi_action`. Crucially, I need to understand how `usb_giveback_urb_bh` interacts with the locking mechanisms and how it could lead to a situation where a workqueue item is left "leaked" with atomic, lock, or RCU context still active, especially considering the dramatic `lock=4->0` transition and the disabling of lockdep. I'll also be looking for any code paths that might conditionally disable `lockdep` or manipulate `lockdep_depth` directly. This is a complex interaction, and I'm going to be running multiple investigative tools in parallel to try and piece together this interlocking puzzle.
|
| 8/2 |
2026/11/19 02:04 |
tool |
read-file |
0m
Args: map[File:kernel/workqueue.c FirstLine:3450 LineCount:100]
Results: map[Contents:3450: /*
3451: * Record the last pool and clear PENDING which should be the last
3452: * update to @work. Also, do this inside @pool->lock so that
3453: * PENDING and queued state changes happen together while IRQ is
3454: * disabled.
3455: */
3456: set_work_pool_and_clear_pending(work, pool->id, pool_offq_flags(pool));
3457:
3458: pwq->stats[PWQ_STAT_STARTED]++;
3459:
3460: rcu_read_lock();
3461: raw_spin_unlock_irq(&pool->lock);
3462: if (wake_task)
3463: wake_up_process(wake_task);
3464: rcu_read_unlock();
3465:
3466: rcu_start_depth = rcu_preempt_depth();
3467: lockdep_start_depth = lockdep_depth(current);
3468: /* see drain_dead_softirq_workfn() */
3469: if (!bh_draining)
3470: lock_map_acquire(pwq->wq->lockdep_map);
3471: lock_map_acquire(&lockdep_map);
3472: /*
3473: * Strictly speaking we should mark the invariant state without holding
3474: * any locks, that is, before these two lock_map_acquire()'s.
3475: *
3476: * However, that would result in:
3477: *
3478: * A(W1)
3479: * WFC(C)
3480: * A(W1)
3481: * C(C)
3482: *
3483: * Which would create W1->C->W1 dependencies, even though there is no
3484: * actual deadlock possible. There are two solutions, using a
3485: * read-recursive acquire on the work(queue) 'locks', but this will then
3486: * hit the lockdep limitation on recursive locks, or simply discard
3487: * these locks.
3488: *
3489: * AFAICT there is no possible deadlock scenario between the
3490: * flush_work() and complete() primitives (except for single-threaded
3491: * workqueues), so hiding them isn't a problem.
3492: */
3493: lockdep_invariant_state(true);
3494: trace_workqueue_execute_start(work);
3495: worker->current_func(work);
3496: /*
3497: * While we must be careful to not use "work" after this, the trace
3498: * point will only record its address.
3499: */
3500: trace_workqueue_execute_end(work, worker->current_func);
3501:
3502: lock_map_release(&lockdep_map);
3503: if (!bh_draining)
3504: lock_map_release(pwq->wq->lockdep_map);
3505:
3506: if (unlikely((worker->task && in_atomic()) ||
3507: lockdep_depth(current) != lockdep_start_depth ||
3508: rcu_preempt_depth() != rcu_start_depth)) {
3509: pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n"
3510: " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n",
3511: current->comm, task_pid_nr(current), preempt_count(),
3512: lockdep_start_depth, lockdep_depth(current),
3513: rcu_start_depth, rcu_preempt_depth(),
3514: worker->current_func);
3515: debug_show_held_locks(current);
3516: dump_stack();
3517: }
3518:
3519: /*
3520: * The following prevents a kworker from hogging CPU on !PREEMPTION
3521: * kernels, where a requeueing work item waiting for something to
3522: * happen could deadlock with stop_machine as such work item could
3523: * indefinitely requeue itself while all other CPUs are trapped in
3524: * stop_machine. At the same time, report a quiescent RCU state so
3525: * the same condition doesn't freeze RCU.
3526: */
3527: if (worker->task)
3528: cond_resched();
3529:
3530: raw_spin_lock_irq(&pool->lock);
3531:
3532: pwq->stats[PWQ_STAT_COMPLETED]++;
3533:
3534: /*
3535: * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked
3536: * CPU intensive by wq_worker_tick() if @work hogged CPU longer than
3537: * wq_cpu_intensive_thresh_us. Clear it.
3538: */
3539: worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
3540:
3541: /* tag the worker for identification in schedule() */
3542: worker->last_func = worker->current_func;
3543:
3544: /* we're done with it, release */
3545: hash_del(&worker->hentry);
3546: worker->current_work = NULL;
3547: worker->current_func = NULL;
3548: worker->current_pwq = NULL;
3549: worker->current_color = INT_MAX;
]
|
| 9/2 |
2026/11/19 02:04 |
tool |
read-file |
0m
Args: map[File:kernel/workqueue.c FirstLine:3840 LineCount:80]
Results: map[Contents:3840: }
3841:
3842: raw_spin_unlock_irq(&wq_mayday_lock);
3843:
3844: if (should_stop) {
3845: __set_current_state(TASK_RUNNING);
3846: set_pf_worker(false);
3847: return 0;
3848: }
3849:
3850: /* rescuers should never participate in concurrency management */
3851: WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
3852: schedule();
3853: goto repeat;
3854: }
3855:
3856: static void bh_worker(struct worker *worker)
3857: {
3858: struct worker_pool *pool = worker->pool;
3859: int nr_restarts = BH_WORKER_RESTARTS;
3860: unsigned long end = jiffies + BH_WORKER_JIFFIES;
3861: bool budget_exhausted = false;
3862: bool timeout = false;
3863: int restarts = 0;
3864:
3865: worker_lock_callback(pool);
3866: raw_spin_lock_irq(&pool->lock);
3867: worker_leave_idle(worker);
3868:
3869: /*
3870: * This function follows the structure of worker_thread(). See there for
3871: * explanations on each step.
3872: */
3873: if (!need_more_worker(pool))
3874: goto done;
3875:
3876: WARN_ON_ONCE(!list_empty(&worker->scheduled));
3877: worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
3878:
3879: do {
3880: struct work_struct *work =
3881: list_first_entry(&pool->worklist,
3882: struct work_struct, entry);
3883:
3884: if (assign_work(work, worker, NULL))
3885: process_scheduled_works(worker);
3886:
3887: if (!keep_working(pool))
3888: break;
3889:
3890: if (!--nr_restarts) {
3891: budget_exhausted = true;
3892: break;
3893: }
3894:
3895: if (!time_before(jiffies, end)) {
3896: budget_exhausted = true;
3897: timeout = true;
3898: break;
3899: }
3900:
3901: restarts++;
3902: } while (1);
3903:
3904: worker_set_flags(worker, WORKER_PREP);
3905: done:
3906: worker_enter_idle(worker);
3907: kick_pool(pool);
3908: raw_spin_unlock_irq(&pool->lock);
3909: worker_unlock_callback(pool);
3910:
3911: if (budget_exhausted)
3912: trace_workqueue_bh_budget_yield(pool, restarts, timeout,
3913: pool->attrs->nice == HIGHPRI_NICE_LEVEL);
3914: }
3915:
3916: /*
3917: * TODO: Convert all tasklet users to workqueue and use softirq directly.
3918: *
3919: * This is currently called from tasklet[_hi]action() and thus is also called
]
|
| 10/2 |
2026/11/19 02:04 |
tool |
read-file |
0m
Args: map[File:kernel/softirq.c FirstLine:990 LineCount:70]
Results: map[Contents: 990: if (tasklet_trylock(t)) {
991: if (!atomic_read(&t->count)) {
992: if (tasklet_clear_sched(t)) {
993: if (t->use_callback) {
994: trace_tasklet_entry(t, t->callback);
995: t->callback(t);
996: trace_tasklet_exit(t, t->callback);
997: } else {
998: trace_tasklet_entry(t, t->func);
999: t->func(t->data);
1000: trace_tasklet_exit(t, t->func);
1001: }
1002: }
1003: tasklet_unlock(t);
1004: tasklet_callback_sync_wait_running();
1005: continue;
1006: }
1007: tasklet_unlock(t);
1008: }
1009:
1010: local_irq_disable();
1011: t->next = NULL;
1012: *tl_head->tail = t;
1013: tl_head->tail = &t->next;
1014: __raise_softirq_irqoff(softirq_nr);
1015: local_irq_enable();
1016: }
1017: tasklet_unlock_callback();
1018: }
1019:
1020: static __latent_entropy void tasklet_action(void)
1021: {
1022: workqueue_softirq_action(false);
1023: tasklet_action_common(this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
1024: }
1025:
1026: static __latent_entropy void tasklet_hi_action(void)
1027: {
1028: workqueue_softirq_action(true);
1029: tasklet_action_common(this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
1030: }
1031:
1032: void tasklet_setup(struct tasklet_struct *t,
1033: void (*callback)(struct tasklet_struct *))
1034: {
1035: t->next = NULL;
1036: t->state = 0;
1037: atomic_set(&t->count, 0);
1038: t->callback = callback;
1039: t->use_callback = true;
1040: t->data = 0;
1041: }
1042: EXPORT_SYMBOL(tasklet_setup);
1043:
1044: void tasklet_init(struct tasklet_struct *t,
1045: void (*func)(unsigned long), unsigned long data)
1046: {
1047: t->next = NULL;
1048: t->state = 0;
1049: atomic_set(&t->count, 0);
1050: t->func = func;
1051: t->use_callback = false;
1052: t->data = data;
1053: }
1054: EXPORT_SYMBOL(tasklet_init);
1055:
1056: #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
1057: /*
1058: * Do not use in new code. Waiting for tasklets from atomic contexts is
1059: * error prone and should be avoided.
]
|
| 11/2 |
2026/11/19 02:04 |
tool |
codesearch-definition-source |
0m
Args: map[Name:usb_giveback_urb_bh]
Results: map[SourceCode:1674: static void usb_giveback_urb_bh(struct work_struct *work)
1675: {
1676: struct giveback_urb_bh *bh =
1677: container_of(work, struct giveback_urb_bh, bh);
1678: struct list_head local_list;
1679:
1680: spin_lock_irq(&bh->lock);
1681: bh->running = true;
1682: list_replace_init(&bh->head, &local_list);
1683: spin_unlock_irq(&bh->lock);
1684:
1685: while (!list_empty(&local_list)) {
1686: struct urb *urb;
1687:
1688: urb = list_entry(local_list.next, struct urb, urb_list);
1689: list_del_init(&urb->urb_list);
1690: bh->completing_ep = urb->ep;
1691: __usb_hcd_giveback_urb(urb);
1692: bh->completing_ep = NULL;
1693: }
1694:
1695: /*
1696: * giveback new URBs next time to prevent this function
1697: * from not exiting for a long time.
1698: */
1699: spin_lock_irq(&bh->lock);
1700: if (!list_empty(&bh->head)) {
1701: if (bh->high_prio)
1702: queue_work(system_bh_highpri_wq, &bh->bh);
1703: else
1704: queue_work(system_bh_wq, &bh->bh);
1705: }
1706: bh->running = false;
1707: spin_unlock_irq(&bh->lock);
1708: }
SourceFile:drivers/usb/core/hcd.c]
|
| 12/2 |
2026/11/19 02:04 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 11594
output: 157
thoughts: 123
Thoughts: **Examining Kernel Workqueue Lock Management Under Disabled Lockdep**
My immediate reaction upon reviewing the `kernel/workqueue.c` file, specifically lines 3506-3516, was one of profound curiosity and a need for immediate clarification. My attention was drawn to the interaction between these lines and the `lockdep_depth(current)` function. This prompted a deep dive into the behavior of `lockdep_depth(current)` precisely when the lockdep subsystem itself is disabled. My thought process then branched out to understand the interplay between `lockdep_depth`, `debug_show_held_locks`, and the mechanisms that disable lockdep, such as `lockdep_off` or `debug_locks_off`. To confirm my understanding and to empirically verify this behavior across the codebase, I initiated a search using codesearch/grepper for instances of `lockdep_depth` in conjunction with the condition that `lockdep is turned off`. This investigative path is critical for ensuring robust and predictable kernel behavior, particularly in scenarios where debug features might be selectively deactivated.
|
| 13/2 |
2026/11/19 02:04 |
tool |
codesearch-definition-source |
0m
Args: map[Name:debug_show_held_locks]
Results: map[SourceCode:6887: /*
6888: * Careful: only use this function if you are sure that
6889: * the task cannot run in parallel!
6890: */
6891: void debug_show_held_locks(struct task_struct *task)
6892: {
6893: if (unlikely(!debug_locks)) {
6894: printk("INFO: lockdep is turned off.\n");
6895: return;
6896: }
6897: lockdep_print_held_locks(task);
6898: }
SourceFile:kernel/locking/lockdep.c]
|
| 14/2 |
2026/11/19 02:04 |
tool |
grepper |
0m
Args: map[Expression:define lockdep_depth|lockdep is turned off PathPrefixes:[include/linux/lockdep.h kernel/locking/lockdep.c]]
Results: map[Output:include/linux/lockdep.h=276=extern u32 lock_sequence(struct lockdep_map *lock);
--
include/linux/lockdep.h-278-
include/linux/lockdep.h:279:#define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
include/linux/lockdep.h-280-
--
include/linux/lockdep.h=378=static inline void lockdep_unregister_key(struct lock_class_key *key)
--
include/linux/lockdep.h-381-
include/linux/lockdep.h:382:#define lockdep_depth(tsk) (0)
include/linux/lockdep.h-383-
--
kernel/locking/lockdep.c=6861=void debug_show_all_locks(void)
--
kernel/locking/lockdep.c-6865- if (unlikely(!debug_locks)) {
kernel/locking/lockdep.c:6866: pr_warn("INFO: lockdep is turned off.\n");
kernel/locking/lockdep.c-6867- return;
--
kernel/locking/lockdep.c=6891=void debug_show_held_locks(struct task_struct *task)
--
kernel/locking/lockdep.c-6893- if (unlikely(!debug_locks)) {
kernel/locking/lockdep.c:6894: printk("INFO: lockdep is turned off.\n");
kernel/locking/lockdep.c-6895- return;
]
|
| 15/2 |
2026/11/19 02:04 |
tool |
read-file |
0m
Args: map[File:kernel/workqueue.c FirstLine:3415 LineCount:50]
Results: map[Contents:3415: worker->current_pwq = pwq;
3416: if (worker->task)
3417: worker->current_at = READ_ONCE(worker->task->se.sum_exec_runtime);
3418: worker->current_start = jiffies;
3419: work_data = *work_data_bits(work);
3420: worker->current_color = get_work_color(work_data);
3421:
3422: /*
3423: * Record wq name for cmdline and debug reporting, may get
3424: * overridden through set_worker_desc().
3425: */
3426: strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN);
3427:
3428: list_del_init(&work->entry);
3429:
3430: /*
3431: * CPU intensive works don't participate in concurrency management.
3432: * They're the scheduler's responsibility. This takes @worker out
3433: * of concurrency management and the next code block will chain
3434: * execution of the pending work items.
3435: */
3436: if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE))
3437: worker_set_flags(worker, WORKER_CPU_INTENSIVE);
3438:
3439: /*
3440: * Kick @pool if necessary. It's always noop for per-cpu worker pools
3441: * since nr_running would always be >= 1 at this point. This is used to
3442: * chain execution of the pending work items for WORKER_NOT_RUNNING
3443: * workers such as the UNBOUND and CPU_INTENSIVE ones.
3444: *
3445: * Select the worker under pool->lock; the wakeup is deferred until
3446: * after the lock is dropped, guarded by the rcu_read_lock() below.
3447: */
3448: kick_pool_pick(pool, &wake_task);
3449:
3450: /*
3451: * Record the last pool and clear PENDING which should be the last
3452: * update to @work. Also, do this inside @pool->lock so that
3453: * PENDING and queued state changes happen together while IRQ is
3454: * disabled.
3455: */
3456: set_work_pool_and_clear_pending(work, pool->id, pool_offq_flags(pool));
3457:
3458: pwq->stats[PWQ_STAT_STARTED]++;
3459:
3460: rcu_read_lock();
3461: raw_spin_unlock_irq(&pool->lock);
3462: if (wake_task)
3463: wake_up_process(wake_task);
3464: rcu_read_unlock();
]
|
| 16/2 |
2026/11/19 02:04 |
tool |
read-file |
0m
Args: map[File:kernel/workqueue.c FirstLine:3915 LineCount:60]
Results: map[Contents:3915:
3916: /*
3917: * TODO: Convert all tasklet users to workqueue and use softirq directly.
3918: *
3919: * This is currently called from tasklet[_hi]action() and thus is also called
3920: * whenever there are tasklets to run. Let's do an early exit if there's nothing
3921: * queued. Once conversion from tasklet is complete, the need_more_worker() test
3922: * can be dropped.
3923: *
3924: * After full conversion, we'll add worker->softirq_action, directly use the
3925: * softirq action and obtain the worker pointer from the softirq_action pointer.
3926: */
3927: void workqueue_softirq_action(bool highpri)
3928: {
3929: struct worker_pool *pool =
3930: &per_cpu(bh_worker_pools, smp_processor_id())[highpri];
3931: if (need_more_worker(pool))
3932: bh_worker(list_first_entry(&pool->workers, struct worker, node));
3933: }
3934:
3935: struct wq_drain_dead_softirq_work {
3936: struct work_struct work;
3937: struct worker_pool *pool;
3938: struct completion done;
3939: };
3940:
3941: static void drain_dead_softirq_workfn(struct work_struct *work)
3942: {
3943: struct wq_drain_dead_softirq_work *dead_work =
3944: container_of(work, struct wq_drain_dead_softirq_work, work);
3945: struct worker_pool *pool = dead_work->pool;
3946: bool repeat;
3947:
3948: /*
3949: * @pool's CPU is dead and we want to execute its still pending work
3950: * items from this BH work item which is running on a different CPU. As
3951: * its CPU is dead, @pool can't be kicked and, as work execution path
3952: * will be nested, a lockdep annotation needs to be suppressed. Mark
3953: * @pool with %POOL_BH_DRAINING for the special treatments.
3954: */
3955: raw_spin_lock_irq(&pool->lock);
3956: pool->flags |= POOL_BH_DRAINING;
3957: raw_spin_unlock_irq(&pool->lock);
3958:
3959: bh_worker(list_first_entry(&pool->workers, struct worker, node));
3960:
3961: raw_spin_lock_irq(&pool->lock);
3962: pool->flags &= ~POOL_BH_DRAINING;
3963: repeat = need_more_worker(pool);
3964: raw_spin_unlock_irq(&pool->lock);
3965:
3966: /*
3967: * bh_worker() might hit consecutive execution limit and bail. If there
3968: * still are pending work items, reschedule self and return so that we
3969: * don't hog this CPU's BH.
3970: */
3971: if (repeat) {
3972: if (pool->attrs->nice == HIGHPRI_NICE_LEVEL)
3973: queue_work(system_bh_highpri_wq, work);
3974: else
]
|
| 17/2 |
2026/11/19 02:04 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 14312
output: 128
thoughts: 1458
Thoughts: ** Eureka! A Critical Workqueue Lockdep Revelation! **
I've just had a significant breakthrough, a real "aha!" moment, while examining a rather perplexing `BUG: workqueue leaked atomic, lock or RCU` message from syzkaller. It all started when I looked at the `lockdep_depth` macro in `include/linux/lockdep.h`, specifically the line:
This macro, as I'm sure you know, is the key to how lockdep tracks nested lock acquisitions. It directly consults the `debug_locks` flag. Then, my eyes fell upon the `process_one_work` function in `kernel/workqueue.c`, around lines 3466-3517, where this `lockdep_depth` macro is used to detect potential lock leaks in workqueue callbacks. The relevant snippet is:
Immediately, a crucial question popped into my head: why would `lockdep_depth(current)` report a different value *after* `worker->current_func(work)` has executed, leading to the `lockdep_depth(current) != lockdep_start_depth` condition triggering the bug report? Specifically, I'm wondering about the scenario where `debug_locks` became `0` *during* the execution of `worker->current_func(work)` or on another CPU while it was running, *without* issuing a prior lockdep splat. Did something silently turn off `debug_locks`? Or did another CPU somehow interfere? Wait, the CPU is listed as `CPU: 0` in the syzkaller report, but syzkaller can utilize multiple CPUs. This raises further questions about why syzkaller wouldn't have caught a more fundamental `debug_locks` issue if it were to occur.
I need to pause and analyze this. Let's consider the specifics of the reported error: `lockdep_start_depth == 4` and `lockdep_depth(current) == 0`. How could `debug_locks` have been `1` before `worker->current_func(work)` (allowing `lockdep_start_depth` to capture the actual depth of 4), but then become `0` by the time `lockdep_depth(current)` is re-evaluated?
The context here is that `current` is `syz-executor[6626]`. This executor likely initiated a call chain starting with `sendto`, which then went into `rtnetlink_rcv_msg`. This function holds locks such as `rtnl_lock`, potentially `rcu_read_lock`, and a softirq lockdep map, contributing to the initial lock depth of 4. The execution then proceeded through `__alloc_skb` -> `local_bh_enable` -> `__do_softirq` -> `handle_softirqs` -> `tasklet_hi_action` -> `bh_worker` -> `process_one_work`.
At line 3467 of `process_one_work`, `lockdep_start_depth` is set to 4, reflecting the actual `current->lockdep_depth` when `debug_locks` was still 1. Then, `worker->current_func(work)`, which is `usb_giveback_urb_bh` in this case, or something happening concurrently, must have caused `debug_locks` to become 0.
This leads to line 3507, where `lockdep_depth(current)` is evaluated again. Because `debug_locks` is now `0`, the macro evaluates to `0`. This mismatch (`0 != 4`) triggers the `BUG: workqueue leaked atomic, lock or RCU` message, and crucially, the accompanying `INFO: lockdep is turned off.` message. The output shows:
This is where two major, urgent questions arise.
**Question A: Why was `debug_locks` turned off WITHOUT any prior kernel warning/splat?**
Syzkaller is designed to halt on the *first* reported `BUG:`, `WARNING:`, lockdep `WARNING:`, or `INFO: possible circular locking dependency`. If lockdep had issued any of its typical warnings, syzkaller would have reported *that* issue, not the `BUG: workqueue leaked atomic, lock or RCU`. This implies that `debug_locks` was set to 0 through a mechanism that bypassed the usual lockdep reporting. Could something have called `debug_locks_off()` silently, or perhaps `lockdep_off()`? I need to investigate what `lockdep_off()` actually does – wait, it increments `current->lockdep_recursion`. No, the `lockdep_depth` macro explicitly checks `debug_locks`. And importantly, `debug_show_held_locks` *also* checks `if (unlikely(!debug_locks)) printk("INFO: lockdep is turned off.\n");`. This confirms `debug_locks` was indeed 0.
So, the core of Question A remains: Why was `debug_locks` 0 without syzkaller catching an earlier error, or what specific function call is responsible for setting `debug_locks = 0` in this context?
**Question B: Even if `debug_locks` becomes 0, should `process_one_work` have a more robust check?**
Specifically, shouldn't the check be `debug_locks && lockdep_depth(current) != lockdep_start_depth`? Or is the fundamental problem that `debug_locks` became 0 in the first place?
To answer these critical questions, I must not rely on assumptions. My next steps are clear and must be executed diligently:
1. **Examine the Git history of `process_one_work`** in `kernel/workqueue.c` around lines 3466-3517. I need to understand any changes that might have introduced this behavior or affected the lockdep checks there.
2. **Audit all kernel locations that modify `debug_locks` or call `debug_locks_off()`**. This is crucial to identify the source that turned off lockdep tracking without apparent warning.
3. **Review recent commits** in areas related to lockdep, workqueue, USB (since `usb_giveback_urb_bh` is involved), and PREEMPT_RT, as these are the most likely candidates to have introduced such a change.
I'll be initiating parallel investigative threads for all these points to expedite the discovery process. This looks like a significant piece of the puzzle!
|
| 18/2 |
2026/11/19 02:05 |
tool |
git-blame |
0m
Args: map[End:3520 File:kernel/workqueue.c Start:3465]
Results: map[Output:a62428c0ae54a 3465)
c35aea39d1e10 3466) rcu_start_depth = rcu_preempt_depth();
c35aea39d1e10 3467) lockdep_start_depth = lockdep_depth(current);
1acd92d95fa24 3468) /* see drain_dead_softirq_workfn() */
1acd92d95fa24 3469) if (!bh_draining)
4f022f430e21e 3470) lock_map_acquire(pwq->wq->lockdep_map);
a62428c0ae54a 3471) lock_map_acquire(&lockdep_map);
e6f3faa734a00 3472) /*
f52be5708076b 3473) * Strictly speaking we should mark the invariant state without holding
f52be5708076b 3474) * any locks, that is, before these two lock_map_acquire()'s.
e6f3faa734a00 3475) *
e6f3faa734a00 3476) * However, that would result in:
e6f3faa734a00 3477) *
e6f3faa734a00 3478) * A(W1)
e6f3faa734a00 3479) * WFC(C)
e6f3faa734a00 3480) * A(W1)
e6f3faa734a00 3481) * C(C)
e6f3faa734a00 3482) *
e6f3faa734a00 3483) * Which would create W1->C->W1 dependencies, even though there is no
e6f3faa734a00 3484) * actual deadlock possible. There are two solutions, using a
e6f3faa734a00 3485) * read-recursive acquire on the work(queue) 'locks', but this will then
f52be5708076b 3486) * hit the lockdep limitation on recursive locks, or simply discard
e6f3faa734a00 3487) * these locks.
e6f3faa734a00 3488) *
e6f3faa734a00 3489) * AFAICT there is no possible deadlock scenario between the
e6f3faa734a00 3490) * flush_work() and complete() primitives (except for single-threaded
e6f3faa734a00 3491) * workqueues), so hiding them isn't a problem.
e6f3faa734a00 3492) */
f52be5708076b 3493) lockdep_invariant_state(true);
e36c886a0f9d6 3494) trace_workqueue_execute_start(work);
a2c1c57be8d9f 3495) worker->current_func(work);
e36c886a0f9d6 3496) /*
e36c886a0f9d6 3497) * While we must be careful to not use "work" after this, the trace
e36c886a0f9d6 3498) * point will only record its address.
e36c886a0f9d6 3499) */
1c5da0ec7f20d 3500) trace_workqueue_execute_end(work, worker->current_func);
70e1683ca3a64 3501)
a62428c0ae54a 3502) lock_map_release(&lockdep_map);
1acd92d95fa24 3503) if (!bh_draining)
4f022f430e21e 3504) lock_map_release(pwq->wq->lockdep_map);
a62428c0ae54a 3505)
c35aea39d1e10 3506) if (unlikely((worker->task && in_atomic()) ||
c35aea39d1e10 3507) lockdep_depth(current) != lockdep_start_depth ||
c35aea39d1e10 3508) rcu_preempt_depth() != rcu_start_depth)) {
c35aea39d1e10 3509) pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n"
c35aea39d1e10 3510) " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n",
c35aea39d1e10 3511) current->comm, task_pid_nr(current), preempt_count(),
c35aea39d1e10 3512) lockdep_start_depth, lockdep_depth(current),
c35aea39d1e10 3513) rcu_start_depth, rcu_preempt_depth(),
c35aea39d1e10 3514) worker->current_func);
a62428c0ae54a 3515) debug_show_held_locks(current);
a62428c0ae54a 3516) dump_stack();
a62428c0ae54a 3517) }
a62428c0ae54a 3518)
b22ce2785d974 3519) /*
025f50f386648 3520) * The following prevents a kworker from hogging CPU on !PREEMPTION
]
|
| 19/2 |
2026/11/19 02:05 |
tool |
grepper |
0m
Args: map[Expression:debug_locks\b|debug_locks_off]
Results: map[Output:
Full output is too long, showing 200 out of 713 lines.
Use more precise expression or PathPrefixes if possible.
Number of matching lines per file (43 files in total):
Documentation/RCU/lockdep-splat.rst:1
arch/microblaze/kernel/traps.c:1
arch/powerpc/kernel/traps.c:1
arch/s390/kernel/ipl.c:2
arch/s390/kernel/machine_kexec.c:2
arch/sh/kernel/dumpstack.c:1
arch/sh/kernel/traps_32.c:1
arch/x86/include/asm/desc.h:1
drivers/gpu/drm/ci/igt_runner.sh:3
drivers/gpu/drm/i915/gem/i915_gem_object.h:1
include/linux/backing-dev.h:1
include/linux/debug_locks.h:5
include/linux/freezer.h:1
include/linux/interrupt_rc.h:1
include/linux/local_lock_internal.h:1
include/linux/lockdep.h:7
include/linux/mm.h:1
include/linux/mutex.h:1
include/net/sock.h:2
init/main.c:1
kernel/debug/debug_core.c:1
kernel/freezer.c:1
kernel/kcsan/report.c:1
kernel/locking/lockdep.c:63
kernel/locking/lockdep_proc.c:4
kernel/locking/mutex-debug.c:2
kernel/locking/mutex.c:1
kernel/locking/qspinlock_paravirt.h:1
kernel/locking/rtmutex_common.h:1
kernel/locking/rwsem.c:1
kernel/locking/spinlock.c:1
kernel/locking/spinlock_debug.c:3
kernel/panic.c:4
kernel/rcu/update.c:1
kernel/sched/core.c:2
kernel/workqueue.c:1
lib/Makefile:1
lib/debug_locks.c:7
lib/locking-selftest.c:10
lib/rhashtable.c:2
net/netfilter/nf_tables_api.c:1
net/netlabel/netlabel_kapi.c:1
net/openvswitch/datapath.c:1
Documentation/RCU/lockdep-splat.rst=28=other info that might help us debug this::
Documentation/RCU/lockdep-splat.rst-29-
Documentation/RCU/lockdep-splat.rst:30: rcu_scheduler_active = 1, debug_locks = 0
Documentation/RCU/lockdep-splat.rst-31- 3 locks held by scsi_scan_6/1552:
--
arch/microblaze/kernel/traps.c-16-#include <linux/sched/debug.h>
arch/microblaze/kernel/traps.c:17:#include <linux/debug_locks.h>
arch/microblaze/kernel/traps.c-18-
--
arch/powerpc/kernel/traps.c=173=void panic_flush_kmsg_end(void)
--
arch/powerpc/kernel/traps.c-176- bust_spinlocks(0);
arch/powerpc/kernel/traps.c:177: debug_locks_off();
arch/powerpc/kernel/traps.c-178- console_flush_on_panic(CONSOLE_FLUSH_PENDING);
--
arch/s390/kernel/ipl.c-21-#include <linux/crash_dump.h>
arch/s390/kernel/ipl.c:22:#include <linux/debug_locks.h>
arch/s390/kernel/ipl.c-23-#include <linux/vmalloc.h>
--
arch/s390/kernel/ipl.c=2174=void do_restart(void *arg)
--
arch/s390/kernel/ipl.c-2176- tracing_off();
arch/s390/kernel/ipl.c:2177: debug_locks_off();
arch/s390/kernel/ipl.c-2178- lgr_info_log();
--
arch/s390/kernel/machine_kexec.c-14-#include <linux/ftrace.h>
arch/s390/kernel/machine_kexec.c:15:#include <linux/debug_locks.h>
arch/s390/kernel/machine_kexec.c-16-#include <linux/cpufeature.h>
--
arch/s390/kernel/machine_kexec.c=251=static void __machine_kexec(void *data)
--
arch/s390/kernel/machine_kexec.c-254- tracing_off();
arch/s390/kernel/machine_kexec.c:255: debug_locks_off();
arch/s390/kernel/machine_kexec.c-256-#ifdef CONFIG_CRASH_DUMP
--
arch/sh/kernel/dumpstack.c-9-#include <linux/ftrace.h>
arch/sh/kernel/dumpstack.c:10:#include <linux/debug_locks.h>
arch/sh/kernel/dumpstack.c-11-#include <linux/sched/debug.h>
--
arch/sh/kernel/traps_32.c-18-#include <linux/bug.h>
arch/sh/kernel/traps_32.c:19:#include <linux/debug_locks.h>
arch/sh/kernel/traps_32.c-20-#include <linux/kdebug.h>
--
arch/x86/include/asm/desc.h-11-
arch/x86/include/asm/desc.h:12:#include <linux/debug_locks.h>
arch/x86/include/asm/desc.h-13-#include <linux/smp.h>
--
drivers/gpu/drm/ci/igt_runner.sh=92=if [ -f /proc/lockdep_stats ]; then
drivers/gpu/drm/ci/igt_runner.sh:93: # If debug_locks is 0, it indicates lockdep is detected and it turns itself off.
drivers/gpu/drm/ci/igt_runner.sh:94: debug_locks=$(grep 'debug_locks:' /proc/lockdep_stats | awk '{print $2}')
drivers/gpu/drm/ci/igt_runner.sh:95: if [ "$debug_locks" -eq 0 ] && [ "$ret" -eq 0 ]; then
drivers/gpu/drm/ci/igt_runner.sh-96- echo "Warning: LOCKDEP issue detected. Please check dmesg logs for more information."
--
drivers/gpu/drm/i915/gem/i915_gem_object.h=96=i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle)
--
drivers/gpu/drm/i915/gem/i915_gem_object.h-98-#ifdef CONFIG_LOCKDEP
drivers/gpu/drm/i915/gem/i915_gem_object.h:99: WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map));
drivers/gpu/drm/i915/gem/i915_gem_object.h-100-#endif
--
include/linux/backing-dev.h=247=static inline struct bdi_writeback *inode_to_wb(const struct inode *inode)
--
include/linux/backing-dev.h-249-#ifdef CONFIG_LOCKDEP
include/linux/backing-dev.h:250: WARN_ON_ONCE(debug_locks &&
include/linux/backing-dev.h-251- (inode->i_sb->s_iflags & SB_I_CGROUPWB) &&
--
include/linux/debug_locks.h=8=struct task_struct;
include/linux/debug_locks.h-9-
include/linux/debug_locks.h:10:extern int debug_locks __read_mostly;
include/linux/debug_locks.h-11-extern int debug_locks_silent __read_mostly;
--
include/linux/debug_locks.h-13-
include/linux/debug_locks.h:14:static __always_inline int __debug_locks_off(void)
include/linux/debug_locks.h-15-{
include/linux/debug_locks.h:16: return xchg(&debug_locks, 0);
include/linux/debug_locks.h-17-}
--
include/linux/debug_locks.h-21- */
include/linux/debug_locks.h:22:extern int debug_locks_off(void);
include/linux/debug_locks.h-23-
--
include/linux/debug_locks.h-29- instrumentation_begin(); \
include/linux/debug_locks.h:30: if (debug_locks_off() && !debug_locks_silent) \
include/linux/debug_locks.h-31- WARN(1, "DEBUG_LOCKS_WARN_ON(%s)", #c); \
--
include/linux/freezer.h-6-
include/linux/freezer.h:7:#include <linux/debug_locks.h>
include/linux/freezer.h-8-#include <linux/sched.h>
--
include/linux/interrupt_rc.h-13-
include/linux/interrupt_rc.h:14:#include <linux/debug_locks.h>
include/linux/interrupt_rc.h-15-#include <linux/irqflags.h>
--
include/linux/local_lock_internal.h-8-#include <linux/lockdep.h>
include/linux/local_lock_internal.h:9:#include <linux/debug_locks.h>
include/linux/local_lock_internal.h-10-#include <asm/current.h>
--
include/linux/lockdep.h=17=struct task_struct;
--
include/linux/lockdep.h-22-#include <linux/list.h>
include/linux/lockdep.h:23:#include <linux/debug_locks.h>
include/linux/lockdep.h-24-#include <linux/stacktrace.h>
--
include/linux/lockdep.h=276=extern u32 lock_sequence(struct lockdep_map *lock);
--
include/linux/lockdep.h-278-
include/linux/lockdep.h:279:#define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
include/linux/lockdep.h-280-
include/linux/lockdep.h-281-#define lockdep_assert(cond) \
include/linux/lockdep.h:282: do { WARN_ON(debug_locks && !(cond)); } while (0)
include/linux/lockdep.h-283-
include/linux/lockdep.h-284-#define lockdep_assert_once(cond) \
include/linux/lockdep.h:285: do { WARN_ON_ONCE(debug_locks && !(cond)); } while (0)
include/linux/lockdep.h-286-
--
include/linux/lockdep.h=335=static inline void lockdep_set_selftest_task(struct task_struct *task)
--
include/linux/lockdep.h-369-
include/linux/lockdep.h:370:# define lockdep_reset() do { debug_locks = 1; } while (0)
include/linux/lockdep.h-371-# define lockdep_free_key_range(start, size) do { } while (0)
--
include/linux/lockdep.h=574=DECLARE_PER_CPU(unsigned int, lockdep_recursion);
include/linux/lockdep.h-575-
include/linux/lockdep.h:576:#define __lockdep_enabled (debug_locks && !this_cpu_read(lockdep_recursion))
include/linux/lockdep.h-577-
--
include/linux/lockdep.h=625=extern void lockdep_assert_in_softirq_func(void);
--
include/linux/lockdep.h-645-# define lockdep_assert_RT_in_threaded_ctx() do { \
include/linux/lockdep.h:646: WARN_ONCE(debug_locks && !current->lockdep_recursion && \
include/linux/lockdep.h-647- lockdep_hardirq_context() && \
--
include/linux/mm.h-14-#include <linux/atomic.h>
include/linux/mm.h:15:#include <linux/debug_locks.h>
include/linux/mm.h-16-#include <linux/compiler.h>
--
include/linux/mutex.h-20-#include <linux/osq_lock.h>
include/linux/mutex.h:21:#include <linux/debug_locks.h>
include/linux/mutex.h-22-#include <linux/cleanup.h>
--
include/net/sock.h=1800=static inline void sock_owned_by_me(const struct sock *sk)
--
include/net/sock.h-1802-#ifdef CONFIG_LOCKDEP
include/net/sock.h:1803: WARN_ON_ONCE(!lockdep_sock_is_held(sk) && debug_locks);
include/net/sock.h-1804-#endif
--
include/net/sock.h=1807=static inline void sock_not_owned_by_me(const struct sock *sk)
--
include/net/sock.h-1809-#ifdef CONFIG_LOCKDEP
include/net/sock.h:1810: WARN_ON_ONCE(lockdep_sock_is_held(sk) && debug_locks);
include/net/sock.h-1811-#endif
--
init/main.c-66-#include <linux/key.h>
init/main.c:67:#include <linux/debug_locks.h>
init/main.c-68-#include <linux/debugobjects.h>
--
kernel/debug/debug_core.c=986=void kgdb_panic(const char *msg)
--
kernel/debug/debug_core.c-998-
kernel/debug/debug_core.c:999: debug_locks_off();
kernel/debug/debug_core.c-1000- console_flush_on_panic(CONSOLE_FLUSH_PENDING);
--
kernel/freezer.c=109=static int __set_task_frozen(struct task_struct *p, void *arg)
--
kernel/freezer.c-138- if (!(state & __TASK_FREEZABLE_UNSAFE))
kernel/freezer.c:139: WARN_ON_ONCE(debug_locks && p->lockdep_depth);
kernel/freezer.c-140-#endif
--
kernel/kcsan/report.c-7-
kernel/kcsan/report.c:8:#include <linux/debug_locks.h>
kernel/kcsan/report.c-9-#include <linux/delay.h>
--
kernel/locking/lockdep.c-42-#include <linux/stacktrace.h>
kernel/locking/lockdep.c:43:#include <linux/debug_locks.h>
kernel/locking/lockdep.c-44-#include <linux/irqflags.h>
--
kernel/locking/lockdep.c=116=static __always_inline bool lockdep_enabled(void)
kernel/locking/lockdep.c-117-{
kernel/locking/lockdep.c:118: if (!debug_locks)
kernel/locking/lockdep.c-119- return false;
--
kernel/locking/lockdep.c=150=static inline void lockdep_unlock(void)
--
kernel/locking/lockdep.c-153-
kernel/locking/lockdep.c:154: if (debug_locks && DEBUG_LOCKS_WARN_ON(__owner != current))
kernel/locking/lockdep.c-155- return;
--
kernel/locking/lockdep.c=172=static int graph_lock(void)
--
kernel/locking/lockdep.c-181- */
kernel/locking/lockdep.c:182: if (!debug_locks) {
kernel/locking/lockdep.c-183- lockdep_unlock();
--
kernel/locking/lockdep.c=189=static inline void graph_unlock(void)
--
kernel/locking/lockdep.c-197- */
kernel/locking/lockdep.c:198:static inline int debug_locks_off_graph_unlock(void)
kernel/locking/lockdep.c-199-{
kernel/locking/lockdep.c:200: int ret = debug_locks_off();
kernel/locking/lockdep.c-201-
--
]
|
| 20/2 |
2026/11/19 02:05 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:kernel/locking/lockdep.c]
Results: map[Output:aa63c0df7992 locking/lockdep: Fix inverted "on"/"last" CPU label in lockdep_print_held_locks()
02c6be7d675b locking/lockdep: Invalidate stale class_cache entries for zapped classes
8f2109843137 locking/lockdep: add sequence counter to held_lock
7577e00b9ab5 locking/lockdep: Fix NULL pointer dereference in __lock_set_class()
051f5d223dfc lockdep: Enable the printing of held locks of remote running tasks and print task CPU
7a3cedafccf8 lockdep: Speed up lockdep_unregister_key() with expedited RCU synchronization
d7c36d6350b5 locking/lockdep: Avoid struct return in lock_stats()
cdb7d2d68cde locking/lockdep: Add number of dynamic keys to /proc/lockdep_stats
6a1a219f535a locking/lockdep: Prevent abuse of lockdep subclass
96ca1830e121 locking/lockdep: Move hlock_equal() to the respective #ifdeffery
495f53d5cca0 locking/lockdep: Decrease nr_unused_locks if lock unused in zap_class()
de4b59d65264 locking/lockdep: Add kasan_check_byte() check in lock_acquire()
a94d32446ab5 locking/lock_events: Add locking events for lockdep
1751f872cc97 treewide: const qualify ctl_tables where applicable
343060092585 lockdep: Move lockdep_assert_locked() under #ifdef CONFIG_PROVE_LOCKING
8148fa2e022b lockdep: Mark chain_hlock_class_idx() with __maybe_unused
e48bf7ca6056 lockdep: Use info level for lockdep initial info messages
0784181b44af lockdep: Add lockdep_cleanup_dead_cpu()
59cd94ef8009 lockdep: Mark emergency sections in lockdep splats
ff9bf4b34104 lockdep: Fix lockdep_set_notrack_class() for CONFIG_LOCK_STAT
]
|
| 21/2 |
2026/11/19 02:05 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:kernel/workqueue.c]
Results: map[Output:0af298e62e5c treewide: refresh kmalloc_obj() conversions
bda345b3fc59 workqueue: mark pool->nr_running as __data_racy
d1a4549a3645 workqueue: Fix NULL current_pwq deref in mem-reclaim helper
980db94e3eee workqueue: Fix NULL current_pwq deref in chained work check
db6365ced4d5 workqueue: Fix NULL current_pwq deref in flush dependency check
9d4815c14f7f workqueue: BUG_ON() a pwq release before workqueue_init()
d8c2ce77cd09 workqueue: Derive the standard attrs from the workqueue flags
27db9dd7f84f workqueue: Give percpu workqueues their own max_active
f1639cde5f5c workqueue: Make flush_workqueue() visit only pwqs active since the last flush
31e33de1f4e1 workqueue: Maintain pwq->total_in_flight
84a42d3829f0 workqueue: Add for_each_node_with_fallback()
1298c43b6732 workqueue: Fix typo "upto" in comment
8bbd858c4710 workqueue: Add workqueue_bh_budget_yield tracepoint
a53b50e21af2 workqueue: Add workqueue_mayday and workqueue_rescued tracepoints
a4d1cdc4356d workqueue: Add workqueue_cpu_intensive tracepoint
ab85b68e630e workqueue: rename the pwq slot helpers shared with percpu
64496075b02c workqueue: build percpu pwqs through the attrs path
2581b2b6d20a workqueue: make the default pwq optional
e92329ef60a8 workqueue: resolve the backing pool in alloc_pwq()
8386ed68ae04 workqueue: move the unbound-only attrs check to apply_workqueue_attrs()
]
|
| 22/2 |
2026/11/19 02:05 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 23020
output: 133
thoughts: 934
Thoughts: **My Diagnostic Thought Process**
WHOA! Everything needs to stop for a moment. I've just spotted something truly peculiar in the `grepper` output. It looks like `debug_locks` is being matched directly in `kernel/workqueue.c` at line 1. This immediately triggers a cascade of related observations.
I'm seeing a `WARN_ON_ONCE(debug_locks && p->lockdep_depth)` in `kernel/freezer.c` at line 139, and then further down, in `include/linux/lockdep.h`, there's the macro `#define lockdep_assert(cond) do { WARN_ON(debug_locks && !(cond)); } while (0)`. This highlights a strong connection between `debug_locks` and lockdep's assertion mechanisms. The mention of a specific commit, `c35aea39d1e10`, on `kernel/workqueue.c` is a crucial pointer.
This discovery necessitates a deep dive. My immediate questions are:
1. Where exactly is `debug_locks` being utilized within `kernel/workqueue.c`?
2. What is the nature and impact of commit `c35aea39d1e10` on `kernel/workqueue.c`?
3. How and why can `debug_locks` be disabled within `kernel/locking/lockdep.c` or `lib/debug_locks.c`? And most importantly, *why* is syzkaller reporting a `BUG: workqueue leaked atomic, lock or RCU` *before* any lockdep message has appeared? This temporal discrepancy is a major red flag.
Then, a rather unsettling thought strikes me: Could another CPU be in the middle of printing a lockdep splat when this happens?
Let's explore this hypothesis:
Imagine CPU 1 encountering a lockdep bug. It might call `debug_locks_off()`, which, as I recall, atomically sets `debug_locks = 0` using `xchg(&debug_locks, 0)` *before* it prints anything! Alternatively, lockdep could be hitting one of its limits like `MAX_LOCKDEP_ENTRIES`, `MAX_LOCKDEP_CHAINS`, or `MAX_LOCK_DEPTH`. In such cases, `debug_locks_off()` is called, followed by a `printk` message like "BUG: MAX_LOCKDEP_... too low!" or "INFO: ...".
Now, let's refine this. If lockdep hits a `MAX_LOCKDEP_*` condition, does syzkaller simply ignore it? Yes, it appears syzkaller explicitly ignores `BUG: MAX_LOCKDEP_* too low!`. This is because, from syzkaller's perspective, table exhaustion isn't a hard kernel crash, so it continues execution with `debug_locks` effectively set to 0.
But what if CPU 1 calls `debug_locks_off()` precisely in the nanosecond *between* lines 3467 and 3507 on CPU 0, or even within `usb_giveback_urb_bh` on CPU 0, right as it hits a `MAX_LOCKDEP_*` condition?
This leads to a critical question: If `MAX_LOCKDEP_*` occurred on CPU 0 within `usb_giveback_urb_bh`, wouldn't the `BUG: MAX_LOCKDEP_* too low!` message be printed in `dmesg` *before* the `BUG: workqueue leaked atomic, lock or RCU`? Syzkaller, I know, extracts its crash report starting from the crash header. If syzkaller ignores the `MAX_LOCKDEP_*` message, or if CPU 1 hadn't yet printed its own message, then anything preceding the actual crash header that was ignored by syzkaller, or not yet printed by CPU 1, would be absent from the extracted crash snippet.
I need to resist jumping to conclusions. A thorough investigation is paramount. My next steps are to meticulously examine:
1. Commit `c35aea39d1e10`, along with `a62428c0ae54a` and `1acd92d95fa24`, specifically within `kernel/workqueue.c`.
2. The exact location and context of the `debug_locks` match in `kernel/workqueue.c`.
3. Every instance of `debug_locks_off` across `kernel/locking/` and `lib/debug_locks.c`.
4. Any recent commits in the general git tree using `git-log` on the root.
I'm initiating these checks immediately.
|
| 23/2 |
2026/11/19 02:06 |
tool |
git-show |
0m
Args: map[Commit:c35aea39d1e10]
Results: map[Output:commit c35aea39d1e106f61fd2130f0d32a3bac8bd4570
Author: Tejun Heo <tj@kernel.org>
Date: Sun Feb 4 11:28:06 2024 -1000
workqueue: Update lock debugging code
These changes are in preparation of BH workqueue which will execute work
items from BH context.
- Update lock and RCU depth checks in process_one_work() so that it
remembers and checks against the starting depths and prints out the depth
changes.
- Factor out lockdep annotations in the flush paths into
touch_{wq|work}_lockdep_map(). The work->lockdep_map touching is moved
from __flush_work() to its callee - start_flush_work(). This brings it
closer to the wq counterpart and will allow testing the associated wq's
flags which will be needed to support BH workqueues. This is not expected
to cause any functional changes.
Signed-off-by: Tejun Heo <tj@kernel.org>
Tested-by: Allen Pais <allen.lkml@gmail.com>
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 695f6f5ad038..ac42c005b00b 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -2965,6 +2965,7 @@ __acquires(&pool->lock)
struct pool_workqueue *pwq = get_work_pwq(work);
struct worker_pool *pool = worker->pool;
unsigned long work_data;
+ int lockdep_start_depth, rcu_start_depth;
#ifdef CONFIG_LOCKDEP
/*
* It is permissible to free the struct work_struct from
@@ -3027,6 +3028,8 @@ __acquires(&pool->lock)
pwq->stats[PWQ_STAT_STARTED]++;
raw_spin_unlock_irq(&pool->lock);
+ rcu_start_depth = rcu_preempt_depth();
+ lockdep_start_depth = lockdep_depth(current);
lock_map_acquire(&pwq->wq->lockdep_map);
lock_map_acquire(&lockdep_map);
/*
@@ -3062,12 +3065,15 @@ __acquires(&pool->lock)
lock_map_release(&lockdep_map);
lock_map_release(&pwq->wq->lockdep_map);
- if (unlikely(in_atomic() || lockdep_depth(current) > 0 ||
- rcu_preempt_depth() > 0)) {
- pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d/%d\n"
- " last function: %ps\n",
- current->comm, preempt_count(), rcu_preempt_depth(),
- task_pid_nr(current), worker->current_func);
+ if (unlikely((worker->task && in_atomic()) ||
+ lockdep_depth(current) != lockdep_start_depth ||
+ rcu_preempt_depth() != rcu_start_depth)) {
+ pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n"
+ " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n",
+ current->comm, task_pid_nr(current), preempt_count(),
+ lockdep_start_depth, lockdep_depth(current),
+ rcu_start_depth, rcu_preempt_depth(),
+ worker->current_func);
debug_show_held_locks(current);
dump_stack();
}
@@ -3549,6 +3555,19 @@ static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
return wait;
}
+static void touch_wq_lockdep_map(struct workqueue_struct *wq)
+{
+ lock_map_acquire(&wq->lockdep_map);
+ lock_map_release(&wq->lockdep_map);
+}
+
+static void touch_work_lockdep_map(struct work_struct *work,
+ struct workqueue_struct *wq)
+{
+ lock_map_acquire(&work->lockdep_map);
+ lock_map_release(&work->lockdep_map);
+}
+
/**
* __flush_workqueue - ensure that any scheduled work has run to completion.
* @wq: workqueue to flush
@@ -3568,8 +3587,7 @@ void __flush_workqueue(struct workqueue_struct *wq)
if (WARN_ON(!wq_online))
return;
- lock_map_acquire(&wq->lockdep_map);
- lock_map_release(&wq->lockdep_map);
+ touch_wq_lockdep_map(wq);
mutex_lock(&wq->mutex);
@@ -3768,6 +3786,7 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
struct worker *worker = NULL;
struct worker_pool *pool;
struct pool_workqueue *pwq;
+ struct workqueue_struct *wq;
might_sleep();
@@ -3791,11 +3810,14 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
pwq = worker->current_pwq;
}
- check_flush_dependency(pwq->wq, work);
+ wq = pwq->wq;
+ check_flush_dependency(wq, work);
insert_wq_barrier(pwq, barr, work, worker);
raw_spin_unlock_irq(&pool->lock);
+ touch_work_lockdep_map(work, wq);
+
/*
* Force a lock recursion deadlock when using flush_work() inside a
* single-threaded or rescuer equipped workqueue.
@@ -3805,11 +3827,9 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
* workqueues the deadlock happens when the rescuer stalls, blocking
* forward progress.
*/
- if (!from_cancel &&
- (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) {
- lock_map_acquire(&pwq->wq->lockdep_map);
- lock_map_release(&pwq->wq->lockdep_map);
- }
+ if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer))
+ touch_wq_lockdep_map(wq);
+
rcu_read_unlock();
return true;
already_gone:
@@ -3828,9 +3848,6 @@ static bool __flush_work(struct work_struct *work, bool from_cancel)
if (WARN_ON(!work->func))
return false;
- lock_map_acquire(&work->lockdep_map);
- lock_map_release(&work->lockdep_map);
-
if (start_flush_work(work, &barr, from_cancel)) {
wait_for_completion(&barr.done);
destroy_work_on_stack(&barr.work);
]
|
| 24/2 |
2026/11/19 02:06 |
tool |
grepper |
0m
Args: map[Expression:debug_locks PathPrefixes:[kernel/workqueue.c lib/debug_locks.c]]
Results: map[Output:kernel/workqueue.c-42-#include <linux/freezer.h>
kernel/workqueue.c:43:#include <linux/debug_locks.h>
kernel/workqueue.c-44-#include <linux/device/devres.h>
--
lib/debug_locks.c-2-/*
lib/debug_locks.c:3: * lib/debug_locks.c
lib/debug_locks.c-4- *
--
lib/debug_locks.c-15-#include <linux/spinlock.h>
lib/debug_locks.c:16:#include <linux/debug_locks.h>
lib/debug_locks.c-17-
--
lib/debug_locks.c-24- */
lib/debug_locks.c:25:int debug_locks __read_mostly = 1;
lib/debug_locks.c:26:EXPORT_SYMBOL_GPL(debug_locks);
lib/debug_locks.c-27-
lib/debug_locks.c-28-/*
lib/debug_locks.c:29: * The locking-testsuite uses <debug_locks_silent> to get a
lib/debug_locks.c-30- * 'silent failure': nothing is printed to the console when
--
lib/debug_locks.c-32- */
lib/debug_locks.c:33:int debug_locks_silent __read_mostly;
lib/debug_locks.c:34:EXPORT_SYMBOL_GPL(debug_locks_silent);
lib/debug_locks.c-35-
--
lib/debug_locks.c-38- */
lib/debug_locks.c:39:int debug_locks_off(void)
lib/debug_locks.c-40-{
lib/debug_locks.c:41: if (debug_locks && __debug_locks_off()) {
lib/debug_locks.c:42: if (!debug_locks_silent) {
lib/debug_locks.c-43- console_verbose();
--
lib/debug_locks.c-48-}
lib/debug_locks.c:49:EXPORT_SYMBOL_GPL(debug_locks_off);
]
|
| 25/2 |
2026/11/19 02:06 |
tool |
grepper |
0m
Args: map[Expression:debug_locks_off|__debug_locks_off|debug_locks = PathPrefixes:[kernel/locking/]]
Results: map[Output:kernel/locking/lockdep.c=189=static inline void graph_unlock(void)
--
kernel/locking/lockdep.c-197- */
kernel/locking/lockdep.c:198:static inline int debug_locks_off_graph_unlock(void)
kernel/locking/lockdep.c-199-{
kernel/locking/lockdep.c:200: int ret = debug_locks_off();
kernel/locking/lockdep.c-201-
--
kernel/locking/lockdep.c=562=static struct lock_trace *save_trace(void)
--
kernel/locking/lockdep.c-576- if (max_entries <= 0) {
kernel/locking/lockdep.c:577: if (!debug_locks_off_graph_unlock())
kernel/locking/lockdep.c-578- return NULL;
--
kernel/locking/lockdep.c=903=look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
--
kernel/locking/lockdep.c-910- instrumentation_begin();
kernel/locking/lockdep.c:911: debug_locks_off();
kernel/locking/lockdep.c-912- nbcon_cpu_emergency_enter();
--
kernel/locking/lockdep.c=999=static bool assign_lock_key(struct lockdep_map *lock)
--
kernel/locking/lockdep.c-1021- /* Debug-check: all keys must be persistent! */
kernel/locking/lockdep.c:1022: debug_locks_off();
kernel/locking/lockdep.c-1023- nbcon_cpu_emergency_enter();
--
kernel/locking/lockdep.c=1329=register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
--
kernel/locking/lockdep.c-1369- if (!class) {
kernel/locking/lockdep.c:1370: if (!debug_locks_off_graph_unlock()) {
kernel/locking/lockdep.c-1371- return NULL;
--
kernel/locking/lockdep.c=1445=static struct lock_list *alloc_list_entry(void)
--
kernel/locking/lockdep.c-1450- if (idx >= ARRAY_SIZE(list_entries)) {
kernel/locking/lockdep.c:1451: if (!debug_locks_off_graph_unlock())
kernel/locking/lockdep.c-1452- return NULL;
--
kernel/locking/lockdep.c=2049=static noinline void print_circular_bug(struct lock_list *this,
--
kernel/locking/lockdep.c-2058-
kernel/locking/lockdep.c:2059: if (!debug_locks_off_graph_unlock() || debug_locks_silent)
kernel/locking/lockdep.c-2060- return;
--
kernel/locking/lockdep.c=2092=static noinline void print_bfs_bug(int ret)
kernel/locking/lockdep.c-2093-{
kernel/locking/lockdep.c:2094: if (!debug_locks_off_graph_unlock())
kernel/locking/lockdep.c-2095- return;
--
kernel/locking/lockdep.c=2591=print_bad_irq_dependency(struct task_struct *curr,
--
kernel/locking/lockdep.c-2601-{
kernel/locking/lockdep.c:2602: if (!debug_locks_off_graph_unlock() || debug_locks_silent)
kernel/locking/lockdep.c-2603- return;
--
kernel/locking/lockdep.c=3054=print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
--
kernel/locking/lockdep.c-3058-
kernel/locking/lockdep.c:3059: if (!debug_locks_off_graph_unlock() || debug_locks_silent)
kernel/locking/lockdep.c-3060- return;
--
kernel/locking/lockdep.c=3302=check_prevs_add(struct task_struct *curr, struct held_lock *next)
--
kernel/locking/lockdep.c-3356-out_bug:
kernel/locking/lockdep.c:3357: if (!debug_locks_off_graph_unlock())
kernel/locking/lockdep.c-3358- return 0;
--
kernel/locking/lockdep.c=3774=static inline int add_chain_cache(struct task_struct *curr,
--
kernel/locking/lockdep.c-3791- if (!chain) {
kernel/locking/lockdep.c:3792: if (!debug_locks_off_graph_unlock())
kernel/locking/lockdep.c-3793- return 0;
--
kernel/locking/lockdep.c-3811- if (j < 0) {
kernel/locking/lockdep.c:3812: if (!debug_locks_off_graph_unlock())
kernel/locking/lockdep.c-3813- return 0;
--
kernel/locking/lockdep.c=3980=static void check_chain_key(struct task_struct *curr)
--
kernel/locking/lockdep.c-3989- if (chain_key != hlock->prev_chain_key) {
kernel/locking/lockdep.c:3990: debug_locks_off();
kernel/locking/lockdep.c-3991- /*
--
kernel/locking/lockdep.c-4015- if (chain_key != curr->curr_chain_key) {
kernel/locking/lockdep.c:4016: debug_locks_off();
kernel/locking/lockdep.c-4017- /*
--
kernel/locking/lockdep.c=4051=print_usage_bug(struct task_struct *curr, struct held_lock *this,
--
kernel/locking/lockdep.c-4053-{
kernel/locking/lockdep.c:4054: if (!debug_locks_off() || debug_locks_silent)
kernel/locking/lockdep.c-4055- return;
--
kernel/locking/lockdep.c=4111=print_irq_inversion_bug(struct task_struct *curr,
--
kernel/locking/lockdep.c-4119-
kernel/locking/lockdep.c:4120: if (!debug_locks_off_graph_unlock() || debug_locks_silent)
kernel/locking/lockdep.c-4121- return;
--
kernel/locking/lockdep.c=4845=print_lock_invalid_wait_context(struct task_struct *curr,
--
kernel/locking/lockdep.c-4849-
kernel/locking/lockdep.c:4850: if (!debug_locks_off())
kernel/locking/lockdep.c-4851- return 0;
--
kernel/locking/lockdep.c=5080=print_lock_nested_lock_not_held(struct task_struct *curr,
--
kernel/locking/lockdep.c-5082-{
kernel/locking/lockdep.c:5083: if (!debug_locks_off())
kernel/locking/lockdep.c-5084- return;
--
kernel/locking/lockdep.c=5124=static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
--
kernel/locking/lockdep.c-5302- if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
kernel/locking/lockdep.c:5303: debug_locks_off();
kernel/locking/lockdep.c-5304- nbcon_cpu_emergency_enter();
--
kernel/locking/lockdep.c=5323=static void print_unlock_imbalance_bug(struct task_struct *curr,
--
kernel/locking/lockdep.c-5326-{
kernel/locking/lockdep.c:5327: if (!debug_locks_off())
kernel/locking/lockdep.c-5328- return;
--
kernel/locking/lockdep.c=6100=static void print_lock_contention_bug(struct task_struct *curr,
--
kernel/locking/lockdep.c-6103-{
kernel/locking/lockdep.c:6104: if (!debug_locks_off())
kernel/locking/lockdep.c-6105- return;
--
kernel/locking/lockdep.c=6268=void lockdep_reset(void)
--
kernel/locking/lockdep.c-6278- nr_process_chains = 0;
kernel/locking/lockdep.c:6279: debug_locks = 1;
kernel/locking/lockdep.c-6280- for (i = 0; i < CHAINHASH_SIZE; i++)
--
kernel/locking/lockdep.c=6592=static void __lockdep_reset_lock(struct pending_free *pf,
--
kernel/locking/lockdep.c-6613- if (WARN_ON_ONCE(lock_class_cache_is_registered(lock)))
kernel/locking/lockdep.c:6614: debug_locks_off();
kernel/locking/lockdep.c-6615-}
--
kernel/locking/lockdep.c-6617-/*
kernel/locking/lockdep.c:6618: * Remove all information lockdep has about a lock if debug_locks == 1. Free
kernel/locking/lockdep.c-6619- * released data structures from RCU context.
--
kernel/locking/lockdep.c=6768=print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
--
kernel/locking/lockdep.c-6770-{
kernel/locking/lockdep.c:6771: if (!debug_locks_off())
kernel/locking/lockdep.c-6772- return;
--
kernel/locking/lockdep.c=6831=static void print_held_locks_bug(void)
kernel/locking/lockdep.c-6832-{
kernel/locking/lockdep.c:6833: if (!debug_locks_off())
kernel/locking/lockdep.c-6834- return;
--
kernel/locking/lockdep.c=6901=asmlinkage __visible void lockdep_sys_exit(void)
--
kernel/locking/lockdep.c-6905- if (unlikely(curr->lockdep_depth)) {
kernel/locking/lockdep.c:6906: if (!debug_locks_off())
kernel/locking/lockdep.c-6907- return;
--
kernel/locking/lockdep.c=6927=void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
--
kernel/locking/lockdep.c-6941- pr_warn("\nother info that might help us debug this:\n\n");
kernel/locking/lockdep.c:6942: pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n%s",
kernel/locking/lockdep.c-6943- !rcu_lockdep_current_cpu_online()
--
kernel/locking/lockdep.c-6946- rcu_scheduler_active, dl,
kernel/locking/lockdep.c:6947: dl ? "" : "Possible false positive due to lockdep disabling via debug_locks = 0\n");
kernel/locking/lockdep.c-6948-
--
kernel/locking/rwsem.c-75- rwsem_is_contended(sem) ? "" : "not ")) \
kernel/locking/rwsem.c:76: debug_locks_off(); \
kernel/locking/rwsem.c-77- } while (0)
--
kernel/locking/spinlock_debug.c=73=static void spin_bug(raw_spinlock_t *lock, const char *msg)
kernel/locking/spinlock_debug.c-74-{
kernel/locking/spinlock_debug.c:75: if (!debug_locks_off())
kernel/locking/spinlock_debug.c-76- return;
--
kernel/locking/spinlock_debug.c=146=static void rwlock_bug(rwlock_t *lock, const char *msg)
kernel/locking/spinlock_debug.c-147-{
kernel/locking/spinlock_debug.c:148: if (!debug_locks_off())
kernel/locking/spinlock_debug.c-149- return;
]
|
| 26/2 |
2026/11/19 02:06 |
tool |
git-log |
0m
Error:
Args:
Results:
|
| 27/2 |
2026/11/19 02:06 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 29956
output: 105
thoughts: 2455
Thoughts: **A Deep Dive into a Potential Lockdep False Positive**
Whoa, I need to stop and really examine `git show c35aea39d1e10`. The changes introduced there, particularly concerning how `process_one_work()` handles lock debugging, seem to have introduced a critical issue. Let's break down what was happening before and after this commit.
**Before `c35aea39d1e10`:**
The check for leaked locks or atomic operations was:
if (unlikely(in_atomic() || lockdep_depth(current) > 0 ||
rcu_preempt_depth() > 0)) {
// ... error reporting ...
}
This was crucial. All workqueues, at that point, ran in `worker_thread()` contexts. Critically, `lockdep_depth(current)` was consistently `0` *before* `process_one_work()` was ever called. The check `lockdep_depth(current) > 0` was therefore safe. If `debug_locks` was turned off (for example, due to lockdep hitting its limits or another CPU disabling it), `lockdep_depth(current)` would evaluate to `(debug_locks ? (current)->lockdep_depth : 0)`, which becomes `0`. Since `0 > 0` is false, disabling `debug_locks` *never* led to a false positive "BUG: workqueue leaked..." message. Everything was predictable and correct.
**After `c35aea39d1e10` (and the introduction of BH workqueues):**
This is where things get messy. The commit `c35aea39d1e10` modified the check to:
rcu_start_depth = rcu_preempt_depth();
lockdep_start_depth = lockdep_depth(current);
// ...
if (unlikely((worker->task && in_atomic()) ||
lockdep_depth(current) != lockdep_start_depth ||
rcu_preempt_depth() != rcu_start_depth)) {
// ... error reporting ...
}
Now, let's consider the scenario with a BH workqueue (`bh_worker`) running in a softirq context. This is happening on a task that *already* holds locks – imagine `current->lockdep_depth` is `4` (due to holding `rtnl_lock`, plus softirq lockdep maps, and an RCU read lock).
1. **Before `worker->current_func(work)`:** `debug_locks` is `1`. `lockdep_start_depth = lockdep_depth(current)` correctly captures this as `4`.
2. **While `worker->current_func(work)` runs:** This is the critical point. `debug_locks` gets turned off (`debug_locks_off()`). This could happen *within* `worker->current_func(work)` itself, or even concurrently on another CPU.
3. **After `worker->current_func(work)` returns:** Now, `lockdep_depth(current)` evaluates to `(debug_locks ? (current)->lockdep_depth : 0)`. Since `debug_locks` is `0`, this returns `0`. Even if all the locks acquired within `usb_giveback_urb_bh` (for example) were correctly released, or if lockdep simply stopped tracking them due to the disabling, `lockdep_depth(current)` is now `0`.
But, `lockdep_start_depth` was `4`! So, the condition `lockdep_depth(current) != lockdep_start_depth` (which is `0 != 4`) evaluates to TRUE. And then `process_one_work()` screams:
`BUG: workqueue leaked atomic, lock or RCU: syz-executor[6626] ... lock=4->0 RCU=2->2 workfn=usb_giveback_urb_bh`
Followed by: `INFO: lockdep is turned off.`
This is exactly what I'm seeing in the trace! And it aligns with `lockdep_rcu_suspicious` messages and the behavior of `lockdep_assert` and other `WARN_ON` macros in header files: they all rely on `debug_locks` being `1` to perform their checks. When `debug_locks` is `0`, these checks are bypassed, and queries like `lockdep_depth()` return `0`.
**A Moment of Pause and Deeper Investigation**
Hold on a second. I can't just jump to conclusions. While Hypothesis 1 (the bug is in `process_one_work`'s modified check) seems plausible, I *must* be absolutely certain. It's crucial to verify *every* detail.
Why did `debug_locks` become `0` during `usb_giveback_urb_bh`?
**Hypothesis 1:** The bug is indeed in `process_one_work()` (specifically in `c35aea39d1e10` or when BH workqueues were introduced). The check `lockdep_depth(current) != lockdep_start_depth` fails because it doesn't account for `debug_locks` being `0`. This leads to false-positive "BUG: workqueue leaked..." messages whenever lockdep disables itself (e.g., due to table exhaustion, which syzkaller might ignore, or a concurrent lockdep splat on another CPU) while a BH work item is executing with a non-zero `lockdep_start_depth`.
**Hypothesis 2:** Something else entirely is causing `debug_locks` to turn off *silently* without a lockdep warning, or it's causing `current->lockdep_depth` to become `0` *and* turning off `debug_locks`.
I *must* thoroughly investigate Hypothesis 2. I can't just assume Hypothesis 1 is correct without examining every possible way `debug_locks` can become `0` and scrutinizing recent commits.
**My Investigation Plan:**
1. **Understand the Environment:** What's the `HEAD` of this git tree? I need to run `git show` (for the default HEAD) and `git log` with `PathPrefix="."` to get the context.
2. **Trace `debug_locks_off()`:** I need to meticulously check *every single caller* of `debug_locks_off()` and `__debug_locks_off()` throughout the entire kernel. Can any of these callers turn off `debug_locks` silently, or perhaps with a message that syzkaller might overlook?
3. **Examine `PREEMPT_RT`:** The crash banner indicates `PREEMPT_RT` is enabled! This is a *major* factor. I need to understand how `PREEMPT_RT` interacts with:
* `rtmutex`
* `local_lock`
* `spinlock_rt`
* And specifically, how `usb_giveback_urb_bh` behaves under `PREEMPT_RT`.
I've noticed a critical detail in `drivers/usb/core/hcd.c` (around line 1680):
`spin_lock_irq(&bh->lock);`
This is *highly suspicious* when `PREEMPT_RT` is involved!
* In `PREEMPT_RT`, `spinlock_t` often becomes a sleeping lock (`rt_mutex`).
* How do softirqs work with `PREEMPT_RT`? `handle_softirqs` and `local_bh_enable` run in thread context. `local_bh_disable` acquires a per-CPU sleeping lock (`local_lock`), and `preempt_count()` is `0x00000000` (matching the `preempt=0x00000000` in the crash report!).
* The `RCU=2->2` in the crash report also needs attention. On `PREEMPT_RT`, `rcu_read_lock()` increments `rcu_preempt_depth()`, and holding RCU read locks is common when softirqs or `rtnetlink` are involved.
* The crucial question: On `PREEMPT_RT`, can `spin_lock_irq(&bh->lock)` (which becomes `rt_spin_lock(&bh->lock)`) be called while holding an `rcu_read_lock()`? Does lockdep flag this, or does something else happen?
* If `spin_lock_irq` *had* triggered a lockdep bug related to an invalid wait context, lockdep would typically print `[ BUG: Invalid wait context ]`. Does syzkaller catch this? Or is there another mechanism at play?
My immediate next step is to review the git log and thoroughly analyze all callers of `debug_locks_off()`. I cannot afford to overlook any possibility.
|
| 28/2 |
2026/11/19 02:06 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca
Merge: 7ccb044c2772 603b4fd69b4e
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date: Tue Nov 17 18:55:10 2026 -0800
Merge tag 'arc-fixes-7.4' of git://git.kernel.org/pub/scm/linux/kernel/git/vgupta/arc
Pull ARC fix from Vineet Gupta:
- workaround a GCC bug which has been causing upstream build fails
* tag 'arc-fixes-7.4' of git://git.kernel.org/pub/scm/linux/kernel/git/vgupta/arc:
ARC: io: work around GCC PR127718 in __raw_write{w,l}()
arch/arc/include/asm/io.h | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
]
|
| 29/2 |
2026/11/19 02:06 |
tool |
git-log |
0m
Args: map[Count:30 PathPrefix:.]
Results: map[Output:c3f98ff6309a rcu/nocb: Avoid unsequenced accesses to jiffies in show_rcu_nocb_state()
38828d44f31e rcu-tasks: Use pr_debug() for callback contend/collapse messages
aebb72744764 Linux 7.4-rc3
acec9637e03c cgroup/cpuset: Don't revive invalid partition without active CPU on hotplug
42ef815ff6b2 cgroup/cpuset: Remove redundant WARN_ON_ONCE() in remote_cpus_update()
7693e1350b06 memcg: make MEMCG select BLK_CGROUP
0dc2a4dac3d0 x86/mm: Ensure write visibility during LDT installation
2d9bcd380585 mm/gup: limit pagecache folio refcount when grabbing references
b8ba6cc99432 iommufd: fix cleanup when adding memfd folio pins fails
317bd7d28b24 mm/mmu_gather: flush pending references early before the folio limit
c6c9a4c1c9a1 mm: limit pagecache folio refcount when splicing into pipes
26b8f029ce52 mm: limit pagecache folio refcount when mapping pages
603b4fd69b4e ARC: io: work around GCC PR127718 in __raw_write{w,l}()
e92cd41e8e5e dm: fail a REQ_PREFLUSH bio with data when its preflush fails
b5c5b67e0240 hwmon: (aht10) Fix update_interval overflow on 64-bit
68af85b730f9 hwmon: (scmi) Configure sensors before exposing channels
a2996d892d01 hwmon: (scmi) Check channel config allocation
ca1abdd1a9b9 hwmon: (nct6775) Handle dual use registers
3ae1af2faad9 hwmon: (scmi) Enable temperature sensors using their sensor IDs
00944852275e hwmon: (pmbus/mp2975) Get vout fault limits dynamically
fc8519fab34e hwmon: (sy7636a) fix negative temperatures
e234ed1bb82c cpuset: generalize the validate_partition() interface
c1ad01173511 cpuset: settle isolation state when CPUs are given back to parent
ad3fc05c7e65 arm64: suspend: Only track cpuidle context for idle tasks
2a490521d74a spi: axi-spi-engine: Start the program length at zero
5306f124f317 arm64: ptrace: don't warn on missing siginfo in a group-stop
c15120853eeb tracing/uprobes: Fix memory leak of tail in __trace_uprobe_create()
0941144ebeaf tracing/probes: Reject array count on non-dereference string sources
2721573e6c6f tracing/probes: Fix user-space dereference string array store type
92d9fa0ed69e tracing/eprobe: Disallow attaching eprobes to fprobe events
]
|
| 30/2 |
2026/11/19 02:06 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:drivers/usb/core/hcd.c]
Results: map[Output:d5559f43d76b usb: core: hcd: fix possible deadlock in rh control transfers
d1e280334b7f usb: core: Fix SuperSpeed root hub wMaxPacketSize
b2f6648c7356 usb: hcd: queue wakeup_work to system_freezable_wq workqueue
32a92f8c8932 Convert more 'alloc_obj' cases to default GFP_KERNEL arguments
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
f9de0dd246ed USB: HCD: remove logic about which hcd is loaded
a0da19eda98c usb: core: Drop spaces after function names
70fb252a84a4 USB: core: Update kerneldoc for usb_hcd_giveback_urb()
9528d32873b3 kcov, usb: Don't disable interrupts in kcov_remote_start_usb_softirq()
8fe06185e11a usb: core: hcd: fix accessing unmapped memory in SINGLE_STEP_SET_FEATURE test
488e6eaab88c usb: core: add dma-noncoherent buffer alloc and free API
41cb08555c41 treewide, timers: Rename from_timer() to timer_container_of()
8fa7292fee5c treewide: Switch/rename to timer_delete[_sync]()
f4aa6caa8b42 usb: core: Don't use %pK through printk
70cd0576aa39 usb: hcd: Bump local buffer size in rh_string()
d2ec94fbc431 usb: core: hcd: only check primary hcd skip_phy_initialization
5f60d5f6bbc1 move asm/unaligned.h to linux/unaligned.h
f85d39dd7ed8 kcov, usb: disable interrupts in kcov_remote_start_usb_softirq
562be61b35d9 usb: core: Remove the useless struct usb_devmap which is just a bitmap
]
|
| 31/2 |
2026/11/19 02:06 |
tool |
grepper |
0m
Args: map[Expression:debug_locks_off|__debug_locks_off]
Results: map[Output:
Full output is too long, showing 200 out of 231 lines.
Use more precise expression or PathPrefixes if possible.
Number of matching lines per file (10 files in total):
arch/powerpc/kernel/traps.c:1
arch/s390/kernel/ipl.c:1
arch/s390/kernel/machine_kexec.c:1
include/linux/debug_locks.h:3
kernel/debug/debug_core.c:1
kernel/locking/lockdep.c:27
kernel/locking/rwsem.c:1
kernel/locking/spinlock_debug.c:2
kernel/panic.c:3
lib/debug_locks.c:3
arch/powerpc/kernel/traps.c=173=void panic_flush_kmsg_end(void)
--
arch/powerpc/kernel/traps.c-176- bust_spinlocks(0);
arch/powerpc/kernel/traps.c:177: debug_locks_off();
arch/powerpc/kernel/traps.c-178- console_flush_on_panic(CONSOLE_FLUSH_PENDING);
--
arch/s390/kernel/ipl.c=2174=void do_restart(void *arg)
--
arch/s390/kernel/ipl.c-2176- tracing_off();
arch/s390/kernel/ipl.c:2177: debug_locks_off();
arch/s390/kernel/ipl.c-2178- lgr_info_log();
--
arch/s390/kernel/machine_kexec.c=251=static void __machine_kexec(void *data)
--
arch/s390/kernel/machine_kexec.c-254- tracing_off();
arch/s390/kernel/machine_kexec.c:255: debug_locks_off();
arch/s390/kernel/machine_kexec.c-256-#ifdef CONFIG_CRASH_DUMP
--
include/linux/debug_locks.h=11=extern int debug_locks_silent __read_mostly;
--
include/linux/debug_locks.h-13-
include/linux/debug_locks.h:14:static __always_inline int __debug_locks_off(void)
include/linux/debug_locks.h-15-{
--
include/linux/debug_locks.h-21- */
include/linux/debug_locks.h:22:extern int debug_locks_off(void);
include/linux/debug_locks.h-23-
--
include/linux/debug_locks.h-29- instrumentation_begin(); \
include/linux/debug_locks.h:30: if (debug_locks_off() && !debug_locks_silent) \
include/linux/debug_locks.h-31- WARN(1, "DEBUG_LOCKS_WARN_ON(%s)", #c); \
--
kernel/debug/debug_core.c=986=void kgdb_panic(const char *msg)
--
kernel/debug/debug_core.c-998-
kernel/debug/debug_core.c:999: debug_locks_off();
kernel/debug/debug_core.c-1000- console_flush_on_panic(CONSOLE_FLUSH_PENDING);
--
kernel/locking/lockdep.c=189=static inline void graph_unlock(void)
--
kernel/locking/lockdep.c-197- */
kernel/locking/lockdep.c:198:static inline int debug_locks_off_graph_unlock(void)
kernel/locking/lockdep.c-199-{
kernel/locking/lockdep.c:200: int ret = debug_locks_off();
kernel/locking/lockdep.c-201-
--
kernel/locking/lockdep.c=562=static struct lock_trace *save_trace(void)
--
kernel/locking/lockdep.c-576- if (max_entries <= 0) {
kernel/locking/lockdep.c:577: if (!debug_locks_off_graph_unlock())
kernel/locking/lockdep.c-578- return NULL;
--
kernel/locking/lockdep.c=903=look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
--
kernel/locking/lockdep.c-910- instrumentation_begin();
kernel/locking/lockdep.c:911: debug_locks_off();
kernel/locking/lockdep.c-912- nbcon_cpu_emergency_enter();
--
kernel/locking/lockdep.c=999=static bool assign_lock_key(struct lockdep_map *lock)
--
kernel/locking/lockdep.c-1021- /* Debug-check: all keys must be persistent! */
kernel/locking/lockdep.c:1022: debug_locks_off();
kernel/locking/lockdep.c-1023- nbcon_cpu_emergency_enter();
--
kernel/locking/lockdep.c=1329=register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
--
kernel/locking/lockdep.c-1369- if (!class) {
kernel/locking/lockdep.c:1370: if (!debug_locks_off_graph_unlock()) {
kernel/locking/lockdep.c-1371- return NULL;
--
kernel/locking/lockdep.c=1445=static struct lock_list *alloc_list_entry(void)
--
kernel/locking/lockdep.c-1450- if (idx >= ARRAY_SIZE(list_entries)) {
kernel/locking/lockdep.c:1451: if (!debug_locks_off_graph_unlock())
kernel/locking/lockdep.c-1452- return NULL;
--
kernel/locking/lockdep.c=2049=static noinline void print_circular_bug(struct lock_list *this,
--
kernel/locking/lockdep.c-2058-
kernel/locking/lockdep.c:2059: if (!debug_locks_off_graph_unlock() || debug_locks_silent)
kernel/locking/lockdep.c-2060- return;
--
kernel/locking/lockdep.c=2092=static noinline void print_bfs_bug(int ret)
kernel/locking/lockdep.c-2093-{
kernel/locking/lockdep.c:2094: if (!debug_locks_off_graph_unlock())
kernel/locking/lockdep.c-2095- return;
--
kernel/locking/lockdep.c=2591=print_bad_irq_dependency(struct task_struct *curr,
--
kernel/locking/lockdep.c-2601-{
kernel/locking/lockdep.c:2602: if (!debug_locks_off_graph_unlock() || debug_locks_silent)
kernel/locking/lockdep.c-2603- return;
--
kernel/locking/lockdep.c=3054=print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
--
kernel/locking/lockdep.c-3058-
kernel/locking/lockdep.c:3059: if (!debug_locks_off_graph_unlock() || debug_locks_silent)
kernel/locking/lockdep.c-3060- return;
--
kernel/locking/lockdep.c=3302=check_prevs_add(struct task_struct *curr, struct held_lock *next)
--
kernel/locking/lockdep.c-3356-out_bug:
kernel/locking/lockdep.c:3357: if (!debug_locks_off_graph_unlock())
kernel/locking/lockdep.c-3358- return 0;
--
kernel/locking/lockdep.c=3774=static inline int add_chain_cache(struct task_struct *curr,
--
kernel/locking/lockdep.c-3791- if (!chain) {
kernel/locking/lockdep.c:3792: if (!debug_locks_off_graph_unlock())
kernel/locking/lockdep.c-3793- return 0;
--
kernel/locking/lockdep.c-3811- if (j < 0) {
kernel/locking/lockdep.c:3812: if (!debug_locks_off_graph_unlock())
kernel/locking/lockdep.c-3813- return 0;
--
kernel/locking/lockdep.c=3980=static void check_chain_key(struct task_struct *curr)
--
kernel/locking/lockdep.c-3989- if (chain_key != hlock->prev_chain_key) {
kernel/locking/lockdep.c:3990: debug_locks_off();
kernel/locking/lockdep.c-3991- /*
--
kernel/locking/lockdep.c-4015- if (chain_key != curr->curr_chain_key) {
kernel/locking/lockdep.c:4016: debug_locks_off();
kernel/locking/lockdep.c-4017- /*
--
kernel/locking/lockdep.c=4051=print_usage_bug(struct task_struct *curr, struct held_lock *this,
--
kernel/locking/lockdep.c-4053-{
kernel/locking/lockdep.c:4054: if (!debug_locks_off() || debug_locks_silent)
kernel/locking/lockdep.c-4055- return;
--
kernel/locking/lockdep.c=4111=print_irq_inversion_bug(struct task_struct *curr,
--
kernel/locking/lockdep.c-4119-
kernel/locking/lockdep.c:4120: if (!debug_locks_off_graph_unlock() || debug_locks_silent)
kernel/locking/lockdep.c-4121- return;
--
kernel/locking/lockdep.c=4845=print_lock_invalid_wait_context(struct task_struct *curr,
--
kernel/locking/lockdep.c-4849-
kernel/locking/lockdep.c:4850: if (!debug_locks_off())
kernel/locking/lockdep.c-4851- return 0;
--
kernel/locking/lockdep.c=5080=print_lock_nested_lock_not_held(struct task_struct *curr,
--
kernel/locking/lockdep.c-5082-{
kernel/locking/lockdep.c:5083: if (!debug_locks_off())
kernel/locking/lockdep.c-5084- return;
--
kernel/locking/lockdep.c=5124=static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
--
kernel/locking/lockdep.c-5302- if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
kernel/locking/lockdep.c:5303: debug_locks_off();
kernel/locking/lockdep.c-5304- nbcon_cpu_emergency_enter();
--
kernel/locking/lockdep.c=5323=static void print_unlock_imbalance_bug(struct task_struct *curr,
--
kernel/locking/lockdep.c-5326-{
kernel/locking/lockdep.c:5327: if (!debug_locks_off())
kernel/locking/lockdep.c-5328- return;
--
kernel/locking/lockdep.c=6100=static void print_lock_contention_bug(struct task_struct *curr,
--
kernel/locking/lockdep.c-6103-{
kernel/locking/lockdep.c:6104: if (!debug_locks_off())
kernel/locking/lockdep.c-6105- return;
--
kernel/locking/lockdep.c=6592=static void __lockdep_reset_lock(struct pending_free *pf,
--
kernel/locking/lockdep.c-6613- if (WARN_ON_ONCE(lock_class_cache_is_registered(lock)))
kernel/locking/lockdep.c:6614: debug_locks_off();
kernel/locking/lockdep.c-6615-}
--
kernel/locking/lockdep.c=6768=print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
--
kernel/locking/lockdep.c-6770-{
kernel/locking/lockdep.c:6771: if (!debug_locks_off())
kernel/locking/lockdep.c-6772- return;
--
kernel/locking/lockdep.c=6831=static void print_held_locks_bug(void)
kernel/locking/lockdep.c-6832-{
kernel/locking/lockdep.c:6833: if (!debug_locks_off())
kernel/locking/lockdep.c-6834- return;
--
kernel/locking/lockdep.c=6901=asmlinkage __visible void lockdep_sys_exit(void)
--
kernel/locking/lockdep.c-6905- if (unlikely(curr->lockdep_depth)) {
kernel/locking/lockdep.c:6906: if (!debug_locks_off())
kernel/locking/lockdep.c-6907- return;
--
kernel/locking/rwsem.c-75- rwsem_is_contended(sem) ? "" : "not ")) \
kernel/locking/rwsem.c:76: debug_locks_off(); \
kernel/locking/rwsem.c-77- } while (0)
--
kernel/locking/spinlock_debug.c=73=static void spin_bug(raw_spinlock_t *lock, const char *msg)
kernel/locking/spinlock_debug.c-74-{
kernel/locking/spinlock_debug.c:75: if (!debug_locks_off())
kernel/locking/spinlock_debug.c-76- return;
--
kernel/locking/spinlock_debug.c=146=static void rwlock_bug(rwlock_t *lock, const char *msg)
]
|
| 32/2 |
2026/11/19 02:06 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 37868
output: 233
thoughts: 359
Thoughts: **The "Not Tainted" Revelation: A Deep Dive into Lock Debugging**
I just had a moment of profound realization while staring at this crash report. Initially, the `BUG: workqueue leaked atomic, lock or RCU: syz-executor[6626]` caught my eye, with the details about `preempt=0x00000000 lock=4->0 RCU=2->2 workfn=usb_giveback_urb_bh`. All standard fare for lock-related debugging. But then, it hit me. I need to look *very* carefully at the `CPU: 0 UID: 0 PID: 6626 Comm: syz-executor Not tainted syzkaller #0 PREEMPT_{RT,(full)}` line.
The crucial piece of information, the one that stopped me dead in my tracks, is `Not tainted`. I need to internalize this. I will read that phrase – **`Not tainted syzkaller #0 PREEMPT_{RT,(full)}`** – a million times. This is the linchpin.
This `Not tainted` status immediately raises a critical question: What exactly is it that *taints* the kernel when `debug_locks_off()` is called? My immediate next step is to scrutinize every single caller of `debug_locks_off()`. I need to meticulously examine each of these calls to determine which ones lead to the kernel becoming tainted (flagged with `WARN`, `TAINT_WARN`, etc.) and, critically, which ones *do not*.
To achieve this, I will be diving deep into the source code. Specifically, I'll be poring over `lib/debug_locks.c`, `kernel/panic.c`, and all the relevant lines within `kernel/locking/lockdep.c`. I'm talking about the functions like `save_trace`, `register_lock_class`, `alloc_list_entry`, `add_chain_cache`, `check_prevs_add`, `print_*_bug`, and `DEBUG_LOCKS_WARN_ON`. My plan is to virtually "read-file" these sections of the code immediately to gain a granular understanding of the taint propagation mechanisms, or lack thereof, in the context of `debug_locks_off()`. This is where the real insight will come from.
|
| 33/2 |
2026/11/19 02:07 |
tool |
read-file |
0m
Args: map[File:lib/debug_locks.c FirstLine:1 LineCount:60]
Results: map[Contents: 1: // SPDX-License-Identifier: GPL-2.0-only
2: /*
3: * lib/debug_locks.c
4: *
5: * Generic place for common debugging facilities for various locks:
6: * spinlocks, rwlocks, mutexes and rwsems.
7: *
8: * Started by Ingo Molnar:
9: *
10: * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
11: */
12: #include <linux/rwsem.h>
13: #include <linux/mutex.h>
14: #include <linux/export.h>
15: #include <linux/spinlock.h>
16: #include <linux/debug_locks.h>
17:
18: /*
19: * We want to turn all lock-debugging facilities on/off at once,
20: * via a global flag. The reason is that once a single bug has been
21: * detected and reported, there might be cascade of followup bugs
22: * that would just muddy the log. So we report the first one and
23: * shut up after that.
24: */
25: int debug_locks __read_mostly = 1;
26: EXPORT_SYMBOL_GPL(debug_locks);
27:
28: /*
29: * The locking-testsuite uses <debug_locks_silent> to get a
30: * 'silent failure': nothing is printed to the console when
31: * a locking bug is detected.
32: */
33: int debug_locks_silent __read_mostly;
34: EXPORT_SYMBOL_GPL(debug_locks_silent);
35:
36: /*
37: * Generic 'turn off all lock debugging' function:
38: */
39: int debug_locks_off(void)
40: {
41: if (debug_locks && __debug_locks_off()) {
42: if (!debug_locks_silent) {
43: console_verbose();
44: return 1;
45: }
46: }
47: return 0;
48: }
49: EXPORT_SYMBOL_GPL(debug_locks_off);
]
|
| 34/2 |
2026/11/19 02:07 |
tool |
read-file |
0m
Args: map[File:kernel/locking/lockdep.c FirstLine:560 LineCount:40]
Results: map[Contents: 560: }
561:
562: static struct lock_trace *save_trace(void)
563: {
564: struct lock_trace *trace, *t2;
565: struct hlist_head *hash_head;
566: u32 hash;
567: int max_entries;
568:
569: BUILD_BUG_ON_NOT_POWER_OF_2(STACK_TRACE_HASH_SIZE);
570: BUILD_BUG_ON(LOCK_TRACE_SIZE_IN_LONGS >= MAX_STACK_TRACE_ENTRIES);
571:
572: trace = (struct lock_trace *)(stack_trace + nr_stack_trace_entries);
573: max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries -
574: LOCK_TRACE_SIZE_IN_LONGS;
575:
576: if (max_entries <= 0) {
577: if (!debug_locks_off_graph_unlock())
578: return NULL;
579:
580: nbcon_cpu_emergency_enter();
581: print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
582: dump_stack();
583: nbcon_cpu_emergency_exit();
584:
585: return NULL;
586: }
587: trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3);
588:
589: hash = jhash(trace->entries, trace->nr_entries *
590: sizeof(trace->entries[0]), 0);
591: trace->hash = hash;
592: hash_head = stack_trace_hash + (hash & (STACK_TRACE_HASH_SIZE - 1));
593: hlist_for_each_entry(t2, hash_head, hash_entry) {
594: if (traces_identical(trace, t2))
595: return t2;
596: }
597: nr_stack_trace_entries += LOCK_TRACE_SIZE_IN_LONGS + trace->nr_entries;
598: hlist_add_head(&trace->hash_entry, hash_head);
599:
]
|
| 35/2 |
2026/11/19 02:07 |
tool |
read-file |
0m
Args: map[File:kernel/locking/lockdep.c FirstLine:1365 LineCount:35]
Results: map[Contents:1365:
1366: /* Allocate a new lock class and add it to the hash. */
1367: class = list_first_entry_or_null(&free_lock_classes, typeof(*class),
1368: lock_entry);
1369: if (!class) {
1370: if (!debug_locks_off_graph_unlock()) {
1371: return NULL;
1372: }
1373:
1374: nbcon_cpu_emergency_enter();
1375: print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
1376: dump_stack();
1377: nbcon_cpu_emergency_exit();
1378: return NULL;
1379: }
1380: nr_lock_classes++;
1381: __set_bit(class - lock_classes, lock_classes_in_use);
1382: debug_atomic_inc(nr_unused_locks);
1383: class->key = key;
1384: class->name = lock->name;
1385: class->subclass = subclass;
1386: WARN_ON_ONCE(!list_empty(&class->locks_before));
1387: WARN_ON_ONCE(!list_empty(&class->locks_after));
1388: class->name_version = count_matching_names(class);
1389: class->wait_type_inner = lock->wait_type_inner;
1390: class->wait_type_outer = lock->wait_type_outer;
1391: class->lock_type = lock->lock_type;
1392: /*
1393: * We use RCU's safe list-add method to make
1394: * parallel walking of the hash-list safe:
1395: */
1396: hlist_add_head_rcu(&class->hash_entry, hash_head);
1397: /*
1398: * Remove the class from the free list and add it to the global list
1399: * of classes.
]
|
| 36/2 |
2026/11/19 02:07 |
tool |
read-file |
0m
Args: map[File:kernel/locking/lockdep.c FirstLine:1445 LineCount:30]
Results: map[Contents:1445: static struct lock_list *alloc_list_entry(void)
1446: {
1447: int idx = find_first_zero_bit(list_entries_in_use,
1448: ARRAY_SIZE(list_entries));
1449:
1450: if (idx >= ARRAY_SIZE(list_entries)) {
1451: if (!debug_locks_off_graph_unlock())
1452: return NULL;
1453:
1454: nbcon_cpu_emergency_enter();
1455: print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
1456: dump_stack();
1457: nbcon_cpu_emergency_exit();
1458: return NULL;
1459: }
1460: nr_list_entries++;
1461: __set_bit(idx, list_entries_in_use);
1462: return list_entries + idx;
1463: }
1464:
1465: /*
1466: * Add a new dependency to the head of the list:
1467: */
1468: static int add_lock_to_list(struct lock_class *this,
1469: struct lock_class *links_to, struct list_head *head,
1470: u16 distance, u8 dep,
1471: const struct lock_trace *trace)
1472: {
1473: struct lock_list *entry;
1474: /*
]
|
| 37/2 |
2026/11/19 02:07 |
tool |
read-file |
0m
Args: map[File:kernel/locking/lockdep.c FirstLine:3785 LineCount:50]
Results: map[Contents:3785: * lockdep won't complain about its own locking errors.
3786: */
3787: if (lockdep_assert_locked())
3788: return 0;
3789:
3790: chain = alloc_lock_chain();
3791: if (!chain) {
3792: if (!debug_locks_off_graph_unlock())
3793: return 0;
3794:
3795: nbcon_cpu_emergency_enter();
3796: print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
3797: dump_stack();
3798: nbcon_cpu_emergency_exit();
3799: return 0;
3800: }
3801: chain->chain_key = chain_key;
3802: chain->irq_context = hlock->irq_context;
3803: i = get_first_held_lock(curr, hlock);
3804: chain->depth = curr->lockdep_depth + 1 - i;
3805:
3806: BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
3807: BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
3808: BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
3809:
3810: j = alloc_chain_hlocks(chain->depth);
3811: if (j < 0) {
3812: if (!debug_locks_off_graph_unlock())
3813: return 0;
3814:
3815: nbcon_cpu_emergency_enter();
3816: print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
3817: dump_stack();
3818: nbcon_cpu_emergency_exit();
3819: return 0;
3820: }
3821:
3822: chain->base = j;
3823: for (j = 0; j < chain->depth - 1; j++, i++) {
3824: int lock_id = hlock_id(curr->held_locks + i);
3825:
3826: chain_hlocks[chain->base + j] = lock_id;
3827: }
3828: chain_hlocks[chain->base + j] = hlock_id(hlock);
3829: hlist_add_head_rcu(&chain->entry, hash_head);
3830: debug_atomic_inc(chain_lookup_misses);
3831: inc_chains(chain->irq_context);
3832:
3833: return 1;
3834: }
]
|
| 38/2 |
2026/11/19 02:07 |
tool |
read-file |
0m
Args: map[File:kernel/locking/lockdep.c FirstLine:5295 LineCount:30]
Results: map[Contents:5295: curr->curr_chain_key = chain_key;
5296: curr->lockdep_depth++;
5297: check_chain_key(curr);
5298: #ifdef CONFIG_DEBUG_LOCKDEP
5299: if (unlikely(!debug_locks))
5300: return 0;
5301: #endif
5302: if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
5303: debug_locks_off();
5304: nbcon_cpu_emergency_enter();
5305: print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
5306: printk(KERN_DEBUG "depth: %i max: %lu!\n",
5307: curr->lockdep_depth, MAX_LOCK_DEPTH);
5308:
5309: lockdep_print_held_locks(current);
5310: debug_show_all_locks();
5311: dump_stack();
5312: nbcon_cpu_emergency_exit();
5313:
5314: return 0;
5315: }
5316:
5317: if (unlikely(curr->lockdep_depth > max_lockdep_depth))
5318: max_lockdep_depth = curr->lockdep_depth;
5319:
5320: return 1;
5321: }
5322:
5323: static void print_unlock_imbalance_bug(struct task_struct *curr,
5324: struct lockdep_map *lock,
]
|
| 39/2 |
2026/11/19 02:07 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 42095
output: 143
thoughts: 662
Thoughts: **My Deep Dive into Lockdep Taint Behavior**
Okay, I need to really get to the bottom of what "not tainted" means in the context of lockdep. My initial thought is it directly equates to no `WARN` or `DEBUG_LOCKS_WARN_ON` having occurred. I recall that `DEBUG_LOCKS_WARN_ON` explicitly calls `WARN(1, ...)` which, I'm confident, sets the `TAINT_WARN` flag. So, that connection seems solid.
Now, what about those `print_lockdep_off(...)` messages? These mention things like `MAX_STACK_TRACE_ENTRIES`, `MAX_LOCKDEP_KEYS`, `MAX_LOCKDEP_ENTRIES`, `MAX_LOCKDEP_CHAINS`, `MAX_LOCKDEP_CHAIN_HLOCKS`, and `MAX_LOCK_DEPTH`. I need to verify if calling `print_lockdep_off` in `kernel/locking/lockdep.c` actually taints the kernel. A quick check reveals it *only* calls `printk(...)`, so it doesn't seem to introduce a taint on its own. Furthermore, I'm looking at syzkaller's `pkg/report/linux.go` and I see that all messages like `BUG: MAX_LOCKDEP_* too low!`, `BUG: MAX_STACK_TRACE_ENTRIES too low!`, and `BUG: MAX_LOCK_DEPTH too low!` are listed under `ignores`. This confirms syzkaller deliberately bypasses these specific messages, which further supports the idea that they don't represent a kernel taint.
But wait, I can't stop there. I need to be thorough. I should also examine other functions in `kernel/locking/lockdep.c` like `print_circular_bug` and `print_lock_invalid_wait_context`. Do *those* taint the kernel? I'll cross-reference their implementations with `add_taint(TAINT_WARN, ...)` in the same file.
And I also need to consider the possibility that specific commits might have *caused* `debug_locks_off()`. I'm thinking about `02c6be7d675b` ("locking/lockdep: Invalidate stale class_cache entries for zapped classes"), `8f2109843137` ("locking/lockdep: add sequence counter to held_lock"), and `aa63c0df7992`. Could any of these have inadvertently triggered a state leading to `debug_locks_off()`?
This line of thinking is triggering another thought: I *just* reviewed recent commits to `kernel/locking/lockdep.c` earlier. I should go back and re-examine those in light of my current concerns. Specifically, I'm referring to these:
* `aa63c0df7992` locking/lockdep: Fix inverted "on"/"last" CPU label in lockdep_print_held_locks()
* `02c6be7d675b` locking/lockdep: Invalidate stale class_cache entries for zapped classes
* `8f2109843137` locking/lockdep: add sequence counter to held_lock
* `7577e00b9ab5` locking/lockdep: Fix NULL pointer dereference in __lock_set_class()
My current approach is to meticulously inspect `print_lockdep_off`, `add_taint` within `lockdep.c`, and then re-evaluate those specific recent lockdep commits. On top of that, I need to trace back when `c35aea39d1e10` and the BH workqueue mechanism (particularly `usb_giveback_urb_bh`) were introduced. I think the best way to tackle this complex web of dependencies and potential causes is to initiate parallel tool calls right now to gather all this information concurrently.
|
| 40/2 |
2026/11/19 02:07 |
tool |
grepper |
0m
Args: map[Expression:add_taint|print_lockdep_off PathPrefixes:[kernel/locking/lockdep.c]]
Results: map[Output:kernel/locking/lockdep.c=514=static int verbose(struct lock_class *class)
--
kernel/locking/lockdep.c-521-
kernel/locking/lockdep.c:522:static void print_lockdep_off(const char *bug_msg)
kernel/locking/lockdep.c-523-{
--
kernel/locking/lockdep.c=562=static struct lock_trace *save_trace(void)
--
kernel/locking/lockdep.c-580- nbcon_cpu_emergency_enter();
kernel/locking/lockdep.c:581: print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
kernel/locking/lockdep.c-582- dump_stack();
--
kernel/locking/lockdep.c=1329=register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
--
kernel/locking/lockdep.c-1374- nbcon_cpu_emergency_enter();
kernel/locking/lockdep.c:1375: print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
kernel/locking/lockdep.c-1376- dump_stack();
--
kernel/locking/lockdep.c=1445=static struct lock_list *alloc_list_entry(void)
--
kernel/locking/lockdep.c-1454- nbcon_cpu_emergency_enter();
kernel/locking/lockdep.c:1455: print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
kernel/locking/lockdep.c-1456- dump_stack();
--
kernel/locking/lockdep.c=3774=static inline int add_chain_cache(struct task_struct *curr,
--
kernel/locking/lockdep.c-3795- nbcon_cpu_emergency_enter();
kernel/locking/lockdep.c:3796: print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
kernel/locking/lockdep.c-3797- dump_stack();
--
kernel/locking/lockdep.c-3815- nbcon_cpu_emergency_enter();
kernel/locking/lockdep.c:3816: print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
kernel/locking/lockdep.c-3817- dump_stack();
--
kernel/locking/lockdep.c=5124=static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
--
kernel/locking/lockdep.c-5304- nbcon_cpu_emergency_enter();
kernel/locking/lockdep.c:5305: print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
kernel/locking/lockdep.c-5306- printk(KERN_DEBUG "depth: %i max: %lu!\n",
]
|
| 41/2 |
2026/11/19 02:07 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 02c6be7d675b21d81f0ba3a524346850a8c0e3bf
Author: Eric Dumazet <edumazet@kernel.org>
Date: Mon Aug 24 15:51:29 2026 +0000
locking/lockdep: Invalidate stale class_cache entries for zapped classes
syzbot reported a lockdep splat hitting DEBUG_LOCKS_WARN_ON(1) in
hlock_class() due to an invalid class_idx:
WARNING: kernel/locking/lockdep.c:238 at __lock_acquire+0x382/0x2cf0 kernel/locking/lockdep.c:5203
Workqueue: wg-crypt-wg0 wg_packet_tx_worker
RIP: 0010:hlock_class kernel/locking/lockdep.c:238 [inline]
RIP: 0010:check_wait_context kernel/locking/lockdep.c:4870 [inline]
RIP: 0010:__lock_acquire+0x389/0x2cf0 kernel/locking/lockdep.c:5203
Call Trace:
<IRQ>
lock_acquire+0x106/0x350 kernel/locking/lockdep.c:5886
_raw_spin_lock+0x2e/0x40 kernel/locking/spinlock.c:173
tcp_tsq_handler+0x29/0x200 net/ipv4/tcp_output.c:1291
tcp_tsq_workfn+0x384/0x410 net/ipv4/tcp_output.c:1325
...
When a lock class is zapped (e.g. during module unload or key
unregistration), zap_class() clears the class's bit in
lock_classes_in_use and removes it from the class hash table.
However, existing lockdep_map instances embedded in data structures
may still retain a pointer to the zapped class in their class_cache[]
array.
When __lock_acquire() subsequently runs on such a lock, it finds
lock->class_cache[subclass] != NULL, skipping register_lock_class()
and assigning hlock->class_idx to the index of the zapped class. When
check_wait_context() or hlock_class() inspects the held_lock, it finds
!test_bit(class_idx, lock_classes_in_use) and warns. Furthermore, if
the zapped slot is subsequently re-allocated to an unrelated lock key,
the stale class_cache entry would erroneously match the unrelated
class (ABA issue).
Add lock_class_cache_is_valid() to validate that the cached class is
within lock_classes bounds, still allocated in lock_classes_in_use
(using uninstrumented arch_test_bit() in __always_inline context so it
is safe in noinstr contexts like match_held_lock()), and that
class->key matches the expected subkey (taking lockdep_set_subclass()
overrides into account). Also use READ_ONCE()/WRITE_ONCE() when
accessing class_cache[]. If the entry is invalid or stale, fall back
to register_lock_class() / look_up_lock_class().
Fixes: a0b0fd53e1e6 ("locking/lockdep: Free lock classes that are no longer in use")
Closes: https://lore.kernel.org/netdev/6a8c66dc.4d75e56a.c9a88.0050.GAE@google.com/T/#u
Reported-by: syzbot+2d770620059281e225a4@syzkaller.appspotmail.com
Assisted-by: Gemini:gemini-3.1-pro
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20260824155129.676096-1-edumazet@google.com
diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
index 25d77d4a1061..763f79806bd8 100644
--- a/kernel/locking/lockdep.c
+++ b/kernel/locking/lockdep.c
@@ -963,6 +963,34 @@ look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
return NULL;
}
+static __always_inline bool lock_class_cache_is_valid(const struct lockdep_map *lock,
+ const struct lock_class *class,
+ unsigned int subclass)
+{
+ unsigned int class_subclass;
+
+ if (!class)
+ return false;
+
+ if (unlikely(class < lock_classes || class >= lock_classes + MAX_LOCKDEP_KEYS))
+ return false;
+
+ if (unlikely(!arch_test_bit(class - lock_classes, lock_classes_in_use)))
+ return false;
+
+ if (unlikely(!lock->key))
+ return false;
+
+ class_subclass = subclass ? subclass : class->subclass;
+ if (unlikely(class_subclass >= MAX_LOCKDEP_SUBCLASSES))
+ return false;
+
+ if (unlikely(READ_ONCE(class->key) != lock->key->subkeys + class_subclass))
+ return false;
+
+ return true;
+}
+
/*
* Static locks do not have their class-keys yet - for them the key is
* the lock object itself. If the lock is in the per cpu area, the
@@ -1395,9 +1423,9 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
out_set_class_cache:
if (!subclass || force)
- lock->class_cache[0] = class;
+ WRITE_ONCE(lock->class_cache[0], class);
else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
- lock->class_cache[subclass] = class;
+ WRITE_ONCE(lock->class_cache[subclass], class);
/*
* Hash collision, did we smoke some? We found a class with a matching
@@ -4957,7 +4985,7 @@ void lockdep_init_map_type(struct lockdep_map *lock, const char *name,
int i;
for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
- lock->class_cache[i] = NULL;
+ WRITE_ONCE(lock->class_cache[i], NULL);
#ifdef CONFIG_LOCK_STAT
lock->cpu = raw_smp_processor_id();
@@ -5022,12 +5050,15 @@ EXPORT_SYMBOL_GPL(__lockdep_no_track__);
void lockdep_set_lock_cmp_fn(struct lockdep_map *lock, lock_cmp_fn cmp_fn,
lock_print_fn print_fn)
{
- struct lock_class *class = lock->class_cache[0];
+ struct lock_class *class = READ_ONCE(lock->class_cache[0]);
unsigned long flags;
raw_local_irq_save(flags);
lockdep_recursion_inc();
+ if (!lock_class_cache_is_valid(lock, class, 0))
+ class = NULL;
+
if (!class)
class = register_lock_class(lock, 0, 0);
@@ -5119,8 +5150,11 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
if (DEBUG_LOCKS_WARN_ON(subclass >= MAX_LOCKDEP_SUBCLASSES))
return 0;
- if (subclass < NR_LOCKDEP_CACHING_CLASSES)
- class = lock->class_cache[subclass];
+ if (subclass < NR_LOCKDEP_CACHING_CLASSES) {
+ class = READ_ONCE(lock->class_cache[subclass]);
+ if (!lock_class_cache_is_valid(lock, class, subclass))
+ class = NULL;
+ }
/*
* Not cached?
*/
@@ -5323,9 +5357,9 @@ static noinstr int match_held_lock(const struct held_lock *hlock,
return 1;
if (hlock->references) {
- const struct lock_class *class = lock->class_cache[0];
+ const struct lock_class *class = READ_ONCE(lock->class_cache[0]);
- if (!class)
+ if (!lock_class_cache_is_valid(lock, class, 0))
class = look_up_lock_class(lock, 0);
/*
]
|
| 42/2 |
2026/11/19 02:07 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 8f2109843137da8068f19a120d79e58ef3838429
Author: Liam R. Howlett (Oracle) <liam@infradead.org>
Date: Fri Aug 21 15:26:10 2026 -0400
locking/lockdep: add sequence counter to held_lock
Add an 8 bit small sequence counter to the held_lock struct to detect if
the lock as been dropped and reacquired. This is useful when a data
structure depends on a constant locking context, but is not able to detect
locking and unlocking of the lock through its own API.
Since the __lock_unpin_lock() will no longer detect underflow by casting
the unsigned int to a signed int, update the casting code to use a temp
variable for calculations using a signed int.
Link: https://lore.kernel.org/20260821192627.4085470-3-liam@infradead.org
Signed-off-by: Liam R. Howlett (Oracle) <liam@infradead.org>
Suggested-by: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Will Deacon <will@kernel.org>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Waiman Long <longman@redhat.com>
Link: https://lore.kernel.org/all/h3tpnj5kzcrxms5picmimtkpg4aypcpip5wbd6bt2rpdj5k7eb@nhtzs3lefrkq/
Cc: Breno Leitao <leitao@debian.org>
Cc: Chris Mason <clm@meta.com>
Cc: Chuck Lever <cel@kernel.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Joe Perches <joe@perches.com>
Cc: Rik van Riel <riel@surriel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
index 621566345406..a6451ecbbe9a 100644
--- a/include/linux/lockdep.h
+++ b/include/linux/lockdep.h
@@ -273,6 +273,9 @@ extern struct pin_cookie lock_pin_lock(struct lockdep_map *lock);
extern void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie);
extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie);
+extern u32 lock_sequence(struct lockdep_map *lock);
+#define lockdep_sequence(lock) lock_sequence(&(lock)->dep_map)
+
#define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
#define lockdep_assert(cond) \
diff --git a/include/linux/lockdep_types.h b/include/linux/lockdep_types.h
index eae115a26488..55c4b152fedf 100644
--- a/include/linux/lockdep_types.h
+++ b/include/linux/lockdep_types.h
@@ -253,7 +253,8 @@ struct held_lock {
unsigned int hardirqs_off:1;
unsigned int sync:1;
unsigned int references:11; /* 32 bits */
- unsigned int pin_count;
+ unsigned int pin_count:24;
+ unsigned int seq_count:8;
};
#else /* !CONFIG_LOCKDEP */
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 373bcc0598d1..14d5ce8dd613 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1288,6 +1288,7 @@ struct task_struct {
u64 curr_chain_key;
int lockdep_depth;
unsigned int lockdep_recursion;
+ unsigned int lockdep_seq;
struct held_lock held_locks[MAX_LOCK_DEPTH];
#endif
diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
index 2d4c5bab5af8..a69567bdd791 100644
--- a/kernel/locking/lockdep.c
+++ b/kernel/locking/lockdep.c
@@ -5077,7 +5077,7 @@ static int __lock_is_held(const struct lockdep_map *lock, int read);
static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
int trylock, int read, int check, int hardirqs_off,
struct lockdep_map *nest_lock, unsigned long ip,
- int references, int pin_count, int sync)
+ int references, int pin_count, int sync, int seq)
{
struct task_struct *curr = current;
struct lock_class *class = NULL;
@@ -5183,6 +5183,7 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
hlock->holdtime_stamp = lockstat_clock();
#endif
hlock->pin_count = pin_count;
+ hlock->seq_count = seq;
if (check_wait_context(curr, hlock))
return 0;
@@ -5388,7 +5389,7 @@ static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
hlock->read, hlock->check,
hlock->hardirqs_off,
hlock->nest_lock, hlock->acquire_ip,
- hlock->references, hlock->pin_count, 0)) {
+ hlock->references, hlock->pin_count, 0, hlock->seq_count)) {
case 0:
return 1;
case 1:
@@ -5669,14 +5670,17 @@ static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie
struct held_lock *hlock = curr->held_locks + i;
if (match_held_lock(hlock, lock)) {
+ int pin_count;
+
if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
return;
- hlock->pin_count -= cookie.val;
+ pin_count = hlock->pin_count - cookie.val;
- if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
- hlock->pin_count = 0;
+ if (WARN(pin_count < 0, "pin count corrupted\n"))
+ pin_count = 0;
+ hlock->pin_count = pin_count;
return;
}
}
@@ -5684,6 +5688,24 @@ static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie
WARN(1, "unpinning an unheld lock\n");
}
+static u32 __lock_sequence(struct lockdep_map *lock)
+{
+ struct task_struct *curr = current;
+ int i;
+
+ if (unlikely(!debug_locks))
+ return ~0;
+
+ for (i = 0; i < curr->lockdep_depth; i++) {
+ struct held_lock *hlock = curr->held_locks + i;
+
+ if (match_held_lock(hlock, lock))
+ return hlock->seq_count;
+ }
+
+ return ~0;
+}
+
/*
* Check whether we follow the irq-flags state precisely:
*/
@@ -5866,7 +5888,8 @@ void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
lockdep_recursion_inc();
__lock_acquire(lock, subclass, trylock, read, check,
- irqs_disabled_flags(flags), nest_lock, ip, 0, 0, 0);
+ irqs_disabled_flags(flags), nest_lock, ip, 0, 0, 0,
+ ++current->lockdep_seq);
lockdep_recursion_finish();
raw_local_irq_restore(flags);
}
@@ -5914,7 +5937,8 @@ void lock_sync(struct lockdep_map *lock, unsigned subclass, int read,
lockdep_recursion_inc();
__lock_acquire(lock, subclass, 0, read, check,
- irqs_disabled_flags(flags), nest_lock, ip, 0, 0, 1);
+ irqs_disabled_flags(flags), nest_lock, ip, 0, 0, 1,
+ ++current->lockdep_seq);
check_chain_key(current);
lockdep_recursion_finish();
raw_local_irq_restore(flags);
@@ -6000,6 +6024,26 @@ void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
}
EXPORT_SYMBOL_GPL(lock_unpin_lock);
+u32 lock_sequence(struct lockdep_map *lock)
+{
+ unsigned long flags;
+ u32 seq = ~0;
+
+ if (unlikely(!lockdep_enabled()))
+ return seq;
+
+ raw_local_irq_save(flags);
+ check_flags(flags);
+
+ lockdep_recursion_inc();
+ seq = __lock_sequence(lock);
+ lockdep_recursion_finish();
+ raw_local_irq_restore(flags);
+
+ return seq;
+}
+EXPORT_SYMBOL_GPL(lock_sequence);
+
#ifdef CONFIG_LOCK_STAT
static void print_lock_contention_bug(struct task_struct *curr,
struct lockdep_map *lock,
]
|
| 43/2 |
2026/11/19 02:07 |
tool |
git-blame |
0m
Args: map[End:1710 File:drivers/usb/core/hcd.c Start:1674]
Results: map[Output:8fea0c8fda301 1674) static void usb_giveback_urb_bh(struct work_struct *work)
94dfd7edfd5c9 1675) {
8fea0c8fda301 1676) struct giveback_urb_bh *bh =
8fea0c8fda301 1677) container_of(work, struct giveback_urb_bh, bh);
94dfd7edfd5c9 1678) struct list_head local_list;
94dfd7edfd5c9 1679)
94dfd7edfd5c9 1680) spin_lock_irq(&bh->lock);
94dfd7edfd5c9 1681) bh->running = true;
94dfd7edfd5c9 1682) list_replace_init(&bh->head, &local_list);
94dfd7edfd5c9 1683) spin_unlock_irq(&bh->lock);
94dfd7edfd5c9 1684)
94dfd7edfd5c9 1685) while (!list_empty(&local_list)) {
94dfd7edfd5c9 1686) struct urb *urb;
94dfd7edfd5c9 1687)
94dfd7edfd5c9 1688) urb = list_entry(local_list.next, struct urb, urb_list);
94dfd7edfd5c9 1689) list_del_init(&urb->urb_list);
c7ccde6eac6d3 1690) bh->completing_ep = urb->ep;
94dfd7edfd5c9 1691) __usb_hcd_giveback_urb(urb);
c7ccde6eac6d3 1692) bh->completing_ep = NULL;
94dfd7edfd5c9 1693) }
94dfd7edfd5c9 1694)
26c6c2f8a907c 1695) /*
26c6c2f8a907c 1696) * giveback new URBs next time to prevent this function
26c6c2f8a907c 1697) * from not exiting for a long time.
26c6c2f8a907c 1698) */
94dfd7edfd5c9 1699) spin_lock_irq(&bh->lock);
26c6c2f8a907c 1700) if (!list_empty(&bh->head)) {
26c6c2f8a907c 1701) if (bh->high_prio)
8fea0c8fda301 1702) queue_work(system_bh_highpri_wq, &bh->bh);
26c6c2f8a907c 1703) else
8fea0c8fda301 1704) queue_work(system_bh_wq, &bh->bh);
26c6c2f8a907c 1705) }
94dfd7edfd5c9 1706) bh->running = false;
94dfd7edfd5c9 1707) spin_unlock_irq(&bh->lock);
94dfd7edfd5c9 1708) }
94dfd7edfd5c9 1709)
32aca56005261 1710) /**
]
|
| 44/2 |
2026/11/19 02:07 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 49706
output: 123
thoughts: 453
Thoughts: **The Critical Moment of Realization**
Wait a second! I need to stop everything and dive deep into commit `02c6be7d675b`, the one titled "locking/lockdep: Invalidate stale class_cache entries for zapped classes." I'm holding everything up to meticulously examine this commit, line by line.
My attention is immediately drawn to lines 966 through 992 within that commit, specifically the `lock_class_cache_is_valid` function. I'm rereading this section with intense scrutiny:
if (!class)
return false;
if (unlikely(!lock->key))
return false;
return true;
}
Hold on again! I need to pause and consider this function's behavior specifically when `lock->key` represents a STATIC key, or in the edge cases where `lock == (void *)lock->key`, or when dealing with a percpu key, or during the `assign_lock_key` operation.
To truly grasp this, I must now meticulously trace how `lock->key` and `class->key` are managed and interact within the `register_lock_class`, `assign_lock_key`, and `look_up_lock_class` functions. I'm going to read kernel/locking/lockdep.c from line 900 to 1430 right now to ensure I have complete clarity.
|
| 45/2 |
2026/11/19 02:08 |
tool |
read-file |
0m
Args: map[File:kernel/locking/lockdep.c FirstLine:900 LineCount:100]
Results: map[Contents: 900:
901: /* used from NMI context -- must be lockless */
902: static noinstr struct lock_class *
903: look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
904: {
905: struct lockdep_subclass_key *key;
906: struct hlist_head *hash_head;
907: struct lock_class *class;
908:
909: if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
910: instrumentation_begin();
911: debug_locks_off();
912: nbcon_cpu_emergency_enter();
913: printk(KERN_ERR
914: "BUG: looking up invalid subclass: %u\n", subclass);
915: printk(KERN_ERR
916: "turning off the locking correctness validator.\n");
917: dump_stack();
918: nbcon_cpu_emergency_exit();
919: instrumentation_end();
920: return NULL;
921: }
922:
923: /*
924: * If it is not initialised then it has never been locked,
925: * so it won't be present in the hash table.
926: */
927: if (unlikely(!lock->key))
928: return NULL;
929:
930: /*
931: * NOTE: the class-key must be unique. For dynamic locks, a static
932: * lock_class_key variable is passed in through the mutex_init()
933: * (or spin_lock_init()) call - which acts as the key. For static
934: * locks we use the lock object itself as the key.
935: */
936: BUILD_BUG_ON(sizeof(struct lock_class_key) >
937: sizeof(struct lockdep_map));
938:
939: key = lock->key->subkeys + subclass;
940:
941: hash_head = classhashentry(key);
942:
943: /*
944: * We do an RCU walk of the hash, see lockdep_free_key_range().
945: */
946: if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
947: return NULL;
948:
949: hlist_for_each_entry_rcu_notrace(class, hash_head, hash_entry) {
950: if (class->key == key) {
951: /*
952: * Huh! same key, different name? Did someone trample
953: * on some memory? We're most confused.
954: */
955: WARN_ONCE(class->name != lock->name &&
956: lock->key != &__lockdep_no_validate__,
957: "Looking for class \"%s\" with key %ps, but found a different class \"%s\" with the same key\n",
958: lock->name, lock->key, class->name);
959: return class;
960: }
961: }
962:
963: return NULL;
964: }
965:
966: static __always_inline bool lock_class_cache_is_valid(const struct lockdep_map *lock,
967: const struct lock_class *class,
968: unsigned int subclass)
969: {
970: unsigned int class_subclass;
971:
972: if (!class)
973: return false;
974:
975: if (unlikely(class < lock_classes || class >= lock_classes + MAX_LOCKDEP_KEYS))
976: return false;
977:
978: if (unlikely(!arch_test_bit(class - lock_classes, lock_classes_in_use)))
979: return false;
980:
981: if (unlikely(!lock->key))
982: return false;
983:
984: class_subclass = subclass ? subclass : class->subclass;
985: if (unlikely(class_subclass >= MAX_LOCKDEP_SUBCLASSES))
986: return false;
987:
988: if (unlikely(READ_ONCE(class->key) != lock->key->subkeys + class_subclass))
989: return false;
990:
991: return true;
992: }
993:
994: /*
995: * Static locks do not have their class-keys yet - for them the key is
996: * the lock object itself. If the lock is in the per cpu area, the
997: * canonical address of the lock (per cpu offset removed) is used.
998: */
999: static bool assign_lock_key(struct lockdep_map *lock)
]
|
| 46/2 |
2026/11/19 02:08 |
tool |
read-file |
0m
Args: map[File:kernel/locking/lockdep.c FirstLine:1000 LineCount:100]
Results: map[Contents:1000: {
1001: unsigned long can_addr, addr = (unsigned long)lock;
1002:
1003: #ifdef __KERNEL__
1004: /*
1005: * lockdep_free_key_range() assumes that struct lock_class_key
1006: * objects do not overlap. Since we use the address of lock
1007: * objects as class key for static objects, check whether the
1008: * size of lock_class_key objects does not exceed the size of
1009: * the smallest lock object.
1010: */
1011: BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(raw_spinlock_t));
1012: #endif
1013:
1014: if (__is_kernel_percpu_address(addr, &can_addr))
1015: lock->key = (void *)can_addr;
1016: else if (__is_module_percpu_address(addr, &can_addr))
1017: lock->key = (void *)can_addr;
1018: else if (static_obj(lock))
1019: lock->key = (void *)lock;
1020: else {
1021: /* Debug-check: all keys must be persistent! */
1022: debug_locks_off();
1023: nbcon_cpu_emergency_enter();
1024: pr_err("INFO: trying to register non-static key.\n");
1025: pr_err("The code is fine but needs lockdep annotation, or maybe\n");
1026: pr_err("you didn't initialize this object before use?\n");
1027: pr_err("turning off the locking correctness validator.\n");
1028: dump_stack();
1029: nbcon_cpu_emergency_exit();
1030: return false;
1031: }
1032:
1033: return true;
1034: }
1035:
1036: #ifdef CONFIG_DEBUG_LOCKDEP
1037:
1038: /* Check whether element @e occurs in list @h */
1039: static bool in_list(struct list_head *e, struct list_head *h)
1040: {
1041: struct list_head *f;
1042:
1043: list_for_each(f, h) {
1044: if (e == f)
1045: return true;
1046: }
1047:
1048: return false;
1049: }
1050:
1051: /*
1052: * Check whether entry @e occurs in any of the locks_after or locks_before
1053: * lists.
1054: */
1055: static bool in_any_class_list(struct list_head *e)
1056: {
1057: struct lock_class *class;
1058: int i;
1059:
1060: for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
1061: class = &lock_classes[i];
1062: if (in_list(e, &class->locks_after) ||
1063: in_list(e, &class->locks_before))
1064: return true;
1065: }
1066: return false;
1067: }
1068:
1069: static bool class_lock_list_valid(struct lock_class *c, struct list_head *h)
1070: {
1071: struct lock_list *e;
1072:
1073: list_for_each_entry(e, h, entry) {
1074: if (e->links_to != c) {
1075: printk(KERN_INFO "class %s: mismatch for lock entry %ld; class %s <> %s",
1076: c->name ? : "(?)",
1077: (unsigned long)(e - list_entries),
1078: e->links_to && e->links_to->name ?
1079: e->links_to->name : "(?)",
1080: e->class && e->class->name ? e->class->name :
1081: "(?)");
1082: return false;
1083: }
1084: }
1085: return true;
1086: }
1087:
1088: #ifdef CONFIG_PROVE_LOCKING
1089: static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1090: #endif
1091:
1092: static bool check_lock_chain_key(struct lock_chain *chain)
1093: {
1094: #ifdef CONFIG_PROVE_LOCKING
1095: u64 chain_key = INITIAL_CHAIN_KEY;
1096: int i;
1097:
1098: for (i = chain->base; i < chain->base + chain->depth; i++)
1099: chain_key = iterate_chain_key(chain_key, chain_hlocks[i]);
]
|
| 47/2 |
2026/11/19 02:08 |
tool |
read-file |
0m
Args: map[File:kernel/locking/lockdep.c FirstLine:1330 LineCount:100]
Results: map[Contents:1330: {
1331: struct lockdep_subclass_key *key;
1332: struct hlist_head *hash_head;
1333: struct lock_class *class;
1334: int idx;
1335:
1336: DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1337:
1338: class = look_up_lock_class(lock, subclass);
1339: if (likely(class))
1340: goto out_set_class_cache;
1341:
1342: if (!lock->key) {
1343: if (!assign_lock_key(lock))
1344: return NULL;
1345: } else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) {
1346: return NULL;
1347: }
1348:
1349: key = lock->key->subkeys + subclass;
1350: hash_head = classhashentry(key);
1351:
1352: if (!graph_lock()) {
1353: return NULL;
1354: }
1355: /*
1356: * We have to do the hash-walk again, to avoid races
1357: * with another CPU:
1358: */
1359: hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
1360: if (class->key == key)
1361: goto out_unlock_set;
1362: }
1363:
1364: init_data_structures_once();
1365:
1366: /* Allocate a new lock class and add it to the hash. */
1367: class = list_first_entry_or_null(&free_lock_classes, typeof(*class),
1368: lock_entry);
1369: if (!class) {
1370: if (!debug_locks_off_graph_unlock()) {
1371: return NULL;
1372: }
1373:
1374: nbcon_cpu_emergency_enter();
1375: print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
1376: dump_stack();
1377: nbcon_cpu_emergency_exit();
1378: return NULL;
1379: }
1380: nr_lock_classes++;
1381: __set_bit(class - lock_classes, lock_classes_in_use);
1382: debug_atomic_inc(nr_unused_locks);
1383: class->key = key;
1384: class->name = lock->name;
1385: class->subclass = subclass;
1386: WARN_ON_ONCE(!list_empty(&class->locks_before));
1387: WARN_ON_ONCE(!list_empty(&class->locks_after));
1388: class->name_version = count_matching_names(class);
1389: class->wait_type_inner = lock->wait_type_inner;
1390: class->wait_type_outer = lock->wait_type_outer;
1391: class->lock_type = lock->lock_type;
1392: /*
1393: * We use RCU's safe list-add method to make
1394: * parallel walking of the hash-list safe:
1395: */
1396: hlist_add_head_rcu(&class->hash_entry, hash_head);
1397: /*
1398: * Remove the class from the free list and add it to the global list
1399: * of classes.
1400: */
1401: list_move_tail(&class->lock_entry, &all_lock_classes);
1402: idx = class - lock_classes;
1403: if (idx > max_lock_class_idx)
1404: max_lock_class_idx = idx;
1405:
1406: if (verbose(class)) {
1407: graph_unlock();
1408:
1409: nbcon_cpu_emergency_enter();
1410: printk("\nnew class %px: %s", class->key, class->name);
1411: if (class->name_version > 1)
1412: printk(KERN_CONT "#%d", class->name_version);
1413: printk(KERN_CONT "\n");
1414: dump_stack();
1415: nbcon_cpu_emergency_exit();
1416:
1417: if (!graph_lock()) {
1418: return NULL;
1419: }
1420: }
1421: out_unlock_set:
1422: graph_unlock();
1423:
1424: out_set_class_cache:
1425: if (!subclass || force)
1426: WRITE_ONCE(lock->class_cache[0], class);
1427: else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
1428: WRITE_ONCE(lock->class_cache[subclass], class);
1429:
]
|
| 48/2 |
2026/11/19 02:08 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 54690
output: 128
thoughts: 3860
Thoughts: **My Mental Breakdown of the Lockdep Crash**
Okay, so I was initially looking at `lock_class_cache_is_valid` in commit `02c6be7d675b`. My initial thought was that it's fine, and even if it returned false, it would just fall back to `register_lock_class` without disabling `debug_locks`.
BUT THEN IT HIT ME. A HUGE red flag.
I immediately started re-examining `assign_lock_key` in `kernel/locking/lockdep.c` (specifically lines 1020-1030), and also `print_lockdep_off` in the same file. I needed to understand *exactly* how `debug_locks` could be turned off. This led me to check *all* 27 occurrences of `debug_locks_off` in `kernel/locking/lockdep.c`, plus its appearances in `kernel/panic.c`, `kernel/locking/spinlock_debug.c`, and `kernel/locking/rwsem.c`.
My core question became: *Why* did `debug_locks` go to `0` during the execution of `usb_giveback_urb_bh`, especially when the syzkaller report didn't indicate an earlier crash? I systematically explored all possible ways `debug_locks` could become `0` without syzkaller flagging it beforehand.
This led me to two primary scenarios:
**Scenario 1: Lockdep Resource Exhaustion**
This happens when any of the `MAX_...` constants for lockdep are too low. Specifically, if we hit `BUG: MAX_STACK_TRACE_ENTRIES too low!`, `BUG: MAX_LOCKDEP_KEYS too low!`, `BUG: MAX_LOCKDEP_ENTRIES too low!`, `BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!`, or `BUG: MAX_LOCK_DEPTH too low!`.
In all of these cases:
1. `debug_locks_off()` is called, setting `debug_locks = 0`. Crucially, this does *not* taint the kernel, so we see `Not tainted`.
2. A `BUG: MAX_LOCKDEP_... too low!` message is printed, along with a `dump_stack()`.
3. Syzkaller is configured to *explicitly ignore* these specific `BUG: MAX_LOCKDEP_*` and `BUG: MAX_LOCK_DEPTH` messages.
4. Execution continues normally.
Now, if one of these resource exhaustion events occurred *while* `usb_giveback_urb_bh` was active, or if `usb_giveback_urb_bh` acquired a lock that pushed the system over the edge (e.g., `spin_lock_irq(&bh->lock)`, or inside `__usb_hcd_giveback_urb(urb)` where the URB completion handler tried to acquire a lock that exhausted `MAX_LOCKDEP_CHAIN_HLOCKS`, `MAX_LOCKDEP_CHAINS`, `MAX_LOCKDEP_ENTRIES`, or `MAX_LOCK_DEPTH`), or if it happened concurrently on another CPU while `usb_giveback_urb_bh` was running – that could explain `debug_locks` turning off. This would then set up the scenario for what we see in `process_one_work()`.
**Scenario 2: Concurrent Lockdep Splat on Another CPU**
Imagine CPU 1 detects a serious lockdep issue, like a circular dependency or an invalid wait context.
1. CPU 1 calls `debug_locks_off_graph_unlock()`, which executes `xchg(&debug_locks, 0)`. Importantly, all the various lockdep splats (like `print_circular_bug`, `print_bad_irq_dependency`, etc.) *also* do not taint the kernel (`Not tainted`).
2. Before CPU 1 can even finish printing its error message (or even while it's acquiring console locks or printing its first line), CPU 0 finishes executing `usb_giveback_urb_bh` within `process_one_work()`.
3. On CPU 0, the `process_one_work()` function performs its checks. It specifically looks at `lockdep_depth(current) != lockdep_start_depth`.
4. Because `debug_locks` is now `0` (thanks to CPU 1), `lockdep_depth(current)` evaluates to `0`. However, `lockdep_start_depth` was, say, `4` (which we'll verify).
5. This inequality (`0 != 4`) triggers the `pr_err` in `process_one_work()`, leading to CPU 0 printing:
`BUG: workqueue leaked atomic, lock or RCU: syz-executor[6626]`
` preempt=0x00000000 lock=4->0 RCU=2->2 workfn=usb_giveback_urb_bh`
`INFO: lockdep is turned off.`
6. Syzkaller captures CPU 0's output as the primary crash.
**Scenario 3: `INFO: trying to register non-static key.`**
In `assign_lock_key()`, this condition calls `debug_locks_off()` and prints `INFO: trying to register non-static key.`. This also does *not* taint the kernel. However, syzkaller *would* normally catch this, unless, again, it happened on another CPU just before CPU 0 finished `usb_giveback_urb_bh`.
**The "AHA!" Moment: Re-examining the Crash Report**
I went back to the crash report, word for word, number by number:
`BUG: workqueue leaked atomic, lock or RCU: syz-executor[6626]`
` preempt=0x00000000 lock=4->0 RCU=2->2 workfn=usb_giveback_urb_bh`
`INFO: lockdep is turned off.`
`CPU: 0 UID: 0 PID: 6626 Comm: syz-executor Not tainted syzkaller #0 PREEMPT_{RT,(full)}`
Let's break down those numbers:
* `preempt=0x00000000`: This means there was no atomic context leak. Also, `worker->task` is `NULL` for a BH worker, so the `in_atomic()` check would be false anyway.
* `RCU=2->2`: This indicates `rcu_start_depth` was 2 and `rcu_preempt_depth()` was 2. No RCU leak here.
* `lock=4->0`: This is the critical part. `lockdep_start_depth` was 4, and `lockdep_depth(current)` was 0.
* `INFO: lockdep is turned off.`: This directly confirms `!debug_locks`.
**The Mathematical Proof: How `lock=4->0` Happens**
1. **Can a work function "leak" a lock by decreasing `lockdep_depth` from 4 to 0?** Absolutely not. If `usb_giveback_urb_bh` tried to unlock a lock it didn't hold, `print_unlock_imbalance_bug` would have triggered immediately, and the depth wouldn't have gone from 4 to 0. Also, `usb_giveback_urb_bh` doesn't even touch the lockdep maps for `rtnl_lock` or `softirq`.
2. **Why is `lockdep_depth(current)` equal to 0?** This is the killer. It's because of the macro definition in `include/linux/lockdep.h` (around line 279):
`#define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)`
When `debug_locks` is `0`, `lockdep_depth(current)` *unconditionally evaluates to `0`*, regardless of the actual value of `current->lockdep_depth` (which might still be 4 or even 6).
**Tracing `process_one_work()` to Confirm**
Let's walk through `process_one_work()` in `kernel/workqueue.c`:
* **Line 3467:**
`rcu_start_depth = rcu_preempt_depth();`
`lockdep_start_depth = lockdep_depth(current);`
At this point, `debug_locks` is `1`. `current->lockdep_depth` is `4` (likely from holding `rtnl_mutex`, an `rcu_read_lock`, `pernet_ops_rwsem`, and the BH lock). So, `lockdep_depth(current)` correctly evaluates to `4`, and `lockdep_start_depth` is set to `4`.
* **Lines 3469-3471:**
`lock_map_acquire(pwq->wq->lockdep_map);`
`lock_map_acquire(&lockdep_map);`
This increases `current->lockdep_depth` to `6`.
* **Line 3495:**
`worker->current_func(work);`
This is where `usb_giveback_urb_bh` executes. *During or just before this call*, `debug_locks` becomes `0`. This could be from a resource exhaustion event (as per Scenario 1) or a concurrent splat on another CPU (Scenario 2).
* **Lines 3502-3504:**
`lock_map_release(&lockdep_map);`
`lock_map_release(pwq->wq->lockdep_map);`
Because `debug_locks == 0`, the `lock_release()` calls return immediately. `current->lockdep_depth` remains `6` internally, but it's effectively ignored by the lockdep system.
* **Lines 3506-3516:** This is the crucial check:
`if (unlikely((worker->task && in_atomic()) ||`
` lockdep_depth(current) != lockdep_start_depth ||`
` rcu_preempt_depth() != rcu_start_depth)) { ... }`
Let's evaluate the conditions at this point:
* `(worker->task && in_atomic())`: `worker->task` is `NULL`, so this is `false`.
* `rcu_preempt_depth() != rcu_start_depth`: This is `2 != 2`, which is `false`.
* `lockdep_depth(current) != lockdep_start_depth`:
* `lockdep_depth(current)` evaluates to `(debug_locks ? (current)->lockdep_depth : 0)`.
* Since `debug_locks == 0`, this evaluates to `0`.
* `lockdep_start_depth` is `4`.
* So, the condition becomes `0 != 4`, which is `TRUE`!
This `TRUE` condition triggers the error reporting:
* **Line 3509:** Prints `BUG: workqueue leaked atomic, lock or RCU: syz-executor[6626]` and the `lock=4->0` part of the message, along with the work function.
* **Line 3515:** Calls `debug_show_held_locks(current)`. This function checks `if (unlikely(!debug_locks))`, which is true, so it prints `INFO: lockdep is turned off.`.
* **Line 3516:** Calls `dump_stack()`.
**Conclusion:**
Every single character of the crash report is now perfectly explained by this sequence of events. The "leak" isn't a real lock leak in the traditional sense, but a consequence of `debug_locks` being turned off *mid-function* while the workqueue's final check uses the `lockdep_depth` macro, which resolves to `0` when `debug_locks` is `0`. The `lockdep_start_depth` captured before `debug_locks` was turned off is what creates the mismatch. The `Not tainted` status strongly suggests one of the resource exhaustion bugs (which syzkaller ignores) or a concurrent lockdep splat on another CPU was the root cause of `debug_locks` being disabled.
I've now got to dig into the specific commit history around `c35aea39d1e10` ("workqueue: Update lock debugging code") and earlier commits that introduced BH workqueues to see if the timing of these changes perfectly aligns with this behavior. I also need to investigate if any other kernel subsystems save and compare `lockdep_depth` in a similar way to `process_one_work()`, and how they handle `debug_locks` being turned off.
|
| 49/2 |
2026/11/19 02:08 |
tool |
grepper |
0m
Args: map[Expression:lockdep_depth\b]
Results: map[Output:
Full output is too long, showing 200 out of 284 lines.
Use more precise expression or PathPrefixes if possible.
Number of matching lines per file (9 files in total):
include/linux/lockdep.h:3
include/linux/sched.h:1
init/init_task.c:1
kernel/freezer.c:1
kernel/locking/lockdep.c:43
kernel/locking/lockdep_internals.h:1
kernel/locking/lockdep_proc.c:1
kernel/workqueue.c:3
net/core/dev.c:1
include/linux/lockdep.h=276=extern u32 lock_sequence(struct lockdep_map *lock);
--
include/linux/lockdep.h-278-
include/linux/lockdep.h:279:#define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
include/linux/lockdep.h-280-
--
include/linux/lockdep.h-302-#define lockdep_assert_none_held_once() \
include/linux/lockdep.h:303: lockdep_assert_once(!current->lockdep_depth)
include/linux/lockdep.h-304-
--
include/linux/lockdep.h=378=static inline void lockdep_unregister_key(struct lock_class_key *key)
--
include/linux/lockdep.h-381-
include/linux/lockdep.h:382:#define lockdep_depth(tsk) (0)
include/linux/lockdep.h-383-
--
include/linux/sched.h=836=struct task_struct {
--
include/linux/sched.h-1305- u64 curr_chain_key;
include/linux/sched.h:1306: int lockdep_depth;
include/linux/sched.h-1307- unsigned int lockdep_recursion;
--
init/init_task.c=105=struct task_struct init_task __aligned(L1_CACHE_BYTES) = {
--
init/init_task.c-246-#ifdef CONFIG_LOCKDEP
init/init_task.c:247: .lockdep_depth = 0, /* no locks held yet */
init/init_task.c-248- .curr_chain_key = INITIAL_CHAIN_KEY,
--
kernel/freezer.c=109=static int __set_task_frozen(struct task_struct *p, void *arg)
--
kernel/freezer.c-138- if (!(state & __TASK_FREEZABLE_UNSAFE))
kernel/freezer.c:139: WARN_ON_ONCE(debug_locks && p->lockdep_depth);
kernel/freezer.c-140-#endif
--
kernel/locking/lockdep.c=456=void lockdep_init_task(struct task_struct *task)
kernel/locking/lockdep.c-457-{
kernel/locking/lockdep.c:458: task->lockdep_depth = 0; /* no locks held yet */
kernel/locking/lockdep.c-459- task->curr_chain_key = INITIAL_CHAIN_KEY;
--
kernel/locking/lockdep.c=635=unsigned int nr_process_chains;
kernel/locking/lockdep.c:636:unsigned int max_lockdep_depth;
kernel/locking/lockdep.c-637-
--
kernel/locking/lockdep.c=786=static void lockdep_print_held_locks(struct task_struct *p)
kernel/locking/lockdep.c-787-{
kernel/locking/lockdep.c:788: int i, depth = READ_ONCE(p->lockdep_depth);
kernel/locking/lockdep.c-789-
--
kernel/locking/lockdep.c=3101=check_deadlock(struct task_struct *curr, struct held_lock *next)
--
kernel/locking/lockdep.c-3107-
kernel/locking/lockdep.c:3108: for (i = 0; i < curr->lockdep_depth; i++) {
kernel/locking/lockdep.c-3109- prev = curr->held_locks + i;
--
kernel/locking/lockdep.c=3302=check_prevs_add(struct task_struct *curr, struct held_lock *next)
--
kernel/locking/lockdep.c-3304- struct lock_trace *trace = NULL;
kernel/locking/lockdep.c:3305: int depth = curr->lockdep_depth;
kernel/locking/lockdep.c-3306- struct held_lock *hlock;
--
kernel/locking/lockdep.c-3323- for (;;) {
kernel/locking/lockdep.c:3324: u16 distance = curr->lockdep_depth - depth + 1;
kernel/locking/lockdep.c-3325- hlock = curr->held_locks + depth - 1;
--
kernel/locking/lockdep.c=3614=static inline int get_first_held_lock(struct task_struct *curr,
--
kernel/locking/lockdep.c-3619-
kernel/locking/lockdep.c:3620: for (i = curr->lockdep_depth - 1; i >= 0; i--) {
kernel/locking/lockdep.c-3621- hlock_curr = curr->held_locks + i;
--
kernel/locking/lockdep.c=3645=print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
--
kernel/locking/lockdep.c-3648- u64 chain_key = INITIAL_CHAIN_KEY;
kernel/locking/lockdep.c:3649: int depth = curr->lockdep_depth;
kernel/locking/lockdep.c-3650- int i = get_first_held_lock(curr, hlock_next);
--
kernel/locking/lockdep.c=3714=static int check_no_collision(struct task_struct *curr,
--
kernel/locking/lockdep.c-3722-
kernel/locking/lockdep.c:3723: if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
kernel/locking/lockdep.c-3724- print_collision(curr, hlock, chain);
--
kernel/locking/lockdep.c=3774=static inline int add_chain_cache(struct task_struct *curr,
--
kernel/locking/lockdep.c-3803- i = get_first_held_lock(curr, hlock);
kernel/locking/lockdep.c:3804: chain->depth = curr->lockdep_depth + 1 - i;
kernel/locking/lockdep.c-3805-
--
kernel/locking/lockdep.c=3980=static void check_chain_key(struct task_struct *curr)
--
kernel/locking/lockdep.c-3986-
kernel/locking/lockdep.c:3987: for (i = 0; i < curr->lockdep_depth; i++) {
kernel/locking/lockdep.c-3988- hlock = curr->held_locks + i;
--
kernel/locking/lockdep.c-3995- WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
kernel/locking/lockdep.c:3996: curr->lockdep_depth, i,
kernel/locking/lockdep.c-3997- (unsigned long long)chain_key,
--
kernel/locking/lockdep.c-4021- WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
kernel/locking/lockdep.c:4022: curr->lockdep_depth, i,
kernel/locking/lockdep.c-4023- (unsigned long long)chain_key,
--
kernel/locking/lockdep.c=4352=mark_held_locks(struct task_struct *curr, enum lock_usage_bit base_bit)
--
kernel/locking/lockdep.c-4356-
kernel/locking/lockdep.c:4357: for (i = 0; i < curr->lockdep_depth; i++) {
kernel/locking/lockdep.c-4358- enum lock_usage_bit hlock_bit = base_bit;
--
kernel/locking/lockdep.c=4730=static int separate_irq_context(struct task_struct *curr,
--
kernel/locking/lockdep.c-4732-{
kernel/locking/lockdep.c:4733: unsigned int depth = curr->lockdep_depth;
kernel/locking/lockdep.c-4734-
--
kernel/locking/lockdep.c=4896=static int check_wait_context(struct task_struct *curr, struct held_lock *next)
--
kernel/locking/lockdep.c-4911- */
kernel/locking/lockdep.c:4912: for (depth = curr->lockdep_depth - 1; depth >= 0; depth--) {
kernel/locking/lockdep.c-4913- struct held_lock *prev = curr->held_locks + depth;
--
kernel/locking/lockdep.c-4920-
kernel/locking/lockdep.c:4921: for (; depth < curr->lockdep_depth; depth++) {
kernel/locking/lockdep.c-4922- struct held_lock *prev = curr->held_locks + depth;
--
kernel/locking/lockdep.c=5124=static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
--
kernel/locking/lockdep.c-5183- */
kernel/locking/lockdep.c:5184: depth = curr->lockdep_depth;
kernel/locking/lockdep.c-5185- /*
--
kernel/locking/lockdep.c-5295- curr->curr_chain_key = chain_key;
kernel/locking/lockdep.c:5296: curr->lockdep_depth++;
kernel/locking/lockdep.c-5297- check_chain_key(curr);
--
kernel/locking/lockdep.c-5301-#endif
kernel/locking/lockdep.c:5302: if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
kernel/locking/lockdep.c-5303- debug_locks_off();
--
kernel/locking/lockdep.c-5306- printk(KERN_DEBUG "depth: %i max: %lu!\n",
kernel/locking/lockdep.c:5307: curr->lockdep_depth, MAX_LOCK_DEPTH);
kernel/locking/lockdep.c-5308-
--
kernel/locking/lockdep.c-5316-
kernel/locking/lockdep.c:5317: if (unlikely(curr->lockdep_depth > max_lockdep_depth))
kernel/locking/lockdep.c:5318: max_lockdep_depth = curr->lockdep_depth;
kernel/locking/lockdep.c-5319-
--
kernel/locking/lockdep.c=5459=__lock_set_class(struct lockdep_map *lock, const char *name,
--
kernel/locking/lockdep.c-5471-
kernel/locking/lockdep.c:5472: depth = curr->lockdep_depth;
kernel/locking/lockdep.c-5473- /*
--
kernel/locking/lockdep.c-5494-
kernel/locking/lockdep.c:5495: curr->lockdep_depth = i;
kernel/locking/lockdep.c-5496- curr->curr_chain_key = hlock->prev_chain_key;
--
kernel/locking/lockdep.c-5504- */
kernel/locking/lockdep.c:5505: if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged))
kernel/locking/lockdep.c-5506- return 0;
--
kernel/locking/lockdep.c=5510=static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
--
kernel/locking/lockdep.c-5519-
kernel/locking/lockdep.c:5520: depth = curr->lockdep_depth;
kernel/locking/lockdep.c-5521- /*
--
kernel/locking/lockdep.c-5533-
kernel/locking/lockdep.c:5534: curr->lockdep_depth = i;
kernel/locking/lockdep.c-5535- curr->curr_chain_key = hlock->prev_chain_key;
--
kernel/locking/lockdep.c-5551- */
kernel/locking/lockdep.c:5552: if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
kernel/locking/lockdep.c-5553- return 0;
--
kernel/locking/lockdep.c=5564=__lock_release(struct lockdep_map *lock, unsigned long ip)
--
kernel/locking/lockdep.c-5573-
kernel/locking/lockdep.c:5574: depth = curr->lockdep_depth;
kernel/locking/lockdep.c-5575- /*
--
kernel/locking/lockdep.c-5616-
kernel/locking/lockdep.c:5617: curr->lockdep_depth = i;
kernel/locking/lockdep.c-5618- curr->curr_chain_key = hlock->prev_chain_key;
--
kernel/locking/lockdep.c-5634- */
kernel/locking/lockdep.c:5635: DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged);
kernel/locking/lockdep.c-5636-
--
kernel/locking/lockdep.c=5646=int __lock_is_held(const struct lockdep_map *lock, int read)
--
kernel/locking/lockdep.c-5650-
kernel/locking/lockdep.c:5651: for (i = 0; i < curr->lockdep_depth; i++) {
kernel/locking/lockdep.c-5652- struct held_lock *hlock = curr->held_locks + i;
--
kernel/locking/lockdep.c=5665=static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
--
kernel/locking/lockdep.c-5673-
kernel/locking/lockdep.c:5674: for (i = 0; i < curr->lockdep_depth; i++) {
kernel/locking/lockdep.c-5675- struct held_lock *hlock = curr->held_locks + i;
]
|
| 50/2 |
2026/11/19 02:08 |
tool |
git-log |
0m
Args: map[CodeRegexp:lockdep_start_depth|bh_worker Count:20 PathPrefix:kernel/workqueue.c]
Results: map[Output:6eca8f94d841 workqueue: Annotate cb_lock nesting when draining a dead BH pool
79f23600bc7b workqueue: factor out get_percpu_pool()
b4722b8593b8 kernel/workqueue.c: fix DEFINE_PER_CPU_SHARED_ALIGNED expansion
7ccc215187a7 workqueue: Clean code in alloc_and_link_pwqs()
1acd92d95fa2 workqueue: Drain BH work items on hot-unplugged CPUs
4cb1ef64609f workqueue: Implement BH workqueues to eventually replace tasklets
c35aea39d1e1 workqueue: Update lock debugging code
]
|
| 51/2 |
2026/11/19 02:09 |
tool |
git-show |
0m
Args: map[Commit:1acd92d95fa24]
Results: map[Output:commit 1acd92d95fa24edca8f0292b21870025da93e24f
Author: Tejun Heo <tj@kernel.org>
Date: Mon Feb 26 15:38:55 2024 -1000
workqueue: Drain BH work items on hot-unplugged CPUs
Boqun pointed out that workqueues aren't handling BH work items on offlined
CPUs. Unlike tasklet which transfers out the pending tasks from
CPUHP_SOFTIRQ_DEAD, BH workqueue would just leave them pending which is
problematic. Note that this behavior is specific to BH workqueues as the
non-BH per-CPU workers just become unbound when the CPU goes offline.
This patch fixes the issue by draining the pending BH work items from an
offlined CPU from CPUHP_SOFTIRQ_DEAD. Because work items carry more context,
it's not as easy to transfer the pending work items from one pool to
another. Instead, run BH work items which execute the offlined pools on an
online CPU.
Note that this assumes that no further BH work items will be queued on the
offlined CPUs. This assumption is shared with tasklet and should be fine for
conversions. However, this issue also exists for per-CPU workqueues which
will just keep executing work items queued after CPU offline on unbound
workers and workqueue should reject per-CPU and BH work items queued on
offline CPUs. This will be addressed separately later.
Signed-off-by: Tejun Heo <tj@kernel.org>
Reported-and-reviewed-by: Boqun Feng <boqun.feng@gmail.com>
Link: http://lkml.kernel.org/r/Zdvw0HdSXcU3JZ4g@boqun-archlinux
diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index 64a60b9232d3..158784dd189a 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -458,6 +458,7 @@ extern struct workqueue_struct *system_bh_wq;
extern struct workqueue_struct *system_bh_highpri_wq;
void workqueue_softirq_action(bool highpri);
+void workqueue_softirq_dead(unsigned int cpu);
/**
* alloc_workqueue - allocate a workqueue
diff --git a/kernel/softirq.c b/kernel/softirq.c
index 547d282548a8..b315b21fb28c 100644
--- a/kernel/softirq.c
+++ b/kernel/softirq.c
@@ -932,6 +932,8 @@ static void run_ksoftirqd(unsigned int cpu)
#ifdef CONFIG_HOTPLUG_CPU
static int takeover_tasklets(unsigned int cpu)
{
+ workqueue_softirq_dead(cpu);
+
/* CPU is dead, so no lock needed. */
local_irq_disable();
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 38783e3a60bb..a60eb65955e7 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -81,6 +81,7 @@ enum worker_pool_flags {
POOL_BH = 1 << 0, /* is a BH pool */
POOL_MANAGER_ACTIVE = 1 << 1, /* being managed */
POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
+ POOL_BH_DRAINING = 1 << 3, /* draining after CPU offline */
};
enum worker_flags {
@@ -1218,7 +1219,9 @@ static struct irq_work *bh_pool_irq_work(struct worker_pool *pool)
static void kick_bh_pool(struct worker_pool *pool)
{
#ifdef CONFIG_SMP
- if (unlikely(pool->cpu != smp_processor_id())) {
+ /* see drain_dead_softirq_workfn() for BH_DRAINING */
+ if (unlikely(pool->cpu != smp_processor_id() &&
+ !(pool->flags & POOL_BH_DRAINING))) {
irq_work_queue_on(bh_pool_irq_work(pool), pool->cpu);
return;
}
@@ -3155,6 +3158,7 @@ __acquires(&pool->lock)
struct worker_pool *pool = worker->pool;
unsigned long work_data;
int lockdep_start_depth, rcu_start_depth;
+ bool bh_draining = pool->flags & POOL_BH_DRAINING;
#ifdef CONFIG_LOCKDEP
/*
* It is permissible to free the struct work_struct from
@@ -3220,7 +3224,9 @@ __acquires(&pool->lock)
rcu_start_depth = rcu_preempt_depth();
lockdep_start_depth = lockdep_depth(current);
- lock_map_acquire(&pwq->wq->lockdep_map);
+ /* see drain_dead_softirq_workfn() */
+ if (!bh_draining)
+ lock_map_acquire(&pwq->wq->lockdep_map);
lock_map_acquire(&lockdep_map);
/*
* Strictly speaking we should mark the invariant state without holding
@@ -3253,7 +3259,8 @@ __acquires(&pool->lock)
trace_workqueue_execute_end(work, worker->current_func);
pwq->stats[PWQ_STAT_COMPLETED]++;
lock_map_release(&lockdep_map);
- lock_map_release(&pwq->wq->lockdep_map);
+ if (!bh_draining)
+ lock_map_release(&pwq->wq->lockdep_map);
if (unlikely((worker->task && in_atomic()) ||
lockdep_depth(current) != lockdep_start_depth ||
@@ -3615,6 +3622,84 @@ void workqueue_softirq_action(bool highpri)
bh_worker(list_first_entry(&pool->workers, struct worker, node));
}
+struct wq_drain_dead_softirq_work {
+ struct work_struct work;
+ struct worker_pool *pool;
+ struct completion done;
+};
+
+static void drain_dead_softirq_workfn(struct work_struct *work)
+{
+ struct wq_drain_dead_softirq_work *dead_work =
+ container_of(work, struct wq_drain_dead_softirq_work, work);
+ struct worker_pool *pool = dead_work->pool;
+ bool repeat;
+
+ /*
+ * @pool's CPU is dead and we want to execute its still pending work
+ * items from this BH work item which is running on a different CPU. As
+ * its CPU is dead, @pool can't be kicked and, as work execution path
+ * will be nested, a lockdep annotation needs to be suppressed. Mark
+ * @pool with %POOL_BH_DRAINING for the special treatments.
+ */
+ raw_spin_lock_irq(&pool->lock);
+ pool->flags |= POOL_BH_DRAINING;
+ raw_spin_unlock_irq(&pool->lock);
+
+ bh_worker(list_first_entry(&pool->workers, struct worker, node));
+
+ raw_spin_lock_irq(&pool->lock);
+ pool->flags &= ~POOL_BH_DRAINING;
+ repeat = need_more_worker(pool);
+ raw_spin_unlock_irq(&pool->lock);
+
+ /*
+ * bh_worker() might hit consecutive execution limit and bail. If there
+ * still are pending work items, reschedule self and return so that we
+ * don't hog this CPU's BH.
+ */
+ if (repeat) {
+ if (pool->attrs->nice == HIGHPRI_NICE_LEVEL)
+ queue_work(system_bh_highpri_wq, work);
+ else
+ queue_work(system_bh_wq, work);
+ } else {
+ complete(&dead_work->done);
+ }
+}
+
+/*
+ * @cpu is dead. Drain the remaining BH work items on the current CPU. It's
+ * possible to allocate dead_work per CPU and avoid flushing. However, then we
+ * have to worry about draining overlapping with CPU coming back online or
+ * nesting (one CPU's dead_work queued on another CPU which is also dead and so
+ * on). Let's keep it simple and drain them synchronously. These are BH work
+ * items which shouldn't be requeued on the same pool. Shouldn't take long.
+ */
+void workqueue_softirq_dead(unsigned int cpu)
+{
+ int i;
+
+ for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
+ struct worker_pool *pool = &per_cpu(bh_worker_pools, cpu)[i];
+ struct wq_drain_dead_softirq_work dead_work;
+
+ if (!need_more_worker(pool))
+ continue;
+
+ INIT_WORK(&dead_work.work, drain_dead_softirq_workfn);
+ dead_work.pool = pool;
+ init_completion(&dead_work.done);
+
+ if (pool->attrs->nice == HIGHPRI_NICE_LEVEL)
+ queue_work(system_bh_highpri_wq, &dead_work.work);
+ else
+ queue_work(system_bh_wq, &dead_work.work);
+
+ wait_for_completion(&dead_work.done);
+ }
+}
+
/**
* check_flush_dependency - check for flush dependency sanity
* @target_wq: workqueue being flushed
]
|
| 52/2 |
2026/11/19 02:09 |
tool |
git-show |
0m
Args: map[Commit:8fea0c8fda301 Stat:true]
Results: map[Output:commit 8fea0c8fda30129b4168464975505d5dc9735ac1
Author: Tejun Heo <tj@kernel.org>
Date: Sun Feb 4 11:34:34 2024 -1000
usb: core: hcd: Convert from tasklet to BH workqueue
The only generic interface to execute asynchronously in the BH context is
tasklet; however, it's marked deprecated and has some design flaws. To
replace tasklets, BH workqueue support was recently added. A BH workqueue
behaves similarly to regular workqueues except that the queued work items
are executed in the BH context.
This patch converts usb hcd from tasklet to BH workqueue.
Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: linux-usb@vger.kernel.org
drivers/usb/core/hcd.c | 23 ++++++++++++-----------
include/linux/usb/hcd.h | 2 +-
2 files changed, 13 insertions(+), 12 deletions(-)
]
|
| 53/2 |
2026/11/19 02:09 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 65688
output: 64
thoughts: 165
Thoughts: **Dawning Realization and Investigative Urge**
Wait a minute! My attention is immediately snagged by the output from `grepper` – specifically, the line indicating a match for `lockdep_depth` right at the very beginning of `net/core/dev.c` (line 1, no less!). This is a significant clue, and my immediate thought is to delve deeper. I need to understand *why* `lockdep_depth` is being utilized in `net/core/dev.c`. More importantly, I need to ascertain *how* it's interacting with or checking `debug_locks`. This isn't just about knowing it's there; it's about understanding the mechanism.
Simultaneously, another piece of information surfaces: a commit, identified by the hash `4cb1ef64609f`, titled "workqueue: Implement BH workqueues to eventually replace tasklets". This commit likely introduces or modifies aspects of the kernel that are relevant to lock management and concurrency. My instinct is to correlate this commit with my findings in `net/core/dev.c`.
Therefore, my next logical steps are clear and driven by a desire to connect these dots: I'll perform a `grepper` search directly on `net/core/dev.c` to fully explore its usage of `lockdep_depth`, and I'll use `git show` on `4cb1ef64609f` to meticulously examine the changes introduced by that commit. This dual approach should illuminate the relationship between the observed `lockdep_depth` usage, the `debug_locks` check, and the historical context provided by the workqueue commit.
|
| 54/2 |
2026/11/19 02:10 |
tool |
grepper |
0m
Args: map[Expression:lockdep_depth PathPrefixes:[net/core/dev.c]]
Results: map[Output:net/core/dev.c=12670=static void netdev_lock_ops_close_many(struct list_head *head,
--
net/core/dev.c-12687- */
net/core/dev.c:12688: if (lockdep_depth(current) > MAX_LOCK_DEPTH / 2)
net/core/dev.c-12689- netif_close_many_and_unlock(close_head);
]
|
| 55/2 |
2026/11/19 02:10 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 4cb1ef64609f9b0254184b2947824f4b46ccab22
Author: Tejun Heo <tj@kernel.org>
Date: Sun Feb 4 11:28:06 2024 -1000
workqueue: Implement BH workqueues to eventually replace tasklets
The only generic interface to execute asynchronously in the BH context is
tasklet; however, it's marked deprecated and has some design flaws such as
the execution code accessing the tasklet item after the execution is
complete which can lead to subtle use-after-free in certain usage scenarios
and less-developed flush and cancel mechanisms.
This patch implements BH workqueues which share the same semantics and
features of regular workqueues but execute their work items in the softirq
context. As there is always only one BH execution context per CPU, none of
the concurrency management mechanisms applies and a BH workqueue can be
thought of as a convenience wrapper around softirq.
Except for the inability to sleep while executing and lack of max_active
adjustments, BH workqueues and work items should behave the same as regular
workqueues and work items.
Currently, the execution is hooked to tasklet[_hi]. However, the goal is to
convert all tasklet users over to BH workqueues. Once the conversion is
complete, tasklet can be removed and BH workqueues can directly take over
the tasklet softirqs.
system_bh[_highpri]_wq are added. As queue-wide flushing doesn't exist in
tasklet, all existing tasklet users should be able to use the system BH
workqueues without creating their own workqueues.
v3: - Add missing interrupt.h include.
v2: - Instead of using tasklets, hook directly into its softirq action
functions - tasklet[_hi]_action(). This is slightly cheaper and closer
to the eventual code structure we want to arrive at. Suggested by Lai.
- Lai also pointed out several places which need NULL worker->task
handling or can use clarification. Updated.
Signed-off-by: Tejun Heo <tj@kernel.org>
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/CAHk-=wjDW53w4-YcSmgKC5RruiRLHmJ1sXeYdp_ZgVoBw=5byA@mail.gmail.com
Tested-by: Allen Pais <allen.lkml@gmail.com>
Reviewed-by: Lai Jiangshan <jiangshanlai@gmail.com>
diff --git a/Documentation/core-api/workqueue.rst b/Documentation/core-api/workqueue.rst
index 33c4539155d9..2d6af6c4665c 100644
--- a/Documentation/core-api/workqueue.rst
+++ b/Documentation/core-api/workqueue.rst
@@ -77,10 +77,12 @@ wants a function to be executed asynchronously it has to set up a work
item pointing to that function and queue that work item on a
workqueue.
-Special purpose threads, called worker threads, execute the functions
-off of the queue, one after the other. If no work is queued, the
-worker threads become idle. These worker threads are managed in so
-called worker-pools.
+A work item can be executed in either a thread or the BH (softirq) context.
+
+For threaded workqueues, special purpose threads, called [k]workers, execute
+the functions off of the queue, one after the other. If no work is queued,
+the worker threads become idle. These worker threads are managed in
+worker-pools.
The cmwq design differentiates between the user-facing workqueues that
subsystems and drivers queue work items on and the backend mechanism
@@ -91,6 +93,12 @@ for high priority ones, for each possible CPU and some extra
worker-pools to serve work items queued on unbound workqueues - the
number of these backing pools is dynamic.
+BH workqueues use the same framework. However, as there can only be one
+concurrent execution context, there's no need to worry about concurrency.
+Each per-CPU BH worker pool contains only one pseudo worker which represents
+the BH execution context. A BH workqueue can be considered a convenience
+interface to softirq.
+
Subsystems and drivers can create and queue work items through special
workqueue API functions as they see fit. They can influence some
aspects of the way the work items are executed by setting flags on the
@@ -106,7 +114,7 @@ unless specifically overridden, a work item of a bound workqueue will
be queued on the worklist of either normal or highpri worker-pool that
is associated to the CPU the issuer is running on.
-For any worker pool implementation, managing the concurrency level
+For any thread pool implementation, managing the concurrency level
(how many execution contexts are active) is an important issue. cmwq
tries to keep the concurrency at a minimal but sufficient level.
Minimal to save resources and sufficient in that the system is used at
@@ -164,6 +172,17 @@ resources, scheduled and executed.
``flags``
---------
+``WQ_BH``
+ BH workqueues can be considered a convenience interface to softirq. BH
+ workqueues are always per-CPU and all BH work items are executed in the
+ queueing CPU's softirq context in the queueing order.
+
+ All BH workqueues must have 0 ``max_active`` and ``WQ_HIGHPRI`` is the
+ only allowed additional flag.
+
+ BH work items cannot sleep. All other features such as delayed queueing,
+ flushing and canceling are supported.
+
``WQ_UNBOUND``
Work items queued to an unbound wq are served by the special
worker-pools which host workers which are not bound to any
diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index 232baea90a1d..283d7891b4c4 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -353,6 +353,7 @@ static inline unsigned int work_static(struct work_struct *work) { return 0; }
* Documentation/core-api/workqueue.rst.
*/
enum wq_flags {
+ WQ_BH = 1 << 0, /* execute in bottom half (softirq) context */
WQ_UNBOUND = 1 << 1, /* not bound to any cpu */
WQ_FREEZABLE = 1 << 2, /* freeze during suspend */
WQ_MEM_RECLAIM = 1 << 3, /* may be used for memory reclaim */
@@ -392,6 +393,9 @@ enum wq_flags {
__WQ_ORDERED = 1 << 17, /* internal: workqueue is ordered */
__WQ_LEGACY = 1 << 18, /* internal: create*_workqueue() */
__WQ_ORDERED_EXPLICIT = 1 << 19, /* internal: alloc_ordered_workqueue() */
+
+ /* BH wq only allows the following flags */
+ __WQ_BH_ALLOWS = WQ_BH | WQ_HIGHPRI,
};
enum wq_consts {
@@ -434,6 +438,9 @@ enum wq_consts {
* they are same as their non-power-efficient counterparts - e.g.
* system_power_efficient_wq is identical to system_wq if
* 'wq_power_efficient' is disabled. See WQ_POWER_EFFICIENT for more info.
+ *
+ * system_bh[_highpri]_wq are convenience interface to softirq. BH work items
+ * are executed in the queueing CPU's BH context in the queueing order.
*/
extern struct workqueue_struct *system_wq;
extern struct workqueue_struct *system_highpri_wq;
@@ -442,6 +449,10 @@ extern struct workqueue_struct *system_unbound_wq;
extern struct workqueue_struct *system_freezable_wq;
extern struct workqueue_struct *system_power_efficient_wq;
extern struct workqueue_struct *system_freezable_power_efficient_wq;
+extern struct workqueue_struct *system_bh_wq;
+extern struct workqueue_struct *system_bh_highpri_wq;
+
+void workqueue_softirq_action(bool highpri);
/**
* alloc_workqueue - allocate a workqueue
diff --git a/kernel/softirq.c b/kernel/softirq.c
index 210cf5f8d92c..547d282548a8 100644
--- a/kernel/softirq.c
+++ b/kernel/softirq.c
@@ -27,6 +27,7 @@
#include <linux/tick.h>
#include <linux/irq.h>
#include <linux/wait_bit.h>
+#include <linux/workqueue.h>
#include <asm/softirq_stack.h>
@@ -802,11 +803,13 @@ static void tasklet_action_common(struct softirq_action *a,
static __latent_entropy void tasklet_action(struct softirq_action *a)
{
+ workqueue_softirq_action(false);
tasklet_action_common(a, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
}
static __latent_entropy void tasklet_hi_action(struct softirq_action *a)
{
+ workqueue_softirq_action(true);
tasklet_action_common(a, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
}
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 767971a29c7a..78b4b992e1a3 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -29,6 +29,7 @@
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/init.h>
+#include <linux/interrupt.h>
#include <linux/signal.h>
#include <linux/completion.h>
#include <linux/workqueue.h>
@@ -72,8 +73,12 @@ enum worker_pool_flags {
* Note that DISASSOCIATED should be flipped only while holding
* wq_pool_attach_mutex to avoid changing binding state while
* worker_attach_to_pool() is in progress.
+ *
+ * As there can only be one concurrent BH execution context per CPU, a
+ * BH pool is per-CPU and always DISASSOCIATED.
*/
- POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */
+ POOL_BH = 1 << 0, /* is a BH pool */
+ POOL_MANAGER_ACTIVE = 1 << 1, /* being managed */
POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
};
@@ -115,6 +120,14 @@ enum wq_internal_consts {
WQ_NAME_LEN = 32,
};
+/*
+ * We don't want to trap softirq for too long. See MAX_SOFTIRQ_TIME and
+ * MAX_SOFTIRQ_RESTART in kernel/softirq.c. These are macros because
+ * msecs_to_jiffies() can't be an initializer.
+ */
+#define BH_WORKER_JIFFIES msecs_to_jiffies(2)
+#define BH_WORKER_RESTARTS 10
+
/*
* Structure fields follow one of the following exclusion rules.
*
@@ -443,8 +456,13 @@ static bool wq_debug_force_rr_cpu = false;
#endif
module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644);
+/* the BH worker pools */
+static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
+ bh_worker_pools);
+
/* the per-cpu worker pools */
-static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools);
+static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
+ cpu_worker_pools);
static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
@@ -478,6 +496,10 @@ struct workqueue_struct *system_power_efficient_wq __ro_after_init;
EXPORT_SYMBOL_GPL(system_power_efficient_wq);
struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init;
EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
+struct workqueue_struct *system_bh_wq;
+EXPORT_SYMBOL_GPL(system_bh_wq);
+struct workqueue_struct *system_bh_highpri_wq;
+EXPORT_SYMBOL_GPL(system_bh_highpri_wq);
static int worker_thread(void *__worker);
static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
@@ -498,6 +520,11 @@ static void show_one_worker_pool(struct worker_pool *pool);
!lockdep_is_held(&wq_pool_mutex), \
"RCU, wq->mutex or wq_pool_mutex should be held")
+#define for_each_bh_worker_pool(pool, cpu) \
+ for ((pool) = &per_cpu(bh_worker_pools, cpu)[0]; \
+ (pool) < &per_cpu(bh_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
+ (pool)++)
+
#define for_each_cpu_worker_pool(pool, cpu) \
for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
(pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
@@ -1186,6 +1213,14 @@ static bool kick_pool(struct worker_pool *pool)
if (!need_more_worker(pool) || !worker)
return false;
+ if (pool->flags & POOL_BH) {
+ if (pool->attrs->nice == HIGHPRI_NICE_LEVEL)
+ raise_softirq_irqoff(HI_SOFTIRQ);
+ else
+ raise_softirq_irqoff(TASKLET_SOFTIRQ);
+ return true;
+ }
+
p = worker->task;
#ifdef CONFIG_SMP
@@ -1668,7 +1703,7 @@ static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill)
lockdep_assert_held(&pool->lock);
if (!nna) {
- /* per-cpu workqueue, pwq->nr_active is sufficient */
+ /* BH or per-cpu workqueue, pwq->nr_active is sufficient */
obtained = pwq->nr_active < READ_ONCE(wq->max_active);
goto out;
}
@@ -2523,19 +2558,21 @@ static cpumask_t *pool_allowed_cpus(struct worker_pool *pool)
* cpu-[un]hotplugs.
*/
static void worker_attach_to_pool(struct worker *worker,
- struct worker_pool *pool)
+ struct worker_pool *pool)
{
mutex_lock(&wq_pool_attach_mutex);
/*
- * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains
- * stable across this function. See the comments above the flag
- * definition for details.
+ * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains stable
+ * across this function. See the comments above the flag definition for
+ * details. BH workers are, while per-CPU, always DISASSOCIATED.
*/
- if (pool->flags & POOL_DISASSOCIATED)
+ if (pool->flags & POOL_DISASSOCIATED) {
worker->flags |= WORKER_UNBOUND;
- else
+ } else {
+ WARN_ON_ONCE(pool->flags & POOL_BH);
kthread_set_per_cpu(worker->task, pool->cpu);
+ }
if (worker->rescue_wq)
set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool));
@@ -2559,6 +2596,9 @@ static void worker_detach_from_pool(struct worker *worker)
struct worker_pool *pool = worker->pool;
struct completion *detach_completion = NULL;
+ /* there is one permanent BH worker per CPU which should never detach */
+ WARN_ON_ONCE(pool->flags & POOL_BH);
+
mutex_lock(&wq_pool_attach_mutex);
kthread_set_per_cpu(worker->task, -1);
@@ -2610,27 +2650,29 @@ static struct worker *create_worker(struct worker_pool *pool)
worker->id = id;
- if (pool->cpu >= 0)
- snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
- pool->attrs->nice < 0 ? "H" : "");
- else
- snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
-
- worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
- "kworker/%s", id_buf);
- if (IS_ERR(worker->task)) {
- if (PTR_ERR(worker->task) == -EINTR) {
- pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n",
- id_buf);
- } else {
- pr_err_once("workqueue: Failed to create a worker thread: %pe",
- worker->task);
+ if (!(pool->flags & POOL_BH)) {
+ if (pool->cpu >= 0)
+ snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
+ pool->attrs->nice < 0 ? "H" : "");
+ else
+ snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
+
+ worker->task = kthread_create_on_node(worker_thread, worker,
+ pool->node, "kworker/%s", id_buf);
+ if (IS_ERR(worker->task)) {
+ if (PTR_ERR(worker->task) == -EINTR) {
+ pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n",
+ id_buf);
+ } else {
+ pr_err_once("workqueue: Failed to create a worker thread: %pe",
+ worker->task);
+ }
+ goto fail;
}
- goto fail;
- }
- set_user_nice(worker->task, pool->attrs->nice);
- kthread_bind_mask(worker->task, pool_allowed_cpus(pool));
+ set_user_nice(worker->task, pool->attrs->nice);
+ kthread_bind_mask(worker->task, pool_allowed_cpus(pool));
+ }
/* successful, attach the worker to the pool */
worker_attach_to_pool(worker, pool);
@@ -2646,7 +2688,8 @@ static struct worker *create_worker(struct worker_pool *pool)
* check if not woken up soon. As kick_pool() is noop if @pool is empty,
* wake it up explicitly.
*/
- wake_up_process(worker->task);
+ if (worker->task)
+ wake_up_process(worker->task);
raw_spin_unlock_irq(&pool->lock);
@@ -2988,7 +3031,8 @@ __acquires(&pool->lock)
worker->current_work = work;
worker->current_func = work->func;
worker->current_pwq = pwq;
- worker->current_at = worker->task->se.sum_exec_runtime;
+ if (worker->task)
+ worker->current_at = worker->task->se.sum_exec_runtime;
work_data = *work_data_bits(work);
worker->current_color = get_work_color(work_data);
@@ -3086,7 +3130,8 @@ __acquires(&pool->lock)
* stop_machine. At the same time, report a quiescent RCU state so
* the same condition doesn't freeze RCU.
*/
- cond_resched();
+ if (worker->task)
+ cond_resched();
raw_spin_lock_irq(&pool->lock);
@@ -3369,6 +3414,61 @@ static int rescuer_thread(void *__rescuer)
goto repeat;
}
+static void bh_worker(struct worker *worker)
+{
+ struct worker_pool *pool = worker->pool;
+ int nr_restarts = BH_WORKER_RESTARTS;
+ unsigned long end = jiffies + BH_WORKER_JIFFIES;
+
+ raw_spin_lock_irq(&pool->lock);
+ worker_leave_idle(worker);
+
+ /*
+ * This function follows the structure of worker_thread(). See there for
+ * explanations on each step.
+ */
+ if (!need_more_worker(pool))
+ goto done;
+
+ WARN_ON_ONCE(!list_empty(&worker->scheduled));
+ worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
+
+ do {
+ struct work_struct *work =
+ list_first_entry(&pool->worklist,
+ struct work_struct, entry);
+
+ if (assign_work(work, worker, NULL))
+ process_scheduled_works(worker);
+ } while (keep_working(pool) &&
+ --nr_restarts && time_before(jiffies, end));
+
+ worker_set_flags(worker, WORKER_PREP);
+done:
+ worker_enter_idle(worker);
+ kick_pool(pool);
+ raw_spin_unlock_irq(&pool->lock);
+}
+
+/*
+ * TODO: Convert all tasklet users to workqueue and use softirq directly.
+ *
+ * This is currently called from tasklet[_hi]action() and thus is also called
+ * whenever there are tasklets to run. Let's do an early exit if there's nothing
+ * queued. Once conversion from tasklet is complete, the need_more_worker() test
+ * can be dropped.
+ *
+ * After full conversion, we'll add worker->softirq_action, directly use the
+ * softirq action and obtain the worker pointer from the softirq_action pointer.
+ */
+void workqueue_softirq_action(bool highpri)
+{
+ struct worker_pool *pool =
+ &per_cpu(bh_worker_pools, smp_processor_id())[highpri];
+ if (need_more_worker(pool))
+ bh_worker(list_first_entry(&pool->workers, struct worker, node));
+}
+
/**
* check_flush_dependency - check for flush dependency sanity
* @target_wq: workqueue being flushed
@@ -3441,6 +3541,7 @@ static void insert_wq_barrier(struct pool_workqueue *pwq,
struct wq_barrier *barr,
struct work_struct *target, struct worker *worker)
{
+ static __maybe_unused struct lock_class_key bh_key, thr_key;
unsigned int work_flags = 0;
unsigned int work_color;
struct list_head *head;
@@ -3450,8 +3551,13 @@ static void insert_wq_barrier(struct pool_workqueue *pwq,
* as we know for sure that this will not trigger any of the
* checks and call back into the fixup functions where we
* might deadlock.
+ *
+ * BH and threaded workqueues need separate lockdep keys to avoid
+ * spuriously triggering "inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W}
+ * usage".
*/
- INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
+ INIT_WORK_ONSTACK_KEY(&barr->work, wq_barrier_func,
+ (pwq->wq->flags & WQ_BH) ? &bh_key : &thr_key);
__set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
init_completion_map(&barr->done, &target->lockdep_map);
@@ -3557,15 +3663,31 @@ static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
static void touch_wq_lockdep_map(struct workqueue_struct *wq)
{
+#ifdef CONFIG_LOCKDEP
+ if (wq->flags & WQ_BH)
+ local_bh_disable();
+
lock_map_acquire(&wq->lockdep_map);
lock_map_release(&wq->lockdep_map);
+
+ if (wq->flags & WQ_BH)
+ local_bh_enable();
+#endif
}
static void touch_work_lockdep_map(struct work_struct *work,
struct workqueue_struct *wq)
{
+#ifdef CONFIG_LOCKDEP
+ if (wq->flags & WQ_BH)
+ local_bh_disable();
+
lock_map_acquire(&work->lockdep_map);
lock_map_release(&work->lockdep_map);
+
+ if (wq->flags & WQ_BH)
+ local_bh_enable();
+#endif
}
/**
@@ -5019,10 +5141,17 @@ static int alloc_and_link_pwqs(struct workqueue_struct *wq)
if (!(wq->flags & WQ_UNBOUND)) {
for_each_possible_cpu(cpu) {
- struct pool_workqueue **pwq_p =
- per_cpu_ptr(wq->cpu_pwq, cpu);
- struct worker_pool *pool =
- &(per_cpu_ptr(cpu_worker_pools, cpu)[highpri]);
+ struct pool_workqueue **pwq_p;
+ struct worker_pool __percpu *pools;
+ struct worker_pool *pool;
+
+ if (wq->flags & WQ_BH)
+ pools = bh_worker_pools;
+ else
+ pools = cpu_worker_pools;
+
+ pool = &(per_cpu_ptr(pools, cpu)[highpri]);
+ pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu);
*pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL,
pool->node);
@@ -5197,6 +5326,13 @@ struct workqueue_struct *alloc_workqueue(const char *fmt,
size_t wq_size;
int name_len;
+ if (flags & WQ_BH) {
+ if (WARN_ON_ONCE(flags & ~__WQ_BH_ALLOWS))
+ return NULL;
+ if (WARN_ON_ONCE(max_active))
+ return NULL;
+ }
+
/*
* Unbound && max_active == 1 used to imply ordered, which is no longer
* the case on many machines due to per-pod pools. While
@@ -5234,8 +5370,16 @@ struct workqueue_struct *alloc_workqueue(const char *fmt,
pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n",
wq->name);
- max_active = max_active ?: WQ_DFL_ACTIVE;
- max_active = wq_clamp_max_active(max_active, flags, wq->name);
+ if (flags & WQ_BH) {
+ /*
+ * BH workqueues always share a single execution context per CPU
+ * and don't impose any max_active limit.
+ */
+ max_active = INT_MAX;
+ } else {
+ max_active = max_active ?: WQ_DFL_ACTIVE;
+ max_active = wq_clamp_max_active(max_active, flags, wq->name);
+ }
/* init wq */
wq->flags = flags;
@@ -5416,6 +5560,9 @@ EXPORT_SYMBOL_GPL(destroy_workqueue);
*/
void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
{
+ /* max_active doesn't mean anything for BH workqueues */
+ if (WARN_ON(wq->flags & WQ_BH))
+ return;
/* disallow meddling with max_active for ordered workqueues */
if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT))
return;
@@ -5617,7 +5764,24 @@ static void pr_cont_pool_info(struct worker_pool *pool)
pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask);
if (pool->node != NUMA_NO_NODE)
pr_cont(" node=%d", pool->node);
- pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice);
+ pr_cont(" flags=0x%x", pool->flags);
+ if (pool->flags & POOL_BH)
+ pr_cont(" bh%s",
+ pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : "");
+ else
+ pr_cont(" nice=%d", pool->attrs->nice);
+}
+
+static void pr_cont_worker_id(struct worker *worker)
+{
+ struct worker_pool *pool = worker->pool;
+
+ if (pool->flags & WQ_BH)
+ pr_cont("bh%s",
+ pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : "");
+ else
+ pr_cont("%d%s", task_pid_nr(worker->task),
+ worker->rescue_wq ? "(RESCUER)" : "");
}
struct pr_cont_work_struct {
@@ -5694,10 +5858,9 @@ static void show_pwq(struct pool_workqueue *pwq)
if (worker->current_pwq != pwq)
continue;
- pr_cont("%s %d%s:%ps", comma ? "," : "",
- task_pid_nr(worker->task),
- worker->rescue_wq ? "(RESCUER)" : "",
- worker->current_func);
+ pr_cont(" %s", comma ? "," : "");
+ pr_cont_worker_id(worker);
+ pr_cont(":%ps", worker->current_func);
list_for_each_entry(work, &worker->scheduled, entry)
pr_cont_work(false, work, &pcws);
pr_cont_work_flush(comma, (work_func_t)-1L, &pcws);
@@ -5816,8 +5979,8 @@ static void show_one_worker_pool(struct worker_pool *pool)
pr_cont(" manager: %d",
task_pid_nr(pool->manager->task));
list_for_each_entry(worker, &pool->idle_list, entry) {
- pr_cont(" %s%d", first ? "idle: " : "",
- task_pid_nr(worker->task));
+ pr_cont(" %s", first ? "idle: " : "");
+ pr_cont_worker_id(worker);
first = false;
}
pr_cont("\n");
@@ -6090,13 +6253,15 @@ int workqueue_online_cpu(unsigned int cpu)
mutex_lock(&wq_pool_mutex);
for_each_pool(pool, pi) {
- mutex_lock(&wq_pool_attach_mutex);
+ /* BH pools aren't affected by hotplug */
+ if (pool->flags & POOL_BH)
+ continue;
+ mutex_lock(&wq_pool_attach_mutex);
if (pool->cpu == cpu)
rebind_workers(pool);
else if (pool->cpu < 0)
restore_unbound_workers_cpumask(pool, cpu);
-
mutex_unlock(&wq_pool_attach_mutex);
}
@@ -7053,7 +7218,7 @@ static void wq_watchdog_timer_fn(struct timer_list *unused)
/* did we stall? */
if (time_after(now, ts + thresh)) {
lockup_detected = true;
- if (pool->cpu >= 0) {
+ if (pool->cpu >= 0 && !(pool->flags & POOL_BH)) {
pool->cpu_stall = true;
cpu_pool_stall = true;
}
@@ -7218,10 +7383,16 @@ void __init workqueue_init_early(void)
pt->pod_node[0] = NUMA_NO_NODE;
pt->cpu_pod[0] = 0;
- /* initialize CPU pools */
+ /* initialize BH and CPU pools */
for_each_possible_cpu(cpu) {
struct worker_pool *pool;
+ i = 0;
+ for_each_bh_worker_pool(pool, cpu) {
+ init_cpu_worker_pool(pool, cpu, std_nice[i++]);
+ pool->flags |= POOL_BH;
+ }
+
i = 0;
for_each_cpu_worker_pool(pool, cpu)
init_cpu_worker_pool(pool, cpu, std_nice[i++]);
@@ -7257,10 +7428,14 @@ void __init workqueue_init_early(void)
system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient",
WQ_FREEZABLE | WQ_POWER_EFFICIENT,
0);
+ system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0);
+ system_bh_highpri_wq = alloc_workqueue("events_bh_highpri",
+ WQ_BH | WQ_HIGHPRI, 0);
BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
!system_unbound_wq || !system_freezable_wq ||
!system_power_efficient_wq ||
- !system_freezable_power_efficient_wq);
+ !system_freezable_power_efficient_wq ||
+ !system_bh_wq || !system_bh_highpri_wq);
}
static void __init wq_cpu_intensive_thresh_init(void)
@@ -7326,9 +7501,10 @@ void __init workqueue_init(void)
* up. Also, create a rescuer for workqueues that requested it.
*/
for_each_possible_cpu(cpu) {
- for_each_cpu_worker_pool(pool, cpu) {
+ for_each_bh_worker_pool(pool, cpu)
+ pool->node = cpu_to_node(cpu);
+ for_each_cpu_worker_pool(pool, cpu)
pool->node = cpu_to_node(cpu);
- }
}
list_for_each_entry(wq, &workqueues, list) {
@@ -7339,7 +7515,16 @@ void __init workqueue_init(void)
mutex_unlock(&wq_pool_mutex);
- /* create the initial workers */
+ /*
+ * Create the initial workers. A BH pool has one pseudo worker that
+ * represents the shared BH execution context and thus doesn't get
+ * affected by hotplug events. Create the BH pseudo workers for all
+ * possible CPUs here.
+ */
+ for_each_possible_cpu(cpu)
+ for_each_bh_worker_pool(pool, cpu)
+ BUG_ON(!create_worker(pool));
+
for_each_online_cpu(cpu) {
for_each_cpu_worker_pool(pool, cpu) {
pool->flags &= ~POOL_DISASSOCIATED;
diff --git a/tools/workqueue/wq_dump.py b/tools/workqueue/wq_dump.py
index bd381511bd9a..d29b918306b4 100644
--- a/tools/workqueue/wq_dump.py
+++ b/tools/workqueue/wq_dump.py
@@ -79,7 +79,9 @@ def cpumask_str(cpumask):
wq_type_len = 9
def wq_type_str(wq):
- if wq.flags & WQ_UNBOUND:
+ if wq.flags & WQ_BH:
+ return f'{"bh":{wq_type_len}}'
+ elif wq.flags & WQ_UNBOUND:
if wq.flags & WQ_ORDERED:
return f'{"ordered":{wq_type_len}}'
else:
@@ -97,6 +99,7 @@ wq_pod_types = prog['wq_pod_types']
wq_affn_dfl = prog['wq_affn_dfl']
wq_affn_names = prog['wq_affn_names']
+WQ_BH = prog['WQ_BH']
WQ_UNBOUND = prog['WQ_UNBOUND']
WQ_ORDERED = prog['__WQ_ORDERED']
WQ_MEM_RECLAIM = prog['WQ_MEM_RECLAIM']
@@ -107,6 +110,8 @@ WQ_AFFN_CACHE = prog['WQ_AFFN_CACHE']
WQ_AFFN_NUMA = prog['WQ_AFFN_NUMA']
WQ_AFFN_SYSTEM = prog['WQ_AFFN_SYSTEM']
+POOL_BH = prog['POOL_BH']
+
WQ_NAME_LEN = prog['WQ_NAME_LEN'].value_()
cpumask_str_len = len(cpumask_str(wq_unbound_cpumask))
@@ -151,10 +156,12 @@ for pi, pool in idr_for_each(worker_pool_idr):
for pi, pool in idr_for_each(worker_pool_idr):
pool = drgn.Object(prog, 'struct worker_pool', address=pool)
- print(f'pool[{pi:0{max_pool_id_len}}] ref={pool.refcnt.value_():{max_ref_len}} nice={pool.attrs.nice.value_():3} ', end='')
+ print(f'pool[{pi:0{max_pool_id_len}}] flags=0x{pool.flags.value_():02x} ref={pool.refcnt.value_():{max_ref_len}} nice={pool.attrs.nice.value_():3} ', end='')
print(f'idle/workers={pool.nr_idle.value_():3}/{pool.nr_workers.value_():3} ', end='')
if pool.cpu >= 0:
print(f'cpu={pool.cpu.value_():3}', end='')
+ if pool.flags & POOL_BH:
+ print(' bh', end='')
else:
print(f'cpus={cpumask_str(pool.attrs.cpumask)}', end='')
print(f' pod_cpus={cpumask_str(pool.attrs.__pod_cpumask)}', end='')
]
|
| 56/2 |
2026/11/19 02:10 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 76429
output: 136
thoughts: 1368
Thoughts: **The Eureka Moment: Unraveling the Lockdep Puzzle**
Okay, hold everything! I need to stop and think this through very, very carefully. I was looking at these `syzbot` reports, specifically the `c35aea39d1e10`, `4cb1ef64609f`, and `8fea0c8fda301` ones, and they're all pointing to issues in **February 2024**, which corresponds to kernel version 6.9. That's ancient history at this point, given that the head of the tree I'm examining is all the way out in **November 2026**, sitting at `Linux 7.4-rc3` with a bunch of merges on top.
This discrepancy immediately set off alarm bells. Why would a bug identified in February 2024 only manifest itself *now*, in a vastly different kernel version? Or, perhaps the bug *has* existed since February 2024, but something recent has drastically changed its behavior. The possibilities swirling in my head are:
1. Did a *recent commit* cause `MAX_LOCKDEP_*` to overflow or trigger much more frequently?
2. Did a *recent commit* cause `debug_locks_off()` to be invoked, but critically, *without printing any output*? This would explain why I'm not seeing obvious crash logs pointing directly to it.
I can't just assume either scenario. I need to investigate. My immediate thought is to question whether a recent change could have silently enabled `debug_locks_off()` or made `MAX_LOCKDEP_*` blow up instantly.
To get to the bottom of this, I need to scrutinize every commit that could have touched these critical areas. I'm thinking about digging into all commits in `kernel/locking/`, `kernel/workqueue.c`, `kernel/softirq.c`, the entire `net/` hierarchy, and `drivers/usb/` over the last several months.
Then, something else caught my eye. I’m looking back at commit `02c6be7d675b`, titled "locking/lockdep: Invalidate stale class_cache entries for zapped classes," which landed on **August 24, 2026**. This is *recent* enough to be highly relevant.
Wait a minute! I need to stop and read that commit, `02c6be7d675b`, *again*, and this time, I need to do it meticulously, line by line. Because, oh my god, I think I might have found something. Let me re-examine lines 984-989 of `kernel/locking/lockdep.c` within that commit:
if (!class)
return false;
if (unlikely(!lock->key))
return false;
return true;
}
Now, I'm thinking about specific scenarios:
1. **PER-CPU static locks:** What happens when `lock` is a PER-CPU static lock? I'm looking at how `assign_lock_key` works:
if (__is_kernel_percpu_address(addr, &can_addr))
lock->key = (void *)can_addr;
In this case, `lock->key` is set to `(void *)can_addr`. This seems to correctly align `lock->key->subkeys + class_subclass` with `class->key`.
2. **`lock_set_subclass` ( `force = 1` in `register_lock_class`) usage:** If `subclass == 0` during acquisition, then `class_subclass` becomes `class->subclass`, and `lock->key->subkeys + class->subclass` should match `class->key`. If `subclass != 0`, then `class_subclass` is `subclass`, and we're checking `lock->key->subkeys + subclass` against `class->key`. This also seems to hold.
3. **`lock_class_cache_is_valid` returning `false`:** Even if `lock_class_cache_is_valid` returns `false`, I'm observing that within `__lock_acquire`, if `class` becomes `NULL` after the cache check, the code proceeds to `register_lock_class`. And crucial here, in `register_lock_class` at line 1345:
class = look_up_lock_class(lock, subclass);
if (likely(class))
goto out_set_class_cache;
if (!lock->key) {
if (!assign_lock_key(lock))
return NULL;
} else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) {
return NULL;
}
This indicates that `look_up_lock_class` will find an *existing* class in the hash table even if the cache is stale, without necessarily allocating a new key.
Therefore, my current analysis suggests that commit `02c6be7d675b` itself **did NOT introduce a leak of keys or cause `debug_locks` to be turned off**.
This means I need to broaden my search again. The problem must lie elsewhere. I need to go back to my original hypothesis: what about *all other commits* in `kernel/locking/` or any other relevant subsystem that *could* have touched `debug_locks` or related locking mechanisms?
To do this, I'm going to run a comprehensive `git-log` search. I'll filter for the string "debug_locks" across the entire repository. Additionally, I'll specifically target commits touching `kernel/softirq.c`, `include/linux/lockdep.h`, `lib/debug_locks.c`, and the `kernel/locking/` directory, looking back over the last three years. This wide net is essential to catch any subtle change that might have enabled this behavior.
|
| 57/2 |
2026/11/19 02:10 |
tool |
git-log |
5m
Args: map[CodeRegexp:debug_locks Count:30 Since:3 years]
Results: map[Output:9e174422f6f1 irq: Add {over,under}flow detection for local_interrupt_{enable,disable}
8f2109843137 locking/lockdep: add sequence counter to held_lock
d084a73714f8 compiler-context-analysis: Introduce scoped init guards
8221b3450501 scsi: lpfc: Clean up extraneous phba dentries
27b6bce72c77 drm/ci: enable lockdep detection
7d9dda6f628f module: Allow __module_address() to be called from RCU section.
0784181b44af lockdep: Add lockdep_cleanup_dead_cpu()
8ec9897ec2e9 netlabel: fix RCU annotation for IPv4 options on socket creation
151c9c724d05 tcp: properly terminate timers for kernel sockets
3fed24fffc76 xfs: Replace xfs_isilocked with xfs_assert_ilocked
]
|
| 58/2 |
2026/11/19 02:16 |
tool |
git-log |
0m
Args: map[Count:25 PathPrefix:kernel/softirq.c Since:3 years]
Results: map[Output:feb5c9a2447a softirq: Preserve interrupt context on return from interrupt
a0e1fdb96578 softirq: Remove redundant h->action from preempt_count mismatch error
a155ac8f0c52 interrupt: Disable interrupt before modifying hardirq_disable counter
3b0e2a22d408 preempt: Introduce HAS_SEPARATE_PREEMPT_RESCHED_BITS
e901c1510e24 irq,spin_lock: Add counted interrupt disabling/enabling
b54aa0edf059 preempt: Track NMI nesting to separate per-CPU counter
7e641e52cf5f softirq: Prepare for deferred hrtimer rearming
3253cb49cbad softirq: Allow to drop the softirq-BKL lock on PREEMPT_RT
fd4e876f59b7 softirq: Provide a handshake for canceling tasklets via polling
61c39d8c83e2 lockdep: Fix wait context check on softirq for PREEMPT_RT
6675ce20046d softirq: Allow raising SCHED_SOFTIRQ from SMP-call-function on RT kernel
49a17639508c softirq: Use a dedicated thread for timer wakeups on PREEMPT_RT.
49994911b401 softirq: use bit waits instead of var waits.
e68ac2b48849 softirq: Remove unused 'action' parameter from action callback
1dd1eff161bd softirq: Fix suspicious RCU usage in __do_softirq()
1acd92d95fa2 workqueue: Drain BH work items on hot-unplugged CPUs
4cb1ef64609f workqueue: Implement BH workqueues to eventually replace tasklets
]
|
| 59/2 |
2026/11/19 02:16 |
tool |
git-log |
0m
Args: map[Count:25 PathPrefix:kernel/locking/ Since:1 year]
Results: map[Output:408b129aac48 locking/osq: Fix unlink queue corruption
1b9e01fcc47a locking/osq: Switch osq_wait_next()/atomic_cmpxchg() ordering
aa63c0df7992 locking/lockdep: Fix inverted "on"/"last" CPU label in lockdep_print_held_locks()
9cd2f4e304d9 rtmutex: Use accessor for hrtimer_sleeper ->task field
912edebe8501 futex: Provide rt_mutex_.*_schedule() equivalents for futex scheduling
02c6be7d675b locking/lockdep: Invalidate stale class_cache entries for zapped classes
8f2109843137 locking/lockdep: add sequence counter to held_lock
e901c1510e24 irq,spin_lock: Add counted interrupt disabling/enabling
b359800c6970 tracing/lock: Use TRACE_EVENT_FN() for contended_release
f7e2cb6d495a locking/qspinlock: Add contended_release tracepoint
c9c8578ad58e locking/percpu-rwsem: Annotate intentional data race in readers_active_check()
7577e00b9ab5 locking/lockdep: Fix NULL pointer dereference in __lock_set_class()
051f5d223dfc lockdep: Enable the printing of held locks of remote running tasks and print task CPU
89038cc87d80 locking/rt: Fix the incorrect RCU protection in rt_spin_unlock()
4f070ccb4dc4 locking: Add contended_release tracepoint to sleepable locks
7cfa62cf9432 locking/percpu-rwsem: Extract __percpu_up_read()
40a25d59e85b locking/rtmutex: Skip remove_waiter() when waiter is not enqueued
ec9d4f1c4241 sched/proxy: Remove PROXY_WAKING
1628b25248d0 sched: Add blocked_donor link to task for smarter mutex handoffs
bdaf235913e1 locking: mutex: Fix proxy-exec potentially deactivating tasks marked TASK_RUNNING
a9e4e50519e9 locking/rtmutex: Annotate API and implementation
0adc92b910b3 locking/mutex: Fix ww_mutex wait_list operations
3bfdc63936dd rtmutex: Use waiter::task instead of current in remove_waiter()
2d7622669836 sched/locking: Add special p->blocked_on==PROXY_WAKING value for proxy return-migration
fa4a1ff8ab23 locking: Add task::blocked_lock to serialize blocked_on state
]
|
| 60/2 |
2026/11/19 02:16 |
tool |
git-log |
10m
Error:
Args: map[CodeRegexp:lockdep_start_depth Count:20]
Results:
|
| 61/2 |
2026/11/19 02:26 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 79378
output: 127
thoughts: 281
Thoughts: **The Urgent Realization of a Critical Connection**
Okay, wait a second here. My mind just hit a massive U-turn. I need to stop everything and *really* scrutinize the very first commit I saw in the `git-log` for `debug_locks`. I'm staring at commit `9e174422f6f1`, which is titled `irq: Add {over,under}flow detection for local_interrupt_{enable,disable}`. And that's not all; I also have `e901c1510e24` (`irq,spin_lock: Add counted interrupt disabling/enabling`), `a155ac8f0c52` (`interrupt: Disable interrupt before modifying hardirq_disable counter`), and `feb5c9a2447a` (`softirq: Preserve interrupt context on return from interrupt`).
Now, this is ringing serious alarm bells, especially when I recall something else we found during our `grep` earlier: the inclusion of `<linux/debug_locks.h>` within `include/linux/interrupt_rc.h`.
This is it. This is the moment. I need to immediately pause and thoroughly examine all of these, specifically `9e174422f6f1`, `e901c1510e24`, `a155ac8f0c52`, `feb5c9a2447a`, and critically, `include/linux/interrupt_rc.h`. There's a vital connection I'm suddenly seeing, and it demands immediate, focused investigation.
|
| 62/2 |
2026/11/19 02:26 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 9e174422f6f15a1d88994a07067f66c920d2f947
Author: Boqun Feng <boqun@kernel.org>
Date: Thu Oct 1 10:25:16 2026 +0200
irq: Add {over,under}flow detection for local_interrupt_{enable,disable}
Currently there is no way to detect overflow and underflow inside the
HARDIRQ_DISABLE_MASK part of preempt_count. Add these detection guarded
by DEBUG_PREEMPT similar to the detection for PREEMPT_MASK
{over,under}flow.
Suggested-by: Shrikanth Hegde <sshegde@linux.ibm.com>
Signed-off-by: Boqun Feng <boqun@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Shrikanth Hegde <sshegde@linux.ibm.com>
Link: https://patch.msgid.link/20261001082519.16195-3-boqun@kernel.org
diff --git a/include/linux/interrupt_rc.h b/include/linux/interrupt_rc.h
index a9ed937a80e7..d385ab05a2b6 100644
--- a/include/linux/interrupt_rc.h
+++ b/include/linux/interrupt_rc.h
@@ -11,6 +11,7 @@
* make it easier to include without hitting circular header dependencies.
*/
+#include <linux/debug_locks.h>
#include <linux/irqflags.h>
#include <linux/preempt.h>
#include <linux/processor.h>
@@ -65,6 +66,13 @@ static inline void local_interrupt_disable(void)
local_irq_save(flags);
new_count = hardirq_disable_enter();
+ /* Is hardirq disable count overflow soon? */
+ if (IS_ENABLED(CONFIG_DEBUG_PREEMPT) &&
+ DEBUG_LOCKS_WARN_ON(((new_count & HARDIRQ_DISABLE_MASK) +
+ (10 << HARDIRQ_DISABLE_SHIFT)) >=
+ HARDIRQ_DISABLE_MASK))
+ return;
+
if ((new_count & HARDIRQ_DISABLE_MASK) == HARDIRQ_DISABLE_OFFSET)
_local_interrupt_save_state(flags);
}
@@ -73,6 +81,11 @@ static inline void local_interrupt_enable(void)
{
int new_count;
+ /* Unpaired local_interrupt_enable()? Warn and abort. */
+ if (IS_ENABLED(CONFIG_DEBUG_PREEMPT) &&
+ DEBUG_LOCKS_WARN_ON((preempt_count() & HARDIRQ_DISABLE_MASK) == 0))
+ return;
+
new_count = hardirq_disable_exit();
if ((new_count & HARDIRQ_DISABLE_MASK) == 0)
]
|
| 63/2 |
2026/11/19 02:26 |
tool |
git-show |
0m
Args:
Results: map[Output:commit e901c1510e24726dcbd6340ee927b3ac8b992043
Author: Boqun Feng <boqun@kernel.org>
Date: Tue Aug 4 11:26:57 2026 -0700
irq,spin_lock: Add counted interrupt disabling/enabling
Currently the nested interrupt disabling and enabling is represented by
_irqsave() and _irqrestore() APIs, which are relatively unsafe, for
example:
<interrupts are enabled as beginning>
spin_lock_irqsave(l1, flag1);
spin_lock_irqsave(l2, flag2);
spin_unlock_irqrestore(l1, flags1);
<l2 is still held but interrupts are enabled>
// accesses to interrupt-disable protected data will cause races
This is even easier to trigger with guard facilities:
unsigned long flag2;
scoped_guard(spin_lock_irqsave, l1) {
spin_lock_irqsave(l2, flag2);
}
// l2 locked but interrupts are enabled.
spin_unlock_irqrestore(l2, flag2);
(Hand-to-hand locking critical sections are not uncommon for a
fine-grained lock design)
And because of this unsafety, Rust cannot easily wrap the
interrupt-disabling locks in a safe API, which complicates the design.
To resolve this, introduce a new set of interrupt disabling APIs:
* local_interrupt_disable();
* local_interrupt_enable();
They work like local_irq_save() and local_irq_restore() except that 1)
the outermost local_interrupt_disable() call saves the interrupt state
into a per-CPU variable, so that the outermost local_interrupt_enable()
can restore the state, and 2) a per-CPU counter is added to record the
nest level of these calls, so that interrupts are not accidentally
enabled inside the outermost critical section.
Also add the corresponding spin_lock primitives: spin_lock_irq_disable()
and spin_unlock_irq_enable(), as a result, code as follows:
spin_lock_irq_disable(l1);
spin_lock_irq_disable(l2);
spin_unlock_irq_enable(l1);
// Interrupts are still disabled.
spin_unlock_irq_enable(l2);
doesn't have the issue that interrupts are accidentally enabled.
This also makes the wrapper of interrupt-disabling locks on Rust easier
to design.
[boqun: Apply Peter's feedback and fix spell errors reported by Ingo]
[boqun: Address the duplicate spin_acquire() spotted by sashiko]
Co-developed-by: Lyude Paul <lyude@redhat.com>
Signed-off-by: Lyude Paul <lyude@redhat.com>
Signed-off-by: Boqun Feng <boqun@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20260804182657.87716-1-boqun@kernel.org
diff --git a/include/linux/interrupt_rc.h b/include/linux/interrupt_rc.h
new file mode 100644
index 000000000000..b9a7f05ecf42
--- /dev/null
+++ b/include/linux/interrupt_rc.h
@@ -0,0 +1,82 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __LINUX_INTERRUPT_RC_H
+#define __LINUX_INTERRUPT_RC_H
+
+/*
+ * include/linux/interrupt_rc.h - refcounted local processor interrupt
+ * management.
+ *
+ * Since the implementation of this API currently depends on
+ * local_irq_save()/local_irq_restore(), we split this into its own header to
+ * make it easier to include without hitting circular header dependencies.
+ */
+
+#include <linux/irqflags.h>
+#include <linux/preempt.h>
+#include <linux/processor.h>
+#include <linux/smp.h>
+
+#ifndef MODULE
+/* Per-CPU interrupt disabling state for local_interrupt_{disable,enable}(). */
+DECLARE_PER_CPU(unsigned long, local_interrupt_disable_state);
+
+static __always_inline void __local_interrupt_disable(void)
+{
+ unsigned long flags;
+
+ local_irq_save(flags);
+ raw_cpu_write(local_interrupt_disable_state, flags);
+}
+
+static __always_inline void __local_interrupt_enable(void)
+{
+ unsigned long flags = raw_cpu_read(local_interrupt_disable_state);
+
+ local_irq_restore(flags);
+}
+
+#ifndef INSTANTIATE_EXPORTED_INTERRUPT_DISABLE
+static __always_inline void _local_interrupt_disable(void)
+{
+ __local_interrupt_disable();
+}
+
+static __always_inline void _local_interrupt_enable(void)
+{
+ __local_interrupt_enable();
+}
+#else
+extern void _local_interrupt_disable(void);
+extern void _local_interrupt_enable(void);
+#endif
+
+#else /* !MODULE */
+extern void _local_interrupt_disable(void);
+extern void _local_interrupt_enable(void);
+#endif /* !MODULE */
+
+static inline void local_interrupt_disable(void)
+{
+ int new_count;
+
+ WARN_ON_ONCE(in_nmi());
+
+ new_count = hardirq_disable_enter();
+
+ /* Interrupts can happen here, but it's OK, see __irq_exit_rcu(). */
+
+ if ((new_count & HARDIRQ_DISABLE_MASK) == HARDIRQ_DISABLE_OFFSET)
+ _local_interrupt_disable();
+}
+
+static inline void local_interrupt_enable(void)
+{
+ int new_count;
+
+ new_count = hardirq_disable_exit();
+
+ if ((new_count & HARDIRQ_DISABLE_MASK) == 0)
+ _local_interrupt_enable();
+}
+
+#endif /* !__LINUX_INTERRUPT_RC_H */
diff --git a/include/linux/preempt.h b/include/linux/preempt.h
index e2d3079d3f5f..33fc4c814a9f 100644
--- a/include/linux/preempt.h
+++ b/include/linux/preempt.h
@@ -151,6 +151,10 @@ static __always_inline unsigned char interrupt_context_level(void)
#define in_softirq() (softirq_count())
#define in_interrupt() (irq_count())
+#define hardirq_disable_count() ((preempt_count() & HARDIRQ_DISABLE_MASK) >> HARDIRQ_DISABLE_SHIFT)
+#define hardirq_disable_enter() __preempt_count_add_return(HARDIRQ_DISABLE_OFFSET)
+#define hardirq_disable_exit() __preempt_count_sub_return(HARDIRQ_DISABLE_OFFSET)
+
/*
* The preempt_count offset after preempt_disable();
*/
diff --git a/include/linux/spinlock.h b/include/linux/spinlock.h
index 241277cd34cf..3d405cc4c121 100644
--- a/include/linux/spinlock.h
+++ b/include/linux/spinlock.h
@@ -57,6 +57,7 @@
#include <linux/linkage.h>
#include <linux/compiler.h>
#include <linux/irqflags.h>
+#include <linux/interrupt_rc.h>
#include <linux/thread_info.h>
#include <linux/stringify.h>
#include <linux/bottom_half.h>
@@ -273,9 +274,11 @@ static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock)
#endif
#define raw_spin_lock_irq(lock) _raw_spin_lock_irq(lock)
+#define raw_spin_lock_irq_disable(lock) _raw_spin_lock_irq_disable(lock)
#define raw_spin_lock_bh(lock) _raw_spin_lock_bh(lock)
#define raw_spin_unlock(lock) _raw_spin_unlock(lock)
#define raw_spin_unlock_irq(lock) _raw_spin_unlock_irq(lock)
+#define raw_spin_unlock_irq_enable(lock) _raw_spin_unlock_irq_enable(lock)
#define raw_spin_unlock_irqrestore(lock, flags) \
do { \
@@ -290,6 +293,8 @@ static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock)
#define raw_spin_trylock_irqsave(lock, flags) _raw_spin_trylock_irqsave(lock, &(flags))
+#define raw_spin_trylock_irq_disable(lock) _raw_spin_trylock_irq_disable(lock)
+
#ifndef CONFIG_PREEMPT_RT
/* Include rwlock functions for !RT */
#include <linux/rwlock.h>
@@ -372,6 +377,12 @@ static __always_inline void spin_lock_irq(spinlock_t *lock)
raw_spin_lock_irq(&lock->rlock);
}
+static __always_inline void spin_lock_irq_disable(spinlock_t *lock)
+ __acquires(lock) __no_context_analysis
+{
+ raw_spin_lock_irq_disable(&lock->rlock);
+}
+
#define spin_lock_irqsave(lock, flags) \
do { \
raw_spin_lock_irqsave(spinlock_check(lock), flags); \
@@ -402,6 +413,12 @@ static __always_inline void spin_unlock_irq(spinlock_t *lock)
raw_spin_unlock_irq(&lock->rlock);
}
+static __always_inline void spin_unlock_irq_enable(spinlock_t *lock)
+ __releases(lock) __no_context_analysis
+{
+ raw_spin_unlock_irq_enable(&lock->rlock);
+}
+
static __always_inline void spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags)
__releases(lock) __no_context_analysis
{
@@ -427,6 +444,12 @@ static __always_inline bool _spin_trylock_irqsave(spinlock_t *lock, unsigned lon
}
#define spin_trylock_irqsave(lock, flags) _spin_trylock_irqsave(lock, &(flags))
+static __always_inline int spin_trylock_irq_disable(spinlock_t *lock)
+ __cond_acquires(true, lock) __no_context_analysis
+{
+ return raw_spin_trylock_irq_disable(&lock->rlock);
+}
+
/**
* spin_is_locked() - Check whether a spinlock is locked.
* @lock: Pointer to the spinlock.
diff --git a/include/linux/spinlock_api_smp.h b/include/linux/spinlock_api_smp.h
index bda5e7a390cd..90909d933dab 100644
--- a/include/linux/spinlock_api_smp.h
+++ b/include/linux/spinlock_api_smp.h
@@ -28,6 +28,8 @@ _raw_spin_lock_nest_lock(raw_spinlock_t *lock, struct lockdep_map *map)
void __lockfunc _raw_spin_lock_bh(raw_spinlock_t *lock) __acquires(lock);
void __lockfunc _raw_spin_lock_irq(raw_spinlock_t *lock)
__acquires(lock);
+void __lockfunc _raw_spin_lock_irq_disable(raw_spinlock_t *lock)
+ __acquires(lock);
unsigned long __lockfunc _raw_spin_lock_irqsave(raw_spinlock_t *lock)
__acquires(lock);
@@ -39,6 +41,7 @@ int __lockfunc _raw_spin_trylock_bh(raw_spinlock_t *lock) __cond_acquires(true,
void __lockfunc _raw_spin_unlock(raw_spinlock_t *lock) __releases(lock);
void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock) __releases(lock);
void __lockfunc _raw_spin_unlock_irq(raw_spinlock_t *lock) __releases(lock);
+void __lockfunc _raw_spin_unlock_irq_enable(raw_spinlock_t *lock) __releases(lock);
void __lockfunc
_raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsigned long flags)
__releases(lock);
@@ -55,6 +58,11 @@ _raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsigned long flags)
#define _raw_spin_lock_irq(lock) __raw_spin_lock_irq(lock)
#endif
+/* Use the same config as spin_lock_irq() temporarily. */
+#ifdef CONFIG_INLINE_SPIN_LOCK_IRQ
+#define _raw_spin_lock_irq_disable(lock) __raw_spin_lock_irq_disable(lock)
+#endif
+
#ifdef CONFIG_INLINE_SPIN_LOCK_IRQSAVE
#define _raw_spin_lock_irqsave(lock) __raw_spin_lock_irqsave(lock)
#endif
@@ -79,6 +87,11 @@ _raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsigned long flags)
#define _raw_spin_unlock_irq(lock) __raw_spin_unlock_irq(lock)
#endif
+/* Use the same config as spin_unlock_irq() temporarily. */
+#ifdef CONFIG_INLINE_SPIN_UNLOCK_IRQ
+#define _raw_spin_unlock_irq_enable(lock) __raw_spin_unlock_irq_enable(lock)
+#endif
+
#ifdef CONFIG_INLINE_SPIN_UNLOCK_IRQRESTORE
#define _raw_spin_unlock_irqrestore(lock, flags) __raw_spin_unlock_irqrestore(lock, flags)
#endif
@@ -105,6 +118,16 @@ static __always_inline bool _raw_spin_trylock_irq(raw_spinlock_t *lock)
return false;
}
+static __always_inline bool _raw_spin_trylock_irq_disable(raw_spinlock_t *lock)
+ __cond_acquires(true, lock)
+{
+ local_interrupt_disable();
+ if (_raw_spin_trylock(lock))
+ return true;
+ local_interrupt_enable();
+ return false;
+}
+
static __always_inline bool _raw_spin_trylock_irqsave(raw_spinlock_t *lock, unsigned long *flags)
__cond_acquires(true, lock)
{
@@ -143,6 +166,15 @@ static inline void __raw_spin_lock_irq(raw_spinlock_t *lock)
LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
}
+static inline void __raw_spin_lock_irq_disable(raw_spinlock_t *lock)
+ __acquires(lock) __no_context_analysis
+{
+ local_interrupt_disable();
+ preempt_disable();
+ spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
+ LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
+}
+
static inline void __raw_spin_lock_bh(raw_spinlock_t *lock)
__acquires(lock) __no_context_analysis
{
@@ -188,6 +220,15 @@ static inline void __raw_spin_unlock_irq(raw_spinlock_t *lock)
preempt_enable();
}
+static inline void __raw_spin_unlock_irq_enable(raw_spinlock_t *lock)
+ __releases(lock)
+{
+ spin_release(&lock->dep_map, _RET_IP_);
+ do_raw_spin_unlock(lock);
+ local_interrupt_enable();
+ preempt_enable();
+}
+
static inline void __raw_spin_unlock_bh(raw_spinlock_t *lock)
__releases(lock)
{
diff --git a/include/linux/spinlock_api_up.h b/include/linux/spinlock_api_up.h
index a9d5c7c66e03..d03d3065ee04 100644
--- a/include/linux/spinlock_api_up.h
+++ b/include/linux/spinlock_api_up.h
@@ -42,6 +42,9 @@
#define __LOCK_IRQSAVE(lock, flags, ...) \
do { local_irq_save(flags); __LOCK(lock, ##__VA_ARGS__); } while (0)
+#define __LOCK_IRQ_DISABLE(lock, ...) \
+ do { local_interrupt_disable(); __LOCK(lock, ##__VA_ARGS__); } while (0)
+
#define ___UNLOCK_(lock) \
do { __release(lock); (void)(lock); } while (0)
@@ -61,6 +64,9 @@
#define __UNLOCK_IRQRESTORE(lock, flags, ...) \
do { local_irq_restore(flags); __UNLOCK(lock, ##__VA_ARGS__); } while (0)
+#define __UNLOCK_IRQ_ENABLE(lock, ...) \
+ do { __UNLOCK(lock, ##__VA_ARGS__); local_interrupt_enable(); } while (0)
+
#define _raw_spin_lock(lock) __LOCK(lock)
#define _raw_spin_lock_nested(lock, subclass) __LOCK(lock)
#define _raw_read_lock(lock) __LOCK(lock, shared)
@@ -70,6 +76,7 @@
#define _raw_read_lock_bh(lock) __LOCK_BH(lock, shared)
#define _raw_write_lock_bh(lock) __LOCK_BH(lock)
#define _raw_spin_lock_irq(lock) __LOCK_IRQ(lock)
+#define _raw_spin_lock_irq_disable(lock) __LOCK_IRQ_DISABLE(lock)
#define _raw_read_lock_irq(lock) __LOCK_IRQ(lock, shared)
#define _raw_write_lock_irq(lock) __LOCK_IRQ(lock)
#define _raw_spin_lock_irqsave(lock, flags) __LOCK_IRQSAVE(lock, flags)
@@ -97,6 +104,13 @@ static __always_inline int _raw_spin_trylock_irq(raw_spinlock_t *lock)
return 1;
}
+static __always_inline int _raw_spin_trylock_irq_disable(raw_spinlock_t *lock)
+ __cond_acquires(true, lock)
+{
+ __LOCK_IRQ_DISABLE(lock);
+ return 1;
+}
+
static __always_inline int _raw_spin_trylock_irqsave(raw_spinlock_t *lock, unsigned long *flags)
__cond_acquires(true, lock)
{
@@ -132,6 +146,7 @@ static __always_inline int _raw_write_trylock_irqsave(rwlock_t *lock, unsigned l
#define _raw_write_unlock_bh(lock) __UNLOCK_BH(lock)
#define _raw_read_unlock_bh(lock) __UNLOCK_BH(lock, shared)
#define _raw_spin_unlock_irq(lock) __UNLOCK_IRQ(lock)
+#define _raw_spin_unlock_irq_enable(lock) __UNLOCK_IRQ_ENABLE(lock)
#define _raw_read_unlock_irq(lock) __UNLOCK_IRQ(lock, shared)
#define _raw_write_unlock_irq(lock) __UNLOCK_IRQ(lock)
#define _raw_spin_unlock_irqrestore(lock, flags) \
diff --git a/include/linux/spinlock_rt.h b/include/linux/spinlock_rt.h
index 373618a4243c..560d06384e0c 100644
--- a/include/linux/spinlock_rt.h
+++ b/include/linux/spinlock_rt.h
@@ -96,6 +96,12 @@ static __always_inline void spin_lock_irq(spinlock_t *lock)
rt_spin_lock(lock);
}
+static __always_inline void spin_lock_irq_disable(spinlock_t *lock)
+ __acquires(lock)
+{
+ rt_spin_lock(lock);
+}
+
#define spin_lock_irqsave(lock, flags) \
do { \
typecheck(unsigned long, flags); \
@@ -122,6 +128,12 @@ static __always_inline void spin_unlock_irq(spinlock_t *lock)
rt_spin_unlock(lock);
}
+static __always_inline void spin_unlock_irq_enable(spinlock_t *lock)
+ __releases(lock)
+{
+ rt_spin_unlock(lock);
+}
+
static __always_inline void spin_unlock_irqrestore(spinlock_t *lock,
unsigned long flags)
__releases(lock)
@@ -131,6 +143,12 @@ static __always_inline void spin_unlock_irqrestore(spinlock_t *lock,
#define spin_trylock(lock) rt_spin_trylock(lock)
+static __always_inline int spin_trylock_irq_disable(spinlock_t *lock)
+ __cond_acquires(true, lock)
+{
+ return rt_spin_trylock(lock);
+}
+
#define spin_trylock_bh(lock) rt_spin_trylock_bh(lock)
#define spin_trylock_irq(lock) rt_spin_trylock(lock)
diff --git a/kernel/locking/spinlock.c b/kernel/locking/spinlock.c
index b42d293da38b..83a17eaf5717 100644
--- a/kernel/locking/spinlock.c
+++ b/kernel/locking/spinlock.c
@@ -129,6 +129,21 @@ static void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \
*/
BUILD_LOCK_OPS(spin, raw_spinlock, __acquires);
+/* No rwlock_t variants for now, so just build this function by hand */
+static void __lockfunc __raw_spin_lock_irq_disable(raw_spinlock_t *lock)
+{
+ for (;;) {
+ preempt_disable();
+ local_interrupt_disable();
+ if (likely(do_raw_spin_trylock(lock)))
+ break;
+ local_interrupt_enable();
+ preempt_enable();
+
+ arch_spin_relax(&lock->raw_lock);
+ }
+}
+
#ifndef CONFIG_PREEMPT_RT
BUILD_LOCK_OPS(read, rwlock, __acquires_shared);
BUILD_LOCK_OPS(write, rwlock, __acquires);
@@ -176,6 +191,14 @@ noinline void __lockfunc _raw_spin_lock_irq(raw_spinlock_t *lock)
EXPORT_SYMBOL(_raw_spin_lock_irq);
#endif
+#ifndef CONFIG_INLINE_SPIN_LOCK_IRQ
+noinline void __lockfunc _raw_spin_lock_irq_disable(raw_spinlock_t *lock)
+{
+ __raw_spin_lock_irq_disable(lock);
+}
+EXPORT_SYMBOL_GPL(_raw_spin_lock_irq_disable);
+#endif
+
#ifndef CONFIG_INLINE_SPIN_LOCK_BH
noinline void __lockfunc _raw_spin_lock_bh(raw_spinlock_t *lock)
{
@@ -208,6 +231,14 @@ noinline void __lockfunc _raw_spin_unlock_irq(raw_spinlock_t *lock)
EXPORT_SYMBOL(_raw_spin_unlock_irq);
#endif
+#ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQ
+noinline void __lockfunc _raw_spin_unlock_irq_enable(raw_spinlock_t *lock)
+{
+ __raw_spin_unlock_irq_enable(lock);
+}
+EXPORT_SYMBOL_GPL(_raw_spin_unlock_irq_enable);
+#endif
+
#ifndef CONFIG_INLINE_SPIN_UNLOCK_BH
noinline void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock)
{
diff --git a/kernel/softirq.c b/kernel/softirq.c
index 10af5ed859e7..0c9b2269a8d6 100644
--- a/kernel/softirq.c
+++ b/kernel/softirq.c
@@ -9,6 +9,7 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+#define INSTANTIATE_EXPORTED_INTERRUPT_DISABLE
#include <linux/export.h>
#include <linux/kernel_stat.h>
#include <linux/interrupt.h>
@@ -88,6 +89,20 @@ EXPORT_PER_CPU_SYMBOL_GPL(hardirqs_enabled);
EXPORT_PER_CPU_SYMBOL_GPL(hardirq_context);
#endif
+DEFINE_PER_CPU(unsigned long, local_interrupt_disable_state);
+
+void _local_interrupt_disable(void)
+{
+ __local_interrupt_disable();
+}
+EXPORT_SYMBOL(_local_interrupt_disable);
+
+void _local_interrupt_enable(void)
+{
+ __local_interrupt_enable();
+}
+EXPORT_SYMBOL(_local_interrupt_enable);
+
DEFINE_PER_CPU(unsigned int, nmi_nesting);
/*
@@ -728,10 +743,19 @@ static inline void __irq_exit_rcu(void)
#endif
account_hardirq_exit(current);
preempt_count_sub(HARDIRQ_OFFSET);
- if (!in_interrupt() && local_softirq_pending()) {
+ /*
+ * Interrupts may happen between hardirq_disable_enter() and
+ * local_irq_save() in local_interrupt_disable(), if irq_exit() invokes
+ * softirq here, we may have a softirq handler calling
+ * local_interrupt_disable() but it won't disable the IRQ because
+ * hardirq disabling count is already 1, hence we need to prevent
+ * invoking softirq when a local_interrupt_disable() is ongoing.
+ */
+ if (!in_interrupt() && !hardirq_disable_count() &&
+ local_softirq_pending()) {
/*
* If we left hrtimers unarmed, make sure to arm them now,
- * before enabling interrupts to run SoftIRQ.
+ * before enabling interrupts to run softirq.
*/
hrtimer_rearm_deferred();
invoke_softirq();
]
|
| 64/2 |
2026/11/19 02:26 |
tool |
read-file |
0m
Args: map[File:include/linux/interrupt_rc.h FirstLine:1 LineCount:100]
Results: map[Contents: 1: /* SPDX-License-Identifier: GPL-2.0 */
2: #ifndef __LINUX_INTERRUPT_RC_H
3: #define __LINUX_INTERRUPT_RC_H
4:
5: /*
6: * include/linux/interrupt_rc.h - refcounted local processor interrupt
7: * management.
8: *
9: * Since the implementation of this API currently depends on
10: * local_irq_save()/local_irq_restore(), we split this into its own header to
11: * make it easier to include without hitting circular header dependencies.
12: */
13:
14: #include <linux/debug_locks.h>
15: #include <linux/irqflags.h>
16: #include <linux/preempt.h>
17: #include <linux/processor.h>
18: #include <linux/smp.h>
19:
20: #ifndef MODULE
21: /* Per-CPU interrupt disabling state for local_interrupt_{disable,enable}(). */
22: DECLARE_PER_CPU(unsigned long, local_interrupt_disable_state);
23:
24: static __always_inline void __local_interrupt_save_state(unsigned long flags)
25: {
26: raw_cpu_write(local_interrupt_disable_state, flags);
27: }
28:
29: static __always_inline void __local_interrupt_enable(void)
30: {
31: unsigned long flags = raw_cpu_read(local_interrupt_disable_state);
32:
33: local_irq_restore(flags);
34: }
35:
36: #ifndef INSTANTIATE_EXPORTED_INTERRUPT_DISABLE
37: static __always_inline void _local_interrupt_save_state(unsigned long flags)
38: {
39: __local_interrupt_save_state(flags);
40: }
41:
42: static __always_inline void _local_interrupt_enable(void)
43: {
44: __local_interrupt_enable();
45: }
46: #else
47: extern void _local_interrupt_save_state(unsigned long flags);
48: extern void _local_interrupt_enable(void);
49: #endif
50:
51: #else /* !MODULE */
52: extern void _local_interrupt_save_state(unsigned long flags);
53: extern void _local_interrupt_enable(void);
54: #endif /* !MODULE */
55:
56: #define hardirq_disable_enter() __preempt_count_add_return(HARDIRQ_DISABLE_OFFSET)
57: #define hardirq_disable_exit() __preempt_count_sub_return(HARDIRQ_DISABLE_OFFSET)
58:
59: static inline void local_interrupt_disable(void)
60: {
61: int new_count;
62: unsigned long flags;
63:
64: WARN_ON_ONCE(in_nmi());
65:
66: local_irq_save(flags);
67: new_count = hardirq_disable_enter();
68:
69: /* Is hardirq disable count overflow soon? */
70: if (IS_ENABLED(CONFIG_DEBUG_PREEMPT) &&
71: DEBUG_LOCKS_WARN_ON(((new_count & HARDIRQ_DISABLE_MASK) +
72: (10 << HARDIRQ_DISABLE_SHIFT)) >=
73: HARDIRQ_DISABLE_MASK))
74: return;
75:
76: if ((new_count & HARDIRQ_DISABLE_MASK) == HARDIRQ_DISABLE_OFFSET)
77: _local_interrupt_save_state(flags);
78: }
79:
80: static inline void local_interrupt_enable(void)
81: {
82: int new_count;
83:
84: /* Unpaired local_interrupt_enable()? Warn and abort. */
85: if (IS_ENABLED(CONFIG_DEBUG_PREEMPT) &&
86: DEBUG_LOCKS_WARN_ON((preempt_count() & HARDIRQ_DISABLE_MASK) == 0))
87: return;
88:
89: new_count = hardirq_disable_exit();
90:
91: if ((new_count & HARDIRQ_DISABLE_MASK) == 0)
92: _local_interrupt_enable();
93: }
94:
95: #endif /* !__LINUX_INTERRUPT_RC_H */
]
|
| 65/2 |
2026/11/19 02:26 |
tool |
git-show |
0m
Args:
Results: map[Output:commit feb5c9a2447a7d65a17d949c81c1f21864cde172
Author: Karl Mehltretter <kmehltretter@gmail.com>
Date: Sun Oct 11 08:07:42 2026 +0200
softirq: Preserve interrupt context on return from interrupt
__irq_exit_rcu() removes HARDIRQ_OFFSET from preempt_count() before the
interrupt exit work. On the outermost interrupt exit, with no NMI or
softirq state remaining, this makes context checks report task context. The
softirq-dispatch, timer-wakeup and tick-exit checks account for the early
decrement, but other code reached in this window sees the wrong context:
- ftrace, perf and the ring buffer record task context and use the
task recursion and context slots.
- KCSAN attributes accesses to the interrupted task. KMSAN uses and
changes the interrupted task's KMSAN context. KCOV and the printk
caller id see a task.
- On PREEMPT_RT can_spin_trylock() and local_trylock() reject hard
interrupt context to avoid interfering with PI when the interrupted
task is blocked on a lock. Those checks do not reject calls made in
this window. BPF programs attached to sched_waking or sched_wakeup
can reach the trylock path through kmalloc_nolock().
- An oops kills the interrupted task instead of ending in "Fatal
exception in interrupt".
The early decrement predates git. No failure caused by this
misclassification is known. lockdep is unaffected because
lockdep_hardirq_exit() is the last operation in irq_exit().
Keep HARDIRQ_OFFSET until the end of __irq_exit_rcu(), including
tick_irq_exit(). Softirq handlers must run in BH context, so replace
HARDIRQ_OFFSET with SOFTIRQ_OFFSET while they run and restore it
afterwards. Use one raw counter update for each transition to preserve
the preemption disable location recorded by irq_enter_rcu(), and update
lockdep explicitly. Detect IRQ exit with in_hardirq() in
softirq_handle_begin() because __do_softirq() can be reached through
do_softirq_own_stack() and cannot take an argument.
Adjust the exit checks for HARDIRQ_OFFSET still being present. Use
irq_count() == HARDIRQ_OFFSET for softirq dispatch, as in irq_enter_rcu().
Use hardirq_count() == HARDIRQ_OFFSET for the timer thread wakeup and
tick_irq_exit(). NMI exits use a separate path. An IRQ nested in an NMI
has an extra HARDIRQ_OFFSET from NMI entry, so it is excluded too. The
timer thread check must ignore softirq_count() to allow wakeups when an
IRQ interrupts softirq processing or a BH-disabled section. The timer
thread handles all pending softirqs, including any raised during its
wakeup.
A softirq raised from a tracepoint on the final preempt_count_sub()
waits for the next interrupt exit and can trigger NOHZ tick-stop
warnings. Handling that instrumentation case does not justify a check
on every interrupt exit. A tracepoint on tick_irq_exit() can also raise
a softirq after the normal softirq handling has finished.
The number of counter updates and interrupt time accounting are
unchanged. The preemptoff tracer now reports the interrupt and subsequent
softirq processing as one section. With nofuncgraph-irqs, function graph
tracing also skips the IRQ exit work, including the __do_softirq() frame.
Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Assisted-by: LLM
Reviewed-by: Bradley Morgan <brads@mainlining.org>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Link: https://lore.kernel.org/r/20260813130826.GW687043@noisy.programming.kicks-ass.net
Link: https://patch.msgid.link/20261011060742.30584-1-kmehltretter@gmail.com
diff --git a/Documentation/core-api/entry.rst b/Documentation/core-api/entry.rst
index 79fdaed954d9..09942135aad1 100644
--- a/Documentation/core-api/entry.rst
+++ b/Documentation/core-api/entry.rst
@@ -197,8 +197,9 @@ return true, handles NOHZ tick state and interrupt time accounting. This
means that up to the point where irq_enter_rcu() is invoked in_hardirq()
returns false.
-irq_exit_rcu() handles interrupt time accounting, undoes the preemption
-count update and eventually handles soft interrupts and NOHZ tick state.
+irq_exit_rcu() handles interrupt time accounting, handles soft interrupts if
+possible, handles the NOHZ tick state and finally undoes the preemption count
+update.
In theory, the preemption count could be updated in irqentry_enter(). In
practice, deferring this update to irq_enter_rcu() allows the preemption-count
@@ -207,10 +208,13 @@ irqentry_exit(), which are described in the next paragraph. The only downside
is that the early entry code up to irq_enter_rcu() must be aware that the
preemption count has not yet been updated with the HARDIRQ_OFFSET state.
-Note that irq_exit_rcu() must remove HARDIRQ_OFFSET from the preemption count
-before it handles soft interrupts, whose handlers must run in BH context rather
-than irq-disabled context. In addition, irqentry_exit() might schedule, which
-also requires that HARDIRQ_OFFSET has been removed from the preemption count.
+Note that soft interrupt handlers must run in BH context rather than in hard
+interrupt context. irq_exit_rcu() therefore replaces HARDIRQ_OFFSET with
+SOFTIRQ_OFFSET in the preemption count while it handles soft interrupts and
+puts HARDIRQ_OFFSET back afterwards, so that the remaining interrupt exit work
+is still attributed to the interrupt. HARDIRQ_OFFSET is removed before
+irq_exit_rcu() returns because irqentry_exit() might schedule, which requires
+that HARDIRQ_OFFSET has been removed from the preemption count.
Even though interrupt handlers are expected to run with local interrupts
disabled, interrupt nesting is common from an entry/exit perspective. For
diff --git a/kernel/softirq.c b/kernel/softirq.c
index c3729c5b284b..7fc2200392cd 100644
--- a/kernel/softirq.c
+++ b/kernel/softirq.c
@@ -350,8 +350,8 @@ static inline void ksoftirqd_run_end(void)
local_irq_enable();
}
-static inline void softirq_handle_begin(void) { }
-static inline void softirq_handle_end(void) { }
+static inline bool softirq_handle_begin(void) { return false; }
+static inline void softirq_handle_end(bool from_irq_exit) { }
static inline bool should_wake_ksoftirqd(void)
{
@@ -481,15 +481,38 @@ void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
}
EXPORT_SYMBOL(__local_bh_enable_ip);
-static inline void softirq_handle_begin(void)
+static inline bool softirq_handle_begin(void)
{
- __local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);
+ if (!in_hardirq()) {
+ __local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);
+ return false;
+ }
+
+ /*
+ * Only reached from irq_exit(), with HARDIRQ_OFFSET still set.
+ * Replace it with SOFTIRQ_OFFSET before handle_softirqs() enables
+ * interrupts. Use the raw operation to preserve the preemption
+ * disable location recorded by irq_enter_rcu(), and update lockdep
+ * directly.
+ */
+ __preempt_count_sub(HARDIRQ_OFFSET - SOFTIRQ_OFFSET);
+ lockdep_softirqs_off(_RET_IP_);
+ WARN_ON_ONCE(irq_count() != SOFTIRQ_OFFSET);
+
+ return true;
}
-static inline void softirq_handle_end(void)
+static inline void softirq_handle_end(bool from_irq_exit)
{
- __local_bh_enable(SOFTIRQ_OFFSET);
- WARN_ON_ONCE(in_interrupt());
+ if (!from_irq_exit) {
+ __local_bh_enable(SOFTIRQ_OFFSET);
+ WARN_ON_ONCE(in_interrupt());
+ return;
+ }
+
+ lockdep_softirqs_on(_RET_IP_);
+ __preempt_count_add(HARDIRQ_OFFSET - SOFTIRQ_OFFSET);
+ WARN_ON_ONCE(irq_count() != HARDIRQ_OFFSET);
}
static inline void ksoftirqd_run_begin(void)
@@ -605,6 +628,7 @@ static void handle_softirqs(bool ksirqd)
unsigned long old_flags = current->flags;
int max_restart = MAX_SOFTIRQ_RESTART;
struct softirq_action *h;
+ bool from_irq_exit;
bool in_hardirq;
__u32 pending;
int softirq_bit;
@@ -618,7 +642,7 @@ static void handle_softirqs(bool ksirqd)
pending = local_softirq_pending();
- softirq_handle_begin();
+ from_irq_exit = softirq_handle_begin();
in_hardirq = lockdep_softirq_start();
account_softirq_enter(current);
@@ -670,7 +694,7 @@ static void handle_softirqs(bool ksirqd)
account_softirq_exit(current);
lockdep_softirq_end(in_hardirq);
- softirq_handle_end();
+ softirq_handle_end(from_irq_exit);
current_restore_flags(old_flags, PF_MEMALLOC);
}
@@ -716,7 +740,7 @@ static inline void tick_irq_exit(void)
/* Make sure that timer wheel updates are propagated */
if ((sched_core_idle_cpu(cpu) && !need_resched()) || tick_nohz_full_cpu(cpu)) {
- if (!in_hardirq())
+ if (hardirq_count() == HARDIRQ_OFFSET)
tick_nohz_irq_exit();
}
#endif
@@ -748,8 +772,12 @@ static inline void __irq_exit_rcu(void)
lockdep_assert_irqs_disabled();
#endif
account_hardirq_exit(current);
- preempt_count_sub(HARDIRQ_OFFSET);
- if (!in_interrupt() && local_softirq_pending()) {
+
+ /*
+ * HARDIRQ_OFFSET is still set. Only the outermost interrupt handles
+ * softirqs, and only if it did not hit a softirq or BH disabled section.
+ */
+ if (irq_count() == HARDIRQ_OFFSET && local_softirq_pending()) {
/*
* If we left hrtimers unarmed, make sure to arm them now,
* before enabling interrupts to run softirq.
@@ -758,11 +786,17 @@ static inline void __irq_exit_rcu(void)
invoke_softirq();
}
+ /*
+ * Wake the timer thread even if the interrupt hit a softirq or a
+ * section with BHs disabled. Only the outermost interrupt can wake it.
+ */
if (IS_ENABLED(CONFIG_IRQ_FORCED_THREADING) && force_irqthreads() &&
- local_timers_pending_force_th() && !(in_nmi() | in_hardirq()))
+ local_timers_pending_force_th() &&
+ hardirq_count() == HARDIRQ_OFFSET)
wake_timersd();
tick_irq_exit();
+ preempt_count_sub(HARDIRQ_OFFSET);
}
/**
]
|
| 66/2 |
2026/11/19 02:26 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 92215
output: 166
thoughts: 2081
Thoughts: **My Detailed Analysis of the Workqueue Leak Bug**
I've been digging into this `BUG: workqueue leaked atomic, lock or RCU` report, and I think I've pieced together the sequence of events that's causing it. It seems to stem from a subtle interaction between lockdep's state tracking and how it's being used within the workqueue processing.
Let's break it down. First, I'm observing that the specific bug is triggered when `DEBUG_LOCKS_WARN_ON` is in effect, which means `WARN()` is being called. This taints the kernel, and syzkaller, as expected, flags this. I also noted that the problematic code path (`9e174422f6f1`) uses `DEBUG_LOCKS_WARN_ON`, while another commit (`feb5c9a2447a`) is conditional on `!CONFIG_PREEMPT_RT`. The `CONFIG_PREEMPT_RT` stubs in `kernel/softirq.c` (lines 350-354) seem to sidestep this particular issue.
Now, I need to be extremely rigorous and check every single fact.
1. **The Crash Report:** The report clearly states:
BUG: workqueue leaked atomic, lock or RCU: syz-executor[6626]
preempt=0x00000000 lock=4->0 RCU=2->2 workfn=usb_giveback_urb_bh
INFO: lockdep is turned off.
CPU: 0 UID: 0 PID: 6626 Comm: syz-executor Not tainted syzkaller #0 PREEMPT_{RT,(full)}
...
Call Trace:
<TASK>
dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120
process_one_work kernel/workqueue.c:3516 [inline]
process_scheduled_works+0x12c2/0x1630 kernel/workqueue.c:3578
bh_worker+0x4b3/0xbb0 kernel/workqueue.c:3885
tasklet_hi_action+0xf/0x70 kernel/softirq.c:1028
handle_softirqs+0x1d4/0x6c0 kernel/softirq.c:669
__do_softirq kernel/softirq.c:703 [inline]
__local_bh_enable_ip+0x16f/0x2b0 kernel/softirq.c:325
local_bh_enable include/linux/bottom_half.h:33 [inline]
__alloc_skb+0x1b1/0x7a0 net/core/skbuff.c:700
...
The key pieces of information here are the `BUG: workqueue leaked atomic, lock or RCU` message, the `lock=4->0` state, and crucially, `INFO: lockdep is turned off.`
2. **Why the BUG Triggered:** I looked into the `process_one_work()` function in `kernel/workqueue.c`. The relevant section is:
rcu_start_depth = rcu_preempt_depth();
lockdep_start_depth = lockdep_depth(current);
// ... lock acquisition ...
worker->current_func(work);
// ... lock release ...
if (unlikely((worker->task && in_atomic()) ||
lockdep_depth(current) != lockdep_start_depth ||
rcu_preempt_depth() != rcu_start_depth)) {
pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n"
" preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n",
current->comm, task_pid_nr(current), preempt_count(),
lockdep_start_depth, lockdep_depth(current),
rcu_start_depth, rcu_preempt_depth(),
worker->current_func);
debug_show_held_locks(current);
dump_stack();
}
The condition `lockdep_depth(current) != lockdep_start_depth` is what's causing the `BUG` message to fire. The `lockdep_depth(tsk)` macro is defined as `(debug_locks ? (tsk)->lockdep_depth : 0)`.
3. **The Core Problem:** Here's where it gets tricky. I hypothesize the following scenario:
* **(a) Initial State:** `process_one_work()` is called, and `current` *already* holds some locks. This means `lockdep_start_depth` is greater than 0. In the crash report, it's `4`. This is plausible because the call trace shows `bh_worker` eventually calling `tasklet_hi_action` which might have acquired locks earlier (e.g., `rtnl_mutex`, `rcu_read_lock`, `softirq_ctrl.lock`).
* **(b) `debug_locks` is Turned Off:** While `process_one_work()` is executing, specifically *before* the final check, `debug_locks` gets set to `0`. This could happen if, for example, the work being processed (`worker->current_func(work)`) triggers a situation that disables lockdep temporarily (e.g., exceeding some lockdep limits, which syzkaller might ignore without tainting the kernel), or if another CPU is concurrently disabling it.
* **(c) The Mismatch:** When `process_one_work()` reaches the final `if` condition, it evaluates `lockdep_depth(current) != lockdep_start_depth`. Because `debug_locks` is now `0`, `lockdep_depth(current)` evaluates to `0`. Since `lockdep_start_depth` was `4` (from step a), the condition `0 != 4` becomes TRUE.
* **False Alarm:** Even though no actual locks, atomic operations, or RCU resources were leaked (as evidenced by `preempt=0x00000000` and `RCU=2->2`), the `lock=4->0` in the report now makes perfect sense: the tracked depth changed from `4` to `0` *because lockdep itself was disabled*. The `BUG` message is therefore misleadingly indicating a leak when, in reality, it's a lockdep tracking issue.
I've noticed that many other kernel assertions that rely on `debug_locks` explicitly guard against this by checking `debug_locks && ...`. For example, `WARN_ON_ONCE(debug_locks && p->lockdep_depth)` in `kernel/freezer.c`. In `process_one_work()`, the condition *should* ideally be `(debug_locks && lockdep_depth(current) != lockdep_start_depth)`. The crucial detail is that this *only* breaks when `debug_locks` transitions from `1` to `0` *between* the initial depth recording and the final depth check, *and* when `lockdep_start_depth` was greater than `0`. If `debug_locks` was `0` from the very beginning of `process_one_work()`, then `lockdep_start_depth` would be `0`, and the check `0 != 0` would be false, so no bug.
4. **Further Investigation Areas:** Now, I need to look deeper.
* I'm scrutinizing both commits (`c35aea39d1e10` and `4cb1ef64609f`) related to BH workqueues to understand their impact on `process_one_work()` and `process_scheduled_works()`.
* I'm also searching for any other changes in `kernel/workqueue.c` or `kernel/softirq.c` that might lead to `lockdep_start_depth` being greater than `0` for kworkers or BH workers, especially considering the `worker_lock_callback(pool)` call within `bh_worker` (`kernel/workqueue.c:3865`). I need to check `worker_lock_callback` and its git blame.
* Crucially, I need to determine if `lockdep_start_depth > 0` could have occurred *before* commit `4cb1ef64609f` ("workqueue: Implement BH workqueues to eventually replace tasklets"). To do this, I'll examine all callers of `process_one_work` and `process_scheduled_works` in `kernel/workqueue.c` from that period.
This is a very specific race condition involving the state of `debug_locks` and prior lock acquisitions. I believe understanding the interaction between `worker_lock_callback`, the new BH workqueue mechanism, and the existing lockdep checks is key to resolving this.
|
| 67/2 |
2026/11/19 02:27 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:kernel/workqueue.c IncludeSnippetLines:10 Name:process_one_work]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:process_scheduled_works SourceFile:kernel/workqueue.c SourceLine:3578 SourceSnippet:3568: {
3569: struct work_struct *work;
3570: bool first = true;
3571:
3572: while ((work = list_first_entry_or_null(&worker->scheduled,
3573: struct work_struct, entry))) {
3574: if (first) {
3575: worker->pool->last_progress_ts = jiffies;
3576: first = false;
3577: }
3578: process_one_work(worker, work);
3579: }
3580: }
]] TruncatedOutput:false]
|
| 68/2 |
2026/11/19 02:27 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:kernel/workqueue.c IncludeSnippetLines:10 Name:process_scheduled_works]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bh_worker SourceFile:kernel/workqueue.c SourceLine:3885 SourceSnippet:3875:
3876: WARN_ON_ONCE(!list_empty(&worker->scheduled));
3877: worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
3878:
3879: do {
3880: struct work_struct *work =
3881: list_first_entry(&pool->worklist,
3882: struct work_struct, entry);
3883:
3884: if (assign_work(work, worker, NULL))
3885: process_scheduled_works(worker);
3886:
3887: if (!keep_working(pool))
3888: break;
3889:
3890: if (!--nr_restarts) {
3891: budget_exhausted = true;
3892: break;
3893: }
3894:
3895: if (!time_before(jiffies, end)) {
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:rescuer_thread SourceFile:kernel/workqueue.c SourceLine:3803 SourceSnippet:3793:
3794: raw_spin_unlock_irq(&wq_mayday_lock);
3795:
3796: worker_attach_to_pool(rescuer, pool);
3797:
3798: raw_spin_lock_irq(&pool->lock);
3799:
3800: WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
3801:
3802: while (assign_rescuer_work(pwq, rescuer)) {
3803: process_scheduled_works(rescuer);
3804:
3805: /*
3806: * If the per-turn work item limit is reached and other
3807: * PWQs are in mayday, requeue mayday for this PWQ and
3808: * let the rescuer handle the other PWQs first.
3809: */
3810: if (++count > RESCUER_BATCH && !list_empty(&pwq->wq->maydays) &&
3811: pwq->nr_active && need_to_create_worker(pool)) {
3812: raw_spin_lock(&wq_mayday_lock);
3813: send_mayday(pwq);
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:worker_thread SourceFile:kernel/workqueue.c SourceLine:3659 SourceSnippet:3649: * after being rebound. See rebind_workers() for details.
3650: */
3651: worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
3652:
3653: do {
3654: struct work_struct *work =
3655: list_first_entry(&pool->worklist,
3656: struct work_struct, entry);
3657:
3658: if (assign_work(work, worker, NULL))
3659: process_scheduled_works(worker);
3660: } while (keep_working(pool));
3661:
3662: worker_set_flags(worker, WORKER_PREP);
3663: sleep:
3664: /*
3665: * pool->lock is held and there's no work to process and no need to
3666: * manage, sleep. Workers are woken up only while holding
3667: * pool->lock or from local cpu, so setting the current state
3668: * before releasing pool->lock is enough to prevent losing any
3669: * event.
]] TruncatedOutput:false]
|
| 69/2 |
2026/11/19 02:27 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:kernel/workqueue.c Name:worker_lock_callback]
Results: map[SourceCode:3297: static void worker_lock_callback(struct worker_pool *pool)
3298: {
3299: /*
3300: * SINGLE_DEPTH_NESTING is for a dead pool's bh_worker() running from
3301: * drain_dead_softirq_workfn() inside a live pool's bh_worker(). The
3302: * unlocked read is stable: the flag is only set while @pool's CPU is
3303: * dead, inside a serialized hotplug operation. data_race() as the value
3304: * only affects the lockdep annotation and the read can be elided when
3305: * lockdep is disabled.
3306: */
3307: spin_lock_nested(&pool->cb_lock,
3308: data_race(pool->flags) & POOL_BH_DRAINING ? SINGLE_DEPTH_NESTING : 0);
3309: }
SourceFile:kernel/workqueue.c]
|
| 70/2 |
2026/11/19 02:27 |
tool |
git-blame |
0m
Args: map[End:3870 File:kernel/workqueue.c Start:3856]
Results: map[Output:4cb1ef64609f9 3856) static void bh_worker(struct worker *worker)
4cb1ef64609f9 3857) {
4cb1ef64609f9 3858) struct worker_pool *pool = worker->pool;
4cb1ef64609f9 3859) int nr_restarts = BH_WORKER_RESTARTS;
4cb1ef64609f9 3860) unsigned long end = jiffies + BH_WORKER_JIFFIES;
8bbd858c4710f 3861) bool budget_exhausted = false;
8bbd858c4710f 3862) bool timeout = false;
8bbd858c4710f 3863) int restarts = 0;
4cb1ef64609f9 3864)
ad7c7f4b9c6c2 3865) worker_lock_callback(pool);
4cb1ef64609f9 3866) raw_spin_lock_irq(&pool->lock);
4cb1ef64609f9 3867) worker_leave_idle(worker);
4cb1ef64609f9 3868)
4cb1ef64609f9 3869) /*
4cb1ef64609f9 3870) * This function follows the structure of worker_thread(). See there for
]
|
| 71/2 |
2026/11/19 02:27 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 96516
output: 60
thoughts: 327
Thoughts: **Sudden Realization Regarding Lock Nesting and Workqueues**
WHOA! I need to stop everything and take a serious look at `worker_lock_callback` (specifically commit `ad7c7f4b9c6c2`) and `6eca8f94d841`, which is titled "workqueue: Annotate cb_lock nesting when draining a dead BH pool." There's something very important here I need to investigate right now.
My immediate thought is to understand why lockdep was disabled during our crash. Could either `worker_lock_callback` or something else within `workqueue.c`, particularly in the context of `PREEMPT_RT`, have triggered a genuine lockdep splat? Or, perhaps more directly, why did I even begin examining `worker_lock_callback`?
Upon inspecting `worker_lock_callback(pool)`, I see this critical line: `spin_lock_nested(&pool->cb_lock, ...)`. This immediately raises a question: What exactly *is* `pool->cb_lock`? On `PREEMPT_RT` systems, a `spinlock_t` is actually a `rt_mutex`, which is a sleeping lock. On systems *without* `PREEMPT_RT`, it's a standard spinlock. To clarify this crucial detail, I need to check the definition of `cb_lock` by referencing commit `ad7c7f4b9c6c2`.
Therefore, my next immediate action is to run `git-show` on both `ad7c7f4b9c6c2` and `6eca8f94d841` to gain a deep understanding of these changes and their implications for lock management within the workqueue subsystem, especially concerning nested locks and the behavior under `PREEMPT_RT`.
|
| 72/2 |
2026/11/19 02:27 |
tool |
git-show |
0m
Args: map[Commit:ad7c7f4b9c6c2]
Results: map[Output:commit ad7c7f4b9c6c2950778e5bd305392a333de73912
Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Date: Thu Sep 4 16:25:23 2025 +0200
workqueue: Provide a handshake for canceling BH workers
While a BH work item is canceled, the core code spins until it
determines that the item completed. On PREEMPT_RT the spinning relies on
a lock in local_bh_disable() to avoid a live lock if the canceling
thread has higher priority than the BH-worker and preempts it. This lock
ensures that the BH-worker makes progress by PI-boosting it.
This lock in local_bh_disable() is a central per-CPU BKL and about to be
removed.
To provide the required synchronisation add a per pool lock. The lock is
acquired by the bh_worker at the begin while the individual callbacks
are invoked. To enforce progress in case of interruption, __flush_work()
needs to acquire the lock.
This will flush all BH-work items assigned to that pool.
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Tejun Heo <tj@kernel.org>
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 63b2685c2cb4..59faf857ee4f 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -222,7 +222,9 @@ struct worker_pool {
struct workqueue_attrs *attrs; /* I: worker attributes */
struct hlist_node hash_node; /* PL: unbound_pool_hash node */
int refcnt; /* PL: refcnt for unbound pools */
-
+#ifdef CONFIG_PREEMPT_RT
+ spinlock_t cb_lock; /* BH worker cancel lock */
+#endif
/*
* Destruction of pool is RCU protected to allow dereferences
* from get_work_pool().
@@ -3078,6 +3080,31 @@ __acquires(&pool->lock)
goto restart;
}
+#ifdef CONFIG_PREEMPT_RT
+static void worker_lock_callback(struct worker_pool *pool)
+{
+ spin_lock(&pool->cb_lock);
+}
+
+static void worker_unlock_callback(struct worker_pool *pool)
+{
+ spin_unlock(&pool->cb_lock);
+}
+
+static void workqueue_callback_cancel_wait_running(struct worker_pool *pool)
+{
+ spin_lock(&pool->cb_lock);
+ spin_unlock(&pool->cb_lock);
+}
+
+#else
+
+static void worker_lock_callback(struct worker_pool *pool) { }
+static void worker_unlock_callback(struct worker_pool *pool) { }
+static void workqueue_callback_cancel_wait_running(struct worker_pool *pool) { }
+
+#endif
+
/**
* manage_workers - manage worker pool
* @worker: self
@@ -3557,6 +3584,7 @@ static void bh_worker(struct worker *worker)
int nr_restarts = BH_WORKER_RESTARTS;
unsigned long end = jiffies + BH_WORKER_JIFFIES;
+ worker_lock_callback(pool);
raw_spin_lock_irq(&pool->lock);
worker_leave_idle(worker);
@@ -3585,6 +3613,7 @@ static void bh_worker(struct worker *worker)
worker_enter_idle(worker);
kick_pool(pool);
raw_spin_unlock_irq(&pool->lock);
+ worker_unlock_callback(pool);
}
/*
@@ -4222,17 +4251,17 @@ static bool __flush_work(struct work_struct *work, bool from_cancel)
(data & WORK_OFFQ_BH)) {
/*
* On RT, prevent a live lock when %current preempted
- * soft interrupt processing or prevents ksoftirqd from
- * running by keeping flipping BH. If the BH work item
- * runs on a different CPU then this has no effect other
- * than doing the BH disable/enable dance for nothing.
- * This is copied from
- * kernel/softirq.c::tasklet_unlock_spin_wait().
+ * soft interrupt processing by blocking on lock which
+ * is owned by the thread invoking the callback.
*/
while (!try_wait_for_completion(&barr.done)) {
if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
- local_bh_disable();
- local_bh_enable();
+ struct worker_pool *pool;
+
+ guard(rcu)();
+ pool = get_work_pool(work);
+ if (pool)
+ workqueue_callback_cancel_wait_running(pool);
} else {
cpu_relax();
}
@@ -4782,6 +4811,9 @@ static int init_worker_pool(struct worker_pool *pool)
ida_init(&pool->worker_ida);
INIT_HLIST_NODE(&pool->hash_node);
pool->refcnt = 1;
+#ifdef CONFIG_PREEMPT_RT
+ spin_lock_init(&pool->cb_lock);
+#endif
/* shouldn't fail above this point */
pool->attrs = alloc_workqueue_attrs();
]
|
| 73/2 |
2026/11/19 02:27 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 6eca8f94d84106d3754b9df27f46a14572af9e7f
Author: Tejun Heo <tj@kernel.org>
Date: Tue Aug 18 08:47:21 2026 -1000
workqueue: Annotate cb_lock nesting when draining a dead BH pool
On PREEMPT_RT, bh_worker() wraps work item execution in pool->cb_lock to
provide a handshake for canceling BH work items. When a CPU goes down,
drain_dead_softirq_workfn() runs the dead pool's bh_worker() nested inside
the local pool's bh_worker(), acquiring the cb_locks of two different pools
without a nesting annotation. lockdep reports possible recursive locking:
============================================
WARNING: possible recursive locking detected
--------------------------------------------
ktimers/0/16 is trying to acquire lock:
ffff8880b873a990 (&pool->cb_lock){+...}-{3:3}, at: bh_worker+0x7d/0x880
but task is already holding lock:
ffff8880b863a990 (&pool->cb_lock){+...}-{3:3}, at: bh_worker+0x7d/0x880
Call Trace:
bh_worker+0x7d/0x880 kernel/workqueue.c:3688
drain_dead_softirq_workfn+0x95/0x220 kernel/workqueue.c:3763
process_scheduled_works+0xa8e/0x14e0 kernel/workqueue.c:3405
bh_worker+0x46a/0x880 kernel/workqueue.c:3708
tasklet_action+0xc/0x70 kernel/softirq.c:965
The nesting can't deadlock. A pool's bh_worker() runs nested only while the
pool's CPU is dead, entered from a live pool's bh_worker() on the draining
CPU, so the ordering is always live to dead. CPU hotplug operations are
serialized and the drain is synchronous, so the nesting depth never exceeds
two. Annotate the inner acquisition with SINGLE_DEPTH_NESTING.
Signed-off-by: Tejun Heo <tj@kernel.org>
Reported-by: syzbot+1bd20115328f8254ed62@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=1bd20115328f8254ed62
Fixes: ad7c7f4b9c6c ("workqueue: Provide a handshake for canceling BH workers")
Cc: stable@vger.kernel.org # v6.18+
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index bfeef512f6dd..c0b72dcc0f03 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -3197,7 +3197,16 @@ __acquires(&pool->lock)
#ifdef CONFIG_PREEMPT_RT
static void worker_lock_callback(struct worker_pool *pool)
{
- spin_lock(&pool->cb_lock);
+ /*
+ * SINGLE_DEPTH_NESTING is for a dead pool's bh_worker() running from
+ * drain_dead_softirq_workfn() inside a live pool's bh_worker(). The
+ * unlocked read is stable: the flag is only set while @pool's CPU is
+ * dead, inside a serialized hotplug operation. data_race() as the value
+ * only affects the lockdep annotation and the read can be elided when
+ * lockdep is disabled.
+ */
+ spin_lock_nested(&pool->cb_lock,
+ data_race(pool->flags) & POOL_BH_DRAINING ? SINGLE_DEPTH_NESTING : 0);
}
static void worker_unlock_callback(struct worker_pool *pool)
]
|
| 74/2 |
2026/11/19 02:27 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 99422
output: 77
thoughts: 192
Thoughts: **The Cascade of Locks in Our Crash**
Ah, I've just had a moment of clarity regarding the crash we're investigating. It's striking to see precisely how many locks we're holding within the `process_one_work()` function at the point of failure. I need to be absolutely certain about this, so I'm going to meticulously count them.
Looking at the call trace, the first lock I identified is the `rtnl_mutex`, held within `rtnetlink_rcv_msg`. That's lock #1.
Next, I'm tracing the path through `fib_table_insert`. This function appears to call `rtmsg_fib`, and I'm momentarily questioning if `__alloc_skb` is involved. However, I need to focus on the locks. My mind is now considering the sequence: `fib_netdev_event`, `fib_add_ifaddr`, `fib_magic`, and then `fib_table_insert`. To truly understand the locking state here, I need to delve into what locks are held in `handle_softirqs` specifically under a `PREEMPT_RT` configuration. I'll be poring over `kernel/softirq.c`, specifically lines 250-355 and then lines 640-710, to map out this critical locking behavior.
|
| 75/2 |
2026/11/19 02:28 |
tool |
read-file |
0m
Args: map[File:kernel/softirq.c FirstLine:250 LineCount:100]
Results: map[Contents: 250: } else {
251: if (current->softirq_disable_cnt == cnt)
252: sirq_en = true;
253: }
254:
255: if (IS_ENABLED(CONFIG_TRACE_IRQFLAGS) && sirq_en) {
256: raw_local_irq_save(flags);
257: lockdep_softirqs_on(_RET_IP_);
258: raw_local_irq_restore(flags);
259: }
260:
261: if (IS_ENABLED(CONFIG_PREEMPT_RT_NEEDS_BH_LOCK)) {
262: newcnt = this_cpu_sub_return(softirq_ctrl.cnt, cnt);
263: current->softirq_disable_cnt = newcnt;
264:
265: if (!newcnt && unlock) {
266: rcu_read_unlock();
267: local_unlock(&softirq_ctrl.lock);
268: }
269: } else {
270: current->softirq_disable_cnt -= cnt;
271: this_cpu_sub(softirq_ctrl.cnt, cnt);
272: if (unlock && !current->softirq_disable_cnt) {
273: migrate_enable();
274: rcu_read_unlock();
275: } else {
276: WARN_ON_ONCE(current->softirq_disable_cnt < 0);
277: }
278: }
279: }
280:
281: void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
282: {
283: bool preempt_on = preemptible();
284: unsigned long flags;
285: u32 pending;
286: int curcnt;
287:
288: WARN_ON_ONCE(in_hardirq());
289: lockdep_assert_irqs_enabled();
290:
291: lock_map_release(&bh_lock_map);
292:
293: local_irq_save(flags);
294: if (IS_ENABLED(CONFIG_PREEMPT_RT_NEEDS_BH_LOCK))
295: curcnt = this_cpu_read(softirq_ctrl.cnt);
296: else
297: curcnt = current->softirq_disable_cnt;
298:
299: /*
300: * If this is not reenabling soft interrupts, no point in trying to
301: * run pending ones.
302: */
303: if (curcnt != cnt)
304: goto out;
305:
306: pending = local_softirq_pending();
307: if (!pending)
308: goto out;
309:
310: /*
311: * If this was called from non preemptible context, wake up the
312: * softirq daemon.
313: */
314: if (!preempt_on) {
315: wakeup_softirqd();
316: goto out;
317: }
318:
319: /*
320: * Adjust softirq count to SOFTIRQ_OFFSET which makes
321: * in_serving_softirq() become true.
322: */
323: cnt = SOFTIRQ_OFFSET;
324: __local_bh_enable(cnt, false);
325: __do_softirq();
326:
327: out:
328: __local_bh_enable(cnt, preempt_on);
329: local_irq_restore(flags);
330: }
331: EXPORT_SYMBOL(__local_bh_enable_ip);
332:
333: /*
334: * Invoked from ksoftirqd_run() outside of the interrupt disabled section
335: * to acquire the per CPU local lock for reentrancy protection.
336: */
337: static inline void ksoftirqd_run_begin(void)
338: {
339: __local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);
340: local_irq_disable();
341: }
342:
343: /* Counterpart to ksoftirqd_run_begin() */
344: static inline void ksoftirqd_run_end(void)
345: {
346: /* pairs with the lock_map_acquire_read() in ksoftirqd_run_begin() */
347: lock_map_release(&bh_lock_map);
348: __local_bh_enable(SOFTIRQ_OFFSET, true);
349: WARN_ON_ONCE(in_interrupt());
]
|
| 76/2 |
2026/11/19 02:28 |
tool |
read-file |
0m
Args: map[File:kernel/softirq.c FirstLine:640 LineCount:75]
Results: map[Contents: 640: */
641: current->flags &= ~PF_MEMALLOC;
642:
643: pending = local_softirq_pending();
644:
645: from_irq_exit = softirq_handle_begin();
646: in_hardirq = lockdep_softirq_start();
647: account_softirq_enter(current);
648:
649: restart:
650: /* Reset the pending bitmask before enabling irqs */
651: set_softirq_pending(0);
652:
653: local_irq_enable();
654:
655: h = softirq_vec;
656:
657: while ((softirq_bit = ffs(pending))) {
658: unsigned int vec_nr;
659: int prev_count;
660:
661: h += softirq_bit - 1;
662:
663: vec_nr = h - softirq_vec;
664: prev_count = preempt_count();
665:
666: kstat_incr_softirqs_this_cpu(vec_nr);
667:
668: trace_softirq_entry(vec_nr);
669: h->action();
670: trace_softirq_exit(vec_nr);
671: if (unlikely(prev_count != preempt_count())) {
672: pr_err("huh, entered softirq %u %s with preempt_count %08x, exited with %08x?\n",
673: vec_nr, softirq_to_name[vec_nr],
674: prev_count, preempt_count());
675: preempt_count_set(prev_count);
676: }
677: h++;
678: pending >>= softirq_bit;
679: }
680:
681: if (!IS_ENABLED(CONFIG_PREEMPT_RT) && ksirqd)
682: rcu_softirq_qs();
683:
684: local_irq_disable();
685:
686: pending = local_softirq_pending();
687: if (pending) {
688: if (time_before(jiffies, end) && !need_resched() &&
689: --max_restart)
690: goto restart;
691:
692: wakeup_softirqd();
693: }
694:
695: account_softirq_exit(current);
696: lockdep_softirq_end(in_hardirq);
697: softirq_handle_end(from_irq_exit);
698: current_restore_flags(old_flags, PF_MEMALLOC);
699: }
700:
701: asmlinkage __visible void __softirq_entry __do_softirq(void)
702: {
703: handle_softirqs(false);
704: }
705:
706: /**
707: * irq_enter_rcu - Enter an interrupt context with RCU watching
708: */
709: void irq_enter_rcu(void)
710: {
711: __irq_enter_raw();
712:
713: /*
714: * If this is a nested interrupt that hits the exit_to_user_mode_loop
]
|
| 77/2 |
2026/11/19 02:28 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 102145
output: 159
thoughts: 2500
Thoughts: **Reflections on a Kernel Lock Debugging Anomaly**
My immediate reaction upon seeing the trace was sheer astonishment: four distinct locks held, coupled with a Recursive Current Update (RCU) depth of two! This isn't just a few locks; it's a complex scenario unfolding within `process_one_work()` at line 3467, and I needed to dissect it meticulously.
My first step was to enumerate these locks:
1. **`rtnl_mutex`**: Acquired within `rtnetlink_rcv_msg`. Its `lockdep_depth` was 1.
2. **`rcu_read_lock`**: This one was trickier. It originated either from `fib_table_insert` or `rtnetlink_rcv_msg`, linking back to the `rcu_lock_map`. The critical detail here is that RCU lock depth was 2, but `lockdep_depth` remained at 2. This suggests a recursive acquisition within the RCU context, where the `rcu_lock_map` merges references, keeping the apparent `lockdep_depth` stable while the RCU count increments.
3. **`rcu_read_lock` (again, but different context!)**: This is where things got really interesting. It was acquired through `local_bh_disable` within `__alloc_skb`. On `PREEMPT_RT` systems, `local_bh_disable` itself acquires an `rcu_read_lock()`. Since `rcu_lock_map` is recursive-read, it merges these references, meaning `lockdep_depth` stayed at 2, but the RCU count was now *truly* 2. *However*, if `CONFIG_PREEMPT_RT_NEEDS_BH_LOCK` was enabled, then `local_bh_disable` could also acquire `softirq_ctrl.lock` (or `netdev_lock` in `do_setlink` if that path was taken). If that happened, the `lockdep_depth` would surge to 3!
4. **`pool->cb_lock`**: This lock was acquired in `bh_worker`, specifically via `worker_lock_callback(pool)` which then called `spin_lock(&pool->cb_lock)`. This pushed the `lockdep_depth` all the way to 4!
So, in summary, I identified precisely four locks (`lock=4`), an RCU depth of 2 (`RCU=2`), and a `preempt` count of 0x00000000. This is a significant lock contention scenario.
Then, a crucial realization struck me: the specific memory addresses associated with `pool->cb_lock` (`ad7c7f4b9c6c2`) versus `c35aea39d1e10` versus `4cb1ef64609f` indicated a potential commit-related issue. My next objective was to pinpoint the exact causal chain and identify the commit that introduced this bug.
My attention immediately turned to `c35aea39d1e10` ("workqueue: Update lock debugging code") and `4cb1ef64609f` ("workqueue: Implement BH workqueues to eventually replace tasklets"). These seemed like prime candidates.
I decided to re-examine `c35aea39d1e10` more closely. The commit message clearly stated its purpose: preparing for BH workqueues that execute from BH context. It specifically mentioned updating lock and RCU depth checks in `process_one_work()` to track starting depths and report changes.
Looking at the diff in `c35aea39d1e10`, I saw these additions:
This change is crucial. It shifts from a simple check against zero depth to comparing the *current* depth against the *starting* depth captured at the beginning of `process_one_work()`.
Then, I remembered a critical rule from my debugging guidelines: **"Do NOT blame commits that merely added debugging or sanitizer checks (WARN_ON, BUG_ON, KASAN/KMSAN/LOCKDEP annotations) that exposed an older bug, unless the check itself is incorrect and is the bug."**
This rule sent a jolt of caution through my thought process. Was commit `c35aea39d1e10` *causing* the bug, or was it merely *revealing* an existing one? The phrase "unless the check itself is incorrect and is the bug" echoed in my mind. Could the *new* checking logic itself be flawed?
I had to ask myself, with absolute certainty: Is the check in `c35aea39d1e10` incorrect, *or* is there a possibility that `usb_giveback_urb_bh` (or something within `__usb_hcd_giveback_urb`) actually *did* have a real bug that managed to disable `lockdep`? If `lockdep` was truly disabled, then this new check would indeed fire incorrectly, flagging a non-existent bug.
This became the absolute linchpin of my investigation. I needed to rigorously test both possibilities.
My next line of inquiry was to understand how `debug_locks_off()` would behave in the context of syzkaller. What would happen on the serial console if `debug_locks_off()` were called? I resolved to examine *every single caller* of `debug_locks_off()` and `__debug_locks_off()` across the entire kernel.
My previous `grepper` search had yielded only about 10 files. I systematically reviewed them:
* `arch/powerpc/kernel/traps.c` (`panic_flush_kmsg_end`) and the `s390` architecture-specific files were immediately discounted as they were platform-specific and related to panics.
* `kernel/debug/debug_core.c` (`kgdb_panic`) and `kernel/panic.c` were also related to kernel panics.
* Crucially, I examined `include/linux/debug_locks.h`. The `DEBUG_LOCKS_WARN_ON(c)` macro, if invoked, would result in `WARN(1, ...)` which sets the `TAINT_WARN` flag. My crash report showed "Not tainted syzkaller #0", meaning `DEBUG_LOCKS_WARN_ON` was *definitely not* called. The `WARN(1, ...)` would also have printed "WARNING: ..." to the console, which syzkaller would have caught as the primary crash.
* `kernel/locking/rwsem.c` (specifically `RWSEM_WARN_ON`) also relied on `DEBUG_LOCKS_WARN_ON` or `WARN_ONCE`, again leading to `TAINT_WARN`, which was absent.
* `kernel/locking/spinlock_debug.c` involved `spin_bug` and `rwlock_bug`, which would print "BUG: spinlock..." directly. If this had happened, syzkaller would have reported that specific bug first, not the "workqueue leaked" error.
* `lib/locking-selftest.c` was related to boot-time self-tests. I needed to verify if `debug_locks_silent` could have been set in a way that masked the issue, but my grepper suggested this was an unlikely scenario.
* Finally, and most extensively, I needed to scrutinize all 27 occurrences of `debug_locks_off` within `kernel/locking/lockdep.c`.
This systematic elimination process, guided by the critical rule about incorrect checks, was paramount. My next step was to delve into `kernel/panic.c`, `kernel/locking/spinlock_debug.c`, confirm the `debug_locks_silent` status, and meticulously review all `debug_locks_off` calls within `kernel/locking/lockdep.c` to definitively determine if the *check itself* was the bug.
|
| 78/2 |
2026/11/19 02:29 |
tool |
grepper |
0m
Args: map[Expression:debug_locks_silent]
Results: map[Output:include/linux/debug_locks.h=10=extern int debug_locks __read_mostly;
include/linux/debug_locks.h:11:extern int debug_locks_silent __read_mostly;
include/linux/debug_locks.h-12-
--
include/linux/debug_locks.h=22=extern int debug_locks_off(void);
--
include/linux/debug_locks.h-29- instrumentation_begin(); \
include/linux/debug_locks.h:30: if (debug_locks_off() && !debug_locks_silent) \
include/linux/debug_locks.h-31- WARN(1, "DEBUG_LOCKS_WARN_ON(%s)", #c); \
--
kernel/locking/lockdep.c=1920=print_circular_bug_entry(struct lock_list *target, int depth)
kernel/locking/lockdep.c-1921-{
kernel/locking/lockdep.c:1922: if (debug_locks_silent)
kernel/locking/lockdep.c-1923- return;
--
kernel/locking/lockdep.c=1995=print_circular_bug_header(struct lock_list *entry, unsigned int depth,
--
kernel/locking/lockdep.c-2000-
kernel/locking/lockdep.c:2001: if (debug_locks_silent)
kernel/locking/lockdep.c-2002- return;
--
kernel/locking/lockdep.c=2049=static noinline void print_circular_bug(struct lock_list *this,
--
kernel/locking/lockdep.c-2058-
kernel/locking/lockdep.c:2059: if (!debug_locks_off_graph_unlock() || debug_locks_silent)
kernel/locking/lockdep.c-2060- return;
--
kernel/locking/lockdep.c=2591=print_bad_irq_dependency(struct task_struct *curr,
--
kernel/locking/lockdep.c-2601-{
kernel/locking/lockdep.c:2602: if (!debug_locks_off_graph_unlock() || debug_locks_silent)
kernel/locking/lockdep.c-2603- return;
--
kernel/locking/lockdep.c=3054=print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
--
kernel/locking/lockdep.c-3058-
kernel/locking/lockdep.c:3059: if (!debug_locks_off_graph_unlock() || debug_locks_silent)
kernel/locking/lockdep.c-3060- return;
--
kernel/locking/lockdep.c=3166=check_prev_add(struct task_struct *curr, struct held_lock *prev,
--
kernel/locking/lockdep.c-3179- */
kernel/locking/lockdep.c:3180: WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key,
kernel/locking/lockdep.c-3181- "Detected use-after-free of lock class %px/%s\n",
--
kernel/locking/lockdep.c-3183- hlock_class(prev)->name);
kernel/locking/lockdep.c:3184: WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key,
kernel/locking/lockdep.c-3185- "Detected use-after-free of lock class %px/%s\n",
--
kernel/locking/lockdep.c=4051=print_usage_bug(struct task_struct *curr, struct held_lock *this,
--
kernel/locking/lockdep.c-4053-{
kernel/locking/lockdep.c:4054: if (!debug_locks_off() || debug_locks_silent)
kernel/locking/lockdep.c-4055- return;
--
kernel/locking/lockdep.c=4111=print_irq_inversion_bug(struct task_struct *curr,
--
kernel/locking/lockdep.c-4119-
kernel/locking/lockdep.c:4120: if (!debug_locks_off_graph_unlock() || debug_locks_silent)
kernel/locking/lockdep.c-4121- return;
--
kernel/locking/lockdep.c=4845=print_lock_invalid_wait_context(struct task_struct *curr,
--
kernel/locking/lockdep.c-4851- return 0;
kernel/locking/lockdep.c:4852: if (debug_locks_silent)
kernel/locking/lockdep.c-4853- return 0;
--
kernel/locking/lockdep.c=5080=print_lock_nested_lock_not_held(struct task_struct *curr,
--
kernel/locking/lockdep.c-5084- return;
kernel/locking/lockdep.c:5085: if (debug_locks_silent)
kernel/locking/lockdep.c-5086- return;
--
kernel/locking/lockdep.c=5124=static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
--
kernel/locking/lockdep.c-5282-
kernel/locking/lockdep.c:5283: if (!debug_locks_silent) {
kernel/locking/lockdep.c-5284- WARN_ON_ONCE(depth && !hlock_class(hlock - 1)->key);
--
kernel/locking/lockdep.c=5323=static void print_unlock_imbalance_bug(struct task_struct *curr,
--
kernel/locking/lockdep.c-5328- return;
kernel/locking/lockdep.c:5329: if (debug_locks_silent)
kernel/locking/lockdep.c-5330- return;
--
kernel/locking/lockdep.c=6100=static void print_lock_contention_bug(struct task_struct *curr,
--
kernel/locking/lockdep.c-6105- return;
kernel/locking/lockdep.c:6106: if (debug_locks_silent)
kernel/locking/lockdep.c-6107- return;
--
kernel/locking/lockdep.c=6768=print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
--
kernel/locking/lockdep.c-6772- return;
kernel/locking/lockdep.c:6773: if (debug_locks_silent)
kernel/locking/lockdep.c-6774- return;
--
kernel/locking/lockdep.c=6831=static void print_held_locks_bug(void)
--
kernel/locking/lockdep.c-6834- return;
kernel/locking/lockdep.c:6835: if (debug_locks_silent)
kernel/locking/lockdep.c-6836- return;
--
kernel/locking/qspinlock_paravirt.h=499=__pv_queued_spin_unlock_slowpath(struct qspinlock *lock, u8 locked)
--
kernel/locking/qspinlock_paravirt.h-503- if (unlikely(locked != _Q_SLOW_VAL)) {
kernel/locking/qspinlock_paravirt.h:504: WARN(!debug_locks_silent,
kernel/locking/qspinlock_paravirt.h-505- "pvqspinlock: lock 0x%lx has corrupted value 0x%x!\n",
--
kernel/locking/rwsem.c-69-# define DEBUG_RWSEMS_WARN_ON(c, sem) do { \
kernel/locking/rwsem.c:70: if (!debug_locks_silent && \
kernel/locking/rwsem.c-71- WARN_ONCE(c, "DEBUG_RWSEMS_WARN_ON(%s): count = 0x%lx, magic = 0x%lx, owner = 0x%lx, curr 0x%lx, list %sempty\n",\
--
lib/debug_locks.c=26=EXPORT_SYMBOL_GPL(debug_locks);
--
lib/debug_locks.c-28-/*
lib/debug_locks.c:29: * The locking-testsuite uses <debug_locks_silent> to get a
lib/debug_locks.c-30- * 'silent failure': nothing is printed to the console when
--
lib/debug_locks.c-32- */
lib/debug_locks.c:33:int debug_locks_silent __read_mostly;
lib/debug_locks.c:34:EXPORT_SYMBOL_GPL(debug_locks_silent);
lib/debug_locks.c-35-
--
lib/debug_locks.c=39=int debug_locks_off(void)
--
lib/debug_locks.c-41- if (debug_locks && __debug_locks_off()) {
lib/debug_locks.c:42: if (!debug_locks_silent) {
lib/debug_locks.c-43- console_verbose();
--
lib/locking-selftest.c=1430=static void dotest(void (*testcase_fn)(void), int expected, int lockclass_mask)
--
lib/locking-selftest.c-1440-
lib/locking-selftest.c:1441: debug_locks_silent = !(debug_locks_verbose & lockclass_mask);
lib/locking-selftest.c-1442-
--
lib/locking-selftest.c=2828=void locking_selftest(void)
--
lib/locking-selftest.c-2986- lockdep_set_selftest_task(NULL);
lib/locking-selftest.c:2987: debug_locks_silent = 0;
lib/locking-selftest.c-2988-}
]
|
| 79/2 |
2026/11/19 02:29 |
tool |
grepper |
0m
Args: map[Expression:debug_locks_off PathPrefixes:[kernel/panic.c kernel/locking/spinlock_debug.c kernel/locking/rwsem.c]]
Results: map[Output:kernel/locking/rwsem.c-75- rwsem_is_contended(sem) ? "" : "not ")) \
kernel/locking/rwsem.c:76: debug_locks_off(); \
kernel/locking/rwsem.c-77- } while (0)
--
kernel/locking/spinlock_debug.c=73=static void spin_bug(raw_spinlock_t *lock, const char *msg)
kernel/locking/spinlock_debug.c-74-{
kernel/locking/spinlock_debug.c:75: if (!debug_locks_off())
kernel/locking/spinlock_debug.c-76- return;
--
kernel/locking/spinlock_debug.c=146=static void rwlock_bug(rwlock_t *lock, const char *msg)
kernel/locking/spinlock_debug.c-147-{
kernel/locking/spinlock_debug.c:148: if (!debug_locks_off())
kernel/locking/spinlock_debug.c-149- return;
--
kernel/panic.c=587=void vpanic(const char *fmt, va_list args)
--
kernel/panic.c-718- */
kernel/panic.c:719: debug_locks_off();
kernel/panic.c-720- console_flush_on_panic(CONSOLE_FLUSH_PENDING);
--
kernel/panic.c=954=void add_taint(unsigned flag, enum lockdep_ok lockdep_ok)
kernel/panic.c-955-{
kernel/panic.c:956: if (lockdep_ok == LOCKDEP_NOW_UNRELIABLE && __debug_locks_off())
kernel/panic.c-957- pr_warn("Disabling lock debugging due to kernel taint\n");
--
kernel/panic.c=1040=void oops_enter(void)
--
kernel/panic.c-1044- /* can't trust the integrity of the kernel anymore: */
kernel/panic.c:1045: debug_locks_off();
kernel/panic.c-1046- do_oops_enter_exit();
]
|
| 80/2 |
2026/11/19 02:29 |
tool |
read-file |
0m
Args: map[File:kernel/locking/lockdep.c FirstLine:3350 LineCount:30]
Results: map[Contents:3350: */
3351: if (curr->held_locks[depth].irq_context !=
3352: curr->held_locks[depth-1].irq_context)
3353: break;
3354: }
3355: return 1;
3356: out_bug:
3357: if (!debug_locks_off_graph_unlock())
3358: return 0;
3359:
3360: /*
3361: * Clearly we all shouldn't be here, but since we made it we
3362: * can reliable say we messed up our state. See the above two
3363: * gotos for reasons why we could possibly end up here.
3364: */
3365: WARN_ON(1);
3366:
3367: return 0;
3368: }
3369:
3370: struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
3371: static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS);
3372: static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
3373: unsigned long nr_zapped_lock_chains;
3374: unsigned int nr_free_chain_hlocks; /* Free chain_hlocks in buckets */
3375: unsigned int nr_lost_chain_hlocks; /* Lost chain_hlocks */
3376: unsigned int nr_large_chain_blocks; /* size > MAX_CHAIN_BUCKETS */
3377:
3378: /*
3379: * The first 2 chain_hlocks entries in the chain block in the bucket
]
|
| 81/2 |
2026/11/19 02:29 |
tool |
read-file |
0m
Args: map[File:kernel/locking/lockdep.c FirstLine:3980 LineCount:50]
Results: map[Contents:3980: static void check_chain_key(struct task_struct *curr)
3981: {
3982: #ifdef CONFIG_DEBUG_LOCKDEP
3983: struct held_lock *hlock, *prev_hlock = NULL;
3984: unsigned int i;
3985: u64 chain_key = INITIAL_CHAIN_KEY;
3986:
3987: for (i = 0; i < curr->lockdep_depth; i++) {
3988: hlock = curr->held_locks + i;
3989: if (chain_key != hlock->prev_chain_key) {
3990: debug_locks_off();
3991: /*
3992: * We got mighty confused, our chain keys don't match
3993: * with what we expect, someone trample on our task state?
3994: */
3995: WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
3996: curr->lockdep_depth, i,
3997: (unsigned long long)chain_key,
3998: (unsigned long long)hlock->prev_chain_key);
3999: return;
4000: }
4001:
4002: /*
4003: * hlock->class_idx can't go beyond MAX_LOCKDEP_KEYS, but is
4004: * it registered lock class index?
4005: */
4006: if (DEBUG_LOCKS_WARN_ON(!test_bit(hlock->class_idx, lock_classes_in_use)))
4007: return;
4008:
4009: if (prev_hlock && (prev_hlock->irq_context !=
4010: hlock->irq_context))
4011: chain_key = INITIAL_CHAIN_KEY;
4012: chain_key = iterate_chain_key(chain_key, hlock_id(hlock));
4013: prev_hlock = hlock;
4014: }
4015: if (chain_key != curr->curr_chain_key) {
4016: debug_locks_off();
4017: /*
4018: * More smoking hash instead of calculating it, damn see these
4019: * numbers float.. I bet that a pink elephant stepped on my memory.
4020: */
4021: WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
4022: curr->lockdep_depth, i,
4023: (unsigned long long)chain_key,
4024: (unsigned long long)curr->curr_chain_key);
4025: }
4026: #endif
4027: }
4028:
4029: #ifdef CONFIG_PROVE_LOCKING
]
|
| 82/2 |
2026/11/19 02:29 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 109236
output: 41
thoughts: 3496
Thoughts: **My Thought Process on the Workqueue Lockdep Bug**
I'm examining an exhaustive proof related to kernel tainting and lockdep bugs, and it's leading me to a very specific conclusion. My primary observation is that every `WARN`, `WARN_ON`, `DEBUG_LOCKS_WARN_ON`, `oops_enter`, and `add_taint` operation inherently taints the kernel. However, the kernel in question is marked `Not tainted`, which immediately allows me to eliminate a whole class of potential issues. This includes `DEBUG_LOCKS_WARN_ON`, `RWSEM_WARN_ON`, `check_prevs_add` (which calls `out_bug -> WARN_ON(1)`), `check_chain_key` (which calls `WARN(1, ...)`), `__lockdep_reset_lock` (which calls `WARN_ON_ONCE(...)`), and critically, `oops_enter`, `vpanic`, and `add_taint`.
Furthermore, I'm considering the scenario where any lockdep bug might have occurred *on CPU 0 within* `usb_giveback_urb_bh`. If such a bug had happened, it would have printed its specific `WARNING: ...`, `BUG: ...`, or `INFO: ...` message *before* `process_one_work` returned. Crucially, syzkaller would have reported *that specific lockdep bug* instead of the current one. Since that didn't happen, I can rule out those direct lockdep splats originating from `usb_giveback_urb_bh`.
This leaves me with two primary scenarios that *could* lead to the observed crash, both related to lockdep and CPU 0's execution within `process_one_work`:
1. **Scenario 1: Lockdep Exhaustion on CPU 0:** If lockdep hit an exhaustion error like `BUG: MAX_LOCKDEP_* too low!`, `BUG: MAX_STACK_TRACE_ENTRIES too low!`, or `BUG: MAX_LOCK_DEPTH too low!` *within* `usb_giveback_urb_bh` on CPU 0, here's what would transpire:
* Lockdep would call `debug_locks_off()`. Importantly, this function *does not* taint the kernel, so we'd still have a `Not tainted` state.
* Lockdep would print its "BUG: MAX_... too low!" message. My understanding is that syzkaller's report parser (`pkg/report/linux.go`) explicitly ignores these specific messages because lockdep static buffer exhaustion isn't considered a true kernel bug.
* `usb_giveback_urb_bh` would then finish its execution cleanly, with all locks assumed to be released.
* However, upon returning to `process_one_work()`, the condition `lockdep_depth(current) != lockdep_start_depth` would be evaluated. Since `lockdep_depth(current)` returns `0` when `!debug_locks` (which is now true because `debug_locks_off()` was called), and `lockdep_start_depth` was likely `4` (as it's a BH workqueue context), the condition `0 != 4` would evaluate to true.
* This would trigger the `BUG: workqueue leaked atomic, lock or RCU:` message. Syzkaller *does not* ignore this message, and it would be reported as a crash.
2. **Scenario 2: Concurrent `debug_locks_off()` on Another CPU:** If, concurrently, another CPU called `debug_locks_off()` (perhaps due to its own `MAX_LOCKDEP_* too low!` issue or right before printing its own lockdep splat) *while* CPU 0 was executing `process_one_work()`, CPU 0 would observe `debug_locks == 0`. This would lead to the same outcome as Scenario 1: `lockdep_depth(current)` would evaluate to `0`, the `0 != 4` comparison would be true, and the `BUG: workqueue leaked atomic, lock or RCU:` would be printed.
In *both* of these detailed scenarios, the ultimate root cause, the bug that is actually triggered and reported, lies squarely within `process_one_work()` in `kernel/workqueue.c`.
Now, I need to dig into `process_one_work()` itself. Looking at lines 3506-3508, I see:
This brings up a crucial question: why didn't `process_one_work()` exhibit this bug *before* commit `c35aea39d1e1`?
Let's examine the code *before* `c35aea39d1e1`:
Here's how that older logic worked:
* `lockdep_depth(current)` was defined as `(debug_locks ? (current)->lockdep_depth : 0)`.
* When `debug_locks` was `1` (enabled):
* If no lock was leaked, `current->lockdep_depth` would be `0`. The condition `0 > 0` would be `false`, correctly indicating no leak.
* If a lock *was* leaked, `current->lockdep_depth` would be greater than `0`. The condition `> 0` would be `true`, correctly triggering the bug.
* Crucially, when `debug_locks` transitioned from `1` to `0` *during* the execution of `worker->current_func(work)` (e.g., due to lockdep disabling itself), `lockdep_depth(current)` would correctly evaluate to `0` (because `debug_locks` is now `0`). The condition `0 > 0` would be `false`, and importantly, *no false positive occurred*.
Now, let's look at what commit `c35aea39d1e1` ("workqueue: Update lock debugging code") changed:
The critical change was replacing `lockdep_depth(current) > 0` with `lockdep_depth(current) != lockdep_start_depth`.
Here's where the problem arises with that change:
* If `lockdep_start_depth > 0` (which is expected and intended for BH workqueues, as this commit was specifically preparing for them — "These changes are in preparation of BH workqueue which will execute work items from BH context" — and also for scenarios on PREEMPT_RT where locks are held),
* *AND* `debug_locks` transitions from `1` to `0` during the work execution (e.g., due to table exhaustion or an unrelated splat on another CPU),
* Then `lockdep_start_depth` was captured when `debug_locks == 1`, so it was `> 0` (e.g., `4`).
* After `worker->current_func(work)`, `debug_locks` becomes `0`.
* Therefore, `lockdep_depth(current)` (defined as `(debug_locks ? current->lockdep_depth : 0)`) now *always* evaluates to `0`, regardless of the actual `current->lockdep_depth`.
* The condition `lockdep_depth(current) != lockdep_start_depth` then becomes `0 != 4` (or whatever `lockdep_start_depth` was), which is `true`.
* This triggers a *false-positive* `BUG: workqueue leaked atomic, lock or RCU:`.
This is a classic bisection scenario, and I need to be absolutely rigorous. Let's re-read the prompt's investigation strategy and output rules very carefully.
**Re-evaluating the Commit:**
My investigation is pointing directly at commit `c35aea39d1e10` ("workqueue: Update lock debugging code"). Let's confirm why.
1. **Same Author, Same Series:** Both `c35aea39d1e10` and `4cb1ef64609f` are from the *same author* (`Tejun Heo <tj@kernel.org>`) and appear in the *same patch series* (same timestamp: `Sun Feb 4 11:28:06 2024 -1000`). This is important context.
2. **Introducing the Bug:** The commit that *introduced* the buggy logic, and thus would logically receive the `Fixes:` tag in a subsequent fix commit, is `c35aea39d1e106f61fd2130f0d32a3bac8bd4570` ("workqueue: Update lock debugging code"). This is the commit that introduced the problematic comparison:
lockdep_start_depth = lockdep_depth(current);
...
if (unlikely((worker->task && in_atomic()) ||
lockdep_depth(current) != lockdep_start_depth ||
rcu_preempt_depth() != rcu_start_depth)) {
Critically, it *omitted* the `debug_locks` check in the `lockdep_depth(current) != lockdep_start_depth` condition, which is the direct cause of the false positive when `debug_locks` is turned off.
3. **The Invariant Violation:** Even within `c35aea39d1e10` itself, the fundamental assumption it makes is flawed. It assumes that `lockdep_depth(current)` after work execution can be directly compared to `lockdep_start_depth` captured before execution, *even when `lockdep_start_depth > 0`*. This is broken because, as established, `lockdep_depth(tsk)` is `(debug_locks ? (tsk)->lockdep_depth : 0)`. When `debug_locks` is cleared, `lockdep_depth(current)` immediately drops to `0`, irrespective of the actual `current->lockdep_depth`.
4. **Exercising the Bug:** Commits like `4cb1ef64609f` ("workqueue: Implement BH workqueues to eventually replace tasklets") and `8fea0c8fda301` ("usb: core: hcd: Convert from tasklet to BH workqueue") are essential context. They are the *callers* or *enablers* that specifically set up scenarios where `process_one_work()` is entered with `lockdep_start_depth > 0`. This is because BH workqueues inherently run in a context where locks might be held (softirq context, or `pool->cb_lock` on `PREEMPT_RT`). These commits don't introduce the bug but *expose* it by creating the conditions for the false positive. According to the output rules, I should *not* include these as candidate commits, but they are crucial for understanding *why* the bug manifests.
5. **No Other Relevant Changes:** I've double-checked `kernel/workqueue.c` lines 3466, 3467, and 3506-3514. `git blame` confirms that *every single line* involved in the problematic logic within `process_one_work` and the capture of `lockdep_start_depth` has remained untouched since `c35aea39d1e106f61fd2130f0d32a3bac8bd4570`.
6. **`lockdep_depth(tsk)` Definition:** Verifying the definition in `include/linux/lockdep.h` shows that `#define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)` is also long-standing and not part of the recent changes.
Therefore, based on the exhaustive analysis and the strict output rules, the single commit that introduced this specific bug, by changing the comparison logic in `process_one_work` without accounting for the potential deactivation of `debug_locks`, is **`c35aea39d1e10`**. The later commits merely exercised the code path that triggers the false positive.
|
| 83/2 |
2026/11/19 02:30 |
tool |
git-blame |
0m
Args: map[End:285 File:include/linux/lockdep.h Start:275]
Results: map[Output:a24fc60d63da2 275)
8f2109843137d 276) extern u32 lock_sequence(struct lockdep_map *lock);
8f2109843137d 277) #define lockdep_sequence(lock) lock_sequence(&(lock)->dep_map)
8f2109843137d 278)
e3a55fd18deab 279) #define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
d5abe669172f2 280)
d19c81378829e 281) #define lockdep_assert(cond) \
d19c81378829e 282) do { WARN_ON(debug_locks && !(cond)); } while (0)
3e31f94752e45 283)
d19c81378829e 284) #define lockdep_assert_once(cond) \
d19c81378829e 285) do { WARN_ON_ONCE(debug_locks && !(cond)); } while (0)
]
|
| 84/2 |
2026/11/19 02:30 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 113124
output: 68
thoughts: 51
Thoughts: **Tracing Our History: A Deep Dive into Git Revisions**
Alright, before I dive too deep into this bisection log, I need to ensure I have a complete and granular understanding of the history we're dealing with. My instinct is to pull up the `git show` command for both the starting point, `e3a55fd18deab`, and the current point, `a62428c0ae54a`. This isn't just about looking at the commit messages; I want to see the *full* historical context. This means understanding not only *what* changed in each of those commits but also *how* it changed – the diffs, the authors, the timestamps. By thoroughly examining these two key revisions, I'm laying the groundwork for an accurate and efficient bisection process, ensuring no critical details are missed and that our journey through the commit history is as informed as possible.
|
| 85/2 |
2026/11/19 02:30 |
tool |
git-show |
0m
Args: map[Commit:e3a55fd18deab Stat:true]
Results: map[Output:commit e3a55fd18deab758a2970e0dfcd60a677a920426
Author: Jarek Poplawski <jarkao2@o2.pl>
Date: Thu Mar 22 00:11:26 2007 -0800
[PATCH] lockdep: lockdep_depth vs. debug_locks
lockdep found a bug during a run of workqueue function - this could be also
caused by a bug from other code running simultaneously.
lockdep really shouldn't be used when debug_locks == 0!
Reported-by: Folkert van Heusden <folkert@vanheusden.com>
Inspired-by: Oleg Nesterov <oleg@tv-sign.ru>
Signed-off-by: Jarek Poplawski <jarkao2@o2.pl>
Cc: Ingo Molnar <mingo@elte.hu>
Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/lockdep.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
]
|
| 86/2 |
2026/11/19 02:30 |
tool |
git-show |
0m
Args: map[Commit:a62428c0ae54a Stat:true]
Results: map[Output:commit a62428c0ae54a39e411251e836c3fe3dc11a5f5f
Author: Tejun Heo <tj@kernel.org>
Date: Tue Jun 29 10:07:10 2010 +0200
workqueue: separate out process_one_work()
Separate out process_one_work() out of run_workqueue(). This patch
doesn't cause any behavior change.
Signed-off-by: Tejun Heo <tj@kernel.org>
kernel/workqueue.c | 100 ++++++++++++++++++++++++++++++++---------------------
1 file changed, 61 insertions(+), 39 deletions(-)
]
|
| 87/2 |
2026/11/19 02:30 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 113791
output: 31
thoughts: 216
Thoughts: **Reflections on a 2007 Commit**
Wow, I just stumbled upon this commit from 2007, and it's honestly blown my mind a little. It's from Jarek Poplawski, dated March 22, 2007. The commit message itself is pretty concise, but the implications are significant. It's titled "[PATCH] lockdep: lockdep_depth vs. debug_locks".
What really grabbed me was the description: "lockdep found a bug during a run of workqueue function - this could be also caused by a bug from other code running simultaneously. lockdep really shouldn't be used when debug_locks == 0!"
This immediately sparked a series of thoughts for me. Firstly, the very fact that this commit exists points to a fundamental design consideration that must have been debated and implemented back then. The idea that `lockdep` should not be operational when `debug_locks` is disabled is a crucial piece of information. It suggests a clear dependency, perhaps for performance reasons or to avoid spurious errors when the full debugging infrastructure isn't active.
My immediate impulse was to dive deeper and see the full diff associated with this commit (`e3a55fd18deab`). I'm assuming this patch likely involves a conditional check or a structural change to ensure `lockdep` functions correctly, or perhaps is gracefully handled, only when the `debug_locks` option is enabled. I'm keen to understand the exact mechanism they employed to achieve this.
This discovery also makes me ponder the evolution of debugging tools. It's fascinating to see how early on the developers were grappling with these complexities – ensuring that debugging features themselves don't introduce instability or incorrect behavior in different configuration contexts. It speaks to a very mature understanding of systems programming and the need for robust, context-aware tooling. I'm eager to see the code itself to fully appreciate the elegance and effectiveness of this solution.
|
| 88/2 |
2026/11/19 02:30 |
tool |
git-show |
0m
Args: map[Commit:e3a55fd18deab]
Results: map[Output:commit e3a55fd18deab758a2970e0dfcd60a677a920426
Author: Jarek Poplawski <jarkao2@o2.pl>
Date: Thu Mar 22 00:11:26 2007 -0800
[PATCH] lockdep: lockdep_depth vs. debug_locks
lockdep found a bug during a run of workqueue function - this could be also
caused by a bug from other code running simultaneously.
lockdep really shouldn't be used when debug_locks == 0!
Reported-by: Folkert van Heusden <folkert@vanheusden.com>
Inspired-by: Oleg Nesterov <oleg@tv-sign.ru>
Signed-off-by: Jarek Poplawski <jarkao2@o2.pl>
Cc: Ingo Molnar <mingo@elte.hu>
Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
index 06fe93a3e916..14c937d345cb 100644
--- a/include/linux/lockdep.h
+++ b/include/linux/lockdep.h
@@ -245,7 +245,7 @@ extern void lock_release(struct lockdep_map *lock, int nested,
# define INIT_LOCKDEP .lockdep_recursion = 0,
-#define lockdep_depth(tsk) ((tsk)->lockdep_depth)
+#define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
#else /* !LOCKDEP */
]
|
| 89/2 |
2026/11/19 02:30 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 114555
output: 56
thoughts: 1680
Thoughts: **AHA! I'VE FOUND IT!**
My mind is racing right now because I've stumbled upon what feels like a monumental bug in the kernel's lockdep subsystem, specifically how it interacts with workqueues. I'm practically vibrating with excitement because it all fits together so perfectly.
It goes back to 2007, with commit `e3a55fd18deab`. The *reason* for that change was to fix a workqueue leak check. The problem was that when `debug_locks` was turned off *during* a workqueue function's execution, `tsk->lockdep_depth` could get "frozen" at a non-zero value because `lock_release` would stop decrementing it once `debug_locks` hit zero. To prevent false positives from the `lockdep_depth(current) > 0` check in `kernel/workqueue.c`, commit `e3a55fd18deab` cleverly made `lockdep_depth(tsk)` return `0` when `!debug_locks`. This was a clean workaround for its time.
Fast forward to 2024, and commit `c35aea39d1e106f61fd2130f0d32a3bac8bd4570` comes along and completely rewrites the lock debugging code within workqueues. It replaced the old `if (unlikely(in_atomic() || lockdep_depth(current) > 0 || rcu_preempt_depth() > 0))` with a new check: `if (unlikely((worker->task && in_atomic()) || lockdep_depth(current) != lockdep_start_depth || rcu_preempt_depth() != rcu_start_depth))`.
And *that's* where the problem lies! This new check *breaks* the exact mechanism that `e3a55fd18deab` had put in place specifically for workqueues. Here's why:
1. The old check (`lockdep_depth(current) > 0`) relied on `lockdep_depth(current)` evaluating to `0` when `debug_locks` was off. This suppressed the check.
2. The new check (`lockdep_depth(current) != lockdep_start_depth`) was introduced to support BH workqueues. These are tricky because `lockdep_start_depth` *can* be non-zero when entering them, as the `current` task might already hold locks when entering a BH/softirq context.
3. Now, when `debug_locks` transitions from `1` to `0` during a work item's execution (which can happen for various reasons, like lockdep exhausting its limits or another CPU disabling it), and `lockdep_start_depth` was already greater than zero (as it can be in BH workqueues or under `PREEMPT_RT`'s `bh_worker()` lock), `lockdep_depth(current)` will evaluate to `0`.
4. Because `lockdep_start_depth` is *greater* than zero, the condition `0 != lockdep_start_depth` becomes *true*. This GUARANTEES a false-positive `BUG: workqueue leaked atomic, lock or RCU`, even though `lockdep` is actually turned off and the state is perfectly fine! It's reporting `lock=4->0` followed by `INFO: lockdep is turned off.`.
It's all crystal clear now. The root cause is `process_one_work()` in `kernel/workqueue.c` recording `lockdep_start_depth` before executing the work, and then comparing it to the *current* `lockdep_depth` without properly accounting for `debug_locks` becoming zero. Commit `c35aea39d1e106f61fd2130f0d32a3bac8bd4570` is the culprit, directly undoing the fix from `e3a55fd18deab758a2970e0dfcd60a677a920426` in a new context introduced by the BH workqueue series (`4cb1ef64609f9b0254184b2947824f4b46ccab22` and subsequent changes like `8fea0c8fda30129b4168464975505d5dc9735ac1` and `ad7c7f4b9c6c2950778e5bd305392a333de73912` on `PREEMPT_RT`).
The commit I've identified is `c35aea39d1e106f61fd2130f0d32a3bac8bd4570`. It's not a merge commit. I'm ready to output the results!
|
| 90/2 |
2026/11/19 02:31 |
tool |
set-results |
0m
Args: map[CandidateCommits:[c35aea39d1e106f61fd2130f0d32a3bac8bd4570]]
Results: map[CandidateCommits:[c35aea39d1e106f61fd2130f0d32a3bac8bd4570]]
|
| 91/2 |
2026/11/19 02:31 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 116343
output: 2031
thoughts: 30
Thoughts: **My Bisection Analysis is Complete**
I've just finished a bisection process, and I'm ready to present the findings. The `set-results` command has been invoked precisely once, and I have the exact candidate commit hash in hand. My goal now is to distill this entire analysis into a clear, concise, and comprehensive summary for you. You, as an expert in this domain, will appreciate a direct and unambiguous presentation of the results.
|
| 92/1 |
2026/11/19 02:32 |
action |
format-bisect |
0m
Results: map[Commits:[map[AuthorEmail:tj@kernel.org AuthorName:Tejun Heo Date:2024-02-04T11:28:06-10:00 Hash:c35aea39d1e106f61fd2130f0d32a3bac8bd4570 Recipients:[map[Email:allen.lkml@gmail.com Name: To:true] map[Email:tj@kernel.org Name: To:true]] Title:workqueue: Update lock debugging code]]]
|