mm: Fix OOM killer inaccuracy on large many-core systems
Subsystems (9)
Mathieu Desnoyers
reviewerv10 cover mm: Fix OOM killer inaccuracy on large many-core systems
Introduce hierarchical per-cpu counters and use them for RSS tracking to fix the per-mm RSS tracking which has become too inaccurate for OOM killer purposes on large many-core systems.
The following rss tracking issues were noted by Sweet Tea Dorminy [1], which lead to picking wrong tasks as OOM kill target:
Recently, several internal services had an RSS usage regression as part of a kernel upgrade. Previously, they were on a pre-6.2 kernel and were able to read RSS statistics in a backup watchdog process to monitor and decide if they'd overrun their memory budget. Now, however, a representative service with five threads, expected to use about a hundred MB of memory, on a 250-cpu machine had memory usage tens of megabytes different from the expected amount -- this constituted a significant percentage of inaccuracy, causing the watchdog to act.
This was a result of commit f1a7941243c1 ("mm: convert mm's rss stats into percpu_counter") [1]. Previously, the memory error was bounded by 64*nr_threads pages, a very livable megabyte. Now, however, as a result of scheduler decisions moving the threads around the CPUs, the memory error could be as large as a gigabyte.
This is a really tremendous inaccuracy for any few-threaded program on a large machine and impedes monitoring significantly. These stat counters are also used to make OOM killing decisions, so this additional inaccuracy could make a big difference in OOM situations -- either resulting in the wrong process being killed, or in less memory being returned from an OOM-kill than expected.
The approach proposed here is to replace this by the hierarchical per-cpu counters, which bounds the inaccuracy based on the system topology with O(N*logN).
Notable change for v10: The new patch 3/3 changes the implementation of the oom killer task selection to a 2-pass algorithm, where the first pass uses the fast approximation provided by the hierarchical percpu counters, and the second pass does a precise sum for all tasks which have badness values within the range of the approximation accuracy.
I've done moderate testing of this series on a 256-core VM with 128GB RAM. Figuring out whether this indeed helps solve issues with real-life workloads will require broader feedback from the community.
The one request I did not have time to fulfill yet is to port the tests from the librseq feature branch implementation (userspace) to the kernel selftests.
This series is based on v6.18.
Andrew, are you interested to try this out in mm-new ?
Thanks,
Mathieu
To: Andrew Morton <[email protected]>
Cc: "Paul E. McKenney" <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Masami Hiramatsu <[email protected]>
Cc: Mathieu Desnoyers <[email protected]>
Cc: Dennis Zhou <[email protected]>
Cc: Tejun Heo <[email protected]>
Cc: Christoph Lameter <[email protected]>
Cc: Martin Liu <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: [email protected]
Cc: Shakeel Butt <[email protected]>
Cc: SeongJae Park <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Sweet Tea Dorminy <[email protected]>
Cc: Lorenzo Stoakes <[email protected]>
Cc: "Liam R . Howlett" <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: Suren Baghdasaryan <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Christian Brauner <[email protected]>
Mathieu Desnoyers (3):
lib: Introduce hierarchical per-cpu counters
mm: Fix OOM killer inaccuracy on large many-core systems
mm: Implement precise OOM killer task selection
fs/proc/base.c | 2 +- include/linux/mm.h | 58 ++- include/linux/mm_types.h | 4 +- include/linux/oom.h | 12 +- include/linux/percpu_counter_tree.h | 242 ++++++++++ include/trace/events/kmem.h | 2 +- init/main.c | 2 + kernel/fork.c | 24 +- lib/Makefile | 1 + lib/percpu_counter_tree.c | 705 ++++++++++++++++++++++++++++ mm/oom_kill.c | 72 ++- 11 files changed, 1089 insertions(+), 35 deletions(-) create mode 100644 include/linux/percpu_counter_tree.h create mode 100644 lib/percpu_counter_tree.c
Signature · 1 line
2.39.5
Mathieu Desnoyers
reviewerPATCH v10 1/3 lib: Introduce hierarchical per-cpu counters
* Motivation
The purpose of this hierarchical split-counter scheme is to:
- Minimize contention when incrementing and decrementing counters,
- Provide fast access to a sum approximation,
- Provide a sum approximation with an acceptable accuracy level when scaling to many-core systems.
- Provide approximate and precise comparison of two counters, and between a counter and a value.
It aims at fixing the per-mm RSS tracking which has become too inaccurate for OOM killer purposes on large many-core systems [1].
* Design
The hierarchical per-CPU counters propagate a sum approximation through a N-way tree. When reaching the batch size, the carry is propagated through a binary tree which consists of logN(nr_cpu_ids) levels. The batch size for each level is twice the batch size of the prior level.
Example propagation diagram with 8 cpus through a binary tree:
Level 0: 0 1 2 3 4 5 6 7
| / | / | / | /
| / | / | / | /
| / | / | / | /
Level 1: 0 1 2 3
| / | /
| / | /
| / | /
Level 2: 0 1
| /
| /
| /
Level 3: 0For a binary tree, the maximum inaccuracy is bound by:
batch_size * log2(nr_cpus) * nr_cpus
which evolves with O(n*log(n)) as the number of CPUs increases.
For a N-way tree, the maximum inaccuracy can be pre-calculated based on the the N-arity of each level and the batch size.
---
Changes since v9:
- Introduce kerneldoc documentation.
- Document structure fields.
- Remove inline from percpu_counter_tree_add().
- Reject batch_size==1 which makes no sense.
- Fix copy-paste bug in percpu_counter_tree_precise_compare() (wrong inequality comparison).
- Rename "inaccuracy" to "accuracy", which makes it easier to document.
- Track accuracy limits more precisely. In two's complement signed integers, the range before a n-bit underflow is one unit larger than the range before a n-bit overflow. This sums for all the counters within the tree. Therefore the "under" vs "over" accuracy is not symmetrical.
Changes since v8:
- Remove migrate guard from the fast path. It does not matter through which path the carry is propagated up
the tree.
- Rebase on top of v6.18-rc6.
- Introduce percpu_counter_tree_init_many and percpu_counter_tree_destroy_many APIs.
- Move tree items allocation to the caller.
- Introduce percpu_counter_tree_items_size().
- Move percpu_counter_tree_subsystem_init() call before mm_core_init() so percpu_counter_tree_items_size() is initialized before it is used.
Changes since v7:
- Explicitly initialize the subsystem from start_kernel() right after mm_core_init() so it is up and running before the creation of the first mm at boot.
- Remove the module.h include which is not needed with the explicit initialization.
- Only consider levels>0 items for order={0,1} nr_items. No functional change except to allocate only the amount of memory which is strictly needed.
- Introduce positive precise sum API to handle a scenario where an unlucky precise sum iteration would hit negative counter values concurrently with counter updates.
Changes since v5:
- Introduce percpu_counter_tree_approximate_sum_positive.
- Introduce !CONFIG_SMP static inlines for UP build.
- Remove percpu_counter_tree_set_bias from the public API and make it static.
Changes since v3:
- Add gfp flags to init function.
Changes since v2:
- Introduce N-way tree to reduce tree depth on larger systems.
Changes since v1:
- Remove percpu_counter_tree_precise_sum_unbiased from public header, make this function static,
- Introduce precise and approximate comparisons between two counters,
- Reorder the struct percpu_counter_tree fields,
- Introduce approx_sum field, which points to the approximate sum for the percpu_counter_tree_approximate_sum() fast path.
--- include/linux/percpu_counter_tree.h | 242 ++++++++++ init/main.c | 2 + lib/Makefile | 1 + lib/percpu_counter_tree.c | 705 ++++++++++++++++++++++++++++ 4 files changed, 950 insertions(+) create mode 100644 include/linux/percpu_counter_tree.h create mode 100644 lib/percpu_counter_tree.c
include/linux/percpu_counter_tree.h new file +242 -0
@@ -0,0 +1,242 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR MIT */ +/* SPDX-FileCopyrightText: 2025 Mathieu Desnoyers <[email protected]> */ + +#ifndef _PERCPU_COUNTER_TREE_H +#define _PERCPU_COUNTER_TREE_H + +#include <linux/preempt.h> +#include <linux/atomic.h> +#include <linux/percpu.h> + +#ifdef CONFIG_SMP + +struct percpu_counter_tree_level_item { +atomic_t count;/* +* Count the number of carry fort this tree item. +* The carry counter is kept at the order of the +* carry accounted for at this tree level. +*/ +}____cacheline_aligned_in_smp ; + +struct percpu_counter_tree { +/* Fast-path fields. */ +unsigned int __percpu *level0;/* Pointer to per-CPU split counters (tree level 0). */ +unsigned int level0_bit_mask;/* Bit mask to apply to detect carry propagation from tree level 0. */ +union { +unsigned int *i;/* Approximate sum for single-CPU topology. */ +atomic_t *a;/* Approximate sum for SMP topology. */ + } approx_sum; +int bias;/* Bias to apply to counter precise and approximate values. */ + +/* Slow-path fields. */ +struct percpu_counter_tree_level_item *items;/* Array of tree items for levels 1 to N. */ +unsigned int batch_size;/* +* The batch size is the increment step at level 0 which +* triggers a carry propagation. The batch size is required +* to be greater than 1, and a power of 2. +*/ +/* +* The tree approximate sum is guaranteed to be within this accuracy range: +* (precise_sum - approx_accuracy_range.under) <= approx_sum <= (precise_sum + approx_accuracy_range.over). +* This accuracy is derived from the hardware topology and the tree batch_size. +* The "under" accuracy is larger than the "over" accuracy because the negative range of a +* two's complement signed integer is one unit larger than the positive range. This delta +* is summed for each tree item, which leads to a significantly larger "under" accuracy range +* compared to the "over" accuracy range. +*/ +struct { +unsigned int under; +unsigned int over; + } approx_accuracy_range; +}; + +size_t percpu_counter_tree_items_size (void ); +int percpu_counter_tree_init_many (struct percpu_counter_tree *counters,struct percpu_counter_tree_level_item *items, +unsigned int nr_counters,unsigned int batch_size,gfp_t gfp_flags); +int percpu_counter_tree_init (struct percpu_counter_tree *counter,struct percpu_counter_tree_level_item *items, +unsigned int batch_size,gfp_t gfp_flags); +void percpu_counter_tree_destroy_many (struct percpu_counter_tree *counter,unsigned int nr_counters); +void percpu_counter_tree_destroy (struct percpu_counter_tree *counter); +void percpu_counter_tree_add (struct percpu_counter_tree *counter,int inc); +int percpu_counter_tree_precise_sum (struct percpu_counter_tree *counter); +int percpu_counter_tree_approximate_compare (struct percpu_counter_tree *a,struct percpu_counter_tree *b); +int percpu_counter_tree_approximate_compare_value (struct percpu_counter_tree *counter,int v); +int percpu_counter_tree_precise_compare (struct percpu_counter_tree *a,struct percpu_counter_tree *b); +int percpu_counter_tree_precise_compare_value (struct percpu_counter_tree *counter,int v); +void percpu_counter_tree_set (struct percpu_counter_tree *counter,int v); +void percpu_counter_tree_approximate_accuracy_range (struct percpu_counter_tree *counter, +unsigned int *under,unsigned int *over); +int percpu_counter_tree_subsystem_init (void ); + +/** +* percpu_counter_tree_approximate_sum() - Return approximate counter sum. +* @counter: The counter to sum. +* +* Querying the approximate sum is fast, but it is only accurate within +* the bounds delimited by percpu_counter_tree_approximate_accuracy_range(). +* This is meant to be used when speed is preferred over accuracy. +* +* Return: The current approximate counter sum. +*/ +static inline +int percpu_counter_tree_approximate_sum (struct percpu_counter_tree *counter) +{ +unsigned int v; + +if (!counter->level0_bit_mask) + v =READ_ONCE (*counter->approx_sum.i); +else + v =atomic_read (counter->approx_sum.a); +return (int ) (v + (unsigned int )READ_ONCE (counter->bias)); +} + +#else /* !CONFIG_SMP */ + +struct percpu_counter_tree_level_item ; + +struct percpu_counter_tree { +atomic_t count; +}; + +static inline +size_t percpu_counter_tree_items_size (void ) +{ +return 0 ; +} + +static inline +int percpu_counter_tree_init_many (struct percpu_counter_tree *counters,struct percpu_counter_tree_level_item *items, +unsigned int nr_counters,unsigned int batch_size,gfp_t gfp_flags) +{ +for (unsigned int i =0 ; i < nr_counters; i++) +atomic_set (&counters[i].count,0 ); +return 0 ; +} + +static inline +int percpu_counter_tree_init (struct percpu_counter_tree *counter,struct percpu_counter_tree_level_item *items, +unsigned int batch_size,gfp_t gfp_flags) +{ +return percpu_counter_tree_init_many (counter, items,1 , batch_size, gfp_flags); +} + +static inline +void percpu_counter_tree_destroy_many (struct percpu_counter_tree *counter,unsigned int nr_counters) +{ +} + +static inline +void percpu_counter_tree_destroy (struct percpu_counter_tree *counter) +{ +} + +static inline +int percpu_counter_tree_precise_sum (struct percpu_counter_tree *counter) +{ +return atomic_read (&counter->count); +} + +static inline +int percpu_counter_tree_precise_compare (struct percpu_counter_tree *a,struct percpu_counter_tree *b) +{ +int count_a =percpu_counter_tree_precise_sum (a), + count_b =percpu_counter_tree_precise_sum (b); + +if (count_a == count_b) +return 0 ; +if (count_a < count_b) +return -1 ; +return 1 ; +} + +static inline +int percpu_counter_tree_precise_compare_value (struct percpu_counter_tree *counter,int v) +{ +int count =percpu_counter_tree_precise_sum (counter); + +if (count == v) +return 0 ; +if (count < v) +return -1 ; +return 1 ; +} + +static inline +int percpu_counter_tree_approximate_compare (struct percpu_counter_tree *a,struct percpu_counter_tree *b) +{ +return percpu_counter_tree_precise_compare (a, b); +} + +static inline +int percpu_counter_tree_approximate_compare_value (struct percpu_counter_tree *counter,int v) +{ +return percpu_counter_tree_precise_compare_value (counter, v); +} + +static inline +void percpu_counter_tree_set (struct percpu_counter_tree *counter,int v) +{ +atomic_set (&counter->count, v); +} + +static inline +void percpu_counter_tree_approximate_accuracy_range (struct percpu_counter_tree *counter, +unsigned int *under,unsigned int *over) +{ + *under =0 ; + *over =0 ; +} + +static inline +void percpu_counter_tree_add (struct percpu_counter_tree *counter,int inc) +{ +atomic_add (inc, &counter->count); +} + +static inline +int percpu_counter_tree_approximate_sum (struct percpu_counter_tree *counter) +{ +return percpu_counter_tree_precise_sum (counter); +} + +static inline +int percpu_counter_tree_subsystem_init (void ) +{ +return 0 ; +} + +#endif /* CONFIG_SMP */ + +/** +* percpu_counter_tree_approximate_sum_positive() - Return a positive approximate counter sum. +* @counter: The counter to sum. +* +* Return an approximate counter sum which is guaranteed to be greater +* or equal to 0. +* +* Return: The current positive approximate counter sum. +*/ +static inline +int percpu_counter_tree_approximate_sum_positive (struct percpu_counter_tree *counter) +{ +int v =percpu_counter_tree_approximate_sum (counter); +return v >0 ? v :0 ; +} + +/** +* percpu_counter_tree_precise_sum_positive() - Return a positive precise counter sum. +* @counter: The counter to sum. +* +* Return a precise counter sum which is guaranteed to be greater +* or equal to 0. +* +* Return: The current positive precise counter sum. +*/ +static inline +int percpu_counter_tree_precise_sum_positive (struct percpu_counter_tree *counter) +{ +int v =percpu_counter_tree_precise_sum (counter); +return v >0 ? v :0 ; +} + +#endif /* _PERCPU_COUNTER_TREE_H */
init/main.c +2 -0
@@ -104,6 +104,7 @@#include <linux/pidfs.h> #include <linux/ptdump.h> #include <linux/time_namespace.h> +#include <linux/percpu_counter_tree.h> #include <net/net_namespace.h> #include <asm/io.h>
@@ -968,6 +969,7 @@ void start_kernel(void)vfs_caches_init_early ();sort_main_extable ();trap_init (); +percpu_counter_tree_subsystem_init ();mm_core_init ();maple_tree_init ();poking_init ();
lib/Makefile +1 -0
@@ -179,6 +179,7 @@ obj-$(CONFIG_TEXTSEARCH_KMP) += ts_kmp.o obj-$(CONFIG_TEXTSEARCH_BM) += ts_bm.o obj-$(CONFIG_TEXTSEARCH_FSM) += ts_fsm.o obj-$(CONFIG_SMP) += percpu_counter.o +obj-$(CONFIG_SMP) += percpu_counter_tree.o obj-$(CONFIG_AUDIT_GENERIC) += audit.o obj-$(CONFIG_AUDIT_COMPAT_GENERIC) += compat_audit.o
lib/percpu_counter_tree.c new file +705 -0
705 lines not on this page · raw message
Signature · 1 line
2.39.5
Mathieu Desnoyers
reviewerPATCH v10 2/3 mm: Fix OOM killer inaccuracy on large many-core systems
In reply to Mathieu DesnoyersUse hierarchical per-cpu counters for rss tracking to fix the per-mm RSS tracking which has become too inaccurate for OOM killer purposes on large many-core systems.
The following rss tracking issues were noted by Sweet Tea Dorminy [1], which lead to picking wrong tasks as OOM kill target:
Recently, several internal services had an RSS usage regression as part of a kernel upgrade. Previously, they were on a pre-6.2 kernel and were able to read RSS statistics in a backup watchdog process to monitor and decide if they'd overrun their memory budget. Now, however, a representative service with five threads, expected to use about a hundred MB of memory, on a 250-cpu machine had memory usage tens of megabytes different from the expected amount -- this constituted a significant percentage of inaccuracy, causing the watchdog to act.
This was a result of commit f1a7941243c1 ("mm: convert mm's rss stats into percpu_counter") [1]. Previously, the memory error was bounded by 64*nr_threads pages, a very livable megabyte. Now, however, as a result of scheduler decisions moving the threads around the CPUs, the memory error could be as large as a gigabyte.
This is a really tremendous inaccuracy for any few-threaded program on a large machine and impedes monitoring significantly. These stat counters are also used to make OOM killing decisions, so this additional inaccuracy could make a big difference in OOM situations -- either resulting in the wrong process being killed, or in less memory being returned from an OOM-kill than expected.
Here is a (possibly incomplete) list of the prior approaches that were used or proposed, along with their downside:
1) Per-thread rss tracking: large error on many-thread processes.
2) Per-CPU counters: up to 12% slower for short-lived processes and 9% increased system time in make test workloads [1]. Moreover, the inaccuracy increases with O(n^2) with the number of CPUs.
3) Per-NUMA-node counters: requires atomics on fast-path (overhead), error is high with systems that have lots of NUMA nodes (32 times the number of NUMA nodes).
The approach proposed here is to replace this by the hierarchical per-cpu counters, which bounds the inaccuracy based on the system topology with O(N*logN).
commit 82241a83cd15 ("mm: fix the inaccurate memory statistics issue for users") introduced get_mm_counter_sum() for precise /proc memory status queries. Implement it with percpu_counter_tree_precise_sum() since it is not a fast path and precision is preferred over speed.
* Testing results:
Test hardware: 2 sockets AMD EPYC 9654 96-Core Processor (384 logical CPUs total)
Methodology:
Comparing the current upstream implementation with the hierarchical counters is done by keeping both implementations wired up in parallel, and running a single-process, single-threaded program which hops randomly across CPUs in the system, calling mmap(2) and munmap(2) on random CPUs, keeping track of an array of allocated mappings, randomly choosing entries to either map or unmap.
get_mm_counter() is instrumented to compare the upstream counter approximation to the precise value, and print the delta when going over a given threshold. The delta of the hierarchical counter approximation to the precise value is also printed for comparison.
After a few minutes running this test, the upstream implementation counter approximation reaches a 1GB delta from the precise value, compared to 80MB delta with the hierarchical counter. The hierarchical counter provides a guaranteed maximum approximation inaccuracy of 192MB on that hardware topology.
* Fast path implementation comparison
The new inline percpu_counter_tree_add() uses a this_cpu_add_return() for the fast path (under a certain allocation size threshold). Above that, it calls a slow path which "trickles up" the carry to upper level counters with atomic_add_return.
In comparison, the upstream counters implementation calls percpu_counter_add_batch which uses this_cpu_try_cmpxchg() on the fast path, and does a raw_spin_lock_irqsave above a certain threshold.
The hierarchical implementation is therefore expected to have less contention on mid-sized allocations than the upstream counters because the atomic counters tracking those bits are only shared across nearby CPUs. In comparison, the upstream counters immediately use a global spinlock when reaching the threshold.
* Benchmarks
Using will-it-scale page_fault1 benchmarks to compare the upstream counters to the hierarchical counters. This is done with hyperthreading disabled. The speedup is within the standard deviation of the upstream runs, so the overhead is not significant.
upstream hierarchical speedup
page_fault1_processes -s 100 -t 1 614783 615558 +0.1% page_fault1_threads -s 100 -t 1 612788 612447 -0.1% page_fault1_processes -s 100 -t 96 37994977 37932035 -0.2% page_fault1_threads -s 100 -t 96 2484130 2504860 +0.8% page_fault1_processes -s 100 -t 192 71262917 71118830 -0.2% page_fault1_threads -s 100 -t 192 2446437 2469296 +0.1%
---
Changes since v8:
- Use percpu_counter_tree_init_many and percpu_counter_tree_destroy_many APIs.
- Remove percpu tree items allocation. Extend mm_struct size to include rss items. Those are handled through the new helpers get_rss_stat_items() and get_rss_stat_items_size() and passed as parameter to percpu_counter_tree_init_many().
Changes since v7:
- Use precise sum positive API to handle a scenario where an unlucky precise sum iteration would observe negative counter values due to concurrent updates.
Changes since v6:
- Rebased on v6.18-rc3.
- Implement get_mm_counter_sum as percpu_counter_tree_precise_sum for /proc virtual files memory state queries.
Changes since v5:
- Use percpu_counter_tree_approximate_sum_positive.
Change since v4:
- get_mm_counter needs to return 0 or a positive value.
get_mm_counter_sum -> precise sum positive
--- include/linux/mm.h | 28 +++++++++++++++++++++++----- include/linux/mm_types.h | 4 ++-- include/trace/events/kmem.h | 2 +- kernel/fork.c | 24 ++++++++++++++---------- 4 files changed, 40 insertions(+), 18 deletions(-)
include/linux/mm.h +23 -5
@@ -2681,38 +2681,56 @@ static inline bool get_user_page_fast_only(unsigned long addr, {return get_user_pages_fast_only (addr,1 , gup_flags, pagep) ==1 ; } + +static inline size_t get_rss_stat_items_size (void ) +{ +return percpu_counter_tree_items_size () *NR_MM_COUNTERS ; +} + +static inline struct percpu_counter_tree_level_item *get_rss_stat_items (struct mm_struct *mm) +{ +unsigned long ptr = (unsigned long )mm; + + ptr += offsetof(struct mm_struct , cpu_bitmap); +/* Skip cpu_bitmap */ + ptr +=cpumask_size (); +/* Skip mm_cidmask */ + ptr +=mm_cid_size (); +return (struct percpu_counter_tree_level_item *)ptr; +} +/* * per-process(per-mm_struct) statistics. */ static inline unsigned long get_mm_counter (struct mm_struct *mm,int member) { -return percpu_counter_read_positive (&mm->rss_stat[member]); +return percpu_counter_tree_approximate_sum_positive (&mm->rss_stat[member]); }static inline unsigned long get_mm_counter_sum (struct mm_struct *mm,int member) { -return percpu_counter_sum_positive (&mm->rss_stat[member]); +return percpu_counter_tree_precise_sum_positive (&mm->rss_stat[member]); }void mm_trace_rss_stat (struct mm_struct *mm,int member);static inline void add_mm_counter (struct mm_struct *mm,int member,long value) { -percpu_counter_add (&mm->rss_stat[member], value); +percpu_counter_tree_add (&mm->rss_stat[member], value);mm_trace_rss_stat (mm, member); }static inline void inc_mm_counter (struct mm_struct *mm,int member) { -percpu_counter_inc (&mm->rss_stat[member]); +percpu_counter_tree_add (&mm->rss_stat[member],1 );mm_trace_rss_stat (mm, member); }static inline void dec_mm_counter (struct mm_struct *mm,int member) { -percpu_counter_dec (&mm->rss_stat[member]); +percpu_counter_tree_add (&mm->rss_stat[member],-1 );mm_trace_rss_stat (mm, member); }
include/linux/mm_types.h +2 -2
@@ -18,7 +18,7 @@#include <linux/page-flags-layout.h> #include <linux/workqueue.h> #include <linux/seqlock.h> -#include <linux/percpu_counter.h> +#include <linux/percpu_counter_tree.h> #include <linux/types.h> #include <linux/bitmap.h>
@@ -1119,7 +1119,7 @@ struct mm_struct {unsigned long saved_e_flags;#endif -struct percpu_counter rss_stat[NR_MM_COUNTERS ]; +struct percpu_counter_tree rss_stat[NR_MM_COUNTERS ];struct linux_binfmt *binfmt;
include/trace/events/kmem.h +1 -1
@@ -442,7 +442,7 @@ TRACE_EVENT(rss_stat, __entry->mm_id =mm_ptr_to_hash (mm); __entry->curr = !!(current->mm == mm); __entry->member = member; - __entry->size = (percpu_counter_sum_positive (&mm->rss_stat[member]) + __entry->size = (percpu_counter_tree_approximate_sum_positive (&mm->rss_stat[member]) <<PAGE_SHIFT ); ),
kernel/fork.c +14 -10
@@ -133,6 +133,11 @@*/ #define MAX_THREADS FUTEX_TID_MASK +/* +* Batch size of rss stat approximation +*/ +#define RSS_STAT_BATCH_SIZE 32 +/* * Protected counters by write_lock_irq(&tasklist_lock) */
@@ -583,14 +588,12 @@ static void check_mm(struct mm_struct *mm) "Please make sure 'struct resident_page_types[]' is updated as well");for (i =0 ; i <NR_MM_COUNTERS ; i++) { -long x =percpu_counter_sum (&mm->rss_stat[i]); - -if (unlikely (x)) { -pr_alert ("BUG: Bad rss-counter state mm:%p type:%s val:%ld Comm:%s Pid:%d\n" , - mm, resident_page_types[i], x, +if (unlikely (percpu_counter_tree_precise_compare_value (&mm->rss_stat[i],0 ) !=0 )) +pr_alert ("BUG: Bad rss-counter state mm:%p type:%s val:%d Comm:%s Pid:%d\n" , + mm, resident_page_types[i], +percpu_counter_tree_precise_sum (&mm->rss_stat[i]), current->comm,task_pid_nr (current)); - } }if (mm_pgtables_bytes (mm))
@@ -688,7 +691,7 @@ void __mmdrop(struct mm_struct *mm)put_user_ns (mm->user_ns);mm_pasid_drop (mm);mm_destroy_cid (mm); -percpu_counter_destroy_many (mm->rss_stat,NR_MM_COUNTERS ); +percpu_counter_tree_destroy_many (mm->rss_stat,NR_MM_COUNTERS );free_mm (mm); }
@@ -1083,8 +1086,9 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,if (mm_alloc_cid (mm, p)) goto fail_cid; -if (percpu_counter_init_many (mm->rss_stat,0 ,GFP_KERNEL_ACCOUNT , -NR_MM_COUNTERS )) +if (percpu_counter_tree_init_many (mm->rss_stat,get_rss_stat_items (mm), +NR_MM_COUNTERS ,RSS_STAT_BATCH_SIZE , +GFP_KERNEL_ACCOUNT )) goto fail_pcpu; mm->user_ns =get_user_ns (user_ns);
@@ -2964,7 +2968,7 @@ void __init mm_cache_init(void)* dynamically sized based on the maximum CPU number this system * can have, taking hotplug into account (nr_cpu_ids). */ - mm_size =sizeof (struct mm_struct ) +cpumask_size () +mm_cid_size (); + mm_size =sizeof (struct mm_struct ) +cpumask_size () +mm_cid_size () +get_rss_stat_items_size (); mm_cachep = kmem_cache_create_usercopy("mm_struct", mm_size, ARCH_MIN_MMSTRUCT_ALIGN,
Signature · 1 line
2.39.5
Quoted 3 lines · Mathieu Desnoyers
Use hierarchical per-cpu counters for rss tracking to fix the per-mm RSS tracking which has become too inaccurate for OOM killer purposes on large many-core systems.
We're seeing boot time crashes in -next on the Arm FVP and Ampere Altra which bisect to this patch which is commit 240587b6cca2822d. Many other platforms aren't showing this, though we do have some other breakage in -next which might be obscuring things. We get a NULL dereference:
[ 2.481143] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000
...
[ 2.485036] Call trace: [ 2.485094] acct_account_cputime+0x40/0xa4 (P) [ 2.485226] irqtime_account_process_tick+0x17c/0x1d8 [ 2.485382] account_process_tick+0x12c/0x148 [ 2.485531] update_process_times+0x28/0xdc [ 2.485656] tick_nohz_handler+0xbc/0x1bc [ 2.485809] __hrtimer_run_queues+0x130/0x184
I note that __acct_update_integrals is being called from here most likely inline and doing get_mm_rss(). That uses get_mm_counter() which we've updated in this patch, though I didn't spot the specific issue yet.
Full log:
https://lava.sirena.org.uk/scheduler/job/2269797#L1305
Bisect log:
# bad: [1058ca9db0edaedcb16480cc74b78ed06f0d1f54] Add linux-next specific files for 20251218
# good: [b67535593a28aff9d355799ec5efc2e90bc405a6] Merge branch 'for-linux-next-fixes' of https://gitlab.freedesktop.org/drm/misc/kernel.git
# good: [f4acea9eef704607d1a950909ce3a52a770d6be2] spi: dt-bindings: st,stm32-spi: add 'power-domains' property
# good: [f25c7d709b93602ee9a08eba522808a18e1f5d56] ASoC: SOF: Intel: pci-nvl: Set on_demand_dsp_boot for NVL-S
# good: [524ee559948d8d079b13466e70fa741f909699c0] ASoC: SOF: Intel: hda: Only check SSP MCLK mask in case of IPC3
# good: [fa08b566860bca8ebf9300090b85174c34de7ca5] spi: rzv2h-rspi: add support for DMA mode
# good: [fee876b2ec75dcc18fdea154eae1f5bf14d82659] spi: stm32-qspi: Simplify SMIE interrupt test
# good: [b884e34994ca41f7b7819f3c41b78ff494787b27] spi: spi-fsl-lpspi: convert min_t() to simple min()
# good: [ba9b28652c75b07383e267328f1759195d5430f7] spi: imx: enable DMA mode for target operation
# good: [124f6155f3d97b0e33f178c10a5138a42c8fd207] ASoC: renesas: rz-ssi: Add support for 32 bits sample width
# good: [aa30193af8873b3ccfd70a4275336ab6cbd4e5e6] ASoC: Intel: catpt: Drop superfluous space in PCM code
# good: [9e92c559d49d6fb903af17a31a469aac51b1766d] regulator: max77675: Add MAX77675 regulator driver
# good: [81acbdc51bbbec822a1525481f2f70677c47aee0] ASoC: sdw-mockup: Drop dummy remove function
# good: [0bb160c92ad400c692984763996b758458adea17] ASoC: qcom: Minor readability improve with new lines
# good: [03d281f384768610bf90697bce9e35d3d596de77] rust: regulator: add __rust_helper to helpers
# good: [e39011184f23de3d04ca8e80b4df76c9047b4026] ASoC: SDCA: functions: Fix confusing cleanup.h syntax
git bisect start '1058ca9db0edaedcb16480cc74b78ed06f0d1f54' 'b67535593a28aff9d355799ec5efc2e90bc405a6' 'f4acea9eef704607d1a950909ce3a52a770d6be2' 'f25c7d709b93602ee9a08eba522808a18e1f5d56' '524ee559948d8d079b13466e70fa741f909699c0' 'fa08b566860bca8ebf9300090b85174c34de7ca5' 'fee876b2ec75dcc18fdea154eae1f5bf14d82659' 'b884e34994ca41f7b7819f3c41b78ff494787b27' 'ba9b28652c75b07383e267328f1759195d5430f7' '124f6155f3d97b0e33f178c10a5138a42c8fd207' 'aa30193af8873b3ccfd70a4275336ab6cbd4e5e6' '9e92c559d49d6fb903af17a31a469aac51b1766d' '81acbdc51bbbec822a1525481f2f70677c47aee0' '0bb160c92ad400c692984763996b758458adea17' '03d281f384768610bf90697bce9e35d3d596de77' 'e39011184f23de3d04ca8e80b4df76c9047b4026'
# test job: [f4acea9eef704607d1a950909ce3a52a770d6be2] https://lava.sirena.org.uk/scheduler/job/2243946 # test job: [f25c7d709b93602ee9a08eba522808a18e1f5d56] https://lava.sirena.org.uk/scheduler/job/2244079 # test job: [524ee559948d8d079b13466e70fa741f909699c0] https://lava.sirena.org.uk/scheduler/job/2243984 # test job: [fa08b566860bca8ebf9300090b85174c34de7ca5] https://lava.sirena.org.uk/scheduler/job/2232928 # test job: [fee876b2ec75dcc18fdea154eae1f5bf14d82659] https://lava.sirena.org.uk/scheduler/job/2231264 # test job: [b884e34994ca41f7b7819f3c41b78ff494787b27] https://lava.sirena.org.uk/scheduler/job/2231779 # test job: [ba9b28652c75b07383e267328f1759195d5430f7] https://lava.sirena.org.uk/scheduler/job/2231420 # test job: [124f6155f3d97b0e33f178c10a5138a42c8fd207] https://lava.sirena.org.uk/scheduler/job/2232853 # test job: [aa30193af8873b3ccfd70a4275336ab6cbd4e5e6] https://lava.sirena.org.uk/scheduler/job/2232678 # test job: [9e92c559d49d6fb903af17a31a469aac51b1766d] https://lava.sirena.org.uk/scheduler/job/2232518 # test job: [81acbdc51bbbec822a1525481f2f70677c47aee0] https://lava.sirena.org.uk/scheduler/job/2232960 # test job: [0bb160c92ad400c692984763996b758458adea17] https://lava.sirena.org.uk/scheduler/job/2233063 # test job: [03d281f384768610bf90697bce9e35d3d596de77] https://lava.sirena.org.uk/scheduler/job/2231118 # test job: [e39011184f23de3d04ca8e80b4df76c9047b4026] https://lava.sirena.org.uk/scheduler/job/2232449 # test job: [1058ca9db0edaedcb16480cc74b78ed06f0d1f54] https://lava.sirena.org.uk/scheduler/job/2269797 # bad: [1058ca9db0edaedcb16480cc74b78ed06f0d1f54] Add linux-next specific files for 20251218 git bisect bad 1058ca9db0edaedcb16480cc74b78ed06f0d1f54 # test job: [066839a14b076089272a60ed81f11e423d5c9361] https://lava.sirena.org.uk/scheduler/job/2270122 # bad: [066839a14b076089272a60ed81f11e423d5c9361] Merge branch 'for-linux-next' of https://gitlab.freedesktop.org/drm/misc/kernel.git git bisect bad 066839a14b076089272a60ed81f11e423d5c9361 # test job: [3f506139d1ada1f7dbb8593973ed287379747c06] https://lava.sirena.org.uk/scheduler/job/2270335 # bad: [3f506139d1ada1f7dbb8593973ed287379747c06] Merge branch 'xtensa-for-next' of https://github.com/jcmvbkbc/linux-xtensa.git git bisect bad 3f506139d1ada1f7dbb8593973ed287379747c06 # test job: [b5d3cb02801b2e109f9dd0e5e39ca47ab1edaf14] https://lava.sirena.org.uk/scheduler/job/2270661 # bad: [b5d3cb02801b2e109f9dd0e5e39ca47ab1edaf14] Merge branch 'for-next' of https://git.kernel.org/pub/scm/linux/kernel/git/khilman/linux-omap.git git bisect bad b5d3cb02801b2e109f9dd0e5e39ca47ab1edaf14 # test job: [8cf5d38999d1dca70f34de411b72a099d07c1b6a] https://lava.sirena.org.uk/scheduler/job/2270863 # bad: [8cf5d38999d1dca70f34de411b72a099d07c1b6a] Merge branch 'kbuild-next' of https://git.kernel.org/pub/scm/linux/kernel/git/kbuild/linux.git git bisect bad 8cf5d38999d1dca70f34de411b72a099d07c1b6a # test job: [6f7df192578220290c5ee01dc146f01c919fdb7b] https://lava.sirena.org.uk/scheduler/job/2271024 # good: [6f7df192578220290c5ee01dc146f01c919fdb7b] kallsyms/bpf: rename __bpf_address_lookup() to bpf_address_lookup() git bisect good 6f7df192578220290c5ee01dc146f01c919fdb7b # test job: [a525b83d913f000bed66b69f6d9c05c0c04551dd] https://lava.sirena.org.uk/scheduler/job/2271528 # bad: [a525b83d913f000bed66b69f6d9c05c0c04551dd] mm: add basic tests for lazy_mmu git bisect bad a525b83d913f000bed66b69f6d9c05c0c04551dd # test job: [667c24fb34a273ffc323d591ac628285602bd324] https://lava.sirena.org.uk/scheduler/job/2271625 # bad: [667c24fb34a273ffc323d591ac628285602bd324] sparc/mm: implement arch_flush_lazy_mmu_mode() git bisect bad 667c24fb34a273ffc323d591ac628285602bd324 # test job: [d70090581c46c001d0886afbaf08bcbc85a5e8bc] https://lava.sirena.org.uk/scheduler/job/2271840 # bad: [d70090581c46c001d0886afbaf08bcbc85a5e8bc] mm: implement precise OOM killer task selection git bisect bad d70090581c46c001d0886afbaf08bcbc85a5e8bc # test job: [eb526e6344d1dd7784bef5aa4cbe7f7fada3bf12] https://lava.sirena.org.uk/scheduler/job/2271925 # good: [eb526e6344d1dd7784bef5aa4cbe7f7fada3bf12] mm/damon/core: fix memory leak of repeat mode damon_call_control objects git bisect good eb526e6344d1dd7784bef5aa4cbe7f7fada3bf12 # test job: [240587b6cca2822dd579caa0ff05a7f5e459c597] https://lava.sirena.org.uk/scheduler/job/2272240 # bad: [240587b6cca2822dd579caa0ff05a7f5e459c597] mm: fix OOM killer inaccuracy on large many-core systems git bisect bad 240587b6cca2822dd579caa0ff05a7f5e459c597 # test job: [f9ff5ba6bbfcc8f8a61cd7dd61a0c33b7c4deb30] https://lava.sirena.org.uk/scheduler/job/2272347 # good: [f9ff5ba6bbfcc8f8a61cd7dd61a0c33b7c4deb30] lib: introduce hierarchical per-cpu counters git bisect good f9ff5ba6bbfcc8f8a61cd7dd61a0c33b7c4deb30 # first bad commit: [240587b6cca2822dd579caa0ff05a7f5e459c597] mm: fix OOM killer inaccuracy on large many-core systems
Mathieu Desnoyers
reviewerQuoted 23 lines · Mark Brown
Quoted 3 lines · Mathieu Desnoyers
Use hierarchical per-cpu counters for rss tracking to fix the per-mm RSS tracking which has become too inaccurate for OOM killer purposes on large many-core systems.
We're seeing boot time crashes in -next on the Arm FVP and Ampere Altra which bisect to this patch which is commit
240587b6cca2822d. Many other platforms aren't showing this, though we do have some other breakage in -next which might be obscuring things. We get a NULL dereference:[ 2.481143] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000...
[ 2.485036] Call trace: [ 2.485094] acct_account_cputime+0x40/0xa4 (P) [ 2.485226] irqtime_account_process_tick+0x17c/0x1d8 [ 2.485382] account_process_tick+0x12c/0x148 [ 2.485531] update_process_times+0x28/0xdc [ 2.485656] tick_nohz_handler+0xbc/0x1bc [ 2.485809] __hrtimer_run_queues+0x130/0x184
Quoted 5 lines
I note that
__acct_update_integralsis being called from here most likely inline and doingget_mm_rss(). That usesget_mm_counter()which we've updated in this patch, though I didn't spot the specific issue yet.
There is something fishy in mm/init-mm.c:init_mm. The initialization of
.cpu_bitmap = CPU_BITS_NONE,
Keeps room for a NR_CPUs cpumask in that structure, but does not take into account the new extra room needed for mm_cid and the hierarchical per-cpu counters:
in mm_cache_init() we have:
mm_size = sizeof(struct mm_struct) + cpumask_size() + mm_cid_size() + get_rss_stat_items_size();
So AFAIU we should extend this end-of-mm size to include room for mm_cid_size() (2 * cpumask_size), which would be an upstream bug, and now room for get_rss_stat_items_size() (which is an issue specific to -next due to hierarchical per-cpu counters).
An ugly work-around that may work (and then we can improve on this), at the end of mm/init-mm.c:init_mm (completely untested):
.cpu_bitmap = { [0 ... ((3*BITS_TO_LONGS(NR_CPUS))-1 + ((69905 * NR_MM_COUNTERS * 64) / BYTES_PER_LONG))] = 0UL },Thoughts ?
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Quoted 6 lines · Mathieu Desnoyers
On 2025-12-18 13:00, Mark Brown wrote:
An ugly work-around that may work (and then we can improve on this), at the end of mm/init-mm.c:init_mm (completely untested):
.cpu_bitmap = { [0 ... ((3*BITS_TO_LONGS(NR_CPUS))-1 + ((69905 * NR_MM_COUNTERS * 64) / BYTES_PER_LONG))] = 0UL },
That doesn't seem to fix the FVP unfortunately (BYTES_PER_LONG doesn't exist, but even just deleting the division entirely fails in the same way).
Mathieu Desnoyers
reviewerQuoted 11 lines · Mark Brown
Quoted 6 lines · Mathieu Desnoyers
On 2025-12-18 13:00, Mark Brown wrote:
An ugly work-around that may work (and then we can improve on this), at the end of mm/init-mm.c:init_mm (completely untested):
.cpu_bitmap = { [0 ... ((3*BITS_TO_LONGS(NR_CPUS))-1 + ((69905 * NR_MM_COUNTERS * 64) / BYTES_PER_LONG))] = 0UL },That doesn't seem to fix the FVP unfortunately (
BYTES_PER_LONGdoesn't exist, but even just deleting the division entirely fails in the same way).
I just noticed that there is another static instance of mm_struct:
drivers/firmware/efi/efi.c struct mm_struct efi_mm
we need to apply the same fix to it as well. It seems to fit with the currently running task when the oops happens:
[ 2.482454] CPU: 2 UID: 0 PID: 12 Comm: kworker/u32:0 Not tainted 6.19.0-rc1-next-20251218 #1 PREEMPT [ 2.482609] Workqueue: efi_rts_wq efi_call_rts
[...]
[ 2.485094] acct_account_cputime+0x40/0xa4 (P) [...] [ 2.487172] el1h_64_irq+0x6c/0x70 [...] [ 2.487853] __efi_rt_asm_wrapper+0x50/0x74
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Mathieu Desnoyers
reviewerPATCH v10 3/3 mm: Implement precise OOM killer task selection
In reply to Mathieu DesnoyersUse the hierarchical tree counter approximation to implement the OOM killer task selection with a 2-pass algorithm. The first pass selects the process that has the highest badness points approximation, and the second pass compares each process using the current max badness points approximation.
The second pass uses an approximate comparison to eliminate all processes which are below the current max badness points approximation accuracy range.
Summing the per-CPU counters to calculate the precise badness of tasks is only required for tasks with an approximate badness within the accuracy range of the current max points value.
Limit to 16 the maximum number of badness sums allowed for an OOM killer task selection before falling back to the approximated comparison. This ensures bounded execution time for scenarios where many tasks have badness within the accuracy of the maximum badness approximation.
Tested with the following script:
#!/bin/sh
for a in $(seq 1 10); do (tail /dev/zero &); done sleep 5 for a in $(seq 1 10); do (tail /dev/zero &); done sleep 2 for a in $(seq 1 10); do (tail /dev/zero &); done echo "Waiting for tasks to finish" wait
Results: OOM kill order on a 128GB memory system
================================================
* systemd and sd-pam are chosen first due to their oom_score_ajd:100:
Out of memory: Killed process 3502 (systemd) total-vm:20096kB, anon-rss:0kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:72kB oom_score_adj:100 Out of memory: Killed process 3503 ((sd-pam)) total-vm:21432kB, anon-rss:0kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:76kB oom_score_adj:100
* The first batch of 10 processes are gradually killed, consecutively picking the one that uses the most memory. The fact that we are freeing memory from the previous processes increases the threshold at which the remaining processes of that group are killed. Processes from the second and third batches of 10 processes have time to start before we complete killing the first 10 processes:
Out of memory: Killed process 3703 (tail) total-vm:6591280kB, anon-rss:6578176kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:12936kB oom_score_adj:0 Out of memory: Killed process 3705 (tail) total-vm:6731716kB, anon-rss:6709248kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:13212kB oom_score_adj:0 Out of memory: Killed process 3707 (tail) total-vm:6977216kB, anon-rss:6946816kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:13692kB oom_score_adj:0 Out of memory: Killed process 3699 (tail) total-vm:7205640kB, anon-rss:7184384kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:14136kB oom_score_adj:0 Out of memory: Killed process 3713 (tail) total-vm:7463204kB, anon-rss:7438336kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:14644kB oom_score_adj:0 Out of memory: Killed process 3701 (tail) total-vm:7739204kB, anon-rss:7716864kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:15180kB oom_score_adj:0 Out of memory: Killed process 3709 (tail) total-vm:8050176kB, anon-rss:8028160kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:15792kB oom_score_adj:0 Out of memory: Killed process 3711 (tail) total-vm:8362236kB, anon-rss:8339456kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:16404kB oom_score_adj:0 Out of memory: Killed process 3715 (tail) total-vm:8649360kB, anon-rss:8634368kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:16972kB oom_score_adj:0 Out of memory: Killed process 3697 (tail) total-vm:8951788kB, anon-rss:8929280kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:17560kB oom_score_adj:0
* Even though there is a 2 seconds delay between the 2nd and 3rd batches those appear to execute in mixed order. Therefore, let's consider them as a single batch of 20 processes. We are hitting oom at a lower memory threshold because at this point the 20 remaining proceses are running rather than the previous 10. The process with highest memory usage is selected for oom, thus making room for the remaining processes so they can use more memory before they fill the available memory, thus explaining why the memory use for selected processes gradually increases, until all system memory is used by the last one:
Out of memory: Killed process 3731 (tail) total-vm:7089868kB, anon-rss:7077888kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:13912kB oom_score_adj:0 Out of memory: Killed process 3721 (tail) total-vm:7417248kB, anon-rss:7405568kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:14556kB oom_score_adj:0 Out of memory: Killed process 3729 (tail) total-vm:7795864kB, anon-rss:7766016kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:15300kB oom_score_adj:0 Out of memory: Killed process 3723 (tail) total-vm:8259620kB, anon-rss:8224768kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:16208kB oom_score_adj:0 Out of memory: Killed process 3737 (tail) total-vm:8695984kB, anon-rss:8667136kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:17060kB oom_score_adj:0 Out of memory: Killed process 3735 (tail) total-vm:9295980kB, anon-rss:9265152kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:18240kB oom_score_adj:0 Out of memory: Killed process 3727 (tail) total-vm:9907900kB, anon-rss:9895936kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:19428kB oom_score_adj:0 Out of memory: Killed process 3719 (tail) total-vm:10631248kB, anon-rss:10600448kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:20844kB oom_score_adj:0 Out of memory: Killed process 3733 (tail) total-vm:11341720kB, anon-rss:11321344kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:22232kB oom_score_adj:0 Out of memory: Killed process 3725 (tail) total-vm:12348124kB, anon-rss:12320768kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:24204kB oom_score_adj:0 Out of memory: Killed process 3759 (tail) total-vm:12978888kB, anon-rss:12967936kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:25440kB oom_score_adj:0 Out of memory: Killed process 3751 (tail) total-vm:14386412kB, anon-rss:14352384kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:28196kB oom_score_adj:0 Out of memory: Killed process 3741 (tail) total-vm:16153168kB, anon-rss:16130048kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:31652kB oom_score_adj:0 Out of memory: Killed process 3753 (tail) total-vm:18414856kB, anon-rss:18391040kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:36076kB oom_score_adj:0 Out of memory: Killed process 3745 (tail) total-vm:21389456kB, anon-rss:21356544kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:41904kB oom_score_adj:0 Out of memory: Killed process 3747 (tail) total-vm:25659348kB, anon-rss:25632768kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:50260kB oom_score_adj:0 Out of memory: Killed process 3755 (tail) total-vm:32030820kB, anon-rss:32006144kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:62720kB oom_score_adj:0 Out of memory: Killed process 3743 (tail) total-vm:42648456kB, anon-rss:42614784kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:83504kB oom_score_adj:0 Out of memory: Killed process 3757 (tail) total-vm:63971028kB, anon-rss:63938560kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:125228kB oom_score_adj:0 Out of memory: Killed process 3749 (tail) total-vm:127799660kB, anon-rss:127778816kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:250140kB oom_score_adj:0
--- fs/proc/base.c | 2 +- include/linux/mm.h | 34 +++++++++++++++++---- include/linux/oom.h | 12 +++++++- mm/oom_kill.c | 72 +++++++++++++++++++++++++++++++++++++-------- 4 files changed, 101 insertions(+), 19 deletions(-)
fs/proc/base.c +1 -1
@@ -589,7 +589,7 @@ static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,unsigned long points =0 ;long badness; - badness =oom_badness (task, totalpages); + badness =oom_badness (task, totalpages, false,NULL ,NULL );/* * Special case OOM_SCORE_ADJ_MIN for all others scale the * badness value into [0, 2000] range which we have been
include/linux/mm.h +29 -5
@@ -2702,14 +2702,32 @@ static inline struct percpu_counter_tree_level_item *get_rss_stat_items(struct m/* * per-process(per-mm_struct) statistics. */ +static inline unsigned long __get_mm_counter (struct mm_struct *mm,int member,bool approximate, +unsigned int *accuracy_under,unsigned int *accuracy_over) +{ +if (approximate) { +if (accuracy_under && accuracy_over) { +unsigned int under, over; + +percpu_counter_tree_approximate_accuracy_range (&mm->rss_stat[member], &under, &over); + *accuracy_under += under; + *accuracy_over += over; + } +return percpu_counter_tree_approximate_sum_positive (&mm->rss_stat[member]); + }else { +return percpu_counter_tree_precise_sum_positive (&mm->rss_stat[member]); + } +} +static inline unsigned long get_mm_counter (struct mm_struct *mm,int member) { -return percpu_counter_tree_approximate_sum_positive (&mm->rss_stat[member]); +return __get_mm_counter (mm, member, true,NULL ,NULL ); } +static inline unsigned long get_mm_counter_sum (struct mm_struct *mm,int member) { -return percpu_counter_tree_precise_sum_positive (&mm->rss_stat[member]); +return get_mm_counter (mm, member); }void mm_trace_rss_stat (struct mm_struct *mm,int member);
@@ -2750,11 +2768,17 @@ static inline int mm_counter(struct folio *folio)return mm_counter_file (folio); } +static inline unsigned long __get_mm_rss (struct mm_struct *mm,bool approximate, +unsigned int *accuracy_under,unsigned int *accuracy_over) +{ +return __get_mm_counter (mm,MM_FILEPAGES , approximate, accuracy_under, accuracy_over) + +__get_mm_counter (mm,MM_ANONPAGES , approximate, accuracy_under, accuracy_over) + +__get_mm_counter (mm,MM_SHMEMPAGES , approximate, accuracy_under, accuracy_over); +} +static inline unsigned long get_mm_rss (struct mm_struct *mm) { -return get_mm_counter (mm,MM_FILEPAGES ) + -get_mm_counter (mm,MM_ANONPAGES ) + -get_mm_counter (mm,MM_SHMEMPAGES ); +return __get_mm_rss (mm, true,NULL ,NULL ); }static inline unsigned long get_mm_hiwater_rss (struct mm_struct *mm)
include/linux/oom.h +11 -1
@@ -48,6 +48,13 @@ struct oom_control {unsigned long totalpages;struct task_struct *chosen;long chosen_points; +bool approximate; +/* +* Number of precise badness points sums performed by this task +* selection. +*/ +int nr_precise; +unsigned long accuracy_under;/* Used to print the constraint info. */ enum oom_constraint constraint;
@@ -97,7 +104,10 @@ static inline vm_fault_t check_stable_address_space(struct mm_struct *mm) }long oom_badness (struct task_struct *p, -unsigned long totalpages); +unsigned long totalpages, +bool approximate, +unsigned int *accuracy_under, +unsigned int *accuracy_over);extern bool out_of_memory (struct oom_control *oc);
mm/oom_kill.c +60 -12
126 lines not on this page · raw message
Signature · 1 line
2.39.5
Andrew Morton
maintainerQuoted 43 lines · On Sat, 13 Dec 2025 13:56:05 -0500 Mathieu Desnoyers…
Introduce hierarchical per-cpu counters and use them for RSS tracking to fix the per-mm RSS tracking which has become too inaccurate for OOM killer purposes on large many-core systems.
The following rss tracking issues were noted by Sweet Tea Dorminy [1], which lead to picking wrong tasks as OOM kill target:
Recently, several internal services had an RSS usage regression as part of a kernel upgrade. Previously, they were on a pre-6.2 kernel and were able to read RSS statistics in a backup watchdog process to monitor and decide if they'd overrun their memory budget. Now, however, a representative service with five threads, expected to use about a hundred MB of memory, on a 250-cpu machine had memory usage tens of megabytes different from the expected amount -- this constituted a significant percentage of inaccuracy, causing the watchdog to act.
This was a result of commit
f1a7941243c1("mm: convert mm's rss stats intopercpu_counter") [1]. Previously, the memory error was bounded by 64*nr_threads pages, a very livable megabyte. Now, however, as a result of scheduler decisions moving the threads around the CPUs, the memory error could be as large as a gigabyte.This is a really tremendous inaccuracy for any few-threaded program on a large machine and impedes monitoring significantly. These stat counters are also used to make OOM killing decisions, so this additional inaccuracy could make a big difference in OOM situations -- either resulting in the wrong process being killed, or in less memory being returned from an OOM-kill than expected.
The approach proposed here is to replace this by the hierarchical per-cpu counters, which bounds the inaccuracy based on the system topology with
O(N*logN).Notable change for v10: The new patch 3/3 changes the implementation of the oom killer task selection to a 2-pass algorithm, where the first pass uses the fast approximation provided by the hierarchical percpu counters, and the second pass does a precise sum for all tasks which have badness values within the range of the approximation accuracy.
I've done moderate testing of this series on a 256-core VM with 128GB RAM. Figuring out whether this indeed helps solve issues with real-life workloads will require broader feedback from the community.
Quoted 7 lines
The one request I did not have time to fulfill yet is to port the tests from the librseq feature branch implementation (userspace) to the kernel selftests.
This series is based on v6.18.
Andrew, are you interested to try this out in mm-new ?
Yes. We have to start somewhere.
As you kind of mention, it's going to be difficult to determine when this is ready to go upstream. I assume that to really know this will required detailed and lengthy fleet-wide operation and observation.
What sort of drawbacks do you think people miht encounter with this change?
Mathieu Desnoyers
reviewerQuoted 1 line · Andrew Morton
On Sat, 13 Dec 2025 13:56:05 -0500 Mathieu Desnoyers <[email protected]> wrote:
[...]
Quoted 4 lines
Quoted 2 lines
Andrew, are you interested to try this out in mm-new ?
Yes. We have to start somewhere.
Cool !
Quoted 4 lines
As you kind of mention, it's going to be difficult to determine when this is ready to go upstream. I assume that to really know this will required detailed and lengthy fleet-wide operation and observation.
For that kind of feature, yes, this is my expectation as well.
Quoted 2 lines
What sort of drawbacks do you think people miht encounter with this change?
Let's see, here are some possible drawbacks to keep an eye out for:
- Taking for instance a machine with 256 logical CPUs topology, although allocation for small amount of memory is typically handled with a this_cpu_add_return, when doing large memory allocations, this will trickle up the carry over 3 levels, each of which require an atomic_add_return.
The upstream implementation would instead go straight for a global spinlock, which may or may not be better than 3 atomics.
- 2-pass OOM killer task selection: with a large number of tasks, and small number of CPUs, the upstream algorithm would be adequately precise, and faster because it does a single iteration pass. So the open question here is do we care about overhead of the OOM killer task selection ?
- I understanding that some people implement their own OOM killer in userspace based on RSS values exposed through /proc. Because those RSS values are the precise counts (split-counter sums), there should be no difference there compared to the upstream implementation, but there would be no performance gain as well. It may be interesting to eventually expose the counter approximations (and the accuracy intervals) to userspace so it could speed up its task selection eventually. Not really a drawback, more something to keep in mind as future improvement.
- I took care not to add additional memory allocation to the mm allocation/free code because it regresses some benchmarks. Still it's good to keep an eye out for bot reports about those regressions.
- The intermediate tree levels counters use extra memory. This is a tradeoff between compactness and cache locality of the counters. I currently used cache-aligned integers (thus favored cache locality and eliminating false-sharing), but I have other prototypes which use packed bytes for the intermediate levels. For instance, on a 256 core machine, we have 37 intermediate levels nodes, for a total of 2368 bytes (that's in addition to the 1024 bytes of per-cpu memory for the per-cpu counters). If we choose to instead go for the packed bytes approach, the 37 intermediate levels nodes will use 37 bytes of memory, but there will be false-sharing across those counters.
An alternative approach there is to use a strided allocator [1] with a byte counter set allocator on top [2]. This way we can benefit from the memory savings of byte counters without the false-sharing.
Thanks,
Mathieu
[1] https://github.com/compudj/librseq/blob/percpu-counter-byte/src/rseq-mempool.c
[2] https://github.com/compudj/librseq/blob/percpu-counter-byte/src/percpu-counter-tree.c#L190
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Mathieu Desnoyers
reviewerQuoted 63 lines · Mathieu Desnoyers
Quoted 1 line · Andrew Morton
On Sat, 13 Dec 2025 13:56:05 -0500 Mathieu Desnoyers <[email protected]> wrote:
[...]
Quoted 4 lines
Quoted 2 lines
Andrew, are you interested to try this out in mm-new ?
Yes. We have to start somewhere.
Cool !
Quoted 4 lines
As you kind of mention, it's going to be difficult to determine when this is ready to go upstream. I assume that to really know this will required detailed and lengthy fleet-wide operation and observation.
For that kind of feature, yes, this is my expectation as well.
Quoted 2 lines
What sort of drawbacks do you think people miht encounter with this change?
Let's see, here are some possible drawbacks to keep an eye out for:
- Taking for instance a machine with 256 logical CPUs topology, although allocation for small amount of memory is typically handled with a
this_cpu_add_return, when doing large memory allocations, this will trickle up the carry over 3 levels, each of which require anatomic_add_return.The upstream implementation would instead go straight for a global spinlock, which may or may not be better than 3 atomics.
- 2-pass OOM killer task selection: with a large number of tasks, and small number of CPUs, the upstream algorithm would be adequately precise, and faster because it does a single iteration pass. So the open question here is do we care about overhead of the OOM killer task selection ?
- I understanding that some people implement their own OOM killer in userspace based on RSS values exposed through /proc. Because those RSS values are the precise counts (split-counter sums), there should be no difference there compared to the upstream implementation, but there would be no performance gain as well. It may be interesting to eventually expose the counter approximations (and the accuracy intervals) to userspace so it could speed up its task selection eventually. Not really a drawback, more something to keep in mind as future improvement.
- I took care not to add additional memory allocation to the mm allocation/free code because it regresses some benchmarks. Still it's good to keep an eye out for bot reports about those regressions.
- The intermediate tree levels counters use extra memory. This is a tradeoff between compactness and cache locality of the counters. I currently used cache-aligned integers (thus favored cache locality and eliminating false-sharing), but I have other prototypes which use packed bytes for the intermediate levels. For instance, on a 256 core machine, we have 37 intermediate levels nodes, for a total of 2368 bytes (that's in addition to the 1024 bytes of per-cpu memory for the per-cpu counters). If we choose to instead go for the packed bytes approach, the 37 intermediate levels nodes will use 37 bytes of memory, but there will be false-sharing across those counters.
Quoted 3 lines
An alternative approach there is to use a strided allocator [1] with a byte counter set allocator on top [2]. This way we can benefit from the memory savings of byte counters without the false-sharing.
One more point:
- The choice of constants is an educated guess at best and would require testing/feedback on real-world workloads:
- The batch size (32),
- The n-arity of the counter tree for each power-of-two number of CPUs
(per_nr_cpu_order_config).
- The choice of making the number of intermediate level counter bits match the n-arity of the counters aggregated into that level is also arbitrary.
- The precise badness sums limit (16) for an OOM killer task selection. This is perhaps something that could become a sysctl tunable.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com