From: Ben Gardon <bgardon@google.com>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Shakeel Butt <shakeel.butt@linux.dev>,
Roman Gushchin <roman.gushchin@linux.dev>,
Tejun Heo <tj@kernel.org>
Cc: JP Kobryn <jp.kobryn@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
David Rientjes <rientjes@google.com>,
Suren Baghdasaryan <surenb@google.com>,
Josh Don <joshdon@google.com>,
Sweet Tea Dorminy <sweettea-kernel@dorminy.me>,
Chuyi Zhou <zhouchuyi@bytedance.com>,
Yafang Shao <laoar.shao@gmail.com>,
benbwgardon@gmail.com, bpf@vger.kernel.org, linux-mm@kvack.org,
cgroups@vger.kernel.org, linux-kernel@vger.kernel.org,
Ben Gardon <bgardon@google.com>,
Muchun Song <muchun.song@linux.dev>
Subject: [PATCH bpf-next v4 15/17] mm, bpf: introduce bpf_mem_cgroup_raise_max() kfunc
Date: Wed, 2 Dec 2026 13:04:05 -0800 [thread overview]
Message-ID: <20261202210415.517651-16-bgardon@google.com> (raw)
In-Reply-To: <20261202210415.517651-1-bgardon@google.com>
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 <mhocko@suse.com>
Signed-off-by: Ben Gardon <bgardon@google.com>
---
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
next prev parent reply other threads:[~2026-12-02 21:04 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 21:03 [PATCH bpf-next v4 00/17] mm: BPF OOM Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 01/17] bpf: mark struct oom_control's memcg field as TRUSTED_OR_NULL Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 02/17] mm, bpf: introduce BPF OOM struct ops Ben Gardon
2026-12-02 21:59 ` bot+bpf-ci
2026-12-02 21:03 ` [PATCH bpf-next v4 03/17] mm, bpf: introduce bpf_oom_kill_process() BPF kfunc Ben Gardon
2026-12-02 21:58 ` bot+bpf-ci
2026-12-02 21:03 ` [PATCH bpf-next v4 04/17] mm, bpf: introduce bpf_out_of_memory() " Ben Gardon
2026-12-02 21:59 ` bot+bpf-ci
2026-12-02 21:03 ` [PATCH bpf-next v4 05/17] selftests/bpf: Add BPF OOM struct ops test Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 06/17] sched/psi: add a tracepoint to psi_avgs_work() Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 07/17] sched/psi: add cgroup_id field to psi_group structure Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 08/17] mm, bpf: allow calling bpf_out_of_memory() from a PSI tracepoint Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 09/17] selftests/bpf: Enable CONFIG_PSI Ben Gardon
2026-12-02 21:04 ` [PATCH bpf-next v4 10/17] selftests/bpf: Add PSI tracepoint OOM test Ben Gardon
2026-12-02 21:04 ` [PATCH bpf-next v4 11/17] selftests/bpf: Add systemd-oomd style OOM policy test Ben Gardon
2026-12-02 21:58 ` bot+bpf-ci
2026-12-02 21:04 ` [PATCH bpf-next v4 12/17] mm, bpf: introduce bpf_oom_proactive_kill() for proactive OOM prevention Ben Gardon
2026-12-02 21:58 ` bot+bpf-ci
2026-12-02 21:04 ` [PATCH bpf-next v4 13/17] selftests/bpf: Add bpf_oom_proactive_kill() proactive OOM test Ben Gardon
2026-12-02 21:04 ` [PATCH bpf-next v4 14/17] selftests/bpf: Add negative and verifier tests for BPF OOM kfuncs Ben Gardon
2026-12-02 21:58 ` bot+bpf-ci
2026-12-02 21:04 ` Ben Gardon [this message]
2026-12-02 21:58 ` [PATCH bpf-next v4 15/17] mm, bpf: introduce bpf_mem_cgroup_raise_max() kfunc bot+bpf-ci
2026-12-02 21:04 ` [PATCH bpf-next v4 16/17] selftests/bpf: Add bpf_mem_cgroup_raise_max() OOM test Ben Gardon
2026-12-02 21:04 ` [PATCH bpf-next v4 17/17] Documentation/bpf: Document BPF OOM handling and kfuncs Ben Gardon
2026-12-02 21:58 ` bot+bpf-ci
2026-12-02 21:38 ` [PATCH bpf-next v4 00/17] mm: BPF OOM Ben Gardon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202210415.517651-16-bgardon@google.com \
--to=bgardon@google.com \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=benbwgardon@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=hannes@cmpxchg.org \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=joshdon@google.com \
--cc=jp.kobryn@linux.dev \
--cc=laoar.shao@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=song@kernel.org \
--cc=surenb@google.com \
--cc=sweettea-kernel@dorminy.me \
--cc=tj@kernel.org \
--cc=yonghong.song@linux.dev \
--cc=zhouchuyi@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox