Skip to contentSkip to conversation

selftests/cgroup: add PSI pressure trigger and validation tests

PATCH Superseded Tao Cui 2 participants 4 messages Jul 24 – Jul 25 1 message not in the archive

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Tao Cui

author
#0 lore raw

PATCH 1/1 selftests/cgroup: add PSI pressure trigger and validation tests

From: Tao Cui <[email protected]>

The cgroup selftest suite had no PSI coverage at all. Add test_psi.c:

- /proc/pressure/{cpu,memory,io} trigger validation: a second trigger on the same fd fails EBUSY; out-of-range window/threshold and unparseable input fail EINVAL; "some" on /proc/pressure/irq fails EINVAL (skipped when the file is absent).
- cgroup.pressure toggle: disabling hides *.pressure files and round-trips; out-of-range input fails ERANGE, non-numeric EINVAL.
- best-effort positive test: arm a sensitive memory.pressure trigger under a tight memory.max, churn memory to induce a stall and expect poll() to wake (SKIP if no stall can be induced).

The validation and toggle tests are pure userspace assertions guarding psi_trigger_create() and cgroup_pressure_write() against regressions.

---
 tools/testing/selftests/cgroup/Makefile   |   2 +
 tools/testing/selftests/cgroup/config     |   1 +
 tools/testing/selftests/cgroup/test_psi.c | 358 ++++++++++++++++++++++
 3 files changed, 361 insertions(+)
 create mode 100644 tools/testing/selftests/cgroup/test_psi.c
tools/testing/selftests/cgroup/Makefile +2 -0
index e01584c2189a..a8c69e37332a 100644
@@ -16,6 +16,7 @@ TEST_GEN_PROGS += test_kill
 TEST_GEN_PROGS += test_kmem
 TEST_GEN_PROGS += test_memcontrol
 TEST_GEN_PROGS += test_pids
+TEST_GEN_PROGS += test_psi
 TEST_GEN_PROGS += test_zswap
 
 LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h
@@ -32,4 +33,5 @@ $(OUTPUT)/test_kill: $(LIBCGROUP_O)
 $(OUTPUT)/test_kmem: $(LIBCGROUP_O)
 $(OUTPUT)/test_memcontrol: $(LIBCGROUP_O)
 $(OUTPUT)/test_pids: $(LIBCGROUP_O)
+$(OUTPUT)/test_psi: $(LIBCGROUP_O)
 $(OUTPUT)/test_zswap: $(LIBCGROUP_O)
tools/testing/selftests/cgroup/config +1 -0
index 39f979690dd3..8a3ef479e83d 100644
@@ -4,3 +4,4 @@ CONFIG_CGROUP_FREEZER=y
 CONFIG_CGROUP_SCHED=y
 CONFIG_MEMCG=y
 CONFIG_PAGE_COUNTER=y
+CONFIG_PSI=y
tools/testing/selftests/cgroup/test_psi.c new file +358 -0

358 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hi.

Quoted 8 lines · Tao Cui

From: Tao Cui <[email protected]>

The cgroup selftest suite had no PSI coverage at all. Add test_psi.c:

- /proc/pressure/{cpu,memory,io} trigger validation: a second trigger on the same fd fails EBUSY; out-of-range window/threshold and unparseable input fail EINVAL; "some" on /proc/pressure/irq fails EINVAL (skipped when the file is absent).

If you want to test each and every returned error code, I'd suggest you try adding this to LTP [1]. However, in general I don't think it's a good idea to ossify the APIs with so strict tests.
The point of these selftests is for developers to have some quick smoketests verifying that the functional concepts don't get broken while having simple tests that are easy to maintain.

Quoted 2 lines

- cgroup.pressure toggle: disabling hides *.pressure files and round-trips; out-of-range input fails ERANGE, non-numeric EINVAL.

This is a good test IMO, except for the too constrained range checking.

Quoted 3 lines

- best-effort positive test: arm a sensitive memory.pressure trigger under a tight memory.max, churn memory to induce a stall and expect poll() to wake (SKIP if no stall can be induced).

