Skip to contentSkip to conversation

Improve proc RSS accuracy and OOM killer latency

PATCH Superseded Mathieu Desnoyers 2 participants 15 messages Jan 14 – Jan 26

Subsystems (10)

v16 of 25 Superseded Latest is v25 →

lore
Share & tools
lore

Mathieu Desnoyers

author reviewer
#0 lore raw

v16 cover Improve proc RSS accuracy and OOM killer latency

This series use the hierarchical tree counter approximation (hpcc) to:

* Increase accuracy of approximated RSS counters exposed through proc interfaces:

With a test program hopping across CPUs doing frequent mmap/munmap operations, the upstream implementation approximation reaches a 1GB delta from the precise value after a few minutes, compared to a 80MB delta with the hierarchical counter. The hierarchical counter provides a guaranteed maximum approximation inaccuracy of 192MB on that hardware topology.

* Implement the OOM killer task selection with a 2-pass algorithm. This is a latency reduction improvement of the OOM killer task selection:

Testing the execution time of select_bad_process() with a single tail -f /dev/zero:

AMD EPYC 9654 96-Core (2 sockets) Within a KVM, configured with 256 logical cpus.

| precise sum | hpcc |
----------------------------------|-------------|----------|
nr_processes=40 | 0.5 ms | 0.3 ms |
nr_processes=10000 | 80.0 ms | 7.9 ms |

I'm sending this series to gather feedback. I plan to re-submit it for inclusion into mm-new _after_ the next merge window closes, so the bug fix "mm: Fix OOM killer inaccuracy on large many-core systems" can be tested in the current release cycle.

Andrew, if you have a prior version of this specific series in mm-new, please drop it for now.

This series is based on v6.19-rc4, on top of the following three preparation series:

https://lore.kernel.org/linux-mm/[email protected]/T/#t
https://lore.kernel.org/linux-mm/[email protected]/T/#t
https://lore.kernel.org/linux-mm/[email protected]/

Thanks,

Mathieu

Cc 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 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]>
Cc Wei Yang <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Miaohe Lin <[email protected]>
Cc Al Viro <[email protected]>
Cc Yu Zhao <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Mateusz Guzik <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Aboorva Devarajan <[email protected]>

Mathieu Desnoyers (3):
lib: Introduce hierarchical per-cpu counters
mm: Improve RSS counter approximation accuracy for proc interfaces
mm: Reduce latency of OOM killer task selection with 2-pass algorithm

 .../core-api/percpu-counter-tree.rst          |  75 ++
 fs/proc/base.c                                |   2 +-
 include/linux/mm.h                            |  49 +-
 include/linux/mm_types.h                      |  54 +-
 include/linux/oom.h                           |  11 +-
 include/linux/percpu_counter_tree.h           | 367 ++++++++++
 include/trace/events/kmem.h                   |   2 +-
 init/main.c                                   |   2 +
 kernel/fork.c                                 |  22 +-
 lib/Makefile                                  |   1 +
 lib/percpu_counter_tree.c                     | 679 ++++++++++++++++++
 mm/oom_kill.c                                 |  84 ++-
 12 files changed, 1295 insertions(+), 53 deletions(-)
 create mode 100644 Documentation/core-api/percpu-counter-tree.rst
 create mode 100644 include/linux/percpu_counter_tree.h
 create mode 100644 lib/percpu_counter_tree.c
Signature · 1 line
2.39.5
author reviewer
#1 lore raw

PATCH v16 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.
- Provide possible precise sum ranges for a given sum approximation.

Its goals are twofold:

- Improve the accuracy of the approximated RSS counter values returned by proc interfaces [1],
- Reduce the latency of the OOM killer on large many-core systems.

* 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:  0

For 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.

Cc 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 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]>
Cc Wei Yang <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Miaohe Lin <[email protected]>
Cc Al Viro <[email protected]>
Cc Yu Zhao <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Mateusz Guzik <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Aboorva Devarajan <[email protected]>

---
Changes since v15:
- Rebase on v2 of "mm: Fix OOM killer inaccuracy on large many-core systems".
- Update the commit message to explain the two goals of hpcc:
provide a more accurate approximation, and reduce OOM killer latency.
- Change percpu_counter_tree_approximate_accuracy_range to increment the under/over parameters rather than set them. This requires callers to initialize them to 0, but facilitates scenarios where accuracy needs to be summed over many counters.
- Clarify comments above percpu_counter_tree_approximate_accuracy_range.

Changes since v14:
- Add Documentation/core-api/percpu-counter-tree.rst.
- Make percpu_counter_tree_approximate_accuracy_range static inline.

Changes since v12:
- Use atomic_long_set for percpu_counter_tree_set in UP build.
- percpu_counter_tree_precise_sum returns long type.

Changes since v11:
- Reduce level0 per-cpu memory allocation to the size required by those percpu counters.
- Use unsigned long type rather than unsigned int.
- Introduce functions to return the min/max range of possible precise sums.

