Skip to content

Commit 046f4c6

Browse files
jianfenwtehcaster
authored andcommitted
slub: introduce count_partial_free_approx()
When reading "/proc/slabinfo", the kernel needs to report the number of free objects for each kmem_cache. The current implementation uses count_partial() to get it by scanning each kmem_cache_node's partial slab list and summing free objects from every partial slab. This process must hold per-kmem_cache_node spinlock and disable IRQ, and may take a long time. Consequently, it can block slab allocations on other CPUs and cause timeouts for network devices, when the partial list is long. In production, even NMI watchdog can be triggered due to this matter: e.g., for "buffer_head", the number of partial slabs was observed to be ~1M in one kmem_cache_node. This problem was also confirmed by others [1-3]. Iterating a partial list to get the exact count of objects can cause soft lockups for a long list with or without the lock (e.g., if preemption is disabled), and may not be very useful: the object count can change after the lock is released. The approach of maintaining free-object counters requires atomic operations on the fast path [3]. So, the fix is to introduce count_partial_free_approx(). This function can be used for getting the free object count in a kmem_cache_node's partial list. It limits the number of slabs to scan and avoids scanning the whole list by giving an approximation for a long list. Suppose the limit is N. If the list's length is not greater than N, output the exact count by traversing the list; if its length is greater than N, output an approximated count by traversing a subset of the list. The proposed method is to scan N/2 slabs from the list's head and N/2 slabs from the tail. For a partial list with ~280K slabs, benchmarks show that it performs better than just counting from the list's head, after slabs get sorted by kmem_cache_shrink(). Default the limit to 10000, as it produces an approximation within 1% of the exact count for both scenarios. Then, use count_partial_free_approx() in get_slabinfo(). Benchmarks: Diff = (exact - approximated) / exact * Normal case (w/o kmem_cache_shrink()): | MAX_TO_SCAN | Diff (count from head)| Diff (count head+tail)| | 1000 | 0.43 % | 1.09 % | | 5000 | 0.06 % | 0.37 % | | 10000 | 0.02 % | 0.16 % | | 20000 | 0.009 % | -0.003 % | * Skewed case (w/ kmem_cache_shrink()): | MAX_TO_SCAN | Diff (count from head)| Diff (count head+tail)| | 1000 | 12.46 % | 6.75 % | | 5000 | 5.38 % | 1.27 % | | 10000 | 4.99 % | 0.22 % | | 20000 | 4.86 % | -0.06 % | [1] https://lore.kernel.org/linux-mm/[email protected]/T/ [2] https://lore.kernel.org/lkml/[email protected]/T/ [3] https://lore.kernel.org/lkml/[email protected]/T/ Signed-off-by: Jianfeng Wang <[email protected]> Acked-by: David Rientjes <[email protected]> Signed-off-by: Vlastimil Babka <[email protected]>
1 parent 5b15f3f commit 046f4c6

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

mm/slub.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3238,6 +3238,43 @@ static unsigned long count_partial(struct kmem_cache_node *n,
32383238
#endif /* CONFIG_SLUB_DEBUG || SLAB_SUPPORTS_SYSFS */
32393239

32403240
#ifdef CONFIG_SLUB_DEBUG
3241+
#define MAX_PARTIAL_TO_SCAN 10000
3242+
3243+
static unsigned long count_partial_free_approx(struct kmem_cache_node *n)
3244+
{
3245+
unsigned long flags;
3246+
unsigned long x = 0;
3247+
struct slab *slab;
3248+
3249+
spin_lock_irqsave(&n->list_lock, flags);
3250+
if (n->nr_partial <= MAX_PARTIAL_TO_SCAN) {
3251+
list_for_each_entry(slab, &n->partial, slab_list)
3252+
x += slab->objects - slab->inuse;
3253+
} else {
3254+
/*
3255+
* For a long list, approximate the total count of objects in
3256+
* it to meet the limit on the number of slabs to scan.
3257+
* Scan from both the list's head and tail for better accuracy.
3258+
*/
3259+
unsigned long scanned = 0;
3260+
3261+
list_for_each_entry(slab, &n->partial, slab_list) {
3262+
x += slab->objects - slab->inuse;
3263+
if (++scanned == MAX_PARTIAL_TO_SCAN / 2)
3264+
break;
3265+
}
3266+
list_for_each_entry_reverse(slab, &n->partial, slab_list) {
3267+
x += slab->objects - slab->inuse;
3268+
if (++scanned == MAX_PARTIAL_TO_SCAN)
3269+
break;
3270+
}
3271+
x = mult_frac(x, n->nr_partial, scanned);
3272+
x = min(x, node_nr_objs(n));
3273+
}
3274+
spin_unlock_irqrestore(&n->list_lock, flags);
3275+
return x;
3276+
}
3277+
32413278
static noinline void
32423279
slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
32433280
{
@@ -7116,7 +7153,7 @@ void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo)
71167153
for_each_kmem_cache_node(s, node, n) {
71177154
nr_slabs += node_nr_slabs(n);
71187155
nr_objs += node_nr_objs(n);
7119-
nr_free += count_partial(n, count_free);
7156+
nr_free += count_partial_free_approx(n);
71207157
}
71217158

71227159
sinfo->active_objs = nr_objs - nr_free;

0 commit comments

Comments
 (0)