Root Cause Analysis
The circular locking dependency reported by lockdep originates in drivers/block/nbd.c in how NBD acquires and reclassifies userspace-provided sockets.
1. Silent Failure of Lockdep Socket Reclassification (nbd_reclassify_socket())
Because NBD is a block driver, its socket lock (sk->sk_lock) can be acquired from the block-layer dispatch path and memory reclaim (fs_reclaim), whereas normal userspace sockets can acquire mm->mmap_lock while holding sk->sk_lock. To separate NBD sockets from general userspace sockets in lockdep, nbd_add_socket() (drivers/block/nbd.c:1277) calls nbd_reclassify_socket(sock) (drivers/block/nbd.c:1245-1272):
static void nbd_reclassify_socket(struct socket *sock)
{
struct sock *sk = sock->sk;
if (!sock_allow_reclassification(sk))
return;
...
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;
Unlike in-kernel socket callers (such as nvme_tcp_reclassify_socket() or svc_reclassify_socket()), NBD obtains sock from a userspace file descriptor via nbd_get_socket() -> sockfd_lookup() (drivers/block/nbd.c:1219). Userspace can retain access to the same socket (or incoming packets can trigger bh_lock_sock() in softirq context), so sk->sk_lock.owned or sk->sk_lock.slock may be held when nbd_reclassify_socket() runs.
When that occurs:
sock_allow_reclassification(sk) (include/net/sock.h:1861) returns false.
nbd_reclassify_socket() silently returns early without reclassifying sk->sk_lock.dep_map (and without failing nbd_add_socket()), leaving the NBD socket in the default sk_lock-AF_INET6 lockdep class instead of sk_lock-AF_INET6-NBD.
- (Note also that
nbd_genl_reconnect_sock_cb() at drivers/block/nbd.c:1348 does not call nbd_reclassify_socket() at all, and calling sock_lock_init_class_and_name() without holding a lock against concurrent lock_sock()/bh_lock_sock() is itself racy under CONFIG_DEBUG_LOCK_ALLOC because sock_lock_init_class_and_name() reinitializes sk->sk_lock.owned, wq, slock, and dep_map.)
2. How the Dependency Cycle Forms
Once the NBD socket remains in the sk_lock-AF_INET6 class, the following lock chain is recorded:
&mm->mmap_lock -> fs_reclaim: Normal GFP_KERNEL allocations performed under mmap_lock (e.g., insert_vm_struct() -> vma_link() -> mas_preallocate() -> kmem_cache_alloc_noprof()).
fs_reclaim -> &q->q_usage_counter(io): Annotated in blk_alloc_queue() (block/blk-core.c:504-507), because memory reclaim submits I/O while blk_mq_freeze_queue() waits on q->q_usage_counter (acquiring q->io_lockdep_map).
&q->q_usage_counter(io) -> &q->elevator_lock -> set->srcu: During nbd_start_device() -> blk_mq_update_nr_hw_queues() (drivers/block/nbd.c:1530) -> elevator_change() (block/elevator.c:668-681), blk_mq_freeze_queue(q) is held across mutex_lock(&q->elevator_lock), and elevator_switch() (block/elevator.c:576) calls blk_mq_quiesce_queue(q) -> synchronize_srcu(set->srcu) (since nbd_dev_add() sets BLK_MQ_F_BLOCKING).
set->srcu -> &cmd->lock -> &nsock->tx_lock -> sk_lock-AF_INET6: Block dispatch runs under srcu_read_lock and calls nbd_queue_rq() (drivers/block/nbd.c:1199) -> mutex_lock(&cmd->lock) -> nbd_handle_cmd() (drivers/block/nbd.c:1143) -> mutex_lock(&nsock->tx_lock). While holding &nsock->tx_lock, both nbd_send_cmd() -> sock_xmit() and sock_shutdown() -> nbd_mark_nsock_dead() (drivers/block/nbd.c:319, reached from nbd_genl_connect() -> nbd_config_put() -> nbd_clear_sock()) call into the socket layer (kernel_sock_shutdown() -> inet_shutdown() -> lock_sock(sk)), acquiring sk_lock-AF_INET6.
sk_lock-AF_INET6 -> &mm->mmap_lock: In tls_sw_sendmsg() (net/tls/tls_sw.c:1032), lock_sock(sk) (sk_lock-AF_INET6) is held while calling tls_sw_sendmsg_locked() (net/tls/tls_sw.c:889) -> sk_msg_zerocopy_from_iter() (net/core/skmsg.c:329) -> iov_iter_get_pages2() -> __iov_iter_get_pages_alloc() -> gup_fast_fallback() (mm/gup.c:3235), which invokes might_lock_read(¤t->mm->mmap_lock).
3. Underlying Socket-Ownership / Validation Issue
Beyond lockdep class aliasing across distinct AF_INET6 sockets, nbd_get_socket() (drivers/block/nbd.c:1213-1239) only checks sk_is_tcp(sock->sk) || sk_is_stream_unix(sock->sk) and sock->ops->shutdown != sock_no_shutdown. It does not prevent userspace from continuing to use the socket FD concurrently (e.g., via tls_sw_sendmsg() with TCP_ULP "tls" attached, or sendmsg() faulting on user buffers) after handing it to NBD, allowing the same sk_lock to participate in both userspace sk_lock -> mmap_lock and block-reclaim fs_reclaim -> ... -> nsock->tx_lock -> sk_lock paths.