This patch series adds a memory.reclaim proactive reclaim interface. The rationale behind the interface and how it works are in the first patch.
---
Changes in V2: - Add the interface to root as well. - Added a selftest. - Documented the interface as a nested-keyed interface, which makes adding optional arguments in the future easier (see doc updates in the first patch). - Modified the commit message to reflect changes and add a timeout argument as a suggested possible extension - Return -EAGAIN if the kernel fails to reclaim the full requested amount.
---
Shakeel Butt (1): memcg: introduce per-memcg reclaim interface
Yosry Ahmed (3): selftests: cgroup: return the errno of write() in cg_write() on failure selftests: cgroup: fix alloc_anon_noexit() instantly freeing memory selftests: cgroup: add a selftest for memory.reclaim
Documentation/admin-guide/cgroup-v2.rst | 21 +++++ mm/memcontrol.c | 37 ++++++++ tools/testing/selftests/cgroup/cgroup_util.c | 11 ++- .../selftests/cgroup/test_memcontrol.c | 94 ++++++++++++++++++- 4 files changed, 156 insertions(+), 7 deletions(-)
From: Shakeel Butt shakeelb@google.com
Introduce a memcg interface to trigger memory reclaim on a memory cgroup.
Use case: Proactive Reclaim ---------------------------
A userspace proactive reclaimer can continuously probe the memcg to reclaim a small amount of memory. This gives more accurate and up-to-date workingset estimation as the LRUs are continuously sorted and can potentially provide more deterministic memory overcommit behavior. The memory overcommit controller can provide more proactive response to the changing behavior of the running applications instead of being reactive.
A userspace reclaimer's purpose in this case is not a complete replacement for kswapd or direct reclaim, it is to proactively identify memory savings opportunities and reclaim some amount of cold pages set by the policy to free up the memory for more demanding jobs or scheduling new jobs.
A user space proactive reclaimer is used in Google data centers. Additionally, Meta's TMO paper recently referenced a very similar interface used for user space proactive reclaim: https://dl.acm.org/doi/pdf/10.1145/3503222.3507731
Benefits of a user space reclaimer: -----------------------------------
1) More flexible on who should be charged for the cpu of the memory reclaim. For proactive reclaim, it makes more sense to be centralized.
2) More flexible on dedicating the resources (like cpu). The memory overcommit controller can balance the cost between the cpu usage and the memory reclaimed.
3) Provides a way to the applications to keep their LRUs sorted, so, under memory pressure better reclaim candidates are selected. This also gives more accurate and uptodate notion of working set for an application.
Why memory.high is not enough? ------------------------------
- memory.high can be used to trigger reclaim in a memcg and can potentially be used for proactive reclaim. However there is a big downside in using memory.high. It can potentially introduce high reclaim stalls in the target application as the allocations from the processes or the threads of the application can hit the temporary memory.high limit.
- Userspace proactive reclaimers usually use feedback loops to decide how much memory to proactively reclaim from a workload. The metrics used for this are usually either refaults or PSI, and these metrics will become messy if the application gets throttled by hitting the high limit.
- memory.high is a stateful interface, if the userspace proactive reclaimer crashes for any reason while triggering reclaim it can leave the application in a bad state.
- If a workload is rapidly expanding, setting memory.high to proactively reclaim memory can result in actually reclaiming more memory than intended.
The benefits of such interface and shortcomings of existing interface were further discussed in this RFC thread: https://lore.kernel.org/linux-mm/5df21376-7dd1-bf81-8414-32a73cea45dd@google...
Interface: ----------
Introducing a very simple memcg interface 'echo 10M > memory.reclaim' to trigger reclaim in the target memory cgroup.
The interface is introduced as a nested-keyed file to allow for future optional arguments to be easily added to configure the behavior of reclaim.
Possible Extensions: --------------------
- This interface can be extended with an additional parameter or flags to allow specifying one or more types of memory to reclaim from (e.g. file, anon, ..).
- The interface can also be extended with a node mask to reclaim from specific nodes. This has use cases for reclaim-based demotion in memory tiering systens.
- A similar per-node interface can also be added to support proactive reclaim and reclaim-based demotion in systems without memcg.
- Add a timeout parameter to make it easier for user space to call the interface without worrying about being blocked for an undefined amount of time.
For now, let's keep things simple by adding the basic functionality.
[yosryahmed@google.com: refreshed to current master, updated commit message based on recent discussions and use cases] Signed-off-by: Shakeel Butt shakeelb@google.com Signed-off-by: Yosry Ahmed yosryahmed@google.com Acked-by: Johannes Weiner hannes@cmpxchg.org Acked-by: Michal Hocko mhocko@suse.com --- Documentation/admin-guide/cgroup-v2.rst | 21 ++++++++++++++ mm/memcontrol.c | 37 +++++++++++++++++++++++++ 2 files changed, 58 insertions(+)
diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 69d7a6983f78..19bcd73cad03 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1208,6 +1208,27 @@ PAGE_SIZE multiple when read back. high limit is used and monitored properly, this limit's utility is limited to providing the final safety net.
+ memory.reclaim + A write-only nested-keyed file which exists for all cgroups. + + This is a simple interface to trigger memory reclaim in the + target cgroup. + + This file accepts a single key, the number of bytes to reclaim. + No nested keys are currently supported. + + Example:: + + echo "1G" > memory.reclaim + + The interface can be later extended with nested keys to + configure the reclaim behavior. For example, specify the + type of memory to reclaim from (anon, file, ..). + + Please note that the kernel can over or under reclaim from + the target cgroup. If less bytes are reclaimed than the + specified amount, -EAGAIN is returned. + memory.oom.group A read-write single value file which exists on non-root cgroups. The default value is "0". diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 725f76723220..2b214b66d333 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6355,6 +6355,38 @@ static ssize_t memory_oom_group_write(struct kernfs_open_file *of, return nbytes; }
+static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, + size_t nbytes, loff_t off) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); + unsigned int nr_retries = MAX_RECLAIM_RETRIES; + unsigned long nr_to_reclaim, nr_reclaimed = 0; + int err; + + buf = strstrip(buf); + err = page_counter_memparse(buf, "", &nr_to_reclaim); + if (err) + return err; + + while (nr_reclaimed < nr_to_reclaim) { + unsigned long reclaimed; + + if (signal_pending(current)) + break; + + reclaimed = try_to_free_mem_cgroup_pages(memcg, + nr_to_reclaim - nr_reclaimed, + GFP_KERNEL, true); + + if (!reclaimed && !nr_retries--) + break; + + nr_reclaimed += reclaimed; + } + + return nr_reclaimed < nr_to_reclaim ? -EAGAIN : nbytes; +} + static struct cftype memory_files[] = { { .name = "current", @@ -6413,6 +6445,11 @@ static struct cftype memory_files[] = { .seq_show = memory_oom_group_show, .write = memory_oom_group_write, }, + { + .name = "reclaim", + .flags = CFTYPE_NS_DELEGATABLE, + .write = memory_reclaim, + }, { } /* terminate */ };
On Thu, Apr 7, 2022 at 3:43 PM Yosry Ahmed yosryahmed@google.com wrote:
From: Shakeel Butt shakeelb@google.com
Introduce a memcg interface to trigger memory reclaim on a memory cgroup.
Use case: Proactive Reclaim
A userspace proactive reclaimer can continuously probe the memcg to reclaim a small amount of memory. This gives more accurate and up-to-date workingset estimation as the LRUs are continuously sorted and can potentially provide more deterministic memory overcommit behavior. The memory overcommit controller can provide more proactive response to the changing behavior of the running applications instead of being reactive.
A userspace reclaimer's purpose in this case is not a complete replacement for kswapd or direct reclaim, it is to proactively identify memory savings opportunities and reclaim some amount of cold pages set by the policy to free up the memory for more demanding jobs or scheduling new jobs.
A user space proactive reclaimer is used in Google data centers. Additionally, Meta's TMO paper recently referenced a very similar interface used for user space proactive reclaim: https://dl.acm.org/doi/pdf/10.1145/3503222.3507731
Benefits of a user space reclaimer:
- More flexible on who should be charged for the cpu of the memory
reclaim. For proactive reclaim, it makes more sense to be centralized.
- More flexible on dedicating the resources (like cpu). The memory
overcommit controller can balance the cost between the cpu usage and the memory reclaimed.
- Provides a way to the applications to keep their LRUs sorted, so,
under memory pressure better reclaim candidates are selected. This also gives more accurate and uptodate notion of working set for an application.
Why memory.high is not enough?
memory.high can be used to trigger reclaim in a memcg and can potentially be used for proactive reclaim. However there is a big downside in using memory.high. It can potentially introduce high reclaim stalls in the target application as the allocations from the processes or the threads of the application can hit the temporary memory.high limit.
Userspace proactive reclaimers usually use feedback loops to decide how much memory to proactively reclaim from a workload. The metrics used for this are usually either refaults or PSI, and these metrics will become messy if the application gets throttled by hitting the high limit.
memory.high is a stateful interface, if the userspace proactive reclaimer crashes for any reason while triggering reclaim it can leave the application in a bad state.
If a workload is rapidly expanding, setting memory.high to proactively reclaim memory can result in actually reclaiming more memory than intended.
The benefits of such interface and shortcomings of existing interface were further discussed in this RFC thread: https://lore.kernel.org/linux-mm/5df21376-7dd1-bf81-8414-32a73cea45dd@google...
Interface:
Introducing a very simple memcg interface 'echo 10M > memory.reclaim' to trigger reclaim in the target memory cgroup.
The interface is introduced as a nested-keyed file to allow for future optional arguments to be easily added to configure the behavior of reclaim.
Possible Extensions:
This interface can be extended with an additional parameter or flags to allow specifying one or more types of memory to reclaim from (e.g. file, anon, ..).
The interface can also be extended with a node mask to reclaim from specific nodes. This has use cases for reclaim-based demotion in memory tiering systens.
A similar per-node interface can also be added to support proactive reclaim and reclaim-based demotion in systems without memcg.
Add a timeout parameter to make it easier for user space to call the interface without worrying about being blocked for an undefined amount of time.
For now, let's keep things simple by adding the basic functionality.
[yosryahmed@google.com: refreshed to current master, updated commit message based on recent discussions and use cases] Signed-off-by: Shakeel Butt shakeelb@google.com Signed-off-by: Yosry Ahmed yosryahmed@google.com Acked-by: Johannes Weiner hannes@cmpxchg.org Acked-by: Michal Hocko mhocko@suse.com
Documentation/admin-guide/cgroup-v2.rst | 21 ++++++++++++++ mm/memcontrol.c | 37 +++++++++++++++++++++++++ 2 files changed, 58 insertions(+)
diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 69d7a6983f78..19bcd73cad03 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1208,6 +1208,27 @@ PAGE_SIZE multiple when read back. high limit is used and monitored properly, this limit's utility is limited to providing the final safety net.
- memory.reclaim
A write-only nested-keyed file which exists for all cgroups.
This is a simple interface to trigger memory reclaim in the
target cgroup.
This file accepts a single key, the number of bytes to reclaim.
No nested keys are currently supported.
Example::
echo "1G" > memory.reclaim
The interface can be later extended with nested keys to
configure the reclaim behavior. For example, specify the
type of memory to reclaim from (anon, file, ..).
Please note that the kernel can over or under reclaim from
the target cgroup. If less bytes are reclaimed than the
specified amount, -EAGAIN is returned.
- memory.oom.group A read-write single value file which exists on non-root cgroups. The default value is "0".
diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 725f76723220..2b214b66d333 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6355,6 +6355,38 @@ static ssize_t memory_oom_group_write(struct kernfs_open_file *of, return nbytes; }
+static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
size_t nbytes, loff_t off)
+{
struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
unsigned int nr_retries = MAX_RECLAIM_RETRIES;
unsigned long nr_to_reclaim, nr_reclaimed = 0;
int err;
buf = strstrip(buf);
err = page_counter_memparse(buf, "", &nr_to_reclaim);
if (err)
return err;
while (nr_reclaimed < nr_to_reclaim) {
unsigned long reclaimed;
if (signal_pending(current))
break;
reclaimed = try_to_free_mem_cgroup_pages(memcg,
nr_to_reclaim - nr_reclaimed,
GFP_KERNEL, true);
if (!reclaimed && !nr_retries--)
break;
nr_reclaimed += reclaimed;
}
return nr_reclaimed < nr_to_reclaim ? -EAGAIN : nbytes;
+}
static struct cftype memory_files[] = { { .name = "current", @@ -6413,6 +6445,11 @@ static struct cftype memory_files[] = { .seq_show = memory_oom_group_show, .write = memory_oom_group_write, },
{
.name = "reclaim",
.flags = CFTYPE_NS_DELEGATABLE,
.write = memory_reclaim,
}, { } /* terminate */
};
-- 2.35.1.1178.g4f1659d476-goog
Acked-by: Wei Xu weixugc@google.com
Currently, cg_write() returns 0 on success and -1 on failure. Modify it to return the errno of write() syscall on failure.
Signed-off-by: Yosry Ahmed yosryahmed@google.com --- tools/testing/selftests/cgroup/cgroup_util.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/selftests/cgroup/cgroup_util.c index dbaa7aabbb4a..ef76db6026aa 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.c +++ b/tools/testing/selftests/cgroup/cgroup_util.c @@ -48,6 +48,8 @@ static ssize_t write_text(const char *path, char *buf, ssize_t len)
len = write(fd, buf, len); if (len < 0) { + /* preserve the errno of write() */ + len = errno; close(fd); return len; } @@ -177,17 +179,16 @@ long cg_read_lc(const char *cgroup, const char *control) return cnt; }
+/* Returns 0 on success, or the errno of write() on failure. */ int cg_write(const char *cgroup, const char *control, char *buf) { char path[PATH_MAX]; - ssize_t len = strlen(buf); + ssize_t len = strlen(buf), ret;
snprintf(path, sizeof(path), "%s/%s", cgroup, control);
- if (write_text(path, buf, len) == len) - return 0; - - return -1; + ret = write_text(path, buf, len); + return ret == len ? 0 : ret; }
int cg_find_unified_root(char *root, size_t len)
Currently, alloc_anon_noexit() calls alloc_anon() which instantly frees the allocated memory. alloc_anon_noexit() is usually used with cg_run_nowait() to run a process in the background that allocates memory. It makes sense for the background process to keep the memory allocated and not instantly free it (otherwise there is no point of running it in the background).
Signed-off-by: Yosry Ahmed yosryahmed@google.com --- tools/testing/selftests/cgroup/test_memcontrol.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index 36ccf2322e21..c1ec71d83af7 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -211,13 +211,18 @@ static int alloc_pagecache_50M_noexit(const char *cgroup, void *arg) static int alloc_anon_noexit(const char *cgroup, void *arg) { int ppid = getppid(); + size_t size = (unsigned long)arg; + char *buf, *ptr;
- if (alloc_anon(cgroup, arg)) - return -1; + buf = malloc(size); + for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE) + *ptr = 0;
while (getppid() == ppid) sleep(1);
+ printf("Freeing buffer"); + free(buf); return 0; }
On Thu, Apr 7, 2022 at 3:43 PM Yosry Ahmed yosryahmed@google.com wrote:
Currently, alloc_anon_noexit() calls alloc_anon() which instantly frees the allocated memory. alloc_anon_noexit() is usually used with cg_run_nowait() to run a process in the background that allocates memory. It makes sense for the background process to keep the memory allocated and not instantly free it (otherwise there is no point of running it in the background).
Signed-off-by: Yosry Ahmed yosryahmed@google.com
tools/testing/selftests/cgroup/test_memcontrol.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index 36ccf2322e21..c1ec71d83af7 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -211,13 +211,18 @@ static int alloc_pagecache_50M_noexit(const char *cgroup, void *arg) static int alloc_anon_noexit(const char *cgroup, void *arg) { int ppid = getppid();
size_t size = (unsigned long)arg;
char *buf, *ptr;
if (alloc_anon(cgroup, arg))
return -1;
buf = malloc(size);
for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
*ptr = 0; while (getppid() == ppid) sleep(1);
printf("Freeing buffer");
Hey Andew,
I am very sorry but I left a debugging printf there by mistake. If it's no hassle, do you mind removing it from the patch (assuming I won't need to send a v3 anyway)?
Thanks!
free(buf); return 0;
}
-- 2.35.1.1178.g4f1659d476-goog
On Thu, Apr 7, 2022 at 4:04 PM Yosry Ahmed yosryahmed@google.com wrote:
On Thu, Apr 7, 2022 at 3:43 PM Yosry Ahmed yosryahmed@google.com wrote:
Currently, alloc_anon_noexit() calls alloc_anon() which instantly frees the allocated memory. alloc_anon_noexit() is usually used with cg_run_nowait() to run a process in the background that allocates memory. It makes sense for the background process to keep the memory allocated and not instantly free it (otherwise there is no point of running it in the background).
Signed-off-by: Yosry Ahmed yosryahmed@google.com
tools/testing/selftests/cgroup/test_memcontrol.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index 36ccf2322e21..c1ec71d83af7 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -211,13 +211,18 @@ static int alloc_pagecache_50M_noexit(const char *cgroup, void *arg) static int alloc_anon_noexit(const char *cgroup, void *arg) { int ppid = getppid();
size_t size = (unsigned long)arg;
char *buf, *ptr;
if (alloc_anon(cgroup, arg))
return -1;
buf = malloc(size);
for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
*ptr = 0; while (getppid() == ppid) sleep(1);
printf("Freeing buffer");
Hey Andew,
I am very sorry but I left a debugging printf there by mistake. If it's no hassle, do you mind removing it from the patch (assuming I won't need to send a v3 anyway)?
Never mind I already sent v3 and removed it with other fixes.
Thanks!
free(buf); return 0;
}
-- 2.35.1.1178.g4f1659d476-goog
Add a new test for memory.reclaim that verifies that the interface correctly reclaims memory as intended, from both anon and file pages.
Signed-off-by: Yosry Ahmed yosryahmed@google.com --- .../selftests/cgroup/test_memcontrol.c | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+)
diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index c1ec71d83af7..915dffef2287 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -761,6 +761,90 @@ static int test_memcg_max(const char *root) return ret; }
+/* + * This test checks that memory.reclaim reclaims the given + * amount of memory (from both anon and file). + */ +static int test_memcg_reclaim(const char *root) +{ + int ret = KSFT_FAIL, fd; + char *memcg; + long current, to_reclaim; + char buf[64]; + + memcg = cg_name(root, "memcg_test"); + if (!memcg) + goto cleanup; + + if (cg_create(memcg)) + goto cleanup; + + current = cg_read_long(memcg, "memory.current"); + if (current != 0) + goto cleanup; + + cg_run_nowait(memcg, alloc_anon_noexit, (void *) MB(50)); + sleep(1); + + fd = get_temp_fd(); + if (fd < 0) + goto cleanup; + + cg_run_nowait(memcg, alloc_pagecache_50M_noexit, (void *)(long)fd); + sleep(1); + + current = cg_read_long(memcg, "memory.current"); + if (!values_close(current, MB(100), 10)) + goto cleanup; + + /* + * Reclaim until current reaches 30M, make sure to reclaim over 50M to + * hit both anon and file. + */ + while (true) { + int err; + + current = cg_read_long(memcg, "memory.current"); + to_reclaim = current - MB(30); + + /* + * We only keep looping if we get EAGAIN, which means we could + * not reclaim the full amount. + */ + if (to_reclaim <= 0) + goto cleanup; + + + snprintf(buf, sizeof(buf), "%ld", to_reclaim); + err = cg_write(memcg, "memory.reclaim", buf); + if (!err) { + /* + * If writing succeeds, then the written amount should have been + * fully reclaimed (and maybe more). + */ + current = cg_read_long(memcg, "memory.current"); + if (!values_close(current, MB(30), 3) && current > MB(30)) + goto cleanup; + break; + } + + /* The kernel could not reclaim the full amount, try again. */ + if (err == EAGAIN) + continue; + + /* We got an unexpected error. */ + goto cleanup; + } + + ret = KSFT_PASS; +cleanup: + cg_destroy(memcg); + free(memcg); + close(fd); + + return ret; +} + static int alloc_anon_50M_check_swap(const char *cgroup, void *arg) { long mem_max = (long)arg; @@ -1264,6 +1348,7 @@ struct memcg_test { T(test_memcg_high), T(test_memcg_high_sync), T(test_memcg_max), + T(test_memcg_reclaim), T(test_memcg_oom_events), T(test_memcg_swap_max), T(test_memcg_sock),
linux-kselftest-mirror@lists.linaro.org