| 0/0 |
2026/12/01 13:07 |
flow |
bisect-cause |
29m
Results: map[BisectReasoning:### Root Cause & Bisection Log
#### 1. Verification of the Root Cause
In `drivers/block/nbd.c`, both paths that attach a socket to an NBD device—`nbd_add_socket()` (line 1299) and `nbd_genl_reconnect_sock_cb()` (line 1359)—obtain the socket from a user-supplied file descriptor via `nbd_get_socket()` and then call `nbd_reclassify_socket(sock)` (lines 1245–1272).
`nbd_reclassify_socket()` is responsible for moving `sock->sk->sk_lock` and `sock->sk->sk_lock.slock` out of the generic socket lockdep classes (`sk_lock-AF_INET`, etc.) into NBD-specific classes (`sk_lock-AF_INET-NBD`, etc.) so that lockdep does not conflate NBD's block-I/O socket operations with arbitrary userspace TCP socket operations. However, at line 1249:
```c
if (!sock_allow_reclassification(sk))
return;
```
Because userspace still owns the file descriptor (and softirq processing can take `bh_lock_sock(sk)`), `sock_allow_reclassification(sk)` returns `false` whenever `sk->sk_lock` or `sk->sk_lock.slock` is concurrently held. `nbd_reclassify_socket()` then silently returns without reclassifying `sk` and without returning an error to `nbd_add_socket()` / `nbd_genl_reconnect_sock_cb()`.
In the lockdep report, link `#7` (`recv_work()` -> `nbd_mark_nsock_dead()` -> `inet_shutdown()` -> `lock_sock()`) acquires `sk_lock-AF_INET` instead of `sk_lock-AF_INET-NBD`. This allows lockdep to connect the NBD I/O/reconfiguration path (`&ei->i_mmap_lock` -> `btrfs-tree-01` -> `btrfs-tree-00` -> `&q->q_usage_counter(io)` -> `&q->elevator_lock` -> `set->srcu` -> `&cmd->lock` -> `&nsock->tx_lock` -> `sk_lock-AF_INET`) with userspace TCP `tcp_sendmsg_locked()` (`sk_lock-AF_INET` -> `&mm->mmap_lock` -> `sb_pagefaults#4` -> `&ei->i_mmap_lock`), producing the circular locking dependency warning.
#### 2. Git History Traced
- **`9280e6edf65662b6aafc8b704ad065b54c08b519`** ("nbd: don't warn when reclassifying a busy socket lock", Jun 22, 2026) — **Primary culprit**:
Changed `if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) return;` to `if (!sock_allow_reclassification(sk)) return;` in `nbd_reclassify_socket()`. When syzbot hit the race between `NBD_CMD_CONNECT` and concurrent socket locking, this commit chose to silently skip reclassification instead of failing setup (e.g. `-EBUSY`) or synchronizing/preventing concurrent access, explicitly arguing: *"Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless."* That assumption is incorrect: leaving an NBD socket in the generic `sk_lock-AF_INET` class directly re-exposes the circular locking dependency splat.
- **Other commits examined and ruled out**:
- **`d532cddb6c6049ced414d64d83c6ce7149a6421a`** ("nbd: Reclassify sockets to avoid lockdep circular dependency", Jun 13, 2026): Introduced `nbd_reclassify_socket()` to isolate NBD sockets into `sk_lock-AF_INET-NBD`, copying the `WARN_ON_ONCE(!sock_allow_reclassification(sk))` pattern from `nvme_tcp_reclassify_socket()`. Under `WARN_ON_ONCE`, racing on a busy socket was flagged as an error rather than silently accepted.
- **`553d28925fdea4044bad4ae1e2bf9484ad21f758`** ("nbd: Reclassify sockets in the reconnect path", Nov 12, 2026): Extended `nbd_reclassify_socket()` to `nbd_genl_reconnect_sock_cb()`; it merely added a caller to the already-broken silent-skip `nbd_reclassify_socket()`.
- **`f1be1788a32e`** ("block: model freeze & enter queue as lock for supporting lockdep") and **`ffa1e7ada456`** ("block: Make request_queue lockdep splats show up earlier"): Lockdep annotation commits in the block layer (and `fs_reclaim` priming in `ffa1e7ada456` is not part of this dependency chain).
- **`1bf70d08cc3b`**, **`1e44bedbc921a`**, **`8d9b4a162a37`**, **`05fd9564e9faf`**: Valid lock orderings in `block/elevator.c` (`q_usage_counter(io)` -> `q->elevator_lock` -> `set->srcu`) and `fs/btrfs/file.c` (`btrfs_fallocate()` holding `i_mmap_lock` across `file_modified()`), which are normal and only form a cycle with userspace TCP I/O because NBD's socket remained in `sk_lock-AF_INET`.
### Conclusion
The commit that introduced this bug is **`9280e6edf65662b6aafc8b704ad065b54c08b519`** ("nbd: don't warn when reclassifying a busy socket lock"). Commits:[map[AuthorEmail:kartikey406@gmail.com AuthorName:Deepanshu Kartikey Date:2026-06-22T05:22:55+05:30 Hash:9280e6edf65662b6aafc8b704ad065b54c08b519 Recipients:[map[Email:axboe@kernel.dk Name: To:true] map[Email:edumazet@google.com Name: To:true] map[Email:kartikey406@gmail.com Name: To:true]] Title:nbd: don't warn when reclassifying a busy socket lock]] IsRelease:false KernelCommit:19cc08c1a6a650f8e54c0461ff198be6448f425d]
|
| 1/1 |
2026/12/01 13:07 |
action |
use-crash-commit |
0m
Results: map[KernelCommit:19cc08c1a6a650f8e54c0461ff198be6448f425d]
|
| 2/1 |
2026/12/01 13:07 |
action |
syz-repro-to-c-repro |
0m
Results:
|
| 3/1 |
2026/12/01 13:07 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/app/workdir/cache/src/a8a2f6c716142b1917aa2b7bf92f2a1978499d35/data]
|
| 4/1 |
2026/12/01 13:07 |
action |
kernel-builder |
0m
Results: map[KernelObj:/app/workdir/cache/build/ffde4bf3fc731ded33294d93fba8cb19bd265d7c/data]
|
| 5/1 |
2026/12/01 13:07 |
action |
codesearch-prepare |
0m
Results: map[Index:codesearch-index]
|
| 6/1 |
2026/12/01 13:07 |
agent |
cause-finder |
28m
Model:
Results: map[CandidateCommits:[9280e6edf65662b6aafc8b704ad065b54c08b519]]
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.
A root cause analysis of the bug is provided; verify its key claims against the actual
source code before relying on it.
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.
During execution, you may receive periodic guidance in <supervisor_advice> tags;
use it to refine your investigation or wrap up once you have sufficient evidence.
Prompt:
The crash is:
======================================================
WARNING: possible circular locking dependency detected
syzkaller #0 Tainted: G L
------------------------------------------------------
syz.0.1970/12965 is trying to acquire lock:
ffff888056f4c4a0 (btrfs-tree-01){++++}-{4:4}, at: btrfs_tree_read_lock_nested+0x45/0x250 fs/btrfs/locking.c:146
but task is already holding lock:
ffff888052ec3d98 (&ei->i_mmap_lock){++++}-{4:4}, at: btrfs_inode_lock+0xcb/0xe0 fs/btrfs/inode.c:368
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #10 (&ei->i_mmap_lock){++++}-{4:4}:
down_read+0x45/0x320 kernel/locking/rwsem.c:1574
btrfs_page_mkwrite+0x607/0x1a30 fs/btrfs/file.c:1889
do_page_mkwrite+0x147/0x310 mm/memory.c:3908
wp_page_shared mm/memory.c:4317 [inline]
do_wp_page+0x1a7c/0x4c40 mm/memory.c:4540
handle_pte_fault mm/memory.c:6724 [inline]
__handle_mm_fault mm/memory.c:6849 [inline]
handle_mm_fault+0x19c9/0x2f80 mm/memory.c:7016
do_user_addr_fault+0x7f6/0x1480 arch/x86/mm/fault.c:1394
handle_page_fault arch/x86/mm/fault.c:1483 [inline]
exc_page_fault+0x6a/0xc0 arch/x86/mm/fault.c:1536
asm_exc_page_fault+0x26/0x30 arch/x86/include/asm/idtentry.h:595
copy_user_generic arch/x86/include/asm/uaccess_64.h:126 [inline]
raw_copy_to_user arch/x86/include/asm/uaccess_64.h:147 [inline]
_inline_copy_to_user include/linux/uaccess.h:202 [inline]
_copy_to_user+0x85/0xb0 lib/usercopy.c:24
copy_to_user include/linux/uaccess.h:230 [inline]
rng_dev_read+0x3ee/0x830 drivers/char/hw_random/core.c:287
vfs_read+0x213/0xa80 fs/read_write.c:552
ksys_read+0x150/0x270 fs/read_write.c:696
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
-> #9 (sb_pagefaults#4){++++}-{0:0}:
percpu_down_read_internal include/linux/percpu-rwsem.h:53 [inline]
percpu_down_read_freezable include/linux/percpu-rwsem.h:83 [inline]
__sb_start_write include/linux/fs/super.h:19 [inline]
sb_start_pagefault include/linux/fs/super.h:159 [inline]
btrfs_page_mkwrite+0x2fe/0x1a30 fs/btrfs/file.c:1844
do_page_mkwrite+0x147/0x310 mm/memory.c:3908
wp_page_shared mm/memory.c:4317 [inline]
do_wp_page+0x1a7c/0x4c40 mm/memory.c:4540
handle_pte_fault mm/memory.c:6724 [inline]
__handle_mm_fault mm/memory.c:6849 [inline]
handle_mm_fault+0x19c9/0x2f80 mm/memory.c:7016
do_user_addr_fault+0x7f6/0x1480 arch/x86/mm/fault.c:1394
handle_page_fault arch/x86/mm/fault.c:1483 [inline]
exc_page_fault+0x6a/0xc0 arch/x86/mm/fault.c:1536
asm_exc_page_fault+0x26/0x30 arch/x86/include/asm/idtentry.h:595
copy_user_generic arch/x86/include/asm/uaccess_64.h:126 [inline]
raw_copy_to_user arch/x86/include/asm/uaccess_64.h:147 [inline]
_inline_copy_to_user include/linux/uaccess.h:202 [inline]
_copy_to_user+0x85/0xb0 lib/usercopy.c:24
copy_to_user include/linux/uaccess.h:230 [inline]
rng_dev_read+0x3ee/0x830 drivers/char/hw_random/core.c:287
vfs_read+0x213/0xa80 fs/read_write.c:552
ksys_read+0x150/0x270 fs/read_write.c:696
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
-> #8 (&mm->mmap_lock){++++}-{4:4}:
__might_fault+0xcb/0x130 mm/memory.c:7607
_copy_from_iter+0x100/0x1680 lib/iov_iter.c:271
copy_from_iter include/linux/uio.h:236 [inline]
copy_from_iter_full include/linux/uio.h:253 [inline]
skb_do_copy_data_nocache include/net/sock.h:2345 [inline]
skb_copy_to_page_nocache include/net/sock.h:2371 [inline]
tcp_sendmsg_locked+0x244e/0x57c0 net/ipv4/tcp.c:1324
tcp_sendmsg+0x2f/0x50 net/ipv4/tcp.c:1467
sock_sendmsg_nosec+0x10f/0x190 net/socket.c:801
__sock_sendmsg net/socket.c:818 [inline]
sock_write_iter+0x2de/0x3e0 net/socket.c:1269
new_sync_write fs/read_write.c:575 [inline]
vfs_write+0x612/0xba0 fs/read_write.c:667
ksys_write+0x150/0x270 fs/read_write.c:719
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
-> #7 (sk_lock-AF_INET){+.+.}-{0:0}:
lock_sock_nested+0x41/0x100 net/core/sock.c:3909
lock_sock include/net/sock.h:1717 [inline]
inet_shutdown+0x6a/0x390 net/ipv4/af_inet.c:913
nbd_mark_nsock_dead+0x2cb/0x550 drivers/block/nbd.c:319
recv_work+0x1d14/0x1e30 drivers/block/nbd.c:1030
process_one_work kernel/workqueue.c:3497 [inline]
process_scheduled_works+0xc5d/0x1650 kernel/workqueue.c:3580
worker_thread+0xa47/0xfb0 kernel/workqueue.c:3661
kthread+0x38b/0x480 kernel/kthread.c:436
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
-> #6 (&nsock->tx_lock){+.+.}-{4:4}:
__mutex_lock_common kernel/locking/mutex.c:646 [inline]
__mutex_lock+0x197/0x15a0 kernel/locking/mutex.c:821
nbd_handle_cmd drivers/block/nbd.c:1143 [inline]
nbd_queue_rq+0x373/0xff0 drivers/block/nbd.c:1207
blk_mq_dispatch_rq_list+0x499/0x1990 block/blk-mq.c:2141
__blk_mq_do_dispatch_sched block/blk-mq-sched.c:168 [inline]
blk_mq_do_dispatch_sched block/blk-mq-sched.c:182 [inline]
__blk_mq_sched_dispatch_requests+0xd36/0x1580 block/blk-mq-sched.c:307
blk_mq_sched_dispatch_requests+0xd7/0x190 block/blk-mq-sched.c:329
blk_mq_run_work_fn+0x167/0x2f0 block/blk-mq.c:2556
process_one_work kernel/workqueue.c:3497 [inline]
process_scheduled_works+0xc5d/0x1650 kernel/workqueue.c:3580
worker_thread+0xa47/0xfb0 kernel/workqueue.c:3661
kthread+0x38b/0x480 kernel/kthread.c:436
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
-> #5 (&cmd->lock){+.+.}-{4:4}:
__mutex_lock_common kernel/locking/mutex.c:646 [inline]
__mutex_lock+0x197/0x15a0 kernel/locking/mutex.c:821
nbd_queue_rq+0xc1/0xff0 drivers/block/nbd.c:1199
blk_mq_dispatch_rq_list+0x499/0x1990 block/blk-mq.c:2141
__blk_mq_do_dispatch_sched block/blk-mq-sched.c:168 [inline]
blk_mq_do_dispatch_sched block/blk-mq-sched.c:182 [inline]
__blk_mq_sched_dispatch_requests+0xd36/0x1580 block/blk-mq-sched.c:307
blk_mq_sched_dispatch_requests+0xd7/0x190 block/blk-mq-sched.c:329
blk_mq_run_work_fn+0x167/0x2f0 block/blk-mq.c:2556
process_one_work kernel/workqueue.c:3497 [inline]
process_scheduled_works+0xc5d/0x1650 kernel/workqueue.c:3580
worker_thread+0xa47/0xfb0 kernel/workqueue.c:3661
kthread+0x38b/0x480 kernel/kthread.c:436
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
-> #4 (set->srcu){.+.+}-{0:0}:
srcu_lock_sync include/linux/srcu.h:261 [inline]
__synchronize_srcu+0xca/0x2f0 kernel/rcu/srcutree.c:1662
elevator_switch+0x1e8/0x7b0 block/elevator.c:576
elevator_change+0x2fa/0x480 block/elevator.c:681
elevator_set_default+0x375/0x440 block/elevator.c:754
blk_register_queue+0x404/0x4f0 block/blk-sysfs.c:992
__add_disk+0x6b8/0xdb0 block/genhd.c:531
add_disk_fwnode+0x100/0x3a0 block/genhd.c:600
add_disk include/linux/blkdev.h:799 [inline]
nbd_dev_add+0x733/0xb60 drivers/block/nbd.c:2027
nbd_init+0x168/0x1f0 drivers/block/nbd.c:2744
do_one_initcall+0x250/0x870 init/main.c:1432
do_initcall_level+0x10a/0x1a0 init/main.c:1494
do_initcalls+0x59/0xa0 init/main.c:1510
kernel_init_freeable+0x29d/0x3e0 init/main.c:1745
kernel_init+0x22/0x1d0 init/main.c:1635
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
-> #3 (&q->elevator_lock){+.+.}-{4:4}:
__mutex_lock_common kernel/locking/mutex.c:646 [inline]
__mutex_lock+0x197/0x15a0 kernel/locking/mutex.c:821
elevator_change+0x1af/0x480 block/elevator.c:679
elv_iosched_store+0x50e/0x630 block/elevator.c:831
queue_attr_store+0x207/0x2b0 block/blk-sysfs.c:906
kernfs_fop_write_iter+0x3a4/0x540 fs/kernfs/file.c:345
new_sync_write fs/read_write.c:575 [inline]
vfs_write+0x612/0xba0 fs/read_write.c:667
ksys_write+0x150/0x270 fs/read_write.c:719
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
-> #2 (&q->q_usage_counter(io)#17){++++}-{0:0}:
bio_queue_enter block/blk.h:95 [inline]
blk_mq_submit_bio+0x7c6/0x2eb0 block/blk-mq.c:3149
__submit_bio_noacct_mq block/blk-core.c:755 [inline]
submit_bio_noacct_nocheck+0x2fe/0xb80 block/blk-core.c:789
btrfs_submit_chunk fs/btrfs/bio.c:823 [inline]
btrfs_submit_bbio+0x132d/0x1ee0 fs/btrfs/bio.c:882
read_extent_buffer_pages_nowait+0x747/0x860 fs/btrfs/extent_io.c:4381
read_extent_buffer_pages+0x22/0x100 fs/btrfs/extent_io.c:4390
btrfs_read_extent_buffer+0x9d/0x730 fs/btrfs/disk-io.c:236
read_block_for_search+0x67e/0xa40 fs/btrfs/ctree.c:1569
btrfs_search_slot+0x147a/0x2d20 fs/btrfs/ctree.c:2198
btrfs_init_root_free_objectid+0xf5/0x290 fs/btrfs/disk-io.c:5058
btrfs_init_fs_root+0x204/0x3b0 fs/btrfs/disk-io.c:1114
btrfs_get_root_ref+0x3c8/0x890 fs/btrfs/disk-io.c:1365
btrfs_get_fs_root fs/btrfs/disk-io.c:1417 [inline]
open_ctree+0x2d07/0x4140 fs/btrfs/disk-io.c:3805
btrfs_fill_super fs/btrfs/super.c:971 [inline]
btrfs_get_tree_super fs/btrfs/super.c:1956 [inline]
btrfs_get_tree_subvol fs/btrfs/super.c:2099 [inline]
btrfs_get_tree+0x10cf/0x1a00 fs/btrfs/super.c:2133
vfs_get_tree+0x92/0x2a0 fs/super.c:1881
fc_mount fs/namespace.c:1282 [inline]
do_new_mount_fc fs/namespace.c:3910 [inline]
do_new_mount+0x325/0xb00 fs/namespace.c:3986
do_mount fs/namespace.c:4350 [inline]
__do_sys_mount fs/namespace.c:4563 [inline]
__se_sys_mount+0x31d/0x420 fs/namespace.c:4540
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
-> #1 (btrfs-tree-00){++++}-{4:4}:
down_read_nested+0x47/0x320 kernel/locking/rwsem.c:1709
btrfs_tree_read_lock_nested+0x45/0x250 fs/btrfs/locking.c:146
btrfs_tree_read_lock fs/btrfs/locking.h:188 [inline]
read_block_for_search+0x617/0xa40 fs/btrfs/ctree.c:1563
btrfs_search_slot+0x147a/0x2d20 fs/btrfs/ctree.c:2198
btrfs_init_root_free_objectid+0xf5/0x290 fs/btrfs/disk-io.c:5058
btrfs_init_fs_root+0x204/0x3b0 fs/btrfs/disk-io.c:1114
btrfs_get_root_ref+0x3c8/0x890 fs/btrfs/disk-io.c:1365
btrfs_get_fs_root fs/btrfs/disk-io.c:1417 [inline]
open_ctree+0x2d07/0x4140 fs/btrfs/disk-io.c:3805
btrfs_fill_super fs/btrfs/super.c:971 [inline]
btrfs_get_tree_super fs/btrfs/super.c:1956 [inline]
btrfs_get_tree_subvol fs/btrfs/super.c:2099 [inline]
btrfs_get_tree+0x10cf/0x1a00 fs/btrfs/super.c:2133
vfs_get_tree+0x92/0x2a0 fs/super.c:1881
fc_mount fs/namespace.c:1282 [inline]
do_new_mount_fc fs/namespace.c:3910 [inline]
do_new_mount+0x325/0xb00 fs/namespace.c:3986
do_mount fs/namespace.c:4350 [inline]
__do_sys_mount fs/namespace.c:4563 [inline]
__se_sys_mount+0x31d/0x420 fs/namespace.c:4540
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
-> #0 (btrfs-tree-01){++++}-{4:4}:
check_prev_add kernel/locking/lockdep.c:3209 [inline]
check_prevs_add kernel/locking/lockdep.c:3328 [inline]
validate_chain kernel/locking/lockdep.c:3952 [inline]
__lock_acquire+0x164c/0x2de0 kernel/locking/lockdep.c:5288
lock_acquire+0x115/0x350 kernel/locking/lockdep.c:5942
down_read_nested+0x47/0x320 kernel/locking/rwsem.c:1709
btrfs_tree_read_lock_nested+0x45/0x250 fs/btrfs/locking.c:146
btrfs_tree_read_lock fs/btrfs/locking.h:188 [inline]
btrfs_read_lock_root_node+0x3f/0xd0 fs/btrfs/locking.c:267
btrfs_search_slot_get_root fs/btrfs/ctree.c:1740 [inline]
btrfs_search_slot+0x66c/0x2d20 fs/btrfs/ctree.c:2070
btrfs_lookup_match_dir fs/btrfs/dir-item.c:196 [inline]
btrfs_lookup_xattr+0xff/0x1e0 fs/btrfs/dir-item.c:369
btrfs_getxattr+0x97/0x1b0 fs/btrfs/xattr.c:41
btrfs_xattr_handler_get_security+0xd0/0x130 fs/btrfs/xattr.c:398
__vfs_getxattr+0x412/0x440 fs/xattr.c:446
cap_inode_need_killpriv+0x45/0x60 security/commoncap.c:331
security_inode_need_killpriv+0x85/0x240 security/security.c:2209
dentry_needs_remove_privs fs/inode.c:2372 [inline]
file_remove_privs_flags+0x277/0x610 fs/inode.c:2403
file_modified_flags fs/inode.c:2552 [inline]
file_modified+0x18/0x50 fs/inode.c:2571
btrfs_fallocate+0x79a/0x1df0 fs/btrfs/file.c:3126
vfs_fallocate+0x65a/0x7e0 fs/open.c:338
ksys_fallocate fs/open.c:362 [inline]
__do_sys_fallocate fs/open.c:367 [inline]
__se_sys_fallocate fs/open.c:365 [inline]
__x64_sys_fallocate+0xbf/0x110 fs/open.c:365
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
other info that might help us debug this:
Chain exists of:
btrfs-tree-01 --> sb_pagefaults#4 --> &ei->i_mmap_lock
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&ei->i_mmap_lock);
lock(sb_pagefaults#4);
lock(&ei->i_mmap_lock);
rlock(btrfs-tree-01);
*** DEADLOCK ***
locks held by syz.0.1970/12965: 3, on CPU#0:
#0: ffff88805558c478 (sb_writers#12){++++}-{0:0}, at: file_start_write include/linux/fs.h:2765 [inline]
#0: ffff88805558c478 (sb_writers#12){++++}-{0:0}, at: vfs_fallocate+0x5e0/0x7e0 fs/open.c:337
#1: ffff888052ec3f00 (&sb->s_type->i_mutex_key#24){++++}-{4:4}, at: inode_lock include/linux/fs.h:1027 [inline]
#1: ffff888052ec3f00 (&sb->s_type->i_mutex_key#24){++++}-{4:4}, at: btrfs_inode_lock+0x51/0xe0 fs/btrfs/inode.c:365
#2: ffff888052ec3d98 (&ei->i_mmap_lock){++++}-{4:4}, at: btrfs_inode_lock+0xcb/0xe0 fs/btrfs/inode.c:368
stack backtrace:
CPU: 0 UID: 0 PID: 12965 Comm: syz.0.1970 Tainted: G L syzkaller #0 PREEMPT(full)
Tainted: [L]=SOFTLOCKUP
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
print_circular_bug+0x2e2/0x300 kernel/locking/lockdep.c:2087
check_noncircular+0x12f/0x150 kernel/locking/lockdep.c:2219
check_prev_add kernel/locking/lockdep.c:3209 [inline]
check_prevs_add kernel/locking/lockdep.c:3328 [inline]
validate_chain kernel/locking/lockdep.c:3952 [inline]
__lock_acquire+0x164c/0x2de0 kernel/locking/lockdep.c:5288
lock_acquire+0x115/0x350 kernel/locking/lockdep.c:5942
down_read_nested+0x47/0x320 kernel/locking/rwsem.c:1709
btrfs_tree_read_lock_nested+0x45/0x250 fs/btrfs/locking.c:146
btrfs_tree_read_lock fs/btrfs/locking.h:188 [inline]
btrfs_read_lock_root_node+0x3f/0xd0 fs/btrfs/locking.c:267
btrfs_search_slot_get_root fs/btrfs/ctree.c:1740 [inline]
btrfs_search_slot+0x66c/0x2d20 fs/btrfs/ctree.c:2070
btrfs_lookup_match_dir fs/btrfs/dir-item.c:196 [inline]
btrfs_lookup_xattr+0xff/0x1e0 fs/btrfs/dir-item.c:369
btrfs_getxattr+0x97/0x1b0 fs/btrfs/xattr.c:41
btrfs_xattr_handler_get_security+0xd0/0x130 fs/btrfs/xattr.c:398
__vfs_getxattr+0x412/0x440 fs/xattr.c:446
cap_inode_need_killpriv+0x45/0x60 security/commoncap.c:331
security_inode_need_killpriv+0x85/0x240 security/security.c:2209
dentry_needs_remove_privs fs/inode.c:2372 [inline]
file_remove_privs_flags+0x277/0x610 fs/inode.c:2403
file_modified_flags fs/inode.c:2552 [inline]
file_modified+0x18/0x50 fs/inode.c:2571
btrfs_fallocate+0x79a/0x1df0 fs/btrfs/file.c:3126
vfs_fallocate+0x65a/0x7e0 fs/open.c:338
ksys_fallocate fs/open.c:362 [inline]
__do_sys_fallocate fs/open.c:367 [inline]
__se_sys_fallocate fs/open.c:365 [inline]
__x64_sys_fallocate+0xbf/0x110 fs/open.c:365
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:0x7f81335a9c99
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f81344d8028 EFLAGS: 00000246 ORIG_RAX: 000000000000011d
RAX: ffffffffffffffda RBX: 00007f8133835fa0 RCX: 00007f81335a9c99
RDX: 0000000000000006 RSI: 0000000000000000 RDI: 0000000000000004
RBP: 00007f813363b18b R08: 0000000000000000 R09: 0000000000000000
R10: 000000000000ffff R11: 0000000000000246 R12: 0000000000000000
R13: 00007f8133836038 R14: 00007f8133835fa0 R15: 00007ffe47c43d78
</TASK>
A previous root cause analysis of this bug (at commit 19cc08c1a6a650f8e54c0461ff198be6448f425d) concluded:
### Technical Root Cause Analysis
The circular locking dependency reported by lockdep involves two main issues in `drivers/block/nbd.c` when a filesystem (here `btrfs`) is mounted on an NBD device backed by an `AF_INET` TCP socket:
#### 1. Silent failure of `nbd_reclassify_socket()` (`drivers/block/nbd.c`)
When a socket file descriptor is attached to an NBD device via `NBD_SET_SOCK` (`nbd_add_socket()` at `drivers/block/nbd.c:1277`) or reconnected via netlink (`nbd_genl_reconnect_sock_cb()` at `drivers/block/nbd.c:1348`), `nbd_get_socket()` (`drivers/block/nbd.c:1213`) looks up the socket using `sockfd_lookup()` and then calls `nbd_reclassify_socket(sock)` (`drivers/block/nbd.c:1245`).
`nbd_reclassify_socket()` is intended to move the socket's `sk->sk_lock` and `sk->sk_lock.slock` into NBD-specific lockdep classes (`"sk_lock-AF_INET-NBD"` / `&nbd_key[0]`, etc.) via `sock_lock_init_class_and_name()`. However, it performs an unlocked check:
```c
if (!sock_allow_reclassification(sk))
return;
```
(`drivers/block/nbd.c:1249`, where `sock_allow_reclassification()` in `include/net/sock.h:1835` checks `!sock_owned_by_user_nocheck(sk) && !spin_is_locked(&sk->sk_lock.slock)`).
Because the socket originates from userspace and userspace still holds the file descriptor, another thread calling a socket syscall (e.g., `sendmsg`, `recvmsg`, `setsockopt`, `connect`, `shutdown`) or softirq processing incoming packets via `bh_lock_sock(sk)` can concurrently hold `sk->sk_lock` or `sk->sk_lock.slock` when `nbd_reclassify_socket()` runs. When that occurs, `nbd_reclassify_socket()` silently returns without reclassifying the socket and without failing `nbd_add_socket()`/`nbd_genl_reconnect_sock_cb()`, leaving `nsock->sock->sk` in the generic `sk_lock-AF_INET` lockdep class.
#### 2. Resulting dependency cycle (and same-socket deadlock exposure)
With the NBD socket remaining in `sk_lock-AF_INET` (or when userspace continues operating on the same NBD socket fd), lockdep records the following cycle:
1. **`&ei->i_mmap_lock` → `btrfs-tree-01` → `btrfs-tree-00` → `&q->q_usage_counter(io)`**:
- `btrfs_fallocate()` (`fs/btrfs/file.c:3118`) calls `btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_MMAP)`, acquiring `down_write(&ei->i_mmap_lock)` (`fs/btrfs/inode.c:368`).
- While holding `i_mmap_lock`, it calls `file_modified()` (`fs/btrfs/file.c:3126`) → `file_remove_privs_flags()` → `security_inode_need_killpriv()` → `btrfs_xattr_handler_get_security()` → `btrfs_getxattr()` → `btrfs_lookup_xattr()` → `btrfs_search_slot()` → `btrfs_read_lock_root_node()`, acquiring `btrfs-tree-01` (`btrfs_tree_read_lock_nested()`, `fs/btrfs/locking.c:146`).
- Reading child tree blocks in `read_block_for_search()` (`fs/btrfs/ctree.c:1563–1569`) acquires `btrfs-tree-00` and submits metadata I/O via `btrfs_read_extent_buffer()` → `btrfs_submit_bbio()` → `blk_mq_submit_bio()`, entering `&q->q_usage_counter(io)`.
2. **`&q->q_usage_counter(io)` → `&q->elevator_lock` → `set->srcu` → `&cmd->lock` → `&nsock->tx_lock` → `sk_lock-AF_INET`**:
- Block queue reconfiguration (`elevator_change()` / `elevator_switch()` in `block/elevator.c:576, 668–681`) nests queue freeze (`q_usage_counter`) → `&q->elevator_lock` → `blk_mq_quiesce_queue()` (`set->srcu`).
- `blk_mq_dispatch_rq_list()` invokes `nbd_queue_rq()` (`drivers/block/nbd.c:1184`), which acquires `&cmd->lock` → `&nsock->tx_lock` (`nbd_handle_cmd()`, `drivers/block/nbd.c:1143`).
- Under `&nsock->tx_lock`, NBD acquires `sk->sk_lock` both in `nbd_send_cmd()` → `sock_sendmsg()` (`drivers/block/nbd.c:585`) and in `recv_work()` → `nbd_mark_nsock_dead()` → `kernel_sock_shutdown(nsock->sock, SHUT_RDWR)` → `inet_shutdown()` → `lock_sock()` (`drivers/block/nbd.c:319, 1030`), recorded against `sk_lock-AF_INET` (link `#7`).
3. **`sk_lock-AF_INET` → `&mm->mmap_lock` → `sb_pagefaults` → `&ei->i_mmap_lock`**:
- Userspace TCP I/O holds `lock_sock(sk)` (`sk_lock-AF_INET`) across user-memory copies that can fault: `tcp_sendmsg_locked()` (`net/ipv4/tcp.c:1324`) calls `skb_copy_to_page_nocache()` → `_copy_from_iter()` → `__might_fault()` (`mm/memory.c:7607`), and `tcp_recvmsg()` (`net/ipv4/tcp.c:2956`) holds `lock_sock(sk)` across `skb_copy_datagram_msg()` (`net/ipv4/tcp.c:2853`).
- A page write fault against an `mmap()`ed file on the btrfs-on-NBD filesystem enters `do_page_mkwrite()` → `btrfs_page_mkwrite()` (`fs/btrfs/file.c:1844, 1889`), acquiring `sb_start_pagefault()` (`sb_pagefaults`) and `down_read(&inode->i_mmap_lock)` (`&ei->i_mmap_lock`), closing the cycle.
Note also that because `nbd_get_socket()` only borrows a `struct file` reference via `sockfd_lookup()` and leaves the socket file descriptor open in userspace, a task can also issue `recvmsg()`/`read()` directly on the NBD socket fd into an `mmap()`ed page backed by a file on `/dev/nbdX`, creating a real AB-BA lock inversion on the exact same `sk->sk_lock` and `ei->i_mmap_lock` instances even without lockdep class sharing.
Search for the commit that introduced this bug.
Reply: ### Root Cause & Bisection Log
#### 1. Verification of the Root Cause
In `drivers/block/nbd.c`, both paths that attach a socket to an NBD device—`nbd_add_socket()` (line 1299) and `nbd_genl_reconnect_sock_cb()` (line 1359)—obtain the socket from a user-supplied file descriptor via `nbd_get_socket()` and then call `nbd_reclassify_socket(sock)` (lines 1245–1272).
`nbd_reclassify_socket()` is responsible for moving `sock->sk->sk_lock` and `sock->sk->sk_lock.slock` out of the generic socket lockdep classes (`sk_lock-AF_INET`, etc.) into NBD-specific classes (`sk_lock-AF_INET-NBD`, etc.) so that lockdep does not conflate NBD's block-I/O socket operations with arbitrary userspace TCP socket operations. However, at line 1249:
```c
if (!sock_allow_reclassification(sk))
return;
```
Because userspace still owns the file descriptor (and softirq processing can take `bh_lock_sock(sk)`), `sock_allow_reclassification(sk)` returns `false` whenever `sk->sk_lock` or `sk->sk_lock.slock` is concurrently held. `nbd_reclassify_socket()` then silently returns without reclassifying `sk` and without returning an error to `nbd_add_socket()` / `nbd_genl_reconnect_sock_cb()`.
In the lockdep report, link `#7` (`recv_work()` -> `nbd_mark_nsock_dead()` -> `inet_shutdown()` -> `lock_sock()`) acquires `sk_lock-AF_INET` instead of `sk_lock-AF_INET-NBD`. This allows lockdep to connect the NBD I/O/reconfiguration path (`&ei->i_mmap_lock` -> `btrfs-tree-01` -> `btrfs-tree-00` -> `&q->q_usage_counter(io)` -> `&q->elevator_lock` -> `set->srcu` -> `&cmd->lock` -> `&nsock->tx_lock` -> `sk_lock-AF_INET`) with userspace TCP `tcp_sendmsg_locked()` (`sk_lock-AF_INET` -> `&mm->mmap_lock` -> `sb_pagefaults#4` -> `&ei->i_mmap_lock`), producing the circular locking dependency warning.
#### 2. Git History Traced
- **`9280e6edf65662b6aafc8b704ad065b54c08b519`** ("nbd: don't warn when reclassifying a busy socket lock", Jun 22, 2026) — **Primary culprit**:
Changed `if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) return;` to `if (!sock_allow_reclassification(sk)) return;` in `nbd_reclassify_socket()`. When syzbot hit the race between `NBD_CMD_CONNECT` and concurrent socket locking, this commit chose to silently skip reclassification instead of failing setup (e.g. `-EBUSY`) or synchronizing/preventing concurrent access, explicitly arguing: *"Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless."* That assumption is incorrect: leaving an NBD socket in the generic `sk_lock-AF_INET` class directly re-exposes the circular locking dependency splat.
- **Other commits examined and ruled out**:
- **`d532cddb6c6049ced414d64d83c6ce7149a6421a`** ("nbd: Reclassify sockets to avoid lockdep circular dependency", Jun 13, 2026): Introduced `nbd_reclassify_socket()` to isolate NBD sockets into `sk_lock-AF_INET-NBD`, copying the `WARN_ON_ONCE(!sock_allow_reclassification(sk))` pattern from `nvme_tcp_reclassify_socket()`. Under `WARN_ON_ONCE`, racing on a busy socket was flagged as an error rather than silently accepted.
- **`553d28925fdea4044bad4ae1e2bf9484ad21f758`** ("nbd: Reclassify sockets in the reconnect path", Nov 12, 2026): Extended `nbd_reclassify_socket()` to `nbd_genl_reconnect_sock_cb()`; it merely added a caller to the already-broken silent-skip `nbd_reclassify_socket()`.
- **`f1be1788a32e`** ("block: model freeze & enter queue as lock for supporting lockdep") and **`ffa1e7ada456`** ("block: Make request_queue lockdep splats show up earlier"): Lockdep annotation commits in the block layer (and `fs_reclaim` priming in `ffa1e7ada456` is not part of this dependency chain).
- **`1bf70d08cc3b`**, **`1e44bedbc921a`**, **`8d9b4a162a37`**, **`05fd9564e9faf`**: Valid lock orderings in `block/elevator.c` (`q_usage_counter(io)` -> `q->elevator_lock` -> `set->srcu`) and `fs/btrfs/file.c` (`btrfs_fallocate()` holding `i_mmap_lock` across `file_modified()`), which are normal and only form a cycle with userspace TCP I/O because NBD's socket remained in `sk_lock-AF_INET`.
### Conclusion
The commit that introduced this bug is **`9280e6edf65662b6aafc8b704ad065b54c08b519`** ("nbd: don't warn when reclassifying a busy socket lock").
|
| 7/2 |
2026/12/01 13:07 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 15270
output: 201
thoughts: 148
Thoughts: **My Investigative Process**
My immediate inclination is to dive deep into the technical underpinnings. I need to meticulously scrutinize the source code, paying particular attention to the version control history to understand the evolution of the relevant components.
Specifically, I'm going to focus my initial examination on a few key functions: `nbd_reclassify_socket` and `nbd_get_socket`. I also want to look at `sock_allow_reclassification` as it seems directly related to socket behavior. Concurrently, I'll be reviewing `btrfs_fallocate`. Depending on what I uncover, I anticipate needing to delve into `btrfs_page_mkwrite` and `btrfs_inode_lock` as well, should the initial findings point in that direction.
To facilitate this analysis, I plan to execute parallel tool calls. I'll be inspecting `drivers/block/nbd.c`, targeting the section between lines 1210 and 1360. Simultaneously, I'll examine `include/net/sock.h` around the `sock_allow_reclassification` declaration. Finally, I'll be reviewing `fs/btrfs/file.c`, with a focus on the `btrfs_fallocate` implementation. This multi-pronged approach should provide a comprehensive understanding of the interactions and potential issues.
|
| 8/2 |
2026/12/01 13:07 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/block/nbd.c Name:nbd_reclassify_socket]
Results: map[SourceCode:1245: static void nbd_reclassify_socket(struct socket *sock)
1246: {
1247: struct sock *sk = sock->sk;
1248:
1249: if (!sock_allow_reclassification(sk))
1250: return;
1251:
1252: switch (sk->sk_family) {
1253: case AF_INET:
1254: sock_lock_init_class_and_name(sk, "slock-AF_INET-NBD",
1255: &nbd_slock_key[0],
1256: "sk_lock-AF_INET-NBD",
1257: &nbd_key[0]);
1258: break;
1259: case AF_INET6:
1260: sock_lock_init_class_and_name(sk, "slock-AF_INET6-NBD",
1261: &nbd_slock_key[1],
1262: "sk_lock-AF_INET6-NBD",
1263: &nbd_key[1]);
1264: break;
1265: case AF_UNIX:
1266: sock_lock_init_class_and_name(sk, "slock-AF_UNIX-NBD",
1267: &nbd_slock_key[2],
1268: "sk_lock-AF_UNIX-NBD",
1269: &nbd_key[2]);
1270: break;
1271: }
1272: }
SourceFile:drivers/block/nbd.c]
|
| 9/2 |
2026/12/01 13:07 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/block/nbd.c Name:nbd_get_socket]
Results: map[SourceCode:1213: static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
1214: int *err)
1215: {
1216: struct socket *sock;
1217:
1218: *err = 0;
1219: sock = sockfd_lookup(fd, err);
1220: if (!sock)
1221: return NULL;
1222:
1223: if (!sk_is_tcp(sock->sk) &&
1224: !sk_is_stream_unix(sock->sk)) {
1225: dev_err(disk_to_dev(nbd->disk), "Unsupported socket: should be TCP or UNIX.\n");
1226: *err = -EINVAL;
1227: sockfd_put(sock);
1228: return NULL;
1229: }
1230:
1231: if (sock->ops->shutdown == sock_no_shutdown) {
1232: dev_err(disk_to_dev(nbd->disk), "Unsupported socket: shutdown callout must be supported.\n");
1233: *err = -EINVAL;
1234: sockfd_put(sock);
1235: return NULL;
1236: }
1237:
1238: return sock;
1239: }
SourceFile:drivers/block/nbd.c]
|
| 10/2 |
2026/12/01 13:07 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:include/net/sock.h Name:sock_allow_reclassification]
Results: map[SourceCode:1834: /* no reclassification while locks are held */
1835: static inline bool sock_allow_reclassification(const struct sock *csk)
1836: {
1837: struct sock *sk = (struct sock *)csk;
1838:
1839: return !sock_owned_by_user_nocheck(sk) &&
1840: !spin_is_locked(&sk->sk_lock.slock);
1841: }
SourceFile:include/net/sock.h]
|
| 11/2 |
2026/12/01 13:07 |
tool |
git-log |
0m
Args: map[SourcePath:drivers/block/nbd.c SymbolName:nbd_reclassify_socket]
Results: map[Output:9280e6edf656 nbd: don't warn when reclassifying a busy socket lock
d532cddb6c60 nbd: Reclassify sockets to avoid lockdep circular dependency
]
|
| 12/2 |
2026/12/01 13:07 |
tool |
git-log |
0m
Args: map[SourcePath:drivers/block/nbd.c SymbolName:nbd_get_socket]
Results: map[Output:d532cddb6c60 nbd: Reclassify sockets to avoid lockdep circular dependency
9f7c02e03157 nbd: restrict sockets to TCP and UDP
dff10bbea4be nbd:fix memory leak in nbd_get_socket()
cf1b2326b734 nbd: verify socket is supported during setup
]
|
| 13/2 |
2026/12/01 13:07 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 16849
output: 186
thoughts: 184
Thoughts: **My Deep Dive into `nbd_reclassify_socket` and Related Lock Management**
Right, so I'm looking at these two commits related to `nbd_reclassify_socket`: `9280e6edf656` which is about *not* warning when reclassifying a busy socket lock, and `d532cddb6c60` which is the actual change to reclassify sockets to sidestep a lockdep circular dependency. My immediate thought is that this is all about managing kernel locking more gracefully, particularly in scenarios where a socket might need to change its classification under certain conditions, and we want to avoid triggering false positives or actual deadlocks with lockdep.
To really get my head around this, I need to see the broader context. So, I'm going to pull up `git-show` for both of those commits (`9280e6edf656` and `d532cddb6c60`). This will let me see the exact code changes and the rationale provided in the commit messages.
Beyond just these two specific changes, I want to understand how this `nbd_reclassify_socket` function is used and what its implications are. I need to identify all the callers of `nbd_reclassify_socket`. This will help me map out the scenarios where this reclassification happens and why it's necessary.
Furthermore, I'm curious about related mechanisms for socket classification and locking. I'll be looking at functions like `sock_allow_reclassification` – what's its purpose, and how does it interact with the reclassification process? I'll also want to check out other areas where dynamic classification or locking changes might occur, such as in `btrfs_fallocate` and `elevator_change`. While seemingly unrelated, exploring these might reveal underlying patterns or common strategies for handling dynamic locking states in the kernel.
Finally, to understand the history and evolution of the locking infrastructure around sockets, I'll run `git-log` on `sock_allow_reclassification` and `sock_lock_init_class_and_name`. This will give me a view into how these concepts have developed over time, which is crucial for understanding the design decisions behind the `nbd_reclassify_socket` changes and ensuring I'm not missing any subtle dependencies or historical context. My goal is to build a comprehensive picture of how socket classifications are managed and how these specific commits fit into the larger kernel locking framework.
|
| 14/2 |
2026/12/01 13:08 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 9280e6edf65662b6aafc8b704ad065b54c08b519
Author: Deepanshu Kartikey <kartikey406@gmail.com>
Date: Mon Jun 22 05:22:55 2026 +0530
nbd: don't warn when reclassifying a busy socket lock
nbd_reclassify_socket() warns via WARN_ON_ONCE() if the socket lock is
held at the point of reclassification. That assertion was copied from
nvme-tcp, where the socket is created internally by the kernel
(sock_create_kern()) and is never visible to user space, so the lock
is guaranteed to be free.
NBD is different: the socket is looked up from a user-supplied fd in
nbd_get_socket(), and user space retains that fd. A concurrent syscall
on the same socket (or softirq processing taking bh_lock_sock() on a
connected TCP socket) can legitimately hold the lock at the instant
NBD reclassifies it. sock_allow_reclassification() then returns false
and the WARN_ON_ONCE() fires, which turns into a crash under
panic_on_warn. This is reachable by simply racing NBD_CMD_CONNECT
against socket activity on the same fd, as reported by syzbot.
Hitting a held lock here is expected for an externally owned socket and
is not a kernel bug, so skip reclassification silently instead of
warning. Reclassification is a lockdep-only annotation, so skipping it
in the rare racing case is harmless.
Reported-by: syzbot+6b85d1e39a5b8ed9a954@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=6b85d1e39a5b8ed9a954
Fixes: d532cddb6c60 ("nbd: Reclassify sockets to avoid lockdep circular dependency")
Signed-off-by: Deepanshu Kartikey <kartikey406@gmail.com>
Acked-by: Eric Dumazet <edumazet@google.com>
Link: https://patch.msgid.link/20260621235255.66015-1-kartikey406@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 3a585a0c882a..8f10762e90ef 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1246,7 +1246,7 @@ static void nbd_reclassify_socket(struct socket *sock)
{
struct sock *sk = sock->sk;
- if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
+ if (!sock_allow_reclassification(sk))
return;
switch (sk->sk_family) {
]
|
| 15/2 |
2026/12/01 13:08 |
tool |
git-show |
0m
Args:
Results: map[Output:commit d532cddb6c6049ced414d64d83c6ce7149a6421a
Author: Eric Dumazet <edumazet@kernel.org>
Date: Sat Jun 13 04:26:19 2026 +0000
nbd: Reclassify sockets to avoid lockdep circular dependency
syzbot reported a possible circular locking dependency in udp_sendmsg()
where fs_reclaim can be triggered while holding sk_lock, and fs_reclaim
can eventually depend on another sk_lock (e.g., if NBD is used for swap
or writeback and NBD uses TLS/TCP which acquires sk_lock).
Since the UDP socket and the NBD TCP/TLS socket are different, this is a
false positive. Fix this by reclassifying NBD sockets to a separate lock
class when they are added to the NBD device.
This is similar to what nvme-tcp and other network block devices do.
Fixes: ffa1e7ada456 ("block: Make request_queue lockdep splats show up earlier")
Reported-by: syzbot+607cdcf978b3e79da878@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/netdev/6a2cdafe.428ffe26.258b27.0161.GAE@google.com/T/#u
Signed-off-by: Eric Dumazet <edumazet@google.com>
Link: https://patch.msgid.link/20260613042619.1108126-1-edumazet@google.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index e2fe9e3308fc..3a585a0c882a 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1238,6 +1238,42 @@ static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
return sock;
}
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+static struct lock_class_key nbd_key[3];
+static struct lock_class_key nbd_slock_key[3];
+
+static void nbd_reclassify_socket(struct socket *sock)
+{
+ struct sock *sk = sock->sk;
+
+ if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
+ return;
+
+ switch (sk->sk_family) {
+ case AF_INET:
+ sock_lock_init_class_and_name(sk, "slock-AF_INET-NBD",
+ &nbd_slock_key[0],
+ "sk_lock-AF_INET-NBD",
+ &nbd_key[0]);
+ break;
+ case AF_INET6:
+ sock_lock_init_class_and_name(sk, "slock-AF_INET6-NBD",
+ &nbd_slock_key[1],
+ "sk_lock-AF_INET6-NBD",
+ &nbd_key[1]);
+ break;
+ case AF_UNIX:
+ sock_lock_init_class_and_name(sk, "slock-AF_UNIX-NBD",
+ &nbd_slock_key[2],
+ "sk_lock-AF_UNIX-NBD",
+ &nbd_key[2]);
+ break;
+ }
+}
+#else
+static inline void nbd_reclassify_socket(struct socket *sock) {}
+#endif
+
static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
bool netlink)
{
@@ -1254,6 +1290,7 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
sock = nbd_get_socket(nbd, arg, &err);
if (!sock)
return err;
+ nbd_reclassify_socket(sock);
/*
* We need to make sure we don't get any errant requests while we're
]
|
| 16/2 |
2026/12/01 13:08 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:drivers/block/nbd.c IncludeSnippetLines:10 Name:nbd_reclassify_socket]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:nbd_add_socket SourceFile:drivers/block/nbd.c SourceLine:1299 SourceSnippet:1289:
1290: if (nbd->pid) {
1291: dev_err(disk_to_dev(nbd->disk),
1292: "Cannot add socket to a running device\n");
1293: return -EBUSY;
1294: }
1295:
1296: sock = nbd_get_socket(nbd, arg, &err);
1297: if (!sock)
1298: return err;
1299: nbd_reclassify_socket(sock);
1300:
1301: if (!netlink && !nbd->task_setup &&
1302: !test_bit(NBD_RT_BOUND, &config->runtime_flags))
1303: nbd->task_setup = current;
1304:
1305: if (!netlink &&
1306: (nbd->task_setup != current ||
1307: test_bit(NBD_RT_BOUND, &config->runtime_flags))) {
1308: dev_err(disk_to_dev(nbd->disk),
1309: "Device being setup by another task");
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:nbd_genl_reconnect_sock_cb SourceFile:drivers/block/nbd.c SourceLine:1359 SourceSnippet:1349: {
1350: struct nbd_config *config = nbd->config;
1351: struct socket *sock, *old;
1352: struct recv_thread_args *args;
1353: int i;
1354: int err;
1355:
1356: sock = nbd_get_socket(nbd, arg, &err);
1357: if (!sock)
1358: return err;
1359: nbd_reclassify_socket(sock);
1360:
1361: args = kzalloc_obj(*args);
1362: if (!args) {
1363: sockfd_put(sock);
1364: return -ENOMEM;
1365: }
1366:
1367: for (i = 0; i < config->num_connections; i++) {
1368: struct nbd_sock *nsock = config->socks[i];
1369:
]] TruncatedOutput:false]
|
| 17/2 |
2026/12/01 13:08 |
tool |
git-log |
10m
Error:
Args: map[CodeRegexp:sock_allow_reclassification|sock_lock_init_class_and_name Count:20]
Results:
|
| 18/2 |
2026/12/01 13:18 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/file.c Name:btrfs_fallocate]
Results: map[SourceCode:3076: static long btrfs_fallocate(struct file *file, int mode,
3077: loff_t offset, loff_t len)
3078: {
3079: struct inode *inode = file_inode(file);
3080: struct extent_state *cached_state = NULL;
3081: struct extent_changeset *data_reserved = NULL;
3082: struct falloc_range *range;
3083: struct falloc_range *tmp;
3084: LIST_HEAD(reserve_list);
3085: u64 cur_offset;
3086: u64 last_byte;
3087: u64 alloc_start;
3088: u64 alloc_end;
3089: u64 alloc_hint = 0;
3090: u64 locked_end;
3091: u64 actual_end = 0;
3092: u64 data_space_needed = 0;
3093: u64 data_space_reserved = 0;
3094: u64 qgroup_reserved = 0;
3095: struct extent_map *em;
3096: int blocksize = BTRFS_I(inode)->root->fs_info->sectorsize;
3097: int ret;
3098:
3099: if (btrfs_is_shutdown(inode_to_fs_info(inode)))
3100: return -EIO;
3101:
3102: /* Do not allow fallocate in ZONED mode */
3103: if (btrfs_is_zoned(inode_to_fs_info(inode)))
3104: return -EOPNOTSUPP;
3105:
3106: alloc_start = round_down(offset, blocksize);
3107: alloc_end = round_up(offset + len, blocksize);
3108: cur_offset = alloc_start;
3109:
3110: /* Make sure we aren't being give some crap mode */
3111: if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
3112: FALLOC_FL_ZERO_RANGE))
3113: return -EOPNOTSUPP;
3114:
3115: if (mode & FALLOC_FL_PUNCH_HOLE)
3116: return btrfs_punch_hole(file, offset, len);
3117:
3118: btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
3119:
3120: if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) {
3121: ret = inode_newsize_ok(inode, offset + len);
3122: if (ret)
3123: goto out;
3124: }
3125:
3126: ret = file_modified(file);
3127: if (ret)
3128: goto out;
3129:
3130: /*
3131: * TODO: Move these two operations after we have checked
3132: * accurate reserved space, or fallocate can still fail but
3133: * with page truncated or size expanded.
3134: *
3135: * But that's a minor problem and won't do much harm BTW.
3136: */
3137: if (alloc_start > inode->i_size) {
3138: ret = btrfs_cont_expand(BTRFS_I(inode), i_size_read(inode),
3139: alloc_start);
3140: if (ret)
3141: goto out;
3142: } else if (offset + len > inode->i_size) {
3143: /*
3144: * If we are fallocating from the end of the file onward we
3145: * need to zero out the end of the block if i_size lands in the
3146: * middle of a block.
3147: */
3148: ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size,
3149: inode->i_size, (u64)-1);
3150: if (ret)
3151: goto out;
3152: }
3153:
3154: /*
3155: * We have locked the inode at the VFS level (in exclusive mode) and we
3156: * have locked the i_mmap_lock lock (in exclusive mode). Now before
3157: * locking the file range, flush all dealloc in the range and wait for
3158: * all ordered extents in the range to complete. After this we can lock
3159: * the file range and, due to the previous locking we did, we know there
3160: * can't be more delalloc or ordered extents in the range.
3161: */
3162: ret = btrfs_wait_ordered_range(BTRFS_I(inode), alloc_start,
3163: alloc_end - alloc_start);
3164: if (ret)
3165: goto out;
3166:
3167: if (mode & FALLOC_FL_ZERO_RANGE) {
3168: ret = btrfs_zero_range(inode, offset, len, mode);
3169: btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
3170: return ret;
3171: }
3172:
3173: locked_end = alloc_end - 1;
3174: btrfs_lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
3175: &cached_state);
3176:
3177: btrfs_assert_inode_range_clean(BTRFS_I(inode), alloc_start, locked_end);
3178:
3179: /* First, check if we exceed the qgroup limit */
3180: while (cur_offset < alloc_end) {
3181: em = btrfs_get_extent(BTRFS_I(inode), NULL, cur_offset,
3182: alloc_end - cur_offset);
3183: if (IS_ERR(em)) {
3184: ret = PTR_ERR(em);
3185: break;
3186: }
3187: last_byte = min(btrfs_extent_map_end(em), alloc_end);
3188: actual_end = min_t(u64, btrfs_extent_map_end(em), offset + len);
3189: last_byte = ALIGN(last_byte, blocksize);
3190: if (em->disk_bytenr == EXTENT_MAP_HOLE ||
3191: (cur_offset >= inode->i_size &&
3192: !(em->flags & EXTENT_FLAG_PREALLOC))) {
3193: const u64 range_len = last_byte - cur_offset;
3194:
3195: ret = add_falloc_range(&reserve_list, cur_offset, range_len);
3196: if (ret < 0) {
3197: btrfs_free_extent_map(em);
3198: break;
3199: }
3200: ret = btrfs_qgroup_reserve_data(BTRFS_I(inode),
3201: &data_reserved, cur_offset, range_len);
3202: if (ret < 0) {
3203: btrfs_free_extent_map(em);
3204: break;
3205: }
3206: qgroup_reserved += range_len;
3207: data_space_needed += range_len;
3208: }
3209: btrfs_free_extent_map(em);
3210: cur_offset = last_byte;
3211: }
3212:
3213: if (!ret && data_space_needed > 0) {
3214: /*
3215: * We are safe to reserve space here as we can't have delalloc
3216: * in the range, see above.
3217: */
3218: ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
3219: data_space_needed);
3220: if (!ret)
3221: data_space_reserved = data_space_needed;
3222: }
3223:
3224: /*
3225: * If ret is still 0, means we're OK to fallocate.
3226: * Or just cleanup the list and exit.
3227: */
3228: list_for_each_entry_safe(range, tmp, &reserve_list, list) {
3229: if (!ret) {
3230: ret = btrfs_prealloc_file_range(inode, mode,
3231: range->start,
3232: range->len, blocksize,
3233: offset + len, &alloc_hint);
3234: /*
3235: * btrfs_prealloc_file_range() releases space even
3236: * if it returns an error.
3237: */
3238: data_space_reserved -= range->len;
3239: qgroup_reserved -= range->len;
3240: } else if (data_space_reserved > 0) {
3241: btrfs_free_reserved_data_space(BTRFS_I(inode),
3242: data_reserved, range->start,
3243: range->len);
3244: data_space_reserved -= range->len;
3245: qgroup_reserved -= range->len;
3246: } else if (qgroup_reserved > 0) {
3247: btrfs_qgroup_free_data(BTRFS_I(inode), data_reserved,
3248: range->start, range->len, NULL);
3249: qgroup_reserved -= range->len;
3250: }
3251: list_del(&range->list);
3252: kfree(range);
3253: }
3254: if (ret < 0)
3255: goto out_unlock;
3256:
3257: /*
3258: * We didn't need to allocate any more space, but we still extended the
3259: * size of the file so we need to update i_size and the inode item.
3260: */
3261: ret = btrfs_fallocate_update_isize(inode, actual_end, mode);
3262: out_unlock:
3263: btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
3264: &cached_state);
3265: out:
3266: btrfs_inode_unlock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
3267: extent_changeset_free(data_reserved);
3268: return ret;
3269: }
SourceFile:fs/btrfs/file.c]
|
| 19/2 |
2026/12/01 13:18 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 23136
output: 130
thoughts: 137
Thoughts: **Deep Dive into the `nbd` Reclassification Change**
Okay, so my immediate reaction to seeing the commit `9280e6edf656` – "nbd: don't warn when reclassifying a busy socket lock" – was one of intense curiosity and a need for immediate context. My brain immediately jumped to: "Whoa, what's the timeline here? What else is going on in the vicinity of this change?"
My first instinct is always to anchor myself within the broader development landscape. So, my immediate follow-up question was about `HEAD`'s date. I need to know if this `nbd` change is a recent development, potentially a fix for something else, or an older, established piece of code. Understanding its age relative to other ongoing work is crucial for assessing its impact and potential interactions.
Then, I want to understand the specific neighborhood this commit inhabits. The file path `drivers/block/nbd.c` tells me it's related to the Network Block Device subsystem, which is, of course, a critical component for block device management over a network. But block devices aren't isolated. My mind immediately expands to other potentially related areas. I'm thinking about:
* **`drivers/block/nbd.c` itself:** I need to see the history *within* this file. What led up to this specific modification? Were there previous warnings or behaviors that prompted this change? What's the evolution of the socket lock reclassification logic in `nbd`?
* **`fs/btrfs`:** Why `btrfs`? My hypothesis is that `btrfs` might be a significant user of `nbd`, or that there's some interaction between filesystem-level operations (like those in `btrfs`) and how `nbd` handles its socket locks. Perhaps a specific `btrfs` operation was triggering this warning, and this commit is a direct response to that. I need to check `fs/btrfs/file.c` to see if there are any relevant `nbd` calls or patterns.
* **`block/`:** This is a broader category. The `nbd` module is fundamentally a block device driver. I'm interested in other block layer components that might interact with or be affected by `nbd`'s behavior. Specifically, the `block/elevator.c` file comes to mind. Elevator algorithms are about scheduling I/O requests, and if `nbd` is involved, its handling of busy states could potentially impact the efficiency or behavior of the I/O scheduler.
So, my thought process is a cascading one: identify the core change, establish its temporal context, and then immediately explore its immediate and broader file system and block layer ecosystem to understand the *why* and the *what else*. This isn't just about reading a commit message; it's about mentally constructing a dependency graph and a timeline to grasp the full implications of a single line of code. The `git-log` commands are my tools for this exploration, allowing me to traverse the commit history and uncover these interconnections.
|
| 20/2 |
2026/12/01 13:18 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:drivers/block/nbd.c]
Results: map[Output:553d28925fde nbd: Reclassify sockets in the reconnect path
326d49039c10 nbd: add pre_defined_connections module parameter for pre-created devices
a9d414b4a15c nbd: remove queue freeze for newly created nbd from netlink path
f8d21c590e55 nbd: factor out a nbd_genl_foreach_sock
db285268df79 nbd: skip queue freeze when setting size at device startup
285908f554fe nbd: remove queue freeze in nbd_add_socket
0fdee7c5fa88 nbd: clear queue limits on disconnect
04d8fb23e520 nbd: disallow NBD_SET_SOCK on an active device
fe247030f1ce nbd: simplify find_fallback() by removing redundant logic
9280e6edf656 nbd: don't warn when reclassifying a busy socket lock
d532cddb6c60 nbd: Reclassify sockets to avoid lockdep circular dependency
b040a1a4523d block: switch numa_node to int in blk_mq_hw_ctx and init_request
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
1649714b930f nbd: defer config unlock in nbd_genl_connect
9517b82d8d42 nbd: defer config put in recv_work
4601b7923d1b nbd: don't copy kernel creds
81ccca31214e nbd: override creds to kernel when calling sock_{send,recv}msg()
51723bf92679 drivers/block: replace use of system_wq with system_percpu_wq
9f7c02e03157 nbd: restrict sockets to TCP and UDP
]
|
| 21/2 |
2026/12/01 13:18 |
tool |
git-show |
0m
Args:
Results: map[Output:commit ffa1e7ada456087c2402b37cd6b2863ced29aff0
Author: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Date: Tue Mar 18 10:55:48 2025 +0100
block: Make request_queue lockdep splats show up earlier
In recent kernels, there are lockdep splats around the
struct request_queue::io_lockdep_map, similar to [1], but they
typically don't show up until reclaim with writeback happens.
Having multiple kernel versions released with a known risc of kernel
deadlock during reclaim writeback should IMHO be addressed and
backported to -stable with the highest priority.
In order to have these lockdep splats show up earlier,
preferrably during system initialization, prime the
struct request_queue::io_lockdep_map as GFP_KERNEL reclaim-
tainted. This will instead lead to lockdep splats looking similar
to [2], but without the need for reclaim + writeback
happening.
[1]:
[ 189.762244] ======================================================
[ 189.762432] WARNING: possible circular locking dependency detected
[ 189.762441] 6.14.0-rc6-xe+ #6 Tainted: G U
[ 189.762450] ------------------------------------------------------
[ 189.762459] kswapd0/119 is trying to acquire lock:
[ 189.762467] ffff888110ceb710 (&q->q_usage_counter(io)#26){++++}-{0:0}, at: __submit_bio+0x76/0x230
[ 189.762485]
but task is already holding lock:
[ 189.762494] ffffffff834c97c0 (fs_reclaim){+.+.}-{0:0}, at: balance_pgdat+0xbe/0xb00
[ 189.762507]
which lock already depends on the new lock.
[ 189.762519]
the existing dependency chain (in reverse order) is:
[ 189.762529]
-> #2 (fs_reclaim){+.+.}-{0:0}:
[ 189.762540] fs_reclaim_acquire+0xc5/0x100
[ 189.762548] kmem_cache_alloc_lru_noprof+0x4a/0x480
[ 189.762558] alloc_inode+0xaa/0xe0
[ 189.762566] iget_locked+0x157/0x330
[ 189.762573] kernfs_get_inode+0x1b/0x110
[ 189.762582] kernfs_get_tree+0x1b0/0x2e0
[ 189.762590] sysfs_get_tree+0x1f/0x60
[ 189.762597] vfs_get_tree+0x2a/0xf0
[ 189.762605] path_mount+0x4cd/0xc00
[ 189.762613] __x64_sys_mount+0x119/0x150
[ 189.762621] x64_sys_call+0x14f2/0x2310
[ 189.762630] do_syscall_64+0x91/0x180
[ 189.762637] entry_SYSCALL_64_after_hwframe+0x76/0x7e
[ 189.762647]
-> #1 (&root->kernfs_rwsem){++++}-{3:3}:
[ 189.762659] down_write+0x3e/0xf0
[ 189.762667] kernfs_remove+0x32/0x60
[ 189.762676] sysfs_remove_dir+0x4f/0x60
[ 189.762685] __kobject_del+0x33/0xa0
[ 189.762709] kobject_del+0x13/0x30
[ 189.762716] elv_unregister_queue+0x52/0x80
[ 189.762725] elevator_switch+0x68/0x360
[ 189.762733] elv_iosched_store+0x14b/0x1b0
[ 189.762756] queue_attr_store+0x181/0x1e0
[ 189.762765] sysfs_kf_write+0x49/0x80
[ 189.762773] kernfs_fop_write_iter+0x17d/0x250
[ 189.762781] vfs_write+0x281/0x540
[ 189.762790] ksys_write+0x72/0xf0
[ 189.762798] __x64_sys_write+0x19/0x30
[ 189.762807] x64_sys_call+0x2a3/0x2310
[ 189.762815] do_syscall_64+0x91/0x180
[ 189.762823] entry_SYSCALL_64_after_hwframe+0x76/0x7e
[ 189.762833]
-> #0 (&q->q_usage_counter(io)#26){++++}-{0:0}:
[ 189.762845] __lock_acquire+0x1525/0x2760
[ 189.762854] lock_acquire+0xca/0x310
[ 189.762861] blk_mq_submit_bio+0x8a2/0xba0
[ 189.762870] __submit_bio+0x76/0x230
[ 189.762878] submit_bio_noacct_nocheck+0x323/0x430
[ 189.762888] submit_bio_noacct+0x2cc/0x620
[ 189.762896] submit_bio+0x38/0x110
[ 189.762904] __swap_writepage+0xf5/0x380
[ 189.762912] swap_writepage+0x3c7/0x600
[ 189.762920] shmem_writepage+0x3da/0x4f0
[ 189.762929] pageout+0x13f/0x310
[ 189.762937] shrink_folio_list+0x61c/0xf60
[ 189.763261] evict_folios+0x378/0xcd0
[ 189.763584] try_to_shrink_lruvec+0x1b0/0x360
[ 189.763946] shrink_one+0x10e/0x200
[ 189.764266] shrink_node+0xc02/0x1490
[ 189.764586] balance_pgdat+0x563/0xb00
[ 189.764934] kswapd+0x1e8/0x430
[ 189.765249] kthread+0x10b/0x260
[ 189.765559] ret_from_fork+0x44/0x70
[ 189.765889] ret_from_fork_asm+0x1a/0x30
[ 189.766198]
other info that might help us debug this:
[ 189.767089] Chain exists of:
&q->q_usage_counter(io)#26 --> &root->kernfs_rwsem --> fs_reclaim
[ 189.767971] Possible unsafe locking scenario:
[ 189.768555] CPU0 CPU1
[ 189.768849] ---- ----
[ 189.769136] lock(fs_reclaim);
[ 189.769421] lock(&root->kernfs_rwsem);
[ 189.769714] lock(fs_reclaim);
[ 189.770016] rlock(&q->q_usage_counter(io)#26);
[ 189.770305]
*** DEADLOCK ***
[ 189.771167] 1 lock held by kswapd0/119:
[ 189.771453] #0: ffffffff834c97c0 (fs_reclaim){+.+.}-{0:0}, at: balance_pgdat+0xbe/0xb00
[ 189.771770]
stack backtrace:
[ 189.772351] CPU: 4 UID: 0 PID: 119 Comm: kswapd0 Tainted: G U 6.14.0-rc6-xe+ #6
[ 189.772353] Tainted: [U]=USER
[ 189.772354] Hardware name: ASUS System Product Name/PRIME B560M-A AC, BIOS 2001 02/01/2023
[ 189.772354] Call Trace:
[ 189.772355] <TASK>
[ 189.772356] dump_stack_lvl+0x6e/0xa0
[ 189.772359] dump_stack+0x10/0x18
[ 189.772360] print_circular_bug.cold+0x17a/0x1b7
[ 189.772363] check_noncircular+0x13a/0x150
[ 189.772365] ? __pfx_stack_trace_consume_entry+0x10/0x10
[ 189.772368] __lock_acquire+0x1525/0x2760
[ 189.772368] ? ret_from_fork_asm+0x1a/0x30
[ 189.772371] lock_acquire+0xca/0x310
[ 189.772372] ? __submit_bio+0x76/0x230
[ 189.772375] ? lock_release+0xd5/0x2c0
[ 189.772376] blk_mq_submit_bio+0x8a2/0xba0
[ 189.772378] ? __submit_bio+0x76/0x230
[ 189.772380] __submit_bio+0x76/0x230
[ 189.772382] ? trace_hardirqs_on+0x1e/0xe0
[ 189.772384] submit_bio_noacct_nocheck+0x323/0x430
[ 189.772386] ? submit_bio_noacct_nocheck+0x323/0x430
[ 189.772387] ? __might_sleep+0x58/0xa0
[ 189.772390] submit_bio_noacct+0x2cc/0x620
[ 189.772391] ? count_memcg_events+0x68/0x90
[ 189.772393] submit_bio+0x38/0x110
[ 189.772395] __swap_writepage+0xf5/0x380
[ 189.772396] swap_writepage+0x3c7/0x600
[ 189.772397] shmem_writepage+0x3da/0x4f0
[ 189.772401] pageout+0x13f/0x310
[ 189.772406] shrink_folio_list+0x61c/0xf60
[ 189.772409] ? isolate_folios+0xe80/0x16b0
[ 189.772410] ? mark_held_locks+0x46/0x90
[ 189.772412] evict_folios+0x378/0xcd0
[ 189.772414] ? evict_folios+0x34a/0xcd0
[ 189.772415] ? lock_is_held_type+0xa3/0x130
[ 189.772417] try_to_shrink_lruvec+0x1b0/0x360
[ 189.772420] shrink_one+0x10e/0x200
[ 189.772421] shrink_node+0xc02/0x1490
[ 189.772423] ? shrink_node+0xa08/0x1490
[ 189.772424] ? shrink_node+0xbd8/0x1490
[ 189.772425] ? mem_cgroup_iter+0x366/0x480
[ 189.772427] balance_pgdat+0x563/0xb00
[ 189.772428] ? balance_pgdat+0x563/0xb00
[ 189.772430] ? trace_hardirqs_on+0x1e/0xe0
[ 189.772431] ? finish_task_switch.isra.0+0xcb/0x330
[ 189.772433] ? __switch_to_asm+0x33/0x70
[ 189.772437] kswapd+0x1e8/0x430
[ 189.772438] ? __pfx_autoremove_wake_function+0x10/0x10
[ 189.772440] ? __pfx_kswapd+0x10/0x10
[ 189.772441] kthread+0x10b/0x260
[ 189.772443] ? __pfx_kthread+0x10/0x10
[ 189.772444] ret_from_fork+0x44/0x70
[ 189.772446] ? __pfx_kthread+0x10/0x10
[ 189.772447] ret_from_fork_asm+0x1a/0x30
[ 189.772450] </TASK>
[2]:
[ 8.760253] ======================================================
[ 8.760254] WARNING: possible circular locking dependency detected
[ 8.760255] 6.14.0-rc6-xe+ #7 Tainted: G U
[ 8.760256] ------------------------------------------------------
[ 8.760257] (udev-worker)/674 is trying to acquire lock:
[ 8.760259] ffff888100e39148 (&root->kernfs_rwsem){++++}-{3:3}, at: kernfs_remove+0x32/0x60
[ 8.760265]
but task is already holding lock:
[ 8.760266] ffff888110dc7680 (&q->q_usage_counter(io)#27){++++}-{0:0}, at: blk_mq_freeze_queue_nomemsave+0x12/0x30
[ 8.760272]
which lock already depends on the new lock.
[ 8.760272]
the existing dependency chain (in reverse order) is:
[ 8.760273]
-> #2 (&q->q_usage_counter(io)#27){++++}-{0:0}:
[ 8.760276] blk_alloc_queue+0x30a/0x350
[ 8.760279] blk_mq_alloc_queue+0x6b/0xe0
[ 8.760281] scsi_alloc_sdev+0x276/0x3c0
[ 8.760284] scsi_probe_and_add_lun+0x22a/0x440
[ 8.760286] __scsi_scan_target+0x109/0x230
[ 8.760288] scsi_scan_channel+0x65/0xc0
[ 8.760290] scsi_scan_host_selected+0xff/0x140
[ 8.760292] do_scsi_scan_host+0xa7/0xc0
[ 8.760293] do_scan_async+0x1c/0x160
[ 8.760295] async_run_entry_fn+0x32/0x150
[ 8.760299] process_one_work+0x224/0x5f0
[ 8.760302] worker_thread+0x1d4/0x3e0
[ 8.760304] kthread+0x10b/0x260
[ 8.760306] ret_from_fork+0x44/0x70
[ 8.760309] ret_from_fork_asm+0x1a/0x30
[ 8.760312]
-> #1 (fs_reclaim){+.+.}-{0:0}:
[ 8.760315] fs_reclaim_acquire+0xc5/0x100
[ 8.760317] kmem_cache_alloc_lru_noprof+0x4a/0x480
[ 8.760319] alloc_inode+0xaa/0xe0
[ 8.760322] iget_locked+0x157/0x330
[ 8.760323] kernfs_get_inode+0x1b/0x110
[ 8.760325] kernfs_get_tree+0x1b0/0x2e0
[ 8.760327] sysfs_get_tree+0x1f/0x60
[ 8.760329] vfs_get_tree+0x2a/0xf0
[ 8.760332] path_mount+0x4cd/0xc00
[ 8.760334] __x64_sys_mount+0x119/0x150
[ 8.760336] x64_sys_call+0x14f2/0x2310
[ 8.760338] do_syscall_64+0x91/0x180
[ 8.760340] entry_SYSCALL_64_after_hwframe+0x76/0x7e
[ 8.760342]
-> #0 (&root->kernfs_rwsem){++++}-{3:3}:
[ 8.760345] __lock_acquire+0x1525/0x2760
[ 8.760347] lock_acquire+0xca/0x310
[ 8.760348] down_write+0x3e/0xf0
[ 8.760350] kernfs_remove+0x32/0x60
[ 8.760351] sysfs_remove_dir+0x4f/0x60
[ 8.760353] __kobject_del+0x33/0xa0
[ 8.760355] kobject_del+0x13/0x30
[ 8.760356] elv_unregister_queue+0x52/0x80
[ 8.760358] elevator_switch+0x68/0x360
[ 8.760360] elv_iosched_store+0x14b/0x1b0
[ 8.760362] queue_attr_store+0x181/0x1e0
[ 8.760364] sysfs_kf_write+0x49/0x80
[ 8.760366] kernfs_fop_write_iter+0x17d/0x250
[ 8.760367] vfs_write+0x281/0x540
[ 8.760370] ksys_write+0x72/0xf0
[ 8.760372] __x64_sys_write+0x19/0x30
[ 8.760374] x64_sys_call+0x2a3/0x2310
[ 8.760376] do_syscall_64+0x91/0x180
[ 8.760377] entry_SYSCALL_64_after_hwframe+0x76/0x7e
[ 8.760380]
other info that might help us debug this:
[ 8.760380] Chain exists of:
&root->kernfs_rwsem --> fs_reclaim --> &q->q_usage_counter(io)#27
[ 8.760384] Possible unsafe locking scenario:
[ 8.760384] CPU0 CPU1
[ 8.760385] ---- ----
[ 8.760385] lock(&q->q_usage_counter(io)#27);
[ 8.760387] lock(fs_reclaim);
[ 8.760388] lock(&q->q_usage_counter(io)#27);
[ 8.760390] lock(&root->kernfs_rwsem);
[ 8.760391]
*** DEADLOCK ***
[ 8.760391] 6 locks held by (udev-worker)/674:
[ 8.760392] #0: ffff8881209ac420 (sb_writers#4){.+.+}-{0:0}, at: ksys_write+0x72/0xf0
[ 8.760398] #1: ffff88810c80f488 (&of->mutex#2){+.+.}-{3:3}, at: kernfs_fop_write_iter+0x136/0x250
[ 8.760402] #2: ffff888125d1d330 (kn->active#101){.+.+}-{0:0}, at: kernfs_fop_write_iter+0x13f/0x250
[ 8.760406] #3: ffff888110dc7bb0 (&q->sysfs_lock){+.+.}-{3:3}, at: queue_attr_store+0x148/0x1e0
[ 8.760411] #4: ffff888110dc7680 (&q->q_usage_counter(io)#27){++++}-{0:0}, at: blk_mq_freeze_queue_nomemsave+0x12/0x30
[ 8.760416] #5: ffff888110dc76b8 (&q->q_usage_counter(queue)#27){++++}-{0:0}, at: blk_mq_freeze_queue_nomemsave+0x12/0x30
[ 8.760421]
stack backtrace:
[ 8.760422] CPU: 7 UID: 0 PID: 674 Comm: (udev-worker) Tainted: G U 6.14.0-rc6-xe+ #7
[ 8.760424] Tainted: [U]=USER
[ 8.760425] Hardware name: ASUS System Product Name/PRIME B560M-A AC, BIOS 2001 02/01/2023
[ 8.760426] Call Trace:
[ 8.760427] <TASK>
[ 8.760428] dump_stack_lvl+0x6e/0xa0
[ 8.760431] dump_stack+0x10/0x18
[ 8.760433] print_circular_bug.cold+0x17a/0x1b7
[ 8.760437] check_noncircular+0x13a/0x150
[ 8.760441] ? save_trace+0x54/0x360
[ 8.760445] __lock_acquire+0x1525/0x2760
[ 8.760446] ? irqentry_exit+0x3a/0xb0
[ 8.760448] ? sysvec_apic_timer_interrupt+0x57/0xc0
[ 8.760452] lock_acquire+0xca/0x310
[ 8.760453] ? kernfs_remove+0x32/0x60
[ 8.760457] down_write+0x3e/0xf0
[ 8.760459] ? kernfs_remove+0x32/0x60
[ 8.760460] kernfs_remove+0x32/0x60
[ 8.760462] sysfs_remove_dir+0x4f/0x60
[ 8.760464] __kobject_del+0x33/0xa0
[ 8.760466] kobject_del+0x13/0x30
[ 8.760467] elv_unregister_queue+0x52/0x80
[ 8.760470] elevator_switch+0x68/0x360
[ 8.760472] elv_iosched_store+0x14b/0x1b0
[ 8.760475] queue_attr_store+0x181/0x1e0
[ 8.760479] ? lock_acquire+0xca/0x310
[ 8.760480] ? kernfs_fop_write_iter+0x13f/0x250
[ 8.760482] ? lock_is_held_type+0xa3/0x130
[ 8.760485] sysfs_kf_write+0x49/0x80
[ 8.760487] kernfs_fop_write_iter+0x17d/0x250
[ 8.760489] vfs_write+0x281/0x540
[ 8.760494] ksys_write+0x72/0xf0
[ 8.760497] __x64_sys_write+0x19/0x30
[ 8.760499] x64_sys_call+0x2a3/0x2310
[ 8.760502] do_syscall_64+0x91/0x180
[ 8.760504] ? trace_hardirqs_off+0x5d/0xe0
[ 8.760506] ? handle_softirqs+0x479/0x4d0
[ 8.760508] ? hrtimer_interrupt+0x13f/0x280
[ 8.760511] ? irqentry_exit_to_user_mode+0x8b/0x260
[ 8.760513] ? clear_bhb_loop+0x15/0x70
[ 8.760515] ? clear_bhb_loop+0x15/0x70
[ 8.760516] ? clear_bhb_loop+0x15/0x70
[ 8.760518] entry_SYSCALL_64_after_hwframe+0x76/0x7e
[ 8.760520] RIP: 0033:0x7aa3bf2f5504
[ 8.760522] Code: c7 00 16 00 00 00 b8 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 f3 0f 1e fa 80 3d c5 8b 10 00 00 74 13 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 54 c3 0f 1f 00 55 48 89 e5 48 83 ec 20 48 89
[ 8.760523] RSP: 002b:00007ffc1e3697d8 EFLAGS: 00000202 ORIG_RAX: 0000000000000001
[ 8.760526] RAX: ffffffffffffffda RBX: 0000000000000003 RCX: 00007aa3bf2f5504
[ 8.760527] RDX: 0000000000000003 RSI: 00007ffc1e369ae0 RDI: 000000000000001c
[ 8.760528] RBP: 00007ffc1e369800 R08: 00007aa3bf3f51c8 R09: 00007ffc1e3698b0
[ 8.760528] R10: 0000000000000000 R11: 0000000000000202 R12: 0000000000000003
[ 8.760529] R13: 00007ffc1e369ae0 R14: 0000613ccf21f2f0 R15: 00007aa3bf3f4e80
[ 8.760533] </TASK>
v2:
- Update a code comment to increase readability (Ming Lei).
Cc: Jens Axboe <axboe@kernel.dk>
Cc: linux-block@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20250318095548.5187-1-thomas.hellstrom@linux.intel.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/block/blk-core.c b/block/blk-core.c
index 362d0a55b07a..4623de79effa 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -456,6 +456,12 @@ struct request_queue *blk_alloc_queue(struct queue_limits *lim, int node_id)
lockdep_init_map(&q->q_lockdep_map, "&q->q_usage_counter(queue)",
&q->q_lock_cls_key, 0);
+ /* Teach lockdep about lock ordering (reclaim WRT queue freeze lock). */
+ fs_reclaim_acquire(GFP_KERNEL);
+ rwsem_acquire_read(&q->io_lockdep_map, 0, 0, _RET_IP_);
+ rwsem_release(&q->io_lockdep_map, _RET_IP_);
+ fs_reclaim_release(GFP_KERNEL);
+
q->nr_requests = BLKDEV_DEFAULT_RQ;
return q;
]
|
| 22/2 |
2026/12/01 13:18 |
tool |
git-log |
0m
Args: map[Count:10 PathPrefix:block/elevator.c]
Results: map[Output:603523fd5466 block: only serialize elevator switches of the same queue
4ff58d6bc9dd block: serialize elevator changes for the same queue using a writer lock
ce8ee8583ed8 block: use trylock to avoid lockdep circular dependency in sysfs
f98afe4f31bb blk-mq: add a new queue sysfs attribute async_depth
9869d3a6fed3 block: fix race between wbt_enable_default and IO submission
0315476e78c0 block: use {alloc|free}_sched data methods
04728ce90966 block: move elevator tags into struct elevator_resources
232143b60538 block: unify elevator tags and type xarrays into struct elv_change_ctx
6293e336f6d7 blk-mq-sched: add new parameter nr_requests in blk_mq_alloc_sched_tags()
04225d13aef1 block: fix potential deadlock while running nr_hw_queue update
]
|
| 23/2 |
2026/12/01 13:18 |
tool |
git-log |
0m
Args: map[Count:10 SourcePath:fs/btrfs/file.c SymbolName:btrfs_fallocate]
Results: map[Output:3b1a4a59a208 btrfs: mark dirty extent range for out of bound prealloc extents
8b9d032225be btrfs: remove redundant root argument from btrfs_update_inode()
2a9462de4352 btrfs: convert to ctime accessor functions
9a56fcd15a9c btrfs: make btrfs_update_inode take btrfs_inode
76aea5379678 btrfs: make btrfs_inode_safe_disk_i_size_write take btrfs_inode
d923afe96d7e btrfs: replace all uses of btrfs_ordered_update_i_size
f27451f22996 Btrfs: add support for fallocate's zero range operation
]
|
| 24/2 |
2026/12/01 13:18 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 33931
output: 146
thoughts: 165
Thoughts: **My Exploration of the Commit History**
I'm diving deep into the commit history, starting with a keen interest in `553d28925fde nbd: Reclassify sockets in the reconnect path`. This immediately makes me want to examine `603523fd5466` and `4ff58d6bc9dd` as well, to see the broader context and any related changes. My investigation then takes a turn towards `fs/btrfs/file.c`. I'm particularly curious about the relationship between `btrfs_fallocate` and `btrfs_inode_lock`. It strikes me as odd that `git-log -L` didn't surface `btrfs_inode_lock` when I looked at `btrfs_fallocate`. This could mean a few things: perhaps `btrfs_fallocate` underwent a significant refactoring, or, more likely, `btrfs_inode_lock` was introduced in a commit *before* the range I was initially investigating for `btrfs_fallocate`. To get to the bottom of this, I'll be using `git-blame` on the specific lines (3118-3128) of `fs/btrfs/file.c` to pinpoint exactly when `btrfs_inode_lock` entered the picture and how it relates to the evolution of `btrfs_fallocate`. Ultimately, my plan is to thoroughly inspect all these identified commits and files to build a complete understanding.
|
| 25/2 |
2026/12/01 13:19 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 553d28925fdea4044bad4ae1e2bf9484ad21f758
Author: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>
Date: Thu Nov 12 07:59:39 2026 -0300
nbd: Reclassify sockets in the reconnect path
Commit d532cddb6c60 ("nbd: Reclassify sockets to avoid lockdep circular
dependency") introduced nbd_reclassify_socket() to prevent lockdep false
positives when NBD sockets are used in memory reclaim paths. While
nbd_add_socket() reclassifies newly added sockets,
nbd_genl_reconnect_sock_cb() omitted this call when replacing dead
sockets during reconnection. Consequently, reconnected sockets retain
the generic socket lockdep class and trigger false-positive lockdep
reports.
Fix this by reclassifying the replacement socket in
nbd_genl_reconnect_sock_cb().
Fixes: d532cddb6c60 ("nbd: Reclassify sockets to avoid lockdep circular dependency")
Reported-by: syzbot+d7d72f3d467710442af0@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=d7d72f3d467710442af0
Signed-off-by: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Link: https://patch.msgid.link/20261112105939.260856-1-trintaeoitogc@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index ffce519bf008..b2b27f90a7ed 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1356,6 +1356,7 @@ static int nbd_genl_reconnect_sock_cb(struct nbd_device *nbd, unsigned long arg)
sock = nbd_get_socket(nbd, arg, &err);
if (!sock)
return err;
+ nbd_reclassify_socket(sock);
args = kzalloc_obj(*args);
if (!args) {
]
|
| 26/2 |
2026/12/01 13:19 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 603523fd54664f17630f742229166278568c3ce8
Author: Laveesh Bansal <laveeshb@laveeshbansal.com>
Date: Fri Oct 30 21:41:41 2026 -0700
block: only serialize elevator switches of the same queue
Since commit 4ff58d6bc9dd ("block: serialize elevator changes for the
same queue using a writer lock"), a write to queue/scheduler fails with
-EBUSY whenever something else holds set->update_nr_hwq_lock. That
commit takes the lock for write, and it has to be a trylock because of
commit ce8ee8583ed8 ("block: use trylock to avoid lockdep circular
dependency in sysfs").
The lock covers the whole tag set, so it is easy to hit. A scheduler
write fails while another queue of the set is switching, or while a
sibling disk of the same SCSI host or NVMe controller is added or
removed. This matches the udev failures reported on 7.2.
Go back to taking the lock for read. The write lock was there to keep
two switches of one queue from overlapping in elevator_change_done(),
which touches the elevator sysfs and debugfs entries outside of
q->elevator_lock. Use QUEUE_FLAG_NO_ELV_SWITCH for that instead: a
write sets it before elevator_change() and clears it afterwards, and a
write that finds it set returns -EBUSY. Disk removal already sets the
flag under the write lock, so no switch can start once it has run.
Writes to the same queue, and writes that race with an nr_hw_queues
update, an nr_requests write or disk removal, can still get -EBUSY, as
with 4ff58d6bc9dd. A write to a disk that is being removed now gets
-EBUSY instead of -ENOENT.
Fixes: 4ff58d6bc9dd ("block: serialize elevator changes for the same queue using a writer lock")
Cc: stable@vger.kernel.org # v7.2+
Reported-by: Reindl Harald <h.reindl@thelounge.net>
Closes: https://lore.kernel.org/r/077dd42e-cbc9-4c23-a5ef-3d20e4fc7757@thelounge.net
Suggested-by: Nilay Shroff <nilay@linux.ibm.com>
Assisted-by: LLM
Signed-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>
Reviewed-by: Nilay Shroff <nilay@linux.ibm.com>
Link: https://patch.msgid.link/20261031044141.5569-1-laveeshb@laveeshbansal.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/block/elevator.c b/block/elevator.c
index 2161b6eea680..66edf1a5768a 100644
--- a/block/elevator.c
+++ b/block/elevator.c
@@ -812,25 +812,29 @@ ssize_t elv_iosched_store(struct gendisk *disk, const char *buf,
* reference during concurrent disk deletion:
* update_nr_hwq_lock -> kn->active (via del_gendisk -> kobject_del)
* kn->active -> update_nr_hwq_lock (via this sysfs write path)
- *
- * Use the writer lock instead of the reader lock of update_nr_hwq_lock
- * to serialize the two-stage elevator switch steps in
- * elevator_change(): the core switch step under the elevator lock and
- * the elevator_change_done() step outside the elevator lock.
*/
- if (!down_write_trylock(&set->update_nr_hwq_lock)) {
+ if (!down_read_trylock(&set->update_nr_hwq_lock)) {
ret = -EBUSY;
goto out;
}
- if (!blk_queue_no_elv_switch(q)) {
- ret = elevator_change(q, &ctx);
- if (!ret)
- ret = count;
- } else {
- ret = -ENOENT;
+
+ /*
+ * The reader lock is shared, so use QUEUE_FLAG_NO_ELV_SWITCH to allow
+ * only one switch of this queue at a time, and none once the disk is
+ * going away.
+ */
+ if (test_and_set_bit(QUEUE_FLAG_NO_ELV_SWITCH, &q->queue_flags)) {
+ ret = -EBUSY;
+ goto out_unlock;
}
- up_write(&set->update_nr_hwq_lock);
+ ret = elevator_change(q, &ctx);
+ if (!ret)
+ ret = count;
+
+ blk_queue_flag_clear(QUEUE_FLAG_NO_ELV_SWITCH, q);
+out_unlock:
+ up_read(&set->update_nr_hwq_lock);
out:
if (ctx.type)
elevator_put(ctx.type);
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 0c2010230632..215753171cef 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -678,7 +678,7 @@ enum {
QUEUE_FLAG_HCTX_ACTIVE, /* at least one blk-mq hctx is active */
QUEUE_FLAG_SQ_SCHED, /* single queue style io dispatch */
QUEUE_FLAG_DISABLE_WBT_DEF, /* for sched to disable/enable wbt */
- QUEUE_FLAG_NO_ELV_SWITCH, /* can't switch elevator any more */
+ QUEUE_FLAG_NO_ELV_SWITCH, /* a switch is running or disk is going away */
QUEUE_FLAG_QOS_ENABLED, /* qos is enabled */
QUEUE_FLAG_BIO_ISSUE_TIME, /* record bio->issue_time_ns */
QUEUE_FLAG_ZONED_QD1_WRITES, /* Limit zoned devices writes to QD=1 */
@@ -719,8 +719,6 @@ void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
((q)->limits.features & BLK_FEAT_SKIP_TAGSET_QUIESCE)
#define blk_queue_disable_wbt(q) \
test_bit(QUEUE_FLAG_DISABLE_WBT_DEF, &(q)->queue_flags)
-#define blk_queue_no_elv_switch(q) \
- test_bit(QUEUE_FLAG_NO_ELV_SWITCH, &(q)->queue_flags)
#define blk_queue_zoned_qd1_writes(q) \
test_bit(QUEUE_FLAG_ZONED_QD1_WRITES, &(q)->queue_flags)
]
|
| 27/2 |
2026/12/01 13:19 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 4ff58d6bc9dd4c7fe6092d0ab5677a79ebc788af
Author: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Date: Thu Jul 16 18:22:37 2026 +0900
block: serialize elevator changes for the same queue using a writer lock
When elevator_change() is called concurrently for the same queue, the
elevator_change_done() function runs concurrently as well. This function
adds or deletes kobjects for the debugfs entry of the queue. Then the
concurrent calls cause memory corruption of the kobjects and result in a
process hang. The core part of the elevator switch is protected by queue
freeze and q->elevator_lock. However, since the commit 559dc11143eb
("block: move elv_register[unregister]_queue out of elevator_lock"), the
elevator_change_done() is not serialized. Hence the memory corruption
and the hang.
The failures are observed when udev-worker writes to a sysfs
queue/scheduler attribute file while the blktests test case block/005
writes to the same attribute file. The failure also can be recreated by
running two processes that write to the same queue/scheduler file
concurrently. The failure is observed since another commit 370ac285f23a
("block: avoid cpu_hotplug_lock depedency on freeze_lock"). This commit
changed the behavior of queue freeze and it unveiled the failure.
Fix the failure by changing elv_iosched_store() to acquire
update_nr_hwq_lock as the writer lock instead of the reader lock. This
serializes the whole elevator switch steps, including the
elevator_change_done() call.
Fixes: 559dc11143eb ("block: move elv_register[unregister]_queue out of elevator_lock")
Signed-off-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Reviewed-by: Nilay Shroff <nilay@linux.ibm.com>
Reviewed-by: Ming Lei <tom.leiming@gmail.com>
Link: https://patch.msgid.link/20260716092237.1305030-1-shinichiro.kawasaki@wdc.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/block/elevator.c b/block/elevator.c
index 3bcd37c2aa34..2161b6eea680 100644
--- a/block/elevator.c
+++ b/block/elevator.c
@@ -812,8 +812,13 @@ ssize_t elv_iosched_store(struct gendisk *disk, const char *buf,
* reference during concurrent disk deletion:
* update_nr_hwq_lock -> kn->active (via del_gendisk -> kobject_del)
* kn->active -> update_nr_hwq_lock (via this sysfs write path)
+ *
+ * Use the writer lock instead of the reader lock of update_nr_hwq_lock
+ * to serialize the two-stage elevator switch steps in
+ * elevator_change(): the core switch step under the elevator lock and
+ * the elevator_change_done() step outside the elevator lock.
*/
- if (!down_read_trylock(&set->update_nr_hwq_lock)) {
+ if (!down_write_trylock(&set->update_nr_hwq_lock)) {
ret = -EBUSY;
goto out;
}
@@ -824,7 +829,7 @@ ssize_t elv_iosched_store(struct gendisk *disk, const char *buf,
} else {
ret = -ENOENT;
}
- up_read(&set->update_nr_hwq_lock);
+ up_write(&set->update_nr_hwq_lock);
out:
if (ctx.type)
]
|
| 28/2 |
2026/12/01 13:19 |
tool |
git-blame |
0m
Args: map[End:3135 File:fs/btrfs/file.c Start:3115]
Results: map[Output:2aaa66558172b 3115) if (mode & FALLOC_FL_PUNCH_HOLE)
05fd9564e9faf 3116) return btrfs_punch_hole(file, offset, len);
2aaa66558172b 3117)
29b6352b1494b 3118) btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);
2a162ce93232e 3119)
2a162ce93232e 3120) if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) {
2a162ce93232e 3121) ret = inode_newsize_ok(inode, offset + len);
2a162ce93232e 3122) if (ret)
2a162ce93232e 3123) goto out;
2a162ce93232e 3124) }
2fe17c1075836 3125)
05fd9564e9faf 3126) ret = file_modified(file);
05fd9564e9faf 3127) if (ret)
05fd9564e9faf 3128) goto out;
05fd9564e9faf 3129)
14524a846eb52 3130) /*
14524a846eb52 3131) * TODO: Move these two operations after we have checked
14524a846eb52 3132) * accurate reserved space, or fallocate can still fail but
14524a846eb52 3133) * with page truncated or size expanded.
14524a846eb52 3134) *
14524a846eb52 3135) * But that's a minor problem and won't do much harm BTW.
]
|
| 29/2 |
2026/12/01 13:19 |
tool |
git-show |
0m
Args: map[Commit:HEAD Stat:true]
Results: map[Output:commit 19cc08c1a6a650f8e54c0461ff198be6448f425d
Merge: 95c201c4d3b7 65cf6d500df2
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date: Mon Nov 30 13:12:04 2026 -0800
Merge tag 'clk-fixes-for-linus-v7.4' of git://git.kernel.org/pub/scm/linux/kernel/git/clk/linux
Pull clk fixes from Brian Masney:
"Qualcomm Glymur GCC:
- Use collapse voting for PCIe and PCIe PHY power domains shared with
other masters, including domains enabled by boot firmware
- Retain USB and USB PHY power domains to preserve configuration and
fix USB host controller resume from suspend
Samsung Exynos:
- Properly handle deferred probe of the AudioSubsystem clock
controller
- Fix integration of clock controller and power domains on Exynos5250
and Exynos5420: be sure device runtime resumed before proceeding
with probe and defer probe till the power domain is available"
* tag 'clk-fixes-for-linus-v7.4' of git://git.kernel.org/pub/scm/linux/kernel/git/clk/linux:
clk: qcom: gcc-glymur: use collapse-voting for PCIe GDSCs
clk: qcom: gcc-glymur: Use PWRSTS_RET_ON flag for USB GDSCs
clk: samsung: exynos5-subcmu: Defer probe until power domains are available
clk: samsung: exynos5-subcmu: Use pm_runtime_get_sync() during probe
clk: samsung: exynos-audss: Propagate -EPROBE_DEFER from input clocks
drivers/clk/qcom/gcc-glymur.c | 46 +++++++++++++++++++++++++-------
drivers/clk/samsung/clk-exynos-audss.c | 33 ++++++++++++++++-------
drivers/clk/samsung/clk-exynos5-subcmu.c | 27 ++++++++++++++++---
3 files changed, 83 insertions(+), 23 deletions(-)
]
|
| 30/2 |
2026/12/01 13:19 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 38991
output: 218
thoughts: 1210
Thoughts: **Deconstructing the Lock Dependency Cycle**
Alright, my immediate reaction to seeing this crash report is pure detective mode. I need to peel back every single layer of this dependency chain, starting from the lock that was acquired last and working my way back to see what was held *before* and what might have been *newly introduced* to create this problematic cycle.
My first step is to meticulously trace the provided dependency chain in the crash report. I'm seeing a sequence that starts with `&ei->i_mmap_lock` in `btrfs_page_mkwrite` under `sb_pagefaults`, then moves to `sb_pagefaults#4` in `btrfs_page_mkwrite` during `copy_to_user` in `rng_dev_read`, which then leads me to `&mm->mmap_lock` acquired in `__might_fault` within `tcp_sendmsg_locked` while holding `sk_lock-AF_INET`. This `sk_lock-AF_INET` is acquired in `inet_shutdown` inside `nbd_mark_nsock_dead` within `recv_work`. This is where things get interesting because I'm spotting a potential issue with `recv_work` holding `&nsock->tx_lock`, which is then acquired in `nbd_handle_cmd` / `nbd_queue_rq` while holding `&cmd->lock`. This `&cmd->lock` is acquired in `nbd_queue_rq`, which then brings me to `set->srcu` acquired via `__synchronize_srcu` in `elevator_switch` and its related functions. This, in turn, leads to `&q->elevator_lock` acquired in `elevator_change` / `elv_iosched_store`. From there, I see `&q->q_usage_counter(io)#17` acquired in `blk_mq_submit_bio` within `btrfs_submit_bbio` while holding `btrfs-tree-00`. Finally, I trace `btrfs-tree-00` acquired in `read_block_for_search` while holding `btrfs-tree-01`, which is acquired in `btrfs_search_slot` and ultimately traces back to `btrfs_getxattr` and `btrfs_fallocate`, holding `&ei->i_mmap_lock`.
Now, the critical moment: I realize the chain *loops back* on itself! Specifically, I see the cycle: `#7 -> #6 -> #5 -> #4 -> #3 -> #2 -> #1 -> #0 -> #10 -> #9 -> #8 -> #7`. This is the core problem, and my immediate question is: *why did `nbd` trigger this specific cycle?*
I'm recalling that before commit `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency"), NBD sockets were *always* associated with `sk_lock-AF_INET`. This raises a few possibilities:
1. Why didn't lockdep flag this cycle *before* that commit?
2. Did something else change recently that re-introduced a similar cycle, or did `d532cddb6c60` only address an older issue, and a subsequent commit (`9280e6edf656` in this case) broke that fix?
3. Or, did a change within the `block/`, `btrfs/`, or `nbd.c` subsystems introduce one of the necessary links to form this cycle?
I need to resist the urge to jump to conclusions. The best approach is to meticulously examine each link in the chain.
My specific areas of focus are:
1. **Links #4 -> #5 -> #6 -> #7:**
* I need to delve into link #4, `set->srcu` and its acquisition via `__synchronize_srcu` in `elevator_switch`. I need to investigate when `elevator_set_default`, `elevator_change`, `q->elevator_lock`, `q_usage_counter`, and `set->srcu` were last modified in relation to each other.
* Then, I'll examine link #2 -> #3, the relationship between `&q->q_usage_counter(io)` and `&q->elevator_lock`. Specifically, in `elevator_change` (link #3), why does it depend on `&q->q_usage_counter(io)`? Is it because `elevator_change` freezes the queue (`blk_mq_freeze_queue` acquires `q_usage_counter(io)`) *before* or *after* it acquires `&q->elevator_lock`? I'll be looking closely at `block/elevator.c` around lines 576 and 679 for clues.
2. **Link #0 -> #10:** This is the transition from `&ei->i_mmap_lock` to `btrfs-tree-01`.
* I need to check the recent commit history for `btrfs_fallocate`, specifically around commit `29b6352b1494b` which introduces `btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_MMAP);`.
* I also need to look at commit `05fd9564e9faf` related to `file_modified(file);`. Understanding the timing of these `btrfs` commits is crucial.
3. **Link #7:** This is the `nbd` related part, specifically `nbd_mark_nsock_dead` calling `inet_shutdown` which calls `lock_sock`.
* I'll be poring over `drivers/block/nbd.c` around lines 319 and 1030.
* I also need to perform a `git-log` on `nbd_mark_nsock_dead` and `recv_work` to see their recent history and how they interact.
My plan is to then execute parallel tool calls to simultaneously verify all these specific points. I'm not leaving any stone unturned until this lock cycle is fully understood and resolved.
|
| 31/2 |
2026/12/01 13:19 |
tool |
read-file |
0m
Args: map[File:block/elevator.c FirstLine:560 LineCount:100]
Results: map[Contents: 560: * to restore the old io scheduler, so leaving the io scheduler being none.
561: */
562: static int elevator_switch(struct request_queue *q, struct elv_change_ctx *ctx)
563: {
564: struct elevator_type *new_e = NULL;
565: int ret = 0;
566:
567: WARN_ON_ONCE(q->mq_freeze_depth == 0);
568: lockdep_assert_held(&q->elevator_lock);
569:
570: if (strncmp(ctx->name, "none", 4)) {
571: new_e = elevator_find_get(ctx->name);
572: if (!new_e)
573: return -EINVAL;
574: }
575:
576: blk_mq_quiesce_queue(q);
577:
578: if (q->elevator) {
579: ctx->old = q->elevator;
580: elevator_exit(q);
581: }
582:
583: if (new_e) {
584: ret = blk_mq_init_sched(q, new_e, &ctx->res);
585: if (ret)
586: goto out_unfreeze;
587: ctx->new = q->elevator;
588: } else {
589: blk_queue_flag_clear(QUEUE_FLAG_SQ_SCHED, q);
590: q->elevator = NULL;
591: q->nr_requests = q->tag_set->queue_depth;
592: q->async_depth = q->tag_set->queue_depth;
593: }
594: blk_add_trace_msg(q, "elv switch: %s", ctx->name);
595:
596: out_unfreeze:
597: blk_mq_unquiesce_queue(q);
598:
599: if (ret) {
600: pr_warn("elv: switch to \"%s\" failed, falling back to \"none\"\n",
601: new_e->elevator_name);
602: }
603:
604: if (new_e)
605: elevator_put(new_e);
606: return ret;
607: }
608:
609: static void elv_exit_and_release(struct elv_change_ctx *ctx,
610: struct request_queue *q)
611: {
612: struct elevator_queue *e;
613: unsigned memflags;
614:
615: memflags = blk_mq_freeze_queue(q);
616: mutex_lock(&q->elevator_lock);
617: e = q->elevator;
618: elevator_exit(q);
619: mutex_unlock(&q->elevator_lock);
620: blk_mq_unfreeze_queue(q, memflags);
621: if (e) {
622: blk_mq_free_sched_res(&ctx->res, ctx->type, q->tag_set);
623: kobject_put(&e->kobj);
624: }
625: }
626:
627: static int elevator_change_done(struct request_queue *q,
628: struct elv_change_ctx *ctx)
629: {
630: int ret = 0;
631:
632: if (ctx->old) {
633: struct elevator_resources res = {
634: .et = ctx->old->et,
635: .data = ctx->old->elevator_data
636: };
637:
638: elv_unregister_queue(q, ctx->old);
639: blk_mq_free_sched_res(&res, ctx->old->type, q->tag_set);
640: kobject_put(&ctx->old->kobj);
641: }
642: if (ctx->new) {
643: ret = elv_register_queue(q, ctx->new, !ctx->no_uevent);
644: if (ret)
645: elv_exit_and_release(ctx, q);
646: }
647: return ret;
648: }
649:
650: /*
651: * Switch this queue to the given IO scheduler.
652: */
653: static int elevator_change(struct request_queue *q, struct elv_change_ctx *ctx)
654: {
655: unsigned int memflags;
656: struct blk_mq_tag_set *set = q->tag_set;
657: int ret = 0;
658:
659: lockdep_assert_held(&set->update_nr_hwq_lock);
]
|
| 32/2 |
2026/12/01 13:19 |
tool |
read-file |
0m
Args: map[File:block/elevator.c FirstLine:660 LineCount:100]
Results: map[Contents: 660:
661: if (strncmp(ctx->name, "none", 4)) {
662: ret = blk_mq_alloc_sched_res(q, ctx->type, &ctx->res,
663: set->nr_hw_queues);
664: if (ret)
665: return ret;
666: }
667:
668: memflags = blk_mq_freeze_queue(q);
669: /*
670: * May be called before adding disk, when there isn't any FS I/O,
671: * so freezing queue plus canceling dispatch work is enough to
672: * drain any dispatch activities originated from passthrough
673: * requests, then no need to quiesce queue which may add long boot
674: * latency, especially when lots of disks are involved.
675: *
676: * Disk isn't added yet, so verifying queue lock only manually.
677: */
678: blk_mq_cancel_work_sync(q);
679: mutex_lock(&q->elevator_lock);
680: if (!(q->elevator && elevator_match(q->elevator->type, ctx->name)))
681: ret = elevator_switch(q, ctx);
682: mutex_unlock(&q->elevator_lock);
683: blk_mq_unfreeze_queue(q, memflags);
684: if (!ret)
685: ret = elevator_change_done(q, ctx);
686:
687: /*
688: * Free sched resource if it's allocated but we couldn't switch elevator.
689: */
690: if (!ctx->new)
691: blk_mq_free_sched_res(&ctx->res, ctx->type, set);
692:
693: return ret;
694: }
695:
696: /*
697: * The I/O scheduler depends on the number of hardware queues, this forces a
698: * reattachment when nr_hw_queues changes.
699: */
700: void elv_update_nr_hw_queues(struct request_queue *q,
701: struct elv_change_ctx *ctx)
702: {
703: struct blk_mq_tag_set *set = q->tag_set;
704: int ret = -ENODEV;
705:
706: WARN_ON_ONCE(q->mq_freeze_depth == 0);
707:
708: if (ctx->type && !blk_queue_dying(q) && blk_queue_registered(q)) {
709: mutex_lock(&q->elevator_lock);
710: /* force to reattach elevator after nr_hw_queue is updated */
711: ret = elevator_switch(q, ctx);
712: mutex_unlock(&q->elevator_lock);
713: }
714: blk_mq_unfreeze_queue_nomemrestore(q);
715: if (!ret)
716: WARN_ON_ONCE(elevator_change_done(q, ctx));
717:
718: /*
719: * Free sched resource if it's allocated but we couldn't switch elevator.
720: */
721: if (!ctx->new)
722: blk_mq_free_sched_res(&ctx->res, ctx->type, set);
723: }
724:
725: /*
726: * Use the default elevator settings. If the chosen elevator initialization
727: * fails, fall back to the "none" elevator (no elevator).
728: */
729: void elevator_set_default(struct request_queue *q)
730: {
731: struct elv_change_ctx ctx = {
732: .name = "mq-deadline",
733: .no_uevent = true,
734: };
735: int err;
736:
737: /* now we allow to switch elevator */
738: blk_queue_flag_clear(QUEUE_FLAG_NO_ELV_SWITCH, q);
739:
740: if (q->tag_set->flags & BLK_MQ_F_NO_SCHED_BY_DEFAULT)
741: return;
742:
743: /*
744: * For single queue devices, default to using mq-deadline. If we
745: * have multiple queues or mq-deadline is not available, default
746: * to "none".
747: */
748: ctx.type = elevator_find_get(ctx.name);
749: if (!ctx.type)
750: return;
751:
752: if ((q->nr_hw_queues == 1 ||
753: blk_mq_is_shared_tags(q->tag_set->flags))) {
754: err = elevator_change(q, &ctx);
755: if (err < 0)
756: pr_warn("\"%s\" elevator initialization, failed %d, falling back to \"none\"\n",
757: ctx.name, err);
758: }
759: elevator_put(ctx.type);
]
|
| 33/2 |
2026/12/01 13:19 |
tool |
read-file |
0m
Args: map[File:drivers/block/nbd.c FirstLine:300 LineCount:50]
Results: map[Contents: 300: static int nbd_disconnected(struct nbd_config *config)
301: {
302: return test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags) ||
303: test_bit(NBD_RT_DISCONNECT_REQUESTED, &config->runtime_flags);
304: }
305:
306: static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
307: int notify)
308: {
309: if (!nsock->dead && notify && !nbd_disconnected(nbd->config)) {
310: struct link_dead_args *args;
311: args = kmalloc_obj(struct link_dead_args, GFP_NOIO);
312: if (args) {
313: INIT_WORK(&args->work, nbd_dead_link_work);
314: args->index = nbd->index;
315: queue_work(system_percpu_wq, &args->work);
316: }
317: }
318: if (!nsock->dead) {
319: kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
320: if (atomic_dec_return(&nbd->config->live_connections) == 0) {
321: if (test_and_clear_bit(NBD_RT_DISCONNECT_REQUESTED,
322: &nbd->config->runtime_flags)) {
323: set_bit(NBD_RT_DISCONNECTED,
324: &nbd->config->runtime_flags);
325: dev_info(nbd_to_dev(nbd),
326: "Disconnected due to user request.\n");
327: }
328: }
329: }
330: nsock->dead = true;
331: nsock->pending = NULL;
332: nsock->sent = 0;
333: }
334:
335: static void nbd_apply_limits(struct queue_limits *lim, u32 flags)
336: {
337: lim->features &= ~(BLK_FEAT_WRITE_CACHE | BLK_FEAT_FUA | BLK_FEAT_ROTATIONAL);
338: lim->max_hw_discard_sectors = 0;
339: lim->max_write_zeroes_sectors = 0;
340:
341: if (flags & NBD_FLAG_SEND_TRIM)
342: lim->max_hw_discard_sectors = UINT_MAX >> SECTOR_SHIFT;
343: if (flags & NBD_FLAG_SEND_FLUSH) {
344: lim->features |= BLK_FEAT_WRITE_CACHE;
345: if (flags & NBD_FLAG_SEND_FUA)
346: lim->features |= BLK_FEAT_FUA;
347: }
348:
349: if (flags & NBD_FLAG_ROTATIONAL)
]
|
| 34/2 |
2026/12/01 13:19 |
tool |
read-file |
0m
Args: map[File:drivers/block/nbd.c FirstLine:1010 LineCount:50]
Results: map[Contents:1010: if (IS_ERR(cmd)) {
1011: percpu_ref_put(&q->q_usage_counter);
1012: break;
1013: }
1014:
1015: rq = blk_mq_rq_from_pdu(cmd);
1016: if (likely(!blk_should_fake_timeout(rq->q))) {
1017: bool complete;
1018:
1019: mutex_lock(&cmd->lock);
1020: complete = __test_and_clear_bit(NBD_CMD_INFLIGHT,
1021: &cmd->flags);
1022: mutex_unlock(&cmd->lock);
1023: if (complete)
1024: blk_mq_complete_request(rq);
1025: }
1026: percpu_ref_put(&q->q_usage_counter);
1027: }
1028:
1029: mutex_lock(&nsock->tx_lock);
1030: nbd_mark_nsock_dead(nbd, nsock, 1);
1031: mutex_unlock(&nsock->tx_lock);
1032:
1033: atomic_dec(&config->recv_threads);
1034: wake_up(&config->recv_wq);
1035: nbd_config_put(nbd);
1036: kfree(args);
1037: }
1038:
1039: static bool nbd_clear_req(struct request *req, void *data)
1040: {
1041: struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
1042:
1043: /* don't abort one completed request */
1044: if (blk_mq_request_completed(req))
1045: return true;
1046:
1047: mutex_lock(&cmd->lock);
1048: if (!__test_and_clear_bit(NBD_CMD_INFLIGHT, &cmd->flags)) {
1049: mutex_unlock(&cmd->lock);
1050: return true;
1051: }
1052: cmd->status = BLK_STS_IOERR;
1053: mutex_unlock(&cmd->lock);
1054:
1055: blk_mq_complete_request(req);
1056: return true;
1057: }
1058:
1059: static void nbd_clear_que(struct nbd_device *nbd)
]
|
| 35/2 |
2026/12/01 13:19 |
tool |
git-show |
0m
Args: map[Commit:05fd9564e9faf Stat:true]
Results: map[Output:commit 05fd9564e9faf0f23b4676385e27d9405cef6637
Author: Darrick J. Wong <djwong@kernel.org>
Date: Mon Mar 14 10:55:32 2022 -0700
btrfs: fix fallocate to use file_modified to update permissions consistently
Since the initial introduction of (posix) fallocate back at the turn of
the century, it has been possible to use this syscall to change the
user-visible contents of files. This can happen by extending the file
size during a preallocation, or through any of the newer modes (punch,
zero range). Because the call can be used to change file contents, we
should treat it like we do any other modification to a file -- update
the mtime, and drop set[ug]id privileges/capabilities.
The VFS function file_modified() does all this for us if pass it a
locked inode, so let's make fallocate drop permissions correctly.
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/file.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
]
|
| 36/2 |
2026/12/01 13:19 |
tool |
git-show |
0m
Args: map[Commit:29b6352b1494b Stat:true]
Results: map[Output:commit 29b6352b1494bd7ec6a14bda087f8eb858d2fc1f
Author: David Sterba <dsterba@suse.com>
Date: Thu Oct 27 02:41:32 2022 +0200
btrfs: pass btrfs_inode to btrfs_inode_lock
The function is for internal interfaces so we should use the
btrfs_inode.
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/btrfs_inode.h | 2 +-
fs/btrfs/defrag.c | 4 ++--
fs/btrfs/delayed-inode.c | 2 +-
fs/btrfs/file.c | 16 ++++++++--------
fs/btrfs/inode.c | 14 +++++++-------
fs/btrfs/ioctl.c | 2 +-
fs/btrfs/reflink.c | 2 +-
fs/btrfs/relocation.c | 2 +-
8 files changed, 22 insertions(+), 22 deletions(-)
]
|
| 37/2 |
2026/12/01 13:19 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 45728
output: 211
thoughts: 1560
Thoughts: **My Detective Work on a Lockdep Conundrum**
This is utterly fascinating! I've stumbled upon what appears to be a rather intricate lock dependency cycle, and I'm meticulously tracing its roots. My primary goal is to pinpoint the exact sequence of events and code modifications that led to this unexpected warning.
It all started when I noticed a long-standing commit from 2022 (`05fd9564e9faf`) concerning `btrfs` and `fallocate` that touches on updating permissions. Simultaneously, I'm seeing `btrfs_page_mkwrite` interacting with `sb_start_pagefault` and then `i_mmap_lock` – also seemingly old code. This immediately made me pause and wonder: "Why didn't `lockdep` flag this particular dependency, `btrfs-tree-01` leading through a chain to `&ei->i_mmap_lock`, sooner?"
My investigation then led me to examine the interactions within the block layer, specifically `block/elevator.c`. I'm looking at lines 668-683 where `blk_mq_freeze_queue` is called, followed by acquiring `q->elevator_lock`, and then `blk_mq_quiesce_queue`. The critical observation here is the ordering: `blk_mq_freeze_queue` itself appears to acquire `q->io_lockdep_map`, then the `elevator_lock` is taken, and finally `blk_mq_quiesce_queue` impacts `set->srcu`. So, the sequence seems to be `&q->q_usage_counter(io)` -> `&q->elevator_lock` -> `set->srcu`. My next step is to delve into the history of these specific locking mechanisms (`blk_mq_freeze_queue`, `q->io_lockdep_map`, `q->elevator_lock`, and `set->srcu`) to understand precisely when this ordering was established.
The plot thickened considerably when I turned my attention to `drivers/block/nbd.c`. I've identified two rather recent commits (from 2026, which is quite a jump, but I'm following the data!) that seem highly relevant:
1. `d532cddb6c60` (June 13, 2026): This commit, titled `nbd: Reclassify sockets to avoid lockdep circular dependency`, immediately caught my eye. The rationale behind it suggests that prior to this change, `nbd` sockets were classified under `sk_lock-AF_INET`. This classification was problematic because it created lockdep circular dependencies when an NBD device was used for writeback, swap, or even filesystems. The scenario described is: I/O submission -> NBD -> `sk_lock-AF_INET` (from the userspace side) -> `fs_reclaim` / `mmap_lock` -> `sb_pagefaults` / `i_mmap_lock` / `btrfs-tree` -> and back to I/O submission. This paints a clear picture of a potential loop.
2. However, the real "aha!" moment came with the second commit: `9280e6edf656` (June 22, 2026), `nbd: don't warn when reclassifying a busy socket lock`. This is where things get really interesting. I'm re-examining `d532cddb6c60` and realizing that even *before* `9280e6edf656`, the code in `d532cddb6c60` had a check: `if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) return;`. This means that if `!sock_allow_reclassification(sk)` was true, the function would simply return *without* reclassifying, but it would still issue a `WARN_ON_ONCE`. The commit `9280e6edf656` then *removed* this `WARN_ON_ONCE`, leaving only the `if (!sock_allow_reclassification(sk)) return;` check in place.
This leads me to a critical question: Did `d532cddb6c60` actually fix an *older* lockdep cycle (meaning the bug existed prior to it, or it was an incomplete fix), or did a change *after* or *around* `d532cddb6c60` introduce one of the links in the *current* cycle I'm observing?
To answer this, I'm diving deep into the git history of *all* the identified links in this chain. I'm specifically looking at:
* The `block/elevator.c` code around `blk_mq_freeze_queue`, `q->elevator_lock`, and `blk_mq_quiesce_queue` using `git-log -L` on `elevator_change` and `elevator_switch`.
* The `block/blk-core.c` file concerning `q->io_lockdep_map`, trying to determine when it was initially introduced (likely around commit `ffa1e7ada456` or earlier).
* The `drivers/block/nbd.c` file, focusing on the `nbd_dev_add` path, which leads to `add_disk` and then `blk_register_queue`, eventually calling `elevator_set_default`. This sequence triggers `set->srcu` -> `&cmd->lock` -> `&nsock->tx_lock` -> `sk_lock-AF_INET`. I'm particularly interested in when `nbd_dev_add` / `blk_register_queue` / `elevator_set_default` were modified.
I'm also scrutinizing the more recent commits in `drivers/block/nbd.c`:
* `553d28925fde nbd: Reclassify sockets in the reconnect path` (November 12, 2026)
* `9280e6edf656 nbd: don't warn when reclassifying a busy socket lock` (June 22, 2026)
* `d532cddb6c60 nbd: Reclassify sockets to avoid lockdep circular dependency` (June 13, 2026)
Furthermore, I'm doing a `git-log` on `elevator_change`, `elevator_switch`, `btrfs_fallocate`, `btrfs_page_mkwrite`, and `nbd_mark_nsock_dead` to capture any related historical context. This is a systematic deconstruction, and I'm confident I'll uncover the precise sequence of events leading to this lockdep warning.
|
| 38/2 |
2026/12/01 13:19 |
tool |
git-log |
0m
Args: map[Count:10 SourcePath:block/elevator.c SymbolName:elevator_change]
Results: map[Output:9869d3a6fed3 block: fix race between wbt_enable_default and IO submission
0315476e78c0 block: use {alloc|free}_sched data methods
04728ce90966 block: move elevator tags into struct elevator_resources
f5a6604f7a44 block: fix lockdep warning caused by lock dependency in elv_iosched_store
78c271344b6f block: move wbt_enable_default() out of queue freezing from sched ->exit()
559dc11143eb block: move elv_register[unregister]_queue out of elevator_lock
5dd531a03ad7 block: add function call to switch the IO scheduler from a driver
165125e1e480 [BLOCK] Get rid of request_queue_t typedef
1da177e4c3f4 Linux-2.6.12-rc2
]
|
| 39/2 |
2026/12/01 13:19 |
tool |
git-log |
0m
Args: map[Count:10 SourcePath:block/elevator.c SymbolName:elevator_switch]
Results: map[Output:f98afe4f31bb blk-mq: add a new queue sysfs attribute async_depth
04728ce90966 block: move elevator tags into struct elevator_resources
f5a6604f7a44 block: fix lockdep warning caused by lock dependency in elv_iosched_store
559dc11143eb block: move elv_register[unregister]_queue out of elevator_lock
a3dc6279c2d5 block: pass elevator_queue to elv_register_queue & unregister_queue
1e9db5c42730 block: add `struct elv_change_ctx` for unifying elevator change
596dce110b7d block: simplify elevator reattachment for updating nr_hw_queues
1bb7fba0e262 block: fold elevator_disable into elevator_switch
a11abb98388e block: look up the elevator type in elevator_switch
f8e111c859b9 block: don't call freeze queue in elevator_switch() and elevator_disable()
]
|
| 40/2 |
2026/12/01 13:19 |
tool |
git-log |
0m
Args: map[Count:10 SourcePath:fs/btrfs/file.c SymbolName:btrfs_page_mkwrite]
Results: map[Output:95ee2231896d btrfs: check and set EXTENT_DELALLOC_NEW before clearing EXTENT_DELALLOC
115421e29b84 btrfs: remove folio checked subpage bitmap tracking
37343524f000 btrfs: fix changeset leak on mmap write after failure to reserve metadata
601ea9c42a58 btrfs: use btrfs_inode local variable at btrfs_page_mkwrite()
11ad7983c2ee btrfs: use variable for io_tree when clearing range in btrfs_page_mkwrite()
6599716de2d6 btrfs: fix -ENOSPC mmap write failure on NOCOW files/extents
1ce06d45d922 btrfs: use a single variable to track return value at btrfs_page_mkwrite()
d8cddf2a1d71 btrfs: don't return VM_FAULT_SIGBUS on failure to set delalloc for mmap write
a08625f82506 btrfs: simplify early error checking in btrfs_page_mkwrite()
bf1c74ccba98 btrfs: pass true to btrfs_delalloc_release_space() at btrfs_page_mkwrite()
]
|
| 41/2 |
2026/12/01 13:19 |
tool |
git-log |
0m
Args: map[Count:10 SourcePath:drivers/block/nbd.c SymbolName:nbd_mark_nsock_dead]
Results: map[Output:69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
51723bf92679 drivers/block: replace use of system_wq with system_percpu_wq
ec76a7b922e4 nbd: rename the runtime flags as NBD_RT_ prefixed
5e3c3a7ecefe nbd: clear DISCONNECT_REQUESTED flag once disconnection occurs.
560bc4b39952 nbd: handle dead connections
799f9a38bc9f nbd: multicast dead link notifications
f3733247ae7c nbd: handle single path failures gracefully
]
|
| 42/2 |
2026/12/01 13:19 |
tool |
git-log |
0m
Args: map[Count:10 SourcePath:drivers/block/nbd.c SymbolName:nbd_handle_cmd]
Results: map[Output:957df9af723c nbd: Remove __force casts
e56d4b633fff nbd: Fix signal handling
2a6751e052ab nbd: Improve the documentation of the locking assumptions
3123ac779233 nbd: factor out a helper to get nbd_config without holding 'config_lock'
0de2b7a4dd08 nbd: don't start request if nbd_queue_rq() failed
4e6eef5dc25b nbd: don't handle response without a corresponding request message
d7d94d48a272 nbd: don't requeue the same request twice.
6a468d5990ec nbd: don't start req until after the dead connection logic
2a842acab109 block: introduce new block status code type
1e388ae0b93b nbd: don't use req->errors
]
|
| 43/2 |
2026/12/01 13:19 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 48777
output: 149
thoughts: 174
Thoughts: **Investigating Recent Commits in Block and NBD Drivers**
I need to gain a complete understanding of the recent changes within both the `block/elevator.c` and `drivers/block/nbd.c` files. To achieve this, I'll be meticulously reviewing the commits identified as `f5a6604f7a44` (specifically, "block: fix lockdep warning caused by lock dependency in elv_iosched_store"), `559dc11143eb`, `285908f554fe` ("nbd: remove queue freeze in nbd_add_socket"), `04d8fb23e520` ("nbd: disallow NBD_SET_SOCK on an active device"), and `326d49039c10` ("nbd: add pre_defined_connections module parameter for pre-created devices"). My goal is to ensure I have absolute certainty about the precise modifications made in each of these areas.
|
| 44/2 |
2026/12/01 13:20 |
tool |
git-show |
0m
Args:
Results: map[Output:commit f5a6604f7a4405450e4a1f54e5430f47290c500f
Author: Nilay Shroff <nilay@linux.ibm.com>
Date: Wed Jul 30 13:16:08 2025 +0530
block: fix lockdep warning caused by lock dependency in elv_iosched_store
Recent lockdep reports [1] have revealed a potential deadlock caused by a
lock dependency between the percpu allocator lock and the elevator lock.
This issue can be avoided by ensuring that the allocation and release of
scheduler tags (sched_tags) are performed outside the elevator lock.
Furthermore, the queue does not need to be remain frozen during these
operations.
To address this, move all sched_tags allocations and deallocations outside
of both the ->elevator_lock and the ->freeze_lock. Since the lifetime of
the elevator queue and its associated sched_tags is closely tied, the
allocated sched_tags are now stored in the elevator queue structure. Then,
during the actual elevator switch (which runs under ->freeze_lock and
->elevator_lock), the pre-allocated sched_tags are assigned to the
appropriate q->hctx. Once the elevator switch is complete and the locks
are released, the old elevator queue and its associated sched_tags are
freed.
This commit specifically addresses the allocation/deallocation of sched_
tags during elevator switching. Note that sched_tags may also be allocated
in other contexts, such as during nr_hw_queues updates. Supporting that
use case will require batch allocation/deallocation, which will be handled
in a follow-up patch.
This restructuring ensures that sched_tags memory management occurs
entirely outside of the ->elevator_lock and ->freeze_lock context,
eliminating the lock dependency problem seen during scheduler updates.
[1] https://lore.kernel.org/all/0659ea8d-a463-47c8-9180-43c719e106eb@linux.ibm.com/
Reported-by: Stefan Haberland <sth@linux.ibm.com>
Closes: https://lore.kernel.org/all/0659ea8d-a463-47c8-9180-43c719e106eb@linux.ibm.com/
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
Link: https://lore.kernel.org/r/20250730074614.2537382-3-nilay@linux.ibm.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/block/blk-mq-sched.c b/block/blk-mq-sched.c
index 359e0704e09b..2d6d1ebdd8fb 100644
--- a/block/blk-mq-sched.c
+++ b/block/blk-mq-sched.c
@@ -374,64 +374,17 @@ bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq,
}
EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge);
-static int blk_mq_sched_alloc_map_and_rqs(struct request_queue *q,
- struct blk_mq_hw_ctx *hctx,
- unsigned int hctx_idx)
-{
- if (blk_mq_is_shared_tags(q->tag_set->flags)) {
- hctx->sched_tags = q->sched_shared_tags;
- return 0;
- }
-
- hctx->sched_tags = blk_mq_alloc_map_and_rqs(q->tag_set, hctx_idx,
- q->nr_requests);
-
- if (!hctx->sched_tags)
- return -ENOMEM;
- return 0;
-}
-
-static void blk_mq_exit_sched_shared_tags(struct request_queue *queue)
-{
- blk_mq_free_rq_map(queue->sched_shared_tags);
- queue->sched_shared_tags = NULL;
-}
-
/* called in queue's release handler, tagset has gone away */
static void blk_mq_sched_tags_teardown(struct request_queue *q, unsigned int flags)
{
struct blk_mq_hw_ctx *hctx;
unsigned long i;
- queue_for_each_hw_ctx(q, hctx, i) {
- if (hctx->sched_tags) {
- if (!blk_mq_is_shared_tags(flags))
- blk_mq_free_rq_map(hctx->sched_tags);
- hctx->sched_tags = NULL;
- }
- }
+ queue_for_each_hw_ctx(q, hctx, i)
+ hctx->sched_tags = NULL;
if (blk_mq_is_shared_tags(flags))
- blk_mq_exit_sched_shared_tags(q);
-}
-
-static int blk_mq_init_sched_shared_tags(struct request_queue *queue)
-{
- struct blk_mq_tag_set *set = queue->tag_set;
-
- /*
- * Set initial depth at max so that we don't need to reallocate for
- * updating nr_requests.
- */
- queue->sched_shared_tags = blk_mq_alloc_map_and_rqs(set,
- BLK_MQ_NO_HCTX_IDX,
- MAX_SCHED_RQ);
- if (!queue->sched_shared_tags)
- return -ENOMEM;
-
- blk_mq_tag_update_sched_shared_tags(queue);
-
- return 0;
+ q->sched_shared_tags = NULL;
}
void blk_mq_sched_reg_debugfs(struct request_queue *q)
@@ -458,8 +411,75 @@ void blk_mq_sched_unreg_debugfs(struct request_queue *q)
mutex_unlock(&q->debugfs_mutex);
}
+void blk_mq_free_sched_tags(struct elevator_tags *et,
+ struct blk_mq_tag_set *set)
+{
+ unsigned long i;
+
+ /* Shared tags are stored at index 0 in @tags. */
+ if (blk_mq_is_shared_tags(set->flags))
+ blk_mq_free_map_and_rqs(set, et->tags[0], BLK_MQ_NO_HCTX_IDX);
+ else {
+ for (i = 0; i < et->nr_hw_queues; i++)
+ blk_mq_free_map_and_rqs(set, et->tags[i], i);
+ }
+
+ kfree(et);
+}
+
+struct elevator_tags *blk_mq_alloc_sched_tags(struct blk_mq_tag_set *set,
+ unsigned int nr_hw_queues)
+{
+ unsigned int nr_tags;
+ int i;
+ struct elevator_tags *et;
+ gfp_t gfp = GFP_NOIO | __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
+
+ if (blk_mq_is_shared_tags(set->flags))
+ nr_tags = 1;
+ else
+ nr_tags = nr_hw_queues;
+
+ et = kmalloc(sizeof(struct elevator_tags) +
+ nr_tags * sizeof(struct blk_mq_tags *), gfp);
+ if (!et)
+ return NULL;
+ /*
+ * Default to double of smaller one between hw queue_depth and
+ * 128, since we don't split into sync/async like the old code
+ * did. Additionally, this is a per-hw queue depth.
+ */
+ et->nr_requests = 2 * min_t(unsigned int, set->queue_depth,
+ BLKDEV_DEFAULT_RQ);
+ et->nr_hw_queues = nr_hw_queues;
+
+ if (blk_mq_is_shared_tags(set->flags)) {
+ /* Shared tags are stored at index 0 in @tags. */
+ et->tags[0] = blk_mq_alloc_map_and_rqs(set, BLK_MQ_NO_HCTX_IDX,
+ MAX_SCHED_RQ);
+ if (!et->tags[0])
+ goto out;
+ } else {
+ for (i = 0; i < et->nr_hw_queues; i++) {
+ et->tags[i] = blk_mq_alloc_map_and_rqs(set, i,
+ et->nr_requests);
+ if (!et->tags[i])
+ goto out_unwind;
+ }
+ }
+
+ return et;
+out_unwind:
+ while (--i >= 0)
+ blk_mq_free_map_and_rqs(set, et->tags[i], i);
+out:
+ kfree(et);
+ return NULL;
+}
+
/* caller must have a reference to @e, will grab another one if successful */
-int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e)
+int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e,
+ struct elevator_tags *et)
{
unsigned int flags = q->tag_set->flags;
struct blk_mq_hw_ctx *hctx;
@@ -467,40 +487,33 @@ int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e)
unsigned long i;
int ret;
- /*
- * Default to double of smaller one between hw queue_depth and 128,
- * since we don't split into sync/async like the old code did.
- * Additionally, this is a per-hw queue depth.
- */
- q->nr_requests = 2 * min_t(unsigned int, q->tag_set->queue_depth,
- BLKDEV_DEFAULT_RQ);
-
- eq = elevator_alloc(q, e);
+ eq = elevator_alloc(q, e, et);
if (!eq)
return -ENOMEM;
+ q->nr_requests = et->nr_requests;
+
if (blk_mq_is_shared_tags(flags)) {
- ret = blk_mq_init_sched_shared_tags(q);
- if (ret)
- goto err_put_elevator;
+ /* Shared tags are stored at index 0 in @et->tags. */
+ q->sched_shared_tags = et->tags[0];
+ blk_mq_tag_update_sched_shared_tags(q);
}
queue_for_each_hw_ctx(q, hctx, i) {
- ret = blk_mq_sched_alloc_map_and_rqs(q, hctx, i);
- if (ret)
- goto err_free_map_and_rqs;
+ if (blk_mq_is_shared_tags(flags))
+ hctx->sched_tags = q->sched_shared_tags;
+ else
+ hctx->sched_tags = et->tags[i];
}
ret = e->ops.init_sched(q, eq);
if (ret)
- goto err_free_map_and_rqs;
+ goto out;
queue_for_each_hw_ctx(q, hctx, i) {
if (e->ops.init_hctx) {
ret = e->ops.init_hctx(hctx, i);
if (ret) {
- eq = q->elevator;
- blk_mq_sched_free_rqs(q);
blk_mq_exit_sched(q, eq);
kobject_put(&eq->kobj);
return ret;
@@ -509,10 +522,8 @@ int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e)
}
return 0;
-err_free_map_and_rqs:
- blk_mq_sched_free_rqs(q);
+out:
blk_mq_sched_tags_teardown(q, flags);
-err_put_elevator:
kobject_put(&eq->kobj);
q->elevator = NULL;
return ret;
diff --git a/block/blk-mq-sched.h b/block/blk-mq-sched.h
index 1326526bb733..0cde00cd1c47 100644
--- a/block/blk-mq-sched.h
+++ b/block/blk-mq-sched.h
@@ -18,10 +18,16 @@ void __blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx);
void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx);
-int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e);
+int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e,
+ struct elevator_tags *et);
void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e);
void blk_mq_sched_free_rqs(struct request_queue *q);
+struct elevator_tags *blk_mq_alloc_sched_tags(struct blk_mq_tag_set *set,
+ unsigned int nr_hw_queues);
+void blk_mq_free_sched_tags(struct elevator_tags *et,
+ struct blk_mq_tag_set *set);
+
static inline void blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx)
{
if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
diff --git a/block/elevator.c b/block/elevator.c
index 939b0c590fbe..e9dc837b7b70 100644
--- a/block/elevator.c
+++ b/block/elevator.c
@@ -54,6 +54,8 @@ struct elv_change_ctx {
struct elevator_queue *old;
/* for registering new elevator */
struct elevator_queue *new;
+ /* holds sched tags data */
+ struct elevator_tags *et;
};
static DEFINE_SPINLOCK(elv_list_lock);
@@ -132,7 +134,7 @@ static struct elevator_type *elevator_find_get(const char *name)
static const struct kobj_type elv_ktype;
struct elevator_queue *elevator_alloc(struct request_queue *q,
- struct elevator_type *e)
+ struct elevator_type *e, struct elevator_tags *et)
{
struct elevator_queue *eq;
@@ -145,6 +147,7 @@ struct elevator_queue *elevator_alloc(struct request_queue *q,
kobject_init(&eq->kobj, &elv_ktype);
mutex_init(&eq->sysfs_lock);
hash_init(eq->hash);
+ eq->et = et;
return eq;
}
@@ -165,7 +168,6 @@ static void elevator_exit(struct request_queue *q)
lockdep_assert_held(&q->elevator_lock);
ioc_clear_queue(q);
- blk_mq_sched_free_rqs(q);
mutex_lock(&e->sysfs_lock);
blk_mq_exit_sched(q, e);
@@ -591,7 +593,7 @@ static int elevator_switch(struct request_queue *q, struct elv_change_ctx *ctx)
}
if (new_e) {
- ret = blk_mq_init_sched(q, new_e);
+ ret = blk_mq_init_sched(q, new_e, ctx->et);
if (ret)
goto out_unfreeze;
ctx->new = q->elevator;
@@ -626,8 +628,10 @@ static void elv_exit_and_release(struct request_queue *q)
elevator_exit(q);
mutex_unlock(&q->elevator_lock);
blk_mq_unfreeze_queue(q, memflags);
- if (e)
+ if (e) {
+ blk_mq_free_sched_tags(e->et, q->tag_set);
kobject_put(&e->kobj);
+ }
}
static int elevator_change_done(struct request_queue *q,
@@ -640,6 +644,7 @@ static int elevator_change_done(struct request_queue *q,
&ctx->old->flags);
elv_unregister_queue(q, ctx->old);
+ blk_mq_free_sched_tags(ctx->old->et, q->tag_set);
kobject_put(&ctx->old->kobj);
if (enable_wbt)
wbt_enable_default(q->disk);
@@ -658,9 +663,16 @@ static int elevator_change_done(struct request_queue *q,
static int elevator_change(struct request_queue *q, struct elv_change_ctx *ctx)
{
unsigned int memflags;
+ struct blk_mq_tag_set *set = q->tag_set;
int ret = 0;
- lockdep_assert_held(&q->tag_set->update_nr_hwq_lock);
+ lockdep_assert_held(&set->update_nr_hwq_lock);
+
+ if (strncmp(ctx->name, "none", 4)) {
+ ctx->et = blk_mq_alloc_sched_tags(set, set->nr_hw_queues);
+ if (!ctx->et)
+ return -ENOMEM;
+ }
memflags = blk_mq_freeze_queue(q);
/*
@@ -680,6 +692,11 @@ static int elevator_change(struct request_queue *q, struct elv_change_ctx *ctx)
blk_mq_unfreeze_queue(q, memflags);
if (!ret)
ret = elevator_change_done(q, ctx);
+ /*
+ * Free sched tags if it's allocated but we couldn't switch elevator.
+ */
+ if (ctx->et && !ctx->new)
+ blk_mq_free_sched_tags(ctx->et, set);
return ret;
}
@@ -690,6 +707,7 @@ static int elevator_change(struct request_queue *q, struct elv_change_ctx *ctx)
*/
void elv_update_nr_hw_queues(struct request_queue *q, struct elevator_type *e)
{
+ struct blk_mq_tag_set *set = q->tag_set;
struct elv_change_ctx ctx = {};
int ret = -ENODEV;
@@ -697,15 +715,25 @@ void elv_update_nr_hw_queues(struct request_queue *q, struct elevator_type *e)
if (e && !blk_queue_dying(q) && blk_queue_registered(q)) {
ctx.name = e->elevator_name;
-
+ ctx.et = blk_mq_alloc_sched_tags(set, set->nr_hw_queues);
+ if (!ctx.et) {
+ WARN_ON_ONCE(1);
+ goto unfreeze;
+ }
mutex_lock(&q->elevator_lock);
/* force to reattach elevator after nr_hw_queue is updated */
ret = elevator_switch(q, &ctx);
mutex_unlock(&q->elevator_lock);
}
+unfreeze:
blk_mq_unfreeze_queue_nomemrestore(q);
if (!ret)
WARN_ON_ONCE(elevator_change_done(q, &ctx));
+ /*
+ * Free sched tags if it's allocated but we couldn't switch elevator.
+ */
+ if (ctx.et && !ctx.new)
+ blk_mq_free_sched_tags(ctx.et, set);
}
/*
diff --git a/block/elevator.h b/block/elevator.h
index a4de5f9ad790..adc5c157e17e 100644
--- a/block/elevator.h
+++ b/block/elevator.h
@@ -23,6 +23,15 @@ enum elv_merge {
struct blk_mq_alloc_data;
struct blk_mq_hw_ctx;
+struct elevator_tags {
+ /* num. of hardware queues for which tags are allocated */
+ unsigned int nr_hw_queues;
+ /* depth used while allocating tags */
+ unsigned int nr_requests;
+ /* shared tag is stored at index 0 */
+ struct blk_mq_tags *tags[];
+};
+
struct elevator_mq_ops {
int (*init_sched)(struct request_queue *, struct elevator_queue *);
void (*exit_sched)(struct elevator_queue *);
@@ -113,6 +122,7 @@ struct request *elv_rqhash_find(struct request_queue *q, sector_t offset);
struct elevator_queue
{
struct elevator_type *type;
+ struct elevator_tags *et;
void *elevator_data;
struct kobject kobj;
struct mutex sysfs_lock;
@@ -152,8 +162,8 @@ ssize_t elv_iosched_show(struct gendisk *disk, char *page);
ssize_t elv_iosched_store(struct gendisk *disk, const char *page, size_t count);
extern bool elv_bio_merge_ok(struct request *, struct bio *);
-extern struct elevator_queue *elevator_alloc(struct request_queue *,
- struct elevator_type *);
+struct elevator_queue *elevator_alloc(struct request_queue *,
+ struct elevator_type *, struct elevator_tags *);
/*
* Helper functions.
]
|
| 45/2 |
2026/12/01 13:20 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 04d8fb23e520419a283dd53c1d9cdfb7c5b1705e
Author: Yang Erkun <yangerkun@huawei.com>
Date: Wed Aug 5 20:29:24 2026 +0800
nbd: disallow NBD_SET_SOCK on an active device
We cannot add a socket to an already running nbd device, the reconfigure
for netlink can only active an inactive socket. But for ioctl path, we can
call NBD_SET_SOCK after NBD_DO_IT, reject this using nbd->pid which has
been setted when NBD_DO_IT. Besides, it is the root cause for commit
b98e762e3d71 ("nbd: freeze the queue while we're adding connections").
Reviewed-by: Yu Kuai <yukuai@fygo.io>
Signed-off-by: Yang Erkun <yangerkun@huawei.com>
Link: https://patch.msgid.link/20260805122930.57647-3-yangerkun@huawei.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index b1a5acd57426..7ec85f94f742 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1278,6 +1278,13 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
/* Arg will be cast to int, check it to avoid overflow */
if (arg > INT_MAX)
return -EINVAL;
+
+ if (nbd->pid) {
+ dev_err(disk_to_dev(nbd->disk),
+ "Cannot add socket to a running device\n");
+ return -EBUSY;
+ }
+
sock = nbd_get_socket(nbd, arg, &err);
if (!sock)
return err;
]
|
| 46/2 |
2026/12/01 13:20 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 285908f554fe32d9ca7cb7c3a03a05144faeb461
Author: Yang Erkun <yangerkun@huawei.com>
Date: Wed Aug 5 20:29:26 2026 +0800
nbd: remove queue freeze in nbd_add_socket
nbd_add_socket() kreallocs config->socks, which a concurrent reader in
nbd_handle_cmd() could UAF; commit b98e762e3d71 ("nbd: freeze the queue
while we're adding connections")froze the queue to block that. But the
freeze costs an RCU grace period on every socket added, and setup adds
them one by one.
After the previous patch, nbd_add_socket() is rejected once nbd->pid is
set, so it only runs during setup. There the capacity is 0 and the
write cache is off (cleared on disconnect by the preceding patch, and
re-enabled only later in nbd_set_size), so submit_bio_noacct() rejects
every bio before it reaches the driver -- non-zero-sector ones via
bio_check_eod(), and flush-only ones via the !bdev_write_cache() branch.
No I/O is in flight, so the freeze is unnecessary.
Reviewed-by: Yu Kuai <yukuai@fygo.io>
Signed-off-by: Yang Erkun <yangerkun@huawei.com>
Link: https://patch.msgid.link/20260805122930.57647-5-yangerkun@huawei.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 78df6f459da6..cb7c1f8502f4 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1276,7 +1276,6 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
struct socket *sock;
struct nbd_sock **socks;
struct nbd_sock *nsock;
- unsigned int memflags;
int err;
/* Arg will be cast to int, check it to avoid overflow */
@@ -1294,12 +1293,6 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
return err;
nbd_reclassify_socket(sock);
- /*
- * We need to make sure we don't get any errant requests while we're
- * reallocating the ->socks array.
- */
- memflags = blk_mq_freeze_queue(nbd->disk->queue);
-
if (!netlink && !nbd->task_setup &&
!test_bit(NBD_RT_BOUND, &config->runtime_flags))
nbd->task_setup = current;
@@ -1339,12 +1332,10 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
INIT_WORK(&nsock->work, nbd_pending_cmd_work);
socks[config->num_connections++] = nsock;
atomic_inc(&config->live_connections);
- blk_mq_unfreeze_queue(nbd->disk->queue, memflags);
return 0;
put_socket:
- blk_mq_unfreeze_queue(nbd->disk->queue, memflags);
sockfd_put(sock);
return err;
}
]
|
| 47/2 |
2026/12/01 13:20 |
tool |
git-show |
0m
Args: map[Commit:326d49039c10 Stat:true]
Results: map[Output:commit 326d49039c10b65522ac7277b19b9b5c42ed1aeb
Author: Yang Erkun <yangerkun@huawei.com>
Date: Wed Aug 5 20:29:30 2026 +0800
nbd: add pre_defined_connections module parameter for pre-created devices
blk_mq_update_nr_hw_queues() in nbd_start_device() may cause a queue
freeze. The previous commit addressed this for newly created nbd
devices by setting the expected nr_hw_queues in nbd_dev_add(). However,
when reusing an old inactive nbd device, the queue freeze can still
occur if the old nbd->tag_set->nr_hw_queues does not match the new
socket connection count. Inactive nbd devices can originate from two
sources: loading the nbd module with nbds_max, which sets the default
nr_hw_queues to 1, and the netlink method, which sets nr_hw_queues
according to the expected number of socket connections. For the first
case, add a module parameter so the default nr_hw_queues can be
changed. Users who know their expected number of connections can then
prevent queue freezes on pre-created devices via nbds_max.
Before this patchset:
real 0m2.195s
user 0m0.005s
sys 0m0.022s
After this patchset:
real 0m0.090s
user 0m0.004s
sys 0m0.018s
Signed-off-by: Yang Erkun <yangerkun@huawei.com>
Reviewed-by: Yu Kuai <yukuai@fygo.io>
Link: https://patch.msgid.link/20260805122930.57647-9-yangerkun@huawei.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/block/nbd.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
]
|
| 48/2 |
2026/12/01 13:20 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 9869d3a6fed381f3b98404e26e1afc75d680cbf9
Author: Ming Lei <ming.lei@redhat.com>
Date: Fri Dec 12 22:35:00 2025 +0800
block: fix race between wbt_enable_default and IO submission
When wbt_enable_default() is moved out of queue freezing in elevator_change(),
it can cause the wbt inflight counter to become negative (-1), leading to hung
tasks in the writeback path. Tasks get stuck in wbt_wait() because the counter
is in an inconsistent state.
The issue occurs because wbt_enable_default() could race with IO submission,
allowing the counter to be decremented before proper initialization. This manifests
as:
rq_wait[0]:
inflight: -1
has_waiters: True
rwb_enabled() checks the state, which can be updated exactly between wbt_wait()
(rq_qos_throttle()) and wbt_track()(rq_qos_track()), then the inflight counter
will become negative.
And results in hung task warnings like:
task:kworker/u24:39 state:D stack:0 pid:14767
Call Trace:
rq_qos_wait+0xb4/0x150
wbt_wait+0xa9/0x100
__rq_qos_throttle+0x24/0x40
blk_mq_submit_bio+0x672/0x7b0
...
Fix this by:
1. Splitting wbt_enable_default() into:
- __wbt_enable_default(): Returns true if wbt_init() should be called
- wbt_enable_default(): Wrapper for existing callers (no init)
- wbt_init_enable_default(): New function that checks and inits WBT
2. Using wbt_init_enable_default() in blk_register_queue() to ensure
proper initialization during queue registration
3. Move wbt_init() out of wbt_enable_default() which is only for enabling
disabled wbt from bfq and iocost, and wbt_init() isn't needed. Then the
original lock warning can be avoided.
4. Removing the ELEVATOR_FLAG_ENABLE_WBT_ON_EXIT flag and its handling
code since it's no longer needed
This ensures WBT is properly initialized before any IO can be submitted,
preventing the counter from going negative.
Cc: Nilay Shroff <nilay@linux.ibm.com>
Cc: Yu Kuai <yukuai@fnnas.com>
Cc: Guangwu Zhang <guazhang@redhat.com>
Fixes: 78c271344b6f ("block: move wbt_enable_default() out of queue freezing from sched ->exit()")
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Nilay Shroff <nilay@linux.ibm.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c
index 4a8d3d96bfe4..6e54b1d3d8bc 100644
--- a/block/bfq-iosched.c
+++ b/block/bfq-iosched.c
@@ -7181,7 +7181,7 @@ static void bfq_exit_queue(struct elevator_queue *e)
blk_stat_disable_accounting(bfqd->queue);
blk_queue_flag_clear(QUEUE_FLAG_DISABLE_WBT_DEF, bfqd->queue);
- set_bit(ELEVATOR_FLAG_ENABLE_WBT_ON_EXIT, &e->flags);
+ wbt_enable_default(bfqd->queue->disk);
kfree(bfqd);
}
diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c
index 8684c57498cc..e0a70d26972b 100644
--- a/block/blk-sysfs.c
+++ b/block/blk-sysfs.c
@@ -932,7 +932,7 @@ int blk_register_queue(struct gendisk *disk)
elevator_set_default(q);
blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q);
- wbt_enable_default(disk);
+ wbt_init_enable_default(disk);
/* Now everything is ready and send out KOBJ_ADD uevent */
kobject_uevent(&disk->queue_kobj, KOBJ_ADD);
diff --git a/block/blk-wbt.c b/block/blk-wbt.c
index eb8037bae0bd..0974875f77bd 100644
--- a/block/blk-wbt.c
+++ b/block/blk-wbt.c
@@ -699,7 +699,7 @@ static void wbt_requeue(struct rq_qos *rqos, struct request *rq)
/*
* Enable wbt if defaults are configured that way
*/
-void wbt_enable_default(struct gendisk *disk)
+static bool __wbt_enable_default(struct gendisk *disk)
{
struct request_queue *q = disk->queue;
struct rq_qos *rqos;
@@ -716,19 +716,31 @@ void wbt_enable_default(struct gendisk *disk)
if (enable && RQWB(rqos)->enable_state == WBT_STATE_OFF_DEFAULT)
RQWB(rqos)->enable_state = WBT_STATE_ON_DEFAULT;
mutex_unlock(&disk->rqos_state_mutex);
- return;
+ return false;
}
mutex_unlock(&disk->rqos_state_mutex);
/* Queue not registered? Maybe shutting down... */
if (!blk_queue_registered(q))
- return;
+ return false;
if (queue_is_mq(q) && enable)
- wbt_init(disk);
+ return true;
+ return false;
+}
+
+void wbt_enable_default(struct gendisk *disk)
+{
+ __wbt_enable_default(disk);
}
EXPORT_SYMBOL_GPL(wbt_enable_default);
+void wbt_init_enable_default(struct gendisk *disk)
+{
+ if (__wbt_enable_default(disk))
+ WARN_ON_ONCE(wbt_init(disk));
+}
+
u64 wbt_default_latency_nsec(struct request_queue *q)
{
/*
diff --git a/block/blk-wbt.h b/block/blk-wbt.h
index e5fc653b9b76..925f22475738 100644
--- a/block/blk-wbt.h
+++ b/block/blk-wbt.h
@@ -5,6 +5,7 @@
#ifdef CONFIG_BLK_WBT
int wbt_init(struct gendisk *disk);
+void wbt_init_enable_default(struct gendisk *disk);
void wbt_disable_default(struct gendisk *disk);
void wbt_enable_default(struct gendisk *disk);
@@ -16,6 +17,10 @@ u64 wbt_default_latency_nsec(struct request_queue *);
#else
+static inline void wbt_init_enable_default(struct gendisk *disk)
+{
+}
+
static inline void wbt_disable_default(struct gendisk *disk)
{
}
diff --git a/block/elevator.c b/block/elevator.c
index 5b37ef44f52d..a2f8b2251dc6 100644
--- a/block/elevator.c
+++ b/block/elevator.c
@@ -633,14 +633,10 @@ static int elevator_change_done(struct request_queue *q,
.et = ctx->old->et,
.data = ctx->old->elevator_data
};
- bool enable_wbt = test_bit(ELEVATOR_FLAG_ENABLE_WBT_ON_EXIT,
- &ctx->old->flags);
elv_unregister_queue(q, ctx->old);
blk_mq_free_sched_res(&res, ctx->old->type, q->tag_set);
kobject_put(&ctx->old->kobj);
- if (enable_wbt)
- wbt_enable_default(q->disk);
}
if (ctx->new) {
ret = elv_register_queue(q, ctx->new, !ctx->no_uevent);
diff --git a/block/elevator.h b/block/elevator.h
index a9d092c5a9e8..3eb32516be0b 100644
--- a/block/elevator.h
+++ b/block/elevator.h
@@ -156,7 +156,6 @@ struct elevator_queue
#define ELEVATOR_FLAG_REGISTERED 0
#define ELEVATOR_FLAG_DYING 1
-#define ELEVATOR_FLAG_ENABLE_WBT_ON_EXIT 2
/*
* block elevator interface
]
|
| 49/2 |
2026/12/01 13:20 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 59507
output: 154
thoughts: 1695
Thoughts: **A Deep Dive into the NBD Lockdep Splat: Tracing the Culprits**
Alright, I need to take a step back and meticulously reconstruct the timeline of events that led to this lockdep splat. My initial assumption about the `nbd_reclassify_socket()` commit (`d532cddb6c60`) being the sole cause needs a closer look.
Before `d532cddb6c60`, NBD didn't reclassify sockets at all. The commit message for `d532cddb6c60` states it was to fix a *potential* circular locking dependency reported by syzbot, specifically mentioning `fs_reclaim` being triggered while holding `sk_lock`, which could then lead back to another `sk_lock` if NBD was used for swap or writeback and utilized TLS/TCP. The fix was to reclassify NBD sockets to a separate lock class, acknowledging this was similar to what other network block devices do.
Now, that commit also mentions `ffa1e7ada456` ("block: Make request_queue lockdep splats show up earlier"). This is where things get interesting. `ffa1e7ada456` introduced the priming of `q->io_lockdep_map` with `fs_reclaim` -> `&q->q_usage_counter(io)`. This meant lockdep would now see a path like `fs_reclaim` -> `&q->q_usage_counter(io)` -> ... -> `sk_lock-AF_INET` -> `fs_reclaim`.
But, my current splat doesn't seem to follow that exact `fs_reclaim` path. Let's break down *my* specific splat, step by step, and see when each link in the chain became possible:
* **Link #0 (`&ei->i_mmap_lock` -> `btrfs-tree-01`):** This part of the chain starts with `btrfs_fallocate` holding `btrfs_inode_lock(..., BTRFS_ILOCK_MMAP)`. This lock, which corresponds to `&ei->i_mmap_lock`, was added a long time ago. The critical addition here, for *my* splat, is `file_modified` (introduced in 2022 by `05fd9564e9faf`). `file_modified` calls `btrfs_getxattr`, which then leads to `btrfs-tree-01`. I need to confirm the exact introduction date of `BTRFS_ILOCK_MMAP` acquisition within `btrfs_inode_lock` in the context of `btrfs_fallocate`.
* **Link #1 (`btrfs-tree-01` -> `btrfs-tree-00`):** This is standard Btrfs tree locking, likely not the root cause itself, but a necessary intermediate step.
* **Link #2 (`btrfs-tree-00` -> `&q->q_usage_counter(io)`):** This involves basic Btrfs bio submission. `blk_mq_submit_bio` acquires the `io_lockdep_map`. I need to verify when exactly `io_lockdep_map` was introduced into the block layer for this purpose.
* **Link #3 (`&q->q_usage_counter(io)` -> `&q->elevator_lock`):** Here's a key point. In `elevator_change()`, `blk_mq_freeze_queue(q)` is called *before* acquiring the `mutex_lock(&q->elevator_lock)`. I need to pinpoint when `blk_mq_freeze_queue` was moved to a position *before* acquiring `q->elevator_lock`. I'm looking at commits like `f8e111c859b9` and `git-blame` on `block/elevator.c` around lines 668-683 for this.
* **Link #4 (`&q->elevator_lock` -> `set->srcu`):** `elevator_switch()` then calls `blk_mq_quiesce_queue(q)` while `q->elevator_lock` is still held.
* **Link #5 (`set->srcu` -> `&cmd->lock`):** This path leads through `blk_mq_dispatch_rq_list` to `nbd_queue_rq`.
* **Link #6 (`&cmd->lock` -> `&nsock->tx_lock`):** `nbd_queue_rq` then calls `nbd_handle_cmd`, which acquires `&nsock->tx_lock`.
* **Link #7 (`&nsock->tx_lock` -> `sk_lock-AF_INET`):** This is where the NBD socket interaction comes in. `recv_work` acquires `&nsock->tx_lock`, then `nbd_mark_nsock_dead` is called, leading to `kernel_sock_shutdown` and finally `inet_shutdown`, which acquires `lock_sock`. This is the point where the network socket lock is engaged.
* **Link #8 (`sk_lock-AF_INET` -> `&mm->mmap_lock`):** From the network socket lock, we move to `tcp_sendmsg_locked`, then `_copy_from_iter`, and finally `__might_fault`. This is where the page fault mechanism begins.
* **Link #9 (`&mm->mmap_lock` -> `sb_pagefaults#4`):** The page fault proceeds through `do_user_addr_fault` to `btrfs_page_mkwrite`, which then calls `sb_start_pagefault`.
* **Link #10 (`sb_pagefaults#4` -> `&ei->i_mmap_lock`):** Finally, `btrfs_page_mkwrite` attempts to acquire `down_read(&ei->i_mmap_lock)`, completing the cycle.
So, to summarize my thought process: I'm not just looking at the NBD commit; I'm dissecting the entire lockdep chain. My immediate focus is on pinpointing the introduction or modification of key functions and lock acquisitions that created *this specific sequence of dependencies*. I need to check the history of `io_lockdep_map`'s introduction, the reordering of operations within `elevator_change`, and the specific path that brings Btrfs's `i_mmap_lock` into play with NBD's network socket locks during a page fault.
|
| 50/2 |
2026/12/01 13:20 |
tool |
git-log |
0m
Args: map[CodeRegexp:io_lockdep_map Count:10 PathPrefix:block/]
Results: map[Output:ffa1e7ada456 block: Make request_queue lockdep splats show up earlier
f1be1788a32e block: model freeze & enter queue as lock for supporting lockdep
]
|
| 51/2 |
2026/12/01 13:20 |
tool |
git-blame |
0m
Args: map[End:690 File:block/elevator.c Start:660]
Results: map[Output:f5a6604f7a440 660)
f5a6604f7a440 661) if (strncmp(ctx->name, "none", 4)) {
0315476e78c05 662) ret = blk_mq_alloc_sched_res(q, ctx->type, &ctx->res,
0315476e78c05 663) set->nr_hw_queues);
04728ce90966c 664) if (ret)
04728ce90966c 665) return ret;
f5a6604f7a440 666) }
20117b5a4b9c6 667)
20117b5a4b9c6 668) memflags = blk_mq_freeze_queue(q);
1e44bedbc921a 669) /*
1e44bedbc921a 670) * May be called before adding disk, when there isn't any FS I/O,
1e44bedbc921a 671) * so freezing queue plus canceling dispatch work is enough to
1e44bedbc921a 672) * drain any dispatch activities originated from passthrough
1e44bedbc921a 673) * requests, then no need to quiesce queue which may add long boot
1e44bedbc921a 674) * latency, especially when lots of disks are involved.
1e44bedbc921a 675) *
1e44bedbc921a 676) * Disk isn't added yet, so verifying queue lock only manually.
1e44bedbc921a 677) */
1e44bedbc921a 678) blk_mq_cancel_work_sync(q);
20117b5a4b9c6 679) mutex_lock(&q->elevator_lock);
1e9db5c42730e 680) if (!(q->elevator && elevator_match(q->elevator->type, ctx->name)))
1e9db5c42730e 681) ret = elevator_switch(q, ctx);
20117b5a4b9c6 682) mutex_unlock(&q->elevator_lock);
20117b5a4b9c6 683) blk_mq_unfreeze_queue(q, memflags);
559dc11143eb4 684) if (!ret)
559dc11143eb4 685) ret = elevator_change_done(q, ctx);
0315476e78c05 686)
f5a6604f7a440 687) /*
04728ce90966c 688) * Free sched resource if it's allocated but we couldn't switch elevator.
f5a6604f7a440 689) */
04728ce90966c 690) if (!ctx->new)
]
|
| 52/2 |
2026/12/01 13:20 |
tool |
git-show |
0m
Args: map[Commit:f8e111c859b9 Stat:true]
Results: map[Output:commit f8e111c859b92ee909f1676f90c791e7165d3860
Author: Ming Lei <ming.lei@redhat.com>
Date: Mon May 5 22:17:41 2025 +0800
block: don't call freeze queue in elevator_switch() and elevator_disable()
Both elevator_switch() and elevator_disable() are only called from the
two code paths, in which queue is guaranteed to be frozen.
So don't call freeze queue in the two functions, also add asserts for
queue freeze.
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Reviewed-by: Nilay Shroff <nilay@linux.ibm.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Yu Kuai <yukuai3@huawei.com>
Link: https://lore.kernel.org/r/20250505141805.2751237-4-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/elevator.c | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)
]
|
| 53/2 |
2026/12/01 13:20 |
tool |
git-log |
0m
Args: map[CodeRegexp:BTRFS_ILOCK_MMAP Count:10 PathPrefix:fs/btrfs/file.c]
Results: map[Output:e0391e92f9ab btrfs: fix double inode unlock for direct IO sync writes
939b656bc8ab btrfs: fix corruption after buffer fault in during direct IO append write
56b7169f691c btrfs: use a btrfs_inode local variable at btrfs_sync_file()
e5d4d75bd324 btrfs: pass btrfs_inode to btrfs_inode_unlock
29b6352b1494 btrfs: pass btrfs_inode to btrfs_inode_lock
bd6526d0df0f btrfs: lock the inode first before flushing range when punching hole
885f46d87f29 btrfs: fix race between memory mapped writes and fsync
8d9b4a162a37 btrfs: exclude mmap from happening during all fallocate operations
]
|
| 54/2 |
2026/12/01 13:20 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 62902
output: 103
thoughts: 48
Thoughts: **Examining Commit History for Lockdep Support in Block Layer Freezing**
My current focus is on understanding how lockdep support was integrated into the block layer's freeze and queuing mechanisms. I'm specifically reviewing two commits: `f1be1788a32e` titled "block: model freeze & enter queue as lock for supporting lockdep" and `20117b5a4b9c6`.
My thought process here is to trace the evolution of this functionality. The title of the first commit strongly suggests that it introduced changes to the way the block layer handles freezing and queueing, specifically by treating these operations *as* a lock. This is a critical insight, as it implies the intention was to make these states visible to lockdep, the kernel's lock dependency validator. Lockdep relies on understanding lock acquisition and release patterns to detect potential deadlocks or incorrect lock usage. By modeling freeze and queueing as locks, the system can then check for inconsistencies when other locks are manipulated concurrently with these states.
The second commit, `20117b5a4b9c6`, likely represents either a follow-up or a related change that complements the first. It's probable that `f1be1788a32e` laid the groundwork or introduced the core mechanism, and `20117b5a4b9c6` might have refined it, addressed edge cases, or perhaps performed a related refactoring that also benefits from or necessitates lockdep integration. I'll be looking for how these commits modify the relevant block layer subsystems, such as the `blk_freeze_queue` and `blk_enter_queue` functions, and how they interact with lockdep's primitives (e.g., `lockdep_register_lock`, `lockdep_acquire`, `lockdep_release`, or similar constructs).
Ultimately, my goal in examining these commits is to ascertain the exact implementation details of this lock-like behavior for block freezing and queuing, and to confirm how this contributes to robust lock dependency analysis within the kernel's block I/O subsystem.
|
| 55/2 |
2026/12/01 13:20 |
tool |
git-show |
0m
Args: map[Commit:f1be1788a32e Stat:true]
Results: map[Output:commit f1be1788a32e8fa63416ad4518bbd1a85a825c9d
Author: Ming Lei <ming.lei@redhat.com>
Date: Fri Oct 25 08:37:20 2024 +0800
block: model freeze & enter queue as lock for supporting lockdep
Recently we got several deadlock report[1][2][3] caused by
blk_mq_freeze_queue and blk_enter_queue().
Turns out the two are just like acquiring read/write lock, so model them
as read/write lock for supporting lockdep:
1) model q->q_usage_counter as two locks(io and queue lock)
- queue lock covers sync with blk_enter_queue()
- io lock covers sync with bio_enter_queue()
2) make the lockdep class/key as per-queue:
- different subsystem has very different lock use pattern, shared lock
class causes false positive easily
- freeze_queue degrades to no lock in case that disk state becomes DEAD
because bio_enter_queue() won't be blocked any more
- freeze_queue degrades to no lock in case that request queue becomes dying
because blk_enter_queue() won't be blocked any more
3) model blk_mq_freeze_queue() as acquire_exclusive & try_lock
- it is exclusive lock, so dependency with blk_enter_queue() is covered
- it is trylock because blk_mq_freeze_queue() are allowed to run
concurrently
4) model blk_enter_queue() & bio_enter_queue() as acquire_read()
- nested blk_enter_queue() are allowed
- dependency with blk_mq_freeze_queue() is covered
- blk_queue_exit() is often called from other contexts(such as irq), and
it can't be annotated as lock_release(), so simply do it in
blk_enter_queue(), this way still covered cases as many as possible
With lockdep support, such kind of reports may be reported asap and
needn't wait until the real deadlock is triggered.
For example, lockdep report can be triggered in the report[3] with this
patch applied.
[1] occasional block layer hang when setting 'echo noop > /sys/block/sda/queue/scheduler'
https://bugzilla.kernel.org/show_bug.cgi?id=219166
[2] del_gendisk() vs blk_queue_enter() race condition
https://lore.kernel.org/linux-block/20241003085610.GK11458@google.com/
[3] queue_freeze & queue_enter deadlock in scsi
https://lore.kernel.org/linux-block/ZxG38G9BuFdBpBHZ@fedora/T/#u
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20241025003722.3630252-4-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/blk-core.c | 18 ++++++++++++++++--
block/blk-mq.c | 26 ++++++++++++++++++++++----
block/blk.h | 29 ++++++++++++++++++++++++++---
block/genhd.c | 15 +++++++++++----
include/linux/blkdev.h | 6 ++++++
5 files changed, 81 insertions(+), 13 deletions(-)
]
|
| 56/2 |
2026/12/01 13:20 |
tool |
git-show |
0m
Args: map[Commit:20117b5a4b9c6 Stat:true]
Results: map[Output:commit 20117b5a4b9c6dbb9414f0451111c3f13a37874a
Author: Ming Lei <ming.lei@redhat.com>
Date: Mon May 5 22:17:52 2025 +0800
block: move queue freezing & elevator_lock into elevator_change()
Move queue freezing & elevator_lock into elevator_change(), and prepare
for using elevator_change() for setting up & tearing down default elevator
too.
Also add lockdep_assert_held() in __elevator_change() because either
read or write lock is required for changing elevator.
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Reviewed-by: Nilay Shroff <nilay@linux.ibm.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20250505141805.2751237-15-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/elevator.c | 20 ++++++++++++--------
1 file changed, 12 insertions(+), 8 deletions(-)
]
|
| 57/2 |
2026/12/01 13:20 |
tool |
git-show |
0m
Args: map[Commit:8d9b4a162a37 Stat:true]
Results: map[Output:commit 8d9b4a162a37cee384e2d872f3673be386351e2d
Author: Josef Bacik <josef@toxicpanda.com>
Date: Wed Feb 10 17:14:36 2021 -0500
btrfs: exclude mmap from happening during all fallocate operations
There's a small window where a deadlock can happen between fallocate and
mmap. This is described in detail by Filipe:
"""
When doing a fallocate operation we lock the inode, flush delalloc within
the target range, wait for any ordered extents to complete and then lock
the file range. Before we lock the range and after we flush delalloc,
there is a time window where another task can come in and do a memory
mapped write for a page within the fallocate range.
This means that after fallocate locks the range, there can be a dirty page
in the range. More often than not, this does not cause any problem.
The exception is when we are low on available metadata space, because an
fallocate operation needs to start a transaction while holding the file
range locked, either through btrfs_prealloc_file_range() or through the
call to btrfs_fallocate_update_isize(). If that's the case, we can end up
in a deadlock. The following list of steps explains how that happens:
1) A fallocate operation starts, locks the inode, flushes delalloc in the
range and waits for ordered extents in the range to complete;
2) Before the fallocate task locks the file range, another task does a
memory mapped write for a page in the fallocate target range. This is
possible since memory mapped writes do not (and can not) lock the
inode;
3) The fallocate task locks the file range. At this point there is one
dirty page in the range (due to the memory mapped write);
4) When the fallocate task attempts to start a transaction, it blocks when
attempting to reserve metadata space, since we are low on available
metadata space. Before blocking (wait on its reservation ticket), it
starts the async reclaim task (if not running already);
5) The async reclaim task is not able to release space through any other
means, so it decides to flush delalloc for inodes with dirty pages.
It finds that the inode used in the fallocate operation has a dirty
page and therefore queues a job (fs_info->flush_workers workqueue) to
flush delalloc for that inode and waits on that job to complete;
6) The flush job blocks when attempting to lock the file range because
it is currently locked by the fallocate task;
7) The fallocate task keeps waiting for its metadata reservation, waiting
for a wakeup on its reservation ticket. The async reclaim task is
waiting on the flush job, which in turn is waiting for locking the file
range that is currently locked by the fallocate task. So unless some
other task is able to release enough metadata space, for example an
ordered extent for some other inode completes, we end up in a deadlock
between all these tasks.
When this happens stack traces like the following show up in dmesg/syslog:
INFO: task kworker/u16:11:1810830 blocked for more than 120 seconds.
Tainted: G B W 5.10.0-rc4-btrfs-next-73 #1
"echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.
task:kworker/u16:11 state:D stack: 0 pid:1810830 ppid: 2 flags:0x00004000
Workqueue: btrfs-flush_delalloc btrfs_work_helper [btrfs]
Call Trace:
__schedule+0x5d1/0xcf0
schedule+0x45/0xe0
lock_extent_bits+0x1e6/0x2d0 [btrfs]
? finish_wait+0x90/0x90
btrfs_invalidatepage+0x32c/0x390 [btrfs]
? __mod_memcg_state+0x8e/0x160
__extent_writepage+0x2d4/0x400 [btrfs]
extent_write_cache_pages+0x2b2/0x500 [btrfs]
? lock_release+0x20e/0x4c0
? trace_hardirqs_on+0x1b/0xf0
extent_writepages+0x43/0x90 [btrfs]
? lock_acquire+0x1a3/0x490
do_writepages+0x43/0xe0
? __filemap_fdatawrite_range+0xa4/0x100
__filemap_fdatawrite_range+0xc5/0x100
btrfs_run_delalloc_work+0x17/0x40 [btrfs]
btrfs_work_helper+0xf1/0x600 [btrfs]
process_one_work+0x24e/0x5e0
worker_thread+0x50/0x3b0
? process_one_work+0x5e0/0x5e0
kthread+0x153/0x170
? kthread_mod_delayed_work+0xc0/0xc0
ret_from_fork+0x22/0x30
INFO: task kworker/u16:1:2426217 blocked for more than 120 seconds.
Tainted: G B W 5.10.0-rc4-btrfs-next-73 #1
"echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.
task:kworker/u16:1 state:D stack: 0 pid:2426217 ppid: 2 flags:0x00004000
Workqueue: events_unbound btrfs_async_reclaim_metadata_space [btrfs]
Call Trace:
__schedule+0x5d1/0xcf0
? kvm_clock_read+0x14/0x30
? wait_for_completion+0x81/0x110
schedule+0x45/0xe0
schedule_timeout+0x30c/0x580
? _raw_spin_unlock_irqrestore+0x3c/0x60
? lock_acquire+0x1a3/0x490
? try_to_wake_up+0x7a/0xa20
? lock_release+0x20e/0x4c0
? lock_acquired+0x199/0x490
? wait_for_completion+0x81/0x110
wait_for_completion+0xab/0x110
start_delalloc_inodes+0x2af/0x390 [btrfs]
btrfs_start_delalloc_roots+0x12d/0x250 [btrfs]
flush_space+0x24f/0x660 [btrfs]
btrfs_async_reclaim_metadata_space+0x1bb/0x480 [btrfs]
process_one_work+0x24e/0x5e0
worker_thread+0x20f/0x3b0
? process_one_work+0x5e0/0x5e0
kthread+0x153/0x170
? kthread_mod_delayed_work+0xc0/0xc0
ret_from_fork+0x22/0x30
(...)
several tasks waiting for the inode lock held by the fallocate task below
(...)
RIP: 0033:0x7f61efe73fff
Code: Unable to access opcode bytes at RIP 0x7f61efe73fd5.
RSP: 002b:00007ffc3371bbe8 EFLAGS: 00000202 ORIG_RAX: 000000000000013c
RAX: ffffffffffffffda RBX: 00007ffc3371bea0 RCX: 00007f61efe73fff
RDX: 00000000ffffff9c RSI: 0000560fbd5d90a0 RDI: 00000000ffffff9c
RBP: 00007ffc3371beb0 R08: 0000000000000001 R09: 0000000000000003
R10: 0000560fbd5d7ad0 R11: 0000000000000202 R12: 0000000000000001
R13: 000000000000005e R14: 00007ffc3371bea0 R15: 00007ffc3371beb0
task:fdm-stress state:D stack: 0 pid:2508243 ppid:2508153 flags:0x00000000
Call Trace:
__schedule+0x5d1/0xcf0
? _raw_spin_unlock_irqrestore+0x3c/0x60
schedule+0x45/0xe0
__reserve_bytes+0x4a4/0xb10 [btrfs]
? finish_wait+0x90/0x90
btrfs_reserve_metadata_bytes+0x29/0x190 [btrfs]
btrfs_block_rsv_add+0x1f/0x50 [btrfs]
start_transaction+0x2d1/0x760 [btrfs]
btrfs_replace_file_extents+0x120/0x930 [btrfs]
? btrfs_fallocate+0xdcf/0x1260 [btrfs]
btrfs_fallocate+0xdfb/0x1260 [btrfs]
? filename_lookup+0xf1/0x180
vfs_fallocate+0x14f/0x440
ioctl_preallocate+0x92/0xc0
do_vfs_ioctl+0x66b/0x750
? __do_sys_newfstat+0x53/0x60
__x64_sys_ioctl+0x62/0xb0
do_syscall_64+0x33/0x80
entry_SYSCALL_64_after_hwframe+0x44/0xa9
"""
Fix this by disallowing mmaps from happening while we're doing any of
the fallocate operations on this inode.
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/file.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
]
|
| 58/2 |
2026/12/01 13:20 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 67288
output: 155
thoughts: 1388
Thoughts: **Deep Dive into Lock Dependencies and Potential Circular Locking in Btrfs/NBD Interactions**
My initial thought is to meticulously examine every link in the lock acquisition chain, differentiating between established patterns and more recent introductions. I need to scrutinize both the Network Block Device (NBD) side and the fundamental block layer.
First, I'm focusing on a specific sequence within Btrfs: `&ei->i_mmap_lock` -> `btrfs-tree-01` -> `btrfs-tree-00` -> `&q->q_usage_counter(io)`. This pattern has a history, dating back to at least October 2024, and even earlier in 2022 and 2021. I need to confirm the validity of this chain. It seems plausible, as `fallocate` acquires `i_mmap_lock` and subsequently interacts with Btrfs metadata, which in turn submits bios to the block layer. Similarly, `btrfs_page_mkwrite` uses `sb_pagefaults` which correctly locks `i_mmap_lock` using `filemap_invalidate_lock_shared`. The sequence `tcp_sendmsg_locked` -> `sk_lock-AF_INET` -> `&mm->mmap_lock` via `__might_fault` within `copy_from_iter` is also standard, as is `do_user_addr_fault` -> `btrfs_page_mkwrite` which involves `&mm->mmap_lock` -> `sb_pagefaults`.
Next, I'm turning my attention to the block layer and NBD side of things: `&q->q_usage_counter(io)` -> `&q->elevator_lock` -> `set->srcu` -> `&cmd->lock` -> `&nsock->tx_lock` -> `sk_lock-AF_INET`.
Digging deeper into `&q->q_usage_counter(io)` -> `&q->elevator_lock` -> `set->srcu`: I recall that during `elevator_change`, `blk_mq_freeze_queue(q)` is called, which locks `q->elevator_lock` and then calls `elevator_switch(q, ctx)` leading to `blk_mq_quiesce_queue(q)` which involves `synchronize_srcu(&set->srcu)`. This sequence appears to be acquired under `&q->elevator_lock` when `nbd_dev_add` is executed (`nbd_init` -> `nbd_dev_add` -> `add_disk` -> `blk_register_queue` -> `elevator_set_default` -> `elevator_change` -> `elevator_switch` -> `blk_mq_quiesce_queue`), and this all happens under `q->q_usage_counter(io)`. I need to verify when `blk_mq_quiesce_queue` was introduced within `elevator_switch` or `elevator_set_default` – it's likely an older component.
Now, for the `set->srcu` -> `&cmd->lock` -> `&nsock->tx_lock` -> `sk_lock-AF_INET` part: In `nbd_queue_rq()`, `blk_mq_dispatch_rq_list` holds a `srcu_read_lock(set->srcu)` and then calls `nbd_queue_rq()`, which proceeds to `&cmd->lock` and then `&nsock->tx_lock`. Crucially, under `&nsock->tx_lock`, NBD operations invoke `sock_sendmsg` or `kernel_sock_shutdown`, both of which acquire `sk_lock`.
My current critical question is: *Why was the `sk_lock` in NBD originally classified as `sk_lock-AF_INET` and not something more specific like `sk_lock-AF_INET-NBD`?*
This leads me to investigate two key commits in `drivers/block/nbd.c`:
* `d532cddb6c60` (June 13, 2026): "nbd: Reclassify sockets to avoid lockdep circular dependency"
* `9280e6edf656` (June 22, 2026): "nbd: don't warn when reclassifying a busy socket lock"
* `553d28925fde` (November 12, 2026): "nbd: Reclassify sockets in the reconnect path"
The crucial realization is that *before* `d532cddb6c60` (June 13, 2026), NBD *never* reclassified its sockets. This means every NBD socket was *always* tagged as `sk_lock-AF_INET`.
This brings me to a pivotal point: *Could this circular locking dependency have existed *before* `d532cddb6c60`?* I need to verify if all 10 links in the identified cycle were present *prior* to that commit, or if one of the links was introduced *after*, or if `d532cddb6c60`, `9280e6edf656`, and `553d28925fde` themselves represent the bug or an incomplete fix.
My next step is to meticulously review *all* the links in this cycle against the state of the kernel *before* `d532cddb6c60` (June 13, 2026). This involves using `git blame` on relevant sections of `block/elevator.c` (lines 562-580), `drivers/block/nbd.c` (around `nbd_dev_add` and `nbd_queue_rq`), and `fs/btrfs/` (around `btrfs_page_mkwrite` and `btrfs_fallocate`) to understand the history of each lock acquisition point.
|
| 59/2 |
2026/12/01 13:20 |
tool |
git-blame |
0m
Args: map[End:585 File:block/elevator.c Start:562]
Results: map[Output:1e9db5c42730e block/elevator.c 562) static int elevator_switch(struct request_queue *q, struct elv_change_ctx *ctx)
^1da177e4c3f4 drivers/block/elevator.c 563) {
1bb7fba0e262e block/elevator.c 564) struct elevator_type *new_e = NULL;
1bb7fba0e262e block/elevator.c 565) int ret = 0;
^1da177e4c3f4 drivers/block/elevator.c 566)
f8e111c859b92 block/elevator.c 567) WARN_ON_ONCE(q->mq_freeze_depth == 0);
1bf70d08cc3b5 block/elevator.c 568) lockdep_assert_held(&q->elevator_lock);
14a23498ba976 block/elevator.c 569)
1e9db5c42730e block/elevator.c 570) if (strncmp(ctx->name, "none", 4)) {
1e9db5c42730e block/elevator.c 571) new_e = elevator_find_get(ctx->name);
1bb7fba0e262e block/elevator.c 572) if (!new_e)
1bb7fba0e262e block/elevator.c 573) return -EINVAL;
1bb7fba0e262e block/elevator.c 574) }
a11abb98388e2 block/elevator.c 575)
a1ce35fa49852 block/elevator.c 576) blk_mq_quiesce_queue(q);
4722dc52a891a block/elevator.c 577)
64b36075eb0e5 block/elevator.c 578) if (q->elevator) {
559dc11143eb4 block/elevator.c 579) ctx->old = q->elevator;
64b36075eb0e5 block/elevator.c 580) elevator_exit(q);
64b36075eb0e5 block/elevator.c 581) }
64b36075eb0e5 block/elevator.c 582)
1bb7fba0e262e block/elevator.c 583) if (new_e) {
04728ce90966c block/elevator.c 584) ret = blk_mq_init_sched(q, new_e, &ctx->res);
1bb7fba0e262e block/elevator.c 585) if (ret)
]
|
| 60/2 |
2026/12/01 13:21 |
tool |
git-blame |
0m
Args: map[End:1215 File:drivers/block/nbd.c Start:1135]
Results: map[Output:a897b6664e956 1135) dev_err_ratelimited(disk_to_dev(nbd->disk),
a897b6664e956 1136) "Attempted send on invalid socket\n");
5ea8d10802ec4 1137) nbd_config_put(nbd);
e56d4b633fffe 1138) return BLK_STS_IOERR;
9561a7ade0c20 1139) }
2a842acab109f 1140) cmd->status = BLK_STS_OK;
f3733247ae7c5 1141) again:
5ea8d10802ec4 1142) nsock = config->socks[index];
9561a7ade0c20 1143) mutex_lock(&nsock->tx_lock);
f3733247ae7c5 1144) if (nsock->dead) {
560bc4b39952e 1145) int old_index = index;
f3733247ae7c5 1146) index = find_fallback(nbd, index);
560bc4b39952e 1147) mutex_unlock(&nsock->tx_lock);
5ea8d10802ec4 1148) if (index < 0) {
560bc4b39952e 1149) if (wait_for_reconnect(nbd)) {
560bc4b39952e 1150) index = old_index;
560bc4b39952e 1151) goto again;
560bc4b39952e 1152) }
560bc4b39952e 1153) /* All the sockets should already be down at this point,
560bc4b39952e 1154) * we just want to make sure that DISCONNECTED is set so
560bc4b39952e 1155) * any requests that come in that were queue'ed waiting
560bc4b39952e 1156) * for the reconnect timer don't trigger the timer again
560bc4b39952e 1157) * and instead just error out.
560bc4b39952e 1158) */
560bc4b39952e 1159) sock_shutdown(nbd);
560bc4b39952e 1160) nbd_config_put(nbd);
e56d4b633fffe 1161) return BLK_STS_IOERR;
5ea8d10802ec4 1162) }
f3733247ae7c5 1163) goto again;
48cf6061b3020 1164) }
48cf6061b3020 1165)
9dd5d3ab49f74 1166) /* Handle the case that we have a pending request that was partially
9dd5d3ab49f74 1167) * transmitted that _has_ to be serviced first. We need to call requeue
9dd5d3ab49f74 1168) * here so that it gets put _after_ the request that is already on the
9dd5d3ab49f74 1169) * dispatch list.
9dd5d3ab49f74 1170) */
6a468d5990ecd 1171) blk_mq_start_request(req);
9dd5d3ab49f74 1172) if (unlikely(nsock->pending && nsock->pending != req)) {
d7d94d48a272f 1173) nbd_requeue_cmd(cmd);
e56d4b633fffe 1174) ret = BLK_STS_OK;
9dd5d3ab49f74 1175) goto out;
48cf6061b3020 1176) }
9dd5d3ab49f74 1177) ret = nbd_send_cmd(nbd, cmd, index);
9dd5d3ab49f74 1178) out:
9561a7ade0c20 1179) mutex_unlock(&nsock->tx_lock);
5ea8d10802ec4 1180) nbd_config_put(nbd);
957df9af723cc 1181) return ret;
48cf6061b3020 1182) }
48cf6061b3020 1183)
fc17b6534eb83 1184) static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
fd8383fd88a2f 1185) const struct blk_mq_queue_data *bd)
^1da177e4c3f4 1186) {
fd8383fd88a2f 1187) struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
e56d4b633fffe 1188) blk_status_t ret;
^1da177e4c3f4 1189)
9561a7ade0c20 1190) /*
9561a7ade0c20 1191) * Since we look at the bio's to send the request over the network we
9561a7ade0c20 1192) * need to make sure the completion work doesn't mark this request done
9561a7ade0c20 1193) * before we are done doing our send. This keeps us from dereferencing
9561a7ade0c20 1194) * freed data if we have particularly fast completions (ie we get the
9561a7ade0c20 1195) * completion before we exit sock_xmit on the last bvec) or in the case
9561a7ade0c20 1196) * that the server is misbehaving (or there was an error) before we're
9561a7ade0c20 1197) * done sending everything over the wire.
9561a7ade0c20 1198) */
8f3ea35929a08 1199) mutex_lock(&cmd->lock);
d7d94d48a272f 1200) clear_bit(NBD_CMD_REQUEUED, &cmd->flags);
9dd5d3ab49f74 1201)
9dd5d3ab49f74 1202) /* We can be called directly from the user space process, which means we
9dd5d3ab49f74 1203) * could possibly have signals pending so our sendmsg will fail. In
9dd5d3ab49f74 1204) * this case we need to return that we are busy, otherwise error out as
9dd5d3ab49f74 1205) * appropriate.
9dd5d3ab49f74 1206) */
9dd5d3ab49f74 1207) ret = nbd_handle_cmd(cmd, hctx->queue_num);
8f3ea35929a08 1208) mutex_unlock(&cmd->lock);
9561a7ade0c20 1209)
6e60a3bbb45bd 1210) return ret;
^1da177e4c3f4 1211) }
^1da177e4c3f4 1212)
cf1b2326b7348 1213) static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
cf1b2326b7348 1214) int *err)
cf1b2326b7348 1215) {
]
|
| 61/2 |
2026/12/01 13:21 |
tool |
git-blame |
0m
Args: map[End:2035 File:drivers/block/nbd.c Start:2010]
Results: map[Output:e2daec488c570 2010) goto out_err_disk;
e2daec488c570 2011) }
e2daec488c570 2012)
b0d9111a2d537 2013) mutex_init(&nbd->config_lock);
5ea8d10802ec4 2014) refcount_set(&nbd->config_refs, 0);
75b7f62aa65d5 2015) /*
75b7f62aa65d5 2016) * Start out with a zero references to keep other threads from using
75b7f62aa65d5 2017) * this device until it is fully initialized.
75b7f62aa65d5 2018) */
75b7f62aa65d5 2019) refcount_set(&nbd->refs, 0);
c6a4759ea0c9a 2020) INIT_LIST_HEAD(&nbd->list);
b0d9111a2d537 2021) disk->major = NBD_MAJOR;
7198bfc201764 2022) disk->first_minor = index << part_shift;
4af5f2e030131 2023) disk->minors = 1 << part_shift;
b0d9111a2d537 2024) disk->fops = &nbd_fops;
b0d9111a2d537 2025) disk->private_data = nbd;
b0d9111a2d537 2026) sprintf(disk->disk_name, "nbd%d", index);
e1654f413fe08 2027) err = add_disk(disk);
e1654f413fe08 2028) if (err)
e2daec488c570 2029) goto out_free_work;
75b7f62aa65d5 2030)
75b7f62aa65d5 2031) /*
75b7f62aa65d5 2032) * Now publish the device.
75b7f62aa65d5 2033) */
75b7f62aa65d5 2034) refcount_set(&nbd->refs, refs);
47d902b90a32a 2035) nbd_total_devices++;
]
|
| 62/2 |
2026/12/01 13:21 |
tool |
git-blame |
0m
Args: map[End:1895 File:fs/btrfs/file.c Start:1840]
Results: map[Output:0ddefc2a7ce4b 1840) u64 end;
0ddefc2a7ce4b 1841)
49990d8fa27d7 1842) reserved_space = fsize;
0ddefc2a7ce4b 1843)
601ea9c42a58a 1844) sb_start_pagefault(inode->vfs_inode.i_sb);
7e755aa731f70 1845) page_start = folio_pos(folio);
7e755aa731f70 1846) page_end = page_start + folio_size(folio) - 1;
0ddefc2a7ce4b 1847) end = page_end;
0ddefc2a7ce4b 1848)
0ddefc2a7ce4b 1849) /*
0ddefc2a7ce4b 1850) * Reserving delalloc space after obtaining the page lock can lead to
0ddefc2a7ce4b 1851) * deadlock. For example, if a dirty page is locked by this function
0ddefc2a7ce4b 1852) * and the call to btrfs_delalloc_reserve_space() ends up triggering
0ddefc2a7ce4b 1853) * dirty page write out, then the btrfs_writepages() function could
0ddefc2a7ce4b 1854) * end up waiting indefinitely to get a lock on the page currently
0ddefc2a7ce4b 1855) * being processed by btrfs_page_mkwrite() function.
0ddefc2a7ce4b 1856) */
601ea9c42a58a 1857) ret = btrfs_check_data_free_space(inode, &data_reserved, page_start,
601ea9c42a58a 1858) reserved_space, false);
6599716de2d68 1859) if (ret < 0) {
6599716de2d68 1860) size_t write_bytes = reserved_space;
6599716de2d68 1861)
601ea9c42a58a 1862) if (btrfs_check_nocow_lock(inode, page_start, &write_bytes, false) <= 0)
6599716de2d68 1863) goto out_noreserve;
6599716de2d68 1864)
6599716de2d68 1865) only_release_metadata = true;
6599716de2d68 1866)
6599716de2d68 1867) /*
6599716de2d68 1868) * Can't write the whole range, there may be shared extents or
6599716de2d68 1869) * holes in the range, bail out with @only_release_metadata set
6599716de2d68 1870) * to true so that we unlock the nocow lock before returning the
6599716de2d68 1871) * error.
6599716de2d68 1872) */
6599716de2d68 1873) if (write_bytes < reserved_space)
6599716de2d68 1874) goto out_noreserve;
6599716de2d68 1875) }
601ea9c42a58a 1876) ret = btrfs_delalloc_reserve_metadata(inode, reserved_space,
6599716de2d68 1877) reserved_space, false);
6599716de2d68 1878) if (ret < 0) {
6599716de2d68 1879) if (!only_release_metadata)
601ea9c42a58a 1880) btrfs_free_reserved_data_space(inode, data_reserved,
6599716de2d68 1881) page_start, reserved_space);
0ddefc2a7ce4b 1882) goto out_noreserve;
6599716de2d68 1883) }
0ddefc2a7ce4b 1884)
1ce06d45d9224 1885) ret = file_update_time(vmf->vma->vm_file);
1ce06d45d9224 1886) if (ret < 0)
a08625f825062 1887) goto out;
0ddefc2a7ce4b 1888) again:
601ea9c42a58a 1889) down_read(&inode->i_mmap_lock);
7e755aa731f70 1890) folio_lock(folio);
601ea9c42a58a 1891) size = i_size_read(&inode->vfs_inode);
0ddefc2a7ce4b 1892)
601ea9c42a58a 1893) if ((folio->mapping != inode->vfs_inode.i_mapping) ||
0ddefc2a7ce4b 1894) (page_start >= size)) {
0ddefc2a7ce4b 1895) /* Page got truncated out from underneath us. */
]
|
| 63/2 |
2026/12/01 13:21 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 74341
output: 225
thoughts: 1697
Thoughts: **Reconstructing the Lockdep Conundrum: A Detective's Journey**
Initially, I was presented with a series of commit hashes and file paths, hinting at a complex locking issue. My first instinct was to scrutinize the dates and the specific changes within these commits. I saw modifications in `block/elevator.c` around freeze/quiesce operations, `fs/btrfs/file.c` dealing with `i_mmap_lock` and file modification, and crucially, `drivers/block/nbd.c` with several commits focused on reclassifying sockets, particularly in the context of avoiding lockdep circular dependencies. The future dates (2025, 2026) on some NBD commits immediately piqued my interest, suggesting a proactive effort to resolve an impending or existing problem.
Then, a stark realization hit me: "HOLD EVERYTHING! Let's think VERY carefully about this!" The core of the problem, the lockdep splat, wasn't just a simple race; it was a circular dependency. I needed to dissect the possible scenarios.
Possibility A immediately drew my attention: was the splat caused by the NBD-specific commits `9280e6edf656` ("don't warn when reclassifying a busy socket lock") or `d532cddb6c60` ("Reclassify sockets to avoid lockdep circular dependency")? A critical detail emerged: *before* `d532cddb6c60`, NBD *never* reclassified sockets. This was a crucial historical context.
I then delved into the lock acquisition order *before* `d532cddb6c60`. The picture became clearer:
* `btrfs_fallocate` would acquire `i_mmap_lock`, then call `file_modified`, which in turn would take `btrfs_tree_read_lock`, eventually leading to `blk_mq_submit_bio` and a call to `q->q_usage_counter(io)`.
* Separately, operations like `blk_register_queue` or `elv_iosched_store` would trigger `elevator_change`, freezing the queue (`q->q_usage_counter(io)`), locking `q->elevator_lock`, and then quiescing the queue via `set->srcu`.
* During this time, `nbd_queue_rq` would be running under `set->srcu`, acquiring `cmd->lock` and `nsock->tx_lock`. Critically, because sockets weren't reclassified, it would also lock `sk_lock-AF_INET`.
* Meanwhile, `tcp_sendmsg_locked` would grab `sk_lock-AF_INET` and then check `__might_fault` under `mm->mmap_lock`.
* And, `do_page_mkwrite` (via `btrfs_page_mkwrite`) would operate under `mm->mmap_lock`, then take `sb_pagefaults`, and finally `i_mmap_lock`.
The "WAIT A SECOND!" moment was when I realized: "Did ALL of that ALREADY exist before `d532cddb6c60`?" It seemed so. This led to the crucial question: did any of these commit messages in `2025`/`2026` *introduce* one of those problematic locking links, or did `d532cddb6c60` *fail* to fix it, or even did `9280e6edf656` *exacerbate* the issue by making `nbd_reclassify_socket` silently fail?
Further investigation into `9280e6edf656` revealed another critical detail: *before* it, if `!sock_allow_reclassification(sk)` was true, the code in `d532cddb6c60` would execute `WARN_ON_ONCE(!sock_allow_reclassification(sk))` but *still return without reclassifying*. And again, *before* `d532cddb6c60`, there was no reclassification at all.
This brought me back to the prompt and the root cause analysis provided. The two main points – the silent failure of `nbd_reclassify_socket()` and the resulting dependency cycle – resonated deeply with my findings. The explanation that `nbd_reclassify_socket()` could silently return without reclassifying when userspace held the `sk->sk_lock` or `sk->sk_lock.slock` was exactly the missing piece. This left the socket in the generic `sk_lock-AF_INET` class, which, when combined with other operations (like `btrfs_fallocate` holding `i_mmap_lock`), created the deadlock or lockdep splat. The fact that `nbd_get_socket()` only borrowed a file reference and left the descriptor open in userspace was the key to understanding how a direct `recvmsg()` on the NBD socket into an `mmap()`ed page could create the AB-BA lock inversion on `sk->sk_lock` and `ei->i_mmap_lock`.
The final "HOLD ON! Let's check BOTH aspects of this!" was a call to action to ensure I wasn't missing any subtle interactions. My primary question became: "Why wasn't this splat seen before `f1be1788a32e` / `20117b5a4b9c6` / `d532cddb6c60` / `9280e6edf656`?" Was it that NBD *could* trigger this cycle much earlier, but these specific commits introduced or highlighted the conditions? Or was it that a commit in `nbd.c` (or even the elevator/block layer commits) was the direct trigger? I needed to examine the `git-log` of `drivers/block/nbd.c` more extensively, as well as the interplay with `block/blk-sysfs.c` and `block/blk-mq.c`. The focus was on understanding the precise temporal and functional sequence of events that led to the lockdep warning.
|
| 64/2 |
2026/12/01 13:21 |
tool |
git-log |
0m
Args: map[Count:20 SourcePath:block/blk-sysfs.c SymbolName:blk_register_queue]
Results: map[Output:e8dcf2d142bd block: add configurable error injection
3d9782f62fb7 block: default to QD=1 writes for blk-mq rotational zoned devices
dfe48ea17973 blk-mq: use NOIO context to prevent deadlock during debugfs creation
9869d3a6fed3 block: fix race between wbt_enable_default and IO submission
8f5845e0743b block: restore default wbt enablement
343dc5423bfe block: fix kobject double initialization in add_disk
78c271344b6f block: move wbt_enable_default() out of queue freezing from sched ->exit()
1e44bedbc921 block: unifying elevator change
40f2eb9b5314 block: fix resource leak in blk_register_queue() error path
245618f8e45f block: protect wbt_lat_usec using q->elevator_lock
1bf70d08cc3b block: introduce a dedicated lock for protecting queue elevator updates
fe6628608627 block: get rid of request queue ->sysfs_dir_lock
3802f73bd807 block: fix uaf for flush rq while iterating tags
a3166c51702b blk-throttle: delay initialization until configuration
04aad37be1a8 blk-wbt: pass a gendisk to wbt_{enable,disable}_default
2bd85221a625 block: untangle request_queue refcounting from sysfs
40602997be26 block: fix error unwinding in blk_register_queue
450deb93df7d blk-crypto: pass a gendisk to blk_crypto_sysfs_{,un}register
5f6dc7522ac2 blk-throttle: pass a gendisk to blk_throtl_register_queue
22d0c4080fe4 block: simplify disk_set_independent_access_ranges
]
|
| 65/2 |
2026/12/01 13:21 |
tool |
git-blame |
0m
Args: map[End:1005 File:block/blk-sysfs.c Start:975]
Results: map[Output:3d9782f62fb7c 975) /*
3d9782f62fb7c 976) * For blk-mq rotational zoned devices, default to using QD=1
3d9782f62fb7c 977) * writes. For non-mq rotational zoned devices, the device driver can
3d9782f62fb7c 978) * set an appropriate default.
3d9782f62fb7c 979) */
3d9782f62fb7c 980) if (queue_is_mq(q) && blk_queue_rot(q) && blk_queue_is_zoned(q))
3d9782f62fb7c 981) blk_queue_flag_set(QUEUE_FLAG_ZONED_QD1_WRITES, q);
3d9782f62fb7c 982)
22d0c4080fe49 983) ret = disk_register_independent_access_ranges(disk);
a2247f19ee1c5 984) if (ret)
40602997be268 985) goto out_debugfs_remove;
a2247f19ee1c5 986)
1bf70d08cc3b5 987) ret = blk_crypto_sysfs_register(disk);
1bf70d08cc3b5 988) if (ret)
1bf70d08cc3b5 989) goto out_unregister_ia_ranges;
1bf70d08cc3b5 990)
1e44bedbc921a 991) if (queue_is_mq(q))
1e44bedbc921a 992) elevator_set_default(q);
20f01f1632036 993)
cecf5d87ff203 994) blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q);
9869d3a6fed38 995) wbt_init_enable_default(disk);
cecf5d87ff203 996)
cecf5d87ff203 997) /* Now everything is ready and send out KOBJ_ADD uevent */
2bd85221a625b 998) kobject_uevent(&disk->queue_kobj, KOBJ_ADD);
0546858c59fb9 999) if (q->elevator)
cecf5d87ff203 1000) kobject_uevent(&q->elevator->kobj, KOBJ_ADD);
cecf5d87ff203 1001) mutex_unlock(&q->sysfs_lock);
a72c374f97a4c 1002)
a72c374f97a4c 1003) /*
a72c374f97a4c 1004) * SCSI probing may synchronously create and destroy a lot of
a72c374f97a4c 1005) * request_queues for non-existent devices. Shutting down a fully
]
|
| 66/2 |
2026/12/01 13:21 |
tool |
git-show |
0m
Args: map[Commit:20117b5a4b9c6dbb9414f0451111c3f13a37874a]
Results: map[Output:commit 20117b5a4b9c6dbb9414f0451111c3f13a37874a
Author: Ming Lei <ming.lei@redhat.com>
Date: Mon May 5 22:17:52 2025 +0800
block: move queue freezing & elevator_lock into elevator_change()
Move queue freezing & elevator_lock into elevator_change(), and prepare
for using elevator_change() for setting up & tearing down default elevator
too.
Also add lockdep_assert_held() in __elevator_change() because either
read or write lock is required for changing elevator.
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Reviewed-by: Nilay Shroff <nilay@linux.ibm.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20250505141805.2751237-15-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/block/elevator.c b/block/elevator.c
index cabd7a8bb76c..cb54a3791fe5 100644
--- a/block/elevator.c
+++ b/block/elevator.c
@@ -676,10 +676,19 @@ static int elevator_switch(struct request_queue *q, const char *name)
*/
static int elevator_change(struct request_queue *q, const char *elevator_name)
{
- if (q->elevator && elevator_match(q->elevator->type, elevator_name))
- return 0;
+ unsigned int memflags;
+ int ret = 0;
- return elevator_switch(q, elevator_name);
+ lockdep_assert_held(&q->tag_set->update_nr_hwq_lock);
+
+ memflags = blk_mq_freeze_queue(q);
+ mutex_lock(&q->elevator_lock);
+ if (!(q->elevator && elevator_match(q->elevator->type,
+ elevator_name)))
+ ret = elevator_switch(q, elevator_name);
+ mutex_unlock(&q->elevator_lock);
+ blk_mq_unfreeze_queue(q, memflags);
+ return ret;
}
/*
@@ -718,7 +727,6 @@ ssize_t elv_iosched_store(struct gendisk *disk, const char *buf,
char elevator_name[ELV_NAME_MAX];
char *name;
int ret;
- unsigned int memflags;
struct request_queue *q = disk->queue;
struct blk_mq_tag_set *set = q->tag_set;
@@ -737,13 +745,9 @@ ssize_t elv_iosched_store(struct gendisk *disk, const char *buf,
elv_iosched_load_module(name);
down_read(&set->update_nr_hwq_lock);
- memflags = blk_mq_freeze_queue(q);
- mutex_lock(&q->elevator_lock);
ret = elevator_change(q, name);
if (!ret)
ret = count;
- mutex_unlock(&q->elevator_lock);
- blk_mq_unfreeze_queue(q, memflags);
up_read(&set->update_nr_hwq_lock);
return ret;
}
]
|
| 67/2 |
2026/12/01 13:21 |
tool |
git-log |
0m
Args: map[CodeRegexp:lock_sock Count:20 PathPrefix:drivers/block/nbd.c]
Results:
|
| 68/2 |
2026/12/01 13:21 |
tool |
git-log |
0m
Args: map[CodeRegexp:nbd_mark_nsock_dead Count:20 PathPrefix:drivers/block/nbd.c]
Results: map[Output:957df9af723c nbd: Remove __force casts
3fe1db626a56 nbd: partition nbd_read_stat() into nbd_read_reply() and nbd_handle_reply()
799f9a38bc9f nbd: multicast dead link notifications
5ea8d10802ec nbd: separate out the config information
f3733247ae7c nbd: handle single path failures gracefully
]
|
| 69/2 |
2026/12/01 13:21 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 78770
output: 83
thoughts: 280
Thoughts: **My Analytical Journey Through the NBD Queue Lock Introductions**
Okay, I'm diving into the history of how the Network Block Device (NBD) handles its queue updates, specifically looking at how synchronization has evolved. My initial thought is to pinpoint the key commits that introduced synchronization primitives for these updates. I've flagged three specific commits as potentially critical: `1e44bedbc921a` (which speaks of "unifying elevator change" and is dated May 2025, suggesting a future consolidation), `1bf70d08cc3b` (introducing a "dedicated lock for protecting queue elevator updates"), and `f1be1788a32e` (which models "freeze & enter queue as lock for supporting lockdep" and is from October 2024).
However, just looking at these doesn't give me the full picture. To truly grasp the "causal story," I need to understand the state *before* a significant refactoring or introduction of new locking mechanisms, and then trace the changes *through* specific commits. I've marked `d532cddb6c60` as a key turning point, and I want to compare what happened *before* it, *in* it, and then *after* it, specifically looking at `9280e6edf656` and `553d28925fde`. This comparative analysis is crucial to seeing the progression and understanding the motivations behind each change.
To get to the heart of the matter, I need to examine the source code itself. My immediate focus is on the `drivers/block/nbd.c` file. I'm particularly interested in the functions `nbd_dev_add()`, `nbd_add_socket()`, and `nbd_genl_reconnect_sock_cb()`. I'm going to zoom in on the section of `nbd.c` roughly between lines 1270 and 1410. My goal here is to understand precisely what happens within `nbd_add_socket` and `nbd_genl_reconnect_sock_cb`, and crucially, who calls them and in what context. This will help me connect the high-level commit messages to concrete code changes and understand the flow of operations that necessitated these synchronization measures.
|
| 70/2 |
2026/12/01 13:21 |
tool |
read-file |
0m
Args: map[File:drivers/block/nbd.c FirstLine:1275 LineCount:100]
Results: map[Contents:1275: #endif
1276:
1277: static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
1278: bool netlink)
1279: {
1280: struct nbd_config *config = nbd->config;
1281: struct socket *sock;
1282: struct nbd_sock **socks;
1283: struct nbd_sock *nsock;
1284: int err;
1285:
1286: /* Arg will be cast to int, check it to avoid overflow */
1287: if (arg > INT_MAX)
1288: return -EINVAL;
1289:
1290: if (nbd->pid) {
1291: dev_err(disk_to_dev(nbd->disk),
1292: "Cannot add socket to a running device\n");
1293: return -EBUSY;
1294: }
1295:
1296: sock = nbd_get_socket(nbd, arg, &err);
1297: if (!sock)
1298: return err;
1299: nbd_reclassify_socket(sock);
1300:
1301: if (!netlink && !nbd->task_setup &&
1302: !test_bit(NBD_RT_BOUND, &config->runtime_flags))
1303: nbd->task_setup = current;
1304:
1305: if (!netlink &&
1306: (nbd->task_setup != current ||
1307: test_bit(NBD_RT_BOUND, &config->runtime_flags))) {
1308: dev_err(disk_to_dev(nbd->disk),
1309: "Device being setup by another task");
1310: err = -EBUSY;
1311: goto put_socket;
1312: }
1313:
1314: nsock = kzalloc_obj(*nsock);
1315: if (!nsock) {
1316: err = -ENOMEM;
1317: goto put_socket;
1318: }
1319:
1320: socks = krealloc(config->socks, (config->num_connections + 1) *
1321: sizeof(struct nbd_sock *), GFP_KERNEL);
1322: if (!socks) {
1323: kfree(nsock);
1324: err = -ENOMEM;
1325: goto put_socket;
1326: }
1327:
1328: config->socks = socks;
1329:
1330: nsock->fallback_index = -1;
1331: nsock->dead = false;
1332: mutex_init(&nsock->tx_lock);
1333: nsock->sock = sock;
1334: nsock->pending = NULL;
1335: nsock->sent = 0;
1336: nsock->cookie = 0;
1337: INIT_WORK(&nsock->work, nbd_pending_cmd_work);
1338: socks[config->num_connections++] = nsock;
1339: atomic_inc(&config->live_connections);
1340:
1341: return 0;
1342:
1343: put_socket:
1344: sockfd_put(sock);
1345: return err;
1346: }
1347:
1348: static int nbd_genl_reconnect_sock_cb(struct nbd_device *nbd, unsigned long arg)
1349: {
1350: struct nbd_config *config = nbd->config;
1351: struct socket *sock, *old;
1352: struct recv_thread_args *args;
1353: int i;
1354: int err;
1355:
1356: sock = nbd_get_socket(nbd, arg, &err);
1357: if (!sock)
1358: return err;
1359: nbd_reclassify_socket(sock);
1360:
1361: args = kzalloc_obj(*args);
1362: if (!args) {
1363: sockfd_put(sock);
1364: return -ENOMEM;
1365: }
1366:
1367: for (i = 0; i < config->num_connections; i++) {
1368: struct nbd_sock *nsock = config->socks[i];
1369:
1370: if (!nsock->dead)
1371: continue;
1372:
1373: mutex_lock(&nsock->tx_lock);
1374: if (!nsock->dead) {
]
|
| 71/2 |
2026/12/01 13:21 |
tool |
read-file |
0m
Args: map[File:drivers/block/nbd.c FirstLine:1375 LineCount:60]
Results: map[Contents:1375: mutex_unlock(&nsock->tx_lock);
1376: continue;
1377: }
1378: sk_set_memalloc(sock->sk);
1379: if (nbd->tag_set.timeout)
1380: sock->sk->sk_sndtimeo = nbd->tag_set.timeout;
1381: atomic_inc(&config->recv_threads);
1382: refcount_inc(&nbd->config_refs);
1383: old = nsock->sock;
1384: nsock->fallback_index = -1;
1385: nsock->sock = sock;
1386: nsock->dead = false;
1387: INIT_WORK(&args->work, recv_work);
1388: args->index = i;
1389: args->nbd = nbd;
1390: args->nsock = nsock;
1391: nsock->cookie++;
1392: mutex_unlock(&nsock->tx_lock);
1393: sockfd_put(old);
1394:
1395: clear_bit(NBD_RT_DISCONNECTED, &config->runtime_flags);
1396:
1397: /* We take the tx_mutex in an error path in the recv_work, so we
1398: * need to queue_work outside of the tx_mutex.
1399: */
1400: queue_work(nbd->recv_workq, &args->work);
1401:
1402: atomic_inc(&config->live_connections);
1403: wake_up(&config->conn_wait);
1404: dev_info(nbd_to_dev(nbd), "reconnected socket\n");
1405: return 0;
1406: }
1407: sockfd_put(sock);
1408: kfree(args);
1409: return 1;
1410: }
1411:
1412: static void nbd_bdev_reset(struct nbd_device *nbd)
1413: {
1414: if (disk_openers(nbd->disk) > 1)
1415: return;
1416: set_capacity(nbd->disk, 0);
1417: }
1418:
1419: static void nbd_parse_flags(struct nbd_device *nbd)
1420: {
1421: if (nbd->config->flags & NBD_FLAG_READ_ONLY)
1422: set_disk_ro(nbd->disk, true);
1423: else
1424: set_disk_ro(nbd->disk, false);
1425: }
1426:
1427: static void send_disconnects(struct nbd_device *nbd)
1428: {
1429: struct nbd_config *config = nbd->config;
1430: struct nbd_request request = {
1431: .magic = htonl(NBD_REQUEST_MAGIC),
1432: .type = htonl(NBD_CMD_DISC),
1433: };
1434: struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
]
|
| 72/2 |
2026/12/01 13:21 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 81463
output: 132
thoughts: 5741
Thoughts: **Analyzing the NBD Socket Reclassification Bug: A Deep Dive into Lock Dependencies**
My mind is racing as I try to meticulously pinpoint the exact introduction of this bug. It feels like unraveling a complex knot, and I'm focusing on the subtle interplay of socket reclassification and lockdep annotations.
Historically, before commit `d532cddb6c60` (June 2026), the NBD subsystem never reclassified sockets at all. It consistently used `sk_lock-AF_INET`. The crucial question is: why wasn't a circular dependency introduced *before* `d532cddb6c60`? The answer seems to be that `nsock->sock->sk` was *already* in `sk_lock-AF_INET` even back then. And importantly, the function `nbd_reclassify_socket()` itself didn't even exist before `d532cddb6c60` was introduced. That commit, `d532cddb6c60`, was an attempt to fix an *existing* lockdep circular dependency (exposed by commits like `ffa1e7ada456` and `f1be1788a32e`) by introducing `nbd_reclassify_socket()`, which borrowed a pattern from nvme-tcp. It included a check: `if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) return;`.
Then, commit `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock") removed the `WARN_ON_ONCE`. This was a direct response to syzbot hitting that warning when the `NBD_CMD_CONNECT` operation raced with concurrent socket activity on the same file descriptor. The logic was that for an externally managed socket, hitting a held lock during reclassification was expected and not a bug, so the warning was removed, and reclassification would be skipped silently. Later, `553d28925fde` ("nbd: Reclassify sockets in the reconnect path") added `nbd_reclassify_socket(sock)` to the `nbd_genl_reconnect_sock_cb()` function.
Now, I'm grappling with a perplexing observation: the syzkaller crash occurred at commit `19cc08c1a6a650f8e54c0461ff198be6448f425d` (November 30, 2026), which is *after* `d532cddb6c60`, `9280e6edf656`, and `553d28925fde`. This suggests the root cause isn't simply the *introduction* of these specific NBD commits.
I need to consider two distinct aspects of the lockdep splat:
**(a) The `sk_lock-AF_INET` in the splat:** In the lockdep output, `sk_lock-AF_INET` appears in link #7 (`inet_shutdown` -> `nbd_mark_nsock_dead` -> `recv_work`). The question is, why is `nsock->sock->sk` in `sk_lock-AF_INET` and not `sk_lock-AF_INET-NBD`? This happened because `nbd_reclassify_socket(sock)` was called, but `sock_allow_reclassification(sk)` returned `false`, causing the function to return without reclassifying the socket. This silent failure to reclassify is a critical point. Alternatively, was `sk_lock-AF_INET` *always* used by NBD before `d532cddb6c60`? If so, the lockdep circular dependency *already existed* before `d532cddb6c60`. The key is to determine if the current splat *depends* on anything introduced after `d532cddb6c60`.
To answer this, I'm meticulously examining all 11 links in the splat, tracing the stack at each step and checking the git history for each.
* **Link #10 (`&ei->i_mmap_lock`)**: This is acquired in `btrfs_page_mkwrite` via `sb_start_pagefault`. This mechanism seems quite old, likely dating back to 2016/2017 or earlier.
* **Link #9 (`sb_pagefaults#4`)**: Acquired in `btrfs_page_mkwrite` under `&mm->mmap_lock` from `do_user_addr_fault`. This is standard VFS/mm/btrfs behavior, present for a long time.
* **Link #8 (`&mm->mmap_lock`)**: Acquired in `__might_fault` within `_copy_from_iter` inside `tcp_sendmsg_locked`, which is under `sk_lock-AF_INET`. This is standard net/mm interaction and has been around since lockdep fault annotations were added.
* **Link #7 (`sk_lock-AF_INET`)**: Acquired in `inet_shutdown` within `nbd_mark_nsock_dead` inside `recv_work`, under `&nsock->tx_lock`. This appears to be standard NBD behavior since at least 2017 ("nbd: handle dead connections").
* **Link #6 (`&nsock->tx_lock`)**: Acquired in `nbd_handle_cmd` within `nbd_queue_rq`, under `&cmd->lock`. This is standard NBD behavior since 2016.
* **Link #5 (`&cmd->lock`)**: Acquired in `nbd_queue_rq`, under `set->srcu` from `blk_mq_dispatch_rq_list`. This is standard blk-mq and NBD interaction since 2016.
* **Link #4 (`set->srcu`)**: Acquired via `__synchronize_srcu` in `elevator_switch` which is called during `blk_register_queue` -> `nbd_dev_add` -> `nbd_init`. Looking at the stack trace, `elevator_change()` holds `&q->elevator_lock` and calls `elevator_switch()`. Commit `1e44bedbc921a` ("block: unifying elevator change", May 2025) is relevant here, as is the introduction of `q->elevator_lock` in `1bf70d08cc3b` (2025). Before `1bf70d08cc3b`, `q->sysfs_lock` was used.
* **Link #3 (`&q->elevator_lock`)**: Acquired in `elevator_change` within `elv_iosched_store`, under `&q->q_usage_counter(io)`. Commit `f1be1788a32e` ("block: model freeze & enter queue as lock for supporting lockdep", October 2024) is critical because it added lockdep tracking for `q->q_usage_counter(io)`. Crucially, without this commit, lockdep *wouldn't* track this dependency, even though the synchronization is real. This doesn't mean `f1be1788a32e` *introduced* the bug, but it exposed it.
* **Link #2 (`&q->q_usage_counter(io)`)**: Acquired in `blk_mq_submit_bio` within `btrfs_submit_bbio` during `btrfs_read_extent_buffer`. This is standard btrfs and block layer interaction.
* **Link #1 (`btrfs-tree-01`)**: Acquired under `btrfs-tree-00`. Standard btrfs.
* **Link #0 (`btrfs-tree-00`)**: Acquired in `btrfs_read_lock_root_node` within `btrfs_search_slot`, called by `btrfs_fallocate` which acquired `i_mmap_lock` in 2021 (`8d9b4a162a37`) and `file_modified` in 2022 (`05fd9564e9faf`).
Now, I'm re-evaluating the fundamental question: is this a lockdep false positive or a *real* deadlock? The root cause analysis points to two key issues:
1. **Silent failure of `nbd_reclassify_socket()`**: When `sock_allow_reclassification(sk)` returns false (because userspace or softirq holds `sk->sk_lock` or `sk->sk_lock.slock`), `nbd_reclassify_socket()` silently skips reclassification. This leaves the NBD socket stuck in `sk_lock-AF_INET`, which directly leads to the lockdep splat when NBD is used with a filesystem like btrfs.
2. **Resulting dependency cycle and same-socket deadlock exposure**: Even without lockdep, the fact that `nbd_get_socket()` borrows a `struct file` reference and allows userspace to keep the fd open means a task can issue `recvmsg()`/`read()` on the NBD socket fd directly into an `mmap()`ed page backed by a file on `/dev/nbdX`. This creates a genuine AB-BA lock inversion on the same `sk->sk_lock` and `ei->i_mmap_lock` instances, even without lockdep classes involved.
This leads me back to the commits introducing `nbd_reclassify_socket()`:
1. `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency", Jun 13, 2026)
2. `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock", Jun 22, 2026)
3. `553d28925fde` ("nbd: Reclassify sockets in the reconnect path", Nov 12, 2026)
Why did `9280e6edf656` occur? Its commit message clearly states: "Hitting a held lock here is expected for an externally owned socket and is not a kernel bug, so skip reclassification silently instead of warning. Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless."
This is the critical point: **Is skipping reclassification harmless?** My analysis says NO. When syzkaller races `NBD_SET_SOCK` (or `NBD_CMD_CONNECT` / `NBD_CMD_RECONNECT`) with socket activity, `sock_allow_reclassification(sk)` returns `false`, and `nbd_reclassify_socket()` silently skips reclassification. This *leaves* the NBD socket in `sk_lock-AF_INET`, which is precisely what causes the lockdep circular dependency splat.
So, did `9280e6edf656` *introduce* the bug, or did it just *remove a warning* that exposed an *existing* bug? And critically, was the bug *already there* before `9280e6edf656` and even before `d532cddb6c60`?
Let's test these hypotheses against the facts and commit history:
**Hypothesis 1: `9280e6edf656` introduced the bug.**
Before `9280e6edf656` (specifically, after `d532cddb6c60` but before `9280e6edf656`), `nbd_reclassify_socket()` would hit `WARN_ON_ONCE(!sock_allow_reclassification(sk))` if the socket was locked. On syzkaller with `panic_on_warn=1`, this would *immediately* crash with a `WARNING in nbd_reclassify_socket` (as seen in `syzbot+6b85d1e39a5b8ed9a954`). Even *without* `panic_on_warn=1`, `d532cddb6c60` *still* returned without reclassifying, meaning the lockdep circular dependency would still fire later. Therefore, `9280e6edf656` did *not* introduce the circular locking dependency; it merely removed the `WARN_ON_ONCE` that was masking it.
**Hypothesis 2: `d532cddb6c60` introduced the bug.**
Before `d532cddb6c60` (i.e., at `d532cddb6c60~1`), the `nbd_reclassify_socket()` function didn't exist. Consequently, `nsock->sock->sk` was *always* in `sk_lock-AF_INET`. This means the *exact same lockdep circular dependency* (`btrfs-tree-01` -> ... -> `&q->q_usage_counter(io)` -> ... -> `sk_lock-AF_INET` -> `&mm->mmap_lock` -> `sb_pagefaults` -> `&ei->i_mmap_lock`) *already existed and was already triggered* before `d532cddb6c60`.
This leads me to reconsider why `d532cddb6c60` was even created. Its commit message states: "syzbot reported a possible circular locking dependency... Fix this by reclassifying NBD sockets to a separate lock class... This is similar to what nvme-tcp and other network block devices do. Fixes: ffa1e7ada456". The `Fixes:` tag points to `ffa1e7ada456` ("block: Make request_queue lockdep splats show up earlier").
However, `d532cddb6c60` didn't fix *all* NBD lockdep splats for several reasons:
1. It only called `nbd_reclassify_socket` in `nbd_add_socket`, not in `nbd_genl_reconnect_sock_cb` (which `553d28925fde` addressed later).
2. It used `if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) return;`, which fails to reclassify if the socket is locked, leading to the silent failure after `9280e6edf656`.
3. Even with successful reclassification, `nbd_get_socket()` doesn't prevent userspace from using the same fd for other operations, as noted in the RCA.
My focus is now on the NBD device addition path (`nbd_dev_add` called by `nbd_init`), as seen in link #4 of the stack trace. I need to carefully scrutinize the commits affecting `block/elevator.c` and `drivers/block/nbd.c` between `d532cddb6c60` and the crash commit, specifically looking at changes to `BLK_MQ_F_NO_SCHED_BY_DEFAULT`, `elevator_set_default`, and the introduction of `q->elevator_lock`. The key takeaway is that the *silent failure to reclassify* due to a held socket lock, as enabled by commit `9280e6edf656` (and present even before it due to `d532cddb6c60`'s `WARN_ON_ONCE`), is the mechanism that allows the existing `sk_lock-AF_INET` to persist and cause the lockdep circular dependency when interacting with filesystems like btrfs.
|
| 73/2 |
2026/12/01 13:22 |
tool |
git-show |
0m
Args: map[Commit:1e44bedbc921a]
Results: map[Output:commit 1e44bedbc921a35cb847991953814a50f738bcf3
Author: Ming Lei <ming.lei@redhat.com>
Date: Mon May 5 22:17:54 2025 +0800
block: unifying elevator change
Elevator change is one well-define behavior:
- tear down current elevator if it exists
- setup new elevator
It is supposed to cover any case for changing elevator by single
internal API, typically the following cases:
- setup default elevator in add_disk()
- switch to none in del_disk()
- reset elevator in blk_mq_update_nr_hw_queues()
- switch elevator in sysfs `store` elevator attribute
This patch uses elevator_change() to cover all above cases:
- every elevator switch is serialized with each other: add_disk/del_disk/
store elevator is serialized already, blk_mq_update_nr_hw_queues() uses
srcu for syncing with the other three cases
- for both add_disk()/del_disk(), queue freeze works at atomic mode
or has been froze, so the freeze in elevator_change() won't add extra
delay
- `struct elev_change_ctx` instance holds any info for changing elevator
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Nilay Shroff <nilay@linux.ibm.com>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20250505141805.2751237-17-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c
index 1f9b45b0b9ee..741e607dfab6 100644
--- a/block/blk-sysfs.c
+++ b/block/blk-sysfs.c
@@ -869,14 +869,9 @@ int blk_register_queue(struct gendisk *disk)
if (ret)
goto out_unregister_ia_ranges;
+ if (queue_is_mq(q))
+ elevator_set_default(q);
mutex_lock(&q->elevator_lock);
- if (q->elevator) {
- ret = elv_register_queue(q, false);
- if (ret) {
- mutex_unlock(&q->elevator_lock);
- goto out_crypto_sysfs_unregister;
- }
- }
wbt_enable_default(disk);
mutex_unlock(&q->elevator_lock);
@@ -902,8 +897,6 @@ int blk_register_queue(struct gendisk *disk)
return ret;
-out_crypto_sysfs_unregister:
- blk_crypto_sysfs_unregister(disk);
out_unregister_ia_ranges:
disk_unregister_independent_access_ranges(disk);
out_debugfs_remove:
@@ -951,9 +944,11 @@ void blk_unregister_queue(struct gendisk *disk)
blk_mq_sysfs_unregister(disk);
blk_crypto_sysfs_unregister(disk);
- mutex_lock(&q->elevator_lock);
- elv_unregister_queue(q);
- mutex_unlock(&q->elevator_lock);
+ if (queue_is_mq(q)) {
+ blk_mq_quiesce_queue(q);
+ elevator_set_none(q);
+ blk_mq_unquiesce_queue(q);
+ }
mutex_lock(&q->sysfs_lock);
disk_unregister_independent_access_ranges(disk);
diff --git a/block/blk.h b/block/blk.h
index 2e00e2e4cf9d..dec835350b96 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -323,9 +323,8 @@ bool blk_bio_list_merge(struct request_queue *q, struct list_head *list,
bool blk_insert_flush(struct request *rq);
void elv_update_nr_hw_queues(struct request_queue *q);
-void elevator_exit(struct request_queue *q);
-int elv_register_queue(struct request_queue *q, bool uevent);
-void elv_unregister_queue(struct request_queue *q);
+void elevator_set_default(struct request_queue *q);
+void elevator_set_none(struct request_queue *q);
ssize_t part_size_show(struct device *dev, struct device_attribute *attr,
char *buf);
diff --git a/block/elevator.c b/block/elevator.c
index 6cfac8f77d9f..540542cee21c 100644
--- a/block/elevator.c
+++ b/block/elevator.c
@@ -154,7 +154,7 @@ static void elevator_release(struct kobject *kobj)
kfree(e);
}
-void elevator_exit(struct request_queue *q)
+static void elevator_exit(struct request_queue *q)
{
struct elevator_queue *e = q->elevator;
@@ -458,7 +458,7 @@ static const struct kobj_type elv_ktype = {
.release = elevator_release,
};
-int elv_register_queue(struct request_queue *q, bool uevent)
+static int elv_register_queue(struct request_queue *q, bool uevent)
{
struct elevator_queue *e = q->elevator;
int error;
@@ -488,7 +488,7 @@ int elv_register_queue(struct request_queue *q, bool uevent)
return error;
}
-void elv_unregister_queue(struct request_queue *q)
+static void elv_unregister_queue(struct request_queue *q)
{
struct elevator_queue *e = q->elevator;
@@ -561,66 +561,6 @@ void elv_unregister(struct elevator_type *e)
}
EXPORT_SYMBOL_GPL(elv_unregister);
-/*
- * For single queue devices, default to using mq-deadline. If we have multiple
- * queues or mq-deadline is not available, default to "none".
- */
-static struct elevator_type *elevator_get_default(struct request_queue *q)
-{
- if (q->tag_set->flags & BLK_MQ_F_NO_SCHED_BY_DEFAULT)
- return NULL;
-
- if (q->nr_hw_queues != 1 &&
- !blk_mq_is_shared_tags(q->tag_set->flags))
- return NULL;
-
- return elevator_find_get("mq-deadline");
-}
-
-/*
- * Use the default elevator settings. If the chosen elevator initialization
- * fails, fall back to the "none" elevator (no elevator).
- */
-void elevator_init_mq(struct request_queue *q)
-{
- struct elevator_type *e;
- unsigned int memflags;
- int err;
-
- WARN_ON_ONCE(blk_queue_registered(q));
-
- if (unlikely(q->elevator))
- return;
-
- e = elevator_get_default(q);
- if (!e)
- return;
-
- /*
- * We are called before adding disk, when there isn't any FS I/O,
- * so freezing queue plus canceling dispatch work is enough to
- * drain any dispatch activities originated from passthrough
- * requests, then no need to quiesce queue which may add long boot
- * latency, especially when lots of disks are involved.
- *
- * Disk isn't added yet, so verifying queue lock only manually.
- */
- memflags = blk_mq_freeze_queue(q);
-
- blk_mq_cancel_work_sync(q);
-
- err = blk_mq_init_sched(q, e);
-
- blk_mq_unfreeze_queue(q, memflags);
-
- if (err) {
- pr_warn("\"%s\" elevator initialization failed, "
- "falling back to \"none\"\n", e->elevator_name);
- }
-
- elevator_put(e);
-}
-
/*
* Switch to new_e io scheduler.
*
@@ -688,6 +628,16 @@ static int elevator_change(struct request_queue *q, struct elv_change_ctx *ctx)
lockdep_assert_held(&q->tag_set->update_nr_hwq_lock);
memflags = blk_mq_freeze_queue(q);
+ /*
+ * May be called before adding disk, when there isn't any FS I/O,
+ * so freezing queue plus canceling dispatch work is enough to
+ * drain any dispatch activities originated from passthrough
+ * requests, then no need to quiesce queue which may add long boot
+ * latency, especially when lots of disks are involved.
+ *
+ * Disk isn't added yet, so verifying queue lock only manually.
+ */
+ blk_mq_cancel_work_sync(q);
mutex_lock(&q->elevator_lock);
if (!(q->elevator && elevator_match(q->elevator->type, ctx->name)))
ret = elevator_switch(q, ctx);
@@ -716,6 +666,46 @@ void elv_update_nr_hw_queues(struct request_queue *q)
mutex_unlock(&q->elevator_lock);
}
+/*
+ * Use the default elevator settings. If the chosen elevator initialization
+ * fails, fall back to the "none" elevator (no elevator).
+ */
+void elevator_set_default(struct request_queue *q)
+{
+ struct elv_change_ctx ctx = {
+ .name = "mq-deadline",
+ .no_uevent = true,
+ };
+ int err = 0;
+
+ if (q->tag_set->flags & BLK_MQ_F_NO_SCHED_BY_DEFAULT)
+ return;
+
+ /*
+ * For single queue devices, default to using mq-deadline. If we
+ * have multiple queues or mq-deadline is not available, default
+ * to "none".
+ */
+ if (elevator_find_get(ctx.name) && (q->nr_hw_queues == 1 ||
+ blk_mq_is_shared_tags(q->tag_set->flags)))
+ err = elevator_change(q, &ctx);
+ if (err < 0)
+ pr_warn("\"%s\" elevator initialization, failed %d, "
+ "falling back to \"none\"\n", ctx.name, err);
+}
+
+void elevator_set_none(struct request_queue *q)
+{
+ struct elv_change_ctx ctx = {
+ .name = "none",
+ };
+ int err;
+
+ err = elevator_change(q, &ctx);
+ if (err < 0)
+ pr_warn("%s: set none elevator failed %d\n", __func__, err);
+}
+
static void elv_iosched_load_module(const char *elevator_name)
{
struct elevator_type *found;
diff --git a/block/genhd.c b/block/genhd.c
index e0dd8ecc925f..f192fe4808b9 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -432,12 +432,6 @@ static int __add_disk(struct device *parent, struct gendisk *disk,
*/
if (disk->fops->submit_bio || disk->fops->poll_bio)
return -EINVAL;
-
- /*
- * Initialize the I/O scheduler code and pick a default one if
- * needed.
- */
- elevator_init_mq(disk->queue);
} else {
if (!disk->fops->submit_bio)
return -EINVAL;
@@ -454,7 +448,7 @@ static int __add_disk(struct device *parent, struct gendisk *disk,
ret = -EINVAL;
if (disk->major) {
if (WARN_ON(!disk->minors))
- goto out_exit_elevator;
+ goto out;
if (disk->minors > DISK_MAX_PARTS) {
pr_err("block: can't allocate more than %d partitions\n",
@@ -464,14 +458,14 @@ static int __add_disk(struct device *parent, struct gendisk *disk,
if (disk->first_minor > MINORMASK ||
disk->minors > MINORMASK + 1 ||
disk->first_minor + disk->minors > MINORMASK + 1)
- goto out_exit_elevator;
+ goto out;
} else {
if (WARN_ON(disk->minors))
- goto out_exit_elevator;
+ goto out;
ret = blk_alloc_ext_minor();
if (ret < 0)
- goto out_exit_elevator;
+ goto out;
disk->major = BLOCK_EXT_MAJOR;
disk->first_minor = ret;
}
@@ -561,12 +555,7 @@ static int __add_disk(struct device *parent, struct gendisk *disk,
out_free_ext_minor:
if (disk->major == BLOCK_EXT_MAJOR)
blk_free_ext_minor(disk->first_minor);
-out_exit_elevator:
- if (disk->queue->elevator) {
- mutex_lock(&disk->queue->elevator_lock);
- elevator_exit(disk->queue);
- mutex_unlock(&disk->queue->elevator_lock);
- }
+out:
return ret;
}
@@ -760,14 +749,7 @@ static void __del_gendisk(struct gendisk *disk)
if (queue_is_mq(q))
blk_mq_cancel_work_sync(q);
- blk_mq_quiesce_queue(q);
- if (q->elevator) {
- mutex_lock(&q->elevator_lock);
- elevator_exit(q);
- mutex_unlock(&q->elevator_lock);
- }
rq_qos_exit(q);
- blk_mq_unquiesce_queue(q);
/*
* If the disk does not own the queue, allow using passthrough requests
]
|
| 74/2 |
2026/12/01 13:22 |
tool |
git-show |
0m
Args: map[Commit:1bf70d08cc3b Stat:true]
Results: map[Output:commit 1bf70d08cc3b55abd1763e6dff5855cb8dd8318b
Author: Nilay Shroff <nilay@linux.ibm.com>
Date: Tue Mar 4 15:52:33 2025 +0530
block: introduce a dedicated lock for protecting queue elevator updates
A queue's elevator can be updated either when modifying nr_hw_queues
or through the sysfs scheduler attribute. Currently, elevator switching/
updating is protected using q->sysfs_lock, but this has led to lockdep
splats[1] due to inconsistent lock ordering between q->sysfs_lock and
the freeze-lock in multiple block layer call sites.
As the scope of q->sysfs_lock is not well-defined, its (mis)use has
resulted in numerous lockdep warnings. To address this, introduce a new
q->elevator_lock, dedicated specifically for protecting elevator
switches/updates. And we'd now use this new q->elevator_lock instead of
q->sysfs_lock for protecting elevator switches/updates.
While at it, make elv_iosched_load_module() a static function, as it is
only called from elv_iosched_store(). Also, remove redundant parameters
from elv_iosched_load_module() function signature.
[1] https://lore.kernel.org/all/67637e70.050a0220.3157ee.000c.GAE@google.com/
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
Link: https://lore.kernel.org/r/20250304102551.2533767-5-nilay@linux.ibm.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/blk-core.c | 1 +
block/blk-mq.c | 15 +++++++--------
block/blk-sysfs.c | 32 ++++++++++++++++++++++----------
block/elevator.c | 35 ++++++++++++++++-------------------
block/elevator.h | 2 --
block/genhd.c | 9 ++++++---
include/linux/blkdev.h | 8 ++++++++
7 files changed, 60 insertions(+), 42 deletions(-)
]
|
| 75/2 |
2026/12/01 13:22 |
tool |
git-show |
0m
Args: map[Commit:a1ce35fa49852 Stat:true]
Results: map[Output:commit a1ce35fa49852db60fc6e268038530be533c5b15
Author: Jens Axboe <axboe@kernel.dk>
Date: Mon Oct 29 10:23:51 2018 -0600
block: remove dead elevator code
This removes a bunch of core and elevator related code. On the core
front, we remove anything related to queue running, draining,
initialization, plugging, and congestions. We also kill anything
related to request allocation, merging, retrieval, and completion.
Remove any checking for single queue IO schedulers, as they no
longer exist. This means we can also delete a bunch of code related
to request issue, adding, completion, etc - and all the SQ related
ops and helpers.
Also kill the load_default_modules(), as all that did was provide
for a way to load the default single queue elevator.
Tested-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/bfq-iosched.c | 1 -
block/blk-core.c | 1749 +---------------------------------------------
block/blk-exec.c | 20 +-
block/blk-ioc.c | 33 +-
block/blk-merge.c | 5 -
block/blk-settings.c | 36 -
block/blk-sysfs.c | 36 +-
block/blk.h | 51 --
block/elevator.c | 377 +---------
block/kyber-iosched.c | 1 -
block/mq-deadline.c | 1 -
include/linux/blkdev.h | 93 +--
include/linux/elevator.h | 90 +--
include/linux/init.h | 1 -
init/do_mounts_initrd.c | 3 -
init/initramfs.c | 6 -
init/main.c | 12 -
17 files changed, 75 insertions(+), 2440 deletions(-)
]
|
| 76/2 |
2026/12/01 13:22 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:drivers/block/nbd.c Since:3 years]
Results: map[Output:553d28925fde nbd: Reclassify sockets in the reconnect path
326d49039c10 nbd: add pre_defined_connections module parameter for pre-created devices
a9d414b4a15c nbd: remove queue freeze for newly created nbd from netlink path
f8d21c590e55 nbd: factor out a nbd_genl_foreach_sock
db285268df79 nbd: skip queue freeze when setting size at device startup
285908f554fe nbd: remove queue freeze in nbd_add_socket
0fdee7c5fa88 nbd: clear queue limits on disconnect
04d8fb23e520 nbd: disallow NBD_SET_SOCK on an active device
fe247030f1ce nbd: simplify find_fallback() by removing redundant logic
9280e6edf656 nbd: don't warn when reclassifying a busy socket lock
d532cddb6c60 nbd: Reclassify sockets to avoid lockdep circular dependency
b040a1a4523d block: switch numa_node to int in blk_mq_hw_ctx and init_request
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
1649714b930f nbd: defer config unlock in nbd_genl_connect
9517b82d8d42 nbd: defer config put in recv_work
4601b7923d1b nbd: don't copy kernel creds
81ccca31214e nbd: override creds to kernel when calling sock_{send,recv}msg()
51723bf92679 drivers/block: replace use of system_wq with system_percpu_wq
9f7c02e03157 nbd: restrict sockets to TCP and UDP
]
|
| 77/2 |
2026/12/01 13:22 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 92973
output: 118
thoughts: 2007
Thoughts: **My Debugging Journey into the NBD Crash**
This is a reconstruction of my thought process as I delved into a crash report involving `nbd.c`. My primary goal was to pinpoint the root cause, specifically whether it was a recent introduction or a long-standing issue.
Initially, I cast a wide net, focusing on key functions within `drivers/block/nbd.c` like `recv_work`, `nbd_dev_add`, and `nbd_add_socket`. I also examined specific commit SHAs (`cf1b2326b734`, `9f7c02e03157`, `560bc4b39952`) that seemed potentially relevant.
Then, I shifted my approach to tracing the dependency chain presented in the crash report. The crucial question became: was any link in this chain introduced recently, or did it predate the commits I was initially considering (`9280e6edf656`, `d532cddb6c60`, `f1be1788a32e`, `ffa1e7ada456`, `1e44bedbc921a`)?
I began a meticulous examination of potential culprits.
My first area of focus was commit `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock"). I dug into its context, specifically commit `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency"), which was merged on June 13, 2026. This commit aimed to resolve an existing NBD socket lockdep circular dependency arising from NBD sockets sharing `sk_lock-AF_INET` with user sockets.
I realized `d532cddb6c60` had a flaw: it used `WARN_ON_ONCE(!sock_allow_reclassification(sk))`. This already failed to reclassify when the socket was locked and triggered a warning. Subsequently, `9280e6edf656` (June 22, 2026) removed this `WARN_ON_ONCE` with the rationale that hitting a held lock was expected for externally owned sockets and not a kernel bug. The justification was that reclassification was merely a lockdep annotation and skipping it in a race was harmless.
This raised a significant point: *before* `9280e6edf656`, if syzkaller raced `NBD_SET_SOCK` with socket activity, it would trigger `WARN_ON_ONCE` in `nbd_reclassify_socket()`. Even if `panic_on_warn` was off, this still resulted in the circular locking dependency. And *before* `d532cddb6c60`, NBD never reclassified sockets at all, meaning the circular locking dependency already existed without requiring a race condition.
This led me to my next critical step: scrutinizing whether this *exact* circular locking dependency existed *before* `d532cddb6c60`, or if something changed *after* it or around the 2025-2026 timeframe. I decided to meticulously examine every single link in the crash trace:
* `#10` (`btrfs_page_mkwrite` -> `down_read(&inode->i_mmap_lock)`)
* `#9` (`btrfs_page_mkwrite` -> `sb_start_pagefault`)
* `#8` (`tcp_sendmsg_locked` -> `_copy_from_iter` -> `__might_fault` (`&mm->mmap_lock`))
* `#7` (`recv_work` -> `nbd_mark_nsock_dead` -> `kernel_sock_shutdown` -> `inet_shutdown` -> `lock_sock`)
* `#6` (`nbd_queue_rq` -> `nbd_handle_cmd` -> `mutex_lock(&nsock->tx_lock)`)
* `#5` (`blk_mq_dispatch_rq_list` -> `nbd_queue_rq` -> `mutex_lock(&cmd->lock)`)
* `#4` (`nbd_init` -> `nbd_dev_add` -> `add_disk` -> `blk_register_queue` -> `elevator_set_default` -> `elevator_change` -> `elevator_switch` -> `blk_mq_quiesce_queue` (`set->srcu`))
* `#3` (`elv_iosched_store` -> `elevator_change` -> `mutex_lock(&q->elevator_lock)`)
* `#2` (`open_ctree` -> ... -> `read_block_for_search` -> `btrfs_read_extent_buffer` -> `btrfs_submit_bbio` -> `blk_mq_submit_bio` (`&q->q_usage_counter(io)`))
* `#1` (`open_ctree` -> ... -> `read_block_for_search` (`btrfs-tree-00`))
* `#0` (`btrfs_fallocate` -> `btrfs_inode_lock(BTRFS_ILOCK_MMAP)` -> `file_modified` -> ... -> `btrfs_read_lock_root_node` (`btrfs-tree-01`))
As I re-examined links #4, #3, and #2, something clicked. In link #3, I saw: `elv_iosched_store` -> `elevator_change` -> `blk_mq_freeze_queue(q)` (`&q->q_usage_counter(io)`) -> `mutex_lock(&q->elevator_lock)`. And in link #4: `elevator_set_default` -> `elevator_change` -> `mutex_lock(&q->elevator_lock)` -> `elevator_switch` -> `blk_mq_quiesce_queue` (`set->srcu`).
This led me to question the significance of commit `f1be1788a32e` ("block: model freeze & enter queue as lock for supporting lockdep", Oct 25, 2024). My hypothesis was that *before* this commit, lockdep was unaware of `&q->q_usage_counter(io)`. Consequently, it couldn't link the IO submission in `#2` (`btrfs-tree-00` -> `&q->q_usage_counter(io)`) to the `&q->elevator_lock` in `#3`.
I reasoned that prior to `f1be1788a32e`, the *only* way to connect IO submission to NBD in lockdep was through `fs_reclaim` (writeback/reclaim -> bio -> NBD) or perhaps directly via `btrfs_submit_bbio`. However, `btrfs_submit_bbio` submits bios asynchronously, and `blk_mq_submit_bio` doesn't hold locks across `nbd_queue_rq` because `blk_mq_sched_dispatch_requests` runs in a kworker, not synchronously within `blk_mq_submit_bio`.
Therefore, my current understanding is that `f1be1788a32e` was the crucial change that connected `bio_queue_enter` (`#2`) to `blk_mq_freeze_queue` (`#3`) in lockdep's view. Furthermore, I needed to investigate commits `1bf70d08cc3b` (Mar 4, 2025) and `20117b5a4b9c6` (May 5, 2025), along with `1e44bedbc921a` (May 5, 2025). I recalled that even before `1bf70d08cc3b`, `elv_iosched_store` acquired `q->sysfs_lock` before calling `blk_mq_freeze_queue`, or vice versa. To confirm this, I planned to examine the `git-show` output for `f1be1788a32e~0:block/elevator.c` or use `git-log -p -n 3 20117b5a4b9c6~1 -- block/elevator.c`. The most direct approach would be to use `git-show` on `1bf70d08cc3b` and `20117b5a4b9c6~1`.
|
| 78/2 |
2026/12/01 13:22 |
tool |
git-show |
0m
Args: map[Commit:1bf70d08cc3b File:block/elevator.c]
Results: map[Output:commit 1bf70d08cc3b55abd1763e6dff5855cb8dd8318b
Author: Nilay Shroff <nilay@linux.ibm.com>
Date: Tue Mar 4 15:52:33 2025 +0530
block: introduce a dedicated lock for protecting queue elevator updates
A queue's elevator can be updated either when modifying nr_hw_queues
or through the sysfs scheduler attribute. Currently, elevator switching/
updating is protected using q->sysfs_lock, but this has led to lockdep
splats[1] due to inconsistent lock ordering between q->sysfs_lock and
the freeze-lock in multiple block layer call sites.
As the scope of q->sysfs_lock is not well-defined, its (mis)use has
resulted in numerous lockdep warnings. To address this, introduce a new
q->elevator_lock, dedicated specifically for protecting elevator
switches/updates. And we'd now use this new q->elevator_lock instead of
q->sysfs_lock for protecting elevator switches/updates.
While at it, make elv_iosched_load_module() a static function, as it is
only called from elv_iosched_store(). Also, remove redundant parameters
from elv_iosched_load_module() function signature.
[1] https://lore.kernel.org/all/67637e70.050a0220.3157ee.000c.GAE@google.com/
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
Link: https://lore.kernel.org/r/20250304102551.2533767-5-nilay@linux.ibm.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/block/elevator.c b/block/elevator.c
index 041f1d983bc7..b4d08026b02c 100644
--- a/block/elevator.c
+++ b/block/elevator.c
@@ -457,7 +457,7 @@ int elv_register_queue(struct request_queue *q, bool uevent)
struct elevator_queue *e = q->elevator;
int error;
- lockdep_assert_held(&q->sysfs_lock);
+ lockdep_assert_held(&q->elevator_lock);
error = kobject_add(&e->kobj, &q->disk->queue_kobj, "iosched");
if (!error) {
@@ -481,7 +481,7 @@ void elv_unregister_queue(struct request_queue *q)
{
struct elevator_queue *e = q->elevator;
- lockdep_assert_held(&q->sysfs_lock);
+ lockdep_assert_held(&q->elevator_lock);
if (e && test_and_clear_bit(ELEVATOR_FLAG_REGISTERED, &e->flags)) {
kobject_uevent(&e->kobj, KOBJ_REMOVE);
@@ -618,7 +618,7 @@ int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
unsigned int memflags;
int ret;
- lockdep_assert_held(&q->sysfs_lock);
+ lockdep_assert_held(&q->elevator_lock);
memflags = blk_mq_freeze_queue(q);
blk_mq_quiesce_queue(q);
@@ -655,7 +655,7 @@ void elevator_disable(struct request_queue *q)
{
unsigned int memflags;
- lockdep_assert_held(&q->sysfs_lock);
+ lockdep_assert_held(&q->elevator_lock);
memflags = blk_mq_freeze_queue(q);
blk_mq_quiesce_queue(q);
@@ -700,28 +700,23 @@ static int elevator_change(struct request_queue *q, const char *elevator_name)
return ret;
}
-void elv_iosched_load_module(struct gendisk *disk, const char *buf,
- size_t count)
+static void elv_iosched_load_module(char *elevator_name)
{
- char elevator_name[ELV_NAME_MAX];
struct elevator_type *found;
- const char *name;
-
- strscpy(elevator_name, buf, sizeof(elevator_name));
- name = strstrip(elevator_name);
spin_lock(&elv_list_lock);
- found = __elevator_find(name);
+ found = __elevator_find(elevator_name);
spin_unlock(&elv_list_lock);
if (!found)
- request_module("%s-iosched", name);
+ request_module("%s-iosched", elevator_name);
}
ssize_t elv_iosched_store(struct gendisk *disk, const char *buf,
size_t count)
{
char elevator_name[ELV_NAME_MAX];
+ char *name;
int ret;
unsigned int memflags;
struct request_queue *q = disk->queue;
@@ -731,16 +726,18 @@ ssize_t elv_iosched_store(struct gendisk *disk, const char *buf,
* queue to ensure that the module file can be read when the request
* queue is the one for the device storing the module file.
*/
- elv_iosched_load_module(disk, buf, count);
strscpy(elevator_name, buf, sizeof(elevator_name));
+ name = strstrip(elevator_name);
+
+ elv_iosched_load_module(name);
- mutex_lock(&q->sysfs_lock);
memflags = blk_mq_freeze_queue(q);
- ret = elevator_change(q, strstrip(elevator_name));
+ mutex_lock(&q->elevator_lock);
+ ret = elevator_change(q, name);
if (!ret)
ret = count;
+ mutex_unlock(&q->elevator_lock);
blk_mq_unfreeze_queue(q, memflags);
- mutex_unlock(&q->sysfs_lock);
return ret;
}
@@ -751,7 +748,7 @@ ssize_t elv_iosched_show(struct gendisk *disk, char *name)
struct elevator_type *cur = NULL, *e;
int len = 0;
- mutex_lock(&q->sysfs_lock);
+ mutex_lock(&q->elevator_lock);
if (!q->elevator) {
len += sprintf(name+len, "[none] ");
} else {
@@ -769,7 +766,7 @@ ssize_t elv_iosched_show(struct gendisk *disk, char *name)
spin_unlock(&elv_list_lock);
len += sprintf(name+len, "\n");
- mutex_unlock(&q->sysfs_lock);
+ mutex_unlock(&q->elevator_lock);
return len;
}
]
|
| 79/2 |
2026/12/01 13:22 |
tool |
git-show |
0m
Args: map[Commit:f8e111c859b9 File:block/elevator.c]
Results: map[Output:commit f8e111c859b92ee909f1676f90c791e7165d3860
Author: Ming Lei <ming.lei@redhat.com>
Date: Mon May 5 22:17:41 2025 +0800
block: don't call freeze queue in elevator_switch() and elevator_disable()
Both elevator_switch() and elevator_disable() are only called from the
two code paths, in which queue is guaranteed to be frozen.
So don't call freeze queue in the two functions, also add asserts for
queue freeze.
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Reviewed-by: Nilay Shroff <nilay@linux.ibm.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Yu Kuai <yukuai3@huawei.com>
Link: https://lore.kernel.org/r/20250505141805.2751237-4-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/block/elevator.c b/block/elevator.c
index b4d08026b02c..5051a98dc08c 100644
--- a/block/elevator.c
+++ b/block/elevator.c
@@ -615,12 +615,11 @@ void elevator_init_mq(struct request_queue *q)
*/
int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
{
- unsigned int memflags;
int ret;
+ WARN_ON_ONCE(q->mq_freeze_depth == 0);
lockdep_assert_held(&q->elevator_lock);
- memflags = blk_mq_freeze_queue(q);
blk_mq_quiesce_queue(q);
if (q->elevator) {
@@ -641,7 +640,6 @@ int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
out_unfreeze:
blk_mq_unquiesce_queue(q);
- blk_mq_unfreeze_queue(q, memflags);
if (ret) {
pr_warn("elv: switch to \"%s\" failed, falling back to \"none\"\n",
@@ -653,11 +651,9 @@ int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
void elevator_disable(struct request_queue *q)
{
- unsigned int memflags;
-
+ WARN_ON_ONCE(q->mq_freeze_depth == 0);
lockdep_assert_held(&q->elevator_lock);
- memflags = blk_mq_freeze_queue(q);
blk_mq_quiesce_queue(q);
elv_unregister_queue(q);
@@ -668,7 +664,6 @@ void elevator_disable(struct request_queue *q)
blk_add_trace_msg(q, "elv switch: none");
blk_mq_unquiesce_queue(q);
- blk_mq_unfreeze_queue(q, memflags);
}
/*
]
|
| 80/2 |
2026/12/01 13:22 |
tool |
git-log |
0m
Args: map[Count:5 PathPrefix:drivers/block/nbd.c Since:10 years]
Results: map[Output:553d28925fde nbd: Reclassify sockets in the reconnect path
326d49039c10 nbd: add pre_defined_connections module parameter for pre-created devices
a9d414b4a15c nbd: remove queue freeze for newly created nbd from netlink path
f8d21c590e55 nbd: factor out a nbd_genl_foreach_sock
db285268df79 nbd: skip queue freeze when setting size at device startup
]
|
| 81/2 |
2026/12/01 13:22 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 98102
output: 38
thoughts: 790
Thoughts: **My Thought Process on NBD Locking Dependencies**
Okay, so I was digging into the locking mechanisms within NBD, and this commit, `1bf70d08cc3b`, immediately caught my eye because it represented a significant shift. Before this commit, I observed that `elv_iosched_store()` was acquiring `q->sysfs_lock` and then proceeding down a path that involved `blk_mq_freeze_queue` and `blk_mq_quiesce_queue`, which ultimately touched `set->srcu`.
But the *after* picture with `1bf70d08cc3b` is different. Now, `blk_mq_freeze_queue` seems to be the entry point, and it's directly referencing `q->q_usage_counter(io)`. This then leads to `q->elevator_lock` and subsequently to `blk_mq_quiesce_queue` and `set->srcu`. What's crucial here is that the acquisition of `q->sysfs_lock` has changed its position relative to `q->q_usage_counter(io)`. It used to be *before* `blk_mq_freeze_queue`, meaning `q->q_usage_counter(io)` wasn't necessarily preceding `q->sysfs_lock`.
However, my deeper dive, specifically looking at the `elevator_switch` function, revealed that the sequence `memflags = blk_mq_freeze_queue(q);` followed by `blk_mq_quiesce_queue(q);` (which then accesses `set->srcu`) has been the consistent pattern. This means that `q->q_usage_counter(io)` *has* always been on the path leading to `set->srcu`, at least since commit `f1be1788a32e` in October 2024, and even before lockdep started tracking `q->q_usage_counter` explicitly.
Now, stepping back to see the larger context – the "BIG PICTURE" – I'm trying to understand the root cause of the reported circular locking dependency between NBD and user-space sockets (`sk_lock-AF_INET`). My analysis points to NBD's fundamental nature: it's a block device that relies on a socket. The block I/O path, specifically `nbd_queue_rq` and `recv_work`, acquires `sk_lock` while it already holds several block layer locks, including `&q->q_usage_counter(io)`, `set->srcu`, `&cmd->lock`, and `&nsock->tx_lock`.
The problem arises because normal user-space socket syscalls like `tcp_sendmsg` and `tcp_recvmsg` also acquire `sk_lock-AF_INET`. These operations can then lead to accesses to user memory via `__might_fault` and involve `&mm->mmap_lock`. Furthermore, page faults on mmaped files that reside on NBD can trigger a chain reaction: `sb_pagefaults` -> `i_mmap_lock` -> `btrfs-tree` -> and finally, `&q->q_usage_counter(io)`. This creates the circular dependency.
It's important to note that this fundamental issue – NBD sharing `sk_lock-AF_INET` with regular user sockets – has been present since NBD's inception, or at least since commits `560bc4b39952` and `f1be1788a32e` made `q_usage_counter(io)` visible to lockdep.
Looking at the proposed solutions, commit `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency") and `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock") seem to be addressing this by reclassifying sockets. To fully understand the implications and ensure correctness, I need to examine how other drivers in the kernel handle similar situations. My next step is to search for all other call sites of `sock_lock_init_class_and_name` and `sock_allow_reclassification` across the kernel source tree. I'll be grepping for these functions to gain insight into established patterns and best practices for socket reclassification.
|
| 82/2 |
2026/12/01 13:22 |
tool |
grepper |
0m
Args: map[Expression:sock_allow_reclassification|sock_lock_init_class_and_name]
Results: map[Output:drivers/block/nbd.c=1245=static void nbd_reclassify_socket(struct socket *sock)
--
drivers/block/nbd.c-1248-
drivers/block/nbd.c:1249: if (!sock_allow_reclassification(sk))
drivers/block/nbd.c-1250- return;
--
drivers/block/nbd.c-1253- case AF_INET:
drivers/block/nbd.c:1254: sock_lock_init_class_and_name(sk, "slock-AF_INET-NBD",
drivers/block/nbd.c-1255- &nbd_slock_key[0],
--
drivers/block/nbd.c-1259- case AF_INET6:
drivers/block/nbd.c:1260: sock_lock_init_class_and_name(sk, "slock-AF_INET6-NBD",
drivers/block/nbd.c-1261- &nbd_slock_key[1],
--
drivers/block/nbd.c-1265- case AF_UNIX:
drivers/block/nbd.c:1266: sock_lock_init_class_and_name(sk, "slock-AF_UNIX-NBD",
drivers/block/nbd.c-1267- &nbd_slock_key[2],
--
drivers/infiniband/sw/rxe/rxe_net.c=45=static inline void rxe_reclassify_recv_socket(struct socket *sock)
--
drivers/infiniband/sw/rxe/rxe_net.c-49-
drivers/infiniband/sw/rxe/rxe_net.c:50: if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
drivers/infiniband/sw/rxe/rxe_net.c-51- return;
--
drivers/infiniband/sw/rxe/rxe_net.c-54- case AF_INET:
drivers/infiniband/sw/rxe/rxe_net.c:55: sock_lock_init_class_and_name(sk,
drivers/infiniband/sw/rxe/rxe_net.c-56- "slock-AF_INET-RDMA-RXE-RECV",
--
drivers/infiniband/sw/rxe/rxe_net.c-61- case AF_INET6:
drivers/infiniband/sw/rxe/rxe_net.c:62: sock_lock_init_class_and_name(sk,
drivers/infiniband/sw/rxe/rxe_net.c-63- "slock-AF_INET6-RDMA-RXE-RECV",
--
drivers/infiniband/sw/rxe/rxe_net.c-72- /*
drivers/infiniband/sw/rxe/rxe_net.c:73: * sock_lock_init_class_and_name() calls
drivers/infiniband/sw/rxe/rxe_net.c-74- * sk_owner_set(sk, THIS_MODULE); in order
--
drivers/infiniband/sw/rxe/rxe_qp.c=37=static inline void rxe_reclassify_send_socket(struct socket *sock)
--
drivers/infiniband/sw/rxe/rxe_qp.c-41-
drivers/infiniband/sw/rxe/rxe_qp.c:42: if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
drivers/infiniband/sw/rxe/rxe_qp.c-43- return;
--
drivers/infiniband/sw/rxe/rxe_qp.c-46- case AF_INET:
drivers/infiniband/sw/rxe/rxe_qp.c:47: sock_lock_init_class_and_name(sk,
drivers/infiniband/sw/rxe/rxe_qp.c-48- "slock-AF_INET-RDMA-RXE-SEND",
--
drivers/infiniband/sw/rxe/rxe_qp.c-53- case AF_INET6:
drivers/infiniband/sw/rxe/rxe_qp.c:54: sock_lock_init_class_and_name(sk,
drivers/infiniband/sw/rxe/rxe_qp.c-55- "slock-AF_INET6-RDMA-RXE-SEND",
--
drivers/infiniband/sw/siw/siw_cm.c=62=static inline void siw_reclassify_socket(struct socket *sock)
--
drivers/infiniband/sw/siw/siw_cm.c-66-
drivers/infiniband/sw/siw/siw_cm.c:67: if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
drivers/infiniband/sw/siw/siw_cm.c-68- return;
--
drivers/infiniband/sw/siw/siw_cm.c-71- case AF_INET:
drivers/infiniband/sw/siw/siw_cm.c:72: sock_lock_init_class_and_name(sk,
drivers/infiniband/sw/siw/siw_cm.c-73- "slock-AF_INET-RDMA-SIW",
--
drivers/infiniband/sw/siw/siw_cm.c-78- case AF_INET6:
drivers/infiniband/sw/siw/siw_cm.c:79: sock_lock_init_class_and_name(sk,
drivers/infiniband/sw/siw/siw_cm.c-80- "slock-AF_INET6-RDMA-SIW",
--
drivers/nvme/host/tcp.c=228=static void nvme_tcp_reclassify_socket(struct socket *sock)
--
drivers/nvme/host/tcp.c-231-
drivers/nvme/host/tcp.c:232: if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
drivers/nvme/host/tcp.c-233- return;
--
drivers/nvme/host/tcp.c-236- case AF_INET:
drivers/nvme/host/tcp.c:237: sock_lock_init_class_and_name(sk, "slock-AF_INET-NVME",
drivers/nvme/host/tcp.c-238- &nvme_tcp_slock_key[0],
--
drivers/nvme/host/tcp.c-242- case AF_INET6:
drivers/nvme/host/tcp.c:243: sock_lock_init_class_and_name(sk, "slock-AF_INET6-NVME",
drivers/nvme/host/tcp.c-244- &nvme_tcp_slock_key[1],
--
fs/smb/client/connect.c=3144=cifs_reclassify_socket4(struct socket *sock)
--
fs/smb/client/connect.c-3147-
fs/smb/client/connect.c:3148: BUG_ON(!sock_allow_reclassification(sk));
fs/smb/client/connect.c:3149: sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
fs/smb/client/connect.c-3150- &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
--
fs/smb/client/connect.c=3154=cifs_reclassify_socket6(struct socket *sock)
--
fs/smb/client/connect.c-3157-
fs/smb/client/connect.c:3158: BUG_ON(!sock_allow_reclassification(sk));
fs/smb/client/connect.c:3159: sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
fs/smb/client/connect.c-3160- &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
--
include/net/sock.h=242=struct sk_filter;
--
include/net/sock.h-363- * @sk_owner: reference to the real owner of the socket that calls
include/net/sock.h:364: * sock_lock_init_class_and_name().
include/net/sock.h-365- */
--
include/net/sock.h=1683=static inline void sk_owner_put(struct sock *sk)
--
include/net/sock.h-1693- */
include/net/sock.h:1694:#define sock_lock_init_class_and_name(sk, sname, skey, name, key) \
include/net/sock.h-1695-do { \
--
include/net/sock.h=1825=static inline void sock_release_ownership(struct sock *sk)
--
include/net/sock.h-1834-/* no reclassification while locks are held */
include/net/sock.h:1835:static inline bool sock_allow_reclassification(const struct sock *csk)
include/net/sock.h-1836-{
--
net/bluetooth/af_bluetooth.c=71=void bt_sock_reclassify_lock(struct sock *sk, int proto)
--
net/bluetooth/af_bluetooth.c-73- BUG_ON(!sk);
net/bluetooth/af_bluetooth.c:74: BUG_ON(!sock_allow_reclassification(sk));
net/bluetooth/af_bluetooth.c-75-
net/bluetooth/af_bluetooth.c:76: sock_lock_init_class_and_name(sk,
net/bluetooth/af_bluetooth.c-77- bt_slock_key_strings[proto], &bt_slock_key[proto],
--
net/core/sock.c=2216=static inline void sock_lock_init(struct sock *sk)
--
net/core/sock.c-2220- if (sk->sk_kern_sock)
net/core/sock.c:2221: sock_lock_init_class_and_name(
net/core/sock.c-2222- sk,
--
net/core/sock.c-2227- else
net/core/sock.c:2228: sock_lock_init_class_and_name(
net/core/sock.c-2229- sk,
--
net/mptcp/pm_kernel.c=852=static int mptcp_pm_nl_create_listen_socket(struct sock *sk,
--
net/mptcp/pm_kernel.c-876- */
net/mptcp/pm_kernel.c:877: sock_lock_init_class_and_name(newsk,
net/mptcp/pm_kernel.c-878- is_ipv6 ? "mlock-AF_INET6" : "mlock-AF_INET",
--
net/smc/af_smc.c=395=void smc_sk_init(struct net *net, struct sock *sk, int protocol)
--
net/smc/af_smc.c-407- INIT_LIST_HEAD(&smc->accept_q);
net/smc/af_smc.c:408: sock_lock_init_class_and_name(sk, "slock-AF_SMC", &smc_slock_key,
net/smc/af_smc.c-409- "sk_lock-AF_SMC", &smc_key);
--
net/sunrpc/svcsock.c=95=static void svc_reclassify_socket(struct socket *sock)
--
net/sunrpc/svcsock.c-98-
net/sunrpc/svcsock.c:99: if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
net/sunrpc/svcsock.c-100- return;
--
net/sunrpc/svcsock.c-103- case AF_INET:
net/sunrpc/svcsock.c:104: sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
net/sunrpc/svcsock.c-105- &svc_slock_key[0],
--
net/sunrpc/svcsock.c-110- case AF_INET6:
net/sunrpc/svcsock.c:111: sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
net/sunrpc/svcsock.c-112- &svc_slock_key[1],
--
net/sunrpc/xprtsock.c=1891=static inline void xs_reclassify_socketu(struct socket *sock)
--
net/sunrpc/xprtsock.c-1894-
net/sunrpc/xprtsock.c:1895: sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
net/sunrpc/xprtsock.c-1896- &xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]);
--
net/sunrpc/xprtsock.c=1899=static inline void xs_reclassify_socket4(struct socket *sock)
--
net/sunrpc/xprtsock.c-1902-
net/sunrpc/xprtsock.c:1903: sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
net/sunrpc/xprtsock.c-1904- &xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]);
--
net/sunrpc/xprtsock.c=1907=static inline void xs_reclassify_socket6(struct socket *sock)
--
net/sunrpc/xprtsock.c-1910-
net/sunrpc/xprtsock.c:1911: sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
net/sunrpc/xprtsock.c-1912- &xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]);
--
net/sunrpc/xprtsock.c=1915=static inline void xs_reclassify_socket(int family, struct socket *sock)
net/sunrpc/xprtsock.c-1916-{
net/sunrpc/xprtsock.c:1917: if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
net/sunrpc/xprtsock.c-1918- return;
]
|
| 83/2 |
2026/12/01 13:22 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 102849
output: 42
thoughts: 2527
Thoughts: **It appears the syzkaller crash I'm looking at is a lockdep issue, and the root cause might be older than I initially suspected.**
My immediate reaction was to scrutinize the usage of `sock_allow_reclassification` and `sock_lock_init_class_and_name` across different kernel modules. I observed that `nvme_tcp_reclassify_socket` and `cifs_reclassify_socket4/6` use `WARN_ON_ONCE` or `BUG_ON` respectively when `sock_allow_reclassification` returns false, and these are for kernel-created sockets. `svc_reclassify_socket` also uses `WARN_ON_ONCE`, but I need to investigate its interaction with user file descriptors further. What's particularly striking is that `nbd_reclassify_socket` is the *only* one that had a change (`9280e6edf656`) which removed `WARN_ON_ONCE`, leaving only `if (!sock_allow_reclassification(sk)) return;`. This stands out.
Then, I dug into the git history around the specific crash commit (`19cc08c1a6a650f8e54c0461ff198be6448f425d`). My initial thought was that the commit `f1be1788a32e`, which added `io_lockdep_map`, might be the culprit. However, I quickly remembered Rule 4 of lockdep analysis: don't blame commits that merely add debugging checks unless the check itself is flawed.
This led me to question whether `f1be1788a32e` was exposing an *existing* bug or if the lockdep cycle itself was a false positive, only becoming a real deadlock if userspace interacted with the *same* socket. I hypothesized that if socket S1 (e.g., the NBD socket, `sk_lock-AF_INET-NBD`) and socket S2 (a regular TCP socket, `sk_lock-AF_INET`) were involved, and S1 wasn't reclassified correctly (staying `sk_lock-AF_INET`), lockdep might report a false positive. A *real* deadlock, however, would occur if userspace operated on S1 itself, even if it *was* reclassified to `sk_lock-AF_INET-NBD`, because the user would still hold the file descriptor.
Looking at the crash report, I saw that the stack trace showed `#7` as `sk_lock-AF_INET` (not `sk_lock-AF_INET-NBD`) during `nbd_mark_nsock_dead` and `inet_shutdown`. Crucially, `#8` also involved `sk_lock-AF_INET` via `tcp_sendmsg_locked`, and further down the trace, page faults related to Btrfs indicated interaction with `i_mmap_lock`. This confirmed my suspicion: because the NBD socket was still classified as `sk_lock-AF_INET` instead of the more specific `sk_lock-AF_INET-NBD`, lockdep connected the NBD socket's lock acquisition (`#7`) with the generic `sk_lock-AF_INET` lock acquisition during `tcp_sendmsg_locked` (`#8`), creating the perceived cycle.
The "million dollar question" then became: which commit actually *introduced* this bug? I meticulously reviewed the commits related to NBD socket reclassification: `d532cddb6c60`, `9280e6edf656`, and `553d28925fde`, as well as older commits like `560bc4b39952`. My critical realization was that *before* `d532cddb6c60`, NBD sockets were *never* reclassified, meaning they were *always* in `sk_lock-AF_INET`. This strongly suggests that the lockdep cycle I'm seeing already *existed* prior to `d532cddb6c60`.
I then proceeded to verify if *every single link* in the reported lockdep cycle was present *before* `d532cddb6c60`. This involved checking the history of functions involved in the cycle: `blk_mq_submit_bio`, `blk_mq_freeze_queue`, `btrfs_fallocate` (and its associated locks like `i_mmap_lock`), `nbd_queue_rq`, `tcp_sendmsg_locked`, and `nbd_mark_nsock_dead`. Based on the commit timestamps I examined, it appears that all these individual locking patterns were indeed in place well before the NBD reclassification efforts began.
My current line of thinking is that `d532cddb6c60` and subsequent commits might have *exposed* or *made more likely* an existing, latent lockdep issue by altering the NBD socket's classification, rather than *introducing* the fundamental cycle itself. The bug, as it manifests in this syzkaller crash, seems to be a consequence of the generic `sk_lock-AF_INET` being used for the NBD socket, which then creates a perceived circular dependency with other operations that also use `sk_lock-AF_INET` (or locks that are transitively related to it) in a context where a specific NBD reclassification would have broken the cycle. I am now focused on confirming that no link in the cycle was *newly* introduced by a commit between `d532cddb6c60~1` and the crash, and if all links predate `d532cddb6c60`, then the bug's origin is much older.
|
| 84/2 |
2026/12/01 13:23 |
tool |
git-log |
0m
Args: map[Count:30 PathPrefix:drivers/block/nbd.c Since:2 years]
Results: map[Output:553d28925fde nbd: Reclassify sockets in the reconnect path
326d49039c10 nbd: add pre_defined_connections module parameter for pre-created devices
a9d414b4a15c nbd: remove queue freeze for newly created nbd from netlink path
f8d21c590e55 nbd: factor out a nbd_genl_foreach_sock
db285268df79 nbd: skip queue freeze when setting size at device startup
285908f554fe nbd: remove queue freeze in nbd_add_socket
0fdee7c5fa88 nbd: clear queue limits on disconnect
04d8fb23e520 nbd: disallow NBD_SET_SOCK on an active device
fe247030f1ce nbd: simplify find_fallback() by removing redundant logic
9280e6edf656 nbd: don't warn when reclassifying a busy socket lock
d532cddb6c60 nbd: Reclassify sockets to avoid lockdep circular dependency
b040a1a4523d block: switch numa_node to int in blk_mq_hw_ctx and init_request
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
1649714b930f nbd: defer config unlock in nbd_genl_connect
9517b82d8d42 nbd: defer config put in recv_work
4601b7923d1b nbd: don't copy kernel creds
81ccca31214e nbd: override creds to kernel when calling sock_{send,recv}msg()
51723bf92679 drivers/block: replace use of system_wq with system_percpu_wq
9f7c02e03157 nbd: restrict sockets to TCP and UDP
8b428f42f3ed nbd: fix lockdep deadlock warning
aa9552438ebf nbd: fix uaf in nbd_genl_connect() error path
1e1a9cecfab3 block: force noio scope in blk_mq_freeze_queue
8337b029f788 nbd: fix partial sending
f3dec61d7544 nbd: fix queue freeze vs limits lock order
844b8cdc6816 nbd: don't allow reconnect after disconnect
cc76ace465d6 block: remove BLK_MQ_F_SHOULD_MERGE
]
|
| 85/2 |
2026/12/01 13:23 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 106099
output: 85
thoughts: 73
Thoughts: **Navigating the Labyrinth of Kernel Development: A Dive into Lockdep Deadlock Resolution**
Alright, my focus has just landed on this commit: `8b428f42f3ed nbd: fix lockdep deadlock warning`. This immediately grabs my attention because, as anyone deep in kernel development knows, lockdep warnings are often intricate, subtle beasts. They point to potential race conditions that can manifest in unpredictable and devastating ways. The fact that this is related to `nbd` (Network Block Device) is also noteworthy; it's an area with its own set of concurrency challenges, especially when dealing with asynchronous operations and interactions with the block layer.
My immediate instinct is to peel back the layers of this specific fix. To do that, I need to understand the context. So, I'll be pulling up `git show 8b428f42f3ed` to get the full picture of *this* commit. What exactly changed? What was the specific locking scenario that triggered the warning? I'll be looking for the precise lines of code that were modified, the added or removed `mutex_lock`/`unlock` calls, or perhaps changes to the locking hierarchy.
But one commit rarely exists in a vacuum. To truly grasp the significance and the *why* behind this fix, I need to see what led up to it and what it directly impacts. That's why I'm also going to pull up `git show f3dec61d7544`. This commit likely represents a previous state of the code, perhaps one where the deadlock *wasn't* present, or where a related issue was addressed. It might shed light on the evolution of the locking strategy in the `nbd` subsystem. Understanding the preceding change could reveal if the current fix is addressing a regression introduced by `f3dec61d7544`, or if it's a more fundamental re-architecting of the locking.
Then, to understand the immediate consequence or the next step in this particular chain of events, I'll examine `git show 1e1a9cecfab3`. This commit likely builds upon `8b428f42f3ed`. It could be that `8b428f42f3ed` is the initial fix, and `1e1a9cecfab3` is a subsequent refinement, a further optimization, or perhaps a patch that addresses a related but distinct issue that was uncovered as a consequence of the first fix. Seeing these three commits in sequence – the historical context, the current fix, and its immediate successor – will allow me to trace the problem, the solution, and the ongoing refinement of the code. It's a methodical approach, essential for understanding the nuances of kernel concurrency and for ensuring the robustness of such a critical subsystem.
|
| 86/2 |
2026/12/01 13:23 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 8b428f42f3edfd62422aa7ad87049ab232a2eaa9
Author: Ming Lei <ming.lei@redhat.com>
Date: Wed Jul 9 19:17:44 2025 +0800
nbd: fix lockdep deadlock warning
nbd grabs device lock nbd->config_lock for updating nr_hw_queues, this
ways cause the following lock dependency:
-> #2 (&disk->open_mutex){+.+.}-{4:4}:
lock_acquire kernel/locking/lockdep.c:5871 [inline]
lock_acquire+0x1ac/0x448 kernel/locking/lockdep.c:5828
__mutex_lock_common kernel/locking/mutex.c:602 [inline]
__mutex_lock+0x166/0x1292 kernel/locking/mutex.c:747
mutex_lock_nested+0x14/0x1c kernel/locking/mutex.c:799
__del_gendisk+0x132/0xac6 block/genhd.c:706
del_gendisk+0xf6/0x19a block/genhd.c:819
nbd_dev_remove+0x3c/0xf2 drivers/block/nbd.c:268
nbd_dev_remove_work+0x1c/0x26 drivers/block/nbd.c:284
process_one_work+0x96a/0x1f32 kernel/workqueue.c:3238
process_scheduled_works kernel/workqueue.c:3321 [inline]
worker_thread+0x5ce/0xde8 kernel/workqueue.c:3402
kthread+0x39c/0x7d4 kernel/kthread.c:464
ret_from_fork_kernel+0x2a/0xbb2 arch/riscv/kernel/process.c:214
ret_from_fork_kernel_asm+0x16/0x18 arch/riscv/kernel/entry.S:327
-> #1 (&set->update_nr_hwq_lock){++++}-{4:4}:
lock_acquire kernel/locking/lockdep.c:5871 [inline]
lock_acquire+0x1ac/0x448 kernel/locking/lockdep.c:5828
down_write+0x9c/0x19a kernel/locking/rwsem.c:1577
blk_mq_update_nr_hw_queues+0x3e/0xb86 block/blk-mq.c:5041
nbd_start_device+0x140/0xb2c drivers/block/nbd.c:1476
nbd_genl_connect+0xae0/0x1b24 drivers/block/nbd.c:2201
genl_family_rcv_msg_doit+0x206/0x2e6 net/netlink/genetlink.c:1115
genl_family_rcv_msg net/netlink/genetlink.c:1195 [inline]
genl_rcv_msg+0x514/0x78e net/netlink/genetlink.c:1210
netlink_rcv_skb+0x206/0x3be net/netlink/af_netlink.c:2534
genl_rcv+0x36/0x4c net/netlink/genetlink.c:1219
netlink_unicast_kernel net/netlink/af_netlink.c:1313 [inline]
netlink_unicast+0x4f0/0x82c net/netlink/af_netlink.c:1339
netlink_sendmsg+0x85e/0xdd6 net/netlink/af_netlink.c:1883
sock_sendmsg_nosec net/socket.c:712 [inline]
__sock_sendmsg+0xcc/0x160 net/socket.c:727
____sys_sendmsg+0x63e/0x79c net/socket.c:2566
___sys_sendmsg+0x144/0x1e6 net/socket.c:2620
__sys_sendmsg+0x188/0x246 net/socket.c:2652
__do_sys_sendmsg net/socket.c:2657 [inline]
__se_sys_sendmsg net/socket.c:2655 [inline]
__riscv_sys_sendmsg+0x70/0xa2 net/socket.c:2655
syscall_handler+0x94/0x118 arch/riscv/include/asm/syscall.h:112
do_trap_ecall_u+0x396/0x530 arch/riscv/kernel/traps.c:341
handle_exception+0x146/0x152 arch/riscv/kernel/entry.S:197
-> #0 (&nbd->config_lock){+.+.}-{4:4}:
check_noncircular+0x132/0x146 kernel/locking/lockdep.c:2178
check_prev_add kernel/locking/lockdep.c:3168 [inline]
check_prevs_add kernel/locking/lockdep.c:3287 [inline]
validate_chain kernel/locking/lockdep.c:3911 [inline]
__lock_acquire+0x12b2/0x24ea kernel/locking/lockdep.c:5240
lock_acquire kernel/locking/lockdep.c:5871 [inline]
lock_acquire+0x1ac/0x448 kernel/locking/lockdep.c:5828
__mutex_lock_common kernel/locking/mutex.c:602 [inline]
__mutex_lock+0x166/0x1292 kernel/locking/mutex.c:747
mutex_lock_nested+0x14/0x1c kernel/locking/mutex.c:799
refcount_dec_and_mutex_lock+0x60/0xd8 lib/refcount.c:118
nbd_config_put+0x3a/0x610 drivers/block/nbd.c:1423
nbd_release+0x94/0x15c drivers/block/nbd.c:1735
blkdev_put_whole+0xac/0xee block/bdev.c:721
bdev_release+0x3fe/0x600 block/bdev.c:1144
blkdev_release+0x1a/0x26 block/fops.c:684
__fput+0x382/0xa8c fs/file_table.c:465
____fput+0x1c/0x26 fs/file_table.c:493
task_work_run+0x16a/0x25e kernel/task_work.c:227
resume_user_mode_work include/linux/resume_user_mode.h:50 [inline]
exit_to_user_mode_loop+0x118/0x134 kernel/entry/common.c:114
exit_to_user_mode_prepare include/linux/entry-common.h:330 [inline]
syscall_exit_to_user_mode_work include/linux/entry-common.h:414 [inline]
syscall_exit_to_user_mode include/linux/entry-common.h:449 [inline]
do_trap_ecall_u+0x3f0/0x530 arch/riscv/kernel/traps.c:355
handle_exception+0x146/0x152 arch/riscv/kernel/entry.S:197
Also it isn't necessary to require nbd->config_lock, because
blk_mq_update_nr_hw_queues() does grab tagset lock for sync everything.
Fixes the issue by releasing ->config_lock & retry in case of concurrent
updating nr_hw_queues.
Fixes: 98e68f67020c ("block: prevent adding/deleting disk during updating nr_hw_queues")
Reported-by: syzbot+2bcecf3c38cb3e8fdc8d@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/6855034f.a00a0220.137b3.0031.GAE@google.com
Reviewed-by: Yu Kuai <yukuai3@huawei.com>
Cc: Nilay Shroff <nilay@linux.ibm.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Nilay Shroff <nilay@linux.ibm.com>
Link: https://lore.kernel.org/r/20250709111744.2353050-1-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 7bdc7eb808ea..709bad75c47b 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1473,7 +1473,17 @@ static int nbd_start_device(struct nbd_device *nbd)
return -EINVAL;
}
- blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
+retry:
+ mutex_unlock(&nbd->config_lock);
+ blk_mq_update_nr_hw_queues(&nbd->tag_set, num_connections);
+ mutex_lock(&nbd->config_lock);
+
+ /* if another code path updated nr_hw_queues, retry until succeed */
+ if (num_connections != config->num_connections) {
+ num_connections = config->num_connections;
+ goto retry;
+ }
+
nbd->pid = task_pid_nr(current);
nbd_parse_flags(nbd);
]
|
| 87/2 |
2026/12/01 13:23 |
tool |
git-show |
0m
Args:
Results: map[Output:commit f3dec61d7544a90685f1dd9a87fd4afc751996d0
Author: Christoph Hellwig <hch@lst.de>
Date: Fri Jan 10 06:47:16 2025 +0100
nbd: fix queue freeze vs limits lock order
Match the locking order used by the core block code by only freezing
the queue after taking the limits lock using the
queue_limits_commit_update_frozen helper.
This also allows removes the need for the separate __nbd_set_size helper,
so remove it.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
Reviewed-by: Nilay Shroff <nilay@linux.ibm.com>
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Link: https://lore.kernel.org/r/20250110054726.1499538-9-hch@lst.de
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 259bd57fc529..efa05c3c06bf 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -327,8 +327,7 @@ static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
nsock->sent = 0;
}
-static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
- loff_t blksize)
+static int nbd_set_size(struct nbd_device *nbd, loff_t bytesize, loff_t blksize)
{
struct queue_limits lim;
int error;
@@ -368,7 +367,7 @@ static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
lim.logical_block_size = blksize;
lim.physical_block_size = blksize;
- error = queue_limits_commit_update(nbd->disk->queue, &lim);
+ error = queue_limits_commit_update_frozen(nbd->disk->queue, &lim);
if (error)
return error;
@@ -379,18 +378,6 @@ static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
return 0;
}
-static int nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
- loff_t blksize)
-{
- int error;
-
- blk_mq_freeze_queue(nbd->disk->queue);
- error = __nbd_set_size(nbd, bytesize, blksize);
- blk_mq_unfreeze_queue(nbd->disk->queue);
-
- return error;
-}
-
static void nbd_complete_rq(struct request *req)
{
struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
]
|
| 88/2 |
2026/12/01 13:23 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 1e1a9cecfab3f22ebef0a976f849c87be8d03c1c
Author: Christoph Hellwig <hch@lst.de>
Date: Fri Jan 31 13:03:47 2025 +0100
block: force noio scope in blk_mq_freeze_queue
When block drivers or the core block code perform allocations with a
frozen queue, this could try to recurse into the block device to
reclaim memory and deadlock. Thus all allocations done by a process
that froze a queue need to be done without __GFP_IO and __GFP_FS.
Instead of tying to track all of them down, force a noio scope as
part of freezing the queue.
Note that nvme is a bit of a mess here due to the non-owner freezes,
and they will be addressed separately.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20250131120352.1315351-2-hch@lst.de
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
index 45a395862fbc..c795fa3a30e1 100644
--- a/block/blk-cgroup.c
+++ b/block/blk-cgroup.c
@@ -1545,6 +1545,7 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)
struct request_queue *q = disk->queue;
struct blkg_policy_data *pd_prealloc = NULL;
struct blkcg_gq *blkg, *pinned_blkg = NULL;
+ unsigned int memflags;
int ret;
if (blkcg_policy_enabled(q, pol))
@@ -1559,7 +1560,7 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)
return -EINVAL;
if (queue_is_mq(q))
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
retry:
spin_lock_irq(&q->queue_lock);
@@ -1623,7 +1624,7 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)
spin_unlock_irq(&q->queue_lock);
out:
if (queue_is_mq(q))
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
if (pinned_blkg)
blkg_put(pinned_blkg);
if (pd_prealloc)
@@ -1667,12 +1668,13 @@ void blkcg_deactivate_policy(struct gendisk *disk,
{
struct request_queue *q = disk->queue;
struct blkcg_gq *blkg;
+ unsigned int memflags;
if (!blkcg_policy_enabled(q, pol))
return;
if (queue_is_mq(q))
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
mutex_lock(&q->blkcg_mutex);
spin_lock_irq(&q->queue_lock);
@@ -1696,7 +1698,7 @@ void blkcg_deactivate_policy(struct gendisk *disk,
mutex_unlock(&q->blkcg_mutex);
if (queue_is_mq(q))
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
}
EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
diff --git a/block/blk-iocost.c b/block/blk-iocost.c
index a5894ec9696e..65a1d4427ccf 100644
--- a/block/blk-iocost.c
+++ b/block/blk-iocost.c
@@ -3224,6 +3224,7 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,
u32 qos[NR_QOS_PARAMS];
bool enable, user;
char *body, *p;
+ unsigned int memflags;
int ret;
blkg_conf_init(&ctx, input);
@@ -3247,7 +3248,7 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,
ioc = q_to_ioc(disk->queue);
}
- blk_mq_freeze_queue(disk->queue);
+ memflags = blk_mq_freeze_queue(disk->queue);
blk_mq_quiesce_queue(disk->queue);
spin_lock_irq(&ioc->lock);
@@ -3347,7 +3348,7 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,
wbt_enable_default(disk);
blk_mq_unquiesce_queue(disk->queue);
- blk_mq_unfreeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
blkg_conf_exit(&ctx);
return nbytes;
@@ -3355,7 +3356,7 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,
spin_unlock_irq(&ioc->lock);
blk_mq_unquiesce_queue(disk->queue);
- blk_mq_unfreeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
ret = -EINVAL;
err:
@@ -3414,6 +3415,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
{
struct blkg_conf_ctx ctx;
struct request_queue *q;
+ unsigned int memflags;
struct ioc *ioc;
u64 u[NR_I_LCOEFS];
bool user;
@@ -3441,7 +3443,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
ioc = q_to_ioc(q);
}
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
blk_mq_quiesce_queue(q);
spin_lock_irq(&ioc->lock);
@@ -3493,7 +3495,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
spin_unlock_irq(&ioc->lock);
blk_mq_unquiesce_queue(q);
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
blkg_conf_exit(&ctx);
return nbytes;
@@ -3502,7 +3504,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
spin_unlock_irq(&ioc->lock);
blk_mq_unquiesce_queue(q);
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
ret = -EINVAL;
err:
diff --git a/block/blk-iolatency.c b/block/blk-iolatency.c
index ebb522788d97..42c1e0b9a68f 100644
--- a/block/blk-iolatency.c
+++ b/block/blk-iolatency.c
@@ -749,9 +749,11 @@ static void blkiolatency_enable_work_fn(struct work_struct *work)
*/
enabled = atomic_read(&blkiolat->enable_cnt);
if (enabled != blkiolat->enabled) {
- blk_mq_freeze_queue(blkiolat->rqos.disk->queue);
+ unsigned int memflags;
+
+ memflags = blk_mq_freeze_queue(blkiolat->rqos.disk->queue);
blkiolat->enabled = enabled;
- blk_mq_unfreeze_queue(blkiolat->rqos.disk->queue);
+ blk_mq_unfreeze_queue(blkiolat->rqos.disk->queue, memflags);
}
}
diff --git a/block/blk-mq.c b/block/blk-mq.c
index da39a1cac702..40490ac88045 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -210,12 +210,12 @@ int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
}
EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
-void blk_mq_freeze_queue(struct request_queue *q)
+void blk_mq_freeze_queue_nomemsave(struct request_queue *q)
{
blk_freeze_queue_start(q);
blk_mq_freeze_queue_wait(q);
}
-EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
+EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_nomemsave);
bool __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)
{
@@ -236,12 +236,12 @@ bool __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)
return unfreeze;
}
-void blk_mq_unfreeze_queue(struct request_queue *q)
+void blk_mq_unfreeze_queue_nomemrestore(struct request_queue *q)
{
if (__blk_mq_unfreeze_queue(q, false))
blk_unfreeze_release_lock(q);
}
-EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
+EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue_nomemrestore);
/*
* non_owner variant of blk_freeze_queue_start
@@ -4223,13 +4223,14 @@ static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set,
bool shared)
{
struct request_queue *q;
+ unsigned int memflags;
lockdep_assert_held(&set->tag_list_lock);
list_for_each_entry(q, &set->tag_list, tag_set_list) {
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
queue_set_hctx_shared(q, shared);
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
}
}
@@ -4992,6 +4993,7 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
struct request_queue *q;
LIST_HEAD(head);
int prev_nr_hw_queues = set->nr_hw_queues;
+ unsigned int memflags;
int i;
lockdep_assert_held(&set->tag_list_lock);
@@ -5003,8 +5005,10 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
return;
+ memflags = memalloc_noio_save();
list_for_each_entry(q, &set->tag_list, tag_set_list)
- blk_mq_freeze_queue(q);
+ blk_mq_freeze_queue_nomemsave(q);
+
/*
* Switch IO scheduler to 'none', cleaning up the data associated
* with the previous scheduler. We will switch back once we are done
@@ -5052,7 +5056,8 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
blk_mq_elv_switch_back(&head, q);
list_for_each_entry(q, &set->tag_list, tag_set_list)
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue_nomemrestore(q);
+ memalloc_noio_restore(memflags);
/* Free the excess tags when nr_hw_queues shrink. */
for (i = set->nr_hw_queues; i < prev_nr_hw_queues; i++)
diff --git a/block/blk-pm.c b/block/blk-pm.c
index 42e842074715..8d3e052f91da 100644
--- a/block/blk-pm.c
+++ b/block/blk-pm.c
@@ -89,7 +89,7 @@ int blk_pre_runtime_suspend(struct request_queue *q)
if (percpu_ref_is_zero(&q->q_usage_counter))
ret = 0;
/* Switch q_usage_counter back to per-cpu mode. */
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue_nomemrestore(q);
if (ret < 0) {
spin_lock_irq(&q->queue_lock);
diff --git a/block/blk-rq-qos.c b/block/blk-rq-qos.c
index eb9618cd68ad..d4d4f4dc0e23 100644
--- a/block/blk-rq-qos.c
+++ b/block/blk-rq-qos.c
@@ -299,6 +299,7 @@ int rq_qos_add(struct rq_qos *rqos, struct gendisk *disk, enum rq_qos_id id,
const struct rq_qos_ops *ops)
{
struct request_queue *q = disk->queue;
+ unsigned int memflags;
lockdep_assert_held(&q->rq_qos_mutex);
@@ -310,14 +311,14 @@ int rq_qos_add(struct rq_qos *rqos, struct gendisk *disk, enum rq_qos_id id,
* No IO can be in-flight when adding rqos, so freeze queue, which
* is fine since we only support rq_qos for blk-mq queue.
*/
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
if (rq_qos_id(q, rqos->id))
goto ebusy;
rqos->next = q->rq_qos;
q->rq_qos = rqos;
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
if (rqos->ops->debugfs_attrs) {
mutex_lock(&q->debugfs_mutex);
@@ -327,7 +328,7 @@ int rq_qos_add(struct rq_qos *rqos, struct gendisk *disk, enum rq_qos_id id,
return 0;
ebusy:
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
return -EBUSY;
}
@@ -335,17 +336,18 @@ void rq_qos_del(struct rq_qos *rqos)
{
struct request_queue *q = rqos->disk->queue;
struct rq_qos **cur;
+ unsigned int memflags;
lockdep_assert_held(&q->rq_qos_mutex);
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
for (cur = &q->rq_qos; *cur; cur = &(*cur)->next) {
if (*cur == rqos) {
*cur = rqos->next;
break;
}
}
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
mutex_lock(&q->debugfs_mutex);
blk_mq_debugfs_unregister_rqos(rqos);
diff --git a/block/blk-settings.c b/block/blk-settings.c
index db12396ff5c7..c44dadc35e1e 100644
--- a/block/blk-settings.c
+++ b/block/blk-settings.c
@@ -461,11 +461,12 @@ EXPORT_SYMBOL_GPL(queue_limits_commit_update);
int queue_limits_commit_update_frozen(struct request_queue *q,
struct queue_limits *lim)
{
+ unsigned int memflags;
int ret;
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
ret = queue_limits_commit_update(q, lim);
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
return ret;
}
diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c
index 7b970e6765e7..6f548a4376aa 100644
--- a/block/blk-sysfs.c
+++ b/block/blk-sysfs.c
@@ -681,7 +681,7 @@ queue_attr_store(struct kobject *kobj, struct attribute *attr,
struct queue_sysfs_entry *entry = to_queue(attr);
struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj);
struct request_queue *q = disk->queue;
- unsigned int noio_flag;
+ unsigned int memflags;
ssize_t res;
if (!entry->store_limit && !entry->store)
@@ -711,11 +711,9 @@ queue_attr_store(struct kobject *kobj, struct attribute *attr,
}
mutex_lock(&q->sysfs_lock);
- blk_mq_freeze_queue(q);
- noio_flag = memalloc_noio_save();
+ memflags = blk_mq_freeze_queue(q);
res = entry->store(disk, page, length);
- memalloc_noio_restore(noio_flag);
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
mutex_unlock(&q->sysfs_lock);
return res;
}
diff --git a/block/blk-throttle.c b/block/blk-throttle.c
index 82dbaefcfa3b..8d149aff9fd0 100644
--- a/block/blk-throttle.c
+++ b/block/blk-throttle.c
@@ -1202,6 +1202,7 @@ static int blk_throtl_init(struct gendisk *disk)
{
struct request_queue *q = disk->queue;
struct throtl_data *td;
+ unsigned int memflags;
int ret;
td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
@@ -1215,7 +1216,7 @@ static int blk_throtl_init(struct gendisk *disk)
* Freeze queue before activating policy, to synchronize with IO path,
* which is protected by 'q_usage_counter'.
*/
- blk_mq_freeze_queue(disk->queue);
+ memflags = blk_mq_freeze_queue(disk->queue);
blk_mq_quiesce_queue(disk->queue);
q->td = td;
@@ -1239,7 +1240,7 @@ static int blk_throtl_init(struct gendisk *disk)
out:
blk_mq_unquiesce_queue(disk->queue);
- blk_mq_unfreeze_queue(disk->queue);
+ blk_mq_unfreeze_queue(disk->queue, memflags);
return ret;
}
diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 9d08a54c201e..761ea662ddc3 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -1717,9 +1717,10 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
else
pr_warn("%s: failed to revalidate zones\n", disk->disk_name);
if (ret) {
- blk_mq_freeze_queue(q);
+ unsigned int memflags = blk_mq_freeze_queue(q);
+
disk_free_zone_resources(disk);
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
}
return ret;
diff --git a/block/elevator.c b/block/elevator.c
index b81216c48b6b..cd2ce4921601 100644
--- a/block/elevator.c
+++ b/block/elevator.c
@@ -570,6 +570,7 @@ static struct elevator_type *elevator_get_default(struct request_queue *q)
void elevator_init_mq(struct request_queue *q)
{
struct elevator_type *e;
+ unsigned int memflags;
int err;
WARN_ON_ONCE(blk_queue_registered(q));
@@ -590,13 +591,13 @@ void elevator_init_mq(struct request_queue *q)
*
* Disk isn't added yet, so verifying queue lock only manually.
*/
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
blk_mq_cancel_work_sync(q);
err = blk_mq_init_sched(q, e);
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
if (err) {
pr_warn("\"%s\" elevator initialization failed, "
@@ -614,11 +615,12 @@ void elevator_init_mq(struct request_queue *q)
*/
int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
{
+ unsigned int memflags;
int ret;
lockdep_assert_held(&q->sysfs_lock);
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
blk_mq_quiesce_queue(q);
if (q->elevator) {
@@ -639,7 +641,7 @@ int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
out_unfreeze:
blk_mq_unquiesce_queue(q);
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
if (ret) {
pr_warn("elv: switch to \"%s\" failed, falling back to \"none\"\n",
@@ -651,9 +653,11 @@ int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
void elevator_disable(struct request_queue *q)
{
+ unsigned int memflags;
+
lockdep_assert_held(&q->sysfs_lock);
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
blk_mq_quiesce_queue(q);
elv_unregister_queue(q);
@@ -664,7 +668,7 @@ void elevator_disable(struct request_queue *q)
blk_add_trace_msg(q, "elv switch: none");
blk_mq_unquiesce_queue(q);
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
}
/*
diff --git a/drivers/block/aoe/aoedev.c b/drivers/block/aoe/aoedev.c
index 3523dd82d7a0..4db7f6ce8ade 100644
--- a/drivers/block/aoe/aoedev.c
+++ b/drivers/block/aoe/aoedev.c
@@ -226,10 +226,11 @@ aoedev_downdev(struct aoedev *d)
/* fast fail all pending I/O */
if (d->blkq) {
/* UP is cleared, freeze+quiesce to insure all are errored */
- blk_mq_freeze_queue(d->blkq);
+ unsigned int memflags = blk_mq_freeze_queue(d->blkq);
+
blk_mq_quiesce_queue(d->blkq);
blk_mq_unquiesce_queue(d->blkq);
- blk_mq_unfreeze_queue(d->blkq);
+ blk_mq_unfreeze_queue(d->blkq, memflags);
}
if (d->gd)
diff --git a/drivers/block/ataflop.c b/drivers/block/ataflop.c
index 110f9aca2667..a81ade622a01 100644
--- a/drivers/block/ataflop.c
+++ b/drivers/block/ataflop.c
@@ -746,6 +746,7 @@ static int do_format(int drive, int type, struct atari_format_descr *desc)
unsigned char *p;
int sect, nsect;
unsigned long flags;
+ unsigned int memflags;
int ret;
if (type) {
@@ -758,7 +759,7 @@ static int do_format(int drive, int type, struct atari_format_descr *desc)
}
q = unit[drive].disk[type]->queue;
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
blk_mq_quiesce_queue(q);
local_irq_save(flags);
@@ -817,7 +818,7 @@ static int do_format(int drive, int type, struct atari_format_descr *desc)
ret = FormatError ? -EIO : 0;
out:
blk_mq_unquiesce_queue(q);
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
return ret;
}
diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index d1f1d6bef2e6..c05fe27a96b6 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -586,6 +586,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
{
struct file *file = fget(arg);
struct file *old_file;
+ unsigned int memflags;
int error;
bool partscan;
bool is_loop;
@@ -623,14 +624,14 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
/* and ... switch */
disk_force_media_change(lo->lo_disk);
- blk_mq_freeze_queue(lo->lo_queue);
+ memflags = blk_mq_freeze_queue(lo->lo_queue);
mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
lo->lo_backing_file = file;
lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
mapping_set_gfp_mask(file->f_mapping,
lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
loop_update_dio(lo);
- blk_mq_unfreeze_queue(lo->lo_queue);
+ blk_mq_unfreeze_queue(lo->lo_queue, memflags);
partscan = lo->lo_flags & LO_FLAGS_PARTSCAN;
loop_global_unlock(lo, is_loop);
@@ -1255,6 +1256,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
int err;
bool partscan = false;
bool size_changed = false;
+ unsigned int memflags;
err = mutex_lock_killable(&lo->lo_mutex);
if (err)
@@ -1272,7 +1274,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
}
/* I/O needs to be drained before changing lo_offset or lo_sizelimit */
- blk_mq_freeze_queue(lo->lo_queue);
+ memflags = blk_mq_freeze_queue(lo->lo_queue);
err = loop_set_status_from_info(lo, info);
if (err)
@@ -1294,7 +1296,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
loop_update_dio(lo);
out_unfreeze:
- blk_mq_unfreeze_queue(lo->lo_queue);
+ blk_mq_unfreeze_queue(lo->lo_queue, memflags);
if (partscan)
clear_bit(GD_SUPPRESS_PART_SCAN, &lo->lo_disk->state);
out_unlock:
@@ -1446,6 +1448,7 @@ static int loop_set_capacity(struct loop_device *lo)
static int loop_set_dio(struct loop_device *lo, unsigned long arg)
{
bool use_dio = !!arg;
+ unsigned int memflags;
if (lo->lo_state != Lo_bound)
return -ENXIO;
@@ -1459,18 +1462,19 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
vfs_fsync(lo->lo_backing_file, 0);
}
- blk_mq_freeze_queue(lo->lo_queue);
+ memflags = blk_mq_freeze_queue(lo->lo_queue);
if (use_dio)
lo->lo_flags |= LO_FLAGS_DIRECT_IO;
else
lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
- blk_mq_unfreeze_queue(lo->lo_queue);
+ blk_mq_unfreeze_queue(lo->lo_queue, memflags);
return 0;
}
static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
{
struct queue_limits lim;
+ unsigned int memflags;
int err = 0;
if (lo->lo_state != Lo_bound)
@@ -1485,10 +1489,10 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
lim = queue_limits_start_update(lo->lo_queue);
loop_update_limits(lo, &lim, arg);
- blk_mq_freeze_queue(lo->lo_queue);
+ memflags = blk_mq_freeze_queue(lo->lo_queue);
err = queue_limits_commit_update(lo->lo_queue, &lim);
loop_update_dio(lo);
- blk_mq_unfreeze_queue(lo->lo_queue);
+ blk_mq_unfreeze_queue(lo->lo_queue, memflags);
return err;
}
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index b63a0f29a54a..7bdc7eb808ea 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1234,6 +1234,7 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
struct socket *sock;
struct nbd_sock **socks;
struct nbd_sock *nsock;
+ unsigned int memflags;
int err;
/* Arg will be cast to int, check it to avoid overflow */
@@ -1247,7 +1248,7 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
* We need to make sure we don't get any errant requests while we're
* reallocating the ->socks array.
*/
- blk_mq_freeze_queue(nbd->disk->queue);
+ memflags = blk_mq_freeze_queue(nbd->disk->queue);
if (!netlink && !nbd->task_setup &&
!test_bit(NBD_RT_BOUND, &config->runtime_flags))
@@ -1288,12 +1289,12 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
INIT_WORK(&nsock->work, nbd_pending_cmd_work);
socks[config->num_connections++] = nsock;
atomic_inc(&config->live_connections);
- blk_mq_unfreeze_queue(nbd->disk->queue);
+ blk_mq_unfreeze_queue(nbd->disk->queue, memflags);
return 0;
put_socket:
- blk_mq_unfreeze_queue(nbd->disk->queue);
+ blk_mq_unfreeze_queue(nbd->disk->queue, memflags);
sockfd_put(sock);
return err;
}
diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index 5b393e4a1ddf..faafd7ff43d6 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -7281,9 +7281,10 @@ static ssize_t do_rbd_remove(const char *buf, size_t count)
* Prevent new IO from being queued and wait for existing
* IO to complete/fail.
*/
- blk_mq_freeze_queue(rbd_dev->disk->queue);
+ unsigned int memflags = blk_mq_freeze_queue(rbd_dev->disk->queue);
+
blk_mark_disk_dead(rbd_dev->disk);
- blk_mq_unfreeze_queue(rbd_dev->disk->queue);
+ blk_mq_unfreeze_queue(rbd_dev->disk->queue, memflags);
}
del_gendisk(rbd_dev->disk);
diff --git a/drivers/block/sunvdc.c b/drivers/block/sunvdc.c
index 88dcae6ec575..05c4aee7f262 100644
--- a/drivers/block/sunvdc.c
+++ b/drivers/block/sunvdc.c
@@ -1113,6 +1113,7 @@ static void vdc_requeue_inflight(struct vdc_port *port)
static void vdc_queue_drain(struct vdc_port *port)
{
struct request_queue *q = port->disk->queue;
+ unsigned int memflags;
/*
* Mark the queue as draining, then freeze/quiesce to ensure
@@ -1121,12 +1122,12 @@ static void vdc_queue_drain(struct vdc_port *port)
port->drain = 1;
spin_unlock_irq(&port->vio.lock);
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
blk_mq_quiesce_queue(q);
spin_lock_irq(&port->vio.lock);
port->drain = 0;
- blk_mq_unquiesce_queue(q);
+ blk_mq_unquiesce_queue(q, memflags);
blk_mq_unfreeze_queue(q);
}
diff --git a/drivers/block/swim3.c b/drivers/block/swim3.c
index 9914153b365b..3aedcb5add61 100644
--- a/drivers/block/swim3.c
+++ b/drivers/block/swim3.c
@@ -840,6 +840,7 @@ static int grab_drive(struct floppy_state *fs, enum swim_state state,
static void release_drive(struct floppy_state *fs)
{
struct request_queue *q = disks[fs->index]->queue;
+ unsigned int memflags;
unsigned long flags;
swim3_dbg("%s", "-> release drive\n");
@@ -848,10 +849,10 @@ static void release_drive(struct floppy_state *fs)
fs->state = idle;
spin_unlock_irqrestore(&swim3_lock, flags);
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
blk_mq_quiesce_queue(q);
blk_mq_unquiesce_queue(q);
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
}
static int fd_eject(struct floppy_state *fs)
diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index bbaa26b523b8..a4af39fc7ea2 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -1584,11 +1584,12 @@ static int virtblk_freeze(struct virtio_device *vdev)
{
struct virtio_blk *vblk = vdev->priv;
struct request_queue *q = vblk->disk->queue;
+ unsigned int memflags;
/* Ensure no requests in virtqueues before deleting vqs. */
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
blk_mq_quiesce_queue_nowait(q);
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
/* Ensure we don't receive any more interrupts */
virtio_reset_device(vdev);
diff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c
index ee7e1d908986..847c11542f02 100644
--- a/drivers/mtd/mtd_blkdevs.c
+++ b/drivers/mtd/mtd_blkdevs.c
@@ -404,6 +404,7 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
{
unsigned long flags;
+ unsigned int memflags;
lockdep_assert_held(&mtd_table_mutex);
@@ -420,10 +421,10 @@ int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
spin_unlock_irqrestore(&old->queue_lock, flags);
/* freeze+quiesce queue to ensure all requests are flushed */
- blk_mq_freeze_queue(old->rq);
+ memflags = blk_mq_freeze_queue(old->rq);
blk_mq_quiesce_queue(old->rq);
blk_mq_unquiesce_queue(old->rq);
- blk_mq_unfreeze_queue(old->rq);
+ blk_mq_unfreeze_queue(old->rq, memflags);
/* If the device is currently open, tell trans driver to close it,
then put mtd device, and don't touch it again */
diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 76b615d4d5b9..40046770f1bf 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -2132,15 +2132,16 @@ static int nvme_update_ns_info_generic(struct nvme_ns *ns,
struct nvme_ns_info *info)
{
struct queue_limits lim;
+ unsigned int memflags;
int ret;
lim = queue_limits_start_update(ns->disk->queue);
nvme_set_ctrl_limits(ns->ctrl, &lim);
- blk_mq_freeze_queue(ns->disk->queue);
+ memflags = blk_mq_freeze_queue(ns->disk->queue);
ret = queue_limits_commit_update(ns->disk->queue, &lim);
set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
- blk_mq_unfreeze_queue(ns->disk->queue);
+ blk_mq_unfreeze_queue(ns->disk->queue, memflags);
/* Hide the block-interface for these devices */
if (!ret)
@@ -2155,6 +2156,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
struct nvme_id_ns_nvm *nvm = NULL;
struct nvme_zone_info zi = {};
struct nvme_id_ns *id;
+ unsigned int memflags;
sector_t capacity;
unsigned lbaf;
int ret;
@@ -2186,7 +2188,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
lim = queue_limits_start_update(ns->disk->queue);
- blk_mq_freeze_queue(ns->disk->queue);
+ memflags = blk_mq_freeze_queue(ns->disk->queue);
ns->head->lba_shift = id->lbaf[lbaf].ds;
ns->head->nuse = le64_to_cpu(id->nuse);
capacity = nvme_lba_to_sect(ns->head, le64_to_cpu(id->nsze));
@@ -2219,7 +2221,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
ret = queue_limits_commit_update(ns->disk->queue, &lim);
if (ret) {
- blk_mq_unfreeze_queue(ns->disk->queue);
+ blk_mq_unfreeze_queue(ns->disk->queue, memflags);
goto out;
}
@@ -2235,7 +2237,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
ns->head->features |= NVME_NS_DEAC;
set_disk_ro(ns->disk, nvme_ns_is_readonly(ns, info));
set_bit(NVME_NS_READY, &ns->flags);
- blk_mq_unfreeze_queue(ns->disk->queue);
+ blk_mq_unfreeze_queue(ns->disk->queue, memflags);
if (blk_queue_is_zoned(ns->queue)) {
ret = blk_revalidate_disk_zones(ns->disk);
@@ -2291,9 +2293,10 @@ static int nvme_update_ns_info(struct nvme_ns *ns, struct nvme_ns_info *info)
if (!ret && nvme_ns_head_multipath(ns->head)) {
struct queue_limits *ns_lim = &ns->disk->queue->limits;
struct queue_limits lim;
+ unsigned int memflags;
lim = queue_limits_start_update(ns->head->disk->queue);
- blk_mq_freeze_queue(ns->head->disk->queue);
+ memflags = blk_mq_freeze_queue(ns->head->disk->queue);
/*
* queue_limits mixes values that are the hardware limitations
* for bio splitting with what is the device configuration.
@@ -2325,7 +2328,7 @@ static int nvme_update_ns_info(struct nvme_ns *ns, struct nvme_ns_info *info)
set_disk_ro(ns->head->disk, nvme_ns_is_readonly(ns, info));
nvme_mpath_revalidate_paths(ns);
- blk_mq_unfreeze_queue(ns->head->disk->queue);
+ blk_mq_unfreeze_queue(ns->head->disk->queue, memflags);
}
return ret;
diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index a85d190942bd..2a7635565083 100644
--- a/drivers/nvme/host/multipath.c
+++ b/drivers/nvme/host/multipath.c
@@ -60,7 +60,7 @@ void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
lockdep_assert_held(&subsys->lock);
list_for_each_entry(h, &subsys->nsheads, entry)
if (h->disk)
- blk_mq_unfreeze_queue(h->disk->queue);
+ blk_mq_unfreeze_queue_nomemrestore(h->disk->queue);
}
void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 4411426a7894..b86e259516a7 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -2723,6 +2723,7 @@ int
scsi_device_quiesce(struct scsi_device *sdev)
{
struct request_queue *q = sdev->request_queue;
+ unsigned int memflags;
int err;
/*
@@ -2737,7 +2738,7 @@ scsi_device_quiesce(struct scsi_device *sdev)
blk_set_pm_only(q);
- blk_mq_freeze_queue(q);
+ memflags = blk_mq_freeze_queue(q);
/*
* Ensure that the effect of blk_set_pm_only() will be visible
* for percpu_ref_tryget() callers that occur after the queue
@@ -2745,7 +2746,7 @@ scsi_device_quiesce(struct scsi_device *sdev)
* was called. See also https://lwn.net/Articles/573497/.
*/
synchronize_rcu();
- blk_mq_unfreeze_queue(q);
+ blk_mq_unfreeze_queue(q, memflags);
mutex_lock(&sdev->state_mutex);
err = scsi_device_set_state(sdev, SDEV_QUIESCE);
diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c
index 042329b74c6e..312d78213954 100644
--- a/drivers/scsi/scsi_scan.c
+++ b/drivers/scsi/scsi_scan.c
@@ -220,6 +220,7 @@ static int scsi_realloc_sdev_budget_map(struct scsi_device *sdev,
int new_shift = sbitmap_calculate_shift(depth);
bool need_alloc = !sdev->budget_map.map;
bool need_free = false;
+ unsigned int memflags;
int ret;
struct sbitmap sb_backup;
@@ -240,7 +241,7 @@ static int scsi_realloc_sdev_budget_map(struct scsi_device *sdev,
* and here disk isn't added yet, so freezing is pretty fast
*/
if (need_free) {
- blk_mq_freeze_queue(sdev->request_queue);
+ memflags = blk_mq_freeze_queue(sdev->request_queue);
sb_backup = sdev->budget_map;
}
ret = sbitmap_init_node(&sdev->budget_map,
@@ -256,7 +257,7 @@ static int scsi_realloc_sdev_budget_map(struct scsi_device *sdev,
else
sbitmap_free(&sb_backup);
ret = 0;
- blk_mq_unfreeze_queue(sdev->request_queue);
+ blk_mq_unfreeze_queue(sdev->request_queue, memflags);
}
return ret;
}
diff --git a/drivers/ufs/core/ufs-sysfs.c b/drivers/ufs/core/ufs-sysfs.c
index 796e37a1d859..3438269a5440 100644
--- a/drivers/ufs/core/ufs-sysfs.c
+++ b/drivers/ufs/core/ufs-sysfs.c
@@ -1439,6 +1439,7 @@ static ssize_t max_number_of_rtt_store(struct device *dev,
struct ufs_hba *hba = dev_get_drvdata(dev);
struct ufs_dev_info *dev_info = &hba->dev_info;
struct scsi_device *sdev;
+ unsigned int memflags;
unsigned int rtt;
int ret;
@@ -1458,14 +1459,16 @@ static ssize_t max_number_of_rtt_store(struct device *dev,
ufshcd_rpm_get_sync(hba);
+ memflags = memalloc_noio_save();
shost_for_each_device(sdev, hba->host)
- blk_mq_freeze_queue(sdev->request_queue);
+ blk_mq_freeze_queue_nomemsave(sdev->request_queue);
ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &rtt);
shost_for_each_device(sdev, hba->host)
- blk_mq_unfreeze_queue(sdev->request_queue);
+ blk_mq_unfreeze_queue_nomemrestore(sdev->request_queue);
+ memalloc_noio_restore(memflags);
ufshcd_rpm_put_sync(hba);
diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
index a0a9007cc1e3..9ebb53f031cd 100644
--- a/include/linux/blk-mq.h
+++ b/include/linux/blk-mq.h
@@ -900,8 +900,22 @@ void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs);
void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
busy_tag_iter_fn *fn, void *priv);
void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset);
-void blk_mq_freeze_queue(struct request_queue *q);
-void blk_mq_unfreeze_queue(struct request_queue *q);
+void blk_mq_freeze_queue_nomemsave(struct request_queue *q);
+void blk_mq_unfreeze_queue_nomemrestore(struct request_queue *q);
+static inline unsigned int __must_check
+blk_mq_freeze_queue(struct request_queue *q)
+{
+ unsigned int memflags = memalloc_noio_save();
+
+ blk_mq_freeze_queue_nomemsave(q);
+ return memflags;
+}
+static inline void
+blk_mq_unfreeze_queue(struct request_queue *q, unsigned int memflags)
+{
+ blk_mq_unfreeze_queue_nomemrestore(q);
+ memalloc_noio_restore(memflags);
+}
void blk_freeze_queue_start(struct request_queue *q);
void blk_mq_freeze_queue_wait(struct request_queue *q);
int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
]
|
| 89/2 |
2026/12/01 13:23 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 124233
output: 61
thoughts: 49
Thoughts: **My Internal Monologue on NBD Driver Code Review**
Alright, time to dive deeper. Beyond what I've already scrutinized, I'm making a conscious effort to be as comprehensive as possible. Therefore, I'm going to take a moment to specifically examine the code sections identified by the commit hashes `cf1b2326b734` and `9f7c02e03157` within the `drivers/block/nbd.c` file. My goal here is not just to glance over them, but to thoroughly understand their implementation and ensure they align with the overall design and any established best practices. This is about leaving no stone unturned in my analysis.
|
| 90/2 |
2026/12/01 13:23 |
tool |
git-show |
0m
Args:
Results: map[Output:commit cf1b2326b734896734c6e167e41766f9cee7686a
Author: Mike Christie <mchristi@redhat.com>
Date: Thu Oct 17 16:27:34 2019 -0500
nbd: verify socket is supported during setup
nbd requires socket families to support the shutdown method so the nbd
recv workqueue can be woken up from its sock_recvmsg call. If the socket
does not support the callout we will leave recv works running or get hangs
later when the device or module is removed.
This adds a check during socket connection/reconnection to make sure the
socket being passed in supports the needed callout.
Reported-by: syzbot+24c12fa8d218ed26011a@syzkaller.appspotmail.com
Fixes: e9e006f5fcf2 ("nbd: fix max number of supported devs")
Tested-by: Richard W.M. Jones <rjones@redhat.com>
Signed-off-by: Mike Christie <mchristi@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index e200c7c9ddc4..a94ee45440b3 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -980,6 +980,25 @@ static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
return ret;
}
+static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
+ int *err)
+{
+ struct socket *sock;
+
+ *err = 0;
+ sock = sockfd_lookup(fd, err);
+ if (!sock)
+ return NULL;
+
+ if (sock->ops->shutdown == sock_no_shutdown) {
+ dev_err(disk_to_dev(nbd->disk), "Unsupported socket: shutdown callout must be supported.\n");
+ *err = -EINVAL;
+ return NULL;
+ }
+
+ return sock;
+}
+
static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
bool netlink)
{
@@ -989,7 +1008,7 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
struct nbd_sock *nsock;
int err;
- sock = sockfd_lookup(arg, &err);
+ sock = nbd_get_socket(nbd, arg, &err);
if (!sock)
return err;
@@ -1041,7 +1060,7 @@ static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
int i;
int err;
- sock = sockfd_lookup(arg, &err);
+ sock = nbd_get_socket(nbd, arg, &err);
if (!sock)
return err;
]
|
| 91/2 |
2026/12/01 13:23 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 9f7c02e031570e8291a63162c6c046dc15ff85b0
Author: Eric Dumazet <edumazet@kernel.org>
Date: Tue Sep 9 13:22:43 2025 +0000
nbd: restrict sockets to TCP and UDP
Recently, syzbot started to abuse NBD with all kinds of sockets.
Commit cf1b2326b734 ("nbd: verify socket is supported during setup")
made sure the socket supported a shutdown() method.
Explicitely accept TCP and UNIX stream sockets.
Fixes: cf1b2326b734 ("nbd: verify socket is supported during setup")
Reported-by: syzbot+e1cd6bd8493060bd701d@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/netdev/CANn89iJ+76eE3A_8S_zTpSyW5hvPRn6V57458hCZGY5hbH_bFA@mail.gmail.com/T/#m081036e8747cd7e2626c1da5d78c8b9d1e55b154
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Mike Christie <mchristi@redhat.com>
Cc: Richard W.M. Jones <rjones@redhat.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Yu Kuai <yukuai1@huaweicloud.com>
Cc: linux-block@vger.kernel.org
Cc: nbd@other.debian.org
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 6463d0e8d0ce..87b0b78249da 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1217,6 +1217,14 @@ static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
if (!sock)
return NULL;
+ if (!sk_is_tcp(sock->sk) &&
+ !sk_is_stream_unix(sock->sk)) {
+ dev_err(disk_to_dev(nbd->disk), "Unsupported socket: should be TCP or UNIX.\n");
+ *err = -EINVAL;
+ sockfd_put(sock);
+ return NULL;
+ }
+
if (sock->ops->shutdown == sock_no_shutdown) {
dev_err(disk_to_dev(nbd->disk), "Unsupported socket: shutdown callout must be supported.\n");
*err = -EINVAL;
]
|
| 92/2 |
2026/12/01 13:23 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 125985
output: 54
thoughts: 5957
Thoughts: **This is where I was thinking about the exact phrasing of the question: "Search for the commit that introduced this bug." My goal is to meticulously analyze all potential commits and compare them against the provided instructions.**
Here's my thought process, breaking down the problem and evaluating each possibility:
First, I need to ensure I understand the instructions completely.
1. **Establish the root cause first:** I need to verify the provided root cause analysis by checking the actual source code. This means pinpointing the exact functions, data structures, invariants, or lines of code where the faulty logic lies.
2. **Use git tools:** I'll be relying on `git blame`, `git log`, and `git show` to trace the history of the relevant code sections.
3. **Trace past refactorings:** I must look beyond simple code cleanups like pure refactors, moves, formatting changes, or variable renames to find the commit that *actually introduced* the buggy behavior or violated an existing invariant.
4. **Exclusions:** Crucially, I *must not* blame commits that only added debugging aids (like `WARN_ON`, `BUG_ON`, or sanitizers like KASAN/KMSAN/LOCKDEP annotations) if they simply *exposed* an older bug. The *only* exception is if the check itself is flawed and is the source of the bug.
Now, I need to consider the phrase "this bug" and what it could refer to. I'll explore different interpretations.
**Interpretation 1: The bug is within `nbd_reclassify_socket()` in `drivers/block/nbd.c`.**
My immediate thought is to question why `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency") was introduced. I realize that *before* this commit, NBD sockets were *never* reclassified. This leads me to ask: could `d532cddb6c60` itself have *introduced* this specific circular locking dependency?
Upon reflection, I conclude: NO. Even before `d532cddb6c60`, `nsock->sock->sk` was *always* unconditionally in the `sk_lock-AF_INET` class.
What about `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock")? I need to examine its effect closely.
In `d532cddb6c60`, the relevant line was:
`if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) return;`
In `9280e6edf656`, this was changed to:
`- if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))`
`+ if (!sock_allow_reclassification(sk))`
` return;`
I pause and think critically: Did `9280e6edf656` *introduce* the circular locking dependency? Again, the answer is no. Before `9280e6edf656`, in `d532cddb6c60`, if `!sock_allow_reclassification(sk)` was true, `nbd_reclassify_socket()` *also* returned without performing the reclassification. It just issued a `WARN_ON_ONCE`. If `panic_on_warn` was enabled, the kernel would crash at that `WARN_ON_ONCE`. If not, the `sk` would remain in `sk_lock-AF_INET`, and the exact same lockdep circular dependency would occur.
Furthermore, before `d532cddb6c60` was even committed, `nbd_reclassify_socket()` didn't exist, meaning `sk` was *always* in `sk_lock-AF_INET`.
**Wait a second!** This prompts me to consider the broader picture from the perspective of both syzkaller and the kernel bug. Why wasn't this specific lockdep circular dependency reported *before* `9280e6edf656`, `d532cddb6c60`, `1e44bedbc921a`, or `f1be1788a32e`? I need to check the history.
Before `d532cddb6c60`: If syzkaller mounted btrfs over NBD, *any* TCP socket combined with btrfs `fallocate` and subsequent operations like `page_mkwrite` and `elevator_change` would trigger this lockdep splat, even without a race condition in `nbd_reclassify_socket()`.
Now, I need to consider the sequence of events: Did syzkaller trigger the `d532cddb6c60` splat first (on `fs_reclaim`), then `d532cddb6c60` fixed the non-racing case, then syzkaller hit the `WARN_ON_ONCE` in `d532cddb6c60` (syzbot+6b85d1e39a5b8ed9a954)? Then `9280e6edf656` removed the `WARN_ON_ONCE`, allowing syzkaller to trigger the `553d28925fde` splat (reconnect path, syzbot+d7d72f3d467710442af0)? And *then* syzkaller finally hit *this* specific splat, where `nbd_reclassify_socket` silently returned without reclassifying, or a userspace race on `NBD_SET_SOCK` with socket activity occurred?
**Hold on! I need to re-evaluate.** Let's think about the state *before* `9280e6edf656`.
Before `9280e6edf656`:
* The NBD driver had a lockdep circular dependency because NBD sockets shared the `sk_lock-AF_INET` class with user sockets.
* This bug arguably existed much earlier, perhaps since `f1be1788a32e` (a lockdep annotation for freeze/enter queue) or `560bc4b39952` (2017, where `nbd_mark_nsock_dead` called `kernel_sock_shutdown` under `tx_lock`), or even earlier.
**Wait, let me check `nbd_send_cmd`!** Does `nbd_send_cmd` also call `sock_sendmsg` under `nsock->tx_lock`? Yes, it does: `nbd_send_cmd` -> `sock_xmit` -> `sock_sendmsg` -> `tcp_sendmsg` -> `lock_sock(sk)`. And NBD has been using `sock_sendmsg` under `tx_lock` since Linux 2.6 (`1da177e4c3f4`).
**This is a critical juncture.** I need to focus on what "the commit that introduced this bug" truly means in this context. I must list *every* potential candidate commit and rigorously test each one against the instructions.
Here are the primary candidates and my analysis:
**Candidate A: `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock")**
* **Why it might be considered:** The Root Cause Analysis (RCA) points to `nbd_reclassify_socket()` silently failing. It states: "When that occurs, `nbd_reclassify_socket()` silently returns without reclassifying the socket and without failing `nbd_add_socket()`/`nbd_genl_reconnect_sock_cb()`, leaving `nsock->sock->sk` in the generic `sk_lock-AF_INET` lockdep class." This is *exactly* what `9280e6edf656` did by changing the `WARN_ON_ONCE` to a silent `return` when `!sock_allow_reclassification(sk)`. Furthermore, the commit message itself claimed: "Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless." This assertion is demonstrably *false*, as skipping reclassification *was not* harmless and led to re-exposing the lockdep circular dependency.
* **Why it might *not* be the original cause of the circular dependency:** Before `9280e6edf656`, `d532cddb6c60` also had a `return;` path without reclassification (though it included `WARN_ON_ONCE`). And before `d532cddb6c60`, NBD didn't reclassify sockets at all. So, while `9280e6edf656` made the failure silent and thus revealed the bug to syzkaller, it didn't create the underlying circular dependency condition. The removal of `WARN_ON_ONCE` in `9280e6edf656` could be seen as either removing a warning or enabling a broken fallback mechanism (silent skipping of reclassification).
**Candidate B: `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency")**
* **Did it introduce the circular dependency?** No. As established earlier, this commit was an *incomplete attempt* to fix the circular dependency (it `Fixes: ffa1e7ada456`, which is a related issue).
* **Adhering to the rules:** Instruction #3 states: "Once you identify the primary commit that introduced (or fixed) the bug, return ONLY that commit... 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..." `d532cddb6c60` was an incomplete fix, not the introducer.
**Candidate C: Is there an *earlier* commit that ACTUALLY introduced this circular locking dependency (or a real deadlock), or did `f1be1788a32e` / `1e44bedbc921a` / `ffa1e7ada456` merely expose an older bug?**
This is where I need to trace the entire chain of lock acquisitions carefully, applying rule #4: "Do NOT blame commits that merely added debugging or sanitizer checks... that exposed an older bug."
Let's examine the dependencies:
1. **Btrfs path:** `btrfs_fallocate()` -> `btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_MMAP)` -> `file_modified()` -> `btrfs_xattr_handler_get_security()` -> `btrfs_search_slot()` -> `btrfs_read_lock_root_node()` (`btrfs-tree-01`) -> `read_block_for_search()` (`btrfs-tree-00`) -> `btrfs_submit_bbio()` -> `blk_mq_submit_bio()` (`&q->q_usage_counter(io)`).
* `8d9b4a162a37` (2021): `btrfs_fallocate` started taking `i_mmap_lock`.
* `05fd9564e9faf` (2022): `btrfs_fallocate` began calling `file_modified()` while holding `i_mmap_lock`.
* `f1be1788a32e` (Oct 2024): `block: model freeze & enter queue as lock for supporting lockdep` - **This added a lockdep annotation (`&q->q_usage_counter(io)`)**. Per rule #4, I cannot blame this for *introducing* the bug; it only *exposed* it.
2. **Elevator path:** `elv_iosched_store()` / `elevator_set_default()` -> `elevator_change()`:
* `blk_mq_freeze_queue(q)` (`&q->q_usage_counter(io)`) -> `mutex_lock(&q->elevator_lock)` -> `elevator_switch()` -> `blk_mq_quiesce_queue()` (`set->srcu`).
* `elevator_set_default()` only started calling `elevator_change()` (which acquires `q->elevator_lock` and `blk_mq_quiesce_queue` -> `set->srcu`) in `1e44bedbc921a` (May 2025)!
* **However**, even before `1e44bedbc921a`, `elv_iosched_store` would call `elevator_change` -> `elevator_switch` -> `blk_mq_quiesce_queue` (`set->srcu`) while holding `q->elevator_lock` (or `q->sysfs_lock`) and `q->q_usage_counter(io)`. So, the `q->q_usage_counter(io)` -> `q->elevator_lock` -> `set->srcu` dependency was already possible via `elv_iosched_store`.
3. **NBD request path:** `blk_mq_dispatch_rq_list()` (under `set->srcu`) -> `nbd_queue_rq()` -> `&cmd->lock` -> `nbd_handle_cmd()` -> `&nsock->tx_lock`.
4. **NBD socket cleanup:** `recv_work()` -> `&nsock->tx_lock` -> `nbd_mark_nsock_dead()` -> `kernel_sock_shutdown()` -> `inet_shutdown()` -> `lock_sock()` (`sk_lock-AF_INET`).
5. **TCP send path:** `tcp_sendmsg_locked()` (under `sk_lock-AF_INET`) -> `skb_copy_to_page_nocache()` -> `__might_fault()` (`&mm->mmap_lock`).
6. **Btrfs page fault path:** `do_user_addr_fault()` (under `&mm->mmap_lock`) -> `do_page_mkwrite()` -> `btrfs_page_mkwrite()` -> `sb_start_pagefault()` (`sb_pagefaults`) -> `down_read(&ei->i_mmap_lock)`.
**Now, let's re-evaluate the critical path and rule #4 again:**
The full dependency chain is:
`btrfs_fallocate` (`i_mmap_lock` or `BTRFS_ILOCK_MMAP`) -> `file_modified` -> `btrfs_search_slot` -> `blk_mq_submit_bio` (`q_usage_counter`) -> `elv_iosched_store` (`elevator_lock`, `set->srcu`) -> `nbd_queue_rq` (`cmd->lock`, `tx_lock`) -> `recv_work` (`tx_lock`, `nbd_mark_nsock_dead` -> `kernel_sock_shutdown` -> `inet_shutdown` -> `lock_sock` (`sk_lock-AF_INET`)) <- *This is the NBD socket*
AND
`tcp_sendmsg_locked` (`sk_lock-AF_INET` - *this could be the same socket or a different one*) -> `__might_fault` (`mm->mmap_lock`) -> `do_page_mkwrite` -> `btrfs_page_mkwrite` -> `sb_start_pagefault` (`sb_pagefaults`) -> `down_read(&ei->i_mmap_lock)`.
Looking at the stack trace provided in the prompt's context, specifically frame `#7` (`nbd_mark_nsock_dead`) showing `sk_lock-AF_INET` and frame `#8` (`tcp_sendmsg_locked`) also showing `sk_lock-AF_INET`, this strongly indicates that in *this particular crash*, the NBD socket *was not* reclassified, and it remained in the generic `sk_lock-AF_INET` class.
The crucial point is rule #4: I *cannot* blame `f1be1788a32e` (which added `q_usage_counter` annotation) or `ffa1e7ada456` (which made lockdep splats more visible) because they merely *exposed* an older bug.
This brings me back to `9280e6edf656`. Why was the RCA focused there? Because at the specific commit hash mentioned (`19cc08c1a6a650f8e54c0461ff198be6448f425d`), the `nbd_reclassify_socket()` function was *already intended* to prevent this false-positive lockdep cycle by moving NBD sockets to the `sk_lock-AF_INET-NBD` class. The *only* reason `nsock->sock->sk` remained in the generic `sk_lock-AF_INET` class at that commit was that `nbd_reclassify_socket()` silently failed to perform the reclassification.
And why did `nbd_reclassify_socket()` fail to reclassify? Because `sock_allow_reclassification(sk)` returned `false` when the socket lock (`sk->sk_lock` or `sk->sk_lock.slock`) was held (likely due to a concurrent userspace syscall on the file descriptor or a softirq `bh_lock_sock`). And at that point, `nbd_reclassify_socket()` simply returned without reclassifying.
The change from `WARN_ON_ONCE` to a silent `return` in `9280e6edf656` directly enabled the scenario where this silent failure led to the observable lockdep splat, making it the most direct cause of the *reported* bug. While the underlying circular dependency *might* have been theoretically possible earlier, `9280e6edf656` is the commit that made it manifest and observable in this specific way, by turning a warning into silent failure.
Therefore, based on the instructions and the evidence, `9280e6edf656` is the commit that introduced the specific bug *as reported*, by making the failure silent and allowing the previously existing, but less exposed, circular dependency to trigger the splat.
|
| 93/2 |
2026/12/01 13:24 |
tool |
grepper |
0m
Args: map[Expression:->sock\s*=|nbd_get_socket|nbd_reclassify_socket PathPrefixes:[drivers/block/nbd.c]]
Results: map[Output:drivers/block/nbd.c=1184=static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
--
drivers/block/nbd.c-1212-
drivers/block/nbd.c:1213:static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
drivers/block/nbd.c-1214- int *err)
--
drivers/block/nbd.c=1243=static struct lock_class_key nbd_slock_key[3];
drivers/block/nbd.c-1244-
drivers/block/nbd.c:1245:static void nbd_reclassify_socket(struct socket *sock)
drivers/block/nbd.c-1246-{
--
drivers/block/nbd.c-1273-#else
drivers/block/nbd.c:1274:static inline void nbd_reclassify_socket(struct socket *sock) {}
drivers/block/nbd.c-1275-#endif
--
drivers/block/nbd.c=1277=static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
--
drivers/block/nbd.c-1295-
drivers/block/nbd.c:1296: sock = nbd_get_socket(nbd, arg, &err);
drivers/block/nbd.c-1297- if (!sock)
drivers/block/nbd.c-1298- return err;
drivers/block/nbd.c:1299: nbd_reclassify_socket(sock);
drivers/block/nbd.c-1300-
--
drivers/block/nbd.c-1332- mutex_init(&nsock->tx_lock);
drivers/block/nbd.c:1333: nsock->sock = sock;
drivers/block/nbd.c-1334- nsock->pending = NULL;
--
drivers/block/nbd.c=1348=static int nbd_genl_reconnect_sock_cb(struct nbd_device *nbd, unsigned long arg)
--
drivers/block/nbd.c-1355-
drivers/block/nbd.c:1356: sock = nbd_get_socket(nbd, arg, &err);
drivers/block/nbd.c-1357- if (!sock)
drivers/block/nbd.c-1358- return err;
drivers/block/nbd.c:1359: nbd_reclassify_socket(sock);
drivers/block/nbd.c-1360-
--
drivers/block/nbd.c-1384- nsock->fallback_index = -1;
drivers/block/nbd.c:1385: nsock->sock = sock;
drivers/block/nbd.c-1386- nsock->dead = false;
]
|
| 94/2 |
2026/12/01 13:24 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 132781
output: 82
thoughts: 2536
Thoughts: **My Deep Dive into the NBD Lock Dependency Conundrum**
Okay, let's get to the heart of this NBD socket reclassification issue. I've been tracing the flow and I'm realizing the core of the problem lies in how and when `nsock->sock` is assigned and subsequently reclassified.
I see that `nsock->sock` is *only* ever assigned in two places: `nbd_add_socket` and `nbd_genl_reconnect_sock_cb`. And, critically, *both* of these functions call `nbd_reclassify_socket(sock)` immediately afterward. Now, `nbd_reclassify_socket` itself only fails to reclassify `AF_INET` sockets when `sock_allow_reclassification(sk)` returns false. This is where things get interesting.
I've been thinking very carefully about how `git bisect` would have handled this. Let's play out that scenario:
1. **Commit `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock"):** If I were to bisect to this commit, the `nbd_reclassify_socket` function would *silently skip* the reclassification. This is precisely where the lockdep circular dependency would manifest – it would trigger a `WARNING: possible circular locking dependency detected`.
2. **Commit `9280e6edf656~1` (the parent/merge before it, which is `d532cddb6c60`):** Moving back one commit, to `d532cddb6c60`, `nbd_reclassify_socket` would *not* silently skip. Instead, it would hit the `WARN_ON_ONCE(!sock_allow_reclassification(sk))` condition. This would produce a *different* warning: `WARNING in nbd_reclassify_socket`, which syzkaller would have reported as `syzbot+6b85d1e39a5b8ed9a954`.
This brings me to a crucial point: why did commit `9280e6edf656` *cause* this specific bug, or did it? It seems counter-intuitive that removing a `WARN_ON_ONCE` would introduce a silent failure. Let's re-examine `9280e6edf656`:
- if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
+ if (!sock_allow_reclassification(sk))
return;
It *only* removed the `WARN_ON_ONCE` macro, not the condition itself. This implies that the change was intentionally to *suppress the warning* when `sock_allow_reclassification(sk)` was false, and to *silently return* instead. This is where Rule 4 of good bisecting practices comes into play: "Do NOT blame commits that merely added debugging or sanitizer checks... that exposed an older bug, unless the check itself is incorrect".
However, `9280e6edf656` *removed* a `WARN_ON_ONCE`, it didn't add one. So, did removing this warning *introduce* the bug of `nbd_reclassify_socket()` silently skipping reclassification?
Let's trace back further:
* **Before `9280e6edf656`:** Commit `d532cddb6c60` also returned without reclassifying, but it *did* issue a `WARN_ON_ONCE`.
* **Before `d532cddb6c60`:** NBD didn't reclassify sockets *at all*.
This leads me to question the rationale in the commit message for `9280e6edf656`: "Hitting a held lock here is expected for an externally owned socket and is not a kernel bug, so skip reclassification silently instead of warning. Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless." This commit seems to have *explicitly opted for silent skipping* as the intended behavior, rather than fixing the underlying race condition that could occur when an externally owned socket (obtained via `sockfd_lookup()`) has its lock held during `nbd_reclassify_socket`, or is being used concurrently by userspace.
So, is `9280e6edf656` the culprit? Or is it `d532cddb6c60`? Or perhaps something much earlier in the NBD or block layer history?
I need to be extremely deliberate here and not rush. Let's thoroughly examine every potential candidate.
Here are the commits I'm considering:
1. `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock") - Jun 22, 2026
2. `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency") - Jun 13, 2026
3. `1e44bedbc921` ("block: unifying elevator change") - May 5, 2025
4. `1bf70d08cc3b` ("block: introduce a dedicated lock for protecting queue elevator updates") - Mar 4, 2025
5. `f1be1788a32e` ("block: model freeze & enter queue as lock for supporting lockdep") - Oct 25, 2024
6. `05fd9564e9fa` ("btrfs: fix fallocate to use file_modified to update permissions consistently") - Mar 14, 2022
7. `8d9b4a162a37` ("btrfs: exclude mmap from happening during all fallocate operations") - Feb 10, 2021
8. `560bc4b39952` (or earlier, concerning NBD's handling of dead connections and `lock_sock` under `tx_lock` / `cmd->lock` / `nbd_queue_rq`) - circa 2017
Let me scrutinize each of these.
### **Initial Scrutiny of Candidates #3 through #8:**
When I look at commits #3, #4, #5, #6, #7, and #8, I'm asking myself: is the fundamental locking pattern within *these specific components* a bug in itself?
* **Btrfs (`8d9b4a162a37`, `05fd9564e9fa`):** The pattern of holding `i_mmap_lock` across `file_modified()` leading into `btrfs_search_slot()` and then `blk_mq_submit_bio()` is entirely expected and valid for a filesystem.
* **Block Layer (`1e44bedbc921`, `1bf70d08cc3b`, `f1be1788a32e`):** The sequence of freezing the queue (`q_usage_counter(io)`), acquiring `q->elevator_lock`, and then calling `blk_mq_quiesce_queue()` (which involves `set->srcu`) is standard block layer operation. In fact, `1bf70d08cc3b` was specifically intended to *correct* lock ordering here, moving from `sysfs_lock` to `elevator_lock`. Commit `f1be1788a32e` only introduced lockdep annotations which, per Rule 4, we shouldn't blame if they just reveal an existing bug.
* **NBD Driver (`560bc4b39952` and earlier):** The core operation of NBD, where `nbd_queue_rq` eventually leads to `sock_sendmsg`, inherently involves acquiring the socket lock (`sk_lock`) while already holding NBD's internal locks (`cmd->lock`, `nsock->tx_lock`). This is fundamental to its network block device nature.
* **TCP Socket Operations (`net/ipv4/tcp.c`):** The standard operation of acquiring `sk_lock` and then potentially `mm->mmap_lock` during memory faulting in `sendmsg`/`recvmsg` is also fundamental to TCP sockets.
* **Memory Management (`mm/memory.c` + `fs/btrfs/file.c`):** The lock ordering involving `mm->mmap_lock` followed by `sb_pagefaults` and `i_mmap_lock` within `page_mkwrite` is standard for memory-mapped files.
**Conclusion on candidates #3-#8:** None of the *individual* lock acquisitions within btrfs, the block layer, NBD's I/O path, or standard TCP operations appear to be a bug in their own right. The problem isn't *that* these locks are acquired, but rather the *combination* and the *order* when they all intersect, particularly with NBD's socket handling.
This brings me to two key insights:
1. **NBD's Socket Lockdep Class:** NBD's socket lockdep class *must* be distinct from the standard `AF_INET` socket lockdep class. If they share the same class, then *any* filesystem, page cache, or swap operation involving NBD will create a lockdep circular dependency with *any* standard userspace TCP socket. This is a fundamental architectural need.
2. **Userspace Socket Interference:** Userspace must not be operating on or holding onto the same socket that NBD is actively using, or `nbd_reclassify_socket` needs to handle this scenario more robustly than simply skipping reclassification.
Now, let's re-focus on the direct NBD changes: `9280e6edf656` and `d532cddb6c60`. It's clear the bug stems from how NBD handles socket reclassification and the associated locking. I need to dig deeper into what exactly each of these commits changed and why.
|
| 95/2 |
2026/12/01 13:25 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/block/nbd.c Name:sock_xmit]
Results: map[SourceCode: 604: /*
605: * Send or receive packet. Return a positive value on success and
606: * negtive value on failure, and never return 0.
607: */
608: static int sock_xmit(struct nbd_device *nbd, int index, int send,
609: struct iov_iter *iter, int msg_flags, int *sent)
610: {
611: struct nbd_config *config = nbd->config;
612: struct socket *sock = config->socks[index]->sock;
613:
614: return __sock_xmit(nbd, sock, send, iter, msg_flags, sent);
615: }
SourceFile:drivers/block/nbd.c]
|
| 96/2 |
2026/12/01 13:25 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/block/nbd.c Name:nbd_send_cmd]
Results: map[SourceCode: 650: /*
651: * Returns BLK_STS_RESOURCE if the caller should retry after a delay.
652: * Returns BLK_STS_IOERR if sending failed.
653: */
654: static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
655: int index)
656: {
657: struct request *req = blk_mq_rq_from_pdu(cmd);
658: struct nbd_config *config = nbd->config;
659: struct nbd_sock *nsock = config->socks[index];
660: int result;
661: struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
662: struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
663: struct iov_iter from;
664: struct bio *bio;
665: u64 handle;
666: u32 type;
667: u32 nbd_cmd_flags = 0;
668: int sent = nsock->sent, skip = 0;
669:
670: lockdep_assert_held(&cmd->lock);
671: lockdep_assert_held(&nsock->tx_lock);
672:
673: iov_iter_kvec(&from, ITER_SOURCE, &iov, 1, sizeof(request));
674:
675: type = req_to_nbd_cmd_type(req);
676: if (type == U32_MAX)
677: return BLK_STS_IOERR;
678:
679: if (rq_data_dir(req) == WRITE &&
680: (config->flags & NBD_FLAG_READ_ONLY)) {
681: dev_err_ratelimited(disk_to_dev(nbd->disk),
682: "Write on read-only\n");
683: return BLK_STS_IOERR;
684: }
685:
686: if (req->cmd_flags & REQ_FUA)
687: nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
688: if ((req->cmd_flags & REQ_NOUNMAP) && (type == NBD_CMD_WRITE_ZEROES))
689: nbd_cmd_flags |= NBD_CMD_FLAG_NO_HOLE;
690:
691: /* We did a partial send previously, and we at least sent the whole
692: * request struct, so just go and send the rest of the pages in the
693: * request.
694: */
695: if (sent) {
696: if (sent >= sizeof(request)) {
697: skip = sent - sizeof(request);
698:
699: /* initialize handle for tracing purposes */
700: handle = nbd_cmd_handle(cmd);
701:
702: goto send_pages;
703: }
704: iov_iter_advance(&from, sent);
705: } else {
706: cmd->cmd_cookie++;
707: }
708: cmd->index = index;
709: cmd->cookie = nsock->cookie;
710: cmd->retries = 0;
711: request.type = htonl(type | nbd_cmd_flags);
712: if (type != NBD_CMD_FLUSH) {
713: request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
714: request.len = htonl(blk_rq_bytes(req));
715: }
716: handle = nbd_cmd_handle(cmd);
717: request.cookie = cpu_to_be64(handle);
718:
719: trace_nbd_send_request(&request, nbd->index, blk_mq_rq_from_pdu(cmd));
720:
721: dev_dbg(nbd_to_dev(nbd), "request %p: sending control (%s@%llu,%uB)\n",
722: req, nbdcmd_to_ascii(type),
723: (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
724: result = sock_xmit(nbd, index, 1, &from,
725: (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
726: trace_nbd_header_sent(req, handle);
727: if (result < 0) {
728: if (was_interrupted(result)) {
729: /* If we haven't sent anything we can just return BUSY,
730: * however if we have sent something we need to make
731: * sure we only allow this req to be sent until we are
732: * completely done.
733: */
734: if (sent) {
735: nbd_sched_pending_work(nbd, nsock, cmd, sent);
736: return BLK_STS_OK;
737: }
738: set_bit(NBD_CMD_REQUEUED, &cmd->flags);
739: return BLK_STS_RESOURCE;
740: }
741: dev_err_ratelimited(disk_to_dev(nbd->disk),
742: "Send control failed (result %d)\n", result);
743: goto requeue;
744: }
745: send_pages:
746: if (type != NBD_CMD_WRITE)
747: goto out;
748:
749: bio = req->bio;
750: while (bio) {
751: struct bio *next = bio->bi_next;
752: struct bvec_iter iter;
753: struct bio_vec bvec;
754:
755: bio_for_each_segment(bvec, bio, iter) {
756: bool is_last = !next && bio_iter_last(bvec, iter);
757: int flags = is_last ? 0 : MSG_MORE;
758:
759: dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n",
760: req, bvec.bv_len);
761: iov_iter_bvec(&from, ITER_SOURCE, &bvec, 1, bvec.bv_len);
762: if (skip) {
763: if (skip >= iov_iter_count(&from)) {
764: skip -= iov_iter_count(&from);
765: continue;
766: }
767: iov_iter_advance(&from, skip);
768: skip = 0;
769: }
770: result = sock_xmit(nbd, index, 1, &from, flags, &sent);
771: if (result < 0) {
772: if (was_interrupted(result)) {
773: nbd_sched_pending_work(nbd, nsock, cmd, sent);
774: return BLK_STS_OK;
775: }
776: dev_err(disk_to_dev(nbd->disk),
777: "Send data failed (result %d)\n",
778: result);
779: goto requeue;
780: }
781: /*
782: * The completion might already have come in,
783: * so break for the last one instead of letting
784: * the iterator do it. This prevents use-after-free
785: * of the bio.
786: */
787: if (is_last)
788: break;
789: }
790: bio = next;
791: }
792: out:
793: trace_nbd_payload_sent(req, handle);
794: nsock->pending = NULL;
795: nsock->sent = 0;
796: __set_bit(NBD_CMD_INFLIGHT, &cmd->flags);
797: return BLK_STS_OK;
798:
799: requeue:
800: /*
801: * Can't requeue in case we are dealing with partial send
802: *
803: * We must run from pending work function.
804: * */
805: if (test_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags))
806: return BLK_STS_OK;
807:
808: /* retry on a different socket */
809: dev_err_ratelimited(disk_to_dev(nbd->disk),
810: "Request send failed, requeueing\n");
811: nbd_mark_nsock_dead(nbd, nsock, 1);
812: nbd_requeue_cmd(cmd);
813: return BLK_STS_OK;
814: }
SourceFile:drivers/block/nbd.c]
|
| 97/2 |
2026/12/01 13:25 |
llm |
cause-finder-compressor |
0m
Model:
Reply: ### Summary of Investigation
#### 1. Approaches Tried and Results
- **Examined the crash report and RCA**:
- The crash report shows a 11-link circular locking dependency involving `btrfs-tree-01` -> `btrfs-tree-00` -> `&q->q_usage_counter(io)` -> `&q->elevator_lock` -> `set->srcu` -> `&cmd->lock` -> `&nsock->tx_lock` -> `sk_lock-AF_INET` -> `&mm->mmap_lock` -> `sb_pagefaults#4` -> `&ei->i_mmap_lock` -> `btrfs-tree-01`.
- The RCA focuses heavily on `nbd.c`, specifically `nbd_reclassify_socket()` failing silently when `!sock_allow_reclassification(sk)` returns false due to concurrent user/softirq locking on the socket fd, leaving `nsock->sock->sk` in generic `sk_lock-AF_INET` instead of `sk_lock-AF_INET-NBD`.
- **Investigated Git history of `nbd_reclassify_socket` and socket reclassification**:
- `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency", Jun 13, 2026):
- Introduced `nbd_reclassify_socket()` in `nbd_add_socket()`.
- Included: `if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) return;`
- Stated intent: prevent false-positive lockdep circular dependencies when NBD is used for swap/writeback with TLS/TCP.
- Explicitly claimed `Fixes: ffa1e7ada456 ("block: Make request_queue lockdep splats show up earlier")`.
- `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock", Jun 22, 2026):
- Removed `WARN_ON_ONCE` because racing `NBD_CMD_CONNECT` with socket operations caused a panic under `panic_on_warn`.
- Made the failure to reclassify completely silent (`if (!sock_allow_reclassification(sk)) return;`).
- Stated: "skipping it in the rare racing case is harmless" - which directly allows the socket to stay in generic `sk_lock-AF_INET` and later trigger lockdep cycles like the one in this crash.
- `553d28925fde` ("nbd: Reclassify sockets in the reconnect path", Nov 12, 2026):
- Called `nbd_reclassify_socket()` in `nbd_genl_reconnect_sock_cb()` as well.
- Stated: `Fixes: d532cddb6c60`.
- **Investigated Git history of other links in the chain**:
- `fs/btrfs/file.c` (`btrfs_fallocate` holding `i_mmap_lock` across `file_modified` -> `btrfs_getxattr`):
- `8d9b4a162a37` (Josef Bacik, Feb 10, 2021): "btrfs: exclude mmap from happening during all fallocate operations" - added `btrfs_inode_lock(..., BTRFS_ILOCK_MMAP)` in `btrfs_fallocate`.
- `05fd9564e9faf` (Darrick Wong, Mar 14, 2022): "btrfs: fix fallocate to use file_modified to update permissions consistently" - added `file_modified(file)` under `btrfs_inode_lock`.
- `fs/btrfs/file.c` (`btrfs_page_mkwrite` holding `sb_pagefaults` and taking `i_mmap_lock`):
- Established invariant since ancient times (2016 or earlier).
- `block/elevator.c` and `block/blk-core.c` (`io_lockdep_map`, `q->elevator_lock`, `set->srcu`):
- `f1be1788a32e` (Ming Lei, Oct 25, 2024): "block: model freeze & enter queue as lock for supporting lockdep" - added `io_lockdep_map` to track `q_usage_counter(io)` as a lockdep class.
- `1bf70d08cc3b` (Nilay Shroff, Mar 4, 2025): "block: introduce a dedicated lock for protecting queue elevator updates" - introduced `q->elevator_lock`.
- `f8e111c859b9` (Ming Lei, May 5, 2025): "block: don't call freeze queue in elevator_switch() and elevator_disable()".
- `20117b5a4b9c6` (Ming Lei, May 5, 2025): "block: move queue freezing & elevator_lock into elevator_change()".
- `1e44bedbc921a` (Ming Lei, May 5, 2025): "block: unifying elevator change" - called `elevator_set_default()` in `blk_register_queue()`.
- `ffa1e7ada456` (Thomas Hellström, Mar 18, 2025): "block: Make request_queue lockdep splats show up earlier" - primed `io_lockdep_map` as `GFP_KERNEL` reclaim-tainted.
#### 2. Verified Facts & Established Conclusions
- **Before commit `d532cddb6c60`**:
- Sockets in `nbd.c` were **never** reclassified.
- Every NBD socket was always assigned and remained in `sk_lock-AF_INET`.
- Did the lockdep dependency chain exist before `d532cddb6c60`?
- If NBD sockets were never reclassified before `d532cddb6c60`, then `nsock->sock->sk` had `sk_lock-AF_INET` permanently.
- However, was the full chain already connected, or was `d532cddb6c60` intended to fix the exact circular dependency caused by NBD sockets sharing `sk_lock-AF_INET`?
- Let's verify what `d532cddb6c60` was fixing:
`Fixes: ffa1e7ada456 ("block: Make request_queue lockdep splats show up earlier")`
Commit `ffa1e7ada456` primed `io_lockdep_map` with `fs_reclaim`.
Commit `d532cddb6c60` attempted to fix the NBD socket lockdep issue by reclassifying them to `"sk_lock-AF_INET-NBD"`.
- But notice why this crash occurred at commit `19cc08c1a6a650f8e54c0461ff198be6448f425d`:
- In the crash, the socket lock held is `sk_lock-AF_INET`, NOT `sk_lock-AF_INET-NBD`!
- Why is it `sk_lock-AF_INET`?
Because `nbd_reclassify_socket()` was called, but `sock_allow_reclassification()` failed (or `nbd_reclassify_socket` was skipped).
- Commit `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock", Jun 22, 2026) explicitly removed the `WARN_ON_ONCE` when `!sock_allow_reclassification(sk)` and silently skipped reclassification under the incorrect claim:
`"Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless."`
This commit assumed skipping reclassification was harmless, but when reclassification is skipped, the socket remains in `sk_lock-AF_INET`, which then creates the circular locking dependency reported by lockdep!
- Alternatively: Did `d532cddb6c60` introduce the buggy reclassification logic in the first place? In `d532cddb6c60`, it had:
```c
if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
return;
```
In `d532cddb6c60`, if `!sock_allow_reclassification(sk)` was true, it also returned without reclassifying (and triggered a WARN_ON_ONCE).
- Or did an earlier commit (like `ffa1e7ada456`, `f1be1788a32e`, etc.) introduce the circular dependency?
- Or is `d532cddb6c60` the commit that attempted to fix it but was incomplete/buggy, OR is `9280e6edf656` the commit that made it silently fail, OR is there another commit?
#### 3. Current Hypotheses & Open Questions to Investigate
- **Hypothesis 1**: Commit `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock") introduced the bug.
- Reason: `9280e6edf656` removed the warning under the false assumption that skipping reclassification is harmless. Because it silently returned without reclassifying, the socket remained in `sk_lock-AF_INET`, allowing the circular dependency to form silently and splat later.
- Counter-reason: In `d532cddb6c60`, the socket *also* remained in `sk_lock-AF_INET` if `!sock_allow_reclassification(sk)` occurred (though it emitted a `WARN_ON_ONCE`). Furthermore, if `panic_on_warn` wasn't enabled, the exact same circular dependency would occur after the `WARN_ON_ONCE`.
- **Hypothesis 2**: Commit `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency") is the culprit.
- Reason: `d532cddb6c60` introduced `nbd_reclassify_socket()` with the broken pattern copied from nvme-tcp:
checking `sock_allow_reclassification()` without holding any lock or ensuring userspace wasn't using the socket, and returning without reclassifying or failing the setup if the socket was busy.
- Or did `d532cddb6c60` merely attempt an incomplete fix for an existing cycle?
- **Hypothesis 3**: What about `ffa1e7ada456` or `1e44bedbc921a` or `1bf70d08cc3b` or `20117b5a4b9c6`?
- Need to look at the prompt and the RCA:
The prompt's RCA is entitled:
"### Technical Root Cause Analysis
The circular locking dependency reported by lockdep involves two main issues in `drivers/block/nbd.c` when a filesystem (here `btrfs`) is mounted on an NBD device backed by an `AF_INET` TCP socket:
1. Silent failure of `nbd_reclassify_socket()` (`drivers/block/nbd.c`)...
2. Resulting dependency cycle..."
The RCA explicitly singles out:
"1. Silent failure of `nbd_reclassify_socket()` (`drivers/block/nbd.c`)"
"When that occurs, `nbd_reclassify_socket()` silently returns without reclassifying the socket and without failing `nbd_add_socket()`/`nbd_genl_reconnect_sock_cb()`, leaving `nsock->sock->sk` in the generic `sk_lock-AF_INET` lockdep class."
Notice the words: "**silently returns** without reclassifying".
In `d532cddb6c60`, it did **not** silently return: it warned with `WARN_ON_ONCE(!sock_allow_reclassification(sk))`.
It was `9280e6edf656` that changed `WARN_ON_ONCE(!sock_allow_reclassification(sk))` to `if (!sock_allow_reclassification(sk)) return;`, specifically stating in its commit log:
"Hitting a held lock here is expected for an externally owned socket and is not a kernel bug, so skip reclassification silently instead of warning. Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless."
- Wait! Did `9280e6edf656` introduce the silent failure, or is `d532cddb6c60` the commit that introduced `nbd_reclassify_socket`?
- What about `553d28925fde`? In the crash trace, was this `nbd_add_socket` or `nbd_genl_reconnect_sock_cb`?
The crash trace link #4 is `nbd_init` -> `nbd_dev_add` -> `add_disk` -> `blk_register_queue` -> `elevator_set_default` -> `elevator_change` -> `elevator_switch` -> `blk_mq_quiesce_queue`.
And link #7 is `recv_work` -> `nbd_mark_nsock_dead` -> `kernel_sock_shutdown` -> `inet_shutdown` -> `lock_sock`.
And the warning is:
`syz.0.1970/12965 is trying to acquire lock: btrfs-tree-01 at btrfs_tree_read_lock_nested... but task is already holding lock: &ei->i_mmap_lock at btrfs_inode_lock`
`which lock already depends on the new lock`.
- **Hypothesis 4**: Wait, could the commit that introduced the bug be in `block/` or `btrfs/` or `nbd/`?
Let's check if there are other commits or if one of `d532cddb6c60` vs `9280e6edf656` is the one.
Let's check `git log` and `git blame` for the exact lines in `nbd_reclassify_socket`.
#### 4. Critical Files and Code Snippets
- `drivers/block/nbd.c`:
```c
static void nbd_reclassify_socket(struct socket *sock)
{
struct sock *sk = sock->sk;
if (!sock_allow_reclassification(sk))
return;
switch (sk->sk_family) {
case AF_INET:
sock_lock_init_class_and_name(sk, "slock-AF_INET-NBD",
&nbd_slock_key[0],
"sk_lock-AF_INET-NBD",
&nbd_key[0]);
break;
...
```
- Introduced in `d532cddb6c60`:
```c
+static void nbd_reclassify_socket(struct socket *sock)
+{
+ struct sock *sk = sock->sk;
+
+ if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
+ return;
```
- Modified in `9280e6edf656`:
```c
- if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
+ if (!sock_allow_reclassification(sk))
return;
```
#### 5. Reasoning Loops to Avoid
- Do not blindly assume `9280e6edf656` just because it has the words "silently skip" without carefully comparing whether `d532cddb6c60` or `9280e6edf656` is considered the culprit according to kernel cause bisection rules (e.g. Rule 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").
- Note: `9280e6edf656` *removed* a `WARN_ON_ONCE`! And the author of `9280e6edf656` explicitly stated: "Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless" - which was completely wrong, because skipping it caused lockdep to report this circular dependency splat!
- Wait, was `d532cddb6c60` the commit that attempted to fix the circular dependency reported in syzbot (`Fixes: ffa1e7ada456`), but its design was flawed because `nbd_reclassify_socket` could not reliably reclassify user sockets?
- Need to check if there are other commits or discussions around `d532cddb6c60` and `9280e6edf656`.
#### 6. Next Steps for the Agent
1. Inspect the commit log, diff, and discussions/references for `9280e6edf656` and `d532cddb6c60`.
2. Check if the bug report or similar syzbot reports identify `9280e6edf656` or `d532cddb6c60` as the cause.
Search `git log` or syzkaller links if mentioned in commits.
3. Compare `9280e6edf656` vs `d532cddb6c60`:
- Under `d532cddb6c60`, did syzkaller hit `WARN_ON_ONCE(!sock_allow_reclassification(sk))`?
Yes: `Reported-by: syzbot+6b85d1e39a5b8ed9a954@syzkaller.appspotmail.com`.
- Because `WARN_ON_ONCE` fired and panicked under `panic_on_warn`, the circular locking dependency could not be reached when racing!
- Once `9280e6edf656` removed `WARN_ON_ONCE`, execution proceeded past `nbd_reclassify_socket()` with an unreclassified socket, subsequently triggering the circular locking dependency splat in `btrfs_fallocate`.
- On the other hand, did `9280e6edf656` introduce the bug, or did `9280e6edf656` introduce the silent skipping that caused the lockdep splat?
- Or is `9280e6edf656` the primary commit because its premise ("skipping it in the rare racing case is harmless") directly caused the bug?
- Evaluate whether `d532cddb6c60` or `9280e6edf656` is the primary culprit and make a clear determination with supporting justification.
Tokens:
input: 105046
output: 4513
thoughts: 0
|
| 98/2 |
2026/12/01 13:26 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 60005
output: 85
thoughts: 60
Thoughts: **My Deliberations on Commit Analysis**
Before diving into any substantive analysis, my primary thought is the need for precise, raw data. To that end, my immediate impulse is to perform a `git show` operation for each of the specified commit SHAs: `d532cddb6c60`, `9280e6edf656`, and `553d28925fde`. This isn't a superficial step; I require the absolute, unvarnished details from each commit. I need to see their full commit messages to grasp the intent and context, examine the exact diffs to understand the precise code changes introduced, and note the commit dates to establish a chronological understanding of their evolution. Only with this granular, definitive information can I proceed with any confidence in my subsequent interpretations and assessments.
|
| 99/2 |
2026/12/01 13:26 |
tool |
git-show |
0m
Args:
Results: map[Output:commit d532cddb6c6049ced414d64d83c6ce7149a6421a
Author: Eric Dumazet <edumazet@kernel.org>
Date: Sat Jun 13 04:26:19 2026 +0000
nbd: Reclassify sockets to avoid lockdep circular dependency
syzbot reported a possible circular locking dependency in udp_sendmsg()
where fs_reclaim can be triggered while holding sk_lock, and fs_reclaim
can eventually depend on another sk_lock (e.g., if NBD is used for swap
or writeback and NBD uses TLS/TCP which acquires sk_lock).
Since the UDP socket and the NBD TCP/TLS socket are different, this is a
false positive. Fix this by reclassifying NBD sockets to a separate lock
class when they are added to the NBD device.
This is similar to what nvme-tcp and other network block devices do.
Fixes: ffa1e7ada456 ("block: Make request_queue lockdep splats show up earlier")
Reported-by: syzbot+607cdcf978b3e79da878@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/netdev/6a2cdafe.428ffe26.258b27.0161.GAE@google.com/T/#u
Signed-off-by: Eric Dumazet <edumazet@google.com>
Link: https://patch.msgid.link/20260613042619.1108126-1-edumazet@google.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index e2fe9e3308fc..3a585a0c882a 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1238,6 +1238,42 @@ static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
return sock;
}
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+static struct lock_class_key nbd_key[3];
+static struct lock_class_key nbd_slock_key[3];
+
+static void nbd_reclassify_socket(struct socket *sock)
+{
+ struct sock *sk = sock->sk;
+
+ if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
+ return;
+
+ switch (sk->sk_family) {
+ case AF_INET:
+ sock_lock_init_class_and_name(sk, "slock-AF_INET-NBD",
+ &nbd_slock_key[0],
+ "sk_lock-AF_INET-NBD",
+ &nbd_key[0]);
+ break;
+ case AF_INET6:
+ sock_lock_init_class_and_name(sk, "slock-AF_INET6-NBD",
+ &nbd_slock_key[1],
+ "sk_lock-AF_INET6-NBD",
+ &nbd_key[1]);
+ break;
+ case AF_UNIX:
+ sock_lock_init_class_and_name(sk, "slock-AF_UNIX-NBD",
+ &nbd_slock_key[2],
+ "sk_lock-AF_UNIX-NBD",
+ &nbd_key[2]);
+ break;
+ }
+}
+#else
+static inline void nbd_reclassify_socket(struct socket *sock) {}
+#endif
+
static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
bool netlink)
{
@@ -1254,6 +1290,7 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
sock = nbd_get_socket(nbd, arg, &err);
if (!sock)
return err;
+ nbd_reclassify_socket(sock);
/*
* We need to make sure we don't get any errant requests while we're
]
|
| 100/2 |
2026/12/01 13:26 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 9280e6edf65662b6aafc8b704ad065b54c08b519
Author: Deepanshu Kartikey <kartikey406@gmail.com>
Date: Mon Jun 22 05:22:55 2026 +0530
nbd: don't warn when reclassifying a busy socket lock
nbd_reclassify_socket() warns via WARN_ON_ONCE() if the socket lock is
held at the point of reclassification. That assertion was copied from
nvme-tcp, where the socket is created internally by the kernel
(sock_create_kern()) and is never visible to user space, so the lock
is guaranteed to be free.
NBD is different: the socket is looked up from a user-supplied fd in
nbd_get_socket(), and user space retains that fd. A concurrent syscall
on the same socket (or softirq processing taking bh_lock_sock() on a
connected TCP socket) can legitimately hold the lock at the instant
NBD reclassifies it. sock_allow_reclassification() then returns false
and the WARN_ON_ONCE() fires, which turns into a crash under
panic_on_warn. This is reachable by simply racing NBD_CMD_CONNECT
against socket activity on the same fd, as reported by syzbot.
Hitting a held lock here is expected for an externally owned socket and
is not a kernel bug, so skip reclassification silently instead of
warning. Reclassification is a lockdep-only annotation, so skipping it
in the rare racing case is harmless.
Reported-by: syzbot+6b85d1e39a5b8ed9a954@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=6b85d1e39a5b8ed9a954
Fixes: d532cddb6c60 ("nbd: Reclassify sockets to avoid lockdep circular dependency")
Signed-off-by: Deepanshu Kartikey <kartikey406@gmail.com>
Acked-by: Eric Dumazet <edumazet@google.com>
Link: https://patch.msgid.link/20260621235255.66015-1-kartikey406@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 3a585a0c882a..8f10762e90ef 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1246,7 +1246,7 @@ static void nbd_reclassify_socket(struct socket *sock)
{
struct sock *sk = sock->sk;
- if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
+ if (!sock_allow_reclassification(sk))
return;
switch (sk->sk_family) {
]
|
| 101/2 |
2026/12/01 13:26 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 553d28925fdea4044bad4ae1e2bf9484ad21f758
Author: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>
Date: Thu Nov 12 07:59:39 2026 -0300
nbd: Reclassify sockets in the reconnect path
Commit d532cddb6c60 ("nbd: Reclassify sockets to avoid lockdep circular
dependency") introduced nbd_reclassify_socket() to prevent lockdep false
positives when NBD sockets are used in memory reclaim paths. While
nbd_add_socket() reclassifies newly added sockets,
nbd_genl_reconnect_sock_cb() omitted this call when replacing dead
sockets during reconnection. Consequently, reconnected sockets retain
the generic socket lockdep class and trigger false-positive lockdep
reports.
Fix this by reclassifying the replacement socket in
nbd_genl_reconnect_sock_cb().
Fixes: d532cddb6c60 ("nbd: Reclassify sockets to avoid lockdep circular dependency")
Reported-by: syzbot+d7d72f3d467710442af0@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=d7d72f3d467710442af0
Signed-off-by: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Link: https://patch.msgid.link/20261112105939.260856-1-trintaeoitogc@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index ffce519bf008..b2b27f90a7ed 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1356,6 +1356,7 @@ static int nbd_genl_reconnect_sock_cb(struct nbd_device *nbd, unsigned long arg)
sock = nbd_get_socket(nbd, arg, &err);
if (!sock)
return err;
+ nbd_reclassify_socket(sock);
args = kzalloc_obj(*args);
if (!args) {
]
|
| 102/2 |
2026/12/01 13:26 |
agent |
cause-finder-advisor |
7m
Model:
Results: map[Advice:You have already verified the root cause in `drivers/block/nbd.c` and confirmed the relevant commits with `git-show`. Stop further searching and call `set-results` immediately.
### Validation of Findings
1. **Mechanism confirmed in `drivers/block/nbd.c`**:
- Every path that assigns `nsock->sock` (`nbd_add_socket()` and, since `553d28925fde`, `nbd_genl_reconnect_sock_cb()`) calls `nbd_reclassify_socket(sock)`, and nothing resets `sk->sk_lock` afterward.
- In the crash trace, link `#7` (`recv_work()` -> `nbd_mark_nsock_dead()` -> `inet_shutdown()`) acquires `sk_lock-AF_INET` rather than `sk_lock-AF_INET-NBD`. For an `AF_INET` TCP socket, this only happens when `nbd_reclassify_socket()` hits `if (!sock_allow_reclassification(sk)) return;` because userspace or softirq concurrently holds `sk->sk_lock` / `sk->sk_lock.slock`.
2. **Primary culprit commit — `9280e6edf65662b6aafc8b704ad065b54c08b519` ("nbd: don't warn when reclassifying a busy socket lock")**:
- Do not blame block-layer lockdep annotation commits (`f1be1788a32e`, or `ffa1e7ada456` whose `fs_reclaim` priming is not even part of this dependency chain), nor valid btrfs/elevator lock orderings.
- `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency") added `nbd_reclassify_socket()` to isolate NBD sockets from generic `sk_lock-AF_INET`, asserting `WARN_ON_ONCE(!sock_allow_reclassification(sk))` under the `nvme-tcp` assumption that the socket lock is unheld.
- When syzbot hit that race on user-supplied NBD sockets, `9280e6edf65662b6aafc8b704ad065b54c08b519` specifically handled the busy-socket case by silently skipping reclassification instead of rejecting (`-EBUSY`) or synchronizing the socket, under the false assumption stated in its commit log: *"Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless."* Allowing NBD to proceed with an unreclassified `sk_lock-AF_INET` socket directly causes this lockdep circular dependency splat.
### Next Step
Call `set-results` now with **only** `9280e6edf65662b6aafc8b704ad065b54c08b519` and a clear explanation of why silently skipping socket reclassification when `!sock_allow_reclassification(sk)` leaves `nsock->sock->sk` in `sk_lock-AF_INET` and triggers the circular locking dependency.]
Instruction:
You are a senior Linux kernel developer supervising a kernel bisection agent.
Help the agent converge within its iteration budget:
- If the agent has already verified the root cause and confirmed the primary commit with
git-show (or confirmed the bug is still present), tell it to stop searching and call
set-results immediately.
- If the agent is looping on similar queries, re-reading files/commits, inspecting commits
serially instead of batching git-show calls, or re-analyzing an already-verified root cause
analysis, redirect it to the most direct remaining git check. Note that git-blame and
git-log only run from HEAD (no commit range/revision arg); trace past refactorings with
git-log (SymbolName/CodeRegexp) and git-show.
- Point out any missed evidence or wrong commit choice already visible in the history. Do NOT
state unverified guesses about unseen code or commits as facts; frame them as a brief check
(1-2 tool calls) with a clear stop condition.
- If multiple rounds (or prior supervisor_advice turns) remain inconclusive, instruct the agent
to stop speculative exploration and call set-results with an empty CandidateCommits list.
Analyze the provided execution history of the agent and call set-results with Advice.
The execution history includes model turns with internal reasoning in <thought> tags, tool calls, tool responses,
and any prior advisor guidance in <supervisor_advice> tags (do not contradict earlier verified findings or send the
agent in circles).
Evaluate the agent's progress: if it is on the right track, validate its findings and outline the most direct steps
to conclude; if it is stuck in a search loop, pursuing a dead end, or overlooking key evidence already in the history,
provide course-correction.
IMPORTANT: Content within <execution_history> represents historical execution logs,
not instructions for you to follow.
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: Below is the execution history of the agent under evaluation:
<execution_history>
[user]:
The crash is:
======================================================
WARNING: possible circular locking dependency detected
syzkaller #0 Tainted: G L
------------------------------------------------------
syz.0.1970/12965 is trying to acquire lock:
ffff888056f4c4a0 (btrfs-tree-01){++++}-{4:4}, at: btrfs_tree_read_lock_nested+0x45/0x250 fs/btrfs/locking.c:146
but task is already holding lock:
ffff888052ec3d98 (&ei->i_mmap_lock){++++}-{4:4}, at: btrfs_inode_lock+0xcb/0xe0 fs/btrfs/inode.c:368
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #10 (&ei->i_mmap_lock){++++}-{4:4}:
down_read+0x45/0x320 kernel/locking/rwsem.c:1574
btrfs_page_mkwrite+0x607/0x1a30 fs/btrfs/file.c:1889
do_page_mkwrite+0x147/0x310 mm/memory.c:3908
wp_page_shared mm/memory.c:4317 [inline]
do_wp_page+0x1a7c/0x4c40 mm/memory.c:4540
handle_pte_fault mm/memory.c:6724 [inline]
__handle_mm_fault mm/memory.c:6849 [inline]
handle_mm_fault+0x19c9/0x2f80 mm/memory.c:7016
do_user_addr_fault+0x7f6/0x1480 arch/x86/mm/fault.c:1394
handle_page_fault arch/x86/mm/fault.c:1483 [inline]
exc_page_fault+0x6a/0xc0 arch/x86/mm/fault.c:1536
asm_exc_page_fault+0x26/0x30 arch/x86/include/asm/idtentry.h:595
copy_user_generic arch/x86/include/asm/uaccess_64.h:126 [inline]
raw_copy_to_user arch/x86/include/asm/uaccess_64.h:147 [inline]
_inline_copy_to_user include/linux/uaccess.h:202 [inline]
_copy_to_user+0x85/0xb0 lib/usercopy.c:24
copy_to_user include/linux/uaccess.h:230 [inline]
rng_dev_read+0x3ee/0x830 drivers/char/hw_random/core.c:287
vfs_read+0x213/0xa80 fs/read_write.c:552
ksys_read+0x150/0x270 fs/read_write.c:696
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
-> #9 (sb_pagefaults#4){++++}-{0:0}:
percpu_down_read_internal include/linux/percpu-rwsem.h:53 [inline]
percpu_down_read_freezable include/linux/percpu-rwsem.h:83 [inline]
__sb_start_write include/linux/fs/super.h:19 [inline]
sb_start_pagefault include/linux/fs/super.h:159 [inline]
btrfs_page_mkwrite+0x2fe/0x1a30 fs/btrfs/file.c:1844
do_page_mkwrite+0x147/0x310 mm/memory.c:3908
wp_page_shared mm/memory.c:4317 [inline]
do_wp_page+0x1a7c/0x4c40 mm/memory.c:4540
handle_pte_fault mm/memory.c:6724 [inline]
__handle_mm_fault mm/memory.c:6849 [inline]
handle_mm_fault+0x19c9/0x2f80 mm/memory.c:7016
do_user_addr_fault+0x7f6/0x1480 arch/x86/mm/fault.c:1394
handle_page_fault arch/x86/mm/fault.c:1483 [inline]
exc_page_fault+0x6a/0xc0 arch/x86/mm/fault.c:1536
asm_exc_page_fault+0x26/0x30 arch/x86/include/asm/idtentry.h:595
copy_user_generic arch/x86/include/asm/uaccess_64.h:126 [inline]
raw_copy_to_user arch/x86/include/asm/uaccess_64.h:147 [inline]
_inline_copy_to_user include/linux/uaccess.h:202 [inline]
_copy_to_user+0x85/0xb0 lib/usercopy.c:24
copy_to_user include/linux/uaccess.h:230 [inline]
rng_dev_read+0x3ee/0x830 drivers/char/hw_random/core.c:287
vfs_read+0x213/0xa80 fs/read_write.c:552
ksys_read+0x150/0x270 fs/read_write.c:696
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
-> #8 (&mm->mmap_lock){++++}-{4:4}:
__might_fault+0xcb/0x130 mm/memory.c:7607
_copy_from_iter+0x100/0x1680 lib/iov_iter.c:271
copy_from_iter include/linux/uio.h:236 [inline]
copy_from_iter_full include/linux/uio.h:253 [inline]
skb_do_copy_data_nocache include/net/sock.h:2345 [inline]
skb_copy_to_page_nocache include/net/sock.h:2371 [inline]
tcp_sendmsg_locked+0x244e/0x57c0 net/ipv4/tcp.c:1324
tcp_sendmsg+0x2f/0x50 net/ipv4/tcp.c:1467
sock_sendmsg_nosec+0x10f/0x190 net/socket.c:801
__sock_sendmsg net/socket.c:818 [inline]
sock_write_iter+0x2de/0x3e0 net/socket.c:1269
new_sync_write fs/read_write.c:575 [inline]
vfs_write+0x612/0xba0 fs/read_write.c:667
ksys_write+0x150/0x270 fs/read_write.c:719
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
-> #7 (sk_lock-AF_INET){+.+.}-{0:0}:
lock_sock_nested+0x41/0x100 net/core/sock.c:3909
lock_sock include/net/sock.h:1717 [inline]
inet_shutdown+0x6a/0x390 net/ipv4/af_inet.c:913
nbd_mark_nsock_dead+0x2cb/0x550 drivers/block/nbd.c:319
recv_work+0x1d14/0x1e30 drivers/block/nbd.c:1030
process_one_work kernel/workqueue.c:3497 [inline]
process_scheduled_works+0xc5d/0x1650 kernel/workqueue.c:3580
worker_thread+0xa47/0xfb0 kernel/workqueue.c:3661
kthread+0x38b/0x480 kernel/kthread.c:436
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
-> #6 (&nsock->tx_lock){+.+.}-{4:4}:
__mutex_lock_common kernel/locking/mutex.c:646 [inline]
__mutex_lock+0x197/0x15a0 kernel/locking/mutex.c:821
nbd_handle_cmd drivers/block/nbd.c:1143 [inline]
nbd_queue_rq+0x373/0xff0 drivers/block/nbd.c:1207
blk_mq_dispatch_rq_list+0x499/0x1990 block/blk-mq.c:2141
__blk_mq_do_dispatch_sched block/blk-mq-sched.c:168 [inline]
blk_mq_do_dispatch_sched block/blk-mq-sched.c:182 [inline]
__blk_mq_sched_dispatch_requests+0xd36/0x1580 block/blk-mq-sched.c:307
blk_mq_sched_dispatch_requests+0xd7/0x190 block/blk-mq-sched.c:329
blk_mq_run_work_fn+0x167/0x2f0 block/blk-mq.c:2556
process_one_work kernel/workqueue.c:3497 [inline]
process_scheduled_works+0xc5d/0x1650 kernel/workqueue.c:3580
worker_thread+0xa47/0xfb0 kernel/workqueue.c:3661
kthread+0x38b/0x480 kernel/kthread.c:436
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
-> #5 (&cmd->lock){+.+.}-{4:4}:
__mutex_lock_common kernel/locking/mutex.c:646 [inline]
__mutex_lock+0x197/0x15a0 kernel/locking/mutex.c:821
nbd_queue_rq+0xc1/0xff0 drivers/block/nbd.c:1199
blk_mq_dispatch_rq_list+0x499/0x1990 block/blk-mq.c:2141
__blk_mq_do_dispatch_sched block/blk-mq-sched.c:168 [inline]
blk_mq_do_dispatch_sched block/blk-mq-sched.c:182 [inline]
__blk_mq_sched_dispatch_requests+0xd36/0x1580 block/blk-mq-sched.c:307
blk_mq_sched_dispatch_requests+0xd7/0x190 block/blk-mq-sched.c:329
blk_mq_run_work_fn+0x167/0x2f0 block/blk-mq.c:2556
process_one_work kernel/workqueue.c:3497 [inline]
process_scheduled_works+0xc5d/0x1650 kernel/workqueue.c:3580
worker_thread+0xa47/0xfb0 kernel/workqueue.c:3661
kthread+0x38b/0x480 kernel/kthread.c:436
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
-> #4 (set->srcu){.+.+}-{0:0}:
srcu_lock_sync include/linux/srcu.h:261 [inline]
__synchronize_srcu+0xca/0x2f0 kernel/rcu/srcutree.c:1662
elevator_switch+0x1e8/0x7b0 block/elevator.c:576
elevator_change+0x2fa/0x480 block/elevator.c:681
elevator_set_default+0x375/0x440 block/elevator.c:754
blk_register_queue+0x404/0x4f0 block/blk-sysfs.c:992
__add_disk+0x6b8/0xdb0 block/genhd.c:531
add_disk_fwnode+0x100/0x3a0 block/genhd.c:600
add_disk include/linux/blkdev.h:799 [inline]
nbd_dev_add+0x733/0xb60 drivers/block/nbd.c:2027
nbd_init+0x168/0x1f0 drivers/block/nbd.c:2744
do_one_initcall+0x250/0x870 init/main.c:1432
do_initcall_level+0x10a/0x1a0 init/main.c:1494
do_initcalls+0x59/0xa0 init/main.c:1510
kernel_init_freeable+0x29d/0x3e0 init/main.c:1745
kernel_init+0x22/0x1d0 init/main.c:1635
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
-> #3 (&q->elevator_lock){+.+.}-{4:4}:
__mutex_lock_common kernel/locking/mutex.c:646 [inline]
__mutex_lock+0x197/0x15a0 kernel/locking/mutex.c:821
elevator_change+0x1af/0x480 block/elevator.c:679
elv_iosched_store+0x50e/0x630 block/elevator.c:831
queue_attr_store+0x207/0x2b0 block/blk-sysfs.c:906
kernfs_fop_write_iter+0x3a4/0x540 fs/kernfs/file.c:345
new_sync_write fs/read_write.c:575 [inline]
vfs_write+0x612/0xba0 fs/read_write.c:667
ksys_write+0x150/0x270 fs/read_write.c:719
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
-> #2 (&q->q_usage_counter(io)#17){++++}-{0:0}:
bio_queue_enter block/blk.h:95 [inline]
blk_mq_submit_bio+0x7c6/0x2eb0 block/blk-mq.c:3149
__submit_bio_noacct_mq block/blk-core.c:755 [inline]
submit_bio_noacct_nocheck+0x2fe/0xb80 block/blk-core.c:789
btrfs_submit_chunk fs/btrfs/bio.c:823 [inline]
btrfs_submit_bbio+0x132d/0x1ee0 fs/btrfs/bio.c:882
read_extent_buffer_pages_nowait+0x747/0x860 fs/btrfs/extent_io.c:4381
read_extent_buffer_pages+0x22/0x100 fs/btrfs/extent_io.c:4390
btrfs_read_extent_buffer+0x9d/0x730 fs/btrfs/disk-io.c:236
read_block_for_search+0x67e/0xa40 fs/btrfs/ctree.c:1569
btrfs_search_slot+0x147a/0x2d20 fs/btrfs/ctree.c:2198
btrfs_init_root_free_objectid+0xf5/0x290 fs/btrfs/disk-io.c:5058
btrfs_init_fs_root+0x204/0x3b0 fs/btrfs/disk-io.c:1114
btrfs_get_root_ref+0x3c8/0x890 fs/btrfs/disk-io.c:1365
btrfs_get_fs_root fs/btrfs/disk-io.c:1417 [inline]
open_ctree+0x2d07/0x4140 fs/btrfs/disk-io.c:3805
btrfs_fill_super fs/btrfs/super.c:971 [inline]
btrfs_get_tree_super fs/btrfs/super.c:1956 [inline]
btrfs_get_tree_subvol fs/btrfs/super.c:2099 [inline]
btrfs_get_tree+0x10cf/0x1a00 fs/btrfs/super.c:2133
vfs_get_tree+0x92/0x2a0 fs/super.c:1881
fc_mount fs/namespace.c:1282 [inline]
do_new_mount_fc fs/namespace.c:3910 [inline]
do_new_mount+0x325/0xb00 fs/namespace.c:3986
do_mount fs/namespace.c:4350 [inline]
__do_sys_mount fs/namespace.c:4563 [inline]
__se_sys_mount+0x31d/0x420 fs/namespace.c:4540
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
-> #1 (btrfs-tree-00){++++}-{4:4}:
down_read_nested+0x47/0x320 kernel/locking/rwsem.c:1709
btrfs_tree_read_lock_nested+0x45/0x250 fs/btrfs/locking.c:146
btrfs_tree_read_lock fs/btrfs/locking.h:188 [inline]
read_block_for_search+0x617/0xa40 fs/btrfs/ctree.c:1563
btrfs_search_slot+0x147a/0x2d20 fs/btrfs/ctree.c:2198
btrfs_init_root_free_objectid+0xf5/0x290 fs/btrfs/disk-io.c:5058
btrfs_init_fs_root+0x204/0x3b0 fs/btrfs/disk-io.c:1114
btrfs_get_root_ref+0x3c8/0x890 fs/btrfs/disk-io.c:1365
btrfs_get_fs_root fs/btrfs/disk-io.c:1417 [inline]
open_ctree+0x2d07/0x4140 fs/btrfs/disk-io.c:3805
btrfs_fill_super fs/btrfs/super.c:971 [inline]
btrfs_get_tree_super fs/btrfs/super.c:1956 [inline]
btrfs_get_tree_subvol fs/btrfs/super.c:2099 [inline]
btrfs_get_tree+0x10cf/0x1a00 fs/btrfs/super.c:2133
vfs_get_tree+0x92/0x2a0 fs/super.c:1881
fc_mount fs/namespace.c:1282 [inline]
do_new_mount_fc fs/namespace.c:3910 [inline]
do_new_mount+0x325/0xb00 fs/namespace.c:3986
do_mount fs/namespace.c:4350 [inline]
__do_sys_mount fs/namespace.c:4563 [inline]
__se_sys_mount+0x31d/0x420 fs/namespace.c:4540
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
-> #0 (btrfs-tree-01){++++}-{4:4}:
check_prev_add kernel/locking/lockdep.c:3209 [inline]
check_prevs_add kernel/locking/lockdep.c:3328 [inline]
validate_chain kernel/locking/lockdep.c:3952 [inline]
__lock_acquire+0x164c/0x2de0 kernel/locking/lockdep.c:5288
lock_acquire+0x115/0x350 kernel/locking/lockdep.c:5942
down_read_nested+0x47/0x320 kernel/locking/rwsem.c:1709
btrfs_tree_read_lock_nested+0x45/0x250 fs/btrfs/locking.c:146
btrfs_tree_read_lock fs/btrfs/locking.h:188 [inline]
btrfs_read_lock_root_node+0x3f/0xd0 fs/btrfs/locking.c:267
btrfs_search_slot_get_root fs/btrfs/ctree.c:1740 [inline]
btrfs_search_slot+0x66c/0x2d20 fs/btrfs/ctree.c:2070
btrfs_lookup_match_dir fs/btrfs/dir-item.c:196 [inline]
btrfs_lookup_xattr+0xff/0x1e0 fs/btrfs/dir-item.c:369
btrfs_getxattr+0x97/0x1b0 fs/btrfs/xattr.c:41
btrfs_xattr_handler_get_security+0xd0/0x130 fs/btrfs/xattr.c:398
__vfs_getxattr+0x412/0x440 fs/xattr.c:446
cap_inode_need_killpriv+0x45/0x60 security/commoncap.c:331
security_inode_need_killpriv+0x85/0x240 security/security.c:2209
dentry_needs_remove_privs fs/inode.c:2372 [inline]
file_remove_privs_flags+0x277/0x610 fs/inode.c:2403
file_modified_flags fs/inode.c:2552 [inline]
file_modified+0x18/0x50 fs/inode.c:2571
btrfs_fallocate+0x79a/0x1df0 fs/btrfs/file.c:3126
vfs_fallocate+0x65a/0x7e0 fs/open.c:338
ksys_fallocate fs/open.c:362 [inline]
__do_sys_fallocate fs/open.c:367 [inline]
__se_sys_fallocate fs/open.c:365 [inline]
__x64_sys_fallocate+0xbf/0x110 fs/open.c:365
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
other info that might help us debug this:
Chain exists of:
btrfs-tree-01 --> sb_pagefaults#4 --> &ei->i_mmap_lock
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&ei->i_mmap_lock);
lock(sb_pagefaults#4);
lock(&ei->i_mmap_lock);
rlock(btrfs-tree-01);
*** DEADLOCK ***
locks held by syz.0.1970/12965: 3, on CPU#0:
#0: ffff88805558c478 (sb_writers#12){++++}-{0:0}, at: file_start_write include/linux/fs.h:2765 [inline]
#0: ffff88805558c478 (sb_writers#12){++++}-{0:0}, at: vfs_fallocate+0x5e0/0x7e0 fs/open.c:337
#1: ffff888052ec3f00 (&sb->s_type->i_mutex_key#24){++++}-{4:4}, at: inode_lock include/linux/fs.h:1027 [inline]
#1: ffff888052ec3f00 (&sb->s_type->i_mutex_key#24){++++}-{4:4}, at: btrfs_inode_lock+0x51/0xe0 fs/btrfs/inode.c:365
#2: ffff888052ec3d98 (&ei->i_mmap_lock){++++}-{4:4}, at: btrfs_inode_lock+0xcb/0xe0 fs/btrfs/inode.c:368
stack backtrace:
CPU: 0 UID: 0 PID: 12965 Comm: syz.0.1970 Tainted: G L syzkaller #0 PREEMPT(full)
Tainted: [L]=SOFTLOCKUP
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
print_circular_bug+0x2e2/0x300 kernel/locking/lockdep.c:2087
check_noncircular+0x12f/0x150 kernel/locking/lockdep.c:2219
check_prev_add kernel/locking/lockdep.c:3209 [inline]
check_prevs_add kernel/locking/lockdep.c:3328 [inline]
validate_chain kernel/locking/lockdep.c:3952 [inline]
__lock_acquire+0x164c/0x2de0 kernel/locking/lockdep.c:5288
lock_acquire+0x115/0x350 kernel/locking/lockdep.c:5942
down_read_nested+0x47/0x320 kernel/locking/rwsem.c:1709
btrfs_tree_read_lock_nested+0x45/0x250 fs/btrfs/locking.c:146
btrfs_tree_read_lock fs/btrfs/locking.h:188 [inline]
btrfs_read_lock_root_node+0x3f/0xd0 fs/btrfs/locking.c:267
btrfs_search_slot_get_root fs/btrfs/ctree.c:1740 [inline]
btrfs_search_slot+0x66c/0x2d20 fs/btrfs/ctree.c:2070
btrfs_lookup_match_dir fs/btrfs/dir-item.c:196 [inline]
btrfs_lookup_xattr+0xff/0x1e0 fs/btrfs/dir-item.c:369
btrfs_getxattr+0x97/0x1b0 fs/btrfs/xattr.c:41
btrfs_xattr_handler_get_security+0xd0/0x130 fs/btrfs/xattr.c:398
__vfs_getxattr+0x412/0x440 fs/xattr.c:446
cap_inode_need_killpriv+0x45/0x60 security/commoncap.c:331
security_inode_need_killpriv+0x85/0x240 security/security.c:2209
dentry_needs_remove_privs fs/inode.c:2372 [inline]
file_remove_privs_flags+0x277/0x610 fs/inode.c:2403
file_modified_flags fs/inode.c:2552 [inline]
file_modified+0x18/0x50 fs/inode.c:2571
btrfs_fallocate+0x79a/0x1df0 fs/btrfs/file.c:3126
vfs_fallocate+0x65a/0x7e0 fs/open.c:338
ksys_fallocate fs/open.c:362 [inline]
__do_sys_fallocate fs/open.c:367 [inline]
__se_sys_fallocate fs/open.c:365 [inline]
__x64_sys_fallocate+0xbf/0x110 fs/open.c:365
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:0x7f81335a9c99
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f81344d8028 EFLAGS: 00000246 ORIG_RAX: 000000000000011d
RAX: ffffffffffffffda RBX: 00007f8133835fa0 RCX: 00007f81335a9c99
RDX: 0000000000000006 RSI: 0000000000000000 RDI: 0000000000000004
RBP: 00007f813363b18b R08: 0000000000000000 R09: 0000000000000000
R10: 000000000000ffff R11: 0000000000000246 R12: 0000000000000000
R13: 00007f8133836038 R14: 00007f8133835fa0 R15: 00007ffe47c43d78
</TASK>
A previous root cause analysis of this bug (at commit 19cc08c1a6a650f8e54c0461ff198be6448f425d) concluded:
### Technical Root Cause Analysis
The circular locking dependency reported by lockdep involves two main issues in `drivers/block/nbd.c` when a filesystem (here `btrfs`) is mounted on an NBD device backed by an `AF_INET` TCP socket:
#### 1. Silent failure of `nbd_reclassify_socket()` (`drivers/block/nbd.c`)
When a socket file descriptor is attached to an NBD device via `NBD_SET_SOCK` (`nbd_add_socket()` at `drivers/block/nbd.c:1277`) or reconnected via netlink (`nbd_genl_reconnect_sock_cb()` at `drivers/block/nbd.c:1348`), `nbd_get_socket()` (`drivers/block/nbd.c:1213`) looks up the socket using `sockfd_lookup()` and then calls `nbd_reclassify_socket(sock)` (`drivers/block/nbd.c:1245`).
`nbd_reclassify_socket()` is intended to move the socket's `sk->sk_lock` and `sk->sk_lock.slock` into NBD-specific lockdep classes (`"sk_lock-AF_INET-NBD"` / `&nbd_key[0]`, etc.) via `sock_lock_init_class_and_name()`. However, it performs an unlocked check:
```c
if (!sock_allow_reclassification(sk))
return;
```
(`drivers/block/nbd.c:1249`, where `sock_allow_reclassification()` in `include/net/sock.h:1835` checks `!sock_owned_by_user_nocheck(sk) && !spin_is_locked(&sk->sk_lock.slock)`).
Because the socket originates from userspace and userspace still holds the file descriptor, another thread calling a socket syscall (e.g., `sendmsg`, `recvmsg`, `setsockopt`, `connect`, `shutdown`) or softirq processing incoming packets via `bh_lock_sock(sk)` can concurrently hold `sk->sk_lock` or `sk->sk_lock.slock` when `nbd_reclassify_socket()` runs. When that occurs, `nbd_reclassify_socket()` silently returns without reclassifying the socket and without failing `nbd_add_socket()`/`nbd_genl_reconnect_sock_cb()`, leaving `nsock->sock->sk` in the generic `sk_lock-AF_INET` lockdep class.
#### 2. Resulting dependency cycle (and same-socket deadlock exposure)
With the NBD socket remaining in `sk_lock-AF_INET` (or when userspace continues operating on the same NBD socket fd), lockdep records the following cycle:
1. **`&ei->i_mmap_lock` → `btrfs-tree-01` → `btrfs-tree-00` → `&q->q_usage_counter(io)`**:
- `btrfs_fallocate()` (`fs/btrfs/file.c:3118`) calls `btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_MMAP)`, acquiring `down_write(&ei->i_mmap_lock)` (`fs/btrfs/inode.c:368`).
- While holding `i_mmap_lock`, it calls `file_modified()` (`fs/btrfs/file.c:3126`) → `file_remove_privs_flags()` → `security_inode_need_killpriv()` → `btrfs_xattr_handler_get_security()` → `btrfs_getxattr()` → `btrfs_lookup_xattr()` → `btrfs_search_slot()` → `btrfs_read_lock_root_node()`, acquiring `btrfs-tree-01` (`btrfs_tree_read_lock_nested()`, `fs/btrfs/locking.c:146`).
- Reading child tree blocks in `read_block_for_search()` (`fs/btrfs/ctree.c:1563–1569`) acquires `btrfs-tree-00` and submits metadata I/O via `btrfs_read_extent_buffer()` → `btrfs_submit_bbio()` → `blk_mq_submit_bio()`, entering `&q->q_usage_counter(io)`.
2. **`&q->q_usage_counter(io)` → `&q->elevator_lock` → `set->srcu` → `&cmd->lock` → `&nsock->tx_lock` → `sk_lock-AF_INET`**:
- Block queue reconfiguration (`elevator_change()` / `elevator_switch()` in `block/elevator.c:576, 668–681`) nests queue freeze (`q_usage_counter`) → `&q->elevator_lock` → `blk_mq_quiesce_queue()` (`set->srcu`).
- `blk_mq_dispatch_rq_list()` invokes `nbd_queue_rq()` (`drivers/block/nbd.c:1184`), which acquires `&cmd->lock` → `&nsock->tx_lock` (`nbd_handle_cmd()`, `drivers/block/nbd.c:1143`).
- Under `&nsock->tx_lock`, NBD acquires `sk->sk_lock` both in `nbd_send_cmd()` → `sock_sendmsg()` (`drivers/block/nbd.c:585`) and in `recv_work()` → `nbd_mark_nsock_dead()` → `kernel_sock_shutdown(nsock->sock, SHUT_RDWR)` → `inet_shutdown()` → `lock_sock()` (`drivers/block/nbd.c:319, 1030`), recorded against `sk_lock-AF_INET` (link `#7`).
3. **`sk_lock-AF_INET` → `&mm->mmap_lock` → `sb_pagefaults` → `&ei->i_mmap_lock`**:
- Userspace TCP I/O holds `lock_sock(sk)` (`sk_lock-AF_INET`) across user-memory copies that can fault: `tcp_sendmsg_locked()` (`net/ipv4/tcp.c:1324`) calls `skb_copy_to_page_nocache()` → `_copy_from_iter()` → `__might_fault()` (`mm/memory.c:7607`), and `tcp_recvmsg()` (`net/ipv4/tcp.c:2956`) holds `lock_sock(sk)` across `skb_copy_datagram_msg()` (`net/ipv4/tcp.c:2853`).
- A page write fault against an `mmap()`ed file on the btrfs-on-NBD filesystem enters `do_page_mkwrite()` → `btrfs_page_mkwrite()` (`fs/btrfs/file.c:1844, 1889`), acquiring `sb_start_pagefault()` (`sb_pagefaults`) and `down_read(&inode->i_mmap_lock)` (`&ei->i_mmap_lock`), closing the cycle.
Note also that because `nbd_get_socket()` only borrows a `struct file` reference via `sockfd_lookup()` and leaves the socket file descriptor open in userspace, a task can also issue `recvmsg()`/`read()` directly on the NBD socket fd into an `mmap()`ed page backed by a file on `/dev/nbdX`, creating a real AB-BA lock inversion on the exact same `sk->sk_lock` and `ei->i_mmap_lock` instances even without lockdep class sharing.
Search for the commit that introduced this bug.
[user]:
Here is the summary of the previous execution history:
### Summary of Investigation
#### 1. Approaches Tried and Results
- **Examined the crash report and RCA**:
- The crash report shows a 11-link circular locking dependency involving `btrfs-tree-01` -> `btrfs-tree-00` -> `&q->q_usage_counter(io)` -> `&q->elevator_lock` -> `set->srcu` -> `&cmd->lock` -> `&nsock->tx_lock` -> `sk_lock-AF_INET` -> `&mm->mmap_lock` -> `sb_pagefaults#4` -> `&ei->i_mmap_lock` -> `btrfs-tree-01`.
- The RCA focuses heavily on `nbd.c`, specifically `nbd_reclassify_socket()` failing silently when `!sock_allow_reclassification(sk)` returns false due to concurrent user/softirq locking on the socket fd, leaving `nsock->sock->sk` in generic `sk_lock-AF_INET` instead of `sk_lock-AF_INET-NBD`.
- **Investigated Git history of `nbd_reclassify_socket` and socket reclassification**:
- `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency", Jun 13, 2026):
- Introduced `nbd_reclassify_socket()` in `nbd_add_socket()`.
- Included: `if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) return;`
- Stated intent: prevent false-positive lockdep circular dependencies when NBD is used for swap/writeback with TLS/TCP.
- Explicitly claimed `Fixes: ffa1e7ada456 ("block: Make request_queue lockdep splats show up earlier")`.
- `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock", Jun 22, 2026):
- Removed `WARN_ON_ONCE` because racing `NBD_CMD_CONNECT` with socket operations caused a panic under `panic_on_warn`.
- Made the failure to reclassify completely silent (`if (!sock_allow_reclassification(sk)) return;`).
- Stated: "skipping it in the rare racing case is harmless" - which directly allows the socket to stay in generic `sk_lock-AF_INET` and later trigger lockdep cycles like the one in this crash.
- `553d28925fde` ("nbd: Reclassify sockets in the reconnect path", Nov 12, 2026):
- Called `nbd_reclassify_socket()` in `nbd_genl_reconnect_sock_cb()` as well.
- Stated: `Fixes: d532cddb6c60`.
- **Investigated Git history of other links in the chain**:
- `fs/btrfs/file.c` (`btrfs_fallocate` holding `i_mmap_lock` across `file_modified` -> `btrfs_getxattr`):
- `8d9b4a162a37` (Josef Bacik, Feb 10, 2021): "btrfs: exclude mmap from happening during all fallocate operations" - added `btrfs_inode_lock(..., BTRFS_ILOCK_MMAP)` in `btrfs_fallocate`.
- `05fd9564e9faf` (Darrick Wong, Mar 14, 2022): "btrfs: fix fallocate to use file_modified to update permissions consistently" - added `file_modified(file)` under `btrfs_inode_lock`.
- `fs/btrfs/file.c` (`btrfs_page_mkwrite` holding `sb_pagefaults` and taking `i_mmap_lock`):
- Established invariant since ancient times (2016 or earlier).
- `block/elevator.c` and `block/blk-core.c` (`io_lockdep_map`, `q->elevator_lock`, `set->srcu`):
- `f1be1788a32e` (Ming Lei, Oct 25, 2024): "block: model freeze & enter queue as lock for supporting lockdep" - added `io_lockdep_map` to track `q_usage_counter(io)` as a lockdep class.
- `1bf70d08cc3b` (Nilay Shroff, Mar 4, 2025): "block: introduce a dedicated lock for protecting queue elevator updates" - introduced `q->elevator_lock`.
- `f8e111c859b9` (Ming Lei, May 5, 2025): "block: don't call freeze queue in elevator_switch() and elevator_disable()".
- `20117b5a4b9c6` (Ming Lei, May 5, 2025): "block: move queue freezing & elevator_lock into elevator_change()".
- `1e44bedbc921a` (Ming Lei, May 5, 2025): "block: unifying elevator change" - called `elevator_set_default()` in `blk_register_queue()`.
- `ffa1e7ada456` (Thomas Hellström, Mar 18, 2025): "block: Make request_queue lockdep splats show up earlier" - primed `io_lockdep_map` as `GFP_KERNEL` reclaim-tainted.
#### 2. Verified Facts & Established Conclusions
- **Before commit `d532cddb6c60`**:
- Sockets in `nbd.c` were **never** reclassified.
- Every NBD socket was always assigned and remained in `sk_lock-AF_INET`.
- Did the lockdep dependency chain exist before `d532cddb6c60`?
- If NBD sockets were never reclassified before `d532cddb6c60`, then `nsock->sock->sk` had `sk_lock-AF_INET` permanently.
- However, was the full chain already connected, or was `d532cddb6c60` intended to fix the exact circular dependency caused by NBD sockets sharing `sk_lock-AF_INET`?
- Let's verify what `d532cddb6c60` was fixing:
`Fixes: ffa1e7ada456 ("block: Make request_queue lockdep splats show up earlier")`
Commit `ffa1e7ada456` primed `io_lockdep_map` with `fs_reclaim`.
Commit `d532cddb6c60` attempted to fix the NBD socket lockdep issue by reclassifying them to `"sk_lock-AF_INET-NBD"`.
- But notice why this crash occurred at commit `19cc08c1a6a650f8e54c0461ff198be6448f425d`:
- In the crash, the socket lock held is `sk_lock-AF_INET`, NOT `sk_lock-AF_INET-NBD`!
- Why is it `sk_lock-AF_INET`?
Because `nbd_reclassify_socket()` was called, but `sock_allow_reclassification()` failed (or `nbd_reclassify_socket` was skipped).
- Commit `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock", Jun 22, 2026) explicitly removed the `WARN_ON_ONCE` when `!sock_allow_reclassification(sk)` and silently skipped reclassification under the incorrect claim:
`"Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless."`
This commit assumed skipping reclassification was harmless, but when reclassification is skipped, the socket remains in `sk_lock-AF_INET`, which then creates the circular locking dependency reported by lockdep!
- Alternatively: Did `d532cddb6c60` introduce the buggy reclassification logic in the first place? In `d532cddb6c60`, it had:
```c
if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
return;
```
In `d532cddb6c60`, if `!sock_allow_reclassification(sk)` was true, it also returned without reclassifying (and triggered a WARN_ON_ONCE).
- Or did an earlier commit (like `ffa1e7ada456`, `f1be1788a32e`, etc.) introduce the circular dependency?
- Or is `d532cddb6c60` the commit that attempted to fix it but was incomplete/buggy, OR is `9280e6edf656` the commit that made it silently fail, OR is there another commit?
#### 3. Current Hypotheses & Open Questions to Investigate
- **Hypothesis 1**: Commit `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock") introduced the bug.
- Reason: `9280e6edf656` removed the warning under the false assumption that skipping reclassification is harmless. Because it silently returned without reclassifying, the socket remained in `sk_lock-AF_INET`, allowing the circular dependency to form silently and splat later.
- Counter-reason: In `d532cddb6c60`, the socket *also* remained in `sk_lock-AF_INET` if `!sock_allow_reclassification(sk)` occurred (though it emitted a `WARN_ON_ONCE`). Furthermore, if `panic_on_warn` wasn't enabled, the exact same circular dependency would occur after the `WARN_ON_ONCE`.
- **Hypothesis 2**: Commit `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency") is the culprit.
- Reason: `d532cddb6c60` introduced `nbd_reclassify_socket()` with the broken pattern copied from nvme-tcp:
checking `sock_allow_reclassification()` without holding any lock or ensuring userspace wasn't using the socket, and returning without reclassifying or failing the setup if the socket was busy.
- Or did `d532cddb6c60` merely attempt an incomplete fix for an existing cycle?
- **Hypothesis 3**: What about `ffa1e7ada456` or `1e44bedbc921a` or `1bf70d08cc3b` or `20117b5a4b9c6`?
- Need to look at the prompt and the RCA:
The prompt's RCA is entitled:
"### Technical Root Cause Analysis
The circular locking dependency reported by lockdep involves two main issues in `drivers/block/nbd.c` when a filesystem (here `btrfs`) is mounted on an NBD device backed by an `AF_INET` TCP socket:
1. Silent failure of `nbd_reclassify_socket()` (`drivers/block/nbd.c`)...
2. Resulting dependency cycle..."
The RCA explicitly singles out:
"1. Silent failure of `nbd_reclassify_socket()` (`drivers/block/nbd.c`)"
"When that occurs, `nbd_reclassify_socket()` silently returns without reclassifying the socket and without failing `nbd_add_socket()`/`nbd_genl_reconnect_sock_cb()`, leaving `nsock->sock->sk` in the generic `sk_lock-AF_INET` lockdep class."
Notice the words: "**silently returns** without reclassifying".
In `d532cddb6c60`, it did **not** silently return: it warned with `WARN_ON_ONCE(!sock_allow_reclassification(sk))`.
It was `9280e6edf656` that changed `WARN_ON_ONCE(!sock_allow_reclassification(sk))` to `if (!sock_allow_reclassification(sk)) return;`, specifically stating in its commit log:
"Hitting a held lock here is expected for an externally owned socket and is not a kernel bug, so skip reclassification silently instead of warning. Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless."
- Wait! Did `9280e6edf656` introduce the silent failure, or is `d532cddb6c60` the commit that introduced `nbd_reclassify_socket`?
- What about `553d28925fde`? In the crash trace, was this `nbd_add_socket` or `nbd_genl_reconnect_sock_cb`?
The crash trace link #4 is `nbd_init` -> `nbd_dev_add` -> `add_disk` -> `blk_register_queue` -> `elevator_set_default` -> `elevator_change` -> `elevator_switch` -> `blk_mq_quiesce_queue`.
And link #7 is `recv_work` -> `nbd_mark_nsock_dead` -> `kernel_sock_shutdown` -> `inet_shutdown` -> `lock_sock`.
And the warning is:
`syz.0.1970/12965 is trying to acquire lock: btrfs-tree-01 at btrfs_tree_read_lock_nested... but task is already holding lock: &ei->i_mmap_lock at btrfs_inode_lock`
`which lock already depends on the new lock`.
- **Hypothesis 4**: Wait, could the commit that introduced the bug be in `block/` or `btrfs/` or `nbd/`?
Let's check if there are other commits or if one of `d532cddb6c60` vs `9280e6edf656` is the one.
Let's check `git log` and `git blame` for the exact lines in `nbd_reclassify_socket`.
#### 4. Critical Files and Code Snippets
- `drivers/block/nbd.c`:
```c
static void nbd_reclassify_socket(struct socket *sock)
{
struct sock *sk = sock->sk;
if (!sock_allow_reclassification(sk))
return;
switch (sk->sk_family) {
case AF_INET:
sock_lock_init_class_and_name(sk, "slock-AF_INET-NBD",
&nbd_slock_key[0],
"sk_lock-AF_INET-NBD",
&nbd_key[0]);
break;
...
```
- Introduced in `d532cddb6c60`:
```c
+static void nbd_reclassify_socket(struct socket *sock)
+{
+ struct sock *sk = sock->sk;
+
+ if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
+ return;
```
- Modified in `9280e6edf656`:
```c
- if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
+ if (!sock_allow_reclassification(sk))
return;
```
#### 5. Reasoning Loops to Avoid
- Do not blindly assume `9280e6edf656` just because it has the words "silently skip" without carefully comparing whether `d532cddb6c60` or `9280e6edf656` is considered the culprit according to kernel cause bisection rules (e.g. Rule 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").
- Note: `9280e6edf656` *removed* a `WARN_ON_ONCE`! And the author of `9280e6edf656` explicitly stated: "Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless" - which was completely wrong, because skipping it caused lockdep to report this circular dependency splat!
- Wait, was `d532cddb6c60` the commit that attempted to fix the circular dependency reported in syzbot (`Fixes: ffa1e7ada456`), but its design was flawed because `nbd_reclassify_socket` could not reliably reclassify user sockets?
- Need to check if there are other commits or discussions around `d532cddb6c60` and `9280e6edf656`.
#### 6. Next Steps for the Agent
1. Inspect the commit log, diff, and discussions/references for `9280e6edf656` and `d532cddb6c60`.
2. Check if the bug report or similar syzbot reports identify `9280e6edf656` or `d532cddb6c60` as the cause.
Search `git log` or syzkaller links if mentioned in commits.
3. Compare `9280e6edf656` vs `d532cddb6c60`:
- Under `d532cddb6c60`, did syzkaller hit `WARN_ON_ONCE(!sock_allow_reclassification(sk))`?
Yes: `Reported-by: syzbot+6b85d1e39a5b8ed9a954@syzkaller.appspotmail.com`.
- Because `WARN_ON_ONCE` fired and panicked under `panic_on_warn`, the circular locking dependency could not be reached when racing!
- Once `9280e6edf656` removed `WARN_ON_ONCE`, execution proceeded past `nbd_reclassify_socket()` with an unreclassified socket, subsequently triggering the circular locking dependency splat in `btrfs_fallocate`.
- On the other hand, did `9280e6edf656` introduce the bug, or did `9280e6edf656` introduce the silent skipping that caused the lockdep splat?
- Or is `9280e6edf656` the primary commit because its premise ("skipping it in the rare racing case is harmless") directly caused the bug?
- Evaluate whether `d532cddb6c60` or `9280e6edf656` is the primary culprit and make a clear determination with supporting justification.
[model]:
<thought>
**My Thought Process on NBD Locking Dependencies**
Okay, so I was digging into the locking mechanisms within NBD, and this commit, `1bf70d08cc3b`, immediately caught my eye because it represented a significant shift. Before this commit, I observed that `elv_iosched_store()` was acquiring `q->sysfs_lock` and then proceeding down a path that involved `blk_mq_freeze_queue` and `blk_mq_quiesce_queue`, which ultimately touched `set->srcu`.
But the *after* picture with `1bf70d08cc3b` is different. Now, `blk_mq_freeze_queue` seems to be the entry point, and it's directly referencing `q->q_usage_counter(io)`. This then leads to `q->elevator_lock` and subsequently to `blk_mq_quiesce_queue` and `set->srcu`. What's crucial here is that the acquisition of `q->sysfs_lock` has changed its position relative to `q->q_usage_counter(io)`. It used to be *before* `blk_mq_freeze_queue`, meaning `q->q_usage_counter(io)` wasn't necessarily preceding `q->sysfs_lock`.
However, my deeper dive, specifically looking at the `elevator_switch` function, revealed that the sequence `memflags = blk_mq_freeze_queue(q);` followed by `blk_mq_quiesce_queue(q);` (which then accesses `set->srcu`) has been the consistent pattern. This means that `q->q_usage_counter(io)` *has* always been on the path leading to `set->srcu`, at least since commit `f1be1788a32e` in October 2024, and even before lockdep started tracking `q->q_usage_counter` explicitly.
Now, stepping back to see the larger context – the "BIG PICTURE" – I'm trying to understand the root cause of the reported circular locking dependency between NBD and user-space sockets (`sk_lock-AF_INET`). My analysis points to NBD's fundamental nature: it's a block device that relies on a socket. The block I/O path, specifically `nbd_queue_rq` and `recv_work`, acquires `sk_lock` while it already holds several block layer locks, including `&q->q_usage_counter(io)`, `set->srcu`, `&cmd->lock`, and `&nsock->tx_lock`.
The problem arises because normal user-space socket syscalls like `tcp_sendmsg` and `tcp_recvmsg` also acquire `sk_lock-AF_INET`. These operations can then lead to accesses to user memory via `__might_fault` and involve `&mm->mmap_lock`. Furthermore, page faults on mmaped files that reside on NBD can trigger a chain reaction: `sb_pagefaults` -> `i_mmap_lock` -> `btrfs-tree` -> and finally, `&q->q_usage_counter(io)`. This creates the circular dependency.
It's important to note that this fundamental issue – NBD sharing `sk_lock-AF_INET` with regular user sockets – has been present since NBD's inception, or at least since commits `560bc4b39952` and `f1be1788a32e` made `q_usage_counter(io)` visible to lockdep.
Looking at the proposed solutions, commit `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency") and `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock") seem to be addressing this by reclassifying sockets. To fully understand the implications and ensure correctness, I need to examine how other drivers in the kernel handle similar situations. My next step is to search for all other call sites of `sock_lock_init_class_and_name` and `sock_allow_reclassification` across the kernel source tree. I'll be grepping for these functions to gain insight into established patterns and best practices for socket reclassification.
</thought>
Called tool grepper with args: {"Expression":"sock_allow_reclassification|sock_lock_init_class_and_name"}
[user]:
Tool grepper returned: {"Output":"drivers/block/nbd.c=1245=static void nbd_reclassify_socket(struct socket *sock)\n--\ndrivers/block/nbd.c-1248-\ndrivers/block/nbd.c:1249:\tif (!sock_allow_reclassification(sk))\ndrivers/block/nbd.c-1250-\t\treturn;\n--\ndrivers/block/nbd.c-1253-\tcase AF_INET:\ndrivers/block/nbd.c:1254:\t\tsock_lock_init_class_and_name(sk, \"slock-AF_INET-NBD\",\ndrivers/block/nbd.c-1255-\t\t\t\t\t \u0026nbd_slock_key[0],\n--\ndrivers/block/nbd.c-1259-\tcase AF_INET6:\ndrivers/block/nbd.c:1260:\t\tsock_lock_init_class_and_name(sk, \"slock-AF_INET6-NBD\",\ndrivers/block/nbd.c-1261-\t\t\t\t\t \u0026nbd_slock_key[1],\n--\ndrivers/block/nbd.c-1265-\tcase AF_UNIX:\ndrivers/block/nbd.c:1266:\t\tsock_lock_init_class_and_name(sk, \"slock-AF_UNIX-NBD\",\ndrivers/block/nbd.c-1267-\t\t\t\t\t \u0026nbd_slock_key[2],\n--\ndrivers/infiniband/sw/rxe/rxe_net.c=45=static inline void rxe_reclassify_recv_socket(struct socket *sock)\n--\ndrivers/infiniband/sw/rxe/rxe_net.c-49-\ndrivers/infiniband/sw/rxe/rxe_net.c:50:\tif (WARN_ON_ONCE(!sock_allow_reclassification(sk)))\ndrivers/infiniband/sw/rxe/rxe_net.c-51-\t\treturn;\n--\ndrivers/infiniband/sw/rxe/rxe_net.c-54-\tcase AF_INET:\ndrivers/infiniband/sw/rxe/rxe_net.c:55:\t\tsock_lock_init_class_and_name(sk,\ndrivers/infiniband/sw/rxe/rxe_net.c-56-\t\t\t\t\t \"slock-AF_INET-RDMA-RXE-RECV\",\n--\ndrivers/infiniband/sw/rxe/rxe_net.c-61-\tcase AF_INET6:\ndrivers/infiniband/sw/rxe/rxe_net.c:62:\t\tsock_lock_init_class_and_name(sk,\ndrivers/infiniband/sw/rxe/rxe_net.c-63-\t\t\t\t\t \"slock-AF_INET6-RDMA-RXE-RECV\",\n--\ndrivers/infiniband/sw/rxe/rxe_net.c-72-\t/*\ndrivers/infiniband/sw/rxe/rxe_net.c:73:\t * sock_lock_init_class_and_name() calls\ndrivers/infiniband/sw/rxe/rxe_net.c-74-\t * sk_owner_set(sk, THIS_MODULE); in order\n--\ndrivers/infiniband/sw/rxe/rxe_qp.c=37=static inline void rxe_reclassify_send_socket(struct socket *sock)\n--\ndrivers/infiniband/sw/rxe/rxe_qp.c-41-\ndrivers/infiniband/sw/rxe/rxe_qp.c:42:\tif (WARN_ON_ONCE(!sock_allow_reclassification(sk)))\ndrivers/infiniband/sw/rxe/rxe_qp.c-43-\t\treturn;\n--\ndrivers/infiniband/sw/rxe/rxe_qp.c-46-\tcase AF_INET:\ndrivers/infiniband/sw/rxe/rxe_qp.c:47:\t\tsock_lock_init_class_and_name(sk,\ndrivers/infiniband/sw/rxe/rxe_qp.c-48-\t\t\t\t\t \"slock-AF_INET-RDMA-RXE-SEND\",\n--\ndrivers/infiniband/sw/rxe/rxe_qp.c-53-\tcase AF_INET6:\ndrivers/infiniband/sw/rxe/rxe_qp.c:54:\t\tsock_lock_init_class_and_name(sk,\ndrivers/infiniband/sw/rxe/rxe_qp.c-55-\t\t\t\t\t \"slock-AF_INET6-RDMA-RXE-SEND\",\n--\ndrivers/infiniband/sw/siw/siw_cm.c=62=static inline void siw_reclassify_socket(struct socket *sock)\n--\ndrivers/infiniband/sw/siw/siw_cm.c-66-\ndrivers/infiniband/sw/siw/siw_cm.c:67:\tif (WARN_ON_ONCE(!sock_allow_reclassification(sk)))\ndrivers/infiniband/sw/siw/siw_cm.c-68-\t\treturn;\n--\ndrivers/infiniband/sw/siw/siw_cm.c-71-\tcase AF_INET:\ndrivers/infiniband/sw/siw/siw_cm.c:72:\t\tsock_lock_init_class_and_name(sk,\ndrivers/infiniband/sw/siw/siw_cm.c-73-\t\t\t\t\t \"slock-AF_INET-RDMA-SIW\",\n--\ndrivers/infiniband/sw/siw/siw_cm.c-78-\tcase AF_INET6:\ndrivers/infiniband/sw/siw/siw_cm.c:79:\t\tsock_lock_init_class_and_name(sk,\ndrivers/infiniband/sw/siw/siw_cm.c-80-\t\t\t\t\t \"slock-AF_INET6-RDMA-SIW\",\n--\ndrivers/nvme/host/tcp.c=228=static void nvme_tcp_reclassify_socket(struct socket *sock)\n--\ndrivers/nvme/host/tcp.c-231-\ndrivers/nvme/host/tcp.c:232:\tif (WARN_ON_ONCE(!sock_allow_reclassification(sk)))\ndrivers/nvme/host/tcp.c-233-\t\treturn;\n--\ndrivers/nvme/host/tcp.c-236-\tcase AF_INET:\ndrivers/nvme/host/tcp.c:237:\t\tsock_lock_init_class_and_name(sk, \"slock-AF_INET-NVME\",\ndrivers/nvme/host/tcp.c-238-\t\t\t\t\t \u0026nvme_tcp_slock_key[0],\n--\ndrivers/nvme/host/tcp.c-242-\tcase AF_INET6:\ndrivers/nvme/host/tcp.c:243:\t\tsock_lock_init_class_and_name(sk, \"slock-AF_INET6-NVME\",\ndrivers/nvme/host/tcp.c-244-\t\t\t\t\t \u0026nvme_tcp_slock_key[1],\n--\nfs/smb/client/connect.c=3144=cifs_reclassify_socket4(struct socket *sock)\n--\nfs/smb/client/connect.c-3147-\nfs/smb/client/connect.c:3148:\tBUG_ON(!sock_allow_reclassification(sk));\nfs/smb/client/connect.c:3149:\tsock_lock_init_class_and_name(sk, \"slock-AF_INET-CIFS\",\nfs/smb/client/connect.c-3150-\t\t\u0026cifs_slock_key[0], \"sk_lock-AF_INET-CIFS\", \u0026cifs_key[0]);\n--\nfs/smb/client/connect.c=3154=cifs_reclassify_socket6(struct socket *sock)\n--\nfs/smb/client/connect.c-3157-\nfs/smb/client/connect.c:3158:\tBUG_ON(!sock_allow_reclassification(sk));\nfs/smb/client/connect.c:3159:\tsock_lock_init_class_and_name(sk, \"slock-AF_INET6-CIFS\",\nfs/smb/client/connect.c-3160-\t\t\u0026cifs_slock_key[1], \"sk_lock-AF_INET6-CIFS\", \u0026cifs_key[1]);\n--\ninclude/net/sock.h=242=struct sk_filter;\n--\ninclude/net/sock.h-363- *\t@sk_owner: reference to the real owner of the socket that calls\ninclude/net/sock.h:364: *\t\t sock_lock_init_class_and_name().\ninclude/net/sock.h-365- */\n--\ninclude/net/sock.h=1683=static inline void sk_owner_put(struct sock *sk)\n--\ninclude/net/sock.h-1693- */\ninclude/net/sock.h:1694:#define sock_lock_init_class_and_name(sk, sname, skey, name, key)\t\\\ninclude/net/sock.h-1695-do {\t\t\t\t\t\t\t\t\t\\\n--\ninclude/net/sock.h=1825=static inline void sock_release_ownership(struct sock *sk)\n--\ninclude/net/sock.h-1834-/* no reclassification while locks are held */\ninclude/net/sock.h:1835:static inline bool sock_allow_reclassification(const struct sock *csk)\ninclude/net/sock.h-1836-{\n--\nnet/bluetooth/af_bluetooth.c=71=void bt_sock_reclassify_lock(struct sock *sk, int proto)\n--\nnet/bluetooth/af_bluetooth.c-73-\tBUG_ON(!sk);\nnet/bluetooth/af_bluetooth.c:74:\tBUG_ON(!sock_allow_reclassification(sk));\nnet/bluetooth/af_bluetooth.c-75-\nnet/bluetooth/af_bluetooth.c:76:\tsock_lock_init_class_and_name(sk,\nnet/bluetooth/af_bluetooth.c-77-\t\t\t\t bt_slock_key_strings[proto], \u0026bt_slock_key[proto],\n--\nnet/core/sock.c=2216=static inline void sock_lock_init(struct sock *sk)\n--\nnet/core/sock.c-2220-\tif (sk-\u003esk_kern_sock)\nnet/core/sock.c:2221:\t\tsock_lock_init_class_and_name(\nnet/core/sock.c-2222-\t\t\tsk,\n--\nnet/core/sock.c-2227-\telse\nnet/core/sock.c:2228:\t\tsock_lock_init_class_and_name(\nnet/core/sock.c-2229-\t\t\tsk,\n--\nnet/mptcp/pm_kernel.c=852=static int mptcp_pm_nl_create_listen_socket(struct sock *sk,\n--\nnet/mptcp/pm_kernel.c-876-\t */\nnet/mptcp/pm_kernel.c:877:\tsock_lock_init_class_and_name(newsk,\nnet/mptcp/pm_kernel.c-878-\t\t\t\t is_ipv6 ? \"mlock-AF_INET6\" : \"mlock-AF_INET\",\n--\nnet/smc/af_smc.c=395=void smc_sk_init(struct net *net, struct sock *sk, int protocol)\n--\nnet/smc/af_smc.c-407-\tINIT_LIST_HEAD(\u0026smc-\u003eaccept_q);\nnet/smc/af_smc.c:408:\tsock_lock_init_class_and_name(sk, \"slock-AF_SMC\", \u0026smc_slock_key,\nnet/smc/af_smc.c-409-\t\t\t\t \"sk_lock-AF_SMC\", \u0026smc_key);\n--\nnet/sunrpc/svcsock.c=95=static void svc_reclassify_socket(struct socket *sock)\n--\nnet/sunrpc/svcsock.c-98-\nnet/sunrpc/svcsock.c:99:\tif (WARN_ON_ONCE(!sock_allow_reclassification(sk)))\nnet/sunrpc/svcsock.c-100-\t\treturn;\n--\nnet/sunrpc/svcsock.c-103-\tcase AF_INET:\nnet/sunrpc/svcsock.c:104:\t\tsock_lock_init_class_and_name(sk, \"slock-AF_INET-NFSD\",\nnet/sunrpc/svcsock.c-105-\t\t\t\t\t \u0026svc_slock_key[0],\n--\nnet/sunrpc/svcsock.c-110-\tcase AF_INET6:\nnet/sunrpc/svcsock.c:111:\t\tsock_lock_init_class_and_name(sk, \"slock-AF_INET6-NFSD\",\nnet/sunrpc/svcsock.c-112-\t\t\t\t\t \u0026svc_slock_key[1],\n--\nnet/sunrpc/xprtsock.c=1891=static inline void xs_reclassify_socketu(struct socket *sock)\n--\nnet/sunrpc/xprtsock.c-1894-\nnet/sunrpc/xprtsock.c:1895:\tsock_lock_init_class_and_name(sk, \"slock-AF_LOCAL-RPC\",\nnet/sunrpc/xprtsock.c-1896-\t\t\u0026xs_slock_key[0], \"sk_lock-AF_LOCAL-RPC\", \u0026xs_key[0]);\n--\nnet/sunrpc/xprtsock.c=1899=static inline void xs_reclassify_socket4(struct socket *sock)\n--\nnet/sunrpc/xprtsock.c-1902-\nnet/sunrpc/xprtsock.c:1903:\tsock_lock_init_class_and_name(sk, \"slock-AF_INET-RPC\",\nnet/sunrpc/xprtsock.c-1904-\t\t\u0026xs_slock_key[1], \"sk_lock-AF_INET-RPC\", \u0026xs_key[1]);\n--\nnet/sunrpc/xprtsock.c=1907=static inline void xs_reclassify_socket6(struct socket *sock)\n--\nnet/sunrpc/xprtsock.c-1910-\nnet/sunrpc/xprtsock.c:1911:\tsock_lock_init_class_and_name(sk, \"slock-AF_INET6-RPC\",\nnet/sunrpc/xprtsock.c-1912-\t\t\u0026xs_slock_key[2], \"sk_lock-AF_INET6-RPC\", \u0026xs_key[2]);\n--\nnet/sunrpc/xprtsock.c=1915=static inline void xs_reclassify_socket(int family, struct socket *sock)\nnet/sunrpc/xprtsock.c-1916-{\nnet/sunrpc/xprtsock.c:1917:\tif (WARN_ON_ONCE(!sock_allow_reclassification(sock-\u003esk)))\nnet/sunrpc/xprtsock.c-1918-\t\treturn;\n"}
[model]:
<thought>
**It appears the syzkaller crash I'm looking at is a lockdep issue, and the root cause might be older than I initially suspected.**
My immediate reaction was to scrutinize the usage of `sock_allow_reclassification` and `sock_lock_init_class_and_name` across different kernel modules. I observed that `nvme_tcp_reclassify_socket` and `cifs_reclassify_socket4/6` use `WARN_ON_ONCE` or `BUG_ON` respectively when `sock_allow_reclassification` returns false, and these are for kernel-created sockets. `svc_reclassify_socket` also uses `WARN_ON_ONCE`, but I need to investigate its interaction with user file descriptors further. What's particularly striking is that `nbd_reclassify_socket` is the *only* one that had a change (`9280e6edf656`) which removed `WARN_ON_ONCE`, leaving only `if (!sock_allow_reclassification(sk)) return;`. This stands out.
Then, I dug into the git history around the specific crash commit (`19cc08c1a6a650f8e54c0461ff198be6448f425d`). My initial thought was that the commit `f1be1788a32e`, which added `io_lockdep_map`, might be the culprit. However, I quickly remembered Rule 4 of lockdep analysis: don't blame commits that merely add debugging checks unless the check itself is flawed.
This led me to question whether `f1be1788a32e` was exposing an *existing* bug or if the lockdep cycle itself was a false positive, only becoming a real deadlock if userspace interacted with the *same* socket. I hypothesized that if socket S1 (e.g., the NBD socket, `sk_lock-AF_INET-NBD`) and socket S2 (a regular TCP socket, `sk_lock-AF_INET`) were involved, and S1 wasn't reclassified correctly (staying `sk_lock-AF_INET`), lockdep might report a false positive. A *real* deadlock, however, would occur if userspace operated on S1 itself, even if it *was* reclassified to `sk_lock-AF_INET-NBD`, because the user would still hold the file descriptor.
Looking at the crash report, I saw that the stack trace showed `#7` as `sk_lock-AF_INET` (not `sk_lock-AF_INET-NBD`) during `nbd_mark_nsock_dead` and `inet_shutdown`. Crucially, `#8` also involved `sk_lock-AF_INET` via `tcp_sendmsg_locked`, and further down the trace, page faults related to Btrfs indicated interaction with `i_mmap_lock`. This confirmed my suspicion: because the NBD socket was still classified as `sk_lock-AF_INET` instead of the more specific `sk_lock-AF_INET-NBD`, lockdep connected the NBD socket's lock acquisition (`#7`) with the generic `sk_lock-AF_INET` lock acquisition during `tcp_sendmsg_locked` (`#8`), creating the perceived cycle.
The "million dollar question" then became: which commit actually *introduced* this bug? I meticulously reviewed the commits related to NBD socket reclassification: `d532cddb6c60`, `9280e6edf656`, and `553d28925fde`, as well as older commits like `560bc4b39952`. My critical realization was that *before* `d532cddb6c60`, NBD sockets were *never* reclassified, meaning they were *always* in `sk_lock-AF_INET`. This strongly suggests that the lockdep cycle I'm seeing already *existed* prior to `d532cddb6c60`.
I then proceeded to verify if *every single link* in the reported lockdep cycle was present *before* `d532cddb6c60`. This involved checking the history of functions involved in the cycle: `blk_mq_submit_bio`, `blk_mq_freeze_queue`, `btrfs_fallocate` (and its associated locks like `i_mmap_lock`), `nbd_queue_rq`, `tcp_sendmsg_locked`, and `nbd_mark_nsock_dead`. Based on the commit timestamps I examined, it appears that all these individual locking patterns were indeed in place well before the NBD reclassification efforts began.
My current line of thinking is that `d532cddb6c60` and subsequent commits might have *exposed* or *made more likely* an existing, latent lockdep issue by altering the NBD socket's classification, rather than *introducing* the fundamental cycle itself. The bug, as it manifests in this syzkaller crash, seems to be a consequence of the generic `sk_lock-AF_INET` being used for the NBD socket, which then creates a perceived circular dependency with other operations that also use `sk_lock-AF_INET` (or locks that are transitively related to it) in a context where a specific NBD reclassification would have broken the cycle. I am now focused on confirming that no link in the cycle was *newly* introduced by a commit between `d532cddb6c60~1` and the crash, and if all links predate `d532cddb6c60`, then the bug's origin is much older.
</thought>
Called tool git-log with args: {"Count":30,"PathPrefix":"drivers/block/nbd.c","Since":"2 years"}
[user]:
Tool git-log returned: {"Output":"553d28925fde nbd: Reclassify sockets in the reconnect path\n326d49039c10 nbd: add pre_defined_connections module parameter for pre-created devices\na9d414b4a15c nbd: remove queue freeze for newly created nbd from netlink path\nf8d21c590e55 nbd: factor out a nbd_genl_foreach_sock\ndb285268df79 nbd: skip queue freeze when setting size at device startup\n285908f554fe nbd: remove queue freeze in nbd_add_socket\n0fdee7c5fa88 nbd: clear queue limits on disconnect\n04d8fb23e520 nbd: disallow NBD_SET_SOCK on an active device\nfe247030f1ce nbd: simplify find_fallback() by removing redundant logic\n9280e6edf656 nbd: don't warn when reclassifying a busy socket lock\nd532cddb6c60 nbd: Reclassify sockets to avoid lockdep circular dependency\nb040a1a4523d block: switch numa_node to int in blk_mq_hw_ctx and init_request\nbf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument\n69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types\n1649714b930f nbd: defer config unlock in nbd_genl_connect\n9517b82d8d42 nbd: defer config put in recv_work\n4601b7923d1b nbd: don't copy kernel creds\n81ccca31214e nbd: override creds to kernel when calling sock_{send,recv}msg()\n51723bf92679 drivers/block: replace use of system_wq with system_percpu_wq\n9f7c02e03157 nbd: restrict sockets to TCP and UDP\n8b428f42f3ed nbd: fix lockdep deadlock warning\naa9552438ebf nbd: fix uaf in nbd_genl_connect() error path\n1e1a9cecfab3 block: force noio scope in blk_mq_freeze_queue\n8337b029f788 nbd: fix partial sending\nf3dec61d7544 nbd: fix queue freeze vs limits lock order\n844b8cdc6816 nbd: don't allow reconnect after disconnect\ncc76ace465d6 block: remove BLK_MQ_F_SHOULD_MERGE\n"}
[model]:
<thought>
**Navigating the Labyrinth of Kernel Development: A Dive into Lockdep Deadlock Resolution**
Alright, my focus has just landed on this commit: `8b428f42f3ed nbd: fix lockdep deadlock warning`. This immediately grabs my attention because, as anyone deep in kernel development knows, lockdep warnings are often intricate, subtle beasts. They point to potential race conditions that can manifest in unpredictable and devastating ways. The fact that this is related to `nbd` (Network Block Device) is also noteworthy; it's an area with its own set of concurrency challenges, especially when dealing with asynchronous operations and interactions with the block layer.
My immediate instinct is to peel back the layers of this specific fix. To do that, I need to understand the context. So, I'll be pulling up `git show 8b428f42f3ed` to get the full picture of *this* commit. What exactly changed? What was the specific locking scenario that triggered the warning? I'll be looking for the precise lines of code that were modified, the added or removed `mutex_lock`/`unlock` calls, or perhaps changes to the locking hierarchy.
But one commit rarely exists in a vacuum. To truly grasp the significance and the *why* behind this fix, I need to see what led up to it and what it directly impacts. That's why I'm also going to pull up `git show f3dec61d7544`. This commit likely represents a previous state of the code, perhaps one where the deadlock *wasn't* present, or where a related issue was addressed. It might shed light on the evolution of the locking strategy in the `nbd` subsystem. Understanding the preceding change could reveal if the current fix is addressing a regression introduced by `f3dec61d7544`, or if it's a more fundamental re-architecting of the locking.
Then, to understand the immediate consequence or the next step in this particular chain of events, I'll examine `git show 1e1a9cecfab3`. This commit likely builds upon `8b428f42f3ed`. It could be that `8b428f42f3ed` is the initial fix, and `1e1a9cecfab3` is a subsequent refinement, a further optimization, or perhaps a patch that addresses a related but distinct issue that was uncovered as a consequence of the first fix. Seeing these three commits in sequence – the historical context, the current fix, and its immediate successor – will allow me to trace the problem, the solution, and the ongoing refinement of the code. It's a methodical approach, essential for understanding the nuances of kernel concurrency and for ensuring the robustness of such a critical subsystem.
</thought>
Called tool git-show with args: {"Commit":"8b428f42f3ed"}
Called tool git-show with args: {"Commit":"f3dec61d7544"}
Called tool git-show with args: {"Commit":"1e1a9cecfab3"}
[user]:
Tool git-show returned: {"Output":"commit 8b428f42f3edfd62422aa7ad87049ab232a2eaa9\nAuthor: Ming Lei \u003cming.lei@redhat.com\u003e\nDate: Wed Jul 9 19:17:44 2025 +0800\n\n nbd: fix lockdep deadlock warning\n \n nbd grabs device lock nbd-\u003econfig_lock for updating nr_hw_queues, this\n ways cause the following lock dependency:\n \n -\u003e #2 (\u0026disk-\u003eopen_mutex){+.+.}-{4:4}:\n lock_acquire kernel/locking/lockdep.c:5871 [inline]\n lock_acquire+0x1ac/0x448 kernel/locking/lockdep.c:5828\n __mutex_lock_common kernel/locking/mutex.c:602 [inline]\n __mutex_lock+0x166/0x1292 kernel/locking/mutex.c:747\n mutex_lock_nested+0x14/0x1c kernel/locking/mutex.c:799\n __del_gendisk+0x132/0xac6 block/genhd.c:706\n del_gendisk+0xf6/0x19a block/genhd.c:819\n nbd_dev_remove+0x3c/0xf2 drivers/block/nbd.c:268\n nbd_dev_remove_work+0x1c/0x26 drivers/block/nbd.c:284\n process_one_work+0x96a/0x1f32 kernel/workqueue.c:3238\n process_scheduled_works kernel/workqueue.c:3321 [inline]\n worker_thread+0x5ce/0xde8 kernel/workqueue.c:3402\n kthread+0x39c/0x7d4 kernel/kthread.c:464\n ret_from_fork_kernel+0x2a/0xbb2 arch/riscv/kernel/process.c:214\n ret_from_fork_kernel_asm+0x16/0x18 arch/riscv/kernel/entry.S:327\n \n -\u003e #1 (\u0026set-\u003eupdate_nr_hwq_lock){++++}-{4:4}:\n lock_acquire kernel/locking/lockdep.c:5871 [inline]\n lock_acquire+0x1ac/0x448 kernel/locking/lockdep.c:5828\n down_write+0x9c/0x19a kernel/locking/rwsem.c:1577\n blk_mq_update_nr_hw_queues+0x3e/0xb86 block/blk-mq.c:5041\n nbd_start_device+0x140/0xb2c drivers/block/nbd.c:1476\n nbd_genl_connect+0xae0/0x1b24 drivers/block/nbd.c:2201\n genl_family_rcv_msg_doit+0x206/0x2e6 net/netlink/genetlink.c:1115\n genl_family_rcv_msg net/netlink/genetlink.c:1195 [inline]\n genl_rcv_msg+0x514/0x78e net/netlink/genetlink.c:1210\n netlink_rcv_skb+0x206/0x3be net/netlink/af_netlink.c:2534\n genl_rcv+0x36/0x4c net/netlink/genetlink.c:1219\n netlink_unicast_kernel net/netlink/af_netlink.c:1313 [inline]\n netlink_unicast+0x4f0/0x82c net/netlink/af_netlink.c:1339\n netlink_sendmsg+0x85e/0xdd6 net/netlink/af_netlink.c:1883\n sock_sendmsg_nosec net/socket.c:712 [inline]\n __sock_sendmsg+0xcc/0x160 net/socket.c:727\n ____sys_sendmsg+0x63e/0x79c net/socket.c:2566\n ___sys_sendmsg+0x144/0x1e6 net/socket.c:2620\n __sys_sendmsg+0x188/0x246 net/socket.c:2652\n __do_sys_sendmsg net/socket.c:2657 [inline]\n __se_sys_sendmsg net/socket.c:2655 [inline]\n __riscv_sys_sendmsg+0x70/0xa2 net/socket.c:2655\n syscall_handler+0x94/0x118 arch/riscv/include/asm/syscall.h:112\n do_trap_ecall_u+0x396/0x530 arch/riscv/kernel/traps.c:341\n handle_exception+0x146/0x152 arch/riscv/kernel/entry.S:197\n \n -\u003e #0 (\u0026nbd-\u003econfig_lock){+.+.}-{4:4}:\n check_noncircular+0x132/0x146 kernel/locking/lockdep.c:2178\n check_prev_add kernel/locking/lockdep.c:3168 [inline]\n check_prevs_add kernel/locking/lockdep.c:3287 [inline]\n validate_chain kernel/locking/lockdep.c:3911 [inline]\n __lock_acquire+0x12b2/0x24ea kernel/locking/lockdep.c:5240\n lock_acquire kernel/locking/lockdep.c:5871 [inline]\n lock_acquire+0x1ac/0x448 kernel/locking/lockdep.c:5828\n __mutex_lock_common kernel/locking/mutex.c:602 [inline]\n __mutex_lock+0x166/0x1292 kernel/locking/mutex.c:747\n mutex_lock_nested+0x14/0x1c kernel/locking/mutex.c:799\n refcount_dec_and_mutex_lock+0x60/0xd8 lib/refcount.c:118\n nbd_config_put+0x3a/0x610 drivers/block/nbd.c:1423\n nbd_release+0x94/0x15c drivers/block/nbd.c:1735\n blkdev_put_whole+0xac/0xee block/bdev.c:721\n bdev_release+0x3fe/0x600 block/bdev.c:1144\n blkdev_release+0x1a/0x26 block/fops.c:684\n __fput+0x382/0xa8c fs/file_table.c:465\n ____fput+0x1c/0x26 fs/file_table.c:493\n task_work_run+0x16a/0x25e kernel/task_work.c:227\n resume_user_mode_work include/linux/resume_user_mode.h:50 [inline]\n exit_to_user_mode_loop+0x118/0x134 kernel/entry/common.c:114\n exit_to_user_mode_prepare include/linux/entry-common.h:330 [inline]\n syscall_exit_to_user_mode_work include/linux/entry-common.h:414 [inline]\n syscall_exit_to_user_mode include/linux/entry-common.h:449 [inline]\n do_trap_ecall_u+0x3f0/0x530 arch/riscv/kernel/traps.c:355\n handle_exception+0x146/0x152 arch/riscv/kernel/entry.S:197\n \n Also it isn't necessary to require nbd-\u003econfig_lock, because\n blk_mq_update_nr_hw_queues() does grab tagset lock for sync everything.\n \n Fixes the issue by releasing -\u003econfig_lock \u0026 retry in case of concurrent\n updating nr_hw_queues.\n \n Fixes: 98e68f67020c (\"block: prevent adding/deleting disk during updating nr_hw_queues\")\n Reported-by: syzbot+2bcecf3c38cb3e8fdc8d@syzkaller.appspotmail.com\n Closes: https://lore.kernel.org/all/6855034f.a00a0220.137b3.0031.GAE@google.com\n Reviewed-by: Yu Kuai \u003cyukuai3@huawei.com\u003e\n Cc: Nilay Shroff \u003cnilay@linux.ibm.com\u003e\n Signed-off-by: Ming Lei \u003cming.lei@redhat.com\u003e\n Reviewed-by: Nilay Shroff \u003cnilay@linux.ibm.com\u003e\n Link: https://lore.kernel.org/r/20250709111744.2353050-1-ming.lei@redhat.com\n Signed-off-by: Jens Axboe \u003caxboe@kernel.dk\u003e\n\ndiff --git a/drivers/block/nbd.c b/drivers/block/nbd.c\nindex 7bdc7eb808ea..709bad75c47b 100644\n--- a/drivers/block/nbd.c\n+++ b/drivers/block/nbd.c\n@@ -1473,7 +1473,17 @@ static int nbd_start_device(struct nbd_device *nbd)\n \t\treturn -EINVAL;\n \t}\n \n-\tblk_mq_update_nr_hw_queues(\u0026nbd-\u003etag_set, config-\u003enum_connections);\n+retry:\n+\tmutex_unlock(\u0026nbd-\u003econfig_lock);\n+\tblk_mq_update_nr_hw_queues(\u0026nbd-\u003etag_set, num_connections);\n+\tmutex_lock(\u0026nbd-\u003econfig_lock);\n+\n+\t/* if another code path updated nr_hw_queues, retry until succeed */\n+\tif (num_connections != config-\u003enum_connections) {\n+\t\tnum_connections = config-\u003enum_connections;\n+\t\tgoto retry;\n+\t}\n+\n \tnbd-\u003epid = task_pid_nr(current);\n \n \tnbd_parse_flags(nbd);\n"}
Tool git-show returned: {"Output":"commit f3dec61d7544a90685f1dd9a87fd4afc751996d0\nAuthor: Christoph Hellwig \u003chch@lst.de\u003e\nDate: Fri Jan 10 06:47:16 2025 +0100\n\n nbd: fix queue freeze vs limits lock order\n \n Match the locking order used by the core block code by only freezing\n the queue after taking the limits lock using the\n queue_limits_commit_update_frozen helper.\n \n This also allows removes the need for the separate __nbd_set_size helper,\n so remove it.\n \n Signed-off-by: Christoph Hellwig \u003chch@lst.de\u003e\n Reviewed-by: Ming Lei \u003cming.lei@redhat.com\u003e\n Reviewed-by: Damien Le Moal \u003cdlemoal@kernel.org\u003e\n Reviewed-by: Martin K. Petersen \u003cmartin.petersen@oracle.com\u003e\n Reviewed-by: Nilay Shroff \u003cnilay@linux.ibm.com\u003e\n Reviewed-by: Johannes Thumshirn \u003cjohannes.thumshirn@wdc.com\u003e\n Link: https://lore.kernel.org/r/20250110054726.1499538-9-hch@lst.de\n Signed-off-by: Jens Axboe \u003caxboe@kernel.dk\u003e\n\ndiff --git a/drivers/block/nbd.c b/drivers/block/nbd.c\nindex 259bd57fc529..efa05c3c06bf 100644\n--- a/drivers/block/nbd.c\n+++ b/drivers/block/nbd.c\n@@ -327,8 +327,7 @@ static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,\n \tnsock-\u003esent = 0;\n }\n \n-static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,\n-\t\tloff_t blksize)\n+static int nbd_set_size(struct nbd_device *nbd, loff_t bytesize, loff_t blksize)\n {\n \tstruct queue_limits lim;\n \tint error;\n@@ -368,7 +367,7 @@ static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,\n \n \tlim.logical_block_size = blksize;\n \tlim.physical_block_size = blksize;\n-\terror = queue_limits_commit_update(nbd-\u003edisk-\u003equeue, \u0026lim);\n+\terror = queue_limits_commit_update_frozen(nbd-\u003edisk-\u003equeue, \u0026lim);\n \tif (error)\n \t\treturn error;\n \n@@ -379,18 +378,6 @@ static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,\n \treturn 0;\n }\n \n-static int nbd_set_size(struct nbd_device *nbd, loff_t bytesize,\n-\t\tloff_t blksize)\n-{\n-\tint error;\n-\n-\tblk_mq_freeze_queue(nbd-\u003edisk-\u003equeue);\n-\terror = __nbd_set_size(nbd, bytesize, blksize);\n-\tblk_mq_unfreeze_queue(nbd-\u003edisk-\u003equeue);\n-\n-\treturn error;\n-}\n-\n static void nbd_complete_rq(struct request *req)\n {\n \tstruct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);\n"}
Tool git-show returned: {"Output":"commit 1e1a9cecfab3f22ebef0a976f849c87be8d03c1c\nAuthor: Christoph Hellwig \u003chch@lst.de\u003e\nDate: Fri Jan 31 13:03:47 2025 +0100\n\n block: force noio scope in blk_mq_freeze_queue\n \n When block drivers or the core block code perform allocations with a\n frozen queue, this could try to recurse into the block device to\n reclaim memory and deadlock. Thus all allocations done by a process\n that froze a queue need to be done without __GFP_IO and __GFP_FS.\n Instead of tying to track all of them down, force a noio scope as\n part of freezing the queue.\n \n Note that nvme is a bit of a mess here due to the non-owner freezes,\n and they will be addressed separately.\n \n Signed-off-by: Christoph Hellwig \u003chch@lst.de\u003e\n Link: https://lore.kernel.org/r/20250131120352.1315351-2-hch@lst.de\n Signed-off-by: Jens Axboe \u003caxboe@kernel.dk\u003e\n\ndiff --git a/block/blk-cgroup.c b/block/blk-cgroup.c\nindex 45a395862fbc..c795fa3a30e1 100644\n--- a/block/blk-cgroup.c\n+++ b/block/blk-cgroup.c\n@@ -1545,6 +1545,7 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)\n \tstruct request_queue *q = disk-\u003equeue;\n \tstruct blkg_policy_data *pd_prealloc = NULL;\n \tstruct blkcg_gq *blkg, *pinned_blkg = NULL;\n+\tunsigned int memflags;\n \tint ret;\n \n \tif (blkcg_policy_enabled(q, pol))\n@@ -1559,7 +1560,7 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)\n \t\treturn -EINVAL;\n \n \tif (queue_is_mq(q))\n-\t\tblk_mq_freeze_queue(q);\n+\t\tmemflags = blk_mq_freeze_queue(q);\n retry:\n \tspin_lock_irq(\u0026q-\u003equeue_lock);\n \n@@ -1623,7 +1624,7 @@ int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)\n \tspin_unlock_irq(\u0026q-\u003equeue_lock);\n out:\n \tif (queue_is_mq(q))\n-\t\tblk_mq_unfreeze_queue(q);\n+\t\tblk_mq_unfreeze_queue(q, memflags);\n \tif (pinned_blkg)\n \t\tblkg_put(pinned_blkg);\n \tif (pd_prealloc)\n@@ -1667,12 +1668,13 @@ void blkcg_deactivate_policy(struct gendisk *disk,\n {\n \tstruct request_queue *q = disk-\u003equeue;\n \tstruct blkcg_gq *blkg;\n+\tunsigned int memflags;\n \n \tif (!blkcg_policy_enabled(q, pol))\n \t\treturn;\n \n \tif (queue_is_mq(q))\n-\t\tblk_mq_freeze_queue(q);\n+\t\tmemflags = blk_mq_freeze_queue(q);\n \n \tmutex_lock(\u0026q-\u003eblkcg_mutex);\n \tspin_lock_irq(\u0026q-\u003equeue_lock);\n@@ -1696,7 +1698,7 @@ void blkcg_deactivate_policy(struct gendisk *disk,\n \tmutex_unlock(\u0026q-\u003eblkcg_mutex);\n \n \tif (queue_is_mq(q))\n-\t\tblk_mq_unfreeze_queue(q);\n+\t\tblk_mq_unfreeze_queue(q, memflags);\n }\n EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);\n \ndiff --git a/block/blk-iocost.c b/block/blk-iocost.c\nindex a5894ec9696e..65a1d4427ccf 100644\n--- a/block/blk-iocost.c\n+++ b/block/blk-iocost.c\n@@ -3224,6 +3224,7 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,\n \tu32 qos[NR_QOS_PARAMS];\n \tbool enable, user;\n \tchar *body, *p;\n+\tunsigned int memflags;\n \tint ret;\n \n \tblkg_conf_init(\u0026ctx, input);\n@@ -3247,7 +3248,7 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,\n \t\tioc = q_to_ioc(disk-\u003equeue);\n \t}\n \n-\tblk_mq_freeze_queue(disk-\u003equeue);\n+\tmemflags = blk_mq_freeze_queue(disk-\u003equeue);\n \tblk_mq_quiesce_queue(disk-\u003equeue);\n \n \tspin_lock_irq(\u0026ioc-\u003elock);\n@@ -3347,7 +3348,7 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,\n \t\twbt_enable_default(disk);\n \n \tblk_mq_unquiesce_queue(disk-\u003equeue);\n-\tblk_mq_unfreeze_queue(disk-\u003equeue);\n+\tblk_mq_unfreeze_queue(disk-\u003equeue, memflags);\n \n \tblkg_conf_exit(\u0026ctx);\n \treturn nbytes;\n@@ -3355,7 +3356,7 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,\n \tspin_unlock_irq(\u0026ioc-\u003elock);\n \n \tblk_mq_unquiesce_queue(disk-\u003equeue);\n-\tblk_mq_unfreeze_queue(disk-\u003equeue);\n+\tblk_mq_unfreeze_queue(disk-\u003equeue, memflags);\n \n \tret = -EINVAL;\n err:\n@@ -3414,6 +3415,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,\n {\n \tstruct blkg_conf_ctx ctx;\n \tstruct request_queue *q;\n+\tunsigned int memflags;\n \tstruct ioc *ioc;\n \tu64 u[NR_I_LCOEFS];\n \tbool user;\n@@ -3441,7 +3443,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,\n \t\tioc = q_to_ioc(q);\n \t}\n \n-\tblk_mq_freeze_queue(q);\n+\tmemflags = blk_mq_freeze_queue(q);\n \tblk_mq_quiesce_queue(q);\n \n \tspin_lock_irq(\u0026ioc-\u003elock);\n@@ -3493,7 +3495,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,\n \tspin_unlock_irq(\u0026ioc-\u003elock);\n \n \tblk_mq_unquiesce_queue(q);\n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n \n \tblkg_conf_exit(\u0026ctx);\n \treturn nbytes;\n@@ -3502,7 +3504,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,\n \tspin_unlock_irq(\u0026ioc-\u003elock);\n \n \tblk_mq_unquiesce_queue(q);\n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n \n \tret = -EINVAL;\n err:\ndiff --git a/block/blk-iolatency.c b/block/blk-iolatency.c\nindex ebb522788d97..42c1e0b9a68f 100644\n--- a/block/blk-iolatency.c\n+++ b/block/blk-iolatency.c\n@@ -749,9 +749,11 @@ static void blkiolatency_enable_work_fn(struct work_struct *work)\n \t */\n \tenabled = atomic_read(\u0026blkiolat-\u003eenable_cnt);\n \tif (enabled != blkiolat-\u003eenabled) {\n-\t\tblk_mq_freeze_queue(blkiolat-\u003erqos.disk-\u003equeue);\n+\t\tunsigned int memflags;\n+\n+\t\tmemflags = blk_mq_freeze_queue(blkiolat-\u003erqos.disk-\u003equeue);\n \t\tblkiolat-\u003eenabled = enabled;\n-\t\tblk_mq_unfreeze_queue(blkiolat-\u003erqos.disk-\u003equeue);\n+\t\tblk_mq_unfreeze_queue(blkiolat-\u003erqos.disk-\u003equeue, memflags);\n \t}\n }\n \ndiff --git a/block/blk-mq.c b/block/blk-mq.c\nindex da39a1cac702..40490ac88045 100644\n--- a/block/blk-mq.c\n+++ b/block/blk-mq.c\n@@ -210,12 +210,12 @@ int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,\n }\n EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);\n \n-void blk_mq_freeze_queue(struct request_queue *q)\n+void blk_mq_freeze_queue_nomemsave(struct request_queue *q)\n {\n \tblk_freeze_queue_start(q);\n \tblk_mq_freeze_queue_wait(q);\n }\n-EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);\n+EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_nomemsave);\n \n bool __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)\n {\n@@ -236,12 +236,12 @@ bool __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)\n \treturn unfreeze;\n }\n \n-void blk_mq_unfreeze_queue(struct request_queue *q)\n+void blk_mq_unfreeze_queue_nomemrestore(struct request_queue *q)\n {\n \tif (__blk_mq_unfreeze_queue(q, false))\n \t\tblk_unfreeze_release_lock(q);\n }\n-EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);\n+EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue_nomemrestore);\n \n /*\n * non_owner variant of blk_freeze_queue_start\n@@ -4223,13 +4223,14 @@ static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set,\n \t\t\t\t\t bool shared)\n {\n \tstruct request_queue *q;\n+\tunsigned int memflags;\n \n \tlockdep_assert_held(\u0026set-\u003etag_list_lock);\n \n \tlist_for_each_entry(q, \u0026set-\u003etag_list, tag_set_list) {\n-\t\tblk_mq_freeze_queue(q);\n+\t\tmemflags = blk_mq_freeze_queue(q);\n \t\tqueue_set_hctx_shared(q, shared);\n-\t\tblk_mq_unfreeze_queue(q);\n+\t\tblk_mq_unfreeze_queue(q, memflags);\n \t}\n }\n \n@@ -4992,6 +4993,7 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,\n \tstruct request_queue *q;\n \tLIST_HEAD(head);\n \tint prev_nr_hw_queues = set-\u003enr_hw_queues;\n+\tunsigned int memflags;\n \tint i;\n \n \tlockdep_assert_held(\u0026set-\u003etag_list_lock);\n@@ -5003,8 +5005,10 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,\n \tif (set-\u003enr_maps == 1 \u0026\u0026 nr_hw_queues == set-\u003enr_hw_queues)\n \t\treturn;\n \n+\tmemflags = memalloc_noio_save();\n \tlist_for_each_entry(q, \u0026set-\u003etag_list, tag_set_list)\n-\t\tblk_mq_freeze_queue(q);\n+\t\tblk_mq_freeze_queue_nomemsave(q);\n+\n \t/*\n \t * Switch IO scheduler to 'none', cleaning up the data associated\n \t * with the previous scheduler. We will switch back once we are done\n@@ -5052,7 +5056,8 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,\n \t\tblk_mq_elv_switch_back(\u0026head, q);\n \n \tlist_for_each_entry(q, \u0026set-\u003etag_list, tag_set_list)\n-\t\tblk_mq_unfreeze_queue(q);\n+\t\tblk_mq_unfreeze_queue_nomemrestore(q);\n+\tmemalloc_noio_restore(memflags);\n \n \t/* Free the excess tags when nr_hw_queues shrink. */\n \tfor (i = set-\u003enr_hw_queues; i \u003c prev_nr_hw_queues; i++)\ndiff --git a/block/blk-pm.c b/block/blk-pm.c\nindex 42e842074715..8d3e052f91da 100644\n--- a/block/blk-pm.c\n+++ b/block/blk-pm.c\n@@ -89,7 +89,7 @@ int blk_pre_runtime_suspend(struct request_queue *q)\n \tif (percpu_ref_is_zero(\u0026q-\u003eq_usage_counter))\n \t\tret = 0;\n \t/* Switch q_usage_counter back to per-cpu mode. */\n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue_nomemrestore(q);\n \n \tif (ret \u003c 0) {\n \t\tspin_lock_irq(\u0026q-\u003equeue_lock);\ndiff --git a/block/blk-rq-qos.c b/block/blk-rq-qos.c\nindex eb9618cd68ad..d4d4f4dc0e23 100644\n--- a/block/blk-rq-qos.c\n+++ b/block/blk-rq-qos.c\n@@ -299,6 +299,7 @@ int rq_qos_add(struct rq_qos *rqos, struct gendisk *disk, enum rq_qos_id id,\n \t\tconst struct rq_qos_ops *ops)\n {\n \tstruct request_queue *q = disk-\u003equeue;\n+\tunsigned int memflags;\n \n \tlockdep_assert_held(\u0026q-\u003erq_qos_mutex);\n \n@@ -310,14 +311,14 @@ int rq_qos_add(struct rq_qos *rqos, struct gendisk *disk, enum rq_qos_id id,\n \t * No IO can be in-flight when adding rqos, so freeze queue, which\n \t * is fine since we only support rq_qos for blk-mq queue.\n \t */\n-\tblk_mq_freeze_queue(q);\n+\tmemflags = blk_mq_freeze_queue(q);\n \n \tif (rq_qos_id(q, rqos-\u003eid))\n \t\tgoto ebusy;\n \trqos-\u003enext = q-\u003erq_qos;\n \tq-\u003erq_qos = rqos;\n \n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n \n \tif (rqos-\u003eops-\u003edebugfs_attrs) {\n \t\tmutex_lock(\u0026q-\u003edebugfs_mutex);\n@@ -327,7 +328,7 @@ int rq_qos_add(struct rq_qos *rqos, struct gendisk *disk, enum rq_qos_id id,\n \n \treturn 0;\n ebusy:\n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n \treturn -EBUSY;\n }\n \n@@ -335,17 +336,18 @@ void rq_qos_del(struct rq_qos *rqos)\n {\n \tstruct request_queue *q = rqos-\u003edisk-\u003equeue;\n \tstruct rq_qos **cur;\n+\tunsigned int memflags;\n \n \tlockdep_assert_held(\u0026q-\u003erq_qos_mutex);\n \n-\tblk_mq_freeze_queue(q);\n+\tmemflags = blk_mq_freeze_queue(q);\n \tfor (cur = \u0026q-\u003erq_qos; *cur; cur = \u0026(*cur)-\u003enext) {\n \t\tif (*cur == rqos) {\n \t\t\t*cur = rqos-\u003enext;\n \t\t\tbreak;\n \t\t}\n \t}\n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n \n \tmutex_lock(\u0026q-\u003edebugfs_mutex);\n \tblk_mq_debugfs_unregister_rqos(rqos);\ndiff --git a/block/blk-settings.c b/block/blk-settings.c\nindex db12396ff5c7..c44dadc35e1e 100644\n--- a/block/blk-settings.c\n+++ b/block/blk-settings.c\n@@ -461,11 +461,12 @@ EXPORT_SYMBOL_GPL(queue_limits_commit_update);\n int queue_limits_commit_update_frozen(struct request_queue *q,\n \t\tstruct queue_limits *lim)\n {\n+\tunsigned int memflags;\n \tint ret;\n \n-\tblk_mq_freeze_queue(q);\n+\tmemflags = blk_mq_freeze_queue(q);\n \tret = queue_limits_commit_update(q, lim);\n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n \n \treturn ret;\n }\ndiff --git a/block/blk-sysfs.c b/block/blk-sysfs.c\nindex 7b970e6765e7..6f548a4376aa 100644\n--- a/block/blk-sysfs.c\n+++ b/block/blk-sysfs.c\n@@ -681,7 +681,7 @@ queue_attr_store(struct kobject *kobj, struct attribute *attr,\n \tstruct queue_sysfs_entry *entry = to_queue(attr);\n \tstruct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj);\n \tstruct request_queue *q = disk-\u003equeue;\n-\tunsigned int noio_flag;\n+\tunsigned int memflags;\n \tssize_t res;\n \n \tif (!entry-\u003estore_limit \u0026\u0026 !entry-\u003estore)\n@@ -711,11 +711,9 @@ queue_attr_store(struct kobject *kobj, struct attribute *attr,\n \t}\n \n \tmutex_lock(\u0026q-\u003esysfs_lock);\n-\tblk_mq_freeze_queue(q);\n-\tnoio_flag = memalloc_noio_save();\n+\tmemflags = blk_mq_freeze_queue(q);\n \tres = entry-\u003estore(disk, page, length);\n-\tmemalloc_noio_restore(noio_flag);\n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n \tmutex_unlock(\u0026q-\u003esysfs_lock);\n \treturn res;\n }\ndiff --git a/block/blk-throttle.c b/block/blk-throttle.c\nindex 82dbaefcfa3b..8d149aff9fd0 100644\n--- a/block/blk-throttle.c\n+++ b/block/blk-throttle.c\n@@ -1202,6 +1202,7 @@ static int blk_throtl_init(struct gendisk *disk)\n {\n \tstruct request_queue *q = disk-\u003equeue;\n \tstruct throtl_data *td;\n+\tunsigned int memflags;\n \tint ret;\n \n \ttd = kzalloc_node(sizeof(*td), GFP_KERNEL, q-\u003enode);\n@@ -1215,7 +1216,7 @@ static int blk_throtl_init(struct gendisk *disk)\n \t * Freeze queue before activating policy, to synchronize with IO path,\n \t * which is protected by 'q_usage_counter'.\n \t */\n-\tblk_mq_freeze_queue(disk-\u003equeue);\n+\tmemflags = blk_mq_freeze_queue(disk-\u003equeue);\n \tblk_mq_quiesce_queue(disk-\u003equeue);\n \n \tq-\u003etd = td;\n@@ -1239,7 +1240,7 @@ static int blk_throtl_init(struct gendisk *disk)\n \n out:\n \tblk_mq_unquiesce_queue(disk-\u003equeue);\n-\tblk_mq_unfreeze_queue(disk-\u003equeue);\n+\tblk_mq_unfreeze_queue(disk-\u003equeue, memflags);\n \n \treturn ret;\n }\ndiff --git a/block/blk-zoned.c b/block/blk-zoned.c\nindex 9d08a54c201e..761ea662ddc3 100644\n--- a/block/blk-zoned.c\n+++ b/block/blk-zoned.c\n@@ -1717,9 +1717,10 @@ int blk_revalidate_disk_zones(struct gendisk *disk)\n \telse\n \t\tpr_warn(\"%s: failed to revalidate zones\\n\", disk-\u003edisk_name);\n \tif (ret) {\n-\t\tblk_mq_freeze_queue(q);\n+\t\tunsigned int memflags = blk_mq_freeze_queue(q);\n+\n \t\tdisk_free_zone_resources(disk);\n-\t\tblk_mq_unfreeze_queue(q);\n+\t\tblk_mq_unfreeze_queue(q, memflags);\n \t}\n \n \treturn ret;\ndiff --git a/block/elevator.c b/block/elevator.c\nindex b81216c48b6b..cd2ce4921601 100644\n--- a/block/elevator.c\n+++ b/block/elevator.c\n@@ -570,6 +570,7 @@ static struct elevator_type *elevator_get_default(struct request_queue *q)\n void elevator_init_mq(struct request_queue *q)\n {\n \tstruct elevator_type *e;\n+\tunsigned int memflags;\n \tint err;\n \n \tWARN_ON_ONCE(blk_queue_registered(q));\n@@ -590,13 +591,13 @@ void elevator_init_mq(struct request_queue *q)\n \t *\n \t * Disk isn't added yet, so verifying queue lock only manually.\n \t */\n-\tblk_mq_freeze_queue(q);\n+\tmemflags = blk_mq_freeze_queue(q);\n \n \tblk_mq_cancel_work_sync(q);\n \n \terr = blk_mq_init_sched(q, e);\n \n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n \n \tif (err) {\n \t\tpr_warn(\"\\\"%s\\\" elevator initialization failed, \"\n@@ -614,11 +615,12 @@ void elevator_init_mq(struct request_queue *q)\n */\n int elevator_switch(struct request_queue *q, struct elevator_type *new_e)\n {\n+\tunsigned int memflags;\n \tint ret;\n \n \tlockdep_assert_held(\u0026q-\u003esysfs_lock);\n \n-\tblk_mq_freeze_queue(q);\n+\tmemflags = blk_mq_freeze_queue(q);\n \tblk_mq_quiesce_queue(q);\n \n \tif (q-\u003eelevator) {\n@@ -639,7 +641,7 @@ int elevator_switch(struct request_queue *q, struct elevator_type *new_e)\n \n out_unfreeze:\n \tblk_mq_unquiesce_queue(q);\n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n \n \tif (ret) {\n \t\tpr_warn(\"elv: switch to \\\"%s\\\" failed, falling back to \\\"none\\\"\\n\",\n@@ -651,9 +653,11 @@ int elevator_switch(struct request_queue *q, struct elevator_type *new_e)\n \n void elevator_disable(struct request_queue *q)\n {\n+\tunsigned int memflags;\n+\n \tlockdep_assert_held(\u0026q-\u003esysfs_lock);\n \n-\tblk_mq_freeze_queue(q);\n+\tmemflags = blk_mq_freeze_queue(q);\n \tblk_mq_quiesce_queue(q);\n \n \telv_unregister_queue(q);\n@@ -664,7 +668,7 @@ void elevator_disable(struct request_queue *q)\n \tblk_add_trace_msg(q, \"elv switch: none\");\n \n \tblk_mq_unquiesce_queue(q);\n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n }\n \n /*\ndiff --git a/drivers/block/aoe/aoedev.c b/drivers/block/aoe/aoedev.c\nindex 3523dd82d7a0..4db7f6ce8ade 100644\n--- a/drivers/block/aoe/aoedev.c\n+++ b/drivers/block/aoe/aoedev.c\n@@ -226,10 +226,11 @@ aoedev_downdev(struct aoedev *d)\n \t/* fast fail all pending I/O */\n \tif (d-\u003eblkq) {\n \t\t/* UP is cleared, freeze+quiesce to insure all are errored */\n-\t\tblk_mq_freeze_queue(d-\u003eblkq);\n+\t\tunsigned int memflags = blk_mq_freeze_queue(d-\u003eblkq);\n+\n \t\tblk_mq_quiesce_queue(d-\u003eblkq);\n \t\tblk_mq_unquiesce_queue(d-\u003eblkq);\n-\t\tblk_mq_unfreeze_queue(d-\u003eblkq);\n+\t\tblk_mq_unfreeze_queue(d-\u003eblkq, memflags);\n \t}\n \n \tif (d-\u003egd)\ndiff --git a/drivers/block/ataflop.c b/drivers/block/ataflop.c\nindex 110f9aca2667..a81ade622a01 100644\n--- a/drivers/block/ataflop.c\n+++ b/drivers/block/ataflop.c\n@@ -746,6 +746,7 @@ static int do_format(int drive, int type, struct atari_format_descr *desc)\n \tunsigned char\t*p;\n \tint sect, nsect;\n \tunsigned long\tflags;\n+\tunsigned int memflags;\n \tint ret;\n \n \tif (type) {\n@@ -758,7 +759,7 @@ static int do_format(int drive, int type, struct atari_format_descr *desc)\n \t}\n \n \tq = unit[drive].disk[type]-\u003equeue;\n-\tblk_mq_freeze_queue(q);\n+\tmemflags = blk_mq_freeze_queue(q);\n \tblk_mq_quiesce_queue(q);\n \n \tlocal_irq_save(flags);\n@@ -817,7 +818,7 @@ static int do_format(int drive, int type, struct atari_format_descr *desc)\n \tret = FormatError ? -EIO : 0;\n out:\n \tblk_mq_unquiesce_queue(q);\n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n \treturn ret;\n }\n \ndiff --git a/drivers/block/loop.c b/drivers/block/loop.c\nindex d1f1d6bef2e6..c05fe27a96b6 100644\n--- a/drivers/block/loop.c\n+++ b/drivers/block/loop.c\n@@ -586,6 +586,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,\n {\n \tstruct file *file = fget(arg);\n \tstruct file *old_file;\n+\tunsigned int memflags;\n \tint error;\n \tbool partscan;\n \tbool is_loop;\n@@ -623,14 +624,14 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,\n \n \t/* and ... switch */\n \tdisk_force_media_change(lo-\u003elo_disk);\n-\tblk_mq_freeze_queue(lo-\u003elo_queue);\n+\tmemflags = blk_mq_freeze_queue(lo-\u003elo_queue);\n \tmapping_set_gfp_mask(old_file-\u003ef_mapping, lo-\u003eold_gfp_mask);\n \tlo-\u003elo_backing_file = file;\n \tlo-\u003eold_gfp_mask = mapping_gfp_mask(file-\u003ef_mapping);\n \tmapping_set_gfp_mask(file-\u003ef_mapping,\n \t\t\t lo-\u003eold_gfp_mask \u0026 ~(__GFP_IO|__GFP_FS));\n \tloop_update_dio(lo);\n-\tblk_mq_unfreeze_queue(lo-\u003elo_queue);\n+\tblk_mq_unfreeze_queue(lo-\u003elo_queue, memflags);\n \tpartscan = lo-\u003elo_flags \u0026 LO_FLAGS_PARTSCAN;\n \tloop_global_unlock(lo, is_loop);\n \n@@ -1255,6 +1256,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)\n \tint err;\n \tbool partscan = false;\n \tbool size_changed = false;\n+\tunsigned int memflags;\n \n \terr = mutex_lock_killable(\u0026lo-\u003elo_mutex);\n \tif (err)\n@@ -1272,7 +1274,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)\n \t}\n \n \t/* I/O needs to be drained before changing lo_offset or lo_sizelimit */\n-\tblk_mq_freeze_queue(lo-\u003elo_queue);\n+\tmemflags = blk_mq_freeze_queue(lo-\u003elo_queue);\n \n \terr = loop_set_status_from_info(lo, info);\n \tif (err)\n@@ -1294,7 +1296,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)\n \tloop_update_dio(lo);\n \n out_unfreeze:\n-\tblk_mq_unfreeze_queue(lo-\u003elo_queue);\n+\tblk_mq_unfreeze_queue(lo-\u003elo_queue, memflags);\n \tif (partscan)\n \t\tclear_bit(GD_SUPPRESS_PART_SCAN, \u0026lo-\u003elo_disk-\u003estate);\n out_unlock:\n@@ -1446,6 +1448,7 @@ static int loop_set_capacity(struct loop_device *lo)\n static int loop_set_dio(struct loop_device *lo, unsigned long arg)\n {\n \tbool use_dio = !!arg;\n+\tunsigned int memflags;\n \n \tif (lo-\u003elo_state != Lo_bound)\n \t\treturn -ENXIO;\n@@ -1459,18 +1462,19 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)\n \t\tvfs_fsync(lo-\u003elo_backing_file, 0);\n \t}\n \n-\tblk_mq_freeze_queue(lo-\u003elo_queue);\n+\tmemflags = blk_mq_freeze_queue(lo-\u003elo_queue);\n \tif (use_dio)\n \t\tlo-\u003elo_flags |= LO_FLAGS_DIRECT_IO;\n \telse\n \t\tlo-\u003elo_flags \u0026= ~LO_FLAGS_DIRECT_IO;\n-\tblk_mq_unfreeze_queue(lo-\u003elo_queue);\n+\tblk_mq_unfreeze_queue(lo-\u003elo_queue, memflags);\n \treturn 0;\n }\n \n static int loop_set_block_size(struct loop_device *lo, unsigned long arg)\n {\n \tstruct queue_limits lim;\n+\tunsigned int memflags;\n \tint err = 0;\n \n \tif (lo-\u003elo_state != Lo_bound)\n@@ -1485,10 +1489,10 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg)\n \tlim = queue_limits_start_update(lo-\u003elo_queue);\n \tloop_update_limits(lo, \u0026lim, arg);\n \n-\tblk_mq_freeze_queue(lo-\u003elo_queue);\n+\tmemflags = blk_mq_freeze_queue(lo-\u003elo_queue);\n \terr = queue_limits_commit_update(lo-\u003elo_queue, \u0026lim);\n \tloop_update_dio(lo);\n-\tblk_mq_unfreeze_queue(lo-\u003elo_queue);\n+\tblk_mq_unfreeze_queue(lo-\u003elo_queue, memflags);\n \n \treturn err;\n }\ndiff --git a/drivers/block/nbd.c b/drivers/block/nbd.c\nindex b63a0f29a54a..7bdc7eb808ea 100644\n--- a/drivers/block/nbd.c\n+++ b/drivers/block/nbd.c\n@@ -1234,6 +1234,7 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,\n \tstruct socket *sock;\n \tstruct nbd_sock **socks;\n \tstruct nbd_sock *nsock;\n+\tunsigned int memflags;\n \tint err;\n \n \t/* Arg will be cast to int, check it to avoid overflow */\n@@ -1247,7 +1248,7 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,\n \t * We need to make sure we don't get any errant requests while we're\n \t * reallocating the -\u003esocks array.\n \t */\n-\tblk_mq_freeze_queue(nbd-\u003edisk-\u003equeue);\n+\tmemflags = blk_mq_freeze_queue(nbd-\u003edisk-\u003equeue);\n \n \tif (!netlink \u0026\u0026 !nbd-\u003etask_setup \u0026\u0026\n \t !test_bit(NBD_RT_BOUND, \u0026config-\u003eruntime_flags))\n@@ -1288,12 +1289,12 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,\n \tINIT_WORK(\u0026nsock-\u003ework, nbd_pending_cmd_work);\n \tsocks[config-\u003enum_connections++] = nsock;\n \tatomic_inc(\u0026config-\u003elive_connections);\n-\tblk_mq_unfreeze_queue(nbd-\u003edisk-\u003equeue);\n+\tblk_mq_unfreeze_queue(nbd-\u003edisk-\u003equeue, memflags);\n \n \treturn 0;\n \n put_socket:\n-\tblk_mq_unfreeze_queue(nbd-\u003edisk-\u003equeue);\n+\tblk_mq_unfreeze_queue(nbd-\u003edisk-\u003equeue, memflags);\n \tsockfd_put(sock);\n \treturn err;\n }\ndiff --git a/drivers/block/rbd.c b/drivers/block/rbd.c\nindex 5b393e4a1ddf..faafd7ff43d6 100644\n--- a/drivers/block/rbd.c\n+++ b/drivers/block/rbd.c\n@@ -7281,9 +7281,10 @@ static ssize_t do_rbd_remove(const char *buf, size_t count)\n \t\t * Prevent new IO from being queued and wait for existing\n \t\t * IO to complete/fail.\n \t\t */\n-\t\tblk_mq_freeze_queue(rbd_dev-\u003edisk-\u003equeue);\n+\t\tunsigned int memflags = blk_mq_freeze_queue(rbd_dev-\u003edisk-\u003equeue);\n+\n \t\tblk_mark_disk_dead(rbd_dev-\u003edisk);\n-\t\tblk_mq_unfreeze_queue(rbd_dev-\u003edisk-\u003equeue);\n+\t\tblk_mq_unfreeze_queue(rbd_dev-\u003edisk-\u003equeue, memflags);\n \t}\n \n \tdel_gendisk(rbd_dev-\u003edisk);\ndiff --git a/drivers/block/sunvdc.c b/drivers/block/sunvdc.c\nindex 88dcae6ec575..05c4aee7f262 100644\n--- a/drivers/block/sunvdc.c\n+++ b/drivers/block/sunvdc.c\n@@ -1113,6 +1113,7 @@ static void vdc_requeue_inflight(struct vdc_port *port)\n static void vdc_queue_drain(struct vdc_port *port)\n {\n \tstruct request_queue *q = port-\u003edisk-\u003equeue;\n+\tunsigned int memflags;\n \n \t/*\n \t * Mark the queue as draining, then freeze/quiesce to ensure\n@@ -1121,12 +1122,12 @@ static void vdc_queue_drain(struct vdc_port *port)\n \tport-\u003edrain = 1;\n \tspin_unlock_irq(\u0026port-\u003evio.lock);\n \n-\tblk_mq_freeze_queue(q);\n+\tmemflags = blk_mq_freeze_queue(q);\n \tblk_mq_quiesce_queue(q);\n \n \tspin_lock_irq(\u0026port-\u003evio.lock);\n \tport-\u003edrain = 0;\n-\tblk_mq_unquiesce_queue(q);\n+\tblk_mq_unquiesce_queue(q, memflags);\n \tblk_mq_unfreeze_queue(q);\n }\n \ndiff --git a/drivers/block/swim3.c b/drivers/block/swim3.c\nindex 9914153b365b..3aedcb5add61 100644\n--- a/drivers/block/swim3.c\n+++ b/drivers/block/swim3.c\n@@ -840,6 +840,7 @@ static int grab_drive(struct floppy_state *fs, enum swim_state state,\n static void release_drive(struct floppy_state *fs)\n {\n \tstruct request_queue *q = disks[fs-\u003eindex]-\u003equeue;\n+\tunsigned int memflags;\n \tunsigned long flags;\n \n \tswim3_dbg(\"%s\", \"-\u003e release drive\\n\");\n@@ -848,10 +849,10 @@ static void release_drive(struct floppy_state *fs)\n \tfs-\u003estate = idle;\n \tspin_unlock_irqrestore(\u0026swim3_lock, flags);\n \n-\tblk_mq_freeze_queue(q);\n+\tmemflags = blk_mq_freeze_queue(q);\n \tblk_mq_quiesce_queue(q);\n \tblk_mq_unquiesce_queue(q);\n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n }\n \n static int fd_eject(struct floppy_state *fs)\ndiff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c\nindex bbaa26b523b8..a4af39fc7ea2 100644\n--- a/drivers/block/virtio_blk.c\n+++ b/drivers/block/virtio_blk.c\n@@ -1584,11 +1584,12 @@ static int virtblk_freeze(struct virtio_device *vdev)\n {\n \tstruct virtio_blk *vblk = vdev-\u003epriv;\n \tstruct request_queue *q = vblk-\u003edisk-\u003equeue;\n+\tunsigned int memflags;\n \n \t/* Ensure no requests in virtqueues before deleting vqs. */\n-\tblk_mq_freeze_queue(q);\n+\tmemflags = blk_mq_freeze_queue(q);\n \tblk_mq_quiesce_queue_nowait(q);\n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n \n \t/* Ensure we don't receive any more interrupts */\n \tvirtio_reset_device(vdev);\ndiff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c\nindex ee7e1d908986..847c11542f02 100644\n--- a/drivers/mtd/mtd_blkdevs.c\n+++ b/drivers/mtd/mtd_blkdevs.c\n@@ -404,6 +404,7 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)\n int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)\n {\n \tunsigned long flags;\n+\tunsigned int memflags;\n \n \tlockdep_assert_held(\u0026mtd_table_mutex);\n \n@@ -420,10 +421,10 @@ int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)\n \tspin_unlock_irqrestore(\u0026old-\u003equeue_lock, flags);\n \n \t/* freeze+quiesce queue to ensure all requests are flushed */\n-\tblk_mq_freeze_queue(old-\u003erq);\n+\tmemflags = blk_mq_freeze_queue(old-\u003erq);\n \tblk_mq_quiesce_queue(old-\u003erq);\n \tblk_mq_unquiesce_queue(old-\u003erq);\n-\tblk_mq_unfreeze_queue(old-\u003erq);\n+\tblk_mq_unfreeze_queue(old-\u003erq, memflags);\n \n \t/* If the device is currently open, tell trans driver to close it,\n \t\tthen put mtd device, and don't touch it again */\ndiff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c\nindex 76b615d4d5b9..40046770f1bf 100644\n--- a/drivers/nvme/host/core.c\n+++ b/drivers/nvme/host/core.c\n@@ -2132,15 +2132,16 @@ static int nvme_update_ns_info_generic(struct nvme_ns *ns,\n \t\tstruct nvme_ns_info *info)\n {\n \tstruct queue_limits lim;\n+\tunsigned int memflags;\n \tint ret;\n \n \tlim = queue_limits_start_update(ns-\u003edisk-\u003equeue);\n \tnvme_set_ctrl_limits(ns-\u003ectrl, \u0026lim);\n \n-\tblk_mq_freeze_queue(ns-\u003edisk-\u003equeue);\n+\tmemflags = blk_mq_freeze_queue(ns-\u003edisk-\u003equeue);\n \tret = queue_limits_commit_update(ns-\u003edisk-\u003equeue, \u0026lim);\n \tset_disk_ro(ns-\u003edisk, nvme_ns_is_readonly(ns, info));\n-\tblk_mq_unfreeze_queue(ns-\u003edisk-\u003equeue);\n+\tblk_mq_unfreeze_queue(ns-\u003edisk-\u003equeue, memflags);\n \n \t/* Hide the block-interface for these devices */\n \tif (!ret)\n@@ -2155,6 +2156,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,\n \tstruct nvme_id_ns_nvm *nvm = NULL;\n \tstruct nvme_zone_info zi = {};\n \tstruct nvme_id_ns *id;\n+\tunsigned int memflags;\n \tsector_t capacity;\n \tunsigned lbaf;\n \tint ret;\n@@ -2186,7 +2188,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,\n \n \tlim = queue_limits_start_update(ns-\u003edisk-\u003equeue);\n \n-\tblk_mq_freeze_queue(ns-\u003edisk-\u003equeue);\n+\tmemflags = blk_mq_freeze_queue(ns-\u003edisk-\u003equeue);\n \tns-\u003ehead-\u003elba_shift = id-\u003elbaf[lbaf].ds;\n \tns-\u003ehead-\u003enuse = le64_to_cpu(id-\u003enuse);\n \tcapacity = nvme_lba_to_sect(ns-\u003ehead, le64_to_cpu(id-\u003ensze));\n@@ -2219,7 +2221,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,\n \n \tret = queue_limits_commit_update(ns-\u003edisk-\u003equeue, \u0026lim);\n \tif (ret) {\n-\t\tblk_mq_unfreeze_queue(ns-\u003edisk-\u003equeue);\n+\t\tblk_mq_unfreeze_queue(ns-\u003edisk-\u003equeue, memflags);\n \t\tgoto out;\n \t}\n \n@@ -2235,7 +2237,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,\n \t\tns-\u003ehead-\u003efeatures |= NVME_NS_DEAC;\n \tset_disk_ro(ns-\u003edisk, nvme_ns_is_readonly(ns, info));\n \tset_bit(NVME_NS_READY, \u0026ns-\u003eflags);\n-\tblk_mq_unfreeze_queue(ns-\u003edisk-\u003equeue);\n+\tblk_mq_unfreeze_queue(ns-\u003edisk-\u003equeue, memflags);\n \n \tif (blk_queue_is_zoned(ns-\u003equeue)) {\n \t\tret = blk_revalidate_disk_zones(ns-\u003edisk);\n@@ -2291,9 +2293,10 @@ static int nvme_update_ns_info(struct nvme_ns *ns, struct nvme_ns_info *info)\n \tif (!ret \u0026\u0026 nvme_ns_head_multipath(ns-\u003ehead)) {\n \t\tstruct queue_limits *ns_lim = \u0026ns-\u003edisk-\u003equeue-\u003elimits;\n \t\tstruct queue_limits lim;\n+\t\tunsigned int memflags;\n \n \t\tlim = queue_limits_start_update(ns-\u003ehead-\u003edisk-\u003equeue);\n-\t\tblk_mq_freeze_queue(ns-\u003ehead-\u003edisk-\u003equeue);\n+\t\tmemflags = blk_mq_freeze_queue(ns-\u003ehead-\u003edisk-\u003equeue);\n \t\t/*\n \t\t * queue_limits mixes values that are the hardware limitations\n \t\t * for bio splitting with what is the device configuration.\n@@ -2325,7 +2328,7 @@ static int nvme_update_ns_info(struct nvme_ns *ns, struct nvme_ns_info *info)\n \t\tset_disk_ro(ns-\u003ehead-\u003edisk, nvme_ns_is_readonly(ns, info));\n \t\tnvme_mpath_revalidate_paths(ns);\n \n-\t\tblk_mq_unfreeze_queue(ns-\u003ehead-\u003edisk-\u003equeue);\n+\t\tblk_mq_unfreeze_queue(ns-\u003ehead-\u003edisk-\u003equeue, memflags);\n \t}\n \n \treturn ret;\ndiff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c\nindex a85d190942bd..2a7635565083 100644\n--- a/drivers/nvme/host/multipath.c\n+++ b/drivers/nvme/host/multipath.c\n@@ -60,7 +60,7 @@ void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)\n \tlockdep_assert_held(\u0026subsys-\u003elock);\n \tlist_for_each_entry(h, \u0026subsys-\u003ensheads, entry)\n \t\tif (h-\u003edisk)\n-\t\t\tblk_mq_unfreeze_queue(h-\u003edisk-\u003equeue);\n+\t\t\tblk_mq_unfreeze_queue_nomemrestore(h-\u003edisk-\u003equeue);\n }\n \n void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)\ndiff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c\nindex 4411426a7894..b86e259516a7 100644\n--- a/drivers/scsi/scsi_lib.c\n+++ b/drivers/scsi/scsi_lib.c\n@@ -2723,6 +2723,7 @@ int\n scsi_device_quiesce(struct scsi_device *sdev)\n {\n \tstruct request_queue *q = sdev-\u003erequest_queue;\n+\tunsigned int memflags;\n \tint err;\n \n \t/*\n@@ -2737,7 +2738,7 @@ scsi_device_quiesce(struct scsi_device *sdev)\n \n \tblk_set_pm_only(q);\n \n-\tblk_mq_freeze_queue(q);\n+\tmemflags = blk_mq_freeze_queue(q);\n \t/*\n \t * Ensure that the effect of blk_set_pm_only() will be visible\n \t * for percpu_ref_tryget() callers that occur after the queue\n@@ -2745,7 +2746,7 @@ scsi_device_quiesce(struct scsi_device *sdev)\n \t * was called. See also https://lwn.net/Articles/573497/.\n \t */\n \tsynchronize_rcu();\n-\tblk_mq_unfreeze_queue(q);\n+\tblk_mq_unfreeze_queue(q, memflags);\n \n \tmutex_lock(\u0026sdev-\u003estate_mutex);\n \terr = scsi_device_set_state(sdev, SDEV_QUIESCE);\ndiff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c\nindex 042329b74c6e..312d78213954 100644\n--- a/drivers/scsi/scsi_scan.c\n+++ b/drivers/scsi/scsi_scan.c\n@@ -220,6 +220,7 @@ static int scsi_realloc_sdev_budget_map(struct scsi_device *sdev,\n \tint new_shift = sbitmap_calculate_shift(depth);\n \tbool need_alloc = !sdev-\u003ebudget_map.map;\n \tbool need_free = false;\n+\tunsigned int memflags;\n \tint ret;\n \tstruct sbitmap sb_backup;\n \n@@ -240,7 +241,7 @@ static int scsi_realloc_sdev_budget_map(struct scsi_device *sdev,\n \t * and here disk isn't added yet, so freezing is pretty fast\n \t */\n \tif (need_free) {\n-\t\tblk_mq_freeze_queue(sdev-\u003erequest_queue);\n+\t\tmemflags = blk_mq_freeze_queue(sdev-\u003erequest_queue);\n \t\tsb_backup = sdev-\u003ebudget_map;\n \t}\n \tret = sbitmap_init_node(\u0026sdev-\u003ebudget_map,\n@@ -256,7 +257,7 @@ static int scsi_realloc_sdev_budget_map(struct scsi_device *sdev,\n \t\telse\n \t\t\tsbitmap_free(\u0026sb_backup);\n \t\tret = 0;\n-\t\tblk_mq_unfreeze_queue(sdev-\u003erequest_queue);\n+\t\tblk_mq_unfreeze_queue(sdev-\u003erequest_queue, memflags);\n \t}\n \treturn ret;\n }\ndiff --git a/drivers/ufs/core/ufs-sysfs.c b/drivers/ufs/core/ufs-sysfs.c\nindex 796e37a1d859..3438269a5440 100644\n--- a/drivers/ufs/core/ufs-sysfs.c\n+++ b/drivers/ufs/core/ufs-sysfs.c\n@@ -1439,6 +1439,7 @@ static ssize_t max_number_of_rtt_store(struct device *dev,\n \tstruct ufs_hba *hba = dev_get_drvdata(dev);\n \tstruct ufs_dev_info *dev_info = \u0026hba-\u003edev_info;\n \tstruct scsi_device *sdev;\n+\tunsigned int memflags;\n \tunsigned int rtt;\n \tint ret;\n \n@@ -1458,14 +1459,16 @@ static ssize_t max_number_of_rtt_store(struct device *dev,\n \n \tufshcd_rpm_get_sync(hba);\n \n+\tmemflags = memalloc_noio_save();\n \tshost_for_each_device(sdev, hba-\u003ehost)\n-\t\tblk_mq_freeze_queue(sdev-\u003erequest_queue);\n+\t\tblk_mq_freeze_queue_nomemsave(sdev-\u003erequest_queue);\n \n \tret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,\n \t\tQUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, \u0026rtt);\n \n \tshost_for_each_device(sdev, hba-\u003ehost)\n-\t\tblk_mq_unfreeze_queue(sdev-\u003erequest_queue);\n+\t\tblk_mq_unfreeze_queue_nomemrestore(sdev-\u003erequest_queue);\n+\tmemalloc_noio_restore(memflags);\n \n \tufshcd_rpm_put_sync(hba);\n \ndiff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h\nindex a0a9007cc1e3..9ebb53f031cd 100644\n--- a/include/linux/blk-mq.h\n+++ b/include/linux/blk-mq.h\n@@ -900,8 +900,22 @@ void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs);\n void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,\n \t\tbusy_tag_iter_fn *fn, void *priv);\n void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset);\n-void blk_mq_freeze_queue(struct request_queue *q);\n-void blk_mq_unfreeze_queue(struct request_queue *q);\n+void blk_mq_freeze_queue_nomemsave(struct request_queue *q);\n+void blk_mq_unfreeze_queue_nomemrestore(struct request_queue *q);\n+static inline unsigned int __must_check\n+blk_mq_freeze_queue(struct request_queue *q)\n+{\n+\tunsigned int memflags = memalloc_noio_save();\n+\n+\tblk_mq_freeze_queue_nomemsave(q);\n+\treturn memflags;\n+}\n+static inline void\n+blk_mq_unfreeze_queue(struct request_queue *q, unsigned int memflags)\n+{\n+\tblk_mq_unfreeze_queue_nomemrestore(q);\n+\tmemalloc_noio_restore(memflags);\n+}\n void blk_freeze_queue_start(struct request_queue *q);\n void blk_mq_freeze_queue_wait(struct request_queue *q);\n int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,\n"}
[model]:
<thought>
**My Internal Monologue on NBD Driver Code Review**
Alright, time to dive deeper. Beyond what I've already scrutinized, I'm making a conscious effort to be as comprehensive as possible. Therefore, I'm going to take a moment to specifically examine the code sections identified by the commit hashes `cf1b2326b734` and `9f7c02e03157` within the `drivers/block/nbd.c` file. My goal here is not just to glance over them, but to thoroughly understand their implementation and ensure they align with the overall design and any established best practices. This is about leaving no stone unturned in my analysis.
</thought>
Called tool git-show with args: {"Commit":"cf1b2326b734"}
Called tool git-show with args: {"Commit":"9f7c02e03157"}
[user]:
Tool git-show returned: {"Output":"commit cf1b2326b734896734c6e167e41766f9cee7686a\nAuthor: Mike Christie \u003cmchristi@redhat.com\u003e\nDate: Thu Oct 17 16:27:34 2019 -0500\n\n nbd: verify socket is supported during setup\n \n nbd requires socket families to support the shutdown method so the nbd\n recv workqueue can be woken up from its sock_recvmsg call. If the socket\n does not support the callout we will leave recv works running or get hangs\n later when the device or module is removed.\n \n This adds a check during socket connection/reconnection to make sure the\n socket being passed in supports the needed callout.\n \n Reported-by: syzbot+24c12fa8d218ed26011a@syzkaller.appspotmail.com\n Fixes: e9e006f5fcf2 (\"nbd: fix max number of supported devs\")\n Tested-by: Richard W.M. Jones \u003crjones@redhat.com\u003e\n Signed-off-by: Mike Christie \u003cmchristi@redhat.com\u003e\n Signed-off-by: Jens Axboe \u003caxboe@kernel.dk\u003e\n\ndiff --git a/drivers/block/nbd.c b/drivers/block/nbd.c\nindex e200c7c9ddc4..a94ee45440b3 100644\n--- a/drivers/block/nbd.c\n+++ b/drivers/block/nbd.c\n@@ -980,6 +980,25 @@ static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,\n \treturn ret;\n }\n \n+static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,\n+\t\t\t\t int *err)\n+{\n+\tstruct socket *sock;\n+\n+\t*err = 0;\n+\tsock = sockfd_lookup(fd, err);\n+\tif (!sock)\n+\t\treturn NULL;\n+\n+\tif (sock-\u003eops-\u003eshutdown == sock_no_shutdown) {\n+\t\tdev_err(disk_to_dev(nbd-\u003edisk), \"Unsupported socket: shutdown callout must be supported.\\n\");\n+\t\t*err = -EINVAL;\n+\t\treturn NULL;\n+\t}\n+\n+\treturn sock;\n+}\n+\n static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,\n \t\t\t bool netlink)\n {\n@@ -989,7 +1008,7 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,\n \tstruct nbd_sock *nsock;\n \tint err;\n \n-\tsock = sockfd_lookup(arg, \u0026err);\n+\tsock = nbd_get_socket(nbd, arg, \u0026err);\n \tif (!sock)\n \t\treturn err;\n \n@@ -1041,7 +1060,7 @@ static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)\n \tint i;\n \tint err;\n \n-\tsock = sockfd_lookup(arg, \u0026err);\n+\tsock = nbd_get_socket(nbd, arg, \u0026err);\n \tif (!sock)\n \t\treturn err;\n \n"}
Tool git-show returned: {"Output":"commit 9f7c02e031570e8291a63162c6c046dc15ff85b0\nAuthor: Eric Dumazet \u003cedumazet@kernel.org\u003e\nDate: Tue Sep 9 13:22:43 2025 +0000\n\n nbd: restrict sockets to TCP and UDP\n \n Recently, syzbot started to abuse NBD with all kinds of sockets.\n \n Commit cf1b2326b734 (\"nbd: verify socket is supported during setup\")\n made sure the socket supported a shutdown() method.\n \n Explicitely accept TCP and UNIX stream sockets.\n \n Fixes: cf1b2326b734 (\"nbd: verify socket is supported during setup\")\n Reported-by: syzbot+e1cd6bd8493060bd701d@syzkaller.appspotmail.com\n Closes: https://lore.kernel.org/netdev/CANn89iJ+76eE3A_8S_zTpSyW5hvPRn6V57458hCZGY5hbH_bFA@mail.gmail.com/T/#m081036e8747cd7e2626c1da5d78c8b9d1e55b154\n Signed-off-by: Eric Dumazet \u003cedumazet@google.com\u003e\n Cc: Mike Christie \u003cmchristi@redhat.com\u003e\n Cc: Richard W.M. Jones \u003crjones@redhat.com\u003e\n Cc: Jens Axboe \u003caxboe@kernel.dk\u003e\n Cc: Yu Kuai \u003cyukuai1@huaweicloud.com\u003e\n Cc: linux-block@vger.kernel.org\n Cc: nbd@other.debian.org\n Signed-off-by: Jens Axboe \u003caxboe@kernel.dk\u003e\n\ndiff --git a/drivers/block/nbd.c b/drivers/block/nbd.c\nindex 6463d0e8d0ce..87b0b78249da 100644\n--- a/drivers/block/nbd.c\n+++ b/drivers/block/nbd.c\n@@ -1217,6 +1217,14 @@ static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,\n \tif (!sock)\n \t\treturn NULL;\n \n+\tif (!sk_is_tcp(sock-\u003esk) \u0026\u0026\n+\t !sk_is_stream_unix(sock-\u003esk)) {\n+\t\tdev_err(disk_to_dev(nbd-\u003edisk), \"Unsupported socket: should be TCP or UNIX.\\n\");\n+\t\t*err = -EINVAL;\n+\t\tsockfd_put(sock);\n+\t\treturn NULL;\n+\t}\n+\n \tif (sock-\u003eops-\u003eshutdown == sock_no_shutdown) {\n \t\tdev_err(disk_to_dev(nbd-\u003edisk), \"Unsupported socket: shutdown callout must be supported.\\n\");\n \t\t*err = -EINVAL;\n"}
[model]:
<thought>
**This is where I was thinking about the exact phrasing of the question: "Search for the commit that introduced this bug." My goal is to meticulously analyze all potential commits and compare them against the provided instructions.**
Here's my thought process, breaking down the problem and evaluating each possibility:
First, I need to ensure I understand the instructions completely.
1. **Establish the root cause first:** I need to verify the provided root cause analysis by checking the actual source code. This means pinpointing the exact functions, data structures, invariants, or lines of code where the faulty logic lies.
2. **Use git tools:** I'll be relying on `git blame`, `git log`, and `git show` to trace the history of the relevant code sections.
3. **Trace past refactorings:** I must look beyond simple code cleanups like pure refactors, moves, formatting changes, or variable renames to find the commit that *actually introduced* the buggy behavior or violated an existing invariant.
4. **Exclusions:** Crucially, I *must not* blame commits that only added debugging aids (like `WARN_ON`, `BUG_ON`, or sanitizers like KASAN/KMSAN/LOCKDEP annotations) if they simply *exposed* an older bug. The *only* exception is if the check itself is flawed and is the source of the bug.
Now, I need to consider the phrase "this bug" and what it could refer to. I'll explore different interpretations.
**Interpretation 1: The bug is within `nbd_reclassify_socket()` in `drivers/block/nbd.c`.**
My immediate thought is to question why `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency") was introduced. I realize that *before* this commit, NBD sockets were *never* reclassified. This leads me to ask: could `d532cddb6c60` itself have *introduced* this specific circular locking dependency?
Upon reflection, I conclude: NO. Even before `d532cddb6c60`, `nsock->sock->sk` was *always* unconditionally in the `sk_lock-AF_INET` class.
What about `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock")? I need to examine its effect closely.
In `d532cddb6c60`, the relevant line was:
`if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) return;`
In `9280e6edf656`, this was changed to:
`- if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))`
`+ if (!sock_allow_reclassification(sk))`
` return;`
I pause and think critically: Did `9280e6edf656` *introduce* the circular locking dependency? Again, the answer is no. Before `9280e6edf656`, in `d532cddb6c60`, if `!sock_allow_reclassification(sk)` was true, `nbd_reclassify_socket()` *also* returned without performing the reclassification. It just issued a `WARN_ON_ONCE`. If `panic_on_warn` was enabled, the kernel would crash at that `WARN_ON_ONCE`. If not, the `sk` would remain in `sk_lock-AF_INET`, and the exact same lockdep circular dependency would occur.
Furthermore, before `d532cddb6c60` was even committed, `nbd_reclassify_socket()` didn't exist, meaning `sk` was *always* in `sk_lock-AF_INET`.
**Wait a second!** This prompts me to consider the broader picture from the perspective of both syzkaller and the kernel bug. Why wasn't this specific lockdep circular dependency reported *before* `9280e6edf656`, `d532cddb6c60`, `1e44bedbc921a`, or `f1be1788a32e`? I need to check the history.
Before `d532cddb6c60`: If syzkaller mounted btrfs over NBD, *any* TCP socket combined with btrfs `fallocate` and subsequent operations like `page_mkwrite` and `elevator_change` would trigger this lockdep splat, even without a race condition in `nbd_reclassify_socket()`.
Now, I need to consider the sequence of events: Did syzkaller trigger the `d532cddb6c60` splat first (on `fs_reclaim`), then `d532cddb6c60` fixed the non-racing case, then syzkaller hit the `WARN_ON_ONCE` in `d532cddb6c60` (syzbot+6b85d1e39a5b8ed9a954)? Then `9280e6edf656` removed the `WARN_ON_ONCE`, allowing syzkaller to trigger the `553d28925fde` splat (reconnect path, syzbot+d7d72f3d467710442af0)? And *then* syzkaller finally hit *this* specific splat, where `nbd_reclassify_socket` silently returned without reclassifying, or a userspace race on `NBD_SET_SOCK` with socket activity occurred?
**Hold on! I need to re-evaluate.** Let's think about the state *before* `9280e6edf656`.
Before `9280e6edf656`:
* The NBD driver had a lockdep circular dependency because NBD sockets shared the `sk_lock-AF_INET` class with user sockets.
* This bug arguably existed much earlier, perhaps since `f1be1788a32e` (a lockdep annotation for freeze/enter queue) or `560bc4b39952` (2017, where `nbd_mark_nsock_dead` called `kernel_sock_shutdown` under `tx_lock`), or even earlier.
**Wait, let me check `nbd_send_cmd`!** Does `nbd_send_cmd` also call `sock_sendmsg` under `nsock->tx_lock`? Yes, it does: `nbd_send_cmd` -> `sock_xmit` -> `sock_sendmsg` -> `tcp_sendmsg` -> `lock_sock(sk)`. And NBD has been using `sock_sendmsg` under `tx_lock` since Linux 2.6 (`1da177e4c3f4`).
**This is a critical juncture.** I need to focus on what "the commit that introduced this bug" truly means in this context. I must list *every* potential candidate commit and rigorously test each one against the instructions.
Here are the primary candidates and my analysis:
**Candidate A: `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock")**
* **Why it might be considered:** The Root Cause Analysis (RCA) points to `nbd_reclassify_socket()` silently failing. It states: "When that occurs, `nbd_reclassify_socket()` silently returns without reclassifying the socket and without failing `nbd_add_socket()`/`nbd_genl_reconnect_sock_cb()`, leaving `nsock->sock->sk` in the generic `sk_lock-AF_INET` lockdep class." This is *exactly* what `9280e6edf656` did by changing the `WARN_ON_ONCE` to a silent `return` when `!sock_allow_reclassification(sk)`. Furthermore, the commit message itself claimed: "Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless." This assertion is demonstrably *false*, as skipping reclassification *was not* harmless and led to re-exposing the lockdep circular dependency.
* **Why it might *not* be the original cause of the circular dependency:** Before `9280e6edf656`, `d532cddb6c60` also had a `return;` path without reclassification (though it included `WARN_ON_ONCE`). And before `d532cddb6c60`, NBD didn't reclassify sockets at all. So, while `9280e6edf656` made the failure silent and thus revealed the bug to syzkaller, it didn't create the underlying circular dependency condition. The removal of `WARN_ON_ONCE` in `9280e6edf656` could be seen as either removing a warning or enabling a broken fallback mechanism (silent skipping of reclassification).
**Candidate B: `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency")**
* **Did it introduce the circular dependency?** No. As established earlier, this commit was an *incomplete attempt* to fix the circular dependency (it `Fixes: ffa1e7ada456`, which is a related issue).
* **Adhering to the rules:** Instruction #3 states: "Once you identify the primary commit that introduced (or fixed) the bug, return ONLY that commit... 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..." `d532cddb6c60` was an incomplete fix, not the introducer.
**Candidate C: Is there an *earlier* commit that ACTUALLY introduced this circular locking dependency (or a real deadlock), or did `f1be1788a32e` / `1e44bedbc921a` / `ffa1e7ada456` merely expose an older bug?**
This is where I need to trace the entire chain of lock acquisitions carefully, applying rule #4: "Do NOT blame commits that merely added debugging or sanitizer checks... that exposed an older bug."
Let's examine the dependencies:
1. **Btrfs path:** `btrfs_fallocate()` -> `btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_MMAP)` -> `file_modified()` -> `btrfs_xattr_handler_get_security()` -> `btrfs_search_slot()` -> `btrfs_read_lock_root_node()` (`btrfs-tree-01`) -> `read_block_for_search()` (`btrfs-tree-00`) -> `btrfs_submit_bbio()` -> `blk_mq_submit_bio()` (`&q->q_usage_counter(io)`).
* `8d9b4a162a37` (2021): `btrfs_fallocate` started taking `i_mmap_lock`.
* `05fd9564e9faf` (2022): `btrfs_fallocate` began calling `file_modified()` while holding `i_mmap_lock`.
* `f1be1788a32e` (Oct 2024): `block: model freeze & enter queue as lock for supporting lockdep` - **This added a lockdep annotation (`&q->q_usage_counter(io)`)**. Per rule #4, I cannot blame this for *introducing* the bug; it only *exposed* it.
2. **Elevator path:** `elv_iosched_store()` / `elevator_set_default()` -> `elevator_change()`:
* `blk_mq_freeze_queue(q)` (`&q->q_usage_counter(io)`) -> `mutex_lock(&q->elevator_lock)` -> `elevator_switch()` -> `blk_mq_quiesce_queue()` (`set->srcu`).
* `elevator_set_default()` only started calling `elevator_change()` (which acquires `q->elevator_lock` and `blk_mq_quiesce_queue` -> `set->srcu`) in `1e44bedbc921a` (May 2025)!
* **However**, even before `1e44bedbc921a`, `elv_iosched_store` would call `elevator_change` -> `elevator_switch` -> `blk_mq_quiesce_queue` (`set->srcu`) while holding `q->elevator_lock` (or `q->sysfs_lock`) and `q->q_usage_counter(io)`. So, the `q->q_usage_counter(io)` -> `q->elevator_lock` -> `set->srcu` dependency was already possible via `elv_iosched_store`.
3. **NBD request path:** `blk_mq_dispatch_rq_list()` (under `set->srcu`) -> `nbd_queue_rq()` -> `&cmd->lock` -> `nbd_handle_cmd()` -> `&nsock->tx_lock`.
4. **NBD socket cleanup:** `recv_work()` -> `&nsock->tx_lock` -> `nbd_mark_nsock_dead()` -> `kernel_sock_shutdown()` -> `inet_shutdown()` -> `lock_sock()` (`sk_lock-AF_INET`).
5. **TCP send path:** `tcp_sendmsg_locked()` (under `sk_lock-AF_INET`) -> `skb_copy_to_page_nocache()` -> `__might_fault()` (`&mm->mmap_lock`).
6. **Btrfs page fault path:** `do_user_addr_fault()` (under `&mm->mmap_lock`) -> `do_page_mkwrite()` -> `btrfs_page_mkwrite()` -> `sb_start_pagefault()` (`sb_pagefaults`) -> `down_read(&ei->i_mmap_lock)`.
**Now, let's re-evaluate the critical path and rule #4 again:**
The full dependency chain is:
`btrfs_fallocate` (`i_mmap_lock` or `BTRFS_ILOCK_MMAP`) -> `file_modified` -> `btrfs_search_slot` -> `blk_mq_submit_bio` (`q_usage_counter`) -> `elv_iosched_store` (`elevator_lock`, `set->srcu`) -> `nbd_queue_rq` (`cmd->lock`, `tx_lock`) -> `recv_work` (`tx_lock`, `nbd_mark_nsock_dead` -> `kernel_sock_shutdown` -> `inet_shutdown` -> `lock_sock` (`sk_lock-AF_INET`)) <- *This is the NBD socket*
AND
`tcp_sendmsg_locked` (`sk_lock-AF_INET` - *this could be the same socket or a different one*) -> `__might_fault` (`mm->mmap_lock`) -> `do_page_mkwrite` -> `btrfs_page_mkwrite` -> `sb_start_pagefault` (`sb_pagefaults`) -> `down_read(&ei->i_mmap_lock)`.
Looking at the stack trace provided in the prompt's context, specifically frame `#7` (`nbd_mark_nsock_dead`) showing `sk_lock-AF_INET` and frame `#8` (`tcp_sendmsg_locked`) also showing `sk_lock-AF_INET`, this strongly indicates that in *this particular crash*, the NBD socket *was not* reclassified, and it remained in the generic `sk_lock-AF_INET` class.
The crucial point is rule #4: I *cannot* blame `f1be1788a32e` (which added `q_usage_counter` annotation) or `ffa1e7ada456` (which made lockdep splats more visible) because they merely *exposed* an older bug.
This brings me back to `9280e6edf656`. Why was the RCA focused there? Because at the specific commit hash mentioned (`19cc08c1a6a650f8e54c0461ff198be6448f425d`), the `nbd_reclassify_socket()` function was *already intended* to prevent this false-positive lockdep cycle by moving NBD sockets to the `sk_lock-AF_INET-NBD` class. The *only* reason `nsock->sock->sk` remained in the generic `sk_lock-AF_INET` class at that commit was that `nbd_reclassify_socket()` silently failed to perform the reclassification.
And why did `nbd_reclassify_socket()` fail to reclassify? Because `sock_allow_reclassification(sk)` returned `false` when the socket lock (`sk->sk_lock` or `sk->sk_lock.slock`) was held (likely due to a concurrent userspace syscall on the file descriptor or a softirq `bh_lock_sock`). And at that point, `nbd_reclassify_socket()` simply returned without reclassifying.
The change from `WARN_ON_ONCE` to a silent `return` in `9280e6edf656` directly enabled the scenario where this silent failure led to the observable lockdep splat, making it the most direct cause of the *reported* bug. While the underlying circular dependency *might* have been theoretically possible earlier, `9280e6edf656` is the commit that made it manifest and observable in this specific way, by turning a warning into silent failure.
Therefore, based on the instructions and the evidence, `9280e6edf656` is the commit that introduced the specific bug *as reported*, by making the failure silent and allowing the previously existing, but less exposed, circular dependency to trigger the splat.
</thought>
Called tool grepper with args: {"Expression":"-\u003esock\\s*=|nbd_get_socket|nbd_reclassify_socket","PathPrefixes":["drivers/block/nbd.c"]}
[user]:
Tool grepper returned: {"Output":"drivers/block/nbd.c=1184=static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,\n--\ndrivers/block/nbd.c-1212-\ndrivers/block/nbd.c:1213:static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,\ndrivers/block/nbd.c-1214-\t\t\t\t int *err)\n--\ndrivers/block/nbd.c=1243=static struct lock_class_key nbd_slock_key[3];\ndrivers/block/nbd.c-1244-\ndrivers/block/nbd.c:1245:static void nbd_reclassify_socket(struct socket *sock)\ndrivers/block/nbd.c-1246-{\n--\ndrivers/block/nbd.c-1273-#else\ndrivers/block/nbd.c:1274:static inline void nbd_reclassify_socket(struct socket *sock) {}\ndrivers/block/nbd.c-1275-#endif\n--\ndrivers/block/nbd.c=1277=static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,\n--\ndrivers/block/nbd.c-1295-\ndrivers/block/nbd.c:1296:\tsock = nbd_get_socket(nbd, arg, \u0026err);\ndrivers/block/nbd.c-1297-\tif (!sock)\ndrivers/block/nbd.c-1298-\t\treturn err;\ndrivers/block/nbd.c:1299:\tnbd_reclassify_socket(sock);\ndrivers/block/nbd.c-1300-\n--\ndrivers/block/nbd.c-1332-\tmutex_init(\u0026nsock-\u003etx_lock);\ndrivers/block/nbd.c:1333:\tnsock-\u003esock = sock;\ndrivers/block/nbd.c-1334-\tnsock-\u003epending = NULL;\n--\ndrivers/block/nbd.c=1348=static int nbd_genl_reconnect_sock_cb(struct nbd_device *nbd, unsigned long arg)\n--\ndrivers/block/nbd.c-1355-\ndrivers/block/nbd.c:1356:\tsock = nbd_get_socket(nbd, arg, \u0026err);\ndrivers/block/nbd.c-1357-\tif (!sock)\ndrivers/block/nbd.c-1358-\t\treturn err;\ndrivers/block/nbd.c:1359:\tnbd_reclassify_socket(sock);\ndrivers/block/nbd.c-1360-\n--\ndrivers/block/nbd.c-1384-\t\tnsock-\u003efallback_index = -1;\ndrivers/block/nbd.c:1385:\t\tnsock-\u003esock = sock;\ndrivers/block/nbd.c-1386-\t\tnsock-\u003edead = false;\n"}
[model]:
<thought>
**My Deep Dive into the NBD Lock Dependency Conundrum**
Okay, let's get to the heart of this NBD socket reclassification issue. I've been tracing the flow and I'm realizing the core of the problem lies in how and when `nsock->sock` is assigned and subsequently reclassified.
I see that `nsock->sock` is *only* ever assigned in two places: `nbd_add_socket` and `nbd_genl_reconnect_sock_cb`. And, critically, *both* of these functions call `nbd_reclassify_socket(sock)` immediately afterward. Now, `nbd_reclassify_socket` itself only fails to reclassify `AF_INET` sockets when `sock_allow_reclassification(sk)` returns false. This is where things get interesting.
I've been thinking very carefully about how `git bisect` would have handled this. Let's play out that scenario:
1. **Commit `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock"):** If I were to bisect to this commit, the `nbd_reclassify_socket` function would *silently skip* the reclassification. This is precisely where the lockdep circular dependency would manifest – it would trigger a `WARNING: possible circular locking dependency detected`.
2. **Commit `9280e6edf656~1` (the parent/merge before it, which is `d532cddb6c60`):** Moving back one commit, to `d532cddb6c60`, `nbd_reclassify_socket` would *not* silently skip. Instead, it would hit the `WARN_ON_ONCE(!sock_allow_reclassification(sk))` condition. This would produce a *different* warning: `WARNING in nbd_reclassify_socket`, which syzkaller would have reported as `syzbot+6b85d1e39a5b8ed9a954`.
This brings me to a crucial point: why did commit `9280e6edf656` *cause* this specific bug, or did it? It seems counter-intuitive that removing a `WARN_ON_ONCE` would introduce a silent failure. Let's re-examine `9280e6edf656`:
- if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
+ if (!sock_allow_reclassification(sk))
return;
It *only* removed the `WARN_ON_ONCE` macro, not the condition itself. This implies that the change was intentionally to *suppress the warning* when `sock_allow_reclassification(sk)` was false, and to *silently return* instead. This is where Rule 4 of good bisecting practices comes into play: "Do NOT blame commits that merely added debugging or sanitizer checks... that exposed an older bug, unless the check itself is incorrect".
However, `9280e6edf656` *removed* a `WARN_ON_ONCE`, it didn't add one. So, did removing this warning *introduce* the bug of `nbd_reclassify_socket()` silently skipping reclassification?
Let's trace back further:
* **Before `9280e6edf656`:** Commit `d532cddb6c60` also returned without reclassifying, but it *did* issue a `WARN_ON_ONCE`.
* **Before `d532cddb6c60`:** NBD didn't reclassify sockets *at all*.
This leads me to question the rationale in the commit message for `9280e6edf656`: "Hitting a held lock here is expected for an externally owned socket and is not a kernel bug, so skip reclassification silently instead of warning. Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless." This commit seems to have *explicitly opted for silent skipping* as the intended behavior, rather than fixing the underlying race condition that could occur when an externally owned socket (obtained via `sockfd_lookup()`) has its lock held during `nbd_reclassify_socket`, or is being used concurrently by userspace.
So, is `9280e6edf656` the culprit? Or is it `d532cddb6c60`? Or perhaps something much earlier in the NBD or block layer history?
I need to be extremely deliberate here and not rush. Let's thoroughly examine every potential candidate.
Here are the commits I'm considering:
1. `9280e6edf656` ("nbd: don't warn when reclassifying a busy socket lock") - Jun 22, 2026
2. `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency") - Jun 13, 2026
3. `1e44bedbc921` ("block: unifying elevator change") - May 5, 2025
4. `1bf70d08cc3b` ("block: introduce a dedicated lock for protecting queue elevator updates") - Mar 4, 2025
5. `f1be1788a32e` ("block: model freeze & enter queue as lock for supporting lockdep") - Oct 25, 2024
6. `05fd9564e9fa` ("btrfs: fix fallocate to use file_modified to update permissions consistently") - Mar 14, 2022
7. `8d9b4a162a37` ("btrfs: exclude mmap from happening during all fallocate operations") - Feb 10, 2021
8. `560bc4b39952` (or earlier, concerning NBD's handling of dead connections and `lock_sock` under `tx_lock` / `cmd->lock` / `nbd_queue_rq`) - circa 2017
Let me scrutinize each of these.
### **Initial Scrutiny of Candidates #3 through #8:**
When I look at commits #3, #4, #5, #6, #7, and #8, I'm asking myself: is the fundamental locking pattern within *these specific components* a bug in itself?
* **Btrfs (`8d9b4a162a37`, `05fd9564e9fa`):** The pattern of holding `i_mmap_lock` across `file_modified()` leading into `btrfs_search_slot()` and then `blk_mq_submit_bio()` is entirely expected and valid for a filesystem.
* **Block Layer (`1e44bedbc921`, `1bf70d08cc3b`, `f1be1788a32e`):** The sequence of freezing the queue (`q_usage_counter(io)`), acquiring `q->elevator_lock`, and then calling `blk_mq_quiesce_queue()` (which involves `set->srcu`) is standard block layer operation. In fact, `1bf70d08cc3b` was specifically intended to *correct* lock ordering here, moving from `sysfs_lock` to `elevator_lock`. Commit `f1be1788a32e` only introduced lockdep annotations which, per Rule 4, we shouldn't blame if they just reveal an existing bug.
* **NBD Driver (`560bc4b39952` and earlier):** The core operation of NBD, where `nbd_queue_rq` eventually leads to `sock_sendmsg`, inherently involves acquiring the socket lock (`sk_lock`) while already holding NBD's internal locks (`cmd->lock`, `nsock->tx_lock`). This is fundamental to its network block device nature.
* **TCP Socket Operations (`net/ipv4/tcp.c`):** The standard operation of acquiring `sk_lock` and then potentially `mm->mmap_lock` during memory faulting in `sendmsg`/`recvmsg` is also fundamental to TCP sockets.
* **Memory Management (`mm/memory.c` + `fs/btrfs/file.c`):** The lock ordering involving `mm->mmap_lock` followed by `sb_pagefaults` and `i_mmap_lock` within `page_mkwrite` is standard for memory-mapped files.
**Conclusion on candidates #3-#8:** None of the *individual* lock acquisitions within btrfs, the block layer, NBD's I/O path, or standard TCP operations appear to be a bug in their own right. The problem isn't *that* these locks are acquired, but rather the *combination* and the *order* when they all intersect, particularly with NBD's socket handling.
This brings me to two key insights:
1. **NBD's Socket Lockdep Class:** NBD's socket lockdep class *must* be distinct from the standard `AF_INET` socket lockdep class. If they share the same class, then *any* filesystem, page cache, or swap operation involving NBD will create a lockdep circular dependency with *any* standard userspace TCP socket. This is a fundamental architectural need.
2. **Userspace Socket Interference:** Userspace must not be operating on or holding onto the same socket that NBD is actively using, or `nbd_reclassify_socket` needs to handle this scenario more robustly than simply skipping reclassification.
Now, let's re-focus on the direct NBD changes: `9280e6edf656` and `d532cddb6c60`. It's clear the bug stems from how NBD handles socket reclassification and the associated locking. I need to dig deeper into what exactly each of these commits changed and why.
</thought>
Called tool codesearch-definition-source with args: {"ContextFile":"drivers/block/nbd.c","Name":"sock_xmit"}
Called tool codesearch-definition-source with args: {"ContextFile":"drivers/block/nbd.c","Name":"nbd_send_cmd"}
[user]:
Tool codesearch-definition-source returned: {"SourceCode":" 604:\t/*\n 605:\t * Send or receive packet. Return a positive value on success and\n 606:\t * negtive value on failure, and never return 0.\n 607:\t */\n 608:\tstatic int sock_xmit(struct nbd_device *nbd, int index, int send,\n 609:\t\t\t struct iov_iter *iter, int msg_flags, int *sent)\n 610:\t{\n 611:\t\tstruct nbd_config *config = nbd-\u003econfig;\n 612:\t\tstruct socket *sock = config-\u003esocks[index]-\u003esock;\n 613:\t\n 614:\t\treturn __sock_xmit(nbd, sock, send, iter, msg_flags, sent);\n 615:\t}\n","SourceFile":"drivers/block/nbd.c"}
Tool codesearch-definition-source returned: {"SourceCode":" 650:\t/*\n 651:\t * Returns BLK_STS_RESOURCE if the caller should retry after a delay.\n 652:\t * Returns BLK_STS_IOERR if sending failed.\n 653:\t */\n 654:\tstatic blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,\n 655:\t\t\t\t\t int index)\n 656:\t{\n 657:\t\tstruct request *req = blk_mq_rq_from_pdu(cmd);\n 658:\t\tstruct nbd_config *config = nbd-\u003econfig;\n 659:\t\tstruct nbd_sock *nsock = config-\u003esocks[index];\n 660:\t\tint result;\n 661:\t\tstruct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};\n 662:\t\tstruct kvec iov = {.iov_base = \u0026request, .iov_len = sizeof(request)};\n 663:\t\tstruct iov_iter from;\n 664:\t\tstruct bio *bio;\n 665:\t\tu64 handle;\n 666:\t\tu32 type;\n 667:\t\tu32 nbd_cmd_flags = 0;\n 668:\t\tint sent = nsock-\u003esent, skip = 0;\n 669:\t\n 670:\t\tlockdep_assert_held(\u0026cmd-\u003elock);\n 671:\t\tlockdep_assert_held(\u0026nsock-\u003etx_lock);\n 672:\t\n 673:\t\tiov_iter_kvec(\u0026from, ITER_SOURCE, \u0026iov, 1, sizeof(request));\n 674:\t\n 675:\t\ttype = req_to_nbd_cmd_type(req);\n 676:\t\tif (type == U32_MAX)\n 677:\t\t\treturn BLK_STS_IOERR;\n 678:\t\n 679:\t\tif (rq_data_dir(req) == WRITE \u0026\u0026\n 680:\t\t (config-\u003eflags \u0026 NBD_FLAG_READ_ONLY)) {\n 681:\t\t\tdev_err_ratelimited(disk_to_dev(nbd-\u003edisk),\n 682:\t\t\t\t\t \"Write on read-only\\n\");\n 683:\t\t\treturn BLK_STS_IOERR;\n 684:\t\t}\n 685:\t\n 686:\t\tif (req-\u003ecmd_flags \u0026 REQ_FUA)\n 687:\t\t\tnbd_cmd_flags |= NBD_CMD_FLAG_FUA;\n 688:\t\tif ((req-\u003ecmd_flags \u0026 REQ_NOUNMAP) \u0026\u0026 (type == NBD_CMD_WRITE_ZEROES))\n 689:\t\t\tnbd_cmd_flags |= NBD_CMD_FLAG_NO_HOLE;\n 690:\t\n 691:\t\t/* We did a partial send previously, and we at least sent the whole\n 692:\t\t * request struct, so just go and send the rest of the pages in the\n 693:\t\t * request.\n 694:\t\t */\n 695:\t\tif (sent) {\n 696:\t\t\tif (sent \u003e= sizeof(request)) {\n 697:\t\t\t\tskip = sent - sizeof(request);\n 698:\t\n 699:\t\t\t\t/* initialize handle for tracing purposes */\n 700:\t\t\t\thandle = nbd_cmd_handle(cmd);\n 701:\t\n 702:\t\t\t\tgoto send_pages;\n 703:\t\t\t}\n 704:\t\t\tiov_iter_advance(\u0026from, sent);\n 705:\t\t} else {\n 706:\t\t\tcmd-\u003ecmd_cookie++;\n 707:\t\t}\n 708:\t\tcmd-\u003eindex = index;\n 709:\t\tcmd-\u003ecookie = nsock-\u003ecookie;\n 710:\t\tcmd-\u003eretries = 0;\n 711:\t\trequest.type = htonl(type | nbd_cmd_flags);\n 712:\t\tif (type != NBD_CMD_FLUSH) {\n 713:\t\t\trequest.from = cpu_to_be64((u64)blk_rq_pos(req) \u003c\u003c 9);\n 714:\t\t\trequest.len = htonl(blk_rq_bytes(req));\n 715:\t\t}\n 716:\t\thandle = nbd_cmd_handle(cmd);\n 717:\t\trequest.cookie = cpu_to_be64(handle);\n 718:\t\n 719:\t\ttrace_nbd_send_request(\u0026request, nbd-\u003eindex, blk_mq_rq_from_pdu(cmd));\n 720:\t\n 721:\t\tdev_dbg(nbd_to_dev(nbd), \"request %p: sending control (%s@%llu,%uB)\\n\",\n 722:\t\t\treq, nbdcmd_to_ascii(type),\n 723:\t\t\t(unsigned long long)blk_rq_pos(req) \u003c\u003c 9, blk_rq_bytes(req));\n 724:\t\tresult = sock_xmit(nbd, index, 1, \u0026from,\n 725:\t\t\t\t(type == NBD_CMD_WRITE) ? MSG_MORE : 0, \u0026sent);\n 726:\t\ttrace_nbd_header_sent(req, handle);\n 727:\t\tif (result \u003c 0) {\n 728:\t\t\tif (was_interrupted(result)) {\n 729:\t\t\t\t/* If we haven't sent anything we can just return BUSY,\n 730:\t\t\t\t * however if we have sent something we need to make\n 731:\t\t\t\t * sure we only allow this req to be sent until we are\n 732:\t\t\t\t * completely done.\n 733:\t\t\t\t */\n 734:\t\t\t\tif (sent) {\n 735:\t\t\t\t\tnbd_sched_pending_work(nbd, nsock, cmd, sent);\n 736:\t\t\t\t\treturn BLK_STS_OK;\n 737:\t\t\t\t}\n 738:\t\t\t\tset_bit(NBD_CMD_REQUEUED, \u0026cmd-\u003eflags);\n 739:\t\t\t\treturn BLK_STS_RESOURCE;\n 740:\t\t\t}\n 741:\t\t\tdev_err_ratelimited(disk_to_dev(nbd-\u003edisk),\n 742:\t\t\t\t\"Send control failed (result %d)\\n\", result);\n 743:\t\t\tgoto requeue;\n 744:\t\t}\n 745:\tsend_pages:\n 746:\t\tif (type != NBD_CMD_WRITE)\n 747:\t\t\tgoto out;\n 748:\t\n 749:\t\tbio = req-\u003ebio;\n 750:\t\twhile (bio) {\n 751:\t\t\tstruct bio *next = bio-\u003ebi_next;\n 752:\t\t\tstruct bvec_iter iter;\n 753:\t\t\tstruct bio_vec bvec;\n 754:\t\n 755:\t\t\tbio_for_each_segment(bvec, bio, iter) {\n 756:\t\t\t\tbool is_last = !next \u0026\u0026 bio_iter_last(bvec, iter);\n 757:\t\t\t\tint flags = is_last ? 0 : MSG_MORE;\n 758:\t\n 759:\t\t\t\tdev_dbg(nbd_to_dev(nbd), \"request %p: sending %d bytes data\\n\",\n 760:\t\t\t\t\treq, bvec.bv_len);\n 761:\t\t\t\tiov_iter_bvec(\u0026from, ITER_SOURCE, \u0026bvec, 1, bvec.bv_len);\n 762:\t\t\t\tif (skip) {\n 763:\t\t\t\t\tif (skip \u003e= iov_iter_count(\u0026from)) {\n 764:\t\t\t\t\t\tskip -= iov_iter_count(\u0026from);\n 765:\t\t\t\t\t\tcontinue;\n 766:\t\t\t\t\t}\n 767:\t\t\t\t\tiov_iter_advance(\u0026from, skip);\n 768:\t\t\t\t\tskip = 0;\n 769:\t\t\t\t}\n 770:\t\t\t\tresult = sock_xmit(nbd, index, 1, \u0026from, flags, \u0026sent);\n 771:\t\t\t\tif (result \u003c 0) {\n 772:\t\t\t\t\tif (was_interrupted(result)) {\n 773:\t\t\t\t\t\tnbd_sched_pending_work(nbd, nsock, cmd, sent);\n 774:\t\t\t\t\t\treturn BLK_STS_OK;\n 775:\t\t\t\t\t}\n 776:\t\t\t\t\tdev_err(disk_to_dev(nbd-\u003edisk),\n 777:\t\t\t\t\t\t\"Send data failed (result %d)\\n\",\n 778:\t\t\t\t\t\tresult);\n 779:\t\t\t\t\tgoto requeue;\n 780:\t\t\t\t}\n 781:\t\t\t\t/*\n 782:\t\t\t\t * The completion might already have come in,\n 783:\t\t\t\t * so break for the last one instead of letting\n 784:\t\t\t\t * the iterator do it. This prevents use-after-free\n 785:\t\t\t\t * of the bio.\n 786:\t\t\t\t */\n 787:\t\t\t\tif (is_last)\n 788:\t\t\t\t\tbreak;\n 789:\t\t\t}\n 790:\t\t\tbio = next;\n 791:\t\t}\n 792:\tout:\n 793:\t\ttrace_nbd_payload_sent(req, handle);\n 794:\t\tnsock-\u003epending = NULL;\n 795:\t\tnsock-\u003esent = 0;\n 796:\t\t__set_bit(NBD_CMD_INFLIGHT, \u0026cmd-\u003eflags);\n 797:\t\treturn BLK_STS_OK;\n 798:\t\n 799:\trequeue:\n 800:\t\t/*\n 801:\t\t * Can't requeue in case we are dealing with partial send\n 802:\t\t *\n 803:\t\t * We must run from pending work function.\n 804:\t\t * */\n 805:\t\tif (test_bit(NBD_CMD_PARTIAL_SEND, \u0026cmd-\u003eflags))\n 806:\t\t\treturn BLK_STS_OK;\n 807:\t\n 808:\t\t/* retry on a different socket */\n 809:\t\tdev_err_ratelimited(disk_to_dev(nbd-\u003edisk),\n 810:\t\t\t\t \"Request send failed, requeueing\\n\");\n 811:\t\tnbd_mark_nsock_dead(nbd, nsock, 1);\n 812:\t\tnbd_requeue_cmd(cmd);\n 813:\t\treturn BLK_STS_OK;\n 814:\t}\n","SourceFile":"drivers/block/nbd.c"}
[model]:
<thought>
**My Deliberations on Commit Analysis**
Before diving into any substantive analysis, my primary thought is the need for precise, raw data. To that end, my immediate impulse is to perform a `git show` operation for each of the specified commit SHAs: `d532cddb6c60`, `9280e6edf656`, and `553d28925fde`. This isn't a superficial step; I require the absolute, unvarnished details from each commit. I need to see their full commit messages to grasp the intent and context, examine the exact diffs to understand the precise code changes introduced, and note the commit dates to establish a chronological understanding of their evolution. Only with this granular, definitive information can I proceed with any confidence in my subsequent interpretations and assessments.
</thought>
Called tool git-show with args: {"Commit":"d532cddb6c60"}
Called tool git-show with args: {"Commit":"9280e6edf656"}
Called tool git-show with args: {"Commit":"553d28925fde"}
[user]:
Tool git-show returned: {"Output":"commit d532cddb6c6049ced414d64d83c6ce7149a6421a\nAuthor: Eric Dumazet \u003cedumazet@kernel.org\u003e\nDate: Sat Jun 13 04:26:19 2026 +0000\n\n nbd: Reclassify sockets to avoid lockdep circular dependency\n \n syzbot reported a possible circular locking dependency in udp_sendmsg()\n where fs_reclaim can be triggered while holding sk_lock, and fs_reclaim\n can eventually depend on another sk_lock (e.g., if NBD is used for swap\n or writeback and NBD uses TLS/TCP which acquires sk_lock).\n \n Since the UDP socket and the NBD TCP/TLS socket are different, this is a\n false positive. Fix this by reclassifying NBD sockets to a separate lock\n class when they are added to the NBD device.\n \n This is similar to what nvme-tcp and other network block devices do.\n \n Fixes: ffa1e7ada456 (\"block: Make request_queue lockdep splats show up earlier\")\n Reported-by: syzbot+607cdcf978b3e79da878@syzkaller.appspotmail.com\n Closes: https://lore.kernel.org/netdev/6a2cdafe.428ffe26.258b27.0161.GAE@google.com/T/#u\n Signed-off-by: Eric Dumazet \u003cedumazet@google.com\u003e\n Link: https://patch.msgid.link/20260613042619.1108126-1-edumazet@google.com\n Signed-off-by: Jens Axboe \u003caxboe@kernel.dk\u003e\n\ndiff --git a/drivers/block/nbd.c b/drivers/block/nbd.c\nindex e2fe9e3308fc..3a585a0c882a 100644\n--- a/drivers/block/nbd.c\n+++ b/drivers/block/nbd.c\n@@ -1238,6 +1238,42 @@ static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,\n \treturn sock;\n }\n \n+#ifdef CONFIG_DEBUG_LOCK_ALLOC\n+static struct lock_class_key nbd_key[3];\n+static struct lock_class_key nbd_slock_key[3];\n+\n+static void nbd_reclassify_socket(struct socket *sock)\n+{\n+\tstruct sock *sk = sock-\u003esk;\n+\n+\tif (WARN_ON_ONCE(!sock_allow_reclassification(sk)))\n+\t\treturn;\n+\n+\tswitch (sk-\u003esk_family) {\n+\tcase AF_INET:\n+\t\tsock_lock_init_class_and_name(sk, \"slock-AF_INET-NBD\",\n+\t\t\t\t\t \u0026nbd_slock_key[0],\n+\t\t\t\t\t \"sk_lock-AF_INET-NBD\",\n+\t\t\t\t\t \u0026nbd_key[0]);\n+\t\tbreak;\n+\tcase AF_INET6:\n+\t\tsock_lock_init_class_and_name(sk, \"slock-AF_INET6-NBD\",\n+\t\t\t\t\t \u0026nbd_slock_key[1],\n+\t\t\t\t\t \"sk_lock-AF_INET6-NBD\",\n+\t\t\t\t\t \u0026nbd_key[1]);\n+\t\tbreak;\n+\tcase AF_UNIX:\n+\t\tsock_lock_init_class_and_name(sk, \"slock-AF_UNIX-NBD\",\n+\t\t\t\t\t \u0026nbd_slock_key[2],\n+\t\t\t\t\t \"sk_lock-AF_UNIX-NBD\",\n+\t\t\t\t\t \u0026nbd_key[2]);\n+\t\tbreak;\n+\t}\n+}\n+#else\n+static inline void nbd_reclassify_socket(struct socket *sock) {}\n+#endif\n+\n static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,\n \t\t\t bool netlink)\n {\n@@ -1254,6 +1290,7 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,\n \tsock = nbd_get_socket(nbd, arg, \u0026err);\n \tif (!sock)\n \t\treturn err;\n+\tnbd_reclassify_socket(sock);\n \n \t/*\n \t * We need to make sure we don't get any errant requests while we're\n"}
Tool git-show returned: {"Output":"commit 9280e6edf65662b6aafc8b704ad065b54c08b519\nAuthor: Deepanshu Kartikey \u003ckartikey406@gmail.com\u003e\nDate: Mon Jun 22 05:22:55 2026 +0530\n\n nbd: don't warn when reclassifying a busy socket lock\n \n nbd_reclassify_socket() warns via WARN_ON_ONCE() if the socket lock is\n held at the point of reclassification. That assertion was copied from\n nvme-tcp, where the socket is created internally by the kernel\n (sock_create_kern()) and is never visible to user space, so the lock\n is guaranteed to be free.\n \n NBD is different: the socket is looked up from a user-supplied fd in\n nbd_get_socket(), and user space retains that fd. A concurrent syscall\n on the same socket (or softirq processing taking bh_lock_sock() on a\n connected TCP socket) can legitimately hold the lock at the instant\n NBD reclassifies it. sock_allow_reclassification() then returns false\n and the WARN_ON_ONCE() fires, which turns into a crash under\n panic_on_warn. This is reachable by simply racing NBD_CMD_CONNECT\n against socket activity on the same fd, as reported by syzbot.\n \n Hitting a held lock here is expected for an externally owned socket and\n is not a kernel bug, so skip reclassification silently instead of\n warning. Reclassification is a lockdep-only annotation, so skipping it\n in the rare racing case is harmless.\n \n Reported-by: syzbot+6b85d1e39a5b8ed9a954@syzkaller.appspotmail.com\n Closes: https://syzkaller.appspot.com/bug?extid=6b85d1e39a5b8ed9a954\n Fixes: d532cddb6c60 (\"nbd: Reclassify sockets to avoid lockdep circular dependency\")\n Signed-off-by: Deepanshu Kartikey \u003ckartikey406@gmail.com\u003e\n Acked-by: Eric Dumazet \u003cedumazet@google.com\u003e\n Link: https://patch.msgid.link/20260621235255.66015-1-kartikey406@gmail.com\n Signed-off-by: Jens Axboe \u003caxboe@kernel.dk\u003e\n\ndiff --git a/drivers/block/nbd.c b/drivers/block/nbd.c\nindex 3a585a0c882a..8f10762e90ef 100644\n--- a/drivers/block/nbd.c\n+++ b/drivers/block/nbd.c\n@@ -1246,7 +1246,7 @@ static void nbd_reclassify_socket(struct socket *sock)\n {\n \tstruct sock *sk = sock-\u003esk;\n \n-\tif (WARN_ON_ONCE(!sock_allow_reclassification(sk)))\n+\tif (!sock_allow_reclassification(sk))\n \t\treturn;\n \n \tswitch (sk-\u003esk_family) {\n"}
Tool git-show returned: {"Output":"commit 553d28925fdea4044bad4ae1e2bf9484ad21f758\nAuthor: Guilherme Giacomo Simoes \u003ctrintaeoitogc@gmail.com\u003e\nDate: Thu Nov 12 07:59:39 2026 -0300\n\n nbd: Reclassify sockets in the reconnect path\n \n Commit d532cddb6c60 (\"nbd: Reclassify sockets to avoid lockdep circular\n dependency\") introduced nbd_reclassify_socket() to prevent lockdep false\n positives when NBD sockets are used in memory reclaim paths. While\n nbd_add_socket() reclassifies newly added sockets,\n nbd_genl_reconnect_sock_cb() omitted this call when replacing dead\n sockets during reconnection. Consequently, reconnected sockets retain\n the generic socket lockdep class and trigger false-positive lockdep\n reports.\n \n Fix this by reclassifying the replacement socket in\n nbd_genl_reconnect_sock_cb().\n \n Fixes: d532cddb6c60 (\"nbd: Reclassify sockets to avoid lockdep circular dependency\")\n Reported-by: syzbot+d7d72f3d467710442af0@syzkaller.appspotmail.com\n Closes: https://syzkaller.appspot.com/bug?extid=d7d72f3d467710442af0\n Signed-off-by: Guilherme Giacomo Simoes \u003ctrintaeoitogc@gmail.com\u003e\n Reviewed-by: Bart Van Assche \u003cbvanassche@acm.org\u003e\n Link: https://patch.msgid.link/20261112105939.260856-1-trintaeoitogc@gmail.com\n Signed-off-by: Jens Axboe \u003caxboe@kernel.dk\u003e\n\ndiff --git a/drivers/block/nbd.c b/drivers/block/nbd.c\nindex ffce519bf008..b2b27f90a7ed 100644\n--- a/drivers/block/nbd.c\n+++ b/drivers/block/nbd.c\n@@ -1356,6 +1356,7 @@ static int nbd_genl_reconnect_sock_cb(struct nbd_device *nbd, unsigned long arg)\n \tsock = nbd_get_socket(nbd, arg, \u0026err);\n \tif (!sock)\n \t\treturn err;\n+\tnbd_reclassify_socket(sock);\n \n \targs = kzalloc_obj(*args);\n \tif (!args) {\n"}
</execution_history>
Provide guidance for the agent based on the history above.
Call set-results with Advice.
|
| 103/3 |
2026/12/01 13:26 |
llm |
cause-finder-advisor |
7m
Model:
Tokens:
input: 153582
output: 740
thoughts: 33174
Thoughts: **My Diagnostic Journey**
I've been carefully analyzing this lockdep issue, and my thought process has led me to a clear conclusion. Initially, I considered several potential culprit commits. I reviewed the block-layer lockdep commits, like `f1be1788a32e` and `ffa1e7ada456`. I quickly dismissed these because they were simply adding valid lockdep annotations, and the lock orderings they introduced for btrfs and the elevator are standard for block devices; they weren't the source of the problem I'm seeing. The `fs_reclaim` commit wasn't even part of that particular chain, further indicating it wasn't the root cause.
Next, I focused on a pair of commits: `d532cddb6c60` and its follow-up `553d28925fde`. These commits introduced the `nbd_reclassify_socket()` function. The intention here was to prevent NBD sockets from sharing the generic `sk_lock-AF_INET`, treating any situation where `!sock_allow_reclassification(sk)` was true as an unexpected condition, issuing a `WARN_ON_ONCE`. This is where things started to get interesting.
The critical turning point, however, was commit `9280e6edf65662b6aafc8b704ad065b54c08b519`. This is the commit that syzbot specifically hit when it encountered a race condition on user-owned NBD sockets. Instead of implementing a robust solution like rejecting the reclassification or synchronizing busy sockets, this commit made a deliberate choice to *silently skip* the reclassification. The rationale provided was that "Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless." This, in my expert opinion, is a flawed assumption. By silently skipping the reclassification for busy NBD sockets, this commit directly leaves them with the generic `sk_lock-AF_INET`, creating the exact circular locking dependency that is causing this splat.
Therefore, my conclusion is firm: `9280e6edf65662b6aafc8b704ad065b54c08b519` is the single commit responsible for this problem. I've analyzed enough; it's time to halt further commit exploration and immediately call `set-results` with this commit as the sole identified culprit. Now, I'll provide the concise and actionable advice.
|
| 104/3 |
2026/12/01 13:33 |
tool |
set-results |
0m
Args: map[Advice:You have already verified the root cause in `drivers/block/nbd.c` and confirmed the relevant commits with `git-show`. Stop further searching and call `set-results` immediately.
### Validation of Findings
1. **Mechanism confirmed in `drivers/block/nbd.c`**:
- Every path that assigns `nsock->sock` (`nbd_add_socket()` and, since `553d28925fde`, `nbd_genl_reconnect_sock_cb()`) calls `nbd_reclassify_socket(sock)`, and nothing resets `sk->sk_lock` afterward.
- In the crash trace, link `#7` (`recv_work()` -> `nbd_mark_nsock_dead()` -> `inet_shutdown()`) acquires `sk_lock-AF_INET` rather than `sk_lock-AF_INET-NBD`. For an `AF_INET` TCP socket, this only happens when `nbd_reclassify_socket()` hits `if (!sock_allow_reclassification(sk)) return;` because userspace or softirq concurrently holds `sk->sk_lock` / `sk->sk_lock.slock`.
2. **Primary culprit commit — `9280e6edf65662b6aafc8b704ad065b54c08b519` ("nbd: don't warn when reclassifying a busy socket lock")**:
- Do not blame block-layer lockdep annotation commits (`f1be1788a32e`, or `ffa1e7ada456` whose `fs_reclaim` priming is not even part of this dependency chain), nor valid btrfs/elevator lock orderings.
- `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency") added `nbd_reclassify_socket()` to isolate NBD sockets from generic `sk_lock-AF_INET`, asserting `WARN_ON_ONCE(!sock_allow_reclassification(sk))` under the `nvme-tcp` assumption that the socket lock is unheld.
- When syzbot hit that race on user-supplied NBD sockets, `9280e6edf65662b6aafc8b704ad065b54c08b519` specifically handled the busy-socket case by silently skipping reclassification instead of rejecting (`-EBUSY`) or synchronizing the socket, under the false assumption stated in its commit log: *"Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless."* Allowing NBD to proceed with an unreclassified `sk_lock-AF_INET` socket directly causes this lockdep circular dependency splat.
### Next Step
Call `set-results` now with **only** `9280e6edf65662b6aafc8b704ad065b54c08b519` and a clear explanation of why silently skipping socket reclassification when `!sock_allow_reclassification(sk)` leaves `nsock->sock->sk` in `sk_lock-AF_INET` and triggers the circular locking dependency.]
Results: map[Advice:You have already verified the root cause in `drivers/block/nbd.c` and confirmed the relevant commits with `git-show`. Stop further searching and call `set-results` immediately.
### Validation of Findings
1. **Mechanism confirmed in `drivers/block/nbd.c`**:
- Every path that assigns `nsock->sock` (`nbd_add_socket()` and, since `553d28925fde`, `nbd_genl_reconnect_sock_cb()`) calls `nbd_reclassify_socket(sock)`, and nothing resets `sk->sk_lock` afterward.
- In the crash trace, link `#7` (`recv_work()` -> `nbd_mark_nsock_dead()` -> `inet_shutdown()`) acquires `sk_lock-AF_INET` rather than `sk_lock-AF_INET-NBD`. For an `AF_INET` TCP socket, this only happens when `nbd_reclassify_socket()` hits `if (!sock_allow_reclassification(sk)) return;` because userspace or softirq concurrently holds `sk->sk_lock` / `sk->sk_lock.slock`.
2. **Primary culprit commit — `9280e6edf65662b6aafc8b704ad065b54c08b519` ("nbd: don't warn when reclassifying a busy socket lock")**:
- Do not blame block-layer lockdep annotation commits (`f1be1788a32e`, or `ffa1e7ada456` whose `fs_reclaim` priming is not even part of this dependency chain), nor valid btrfs/elevator lock orderings.
- `d532cddb6c60` ("nbd: Reclassify sockets to avoid lockdep circular dependency") added `nbd_reclassify_socket()` to isolate NBD sockets from generic `sk_lock-AF_INET`, asserting `WARN_ON_ONCE(!sock_allow_reclassification(sk))` under the `nvme-tcp` assumption that the socket lock is unheld.
- When syzbot hit that race on user-supplied NBD sockets, `9280e6edf65662b6aafc8b704ad065b54c08b519` specifically handled the busy-socket case by silently skipping reclassification instead of rejecting (`-EBUSY`) or synchronizing the socket, under the false assumption stated in its commit log: *"Reclassification is a lockdep-only annotation, so skipping it in the rare racing case is harmless."* Allowing NBD to proceed with an unreclassified `sk_lock-AF_INET` socket directly causes this lockdep circular dependency splat.
### Next Step
Call `set-results` now with **only** `9280e6edf65662b6aafc8b704ad065b54c08b519` and a clear explanation of why silently skipping socket reclassification when `!sock_allow_reclassification(sk)` leaves `nsock->sock->sk` in `sk_lock-AF_INET` and triggers the circular locking dependency.]
|
| 105/2 |
2026/12/01 13:33 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 63518
output: 57
thoughts: 72
Thoughts: **My Bisection Process and Findings**
I've meticulously gone through the source code and reviewed the entire git history. My objective was to pinpoint the exact commit responsible for a particular change, and I'm confident in the results. To that end, I've initiated the `set-results` command with the commit hash `["9280e6edf65662b6aafc8b704ad065b54c08b519"]`. I've also generated a bisection log that I believe is clear and accurately reflects the process and the identified commit.
|
| 106/2 |
2026/12/01 13:34 |
tool |
set-results |
0m
Args: map[CandidateCommits:[9280e6edf65662b6aafc8b704ad065b54c08b519]]
Results: map[CandidateCommits:[9280e6edf65662b6aafc8b704ad065b54c08b519]]
|
| 107/2 |
2026/12/01 13:34 |
llm |
cause-finder |
2m
Model:
Tokens:
input: 63700
output: 1411
thoughts: 6
Thoughts: **My Process of Understanding and Synthesizing Information**
My primary objective here is to engage with and deeply understand the provided text. I'm not just looking to extract keywords or superficial meanings; I'm aiming for a thorough comprehension of the underlying concepts, the author's intent, and the nuances of the argument presented. My approach is multi-layered, beginning with a careful reading to grasp the literal meaning of each sentence and paragraph.
As I read, I'm actively identifying the core subject matter. What is the central theme or topic being discussed? Simultaneously, I'm looking for the main argument or thesis the author is trying to convey. What point are they ultimately trying to make? This involves dissecting the structure of the text, understanding how different ideas connect and build upon each other. I'm also paying close attention to the evidence or reasoning provided to support the main argument, evaluating its strength and relevance.
Beyond the explicit statements, I'm considering the implicit messages and undertones. Is there any underlying bias, assumption, or perspective that colors the author's presentation? Understanding the context in which the text was created is also crucial – who is the intended audience, and what is the author's likely purpose in writing it? This contextual awareness helps me to interpret the text more accurately and avoid misinterpretations.
Once I have a solid grasp of the content and its implications, my next step is to synthesize this understanding. This involves distilling the essential information into a concise yet comprehensive summary. I'm focusing on capturing the essence of the text without getting bogged down in minor details. This distillation process requires me to identify the most critical points, the key takeaways, and the overarching message.
The final stage of my process is to articulate this synthesized understanding in a manner that is clear, coherent, and reflective of the original text's core message. I'm aiming for a summary that not only accurately represents the information but also demonstrates a deep level of engagement with it. I want to ensure that the summary is both informative and insightful, providing a valuable distillation of the original work for myself and, in this instance, for the user.
|
| 108/1 |
2026/12/01 13:36 |
action |
format-bisect |
0m
Results: map[Commits:[map[AuthorEmail:kartikey406@gmail.com AuthorName:Deepanshu Kartikey Date:2026-06-22T05:22:55+05:30 Hash:9280e6edf65662b6aafc8b704ad065b54c08b519 Recipients:[map[Email:axboe@kernel.dk Name: To:true] map[Email:edumazet@google.com Name: To:true] map[Email:kartikey406@gmail.com Name: To:true]] Title:nbd: don't warn when reclassifying a busy socket lock]] IsRelease:false]
|