1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0+
3#
4# Run a series of tests under KVM.  By default, this series is specified
5# by the relevant CFLIST file, but can be overridden by the --configs
6# command-line argument.
7#
8# Usage: kvm.sh [ options ]
9#
10# Copyright (C) IBM Corporation, 2011
11#
12# Authors: Paul E. McKenney <paulmck@linux.ibm.com>
13
14scriptname=$0
15args="$*"
16
17T=${TMPDIR-/tmp}/kvm.sh.$$
18trap 'rm -rf $T' 0
19mkdir $T
20
21cd `dirname $scriptname`/../../../../../
22
23dur=$((30*60))
24dryrun=""
25KVM="`pwd`/tools/testing/selftests/rcutorture"; export KVM
26PATH=${KVM}/bin:$PATH; export PATH
27. functions.sh
28
29TORTURE_ALLOTED_CPUS="`identify_qemu_vcpus`"
30TORTURE_DEFCONFIG=defconfig
31TORTURE_BOOT_IMAGE=""
32TORTURE_INITRD="$KVM/initrd"; export TORTURE_INITRD
33TORTURE_KCONFIG_ARG=""
34TORTURE_KCONFIG_GDB_ARG=""
35TORTURE_BOOT_GDB_ARG=""
36TORTURE_QEMU_GDB_ARG=""
37TORTURE_KCONFIG_KASAN_ARG=""
38TORTURE_KCONFIG_KCSAN_ARG=""
39TORTURE_KMAKE_ARG=""
40TORTURE_QEMU_MEM=512
41TORTURE_SHUTDOWN_GRACE=180
42TORTURE_SUITE=rcu
43TORTURE_TRUST_MAKE=""
44resdir=""
45configs=""
46cpus=0
47ds=`date +%Y.%m.%d-%H.%M.%S`
48jitter="-1"
49
50usage () {
51	echo "Usage: $scriptname optional arguments:"
52	echo "       --allcpus"
53	echo "       --bootargs kernel-boot-arguments"
54	echo "       --bootimage relative-path-to-kernel-boot-image"
55	echo "       --buildonly"
56	echo "       --configs \"config-file list w/ repeat factor (3*TINY01)\""
57	echo "       --cpus N"
58	echo "       --datestamp string"
59	echo "       --defconfig string"
60	echo "       --dryrun sched|script"
61	echo "       --duration minutes | <seconds>s | <hours>h | <days>d"
62	echo "       --gdb"
63	echo "       --help"
64	echo "       --interactive"
65	echo "       --jitter N [ maxsleep (us) [ maxspin (us) ] ]"
66	echo "       --kconfig Kconfig-options"
67	echo "       --kmake-arg kernel-make-arguments"
68	echo "       --mac nn:nn:nn:nn:nn:nn"
69	echo "       --memory megabytes|nnnG"
70	echo "       --no-initrd"
71	echo "       --qemu-args qemu-arguments"
72	echo "       --qemu-cmd qemu-system-..."
73	echo "       --results absolute-pathname"
74	echo "       --torture lock|rcu|rcuscale|refscale|scf"
75	echo "       --trust-make"
76	exit 1
77}
78
79while test $# -gt 0
80do
81	case "$1" in
82	--allcpus)
83		cpus=$TORTURE_ALLOTED_CPUS
84		max_cpus=$TORTURE_ALLOTED_CPUS
85		;;
86	--bootargs|--bootarg)
87		checkarg --bootargs "(list of kernel boot arguments)" "$#" "$2" '.*' '^--'
88		TORTURE_BOOTARGS="$2"
89		shift
90		;;
91	--bootimage)
92		checkarg --bootimage "(relative path to kernel boot image)" "$#" "$2" '[a-zA-Z0-9][a-zA-Z0-9_]*' '^--'
93		TORTURE_BOOT_IMAGE="$2"
94		shift
95		;;
96	--buildonly|--build-only)
97		TORTURE_BUILDONLY=1
98		;;
99	--configs|--config)
100		checkarg --configs "(list of config files)" "$#" "$2" '^[^/]*$' '^--'
101		configs="$2"
102		shift
103		;;
104	--cpus)
105		checkarg --cpus "(number)" "$#" "$2" '^[0-9]*$' '^--'
106		cpus=$2
107		TORTURE_ALLOTED_CPUS="$2"
108		max_cpus="`identify_qemu_vcpus`"
109		if test "$TORTURE_ALLOTED_CPUS" -gt "$max_cpus"
110		then
111			TORTURE_ALLOTED_CPUS=$max_cpus
112		fi
113		shift
114		;;
115	--datestamp)
116		checkarg --datestamp "(relative pathname)" "$#" "$2" '^[^/]*$' '^--'
117		ds=$2
118		shift
119		;;
120	--defconfig)
121		checkarg --defconfig "defconfigtype" "$#" "$2" '^[^/][^/]*$' '^--'
122		TORTURE_DEFCONFIG=$2
123		shift
124		;;
125	--dryrun)
126		checkarg --dryrun "sched|script" $# "$2" 'sched\|script' '^--'
127		dryrun=$2
128		shift
129		;;
130	--duration)
131		checkarg --duration "(minutes)" $# "$2" '^[0-9][0-9]*\(s\|m\|h\|d\|\)$' '^error'
132		mult=60
133		if echo "$2" | grep -q 's$'
134		then
135			mult=1
136		elif echo "$2" | grep -q 'h$'
137		then
138			mult=3600
139		elif echo "$2" | grep -q 'd$'
140		then
141			mult=86400
142		fi
143		ts=`echo $2 | sed -e 's/[smhd]$//'`
144		dur=$(($ts*mult))
145		shift
146		;;
147	--gdb)
148		TORTURE_KCONFIG_GDB_ARG="CONFIG_DEBUG_INFO=y"; export TORTURE_KCONFIG_GDB_ARG
149		TORTURE_BOOT_GDB_ARG="nokaslr"; export TORTURE_BOOT_GDB_ARG
150		TORTURE_QEMU_GDB_ARG="-s -S"; export TORTURE_QEMU_GDB_ARG
151		;;
152	--help|-h)
153		usage
154		;;
155	--interactive)
156		TORTURE_QEMU_INTERACTIVE=1; export TORTURE_QEMU_INTERACTIVE
157		;;
158	--jitter)
159		checkarg --jitter "(# threads [ sleep [ spin ] ])" $# "$2" '^-\{,1\}[0-9]\+\( \+[0-9]\+\)\{,2\} *$' '^error$'
160		jitter="$2"
161		shift
162		;;
163	--kconfig|--kconfigs)
164		checkarg --kconfig "(Kconfig options)" $# "$2" '^CONFIG_[A-Z0-9_]\+=\([ynm]\|[0-9]\+\)\( CONFIG_[A-Z0-9_]\+=\([ynm]\|[0-9]\+\)\)*$' '^error$'
165		TORTURE_KCONFIG_ARG="$2"
166		shift
167		;;
168	--kasan)
169		TORTURE_KCONFIG_KASAN_ARG="CONFIG_DEBUG_INFO=y CONFIG_KASAN=y"; export TORTURE_KCONFIG_KASAN_ARG
170		;;
171	--kcsan)
172		TORTURE_KCONFIG_KCSAN_ARG="CONFIG_DEBUG_INFO=y CONFIG_KCSAN=y CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC=n CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY=n CONFIG_KCSAN_REPORT_ONCE_IN_MS=100000 CONFIG_KCSAN_VERBOSE=y CONFIG_KCSAN_INTERRUPT_WATCHER=y"; export TORTURE_KCONFIG_KCSAN_ARG
173		;;
174	--kmake-arg|--kmake-args)
175		checkarg --kmake-arg "(kernel make arguments)" $# "$2" '.*' '^error$'
176		TORTURE_KMAKE_ARG="$2"
177		shift
178		;;
179	--mac)
180		checkarg --mac "(MAC address)" $# "$2" '^\([0-9a-fA-F]\{2\}:\)\{5\}[0-9a-fA-F]\{2\}$' error
181		TORTURE_QEMU_MAC=$2
182		shift
183		;;
184	--memory)
185		checkarg --memory "(memory size)" $# "$2" '^[0-9]\+[MG]\?$' error
186		TORTURE_QEMU_MEM=$2
187		shift
188		;;
189	--no-initrd)
190		TORTURE_INITRD=""; export TORTURE_INITRD
191		;;
192	--qemu-args|--qemu-arg)
193		checkarg --qemu-args "(qemu arguments)" $# "$2" '^-' '^error'
194		TORTURE_QEMU_ARG="$2"
195		shift
196		;;
197	--qemu-cmd)
198		checkarg --qemu-cmd "(qemu-system-...)" $# "$2" 'qemu-system-' '^--'
199		TORTURE_QEMU_CMD="$2"
200		shift
201		;;
202	--results)
203		checkarg --results "(absolute pathname)" "$#" "$2" '^/' '^error'
204		resdir=$2
205		shift
206		;;
207	--shutdown-grace)
208		checkarg --shutdown-grace "(seconds)" "$#" "$2" '^[0-9]*$' '^error'
209		TORTURE_SHUTDOWN_GRACE=$2
210		shift
211		;;
212	--torture)
213		checkarg --torture "(suite name)" "$#" "$2" '^\(lock\|rcu\|rcuscale\|refscale\|scf\)$' '^--'
214		TORTURE_SUITE=$2
215		shift
216		if test "$TORTURE_SUITE" = rcuscale || test "$TORTURE_SUITE" = refscale
217		then
218			# If you really want jitter for refscale or
219			# rcuscale, specify it after specifying the rcuscale
220			# or the refscale.  (But why jitter in these cases?)
221			jitter=0
222		fi
223		;;
224	--trust-make)
225		TORTURE_TRUST_MAKE="y"
226		;;
227	*)
228		echo Unknown argument $1
229		usage
230		;;
231	esac
232	shift
233done
234
235if test -z "$TORTURE_INITRD" || tools/testing/selftests/rcutorture/bin/mkinitrd.sh
236then
237	:
238else
239	echo No initrd and unable to create one, aborting test >&2
240	exit 1
241fi
242
243CONFIGFRAG=${KVM}/configs/${TORTURE_SUITE}; export CONFIGFRAG
244
245defaultconfigs="`tr '\012' ' ' < $CONFIGFRAG/CFLIST`"
246if test -z "$configs"
247then
248	configs=$defaultconfigs
249fi
250
251if test -z "$resdir"
252then
253	resdir=$KVM/res
254fi
255
256# Create a file of test-name/#cpus pairs, sorted by decreasing #cpus.
257configs_derep=
258for CF in $configs
259do
260	case $CF in
261	[0-9]\**|[0-9][0-9]\**|[0-9][0-9][0-9]\**)
262		config_reps=`echo $CF | sed -e 's/\*.*$//'`
263		CF1=`echo $CF | sed -e 's/^[^*]*\*//'`
264		;;
265	*)
266		config_reps=1
267		CF1=$CF
268		;;
269	esac
270	for ((cur_rep=0;cur_rep<$config_reps;cur_rep++))
271	do
272		configs_derep="$configs_derep $CF1"
273	done
274done
275touch $T/cfgcpu
276configs_derep="`echo $configs_derep | sed -e "s/\<CFLIST\>/$defaultconfigs/g"`"
277if test -n "$TORTURE_KCONFIG_GDB_ARG"
278then
279	if test "`echo $configs_derep | wc -w`" -gt 1
280	then
281		echo "The --config list is: $configs_derep."
282		echo "Only one --config permitted with --gdb, terminating."
283		exit 1
284	fi
285fi
286for CF1 in $configs_derep
287do
288	if test -f "$CONFIGFRAG/$CF1"
289	then
290		cpu_count=`configNR_CPUS.sh $CONFIGFRAG/$CF1`
291		cpu_count=`configfrag_boot_cpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
292		cpu_count=`configfrag_boot_maxcpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
293		echo $CF1 $cpu_count >> $T/cfgcpu
294	else
295		echo "The --configs file $CF1 does not exist, terminating."
296		exit 1
297	fi
298done
299sort -k2nr $T/cfgcpu -T="$T" > $T/cfgcpu.sort
300
301# Use a greedy bin-packing algorithm, sorting the list accordingly.
302awk < $T/cfgcpu.sort > $T/cfgcpu.pack -v ncpus=$cpus '
303BEGIN {
304	njobs = 0;
305}
306
307{
308	# Read file of tests and corresponding required numbers of CPUs.
309	cf[njobs] = $1;
310	cpus[njobs] = $2;
311	njobs++;
312}
313
314END {
315	batch = 0;
316	nc = -1;
317
318	# Each pass through the following loop creates on test batch
319	# that can be executed concurrently given ncpus.  Note that a
320	# given test that requires more than the available CPUs will run in
321	# their own batch.  Such tests just have to make do with what
322	# is available.
323	while (nc != ncpus) {
324		batch++;
325		nc = ncpus;
326
327		# Each pass through the following loop considers one
328		# test for inclusion in the current batch.
329		for (i = 0; i < njobs; i++) {
330			if (done[i])
331				continue; # Already part of a batch.
332			if (nc >= cpus[i] || nc == ncpus) {
333
334				# This test fits into the current batch.
335				done[i] = batch;
336				nc -= cpus[i];
337				if (nc <= 0)
338					break; # Too-big test in its own batch.
339			}
340		}
341	}
342
343	# Dump out the tests in batch order.
344	for (b = 1; b <= batch; b++)
345		for (i = 0; i < njobs; i++)
346			if (done[i] == b)
347				print cf[i], cpus[i];
348}'
349
350# Generate a script to execute the tests in appropriate batches.
351cat << ___EOF___ > $T/script
352CONFIGFRAG="$CONFIGFRAG"; export CONFIGFRAG
353KVM="$KVM"; export KVM
354PATH="$PATH"; export PATH
355TORTURE_ALLOTED_CPUS="$TORTURE_ALLOTED_CPUS"; export TORTURE_ALLOTED_CPUS
356TORTURE_BOOT_IMAGE="$TORTURE_BOOT_IMAGE"; export TORTURE_BOOT_IMAGE
357TORTURE_BUILDONLY="$TORTURE_BUILDONLY"; export TORTURE_BUILDONLY
358TORTURE_DEFCONFIG="$TORTURE_DEFCONFIG"; export TORTURE_DEFCONFIG
359TORTURE_INITRD="$TORTURE_INITRD"; export TORTURE_INITRD
360TORTURE_KCONFIG_ARG="$TORTURE_KCONFIG_ARG"; export TORTURE_KCONFIG_ARG
361TORTURE_KCONFIG_GDB_ARG="$TORTURE_KCONFIG_GDB_ARG"; export TORTURE_KCONFIG_GDB_ARG
362TORTURE_BOOT_GDB_ARG="$TORTURE_BOOT_GDB_ARG"; export TORTURE_BOOT_GDB_ARG
363TORTURE_QEMU_GDB_ARG="$TORTURE_QEMU_GDB_ARG"; export TORTURE_QEMU_GDB_ARG
364TORTURE_KCONFIG_KASAN_ARG="$TORTURE_KCONFIG_KASAN_ARG"; export TORTURE_KCONFIG_KASAN_ARG
365TORTURE_KCONFIG_KCSAN_ARG="$TORTURE_KCONFIG_KCSAN_ARG"; export TORTURE_KCONFIG_KCSAN_ARG
366TORTURE_KMAKE_ARG="$TORTURE_KMAKE_ARG"; export TORTURE_KMAKE_ARG
367TORTURE_QEMU_CMD="$TORTURE_QEMU_CMD"; export TORTURE_QEMU_CMD
368TORTURE_QEMU_INTERACTIVE="$TORTURE_QEMU_INTERACTIVE"; export TORTURE_QEMU_INTERACTIVE
369TORTURE_QEMU_MAC="$TORTURE_QEMU_MAC"; export TORTURE_QEMU_MAC
370TORTURE_QEMU_MEM="$TORTURE_QEMU_MEM"; export TORTURE_QEMU_MEM
371TORTURE_SHUTDOWN_GRACE="$TORTURE_SHUTDOWN_GRACE"; export TORTURE_SHUTDOWN_GRACE
372TORTURE_SUITE="$TORTURE_SUITE"; export TORTURE_SUITE
373TORTURE_TRUST_MAKE="$TORTURE_TRUST_MAKE"; export TORTURE_TRUST_MAKE
374if ! test -e $resdir
375then
376	mkdir -p "$resdir" || :
377fi
378mkdir $resdir/$ds
379TORTURE_RESDIR="$resdir/$ds"; export TORTURE_RESDIR
380TORTURE_STOPFILE="$resdir/$ds/STOP"; export TORTURE_STOPFILE
381echo Results directory: $resdir/$ds
382echo $scriptname $args
383touch $resdir/$ds/log
384echo $scriptname $args >> $resdir/$ds/log
385echo ${TORTURE_SUITE} > $resdir/$ds/TORTURE_SUITE
386pwd > $resdir/$ds/testid.txt
387if test -d .git
388then
389	git status >> $resdir/$ds/testid.txt
390	git rev-parse HEAD >> $resdir/$ds/testid.txt
391	git diff HEAD >> $resdir/$ds/testid.txt
392fi
393___EOF___
394awk < $T/cfgcpu.pack \
395	-v TORTURE_BUILDONLY="$TORTURE_BUILDONLY" \
396	-v CONFIGDIR="$CONFIGFRAG/" \
397	-v KVM="$KVM" \
398	-v ncpus=$cpus \
399	-v jitter="$jitter" \
400	-v rd=$resdir/$ds/ \
401	-v dur=$dur \
402	-v TORTURE_QEMU_ARG="$TORTURE_QEMU_ARG" \
403	-v TORTURE_BOOTARGS="$TORTURE_BOOTARGS" \
404'BEGIN {
405	i = 0;
406}
407
408{
409	cf[i] = $1;
410	cpus[i] = $2;
411	i++;
412}
413
414# Dump out the scripting required to run one test batch.
415function dump(first, pastlast, batchnum)
416{
417	print "echo ----Start batch " batchnum ": `date` | tee -a " rd "log";
418	print "needqemurun="
419	jn=1
420	for (j = first; j < pastlast; j++) {
421		builddir=KVM "/b" j - first + 1
422		cpusr[jn] = cpus[j];
423		if (cfrep[cf[j]] == "") {
424			cfr[jn] = cf[j];
425			cfrep[cf[j]] = 1;
426		} else {
427			cfrep[cf[j]]++;
428			cfr[jn] = cf[j] "." cfrep[cf[j]];
429		}
430		if (cpusr[jn] > ncpus && ncpus != 0)
431			ovf = "-ovf";
432		else
433			ovf = "";
434		print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date` | tee -a " rd "log";
435		print "rm -f " builddir ".*";
436		print "touch " builddir ".wait";
437		print "mkdir " rd cfr[jn] " || :";
438		print "kvm-test-1-run.sh " CONFIGDIR cf[j], builddir, rd cfr[jn], dur " \"" TORTURE_QEMU_ARG "\" \"" TORTURE_BOOTARGS "\" > " rd cfr[jn]  "/kvm-test-1-run.sh.out 2>&1 &"
439		print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date` | tee -a " rd "log";
440		print "while test -f " builddir ".wait"
441		print "do"
442		print "\tsleep 1"
443		print "done"
444		print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date` | tee -a " rd "log";
445		jn++;
446	}
447	for (j = 1; j < jn; j++) {
448		builddir=KVM "/b" j
449		print "rm -f " builddir ".ready"
450		print "if test -f \"" rd cfr[j] "/builtkernel\""
451		print "then"
452		print "\techo ----", cfr[j], cpusr[j] ovf ": Kernel present. `date` | tee -a " rd "log";
453		print "\tneedqemurun=1"
454		print "fi"
455	}
456	njitter = 0;
457	split(jitter, ja);
458	if (ja[1] == -1 && ncpus == 0)
459		njitter = 1;
460	else if (ja[1] == -1)
461		njitter = ncpus;
462	else
463		njitter = ja[1];
464	if (TORTURE_BUILDONLY && njitter != 0) {
465		njitter = 0;
466		print "echo Build-only run, so suppressing jitter | tee -a " rd "log"
467	}
468	if (TORTURE_BUILDONLY) {
469		print "needqemurun="
470	}
471	print "if test -n \"$needqemurun\""
472	print "then"
473	print "\techo ---- Starting kernels. `date` | tee -a " rd "log";
474	print "\techo > " rd "jitter_pids"
475	for (j = 0; j < njitter; j++) {
476		print "\tjitter.sh " j " " dur " " ja[2] " " ja[3] "&"
477		print "\techo $! >> " rd "jitter_pids"
478	}
479	print "\twait"
480	print "\techo ---- All kernel runs complete. `date` | tee -a " rd "log";
481	print "else"
482	print "\twait"
483	print "\techo ---- No kernel runs. `date` | tee -a " rd "log";
484	print "fi"
485	for (j = 1; j < jn; j++) {
486		builddir=KVM "/b" j
487		print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results: | tee -a " rd "log";
488		print "cat " rd cfr[j]  "/kvm-test-1-run.sh.out | tee -a " rd "log";
489	}
490}
491
492END {
493	njobs = i;
494	nc = ncpus;
495	first = 0;
496	batchnum = 1;
497
498	# Each pass through the following loop considers one test.
499	for (i = 0; i < njobs; i++) {
500		if (ncpus == 0) {
501			# Sequential test specified, each test its own batch.
502			dump(i, i + 1, batchnum);
503			first = i;
504			batchnum++;
505		} else if (nc < cpus[i] && i != 0) {
506			# Out of CPUs, dump out a batch.
507			dump(first, i, batchnum);
508			first = i;
509			nc = ncpus;
510			batchnum++;
511		}
512		# Account for the CPUs needed by the current test.
513		nc -= cpus[i];
514	}
515	# Dump the last batch.
516	if (ncpus != 0)
517		dump(first, i, batchnum);
518}' >> $T/script
519
520cat << ___EOF___ >> $T/script
521echo
522echo
523echo " --- `date` Test summary:"
524echo Results directory: $resdir/$ds
525kcsan-collapse.sh $resdir/$ds
526kvm-recheck.sh $resdir/$ds
527___EOF___
528
529if test "$dryrun" = script
530then
531	cat $T/script
532	exit 0
533elif test "$dryrun" = sched
534then
535	# Extract the test run schedule from the script.
536	egrep 'Start batch|Starting build\.' $T/script |
537		grep -v ">>" |
538		sed -e 's/:.*$//' -e 's/^echo //'
539	exit 0
540else
541	# Not a dryrun, so run the script.
542	sh $T/script
543fi
544
545# Tracing: trace_event=rcu:rcu_grace_period,rcu:rcu_future_grace_period,rcu:rcu_grace_period_init,rcu:rcu_nocb_wake,rcu:rcu_preempt_task,rcu:rcu_unlock_preempted_task,rcu:rcu_quiescent_state_report,rcu:rcu_fqs,rcu:rcu_callback,rcu:rcu_kfree_callback,rcu:rcu_batch_start,rcu:rcu_invoke_callback,rcu:rcu_invoke_kfree_callback,rcu:rcu_batch_end,rcu:rcu_torture_read,rcu:rcu_barrier
546# Function-graph tracing: ftrace=function_graph ftrace_graph_filter=sched_setaffinity,migration_cpu_stop
547# Also --kconfig "CONFIG_FUNCTION_TRACER=y CONFIG_FUNCTION_GRAPH_TRACER=y"
548# Control buffer size: --bootargs trace_buf_size=3k
549# Get trace-buffer dumps on all oopses: --bootargs ftrace_dump_on_oops
550# Ditto, but dump only the oopsing CPU: --bootargs ftrace_dump_on_oops=orig_cpu
551# Heavy-handed way to also dump on warnings: --bootargs panic_on_warn
552