1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0+
3#
4# Run a kvm-based test of the specified tree on the specified configs.
5# Fully automated run and error checking, no graphics console.
6#
7# Execute this in the source tree.  Do not run it as a background task
8# because qemu does not seem to like that much.
9#
10# Usage: kvm-test-1-run.sh config builddir resdir seconds qemu-args boot_args
11#
12# qemu-args defaults to "-enable-kvm -nographic", along with arguments
13#			specifying the number of CPUs and other options
14#			generated from the underlying CPU architecture.
15# boot_args defaults to value returned by the per_version_boot_params
16#			shell function.
17#
18# Anything you specify for either qemu-args or boot_args is appended to
19# the default values.  The "-smp" value is deduced from the contents of
20# the config fragment.
21#
22# More sophisticated argument parsing is clearly needed.
23#
24# Copyright (C) IBM Corporation, 2011
25#
26# Authors: Paul E. McKenney <paulmck@linux.ibm.com>
27
28T=${TMPDIR-/tmp}/kvm-test-1-run.sh.$$
29trap 'rm -rf $T' 0
30mkdir $T
31
32. functions.sh
33. $CONFIGFRAG/ver_functions.sh
34
35config_template=${1}
36config_dir=`echo $config_template | sed -e 's,/[^/]*$,,'`
37title=`echo $config_template | sed -e 's/^.*\///'`
38builddir=${2}
39resdir=${3}
40if test -z "$resdir" -o ! -d "$resdir" -o ! -w "$resdir"
41then
42	echo "kvm-test-1-run.sh :$resdir: Not a writable directory, cannot store results into it"
43	exit 1
44fi
45echo ' ---' `date`: Starting build
46echo ' ---' Kconfig fragment at: $config_template >> $resdir/log
47touch $resdir/ConfigFragment.input
48
49# Combine additional Kconfig options into an existing set such that
50# newer options win.  The first argument is the Kconfig source ID, the
51# second the to-be-updated file within $T, and the third and final the
52# list of additional Kconfig options.  Note that a $2.tmp file is
53# created when doing the update.
54config_override_param () {
55	if test -n "$3"
56	then
57		echo $3 | sed -e 's/^ *//' -e 's/ *$//' | tr -s " " "\012" > $T/Kconfig_args
58		echo " --- $1" >> $resdir/ConfigFragment.input
59		cat $T/Kconfig_args >> $resdir/ConfigFragment.input
60		config_override.sh $T/$2 $T/Kconfig_args > $T/$2.tmp
61		mv $T/$2.tmp $T/$2
62		# Note that "#CHECK#" is not permitted on commandline.
63	fi
64}
65
66echo > $T/KcList
67config_override_param "$config_dir/CFcommon" KcList "`cat $config_dir/CFcommon 2> /dev/null`"
68config_override_param "$config_template" KcList "`cat $config_template 2> /dev/null`"
69config_override_param "--gdb options" KcList "$TORTURE_KCONFIG_GDB_ARG"
70config_override_param "--kasan options" KcList "$TORTURE_KCONFIG_KASAN_ARG"
71config_override_param "--kcsan options" KcList "$TORTURE_KCONFIG_KCSAN_ARG"
72config_override_param "--kconfig argument" KcList "$TORTURE_KCONFIG_ARG"
73cp $T/KcList $resdir/ConfigFragment
74
75base_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'`
76if test "$base_resdir" != "$resdir" -a -f $base_resdir/bzImage -a -f $base_resdir/vmlinux
77then
78	# Rerunning previous test, so use that test's kernel.
79	QEMU="`identify_qemu $base_resdir/vmlinux`"
80	BOOT_IMAGE="`identify_boot_image $QEMU`"
81	KERNEL=$base_resdir/${BOOT_IMAGE##*/} # use the last component of ${BOOT_IMAGE}
82	ln -s $base_resdir/Make*.out $resdir  # for kvm-recheck.sh
83	ln -s $base_resdir/.config $resdir  # for kvm-recheck.sh
84	# Arch-independent indicator
85	touch $resdir/builtkernel
86elif kvm-build.sh $T/KcList $resdir
87then
88	# Had to build a kernel for this test.
89	QEMU="`identify_qemu vmlinux`"
90	BOOT_IMAGE="`identify_boot_image $QEMU`"
91	cp vmlinux $resdir
92	cp .config $resdir
93	cp Module.symvers $resdir > /dev/null || :
94	cp System.map $resdir > /dev/null || :
95	if test -n "$BOOT_IMAGE"
96	then
97		cp $BOOT_IMAGE $resdir
98		KERNEL=$resdir/${BOOT_IMAGE##*/}
99		# Arch-independent indicator
100		touch $resdir/builtkernel
101	else
102		echo No identifiable boot image, not running KVM, see $resdir.
103		echo Do the torture scripts know about your architecture?
104	fi
105	parse-build.sh $resdir/Make.out $title
106else
107	# Build failed.
108	cp .config $resdir || :
109	echo Build failed, not running KVM, see $resdir.
110	if test -f $builddir.wait
111	then
112		mv $builddir.wait $builddir.ready
113	fi
114	exit 1
115fi
116if test -f $builddir.wait
117then
118	mv $builddir.wait $builddir.ready
119fi
120while test -f $builddir.ready
121do
122	sleep 1
123done
124seconds=$4
125qemu_args=$5
126boot_args=$6
127
128if test -z "$TORTURE_BUILDONLY"
129then
130	echo ' ---' `date`: Starting kernel
131fi
132
133# Generate -smp qemu argument.
134qemu_args="-enable-kvm -nographic $qemu_args"
135cpu_count=`configNR_CPUS.sh $resdir/ConfigFragment`
136cpu_count=`configfrag_boot_cpus "$boot_args" "$config_template" "$cpu_count"`
137if test "$cpu_count" -gt "$TORTURE_ALLOTED_CPUS"
138then
139	echo CPU count limited from $cpu_count to $TORTURE_ALLOTED_CPUS | tee -a $resdir/Warnings
140	cpu_count=$TORTURE_ALLOTED_CPUS
141fi
142qemu_args="`specify_qemu_cpus "$QEMU" "$qemu_args" "$cpu_count"`"
143qemu_args="`specify_qemu_net "$qemu_args"`"
144
145# Generate architecture-specific and interaction-specific qemu arguments
146qemu_args="$qemu_args `identify_qemu_args "$QEMU" "$resdir/console.log"`"
147
148# Generate qemu -append arguments
149qemu_append="`identify_qemu_append "$QEMU"`"
150
151# Pull in Kconfig-fragment boot parameters
152boot_args="`configfrag_boot_params "$boot_args" "$config_template"`"
153# Generate kernel-version-specific boot parameters
154boot_args="`per_version_boot_params "$boot_args" $resdir/.config $seconds`"
155if test -n "$TORTURE_BOOT_GDB_ARG"
156then
157	boot_args="$boot_args $TORTURE_BOOT_GDB_ARG"
158fi
159echo $QEMU $qemu_args -m $TORTURE_QEMU_MEM -kernel $KERNEL -append \"$qemu_append $boot_args\" $TORTURE_QEMU_GDB_ARG > $resdir/qemu-cmd
160echo "# TORTURE_SHUTDOWN_GRACE=$TORTURE_SHUTDOWN_GRACE" >> $resdir/qemu-cmd
161echo "# seconds=$seconds" >> $resdir/qemu-cmd
162
163if test -n "$TORTURE_BUILDONLY"
164then
165	echo Build-only run specified, boot/test omitted.
166	touch $resdir/buildonly
167	exit 0
168fi
169
170# Decorate qemu-cmd with redirection, backgrounding, and PID capture
171sed -e 's/$/ 2>\&1 \&/' < $resdir/qemu-cmd > $T/qemu-cmd
172echo 'echo $! > $resdir/qemu_pid' >> $T/qemu-cmd
173
174# In case qemu refuses to run...
175echo "NOTE: $QEMU either did not run or was interactive" > $resdir/console.log
176
177# Attempt to run qemu
178kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`
179( . $T/qemu-cmd; wait `cat  $resdir/qemu_pid`; echo $? > $resdir/qemu-retval ) &
180commandcompleted=0
181if test -z "$TORTURE_KCONFIG_GDB_ARG"
182then
183	sleep 10 # Give qemu's pid a chance to reach the file
184	if test -s "$resdir/qemu_pid"
185	then
186		qemu_pid=`cat "$resdir/qemu_pid"`
187		echo Monitoring qemu job at pid $qemu_pid
188	else
189		qemu_pid=""
190		echo Monitoring qemu job at yet-as-unknown pid
191	fi
192fi
193if test -n "$TORTURE_KCONFIG_GDB_ARG"
194then
195	echo Waiting for you to attach a debug session, for example: > /dev/tty
196	echo "    gdb $base_resdir/vmlinux" > /dev/tty
197	echo 'After symbols load and the "(gdb)" prompt appears:' > /dev/tty
198	echo "    target remote :1234" > /dev/tty
199	echo "    continue" > /dev/tty
200	kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`
201fi
202while :
203do
204	if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
205	then
206		qemu_pid=`cat "$resdir/qemu_pid"`
207	fi
208	kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
209	if test -z "$qemu_pid" || kill -0 "$qemu_pid" > /dev/null 2>&1
210	then
211		if test -n "$TORTURE_KCONFIG_GDB_ARG"
212		then
213			:
214		elif test $kruntime -ge $seconds || test -f "$resdir/../STOP.1"
215		then
216			break;
217		fi
218		sleep 1
219	else
220		commandcompleted=1
221		if test $kruntime -lt $seconds
222		then
223			echo Completed in $kruntime vs. $seconds >> $resdir/Warnings 2>&1
224			grep "^(qemu) qemu:" $resdir/kvm-test-1-run.sh.out >> $resdir/Warnings 2>&1
225			killpid="`sed -n "s/^(qemu) qemu: terminating on signal [0-9]* from pid \([0-9]*\).*$/\1/p" $resdir/Warnings`"
226			if test -n "$killpid"
227			then
228				echo "ps -fp $killpid" >> $resdir/Warnings 2>&1
229				ps -fp $killpid >> $resdir/Warnings 2>&1
230			fi
231			# Reduce probability of PID reuse by allowing a one-minute buffer
232			if test $((kruntime + 60)) -lt $seconds && test -s "$resdir/../jitter_pids"
233			then
234				awk < "$resdir/../jitter_pids" '
235				NF > 0 {
236					pidlist = pidlist " " $1;
237					n++;
238				}
239				END {
240					if (n > 0) {
241						print "kill " pidlist;
242					}
243				}' | sh
244			fi
245		else
246			echo ' ---' `date`: "Kernel done"
247		fi
248		break
249	fi
250done
251if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
252then
253	qemu_pid=`cat "$resdir/qemu_pid"`
254fi
255if test $commandcompleted -eq 0 -a -n "$qemu_pid"
256then
257	if ! test -f "$resdir/../STOP.1"
258	then
259		echo Grace period for qemu job at pid $qemu_pid
260	fi
261	oldline="`tail $resdir/console.log`"
262	while :
263	do
264		if test -f "$resdir/../STOP.1"
265		then
266			echo "PID $qemu_pid killed due to run STOP.1 request" >> $resdir/Warnings 2>&1
267			kill -KILL $qemu_pid
268			break
269		fi
270		kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
271		if kill -0 $qemu_pid > /dev/null 2>&1
272		then
273			:
274		else
275			break
276		fi
277		must_continue=no
278		newline="`tail $resdir/console.log`"
279		if test "$newline" != "$oldline" && echo $newline | grep -q ' [0-9]\+us : '
280		then
281			must_continue=yes
282		fi
283		last_ts="`tail $resdir/console.log | grep '^\[ *[0-9]\+\.[0-9]\+]' | tail -1 | sed -e 's/^\[ *//' -e 's/\..*$//'`"
284		if test -z "$last_ts"
285		then
286			last_ts=0
287		fi
288		if test "$newline" != "$oldline" -a "$last_ts" -lt $((seconds + $TORTURE_SHUTDOWN_GRACE))
289		then
290			must_continue=yes
291		fi
292		if test $must_continue = no -a $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))
293		then
294			echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds" >> $resdir/Warnings 2>&1
295			kill -KILL $qemu_pid
296			break
297		fi
298		oldline=$newline
299		sleep 10
300	done
301elif test -z "$qemu_pid"
302then
303	echo Unknown PID, cannot kill qemu command
304fi
305
306parse-console.sh $resdir/console.log $title
307