|
|
Log in / Subscribe / Register

memcg_ext: dynamic memory.low and memory.min protection

From:  Hui Zhu <hui.zhu-AT-linux.dev>
To:  Alexei Starovoitov <ast-AT-kernel.org>, Daniel Borkmann <daniel-AT-iogearbox.net>, John Fastabend <john.fastabend-AT-gmail.com>, Andrii Nakryiko <andrii-AT-kernel.org>, Martin KaFai Lau <martin.lau-AT-linux.dev>, Eduard Zingerman <eddyz87-AT-gmail.com>, Kumar Kartikeya Dwivedi <memxor-AT-gmail.com>, Song Liu <song-AT-kernel.org>, Yonghong Song <yonghong.song-AT-linux.dev>, Jiri Olsa <jolsa-AT-kernel.org>, Johannes Weiner <hannes-AT-cmpxchg.org>, Michal Hocko <mhocko-AT-kernel.org>, Roman Gushchin <roman.gushchin-AT-linux.dev>, Shakeel Butt <shakeel.butt-AT-linux.dev>, Muchun Song <muchun.song-AT-linux.dev>, JP Kobryn <jp.kobryn-AT-linux.dev>, Andrew Morton <akpm-AT-linux-foundation.org>, Shuah Khan <shuah-AT-kernel.org>, davem-AT-davemloft.net, Jakub Kicinski <kuba-AT-kernel.org>, Jesper Dangaard Brouer <hawk-AT-kernel.org>, Stanislav Fomichev <sdf-AT-fomichev.me>, KP Singh <kpsingh-AT-kernel.org>, Tao Chen <chen.dylane-AT-linux.dev>, Mykyta Yatsenko <yatsenko-AT-meta.com>, Leon Hwang <leon.hwang-AT-linux.dev>, Anton Protopopov <a.s.protopopov-AT-gmail.com>, Amery Hung <ameryhung-AT-gmail.com>, Tobias Klauser <tklauser-AT-distanz.ch>, Eyal Birger <eyal.birger-AT-gmail.com>, Rong Tao <rongtao-AT-cestc.cn>, Hao Luo <haoluo-AT-google.com>, Peter Zijlstra <peterz-AT-infradead.org>, Miguel Ojeda <ojeda-AT-kernel.org>, Nathan Chancellor <nathan-AT-kernel.org>, Kees Cook <kees-AT-kernel.org>, Tejun Heo <tj-AT-kernel.org>, Jeff Xu <jeffxu-AT-chromium.org>, mkoutny-AT-suse.com, Jan Hendrik Farr <kernel-AT-jfarr.cc>, Christian Brauner <brauner-AT-kernel.org>, Randy Dunlap <rdunlap-AT-infradead.org>, Brian Gerst <brgerst-AT-gmail.com>, Masahiro Yamada <masahiroy-AT-kernel.org>, Willem de Bruijn <willemb-AT-google.com>, Jason Xing <kerneljasonxing-AT-gmail.com>, Paul Chaignon <paul.chaignon-AT-gmail.com>, Lance Yang <lance.yang-AT-linux.dev>, Jiayuan Chen <jiayuan.chen-AT-linux.dev>, Emil Tsalapatis <emil-AT-etsalapatis.com>, Ihor Solodrai <ihor.solodrai-AT-linux.dev>, Barry Song <baohua-AT-kernel.org>, Geliang Tang <geliang-AT-kernel.org>, linux-kernel-AT-vger.kernel.org, bpf-AT-vger.kernel.org, cgroups-AT-vger.kernel.org, linux-mm-AT-kvack.org, netdev-AT-vger.kernel.org, linux-kselftest-AT-vger.kernel.org
Subject:  [RFC PATCH 0/2] memcg_ext: dynamic memory.low and memory.min protection
Date:  Tue, 27 Oct 2026 17:11:58 +0800
Message-ID:  <cover.1793086858.git.zhuhui@kylinos.cn>
Cc:  Hui Zhu <zhuhui-AT-kylinos.cn>
Archive-link:  Article

From: Hui Zhu <zhuhui@kylinos.cn>

This follows the memcg_ext RFC [1], which adds bpf_memcg_ops - a
struct_ops type attaching memory controller policy to a cgroup - and
one member, high_policy().  Its cover letter names dynamic protection
as future work; these two patches are that.

Why it matters: memory.low and memory.min protect a cgroup by a fixed
threshold and have no idea whether it is using the memory.  A
high-priority cgroup that holds a large working set and then goes idle
keeps suppressing reclaim on its siblings, and that memory is wasted.
A policy program sees the real activity - page faults, charges - and
can grant or withdraw protection as it changes.

The hooks are not new: [2] proposed the same below-low and below-min
decision on its own memcg_bpf_ops type.  Only the carrier changes
here.  bpf_memcg_ops already supplies attach, the ancestor walk,
result combination and the re-entry guard, so protection costs two
members and one walk helper shared with high_policy().

Patch 1 adds low_policy() and min_policy().  A program gets a
read-only snapshot of the check the kernel is making - the memcg, the
reclaim target, the threshold and the usage - and returns a request;
BPF_MEMCG_PROT_BELOW, the only one so far, treats the cgroup as below
its threshold, so reclaim skips it and counts it in memory.events.
The kernel compares elow/emin first and asks BPF only when it found
the cgroup unprotected, and ORs the answers of the cgroup and every
ancestor, so a policy can only add protection, never take it away.

Patch 2 is the prog_tests runner: attach both policies to a child,
fault 8M of anonymous memory into it, reclaim from its parent, and
check that both hooks were consulted and that the ctx described the
check faithfully.  Both answer NO_OPINION, so nothing about reclaim's
outcome is asserted.  A working example that does answer BELOW, so
reclaim skips one cgroup and takes the memory from a sibling instead,
is maintained out of tree at [3], released under GPLv2.

[1] https://lore.kernel.org/all/20260921192559.2619635-1-shak...
[2] https://lore.kernel.org/all/cover.1779760876.git.zhuhui@k...
[3] https://github.com/teawater/memcg-async-reclaim/tree/memc...

Hui Zhu (2):
  memcg_ext: add memory.low and memory.min protection policies
  selftests/bpf: Add bpf_memcg_ops protection test

 include/linux/bpf_memcontrol.h                | 104 ++++++++++-
 include/linux/memcontrol.h                    |  23 ++-
 mm/bpf_memcontrol.c                           | 162 ++++++++++++++----
 .../bpf/prog_tests/memcg_ops_protection.c     | 151 ++++++++++++++++
 .../bpf/progs/memcg_ops_protection.c          |  72 ++++++++
 5 files changed, 470 insertions(+), 42 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/memcg_ops_protection.c
 create mode 100644 tools/testing/selftests/bpf/progs/memcg_ops_protection.c

-- 
2.43.0




Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds