Skip to contentSkip to conversation

Memory Controller eBPF support

RFC PATCH In review Hui Zhu 3 participants 8 messages Dec 30, 2025 – Jan 9

Subsystems (7)
lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

v2 cover Memory Controller eBPF support

From: Hui Zhu <[email protected]>

This series adds BPF struct_ops support to the memory controller, enabling dynamic control over memory pressure through the memcg_nr_pages_over_high mechanism. This allows administrators to suppress low-priority cgroups' memory usage based on custom policies implemented in BPF programs.

Background and Motivation

The memory controller provides memory.high limits to throttle cgroups exceeding their soft limit. However, the current implementation applies the same policy across all cgroups without considering priority or workload characteristics.

This series introduces a BPF hook that allows reporting additional "pages over high" for specific cgroups, effectively increasing memory pressure and throttling for lower-priority workloads when higher-priority cgroups need resources.

Use Case: Priority-Based Memory Management

Consider a system running both latency-sensitive services and batch processing workloads. When the high-priority service experiences memory pressure (detected via page scan events), the BPF program can artificially inflate the "over high" count for low-priority cgroups, causing them to be throttled more aggressively and freeing up memory for the critical workload.

Implementation

This series builds upon Roman Gushchin's BPF OOM patch series in [1].

The implementation adds:
1. A memcg_bpf_ops struct_ops type with memcg_nr_pages_over_high hook
2. Integration into memory pressure calculation paths
3. Cgroup hierarchy management (inheritance during online/offline)
4. SRCU protection for safe concurrent access

Why Not PSI?

This implementation does not use PSI for triggering, as discussed in [2].
Instead, the sample code monitors PGSCAN events via tracepoints, which provides more direct feedback on memory pressure.

Example Results

Testing on x86_64 QEMU (10 CPU, 4GB RAM, cache=none swap):

root@ubuntu:~# cat /proc/sys/vm/swappiness
60
root@ubuntu:~# mkdir /sys/fs/cgroup/high
root@ubuntu:~# mkdir /sys/fs/cgroup/low
root@ubuntu:~# ./memcg /sys/fs/cgroup/low /sys/fs/cgroup/high 100 1024
Successfully attached!
root@ubuntu:~# cgexec -g memory:low stress-ng --vm 4 --vm-keep --vm-bytes 80% \
--vm-method all --seed 2025 --metrics -t 60 \
& cgexec -g memory:high stress-ng --vm 4 --vm-keep --vm-bytes 80% \
--vm-method all --seed 2025 --metrics -t 60
[1] 1075
stress-ng: info:  [1075] setting to a 1 min, 0 secs run per stressor
stress-ng: info:  [1076] setting to a 1 min, 0 secs run per stressor
stress-ng: info:  [1075] dispatching hogs: 4 vm
stress-ng: info:  [1076] dispatching hogs: 4 vm
stress-ng: metrc: [1076] stressor       bogo ops real time  usr time  sys time   bogo ops/s     bogo ops/s CPU used per       RSS Max
stress-ng: metrc: [1076]                           (secs)    (secs)    (secs)   (real time) (usr+sys time) instance (%)          (KB)
stress-ng: metrc: [1076] vm             21033377     60.47    158.04      3.66    347825.55      130076.67        66.85        834836
stress-ng: info:  [1076] skipped: 0
stress-ng: info:  [1076] passed: 4: vm (4)
stress-ng: info:  [1076] failed: 0
stress-ng: info:  [1076] metrics untrustworthy: 0
stress-ng: info:  [1076] successful run completed in 1 min, 0.72 secs
root@ubuntu:~# stress-ng: metrc: [1075] stressor       bogo ops real time  usr time  sys time   bogo ops/s     bogo ops/s CPU used per       RSS Max
stress-ng: metrc: [1075]                           (secs)    (secs)    (secs)   (real time) (usr+sys time) instance (%)          (KB)
stress-ng: metrc: [1075] vm                11568     65.05      0.00      0.21       177.83       56123.74         0.08          3200
stress-ng: info:  [1075] skipped: 0
stress-ng: info:  [1075] passed: 4: vm (4)
stress-ng: info:  [1075] failed: 0
stress-ng: info:  [1075] metrics untrustworthy: 0
stress-ng: info:  [1075] successful run completed in 1 min, 5.06 secs

