mm/slab: take n->list_lock in __slab_try_return_freelist() to avoid race
Subsystems (3)
Harry Yoo (Meta)
maintainerPATCH v3 1/1 mm/slab: take n->list_lock in __slab_try_return_freelist() to avoid race
Commit ba7425312607 ("mm, slab: add an optimistic __slab_try_return_freelist()") incorrectly assumed that nobody has freed an object to the slab as long as slab->freelist is NULL and cmpxchg succeeds.
However, as reported by Hyunwoo Kim [1], other CPUs might have freed an object to the slab, insert the slab to the partial list, then allocated an object from the slab, and be in the middle of removing the slab from the list under n->list_lock.
Since __refill_objects_node() puts the slab back on pc.slabs outside n->list_lock, it might insert the slab into that list while the slab is concurrently being removed from n->partial. This led to a list corruption [1]:
list_add corruption. next->prev should be prev
(ffff888100000248), but was dead000000000122.
(next=ffffea000416e410).
kernel BUG at lib/list_debug.c:29!
Oops: invalid opcode: 0000 [#1] SMP NOPTI CPU: 1 UID: 65534 PID: 144 Comm: poc Not tainted 7.2.0-16172-gcf72cbb39da8-dirty #1 PREEMPT(lazy) RIP: 0010:__list_add_valid_or_report+0x80/0xd0 ... Call Trace: alloc_from_new_slab+0x183/0x300 ___slab_alloc+0x31c/0x890 __kmalloc_noprof+0x3d4/0x800 lsm_blob_alloc+0x2d/0x50 security_msg_msg_alloc+0x26/0x90 load_msg+0x1aa/0x210 do_msgsnd+0x91/0x800 do_syscall_64+0x109/0x5d0 entry_SYSCALL_64_after_hwframe+0x77/0x7f ... Kernel panic - not syncing: Fatal exception
This is a classic ABA problem where cmpxchg succeeds but the state has changed since __refill_objects_node() took the freelist from the slab.
As Vlastimil Babka mentioned [2], it should be rare to return more than one slab (due to the racy read of slab->counters in get_partial_node_bulk()). Therefore, instead of introducing additional complexity, acquire and release n->list_lock twice in the worst case.
Return the slab directly to the partial list and hold n->list_lock across the cmpxchg and add_partial(). This is similar to the initial version of commit ba7425312607 [3]. This is enough to avoid the race as the list manipulation is serialized by n->list_lock. While at it, bring back unlikely() hint now that the condition is unlikely.
---
Changes in v3:
- Don't repeat "As reported by Hyunwoo Kim" in changelog (Vlastimil)
- Pass kmem_cache_node pointer directly to __slab_try_return_freelist() (Vlastimil)
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Simplify the code to hold n->list_lock across cmpxchg + add_partial() and acquire the lock twice in the rare worst case.
- Link to v1: https://lore.kernel.org/r/apPa-cGLcyt90l-E@v4bel
--- mm/slub.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
mm/slub.c +13 -7
@@ -5680,10 +5680,12 @@ static noinline void free_to_partial_list(* * Fail if the slab isn't full anymore due to a concurrent free. */ -static bool __slab_try_return_freelist (struct kmem_cache *s,struct slab *slab, -void *head,int cnt) +static bool __slab_try_return_freelist (struct kmem_cache *s, +struct kmem_cache_node *n, +struct slab *slab,void *head,int cnt) {struct freelist_counters old, new; +unsigned long flags; old.freelist = slab->freelist; old.counters = slab->counters;
@@ -5695,9 +5697,15 @@ static bool __slab_try_return_freelist(struct kmem_cache *s, struct slab *slab, new.counters = old.counters; new.inuse -= cnt; -if (!slab_update_freelist (s, slab, &old, &new,"__slab_try_return_freelist" )) +spin_lock_irqsave (&n->list_lock, flags); + +if (!slab_update_freelist (s, slab, &old, &new,"__slab_try_return_freelist" )) { +spin_unlock_irqrestore (&n->list_lock, flags);return false; + } +add_partial (n, slab,ADD_TO_TAIL ); +spin_unlock_irqrestore (&n->list_lock, flags);return true; }
@@ -7296,10 +7304,8 @@ __refill_objects_node(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int mivoid *head = object;void *tail; -if (__slab_try_return_freelist (s, slab, head, count)) { -list_add (&slab->slab_list, &pc.slabs); +if (__slab_try_return_freelist (s, n, slab, head, count))break ; - } do { tail = object;
@@ -7312,7 +7318,7 @@ __refill_objects_node(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int mibreak ; } -if (!list_empty (&pc.slabs)) { +if (unlikely (!list_empty (&pc.slabs))) {spin_lock_irqsave (&n->list_lock, flags);list_for_each_entry (slab, &pc.slabs, slab_list)
---
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
change-id: 20260902-slab-fix-aba-d60f39fc9dee
Best regards,
Signature · 2 lines
Cheers, Harry / Hyeonggon
Vlastimil Babka (SUSE)
maintainerQuoted 51 lines · Harry Yoo (Meta)
Commit
ba7425312607("mm, slab: add an optimistic__slab_try_return_freelist()") incorrectly assumed that nobody has freed an object to the slab as long asslab->freelistis NULL and cmpxchg succeeds.However, as reported by Hyunwoo Kim [1], other CPUs might have freed an object to the slab, insert the slab to the partial list, then allocated an object from the slab, and be in the middle of removing the slab from the list under
n->list_lock.Since
__refill_objects_node()puts the slab back on pc.slabs outsiden->list_lock, it might insert the slab into that list while the slab is concurrently being removed fromn->partial. This led to a list corruption [1]:
list_addcorruption.next->prevshould be prev
(ffff888100000248), but wasdead000000000122.
(next=ffffea000416e410).
kernel BUG atlib/list_debug.c:29!Oops: invalid opcode: 0000 [#1] SMP NOPTI CPU: 1 UID: 65534 PID: 144 Comm: poc Not tainted 7.2.0-16172-gcf72cbb39da8-dirty #1 PREEMPT(lazy) RIP: 0010:__list_add_valid_or_report+0x80/0xd0 ... Call Trace: alloc_from_new_slab+0x183/0x300 ___slab_alloc+0x31c/0x890 __kmalloc_noprof+0x3d4/0x800 lsm_blob_alloc+0x2d/0x50 security_msg_msg_alloc+0x26/0x90 load_msg+0x1aa/0x210 do_msgsnd+0x91/0x800 do_syscall_64+0x109/0x5d0 entry_SYSCALL_64_after_hwframe+0x77/0x7f ... Kernel panic - not syncing: Fatal exceptionThis is a classic ABA problem where cmpxchg succeeds but the state has changed since
__refill_objects_node()took the freelist from the slab.As Vlastimil Babka mentioned [2], it should be rare to return more than one slab (due to the racy read of
slab->countersinget_partial_node_bulk()). Therefore, instead of introducing additional complexity, acquire and releasen->list_locktwice in the worst case.Return the slab directly to the partial list and hold
n->list_lockacross the cmpxchg andadd_partial(). This is similar to the initial version of commitba7425312607[3]. This is enough to avoid the race as the list manipulation is serialized byn->list_lock. While at it, bring backunlikely()hint now that the condition is unlikely.
Quoted 7 lines
Reported-by Hyunwoo Kim <[email protected]>Link https://lore.kernel.org/all/20260421-b4-refill-optimistic-return-v1-1-24f0bfc1acff@kernel.org [3] open hereFixes ba7425312607 ("mm, slab: add an optimistic __slab_try_return_freelist()")Signed-off-by Harry Yoo (Meta) <[email protected]>
Added to mm/slab.git slab/for-next-fixes, thanks!
Quoted 64 lines · Harry Yoo (Meta)
Commit
ba7425312607("mm, slab: add an optimistic__slab_try_return_freelist()") incorrectly assumed that nobody has freed an object to the slab as long asslab->freelistis NULL and cmpxchg succeeds.However, as reported by Hyunwoo Kim [1], other CPUs might have freed an object to the slab, insert the slab to the partial list, then allocated an object from the slab, and be in the middle of removing the slab from the list under
n->list_lock.Since
__refill_objects_node()puts the slab back on pc.slabs outsiden->list_lock, it might insert the slab into that list while the slab is concurrently being removed fromn->partial. This led to a list corruption [1]:
list_addcorruption.next->prevshould be prev
(ffff888100000248), but wasdead000000000122.
(next=ffffea000416e410).
kernel BUG atlib/list_debug.c:29!Oops: invalid opcode: 0000 [#1] SMP NOPTI CPU: 1 UID: 65534 PID: 144 Comm: poc Not tainted 7.2.0-16172-gcf72cbb39da8-dirty #1 PREEMPT(lazy) RIP: 0010:__list_add_valid_or_report+0x80/0xd0 ... Call Trace: alloc_from_new_slab+0x183/0x300 ___slab_alloc+0x31c/0x890 __kmalloc_noprof+0x3d4/0x800 lsm_blob_alloc+0x2d/0x50 security_msg_msg_alloc+0x26/0x90 load_msg+0x1aa/0x210 do_msgsnd+0x91/0x800 do_syscall_64+0x109/0x5d0 entry_SYSCALL_64_after_hwframe+0x77/0x7f ... Kernel panic - not syncing: Fatal exceptionThis is a classic ABA problem where cmpxchg succeeds but the state has changed since
__refill_objects_node()took the freelist from the slab.As Vlastimil Babka mentioned [2], it should be rare to return more than one slab (due to the racy read of
slab->countersinget_partial_node_bulk()). Therefore, instead of introducing additional complexity, acquire and releasen->list_locktwice in the worst case.Return the slab directly to the partial list and hold
n->list_lockacross the cmpxchg andadd_partial(). This is similar to the initial version of commitba7425312607[3]. This is enough to avoid the race as the list manipulation is serialized byn->list_lock. While at it, bring backunlikely()hint now that the condition is unlikely.Reported-by Hyunwoo Kim <[email protected]>Link https://lore.kernel.org/all/20260421-b4-refill-optimistic-return-v1-1-24f0bfc1acff@kernel.org [3] open hereFixes ba7425312607 ("mm, slab: add an optimistic __slab_try_return_freelist()")Signed-off-by Harry Yoo (Meta) <[email protected]>---
Changes in v3:
- Don't repeat "As reported by Hyunwoo Kim" in changelog (Vlastimil)
- Passkmem_cache_nodepointer directly to__slab_try_return_freelist()(Vlastimil)
- Link to v2: https://lore.kernel.org/r/[email protected]
Quoted 8 lines
Changes in v2:
- Simplify the code to holdn->list_lockacross cmpxchg +add_partial()and acquire the lock twice in the rare worst case.
- Link to v1: https://lore.kernel.org/r/apPa-cGLcyt90l-E@v4bel--- mm/slub.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
Looks good to me! Thanks for the fix!
Signature · 2 lines
Thanks, Hao
kernel test robot
build botkernel test robot noticed a 2.3% regression of stress-ng.pagemove.page_remaps_per_sec on:
Hello,
kernel test robot noticed a 2.3% regression of stress-ng.pagemove.page_remaps_per_sec on:
commit: 710555228bde64392410a9e8959d0324c1a0a598 ("[PATCH v3] mm/slab: take n->list_lock in __slab_try_return_freelist() to avoid race")
url: https://github.com/intel-lab-lkp/linux/commits/Harry-Yoo-Meta/mm-slab-take-n-list_lock-in-__slab_try_return_freelist-to-avoid-race/20260903-153226
patch link: https://lore.kernel.org/all/[email protected]/
patch subject: [PATCH v3] mm/slab: take n->list_lock in __slab_try_return_freelist() to avoid race
testcase: stress-ng
config: x86_64-rhel-9.4
compiler: gcc-14
test machine: 192 threads 2 sockets Intel(R) Xeon(R) 6740E CPU @ 2.4GHz (Sierra Forest) with 256G memory parameters:
nr_threads: 100% testtime: 60s test: pagemove cpufreq_governor: performance
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-lkp/[email protected]
Details are as below:
-------------------------------------------------------------------------------------------------->
The kernel config and materials to reproduce are available at:
https://download.01.org/0day-ci/archive/20260924/[email protected]
=========================================================================================
compiler/cpufreq_governor/kconfig/nr_threads/rootfs/tbox_group/test/testcase/testtime:
gcc-14/performance/x86_64-rhel-9.4/100%/debian-13-x86_64-20250902.cgz/igk-srf-2sp3/pagemove/stress-ng/60s
commit:
v7.3-rc1
710555228b ("[PATCH v3] mm/slab: take n->list_lock in __slab_try_return_freelist() to avoid race")
v7.3-rc1 710555228bde64392410a9e8959
---------------- ---------------------------
%stddev %change %stddev
\ | \
74699042 -2.5% 72797565 stress-ng.pagemove.ops
1245981 -2.5% 1214288 stress-ng.pagemove.ops_per_sec
291125 -2.3% 284309 stress-ng.pagemove.page_remaps_per_secDisclaimer:
Results have been estimated based on internal Intel analysis and are provided for informational purposes only. Any difference in system hardware or software design or configuration may affect actual performance.
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki