[LTP] [PATCH] controllers/pids.sh: get available for cgroup v2

Masayoshi Mizuma msys.mizuma@gmail.com
Sat Nov 13 05:17:06 CET 2021


From: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>

controllers/pids/pids.sh doesn't work in the cgroup v2 environment because
some interfaces of cgroup v2 are different from cgroup v1.

The test itself is useful for cgroup v2 as well, so let's get it
available for cgroup v2.

The test starts with cgroup v2 if it's mounted already. On some systems,
like as Fedora 35 and CentOS Stream 9, systemd mounts cgroup v2 as the
default while system booting.

Signed-off-by: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
---
 testcases/kernel/controllers/pids/pids.sh | 68 +++++++++++++++++------
 1 file changed, 52 insertions(+), 16 deletions(-)

diff --git a/testcases/kernel/controllers/pids/pids.sh b/testcases/kernel/controllers/pids/pids.sh
index afcf0a283..a3d644eff 100755
--- a/testcases/kernel/controllers/pids/pids.sh
+++ b/testcases/kernel/controllers/pids/pids.sh
@@ -47,7 +47,21 @@ cleanup()
 	fi
 }
 
-setup()
+setup_cgroupv2()
+{
+	mount_point=$(grep -w cgroup2 /proc/mounts | cut -f 2 | cut -d " " -f2)
+	if ! grep -q pids "$mount_point"/cgroup.controllers; then
+		tst_res TINFO "pids not supported on cgroup v2."
+		return
+	fi
+
+	testpath="$mount_point/ltp_pids_$caseno"
+	ROD mkdir -p "$testpath"
+	task_list="cgroup.procs"
+	cgroup_v="v2"
+}
+
+setup_cgroupv1()
 {
 	exist=`grep -w pids /proc/cgroups | cut -f1`;
 	if [ "$exist" = "" ]; then
@@ -68,6 +82,22 @@ setup()
 		ROD mount -t cgroup -o pids none $mount_point
 	fi
 	ROD mkdir -p $testpath
+	task_list="tasks"
+	cgroup_v="v1"
+}
+
+setup()
+{
+	# If cgroup2 is mounted already, then let's
+	# try to start with cgroup v2.
+	if grep -q cgroup2 /proc/mounts; then
+		setup_cgroupv2
+	fi
+	if [ -z "$cgroup_v" ]; then
+		setup_cgroupv1
+	fi
+
+	tst_res TINFO "test starts with cgroup $cgroup_v"
 }
 
 start_pids_tasks2()
@@ -81,10 +111,10 @@ start_pids_tasks2_path()
 	nb=$2
 	for i in `seq 1 $nb`; do
 		pids_task2 &
-		echo $! > $path/tasks
+		echo $! > "$path/$task_list"
 	done
 
-	if [ $(cat "$path/tasks" | wc -l) -ne $nb ]; then
+	if [ $(wc -l < "$path/$task_list") -ne "$nb" ]; then
 		tst_brk TBROK "failed to attach process"
 	fi
 }
@@ -99,7 +129,7 @@ stop_pids_tasks_path()
 	local i
 	path=$1
 
-	for i in `cat $path/tasks`; do
+	for i in $(cat "$path/$task_list"); do
 		ROD kill -9 $i
 		wait $i
 	done
@@ -110,7 +140,7 @@ case1()
 	start_pids_tasks2 $max
 
 	# should return 0 because there is no limit
-	pids_task1 "$testpath/tasks"
+	pids_task1 "$testpath/$task_list"
 	ret=$?
 
 	if [ "$ret" -eq "2" ]; then
@@ -133,7 +163,7 @@ case2()
 	start_pids_tasks2 $tmp
 
 	# should return 2 because the limit of pids is reached
-	pids_task1 "$testpath/tasks"
+	pids_task1 "$testpath/$task_list"
 	ret=$?
 
 	if [ "$ret" -eq "2" ]; then
@@ -155,7 +185,7 @@ case3()
 
 	start_pids_tasks2 $max
 
-	pids_task1 "$testpath/tasks"
+	pids_task1 "$testpath/$task_list"
 	ret=$?
 
 	if [ "$ret" -eq "2" ]; then
@@ -201,7 +231,7 @@ case6()
 	lim=$((max - 1))
 	ROD echo $lim \> $testpath/pids.max
 
-	pids_task1 "$testpath/tasks"
+	pids_task1 "$testpath/$task_list"
 	ret=$?
 
 	if [ "$ret" -eq "2" ]; then
@@ -232,7 +262,7 @@ case7()
 		start_pids_tasks2_path $testpath/child$i $lim
 	done
 
-	pids_task1 "$testpath/tasks"
+	pids_task1 "$testpath/$task_list"
 	ret=$?
 
 	if [ "$ret" -eq "2" ]; then
@@ -244,7 +274,7 @@ case7()
 	fi
 
 	for i in `seq 1 $subcgroup_num`; do
-		pids_task1 "$testpath/child$i/tasks"
+		pids_task1 "$testpath/child$i/$task_list"
 		ret=$?
 
 		if [ "$ret" -eq "2" ]; then
@@ -268,6 +298,9 @@ case8()
 {
 	tst_res TINFO "set child cgroup limit smaller than its parent limit"
 	ROD echo $max \> $testpath/pids.max
+	if [ "$cgroup_v" = "v2" ]; then
+		ROD echo +pids \> "$testpath"/cgroup.subtree_control
+	fi
 	mkdir $testpath/child
 
 	lim=$((max - 1))
@@ -275,7 +308,7 @@ case8()
 	tmp=$((max - 2))
 	start_pids_tasks2_path $testpath/child $tmp
 
-	pids_task1 "$testpath/child/tasks"
+	pids_task1 "$testpath/child/$task_list"
 	ret=$?
 
 	if [ "$ret" -eq "2" ]; then
@@ -295,16 +328,19 @@ case9()
 	tst_res TINFO "migrate cgroup"
 	lim=$((max - 1))
 
+	if [ "$cgroup_v" = "v2" ]; then
+		ROD echo +pids \> "$testpath"/cgroup.subtree_control
+	fi
 	for i in 1 2; do
 		mkdir $testpath/child$i
 		ROD echo $max \> $testpath/child$i/pids.max
 		start_pids_tasks2_path $testpath/child$i $lim
 	done
 
-	pid=`head -n 1 $testpath/child1/tasks`;
-	ROD echo $pid \> $testpath/child2/tasks
+	pid=`head -n 1 "$testpath/child1/$task_list"`;
+	ROD echo $pid \> "$testpath/child2/$task_list"
 
-	if grep -q "$pid" "$testpath/child2/tasks"; then
+	if grep -q "$pid" "$testpath/child2/$task_list"; then
 		tst_res TPASS "migrate pid $pid from cgroup1 to cgroup2 as expected"
 	else
 		tst_res TPASS "migrate pid $pid from cgroup1 to cgroup2 failed"
@@ -322,7 +358,7 @@ case9()
 		tst_res TFAIL "migrate child2 cgroup failed"
 	fi
 
-	pids_task1 "$testpath/child1/tasks"
+	pids_task1 "$testpath/child1/$task_list"
 	ret=$?
 
 	if [ "$ret" -eq "2" ]; then
@@ -333,7 +369,7 @@ case9()
 		tst_res TBROK "child1 pids_task1 failed"
 	fi
 
-	pids_task1 "$testpath/child2/tasks"
+	pids_task1 "$testpath/child2/$task_list"
 	ret=$?
 
 	if [ "$ret" -eq "2" ]; then
-- 
2.27.0



More information about the ltp mailing list