Results show the low-priority cgroup (/sys/fs/cgroup/low) was significantly throttled:
- High-priority cgroup: 21,033,377 bogo ops at 347,825 ops/s
- Low-priority cgroup: 11,568 bogo ops at 177 ops/s

The stress-ng process in the low-priority cgroup experienced a ~99.9% slowdown in memory operations compared to the high-priority cgroup, demonstrating effective priority enforcement through BPF-controlled memory pressure.

Patch Overview

PATCH 1/3: Core kernel implementation
- Adds memcg_bpf_ops struct_ops support
- Implements cgroup lifecycle management
- Integrates hook into pressure calculation

PATCH 2/3: Selftest suite
- Validates attach/detach behavior
- Tests hierarchy inheritance
- Verifies throttling effectiveness

PATCH 3/3: Sample programs
- Demonstrates PGSCAN-based triggering
- Shows priority-based throttling
- Provides reference implementation

Changelog:
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]/
[2] https://lore.kernel.org/lkml/[email protected]/

Hui Zhu (3):
mm: memcontrol: Add BPF struct_ops for memory pressure control selftests/bpf: Add tests for memcg_bpf_ops samples/bpf: Add memcg priority control example

 MAINTAINERS                                   |   5 +
 include/linux/memcontrol.h                    |   2 +
 mm/bpf_memcontrol.c                           | 241 ++++++++++++-
 mm/bpf_memcontrol.h                           |  73 ++++
 mm/memcontrol.c                               |  27 +-
 samples/bpf/.gitignore                        |   1 +
 samples/bpf/Makefile                          |   9 +-
 samples/bpf/memcg.bpf.c                       |  95 +++++
 samples/bpf/memcg.c                           | 204 +++++++++++
 .../selftests/bpf/prog_tests/memcg_ops.c      | 340 ++++++++++++++++++
 .../selftests/bpf/progs/memcg_ops_over_high.c |  95 +++++
 11 files changed, 1082 insertions(+), 10 deletions(-)
 create mode 100644 mm/bpf_memcontrol.h
 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_over_high.c
Signature · 1 line
2.43.0
author
#1 lore raw

RFC PATCH v2 1/3 mm: memcontrol: Add BPF struct_ops for memory pressure control

From: Hui Zhu <[email protected]>

Add BPF struct_ops support to the memory controller, enabling custom control over memory pressure through the memcg_nr_pages_over_high mechanism.

This patch introduces a new memcg_bpf_ops struct_ops type that allows BPF programs to report additional "pages over high" for specific memory cgroups. This additional count is integrated into the existing memory pressure calculation, causing affected cgroups to be throttled more aggressively.

Key components:

1. memcg_bpf_ops structure:
- memcg_nr_pages_over_high: Returns custom over-high count
- handle_cgroup_online: Called when cgroup comes online
- handle_cgroup_offline: Called when cgroup goes offline

2. Integration points:
- reclaim_high(): Check BPF hook before reclaim decisions
- calculate_overage(): Add BPF-reported overage to calculation
- __mem_cgroup_handle_over_high(): Include BPF overage in throttling decisions
- try_charge_memcg(): Trigger handling if BPF reports high overage

3. Lifecycle management:
- Programs inherit from parent during cgroup online
- SRCU protection for safe concurrent access
- Clean detachment during cgroup offline
- Hierarchy-wide attachment/detachment

Use case: A high-priority cgroup experiencing memory pressure can trigger BPF logic to report additional overage for low-priority cgroups, causing them to be throttled and free up memory.

This builds upon Roman Gushchin's BPF OOM patch series:
https://lore.kernel.org/lkml/[email protected]/

---
 MAINTAINERS                |   1 +
 include/linux/memcontrol.h |   2 +
 mm/bpf_memcontrol.c        | 241 ++++++++++++++++++++++++++++++++++++-
 mm/bpf_memcontrol.h        |  73 +++++++++++
 mm/memcontrol.c            |  27 +++--
 5 files changed, 335 insertions(+), 9 deletions(-)
 create mode 100644 mm/bpf_memcontrol.h
