Skip to content

Commit d532cdd

Browse files
edumazetaxboe
authored andcommitted
nbd: Reclassify sockets to avoid lockdep circular dependency
syzbot reported a possible circular locking dependency in udp_sendmsg() where fs_reclaim can be triggered while holding sk_lock, and fs_reclaim can eventually depend on another sk_lock (e.g., if NBD is used for swap or writeback and NBD uses TLS/TCP which acquires sk_lock). Since the UDP socket and the NBD TCP/TLS socket are different, this is a false positive. Fix this by reclassifying NBD sockets to a separate lock class when they are added to the NBD device. This is similar to what nvme-tcp and other network block devices do. Fixes: ffa1e7a ("block: Make request_queue lockdep splats show up earlier") Reported-by: syzbot+607cdcf978b3e79da878@syzkaller.appspotmail.com Closes: https://lore.kernel.org/netdev/6a2cdafe.428ffe26.258b27.0161.GAE@google.com/T/#u Signed-off-by: Eric Dumazet <edumazet@google.com> Link: https://patch.msgid.link/20260613042619.1108126-1-edumazet@google.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent e8dcf2d commit d532cdd

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

drivers/block/nbd.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,42 @@ static struct socket *nbd_get_socket(struct nbd_device *nbd, unsigned long fd,
12381238
return sock;
12391239
}
12401240

1241+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
1242+
static struct lock_class_key nbd_key[3];
1243+
static struct lock_class_key nbd_slock_key[3];
1244+
1245+
static void nbd_reclassify_socket(struct socket *sock)
1246+
{
1247+
struct sock *sk = sock->sk;
1248+
1249+
if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
1250+
return;
1251+
1252+
switch (sk->sk_family) {
1253+
case AF_INET:
1254+
sock_lock_init_class_and_name(sk, "slock-AF_INET-NBD",
1255+
&nbd_slock_key[0],
1256+
"sk_lock-AF_INET-NBD",
1257+
&nbd_key[0]);
1258+
break;
1259+
case AF_INET6:
1260+
sock_lock_init_class_and_name(sk, "slock-AF_INET6-NBD",
1261+
&nbd_slock_key[1],
1262+
"sk_lock-AF_INET6-NBD",
1263+
&nbd_key[1]);
1264+
break;
1265+
case AF_UNIX:
1266+
sock_lock_init_class_and_name(sk, "slock-AF_UNIX-NBD",
1267+
&nbd_slock_key[2],
1268+
"sk_lock-AF_UNIX-NBD",
1269+
&nbd_key[2]);
1270+
break;
1271+
}
1272+
}
1273+
#else
1274+
static inline void nbd_reclassify_socket(struct socket *sock) {}
1275+
#endif
1276+
12411277
static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
12421278
bool netlink)
12431279
{
@@ -1254,6 +1290,7 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
12541290
sock = nbd_get_socket(nbd, arg, &err);
12551291
if (!sock)
12561292
return err;
1293+
nbd_reclassify_socket(sock);
12571294

12581295
/*
12591296
* We need to make sure we don't get any errant requests while we're

0 commit comments

Comments
 (0)