1#!/bin/sh
2#
3# Copyright (c) 2015 Fujitsu Ltd.
4# Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19#
20# This is a regression test for commit:
21# http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/
22# ?id=bb2bc55
23#
24
25TCID=cpuset_regression_test
26TST_TOTAL=1
27. test.sh
28
29setup()
30{
31	tst_require_root
32
33	tst_kvercmp 3 18 0
34	if [ $? -eq 0 ]; then
35		tst_brkm TCONF "Test must be run with kernel 3.18.0 or newer"
36	fi
37
38	local cpu_num=$(getconf _NPROCESSORS_ONLN)
39	if [ $cpu_num -lt 2 ]; then
40		tst_brkm TCONF "We need 2 cpus at least to have test"
41	fi
42
43	tst_tmpdir
44
45	TST_CLEANUP=cleanup
46
47	# We need to mount cpuset if it is not found.
48	mount_flag=0
49	grep -w cpuset /proc/mounts > tmpfile
50	if [ $? -eq 0 ]; then
51		root_cpuset_dir=$(cat tmpfile | awk '{print $2}')
52	else
53		root_cpuset_dir="cpuset_test"
54
55		ROD_SILENT mkdir -p ${root_cpuset_dir}
56
57		ROD_SILENT mount -t cpuset cpuset ${root_cpuset_dir}
58
59		mount_flag=1
60	fi
61
62	if [ -f ${root_cpuset_dir}/cpuset.cpu_exclusive ]; then
63		cpu_exclusive=cpuset.cpu_exclusive
64		cpus=cpuset.cpus
65	elif [ -f ${root_cpuset_dir}/cpu_exclusive ]; then
66		cpu_exclusive=cpu_exclusive
67		cpus=cpus
68	else
69		tst_brkm TBROK "Both cpuset.cpu_exclusive and cpu_exclusive" \
70			       "do not exist."
71	fi
72
73	cpu_exclusive_value=$(cat ${root_cpuset_dir}/${cpu_exclusive})
74	if [ "${cpu_exclusive_value}" != "1" ];then
75		echo 1 > ${root_cpuset_dir}/${cpu_exclusive}
76		if [ $? -ne 0 ]; then
77			tst_brkm TBROK "'echo 1 >" \
78				       "${root_cpuset_dir}/${cpu_exclusive}'" \
79				       "failed"
80		fi
81	fi
82}
83
84cleanup()
85{
86	if [ -d "${root_cpuset_dir}/testdir" ]; then
87		rmdir ${root_cpuset_dir}/testdir
88	fi
89
90	if [ -n ${cpu_exclusive_value} -a ${cpu_exclusive_value} -ne 1 ]; then
91		# Need to flush, or may be output:
92		# "write error: Device or resource busy"
93		sync
94
95		echo ${cpu_exclusive_value} > \
96		     ${root_cpuset_dir}/${cpu_exclusive}
97	fi
98
99	if [ "${mount_flag}" == "1" ]; then
100		umount ${root_cpuset_dir}
101		if [ $? -ne 0 ]; then
102			tst_resm TWARN "'umount ${root_cpuset_dir}' failed"
103		fi
104
105		if [ -d "${root_cpuset_dir}" ]; then
106			rmdir ${root_cpuset_dir}
107		fi
108	fi
109
110	tst_rmdir
111}
112
113cpuset_test()
114{
115	ROD_SILENT mkdir ${root_cpuset_dir}/testdir
116
117	# Creat an exclusive cpuset.
118	echo 1 > ${root_cpuset_dir}/testdir/${cpu_exclusive}
119	if [ $? -ne 0 ]; then
120		tst_brkm TFAIL "'echo 1 >" \
121			       "${root_cpuset_dir}/testdir/${cpu_exclusive}'" \
122			       "failed"
123	fi
124
125	local cpu_exclusive_tmp=$(cat \
126				  ${root_cpuset_dir}/testdir/${cpu_exclusive})
127	if [ "${cpu_exclusive_tmp}" != "1" ]; then
128		tst_brkm TFAIL "${cpu_exclusive} is '${cpu_exclusive_tmp}'," \
129			       "expected '1'"
130	fi
131
132	# ${cpus} is empty at the begin, that maybe make the system *crash*.
133	echo 0-1 > ${root_cpuset_dir}/testdir/${cpus}
134	if [ $? -ne 0 ]; then
135		tst_brkm TFAIL "'echo 0-1 >" \
136			       "${root_cpuset_dir}/testdir/${cpus}' failed"
137	fi
138
139	local cpus_value=$(cat ${root_cpuset_dir}/testdir/${cpus})
140	if [ "${cpus_value}" != "0-1" ]; then
141		tst_brkm TFAIL "${cpus} is '${cpus_value}', expected '0-1'"
142	fi
143
144	tst_resm TPASS "Bug is not reproduced"
145}
146
147setup
148
149cpuset_test
150
151tst_exit
152