Changes since v10:
- Fold "mm: Take into account hierarchical percpu tree items for static mm_struct definitions".

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.

---
 .../core-api/percpu-counter-tree.rst          |  75 ++
 include/linux/mm_types.h                      |   6 +-
 include/linux/percpu_counter_tree.h           | 367 ++++++++++
 init/main.c                                   |   2 +
 lib/Makefile                                  |   1 +
 lib/percpu_counter_tree.c                     | 679 ++++++++++++++++++
 6 files changed, 1127 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/core-api/percpu-counter-tree.rst
 create mode 100644 include/linux/percpu_counter_tree.h
 create mode 100644 lib/percpu_counter_tree.c
Documentation/core-api/percpu-counter-tree.rst new file +75 -0
new file mode 100644
index 000000000000..f47ce44a539d
@@ -0,0 +1,75 @@
+========================================
+The Hierarchical Per-CPU Counters (HPCC)
+========================================
+
+:Author: Mathieu Desnoyers
+
+Introduction
+============
+
+Counters come in many varieties, each with their own trade offs:
+
+ * A global atomic counter provides a fast read access to the current
+   sum, at the expense of cache-line bouncing on updates. This leads to
+   poor performance of frequent updates from various cores on large SMP
+   systems.
+
+ * A per-cpu split counter provides fast updates to per-cpu counters,
+   at the expense of a slower aggregation (sum). The sum operation needs
+   to iterate over all per-cpu counters to calculate the current total.
+
+The hierarchical per-cpu counters attempt to provide the best of both
+worlds (fast updates, and fast sum) by relaxing requirements on the sum
+accuracy. It allows quickly querying an approximated sum value, along
+with the possible min/max ranges of the associated precise sum. The
+exact precise sum can still be calculated with an iteration on all
+per-cpu counter, but the availability of an approximated sum value with
+possible precise sum min/max ranges allows eliminating candidates which
+are certainly outside of a known target range without the overhead of
+precise sums.
+
+Overview
+========
+
+The herarchical per-cpu counters are organized as a tree with the tree
+root at the bottom (last level) and the first level of the tree
+consisting of per-cpu counters.
+
+The intermediate tree levels contain carry propagation counters. When
+reaching a threshold (batch size), the carry is propagated down the
+tree.
+
+This allows reading an approximated value at the root, which has a
+bounded accuracy (minimum/maximum possible precise sum range) determined
+by the tree topology.
+
+Use Cases
+=========
+
+Use cases HPCC is meant to handle invove tracking resources which are
+used across many CPUs to quickly sum as feedback for decision making to
+apply throttling, quota limits, sort tasks, and perform memory or task
+migration decisions. When considering approximated sums within the
+accuracy range of the decision threshold, the user can either:
+
+ * Be conservative and fast: Consider that the sum has reached the
+   limit as soon as the given limit is within the approximation range.
+
+ * Be aggressive and fast: Consider that the sum is over the
+   limit only when the approximation range is over the given limit.
+
+ * Be precise and slow: Do a precise comparison with the limit, which
+   requires a precise sum when the limit is within the approximated
+   range.
+
+One primary example use-case for this is per-task RSS tracking for OOM
+killer task selection. The task selection can use the approximated value
+in a first pass over all processes, and then a second pass only needs
+the RSS precise sum for tasks which are within the possible precise sum
+range of the task chosen by the first pass.
+
+Functions and structures
+========================
+
+.. kernel-doc:: include/linux/percpu_counter_tree.h
+.. kernel-doc:: lib/percpu_counter_tree.c
include/linux/mm_types.h +3 -3
index aa4639888f89..9f861ceabe61 100644
@@ -1366,9 +1366,9 @@ static inline void __mm_flags_set_mask_bits_word(struct mm_struct *mm,
 			 MT_FLAGS_USE_RCU)
 extern struct mm_struct init_mm;
 
