Commit 01dabed2 authored by Yang Xu's avatar Yang Xu Committed by Shuah Khan

selftests/zram: Adapt the situation that /dev/zram0 is being used

If zram-generator package is installed and works, then we can not remove
zram module because zram swap is being used. This case needs a clean zram
environment, change this test by using hot_add/hot_remove interface. So
even zram device is being used, we still can add zram device and remove
them in cleanup.

The two interface was introduced since kernel commit 6566d1a3("zram:
add dynamic device add/remove functionality") in v4.2-rc1. If kernel
supports these two interface, we use hot_add/hot_remove to slove this
problem, if not, just check whether zram is being used or built in, then
skip it on old kernel.
Signed-off-by: default avatarYang Xu <xuyang2018.jy@fujitsu.com>
Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent d18da7ec
...@@ -2,9 +2,6 @@ ...@@ -2,9 +2,6 @@
# SPDX-License-Identifier: GPL-2.0 # SPDX-License-Identifier: GPL-2.0
TCID="zram.sh" TCID="zram.sh"
# Kselftest framework requirement - SKIP code is 4.
ksft_skip=4
. ./zram_lib.sh . ./zram_lib.sh
run_zram () { run_zram () {
...@@ -18,14 +15,4 @@ echo "" ...@@ -18,14 +15,4 @@ echo ""
check_prereqs check_prereqs
# check zram module exists run_zram
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
...@@ -33,7 +33,7 @@ zram_algs="lzo" ...@@ -33,7 +33,7 @@ zram_algs="lzo"
zram_fill_fs() 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..." echo "fill zram$i..."
local b=0 local b=0
while [ true ]; do while [ true ]; do
...@@ -67,7 +67,6 @@ zram_mount ...@@ -67,7 +67,6 @@ zram_mount
zram_fill_fs zram_fill_fs
zram_cleanup zram_cleanup
zram_unload
if [ $ERR_CODE -ne 0 ]; then if [ $ERR_CODE -ne 0 ]; then
echo "$TCID : [FAIL]" echo "$TCID : [FAIL]"
......
...@@ -36,7 +36,6 @@ zram_set_memlimit ...@@ -36,7 +36,6 @@ zram_set_memlimit
zram_makeswap zram_makeswap
zram_swapoff zram_swapoff
zram_cleanup zram_cleanup
zram_unload
if [ $ERR_CODE -ne 0 ]; then if [ $ERR_CODE -ne 0 ]; then
echo "$TCID : [FAIL]" echo "$TCID : [FAIL]"
......
...@@ -5,10 +5,12 @@ ...@@ -5,10 +5,12 @@
# Author: Alexey Kodanev <alexey.kodanev@oracle.com> # Author: Alexey Kodanev <alexey.kodanev@oracle.com>
# Modified: Naresh Kamboju <naresh.kamboju@linaro.org> # Modified: Naresh Kamboju <naresh.kamboju@linaro.org>
MODULE=0
dev_makeswap=-1 dev_makeswap=-1
dev_mounted=-1 dev_mounted=-1
dev_start=0
dev_end=-1
module_load=-1
sys_control=-1
# Kselftest framework requirement - SKIP code is 4. # Kselftest framework requirement - SKIP code is 4.
ksft_skip=4 ksft_skip=4
kernel_version=`uname -r | cut -d'.' -f1,2` kernel_version=`uname -r | cut -d'.' -f1,2`
...@@ -46,57 +48,72 @@ zram_cleanup() ...@@ -46,57 +48,72 @@ zram_cleanup()
{ {
echo "zram cleanup" echo "zram cleanup"
local i= local i=
for i in $(seq 0 $dev_makeswap); do for i in $(seq $dev_start $dev_makeswap); do
swapoff /dev/zram$i swapoff /dev/zram$i
done done
for i in $(seq 0 $dev_mounted); do for i in $(seq $dev_start $dev_mounted); do
umount /dev/zram$i umount /dev/zram$i
done 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 echo 1 > /sys/block/zram${i}/reset
rm -rf zram$i rm -rf zram$i
done done
} if [ $sys_control -eq 1 ]; then
for i in $(seq $dev_start $dev_end); do
echo $i > /sys/class/zram-control/hot_remove
done
fi
zram_unload() if [ $module_load -eq 1 ]; then
{
if [ $MODULE -ne 0 ] ; then
echo "zram rmmod zram"
rmmod zram > /dev/null 2>&1 rmmod zram > /dev/null 2>&1
fi fi
} }
zram_load() zram_load()
{ {
# check zram module exists echo "create '$dev_num' zram device(s)"
MODULE_PATH=/lib/modules/`uname -r`/kernel/drivers/block/zram/zram.ko
if [ -f $MODULE_PATH ]; then # zram module loaded, new kernel
MODULE=1 if [ -d "/sys/class/zram-control" ]; then
echo "create '$dev_num' zram device(s)" echo "zram modules already loaded, kernel supports" \
modprobe zram num_devices=$dev_num "zram-control interface"
if [ $? -ne 0 ]; then dev_start=$(ls /dev/zram* | wc -w)
echo "failed to insert zram module" dev_end=$(($dev_start + $dev_num - 1))
exit 1 sys_control=1
fi
for i in $(seq $dev_start $dev_end); do
dev_num_created=$(ls /dev/zram* | wc -w) cat /sys/class/zram-control/hot_add > /dev/null
done
echo "all zram devices (/dev/zram$dev_start~$dev_end" \
"successfully created"
return 0
fi
if [ "$dev_num_created" -ne "$dev_num" ]; then # detect old kernel or built-in
echo "unexpected num of devices: $dev_num_created" modprobe zram num_devices=$dev_num
ERR_CODE=-1 if [ ! -d "/sys/class/zram-control" ]; then
if grep -q '^zram' /proc/modules; then
rmmod zram > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "zram module is being used on old kernel" \
"without zram-control interface"
exit $ksft_skip
fi
else else
echo "zram load module successful" echo "test needs CONFIG_ZRAM=m on old kernel without" \
"zram-control interface"
exit $ksft_skip
fi fi
elif [ -b /dev/zram0 ]; then modprobe zram num_devices=$dev_num
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 fi
module_load=1
dev_end=$(($dev_num - 1))
echo "all zram devices (/dev/zram0~$dev_end) successfully created"
} }
zram_max_streams() zram_max_streams()
...@@ -110,7 +127,7 @@ zram_max_streams() ...@@ -110,7 +127,7 @@ zram_max_streams()
return 0 return 0
fi fi
local i=0 local i=$dev_start
for max_s in $zram_max_streams; do for max_s in $zram_max_streams; do
local sys_path="/sys/block/zram${i}/max_comp_streams" local sys_path="/sys/block/zram${i}/max_comp_streams"
echo $max_s > $sys_path || \ echo $max_s > $sys_path || \
...@@ -122,7 +139,7 @@ zram_max_streams() ...@@ -122,7 +139,7 @@ zram_max_streams()
echo "FAIL can't set max_streams '$max_s', get $max_stream" echo "FAIL can't set max_streams '$max_s', get $max_stream"
i=$(($i + 1)) i=$(($i + 1))
echo "$sys_path = '$max_streams' ($i/$dev_num)" echo "$sys_path = '$max_streams'"
done done
echo "zram max streams: OK" echo "zram max streams: OK"
...@@ -132,15 +149,16 @@ zram_compress_alg() ...@@ -132,15 +149,16 @@ zram_compress_alg()
{ {
echo "test that we can set compression algorithm" echo "test that we can set compression algorithm"
local algs=$(cat /sys/block/zram0/comp_algorithm) local i=$dev_start
local algs=$(cat /sys/block/zram${i}/comp_algorithm)
echo "supported algs: $algs" echo "supported algs: $algs"
local i=0
for alg in $zram_algs; do for alg in $zram_algs; do
local sys_path="/sys/block/zram${i}/comp_algorithm" local sys_path="/sys/block/zram${i}/comp_algorithm"
echo "$alg" > $sys_path || \ echo "$alg" > $sys_path || \
echo "FAIL can't set '$alg' to $sys_path" echo "FAIL can't set '$alg' to $sys_path"
i=$(($i + 1)) i=$(($i + 1))
echo "$sys_path = '$alg' ($i/$dev_num)" echo "$sys_path = '$alg'"
done done
echo "zram set compression algorithm: OK" echo "zram set compression algorithm: OK"
...@@ -149,14 +167,14 @@ zram_compress_alg() ...@@ -149,14 +167,14 @@ zram_compress_alg()
zram_set_disksizes() zram_set_disksizes()
{ {
echo "set disk size to zram device(s)" echo "set disk size to zram device(s)"
local i=0 local i=$dev_start
for ds in $zram_sizes; do for ds in $zram_sizes; do
local sys_path="/sys/block/zram${i}/disksize" local sys_path="/sys/block/zram${i}/disksize"
echo "$ds" > $sys_path || \ echo "$ds" > $sys_path || \
echo "FAIL can't set '$ds' to $sys_path" echo "FAIL can't set '$ds' to $sys_path"
i=$(($i + 1)) i=$(($i + 1))
echo "$sys_path = '$ds' ($i/$dev_num)" echo "$sys_path = '$ds'"
done done
echo "zram set disksizes: OK" echo "zram set disksizes: OK"
...@@ -166,14 +184,14 @@ zram_set_memlimit() ...@@ -166,14 +184,14 @@ zram_set_memlimit()
{ {
echo "set memory limit to zram device(s)" echo "set memory limit to zram device(s)"
local i=0 local i=$dev_start
for ds in $zram_mem_limits; do for ds in $zram_mem_limits; do
local sys_path="/sys/block/zram${i}/mem_limit" local sys_path="/sys/block/zram${i}/mem_limit"
echo "$ds" > $sys_path || \ echo "$ds" > $sys_path || \
echo "FAIL can't set '$ds' to $sys_path" echo "FAIL can't set '$ds' to $sys_path"
i=$(($i + 1)) i=$(($i + 1))
echo "$sys_path = '$ds' ($i/$dev_num)" echo "$sys_path = '$ds'"
done done
echo "zram set memory limit: OK" echo "zram set memory limit: OK"
...@@ -182,8 +200,8 @@ zram_set_memlimit() ...@@ -182,8 +200,8 @@ zram_set_memlimit()
zram_makeswap() zram_makeswap()
{ {
echo "make swap with zram device(s)" echo "make swap with zram device(s)"
local i=0 local i=$dev_start
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 mkswap /dev/zram$i > err.log 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
cat err.log cat err.log
...@@ -206,7 +224,7 @@ zram_makeswap() ...@@ -206,7 +224,7 @@ zram_makeswap()
zram_swapoff() zram_swapoff()
{ {
local i= local i=
for i in $(seq 0 $dev_makeswap); do for i in $(seq $dev_start $dev_end); do
swapoff /dev/zram$i > err.log 2>&1 swapoff /dev/zram$i > err.log 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
cat err.log cat err.log
...@@ -220,7 +238,7 @@ zram_swapoff() ...@@ -220,7 +238,7 @@ zram_swapoff()
zram_makefs() zram_makefs()
{ {
local i=0 local i=$dev_start
for fs in $zram_filesystems; do for fs in $zram_filesystems; do
# if requested fs not supported default it to ext2 # if requested fs not supported default it to ext2
which mkfs.$fs > /dev/null 2>&1 || fs=ext2 which mkfs.$fs > /dev/null 2>&1 || fs=ext2
...@@ -239,7 +257,7 @@ zram_makefs() ...@@ -239,7 +257,7 @@ zram_makefs()
zram_mount() zram_mount()
{ {
local i=0 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" echo "mount /dev/zram$i"
mkdir zram$i mkdir zram$i
mount /dev/zram$i zram$i > /dev/null || \ mount /dev/zram$i zram$i > /dev/null || \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment