Skip to content

Commit

Permalink
zram/zram_lib.sh: adapt the situation that zram device is being used
Browse files Browse the repository at this point in the history
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[1]. 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 6566d1a32("zram: add dynamic
device add/remove functionality")[2] 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.

Also, zram01,02 case are adjuested to adapt the situation that CONFIG_ZRAM=y and can
run zram01,02 simultaneously on new kernel.

[1]https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html#add-remove-zram-devices
[2]https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6566d1a32bf7

Fixes: #888
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
  • Loading branch information
xuyang0410 committed Dec 22, 2021
1 parent 879093b commit caed783
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
6 changes: 3 additions & 3 deletions testcases/kernel/device-drivers/zram/zram01.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ setup()

zram_makefs()
{
local i=0
local i=$dev_start
local fs

for fs in $zram_filesystems; do
Expand All @@ -90,7 +90,7 @@ zram_mount()
{
local i=0

for i in $(seq 0 $(($dev_num - 1))); do
for i in $(seq $dev_start $dev_end); do
tst_res TINFO "mount /dev/zram$i"
mkdir zram$i
ROD mount /dev/zram$i zram$i
Expand All @@ -102,7 +102,7 @@ zram_mount()

zram_fill_fs()
{
for i in $(seq 0 $(($dev_num - 1))); do
for i in $(seq $dev_start $dev_end); do
tst_res TINFO "filling zram$i (it can take long time)"
local b=0
while true; do
Expand Down
4 changes: 2 additions & 2 deletions testcases/kernel/device-drivers/zram/zram02.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ zram_makeswap()
tst_require_cmds mkswap swapon swapoff
local i=0

for i in $(seq 0 $(($dev_num - 1))); do
for i in $(seq $dev_start $dev_end); do
ROD mkswap /dev/zram$i
ROD swapon /dev/zram$i
tst_res TINFO "done with /dev/zram$i"
Expand All @@ -44,7 +44,7 @@ zram_swapoff()
tst_require_cmds swapoff
local i

for i in $(seq 0 $dev_makeswap); do
for i in $(seq $dev_start $dev_end); do
ROD swapoff /dev/zram$i
done
dev_makeswap=-1
Expand Down
74 changes: 52 additions & 22 deletions testcases/kernel/device-drivers/zram/zram_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

dev_makeswap=-1
dev_mounted=-1
dev_start=0
dev_end=-1
module_load=-1
sys_control=-1

TST_NEEDS_TMPDIR=1
TST_NEEDS_ROOT=1
Expand All @@ -17,19 +21,27 @@ 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
done

rmmod zram > /dev/null 2>&1
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

if [ $module_load -eq 1 ]; then
rmmod zram > /dev/null 2>&1
fi
}

zram_load()
Expand All @@ -51,16 +63,36 @@ zram_load()

tst_res TINFO "create '$dev_num' zram device(s)"

modprobe zram num_devices=$dev_num || \
tst_brk TBROK "failed to insert zram module"
# zram module loaded, new kernel
if [ -d "/sys/class/zram-control" ]; then
tst_res TINFO "zram module already loaded, kernel supports zram-control interface"
dev_start=$(ls /dev/zram* | wc -w)
dev_end=$(($dev_start + $dev_num - 1))
sys_control=1

dev_num_created=$(ls /dev/zram* | wc -w)
for i in $(seq $dev_start $dev_end); do
cat /sys/class/zram-control/hot_add > /dev/null
done

if [ "$dev_num_created" -ne "$dev_num" ]; then
tst_brk TFAIL "unexpected num of devices: $dev_num_created"
tst_res TPASS "all zram devices (/dev/zram$dev_start~$dev_end) successfully created"
return
fi

# detect old kernel or built-in
modprobe zram num_devices=$dev_num
if [ ! -d "/sys/class/zram-control" ]; then
if grep -q '^zram' /proc/modules; then
rmmod zram > /dev/null 2>&1 || \
tst_brk TCONF "zram module is being used on old kernel without zram-control interface"
else
tst_brk TCONF "test needs CONFIG_ZRAM=m on old kernel without zram-control interface"
fi
modprobe zram num_devices=$dev_num
fi

tst_res TPASS "all zram devices successfully created"
module_load=1
dev_end=$(($dev_num - 1))
tst_res TPASS "all zram devices (/dev/zram0~$dev_end) successfully created"
}

zram_max_streams()
Expand All @@ -73,7 +105,7 @@ zram_max_streams()

tst_res TINFO "set max_comp_streams to zram device(s)"

local i=0
local i=$dev_start

for max_s in $zram_max_streams; do
local sys_path="/sys/block/zram${i}/max_comp_streams"
Expand All @@ -85,7 +117,7 @@ zram_max_streams()
tst_brk TFAIL "can't set max_streams '$max_s', get $max_stream"

i=$(($i + 1))
tst_res TINFO "$sys_path = '$max_streams' ($i/$dev_num)"
tst_res TINFO "$sys_path = '$max_streams'"
done

tst_res TPASS "test succeeded"
Expand All @@ -100,20 +132,18 @@ zram_compress_alg()
return
fi

local i=0
local i=$dev_start

tst_res TINFO "test that we can set compression algorithm"
local algs="$(sed 's/[][]//g' /sys/block/zram0/comp_algorithm)"
local algs="$(sed 's/[][]//g' /sys/block/zram${i}/comp_algorithm)"
tst_res TINFO "supported algs: $algs"

local dev_max=$(($dev_num - 1))

for i in $(seq 0 $dev_max); do
for i in $(seq $dev_start $dev_end); do
for alg in $algs; do
local sys_path="/sys/block/zram${i}/comp_algorithm"
echo "$alg" > $sys_path || \
tst_brk TFAIL "can't set '$alg' to $sys_path"
tst_res TINFO "$sys_path = '$alg' ($i/$dev_max)"
tst_res TINFO "$sys_path = '$alg'"
done
done

Expand All @@ -122,7 +152,7 @@ zram_compress_alg()

zram_set_disksizes()
{
local i=0
local i=$dev_start
local ds

tst_res TINFO "set disk size to zram device(s)"
Expand All @@ -132,7 +162,7 @@ zram_set_disksizes()
tst_brk TFAIL "can't set '$ds' to $sys_path"

i=$(($i + 1))
tst_res TINFO "$sys_path = '$ds' ($i/$dev_num)"
tst_res TINFO "$sys_path = '$ds'"
done

tst_res TPASS "test succeeded"
Expand All @@ -147,7 +177,7 @@ zram_set_memlimit()
return
fi

local i=0
local i=$dev_start
local ds

tst_res TINFO "set memory limit to zram device(s)"
Expand All @@ -158,7 +188,7 @@ zram_set_memlimit()
tst_brk TFAIL "can't set '$ds' to $sys_path"

i=$(($i + 1))
tst_res TINFO "$sys_path = '$ds' ($i/$dev_num)"
tst_res TINFO "$sys_path = '$ds'"
done

tst_res TPASS "test succeeded"
Expand Down

0 comments on commit caed783

Please sign in to comment.