mm: memcontrol: Add BPF hooks for memory controller
Subsystems (11)
v3 cover mm: memcontrol: Add BPF hooks for memory controller
From: Hui Zhu <[email protected]>
eBPF infrastructure provides rich visibility into system performance metrics through various tracepoints and statistics.
This patch series introduces BPF struct_ops for the memory controller. Then the eBPF program can help the system control the memory controller based on system performance metrics, thereby improving the utilization of system memory resources while ensuring memory limits are respected.
The following example illustrates how memcg eBPF can improve memory utilization in some scenarios.
The example running on x86_64 QEMU (10 CPUs, 4GB RAM), using a file in tmpfs on the host as a swap device to reduce I/O impact.
root@ubuntu:~# cat /proc/sys/vm/swappiness
60
This the high priority memcg.
root@ubuntu:~# mkdir /sys/fs/cgroup/high
This the low priority memcg.
root@ubuntu:~# mkdir /sys/fs/cgroup/low
root@ubuntu:~# free
total used free shared buff/cache available
Mem: 4007276 392320 3684940 908 101476 3614956
Swap: 10485756 0 10485756First, The following test uses memory.low to reduce the likelihood of tasks in high-priority memory cgroups being reclaimed.
root@ubuntu:~# echo $((3 * 1024 * 1024 * 1024)) > /sys/fs/cgroup/high/memory.low root@ubuntu:~# cgexec -g memory:low stress-ng --vm 4 --vm-keep \ --vm-bytes $((3 * 1024 * 1024 * 1024)) \ --vm-method all --seed 2025 --metrics -t 60 \ & cgexec -g memory:high stress-ng --vm 4 --vm-keep \ --vm-bytes $((3 * 1024 * 1024 * 1024)) \ --vm-method all --seed 2025 --metrics -t 60 [1] 1176 stress-ng: info: [1177] setting to a 1 min, 0 secs run per stressor stress-ng: info: [1176] setting to a 1 min, 0 secs run per stressor stress-ng: info: [1177] dispatching hogs: 4 vm stress-ng: info: [1176] dispatching hogs: 4 vm stress-ng: metrc: [1177] stressor bogo ops real time usr time sys time bogo ops/s bogo ops/s CPU used per RSS Max stress-ng: metrc: [1177] (secs) (secs) (secs) (real time) (usr+sys time) instance (%) (KB) stress-ng: metrc: [1177] vm 27047770 60.07 217.79 8.87 450289.91 119330.63 94.34 886936 stress-ng: info: [1177] skipped: 0 stress-ng: info: [1177] passed: 4: vm (4) stress-ng: info: [1177] failed: 0 stress-ng: info: [1177] metrics untrustworthy: 0 stress-ng: info: [1177] successful run completed in 1 min, 0.07 secs stress-ng: metrc: [1176] stressor bogo ops real time usr time sys time bogo ops/s bogo ops/s CPU used per RSS Max stress-ng: metrc: [1176] (secs) (secs) (secs) (real time) (usr+sys time) instance (%) (KB) stress-ng: metrc: [1176] vm 679754 60.12 11.82 72.78 11307.18 8034.42 35.18 469884 stress-ng: info: [1176] skipped: 0 stress-ng: info: [1176] passed: 4: vm (4) stress-ng: info: [1176] failed: 0 stress-ng: info: [1176] metrics untrustworthy: 0 stress-ng: info: [1176] successful run completed in 1 min, 0.13 secs [1]+ Done cgexec -g memory:low stress-ng --vm 4 --vm-keep --vm-bytes $((3 * 1024 * 1024 * 1024)) --vm-method all --seed 2025 --metrics -t 60
The following test continues to use memory.low to reduce the likelihood of tasks in high-priority memory cgroups (memcg) being reclaimed. In this scenario, a Python script within the high-priority memcg simulates a low-load task.
As a result, the Python script's performance is not affected by memory reclamation (as it sleeps after allocating memory).
However, the performance of stress-ng is still impacted due to the memory.low setting.
root@ubuntu:~# cgexec -g memory:low stress-ng --vm 4 --vm-keep \ --vm-bytes $((3 * 1024 * 1024 * 1024)) \ --vm-method all --seed 2025 --metrics -t 60 \ & cgexec -g memory:high python3 -c \ "import time; a = bytearray(3*1024*1024*1024); time.sleep(62)" [1] 1196 stress-ng: info: [1196] setting to a 1 min, 0 secs run per stressor stress-ng: info: [1196] dispatching hogs: 4 vm stress-ng: metrc: [1196] stressor bogo ops real time usr time sys time bogo ops/s bogo ops/s CPU used per RSS Max stress-ng: metrc: [1196] (secs) (secs) (secs) (real time) (usr+sys time) instance (%) (KB) stress-ng: metrc: [1196] vm 886893 60.10 17.76 56.61 14756.92 11925.69 30.94 788676 stress-ng: info: [1196] skipped: 0 stress-ng: info: [1196] passed: 4: vm (4) stress-ng: info: [1196] failed: 0 stress-ng: info: [1196] metrics untrustworthy: 0 stress-ng: info: [1196] successful run completed in 1 min, 0.10 secs [1]+ Done cgexec -g memory:low stress-ng --vm 4 --vm-keep --vm-bytes $((3 * 1024 * 1024 * 1024)) --vm-method all --seed 2025 --metrics -t 60 root@ubuntu:~# echo 0 > /sys/fs/cgroup/high/memory.low
Now, we switch to using the memcg eBPF program for memory priority control. memcg is a test program added to samples/bpf in this patch series. It loads memcg.bpf.c into the kernel.memcg.bpf.c monitors PGFAULT events in the high-priority memory cgroup. When the number of events triggered within one second exceeds a predefined threshold, the eBPF hook for the memory cgroup activates its control for one second.
The following command configures the high-priority memory cgroup to return below_min during memory reclamation if the number of PGFAULT events per second exceeds one.
root@ubuntu:~# ./memcg --low_path=/sys/fs/cgroup/low \
--high_path=/sys/fs/cgroup/high \
--threshold=1 --use_below_min
Successfully attached!root@ubuntu:~# cgexec -g memory:low stress-ng --vm 4 --vm-keep \ --vm-bytes $((3 * 1024 * 1024 * 1024)) \ --vm-method all --seed 2025 --metrics -t 60 \ & cgexec -g memory:high stress-ng --vm 4 --vm-keep \ --vm-bytes $((3 * 1024 * 1024 * 1024)) \ --vm-method all --seed 2025 --metrics -t 60 [1] 1220 stress-ng: info: [1220] setting to a 1 min, 0 secs run per stressor stress-ng: info: [1221] setting to a 1 min, 0 secs run per stressor stress-ng: info: [1220] dispatching hogs: 4 vm stress-ng: info: [1221] dispatching hogs: 4 vm stress-ng: metrc: [1221] stressor bogo ops real time usr time sys time bogo ops/s bogo ops/s CPU used per RSS Max stress-ng: metrc: [1221] (secs) (secs) (secs) (real time) (usr+sys time) instance (%) (KB) stress-ng: metrc: [1221] vm 24295240 60.08 221.36 7.64 404392.49 106095.60 95.29 886684 stress-ng: info: [1221] skipped: 0 stress-ng: info: [1221] passed: 4: vm (4) stress-ng: info: [1221] failed: 0 stress-ng: info: [1221] metrics untrustworthy: 0 stress-ng: info: [1221] successful run completed in 1 min, 0.11 secs stress-ng: metrc: [1220] stressor bogo ops real time usr time sys time bogo ops/s bogo ops/s CPU used per RSS Max stress-ng: metrc: [1220] (secs) (secs) (secs) (real time) (usr+sys time) instance (%) (KB) stress-ng: metrc: [1220] vm 685732 60.13 11.69 75.98 11403.88 7822.30 36.45 496496 stress-ng: info: [1220] skipped: 0 stress-ng: info: [1220] passed: 4: vm (4) stress-ng: info: [1220] failed: 0 stress-ng: info: [1220] metrics untrustworthy: 0 stress-ng: info: [1220] successful run completed in 1 min, 0.14 secs [1]+ Done cgexec -g memory:low stress-ng --vm 4 --vm-keep --vm-bytes $((3 * 1024 * 1024 * 1024)) --vm-method all --seed 2025 --metrics -t 60
This test demonstrates that because the Python process within the high-priority memory cgroup is sleeping after memory allocation, no page fault events occur.
As a result, the stress-ng process in the low-priority memory cgroup achieves normal memory performance.
root@ubuntu:~# cgexec -g memory:low stress-ng --vm 4 --vm-keep \ --vm-bytes $((3 * 1024 * 1024 * 1024)) \ --vm-method all --seed 2025 --metrics -t 60 \ & cgexec -g memory:high python3 -c \ "import time; a = bytearray(3*1024*1024*1024); time.sleep(62)" [1] 1238 stress-ng: info: [1238] setting to a 1 min, 0 secs run per stressor stress-ng: info: [1238] dispatching hogs: 4 vm stress-ng: metrc: [1238] stressor bogo ops real time usr time sys time bogo ops/s bogo ops/s CPU used per RSS Max stress-ng: metrc: [1238] (secs) (secs) (secs) (real time) (usr+sys time) instance (%) (KB) stress-ng: metrc: [1238] vm 33107485 60.08 205.41 13.19 551082.91 151448.44 90.97 886064 stress-ng: info: [1238] skipped: 0 stress-ng: info: [1238] passed: 4: vm (4) stress-ng: info: [1238] failed: 0 stress-ng: info: [1238] metrics untrustworthy: 0 stress-ng: info: [1238] successful run completed in 1 min, 0.09 secs
In this patch series, I've incorporated a portion of Roman's patch in
[1] to ensure the entire series can be compiled cleanly with bpf-next. I made some modifications to bpf_struct_ops_link_create
in "bpf: Pass flags in bpf_link_create for struct_ops" and "libbpf: Support passing user-defined flags for struct_ops" to allow the flags parameter to be passed into the kernel.
With this change, patch "mm/bpf: Add BPF_F_ALLOW_OVERRIDE support for memcg_bpf_ops" enables BPF_F_ALLOW_OVERRIDE support for memcg_bpf_ops.
Patch "mm: memcontrol: Add BPF struct_ops for memory controller" introduces BPF struct_ops support to the memory controller, enabling custom and dynamic control over memory pressure. This is achieved through a new struct_ops type, memcg_bpf_ops.
The memcg_bpf_ops struct provides the following hooks:
- get_high_delay_ms: Returns a custom throttling delay in milliseconds for a cgroup that has breached its memory.high limit. This is the primary mechanism for BPF-driven throttling.
- below_low: Overrides the memory.low protection check. If this hook returns true, the cgroup is considered to be protected by its memory.low setting, regardless of its actual usage.
- below_min: Similar to below_low, this overrides the memory.min protection check.
- handle_cgroup_online/offline: Callbacks invoked when a cgroup with an attached program comes online or goes offline, allowing for state management.
Patch "samples/bpf: Add memcg priority control example" introduces the programs memcg.c and memcg.bpf.c that were used in the previous examples.
Changelog:
v3:
According to the comments of Michal Koutný and Chen Ridong, update hooks to get_high_delay_ms, below_low, below_min, handle_cgroup_online and handle_cgroup_offline.
According to the comments of Michal Koutný, add BPF_F_ALLOW_OVERRIDE support to memcg_bpf_ops.
v2:
According to the comments of Tejun Heo, rebased on Roman Gushchin's BPF OOM patch series [1] and added hierarchical delegation support. According to the comments of Roman Gushchin and Michal Hocko, Designed concrete use case scenarios and provided test results.
[1] https://lore.kernel.org/lkml/[email protected]/
Hui Zhu (7):
bpf: Pass flags in bpf_link_create for struct_ops
libbpf: Support passing user-defined flags for struct_ops
mm: memcontrol: Add BPF struct_ops for memory controller selftests/bpf: Add tests for memcg_bpf_ops mm/bpf: Add BPF_F_ALLOW_OVERRIDE support for memcg_bpf_ops selftests/bpf: Add test for memcg_bpf_ops hierarchies samples/bpf: Add memcg priority control example
Roman Gushchin (5):
bpf: move bpf_struct_ops_link into bpf.h
bpf: initial support for attaching struct ops to cgroups
bpf: mark struct oom_control's memcg field as TRUSTED_OR_NULL
mm: define mem_cgroup_get_from_ino() outside of CONFIG_SHRINKER_DEBUG
libbpf: introduce bpf_map__attach_struct_ops_opts()
MAINTAINERS | 4 + include/linux/bpf.h | 8 + include/linux/memcontrol.h | 111 +++- kernel/bpf/bpf_struct_ops.c | 20 +- kernel/bpf/verifier.c | 5 + mm/bpf_memcontrol.c | 274 +++++++- mm/memcontrol.c | 34 +- samples/bpf/.gitignore | 1 + samples/bpf/Makefile | 9 +- samples/bpf/memcg.bpf.c | 129 ++++ samples/bpf/memcg.c | 327 ++++++++++ tools/include/uapi/linux/bpf.h | 2 +- tools/lib/bpf/bpf.c | 8 + tools/lib/bpf/libbpf.c | 19 +- tools/lib/bpf/libbpf.h | 14 + tools/lib/bpf/libbpf.map | 2 +- .../selftests/bpf/prog_tests/memcg_ops.c | 606 ++++++++++++++++++ tools/testing/selftests/bpf/progs/memcg_ops.c | 129 ++++ 18 files changed, 1674 insertions(+), 28 deletions(-) create mode 100644 samples/bpf/memcg.bpf.c create mode 100644 samples/bpf/memcg.c create mode 100644 tools/testing/selftests/bpf/prog_tests/memcg_ops.c create mode 100644 tools/testing/selftests/bpf/progs/memcg_ops.c
Signature · 1 line
2.43.0
RFC PATCH v3 1/12 bpf: move bpf_struct_ops_link into bpf.h
From: Roman Gushchin <[email protected]>
Move struct bpf_struct_ops_link's definition into bpf.h, where other custom bpf links definitions are.
It's necessary to access its members from outside of generic bpf_struct_ops implementation, which will be done by following patches in the series.
--- include/linux/bpf.h | 6 ++++++ kernel/bpf/bpf_struct_ops.c | 6 ------ 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/bpf.h +6 -0
@@ -1881,6 +1881,12 @@ struct bpf_raw_tp_link {u64 cookie; }; +struct bpf_struct_ops_link { +struct bpf_link link; +struct bpf_map __rcu *map; +wait_queue_head_t wait_hup; +}; +struct bpf_link_primer {struct bpf_link *link;struct file *file;
kernel/bpf/bpf_struct_ops.c +0 -6
@@ -55,12 +55,6 @@ struct bpf_struct_ops_map {struct bpf_struct_ops_value kvalue; }; -struct bpf_struct_ops_link { -struct bpf_link link; -struct bpf_map __rcu *map; -wait_queue_head_t wait_hup; -}; -static DEFINE_MUTEX (update_mutex);#define VALUE_PREFIX "bpf_struct_ops_"
Signature · 1 line
2.43.0
RFC PATCH v3 2/12 bpf: initial support for attaching struct ops to cgroups
In reply to Hui ZhuFrom: Roman Gushchin <[email protected]>
When a struct ops is being attached and a bpf link is created, allow to pass a cgroup fd using bpf attr, so that struct ops can be attached to a cgroup instead of globally.
Attached struct ops doesn't hold a reference to the cgroup, only preserves cgroup id.
--- include/linux/bpf.h | 1 + kernel/bpf/bpf_struct_ops.c | 13 +++++++++++++ 2 files changed, 14 insertions(+)
include/linux/bpf.h +1 -0
@@ -1885,6 +1885,7 @@ struct bpf_struct_ops_link {struct bpf_link link;struct bpf_map __rcu *map;wait_queue_head_t wait_hup; +u64 cgroup_id; };struct bpf_link_primer {
kernel/bpf/bpf_struct_ops.c +13 -0
@@ -13,6 +13,7 @@#include <linux/btf_ids.h> #include <linux/rcupdate_wait.h> #include <linux/poll.h> +#include <linux/cgroup.h> struct bpf_struct_ops_value {struct bpf_struct_ops_common_value common;
@@ -1377,6 +1378,18 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) }bpf_link_init (&link->link,BPF_LINK_TYPE_STRUCT_OPS , &bpf_struct_ops_map_lops,NULL , attr->link_create.attach_type); +#ifdef CONFIG_CGROUPS +if (attr->link_create.cgroup.relative_fd) { +struct cgroup *cgrp; + + cgrp =cgroup_get_from_fd (attr->link_create.cgroup.relative_fd); +if (IS_ERR (cgrp)) +return PTR_ERR (cgrp); + + link->cgroup_id =cgroup_id (cgrp); +cgroup_put (cgrp); + } +#endif /* CONFIG_CGROUPS */ err =bpf_link_prime (&link->link, &link_primer);if (err)
Signature · 1 line
2.43.0
RFC PATCH v3 3/12 bpf: mark struct oom_control's memcg field as TRUSTED_OR_NULL
In reply to Hui ZhuFrom: Roman Gushchin <[email protected]>
Struct oom_control is used to describe the OOM context. It's memcg field defines the scope of OOM: it's NULL for global OOMs and a valid memcg pointer for memcg-scoped OOMs. Teach bpf verifier to recognize it as trusted or NULL pointer. It will provide the bpf OOM handler a trusted memcg pointer, which for example is required for iterating the memcg's subtree.
--- kernel/bpf/verifier.c | 5 +++++ 1 file changed, 5 insertions(+)
kernel/bpf/verifier.c +5 -0
@@ -7152,6 +7152,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct vm_area_struct) {struct file *vm_file; }; +BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct oom_control) { +struct mem_cgroup *memcg; +}; +static bool type_is_rcu (struct bpf_verifier_env *env,struct bpf_reg_state *reg,const char *field_name,u32 btf_id)
@@ -7194,6 +7198,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,BTF_TYPE_EMIT (BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket ));BTF_TYPE_EMIT (BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry ));BTF_TYPE_EMIT (BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct vm_area_struct )); +BTF_TYPE_EMIT (BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct oom_control ));return btf_nested_type_is_trusted (&env->log, reg, field_name, btf_id,"__safe_trusted_or_null" );
Signature · 1 line
2.43.0
RFC PATCH v3 4/12 mm: define mem_cgroup_get_from_ino() outside of CONFIG_SHRINKER_DEBUG
In reply to Hui ZhuFrom: Roman Gushchin <[email protected]>
mem_cgroup_get_from_ino() can be reused by the BPF OOM implementation, but currently depends on CONFIG_SHRINKER_DEBUG. Remove this dependency.
--- include/linux/memcontrol.h | 4 ++-- mm/memcontrol.c | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-)
include/linux/memcontrol.h +2 -2
@@ -833,9 +833,9 @@ static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg) {return memcg ?cgroup_ino (memcg->css.cgroup) :0 ; } +#endif struct mem_cgroup *mem_cgroup_get_from_ino (unsigned long ino); -#endif static inline struct mem_cgroup *mem_cgroup_from_seq (struct seq_file *m) {
@@ -1298,12 +1298,12 @@ static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg) {return 0 ; } +#endif static inline struct mem_cgroup *mem_cgroup_get_from_ino (unsigned long ino) {return NULL ; } -#endif static inline struct mem_cgroup *mem_cgroup_from_seq (struct seq_file *m) {
mm/memcontrol.c +0 -2
@@ -3658,7 +3658,6 @@ struct mem_cgroup *mem_cgroup_from_id(unsigned short id)return xa_load (&mem_cgroup_ids, id); } -#ifdef CONFIG_SHRINKER_DEBUG struct mem_cgroup *mem_cgroup_get_from_ino (unsigned long ino) {struct cgroup *cgrp;
@@ -3679,7 +3678,6 @@ struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino)return memcg; } -#endif static void free_mem_cgroup_per_node_info (struct mem_cgroup_per_node *pn) {
Signature · 1 line
2.43.0
RFC PATCH v3 5/12 libbpf: introduce bpf_map__attach_struct_ops_opts()
In reply to Hui ZhuFrom: Roman Gushchin <[email protected]>
Introduce bpf_map__attach_struct_ops_opts(), an extended version of bpf_map__attach_struct_ops(), which takes additional struct bpf_struct_ops_opts argument.
struct bpf_struct_ops_opts has the relative_fd member, which allows to pass an additional file descriptor argument. It can be used to attach struct ops maps to cgroups.
--- tools/lib/bpf/bpf.c | 8 ++++++++ tools/lib/bpf/libbpf.c | 18 ++++++++++++++++-- tools/lib/bpf/libbpf.h | 14 ++++++++++++++ tools/lib/bpf/libbpf.map | 2 +- 4 files changed, 39 insertions(+), 3 deletions(-)
tools/lib/bpf/bpf.c +8 -0
@@ -883,6 +883,14 @@ int bpf_link_create(int prog_fd, int target_fd,if (!OPTS_ZEROED (opts, cgroup))return libbpf_err (-EINVAL );break ; +case BPF_STRUCT_OPS : + relative_fd =OPTS_GET (opts, cgroup.relative_fd,0 ); + attr.link_create.cgroup.relative_fd = relative_fd; + attr.link_create.cgroup.expected_revision = +OPTS_GET (opts, cgroup.expected_revision,0 ); +if (!OPTS_ZEROED (opts, cgroup)) +return libbpf_err (-EINVAL ); +break ;default :if (!OPTS_ZEROED (opts, flags))return libbpf_err (-EINVAL );
tools/lib/bpf/libbpf.c +16 -2
@@ -13459,12 +13459,19 @@ static int bpf_link__detach_struct_ops(struct bpf_link *link)return close (link->fd); } -struct bpf_link *bpf_map__attach_struct_ops (const struct bpf_map *map) +struct bpf_link *bpf_map__attach_struct_ops_opts (const struct bpf_map *map, +const struct bpf_struct_ops_opts *opts) { +DECLARE_LIBBPF_OPTS (bpf_link_create_opts, link_opts);struct bpf_link_struct_ops *link;__u32 zero =0 ;int err, fd; +if (!OPTS_VALID (opts, bpf_struct_ops_opts)) { +pr_warn ("map '%s': invalid opts\n" , map->name); +return libbpf_err_ptr (-EINVAL ); + } +if (!bpf_map__is_struct_ops (map)) {pr_warn ("map '%s': can't attach non-struct_ops map\n" , map->name);return libbpf_err_ptr (-EINVAL );
@@ -13500,7 +13507,9 @@ struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)return &link->link; } - fd =bpf_link_create (map->fd,0 ,BPF_STRUCT_OPS ,NULL ); + link_opts.cgroup.relative_fd =OPTS_GET (opts, relative_fd,0 ); + + fd =bpf_link_create (map->fd,0 ,BPF_STRUCT_OPS , &link_opts);if (fd <0 ) {free (link);return libbpf_err_ptr (fd);
@@ -13512,6 +13521,11 @@ struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)return &link->link; } +struct bpf_link *bpf_map__attach_struct_ops (const struct bpf_map *map) +{ +return bpf_map__attach_struct_ops_opts (map,NULL ); +} +/* * Swap the back struct_ops of a link with a new struct_ops map. */
tools/lib/bpf/libbpf.h +14 -0
@@ -920,6 +920,20 @@ bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,struct bpf_map ; LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map); + +struct bpf_struct_ops_opts { +/* size of this struct, for forward/backward compatibility */ +size_t sz; +__u32 flags; +__u32 relative_fd; +__u64 expected_revision; + size_t :0; +}; +#define bpf_struct_ops_opts__last_field expected_revision + +LIBBPF_API struct bpf_link * +bpf_map__attach_struct_ops_opts (const struct bpf_map *map, +const struct bpf_struct_ops_opts *opts); LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map);struct bpf_iter_attach_opts {
tools/lib/bpf/libbpf.map +1 -1
@@ -453,5 +453,5 @@ LIBBPF_1.7.0 { bpf_map__exclusive_program; bpf_prog_assoc_struct_ops; bpf_program__assoc_struct_ops; - btf__permute; + bpf_map__attach_struct_ops_opts; } LIBBPF_1.6.0;
Signature · 1 line
2.43.0
RFC PATCH v3 6/12 bpf: Pass flags in bpf_link_create for struct_ops
In reply to Hui ZhuFrom: Hui Zhu <[email protected]>
To support features like allowing overrides in cgroup hierarchies, we need a way to pass flags from userspace to the kernel when attaching a struct_ops.
Extend bpf_struct_ops_link to include a flags field. This field is populated from attr->link_create.flags during link creation. This will allow struct_ops implementations, such as the upcoming memory controller ops, to interpret these flags and modify their attachment behavior accordingly.
--- include/linux/bpf.h | 1 + kernel/bpf/bpf_struct_ops.c | 1 + tools/include/uapi/linux/bpf.h | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-)
include/linux/bpf.h +1 -0
@@ -1886,6 +1886,7 @@ struct bpf_struct_ops_link {struct bpf_map __rcu *map;wait_queue_head_t wait_hup;u64 cgroup_id; +u32 flags; };struct bpf_link_primer {
kernel/bpf/bpf_struct_ops.c +1 -0
@@ -1390,6 +1390,7 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) cgroup_put(cgrp); }#endif /* CONFIG_CGROUPS */ + link->flags = attr->link_create.flags; err =bpf_link_prime (&link->link, &link_primer);if (err)
tools/include/uapi/linux/bpf.h +1 -1
@@ -1184,7 +1184,7 @@ enum bpf_perf_event_type { BPF_PERF_EVENT_EVENT = 6, }; -/* cgroup-bpf attach flags used in BPF_PROG_ATTACH command +/* cgroup-bpf attach flags used in BPF_PROG_ATTACH and BPF_LINK_CREATE command * * NONE(default): No further bpf programs allowed in the subtree. *
Signature · 1 line
2.43.0
RFC PATCH v3 7/12 libbpf: Support passing user-defined flags for struct_ops
In reply to Hui ZhuFrom: Hui Zhu <[email protected]>
Building on the previous change that added flags to the kernel's link creation path, this patch exposes this functionality through libbpf.
The bpf_struct_ops_opts struct is extended with a flags member, which is then passed to the bpf_link_create syscall within bpf_map__attach_struct_ops_opts.
This enables userspace applications to pass flags, such as BPF_F_ALLOW_OVERRIDE, when attaching struct_ops to cgroups, providing more control over the attachment behavior in nested hierarchies.
--- tools/lib/bpf/libbpf.c | 1 + 1 file changed, 1 insertion(+)
tools/lib/bpf/libbpf.c +1 -0
@@ -13508,6 +13508,7 @@ struct bpf_link *bpf_map__attach_struct_ops_opts(const struct bpf_map *map, } link_opts.cgroup.relative_fd =OPTS_GET (opts, relative_fd,0 ); + link_opts.flags =OPTS_GET (opts, flags,0 ); fd =bpf_link_create (map->fd,0 ,BPF_STRUCT_OPS , &link_opts);if (fd <0 ) {
Signature · 1 line
2.43.0
RFC PATCH v3 8/12 mm: memcontrol: Add BPF struct_ops for memory controller
In reply to Hui ZhuFrom: Hui Zhu <[email protected]>
Introduce BPF struct_ops support to the memory controller, enabling custom and dynamic control over memory pressure. This is achieved through a new struct_ops type, memcg_bpf_ops.
This new interface allows a BPF program to implement hooks that influence a memory cgroup's behavior. The memcg_bpf_ops struct provides the following hooks:
- get_high_delay_ms: Returns a custom throttling delay in milliseconds for a cgroup that has breached its memory.high limit. This is the primary mechanism for BPF-driven throttling.
- below_low: Overrides the memory.low protection check. If this hook returns true, the cgroup is considered to be protected by its memory.low setting, regardless of its actual usage.
- below_min: Similar to below_low, this overrides the memory.min protection check.
- handle_cgroup_online/offline: Callbacks invoked when a cgroup with an attached program comes online or goes offline, allowing for state management.
This patch integrates these hooks into the core memory control logic. The get_high_delay_ms value is incorporated into charge paths like try_charge_memcg and the high-limit handler __mem_cgroup_handle_over_high. The below_low and below_min hooks are checked within their respective protection functions.
Lifecycle management is handled to ensure BPF programs are correctly inherited by child cgroups and cleaned up on detachment. SRCU is used to protect concurrent access to the memcg->bpf_ops pointer.
--- include/linux/memcontrol.h | 106 ++++++++++++++- mm/bpf_memcontrol.c | 255 ++++++++++++++++++++++++++++++++++++- mm/memcontrol.c | 32 +++-- 3 files changed, 380 insertions(+), 13 deletions(-)
include/linux/memcontrol.h +104 -2
@@ -181,6 +181,37 @@ struct obj_cgroup { }; }; +#ifdef CONFIG_BPF_SYSCALL +/** +* struct memcg_bpf_ops - BPF callbacks for memory cgroup operations +* @handle_cgroup_online: Called when a cgroup comes online +* @handle_cgroup_offline: Called when a cgroup goes offline +* @below_low: Override memory.low protection check. If this callback returns +* true, mem_cgroup_below_low() will return true immediately without +* performing the standard comparison. If it returns false, the +* original memory.low threshold comparison will proceed normally. +* @below_min: Override memory.min protection check. If this callback returns +* true, mem_cgroup_below_min() will return true immediately without +* performing the standard comparison. If it returns false, the +* original memory.min threshold comparison will proceed normally. +* @get_high_delay_ms: Return custom throttle delay in milliseconds +* +* This structure defines the interface for BPF programs to customize +* memory cgroup behavior through struct_ops programs. +*/ +struct memcg_bpf_ops { +void (*handle_cgroup_online)(struct mem_cgroup *memcg); + +void (*handle_cgroup_offline)(struct mem_cgroup *memcg); + +bool (*below_low)(struct mem_cgroup *memcg); + +bool (*below_min)(struct mem_cgroup *memcg); + +unsigned int (*get_high_delay_ms)(struct mem_cgroup *memcg); +}; +#endif /* CONFIG_BPF_SYSCALL */ +/* * The memory controller data structure. The memory controller controls both * page cache and RSS per cgroup. We would eventually like to provide
@@ -321,6 +352,10 @@ struct mem_cgroup {spinlock_t event_list_lock;#endif /* CONFIG_MEMCG_V1 */ +#ifdef CONFIG_BPF_SYSCALL +struct memcg_bpf_ops *bpf_ops; +#endif +struct mem_cgroup_per_node *nodeinfo[]; };
@@ -554,6 +589,66 @@ static inline bool mem_cgroup_disabled(void)return !cgroup_subsys_enabled (memory_cgrp_subsys); } +#ifdef CONFIG_BPF_SYSCALL + +/* SRCU for protecting concurrent access to memcg->bpf_ops */ +extern struct srcu_struct memcg_bpf_srcu; + +/** +* BPF_MEMCG_CALL - Safely invoke a BPF memcg callback +* @memcg: The memory cgroup +* @op: The operation name (struct member) +* @default_val: Default return value if no BPF program attached +* +* This macro safely calls a BPF callback under SRCU protection. +*/ +#define BPF_MEMCG_CALL (memcg, op, default_val) ({ \ + typeof(default_val) __ret = (default_val); \ + struct memcg_bpf_ops *__ops; \ + int __idx; \ + \ + __idx = srcu_read_lock(&memcg_bpf_srcu); \ + __ops = READ_ONCE((memcg)->bpf_ops); \ + if (__ops && __ops->op) \ + __ret = __ops->op(memcg); \ + srcu_read_unlock(&memcg_bpf_srcu, __idx); \ + __ret; \ +}) + +static inline bool bpf_memcg_below_low (struct mem_cgroup *memcg) +{ +return BPF_MEMCG_CALL (memcg, below_low, false); +} + +static inline bool bpf_memcg_below_min (struct mem_cgroup *memcg) +{ +return BPF_MEMCG_CALL (memcg, below_min, false); +} + +static inline unsigned long bpf_memcg_get_high_delay (struct mem_cgroup *memcg) +{ +unsigned int ret; + + ret =BPF_MEMCG_CALL (memcg, get_high_delay_ms,0U ); +return msecs_to_jiffies (ret); +} + +#undef BPF_MEMCG_CALL + +extern void memcontrol_bpf_online (struct mem_cgroup *memcg); +extern void memcontrol_bpf_offline (struct mem_cgroup *memcg); + +#else /* CONFIG_BPF_SYSCALL */ + +static inline unsigned long +bpf_memcg_get_high_delay(struct mem_cgroup *memcg) { return 0 } +static inline bpf_memcg_below_low(struct mem_cgroup *memcg) { return false } +static inline bpf_memcg_below_min(struct mem_cgroup *memcg) { return false } +static inline void memcontrol_bpf_online (struct mem_cgroup *memcg) { } +static inline void memcontrol_bpf_offline (struct mem_cgroup *memcg) { } + +#endif /* CONFIG_BPF_SYSCALL */ + static inline void mem_cgroup_protection(struct mem_cgroup *root, struct mem_cgroup *memcg, unsigned long *min,
@@ -625,6 +720,9 @@ static inline bool mem_cgroup_below_low(struct mem_cgroup *target,if (mem_cgroup_unprotected (target, memcg))return false; +if (bpf_memcg_below_low (memcg)) +return true; +return READ_ONCE (memcg->memory.elow) >=page_counter_read (&memcg->memory); }
@@ -635,6 +733,9 @@ static inline bool mem_cgroup_below_min(struct mem_cgroup *target,if (mem_cgroup_unprotected (target, memcg))return false; +if (bpf_memcg_below_min (memcg)) +return true; +return READ_ONCE (memcg->memory.emin) >=page_counter_read (&memcg->memory); }
@@ -909,12 +1010,13 @@ unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,return READ_ONCE (mz->lru_zone_size[zone_idx][lru]); } -void __mem_cgroup_handle_over_high (gfp_t gfp_mask); +void __mem_cgroup_handle_over_high (gfp_t gfp_mask, +unsigned long bpf_high_delay);static inline void mem_cgroup_handle_over_high (gfp_t gfp_mask) {if (unlikely (current->memcg_nr_pages_over_high)) -__mem_cgroup_handle_over_high (gfp_mask); +__mem_cgroup_handle_over_high (gfp_mask,0 ); }unsigned long mem_cgroup_get_max (struct mem_cgroup *memcg);
mm/bpf_memcontrol.c +253 -2
274 lines not on this page · raw message
mm/memcontrol.c +23 -9
@@ -2252,7 +2252,8 @@ static unsigned long calculate_high_delay(struct mem_cgroup *memcg,* try_charge() (context permitting), as well as from the userland * return path where reclaim is always able to block. */ -void __mem_cgroup_handle_over_high (gfp_t gfp_mask) +void +__mem_cgroup_handle_over_high (gfp_t gfp_mask,unsigned long bpf_high_delay) {unsigned long penalty_jiffies;unsigned long pflags;
@@ -2294,11 +2295,15 @@ void __mem_cgroup_handle_over_high(gfp_t gfp_mask)* memory.high is breached and reclaim is unable to keep up. Throttle * allocators proactively to slow down excessive growth. */ - penalty_jiffies =calculate_high_delay (memcg, nr_pages, -mem_find_max_overage (memcg)); +if (nr_pages) { + penalty_jiffies =calculate_high_delay ( + memcg, nr_pages,mem_find_max_overage (memcg)); - penalty_jiffies +=calculate_high_delay (memcg, nr_pages, -swap_find_max_overage (memcg)); + penalty_jiffies +=calculate_high_delay ( + memcg, nr_pages,swap_find_max_overage (memcg)); + }else + penalty_jiffies =0 ; + penalty_jiffies =max (penalty_jiffies, bpf_high_delay);/* * Clamp the max delay per usermode return so as to still keep the
@@ -2356,6 +2361,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,bool raised_max_event = false;unsigned long pflags;bool allow_spinning =gfpflags_allow_spinning (gfp_mask); +struct mem_cgroup *orig_memcg; retry:if (consume_stock (memcg, nr_pages))
@@ -2481,6 +2487,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,if (batch > nr_pages)refill_stock (memcg, batch - nr_pages); + orig_memcg = memcg;/* * If the hierarchy is above the normal consumption range, schedule * reclaim on returning to userland. We can perform reclaim here
@@ -2530,10 +2537,14 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,* kernel. If this is successful, the return path will see it * when it rechecks the overage and simply bail out. */ -if (current->memcg_nr_pages_over_high >MEMCG_CHARGE_BATCH && - !(current->flags &PF_MEMALLOC ) && -gfpflags_allow_blocking (gfp_mask)) -__mem_cgroup_handle_over_high (gfp_mask); +if (gfpflags_allow_blocking (gfp_mask)) { +unsigned long bpf_high_delay; + + bpf_high_delay =bpf_memcg_get_high_delay (orig_memcg); +if (bpf_high_delay || + current->memcg_nr_pages_over_high >MEMCG_CHARGE_BATCH ) +__mem_cgroup_handle_over_high (gfp_mask, bpf_high_delay); + }return 0 ; }
@@ -3906,6 +3917,8 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)*/ xa_store (&mem_cgroup_ids, memcg->id.id, memcg,GFP_KERNEL ); +memcontrol_bpf_online (memcg); +return 0 ; offline_kmem:memcg_offline_kmem (memcg);
@@ -3925,6 +3938,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)zswap_memcg_offline_cleanup (memcg); +memcontrol_bpf_offline (memcg);memcg_offline_kmem (memcg);reparent_deferred_split_queue (memcg);reparent_shrinker_deferred (memcg);
Signature · 1 line
2.43.0
RFC PATCH v3 9/12 selftests/bpf: Add tests for memcg_bpf_ops
In reply to Hui ZhuFrom: Hui Zhu <[email protected]>
Add a comprehensive selftest suite for the memcg_bpf_ops functionality. These tests validate that BPF programs can correctly influence memory cgroup throttling behavior by implementing the new hooks.
The test suite is added in prog_tests/memcg_ops.c and covers several key scenarios:
1. test_memcg_ops_over_high:
Verifies that a BPF program can trigger throttling on a low-priority cgroup by returning a delay from the get_high_delay_ms hook when a high-priority cgroup is under pressure.
2. test_memcg_ops_below_low_over_high:
Tests the combination of the below_low and get_high_delay_ms hooks, ensuring they work together as expected.
3. test_memcg_ops_below_min_over_high:
Validates the interaction between the below_min and get_high_delay_ms hooks.
The test framework sets up a cgroup hierarchy with high and low priority groups, attaches BPF programs, runs memory-intensive workloads, and asserts that the observed throttling (measured by workload execution time) matches expectations.
The BPF program (progs/memcg_ops.c) uses a tracepoint on memcg:count_memcg_events (specifically PGFAULT) to detect memory pressure and trigger the appropriate hooks in response. This test suite provides essential validation for the new memory control mechanisms.
--- MAINTAINERS | 2 + .../selftests/bpf/prog_tests/memcg_ops.c | 537 ++++++++++++++++++ tools/testing/selftests/bpf/progs/memcg_ops.c | 129 +++++ 3 files changed, 668 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/memcg_ops.c create mode 100644 tools/testing/selftests/bpf/progs/memcg_ops.c
MAINTAINERS +2 -0
@@ -6471,6 +6471,8 @@ F: mm/memcontrol-v1.h F: mm/page_counter.c F: mm/swap_cgroup.c F: samples/cgroup/* +F: tools/testing/selftests/bpf/prog_tests/memcg_ops.c +F: tools/testing/selftests/bpf/progs/memcg_ops.c F: tools/testing/selftests/cgroup/memcg_protection.m F: tools/testing/selftests/cgroup/test_hugetlb_memcg.c F: tools/testing/selftests/cgroup/test_kmem.c
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +537 -0
537 lines not on this page · raw message
tools/testing/selftests/bpf/progs/memcg_ops.c new file +129 -0
129 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v3 10/12 mm/bpf: Add BPF_F_ALLOW_OVERRIDE support for memcg_bpf_ops
In reply to Hui ZhuFrom: Hui Zhu <[email protected]>
To allow for more flexible attachment policies in nested cgroup hierarchies, this patch introduces support for the BPF_F_ALLOW_OVERRIDE flag for memcg_bpf_ops.
When a memcg_bpf_ops is attached to a cgroup with this flag, it permits child cgroups to attach their own, different memcg_bpf_ops, overriding the parent's inherited program. Without this flag, attaching a BPF program to a cgroup that already has one (either directly or via inheritance) will fail.
The implementation involves:
- Adding a bpf_ops_flags field to struct mem_cgroup.
- During registration (bpf_memcg_ops_reg), checking for existing programs and the BPF_F_ALLOW_OVERRIDE flag.
- During unregistration (bpf_memcg_ops_unreg), correctly restoring the parent's BPF program to the cgroup hierarchy.
- Ensuring flags are inherited by child cgroups during online events.
This change enables complex, multi-level policy enforcement where different subtrees of the cgroup hierarchy can have distinct memory management BPF programs.
--- include/linux/memcontrol.h | 1 + mm/bpf_memcontrol.c | 77 ++++++++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 29 deletions(-)
include/linux/memcontrol.h +1 -0
@@ -354,6 +354,7 @@ struct mem_cgroup {#ifdef CONFIG_BPF_SYSCALL struct memcg_bpf_ops *bpf_ops; +u32 bpf_ops_flags;#endif struct mem_cgroup_per_node *nodeinfo[];
mm/bpf_memcontrol.c +48 -29
117 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v3 11/12 selftests/bpf: Add test for memcg_bpf_ops hierarchies
In reply to Hui ZhuFrom: Hui Zhu <[email protected]>
Add a new selftest, test_memcg_ops_hierarchies, to validate the behavior of attaching memcg_bpf_ops in a nested cgroup hierarchy, specifically testing the BPF_F_ALLOW_OVERRIDE flag.
The test case performs the following steps:
1. Creates a three-level deep cgroup hierarchy: /cg, /cg/cg, and /cg/cg/cg.
2. Attaches a BPF struct_ops to the top-level cgroup (/cg) with the BPF_F_ALLOW_OVERRIDE flag.
3. Successfully attaches a new struct_ops to the middle cgroup (/cg/cg) without the flag, overriding the inherited one.
4. Asserts that attaching another struct_ops to the deepest cgroup (/cg/cg/cg) fails with -EBUSY, because its parent did not specify BPF_F_ALLOW_OVERRIDE.
This test ensures that the attachment logic correctly enforces the override rules across a cgroup subtree.
--- .../selftests/bpf/prog_tests/memcg_ops.c | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+)
tools/testing/selftests/bpf/prog_tests/memcg_ops.c +69 -0
@@ -535,3 +535,72 @@ void test_memcg_ops_below_min_over_high(void)close (low_cgroup_fd);cleanup_cgroup_environment (); } + +void test_memcg_ops_hierarchies (void ) +{ +int ret, first =-1 , second =-1 , third =-1 ; +struct memcg_ops *skel; +struct bpf_map *map; +struct bpf_link *link1 =NULL , *link2 =NULL , *link3 =NULL ; +DECLARE_LIBBPF_OPTS (bpf_struct_ops_opts, opts); + + ret =setup_cgroup_environment (); +if (!ASSERT_OK (ret,"setup_cgroup_environment" )) + goto cleanup; + + first =create_and_get_cgroup ("/cg" ); +if (!ASSERT_GE (first,0 ,"create_and_get_cgroup /cg" )) + goto cleanup; + ret =enable_controllers ("/cg" ,"memory" ); +if (!ASSERT_OK (ret,"enable_controllers" )) + goto cleanup; + + second =create_and_get_cgroup ("/cg/cg" ); +if (!ASSERT_GE (second,0 ,"create_and_get_cgroup /cg/cg" )) + goto cleanup; + ret =enable_controllers ("/cg/cg" ,"memory" ); +if (!ASSERT_OK (ret,"enable_controllers" )) + goto cleanup; + + third =create_and_get_cgroup ("/cg/cg/cg" ); +if (!ASSERT_GE (third,0 ,"create_and_get_cgroup /cg/cg/cg" )) + goto cleanup; + ret =enable_controllers ("/cg/cg/cg" ,"memory" ); +if (!ASSERT_OK (ret,"enable_controllers" )) + goto cleanup; + + skel =memcg_ops__open_and_load (); +if (!ASSERT_OK_PTR (skel,"memcg_ops__open_and_load" )) + goto cleanup; + + map =bpf_object__find_map_by_name (skel->obj,"low_mcg_ops" ); +if (!ASSERT_OK_PTR (map,"bpf_object__find_map_by_name low_mcg_ops" )) + goto cleanup; + + opts.relative_fd = first; + opts.flags =BPF_F_ALLOW_OVERRIDE ; + link1 =bpf_map__attach_struct_ops_opts (map, &opts); +if (!ASSERT_OK_PTR (link1,"bpf_map__attach_struct_ops_opts" )) + goto cleanup; + + opts.relative_fd = second; + opts.flags =0 ; + link2 =bpf_map__attach_struct_ops_opts (map, &opts); +if (!ASSERT_OK_PTR (link2,"bpf_map__attach_struct_ops_opts" )) + goto cleanup; + + opts.relative_fd = third; + opts.flags =0 ; + link2 =bpf_map__attach_struct_ops_opts (map, &opts); +if (!ASSERT_ERR_PTR (link3,"bpf_map__attach_struct_ops_opts" )) + goto cleanup; + +cleanup: +bpf_link__destroy (link1); +bpf_link__destroy (link2); +memcg_ops__detach (skel); +close (first); +close (second); +close (third); +cleanup_cgroup_environment (); +}
Signature · 1 line
2.43.0
RFC PATCH v3 12/12 samples/bpf: Add memcg priority control example
In reply to Hui ZhuFrom: Hui Zhu <[email protected]>
Add a sample program to demonstrate a practical use case for the memcg_bpf_ops feature: priority-based memory throttling.
The sample consists of a BPF program and a userspace loader:
1. memcg.bpf.c: A BPF program that monitors PGFAULT events on a high-priority cgroup. When activity exceeds a threshold, it uses the get_high_delay_ms, below_low, or below_min hooks to apply pressure on a low-priority cgroup.
2. memcg.c: A userspace loader that configures and attaches the BPF program. It takes command-line arguments for the high and low priority cgroup paths, a pressure threshold, and the desired throttling delay (over_high_ms).
This provides a clear, working example of how to implement a dynamic, priority-aware memory management policy. A user can create two cgroups, run workloads of different priorities, and observe the low-priority workload being throttled to protect the high-priority one.
Example usage:
# ./memcg --low_path /sys/fs/cgroup/low \ # --high_path /sys/fs/cgroup/high \ # --threshold 100 --over_high_ms 1024
--- MAINTAINERS | 2 + samples/bpf/.gitignore | 1 + samples/bpf/Makefile | 9 +- samples/bpf/memcg.bpf.c | 129 ++++++++++++++++ samples/bpf/memcg.c | 327 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 467 insertions(+), 1 deletion(-) create mode 100644 samples/bpf/memcg.bpf.c create mode 100644 samples/bpf/memcg.c
MAINTAINERS +2 -0
@@ -6470,6 +6470,8 @@ F: mm/memcontrol-v1.c F: mm/memcontrol-v1.h F: mm/page_counter.c F: mm/swap_cgroup.c +F: samples/bpf/memcg.bpf.c +F: samples/bpf/memcg.c F: samples/cgroup/* F: tools/testing/selftests/bpf/prog_tests/memcg_ops.c F: tools/testing/selftests/bpf/progs/memcg_ops.c
samples/bpf/.gitignore +1 -0
@@ -49,3 +49,4 @@ iperf.* /vmlinux.h /bpftool/ /libbpf/ +memcg
samples/bpf/Makefile +8 -1
@@ -37,6 +37,7 @@ tprogs-y += xdp_fwd tprogs-y += task_fd_query tprogs-y += ibumad tprogs-y += hbm +tprogs-y += memcg # Libbpf dependencies LIBBPF_SRC = $(TOOLS_PATH)/lib/bpf
@@ -122,6 +123,7 @@ always-y += task_fd_query_kern.o always-y += ibumad_kern.o always-y += hbm_out_kern.o always-y += hbm_edt_kern.o +always-y += memcg.bpf.o COMMON_CFLAGS = $(TPROGS_USER_CFLAGS) TPROGS_LDFLAGS = $(TPROGS_USER_LDFLAGS)
@@ -289,6 +291,8 @@ $(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h $(obj)/hbm.o: $(src)/hbm.h $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h +memcg: $(obj)/memcg.skel.h + # Override includes for xdp_sample_user.o because $(srctree)/usr/include in # TPROGS_CFLAGS causes conflicts XDP_SAMPLE_CFLAGS += -Wall -O2 \
@@ -347,11 +351,13 @@ $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/x -I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \ -c $(filter %.bpf.c,$^) -o $@ -LINKED_SKELS := xdp_router_ipv4.skel.h +LINKED_SKELS := xdp_router_ipv4.skel.h memcg.skel.h clean-files += $(LINKED_SKELS) xdp_router_ipv4.skel.h-deps := xdp_router_ipv4.bpf.o xdp_sample.bpf.o +memcg.skel.h-deps := memcg.bpf.o + LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps))) BPF_SRCS_LINKED := $(notdir $(wildcard $(src)/*.bpf.c))
@@ -360,6 +366,7 @@ BPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS)) $(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL) @echo " BPF GEN-OBJ " $(@:.skel.h=) + echo $(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps)) $(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps)) @echo " BPF GEN-SKEL" $(@:.skel.h=) $(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@
samples/bpf/memcg.bpf.c new file +129 -0
129 lines not on this page · raw message
samples/bpf/memcg.c new file +327 -0
327 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [RFC PATCH bpf-next v3 11/12] selftests/bpf: Add test for memcg_bpf_ops hierarchies
In reply to Hui ZhuThe result of the third bpf_map__attach_struct_ops_opts() call is stored in link2, but the assertion checks link3. Since link3 is initialized to NULL and never modified, the ASSERT_ERR_PTR check…
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c +0
-0
[ ... ]
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c +6
-0+void test_memcg_ops_hierarchies (void ) +{ +int ret, first =-1 , second =-1 , third =-1 ; +struct memcg_ops *skel; +struct bpf_map *map; +struct bpf_link *link1 =NULL , *link2 =NULL , *link3 =NULL ;
[ ... ]
tools/testing/selftests/bpf/prog_tests/memcg_ops.c void test_memcg_ops_below_min_over_high(void) · line 595
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c +10
-0+ opts.relative_fd = second; + opts.flags =0 ; + link2 =bpf_map__attach_struct_ops_opts (map, &opts); +if (!ASSERT_OK_PTR (link2,"bpf_map__attach_struct_ops_opts" )) + goto cleanup; + + opts.relative_fd = third; + opts.flags =0 ; + link2 =bpf_map__attach_struct_ops_opts (map, &opts); +if (!ASSERT_ERR_PTR (link3,"bpf_map__attach_struct_ops_opts" ))
^^^^^
The result of the third bpf_map__attach_struct_ops_opts() call is stored in link2, but the assertion checks link3. Since link3 is initialized to NULL and never modified, the ASSERT_ERR_PTR check does not validate the actual return value of the third attachment.
Should this be assigning to link3 instead of link2?
tools/testing/selftests/bpf/prog_tests/memcg_ops.c void test_memcg_ops_below_min_over_high(void) · line 600
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c +5
-0+ goto cleanup; + +cleanup: +bpf_link__destroy (link1); +bpf_link__destroy (link2);
If the fix is to use link3 for the third attachment result, should bpf_link__destroy(link3) be added here as well?
tools/testing/selftests/bpf/prog_tests/memcg_ops.c void test_memcg_ops_below_min_over_high(void) · line 606
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c +6
-0+memcg_ops__detach (skel); +close (first); +close (second); +close (third); +cleanup_cgroup_environment (); +}
---
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/21280790825
[email protected]
AI reviewRe: [RFC PATCH bpf-next v3 12/12] samples/bpf: Add memcg priority control example
In reply to Hui ZhuWas this debug echo intended to be left in? The line without the @ prefix will print the unexpanded command during the build, which looks like leftover debug output.
Quoted 2 lines
samples/bpf/Makefile +0
-0
[ ... ]
samples/bpf/Makefile BPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS)) · line 370
Quoted 6 lines
samples/bpf/Makefile +1
-0@@ -360,6 +366,7 @@ BPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS)) $(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL) @echo " BPF GEN-OBJ " $(@:.skel.h=) + echo $(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps)) $(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps))
Was this debug echo intended to be left in? The line without the @ prefix will print the unexpanded command during the build, which looks like leftover debug output.
Quoted 5 lines
samples/bpf/memcg.c new file +0
-0
[ ... ]
samples/bpf/memcg.c line 192
Quoted 8 lines
samples/bpf/memcg.c new file +8
-0+ high_cgroup_id =get_cgroup_id (high_path); +if (!high_cgroup_id) + goto out; + high_cgroup_fd =open (high_path,O_RDONLY ); +if (high_cgroup_fd <0 ) { +fprintf (stderr, +"ERROR: open high cgroup '%s' failed: %d\n" , + low_path, errno);
^^^^^^^^
Should this be high_path instead of low_path? The error message says "open high cgroup" but prints low_path.
[ ... ]
Quoted 1 line
samples/bpf/memcg.c new file +1
-0+struct bpf_link *link =NULL , *link_low =NULL , *link_high =NULL ;
[ ... ]
Quoted 1 line
samples/bpf/memcg.c new file +1
-0+if (over_high_ms) {
[ ... ]
Quoted 1 line
samples/bpf/memcg.c new file +1
-0+ link_low =bpf_map__attach_struct_ops_opts (map, &opts);
[ ... ]
Quoted 2 lines
samples/bpf/memcg.c new file +2
-0+if (use_below_low || use_below_min) { + map =bpf_object__find_map_by_name (obj,"high_mcg_ops" );
[ ... ]
samples/bpf/memcg.c
Quoted 1 line
samples/bpf/memcg.c new file +1
-0+ link_low =bpf_map__attach_struct_ops_opts (map, &opts);
^^^^^^^^
This assigns to link_low again, overwriting the previous link if both over_high_ms and (use_below_low || use_below_min) are set. Should this be link_high instead? The link_high variable is declared but never assigned, and the first bpf_link would be leaked in this scenario.
[ ... ]
samples/bpf/memcg.c line 322
Quoted 4 lines
samples/bpf/memcg.c new file +4
-0+out: +bpf_link__destroy (link); +bpf_link__destroy (link_low); +bpf_link__destroy (link_high);
Here link_high is always NULL since it was never assigned above.
---
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/21280790825
[email protected]
AI reviewRe: [RFC PATCH bpf-next v3 05/12] libbpf: introduce bpf_map__attach_struct_ops_opts()
In reply to Hui ZhuWas the removal of btf__permute from the symbol map intentional? The function is still implemented in btf.c and declared with LIBBPF_API in btf.h.
tools/lib/bpf/libbpf.map LIBBPF_1.7.0 { · line 457
Quoted 11 lines
tools/lib/bpf/libbpf.map +1
-1@@ -453,5 +453,5 @@ LIBBPF_1.7.0 { bpf_map__exclusive_program; bpf_prog_assoc_struct_ops; bpf_program__assoc_struct_ops; - btf__permute; + bpf_map__attach_struct_ops_opts; } LIBBPF_1.6.0;
Was the removal of btf__permute from the symbol map intentional? The function is still implemented in btf.c and declared with LIBBPF_API in btf.h.
It looks like the line was replaced instead of having a new line added. This would break applications using btf__permute() with link-time or runtime errors.
Perhaps this should be:
bpf_program__assoc_struct_ops;
bpf_map__attach_struct_ops_opts;
btf__permute;
---
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/21280790825
[email protected]
AI reviewRe: [RFC PATCH bpf-next v3 02/12] bpf: initial support for attaching struct ops to cgroups
In reply to Hui ZhuWhen cgroup_get_from_fd() fails, this returns directly without cleaning up. At this point in bpf_struct_ops_link_create(), link has already been allocated via kzalloc() and map has a reference from…
Quoted 2 lines
kernel/bpf/bpf_struct_ops.c +0
-0
[ ... ]
kernel/bpf/bpf_struct_ops.c int bpf_struct_ops_link_create(union bpf_attr *attr) · line 1387
Quoted 11 lines
kernel/bpf/bpf_struct_ops.c +7
-0@@ -1377,6 +1378,18 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) }bpf_link_init (&link->link,BPF_LINK_TYPE_STRUCT_OPS , &bpf_struct_ops_map_lops,NULL , attr->link_create.attach_type); +#ifdef CONFIG_CGROUPS +if (attr->link_create.cgroup.relative_fd) { +struct cgroup *cgrp; + + cgrp =cgroup_get_from_fd (attr->link_create.cgroup.relative_fd); +if (IS_ERR (cgrp)) +return PTR_ERR (cgrp);
^^^^^^^^^^^^^^^^^
When cgroup_get_from_fd() fails, this returns directly without cleaning up. At this point in bpf_struct_ops_link_create(), link has already been allocated via kzalloc() and map has a reference from bpf_map_get().
Should this use goto err_out instead? The err_out label calls bpf_map_put(map) and kfree(link), which would be needed here but are bypassed by the direct return.
kernel/bpf/bpf_struct_ops.c int bpf_struct_ops_link_create(union bpf_attr *attr) · line 1395
Quoted 8 lines
kernel/bpf/bpf_struct_ops.c +5
-0+ + link->cgroup_id =cgroup_id (cgrp); + cgroup_put(cgrp); + } +#endif /* CONFIG_CGROUPS */ err =bpf_link_prime (&link->link, &link_primer);if (err)
---
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/21280790825
[email protected]
AI reviewRe: [RFC PATCH bpf-next v3 09/12] selftests/bpf: Add tests for memcg_bpf_ops
In reply to Hui ZhuShould this check pid2 instead of pid1? After assigning pid2 = fork(), checking pid1 means a failed second fork() would go undetected if the first fork() succeeded.
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +0
-0
[ ... ]
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +9
-0+static void real_test_memcg_ops (int read_times) +{ +int ret; +char data_file1[] ="/tmp/test_data_XXXXXX" ; +char data_file2[] ="/tmp/test_data_XXXXXX" ; +char time_file1[] ="/tmp/test_time_XXXXXX" ; +char time_file2[] ="/tmp/test_time_XXXXXX" ; +pid_t pid1, pid2; +double time1, time2;
[ ... ]
tools/testing/selftests/bpf/prog_tests/memcg_ops.c line 278
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +11
-0+ pid1 =fork (); +if (!ASSERT_GE (pid1,0 ,"fork" )) + goto cleanup; +if (pid1 ==0 ) +real_test_memcg_ops_child_work (CG_LOW_DIR , + data_file1, + time_file1, + read_times); + + pid2 =fork (); +if (!ASSERT_GE (pid1,0 ,"fork" ))
^^^^
Should this check pid2 instead of pid1? After assigning pid2 = fork(), checking pid1 means a failed second fork() would go undetected if the first fork() succeeded.
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +6
-0+ goto cleanup; +if (pid2 ==0 ) +real_test_memcg_ops_child_work (CG_HIGH_DIR , + data_file2, + time_file2, + read_times);
[ ... ]
tools/testing/selftests/bpf/prog_tests/memcg_ops.c line 317
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +4
-0+void test_memcg_ops_over_high (void ) +{ +int err, map_fd; +struct memcg_ops *skel;
^^^^^^^^^^^^^^^
The skel variable is not initialized to NULL here. If setup_cgroup() fails early and jumps to the out label, memcg_ops__detach(skel) would be called with an uninitialized pointer.
Also, the cleanup path calls memcg_ops__detach() but never calls memcg_ops__destroy(). Would this leak the skeleton object and its associated BPF resources?
Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +14
-0+struct bpf_map *map; +size_t bss_sz; +struct memcg_ops__bss *bss_data; +__u32 key =0 ; +struct bpf_program *prog =NULL ; +struct bpf_link *link =NULL , *link2 =NULL ; +DECLARE_LIBBPF_OPTS (bpf_struct_ops_opts, opts); +int high_cgroup_id, low_cgroup_fd =-1 ; + + err =setup_cgroup (&high_cgroup_id, &low_cgroup_fd,NULL ); +if (!ASSERT_OK (err,"setup_cgroup" )) + goto out; + + skel =memcg_ops__open_and_load ();
[ ... ]
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +7
-0+out: +bpf_link__destroy (link); +bpf_link__destroy (link2); +memcg_ops__detach (skel); +close (low_cgroup_fd); +cleanup_cgroup_environment (); +}
[ ... ]
tools/testing/selftests/bpf/prog_tests/memcg_ops.c line 386
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +4
-0+void test_memcg_ops_below_low_over_high (void ) +{ +int err, map_fd; +struct memcg_ops *skel;
Same issue here - skel is not initialized to NULL.
[ ... ]
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +5
-0+out: +bpf_link__destroy (link); +bpf_link__destroy (link_high); +bpf_link__destroy (link_low); +memcg_ops__detach (skel);
[ ... ]
tools/testing/selftests/bpf/prog_tests/memcg_ops.c line 464
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +4
-0+void test_memcg_ops_below_min_over_high (void ) +{ +int err, map_fd; +struct memcg_ops *skel;
Same issue here as well - skel is not initialized to NULL.
[ ... ]
tools/testing/selftests/bpf/prog_tests/memcg_ops.c line 459
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +9
-0+out: +bpf_link__destroy (link); +bpf_link__destroy (link_high); +bpf_link__destroy (link_low); +memcg_ops__detach (skel); +close (high_cgroup_fd); +close (low_cgroup_fd); +cleanup_cgroup_environment (); +}
---
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/21280790825
[email protected]
AI reviewRe: [RFC PATCH bpf-next v3 10/12] mm/bpf: Add BPF_F_ALLOW_OVERRIDE support for memcg_bpf_ops
In reply to Hui ZhuWhen the override check passes (all conditions at lines 363-377 are met), iter is set to parent_mem_cgroup(memcg) but is never reset to NULL before this loop. Should iter be reset to NULL here?
Quoted 4 lines
mm/bpf_memcontrol.c +0
-0
[ ... ]
Quoted 36 lines
static int
bpf_memcg_ops_reg(void *kdata, struct bpf_link *link){ struct bpf_struct_ops_link *ops_link = container_of(link, struct bpf_struct_ops_link, link); struct memcg_bpf_ops *ops = kdata; struct mem_cgroup *memcg, *iter = NULL; int err = 0;mm/bpf_memcontrol.c +5
-0+if (ops_link->flags & ~BPF_F_ALLOW_OVERRIDE ) { +pr_err ("attach only support BPF_F_ALLOW_OVERRIDE\n" ); +return -EOPNOTSUPP ; + } +memcg = mem_cgroup_get_from_ino(ops_link->cgroup_id); if (IS_ERR_OR_NULL(memcg)) return PTR_ERR(memcg); cgroup_lock();mm/bpf_memcontrol.c +18
-0+ +if (READ_ONCE (memcg->bpf_ops)) { +/* Check if bpf_ops of the parent is BPF_F_ALLOW_OVERRIDE. */ +if (memcg->bpf_ops_flags &BPF_F_ALLOW_OVERRIDE ) { + iter =parent_mem_cgroup (memcg); + +if (!iter) + goto busy_out; +if (READ_ONCE (iter->bpf_ops) != +READ_ONCE (memcg->bpf_ops)) + goto busy_out; + }else { +busy_out: + err = -EBUSY ; + goto unlock_out; + } + } +
Quoted 1 line
while ((iter = mem_cgroup_iter(memcg, iter, NULL))) {^^^^
When the override check passes (all conditions at lines 363-377 are met), iter is set to parent_mem_cgroup(memcg) but is never reset to NULL before this loop. Should iter be reset to NULL here?
The mem_cgroup_iter() function expects prev (iter) to be either NULL or a previous result from iterating within memcg's descendants. When iter points to memcg's parent, the iteration will not correctly traverse memcg's subtree since parent is outside that subtree.
For comparison, bpf_memcg_ops_unreg() correctly resets iter to NULL before its loop:
Quoted 12 lines
mm/bpf_memcontrol.c +2
-0+ iter =NULL ; +while ((iter =mem_cgroup_iter (memcg, iter,NULL ))) {if (READ_ONCE(iter->bpf_ops)) {mm/bpf_memcontrol.c +2
-3-mem_cgroup_iter_break (memcg, iter); - err = -EBUSY ; -break ; +/* cannot override existing bpf_ops of sub-cgroup. */ +continue ;} WRITE_ONCE(iter->bpf_ops, ops);mm/bpf_memcontrol.c +1
-0+ iter->bpf_ops_flags = ops_link->flags;
Quoted 1 line
}
[ ... ]
---
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/21280790825
[email protected]
AI reviewRe: [RFC PATCH bpf-next v3 08/12] mm: memcontrol: Add BPF struct_ops for memory controller
In reply to Hui ZhuThese stub functions appear to have syntax issues that would prevent compilation when CONFIG_BPF_SYSCALL is disabled:
Quoted 4 lines
include/linux/memcontrol.h +0
-0
[ ... ]
include/linux/memcontrol.h static inline bool mem_cgroup_disabled(void) · line 646
Quoted 6 lines
include/linux/memcontrol.h +6
-0+#else /* CONFIG_BPF_SYSCALL */ + +static inline unsigned long +bpf_memcg_get_high_delay(struct mem_cgroup *memcg) { return 0 } +static inline bpf_memcg_below_low(struct mem_cgroup *memcg) { return false } +static inline bpf_memcg_below_min(struct mem_cgroup *memcg) { return false }
^^^^^^^^^^^^^^^^^^^^
These stub functions appear to have syntax issues that would prevent compilation when CONFIG_BPF_SYSCALL is disabled:
1. bpf_memcg_get_high_delay() is missing a semicolon after "return 0"
2. bpf_memcg_below_low() is missing the "bool" return type
3. bpf_memcg_below_min() is missing the "bool" return type
Should these be:
static inline unsigned long
bpf_memcg_get_high_delay(struct mem_cgroup *memcg) { return 0; } static inline bool bpf_memcg_below_low(struct mem_cgroup *memcg) { return false; } static inline bool bpf_memcg_below_min(struct mem_cgroup *memcg) { return false; }
Quoted 4 lines
include/linux/memcontrol.h +4
-0+static inline void memcontrol_bpf_online (struct mem_cgroup *memcg) { } +static inline void memcontrol_bpf_offline (struct mem_cgroup *memcg) { } + +#endif /* CONFIG_BPF_SYSCALL */
[ ... ]
Quoted 4 lines
mm/bpf_memcontrol.c +0
-0
[ ... ]
mm/bpf_memcontrol.c static const struct btf_kfunc_id_set bpf_memcontrol_kfunc_set = { · line 360
Quoted 9 lines
mm/bpf_memcontrol.c +9
-0+static void clean_memcg_bpf_ops (struct mem_cgroup *memcg, +struct memcg_bpf_ops *ops) +{ +struct mem_cgroup *iter =NULL ; + +while ((iter =mem_cgroup_iter (memcg, iter,NULL ))) { +if (ops) { +if (!WARN_ON (READ_ONCE (memcg->bpf_ops) != ops)) +WRITE_ONCE (memcg->bpf_ops,NULL );
^^^^^
When ops is non-NULL, this code checks and writes to memcg->bpf_ops, but should it be using iter->bpf_ops instead? The else branch at the bottom correctly uses iter. As written, when called from bpf_memcg_ops_unreg() with a non-NULL ops argument, this only clears the root cgroup's bpf_ops pointer while leaving all descendant cgroups with stale pointers to the ops structure that will be freed after synchronize_srcu().
Quoted 4 lines
mm/bpf_memcontrol.c +4
-0+ }else +WRITE_ONCE (iter->bpf_ops,NULL ); + } +}
[ ... ]
mm/bpf_memcontrol.c static const struct btf_kfunc_id_set bpf_memcontrol_kfunc_set = { · line 439
Quoted 11 lines
mm/bpf_memcontrol.c +11
-0+static int __init bpf_memcontrol_init (void ) +{ +int err, err2; + + 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); + + err2 =register_bpf_struct_ops (&bpf_memcg_bpf_ops, memcg_bpf_ops); +if (err)
^^^
The condition here checks err but prints err2. Should this be "if (err2)" to log when the struct_ops registration fails?
mm/bpf_memcontrol.c static const struct btf_kfunc_id_set bpf_memcontrol_kfunc_set = {
Quoted 5 lines
mm/bpf_memcontrol.c +5
-0+pr_warn ("error while registering memcontrol bpf ops: %d" , err2); + +return err ? err : err2; +} +late_initcall (bpf_memcontrol_init);
Note: The clean_memcg_bpf_ops() issue appears to be addressed in a later commit in this series (90eaa9cd0346 "mm/bpf: Add BPF_F_ALLOW_OVERRIDE support for memcg_bpf_ops") which removes and rewrites the function entirely. However, the syntax errors in the stubs and the error check typo in bpf_memcontrol_init() do not appear to be fixed in the series.
---
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/21280790825
kernel test robot
build botRe: [RFC PATCH bpf-next v3 05/12] libbpf: introduce bpf_map__attach_struct_ops_opts()
In reply to Hui Zhukernel test robot noticed the following build warnings:
Hi Hui,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on bpf-next/master]
[cannot apply to akpm-mm/mm-everything bpf-next/net bpf/master linus/master v6.16-rc1 next-20260122] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Hui-Zhu/bpf-move-bpf_struct_ops_link-into-bpf-h/20260123-170445 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/03bf6aafe30def690c63e454d76e733f27dff4b3.1769157382.git.zhuhui%40kylinos.cn patch subject: [RFC PATCH bpf-next v3 05/12] libbpf: introduce bpf_map__attach_struct_ops_opts() config: i386-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260123/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260123/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: Num of global symbols in tools/testing/selftests/hid/tools/build/libbpf/sharedobjs/libbpf-in.o (357) does NOT match with num of versioned symbols in tools/testing/selftests/hid/tools/build/libbpf/libbpf.so libbpf.map (356). Please make sure all LIBBPF_API symbols are versioned in libbpf.map. make[3]: *** [Makefile:179: check_abi] Error 1 make[3]: Target 'all_cmd' not remade because of errors. -- >> Warning: Num of global symbols in tools/testing/selftests/net/tools/build/libbpf/sharedobjs/libbpf-in.o (357) does NOT match with num of versioned symbols in tools/testing/selftests/net/tools/build/libbpf/libbpf.so libbpf.map (356). Please make sure all LIBBPF_API symbols are versioned in libbpf.map. make[3]: *** [Makefile:179: check_abi] Error 1 make[3]: Target 'all_cmd' not remade because of errors. -- >> Warning: Num of global symbols in tools/testing/selftests/net/lib/tools/build/libbpf/sharedobjs/libbpf-in.o (357) does NOT match with num of versioned symbols in tools/testing/selftests/net/lib/tools/build/libbpf/libbpf.so libbpf.map (356). Please make sure all LIBBPF_API symbols are versioned in libbpf.map.
make[3]: *** [Makefile:179: check_abi] Error 1 make[3]: Target 'all_cmd' not remade because of errors.
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [RFC PATCH bpf-next v3 05/12] libbpf: introduce bpf_map__attach_struct_ops_opts()
In reply to Hui Zhukernel test robot noticed the following build warnings:
Hi Hui,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on bpf-next/master]
[cannot apply to akpm-mm/mm-everything bpf-next/net bpf/master linus/master v6.19-rc6 next-20260122] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Hui-Zhu/bpf-move-bpf_struct_ops_link-into-bpf-h/20260123-170445 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/03bf6aafe30def690c63e454d76e733f27dff4b3.1769157382.git.zhuhui%40kylinos.cn patch subject: [RFC PATCH bpf-next v3 05/12] libbpf: introduce bpf_map__attach_struct_ops_opts() config: powerpc64-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260123/[email protected]/config) compiler: powerpc64-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260123/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: Num of global symbols in tools/testing/selftests/hid/tools/build/host/libbpf/sharedobjs/libbpf-in.o (357) does NOT match with num of versioned symbols in tools/testing/selftests/hid/tools/build/host/libbpf/libbpf.so libbpf.map (356). Please make sure all LIBBPF_API symbols are versioned in libbpf.map.
make[3]: *** [Makefile:179: check_abi] Error 1 make[3]: Target 'all_cmd' not remade because of errors.
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [RFC PATCH bpf-next v3 09/12] selftests/bpf: Add tests for memcg_bpf_ops
In reply to Hui ZhuHi Hui,
Quoted 37 lines · Hui Zhu
From: Hui Zhu <[email protected]>
Add a comprehensive selftest suite for the
memcg_bpf_opsfunctionality. These tests validate that BPF programs can correctly influence memory cgroup throttling behavior by implementing the new hooks.The test suite is added in
prog_tests/memcg_ops.cand covers several key scenarios:1.
test_memcg_ops_over_high:
Verifies that a BPF program can trigger throttling on a low-priority cgroup by returning a delay from theget_high_delay_mshook when a high-priority cgroup is under pressure.2.
test_memcg_ops_below_low_over_high:
Tests the combination of thebelow_lowandget_high_delay_mshooks, ensuring they work together as expected.3.
test_memcg_ops_below_min_over_high:
Validates the interaction between thebelow_minandget_high_delay_mshooks.The test framework sets up a cgroup hierarchy with high and low priority groups, attaches BPF programs, runs memory-intensive workloads, and asserts that the observed throttling (measured by workload execution time) matches expectations.
The BPF program (
progs/memcg_ops.c) uses a tracepoint onmemcg:count_memcg_events(specifically PGFAULT) to detect memory pressure and trigger the appropriate hooks in response. This test suite provides essential validation for the new memory control mechanisms.Signed-off-by Geliang Tang <[email protected]>Signed-off-by Hui Zhu <[email protected]>---
[..]
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +0
-0@@ -0,0 +1,537 @@
[..]
Quoted 46 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +46
-0+ +static void +real_test_memcg_ops_child_work (const char *cgroup_path, +char *data_filename, +char *time_filename, +int read_times) +{ +struct timeval start, end; +double elapsed; +FILE *fp; + +if (!ASSERT_OK (join_parent_cgroup (cgroup_path),"join_parent_cgroup" )) + goto out; + +if (env.verbosity >=VERBOSE_NORMAL ) +printf ("%s %d begin\n" , __func__,getpid ()); + +gettimeofday (&start,NULL ); + +if (!ASSERT_OK (write_file (data_filename),"write_file" )) + goto out; + +if (env.verbosity >=VERBOSE_NORMAL ) +printf ("%s %d write_file done\n" , __func__,getpid ()); + +if (!ASSERT_OK (read_file (data_filename, read_times),"read_file" )) + goto out; + +gettimeofday (&end,NULL ); + + elapsed = (end.tv_sec - start.tv_sec) + + (end.tv_usec - start.tv_usec) /1000000.0 ; + +if (env.verbosity >=VERBOSE_NORMAL ) +printf ("%s %d end %.6f\n" , __func__,getpid (), elapsed); + + fp =fopen (time_filename,"w" ); +if (!ASSERT_OK_PTR (fp,"fopen" )) + goto out; +fprintf (fp,"%.6f" , elapsed); +fclose (fp); + +out: +exit (0 ); +} +
[..]
Quoted 25 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +25
-0+static void real_test_memcg_ops (int read_times) +{ +int ret; +char data_file1[] ="/tmp/test_data_XXXXXX" ; +char data_file2[] ="/tmp/test_data_XXXXXX" ; +char time_file1[] ="/tmp/test_time_XXXXXX" ; +char time_file2[] ="/tmp/test_time_XXXXXX" ; +pid_t pid1, pid2; +double time1, time2; + + ret =mkstemp (data_file1); +if (!ASSERT_GT (ret,0 ,"mkstemp" )) +return ; +close (ret); + ret =mkstemp (data_file2); +if (!ASSERT_GT (ret,0 ,"mkstemp" )) + goto cleanup_data_file1; +close (ret); + ret =mkstemp (time_file1); +if (!ASSERT_GT (ret,0 ,"mkstemp" )) + goto cleanup_data_file2; +close (ret); + ret =mkstemp (time_file2); +if (!ASSERT_GT (ret,0 ,"mkstemp" )) + goto cleanup_time_file1;
tools/testing/selftests/bpf/prog_tests/memcg_ops.c line 275
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c new file +10
-0+close (ret); + + pid1 =fork (); +if (!ASSERT_GE (pid1,0 ,"fork" )) + goto cleanup; +if (pid1 ==0 ) +real_test_memcg_ops_child_work (CG_LOW_DIR , + data_file1, + time_file1, + read_times);
Would it be better to call exit() after real_test_memcg_ops_child_work() instead of within it? This way the fork/exit/wait logic is contained in the same scope making the lifetimes easier to track. I had to go back and search for the call to exit() since at a glance this function appears to proceed to call fork() and waitpid() from within both parent and child procs (though it really does not).
kernel test robot
build botRe: [RFC PATCH bpf-next v3 08/12] mm: memcontrol: Add BPF struct_ops for memory controller
In reply to Hui Zhukernel test robot noticed the following build errors:
Hi Hui,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
[cannot apply to akpm-mm/mm-everything bpf-next/net bpf/master linus/master v6.19-rc6 next-20260123] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Hui-Zhu/bpf-move-bpf_struct_ops_link-into-bpf-h/20260123-170445 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/863b91cb85097f137bc741e7cb686ff78d9bb95e.1769157382.git.zhuhui%40kylinos.cn patch subject: [RFC PATCH bpf-next v3 08/12] mm: memcontrol: Add BPF struct_ops for memory controller config: loongarch-randconfig-002-20260125 (https://download.01.org/0day-ci/archive/20260125/[email protected]/config) compiler: loongarch64-linux-gcc (GCC) 14.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260125/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from include/linux/swap.h:9,
from include/linux/suspend.h:5,
from arch/loongarch/kernel/asm-offsets.c:13:
include/linux/memcontrol.h: In function 'bpf_memcg_get_high_delay':
>> include/linux/memcontrol.h:644:62: error: expected ';' before '}' token
644 | bpf_memcg_get_high_delay(struct mem_cgroup *memcg) { return 0 }
| ^~
| ;
include/linux/memcontrol.h: At top level:
>> include/linux/memcontrol.h:645:15: error: return type defaults to 'int' [-Wimplicit-int]
645 | static inline bpf_memcg_below_low(struct mem_cgroup *memcg) { return false }
| ^~~~~~~~~~~~~~~~~~~
include/linux/memcontrol.h: In function 'bpf_memcg_below_low':
include/linux/memcontrol.h:645:75: error: expected ';' before '}' token
645 | static inline bpf_memcg_below_low(struct mem_cgroup *memcg) { return false }
| ^~
| ;
include/linux/memcontrol.h: At top level:
include/linux/memcontrol.h:646:15: error: return type defaults to 'int' [-Wimplicit-int]
646 | static inline bpf_memcg_below_min(struct mem_cgroup *memcg) { return false }
| ^~~~~~~~~~~~~~~~~~~
include/linux/memcontrol.h: In function 'bpf_memcg_below_min':
include/linux/memcontrol.h:646:75: error: expected ';' before '}' token
646 | static inline bpf_memcg_below_min(struct mem_cgroup *memcg) { return false }
| ^~
| ; make[3]: *** [scripts/Makefile.build:182: arch/loongarch/kernel/asm-offsets.s] Error 1 shuffle=1717507361 make[3]: Target 'prepare' not remade because of errors.
make[2]: *** [Makefile:1315: prepare0] Error 2 shuffle=1717507361
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:248: __sub-make] Error 2 shuffle=1717507361
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:248: __sub-make] Error 2 shuffle=1717507361
make: Target 'prepare' not remade because of errors.
vim +644 include/linux/memcontrol.h
642
643 static inline unsigned long
> 644 bpf_memcg_get_high_delay(struct mem_cgroup *memcg) { return 0 }
> 645 static inline bpf_memcg_below_low(struct mem_cgroup *memcg) { return false } 646 static inline bpf_memcg_below_min(struct mem_cgroup *memcg) { return false }
647 static inline void memcontrol_bpf_online(struct mem_cgroup *memcg) { }
648 static inline void memcontrol_bpf_offline(struct mem_cgroup *memcg) { }
649
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [RFC PATCH bpf-next v3 08/12] mm: memcontrol: Add BPF struct_ops for memory controller
In reply to Hui Zhukernel test robot noticed the following build errors:
Hi Hui,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
[cannot apply to akpm-mm/mm-everything bpf-next/net bpf/master linus/master v6.19-rc6 next-20260123] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Hui-Zhu/bpf-move-bpf_struct_ops_link-into-bpf-h/20260123-170445 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/863b91cb85097f137bc741e7cb686ff78d9bb95e.1769157382.git.zhuhui%40kylinos.cn patch subject: [RFC PATCH bpf-next v3 08/12] mm: memcontrol: Add BPF struct_ops for memory controller config: hexagon-randconfig-002-20260125 (https://download.01.org/0day-ci/archive/20260125/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260125/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from kernel/sched/rq-offsets.c:5:
In file included from kernel/sched/sched.h:61:
In file included from include/linux/syscalls_api.h:1:
In file included from include/linux/syscalls.h:96:
In file included from include/trace/syscall.h:7:
In file included from include/linux/trace_events.h:10:
In file included from include/linux/perf_event.h:53:
In file included from include/linux/security.h:35:
In file included from include/linux/bpf.h:32:
>> include/linux/memcontrol.h:644:62: error: expected ';' after return statement
644 | bpf_memcg_get_high_delay(struct mem_cgroup *memcg) { return 0 }
| ^
| ;
>> include/linux/memcontrol.h:645:15: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
645 | static inline bpf_memcg_below_low(struct mem_cgroup *memcg) { return false }
| ~~~~~~~~~~~~~ ^
| int
include/linux/memcontrol.h:645:75: error: expected ';' after return statement
645 | static inline bpf_memcg_below_low(struct mem_cgroup *memcg) { return false }
| ^
| ;
include/linux/memcontrol.h:646:15: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
646 | static inline bpf_memcg_below_min(struct mem_cgroup *memcg) { return false }
| ~~~~~~~~~~~~~ ^
| int
include/linux/memcontrol.h:646:75: error: expected ';' after return statement
646 | static inline bpf_memcg_below_min(struct mem_cgroup *memcg) { return false }
| ^
| ; 5 errors generated.
make[3]: *** [scripts/Makefile.build:182: kernel/sched/rq-offsets.s] Error 1 shuffle=710836874 make[3]: Target 'prepare' not remade because of errors.
make[2]: *** [Makefile:1315: prepare0] Error 2 shuffle=710836874
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:248: __sub-make] Error 2 shuffle=710836874
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:248: __sub-make] Error 2 shuffle=710836874
make: Target 'prepare' not remade because of errors.
vim +644 include/linux/memcontrol.h
642
643 static inline unsigned long
> 644 bpf_memcg_get_high_delay(struct mem_cgroup *memcg) { return 0 }
> 645 static inline bpf_memcg_below_low(struct mem_cgroup *memcg) { return false } 646 static inline bpf_memcg_below_min(struct mem_cgroup *memcg) { return false }
647 static inline void memcontrol_bpf_online(struct mem_cgroup *memcg) { }
648 static inline void memcontrol_bpf_offline(struct mem_cgroup *memcg) { }
649
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [RFC PATCH bpf-next v3 09/12] selftests/bpf: Add tests for memcg_bpf_ops
In reply to JP Kobryn2026年1月24日 04:47, "JP Kobryn" <[email protected] mailto:[email protected]?to=%22JP%20Kobryn%22%20%3Cinwardvessel%40gmail.com%3E > 写到:
Quoted 135 lines
Hi Hui,
Quoted 31 lines · Hui Zhu
From: Hui Zhu <[email protected]>
Add a comprehensive selftest suite for thememcg_bpf_opsfunctionality. These tests validate that BPF programs can correctly influence memory cgroup throttling behavior by implementing the new hooks.
The test suite is added inprog_tests/memcg_ops.cand covers several key scenarios:
1.test_memcg_ops_over_high:
Verifies that a BPF program can trigger throttling on a low-priority cgroup by returning a delay from theget_high_delay_mshook when a high-priority cgroup is under pressure.
2.test_memcg_ops_below_low_over_high:
Tests the combination of thebelow_lowandget_high_delay_mshooks, ensuring they work together as expected.
3.test_memcg_ops_below_min_over_high:
Validates the interaction between thebelow_minandget_high_delay_mshooks.
The test framework sets up a cgroup hierarchy with high and low priority groups, attaches BPF programs, runs memory-intensive workloads, and asserts that the observed throttling (measured by workload execution time) matches expectations.
The BPF program (progs/memcg_ops.c) uses a tracepoint onmemcg:count_memcg_events(specifically PGFAULT) to detect memory pressure and trigger the appropriate hooks in response. This test suite provides essential validation for the new memory control mechanisms.Signed-off-by Geliang Tang <[email protected]>Signed-off-by Hui Zhu <[email protected]>---
[..]
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/memcg_ops.c +0
-0new file mode 100644 index 000000000000..9a8d16296f2d --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/memcg_ops.c @@ -0,0 +1,537 @@[..]
Quoted 48 lines
+
+static void
+real_test_memcg_ops_child_work(const char*cgroup_path,
+ char*data_filename,
+ char*time_filename,
+ intread_times)
+{
+ struct timeval start, end;
+ double elapsed;
+ FILE *fp;
+
+ if (!ASSERT_OK(join_parent_cgroup(cgroup_path), "join_parent_cgroup"))
+ goto out;
+
+ if (env.verbosity >=VERBOSE_NORMAL)
+printf("%s %d begin\n", __func__, getpid());
+
+gettimeofday(&start, NULL);
+
+ if (!ASSERT_OK(write_file(data_filename), "write_file"))
+ goto out;
+
+ if (env.verbosity >=VERBOSE_NORMAL)
+printf("%s %d write_file done\n", __func__, getpid());
+
+ if (!ASSERT_OK(read_file(data_filename,read_times), "read_file"))
+ goto out;
+
+gettimeofday(&end, NULL);
+
+ elapsed = (end.tv_sec - start.tv_sec) +
+ (end.tv_usec - start.tv_usec) / 1000000.0;
+
+ if (env.verbosity >=VERBOSE_NORMAL)
+printf("%s %d end %.6f\n", __func__, getpid(), elapsed);
+
+ fp =fopen(time_filename, "w");
+ if (!ASSERT_OK_PTR(fp, "fopen"))
+ goto out;
+fprintf(fp, "%.6f", elapsed);
+ fclose(fp);
+
+out:
+ exit(0);
+}
+[..]
Quoted 28 lines
+static void
real_test_memcg_ops(int read_times)
+{
+ int ret;
+ char data_file1[] = "/tmp/test_data_XXXXXX";
+ char data_file2[] = "/tmp/test_data_XXXXXX";
+ char time_file1[] = "/tmp/test_time_XXXXXX";
+ char time_file2[] = "/tmp/test_time_XXXXXX";
+pid_tpid1, pid2;
+ double time1, time2;
+
+ ret =mkstemp(data_file1);
+ if (!ASSERT_GT(ret, 0, "mkstemp"))
+ return;
+ close(ret);
+ ret =mkstemp(data_file2);
+ if (!ASSERT_GT(ret, 0, "mkstemp"))
+ gotocleanup_data_file1;
+ close(ret);
+ ret =mkstemp(time_file1);
+ if (!ASSERT_GT(ret, 0, "mkstemp"))
+ gotocleanup_data_file2;
+ close(ret);
+ ret =mkstemp(time_file2);
+ if (!ASSERT_GT(ret, 0, "mkstemp"))
+ gotocleanup_time_file1;
+ close(ret);
+Quoted 9 lines
+ pid1 =
fork();
+ if (!ASSERT_GE(pid1, 0, "fork"))
+ goto cleanup;
+ if (pid1 == 0)
+ real_test_memcg_ops_child_work(CG_LOW_DIR,
+data_file1,
+time_file1,
+read_times);
Quoted 7 lines
Would it be better to call
exit()afterreal_test_memcg_ops_child_work()instead of within it? This way the fork/exit/wait logic is contained in the same scope making the lifetimes easier to track. I had to go back and search for the call toexit()since at a glance this function appears to proceed to callfork()andwaitpid()from within both parent and child procs (though it really does not).
I will fix it.
Best,
Hui