-#define MM_STRUCT_FLEXIBLE_ARRAY_INIT				\
-{								\
-	[0 ... sizeof(cpumask_t) + MM_CID_STATIC_SIZE - 1] = 0	\
+#define MM_STRUCT_FLEXIBLE_ARRAY_INIT									\
+{													\
+	[0 ... sizeof(cpumask_t) + MM_CID_STATIC_SIZE + PERCPU_COUNTER_TREE_ITEMS_STATIC_SIZE - 1] = 0	\
 }
 
 /* Pointer magic because the dynamic array size confuses some compilers. */
include/linux/percpu_counter_tree.h new file +367 -0

367 lines not on this page · raw message

init/main.c +2 -0
index b84818ad9685..85bcc7c1ccd0 100644
@@ -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>
@@ -1063,6 +1064,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
index aaf677cf4527..bff117556424 100644
@@ -181,6 +181,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 +679 -0

679 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer
#2 lore raw

Re: [PATCH v16 1/3] lib: Introduce hierarchical per-cpu counters

Quoted 41 lines · Mathieu Desnoyers

* 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.
- Provide possible precise sum ranges for a given sum approximation.

Its goals are twofold:

- Improve the accuracy of the approximated RSS counter values returned by proc interfaces [1],
- Reduce the latency of the OOM killer on large many-core systems.

* 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:  0
Quoted 6 lines

For 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.

One thing you should probably mention here is the memory consumption of the structure.

I have briefly looked at the implementation and concluded that I do not have enough time to make a thorough review. Sorry about that. As I've said in previous version the overall idea is sound. Especially if the additional memory consumption is not a factor. I will let others judge implementation details.

Thanks!

Signature · 2 lines
Michal Hocko
SUSE Labs
author reviewer
#3 lore raw

Re: [PATCH v16 1/3] lib: Introduce hierarchical per-cpu counters

Quoted 3 lines · Michal Hocko

One thing you should probably mention here is the memory consumption of the structure.

Good point.

The most important parts are the per-cpu counters and the tree items which propagate the carry.

In the proposed implementation, the per-cpu counters are allocated within per-cpu data structures, so they end up using:

nr_possible_cpus * sizeof(unsigned long)

In addition, the tree items are appended at the end of the mm_struct. The size of those items is defined by the per_nr_cpu_order_config table "nr_items" field.

Each item is aligned on cacheline size (typically 64 bytes) to minimize false sharing.

Here is the footprint for a few nr_cpus on a 64-bit arch:

nr_cpus percpu counters (bytes) nr_items items size (bytes) total (bytes)

  2                 16                     1                 64                    80
  4                 32                     3                192                   224
  8                 64                     7                448                   512
 64                 512                   21               1344                  1856
128                1024                   21               1344                  2368
256                2048                   37               2368                  4416
512                4096                   73               4672                  8768

There are of course various trade offs we can make here. We can:

* Increase the n-arity of the intermediate items to shrink the nr_items required for a given nr_cpus. This will increase contention of carry propagation across more cores.

* Remove cacheline alignment of intermediate tree items. This will shrink the memory needed for tree items, but will increase false sharing.

* Represent intermediate tree items on a byte rather than long. This further reduces the memory required for intermediate tree items, but further increases false sharing.

* Represent per-cpu counters on bytes rather than long. This makes the "sum" operation trickier, because it needs to iterate on the intermediate carry propagation nodes as well and synchronize with ongoing "tree add" operations. It further reduces memory use.

* Implement a custom strided allocator for intermediate items carry propagation bytes. This shares cachelines across different tree instances, keeping good locality. This ensures that all accesses from a given location in the machine topology touch the same cacheline for the various tree instances. This adds complexity, but provides compactness as well as minimal false-sharing.

Compared to this, the upstream percpu counters use a 32-bit integer per-cpu (4 bytes), and accumulate within a 64-bit global value.

So yes, there is an extra memory footprint added by the current hpcc implementation, but if it's an issue we have various options to consider to reduce its footprint.

Is it OK if I add this discussion to the commit message, or should it be also added into the high level design doc within Documentation/core-api/percpu-counter-tree.rst ?

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
maintainer
#4 lore raw

Re: [PATCH v16 1/3] lib: Introduce hierarchical per-cpu counters

Quoted 23 lines · Mathieu Desnoyers
Quoted 3 lines · Michal Hocko

One thing you should probably mention here is the memory consumption of the structure.

Good point.

The most important parts are the per-cpu counters and the tree items which propagate the carry.

In the proposed implementation, the per-cpu counters are allocated within per-cpu data structures, so they end up using:

nr_possible_cpus * sizeof(unsigned long)

In addition, the tree items are appended at the end of the mm_struct. The size of those items is defined by the per_nr_cpu_order_config table "nr_items" field.

Each item is aligned on cacheline size (typically 64 bytes) to minimize false sharing.

Here is the footprint for a few nr_cpus on a 64-bit arch:

Quoted 8 lines

nr_cpus percpu counters (bytes) nr_items items size (bytes) total (bytes)

  2                 16                     1                 64                    80
  4                 32                     3                192                   224
  8                 64                     7                448                   512
 64                 512                   21               1344                  1856
128                1024                   21               1344                  2368
256                2048                   37               2368                  4416
512                4096                   73               4672                  8768

I assume this is nr_possible_cpus not NR_CPUS, right?

Quoted 29 lines

There are of course various trade offs we can make here. We can:

* Increase the n-arity of the intermediate items to shrink the nr_items required for a given nr_cpus. This will increase contention of carry propagation across more cores.

* Remove cacheline alignment of intermediate tree items. This will shrink the memory needed for tree items, but will increase false sharing.

* Represent intermediate tree items on a byte rather than long. This further reduces the memory required for intermediate tree items, but further increases false sharing.

* Represent per-cpu counters on bytes rather than long. This makes the "sum" operation trickier, because it needs to iterate on the intermediate carry propagation nodes as well and synchronize with ongoing "tree add" operations. It further reduces memory use.

* Implement a custom strided allocator for intermediate items carry propagation bytes. This shares cachelines across different tree instances, keeping good locality. This ensures that all accesses from a given location in the machine topology touch the same cacheline for the various tree instances. This adds complexity, but provides compactness as well as minimal false-sharing.

Compared to this, the upstream percpu counters use a 32-bit integer per-cpu (4 bytes), and accumulate within a 64-bit global value.

Quoted 7 lines

So yes, there is an extra memory footprint added by the current hpcc implementation, but if it's an issue we have various options to consider to reduce its footprint.

Is it OK if I add this discussion to the commit message, or should it be also added into the high level design doc within Documentation/core-api/percpu-counter-tree.rst ?

I would mention them in both changelog and the documentation.

Signature · 2 lines
Michal Hocko
SUSE Labs
author reviewer
#5 lore raw

Re: [PATCH v16 1/3] lib: Introduce hierarchical per-cpu counters

Quoted 33 lines · Michal Hocko
Quoted 23 lines · Mathieu Desnoyers
Quoted 3 lines · Michal Hocko

One thing you should probably mention here is the memory consumption of the structure.

Good point.

The most important parts are the per-cpu counters and the tree items which propagate the carry.

In the proposed implementation, the per-cpu counters are allocated within per-cpu data structures, so they end up using:

nr_possible_cpus * sizeof(unsigned long)

In addition, the tree items are appended at the end of the mm_struct. The size of those items is defined by the per_nr_cpu_order_config table "nr_items" field.

Each item is aligned on cacheline size (typically 64 bytes) to minimize false sharing.

Here is the footprint for a few nr_cpus on a 64-bit arch:

Quoted 8 lines

nr_cpus percpu counters (bytes) nr_items items size (bytes) total (bytes)

   2                 16                     1                 64                    80
   4                 32                     3                192                   224
   8                 64                     7                448                   512
  64                 512                   21               1344                  1856
128                1024                   21               1344                  2368
256                2048                   37               2368                  4416
512                4096                   73               4672                  8768
Quoted 1 line

I assume this is nr_possible_cpus not NR_CPUS, right?

More precisely, this is nr_cpu_ids, at least for the nr_items.

percpu counters are effectively allocated for nr_possible_cpus, but we need to allocate the internal items for nr_cpu_ids (based on the max limits a cpumask would need). For the sake of keeping the table easy to understand, I will use nr_cpu_ids for the first column.

I'll update the commit message.

Quoted 34 lines
Quoted 34 lines

There are of course various trade offs we can make here. We can:

* Increase the n-arity of the intermediate items to shrink the nr_items required for a given nr_cpus. This will increase contention of carry propagation across more cores.

* Remove cacheline alignment of intermediate tree items. This will shrink the memory needed for tree items, but will increase false sharing.

* Represent intermediate tree items on a byte rather than long. This further reduces the memory required for intermediate tree items, but further increases false sharing.

* Represent per-cpu counters on bytes rather than long. This makes the "sum" operation trickier, because it needs to iterate on the intermediate carry propagation nodes as well and synchronize with ongoing "tree add" operations. It further reduces memory use.

* Implement a custom strided allocator for intermediate items carry propagation bytes. This shares cachelines across different tree instances, keeping good locality. This ensures that all accesses from a given location in the machine topology touch the same cacheline for the various tree instances. This adds complexity, but provides compactness as well as minimal false-sharing.

Compared to this, the upstream percpu counters use a 32-bit integer per-cpu (4 bytes), and accumulate within a 64-bit global value.

So yes, there is an extra memory footprint added by the current hpcc implementation, but if it's an issue we have various options to consider to reduce its footprint.

Quoted 6 lines
Quoted 3 lines

Is it OK if I add this discussion to the commit message, or should it be also added into the high level design doc within Documentation/core-api/percpu-counter-tree.rst ?

I would mention them in both changelog and the documentation.

OK, will do for v17.

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
author reviewer
#6 lore raw

PATCH v16 2/3 mm: Improve RSS counter approximation accuracy for proc interfaces

In reply to Mathieu Desnoyers

Use hierarchical per-cpu counters for RSS tracking to improve the accuracy of per-mm RSS sum approximation on large many-core systems [1]. This improves the accuracy of the RSS values returned by proc interfaces.

This is also a preparation step to introduce a 2-pass OOM killer task selection which leverages the approximation and accuracy ranges to quickly eliminate tasks which are outside of the range of the current selection, and thus reduce the latency introduced by execution of the OOM killer.

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).