MAINTAINERS +1 -0
index 229b5fae7a91..158f3ba63ee7 100644
@@ -6349,6 +6349,7 @@ L:	[email protected]
 S:	Maintained
 F:	include/linux/memcontrol.h
 F:	include/linux/page_counter.h
+F:	mm/bpf_memcontrol.h
 F:	mm/memcontrol.c
 F:	mm/memcontrol-v1.c
 F:	mm/memcontrol-v1.h
include/linux/memcontrol.h +2 -0
index b9e08dddd7ad..ea623aee4ca5 100644
@@ -229,6 +229,8 @@ struct mem_cgroup {
 
 #ifdef CONFIG_BPF_SYSCALL
 	struct bpf_oom_ops *bpf_oom;
+
+	struct memcg_bpf_ops *bpf_ops;
 #endif
 
 	int swappiness;
mm/bpf_memcontrol.c +239 -2

260 lines not on this page · raw message

mm/bpf_memcontrol.h new file +73 -0

73 lines not on this page · raw message

mm/memcontrol.c +20 -7
index d44c1f293e16..5cbb0f343bc3 100644
@@ -69,6 +69,7 @@
 #include <net/ip.h>
 #include "slab.h"
 #include "memcontrol-v1.h"
+#include "bpf_memcontrol.h"
 
 #include <linux/uaccess.h>
 
@@ -2038,7 +2039,8 @@ static unsigned long reclaim_high(struct mem_cgroup *memcg,
 		unsigned long pflags;
 
 		if (page_counter_read(&memcg->memory) <=
-		    READ_ONCE(memcg->memory.high))
+		    READ_ONCE(memcg->memory.high) &&
+		    !bpf_memcg_nr_pages_over_high(memcg))
 			continue;
 
 		memcg_memory_event(memcg, MEMCG_HIGH);
@@ -2116,11 +2118,12 @@ static void high_work_func(struct work_struct *work)
  #define MEMCG_DELAY_PRECISION_SHIFT 20
  #define MEMCG_DELAY_SCALING_SHIFT 14
 
-static u64 calculate_overage(unsigned long usage, unsigned long high)
+static u64 calculate_overage(unsigned long usage, unsigned long high,
+			     unsigned long bpf_over_high)
 {
 	u64 overage;
 
-	if (usage <= high)
+	if (!bpf_over_high && usage <= high)
 		return 0;
 
 	/*
@@ -2130,6 +2133,7 @@ static u64 calculate_overage(unsigned long usage, unsigned long high)
 	high = max(high, 1UL);
 
 	overage = usage - high;
+	overage = max(overage, bpf_over_high);
 	overage <<= MEMCG_DELAY_PRECISION_SHIFT;
 	return div64_u64(overage, high);
 }
@@ -2140,7 +2144,8 @@ static u64 mem_find_max_overage(struct mem_cgroup *memcg)
 
 	do {
 		overage = calculate_overage(page_counter_read(&memcg->memory),
-					    READ_ONCE(memcg->memory.high));
+					    READ_ONCE(memcg->memory.high),
+					    bpf_memcg_nr_pages_over_high(memcg));
 		max_overage = max(overage, max_overage);
 	} while ((memcg = parent_mem_cgroup(memcg)) &&
 		 !mem_cgroup_is_root(memcg));
@@ -2154,7 +2159,7 @@ static u64 swap_find_max_overage(struct mem_cgroup *memcg)
 
 	do {
 		overage = calculate_overage(page_counter_read(&memcg->swap),
-					    READ_ONCE(memcg->swap.high));
+					    READ_ONCE(memcg->swap.high), 0);
 		if (overage)
 			memcg_memory_event(memcg, MEMCG_SWAP_HIGH);
 		max_overage = max(overage, max_overage);
@@ -2210,12 +2215,14 @@ void __mem_cgroup_handle_over_high(gfp_t gfp_mask)
 	unsigned long penalty_jiffies;
 	unsigned long pflags;
 	unsigned long nr_reclaimed;
-	unsigned int nr_pages = current->memcg_nr_pages_over_high;
+	unsigned int nr_pages;
 	int nr_retries = MAX_RECLAIM_RETRIES;
 	struct mem_cgroup *memcg;
 	bool in_retry = false;
 
 	memcg = get_mem_cgroup_from_mm(current->mm);
+	nr_pages = max(current->memcg_nr_pages_over_high,
+		       bpf_memcg_nr_pages_over_high(memcg));
 	current->memcg_nr_pages_over_high = 0;
 
 retry_reclaim:
@@ -2309,6 +2316,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))
@@ -2434,6 +2442,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
@@ -2483,7 +2492,8 @@ 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 &&
+	if ((current->memcg_nr_pages_over_high > MEMCG_CHARGE_BATCH ||
+	     bpf_memcg_nr_pages_over_high(orig_memcg) > MEMCG_CHARGE_BATCH) &&
 	    !(current->flags & PF_MEMALLOC) &&
 	    gfpflags_allow_blocking(gfp_mask))
 		__mem_cgroup_handle_over_high(gfp_mask);
@@ -3867,6 +3877,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);
@@ -3887,6 +3899,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
 	zswap_memcg_offline_cleanup(memcg);
 
 	bpf_oom_memcg_offline(memcg);
+	memcontrol_bpf_offline(memcg);
 	memcg_offline_kmem(memcg);
 	reparent_shrinker_deferred(memcg);
 	wb_memcg_offline(memcg);
Signature · 1 line
2.43.0
author
#2 lore raw

RFC PATCH v2 2/3 selftests/bpf: Add tests for memcg_bpf_ops

In reply to Hui Zhu

From: Hui Zhu <[email protected]>

Add comprehensive selftests for the memcg_bpf_ops struct_ops functionality.

The test creates a cgroup hierarchy with high and low priority cgroups, attaches a BPF program that monitors PGSCAN events on the high-priority cgroup, and verifies that low-priority tasks are throttled when the BPF program reports additional overage.

Test flow:
1. Create /memcg_ops_test/high and /memcg_ops_test/low cgroups
2. Attach BPF program to monitor high cgroup's PGSCAN events
3. When PGSCAN events exceed threshold (64 pages/sec), report 512 pages over high for low cgroup
4. Run memory-intensive workloads in both cgroups
5. Verify low-priority workload is significantly slower

The BPF program uses:
- Tracepoint to monitor memcg:count_memcg_events
- One-second sliding window for PGSCAN aggregation
- Trigger mechanism with configurable threshold and overage

This validates that:
- BPF programs can be attached to cgroup hierarchies
- memcg_nr_pages_over_high hook is called correctly
- Memory pressure calculation includes BPF-reported overage
- Throttling behavior works as expected

This test does not use PSI for triggering due to the reasons discussed in:
https://lore.kernel.org/lkml/[email protected]/

---
 MAINTAINERS                                   |   2 +
 .../selftests/bpf/prog_tests/memcg_ops.c      | 340 ++++++++++++++++++
 .../selftests/bpf/progs/memcg_ops_over_high.c |  95 +++++
 3 files changed, 437 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/memcg_ops.c
 create mode 100644 tools/testing/selftests/bpf/progs/memcg_ops_over_high.c
MAINTAINERS +2 -0
index 158f3ba63ee7..10508c90136a 100644
@@ -6356,6 +6356,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_over_high.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 +340 -0

340 lines not on this page · raw message

tools/testing/selftests/bpf/progs/memcg_ops_over_high.c new file +95 -0

95 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#3 lore raw

RFC PATCH v2 3/3 samples/bpf: Add memcg priority control example

In reply to Hui Zhu

From: Hui Zhu <[email protected]>

Add a sample program demonstrating priority-based memory throttling using memcg_bpf_ops struct_ops.

This sample consists of:

1. memcg.bpf.c: BPF program that monitors PGSCAN events on a high-priority cgroup. When page scan activity exceeds a threshold, it reports additional "over high" pages for a low-priority cgroup, causing it to be throttled.

2. memcg.c: Userspace loader that configures and attaches the BPF program. Takes parameters:
- low_path: Path to low-priority cgroup
- high_path: Path to high-priority cgroup
- threshold: PGSCAN threshold to trigger throttling
- over_high: Number of pages to report as over-high

Usage example:

  # mkdir /sys/fs/cgroup/high
  # mkdir /sys/fs/cgroup/low
  # ./memcg /sys/fs/cgroup/low /sys/fs/cgroup/high 100 1024

When the high-priority cgroup experiences memory pressure (>100 PGSCAN events/sec), the low-priority cgroup will be throttled as if it were 1024 pages over its memory.high limit.

Real-world test results on x86_64 QEMU (10 CPU, 4GB RAM):
- High-priority: 347,825 ops/sec (unaffected)
- Low-priority: 177 ops/sec (throttled by ~99.9%)

This test does not use PSI for triggering due to the reasons discussed in:
https://lore.kernel.org/lkml/[email protected]/

---
 MAINTAINERS             |   2 +
 samples/bpf/.gitignore  |   1 +
 samples/bpf/Makefile    |   9 +-
 samples/bpf/memcg.bpf.c |  95 +++++++++++++++++++
 samples/bpf/memcg.c     | 204 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 310 insertions(+), 1 deletion(-)
 create mode 100644 samples/bpf/memcg.bpf.c
 create mode 100644 samples/bpf/memcg.c
MAINTAINERS +2 -0
index 10508c90136a..91af1f28eb14 100644
@@ -6355,6 +6355,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_over_high.c
samples/bpf/.gitignore +1 -0
index 0002cd359fb1..0de6569cdefd 100644
@@ -49,3 +49,4 @@ iperf.*
 /vmlinux.h
 /bpftool/
 /libbpf/
+memcg
samples/bpf/Makefile +8 -1
index 95a4fa1f1e44..6416c8aa3034 100644
@@ -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 +95 -0

95 lines not on this page · raw message

samples/bpf/memcg.c new file +204 -0

204 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#4 lore raw
In reply to Hui Zhu

Hi Hui.

Quoted 2 lines · Hui Zhu

This allows administrators to suppress low-priority cgroups' memory usage based on custom policies implemented in BPF programs.

BTW memory.low was conceived as a work-conserving mechanism for prioritization of different workloads. Have you tried that? No need to go directly to (high) limits. (<- Main question, below are some secondary implementation questions/remarks.)

...

Quoted 4 lines

This series introduces a BPF hook that allows reporting additional "pages over high" for specific cgroups, effectively increasing memory pressure and throttling for lower-priority workloads when higher-priority cgroups need resources.

Have you considered hooking into calculate_high_delay() instead? (That function has undergone some evolution so it'd seem like the candidate for BPFication.)

...

Quoted 1 line

3. Cgroup hierarchy management (inheritance during online/offline)

I see you're copying the program upon memcg creation. Configuration copies aren't such a good way to properly handle hierarchical behavior.
I wonder if this could follow the more generic pattern of how BPF progs are evaluated in hierarchies, see BPF_F_ALLOW_OVERRIDE and BPF_F_ALLOW_MULTI.

Quoted 1 line

Example Results

...

Quoted 9 lines

Results show the low-priority cgroup (/sys/fs/cgroup/low) was significantly throttled:
- High-priority cgroup: 21,033,377 bogo ops at 347,825 ops/s
- Low-priority cgroup: 11,568 bogo ops at 177 ops/s

The stress-ng process in the low-priority cgroup experienced a ~99.9% slowdown in memory operations compared to the high-priority cgroup, demonstrating effective priority enforcement through BPF-controlled memory pressure.

As a demonstrator, it'd be good to compare this with a baseline without any extra progs, e.g. show that high-prio performed better and low-prio wasn't throttled for nothing.

Thanks,
Michal

#5 lore raw
Quoted 12 lines · Michal Koutný

Hi Hui.

Quoted 2 lines · Hui Zhu

This allows administrators to suppress low-priority cgroups' memory usage based on custom policies implemented in BPF programs.

BTW memory.low was conceived as a work-conserving mechanism for prioritization of different workloads. Have you tried that? No need to go directly to (high) limits. (<- Main question, below are some secondary implementation questions/remarks.)

...

Quoted 9 lines
Quoted 4 lines

This series introduces a BPF hook that allows reporting additional "pages over high" for specific cgroups, effectively increasing memory pressure and throttling for lower-priority workloads when higher-priority cgroups need resources.

Have you considered hooking into calculate_high_delay() instead? (That function has undergone some evolution so it'd seem like the candidate for BPFication.)

+1

This issue[1] might be resolved by hooking into calculate_high_delay().

[1] https://lore.kernel.org/cgroups/4txrfjc5lqkmydmsesfq3l5drmzdio6pkmtfb64sk3ld6bwkhs@w4dkn76s4dbo/T/#t

Quoted 29 lines

...

Quoted 1 line

3. Cgroup hierarchy management (inheritance during online/offline)

I see you're copying the program upon memcg creation. Configuration copies aren't such a good way to properly handle hierarchical behavior.
I wonder if this could follow the more generic pattern of how BPF progs are evaluated in hierarchies, see BPF_F_ALLOW_OVERRIDE and BPF_F_ALLOW_MULTI.

Quoted 1 line

Example Results

...

Quoted 9 lines

Results show the low-priority cgroup (/sys/fs/cgroup/low) was significantly throttled:
- High-priority cgroup: 21,033,377 bogo ops at 347,825 ops/s
- Low-priority cgroup: 11,568 bogo ops at 177 ops/s

The stress-ng process in the low-priority cgroup experienced a ~99.9% slowdown in memory operations compared to the high-priority cgroup, demonstrating effective priority enforcement through BPF-controlled memory pressure.

As a demonstrator, it'd be good to compare this with a baseline without any extra progs, e.g. show that high-prio performed better and low-prio wasn't throttled for nothing.

Thanks,
Michal

Signature · 2 lines
Best regards,
Ridong
#6 lore raw
In reply to Michal Koutný

2025年12月30日 17:49, "Michal Koutný" <[email protected] mailto:[email protected]?to=%22Michal%20Koutn%C3%BD%22%20%3Cmkoutny%40suse.com%3E > 写到:

Hi Michal and Ridong,

Quoted 9 lines

Hi Hui.

Quoted 4 lines · Hui Zhu

This allows administrators to suppress low-priority cgroups' memory
usage based on custom policies implemented in BPF programs.

Quoted 7 lines

BTW memory.low was conceived as a work-conserving mechanism for prioritization of different workloads. Have you tried that? No need to go directly to (high) limits. (<- Main question, below are some secondary implementation questions/remarks.)

...

memory.low is a helpful feature, but it can struggle to effectively throttle low-priority processes that continuously access their memory.

For instance, consider the following example I ran:

root@ubuntu:~# echo $((4 * 1024 * 1024 * 1024)) > /sys/fs/cgroup/high/memory.low
root@ubuntu:~# cgexec -g memory:low stress-ng --vm 4 --vm-keep --vm-bytes 80% --vm-method all --seed 2025 --metrics -t 60 & cgexec -g memory:high stress-ng --vm 4 --vm-keep --vm-bytes 80% --vm-method all --seed 2025 --metrics -t 60
[1] 2011
stress-ng: info:  [2011] setting to a 1 min, 0 secs run per stressor
stress-ng: info:  [2012] setting to a 1 min, 0 secs run per stressor
stress-ng: info:  [2011] dispatching hogs: 4 vm
stress-ng: info:  [2012] dispatching hogs: 4 vm
stress-ng: metrc: [2012] stressor       bogo ops real time  usr time  sys time   bogo ops/s     bogo ops/s CPU used per       RSS Max
stress-ng: metrc: [2012]                           (secs)    (secs)    (secs)   (real time) (usr+sys time) instance (%)          (KB)
stress-ng: metrc: [2012] vm                23584     60.21      2.75     15.94       391.73        1262.07         7.76        649988
stress-ng: info:  [2012] skipped: 0
stress-ng: info:  [2012] passed: 4: vm (4)
stress-ng: info:  [2012] failed: 0
stress-ng: info:  [2012] metrics untrustworthy: 0
stress-ng: info:  [2012] successful run completed in 1 min, 0.22 secs
stress-ng: metrc: [2011] stressor       bogo ops real time  usr time  sys time   bogo ops/s     bogo ops/s CPU used per       RSS Max
stress-ng: metrc: [2011]                           (secs)    (secs)    (secs)   (real time) (usr+sys time) instance (%)          (KB)
stress-ng: metrc: [2011] vm                23584     60.22      3.06     16.19       391.63        1224.97         7.99        688836
stress-ng: info:  [2011] skipped: 0
stress-ng: info:  [2011] passed: 4: vm (4)
stress-ng: info:  [2011] failed: 0
stress-ng: info:  [2011] metrics untrustworthy: 0
stress-ng: info:  [2011] successful run completed in 1 min, 0.23 secs

As the results show, setting memory.low on the cgroup with the high-priority workload did not improve its memory performance.

However, memory.low is beneficial in many other scenarios. Perhaps extending it with eBPF support could help address a wider range of issues.

Quoted 10 lines
Quoted 6 lines

This series introduces a BPF hook that allows reporting
additional "pages over high" for specific cgroups, effectively increasing memory pressure and throttling for lower-priority workloads when higher-priority cgroups need resources.

Have you considered hooking into calculate_high_delay() instead? (That function has undergone some evolution so it'd seem like the candidate for BPFication.)

It seems that try_charge_memcg will not reach __mem_cgroup_handle_over_high if it only hook calculate_high_delay without setting memory.high.

What do you think about hooking try_charge_memcg as well, so that it ensures __mem_cgroup_handle_over_high is called?

Quoted 11 lines

...

Quoted 3 lines

3. Cgroup hierarchy management (inheritance during online/offline)

I see you're copying the program upon memcg creation. Configuration copies aren't such a good way to properly handle hierarchical behavior.
I wonder if this could follow the more generic pattern of how BPF progs are evaluated in hierarchies, see BPF_F_ALLOW_OVERRIDE and BPF_F_ALLOW_MULTI.

I will support them in the next version.

Quoted 12 lines
Quoted 4 lines

Example Results

...

Quoted 6 lines

Results show the low-priority cgroup (/sys/fs/cgroup/low) was
significantly throttled:
- High-priority cgroup: 21,033,377 bogo ops at 347,825 ops/s
- Low-priority cgroup: 11,568 bogo ops at 177 ops/s

Quoted 8 lines
Quoted 5 lines

The stress-ng process in the low-priority cgroup experienced a ~99.9% slowdown in memory operations compared to the high-priority cgroup, demonstrating effective priority enforcement through BPF-controlled memory pressure.

As a demonstrator, it'd be good to compare this with a baseline without any extra progs, e.g. show that high-prio performed better and low-prio wasn't throttled for nothing.

Thanks for your remind.
This is a test log in the test environment without any extra progs:

root@ubuntu:~# cgexec -g memory:low stress-ng --vm 4 --vm-keep --vm-bytes 80% \
--vm-method all --seed 2025 --metrics -t 60 \
& cgexec -g memory:high stress-ng --vm 4 --vm-keep --vm-bytes 80% \
--vm-method all --seed 2025 --metrics -t 60
[1] 982
stress-ng: info:  [982] setting to a 1 min, 0 secs run per stressor
stress-ng: info:  [983] setting to a 1 min, 0 secs run per stressor
stress-ng: info:  [982] dispatching hogs: 4 vm
stress-ng: info:  [983] dispatching hogs: 4 vm

stress-ng: metrc: [982] stressor bogo ops real time usr time sys time bogo ops/s bogo ops/s CPU used per RSS Max
stress-ng: metrc: [982] (secs) (secs) (secs) (real time) (usr+sys time) instance (%) (KB)
stress-ng: metrc: [982] vm 23544 60.08 2.90 15.74 391.85 1263.43 7.75 524708
stress-ng: info: [982] skipped: 0
stress-ng: info: [982] passed: 4: vm (4)
stress-ng: info: [982] failed: 0
stress-ng: info: [982] metrics untrustworthy: 0
stress-ng: info: [982] successful run completed in 1 min, 0.09 secs
stress-ng: metrc: [983] stressor bogo ops real time usr time sys time bogo ops/s bogo ops/s CPU used per RSS Max
stress-ng: metrc: [983] (secs) (secs) (secs) (real time) (usr+sys time) instance (%) (KB)
stress-ng: metrc: [983] vm 23544 60.09 3.12 15.91 391.81 1237.10 7.92 705076
stress-ng: info: [983] skipped: 0
stress-ng: info: [983] passed: 4: vm (4)
stress-ng: info: [983] failed: 0
stress-ng: info: [983] metrics untrustworthy: 0
stress-ng: info: [983] successful run completed in 1 min, 0.09 secs

Best,
Hui

Quoted 4 lines

Thanks,
Michal

maintainer
#7 lore raw
Quoted 31 lines · On Sun, Jan 04, 2026 at 09:30:46AM +0000, [email protected] wrote:

memory.low is a helpful feature, but it can struggle to effectively throttle low-priority processes that continuously access their memory.

For instance, consider the following example I ran:

root@ubuntu:~# echo $((4 * 1024 * 1024 * 1024)) > /sys/fs/cgroup/high/memory.low
root@ubuntu:~# cgexec -g memory:low stress-ng --vm 4 --vm-keep --vm-bytes 80% --vm-method all --seed 2025 --metrics -t 60 & 
               cgexec -g memory:high stress-ng --vm 4 --vm-keep --vm-bytes 80% --vm-method all --seed 2025 --metrics -t 60
[1] 2011
stress-ng: info:  [2011] setting to a 1 min, 0 secs run per stressor
stress-ng: info:  [2011] dispatching hogs: 4 vm
stress-ng: metrc: [2011] stressor       bogo ops real time  usr time  sys time   bogo ops/s     bogo ops/s CPU used per       RSS Max
stress-ng: metrc: [2011]                           (secs)    (secs)    (secs)   (real time) (usr+sys time) instance (%)          (KB)
stress-ng: metrc: [2011] vm                23584     60.22      3.06     16.19       391.63        1224.97         7.99        688836
stress-ng: info:  [2011] skipped: 0
stress-ng: info:  [2011] passed: 4: vm (4)
stress-ng: info:  [2011] failed: 0
stress-ng: info:  [2011] metrics untrustworthy: 0
stress-ng: info:  [2011] successful run completed in 1 min, 0.23 secs

stress-ng: info: [2012] setting to a 1 min, 0 secs run per stressor
stress-ng: info: [2012] dispatching hogs: 4 vm
stress-ng: metrc: [2012] stressor bogo ops real time usr time sys time bogo ops/s bogo ops/s CPU used per RSS Max
stress-ng: metrc: [2012] (secs) (secs) (secs) (real time) (usr+sys time) instance (%) (KB)
stress-ng: metrc: [2012] vm 23584 60.21 2.75 15.94 391.73 1262.07 7.76 649988
stress-ng: info: [2012] skipped: 0
stress-ng: info: [2012] passed: 4: vm (4)
stress-ng: info: [2012] failed: 0
stress-ng: info: [2012] metrics untrustworthy: 0
stress-ng: info: [2012] successful run completed in 1 min, 0.22 secs

Quoted 2 lines

As the results show, setting memory.low on the cgroup with the high-priority workload did not improve its memory performance.

It could also be that memory isn't the bottleneck here. I reckon that 80%+80% > 100% but I don't know how quickly stress-ng accesses it. I.e. actual workingset size may be lower than those 80%.

If it was accompanied with a run in one cg only, it'd help determning benchmark's baseline.

Quoted 3 lines

It seems that try_charge_memcg will not reach __mem_cgroup_handle_over_high if it only hook calculate_high_delay without setting memory.high.

That's expected, no action is needed when the current consumption is below memory.high.

Quoted 2 lines

What do you think about hooking try_charge_memcg as well, so that it ensures __mem_cgroup_handle_over_high is called?

The logic in try_charge_memcg is alredy quite involved and I think only simple concepts (that won't deviate too much as implementation changes) should be exposed to the hooks.

Quoted 2 lines

Thanks for your remind.
This is a test log in the test environment without any extra progs:

Thanks, it's similar to the example above (I assume you're after "bogo ops/s" in real time, RSS footprint isn't the observed metric), i.e. the jobs don't differ.
But it made me to review the results in your original posting (with your patch) and the high group has RSS Max of 834836 KB (so that'd be the actual workingset size for the stressor). So both of them should easily fit into the 4G of the machine, hence I guess the bottleneck is IO (you have swap right?), that's where prioritization should be applied (at least in this demostration/representative case).

HTH,
Michal