Allow bpf_oom_ops handlers and proactive SYSCALL/PSI programs to resolve memory cgroup pressure by increasing a non-root cgroup's memory.max limit via bpf_mem_cgroup_raise_max(memcg, max_bytes). Only strictly higher limits are permitted so updating memory.max never triggers synchronous reclaim or recursive OOMs while oom_lock is held. On cgroup v1, memcg1_raise_max() enforces the memory.max <= memsw.max invariant under memcg_max_mutex and wakes waiters via memcg1_oom_recover(). The kfunc is marked KF_SLEEPABLE because memcg_wb_domain_size_changed() acquires a spinlock and cgroup v1 acquires memcg_max_mutex. When invoked from handle_out_of_memory() returning > 0, try_charge_memcg() immediately retries against the expanded limit and succeeds without terminating any task. Suggested-by: Michal Hocko Signed-off-by: Ben Gardon --- include/linux/memcontrol.h | 7 +++++++ mm/bpf_memcontrol.c | 24 ++++++++++++++++++++++++ mm/memcontrol-v1.c | 16 ++++++++++++++++ mm/memcontrol-v1.h | 6 ++++++ mm/memcontrol.c | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index bc30d7baecd7..1dc5176e2682 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1002,6 +1002,7 @@ void mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, int val); void mem_cgroup_flush_stats(struct mem_cgroup *memcg); +int mem_cgroup_raise_max(struct mem_cgroup *memcg, unsigned long max_pages); void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg); void mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val); @@ -1533,6 +1534,12 @@ static inline bool mem_cgroup_task_in_hierarchy(struct task_struct *task, return true; } +static inline int mem_cgroup_raise_max(struct mem_cgroup *memcg, + unsigned long max_pages) +{ + return -EOPNOTSUPP; +} + static inline int mem_cgroup_init(void) { return 0; } #endif /* CONFIG_MEMCG */ diff --git a/mm/bpf_memcontrol.c b/mm/bpf_memcontrol.c index d8f579c28560..8c00d98b5826 100644 --- a/mm/bpf_memcontrol.c +++ b/mm/bpf_memcontrol.c @@ -206,6 +206,29 @@ __bpf_kfunc long bpf_proactive_reclaim(struct mem_cgroup *memcg, return nr_reclaimed * PAGE_SIZE; } +/** + * bpf_mem_cgroup_raise_max() - Increase a memory cgroup's memory.max limit + * @memcg: target non-root memory cgroup + * @max_bytes: new limit in bytes (truncated to PAGE_SIZE; clamped to + * PAGE_COUNTER_MAX, which represents unlimited/"max" -- callers + * may pass U64_MAX) + * + * Allows a BPF OOM policy (or proactive program) to resolve a memcg OOM + * non-destructively by raising memory.max instead of terminating a task. + * Only strictly higher limits are accepted so no reclaim is needed while + * holding oom_lock. On cgroup v1, enforces memory.max <= memsw.max. + * + * Return: 0 on success, or a negative errno on failure. + */ +__bpf_kfunc int bpf_mem_cgroup_raise_max(struct mem_cgroup *memcg, + u64 max_bytes) +{ + unsigned long max_pages = min_t(u64, max_bytes >> PAGE_SHIFT, + PAGE_COUNTER_MAX); + + return mem_cgroup_raise_max(memcg, max_pages); +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(bpf_memcontrol_kfuncs) @@ -218,6 +241,7 @@ BTF_ID_FLAGS(func, bpf_mem_cgroup_memory_events) BTF_ID_FLAGS(func, bpf_mem_cgroup_usage) BTF_ID_FLAGS(func, bpf_mem_cgroup_page_state) BTF_ID_FLAGS(func, bpf_mem_cgroup_flush_stats, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_mem_cgroup_raise_max, KF_SLEEPABLE) BTF_KFUNCS_END(bpf_memcontrol_kfuncs) diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c index b7f286888507..d50fe775f1f3 100644 --- a/mm/memcontrol-v1.c +++ b/mm/memcontrol-v1.c @@ -1418,6 +1418,22 @@ void vmpressure_unregister_event(struct mem_cgroup *memcg, static DEFINE_MUTEX(memcg_max_mutex); +int memcg1_raise_max(struct mem_cgroup *memcg, unsigned long max_pages) +{ + int ret = 0; + + mutex_lock(&memcg_max_mutex); + if (max_pages <= memcg->memory.max || max_pages > memcg->memsw.max) + ret = -EINVAL; + else + WRITE_ONCE(memcg->memory.max, max_pages); + mutex_unlock(&memcg_max_mutex); + + if (!ret) + memcg1_oom_recover(memcg); + return ret; +} + static int mem_cgroup_resize_max(struct mem_cgroup *memcg, unsigned long max, bool memsw) { diff --git a/mm/memcontrol-v1.h b/mm/memcontrol-v1.h index 820139657184..3c22f124a5dc 100644 --- a/mm/memcontrol-v1.h +++ b/mm/memcontrol-v1.h @@ -63,6 +63,7 @@ enum res_type { bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked); void memcg1_oom_finish(struct mem_cgroup *memcg, bool locked); void memcg1_oom_recover(struct mem_cgroup *memcg); +int memcg1_raise_max(struct mem_cgroup *memcg, unsigned long max_pages); void memcg1_commit_charge(struct folio *folio, struct mem_cgroup *memcg); void memcg1_uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout, @@ -106,6 +107,11 @@ static inline bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked) } static inline void memcg1_oom_finish(struct mem_cgroup *memcg, bool locked) {} static inline void memcg1_oom_recover(struct mem_cgroup *memcg) {} +static inline int memcg1_raise_max(struct mem_cgroup *memcg, + unsigned long max_pages) +{ + return -EOPNOTSUPP; +} static inline void memcg1_commit_charge(struct folio *folio, struct mem_cgroup *memcg) {} diff --git a/mm/memcontrol.c b/mm/memcontrol.c index afbb638e7e1c..8440cca757b9 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5025,6 +5025,44 @@ static int memory_max_show(struct seq_file *m, void *v) READ_ONCE(mem_cgroup_from_seq(m)->memory.max)); } +/** + * mem_cgroup_raise_max() - Increase a memory cgroup's hard limit + * @memcg: target non-root memory cgroup + * @max_pages: new limit in pages (must exceed current memory.max) + * + * Safe to call from OOM handler context while oom_lock is held: only + * increases are permitted, so no stock draining or reclaim is required. + * On cgroup v1, enforces the memory.max <= memsw.max invariant under + * memcg_max_mutex. + * + * Return: 0 on success, or -EINVAL if @memcg is root/dying or @max_pages + * does not satisfy limit bounds. + */ +int mem_cgroup_raise_max(struct mem_cgroup *memcg, unsigned long max_pages) +{ + unsigned long old; + + if (!memcg || mem_cgroup_is_root(memcg) || memcg_is_dying(memcg)) + return -EINVAL; + + max_pages = min_t(unsigned long, max_pages, PAGE_COUNTER_MAX); + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) { + int ret = memcg1_raise_max(memcg, max_pages); + + if (ret) + return ret; + } else { + old = READ_ONCE(memcg->memory.max); + do { + if (max_pages <= old) + return -EINVAL; + } while (!try_cmpxchg(&memcg->memory.max, &old, max_pages)); + } + + memcg_wb_domain_size_changed(memcg); + return 0; +} + static ssize_t memory_max_write(struct kernfs_open_file *of, char *buf, size_t nbytes, loff_t off) { -- 2.56.1.911.g883d8bcb20-goog