xref: /aosp_15_r20/external/ltp/testcases/kernel/device-drivers/zram/zram_lib.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
3# Copyright (c) 2019-2022 Petr Vorel <[email protected]>
4# Author: Alexey Kodanev <[email protected]>
5
6dev_makeswap=-1
7dev_mounted=-1
8dev_start=0
9dev_end=-1
10module_load=-1
11sys_control=-1
12
13TST_NEEDS_TMPDIR=1
14TST_NEEDS_ROOT=1
15TST_SETUP="${TST_SETUP:-zram_load}"
16TST_CLEANUP="${TST_CLEANUP:-zram_cleanup}"
17TST_NEEDS_DRIVERS="zram"
18
19zram_cleanup()
20{
21	local i
22
23	for i in $(seq $dev_start $dev_makeswap); do
24		swapoff /dev/zram$i
25	done
26
27	for i in $(seq $dev_start $dev_mounted); do
28		umount /dev/zram$i
29	done
30
31	for i in $(seq $dev_start $dev_end); do
32		echo 1 > /sys/block/zram${i}/reset
33	done
34
35	if [ $sys_control -eq 1 ]; then
36		for i in $(seq $dev_start $dev_end); do
37			echo $i > /sys/class/zram-control/hot_remove
38		done
39	fi
40
41	if [ $module_load -eq 1 ]; then
42		rmmod zram > /dev/null 2>&1
43	fi
44}
45
46zram_load()
47{
48	local tmp
49
50	if [ -z "$dev_num" ]; then
51		dev_num=0
52		for tmp in $zram_max_streams; do
53			dev_num=$((dev_num+1))
54		done
55	fi
56
57	if [ $dev_num -le 0 ]; then
58		tst_brk TBROK "dev_num must be > 0"
59	fi
60
61	tst_set_timeout $((dev_num*450))
62
63	tst_res TINFO "create '$dev_num' zram device(s)"
64
65	# zram module loaded, new kernel
66	if [ -d "/sys/class/zram-control" ]; then
67		tst_res TINFO "zram module already loaded, kernel supports zram-control interface"
68		dev_start=$(ls /dev/zram* | wc -w)
69		dev_end=$(($dev_start + $dev_num - 1))
70		sys_control=1
71
72		for i in $(seq  $dev_start $dev_end); do
73			cat /sys/class/zram-control/hot_add > /dev/null
74		done
75
76		tst_res TPASS "all zram devices (/dev/zram$dev_start~$dev_end) successfully created"
77		return
78	fi
79
80	# detect old kernel or built-in
81	modprobe zram num_devices=$dev_num
82	if [ ! -d "/sys/class/zram-control" ]; then
83		if grep -q '^zram' /proc/modules; then
84			rmmod zram > /dev/null 2>&1 || \
85				tst_brk TCONF "zram module is being used on old kernel without zram-control interface"
86		else
87			tst_brk TCONF "test needs CONFIG_ZRAM=m on old kernel without zram-control interface"
88		fi
89		modprobe zram num_devices=$dev_num
90	fi
91
92	module_load=1
93	dev_end=$(($dev_num - 1))
94	tst_res TPASS "all zram devices (/dev/zram0~$dev_end) successfully created"
95}
96
97zram_max_streams()
98{
99	if tst_kvcmp -lt "3.15" -o -ge "4.7"; then
100		tst_res TCONF "The device attribute max_comp_streams was"\
101		               "introduced in kernel 3.15 and deprecated in 4.7"
102		return
103	fi
104
105	tst_res TINFO "set max_comp_streams to zram device(s)"
106
107	local i=$dev_start
108
109	for max_s in $zram_max_streams; do
110		local sys_path="/sys/block/zram${i}/max_comp_streams"
111		if ! echo $max_s > $sys_path; then
112			tst_res TFAIL "failed to set '$max_s' to $sys_path"
113			return
114		fi
115		local max_streams=$(cat $sys_path)
116
117		if [ "$max_s" -ne "$max_streams" ]; then
118			tst_res TFAIL "can't set max_streams '$max_s', get $max_stream"
119			return
120		fi
121
122		i=$(($i + 1))
123		tst_res TINFO "$sys_path = '$max_streams'"
124	done
125
126	tst_res TPASS "test succeeded"
127}
128
129zram_compress_alg()
130{
131	if tst_kvcmp -lt "3.15"; then
132		tst_res TCONF "device attribute comp_algorithm is"\
133			"introduced since kernel v3.15, the running kernel"\
134			"does not support it"
135		return
136	fi
137
138	local i=$dev_start
139
140	tst_res TINFO "test that we can set compression algorithm"
141	local algs="$(sed 's/[][]//g' /sys/block/zram${i}/comp_algorithm)"
142	tst_res TINFO "supported algs: $algs"
143
144	for i in $(seq $dev_start $dev_end); do
145		for alg in $algs; do
146			local sys_path="/sys/block/zram${i}/comp_algorithm"
147			if ! echo "$alg" >  $sys_path; then
148				tst_res TFAIL "can't set '$alg' to $sys_path"
149				return
150			fi
151			tst_res TINFO "$sys_path = '$alg'"
152		done
153	done
154
155	tst_res TPASS "test succeeded"
156}
157
158zram_set_disksizes()
159{
160	local i=$dev_start
161	local ds
162
163	tst_res TINFO "set disk size to zram device(s)"
164	for ds in $zram_sizes; do
165		local sys_path="/sys/block/zram${i}/disksize"
166		if ! echo "$ds" >  $sys_path; then
167			tst_res TFAIL "can't set '$ds' to $sys_path"
168			return
169		fi
170
171		i=$(($i + 1))
172		tst_res TINFO "$sys_path = '$ds'"
173	done
174
175	tst_res TPASS "test succeeded"
176}
177
178zram_set_memlimit()
179{
180	if tst_kvcmp -lt "3.18"; then
181		tst_res TCONF "device attribute mem_limit is"\
182			"introduced since kernel v3.18, the running kernel"\
183			"does not support it"
184		return
185	fi
186
187	local i=$dev_start
188	local ds
189
190	tst_res TINFO "set memory limit to zram device(s)"
191
192	for ds in $zram_mem_limits; do
193		local sys_path="/sys/block/zram${i}/mem_limit"
194		if ! echo "$ds" >  $sys_path; then
195			tst_res TFAIL "can't set '$ds' to $sys_path"
196			return
197		fi
198
199		i=$(($i + 1))
200		tst_res TINFO "$sys_path = '$ds'"
201	done
202
203	tst_res TPASS "test succeeded"
204}
205
206. tst_test.sh
207