Skip to contentSkip to conversation

OOM protection for shell tests

PATCH Superseded Andrea Cervesato 2 participants 4 messages Jul 13

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover OOM protection for shell tests

Under Li's idea, implement a OOM protection mechanism for the shell tests so we can avoid OOM for memcg stress tests.

---
Andrea Cervesato (2):
shell: add optional OOM protection
memcg_stress: survive OOM by targeting the stressors

 doc/developers/writing_tests.rst                   |  3 ++
 .../controllers/memcg/stress/memcg_stress_test.sh  |  1 +
 testcases/lib/tests/shell_oom_protection.sh        | 34 +++++++++++++
 testcases/lib/tst_test.sh                          | 57 +++++++++++++++++++++-
 4 files changed, 94 insertions(+), 1 deletion(-)

---
base-commit: ed2758122173ffd8d1e8dff6c5efc1231073ee4a
change-id: 20260713-shell_oom_protection-6221ab825220

Best regards,

Signature · 5 lines
Andrea Cervesato <[email protected]>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 1/2 shell: add optional OOM protection

From: Andrea Cervesato <[email protected]>

Add TST_OOM_PROTECTION to activate OOM protection in shell tests. When enabled, the shell harness shields itself from the OOM killer and runs the test in a child process, so it survives memory pressure and can still report results (e.g. during memcg stress tests).

---
 doc/developers/writing_tests.rst            |  3 ++
 testcases/lib/tests/shell_oom_protection.sh | 34 +++++++++++++++++
 testcases/lib/tst_test.sh                   | 57 ++++++++++++++++++++++++++++-
 3 files changed, 93 insertions(+), 1 deletion(-)
doc/developers/writing_tests.rst +3 -0
index 4db57898fcf08b83e68be996f666e91c418838fc..2d5bc294083fa2b89212714f0a6c5e5c3f22777a 100644
@@ -549,6 +549,9 @@ LTP C And Shell Test API Comparison
     * - not applicable
       - TST_FS_TYPE
 
+    * - not applicable
+      - TST_OOM_PROTECTION
+
 .. list-table::
     :header-rows: 1
 
testcases/lib/tests/shell_oom_protection.sh new file +34 -0
new file mode 100755
index 0000000000000000000000000000000000000000..a37ec51d2955e176c3b54297b8ca73ffbf2999f6
@@ -0,0 +1,34 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2026 Linux Test Project
+
+TST_TESTFUNC=test
+TST_OOM_PROTECTION=1
+
+read_oom_score_adj()
+{
+	cat "/proc/$1/oom_score_adj" 2>/dev/null
+}
+
+test()
+{
+	local self_score harness_score
+
+	self_score=$(read_oom_score_adj self)
+	harness_score=$(read_oom_score_adj "$$")
+
+	if [ "$self_score" = 0 ]; then
+		tst_res TPASS "test process has oom_score_adj reset to 0"
+	else
+		tst_res TFAIL "test process oom_score_adj is $self_score, expected 0"
+	fi
+
+	if [ "$harness_score" = -1000 ]; then
+		tst_res TPASS "shell harness is protected from OOM"
+	else
+		tst_res TCONF "shell harness OOM protection unavailable"
+	fi
+}
+
+. tst_test.sh
+tst_run
testcases/lib/tst_test.sh +56 -1

74 lines not on this page · raw message

Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] shell: add optional OOM protection

Hi Andrea,

Quoted 1 line · Andrea Cervesato

shell: add optional OOM protection

--- [PATCH 1/2] ---

testcases/lib/tests/shell_oom_protection.sh line 6

Quoted 6 lines
testcases/lib/tests/shell_oom_protection.sh new file +6 -0
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2026 Linux Test Project
+
+TST_TESTFUNC=test
+TST_OOM_PROTECTION=1

The new test is missing both the mandatory doc block and the mandatory env block. All other tests in testcases/lib/tests/ carry them (e.g. shell_loader.sh, shell_loader_cleanup.sh). Add at minimum:

# ---

  # doc
  # Verify that TST_OOM_PROTECTION shields the shell harness from the OOM
  # killer and resets the test process oom_score_adj to 0.
  # ---
  #
  # ---
  # env
  # {
  # }
  # ---
Quoted 1 line

+TST_TESTFUNC=test

Is naming the test function "test" intentional? "test" is a POSIX shell builtin and a standalone utility; shell-tests.md forbids naming functions after common shell commands. Rename to e.g. "do_test" and update TST_TESTFUNC accordingly.

Quoted 2 lines

+. tst_test.sh
+tst_run

Every other test in testcases/lib/tests/ uses the ". tst_loader.sh" / ". tst_run.sh" pattern. This test uses the pure-shell harness directly. Is that because tst_loader.sh goes through the C binary (tst_run_shell) and therefore never exercises the OOM protection that lives in tst_test.sh?

If so, can the test still be brought into the standard structure, or does this require the OOM protection to be moved into the C backend first?

Verdict - Needs revision

Pre-existing issues:
testcases/kernel/controllers/memcg/stress/memcg_stress_test.sh uses TST_TESTFUNC=test (function named after shell builtin); it also lacks doc/env blocks. These pre-date this series.

---
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
#3 lore raw

PATCH 2/2 memcg_stress: survive OOM by targeting the stressors

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

The single-cgroup subtest intentionally lets one process fault in the whole memory budget to push the system to its memory ceiling, so hitting the OOM killer is an expected part of the stress. The problem is that the OOM killer could reap the driver script, turning that expected pressure into a spurious TBROK.

Activate OOM protection so the driver script won't be the OOM victim.

Fixes 02961a7b2bb4 ("memcg_stress_test.sh: Fix reserved mem calculate")
---
 testcases/kernel/controllers/memcg/stress/memcg_stress_test.sh | 1 +
 1 file changed, 1 insertion(+)
testcases/kernel/controllers/memcg/stress/memcg_stress_test.sh +1 -0
index 47cac9af8bec970b7fb338c5469ab73d55267740..f32161f3f52a500d83cb2f6465adfb5130055dba 100755
@@ -14,6 +14,7 @@ TST_CLEANUP=cleanup
 TST_CNT=2
 TST_NEEDS_ROOT=1
 TST_NEEDS_CMDS="mount umount cat kill mkdir rmdir grep awk cut"
+TST_OOM_PROTECTION=1
 
 # Each test case runs for 900 secs when everything fine
 # therefore the default 5 mins timeout is not enough.
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp