Skip to contentSkip to conversation

slub: Don't call lockdep_unregister_key() for immature kmem_cache.

PATCH In review Kuniyuki Iwashima 4 participants 5 messages Oct 7, 2025 – Oct 13, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 slub: Don't call lockdep_unregister_key() for immature kmem_cache.

syzbot reported the lockdep splat below in __kmem_cache_release(). [0]

The problem is that __kmem_cache_release() could be called from do_kmem_cache_create() before init_kmem_cache_cpus() registers the lockdep key.

Let's move lockdep_unregister_key() from __kmem_cache_release() to slab_kmem_cache_release() and do_kmem_cache_create().

[0]:

WARNING: CPU: 1 PID: 6128 at kernel/locking/lockdep.c:6606 lockdep_unregister_key+0x2ca/0x310 kernel/locking/lockdep.c:6606
Modules linked in:
CPU: 1 UID: 0 PID: 6128 Comm: syz.4.21 Not tainted syzkaller #0 PREEMPT_{RT,(full)}
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 08/18/2025
RIP: 0010:lockdep_unregister_key+0x2ca/0x310 kernel/locking/lockdep.c:6606
Code: 50 e4 0f 48 3b 44 24 10 0f 84 26 fe ff ff e8 bd cd 17 09 e8 e8 ce 17 09 41 f7 c7 00 02 00 00 74 bd fb 40 84 ed 75 bc eb cd 90 <0f> 0b 90 e9 19 ff ff ff 90 0f 0b 90 e9 2a ff ff ff 48 c7 c7 d0 ac
RSP: 0018:ffffc90003e870d0 EFLAGS: 00010002
RAX: eb1525397f5bdf00 RBX: ffff88803c121148 RCX: 1ffff920007d0dfc
RDX: 0000000000000000 RSI: ffffffff8acb1500 RDI: ffffffff8b1dd0e0
RBP: 00000000ffffffea R08: ffffffff8eb5aa37 R09: 1ffffffff1d6b546
R10: dffffc0000000000 R11: fffffbfff1d6b547 R12: 0000000000000000
R13: ffff88814d1b8900 R14: 0000000000000000 R15: 0000000000000203
FS:  00007f773f75e6c0(0000) GS:ffff88812712f000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007ffdaea3af52 CR3: 000000003a5ca000 CR4: 00000000003526f0
Call Trace:
 <TASK>
 __kmem_cache_release+0xe3/0x1e0 mm/slub.c:7696
 do_kmem_cache_create+0x74e/0x790 mm/slub.c:8575
 create_cache mm/slab_common.c:242 [inline]
 __kmem_cache_create_args+0x1ce/0x330 mm/slab_common.c:340
 nfsd_file_cache_init+0x1d6/0x530 fs/nfsd/filecache.c:816
 nfsd_startup_generic fs/nfsd/nfssvc.c:282 [inline]
 nfsd_startup_net fs/nfsd/nfssvc.c:377 [inline]
 nfsd_svc+0x393/0x900 fs/nfsd/nfssvc.c:786
 nfsd_nl_threads_set_doit+0x84a/0x960 fs/nfsd/nfsctl.c:1639
 genl_family_rcv_msg_doit+0x212/0x300 net/netlink/genetlink.c:1115
 genl_family_rcv_msg net/netlink/genetlink.c:1195 [inline]
 genl_rcv_msg+0x60e/0x790 net/netlink/genetlink.c:1210
 netlink_rcv_skb+0x208/0x470 net/netlink/af_netlink.c:2552
 genl_rcv+0x28/0x40 net/netlink/genetlink.c:1219
 netlink_unicast_kernel net/netlink/af_netlink.c:1320 [inline]
 netlink_unicast+0x846/0xa10 net/netlink/af_netlink.c:1346
 netlink_sendmsg+0x805/0xb30 net/netlink/af_netlink.c:1896
 sock_sendmsg_nosec net/socket.c:727 [inline]
 __sock_sendmsg+0x219/0x270 net/socket.c:742
 ____sys_sendmsg+0x508/0x820 net/socket.c:2630
 ___sys_sendmsg+0x21f/0x2a0 net/socket.c:2684
 __sys_sendmsg net/socket.c:2716 [inline]
 __do_sys_sendmsg net/socket.c:2721 [inline]
 __se_sys_sendmsg net/socket.c:2719 [inline]
 __x64_sys_sendmsg+0x1a1/0x260 net/socket.c:2719
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xfa/0x3b0 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f77400eeec9
Code: ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 a8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f773f75e038 EFLAGS: 00000246 ORIG_RAX: 000000000000002e
RAX: ffffffffffffffda RBX: 00007f7740345fa0 RCX: 00007f77400eeec9
RDX: 0000000000008004 RSI: 0000200000000180 RDI: 0000000000000006
RBP: 00007f7740171f91 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007f7740346038 R14: 00007f7740345fa0 R15: 00007ffce616f8d8
 </TASK>
