Skip to contentSkip to conversation

memcg_control_test: Disable swapping in test cgroup for v2

PATCH In review T.J. Mercier via ltp 1 participant 3 messages Mar 27, 2025 – Apr 28, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memcg_control_test: Disable swapping in test cgroup for v2

memcg_control_test_1 sets both a memory limit and a memsw/swap limit to a single page, and then asks mem_process to allocate 2 pages in an attempt to cause a memcg OOM kill. However, if swapping is enabled the first page can be swapped before the second is allocated causing neither limit to be exceeded in v2 since there is no combined memsw counter in v2. In this case no OOM kill will occur which results in a test failure. Fix this by disabling swap in the test cgroup for v2 if swap is enabled, causing both pages to always be accounted under the same counter such that memory.max causes an OOM kill.

Fixes c0b815736 ("New testcase added by Naufal.")
---
 .../memcg/control/memcg_control_test.sh            | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
testcases/kernel/controllers/memcg/control/memcg_control_test.sh +11 -3
index 68287a70c..79b3a02a9 100644
@@ -12,7 +12,6 @@ TST_NEEDS_TMPDIR=1
 
 PAGE_SIZE=$(tst_getconf PAGESIZE)
 
-TOT_MEM_LIMIT=$PAGE_SIZE
 ACTIVE_MEM_LIMIT=$PAGE_SIZE
 PROC_MEM=$((PAGE_SIZE * 2))
 
@@ -50,13 +49,22 @@ test1()
 
 	# If the kernel is built without swap, the $memsw_memory_limit file is missing
 	if [ -e "$test_dir/$memsw_memory_limit" ]; then
-		ROD echo "$TOT_MEM_LIMIT" \> "$test_dir/$memsw_memory_limit"
+		if [ "$cgroup_version" = "2" ]; then
+			# v2 does not have a combined memsw limit like v1.
+			# Disable swapping in v2 so all pages get acccounted to the non-swap counter.
+			SWAP_LIMIT=0
+		else
+			# Swapping cannot be disabled via memsw.limit_in_bytes in v1.
+			# Apply a memsw limit in v1 to capture any swapped pages
+			SWAP_LIMIT=$ACTIVE_MEM_LIMIT
+		fi
+		ROD echo "$SWAP_LIMIT" \> "$test_dir/$memsw_memory_limit"
 	fi
 
 	KILLED_CNT=0
 	test_proc_kill
 
-	if [ $PROC_MEM -gt $TOT_MEM_LIMIT ] && [ $KILLED_CNT -eq 0 ]; then
+	if [ $KILLED_CNT -eq 0 ]; then
 		tst_res TFAIL "Test #1: failed"
 	else
 		tst_res TPASS "Test #1: passed"
Signature · 5 lines
2.49.0.472.ge94155a9ec-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author Reviewed-by Li Wang on v1 1/1
#1 lore raw
Quoted 10 lines · T.J. Mercier via ltp

memcg_control_test_1 sets both a memory limit and a memsw/swap limit to a single page, and then asks mem_process to allocate 2 pages in an attempt to cause a memcg OOM kill. However, if swapping is enabled the first page can be swapped before the second is allocated causing neither limit to be exceeded in v2 since there is no combined memsw counter in v2. In this case no OOM kill will occur which results in a test failure. Fix this by disabling swap in the test cgroup for v2 if swap is enabled, causing both pages to always be accounted under the same counter such that memory.max causes an OOM kill.

Quoted 3 lines
Fixes c0b815736 ("New testcase added by Naufal.")

---

Quoted 55 lines
 .../memcg/control/memcg_control_test.sh            | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/testcases/kernel/controllers/memcg/control/memcg_control_test.sh b/testcases/kernel/controllers/memcg/control/memcg_control_test.sh index 68287a70c..79b3a02a9 100644

testcases/kernel/controllers/memcg/control/memcg_control_test.sh +0 -1
@@ -12,7 +12,6 @@ TST_NEEDS_TMPDIR=1
 
 PAGE_SIZE=$(tst_getconf PAGESIZE)
 
-TOT_MEM_LIMIT=$PAGE_SIZE
 ACTIVE_MEM_LIMIT=$PAGE_SIZE
 PROC_MEM=$((PAGE_SIZE * 2))
 
@@ -50,13 +49,22 @@ test1()
 
        # If the kernel is built without swap, the $memsw_memory_limit

file is missing

        if [ -e "$test_dir/$memsw_memory_limit" ]; then

- ROD echo "$TOT_MEM_LIMIT" \>
"$test_dir/$memsw_memory_limit"

