Root Cause Analysis
The circular locking dependency originates in drivers/block/nbd.c because NBD accepts userspace socket file descriptors (NBD_SET_SOCK, NBD_CMD_CONNECT, and NBD_CMD_RECONNECT) and reclassifies their socket lock under the NBD lockdep class while userspace still retains access to the socket file descriptor.
1. Socket Reclassification and Continued Userspace Access (drivers/block/nbd.c)
nbd_add_socket() (called from __nbd_ioctl() for NBD_SET_SOCK and from nbd_genl_connect_sock_cb() for NBD_CMD_CONNECT) and nbd_genl_reconnect_sock_cb() (for NBD_CMD_RECONNECT) resolve a userspace file descriptor via nbd_get_socket() (sockfd_lookup()).
- Both paths call
nbd_reclassify_socket(), which invokes sock_lock_init_class_and_name() to assign the socket's sk->sk_lock to the global NBD lockdep class &nbd_key[0] ("sk_lock-AF_INET-NBD" for AF_INET, or &nbd_key[1]/&nbd_key[2] for AF_INET6/AF_UNIX).
- Unlike kernel-only socket users (e.g., NVMe/TCP, CIFS, or SunRPC), NBD does not prevent userspace from continuing to invoke socket syscalls on the retained file descriptor after it has been attached to NBD.
- When userspace calls
recvfrom()/recv() on an NBD-reclassified socket with a userspace buffer, tcp_recvmsg() (net/ipv4/tcp.c) acquires sk->sk_lock (lock_sock(sk), class sk_lock-AF_INET-NBD) and calls skb_copy_datagram_msg() -> _copy_to_iter() (lib/iov_iter.c) -> __might_fault() (mm/memory.c), establishing the dependency:
sk_lock-AF_INET-NBD -> &mm->mmap_lock (and allowing real page faults into userspace mappings while sk->sk_lock is held).
2. Complete Circular Dependency Chain
Combining this userspace socket dependency with standard VM, filesystem, block layer, and NBD locking creates the reported 9-link cycle:
&ei->i_mmap_lock -> &q->q_usage_counter(io):
In btrfs_fallocate() (fs/btrfs/file.c), btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_MMAP) (fs/btrfs/inode.c) acquires &ei->i_mmap_lock (struct btrfs_inode::i_mmap_lock) in write mode, then calls btrfs_wait_ordered_range() -> btrfs_fdatawrite_range() -> btrfs_writepages() -> btrfs_submit_bbio() -> blk_mq_submit_bio() (block/blk-mq.c) -> bio_queue_enter() (block/blk.h), acquiring &q->io_lockdep_map (&q->q_usage_counter(io)).
&q->q_usage_counter(io) -> &q->elevator_lock:
In elv_iosched_store() -> elevator_change() (block/elevator.c), blk_mq_freeze_queue() acquires &q->io_lockdep_map (freeze/write side via blk_freeze_acquire_lock()), then acquires mutex_lock(&q->elevator_lock).
&q->elevator_lock -> set->srcu:
elevator_change() calls elevator_switch() -> blk_mq_quiesce_queue() -> blk_mq_wait_quiesce_done() -> __synchronize_srcu() on q->tag_set->srcu while holding &q->elevator_lock.
set->srcu -> &cmd->lock:
Queue dispatch (blk_mq_run_work_fn() -> blk_mq_sched_dispatch_requests() -> blk_mq_dispatch_rq_list() -> nbd_queue_rq() in drivers/block/nbd.c) executes under set->srcu read-side protection and acquires mutex_lock(&cmd->lock) (struct nbd_cmd::lock).
&cmd->lock -> sk_lock-AF_INET-NBD:
On the receive path (recv_work() -> nbd_handle_reply() in drivers/block/nbd.c), nbd_handle_reply() acquires mutex_lock(&cmd->lock) and calls sock_xmit() -> __sock_xmit() -> sock_recvmsg() -> tcp_recvmsg() -> lock_sock(sk) (sk_lock-AF_INET-NBD).
sk_lock-AF_INET-NBD -> &mm->mmap_lock:
Recorded when userspace calls recv() on an NBD-reclassified socket (__sys_recvfrom() -> tcp_recvmsg() -> skb_copy_datagram_msg() -> _copy_to_iter() -> __might_fault()).
&mm->mmap_lock -> vm_lock:
mprotect_fixup() (mm/mprotect.c) holds &mm->mmap_lock and calls vma_start_write() -> __vma_start_write() -> __vma_start_exclude_readers() (mm/mmap_lock.c).
vm_lock -> sb_pagefaults -> &ei->i_mmap_lock:
On a write page fault to a shared writable mmap() of a Btrfs file, do_user_addr_fault() (arch/x86/mm/fault.c) acquires the per-VMA read lock (vma->vm_lock via lock_vma_under_rcu()) and invokes handle_mm_fault() -> do_page_mkwrite() -> btrfs_page_mkwrite() (fs/btrfs/file.c), which calls sb_start_pagefault() (sb_pagefaults) and down_read(&inode->i_mmap_lock) (&ei->i_mmap_lock).
3. Underlying Invariant Violation
Because NBD is a block driver sitting below filesystem and memory-management locks (mmap_lock / vm_lock -> sb_pagefaults -> i_mmap_lock -> q_usage_counter -> elevator_lock -> set->srcu -> cmd->lock -> sk_lock), its socket lock (sk_lock) must never be acquired above mm->mmap_lock. Leaving a reclassified NBD socket accessible to userspace syscalls (and sharing a global nbd_key lockdep class across all AF_INET NBD sockets) violates this invariant: even without the scheduler-switch links, a userspace recv()/send() on the active NBD socket into a shared mmap() buffer backed by the mounted Btrfs filesystem on that NBD device can fault into btrfs_page_mkwrite() (&ei->i_mmap_lock) while holding sk->sk_lock, deadlocking against btrfs_fallocate() holding &ei->i_mmap_lock and waiting on NBD I/O.