mm, memcg: cg2 memory{.swap,}.peak write handlers
Subsystems (6)
PATCH v5 1/2 mm, memcg: cg2 memory{.swap,}.peak write handlers
This patch revision addresses a few comments from Longman and Johannes here[1], and rebases onto master again.
(I still have issues with starting my UML instance to run the tests. I'll see if I an figure out what's going on with that after I get some other work done today)
[1]: https://lore.kernel.org/cgroups/[email protected]/T/
Thanks again,
David Finkel
Senior Principal Software Engineer, Core Service
Vimeo Inc.
PATCH v5 1/2 mm, memcg: cg2 memory{.swap,}.peak write handlers
Other mechanisms for querying the peak memory usage of either a process or v1 memory cgroup allow for resetting the high watermark. Restore parity with those mechanisms, but with a less racy API.
For example:
- Any write to memory.max_usage_in_bytes in a cgroup v1 mount resets the high watermark.
- writing "5" to the clear_refs pseudo-file in a processes's proc directory resets the peak RSS.
This change is an evolution of a previous patch, which mostly copied the cgroup v1 behavior, however, there were concerns about races/ownership issues with a global reset, so instead this change makes the reset filedescriptor-local.
Writing a specific string to the memory.peak and memory.swap.peak pseudo-files reset the high watermark to the current usage for subsequent reads through that same fd.
Notably, following Johannes's suggestion, this implementation moves the O(fds that have written) behavior onto the fd write(2) path. Instead, on the page-allocation path, we simply add one additional watermark to conditionally bump per-hierarchy level in the page-counter.
Additionally, this takes Longman's suggestion of nesting the page-charging-path checks for the two watermarks to reduce the number of common-case comparisons.
This behavior is particularly useful for work scheduling systems that need to track memory usage of worker processes/cgroups per-work-item. Since memory can't be squeezed like CPU can (the OOM-killer has opinions), these systems need to track the peak memory usage to compute system/container fullness when binpacking workitems.
Most notably, Vimeo's use-case involves a system that's doing global binpacking across many Kubernetes pods/containers, and while we can use PSI for some local decisions about overload, we strive to avoid packing workloads too tightly in the first place. To facilitate this, we track the peak memory usage. However, since we run with long-lived workers (to amortize startup costs) we need a way to track the high watermark while a work-item is executing. Polling runs the risk of missing short spikes that last for timescales below the polling interval, and peak memory tracking at the cgroup level is otherwise perfect for this use-case.
As this data is used to ensure that binpacked work ends up with sufficient headroom, this use-case mostly avoids the inaccuracies surrounding reclaimable memory.
--- Documentation/admin-guide/cgroup-v2.rst | 26 ++++-- include/linux/cgroup-defs.h | 5 + include/linux/cgroup.h | 3 + include/linux/memcontrol.h | 5 + include/linux/page_counter.h | 10 +- kernel/cgroup/cgroup-internal.h | 2 + kernel/cgroup/cgroup.c | 7 ++ mm/memcontrol.c | 117 ++++++++++++++++++++++-- mm/page_counter.c | 30 ++++-- 9 files changed, 178 insertions(+), 27 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +18 -8
@@ -1333,11 +1333,16 @@ The following nested keys are defined. all the existing limitations and potential future extensions. memory.peak - A read-only single value file which exists on non-root - cgroups. + A read-write single value file which exists on non-root cgroups. + + The max memory usage recorded for the cgroup and its descendants since + either the creation of the cgroup or the most recent reset for that FD. - The max memory usage recorded for the cgroup and its - descendants since the creation of the cgroup. + A write of the string "reset" to this file resets it to the + current memory usage for subsequent reads through the same + file descriptor. + Attempts to write any other non-empty string will return EINVAL + (modulo leading and trailing whitespace). memory.oom.group A read-write single value file which exists on non-root
@@ -1663,11 +1668,16 @@ The following nested keys are defined. Healthy workloads are not expected to reach this limit. memory.swap.peak - A read-only single value file which exists on non-root - cgroups. + A read-write single value file which exists on non-root cgroups. + + The max swap usage recorded for the cgroup and its descendants since + the creation of the cgroup or the most recent reset for that FD. - The max swap usage recorded for the cgroup and its - descendants since the creation of the cgroup. + A write of the string "reset" to this file resets it to the + current memory usage for subsequent reads through the same + file descriptor. + Attempts to write any other non-empty string will return EINVAL + (modulo leading and trailing whitespace). memory.swap.max A read-write single value file which exists on non-root
include/linux/cgroup-defs.h +5 -0
@@ -775,6 +775,11 @@ struct cgroup_subsys {extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem; +struct cgroup_of_peak { +long value; +struct list_head list; +}; +/** * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups * @tsk: target task
include/linux/cgroup.h +3 -0
@@ -11,6 +11,7 @@#include <linux/sched.h> #include <linux/nodemask.h> +#include <linux/list.h> #include <linux/rculist.h> #include <linux/cgroupstats.h> #include <linux/fs.h>
@@ -854,4 +855,6 @@ static inline void cgroup_bpf_put(struct cgroup *cgrp) {}struct cgroup *task_get_cgroup1 (struct task_struct *tsk,int hierarchy_id); +struct cgroup_of_peak *of_peak (struct kernfs_open_file *of); +#endif /* _LINUX_CGROUP_H */
include/linux/memcontrol.h +5 -0
@@ -192,6 +192,11 @@ struct mem_cgroup {struct page_counter memsw;/* v1 only */ }; +/* registered local peak watchers */ +struct list_head memory_peaks; +struct list_head swap_peaks; +spinlock_t peaks_lock; +/* Range enforcement for interrupt charges */ struct work_struct high_work;
include/linux/page_counter.h +9 -1
@@ -26,6 +26,7 @@ struct page_counter {atomic_long_t children_low_usage;unsigned long watermark; +unsigned long local_watermark;unsigned long failcnt;/* Keep all the read most fields in a separete cacheline. */
@@ -78,7 +79,14 @@ int page_counter_memparse(const char *buf, const char *max,static inline void page_counter_reset_watermark (struct page_counter *counter) { - counter->watermark =page_counter_read (counter); +unsigned long usage =page_counter_read (counter); + +/* +* Update local_watermark first, so it's always <= watermark +* (modulo CPU/compiler re-ordering) +*/ + counter->local_watermark = usage; + counter->watermark = usage; } void page_counter_calculate_protection(struct page_counter *root,
kernel/cgroup/cgroup-internal.h +2 -0
@@ -81,6 +81,8 @@ struct cgroup_file_ctx {struct {struct cgroup_pidlist *pidlist; } procs1; + +struct cgroup_of_peak peak; };/*
kernel/cgroup/cgroup.c +7 -0
@@ -1972,6 +1972,13 @@ static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *paramreturn -EINVAL ; } +struct cgroup_of_peak *of_peak (struct kernfs_open_file *of) +{ +struct cgroup_file_ctx *ctx = of->priv; + +return &ctx->peak; +} +static void apply_cgroup_root_flags (unsigned int root_flags) {if (current->nsproxy->cgroup_ns == &init_cgroup_ns) {
mm/memcontrol.c +107 -10
163 lines not on this page · raw message
mm/page_counter.c +22 -8
@@ -79,9 +79,22 @@ void page_counter_charge(struct page_counter *counter, unsigned long nr_pages)/* * This is indeed racy, but we can live with some * inaccuracy in the watermark. +* +* Notably, we have two watermarks to allow for both a globally +* visible peak and one that can be reset at a smaller scope. +* +* Since we reset both watermarks when the global reset occurs, +* we can guarantee that watermark >= local_watermark, so we +* don't need to do both comparisons every time. +* +* On systems with branch predictors, the inner condition should +* be almost free. */ -if (new >READ_ONCE (c->watermark)) -WRITE_ONCE (c->watermark, new); +if (new >READ_ONCE (c->local_watermark)) { +WRITE_ONCE (c->local_watermark, new); +if (new >READ_ONCE (c->watermark)) +WRITE_ONCE (c->watermark, new); + } } }
@@ -129,12 +142,13 @@ bool page_counter_try_charge(struct page_counter *counter, goto failed; }propagate_protected_usage (c, new); -/* -* Just like with failcnt, we can live with some -* inaccuracy in the watermark. -*/ -if (new >READ_ONCE (c->watermark)) -WRITE_ONCE (c->watermark, new); + +/* see comment on page_counter_charge */ +if (new >READ_ONCE (c->local_watermark)) { +WRITE_ONCE (c->local_watermark, new); +if (new >READ_ONCE (c->watermark)) +WRITE_ONCE (c->watermark, new); + } }return true;
Signature · 1 line
2.40.1
Michal Koutný
maintainerHello David.
Quoted 3 lines · David Finkel
Writing a specific string to the memory.peak and memory.swap.peak pseudo-files reset the high watermark to the current usage for subsequent reads through that same fd.
This is elegant and nice work! (Caught my attention, so a few nits below.)
include/linux/cgroup-defs.h struct cgroup_subsys { · line 779
Quoted 8 lines
include/linux/cgroup-defs.h +2
-0@@ -775,6 +775,11 @@ struct cgroup_subsys {extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem; +struct cgroup_of_peak { +long value;
Wouldn't this better be unsigned like watermarks themselves?
include/linux/page_counter.h struct page_counter { · line 29
Quoted 11 lines
include/linux/cgroup-defs.h +2
-0+struct list_head list; +};include/linux/page_counter.h +1
-0@@ -26,6 +26,7 @@ struct page_counter {atomic_long_t children_low_usage;unsigned long watermark; +unsigned long local_watermark;
At first, I struggled understading what the locality is (when the local value is actually in of_peak), IIUC, it's more about temporal position.
I'd suggest a comment (if not a name) like:
/* latest reset watermark */
include/linux/page_counter.h struct page_counter {
Quoted 6 lines
include/linux/page_counter.h +4
-0+unsigned long local_watermark; + +/* User wants global or local peak? */ +if (fd_peak ==-1UL )
Here you use typed -1UL but not in other places. (Maybe define an explicit macro value ((unsigned long)-1)?)
Quoted 4 lines
include/linux/page_counter.h +4
-0+static ssize_t peak_write (struct kernfs_open_file *of,char *buf,size_t nbytes, +loff_t off,struct page_counter *pc, +struct list_head *watchers) +{
...
include/linux/page_counter.h
Quoted 3 lines
include/linux/page_counter.h +3
-0+list_for_each_entry (peer_ctx, watchers, list) +if (usage > peer_ctx->value) + peer_ctx->value = usage;
The READ_ONCE() in peak_show() suggests it could be WRITE_ONCE() here.
include/linux/page_counter.h
Quoted 6 lines
include/linux/page_counter.h +6
-0+ +/* initial write, register watcher */ +if (ofp->value ==-1 ) +list_add (&ofp->list, watchers); + + ofp->value = usage;
Move the registration before iteration and drop the extra assignment?
Thanks,
Michal
Hi Michal,
Quoted 9 lines · Michal Koutný
Hello David.
Quoted 3 lines · David Finkel
Writing a specific string to the memory.peak and memory.swap.peak pseudo-files reset the high watermark to the current usage for subsequent reads through that same fd.
This is elegant and nice work! (Caught my attention, so a few nits below.)
Thanks!
You can thank Johannes for the algorithm.
Quoted 11 lines
include/linux/cgroup-defs.h struct cgroup_subsys { · line 779
Quoted 9 lines
include/linux/cgroup-defs.h +2
-0@@ -775,6 +775,11 @@ struct cgroup_subsys {extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem; +struct cgroup_of_peak { +long value;Wouldn't this better be unsigned like watermarks themselves?
Hmm, interesting question.
I originally set that to be signed to handle the special value of -1. However, that's kind of irrelevant if I'm casting it to an unsigned u64 in the only place that value's being handled.
I've switched this over now.
Quoted 10 lines
Quoted 10 lines
include/linux/cgroup-defs.h +2
-0+struct list_head list; +};include/linux/page_counter.h +0
-0@@ -26,6 +26,7 @@ struct page_counter {atomic_long_t children_low_usage;
Quoted 9 lines
include/linux/page_counter.h struct page_counter { · line 29
Quoted 2 lines
include/linux/page_counter.h +1
-0unsigned long watermark; +unsigned long local_watermark;At first, I struggled understading what the locality is (when the local value is actually in
of_peak), IIUC, it's more about temporal position.I'd suggest a comment (if not a name) like:
/* latest reset watermark */Quoted 1 line
include/linux/page_counter.h +1
-0+unsigned long local_watermark;
Yeah, I had a comment before that was a bit inaccurate, and was advised to remove it instead of trying to fix it in a previous round.
I've added one that says "Latest cg2 reset watermark".
Quoted 8 lines
include/linux/page_counter.h struct page_counter {
Quoted 5 lines
include/linux/page_counter.h +3
-0+ +/* User wants global or local peak? */ +if (fd_peak ==-1UL )Here you use typed -1UL but not in other places. (Maybe define an explicit macro value ((unsigned long)-1)?)
Good idea!
Quoted 11 lines
Quoted 5 lines
include/linux/page_counter.h +4
-0+static ssize_t peak_write (struct kernfs_open_file *of,char *buf,size_t nbytes, +loff_t off,struct page_counter *pc, +struct list_head *watchers) +{...
include/linux/page_counter.h
Quoted 3 lines
include/linux/page_counter.h +3
-0+list_for_each_entry (peer_ctx, watchers, list) +if (usage > peer_ctx->value) + peer_ctx->value = usage;The
READ_ONCE()inpeak_show()suggests it could beWRITE_ONCE()here.
Good point. I've sprinkled a few more READ_ONCE and WRITE_ONCE calls.
Quoted 9 lines
include/linux/page_counter.h
Quoted 7 lines
include/linux/page_counter.h +6
-0+ +/* initial write, register watcher */ +if (ofp->value ==-1 ) +list_add (&ofp->list, watchers); + + ofp->value = usage;Move the registration before iteration and drop the extra assignment?
My original reason is that I could avoid an extra list hop and conditional, but at this point I see two reasons to keep it separate:
- We need to reset this value either way. If it's already been reset, it may not get reset by the loop.
- since these are now unsigned ints, -1 compares greater than everything, so it would need a special case (or an additional cast). (Assuming we're on a system that uses twos complement)
- I think it's a bit clearer this way
Quoted 3 lines
Thanks,
Michal
Thanks for the review!
--
David Finkel
Senior Principal Software Engineer, Core Services
PATCH v5 2/2 mm, memcg: cg2 memory{.swap,}.peak write tests
In reply to David FinkelExtend two existing tests to cover extracting memory usage through the newly mutable memory.peak and memory.swap.peak handlers.
In particular, make sure to exercise adding and removing watchers with overlapping lifetimes so the less-trivial logic gets tested.
--- tools/testing/selftests/cgroup/cgroup_util.c | 22 ++ tools/testing/selftests/cgroup/cgroup_util.h | 2 + .../selftests/cgroup/test_memcontrol.c | 226 +++++++++++++++++- 3 files changed, 242 insertions(+), 8 deletions(-)
tools/testing/selftests/cgroup/cgroup_util.c +22 -0
@@ -141,6 +141,16 @@ long cg_read_long(const char *cgroup, const char *control)return atol (buf); } +long cg_read_long_fd (int fd) +{ +char buf[128 ]; + +if (pread (fd, buf,sizeof (buf),0 ) <=0 ) +return -1 ; + +return atol (buf); +} +long cg_read_key_long (const char *cgroup,const char *control,const char *key) {char buf[PAGE_SIZE ];
@@ -183,6 +193,18 @@ int cg_write(const char *cgroup, const char *control, char *buf)return ret == len ?0 : ret; } +/* +* Returns fd on success, or -1 on failure. +* (fd should be closed with close() as usual) +*/ +int cg_open (const char *cgroup,const char *control,int flags) +{ +char path[PATH_MAX ]; + +snprintf (path,sizeof (path),"%s/%s" , cgroup, control); +return open (path, flags); +} +int cg_write_numeric (const char *cgroup,const char *control,long value) {char buf[64 ];
tools/testing/selftests/cgroup/cgroup_util.h +2 -0
@@ -34,9 +34,11 @@ extern int cg_read_strcmp(const char *cgroup, const char *control,extern int cg_read_strstr (const char *cgroup,const char *control,const char *needle);extern long cg_read_long (const char *cgroup,const char *control); +extern long cg_read_long_fd (int fd);long cg_read_key_long (const char *cgroup,const char *control,const char *key);extern long cg_read_lc (const char *cgroup,const char *control);extern int cg_write (const char *cgroup,const char *control,char *buf); +extern int cg_open (const char *cgroup,const char *control,int flags);int cg_write_numeric (const char *cgroup,const char *control,long value); extern int cg_run(const char *cgroup, int (*fn)(const char *cgroup, void *arg),
tools/testing/selftests/cgroup/test_memcontrol.c +218 -8
293 lines not on this page · raw message
Signature · 1 line
2.40.1