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:
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:
-
&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).
-
&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).
-
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.