Root Cause Analysis
The lockdep warning reports an 11-lock circular dependency cycle between btrfs-tree-01 and btrfs-tree-00 spanning Btrfs, the block layer, NBD, TCP sockets, and mm->mmap_lock. The primary defect causing this cycle is in drivers/block/nbd.c, where nbd_reclassify_socket() silently skips lockdep class reclassification when the user-supplied socket is busy, leaving it in the generic sk_lock-AF_INET class.
1. Silent bypass of socket lockdep reclassification in NBD (drivers/block/nbd.c)
To avoid lockdep cycles between block-layer I/O paths and normal userspace socket operations (sk_lock-AF_INET -> &mm->mmap_lock -> filesystem/block I/O), nbd_add_socket() (drivers/block/nbd.c:1299) and nbd_genl_reconnect_sock_cb() (drivers/block/nbd.c:1359) call nbd_reclassify_socket() (drivers/block/nbd.c:1245-1272) to move sock->sk->sk_lock to the NBD-specific sk_lock-AF_INET-NBD class (nbd_key[0]).
However, nbd_reclassify_socket() begins with:
if (!sock_allow_reclassification(sk))
return;
where sock_allow_reclassification() (include/net/sock.h:1835-1841) checks !sock_owned_by_user_nocheck(sk) && !spin_is_locked(&sk->sk_lock.slock). Because nbd_get_socket() (drivers/block/nbd.c:1213-1239) obtains struct socket * via sockfd_lookup() from a userspace file descriptorβand userspace retains that descriptorβa concurrent syscall on the same fd or softirq BH packet processing (bh_lock_sock()) can hold the socket lock when nbd_reclassify_socket() runs. Instead of synchronizing or rejecting the busy socket, nbd_reclassify_socket() returns early and NBD attaches the socket while its sk_lock remains in the generic sk_lock-AF_INET lockdep class.
2. How the full lockdep cycle is formed
Once NBD uses an unreclassified sk_lock-AF_INET socket, lockdep connects the block/filesystem stack back to mm->mmap_lock and Btrfs:
btrfs-tree-01 -> btrfs-tree-00 (#0): During openat() -> btrfs_lookup() -> btrfs_search_slot() (fs/btrfs/ctree.c:2224), Btrfs descends from a level-1 node (btrfs-tree-01) and read-locks a level-0 leaf (btrfs-tree-00) via btrfs_tree_read_lock() (fs/btrfs/locking.c:146).
btrfs-tree-00 -> &q->q_usage_counter(io)#17 (#1): In read_block_for_search() (fs/btrfs/ctree.c:1518-1525, 1563-1569), Btrfs acquires btrfs_tree_read_lock(tmp) on the level-0 extent buffer (btrfs-tree-00), releases the parent path (btrfs_release_path(p)), and then performs synchronous metadata I/O via btrfs_read_extent_buffer() -> read_extent_buffer_pages() -> btrfs_submit_bbio() -> blk_mq_submit_bio() -> bio_queue_enter() (block/blk.h:90-100), which acquires q->io_lockdep_map (&q->q_usage_counter(io)#17).
&q->q_usage_counter(io)#17 -> &q->elevator_lock (#2): elv_iosched_store() -> elevator_change() (block/elevator.c:668-681) freezes the queue (blk_mq_freeze_queue(q), acquiring q->io_lockdep_map as writer) and then acquires mutex_lock(&q->elevator_lock). Note that &q->elevator_lock shares a single static lockdep class across all request_queue instances, bridging loop8 (#17) and nbd.
&q->elevator_lock -> set->srcu (#3): During nbd_dev_add() (drivers/block/nbd.c:2027) -> add_disk() -> blk_register_queue() -> elevator_set_default() -> elevator_change() -> elevator_switch() (block/elevator.c:576), blk_mq_quiesce_queue() calls synchronize_srcu(set->srcu) (block/blk-mq.c:284) while holding &q->elevator_lock. All BLK_MQ_F_BLOCKING tag sets initialized via blk_mq_alloc_tag_set() (block/blk-mq.c:4957) share the same set->srcu lockdep class.
set->srcu -> &cmd->lock -> &nsock->tx_lock -> sk_lock-AF_INET (#4β#6):
blk_mq_run_work_fn() (block/blk-mq.c:2555) enters blk_mq_run_dispatch_ops() (srcu_read_lock(set->srcu), block/blk-mq.h:448) and dispatches via nbd_queue_rq() (drivers/block/nbd.c:1199), acquiring &cmd->lock.
nbd_handle_cmd() (drivers/block/nbd.c:1143) acquires &nsock->tx_lock.
recv_work() -> nbd_mark_nsock_dead() (drivers/block/nbd.c:1029-1030, 319) holds &nsock->tx_lock and calls kernel_sock_shutdown() -> inet_shutdown() -> lock_sock() (net/ipv4/af_inet.c:913), recording &nsock->tx_lock -> sk_lock-AF_INET because reclassification was skipped.
sk_lock-AF_INET -> &mm->mmap_lock (#7): tcp_sendmsg_locked() (net/ipv4/tcp.c:1324) holds sk_lock-AF_INET while calling _copy_from_iter() -> __might_fault(), establishing sk_lock-AF_INET -> &mm->mmap_lock.
&mm->mmap_lock -> btrfs_trans_num_writers -> btrfs_trans_num_extwriters -> btrfs-tree-01 (#8β#10):
mmap_region() holds &mm->mmap_lock and calls btrfs_file_mmap_prepare() (fs/btrfs/file.c:2010) -> file_accessed() -> touch_atime() -> btrfs_dirty_inode() (fs/btrfs/inode.c:6386, 6400), entering a Btrfs transaction (btrfs_trans_num_writers, btrfs_trans_num_extwriters) and releasing sb_end_intwrite() before the transaction lockdep maps in __btrfs_end_transaction() (fs/btrfs/transaction.c:1124, 1140-1141).
btrfs_fallocate() (fs/btrfs/file.c:3126, 3162) -> btrfs_wait_ordered_range() -> __cow_file_range_inline() (fs/btrfs/inode.c:624, 638) -> btrfs_drop_extents() -> btrfs_search_slot() locks btrfs-tree-01 (btrfs_read_lock_root_node()) while holding btrfs_trans_num_extwriters, completing the cycle.
3. Cross-device lockdep aliasing vs. single-socket shared-fd deadlock
In the reported trace, the cycle bridges unrelated block devices (loop8 and nbd) via &q->elevator_lock and set->srcu, and bridges an NBD socket with a regular TCP socket via the unreclassified sk_lock-AF_INET. However, because NBD does not take exclusive ownership of the user-supplied socket fd (both nbd_ioctl() and nbd_genl_connect() gate access with CAP_SYS_ADMIN, and userspace retains the fd), a privileged user can also construct a real single-socket deadlock by attaching a TCP socket fd to NBD and writing to that same fd from an mmap()ed buffer backed by a filesystem mounted on that NBD device.