sched/psi: Add tests for PSI interface
cover sched/psi: Add tests for PSI interface
This patch series adds four new test cases for the Linux kernel's Pressure Stall Information (PSI) interface (/proc/pressure/{cpu,memory,io}).
Currently, LTP lacks dedicated test coverage for PSI functionality, trigger parsing rules, and stall accounting. These tests ensure kernel compliance and validate PSI behavior.
Test series breakdown:
1. psi_invalid_trigger01 (1/4):
Tests trigger input validation in /proc/pressure/memory. Checks that invalid trigger strings return EINVAL and valid boundary values succeed.
2. psi_multiple_trigger01 (2/4):
Tests that only one trigger is allowed per open file description, and second triggers on the same or dup()'d file descriptor fail with EBUSY.
3. psi_proc_format01 (3/4):
Validates the file format of /proc/pressure/{cpu,memory,io}, ensuring percentages are between 0 and 100, totals never decrease, and CPU full stall reports 0.00.
4. psi_accounting01 (4/4):
Validates real stall accounting within isolated cgroups (cgroup v2) for CPU contention, memory throttling via memory.high, and O_DIRECT synchronous block I/O stalls.
Samir Mulani (4):
sched/psi: Add psi_invalid_trigger01 test
sched/psi: Add psi_multiple_trigger01 test
sched/psi: Add psi_proc_format01 test
sched/psi: Add psi_accounting01 test
runtest/sched | 5 + testcases/kernel/sched/psi/.gitignore | 4 + testcases/kernel/sched/psi/Makefile | 7 + testcases/kernel/sched/psi/psi_accounting01.c | 356 ++++++++++++++++++ .../kernel/sched/psi/psi_invalid_trigger01.c | 135 +++++++ .../kernel/sched/psi/psi_multiple_trigger01.c | 172 +++++++++ .../kernel/sched/psi/psi_proc_format01.c | 191 ++++++++++ 7 files changed, 870 insertions(+) create mode 100644 testcases/kernel/sched/psi/.gitignore create mode 100644 testcases/kernel/sched/psi/Makefile create mode 100644 testcases/kernel/sched/psi/psi_accounting01.c create mode 100644 testcases/kernel/sched/psi/psi_invalid_trigger01.c create mode 100644 testcases/kernel/sched/psi/psi_multiple_trigger01.c create mode 100644 testcases/kernel/sched/psi/psi_proc_format01.c
Signature · 5 lines
2.53.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 1/4 sched/psi: Add psi_invalid_trigger01 test
Add a test case to verify trigger input validation in /proc/pressure/memory.
In the kernel (kernel/sched/psi.c), the trigger parsing logic (psi_trigger_create) is identical and shared across /proc/pressure/{cpu,memory,io}. Therefore, testing /proc/pressure/memory validates the parser for all PSI interfaces.
The test checks that invalid trigger strings return -EINVAL:
- Invalid trigger state (not "some" or "full")
- Missing threshold or window values
- Non-numeric or negative numbers
- Zero threshold or zero window
- Threshold larger than window
- Window larger than the 10 second maximum
It also checks that valid boundary values succeed:
- Smallest threshold (1us)
- Threshold equal to window
- Valid windows of 500ms, 2s, and 10s
- Valid "full" state
Note: The kernel overwrites the last byte of the input buffer with '\0', so we write strlen() + 1 bytes to prevent losing the last character.
Requires CAP_SYS_RESOURCE for custom window sizes. Reports TCONF if CONFIG_PSI is not enabled or disabled with psi=0.
This patch also adds the testcases/kernel/sched/psi directory, Makefile, and .gitignore.
--- runtest/sched | 2 + testcases/kernel/sched/psi/.gitignore | 1 + testcases/kernel/sched/psi/Makefile | 7 + .../kernel/sched/psi/psi_invalid_trigger01.c | 135 ++++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 testcases/kernel/sched/psi/.gitignore create mode 100644 testcases/kernel/sched/psi/Makefile create mode 100644 testcases/kernel/sched/psi/psi_invalid_trigger01.c
runtest/sched +2 -0
@@ -20,3 +20,5 @@ sched_stress sched_stress.sh autogroup01 autogroup01 sched_football sched_football + +psi_invalid_trigger01 psi_invalid_trigger01
testcases/kernel/sched/psi/.gitignore new file +1 -0
@@ -0,0 +1 @@ +/psi_invalid_trigger01
testcases/kernel/sched/psi/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk + +include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/sched/psi/psi_invalid_trigger01.c new file +135 -0
135 lines not on this page · raw message
Signature · 5 lines
2.53.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] sched/psi: Add psi_invalid_trigger01 test
Hi Samir,
Quoted 1 line · Samir Mulani
sched/psi: Add
psi_invalid_trigger01test
--- [PATCH 1/4] ---
testcases/kernel/sched/psi/psi_invalid_trigger01.c line 132
Quoted 2 lines
testcases/kernel/sched/psi/psi_invalid_trigger01.c new file +2
-0+ .needs_kconfigs = (const char *const[]) { + "CONFIG_PSI=y",
Medium: PSI statistics exist in Linux 4.20-5.1, but trigger writes were introduced in 5.2, so CONFIG_PSI and the setup read do not establish monitor support. Add .min_kver = "5.2" or equivalent runtime detection to avoid failing on supported kernels without this interface.
testcases/kernel/sched/psi/psi_invalid_trigger01.c line 90
Quoted 4 lines
testcases/kernel/sched/psi/psi_invalid_trigger01.c new file +4
-0+TEST (write (fd, tc->input, len)); + +if (TST_RET == (long )len) +tst_res (TPASS ,"%s: trigger accepted" , tc->desc);
Low: Use TST_EXP_VAL(write(fd, tc->input, len), (long)len, "%s", tc->desc) instead of the manual return-value reporting block.
testcases/kernel/sched/psi/psi_invalid_trigger01.c line 128
Quoted 3 lines
testcases/kernel/sched/psi/psi_invalid_trigger01.c new file +3
-0+ .needs_root = 1, + .caps = (struct tst_cap []) { +TST_CAP (TST_CAP_REQ ,CAP_SYS_RESOURCE ),
Low: Document the root/CAP_SYS_RESOURCE requirement for custom trigger windows in the high-level test description, not only the commit message.
testcases/kernel/sched/psi/Makefile
Quoted 3 lines
testcases/kernel/sched/psi/Makefile +2
-0+# SPDX-License-Identifier: GPL-2.0-or-later +
Low: Add the required copyright line after the SPDX tag.
--- [PATCH 2/4] ---
testcases/kernel/sched/psi/Makefile
Quoted 4 lines
testcases/kernel/sched/psi/Makefile +4
-0+static struct tst_test test = { + .tcnt = ARRAY_SIZE(tests), + .test = run, + .setup = setup,
Medium: This test also needs a monitor-availability gate for PSI-enabled 4.20-5.1 kernels, where setup succeeds but trigger writes are unsupported. Use .min_kver = "5.2" or equivalent runtime detection.
testcases/kernel/sched/psi/Makefile
Quoted 2 lines
testcases/kernel/sched/psi/Makefile +2
-0+ TST_EXP_FAIL2(WRITE_TRIG(fd, TRIG_B), EBUSY, + "second write to the same fd");
Medium: This checks the behavior introduced by kernel fix a06247c6804f ("psi: Fix uaf issue when psi trigger is destroyed while being polled"). Add a linux-git regression tag for that fix and explain the relationship in the commit message; do not skip kernels merely because they lack the fix.
testcases/kernel/sched/psi/Makefile
Quoted 4 lines
testcases/kernel/sched/psi/Makefile +4
-0+static void (*const tests[])(void) = { + first_write_succeeds, + second_write_ebusy, + dup_fd_shares_trigger,
Low: Use the required struct tcase table and common .test handler, parameterizing trigger strings, descriptor sharing and expected results instead of dispatching separate per-case functions.
testcases/kernel/sched/psi/Makefile
Quoted 1 line
testcases/kernel/sched/psi/Makefile +1
-0+ .needs_root = 1,
Low: Explain the writable PSI interface and custom-window privilege requirements in the high-level test description.
--- [PATCH 3/4] ---
testcases/kernel/sched/psi/Makefile
Quoted 5 lines
testcases/kernel/sched/psi/Makefile +4
-0+#define PSI_LINE "%7s avg10=%lf avg60=%lf avg300=%lf total=%llu" + SAFE_FCLOSE(fp); + + if (ret != 10) {
Medium: Supplying total=-1 and an unexpected third line still produces seven passing results: %llu accepts the sign and the parser ignores everything after the tenth conversion. Validate unsigned decimal totals, consume each expected line completely, and reject extra records.
testcases/kernel/sched/psi/Makefile
Quoted 6 lines
testcases/kernel/sched/psi/Makefile +6
-0+ if (n < ARRAY_SIZE(paths)) + verify_format(n); + else if (n < 2 * ARRAY_SIZE(paths)) + verify_monotonic(n - ARRAY_SIZE(paths)); + else + verify_cpu_full_zero();
Low: Replace the index-range dispatch with a struct tcase table describing each path and check, using the common .test callback.
--- [PATCH 4/4] ---
testcases/kernel/sched/psi/Makefile
Quoted 7 lines
testcases/kernel/sched/psi/Makefile +7
-0+ tst_clock_gettime(CLOCK_MONOTONIC, &start); + + if (pwrite(fd, io_buf, IO_SIZE, 0) != IO_SIZE) + tst_brk(TBROK | TERRNO, "pwrite() failed"); + + tst_clock_gettime(CLOCK_MONOTONIC, &end); + *io_wait_us += tst_timespec_diff_us(end, start);
High: This measures syscall wall time, including CPU work and preemption, not time blocked on I/O. An O_DIRECT shmem reproduction measured 1995579us with zero voluntary context switches, yet the test would require 498894us of I/O pressure; LTP_DEV_FS_TYPE=tmpfs reaches this no-storage path. Measure actual blocked-I/O time independently or use a controlled blocking-I/O workload before deriving the expected PSI delta.
Quoted 1 line
+static char io_buf[IO_SIZE] __attribute__((aligned(4096)));
Medium: A fixed 4KiB alignment is insufficient on supported 64KiB-page systems with 64KiB-sector Btrfs, which falls back to buffered writes for this misalignment and invalidates the workload. Allocate a buffer using the runtime DIO alignment or a suitable runtime page alignment; opening the file successfully does not validate buffer alignment.
testcases/kernel/sched/psi/Makefile
Quoted 6 lines
testcases/kernel/sched/psi/Makefile +6
-0+ for (i = 0; i < 2; i++) { + if (!SAFE_FORK()) { + join_cgroup(); + pin_to_cpu(pin_cpu); + + tst_timer_start(CLOCK_MONOTONIC);
Medium: If the parent is delayed between forks for two seconds, the first worker can finish before the second joins, producing serial workloads and a false failure on an otherwise idle CPU. Use checkpoints to wait until both workers have joined and pinned themselves, then release both before starting their workload timers.
testcases/kernel/sched/psi/Makefile
Quoted 2 lines
testcases/kernel/sched/psi/Makefile +2
-0+ if (pwrite(fd, io_buf, IO_SIZE, 0) != IO_SIZE) + tst_brk(TBROK | TERRNO, "pwrite() failed");
Low: Use SAFE_PWRITE(SAFE_WRITE_ALL, fd, io_buf, IO_SIZE, 0) for this workload operation; the subject under test is PSI accounting, not pwrite.
testcases/kernel/sched/psi/Makefile
Quoted 4 lines
testcases/kernel/sched/psi/Makefile +4
-0+static void (*const tests[])(void) = { + verify_cpu, + verify_memory, + verify_io,
Low: Use a struct tcase table and common .test handler rather than dispatching separate per-case functions.
testcases/kernel/sched/psi/Makefile
Quoted 1 line
testcases/kernel/sched/psi/Makefile +1
-0+ .needs_root = 1,
Low: Document the cgroup/controller and test-filesystem setup as the reason for requiring root in the high-level description.
testcases/kernel/sched/psi/.gitignore
Quoted 3 lines
testcases/kernel/sched/psi/.gitignore +1
-0/psi_proc_format01 +/psi_accounting01
Low: Keep the entries sorted; /psi_accounting01 belongs first.
Verdict - Needs revision
---
Note:
The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 2/4 sched/psi: Add psi_multiple_trigger01 test
In reply to Samir MulaniAdd a test case to check that only one PSI trigger can be registered on a single open file description.
The test verifies that:
- Writing the first trigger to an open pressure file succeeds.
- Writing a second trigger to the same file descriptor fails with -EBUSY.
- Writing a second trigger through a dup() descriptor also fails with -EBUSY.
- Opening two separate file descriptors allows registering two independent triggers without conflict.
The test requires CAP_SYS_RESOURCE and reports TCONF if PSI is disabled.
--- runtest/sched | 1 + testcases/kernel/sched/psi/.gitignore | 1 + .../kernel/sched/psi/psi_multiple_trigger01.c | 172 ++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 testcases/kernel/sched/psi/psi_multiple_trigger01.c
runtest/sched +1 -0
@@ -22,3 +22,4 @@ autogroup01 autogroup01 sched_football sched_football psi_invalid_trigger01 psi_invalid_trigger01 +psi_multiple_trigger01 psi_multiple_trigger01
testcases/kernel/sched/psi/.gitignore +1 -0
@@ -1 +1,2 @@ /psi_invalid_trigger01 +/psi_multiple_trigger01
testcases/kernel/sched/psi/psi_multiple_trigger01.c new file +172 -0
172 lines not on this page · raw message
Signature · 5 lines
2.53.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 3/4 sched/psi: Add psi_proc_format01 test
In reply to Samir MulaniAdd a test case to verify the format and values in /proc/pressure/{cpu,memory,io}.
The test checks that:
- Each file has valid "some" and "full" lines with avg10, avg60, avg300, and total fields.
- Averages are valid percentages between 0.00 and 100.00.
- Total stall times do not decrease over time.
- /proc/pressure/cpu reports "full" lines as 0.00 (per Linux 5.13+ behavior).
This test does not generate heavy system load and only validates interface parsing. It reports TCONF if PSI is disabled.
--- runtest/sched | 1 + testcases/kernel/sched/psi/.gitignore | 1 + .../kernel/sched/psi/psi_proc_format01.c | 191 ++++++++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 testcases/kernel/sched/psi/psi_proc_format01.c
runtest/sched +1 -0
@@ -23,3 +23,4 @@ sched_football sched_football psi_invalid_trigger01 psi_invalid_trigger01 psi_multiple_trigger01 psi_multiple_trigger01 +psi_proc_format01 psi_proc_format01
testcases/kernel/sched/psi/.gitignore +1 -0
@@ -1,2 +1,3 @@ /psi_invalid_trigger01 /psi_multiple_trigger01 +/psi_proc_format01
testcases/kernel/sched/psi/psi_proc_format01.c new file +191 -0
191 lines not on this page · raw message
Signature · 5 lines
2.53.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 4/4 sched/psi: Add psi_accounting01 test
In reply to Samir MulaniAdd a test case to verify that PSI correctly records stall time when tasks experience CPU, memory, and I/O pressure.
The test creates a cgroup v2 to isolate the workload and checks that:
- Running two CPU-heavy tasks on a single CPU increases cpu "some" total.
- Touching memory above memory.high triggers reclaim and increases memory "some" total.
- Doing synchronous O_DIRECT writes to a block device increases io "some" total.
If the system cannot create enough memory or I/O pressure (for example, on very fast storage or systems without a test block device), the test cleanly reports TCONF instead of failing.
The test requires cgroup v2 with memory controller enabled.
--- runtest/sched | 1 + testcases/kernel/sched/psi/.gitignore | 1 + testcases/kernel/sched/psi/psi_accounting01.c | 356 ++++++++++++++++++ 3 files changed, 358 insertions(+) create mode 100644 testcases/kernel/sched/psi/psi_accounting01.c
runtest/sched +1 -0
@@ -24,3 +24,4 @@ sched_football sched_football psi_invalid_trigger01 psi_invalid_trigger01 psi_multiple_trigger01 psi_multiple_trigger01 psi_proc_format01 psi_proc_format01 +psi_accounting01 psi_accounting01
testcases/kernel/sched/psi/.gitignore +1 -0
@@ -1,3 +1,4 @@ /psi_invalid_trigger01 /psi_multiple_trigger01 /psi_proc_format01 +/psi_accounting01
testcases/kernel/sched/psi/psi_accounting01.c new file +356 -0
356 lines not on this page · raw message
Signature · 5 lines
2.53.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp