[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250129191204.368199-1-longman@redhat.com>
Date: Wed, 29 Jan 2025 14:12:04 -0500
From: Waiman Long <longman@...hat.com>
To: Tejun Heo <tj@...nel.org>,
Johannes Weiner <hannes@...xchg.org>,
Michal Koutný <mkoutny@...e.com>,
Jonathan Corbet <corbet@....net>,
Michal Hocko <mhocko@...nel.org>,
Roman Gushchin <roman.gushchin@...ux.dev>,
Shakeel Butt <shakeel.butt@...ux.dev>,
Muchun Song <muchun.song@...ux.dev>,
Andrew Morton <akpm@...ux-foundation.org>
Cc: linux-kernel@...r.kernel.org,
cgroups@...r.kernel.org,
linux-mm@...ck.org,
linux-doc@...r.kernel.org,
Peter Hunt <pehunt@...hat.com>,
Waiman Long <longman@...hat.com>
Subject: [RFC PATCH] mm, memcg: introduce memory.high.throttle
Since commit 0e4b01df8659 ("mm, memcg: throttle allocators when failing
reclaim over memory.high"), the amount of allocator throttling had
increased substantially. As a result, it could be difficult for a
misbehaving application that consumes increasing amount of memory from
being OOM-killed if memory.high is set. Instead, the application may
just be crawling along holding close to the allowed memory.high memory
for the current memory cgroup for a very long time especially those
that do a lot of memcg charging and uncharging operations.
This behavior makes the upstream Kubernetes community hesitate to
use memory.high. Instead, they use only memory.max for memory control
similar to what is being done for cgroup v1 [1].
To allow better control of the amount of throttling and hence the
speed that a misbehving task can be OOM killed, a new single-value
memory.high.throttle control file is now added. The allowable range
is 0-32. By default, it has a value of 0 which means maximum throttling
like before. Any non-zero positive value represents the corresponding
power of 2 reduction of throttling and makes OOM kills easier to happen.
System administrators can now use this parameter to determine how easy
they want OOM kills to happen for applications that tend to consume
a lot of memory without the need to run a special userspace memory
management tool to monitor memory consumption when memory.high is set.
Below are the test results of a simple program showing how different
values of memory.high.throttle can affect its run time (in secs) until
it gets OOM killed. This test program allocates pages from kernel
continuously. There are some run-to-run variations and the results
are just one possible set of samples.
# systemd-run -p MemoryHigh=10M -p MemoryMax=20M -p MemorySwapMax=10M \
--wait -t timeout 300 /tmp/mmap-oom
memory.high.throttle service runtime
-------------------- ---------------
0 120.521
1 103.376
2 85.881
3 69.698
4 42.668
5 45.782
6 22.179
7 9.909
8 5.347
9 3.100
10 1.757
11 1.084
12 0.919
13 0.650
14 0.650
15 0.655
[1] https://docs.google.com/document/d/1mY0MTT34P-Eyv5G1t_Pqs4OWyIH-cg9caRKWmqYlSbI/edit?tab=t.0
Signed-off-by: Waiman Long <longman@...hat.com>
---
Documentation/admin-guide/cgroup-v2.rst | 16 ++++++++--
include/linux/memcontrol.h | 2 ++
mm/memcontrol.c | 41 +++++++++++++++++++++++++
3 files changed, 57 insertions(+), 2 deletions(-)
diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
index cb1b4e759b7e..df9410ad8b3b 100644
--- a/Documentation/admin-guide/cgroup-v2.rst
+++ b/Documentation/admin-guide/cgroup-v2.rst
@@ -1291,8 +1291,20 @@ PAGE_SIZE multiple when read back.
Going over the high limit never invokes the OOM killer and
under extreme conditions the limit may be breached. The high
limit should be used in scenarios where an external process
- monitors the limited cgroup to alleviate heavy reclaim
- pressure.
+ monitors the limited cgroup to alleviate heavy reclaim pressure
+ unless a high enough value is set in "memory.high.throttle".
+
+ memory.high.throttle
+ A read-write single value file which exists on non-root
+ cgroups. The default is 0.
+
+ Memory usage throttle control. This value controls the amount
+ of throttling that will be applied when memory consumption
+ exceeds the "memory.high" limit. The larger the value is,
+ the smaller the amount of throttling will be and the easier an
+ offending application may get OOM killed.
+
+ The valid range of this control file is 0-32.
memory.max
A read-write single value file which exists on non-root
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 6e74b8254d9b..b184d7b008d4 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -199,6 +199,8 @@ struct mem_cgroup {
struct list_head swap_peaks;
spinlock_t peaks_lock;
+ int high_throttle_shift;
+
/* Range enforcement for interrupt charges */
struct work_struct high_work;
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 46f8b372d212..2fa3fd99ebc9 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2112,6 +2112,7 @@ void mem_cgroup_handle_over_high(gfp_t gfp_mask)
unsigned long nr_reclaimed;
unsigned int nr_pages = current->memcg_nr_pages_over_high;
int nr_retries = MAX_RECLAIM_RETRIES;
+ int throttle_shift;
struct mem_cgroup *memcg;
bool in_retry = false;
@@ -2156,6 +2157,13 @@ void mem_cgroup_handle_over_high(gfp_t gfp_mask)
penalty_jiffies += calculate_high_delay(memcg, nr_pages,
swap_find_max_overage(memcg));
+ /*
+ * Reduce penalty according to the high_throttle_shift value.
+ */
+ throttle_shift = READ_ONCE(memcg->high_throttle_shift);
+ if (throttle_shift)
+ penalty_jiffies >>= throttle_shift;
+
/*
* Clamp the max delay per usermode return so as to still keep the
* application moving forwards and also permit diagnostics, albeit
@@ -4172,6 +4180,33 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
return nbytes;
}
+static u64 memory_high_throttle_read(struct cgroup_subsys_state *css,
+ struct cftype *cft)
+{
+ struct mem_cgroup *memcg = mem_cgroup_from_css(css);
+
+ return READ_ONCE(memcg->high_throttle_shift);
+}
+
+static ssize_t memory_high_throttle_write(struct kernfs_open_file *of,
+ char *buf, size_t nbytes, loff_t off)
+{
+ struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
+ u64 val;
+ int err;
+
+ buf = strstrip(buf);
+ err = kstrtoull(buf, 10, &val);
+ if (err)
+ return err;
+
+ if (val > 32)
+ return -EINVAL;
+
+ WRITE_ONCE(memcg->high_throttle_shift, (int)val);
+ return nbytes;
+}
+
/*
* Note: don't forget to update the 'samples/cgroup/memcg_event_listener'
* if any new events become available.
@@ -4396,6 +4431,12 @@ static struct cftype memory_files[] = {
.seq_show = memory_high_show,
.write = memory_high_write,
},
+ {
+ .name = "high.throttle",
+ .flags = CFTYPE_NOT_ON_ROOT,
+ .read_u64 = memory_high_throttle_read,
+ .write = memory_high_throttle_write,
+ },
{
.name = "max",
.flags = CFTYPE_NOT_ON_ROOT,
--
2.48.1
Powered by blists - more mailing lists