If you want to test the trigger, it might be better to pick a different resource than memory (also why you sometimes cover all resource with tests and sometimes pick only memory?), cpu pressure can be in my experience (and in theory) induced much more predictably.

Also if you stick to memory & SKIP, could be expand on the reasons behind selection of PSI_POLL_TIMEOUT_MS and the proportion of skip vs pass results?

tools/testing/selftests/cgroup/test_psi.c line 223

Quoted 13 lines
tools/testing/selftests/cgroup/test_psi.c new file +13 -0
+/* Touch every page to force allocation under a tight memory.max. */
+static void churn_memory(size_t size)
+{
+	char *p = mmap(NULL, size, PROT_READ | PROT_WRITE,
+		       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	int iter;
+
+	if (p == MAP_FAILED)
+		return;
+	for (iter = 0; iter < 5; iter++) {
+		size_t i;
+
+		for (i = 0; i < size; i += 4096)

This would ideally be sysconf(_SC_PAGE_SIZE) (see test_memcontrol.c). Generally, this looks like something to extract and share across tests, also check [2].

(But as I wrote above, memory is not a good resource to predictable behavior of PSI triggers.)

Quoted 7 lines
tools/testing/selftests/cgroup/test_psi.c new file +7 -0
+int main(int argc, char **argv)
+{
+	char root[PATH_MAX];
+	int i;
+
+	(void)argc;
+

tools/testing/selftests/cgroup/test_psi.c line 341

Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10 -0
+	ksft_print_header();
+	ksft_set_plan(ARRAY_SIZE(tests));
+
+	if (cg_find_unified_root(root, sizeof(root), NULL))
+		ksft_exit_skip("cgroup v2 isn't mounted\n");
+	if (cg_read_strstr(root, "cgroup.controllers", "memory"))
+		ksft_exit_skip("memory controller isn't available\n");
+	if (cg_read_strstr(root, "cgroup.subtree_control", "memory"))
+		if (cg_write(root, "cgroup.subtree_control", "+memory"))
+			ksft_exit_skip("failed to enable memory controller\n");

Here'd be good some skip-guards so that the test doesn't fail on CONFIG_PSI=n or CONFIG_PSI_DEFAULT_DISABLED=y kernels.

Similar for the IRQ test and CONFIG_IRQ_TIME_ACCOUNTING (which additionally would be IMO more uniform to have the IRQ test together with others and just skip the "some" part).

Thanks,
Michal

maintainer
#4 lore raw
In reply to a message not in the archive
Quoted 3 lines · On Fri, Jul 24, 2026 at 03:10:14AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] The test runner moves itself into the constrained cgroup before forking, risking being OOM killed or stalled.
- [Medium] Test hangs or experiences severe delays due to unconstrained swap usage and synchronous waitpid() without timeout.

Valid points, cf what test_memcontrol.c does with cg_run() (and swap limits).

Quoted 1 line

- [Medium] Uninitialized stack buffer buf is printed to stdout when cg_read() fails.

Here it seems to have swapped the logic and the code is correct.

Quoted 1 line

- [Low] Missing .gitignore entry for the newly added test_psi binary.

Valid point.

Michal

author
#2 lore raw
In reply to Michal Koutný

Hi Michal,

Good to hear from you, and thanks for taking the time to go through this in detail -- it's genuinely helpful.

Quoted 12 lines · Michal Koutný

Hi.

Quoted 8 lines · Tao Cui

From: Tao Cui <[email protected]>

The cgroup selftest suite had no PSI coverage at all. Add test_psi.c:

- /proc/pressure/{cpu,memory,io} trigger validation: a second trigger on the same fd fails EBUSY; out-of-range window/threshold and unparseable input fail EINVAL; "some" on /proc/pressure/irq fails EINVAL (skipped when the file is absent).

Quoted 7 lines

If you want to test each and every returned error code, I'd suggest you try adding this to LTP [1]. However, in general I don't think it's a good idea to ossify the APIs with so strict tests.
The point of these selftests is for developers to have some quick smoketests verifying that the functional concepts don't get broken while having simple tests that are easy to maintain.

Yeah, fair point -- I got a bit carried away enumerating errnos. I'll keep just the functional checks (one trigger per fd, irq has no "some").

Quoted 6 lines
Quoted 3 lines

- cgroup.pressure toggle: disabling hides *.pressure files and round-trips; out-of-range input fails ERANGE, non-numeric EINVAL.

This is a good test IMO, except for the too constrained range checking.

Noted, I'll loosen that up.

Quoted 5 lines
Quoted 4 lines

- best-effort positive test: arm a sensitive memory.pressure trigger under a tight memory.max, churn memory to induce a stall and expect poll() to wake (SKIP if no stall can be induced).

Quoted 9 lines

If you want to test the trigger, it might be better to pick a different resource than memory (also why you sometimes cover all resource with tests and sometimes pick only memory?), cpu pressure can be in my experience (and in theory) induced much more predictably.

Also if you stick to memory & SKIP, could be expand on the reasons behind selection of PSI_POLL_TIMEOUT_MS and the proportion of skip vs pass results?

That's a much better idea, thanks -- I'll switch to CPU pressure (more tasks than CPUs), which should make it deterministic and drop the SKIP, and clears up only touching memory here. I'll also rework the harness so the child enters the constrained cgroup rather than the parent, following test_memcontrol.c's cg_run(), and cap memory.swap.max so the child can't thrash and the parent's waitpid can't hang (the sashiko points there).

Quoted 20 lines
Quoted 5 lines
tools/testing/selftests/cgroup/test_psi.c new file +3 -0
+/* Touch every page to force allocation under a tight memory.max. */
+static void churn_memory(size_t size)
+{

tools/testing/selftests/cgroup/test_psi.c line 223

Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10 -0
+	char *p = mmap(NULL, size, PROT_READ | PROT_WRITE,
+		       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	int iter;
+
+	if (p == MAP_FAILED)
+		return;
+	for (iter = 0; iter < 5; iter++) {
+		size_t i;
+
+		for (i = 0; i < size; i += 4096)

This would ideally be sysconf(_SC_PAGE_SIZE) (see test_memcontrol.c). Generally, this looks like something to extract and share across tests, also check [2].

Quoted 3 lines

(But as I wrote above, memory is not a good resource to predictable behavior of PSI triggers.)

Good catch on the hardcoded 4096 -- I'll use sysconf(_SC_PAGE_SIZE), matching test_memcontrol.c, and see whether the page-touch helper is worth sharing across the cgroup tests.

Quoted 21 lines
Quoted 7 lines
tools/testing/selftests/cgroup/test_psi.c new file +7 -0
+int main(int argc, char **argv)
+{
+	char root[PATH_MAX];
+	int i;
+
+	(void)argc;
+

tools/testing/selftests/cgroup/test_psi.c line 341

Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10 -0
+	ksft_print_header();
+	ksft_set_plan(ARRAY_SIZE(tests));
+
+	if (cg_find_unified_root(root, sizeof(root), NULL))
+		ksft_exit_skip("cgroup v2 isn't mounted\n");
+	if (cg_read_strstr(root, "cgroup.controllers", "memory"))
+		ksft_exit_skip("memory controller isn't available\n");
+	if (cg_read_strstr(root, "cgroup.subtree_control", "memory"))
+		if (cg_write(root, "cgroup.subtree_control", "+memory"))
+			ksft_exit_skip("failed to enable memory controller\n");

Here'd be good some skip-guards so that the test doesn't fail on CONFIG_PSI=n or CONFIG_PSI_DEFAULT_DISABLED=y kernels.

Quoted 4 lines

Similar for the IRQ test and CONFIG_IRQ_TIME_ACCOUNTING (which additionally would be IMO more uniform to have the IRQ test together with others and just skip the "some" part).

Makes sense -- I'll add the skip-guards (PSI disabled, IRQ_TIME_ACCOUNTING) and fold irq into the per-resource loop, skipping only "some".

On the sashiko follow-up: agreed on adding test_psi to .gitignore. The buf flags are only reached on a cg_read() failure path, so harmless in practice, but I'll zero-initialize the buffers to be clean.

I'll put together a v2 with all of the above.

Thanks again!

--
Tao

Quoted 2 lines

Thanks,
Michal