All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yang Xu <xuyang2018.jy@fujitsu.com>
To: <linux-kselftest@vger.kernel.org>, <shuah@kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <naresh.kamboju@linaro.org>,
	<aleksei.kodanev@bell-sw.com>,
	Yang Xu <xuyang2018.jy@fujitsu.com>
Subject: [PATCH 3/3] selftests/zram: Adapt the situation that /dev/zram0 is being used
Date: Wed, 15 Dec 2021 17:56:11 +0800	[thread overview]
Message-ID: <1639562171-4434-3-git-send-email-xuyang2018.jy@fujitsu.com> (raw)
In-Reply-To: <1639562171-4434-1-git-send-email-xuyang2018.jy@fujitsu.com>

On some linux distributions, if it used /dev/zram0 as default by zram-generate
service, then this case will fail or report EBUSY.

To fix this, use hot_add/hot_remove interface. Also, move module check code into
zram_load from zram.sh. We can use /sys/class/zram-control to detect the
module whether existed.

After this patch, we still run case pass even /dev/zram0 is being used.

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 tools/testing/selftests/zram/zram.sh     | 16 +----
 tools/testing/selftests/zram/zram01.sh   |  2 +-
 tools/testing/selftests/zram/zram_lib.sh | 75 ++++++++++++------------
 3 files changed, 41 insertions(+), 52 deletions(-)

diff --git a/tools/testing/selftests/zram/zram.sh b/tools/testing/selftests/zram/zram.sh
index 232e958ec454..d002171fe86f 100755
--- a/tools/testing/selftests/zram/zram.sh
+++ b/tools/testing/selftests/zram/zram.sh
@@ -2,9 +2,6 @@
 # SPDX-License-Identifier: GPL-2.0
 TCID="zram.sh"
 
-# Kselftest framework requirement - SKIP code is 4.
-ksft_skip=4
-
 . ./zram_lib.sh
 
 run_zram () {
@@ -17,15 +14,4 @@ echo ""
 }
 
 check_prereqs