testcases/kernel/controllers/memcg/control/memcg_control_test.sh +3 -0
+               if [ "$cgroup_version" = "2" ]; then
+                       # v2 does not have a combined memsw limit like v1.
+                       # Disable swapping in v2 so all pages get

acccounted to the non-swap counter.

testcases/kernel/controllers/memcg/control/memcg_control_test.sh +3 -0
+                       SWAP_LIMIT=0
+               else
+                       # Swapping cannot be disabled via

memsw.limit_in_bytes in v1.
+ # Apply a memsw limit in v1 to capture any swapped
pages

testcases/kernel/controllers/memcg/control/memcg_control_test.sh +4 -2
+                       SWAP_LIMIT=$ACTIVE_MEM_LIMIT
+               fi
+               ROD echo "$SWAP_LIMIT" \> "$test_dir/$memsw_memory_limit"
        fi
 
        KILLED_CNT=0
        test_proc_kill
 
-       if [ $PROC_MEM -gt $TOT_MEM_LIMIT ] && [ $KILLED_CNT -eq 0 ]; then
+       if [ $KILLED_CNT -eq 0 ]; then
                tst_res TFAIL "Test #1: failed"
        else
                tst_res TPASS "Test #1: passed"
--

2.49.0.472.ge94155a9ec-goog

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

Signature · 5 lines
Regards,
Li Wang

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

Patch merged, thanks!

Quoted 82 lines · Li Wang
Quoted 10 lines · T.J. Mercier via ltp

memcg_control_test_1 sets both a memory limit and a memsw/swap limit to a single page, and then asks mem_process to allocate 2 pages in an attempt to cause a memcg OOM kill. However, if swapping is enabled the first page can be swapped before the second is allocated causing neither limit to be exceeded in v2 since there is no combined memsw counter in v2. In this case no OOM kill will occur which results in a test failure. Fix this by disabling swap in the test cgroup for v2 if swap is enabled, causing both pages to always be accounted under the same counter such that memory.max causes an OOM kill.

Quoted 3 lines
Fixes c0b815736 ("New testcase added by Naufal.")

---

Quoted 51 lines
 .../memcg/control/memcg_control_test.sh            | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/testcases/kernel/controllers/memcg/control/memcg_control_test.sh b/testcases/kernel/controllers/memcg/control/memcg_control_test.sh index 68287a70c..79b3a02a9 100644

testcases/kernel/controllers/memcg/control/memcg_control_test.sh +0 -1
@@ -12,7 +12,6 @@ TST_NEEDS_TMPDIR=1
 
 PAGE_SIZE=$(tst_getconf PAGESIZE)
 
-TOT_MEM_LIMIT=$PAGE_SIZE
 ACTIVE_MEM_LIMIT=$PAGE_SIZE
 PROC_MEM=$((PAGE_SIZE * 2))
 
@@ -50,13 +49,22 @@ test1()
 
        # If the kernel is built without swap, the $memsw_memory_limit

file is missing

        if [ -e "$test_dir/$memsw_memory_limit" ]; then

- ROD echo "$TOT_MEM_LIMIT" \>
"$test_dir/$memsw_memory_limit"

testcases/kernel/controllers/memcg/control/memcg_control_test.sh +3 -0
+               if [ "$cgroup_version" = "2" ]; then
+                       # v2 does not have a combined memsw limit like v1.
+                       # Disable swapping in v2 so all pages get

acccounted to the non-swap counter.

testcases/kernel/controllers/memcg/control/memcg_control_test.sh +3 -0
+                       SWAP_LIMIT=0
+               else
+                       # Swapping cannot be disabled via

memsw.limit_in_bytes in v1.
+ # Apply a memsw limit in v1 to capture any
swapped pages

testcases/kernel/controllers/memcg/control/memcg_control_test.sh +4 -2
+                       SWAP_LIMIT=$ACTIVE_MEM_LIMIT
+               fi
+               ROD echo "$SWAP_LIMIT" \> "$test_dir/$memsw_memory_limit"
        fi
 
        KILLED_CNT=0
        test_proc_kill
 
-       if [ $PROC_MEM -gt $TOT_MEM_LIMIT ] && [ $KILLED_CNT -eq 0 ]; then
+       if [ $KILLED_CNT -eq 0 ]; then
                tst_res TFAIL "Test #1: failed"
        else
                tst_res TPASS "Test #1: passed"
--

2.49.0.472.ge94155a9ec-goog

Quoted 4 lines

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

--
Regards,
Li Wang

Signature · 5 lines
Regards,
Li Wang

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