Skip to content

Commit 0fc52de

Browse files
Hao LiVlastimil Babka (SUSE)
authored andcommitted
mm/slub: detach and reattach partial slabs in batch
get_partial_node_bulk() moves each selected slab from the node's partial list to the local pc->slabs list using a remove_partial() and list_add() pair. In practice, the loop often detaches several adjacent slabs. Doing this individually repeatedly manipulates list pointers while holding n->list_lock, which causes unnecessary churn. To demonstrate this, the counts below show how often single vs. multiple consecutive slabs are retrieved during a will-it-scale mmap stress test: consecutive_slabs_count frequency = 1 277345324 = 2 335238023 = 3 175717884 >= 4 88862337 The data confirms that retrieving multiple contiguous slabs is highly frequent. To optimize this, track contiguous runs of matching slabs and move each run in a single operation using list_bulk_move_tail(). This reduces list pointer churn inside the lock critical section. Apply the same optimization to __refill_objects_node() when reattaching leftover partial slabs back to the node's partial list. The will-it-scale mmap benchmark shows a 2% ~ 5% performance improvement after applying this patch. Signed-off-by: Hao Li <hao.li@linux.dev> Link: https://patch.msgid.link/20260529035120.81304-3-hao.li@linux.dev Reviewed-by: Harry Yoo (Oracle) <harry@kernel.org> Signed-off-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
1 parent 7e23073 commit 0fc52de

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

mm/slub.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3751,6 +3751,7 @@ static bool get_partial_node_bulk(struct kmem_cache *s,
37513751
bool allow_spin)
37523752
{
37533753
struct slab *slab, *slab2;
3754+
struct slab *first = NULL, *last = NULL;
37543755
unsigned int total_free = 0;
37553756
unsigned long flags;
37563757

@@ -3769,8 +3770,15 @@ static bool get_partial_node_bulk(struct kmem_cache *s,
37693770
struct freelist_counters flc;
37703771
unsigned int slab_free;
37713772

3772-
if (!pfmemalloc_match(slab, pc->flags))
3773+
if (!pfmemalloc_match(slab, pc->flags)) {
3774+
if (first) {
3775+
list_bulk_move_tail(&pc->slabs,
3776+
&first->slab_list,
3777+
&last->slab_list);
3778+
first = NULL;
3779+
}
37733780
continue;
3781+
}
37743782

37753783
/*
37763784
* determine the number of free objects in the slab racily
@@ -3787,15 +3795,20 @@ static bool get_partial_node_bulk(struct kmem_cache *s,
37873795
&& total_free + slab_free > pc->max_objects)
37883796
break;
37893797

3790-
remove_partial(n, slab);
3791-
3792-
list_add(&slab->slab_list, &pc->slabs);
3798+
if (!first)
3799+
first = slab;
3800+
last = slab;
3801+
clear_node_partial_state(n, slab);
37933802

37943803
total_free += slab_free;
37953804
if (total_free >= pc->max_objects)
37963805
break;
37973806
}
37983807

3808+
if (first)
3809+
list_bulk_move_tail(&pc->slabs, &first->slab_list,
3810+
&last->slab_list);
3811+
37993812
spin_unlock_irqrestore(&n->list_lock, flags);
38003813
return total_free > 0;
38013814
}
@@ -7205,11 +7218,10 @@ __refill_objects_node(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int mi
72057218
if (!list_empty(&pc.slabs)) {
72067219
spin_lock_irqsave(&n->list_lock, flags);
72077220

7208-
list_for_each_entry_safe(slab, slab2, &pc.slabs, slab_list) {
7221+
list_for_each_entry(slab, &pc.slabs, slab_list)
7222+
set_node_partial_state(n, slab);
72097223

7210-
list_del(&slab->slab_list);
7211-
add_partial(n, slab, ADD_TO_TAIL);
7212-
}
7224+
list_splice_tail(&pc.slabs, &n->partial);
72137225

72147226
spin_unlock_irqrestore(&n->list_lock, flags);
72157227
}

0 commit comments

Comments
 (0)