mm: bpf kfuncs to access memcg data
Subsystems (6)
Roman Gushchin
maintainercover mm: bpf kfuncs to access memcg data
Introduce kfuncs to simplify the access to the memcg data. These kfuncs can be used to accelerate monitoring use cases and for implementing custom OOM policies once BPF OOM is landed.
This patchset was separated out from the BPF OOM patchset to simplify the logistics and accelerate the landing of the part which is useful by itself. No functional changes since BPF OOM v2.
JP Kobryn (2):
mm: introduce BPF kfunc to access memory events
bpf: selftests: selftests for memcg stat kfuncs
Roman Gushchin (4):
mm: declare memcg_page_state_output() in memcontrol.h
mm: introduce BPF kfuncs to deal with memcg pointers
mm: introduce bpf_get_root_mem_cgroup() BPF kfunc
mm: introduce BPF kfuncs to access memcg statistics and events
include/linux/memcontrol.h | 3 + mm/Makefile | 3 + mm/bpf_memcontrol.c | 175 ++++++++++++++ mm/memcontrol-v1.h | 1 - .../testing/selftests/bpf/cgroup_iter_memcg.h | 18 ++ .../bpf/prog_tests/cgroup_iter_memcg.c | 223 ++++++++++++++++++ .../selftests/bpf/progs/cgroup_iter_memcg.c | 42 ++++ 7 files changed, 464 insertions(+), 1 deletion(-) create mode 100644 mm/bpf_memcontrol.c create mode 100644 tools/testing/selftests/bpf/cgroup_iter_memcg.h create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c
Signature · 1 line
2.52.0
Roman Gushchin
maintainerPATCH 1/6 mm: declare memcg_page_state_output() in memcontrol.h
To use memcg_page_state_output() in bpf_memcontrol.c move the declaration from v1-specific memcontrol-v1.h to memcontrol.h.
--- include/linux/memcontrol.h | 1 + mm/memcontrol-v1.h | 1 - 2 files changed, 1 insertion(+), 1 deletion(-)
include/linux/memcontrol.h +1 -0
@@ -950,6 +950,7 @@ static inline void mod_memcg_page_state(struct page *page, }unsigned long memcg_page_state (struct mem_cgroup *memcg,int idx); +unsigned long memcg_page_state_output (struct mem_cgroup *memcg,int item);unsigned long lruvec_page_state (struct lruvec *lruvec,enum node_stat_item idx);unsigned long lruvec_page_state_local (struct lruvec *lruvec,enum node_stat_item idx);
mm/memcontrol-v1.h +0 -1
@@ -27,7 +27,6 @@ unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap);void drain_all_stock (struct mem_cgroup *root_memcg);unsigned long memcg_events (struct mem_cgroup *memcg,int event); -unsigned long memcg_page_state_output (struct mem_cgroup *memcg,int item);int memory_stat_show (struct seq_file *m,void *v);void mem_cgroup_id_get_many (struct mem_cgroup *memcg,unsigned int n);
Signature · 1 line
2.52.0
Re: [PATCH bpf-next v1 1/6] mm: declare memcg_page_state_output() in memcontrol.h
Quoted 5 lines · Roman Gushchin
To use
memcg_page_state_output()inbpf_memcontrol.cmove the declaration from v1-specificmemcontrol-v1.htomemcontrol.h.Signed-off-by Roman Gushchin <[email protected]>Acked-by Michal Hocko <[email protected]>
Roman Gushchin
maintainerPATCH 2/6 mm: introduce BPF kfuncs to deal with memcg pointers
In reply to Roman GushchinTo effectively operate with memory cgroups in BPF there is a need to convert css pointers to memcg pointers. A simple container_of cast which is used in the kernel code can't be used in BPF because from the verifier's point of view that's a out-of-bounds memory access.
Introduce helper get/put kfuncs which can be used to get a refcounted memcg pointer from the css pointer:
- bpf_get_mem_cgroup,
- bpf_put_mem_cgroup.
bpf_get_mem_cgroup() can take both memcg's css and the corresponding cgroup's "self" css. It allows it to be used with the existing cgroup iterator which iterates over cgroup tree, not memcg tree.
--- mm/Makefile | 3 ++ mm/bpf_memcontrol.c | 88 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 mm/bpf_memcontrol.c
mm/Makefile +3 -0
@@ -106,6 +106,9 @@ obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o ifdef CONFIG_SWAP obj-$(CONFIG_MEMCG) += swap_cgroup.o endif +ifdef CONFIG_BPF_SYSCALL +obj-$(CONFIG_MEMCG) += bpf_memcontrol.o +endif obj-$(CONFIG_CGROUP_HUGETLB) += hugetlb_cgroup.o obj-$(CONFIG_GUP_TEST) += gup_test.o obj-$(CONFIG_DMAPOOL_TEST) += dmapool_test.o
mm/bpf_memcontrol.c new file +88 -0
88 lines not on this page · raw message
Signature · 1 line
2.52.0
Shakeel Butt
maintainerRe: [PATCH bpf-next v1 2/6] mm: introduce BPF kfuncs to deal with memcg pointers
Quoted 10 lines · Roman Gushchin
To effectively operate with memory cgroups in BPF there is a need to convert css pointers to memcg pointers. A simple
container_ofcast which is used in the kernel code can't be used in BPF because from the verifier's point of view that's a out-of-bounds memory access.Introduce helper get/put kfuncs which can be used to get a refcounted memcg pointer from the css pointer:
-bpf_get_mem_cgroup,
-bpf_put_mem_cgroup.
Quoted 8 lines
bpf_get_mem_cgroup()can take both memcg's css and the corresponding cgroup's "self" css. It allows it to be used with the existing cgroup iterator which iterates over cgroup tree, not memcg tree.Signed-off-by Roman Gushchin <[email protected]>--- mm/Makefile | 3 ++ mm/bpf_memcontrol.c | 88 +++++++++++++++++++++++++++++++++++++++++++++
Let's add this file to MAINTAINERS file.
Quoted 45 lines
2 files changed, 91 insertions(+) create mode 100644 mm/bpf_memcontrol.cmm/Makefile +3
-0@@ -106,6 +106,9 @@ obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o ifdef CONFIG_SWAP obj-$(CONFIG_MEMCG) += swap_cgroup.o endif +ifdef CONFIG_BPF_SYSCALL +obj-$(CONFIG_MEMCG) += bpf_memcontrol.o +endif obj-$(CONFIG_CGROUP_HUGETLB) += hugetlb_cgroup.o obj-$(CONFIG_GUP_TEST) += gup_test.o obj-$(CONFIG_DMAPOOL_TEST) += dmapool_test.omm/bpf_memcontrol.c new file +22
-0@@ -0,0 +1,88 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Memory Controller-related BPF kfuncs and auxiliary code +* +* Author: Roman Gushchin <[email protected]> +*/ + +#include <linux/memcontrol.h> +#include <linux/bpf.h> + +__bpf_kfunc_start_defs (); + +/** +* bpf_get_mem_cgroup - Get a reference to a memory cgroup +* @css: pointer to the css structure +* +* Returns a pointer to a mem_cgroup structure after bumping +* the corresponding css's reference counter. +* +* It's fine to pass a css which belongs to any cgroup controller, +* e.g. unified hierarchy's main css. +*
mm/bpf_memcontrol.c line 32
Quoted 10 lines
mm/bpf_memcontrol.c new file +10
-0+* Implements KF_ACQUIRE semantics. +*/ +__bpf_kfunc struct mem_cgroup * +bpf_get_mem_cgroup (struct cgroup_subsys_state *css) +{ +struct mem_cgroup *memcg =NULL ; +bool rcu_unlock = false; + +if (!root_mem_cgroup) +return NULL ;
Should we also handle mem_cgroup_disabled() here?
Quoted 5 lines
mm/bpf_memcontrol.c new file +5
-0+ +if (root_mem_cgroup->css.ss != css->ss) { +struct cgroup *cgroup = css->cgroup; +int ssid = root_mem_cgroup->css.ss->id; +
mm/bpf_memcontrol.c line 47
Quoted 10 lines
mm/bpf_memcontrol.c new file +10
-0+rcu_read_lock (); + rcu_unlock = true; + css =rcu_dereference_raw (cgroup->subsys[ssid]); + } + +if (css &&css_tryget (css)) + memcg = container_of(css, struct mem_cgroup, css); + +if (rcu_unlock) +rcu_read_unlock ();
Any reason to handle rcu lock like this? Why not just take the rcu read lock irrespective? It is cheap.
Quoted 4 lines
mm/bpf_memcontrol.c new file +4
-0+ +return memcg; +} +
mm/bpf_memcontrol.c line 61
Quoted 10 lines
mm/bpf_memcontrol.c new file +10
-0+/** +* bpf_put_mem_cgroup - Put a reference to a memory cgroup +* @memcg: memory cgroup to release +* +* Releases a previously acquired memcg reference. +* Implements KF_RELEASE semantics. +*/ +__bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) +{ +css_put (&memcg->css);
Should we NULL check memcg here? bpf_get_mem_cgroup() can return NULL.
mm/bpf_memcontrol.c line 68
Quoted 7 lines
mm/bpf_memcontrol.c new file +7
-0+} + +__bpf_kfunc_end_defs (); + +BTF_KFUNCS_START(bpf_memcontrol_kfuncs) +BTF_ID_FLAGS(func, bpf_get_mem_cgroup, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NULL | KF_RCU) +BTF_ID_FLAGS(func, bpf_put_mem_cgroup, KF_TRUSTED_ARGS | KF_RELEASE)
Will the verifier enforce that bpf_put_mem_cgroup() can not be called with NULL?
Quoted 23 lines
mm/bpf_memcontrol.c new file +20
-0+ +BTF_KFUNCS_END (bpf_memcontrol_kfuncs) + +static const struct btf_kfunc_id_set bpf_memcontrol_kfunc_set = { + .owner =THIS_MODULE , + .set = &bpf_memcontrol_kfuncs, +}; + +static int __init bpf_memcontrol_init (void ) +{ +int err; + + err =register_btf_kfunc_id_set (BPF_PROG_TYPE_UNSPEC , + &bpf_memcontrol_kfunc_set); +if (err) +pr_warn ("error while registering bpf memcontrol kfuncs: %d" , err); + +return err; +} +late_initcall (bpf_memcontrol_init);Signature · 1 line
2.52.0
Roman Gushchin
maintainerRe: [PATCH bpf-next v1 2/6] mm: introduce BPF kfuncs to deal with memcg pointers
Quoted 15 lines · Shakeel Butt
Quoted 14 lines · Roman Gushchin
To effectively operate with memory cgroups in BPF there is a need to convert css pointers to memcg pointers. A simple
container_ofcast which is used in the kernel code can't be used in BPF because from the verifier's point of view that's a out-of-bounds memory access.Introduce helper get/put kfuncs which can be used to get a refcounted memcg pointer from the css pointer:
-bpf_get_mem_cgroup,
-bpf_put_mem_cgroup.
bpf_get_mem_cgroup()can take both memcg's css and the corresponding cgroup's "self" css. It allows it to be used with the existing cgroup iterator which iterates over cgroup tree, not memcg tree.
Quoted 6 lines
Quoted 4 lines
Signed-off-by Roman Gushchin <[email protected]>--- mm/Makefile | 3 ++ mm/bpf_memcontrol.c | 88 +++++++++++++++++++++++++++++++++++++++++++++Let's add this file to
MAINTAINERSfile.
Will do. I planned to create a new entry for mm-related bpf files as part of the bpf oom patchset.
Quoted 50 lines
Quoted 50 lines
2 files changed, 91 insertions(+) create mode 100644 mm/bpf_memcontrol.cmm/Makefile +3
-0@@ -106,6 +106,9 @@ obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o ifdef CONFIG_SWAP obj-$(CONFIG_MEMCG) += swap_cgroup.o endif +ifdef CONFIG_BPF_SYSCALL +obj-$(CONFIG_MEMCG) += bpf_memcontrol.o +endif obj-$(CONFIG_CGROUP_HUGETLB) += hugetlb_cgroup.o obj-$(CONFIG_GUP_TEST) += gup_test.o obj-$(CONFIG_DMAPOOL_TEST) += dmapool_test.omm/bpf_memcontrol.c new file +26
-0@@ -0,0 +1,88 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Memory Controller-related BPF kfuncs and auxiliary code +* +* Author: Roman Gushchin <[email protected]> +*/ + +#include <linux/memcontrol.h> +#include <linux/bpf.h> + +__bpf_kfunc_start_defs (); + +/** +* bpf_get_mem_cgroup - Get a reference to a memory cgroup +* @css: pointer to the css structure +* +* Returns a pointer to a mem_cgroup structure after bumping +* the corresponding css's reference counter. +* +* It's fine to pass a css which belongs to any cgroup controller, +* e.g. unified hierarchy's main css. +* +* Implements KF_ACQUIRE semantics. +*/ +__bpf_kfunc struct mem_cgroup * +bpf_get_mem_cgroup (struct cgroup_subsys_state *css)
Quoted 8 lines
mm/bpf_memcontrol.c line 32
Quoted 6 lines
mm/bpf_memcontrol.c new file +6
-0+{ +struct mem_cgroup *memcg =NULL ; +bool rcu_unlock = false; + +if (!root_mem_cgroup) +return NULL ;Should we also handle
mem_cgroup_disabled()here?
Good point, will add in v2. Same with bpf_get_root_mem_cgroup() patch.
Quoted 11 lines
Quoted 11 lines
mm/bpf_memcontrol.c new file +10
-0+ +if (root_mem_cgroup->css.ss != css->ss) { +struct cgroup *cgroup = css->cgroup; +int ssid = root_mem_cgroup->css.ss->id; + +rcu_read_lock (); + rcu_unlock = true; + css =rcu_dereference_raw (cgroup->subsys[ssid]); + } +
Quoted 8 lines
mm/bpf_memcontrol.c line 47
Quoted 5 lines
mm/bpf_memcontrol.c new file +5
-0+if (css &&css_tryget (css)) + memcg = container_of(css, struct mem_cgroup, css); + +if (rcu_unlock) +rcu_read_unlock ();Any reason to handle rcu lock like this? Why not just take the rcu read lock irrespective? It is cheap.
Idk, it's cheap but not entirely free and I think the code is still perfectly readable.
Quoted 17 lines
Quoted 5 lines
mm/bpf_memcontrol.c new file +4
-0+ +return memcg; +} +mm/bpf_memcontrol.c line 61
Quoted 10 lines
mm/bpf_memcontrol.c new file +10
-0+/** +* bpf_put_mem_cgroup - Put a reference to a memory cgroup +* @memcg: memory cgroup to release +* +* Releases a previously acquired memcg reference. +* Implements KF_RELEASE semantics. +*/ +__bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) +{ +css_put (&memcg->css);Should we NULL check memcg here?
bpf_get_mem_cgroup()can return NULL.
No, the verifier ensures it's a valid memcg pointer. No need for an additional check.
Quoted 11 lines
mm/bpf_memcontrol.c line 68
Quoted 8 lines
mm/bpf_memcontrol.c new file +7
-0+} + +__bpf_kfunc_end_defs (); + +BTF_KFUNCS_START(bpf_memcontrol_kfuncs) +BTF_ID_FLAGS(func, bpf_get_mem_cgroup, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NULL | KF_RCU) +BTF_ID_FLAGS(func, bpf_put_mem_cgroup, KF_TRUSTED_ARGS | KF_RELEASE)Will the verifier enforce that
bpf_put_mem_cgroup()can not be called with NULL?
Yep.
Thanks for the review!
Roman Gushchin
maintainerPATCH 3/6 mm: introduce bpf_get_root_mem_cgroup() BPF kfunc
In reply to Roman GushchinIntroduce a BPF kfunc to get a trusted pointer to the root memory cgroup. It's very handy to traverse the full memcg tree, e.g. for handling a system-wide OOM.
It's possible to obtain this pointer by traversing the memcg tree up from any known memcg, but it's sub-optimal and makes BPF programs more complex and less efficient.
bpf_get_root_mem_cgroup() has a KF_ACQUIRE | KF_RET_NULL semantics, however in reality it's not necessarily to bump the corresponding reference counter - root memory cgroup is immortal, reference counting is skipped, see css_get(). Once set, root_mem_cgroup is always a valid memcg pointer. It's safe to call bpf_put_mem_cgroup() for the pointer obtained with bpf_get_root_mem_cgroup(), it's effectively a no-op.
--- mm/bpf_memcontrol.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
mm/bpf_memcontrol.c +15 -0
@@ -10,6 +10,20 @@__bpf_kfunc_start_defs (); +/** +* bpf_get_root_mem_cgroup - Returns a pointer to the root memory cgroup +* +* The function has KF_ACQUIRE semantics, even though the root memory +* cgroup is never destroyed after being created and doesn't require +* reference counting. And it's perfectly safe to pass it to +* bpf_put_mem_cgroup() +*/ +__bpf_kfunc struct mem_cgroup *bpf_get_root_mem_cgroup(void) +{ +/* css_get() is not needed */ +return root_mem_cgroup; +} +/** * bpf_get_mem_cgroup - Get a reference to a memory cgroup * @css: pointer to the css structure
@@ -64,6 +78,7 @@ __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)__bpf_kfunc_end_defs (); BTF_KFUNCS_START(bpf_memcontrol_kfuncs) +BTF_ID_FLAGS(func, bpf_get_root_mem_cgroup, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_get_mem_cgroup, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NULL | KF_RCU) BTF_ID_FLAGS(func, bpf_put_mem_cgroup, KF_TRUSTED_ARGS | KF_RELEASE)
Signature · 1 line
2.52.0
Shakeel Butt
maintainerRe: [PATCH bpf-next v1 3/6] mm: introduce bpf_get_root_mem_cgroup() BPF kfunc
Quoted 10 lines · Roman Gushchin
Introduce a BPF kfunc to get a trusted pointer to the root memory cgroup. It's very handy to traverse the full memcg tree, e.g. for handling a system-wide OOM.
It's possible to obtain this pointer by traversing the memcg tree up from any known memcg, but it's sub-optimal and makes BPF programs more complex and less efficient.
bpf_get_root_mem_cgroup()has aKF_ACQUIRE|KF_RET_NULLsemantics, however in reality it's not necessarily
necessary*
Quoted 21 lines
to bump the corresponding
reference counter - root memory cgroup is immortal, reference counting is skipped, seecss_get(). Once set,root_mem_cgroupis always a valid memcg pointer. It's safe to callbpf_put_mem_cgroup()for the pointer obtained withbpf_get_root_mem_cgroup(), it's effectively a no-op.Signed-off-by Roman Gushchin <[email protected]>--- mm/bpf_memcontrol.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)mm/bpf_memcontrol.c +2
-0@@ -10,6 +10,20 @@__bpf_kfunc_start_defs (); +/** +* bpf_get_root_mem_cgroup - Returns a pointer to the root memory cgroup
mm/bpf_memcontrol.c line 24
Quoted 10 lines
mm/bpf_memcontrol.c +10
-0+* +* The function has KF_ACQUIRE semantics, even though the root memory +* cgroup is never destroyed after being created and doesn't require +* reference counting. And it's perfectly safe to pass it to +* bpf_put_mem_cgroup() +*/ +__bpf_kfunc struct mem_cgroup *bpf_get_root_mem_cgroup(void) +{ +/* css_get() is not needed */ +return root_mem_cgroup;
I think we need mem_cgroup_disabled() check here as well because I think root_mem_cgroup can get allocated before memcg can be disabled due to boot param.
Quoted 16 lines
mm/bpf_memcontrol.c +3
-0+} +/** * bpf_get_mem_cgroup - Get a reference to a memory cgroup * @css: pointer to the css structure @@ -64,6 +78,7 @@ __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)__bpf_kfunc_end_defs (); BTF_KFUNCS_START(bpf_memcontrol_kfuncs) +BTF_ID_FLAGS(func, bpf_get_root_mem_cgroup, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_get_mem_cgroup, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NULL | KF_RCU) BTF_ID_FLAGS(func, bpf_put_mem_cgroup, KF_TRUSTED_ARGS | KF_RELEASE)Signature · 1 line
2.52.0
Roman Gushchin
maintainerPATCH 4/6 mm: introduce BPF kfuncs to access memcg statistics and events
In reply to Roman GushchinIntroduce BPF kfuncs to conveniently access memcg data:
- bpf_mem_cgroup_vm_events(),
- bpf_mem_cgroup_usage(),
- bpf_mem_cgroup_page_state(),
- bpf_mem_cgroup_flush_stats().
These functions are useful for implementing BPF OOM policies, but also can be used to accelerate access to the memcg data. Reading it through cgroupfs is much more expensive, roughly 5x, mostly because of the need to convert the data into the text and back.
JP Kobryn:
An experiment was setup to compare the performance of a program that uses the traditional method of reading memory.stat vs a program using the new kfuncs. The control program opens up the root memory.stat file and for 1M iterations reads, converts the string values to numeric data, then seeks back to the beginning. The experimental program sets up the requisite libbpf objects and for 1M iterations invokes a bpf program which uses the kfuncs to fetch all available stats for node_stat_item, memcg_stat_item, and vm_event_item types.
The results showed a significant perf benefit on the experimental side, outperforming the control side by a margin of 93%. In kernel mode, elapsed time was reduced by 80%, while in user mode, over 99% of time was saved.
control: elapsed time
real 0m38.318s user 0m25.131s sys 0m13.070s
experiment: elapsed time
real 0m2.789s user 0m0.187s sys 0m2.512s
control: perf data
33.43% a.out libc.so.6 [.] __vfscanf_internal
6.88% a.out [kernel.kallsyms] [k] vsnprintf
6.33% a.out libc.so.6 [.] _IO_fgets
5.51% a.out [kernel.kallsyms] [k] format_decode
4.31% a.out libc.so.6 [.] __GI_____strtoull_l_internal
3.78% a.out [kernel.kallsyms] [k] string
3.53% a.out [kernel.kallsyms] [k] number
2.71% a.out libc.so.6 [.] _IO_sputbackc
2.41% a.out [kernel.kallsyms] [k] strlen
1.98% a.out a.out [.] main 1.70% a.out libc.so.6 [.] _IO_getline_info 1.51% a.out libc.so.6 [.] __isoc99_sscanf
1.47% a.out [kernel.kallsyms] [k] memory_stat_format 1.47% a.out [kernel.kallsyms] [k] memcpy_orig 1.41% a.out [kernel.kallsyms] [k] seq_buf_printf
experiment: perf data
10.55% memcgstat bpf_prog_..._query [k] bpf_prog_16aab2f19fa982a7_query
6.90% memcgstat [kernel.kallsyms] [k] memcg_page_state_output 3.55% memcgstat [kernel.kallsyms] [k] _raw_spin_lock 3.12% memcgstat [kernel.kallsyms] [k] memcg_events 2.87% memcgstat [kernel.kallsyms] [k] __memcg_slab_post_alloc_hook 2.73% memcgstat [kernel.kallsyms] [k] kmem_cache_free 2.70% memcgstat [kernel.kallsyms] [k] entry_SYSRETQ_unsafe_stack 2.25% memcgstat [kernel.kallsyms] [k] __memcg_slab_free_hook 2.06% memcgstat [kernel.kallsyms] [k] get_page_from_freelist
--- include/linux/memcontrol.h | 2 ++ mm/bpf_memcontrol.c | 55 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+)
include/linux/memcontrol.h +2 -0
@@ -949,6 +949,8 @@ static inline void mod_memcg_page_state(struct page *page,rcu_read_unlock (); } +unsigned long memcg_events (struct mem_cgroup *memcg,int event); +unsigned long mem_cgroup_usage (struct mem_cgroup *memcg,bool swap);unsigned long memcg_page_state (struct mem_cgroup *memcg,int idx);unsigned long memcg_page_state_output (struct mem_cgroup *memcg,int item);unsigned long lruvec_page_state (struct lruvec *lruvec,enum node_stat_item idx);
mm/bpf_memcontrol.c +55 -0
67 lines not on this page · raw message
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH bpf-next v1 4/6] mm: introduce BPF kfuncs to access memcg statistics and events
Should bpf_mem_cgroup_vm_events() validate the event parameter before passing it to memcg_events()? The call chain is:
Quoted 4 lines
mm/bpf_memcontrol.c +0
-0
[ ... ]
mm/bpf_memcontrol.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 89
Quoted 12 lines
mm/bpf_memcontrol.c +12
-0+/** +* bpf_mem_cgroup_vm_events - Read memory cgroup's vm event counter +* @memcg: memory cgroup +* @event: event id +* +* Allows to read memory cgroup event counters. +*/ +__bpf_kfunc unsigned long bpf_mem_cgroup_vm_events (struct mem_cgroup *memcg, +enum vm_event_item event) +{ +return memcg_events (memcg, event); +}
Should bpf_mem_cgroup_vm_events() validate the event parameter before passing it to memcg_events()? The call chain is:
bpf_mem_cgroup_vm_events() -> memcg_events() -> memcg_events_index()
And memcg_events_index() does:
return mem_cgroup_events_index[idx];
where mem_cgroup_events_index is an array of size NR_VM_EVENT_ITEMS. If a BPF program passes event >= NR_VM_EVENT_ITEMS, this would be an out-of-bounds array read.
The sister function bpf_mem_cgroup_page_state() below validates its idx parameter before calling its callee:
Quoted 4 lines
mm/bpf_memcontrol.c +4
-0+/** +* bpf_mem_cgroup_page_state - Read memory cgroup's page state counter +* @memcg: memory cgroup +* @idx: counter idx
mm/bpf_memcontrol.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 115
Quoted 10 lines
mm/bpf_memcontrol.c +10
-0+* +* Allows to read memory cgroup statistics. The output is in bytes. +*/ +__bpf_kfunc unsigned long bpf_mem_cgroup_page_state (struct mem_cgroup *memcg,int idx) +{ +if (idx <0 || idx >=MEMCG_NR_STAT ) +return (unsigned long )-1 ; + +return memcg_page_state_output (memcg, idx); +}
Should bpf_mem_cgroup_vm_events() have a similar bounds check, perhaps:
if (event >= NR_VM_EVENT_ITEMS)
return (unsigned long)-1;before calling memcg_events()?
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20357445962
Roman Gushchin
maintainerRe: [PATCH bpf-next v1 4/6] mm: introduce BPF kfuncs to access memcg statistics and events
Quoted 27 lines · [email protected] writes:
Quoted 4 lines
mm/bpf_memcontrol.c +0
-0[ ... ]
mm/bpf_memcontrol.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 89
Quoted 12 lines
mm/bpf_memcontrol.c +12
-0+/** +* bpf_mem_cgroup_vm_events - Read memory cgroup's vm event counter +* @memcg: memory cgroup +* @event: event id +* +* Allows to read memory cgroup event counters. +*/ +__bpf_kfunc unsigned long bpf_mem_cgroup_vm_events (struct mem_cgroup *memcg, +enum vm_event_item event) +{ +return memcg_events (memcg, event); +}Should
bpf_mem_cgroup_vm_events()validate the event parameter before passing it tomemcg_events()? The call chain is:
bpf_mem_cgroup_vm_events()->memcg_events()->memcg_events_index()And
memcg_events_index()does:
Quoted 8 lines
return mem_cgroup_events_index[idx];where
mem_cgroup_events_indexis an array of sizeNR_VM_EVENT_ITEMS. If a BPF program passes event >=NR_VM_EVENT_ITEMS, this would be an out-of-bounds array read.The sister function
bpf_mem_cgroup_page_state()below validates its idx parameter before calling its callee:
It's a false positive: the check is enforced by the bpf verifier based on type information.
Quoted 18 lines
Quoted 5 lines
mm/bpf_memcontrol.c +4
-0+/** +* bpf_mem_cgroup_page_state - Read memory cgroup's page state counter +* @memcg: memory cgroup +* @idx: counter idx mm/bpf_memcontrol.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 115
Quoted 10 lines
mm/bpf_memcontrol.c +10
-0+* +* Allows to read memory cgroup statistics. The output is in bytes. +*/ +__bpf_kfunc unsigned long bpf_mem_cgroup_page_state (struct mem_cgroup *memcg,int idx) +{ +if (idx <0 || idx >=MEMCG_NR_STAT ) +return (unsigned long )-1 ; + +return memcg_page_state_output (memcg, idx); +}Should
bpf_mem_cgroup_vm_events()have a similar bounds check, perhaps:
Quoted 4 lines
if (event >= NR_VM_EVENT_ITEMS) return (unsigned long)-1;before calling
memcg_events()?
No. Here is required because idx is a simple int.
Shakeel Butt
maintainerRe: [PATCH bpf-next v1 4/6] mm: introduce BPF kfuncs to access memcg statistics and events
In reply to Roman GushchinQuoted 69 lines · Roman Gushchin
Introduce BPF kfuncs to conveniently access memcg data:
-bpf_mem_cgroup_vm_events(),
-bpf_mem_cgroup_usage(),
-bpf_mem_cgroup_page_state(),
-bpf_mem_cgroup_flush_stats().These functions are useful for implementing BPF OOM policies, but also can be used to accelerate access to the memcg data. Reading it through cgroupfs is much more expensive, roughly 5x, mostly because of the need to convert the data into the text and back.
JP Kobryn:
An experiment was setup to compare the performance of a program that uses the traditional method of reading memory.stat vs a program using the new kfuncs. The control program opens up the root memory.stat file and for 1M iterations reads, converts the string values to numeric data, then seeks back to the beginning. The experimental program sets up the requisite libbpf objects and for 1M iterations invokes a bpf program which uses the kfuncs to fetch all available stats fornode_stat_item,memcg_stat_item, andvm_event_itemtypes.The results showed a significant perf benefit on the experimental side, outperforming the control side by a margin of 93%. In kernel mode, elapsed time was reduced by 80%, while in user mode, over 99% of time was saved.
control: elapsed time
real 0m38.318s user 0m25.131s sys 0m13.070sexperiment: elapsed time
real 0m2.789s user 0m0.187s sys 0m2.512scontrol: perf data
33.43% a.out libc.so.6 [.]__vfscanf_internal
6.88% a.out [kernel.kallsyms] [k] vsnprintf
6.33% a.out libc.so.6 [.]_IO_fgets
5.51% a.out [kernel.kallsyms] [k]format_decode
4.31% a.out libc.so.6 [.]__GI_____strtoull_l_internal
3.78% a.out [kernel.kallsyms] [k] string
3.53% a.out [kernel.kallsyms] [k] number
2.71% a.out libc.so.6 [.]_IO_sputbackc
2.41% a.out [kernel.kallsyms] [k] strlen1.98% a.out a.out [.] main 1.70% a.out libc.so.6 [.] _IO_getline_info 1.51% a.out libc.so.6 [.] __isoc99_sscanf1.47% a.out [kernel.kallsyms] [k]
memory_stat_format1.47% a.out [kernel.kallsyms] [k]memcpy_orig1.41% a.out [kernel.kallsyms] [k]seq_buf_printfexperiment: perf data
10.55% memcgstat bpf_prog_..._query [k]bpf_prog_16aab2f19fa982a7_query6.90% memcgstat [kernel.kallsyms] [k] memcg_page_state_output 3.55% memcgstat [kernel.kallsyms] [k] _raw_spin_lock 3.12% memcgstat [kernel.kallsyms] [k] memcg_events 2.87% memcgstat [kernel.kallsyms] [k] __memcg_slab_post_alloc_hook 2.73% memcgstat [kernel.kallsyms] [k] kmem_cache_free 2.70% memcgstat [kernel.kallsyms] [k] entry_SYSRETQ_unsafe_stack 2.25% memcgstat [kernel.kallsyms] [k] __memcg_slab_free_hook 2.06% memcgstat [kernel.kallsyms] [k] get_page_from_freelistSigned-off-by Roman Gushchin <[email protected]>Co-developed-by JP Kobryn <[email protected]>Signed-off-by JP Kobryn <[email protected]>Acked-by Michal Hocko <[email protected]>---
[...]
mm/bpf_memcontrol.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 99
Quoted 10 lines
mm/bpf_memcontrol.c +10
-0+ +/** +* bpf_mem_cgroup_usage - Read memory cgroup's usage +* @memcg: memory cgroup +* +* Returns current memory cgroup size in bytes. +*/ +__bpf_kfunc unsigned long bpf_mem_cgroup_usage (struct mem_cgroup *memcg) +{ +return page_counter_read (&memcg->memory) *PAGE_SIZE ;
Similar to mem_cgroup_usage() should we special handle root memcg?
Otherwise looks good to me.
Roman Gushchin
maintainerPATCH 5/6 mm: introduce BPF kfunc to access memory events
In reply to Roman GushchinFrom: JP Kobryn <[email protected]>
Introduce BPF kfunc to access memory events, e.g.:MEMCG_LOW, MEMCG_MAX, MEMCG_OOM, MEMCG_OOM_KILL etc.
--- mm/bpf_memcontrol.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
mm/bpf_memcontrol.c +17 -0
@@ -99,6 +99,22 @@ __bpf_kfunc unsigned long bpf_mem_cgroup_usage(struct mem_cgroup *memcg)return page_counter_read (&memcg->memory) *PAGE_SIZE ; } +/** +* bpf_mem_cgroup_memory_events - Read memory cgroup's memory event value +* @memcg: memory cgroup +* @event: memory event id +* +* Returns current memory event count. +*/ +__bpf_kfunc unsigned long bpf_mem_cgroup_memory_events (struct mem_cgroup *memcg, +enum memcg_memory_event event) +{ +if (event >=MEMCG_NR_MEMORY_EVENTS ) +return (unsigned long )-1 ; + +return atomic_long_read (&memcg->memory_events[event]); +} +/** * bpf_mem_cgroup_page_state - Read memory cgroup's page state counter * @memcg: memory cgroup
@@ -133,6 +149,7 @@ BTF_ID_FLAGS(func, bpf_get_mem_cgroup, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NUL BTF_ID_FLAGS(func, bpf_put_mem_cgroup, KF_TRUSTED_ARGS | KF_RELEASE) BTF_ID_FLAGS(func, bpf_mem_cgroup_vm_events, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_mem_cgroup_memory_events, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_mem_cgroup_usage, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_mem_cgroup_page_state, KF_TRUSTED_ARGS)BTF_ID_FLAGS (func, bpf_mem_cgroup_flush_stats,KF_TRUSTED_ARGS |KF_SLEEPABLE )
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH bpf-next v1 5/6] mm: introduce BPF kfunc to access memory events
Could this cause an out-of-bounds access if a BPF program passes a negative value for event? The enum type is signed, so if event is -1, the check "event >= MEMCG_NR_MEMORY_EVENTS" would evaluate to…
Quoted 4 lines
mm/bpf_memcontrol.c +0
-0
[ ... ]
mm/bpf_memcontrol.c __bpf_kfunc unsigned long bpf_mem_cgroup_usage(struct mem_cgroup *memcg) · line 116
Quoted 8 lines
mm/bpf_memcontrol.c +8
-0+__bpf_kfunc unsigned long bpf_mem_cgroup_memory_events (struct mem_cgroup *memcg, +enum memcg_memory_event event) +{ +if (event >=MEMCG_NR_MEMORY_EVENTS ) +return (unsigned long )-1 ; + +return atomic_long_read (&memcg->memory_events[event]); +}
Could this cause an out-of-bounds access if a BPF program passes a negative value for event? The enum type is signed, so if event is -1, the check "event >= MEMCG_NR_MEMORY_EVENTS" would evaluate to false (-1 >= 10 is false), allowing access to memcg->memory_events[-1].
The existing bpf_mem_cgroup_page_state() in the same file uses both lower and upper bound checks:
if (idx < 0 || idx >= MEMCG_NR_STAT)
return (unsigned long)-1;Should bpf_mem_cgroup_memory_events() follow the same pattern with:
if (event < 0 || event >= MEMCG_NR_MEMORY_EVENTS)
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20357445962
Roman Gushchin
maintainerRe: [PATCH bpf-next v1 5/6] mm: introduce BPF kfunc to access memory events
Quoted 24 lines · [email protected] writes:
Quoted 4 lines
mm/bpf_memcontrol.c +0
-0[ ... ]
mm/bpf_memcontrol.c __bpf_kfunc unsigned long bpf_mem_cgroup_usage(struct mem_cgroup *memcg) · line 116
Quoted 8 lines
mm/bpf_memcontrol.c +8
-0+__bpf_kfunc unsigned long bpf_mem_cgroup_memory_events (struct mem_cgroup *memcg, +enum memcg_memory_event event) +{ +if (event >=MEMCG_NR_MEMORY_EVENTS ) +return (unsigned long )-1 ; + +return atomic_long_read (&memcg->memory_events[event]); +}Could this cause an out-of-bounds access if a BPF program passes a negative value for event? The enum type is signed, so if event is -1, the check "event >=
MEMCG_NR_MEMORY_EVENTS" would evaluate to false (-1 >= 10 is false), allowing access to memcg->memory_events[-1].The existing
bpf_mem_cgroup_page_state()in the same file uses both lower and upper bound checks:
Quoted 6 lines
if (idx < 0 || idx >= MEMCG_NR_STAT) return (unsigned long)-1;Should
bpf_mem_cgroup_memory_events()follow the same pattern with:if (event < 0 || event >= MEMCG_NR_MEMORY_EVENTS)
Same here: the check is enforced by the bpf verifier based on the type information. Not a real issue.
Re: [PATCH bpf-next v1 5/6] mm: introduce BPF kfunc to access memory events
In reply to Roman GushchinQuoted 6 lines · Roman Gushchin
From: JP Kobryn <[email protected]>
Introduce BPF kfunc to access memory events, e.g.:
MEMCG_LOW,MEMCG_MAX,MEMCG_OOM,MEMCG_OOM_KILLetc.Signed-off-by JP Kobryn <[email protected]>
Roman, you need to add your signoff as well.
Roman Gushchin
maintainerPATCH 6/6 bpf: selftests: selftests for memcg stat kfuncs
In reply to Roman GushchinFrom: JP Kobryn <[email protected]>
Add test coverage for the kfuncs that fetch memcg stats. Using some common stats, test scenarios ensuring that the given stat increases by some arbitrary amount. The stats selected cover the three categories represented by the enums: node_stat_item, memcg_stat_item, vm_event_item.
Since only a subset of all stats are queried, use a static struct made up of fields for each stat. Write to the struct with the fetched values when the bpf program is invoked and read the fields in the user mode program for verification.
--- .../testing/selftests/bpf/cgroup_iter_memcg.h | 18 ++ .../bpf/prog_tests/cgroup_iter_memcg.c | 223 ++++++++++++++++++ .../selftests/bpf/progs/cgroup_iter_memcg.c | 42 ++++ 3 files changed, 283 insertions(+) create mode 100644 tools/testing/selftests/bpf/cgroup_iter_memcg.h create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c
tools/testing/selftests/bpf/cgroup_iter_memcg.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ +#ifndef __CGROUP_ITER_MEMCG_H +#define __CGROUP_ITER_MEMCG_H + +struct memcg_query { +/* some node_stat_item's */ +unsigned long nr_anon_mapped; +unsigned long nr_shmem; +unsigned long nr_file_pages; +unsigned long nr_file_mapped; +/* some memcg_stat_item */ +unsigned long memcg_kmem; +/* some vm_event_item */ +unsigned long pgfault; +}; + +#endif /* __CGROUP_ITER_MEMCG_H */
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c new file +223 -0
223 lines not on this page · raw message
tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
2.52.0
Shakeel Butt
maintainerRe: [PATCH bpf-next v1 6/6] bpf: selftests: selftests for memcg stat kfuncs
Quoted 7 lines · Roman Gushchin
From: JP Kobryn <[email protected]>
Add test coverage for the kfuncs that fetch memcg stats. Using some common stats, test scenarios ensuring that the given stat increases by some arbitrary amount. The stats selected cover the three categories represented by the enums:
node_stat_item,memcg_stat_item,vm_event_item.
Quoted 6 lines
Since only a subset of all stats are queried, use a static struct made up of fields for each stat. Write to the struct with the fetched values when the bpf program is invoked and read the fields in the user mode program for verification.
Signed-off-by JP Kobryn <[email protected]>
Need your signoff
[...]
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c new file +12
-0+ +#define NR_PIPES 64 +static void test_kmem (struct bpf_link *link,struct memcg_query *memcg_query) +{ +int fds[NR_PIPES ][2 ], i; + +/* +* Increase kmem value by creating pipes which will allocate some +* kernel buffers. +*/ +for (i =0 ; i <NR_PIPES ; i++) { +if (!ASSERT_OK (pipe (fds[i]),"pipe" ))
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c line 149
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c new file +10
-0+ goto cleanup; + } + +if (!ASSERT_OK (read_stats (link),"read stats" )) + goto cleanup; + +ASSERT_GT (memcg_query->memcg_kmem,0 ,"kmem value" ); + +cleanup: +for (i =0 ; i <NR_PIPES ; i++) {
Instead of from 0 to NR_PIPES, we need to go from i-1 to (and equal to) 0 otherwise we can potentially close() junk values.
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c new file +5
-0+close (fds[i][0 ]); +close (fds[i][1 ]); + } +} +
[...]
tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c line 23
Quoted 13 lines
tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c new file +13
-0+ +SEC("iter.s/cgroup") +int cgroup_memcg_query (struct bpf_iter__cgroup *ctx) +{ +struct cgroup *cgrp = ctx->cgroup; +struct cgroup_subsys_state *css; +struct mem_cgroup *memcg; + +if (!cgrp) +return 1 ; + + css = &cgrp->self; +if (!css)
Will css ever be NULL here?
Quoted 22 lines
tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c new file +19
-0+return 1 ; + + memcg =bpf_get_mem_cgroup (css); +if (!memcg) +return 1 ; + +bpf_mem_cgroup_flush_stats (memcg); + + memcg_query.nr_anon_mapped =bpf_mem_cgroup_page_state (memcg,NR_ANON_MAPPED ); + memcg_query.nr_shmem =bpf_mem_cgroup_page_state (memcg,NR_SHMEM ); + memcg_query.nr_file_pages =bpf_mem_cgroup_page_state (memcg,NR_FILE_PAGES ); + memcg_query.nr_file_mapped =bpf_mem_cgroup_page_state (memcg,NR_FILE_MAPPED ); + memcg_query.memcg_kmem =bpf_mem_cgroup_page_state (memcg,MEMCG_KMEM ); + memcg_query.pgfault =bpf_mem_cgroup_vm_events (memcg,PGFAULT ); + +bpf_put_mem_cgroup (memcg); + +return 0 ; +}Signature · 1 line
2.52.0
Roman Gushchin
maintainerRe: [PATCH bpf-next v1 6/6] bpf: selftests: selftests for memcg stat kfuncs
Quoted 8 lines · Shakeel Butt
Quoted 7 lines · Roman Gushchin
From: JP Kobryn <[email protected]>
Add test coverage for the kfuncs that fetch memcg stats. Using some common stats, test scenarios ensuring that the given stat increases by some arbitrary amount. The stats selected cover the three categories represented by the enums:
node_stat_item,memcg_stat_item,vm_event_item.
Quoted 8 lines
Quoted 6 lines
Since only a subset of all stats are queried, use a static struct made up of fields for each stat. Write to the struct with the fetched values when the bpf program is invoked and read the fields in the user mode program for verification.
Signed-off-by JP Kobryn <[email protected]>Need your signoff
Sure, will add, thanks.
Quoted 19 lines
[...]
Quoted 17 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c new file +17
-0+ +#define NR_PIPES 64 +static void test_kmem (struct bpf_link *link,struct memcg_query *memcg_query) +{ +int fds[NR_PIPES ][2 ], i; + +/* +* Increase kmem value by creating pipes which will allocate some +* kernel buffers. +*/ +for (i =0 ; i <NR_PIPES ; i++) { +if (!ASSERT_OK (pipe (fds[i]),"pipe" )) + goto cleanup; + } + +if (!ASSERT_OK (read_stats (link),"read stats" )) + goto cleanup;
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c line 149
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c new file +5
-0+ +ASSERT_GT (memcg_query->memcg_kmem,0 ,"kmem value" ); + +cleanup: +for (i =0 ; i <NR_PIPES ; i++) {Instead of from 0 to
NR_PIPES, we need to go from i-1 to (and equal to) 0 otherwise we can potentiallyclose()junk values.
Good catch, will fix.
Quoted 9 lines
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c new file +5
-0+close (fds[i][0 ]); +close (fds[i][1 ]); + } +} +[...]
Quoted 15 lines
tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c line 23
Quoted 13 lines
tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c new file +13
-0+ +SEC("iter.s/cgroup") +int cgroup_memcg_query (struct bpf_iter__cgroup *ctx) +{ +struct cgroup *cgrp = ctx->cgroup; +struct cgroup_subsys_state *css; +struct mem_cgroup *memcg; + +if (!cgrp) +return 1 ; + + css = &cgrp->self; +if (!css)Will css ever be NULL here?
Hm, I think previously the verifier wasn't smart enough to understand that it's always a valid pointer, but I just tested it with linux-next and it worked well. I'll drop the check.
Thanks!
Roman Gushchin
maintainerRoman Gushchin <[email protected]> writes:
I believe it can go through the bpf tree or the mm tree.
I'd slightly prefer the mm tree, simple because follow up bpf oom patches have more changes on the mm side.
If Alexei, Daniel and Andrii is fine with it, Andrew, can you, please, pick them up?
(bpf oom patches will follow likely next week)
Thank you!
Quoted 31 lines
Introduce kfuncs to simplify the access to the memcg data. These kfuncs can be used to accelerate monitoring use cases and for implementing custom OOM policies once BPF OOM is landed.
This patchset was separated out from the BPF OOM patchset to simplify the logistics and accelerate the landing of the part which is useful by itself. No functional changes since BPF OOM v2.
JP Kobryn (2):
mm: introduce BPF kfunc to access memory events
bpf: selftests: selftests for memcg stat kfuncsRoman Gushchin (4):
mm: declarememcg_page_state_output()inmemcontrol.h
mm: introduce BPF kfuncs to deal with memcg pointers
mm: introducebpf_get_root_mem_cgroup()BPF kfunc
mm: introduce BPF kfuncs to access memcg statistics and eventsinclude/linux/memcontrol.h | 3 + mm/Makefile | 3 + mm/bpf_memcontrol.c | 175 ++++++++++++++ mm/memcontrol-v1.h | 1 - .../testing/selftests/bpf/cgroup_iter_memcg.h | 18 ++ .../bpf/prog_tests/cgroup_iter_memcg.c | 223 ++++++++++++++++++ .../selftests/bpf/progs/cgroup_iter_memcg.c | 42 ++++ 7 files changed, 464 insertions(+), 1 deletion(-) create mode 100644 mm/bpf_memcontrol.c create mode 100644 tools/testing/selftests/bpf/cgroup_iter_memcg.h create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_iter_memcg.c
Quoted 10 lines · Roman Gushchin
Roman Gushchin <[email protected]> writes:
I believe it can go through the bpf tree or the mm tree.
I'd slightly prefer the mm tree, simple because follow up bpf oom patches have more changes on the mm side.
If Alexei, Daniel and Andrii is fine with it, Andrew, can you, please, pick them up?
mm tree has no CI and no ability to test bpf things, so in mm tree it will dead weight while in bpf tree it will be continuously tested.
So I don't really like the idea of anything bpf related to being in some random trees.
Quoted 10 lines · Alexei Starovoitov
Quoted 8 lines · Roman Gushchin
Roman Gushchin <[email protected]> writes:
I believe it can go through the bpf tree or the mm tree.
I'd slightly prefer the mm tree, simple because follow up bpf oom patches have more changes on the mm side.
Quoted 8 lines
Quoted 2 lines
If Alexei, Daniel and Andrii is fine with it, Andrew, can you, please, pick them up?
mm tree has no CI and no ability to test bpf things, so in mm tree it will dead weight while in bpf tree it will be continuously tested.
So I don't really like the idea of anything bpf related to being in some random trees.
Adding to the above...
bpf-oom needs to target bpf-next as well to actually go through CI and AI bot review...
mm tree is not a good idea at all.
Roman Gushchin
maintainerQuoted 9 lines · Alexei Starovoitov
Quoted 8 lines · Roman Gushchin
Roman Gushchin <[email protected]> writes:
I believe it can go through the bpf tree or the mm tree.
I'd slightly prefer the mm tree, simple because follow up bpf oom patches have more changes on the mm side.
Quoted 8 lines
Quoted 2 lines
If Alexei, Daniel and Andrii is fine with it, Andrew, can you, please, pick them up?
mm tree has no CI and no ability to test bpf things, so in mm tree it will dead weight while in bpf tree it will be continuously tested.
So I don't really like the idea of anything bpf related to being in some random trees.
Ok, no problems. I've sent v1 with bpf-next prefix so it already got covered. I'll do the same with v2 and will rely on bpf maintainers to pick it up.
Thanks!