4) Use a percise per-cpu counter sum for each counter value query:
Requires iteration on each possible CPUs for each sum, which adds overhead (and thus increases OOM killer latency) on large many-core systems running many processes.

The approach proposed here is to replace the per-cpu counters by the hierarchical per-cpu counters, which bounds the inaccuracy based on the system topology with O(N*logN).

* 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%

This change depends on the following patch:
"mm: Fix OOM killer inaccuracy on large many-core systems" [2]

Cc 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 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]>
Cc Wei Yang <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Miaohe Lin <[email protected]>
Cc Al Viro <[email protected]>
Cc Yu Zhao <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Mateusz Guzik <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Aboorva Devarajan <[email protected]>

---
Changes since v15:
- Update the commit message to explain that this change is an improvement to the accuracy of proc interfaces approximated RSS values, and a preparation step for reducing OOM killer latency.
- Rebase on v2 of "mm: Fix OOM killer and proc stats inaccuracy on large many-core systems".

Changes since v14:
- This change becomes the preparation for reducing the OOM killer latency.

Changes since v13:
- Change check_mm print format from %d to %ld.

Changes since v10:
- Rebase on top of mm_struct static init fixes.
- Change the alignment of mm_struct flexible array to the alignment of the rss counters items (which are cacheline aligned on SMP).
- Move the rss counters items to first position within the flexible array at the end of the mm_struct to place content in decreasing alignment requirement order.

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.