Fixes 83382af9ddc3 ("slab: Make slub local_(try)lock more precise for LOCKDEP")
Reported-by [email protected]
---
 mm/slab_common.c |  3 +++
 mm/slub.c        | 11 +++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)
mm/slab_common.c +3 -0
index 932d13ada36c..baa934cad7ac 100644
@@ -479,6 +479,9 @@ static void kmem_cache_release(struct kmem_cache *s)
 
 void slab_kmem_cache_release(struct kmem_cache *s)
 {
+#if !IS_ENABLED(CONFIG_SLUB_TINY) && IS_ENABLED(CONFIG_PREEMPT_RT)
+	lockdep_unregister_key(&s->lock_key);
+#endif
 	__kmem_cache_release(s);
 	kfree_const(s->name);
 	kmem_cache_free(kmem_cache, s);
mm/slub.c +7 -4
index 584a5ff1828b..8da20552995a 100644
@@ -7692,9 +7692,6 @@ void __kmem_cache_release(struct kmem_cache *s)
 	if (s->cpu_sheaves)
 		pcs_destroy(s);
 #ifndef CONFIG_SLUB_TINY
-#ifdef CONFIG_PREEMPT_RT
-	lockdep_unregister_key(&s->lock_key);
-#endif
 	free_percpu(s->cpu_slab);
 #endif
 	free_kmem_cache_nodes(s);
@@ -8551,7 +8548,7 @@ int do_kmem_cache_create(struct kmem_cache *s, const char *name,
 	if (s->cpu_sheaves) {
 		err = init_percpu_sheaves(s);
 		if (err)
-			goto out;
+			goto out_unreg_lockdep;
 	}
 
 	err = 0;
@@ -8574,6 +8571,12 @@ int do_kmem_cache_create(struct kmem_cache *s, const char *name,
 	if (err)
 		__kmem_cache_release(s);
 	return err;
+
+out_unreg_lockdep:
+#if !IS_ENABLED(CONFIG_SLUB_TINY) && IS_ENABLED(CONFIG_PREEMPT_RT)
+	lockdep_unregister_key(&s->lock_key);
+#endif
+	goto out;
 }
 
 #ifdef SLAB_SUPPORTS_SYSFS
Signature · 1 line
2.51.0.710.ga91ca5db03-goog
#1 lore raw
Quoted 65 lines · Kuniyuki Iwashima

syzbot reported the lockdep splat below in __kmem_cache_release(). [0]

The problem is that __kmem_cache_release() could be called from do_kmem_cache_create() before init_kmem_cache_cpus() registers the lockdep key.

Let's move lockdep_unregister_key() from __kmem_cache_release() to slab_kmem_cache_release() and do_kmem_cache_create().

[0]:

WARNING: CPU: 1 PID: 6128 at kernel/locking/lockdep.c:6606 lockdep_unregister_key+0x2ca/0x310 kernel/locking/lockdep.c:6606
Modules linked in:
CPU: 1 UID: 0 PID: 6128 Comm: syz.4.21 Not tainted syzkaller #0 PREEMPT_{RT,(full)}
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 08/18/2025
RIP: 0010:lockdep_unregister_key+0x2ca/0x310 kernel/locking/lockdep.c:6606
Code: 50 e4 0f 48 3b 44 24 10 0f 84 26 fe ff ff e8 bd cd 17 09 e8 e8 ce 17 09 41 f7 c7 00 02 00 00 74 bd fb 40 84 ed 75 bc eb cd 90 <0f> 0b 90 e9 19 ff ff ff 90 0f 0b 90 e9 2a ff ff ff 48 c7 c7 d0 ac
RSP: 0018:ffffc90003e870d0 EFLAGS: 00010002
RAX: eb1525397f5bdf00 RBX: ffff88803c121148 RCX: 1ffff920007d0dfc
RDX: 0000000000000000 RSI: ffffffff8acb1500 RDI: ffffffff8b1dd0e0
RBP: 00000000ffffffea R08: ffffffff8eb5aa37 R09: 1ffffffff1d6b546
R10: dffffc0000000000 R11: fffffbfff1d6b547 R12: 0000000000000000
R13: ffff88814d1b8900 R14: 0000000000000000 R15: 0000000000000203
FS:  00007f773f75e6c0(0000) GS:ffff88812712f000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007ffdaea3af52 CR3: 000000003a5ca000 CR4: 00000000003526f0
Call Trace:
 <TASK>
 __kmem_cache_release+0xe3/0x1e0 mm/slub.c:7696
 do_kmem_cache_create+0x74e/0x790 mm/slub.c:8575
 create_cache mm/slab_common.c:242 [inline]
 __kmem_cache_create_args+0x1ce/0x330 mm/slab_common.c:340
 nfsd_file_cache_init+0x1d6/0x530 fs/nfsd/filecache.c:816
 nfsd_startup_generic fs/nfsd/nfssvc.c:282 [inline]
 nfsd_startup_net fs/nfsd/nfssvc.c:377 [inline]
 nfsd_svc+0x393/0x900 fs/nfsd/nfssvc.c:786
 nfsd_nl_threads_set_doit+0x84a/0x960 fs/nfsd/nfsctl.c:1639
 genl_family_rcv_msg_doit+0x212/0x300 net/netlink/genetlink.c:1115
 genl_family_rcv_msg net/netlink/genetlink.c:1195 [inline]
 genl_rcv_msg+0x60e/0x790 net/netlink/genetlink.c:1210
 netlink_rcv_skb+0x208/0x470 net/netlink/af_netlink.c:2552
 genl_rcv+0x28/0x40 net/netlink/genetlink.c:1219
 netlink_unicast_kernel net/netlink/af_netlink.c:1320 [inline]
 netlink_unicast+0x846/0xa10 net/netlink/af_netlink.c:1346
 netlink_sendmsg+0x805/0xb30 net/netlink/af_netlink.c:1896
 sock_sendmsg_nosec net/socket.c:727 [inline]
 __sock_sendmsg+0x219/0x270 net/socket.c:742
 ____sys_sendmsg+0x508/0x820 net/socket.c:2630
 ___sys_sendmsg+0x21f/0x2a0 net/socket.c:2684
 __sys_sendmsg net/socket.c:2716 [inline]
 __do_sys_sendmsg net/socket.c:2721 [inline]
 __se_sys_sendmsg net/socket.c:2719 [inline]
 __x64_sys_sendmsg+0x1a1/0x260 net/socket.c:2719
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xfa/0x3b0 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f77400eeec9
Code: ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 a8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f773f75e038 EFLAGS: 00000246 ORIG_RAX: 000000000000002e
RAX: ffffffffffffffda RBX: 00007f7740345fa0 RCX: 00007f77400eeec9
RDX: 0000000000008004 RSI: 0000200000000180 RDI: 0000000000000006
RBP: 00007f7740171f91 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007f7740346038 R14: 00007f7740345fa0 R15: 00007ffce616f8d8
 </TASK>
Quoted 4 lines
Fixes 83382af9ddc3 ("slab: Make slub local_(try)lock more precise for LOCKDEP")
Reported-by [email protected]

Thanks, added to slab/for-next-fixes

#2 lore raw
Quoted 7 lines · Vlastimil Babka
Quoted 5 lines · Kuniyuki Iwashima

syzbot reported the lockdep splat below in __kmem_cache_release(). [0]

The problem is that __kmem_cache_release() could be called from do_kmem_cache_create() before init_kmem_cache_cpus() registers the lockdep key.

Hmm. If that's the case then alloc_kmem_cache_cpus() wasn't called yet and s->cpu_slab == NULL, so any kmalloc/kmem_cache_alloc from that slub will crash. The above sounds like it's a race condition, but it's not. It's a weird error path in do_kmem_cache_create() due to syzbot pressure.
So the fix isn't quite right.
There is no need to sprinkle #ifdef all over the code.

Quoted 3 lines
Quoted 3 lines

Let's move lockdep_unregister_key() from __kmem_cache_release() to slab_kmem_cache_release() and do_kmem_cache_create().

I wouldn't.

Quoted 1 line

Thanks, added to slab/for-next-fixes

The following is imo much cleaner fix:

mm/slub.c +2 -1
index 584a5ff1828b..0a1fbddb77f8 100644
@@ -7693,7 +7693,8 @@ void __kmem_cache_release(struct kmem_cache *s)
                pcs_destroy(s);
 #ifndef CONFIG_SLUB_TINY
 #ifdef CONFIG_PREEMPT_RT
-       lockdep_unregister_key(&s->lock_key);
+       if (s->cpu_slab)
+               lockdep_unregister_key(&s->lock_key);
 #endif
        free_percpu(s->cpu_slab);
 #endif

compile tested only...

#3 lore raw
Quoted 5 lines · Alexei Starovoitov
Quoted 4 lines · Vlastimil Babka
Quoted 2 lines · Kuniyuki Iwashima

syzbot reported the lockdep splat below in __kmem_cache_release(). [0]

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

The problem is that __kmem_cache_release() could be called from do_kmem_cache_create() before init_kmem_cache_cpus() registers the lockdep key.

Hmm. If that's the case then alloc_kmem_cache_cpus() wasn't called yet and s->cpu_slab == NULL, so any kmalloc/kmem_cache_alloc from that slub will crash. The above sounds like it's a race condition, but it's not. It's a weird error path in do_kmem_cache_create()

Indeed.

Quoted 5 lines

due to syzbot pressure.
So the fix isn't quite right.
There is no need to sprinkle #ifdef all over the code.

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

Let's move lockdep_unregister_key() from __kmem_cache_release() to slab_kmem_cache_release() and do_kmem_cache_create().

I wouldn't.

Quoted 1 line

Thanks, added to slab/for-next-fixes

The following is imo much cleaner fix:

Yes much better, I'm changing it. Thanks!

Quoted 16 lines
mm/slub.c +2 -1
index 584a5ff1828b..0a1fbddb77f8 100644
@@ -7693,7 +7693,8 @@ void __kmem_cache_release(struct kmem_cache *s)
                pcs_destroy(s);
 #ifndef CONFIG_SLUB_TINY
 #ifdef CONFIG_PREEMPT_RT
-       lockdep_unregister_key(&s->lock_key);
+       if (s->cpu_slab)
+               lockdep_unregister_key(&s->lock_key);
 #endif
        free_percpu(s->cpu_slab);
 #endif

compile tested only...

Reviewed-by
#4 lore raw
Quoted 47 lines · Vlastimil Babka
Quoted 5 lines · Alexei Starovoitov
Quoted 4 lines · Vlastimil Babka
Quoted 2 lines · Kuniyuki Iwashima

syzbot reported the lockdep splat below in __kmem_cache_release(). [0]

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

The problem is that __kmem_cache_release() could be called from do_kmem_cache_create() before init_kmem_cache_cpus() registers the lockdep key.

Hmm. If that's the case then alloc_kmem_cache_cpus() wasn't called yet and s->cpu_slab == NULL, so any kmalloc/kmem_cache_alloc from that slub will crash. The above sounds like it's a race condition, but it's not. It's a weird error path in do_kmem_cache_create()

Indeed.

Quoted 5 lines

due to syzbot pressure.
So the fix isn't quite right.
There is no need to sprinkle #ifdef all over the code.

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

Let's move lockdep_unregister_key() from __kmem_cache_release() to slab_kmem_cache_release() and do_kmem_cache_create().

I wouldn't.

Quoted 1 line

Thanks, added to slab/for-next-fixes

The following is imo much cleaner fix:

Yes much better, I'm changing it. Thanks!

Quoted 15 lines
mm/slub.c +2 -1
index 584a5ff1828b..0a1fbddb77f8 100644
@@ -7693,7 +7693,8 @@ void __kmem_cache_release(struct kmem_cache *s)
                pcs_destroy(s);
 #ifndef CONFIG_SLUB_TINY
 #ifdef CONFIG_PREEMPT_RT
-       lockdep_unregister_key(&s->lock_key);
+       if (s->cpu_slab)
+               lockdep_unregister_key(&s->lock_key);
 #endif
        free_percpu(s->cpu_slab);
 #endif
Quoted 1 line
Quoted 1 line

compile tested only...

Looks good to me,

Signature · 2 lines
Cheers,
Harry / Hyeonggon