memcg_ext: dynamic memory.low and memory.min protection
Subsystems (10)
cover memcg_ext: dynamic memory.low and memory.min protection
From: Hui Zhu <[email protected]>
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/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://github.com/teawater/memcg-async-reclaim/tree/memcg_ext_plus
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
Signature · 1 line
2.43.0
RFC PATCH 1/2 memcg_ext: add memory.low and memory.min protection policies
From: Hui Zhu <[email protected]>
mem_cgroup_below_low() and mem_cgroup_below_min() decide whether reclaim may touch a cgroup by comparing the effective threshold against usage. The comparison is fixed, so a workload that wants protection on other terms has no way to ask for it.
Add low_policy and min_policy to bpf_memcg_ops. A program gets a read-only snapshot of the check: the memcg, the reclaim target, the threshold and the usage. It returns a request, and there is one so
far: BPF_MEMCG_PROT_BELOW treats the cgroup as below its threshold, so reclaim skips it.
The kernel compares first and asks BPF only when it found the cgroup unprotected. A check runs the policies of the cgroup and of every ancestor and ORs what they return, so a policy can only add protection, never take it away. Reclaim then treats the cgroup as below its threshold throughout, which includes counting it in memory.events.
Both dispatchers share one walk setup and teardown, factored out of the high dispatcher. The setup carries the task_struct::in_bpf_memcg re-entry guard and a cgroup_on_dfl()
test: a v1 cgroup has no effective array and an uninitialised cgrp->bpf.refcnt. The memory.high debt is v2-only, so a v1 memcg never reached the high dispatcher and no reachable path of it changes, but it does reach the protection checks: emin and elow stay 0 and reclaim still asks.
Guard the dispatcher declarations with CONFIG_BPF_SYSCALL and add stubs so every supported configuration builds. mm/bpf_memcontrol.c is built only with struct_ops support, while CONFIG_MEMCG does not select it, which already left bpf_memcg_high_policy() undefined at link time.
--- include/linux/bpf_memcontrol.h | 104 ++++++++++++++++++++- include/linux/memcontrol.h | 23 ++++- mm/bpf_memcontrol.c | 162 ++++++++++++++++++++++++++------- 3 files changed, 247 insertions(+), 42 deletions(-)
include/linux/bpf_memcontrol.h +101 -3
@@ -2,9 +2,9 @@/* * BPF policy hooks for the memory controller. * -* A bpf_memcg_ops is attached to a cgroup. A charge runs the policies of -* that cgroup and of every ancestor, and the kernel combines what they -* return. BPF only picks between things the kernel already does. +* A bpf_memcg_ops is attached to a cgroup. A hook runs the policies of that +* cgroup and of every ancestor, and the kernel combines what they return. +* BPF only picks between things the kernel already does. */ #ifndef _LINUX_BPF_MEMCONTROL_H#define _LINUX_BPF_MEMCONTROL_H
@@ -30,6 +30,22 @@ enum bpf_memcg_high_request {#define BPF_MEMCG_HIGH_VALID_MASK BPF_MEMCG_HIGH_DEFER_INLINE +/* +* What a policy can ask for when the kernel checks memory.low or memory.min +* protection. The kernel ORs them, so one policy cannot undo another. +*/ +enum bpf_memcg_prot_request { +BPF_MEMCG_PROT_NO_OPINION =0 , +/* +* Treat the cgroup as below its threshold, so reclaim skips it. This +* only adds protection: a policy cannot expose a cgroup the kernel +* already considers below its threshold. +*/ +BPF_MEMCG_PROT_BELOW =1U <<0 , +}; + +#define BPF_MEMCG_PROT_VALID_MASK BPF_MEMCG_PROT_BELOW +/* Read-only snapshot. Only values the caller already has. */ struct bpf_memcg_ctx {struct mem_cgroup *memcg;/* charged memcg */
@@ -41,6 +57,21 @@ struct bpf_memcg_ctx {u32 gfp_flags; }; +/* +* Read-only snapshot of a protection check. @threshold is the effective +* memory.low for low_policy and the effective memory.min for min_policy, and +* the kernel has already found it below @usage. +*/ +struct bpf_memcg_prot_ctx { +struct mem_cgroup *memcg;/* cgroup checked */ +struct mem_cgroup *target;/* NULL for global reclaim */ +struct task_struct *task;/* current */ +u64 cgroup_id; +u64 target_cgroup_id;/* 0 if none */ +u64 threshold;/* elow or emin, pages */ +u64 usage;/* pages */ +}; +struct bpf_memcg_ops {/** * high_policy - say where memory.high should be enforced
@@ -50,8 +81,35 @@ struct bpf_memcg_ops {* are dropped. */ u32 (*high_policy)(const struct bpf_memcg_ctx *ctx); + +/** +* low_policy - say whether a cgroup is below its memory.low +* @ctx: snapshot of the protection check +* +* Only runs once the kernel has found @ctx->usage above +* @ctx->threshold, so a policy can only add protection. Reclaim then +* treats the cgroup as below memory.low throughout, which includes +* counting it in memory.events. +* +* Return: bits from enum bpf_memcg_prot_request, or 0. Other bits +* are dropped. +*/ +u32 (*low_policy)(const struct bpf_memcg_prot_ctx *ctx); + +/** +* min_policy - say whether a cgroup is below its memory.min +* @ctx: snapshot of the protection check +* +* Same as low_policy, for the hard memory.min protection. +* +* Return: bits from enum bpf_memcg_prot_request, or 0. Other bits +* are dropped. +*/ +u32 (*min_policy)(const struct bpf_memcg_prot_ctx *ctx); }; +#if IS_ENABLED (CONFIG_BPF_SYSCALL ) +/* * Run every high_policy on @memcg's cgroup and its ancestors, and return the * combined request for the caller to act on.
@@ -63,4 +121,44 @@ struct bpf_memcg_ops {u32 bpf_memcg_high_policy (struct mem_cgroup *memcg,struct mem_cgroup *over_limit,gfp_t gfp_mask); +/* +* Run every low_policy or min_policy on @memcg's cgroup and its ancestors, +* and return the combined request for the caller to act on. +* +* @memcg: the memcg whose protection is being checked, never the root +* @target: the reclaim root, or NULL for global reclaim +* @elow/@emin: the effective threshold, in pages +* @usage: the current usage, in pages +*/ +u32 bpf_memcg_low_policy (struct mem_cgroup *memcg,struct mem_cgroup *target, +unsigned long elow,unsigned long usage); +u32 bpf_memcg_min_policy (struct mem_cgroup *memcg,struct mem_cgroup *target, +unsigned long emin,unsigned long usage); + +#else /* CONFIG_BPF_SYSCALL */ + +/* mm/bpf_memcontrol.c is only built with struct_ops support. */ +static inline u32 bpf_memcg_high_policy (struct mem_cgroup *memcg, +struct mem_cgroup *over_limit, +gfp_t gfp_mask) +{ +return BPF_MEMCG_HIGH_NO_OPINION ; +} + +static inline u32 bpf_memcg_low_policy (struct mem_cgroup *memcg, +struct mem_cgroup *target, +unsigned long elow,unsigned long usage) +{ +return BPF_MEMCG_PROT_NO_OPINION ; +} + +static inline u32 bpf_memcg_min_policy (struct mem_cgroup *memcg, +struct mem_cgroup *target, +unsigned long emin,unsigned long usage) +{ +return BPF_MEMCG_PROT_NO_OPINION ; +} + +#endif /* CONFIG_BPF_SYSCALL */ +#endif /* _LINUX_BPF_MEMCONTROL_H */
include/linux/memcontrol.h +19 -4
@@ -23,6 +23,7 @@#include <linux/writeback.h> #include <linux/page-flags.h> #include <linux/shrinker.h> +#include <linux/bpf_memcontrol.h> struct mem_cgroup ;struct obj_cgroup ;
@@ -621,21 +622,35 @@ static inline bool mem_cgroup_unprotected(struct mem_cgroup *target,static inline bool mem_cgroup_below_low (struct mem_cgroup *target,struct mem_cgroup *memcg) { +unsigned long elow, usage; +if (mem_cgroup_unprotected (target, memcg))return false; -return READ_ONCE (memcg->memory.elow) >= -page_counter_read (&memcg->memory); + elow =READ_ONCE (memcg->memory.elow); + usage =page_counter_read (&memcg->memory); +if (elow >= usage) +return true; + +return bpf_memcg_low_policy (memcg, target, elow, usage) & +BPF_MEMCG_PROT_BELOW ; }static inline bool mem_cgroup_below_min (struct mem_cgroup *target,struct mem_cgroup *memcg) { +unsigned long emin, usage; +if (mem_cgroup_unprotected (target, memcg))return false; -return READ_ONCE (memcg->memory.emin) >= -page_counter_read (&memcg->memory); + emin =READ_ONCE (memcg->memory.emin); + usage =page_counter_read (&memcg->memory); +if (emin >= usage) +return true; + +return bpf_memcg_min_policy (memcg, target, emin, usage) & +BPF_MEMCG_PROT_BELOW ; }int __mem_cgroup_charge (struct folio *folio,struct mm_struct *mm,gfp_t gfp);
mm/bpf_memcontrol.c +127 -35
206 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH 2/2 selftests/bpf: Add bpf_memcg_ops protection test
In reply to Hui ZhuFrom: Hui Zhu <[email protected]>
low_policy and min_policy are asked after the kernel compares a cgroup's own memory.low/min, so a policy can protect a cgroup that carries neither. Attach both to a child cgroup, fault 8M of anonymous memory into it and reclaim from its parent. Check that the kernel consulted both hooks and that the ctx named the child, the parent as the reclaim target, a zero threshold and a nonzero usage. Without swap nothing is reclaimed and the write returns EAGAIN, but every scan still consulted the hooks; both policies answer NO_OPINION, so the outcome is not asserted.
The counters are filtered by cgroup id: attaching any policy turns the dispatcher on for every memcg, so background reclaim would otherwise move them too.
A full example of answering BELOW so that reclaim skips a cgroup and takes the memory from a sibling is maintained out of tree at [1], released under GPLv2.
[1] https://github.com/teawater/memcg-async-reclaim/tree/memcg_ext_plus
--- .../bpf/prog_tests/memcg_ops_protection.c | 151 ++++++++++++++++++ .../bpf/progs/memcg_ops_protection.c | 72 +++++++++ 2 files changed, 223 insertions(+) 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
tools/testing/selftests/bpf/prog_tests/memcg_ops_protection.c new file +151 -0
@@ -0,0 +1,151 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Drive the memcg_ops_protection sample: check that the kernel consults +* min_policy and low_policy during reclaim, and that the ctx they receive +* describes the check faithfully. The policies themselves always say +* NO_OPINION, so nothing about reclaim's outcome changes. +* +* Layout, under the test workdir: +* +* memcg_ops_prot P reclaim runs from P/memory.reclaim +* memcg_ops_prot/victim C holds the memory, carries the ops +* memcg_ops_prot_ctl - where the test process normally sits +* +* The test process joins C itself and faults in 8M of anonymous memory: +* C's usage above its zero emin/elow is exactly what makes the kernel ask +* the hooks. Nothing is actually reclaimed -- without swap there is +* nothing to take -- so the write returns EAGAIN after seventeen full +* scans, and every one of them consulted the hooks. No file is involved +* on purpose: a VM's working directory is often a shared mount where +* temporary files are unsupported or unevictable. On the way out the +* process rejoins the ctl cgroup, so the framework's cleanup can rmdir +* C and P. +*/ + +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <sys/mman.h> +#include <unistd.h> + +#include <bpf/libbpf.h> + +#include <test_progs.h> +#include "cgroup_helpers.h" +#include "memcg_ops_protection.skel.h" + +#define CTL_PATH "/memcg_ops_prot_ctl" +#define PARENT_PATH "/memcg_ops_prot" +#define VICTIM_PATH PARENT_PATH "/victim" + +#define FILL_SZ (8UL << 20) +#define RECLAIM_SZ "1M" + +void test_memcg_ops_protection (void ) +{ +struct memcg_ops_protection *skel =NULL ; +struct bpf_link *ops_link =NULL ; +int ctlfd =-1 , pfd =-1 , cfd =-1 , rfd; +void *mem =MAP_FAILED ; +ssize_t n; +int err; + +/* Sets the environment up and parks this process outside of P. */ + ctlfd =test__join_cgroup (CTL_PATH ); +if (!ASSERT_GE (ctlfd,0 ,"join_cgroup" )) +return ; + + pfd =create_and_get_cgroup (PARENT_PATH ); +if (!ASSERT_GE (pfd,0 ,"create_parent" )) + goto done; + +/* +* The victim's memory.* files need the memory controller in P's +* subtree_control. A failure here is the usual "v1 still owns +* memory", which is a skip rather than a fail. +*/ +if (enable_controllers (PARENT_PATH ,"memory" )) { +fprintf (stderr,"no memory controller under %s\n" ,PARENT_PATH ); +test__skip (); + goto done; + } + + cfd =create_and_get_cgroup (VICTIM_PATH ); +if (!ASSERT_GE (cfd,0 ,"create_victim" )) + goto done; + + skel =memcg_ops_protection__open (); +if (!ASSERT_OK_PTR (skel,"open" )) + goto done; + + skel->bss->target_cgroup_id =get_cgroup_id (VICTIM_PATH ); + skel->bss->parent_cgroup_id =get_cgroup_id (PARENT_PATH ); +if (!ASSERT_NEQ (skel->bss->target_cgroup_id,0ULL ,"victim_id" )) + goto done; +if (!ASSERT_NEQ (skel->bss->parent_cgroup_id,0ULL ,"parent_id" )) + goto done; + +if (!ASSERT_OK (memcg_ops_protection__load (skel),"load" )) + goto done; + + ops_link =bpf_map__attach_cgroup_opts (skel->maps.prot_ops, cfd,NULL ); +if (!ASSERT_OK_PTR (ops_link,"attach_ops" )) + goto done; + +/* From here on the test process lives in C; leave via done. */ +if (!ASSERT_OK (join_cgroup (VICTIM_PATH ),"join_victim" )) + goto done; + + mem =mmap (NULL ,FILL_SZ ,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (!ASSERT_TRUE (mem !=MAP_FAILED ,"mmap" )) + goto done; +memset (mem,0xa5 ,FILL_SZ ); + +/* The trigger. EAGAIN is as good a scan as success. */ + rfd =openat (pfd,"memory.reclaim" ,O_RDWR ); +if (!ASSERT_GE (rfd,0 ,"open_reclaim" )) + goto done; + n =write (rfd,RECLAIM_SZ ,sizeof (RECLAIM_SZ ) -1 ); + err = n <0 ? -errno :0 ; +close (rfd); +if (!ASSERT_TRUE (err ==0 || err == -EAGAIN ,"reclaim_trigger" )) + goto done; + +/* Both hooks run for one unprotected cgroup in one scan. */ +if (!ASSERT_GT (skel->bss->min_calls,0ULL ,"min_calls" )) + goto done; +if (!ASSERT_GT (skel->bss->low_calls,0ULL ,"low_calls" )) + goto done; + +/* The victim has no memory.min/low, and it is not empty. */ +if (!ASSERT_EQ (skel->bss->seen_min_threshold,0ULL ,"emin" )) + goto done; +if (!ASSERT_GT (skel->bss->seen_min_usage,0ULL ,"min_usage" )) + goto done; +if (!ASSERT_EQ (skel->bss->seen_low_threshold,0ULL ,"elow" )) + goto done; +if (!ASSERT_GT (skel->bss->seen_low_usage,0ULL ,"low_usage" )) + goto done; + +/* The reclaim came from P, and the ctx said so. */ +if (!ASSERT_EQ (skel->bss->saw_min_from_parent,1 ,"min_target" )) + goto done; +ASSERT_EQ (skel->bss->saw_low_from_parent,1 ,"low_target" ); + +done: +/* Detach before anything else: no policy may run during teardown. */ +bpf_link__destroy (ops_link); +memcg_ops_protection__destroy (skel); +if (mem !=MAP_FAILED ) +munmap (mem,FILL_SZ ); +/* A no-op when the process never made it into the victim. */ +join_cgroup (CTL_PATH ); +if (cfd >=0 ) +close (cfd); +if (pfd >=0 ) +close (pfd); +if (ctlfd >=0 ) +close (ctlfd); +}
tools/testing/selftests/bpf/progs/memcg_ops_protection.c new file +72 -0
72 lines not on this page · raw message
Signature · 1 line
2.43.0
Tejun Heo
maintainerHello, Hui.
Quoted 6 lines · Hui Zhu
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.
This doesn't justify the changes. Why can't the same be achieved by adjusting memory.low and memory.min from userspace? The arguments made so far are neither convincing nor proven.
Thanks.
--
tejun
"Tejun Heo" <[email protected] mailto:[email protected]?to=%22Tejun%20Heo%22%20%3Ctj%40kernel.org%3E > :
Quoted 13 lines
Hello, Hui.
Quoted 8 lines · Hui Zhu
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.
Quoted 9 lines
This doesn't justify the changes. Why can't the same be achieved by adjusting memory.low and memory.min from userspace? The arguments made so far are neither convincing nor proven.
Thanks.
--
tejun
Hi Tejun,
Thanks for taking a look.
You are right about that paragraph. It describes what dynamic protection does rather than saying why it has to happen in the kernel, and at the timescale I described a userspace agent writing memory.low would keep up fine. I will drop it.
Let me try to say what I was reaching for, from the code rather than from a benchmark - a comparison between BPF and a userspace agent would say at least as much about how well I wrote the agent as about the interface, so I am not sure it would settle much.
memory.low is a request, and what reclaim honors is elow, which page_counter_calculate_protection() derives on each pass from the parent's elow, the siblings' requests and the tree's usage. At the first level below the reclaim root that comes out equal to what was written, and there the knob is enough. Deeper down it is capped by the parent's budget and shared out between siblings according to what they claimed and what they use, so what a cgroup wrote and what it ends up with can differ. As far as I can tell none of that is visible from
userspace: memory.low reads back the declared value, and I could not find anything that exports elow.
Exporting elow would not help, because elow is not a property of the cgroup alone. It depends on the reclaim root as well, which is why page_counter_calculate_protection() warns that it is not stateless and is only valid as part of a top-down tree iteration, and why mem_cgroup_protection() carries the example of a concurrent global and targeted reclaim driving a child's elow to zero. A file would show whichever value the last reclaim happened to leave there.
So the derived value seems to be usable only where it is derived, inside the check, and that is where low_policy() sits: it is evaluated per pass and is handed that pass's own inputs - the memcg, the reclaim target, the elow just computed, the usage - instead of being state that is read at the check.
The other half is what a policy can key on. memory.low has one dimension, bytes. A policy program is not limited to the ctx it is handed; it can also use state that other BPF programs keep, so protection can be a function of activity rather than of size - a workingset refault rate, for instance. [1] reads those counters from BPF and acts on them; it drives reclaim rather than answering a protection hook, but the signal it reads is the one such a policy would want. The limit is on the output rather than the input: the hook returns a boolean, so it cannot grant anything memory.low cannot.
I would be interested in how you see this - in particular whether the nested case is worth supporting at all, and if it is, whether this is the right shape for it. I am happy to rework the patches, and to add a demonstration of one concrete policy if that would make it easier to judge.
[1] https://github.com/teawater/memcg-async-reclaim/tree/memcg_ext_plus
Thanks,
Hui
Tejun Heo
maintainerHello, Hui.
Quoted 10 lines · Hui Zhu
memory.low is a request, and what reclaim honors is elow, which
page_counter_calculate_protection()derives on each pass from the parent's elow, the siblings' requests and the tree's usage. At the first level below the reclaim root that comes out equal to what was written, and there the knob is enough. Deeper down it is capped by the parent's budget and shared out between siblings according to what they claimed and what they use, so what a cgroup wrote and what it ends up with can differ. As far as I can tell none of that is visible from
userspace: memory.low reads back the declared value, and I could not find anything that exports elow.
...
Quoted 5 lines
So the derived value seems to be usable only where it is derived, inside the check, and that is where
low_policy()sits: it is evaluated per pass and is handed that pass's own inputs - the memcg, the reclaim target, the elow just computed, the usage - instead of being state that is read at the check.
The visibility of effective values is a problem of wider scope, not an argument for implementing bpf struct_ops. There were discussions in the past on whether we could expose some meaningful effective value. They didn't lead to anything conclusive, and we may yet find a way to expose something more useful.
Also, the hook doesn't use elow to decide anything. mem_cgroup_below_low() and below_min() return true as long as elow or emin covers the usage. They ask the policies only once the usage exceeds it, and the only answer a policy can give is BELOW, which makes reclaim skip the cgroup. So, what the hook buys in the nested case isn't access to the per-pass elow. It lets a cgroup escape the ceiling that its parent's protection imposes on the subtree.
Some of that may be innate to nested protection semantics. One shortcoming may be that there's no way for a parent to "passthrough" in terms of protection, because there's no way for a parent to be neutral. If its memory.low is too low, that's the ceiling for the whole subhierarchy. If it's set high, the subtree gets the total protection one way or another. There may be arguments for a different semantics which changes the shape of the protection.
However, it doesn't make sense to let a BPF program escape the parent's policy, and, again, none of this is an argument for adding bpf struct_ops.
Quoted 9 lines
The other half is what a policy can key on. memory.low has one dimension, bytes. A policy program is not limited to the ctx it is handed; it can also use state that other BPF programs keep, so protection can be a function of activity rather than of size - a workingset refault rate, for instance. [1] reads those counters from BPF and acts on them; it drives reclaim rather than answering a protection hook, but the signal it reads is the one such a policy would want. The limit is on the output rather than the input: the hook returns a boolean, so it cannot grant anything memory.low cannot.
These signals are all available to userspace. The workingset refault counters are in memory.stat, PSI is in memory.pressure, and whatever state other BPF programs keep sits in BPF maps which userspace can read. So, the inputs aren't the difference. What's left is how quickly and how often the protection gets adjusted.
That may be an argument, but nothing so far shows that struct_ops is needed for it. Can you demonstrate a realistic and useful scenario which can't be achieved from userspace but can be with struct_ops?
Thanks.
--
tejun