---
 include/linux/mm.h          | 19 ++++++++++----
 include/linux/mm_types.h    | 50 +++++++++++++++++++++++++++----------
 include/trace/events/kmem.h |  2 +-
 kernel/fork.c               | 22 +++++++++-------
 4 files changed, 65 insertions(+), 28 deletions(-)
include/linux/mm.h +14 -5
index bfa1307264df..10d62e8f35e7 100644
@@ -2843,38 +2843,47 @@ 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 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, flexible_array);
+	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 +37 -13

95 lines not on this page · raw message

include/trace/events/kmem.h +1 -1
index 7f93e754da5c..91c81c44f884 100644
@@ -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 +13 -9
index b1f3915d5f8e..8b56d81af734 100644
@@ -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)
  */
@@ -626,14 +631,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)) {
+		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:%ld Comm:%s Pid:%d\n",
-				 mm, resident_page_types[i], x,
+				 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))
@@ -731,7 +734,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);
 }
@@ -1123,8 +1126,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);
@@ -3006,7 +3010,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
maintainer
#7 lore raw

Re: [PATCH v16 2/3] mm: Improve RSS counter approximation accuracy for proc interfaces

Quoted 89 lines · Mathieu Desnoyers

Use hierarchical per-cpu counters for RSS tracking to improve the accuracy of per-mm RSS sum approximation on large many-core systems [1]. This improves the accuracy of the RSS values returned by proc interfaces.

This is also a preparation step to introduce a 2-pass OOM killer task selection which leverages the approximation and accuracy ranges to quickly eliminate tasks which are outside of the range of the current selection, and thus reduce the latency introduced by execution of the OOM killer.

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).

4) Use a percise per-cpu counter sum for each counter value query:
Requires iteration on each possible CPUs for each sum, which adds overhead (and thus increases OOM killer latency) on large many-core systems running many processes.

The approach proposed here is to replace the per-cpu counters by the hierarchical per-cpu counters, which bounds the inaccuracy based on the system topology with O(N*logN).

* 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%
Quoted 2 lines

This change depends on the following patch:
"mm: Fix OOM killer inaccuracy on large many-core systems" [2]

As mentioned in the previous patch, it would be great to explicitly mention what is the memory price for the new tracking data structure.

Other than that this seems like a generally useful improvement for larger systems and it is my understanding that it doesn't add almost any overhead on small end systems, correct?

Signature · 2 lines
Michal Hocko
SUSE Labs
author reviewer
#8 lore raw

Re: [PATCH v16 2/3] mm: Improve RSS counter approximation accuracy for proc interfaces

Quoted 90 lines · Michal Hocko
Quoted 89 lines · Mathieu Desnoyers

Use hierarchical per-cpu counters for RSS tracking to improve the accuracy of per-mm RSS sum approximation on large many-core systems [1]. This improves the accuracy of the RSS values returned by proc interfaces.

This is also a preparation step to introduce a 2-pass OOM killer task selection which leverages the approximation and accuracy ranges to quickly eliminate tasks which are outside of the range of the current selection, and thus reduce the latency introduced by execution of the OOM killer.

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).

4) Use a percise per-cpu counter sum for each counter value query:
Requires iteration on each possible CPUs for each sum, which adds overhead (and thus increases OOM killer latency) on large many-core systems running many processes.

The approach proposed here is to replace the per-cpu counters by the hierarchical per-cpu counters, which bounds the inaccuracy based on the system topology with O(N*logN).

* 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%
Quoted 5 lines
Quoted 2 lines

This change depends on the following patch:
"mm: Fix OOM killer inaccuracy on large many-core systems" [2]

As mentioned in the previous patch, it would be great to explicitly mention what is the memory price for the new tracking data structure.

Yes, I can add the explanation here as well.

Quoted 4 lines

Other than that this seems like a generally useful improvement for larger systems and it is my understanding that it doesn't add almost any overhead on small end systems, correct?

Indeed, the impact is mostly on large many-core systems, not so much on smaller systems.

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
author reviewer
#9 lore raw

PATCH v16 3/3 mm: Reduce latency of OOM killer task selection with 2-pass algorithm

In reply to Mathieu Desnoyers

Use the hierarchical tree counter approximation (hpcc) 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.

Testing the execution time of select_bad_process() with a single tail -f /dev/zero:

AMD EPYC 9654 96-Core (2 sockets) Within a KVM, configured with 256 logical cpus.

| precise sum | hpcc |
----------------------------------|-------------|----------|
nr_processes=40 | 0.5 ms | 0.3 ms |
nr_processes=10000 | 80.0 ms | 7.9 ms |

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

Cc 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 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]>
Cc Wei Yang <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Miaohe Lin <[email protected]>
Cc Al Viro <[email protected]>
Cc Yu Zhao <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Mateusz Guzik <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Aboorva Devarajan <[email protected]>

---
Changes since v14:
- This change becomes an OOM killer latency reduction (performance improvement).

Changes since v12:
- The oom_task_origin case needs to set points_min rather than points variable now, otherwise points_min is used without being initialized.

Changes since v11:
- get_mm_counter_sum() returns a precise sum.
- Use unsigned long type rather than unsigned int for accuracy.
- Use precise sum min/max calculation to compare the chosen vs current points.
- The first pass finds the maximum task's min points. The second pass eliminates all tasks for which the max points are below the currently chosen min points, and uses a precise sum to validate the candidates which are possibly in range.

---
 fs/proc/base.c      |  2 +-
 include/linux/mm.h  | 34 +++++++++++++-----
 include/linux/oom.h | 11 +++++-
 mm/oom_kill.c       | 84 +++++++++++++++++++++++++++++++++++++--------
 4 files changed, 106 insertions(+), 25 deletions(-)
fs/proc/base.c +1 -1
index 4eec684baca9..d75d0ce97032 100644
@@ -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 +26 -8
index 10d62e8f35e7..298b8223fbfb 100644
@@ -2855,14 +2855,28 @@ 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 long *accuracy_under, unsigned long *accuracy_over)
+{
+	if (approximate) {
+		if (accuracy_under && accuracy_over) {
+			percpu_counter_tree_approximate_accuracy_range(&mm->rss_stat[member],
+								       accuracy_under, accuracy_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, false, NULL, NULL);
 }
 
 void mm_trace_rss_stat(struct mm_struct *mm, int member);
@@ -2903,18 +2917,22 @@ 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 long *accuracy_under, unsigned long *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_rss_sum(struct mm_struct *mm)
 {
-	return get_mm_counter_sum(mm, MM_FILEPAGES) +
-		get_mm_counter_sum(mm, MM_ANONPAGES) +
-		get_mm_counter_sum(mm, MM_SHMEMPAGES);
+	return __get_mm_rss(mm, false, NULL, NULL);
 }
 
 static inline unsigned long get_mm_hiwater_rss(struct mm_struct *mm)
include/linux/oom.h +10 -1
index 7b02bc1d0a7e..f8e5bfaf7b39 100644
@@ -48,6 +48,12 @@ 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;
 
 	/* Used to print the constraint info. */
 	enum oom_constraint constraint;
@@ -97,7 +103,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 long *accuracy_under,
+		unsigned long *accuracy_over);
 
 extern bool out_of_memory(struct oom_control *oc);
 
mm/oom_kill.c +69 -15

141 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer
#10 lore raw

Re: [PATCH v16 3/3] mm: Reduce latency of OOM killer task selection with 2-pass algorithm

Quoted 25 lines · Mathieu Desnoyers

Use the hierarchical tree counter approximation (hpcc) 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.

Testing the execution time of select_bad_process() with a single tail -f /dev/zero:

AMD EPYC 9654 96-Core (2 sockets) Within a KVM, configured with 256 logical cpus.

Quoted 6 lines

| precise sum | hpcc |
----------------------------------|-------------|----------|
nr_processes=40 | 0.5 ms | 0.3 ms |
nr_processes=10000 | 80.0 ms | 7.9 ms |

Tested with the following script:

I am confused by these numbers. Are you saying that 2 pass over all tasks and evaluating all of them is 10 times faster than a single pass with exact sum of pcp counters?

Quoted 11 lines

#!/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
Quoted 2 lines

Results: OOM kill order on a 128GB memory system
================================================

I find this section confusing as well. Is that before/after comparision. If yes it would be great to call out explicit behavior before and after.

My overall impression is that the implementation is really involved and at this moment I do not really see a big benefit of all the complexity.

It would help to explicitly mention what is the the overall imprecision of the oom victim selection with the new data structure (maybe this is good enough[*]). What if we go with exact precision with the new data structure comparing to the original pcp counters.

[*] please keep in mind that oom victim selection is by no means an exact science, we try to pick up a task that is likely to free up some memory to unlock the system from memory depletion. We want that to be a big memory consumer to reduce number of tasks to kill and we want to roughly apply oom_score_adj.

Signature · 2 lines
Michal Hocko
SUSE Labs
author reviewer
#11 lore raw

Re: [PATCH v16 3/3] mm: Reduce latency of OOM killer task selection with 2-pass algorithm

Quoted 31 lines · Michal Hocko
Quoted 30 lines · Mathieu Desnoyers

Use the hierarchical tree counter approximation (hpcc) 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.

Testing the execution time of select_bad_process() with a single tail -f /dev/zero:

AMD EPYC 9654 96-Core (2 sockets) Within a KVM, configured with 256 logical cpus.

| precise sum | hpcc |
----------------------------------|-------------|----------|
nr_processes=40 | 0.5 ms | 0.3 ms |
nr_processes=10000 | 80.0 ms | 7.9 ms |

Quoted 5 lines
Quoted 1 line

Tested with the following script:

I am confused by these numbers. Are you saying that 2 pass over all tasks and evaluating all of them is 10 times faster than a single pass with exact sum of pcp counters?

Yes, that's correct.

This is because we can use the approximate value and the min/max possible precise sum ranges to eliminate most tasks, so we only have to do a precise sum on very few tasks.

So we're basically going from a 1-pass doing precise sum for all tasks to 2-passes doing approximated sum for all tasks, and only precise sums for the very few tasks that happen to be in the vicinity of the task chosen by the 1st pass.

We could probably do something more clever and do all this within a single pass if we keep track of the possible candidates in a separate list, and then only iterate on those candidates to do a precise sum at the end of the pass. But I chose to keep things simple, hence the 2 pass algo.

Quoted 12 lines
Quoted 12 lines

   #!/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
Quoted 5 lines
Quoted 2 lines

Results: OOM kill order on a 128GB memory system
================================================

I find this section confusing as well. Is that before/after comparision. If yes it would be great to call out explicit behavior before and after.

Not really. I'm just testing the "after" to make sure we get the expected killing order.

Quoted 3 lines

My overall impression is that the implementation is really involved and at this moment I do not really see a big benefit of all the complexity.

Note that we can get the proc ABI RSS accuracy improvements with the previous 2 patches without this 2-pass algo. Do you see more value in the RSS accuracy improvements than in the oom killer latency reduction ?

Quoted 5 lines

It would help to explicitly mention what is the the overall imprecision of the oom victim selection with the new data structure (maybe this is good enough[*]). What if we go with exact precision with the new data structure comparing to the original pcp counters.

Do you mean comparing using approximate sums with the new data structure (which has a bounded accuracy of O(nr_cpus*log(nr_cpus))) compared to the old data structure which had an inaccuracy of O(nr_cpus^2) ? So if the inaccuracy provided by the new data structure is good enough for OOM task selection, we could go from precise sum back to an approximation and just use that with the new data structure.

Thanks,

Mathieu

Quoted 7 lines

[*] please keep in mind that oom victim selection is by no means an exact science, we try to pick up a task that is likely to free up some memory to unlock the system from memory depletion. We want that to be a big memory consumer to reduce number of tasks to kill and we want to roughly apply oom_score_adj.

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
maintainer
#12 lore raw

Re: [PATCH v16 3/3] mm: Reduce latency of OOM killer task selection with 2-pass algorithm

Quoted 2 lines · Mathieu Desnoyers
Quoted 1 line · Michal Hocko

On Wed 14-01-26 09:59:15, Mathieu Desnoyers wrote:

[...]
Thanks to those clarifications

Quoted 6 lines
Quoted 2 lines

My overall impression is that the implementation is really involved and at this moment I do not really see a big benefit of all the complexity.

Note that we can get the proc ABI RSS accuracy improvements with the previous 2 patches without this 2-pass algo. Do you see more value in the RSS accuracy improvements than in the oom killer latency reduction ?

Yes, TBH I do not see oom latency as a big problem. As already mention this is a slow path and we are not talking about a huge latency anyway. proc numbers are much more sensitive to latency as they are regularly read by user space tools and accuracy for those matters as well (being off by 100s MB or GBs is simply making those numbers completely bogus).

Quoted 5 lines
Quoted 4 lines

It would help to explicitly mention what is the the overall imprecision of the oom victim selection with the new data structure (maybe this is good enough[*]). What if we go with exact precision with the new data structure comparing to the original pcp counters.

Quoted 7 lines

Do you mean comparing using approximate sums with the new data structure (which has a bounded accuracy of O(nr_cpus*log(nr_cpus))) compared to the old data structure which had an inaccuracy of O(nr_cpus^2) ? So if the inaccuracy provided by the new data structure is good enough for OOM task selection, we could go from precise sum back to an approximation and just use that with the new data structure.

Exactly!

Signature · 2 lines
Michal Hocko
SUSE Labs
author reviewer
#13 lore raw

Re: [PATCH v16 3/3] mm: Reduce latency of OOM killer task selection with 2-pass algorithm

Quoted 13 lines · Michal Hocko
Quoted 2 lines · Mathieu Desnoyers
Quoted 1 line · Michal Hocko

On Wed 14-01-26 09:59:15, Mathieu Desnoyers wrote:

[...]
Thanks to those clarifications

Quoted 6 lines
Quoted 2 lines

My overall impression is that the implementation is really involved and at this moment I do not really see a big benefit of all the complexity.

Note that we can get the proc ABI RSS accuracy improvements with the previous 2 patches without this 2-pass algo. Do you see more value in the RSS accuracy improvements than in the oom killer latency reduction ?

Quoted 5 lines

Yes, TBH I do not see oom latency as a big problem. As already mention this is a slow path and we are not talking about a huge latency anyway. proc numbers are much more sensitive to latency as they are regularly read by user space tools and accuracy for those matters as well (being off by 100s MB or GBs is simply making those numbers completely bogus).

It makes sense.

Quoted 14 lines
Quoted 6 lines
Quoted 5 lines

It would help to explicitly mention what is the the overall imprecision of the oom victim selection with the new data structure (maybe this is good enough[*]). What if we go with exact precision with the new data structure comparing to the original pcp counters.

Quoted 7 lines

Do you mean comparing using approximate sums with the new data structure (which has a bounded accuracy of O(nr_cpus*log(nr_cpus))) compared to the old data structure which had an inaccuracy of O(nr_cpus^2) ? So if the inaccuracy provided by the new data structure is good enough for OOM task selection, we could go from precise sum back to an approximation and just use that with the new data structure.

Quoted 1 line

Exactly!

OK, so based on your feedback, I plan to remove this 2-pass algo from the series, and simply keep using the precise sum for the OOM killer. If people complain about its latency, then we can eventually use the approximation provided by the hierarchical counters. But let's wait until someone asks for it rather than add this complexity when there is no need.

The hierarchical counters are still useful as they increase the accuracy of approximations exported through /proc.

How does that sound ?

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
maintainer
#14 lore raw

Re: [PATCH v16 3/3] mm: Reduce latency of OOM killer task selection with 2-pass algorithm

Quoted 42 lines · Mathieu Desnoyers
Quoted 12 lines · Michal Hocko
Quoted 2 lines · Mathieu Desnoyers
Quoted 1 line · Michal Hocko

On Wed 14-01-26 09:59:15, Mathieu Desnoyers wrote:

[...]
Thanks to those clarifications

Quoted 6 lines
Quoted 2 lines

My overall impression is that the implementation is really involved and at this moment I do not really see a big benefit of all the complexity.

Note that we can get the proc ABI RSS accuracy improvements with the previous 2 patches without this 2-pass algo. Do you see more value in the RSS accuracy improvements than in the oom killer latency reduction ?

Quoted 5 lines

Yes, TBH I do not see oom latency as a big problem. As already mention this is a slow path and we are not talking about a huge latency anyway. proc numbers are much more sensitive to latency as they are regularly read by user space tools and accuracy for those matters as well (being off by 100s MB or GBs is simply making those numbers completely bogus).

It makes sense.

Quoted 13 lines
Quoted 5 lines
Quoted 4 lines

It would help to explicitly mention what is the the overall imprecision of the oom victim selection with the new data structure (maybe this is good enough[*]). What if we go with exact precision with the new data structure comparing to the original pcp counters.

Quoted 7 lines

Do you mean comparing using approximate sums with the new data structure (which has a bounded accuracy of O(nr_cpus*log(nr_cpus))) compared to the old data structure which had an inaccuracy of O(nr_cpus^2) ? So if the inaccuracy provided by the new data structure is good enough for OOM task selection, we could go from precise sum back to an approximation and just use that with the new data structure.

Quoted 1 line

Exactly!

OK, so based on your feedback, I plan to remove this 2-pass algo from the series, and simply keep using the precise sum for the OOM killer. If people complain about its latency, then we can eventually use the approximation provided by the hierarchical counters. But let's wait until someone asks for it rather than add this complexity when there is no need.

Quoted 4 lines

The hierarchical counters are still useful as they increase the accuracy of approximations exported through /proc.

How does that sound ?

Works for me.

Thanks!

Signature · 2 lines
Michal Hocko
SUSE Labs