mm/slub: Removing unnecessary variable accesses in the get_freelist()
Subsystems (2)
PATCH 1/1 mm/slub: Removing unnecessary variable accesses in the get_freelist()
It pass a NULL pointer to the freelist_new variable
in the __slab_update_freelist() function so that it don't have to re-fetch the variable values inside the while loop.
Removing unnecessary variable accesses as shown below
will reduce the code size of the get_freelist() function and make it faster.
--- mm/slub.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-)
mm/slub.c +4 -17
@@ -3654,27 +3654,14 @@ __update_cpu_freelist_fast(struct kmem_cache *s,*/ static inline void *get_freelist (struct kmem_cache *s,struct slab *slab) { -struct slab new; -unsigned long counters; -void *freelist; -lockdep_assert_held (this_cpu_ptr (&s->cpu_slab->lock)); -do { - freelist = slab->freelist; - counters = slab->counters; - - new.counters = counters; - - new.inuse = slab->objects; - new.frozen = freelist !=NULL ; - - }while (!__slab_update_freelist (s, slab, - freelist, counters, -NULL , new.counters, +while (!__slab_update_freelist (s, slab, + slab->freelist, slab->counters, +NULL , slab->counters,"get_freelist" )); -return freelist; +return slab->freelist; }/*
Signature · 1 line
2.43.0
Hi Jaejoon,
I updated my email from [email protected] to [email protected] a while ago. Please check up-to-date MAINTAINERS file when sending a patch.
Quoted 3 lines · JaeJoon Jung
It pass a NULL pointer to the
freelist_newvariable
in the__slab_update_freelist()function so that it don't have to re-fetch the variable values inside the while loop.
No, it needs to re-fetch values when cmpxchg fails. Otherwise it would fall into an infinite loop, no?
at a high level overview, cmpxchg works like this (atomically, of course):
retry:
old = var;
// modify some bits in 'old' and store it to 'new'
new = old + something;
if (var == old) { // compare
var = new; // exchange if the value is expected
} else {
// if var != old, someone else updated the variable. retry
goto retry;
}and this retry will certainly fail if you don't you re-fetch the value, modify it, and try cmpxchg again. The 'old' value fetched before failing cmpxchg will not match anymore because other CPUs already updated that variable.
Quoted 20 lines
Removing unnecessary variable accesses as shown below
will reduce the code size of theget_freelist()function and make it faster.Signed-off-by JaeJoon Jung <[email protected]>--- mm/slub.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-)mm/slub.c +0
-4@@ -3654,27 +3654,14 @@ __update_cpu_freelist_fast(struct kmem_cache *s,*/ static inline void *get_freelist (struct kmem_cache *s,struct slab *slab) { -struct slab new; -unsigned long counters; -void *freelist; -
mm/slub.c __update_cpu_freelist_fast(struct kmem_cache *s, · line 3670
Quoted 10 lines
mm/slub.c +0
-8lockdep_assert_held (this_cpu_ptr (&s->cpu_slab->lock)); - do { - freelist = slab->freelist; - counters = slab->counters; - - new.counters = counters; - - new.inuse = slab->objects; - new.frozen = freelist != NULL;
...and the frozen and inuse bits are part of counters field, so they are not updated anymore?
Quoted 16 lines
mm/slub.c +4
-5- - }while (!__slab_update_freelist (s, slab, - freelist, counters, -NULL , new.counters, +while (!__slab_update_freelist (s, slab, + slab->freelist, slab->counters, +NULL , slab->counters,"get_freelist" )); -return freelist; +return slab->freelist; }/* Signature · 1 line
2.43.0
Signature · 2 lines
Cheers, Harry / Hyeonggon
Hi, Harry Hyeonggon Yoo,
Thank you for your kind and detailed reply.
I hadn't thought of the union data type.
Since the counters, inuse, and frozen members are unioned within the slab structure,
there is the problem that the above values are not applied to __slab_update_freelist() and try_cmpxchg_freelist() if the existing code is not followed.
I canceled my patch request.
I apologized for my inconvenience.
Quoted 86 lines · Harry Yoo
Hi Jaejoon,
I updated my email from [email protected] to [email protected] a while ago. Please check up-to-date
MAINTAINERSfile when sending a patch.Quoted 3 lines · JaeJoon Jung
It pass a NULL pointer to the
freelist_newvariable
in the__slab_update_freelist()function so that it don't have to re-fetch the variable values inside the while loop.No, it needs to re-fetch values when cmpxchg fails. Otherwise it would fall into an infinite loop, no?
at a high level overview, cmpxchg works like this (atomically, of course):
retry:
old = var; // modify some bits in 'old' and store it to 'new' new = old + something; if (var == old) { // compare var = new; // exchange if the value is expected } else { // if var != old, someone else updated the variable. retry goto retry; }and this retry will certainly fail if you don't you re-fetch the value, modify it, and try cmpxchg again. The 'old' value fetched before failing cmpxchg will not match anymore because other CPUs already updated that variable.
Quoted 20 lines
Removing unnecessary variable accesses as shown below
will reduce the code size of theget_freelist()function and make it faster.Signed-off-by JaeJoon Jung <[email protected]>--- mm/slub.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-)mm/slub.c +0
-4@@ -3654,27 +3654,14 @@ __update_cpu_freelist_fast(struct kmem_cache *s,*/ static inline void *get_freelist (struct kmem_cache *s,struct slab *slab) { -struct slab new; -unsigned long counters; -void *freelist; -mm/slub.c __update_cpu_freelist_fast(struct kmem_cache *s, · line 3670
Quoted 10 lines
mm/slub.c +0
-8lockdep_assert_held (this_cpu_ptr (&s->cpu_slab->lock)); - do { - freelist = slab->freelist; - counters = slab->counters; - - new.counters = counters; - - new.inuse = slab->objects; - new.frozen = freelist != NULL;...and the frozen and inuse bits are part of counters field, so they are not updated anymore?
Quoted 15 lines
mm/slub.c +4
-6- - }while (!__slab_update_freelist (s, slab, - freelist, counters, -NULL , new.counters, +while (!__slab_update_freelist (s, slab, + slab->freelist, slab->counters, +NULL , slab->counters,"get_freelist" )); -return freelist; +return slab->freelist; }/* -- Quoted 1 line
2.43.0
--
Cheers,
Harry / Hyeonggon
Quoted 6 lines · JaeJoon Jung
Hi, Harry Hyeonggon Yoo,
Thank you for your kind and detailed reply.
I hadn't thought of the union data type.
Quoted 8 lines
Since the counters, inuse, and frozen members are unioned within the slab structure,
there is the problem that the above values are not applied to__slab_update_freelist()andtry_cmpxchg_freelist()if the existing code is not followed.I canceled my patch request.
I apologized for my inconvenience.
For the future, please
(1) Don't clean up code you don't fully understand
(2) Test your patches before submission
Signature · 3 lines
Cheers David / dhildenb
syzbot ci has tested the following series
syzbot ci has tested the following series
[v1] mm/slub: Removing unnecessary variable accesses in the get_freelist()
https://lore.kernel.org/all/[email protected]
* [PATCH] mm/slub: Removing unnecessary variable accesses in the get_freelist()
and found the following issue:
kernel BUG in __smpboot_create_thread
Full report is available here:
https://ci.syzbot.org/series/832d6128-4556-4bd1-ab18-0676b8e7dbc1
***
kernel BUG in __smpboot_create_thread
tree: torvalds URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux base: f777d1112ee597d7f7dd3ca232220873a34ad0c8 arch: amd64 compiler: Debian clang version 20.1.8 (++20250708063551+0c9f909b7976-1~exp1~20250708183702.136), Debian LLD 20.1.8 config: https://ci.syzbot.org/builds/7bff7792-4edd-4680-a5e7-3cc9d97ac5ed/config
smpboot: CPU0: Intel QEMU Virtual CPU version 2.5+ (family: 0xf, model: 0x6b, stepping: 0x1) Performance Events: unsupported Netburst CPU model 107 no PMU driver, software events only.
signal: max sigframe size: 1440
------------[ cut here ]------------ kernel BUG at mm/slub.c:3832! Oops: invalid opcode: 0000 [#1] SMP KASAN PTI CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:___slab_alloc+0x13ab/0x1420 Code: ff ff 4c 89 e7 48 c7 c6 3a a1 a0 8d e8 3e 4f 0f ff 90 0f 0b 90 0f 0b 48 89 f7 48 c7 c6 3a a1 a0 8d e8 29 4f 0f ff 90 0f 0b 90 <0f> 0b 90 0f 0b 4c 89 e7 48 c7 c6 3a a1 a0 8d e8 11 4f 0f ff 90 0f RSP: 0000:ffffc90000047750 EFLAGS: 00010046 RAX: 0000000000000000 RBX: ffff888100028000 RCX: 0000000000800001 RDX: 0000000000800001 RSI: ffffffff8dba8965 RDI: ffffffff8be33980 RBP: 0000000000000000 R08: 0000000000000000 R09: ffffffff82189e55 R10: dffffc0000000000 R11: fffffbfff1f47407 R12: ffff88804b03e480 R13: ffff88801a441640 R14: ffff88804b03e460 R15: ffffffff82189e55 FS: 0000000000000000(0000) GS:ffff8880b8615000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: ffff88813ffff000 CR3: 000000000df36000 CR4: 00000000000006f0 Call Trace: <TASK> __kmalloc_cache_node_noprof+0x29a/0x3d0 __smpboot_create_thread+0x11a/0x3b0 smpboot_register_percpu_thread+0xc6/0x420 spawn_ksoftirqd+0x2f/0xc0 do_one_initcall+0x236/0x820 do_pre_smp_initcalls+0x4c/0x80 kernel_init_freeable+0x30c/0x4b0 kernel_init+0x1d/0x1d0 ret_from_fork+0x3fc/0x770 ret_from_fork_asm+0x1a/0x30 </TASK> Modules linked in: ---[ end trace 0000000000000000 ]--- RIP: 0010:___slab_alloc+0x13ab/0x1420 Code: ff ff 4c 89 e7 48 c7 c6 3a a1 a0 8d e8 3e 4f 0f ff 90 0f 0b 90 0f 0b 48 89 f7 48 c7 c6 3a a1 a0 8d e8 29 4f 0f ff 90 0f 0b 90 <0f> 0b 90 0f 0b 4c 89 e7 48 c7 c6 3a a1 a0 8d e8 11 4f 0f ff 90 0f RSP: 0000:ffffc90000047750 EFLAGS: 00010046 RAX: 0000000000000000 RBX: ffff888100028000 RCX: 0000000000800001 RDX: 0000000000800001 RSI: ffffffff8dba8965 RDI: ffffffff8be33980 RBP: 0000000000000000 R08: 0000000000000000 R09: ffffffff82189e55 R10: dffffc0000000000 R11: fffffbfff1f47407 R12: ffff88804b03e480 R13: ffff88801a441640 R14: ffff88804b03e460 R15: ffffffff82189e55 FS: 0000000000000000(0000) GS:ffff8880b8615000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: ffff88813ffff000 CR3: 000000000df36000 CR4: 00000000000006f0
***
If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:
---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].