Root Cause Analysis
Although the lockdep warning triggers in mfill_atomic_hugetlb() (mm/userfaultfd.c:841), the underlying defect is in drivers/block/nbd.c, where NBD fails to reliably isolate its socket lock class (sk_lock) from general userspace socketsβand allows userspace to retain and use the socket file descriptor concurrently with block I/O.
1. Silent Failure of Socket Lockdep Reclassification in drivers/block/nbd.c
NBD accepts userspace socket file descriptors via NBD_SET_SOCK / NBD_CMD_CONNECT (nbd_add_socket() at drivers/block/nbd.c:1277) and NBD_CMD_RECONNECT (nbd_genl_reconnect_sock_cb() at drivers/block/nbd.c:1348). Both functions call nbd_get_socket() (sockfd_lookup(fd, err)) and then nbd_reclassify_socket(sock) (drivers/block/nbd.c:1245):
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;
...
sock_allow_reclassification(sk) (include/net/sock.h:1835) returns false whenever:
sock_owned_by_user_nocheck(sk) is true (another userspace thread holds lock_sock(sk) via a syscall on the socket fd), or
spin_is_locked(&sk->sk_lock.slock) is true (softirq/BH is processing a packet on sk).
Unlike kernel-only socket users (e.g., nvme-tcp, sunrpc), nbd operates on a userspace-supplied fd that may be actively locked by another thread or by incoming TCP traffic at the moment nbd_add_socket() or nbd_genl_reconnect_sock_cb() runs. When sock_allow_reclassification(sk) is false, nbd_reclassify_socket() silently returns without reclassifying sk, and the caller attaches the socket to nsock->sock anyway with its original sk_lock-AF_INET lockdep class.
2. How the Circular Dependency Forms
Once an nbd socket retains the global sk_lock-AF_INET class, the block/NBD locking hierarchy becomes transitively linked to mm->mmap_lock and fs_reclaim:
-
fs_reclaim β &q->q_usage_counter(io) β &q->elevator_lock β set->srcu β &cmd->lock β &nsock->tx_lock:
-
&nsock->tx_lock β sk_lock-AF_INET:
-
sk_lock-AF_INET β &mm->mmap_lock:
- Standard user-space
tcp_sendmsg() / tcp_sendmsg_locked() (net/ipv4/tcp.c:1324) holds sk_lock-AF_INET while copying from user iterators (skb_copy_to_page_nocache() β _copy_from_iter() β __might_fault()), recording sk_lock-AF_INET β &mm->mmap_lock.
-
&mm->mmap_lock β &hugetlb_fault_mutex_table[i] β &resv_map->rw_sema β fs_reclaim:
hugetlb_fault() (mm/hugetlb.c:6165-6173, called under mm->mmap_lock from mm_populate() / page faults) acquires &hugetlb_fault_mutex_table[hash], then hugetlb_vma_lock_read(vma) (which takes &resv_map->rw_sema on private hugetlb VMAs at mm/hugetlb.c:318), and then allocates page tables via huge_pte_alloc() (GFP_KERNEL, triggering fs_reclaim_acquire()).
mfill_atomic_hugetlb() (mm/userfaultfd.c:840-841) uses the same locking order (&hugetlb_fault_mutex_table[hash] β hugetlb_vma_lock_read(dst_vma)), closing the cycle when lockdep checks &hugetlb_fault_mutex_table[i] β &resv_map->rw_sema.
3. Underlying Lifetime / Ownership Issue
Furthermore, even if nbd_reclassify_socket() succeeds during setup, userspace retains its file descriptor to the socket and can invoke sendmsg()/write() on that same struct socket concurrently with NBD I/O and shutdown (nbd_mark_nsock_dead() holding nsock->tx_lock across kernel_sock_shutdown()), allowing the sk->sk_lock β mm->mmap_lock β fs_reclaim β NBD I/O / nsock->tx_lock β sk->sk_lock cycle to occur on the same socket instance.