-
-# check zram module exists
-MODULE_PATH=/lib/modules/`uname -r`/kernel/drivers/block/zram/zram.ko
-if [ -f $MODULE_PATH ]; then
-	run_zram
-elif [ -b /dev/zram0 ]; then
-	run_zram
-else
-	echo "$TCID : No zram.ko module or /dev/zram0 device file not found"
-	echo "$TCID : CONFIG_ZRAM is not set"
-	exit $ksft_skip
-fi
+run_zram
diff --git a/tools/testing/selftests/zram/zram01.sh b/tools/testing/selftests/zram/zram01.sh
index e083a4c628a8..f87cc1ebf20f 100755
--- a/tools/testing/selftests/zram/zram01.sh
+++ b/tools/testing/selftests/zram/zram01.sh
@@ -30,7 +30,7 @@ zram_algs="lzo"
 
 zram_fill_fs()
 {
-	for i in $(seq 0 $(($dev_num - 1))); do
+	for i in $(seq $dev_start $dev_end); do
 		echo "fill zram$i..."
 		local b=0
 		while [ true ]; do
diff --git a/tools/testing/selftests/zram/zram_lib.sh b/tools/testing/selftests/zram/zram_lib.sh
index 0c49f9d1d563..ee57990c322d 100755
--- a/tools/testing/selftests/zram/zram_lib.sh
+++ b/tools/testing/selftests/zram/zram_lib.sh
@@ -8,6 +8,8 @@
 MODULE=0
 dev_makeswap=-1
 dev_mounted=-1
+dev_start=-1
+dev_end=-1
 
 # Kselftest framework requirement - SKIP code is 4.
 ksft_skip=4
@@ -29,19 +31,22 @@ zram_cleanup()
 {
 	echo "zram cleanup"
 	local i=
-	for i in $(seq 0 $dev_makeswap); do
+	for i in $(seq $dev_start $dev_makeswap); do
 		swapoff /dev/zram$i
 	done
 
-	for i in $(seq 0 $dev_mounted); do
+	for i in $(seq $dev_start $dev_mounted); do
 		umount /dev/zram$i
 	done
 
-	for i in $(seq 0 $(($dev_num - 1))); do
+	for i in $(seq $dev_start $dev_end); do
 		echo 1 > /sys/block/zram${i}/reset
-		rm -rf zram$i
+		rm -rf zram${i}
 	done
 
+	for i in $(seq $dev_start $dev_end); do
+		echo $i > /sys/class/zram-control/hot_remove
+	done
 }
 
 zram_unload()
@@ -54,32 +59,30 @@ zram_unload()
 
 zram_load()
 {
-	# check zram module exists
-	MODULE_PATH=/lib/modules/`uname -r`/kernel/drivers/block/zram/zram.ko
-	if [ -f $MODULE_PATH ]; then
-		MODULE=1
-		echo "create '$dev_num' zram device(s)"
+	echo "create '$dev_num' zram device(s)"
+
+	if [ ! -d "/sys/class/zram-control" ]; then
 		modprobe zram num_devices=$dev_num
-		if [ $? -ne 0 ]; then
-			echo "failed to insert zram module"
-			exit 1
+		if grep -q '^zram' /proc/modules; then
+			echo "ERROR: No zram.ko module"
+			echo "$TCID : CONFIG_ZRAM is not set"
+			exit $ksft_skip
 		fi
+		MODULE=1
+		dev_start=0
+		dev_end=$(($dev_num - 1))
+		echo "all zram devices(/dev/zram0~$dev_end) successfully created"
+		return
+	fi
 
-		dev_num_created=$(ls /dev/zram* | wc -w)
+	dev_start=$(ls /dev/zram* | wc -w)
+	dev_end=$(($dev_start + $dev_num - 1))
 
-		if [ "$dev_num_created" -ne "$dev_num" ]; then
-			echo "unexpected num of devices: $dev_num_created"
-			ERR_CODE=-1
-		else
-			echo "zram load module successful"
-		fi
-	elif [ -b /dev/zram0 ]; then
-		echo "/dev/zram0 device file found: OK"
-	else
-		echo "ERROR: No zram.ko module or no /dev/zram0 device found"
-		echo "$TCID : CONFIG_ZRAM is not set"
-		exit 1
-	fi
+	for i in $(seq $dev_start $dev_end); do
+		cat /sys/class/zram-control/hot_add > /dev/null
+	done
+
+	echo "all zram devices(/dev/zram$dev_start~$dev_end) successfully created"
 }
 
 zram_compress_alg()
@@ -88,13 +91,13 @@ zram_compress_alg()
 
 	local algs=$(cat /sys/block/zram0/comp_algorithm)
 	echo "supported algs: $algs"
-	local i=0
+	local i=$dev_start
 	for alg in $zram_algs; do
 		local sys_path="/sys/block/zram${i}/comp_algorithm"
 		echo "$alg" >	$sys_path || \
 			echo "FAIL can't set '$alg' to $sys_path"
 		i=$(($i + 1))
-		echo "$sys_path = '$alg' ($i/$dev_num)"
+		echo "$sys_path = '$alg'"
 	done
 
 	echo "zram set compression algorithm: OK"
@@ -103,14 +106,14 @@ zram_compress_alg()
 zram_set_disksizes()
 {
 	echo "set disk size to zram device(s)"
-	local i=0
+	local i=$dev_start
 	for ds in $zram_sizes; do
 		local sys_path="/sys/block/zram${i}/disksize"
 		echo "$ds" >	$sys_path || \
 			echo "FAIL can't set '$ds' to $sys_path"
 
 		i=$(($i + 1))
-		echo "$sys_path = '$ds' ($i/$dev_num)"
+		echo "$sys_path = '$ds'"
 	done
 
 	echo "zram set disksizes: OK"
@@ -120,14 +123,14 @@ zram_set_memlimit()
 {
 	echo "set memory limit to zram device(s)"
 
-	local i=0
+	local i=$dev_start
 	for ds in $zram_mem_limits; do
 		local sys_path="/sys/block/zram${i}/mem_limit"
 		echo "$ds" >	$sys_path || \
 			echo "FAIL can't set '$ds' to $sys_path"
 
 		i=$(($i + 1))
-		echo "$sys_path = '$ds' ($i/$dev_num)"
+		echo "$sys_path = '$ds'"
 	done
 
 	echo "zram set memory limit: OK"
@@ -137,7 +140,7 @@ zram_makeswap()
 {
 	echo "make swap with zram device(s)"
 	local i=0
-	for i in $(seq 0 $(($dev_num - 1))); do
+	for i in $(seq $dev_start $dev_end); do
 		mkswap /dev/zram$i > err.log 2>&1
 		if [ $? -ne 0 ]; then
 			cat err.log
@@ -160,7 +163,7 @@ zram_makeswap()
 zram_swapoff()
 {
 	local i=
-	for i in $(seq 0 $dev_makeswap); do
+	for i in $(seq $dev_start $dev_makeswap); do
 		swapoff /dev/zram$i > err.log 2>&1
 		if [ $? -ne 0 ]; then
 			cat err.log
@@ -174,7 +177,7 @@ zram_swapoff()
 
 zram_makefs()
 {
-	local i=0
+	local i=$dev_start
 	for fs in $zram_filesystems; do
 		# if requested fs not supported default it to ext2
 		which mkfs.$fs > /dev/null 2>&1 || fs=ext2
@@ -193,7 +196,7 @@ zram_makefs()
 zram_mount()
 {
 	local i=0
-	for i in $(seq 0 $(($dev_num - 1))); do
+	for i in $(seq $dev_start $dev_end); do
 		echo "mount /dev/zram$i"
 		mkdir zram$i
 		mount /dev/zram$i zram$i > /dev/null || \
-- 
2.23.0


  parent reply	other threads:[~2021-12-15  9:56 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-15  9:56 [PATCH 1/3] selftests/zram: Remove obsolete max_comp_streams interface Yang Xu
2021-12-15  9:56 ` [PATCH 2/3] selftests/zram01.sh: Fix compression ratio calculation Yang Xu
2022-01-25 20:37   ` Shuah Khan
2022-01-26  6:08     ` xuyang2018.jy
2021-12-15  9:56 ` Yang Xu [this message]
2022-01-25 20:40   ` [PATCH 3/3] selftests/zram: Adapt the situation that /dev/zram0 is being used Shuah Khan
2022-01-13  6:26 ` [PATCH 1/3] selftests/zram: Remove obsolete max_comp_streams interface xuyang2018.jy
2022-01-25 20:52   ` Shuah Khan
2022-01-25 20:33 ` Shuah Khan
2022-01-26  5:19   ` xuyang2018.jy
2022-01-26  7:13     ` Petr Vorel
2022-01-26 17:35       ` Shuah Khan
2022-01-26 18:24         ` Petr Vorel
2022-01-26 18:37           ` Shuah Khan
2022-01-27  9:11         ` [PATCH v2 1/3] selftests/zram: Skip max_comp_streams interface on newer kernel Yang Xu
2022-01-27  9:11           ` [PATCH v2 2/3] selftests/zram01.sh: Fix compression ratio calculation Yang Xu
2022-01-27  9:11           ` [PATCH v2 3/3] selftests/zram: Adapt the situation that /dev/zram0 is being used Yang Xu
2022-01-27 18:05           ` [PATCH v2 1/3] selftests/zram: Skip max_comp_streams interface on newer kernel Shuah Khan
2022-01-28  1:17             ` xuyang2018.jy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1639562171-4434-3-git-send-email-xuyang2018.jy@fujitsu.com \
    --to=xuyang2018.jy@fujitsu.com \
    --cc=aleksei.kodanev@bell-sw.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=naresh.kamboju@linaro.org \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.