Skip to contentSkip to conversation

Multi-migration support

PATCH Superseded Nicholas Piggin 2 participants 20 messages Feb 9, 2024

Subsystems (6)
KVM Library code RISC-V s390 +2 more subsystems

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Multi-migration support

Since v2:
- Rebase on riscv port and auxvinfo fix was merged.
- Clean up initrd cleanup moves more commands into the new cleanup function from the trap handler comands (suggested by Thomas).
- "arch-run: Clean up temporary files properly" patch is now renamed to "arch-run: Fix TRAP handler..."
- Fix TRAP handler patch has redone changelog to be more precise about the problem and including recipe to recreate it.
- Fix TRAP handler patch reworked slightly to remove the theoretical race rather than just adding a comment about it.
- Patch 3 was missing a couple of fixes that leaked into patch 4, those are moved into patch 3.

I did look into doing a better job at handling timeouts in places where the migration script can hang (the timeout command only kills the qemu process, but there are other places the bash script itself can still timeout). There are ways it might be possible (along the lines of starting ( sleep N ; kill ) subshell in the backround), but it's very tricky to handle all the details. Existing script has timeout issues already, so this series doesn't add a fundamentally new type of problem here.

Thanks,
Nick

Nicholas Piggin (8):
arch-run: Fix TRAP handler recursion to remove temporary files
properly
arch-run: Clean up initrd cleanup
migration: use a more robust way to wait for background job
migration: Support multiple migrations
arch-run: rename migration variables
migration: Add quiet migration support
Add common/ directory for architecture-independent tests
migration: add a migration selftest

 arm/Makefile.common          |   1 +
 arm/selftest-migration.c     |   1 +
 arm/sieve.c                  |   2 +-
 arm/unittests.cfg            |   6 ++
 common/selftest-migration.c  |  34 +++++++
 common/sieve.c               |  51 ++++++++++
 lib/migrate.c                |  20 +++-
 lib/migrate.h                |   2 +
 powerpc/Makefile.common      |   1 +
 powerpc/selftest-migration.c |   1 +
 powerpc/unittests.cfg        |   4 +
 riscv/sieve.c                |   2 +-
 s390x/Makefile               |   1 +
 s390x/selftest-migration.c   |   1 +
 s390x/sieve.c                |   2 +-
 s390x/unittests.cfg          |   4 +
 scripts/arch-run.bash        | 182 ++++++++++++++++++++++++++---------
 x86/sieve.c                  |  52 +---------
 18 files changed, 261 insertions(+), 106 deletions(-)
 create mode 120000 arm/selftest-migration.c
 create mode 100644 common/selftest-migration.c
 create mode 100644 common/sieve.c
 create mode 120000 powerpc/selftest-migration.c
 create mode 120000 s390x/selftest-migration.c
 mode change 100644 => 120000 x86/sieve.c
Signature · 1 line
2.42.0
#1 lore raw

PATCH v3 1/8 arch-run: Fix TRAP handler recursion to remove temporary files properly

Migration files were not being removed when the QEMU process is interrupted (e.g., with ^C). This is becaus the SIGINT propagates to the bash TRAP handler, which recursively TRAPs due to the 'kill 0' in the handler. This eventually crashes bash.

This can be observed by interrupting a long-running test program that is run with MIGRATION=yes, /tmp/mig-helper-* files remain afterwards.

Removing TRAP recursion solves this problem and allows the EXIT handler to run and clean up the files.

This also moves the trap handler before temp file creation, and expands the name variables at trap-time rather than install-time, which closes the small race between creation trap handler install.

---
 scripts/arch-run.bash | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
scripts/arch-run.bash +6 -6
index d0864360..11d47a85 100644
@@ -129,6 +129,9 @@ run_migration ()
 		return 77
 	fi
 
+	trap 'trap - TERM ; kill 0 ; exit 2' INT TERM
+	trap 'rm -f ${migout1} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
+
 	migsock=$(mktemp -u -t mig-helper-socket.XXXXXXXXXX)
 	migout1=$(mktemp -t mig-helper-stdout1.XXXXXXXXXX)
 	qmp1=$(mktemp -u -t mig-helper-qmp1.XXXXXXXXXX)
@@ -137,9 +140,6 @@ run_migration ()
 	qmpout1=/dev/null
 	qmpout2=/dev/null
 
-	trap 'kill 0; exit 2' INT TERM
-	trap 'rm -f ${migout1} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
-
 	eval "$@" -chardev socket,id=mon1,path=${qmp1},server=on,wait=off \
 		-mon chardev=mon1,mode=control | tee ${migout1} &
 	live_pid=`jobs -l %+ | grep "eval" | awk '{print$2}'`
@@ -209,11 +209,11 @@ run_panic ()
 		return 77
 	fi
 
-	qmp=$(mktemp -u -t panic-qmp.XXXXXXXXXX)
-
-	trap 'kill 0; exit 2' INT TERM
+	trap 'trap - TERM ; kill 0 ; exit 2' INT TERM
 	trap 'rm -f ${qmp}' RETURN EXIT
 
+	qmp=$(mktemp -u -t panic-qmp.XXXXXXXXXX)
+
 	# start VM stopped so we don't miss any events
 	eval "$@" -chardev socket,id=mon1,path=${qmp},server=on,wait=off \
 		-mon chardev=mon1,mode=control -S &
Signature · 1 line
2.42.0
#3 lore raw

PATCH v3 2/8 arch-run: Clean up initrd cleanup

In reply to Nicholas Piggin

Rather than put a big script into the trap handler, have it call a function.

---
 scripts/arch-run.bash | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
scripts/arch-run.bash +12 -1
index 11d47a85..1e903e83 100644
@@ -269,10 +269,21 @@ search_qemu_binary ()
 	export PATH=$save_path
 }
 
+initrd_cleanup ()
+{
+	rm -f $KVM_UNIT_TESTS_ENV
+	if [ "$KVM_UNIT_TESTS_ENV_OLD" ]; then
+		export KVM_UNIT_TESTS_ENV="$KVM_UNIT_TESTS_ENV_OLD"
+	else
+		unset KVM_UNIT_TESTS_ENV
+		unset KVM_UNIT_TESTS_ENV_OLD
+	fi
+}
+
 initrd_create ()
 {
 	if [ "$ENVIRON_DEFAULT" = "yes" ]; then
-		trap_exit_push 'rm -f $KVM_UNIT_TESTS_ENV; [ "$KVM_UNIT_TESTS_ENV_OLD" ] && export KVM_UNIT_TESTS_ENV="$KVM_UNIT_TESTS_ENV_OLD" || unset KVM_UNIT_TESTS_ENV; unset KVM_UNIT_TESTS_ENV_OLD'
+		trap_exit_push 'initrd_cleanup'
 		[ -f "$KVM_UNIT_TESTS_ENV" ] && export KVM_UNIT_TESTS_ENV_OLD="$KVM_UNIT_TESTS_ENV"
 		export KVM_UNIT_TESTS_ENV=$(mktemp)
 		env_params
Signature · 1 line
2.42.0
#6 lore raw

PATCH v3 3/8 migration: use a more robust way to wait for background job

In reply to Nicholas Piggin

Starting a pipeline of jobs in the background does not seem to have a simple way to reliably find the pid of a particular process in the pipeline (because not all processes are started when the shell continues to execute).

The way PID of QEMU is derived can result in a failure waiting on a PID that is not running. This is easier to hit with subsequent multiple-migration support. Changing this to use $! by swapping the pipeline for a fifo is more robust.

---
 scripts/arch-run.bash | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
scripts/arch-run.bash +11 -4
index 1e903e83..3689d7c2 100644
@@ -130,19 +130,22 @@ run_migration ()
 	fi
 
 	trap 'trap - TERM ; kill 0 ; exit 2' INT TERM
-	trap 'rm -f ${migout1} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
+	trap 'rm -f ${migout1} ${migout_fifo1} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
 
 	migsock=$(mktemp -u -t mig-helper-socket.XXXXXXXXXX)
 	migout1=$(mktemp -t mig-helper-stdout1.XXXXXXXXXX)
+	migout_fifo1=$(mktemp -u -t mig-helper-fifo-stdout1.XXXXXXXXXX)
 	qmp1=$(mktemp -u -t mig-helper-qmp1.XXXXXXXXXX)
 	qmp2=$(mktemp -u -t mig-helper-qmp2.XXXXXXXXXX)
 	fifo=$(mktemp -u -t mig-helper-fifo.XXXXXXXXXX)
 	qmpout1=/dev/null
 	qmpout2=/dev/null
 
+	mkfifo ${migout_fifo1}
 	eval "$@" -chardev socket,id=mon1,path=${qmp1},server=on,wait=off \
-		-mon chardev=mon1,mode=control | tee ${migout1} &
-	live_pid=`jobs -l %+ | grep "eval" | awk '{print$2}'`
+		-mon chardev=mon1,mode=control > ${migout_fifo1} &
+	live_pid=$!
+	cat ${migout_fifo1} | tee ${migout1} &
 
 	# We have to use cat to open the named FIFO, because named FIFO's, unlike
 	# pipes, will block on open() until the other end is also opened, and that
@@ -150,7 +153,7 @@ run_migration ()
 	mkfifo ${fifo}
 	eval "$@" -chardev socket,id=mon2,path=${qmp2},server=on,wait=off \
 		-mon chardev=mon2,mode=control -incoming unix:${migsock} < <(cat ${fifo}) &
-	incoming_pid=`jobs -l %+ | awk '{print$2}'`
+	incoming_pid=$!
 
 	# The test must prompt the user to migrate, so wait for the "migrate" keyword
 	while ! grep -q -i "Now migrate the VM" < ${migout1} ; do
@@ -164,6 +167,10 @@ run_migration ()
 		sleep 1
 	done
 
+	# Wait until the destination has created the incoming and qmp sockets
+	while ! [ -S ${migsock} ] ; do sleep 0.1 ; done
+	while ! [ -S ${qmp2} ] ; do sleep 0.1 ; done
+
 	qmp ${qmp1} '"migrate", "arguments": { "uri": "unix:'${migsock}'" }' > ${qmpout1}
 
 	# Wait for the migration to complete
Signature · 1 line
2.42.0
#8 lore raw

PATCH v3 4/8 migration: Support multiple migrations

In reply to Nicholas Piggin

Support multiple migrations by flipping dest file/socket variables to source after the migration is complete, ready to start again. A new destination is created if the test outputs the migrate line again. Test cases may now switch to calling migrate() one or more times.

---
 lib/migrate.c         |  8 ++--
 lib/migrate.h         |  1 +
 scripts/arch-run.bash | 93 +++++++++++++++++++++++++++++++++++++------
 3 files changed, 85 insertions(+), 17 deletions(-)
lib/migrate.c +4 -4
index 527e63ae..b7721659 100644
@@ -8,8 +8,10 @@
 #include <libcflat.h>
 #include "migrate.h"
 
-/* static for now since we only support migrating exactly once per test. */
-static void migrate(void)
+/*
+ * Initiate migration and wait for it to complete.
+ */
+void migrate(void)
 {
 	puts("Now migrate the VM, then press a key to continue...\n");
 	(void)getchar();
@@ -19,8 +21,6 @@ static void migrate(void)
 /*
  * Initiate migration and wait for it to complete.
  * If this function is called more than once, it is a no-op.
- * Since migrate_cmd can only migrate exactly once this function can
- * simplify the control flow, especially when skipping tests.
  */
 void migrate_once(void)
 {
lib/migrate.h +1 -0
index 3c94e6af..2af06a72 100644
@@ -6,4 +6,5 @@
  * Author: Nico Boehr <[email protected]>
  */
 
+void migrate(void);
 void migrate_once(void);
scripts/arch-run.bash +80 -13

138 lines not on this page · raw message

Signature · 1 line
2.42.0
#12 lore raw

PATCH v3 5/8 arch-run: rename migration variables

In reply to Nicholas Piggin

Using 1 and 2 for source and destination is confusing, particularly now with multiple migrations that flip between them. Do a rename pass to tidy things up.

---
 scripts/arch-run.bash | 115 +++++++++++++++++++++---------------------
 1 file changed, 58 insertions(+), 57 deletions(-)
scripts/arch-run.bash +58 -57
index a914ba17..0b45eb61 100644
@@ -132,34 +132,34 @@ run_migration ()
 	migcmdline=$@
 
 	trap 'trap - TERM ; kill 0 ; exit 2' INT TERM
-	trap 'rm -f ${migout1} ${migout2} ${migout_fifo1} ${migout_fifo2} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
-
-	migsock=$(mktemp -u -t mig-helper-socket.XXXXXXXXXX)
-	migout1=$(mktemp -t mig-helper-stdout1.XXXXXXXXXX)
-	migout_fifo1=$(mktemp -u -t mig-helper-fifo-stdout1.XXXXXXXXXX)
-	migout2=$(mktemp -t mig-helper-stdout2.XXXXXXXXXX)
-	migout_fifo2=$(mktemp -u -t mig-helper-fifo-stdout2.XXXXXXXXXX)
-	qmp1=$(mktemp -u -t mig-helper-qmp1.XXXXXXXXXX)
-	qmp2=$(mktemp -u -t mig-helper-qmp2.XXXXXXXXXX)
-	fifo=$(mktemp -u -t mig-helper-fifo.XXXXXXXXXX)
-	qmpout1=/dev/null
-	qmpout2=/dev/null
-
-	mkfifo ${migout_fifo1}
-	mkfifo ${migout_fifo2}
+	trap 'rm -f ${src_out} ${dst_out} ${src_outfifo} ${dst_outfifo} ${dst_incoming} ${src_qmp} ${dst_qmp} ${dst_infifo}' RETURN EXIT
+
+	dst_incoming=$(mktemp -u -t mig-helper-socket-incoming.XXXXXXXXXX)
+	src_out=$(mktemp -t mig-helper-stdout1.XXXXXXXXXX)
+	src_outfifo=$(mktemp -u -t mig-helper-fifo-stdout1.XXXXXXXXXX)
+	dst_out=$(mktemp -t mig-helper-stdout2.XXXXXXXXXX)
+	dst_outfifo=$(mktemp -u -t mig-helper-fifo-stdout2.XXXXXXXXXX)
+	src_qmp=$(mktemp -u -t mig-helper-qmp1.XXXXXXXXXX)
+	dst_qmp=$(mktemp -u -t mig-helper-qmp2.XXXXXXXXXX)
+	dst_infifo=$(mktemp -u -t mig-helper-fifo-stdin.XXXXXXXXXX)
+	src_qmpout=/dev/null
+	dst_qmpout=/dev/null
+
+	mkfifo ${src_outfifo}
+	mkfifo ${dst_outfifo}
 
 	eval "$migcmdline" \
-		-chardev socket,id=mon1,path=${qmp1},server=on,wait=off \
-		-mon chardev=mon1,mode=control > ${migout_fifo1} &
+		-chardev socket,id=mon,path=${src_qmp},server=on,wait=off \
+		-mon chardev=mon,mode=control > ${src_outfifo} &
 	live_pid=$!
-	cat ${migout_fifo1} | tee ${migout1} &
+	cat ${src_outfifo} | tee ${src_out} &
 
 	# The test must prompt the user to migrate, so wait for the "migrate"
 	# keyword
-	while ! grep -q -i "Now migrate the VM" < ${migout1} ; do
+	while ! grep -q -i "Now migrate the VM" < ${src_out} ; do
 		if ! ps -p ${live_pid} > /dev/null ; then
 			echo "ERROR: Test exit before migration point." >&2
-			qmp ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
+			qmp ${src_qmp} '"quit"'> ${src_qmpout} 2>/dev/null
 			return 3
 		fi
 		sleep 0.1
@@ -172,7 +172,7 @@ run_migration ()
 
 	while ps -p ${live_pid} > /dev/null ; do
 		# Wait for EXIT or further migrations
-		if ! grep -q -i "Now migrate the VM" < ${migout1} ; then
+		if ! grep -q -i "Now migrate the VM" < ${src_out} ; then
 			sleep 0.1
 		else
 			do_migration || return $?
@@ -194,79 +194,80 @@ do_migration ()
 	# We have to use cat to open the named FIFO, because named FIFO's,
 	# unlike pipes, will block on open() until the other end is also
 	# opened, and that totally breaks QEMU...
-	mkfifo ${fifo}
+	mkfifo ${dst_infifo}
 	eval "$migcmdline" \
-		-chardev socket,id=mon2,path=${qmp2},server=on,wait=off \
-		-mon chardev=mon2,mode=control -incoming unix:${migsock} \
-		< <(cat ${fifo}) > ${migout_fifo2} &
+		-chardev socket,id=mon,path=${dst_qmp},server=on,wait=off \
+		-mon chardev=mon,mode=control -incoming unix:${dst_incoming} \
+		< <(cat ${dst_infifo}) > ${dst_outfifo} &
 	incoming_pid=$!
-	cat ${migout_fifo2} | tee ${migout2} &
+	cat ${dst_outfifo} | tee ${dst_out} &
 
 	# The test must prompt the user to migrate, so wait for the "migrate" keyword
-	while ! grep -q -i "Now migrate the VM" < ${migout1} ; do
+	while ! grep -q -i "Now migrate the VM" < ${src_out} ; do
 		if ! ps -p ${live_pid} > /dev/null ; then
 			echo "ERROR: Test exit before migration point." >&2
-			echo > ${fifo}
-			qmp ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
-			qmp ${qmp2} '"quit"'> ${qmpout2} 2>/dev/null
+			echo > ${dst_infifo}
+			qmp ${src_qmp} '"quit"'> ${src_qmpout} 2>/dev/null
+			qmp ${dst_qmp} '"quit"'> ${dst_qmpout} 2>/dev/null
 			return 3
 		fi
 		sleep 0.1
 	done
 
 	# Wait until the destination has created the incoming and qmp sockets
-	while ! [ -S ${migsock} ] ; do sleep 0.1 ; done
-	while ! [ -S ${qmp2} ] ; do sleep 0.1 ; done
+	while ! [ -S ${dst_incoming} ] ; do sleep 0.1 ; done
+	while ! [ -S ${dst_qmp} ] ; do sleep 0.1 ; done
 
-	qmp ${qmp1} '"migrate", "arguments": { "uri": "unix:'${migsock}'" }' > ${qmpout1}
+	qmp ${src_qmp} '"migrate", "arguments": { "uri": "unix:'${dst_incoming}'" }' > ${src_qmpout}
 
 	# Wait for the migration to complete
-	migstatus=`qmp ${qmp1} '"query-migrate"' | grep return`
+	migstatus=`qmp ${src_qmp} '"query-migrate"' | grep return`
 	while ! grep -q '"completed"' <<<"$migstatus" ; do
 		sleep 0.1
-		if ! migstatus=`qmp ${qmp1} '"query-migrate"'`; then
+		if ! migstatus=`qmp ${src_qmp} '"query-migrate"'`; then
 			echo "ERROR: Querying migration state failed." >&2
-			echo > ${fifo}
-			qmp ${qmp2} '"quit"'> ${qmpout2} 2>/dev/null
+			echo > ${dst_infifo}
+			qmp ${dst_qmp} '"quit"'> ${dst_qmpout} 2>/dev/null
 			return 2
 		fi
 		migstatus=`grep return <<<"$migstatus"`
 		if grep -q '"failed"' <<<"$migstatus"; then
 			echo "ERROR: Migration failed." >&2
-			echo > ${fifo}
-			qmp ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
-			qmp ${qmp2} '"quit"'> ${qmpout2} 2>/dev/null
+			echo > ${dst_infifo}
+			qmp ${src_qmp} '"quit"'> ${src_qmpout} 2>/dev/null
+			qmp ${dst_qmp} '"quit"'> ${dst_qmpout} 2>/dev/null
 			return 2
 		fi
 	done
 
-	qmp ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
+	qmp ${src_qmp} '"quit"'> ${src_qmpout} 2>/dev/null
 
 	# keypress to dst so getchar completes and test continues
-	echo > ${fifo}
-	rm ${fifo}
+	echo > ${dst_infifo}
+	rm ${dst_infifo}
 
 	# Ensure the incoming socket is removed, ready for next destination
-	if [ -S ${migsock} ] ; then
+	if [ -S ${dst_incoming} ] ; then
 		echo "ERROR: Incoming migration socket not removed after migration." >& 2
-		qmp ${qmp2} '"quit"'> ${qmpout2} 2>/dev/null
+		qmp ${dst_qmp} '"quit"'> ${dst_qmpout} 2>/dev/null
 		return 2
 	fi
 
 	wait ${live_pid}
 	ret=$?
 
-	# Now flip the variables because dest becomes source
+	# Now flip the variables because destination machine becomes source
+	# for the next migration.
 	live_pid=${incoming_pid}
-	tmp=${migout1}
-	migout1=${migout2}
-	migout2=${tmp}
-	tmp=${migout_fifo1}
-	migout_fifo1=${migout_fifo2}
-	migout_fifo2=${tmp}
-	tmp=${qmp1}
-	qmp1=${qmp2}
-	qmp2=${tmp}
+	tmp=${src_out}
+	src_out=${dst_out}
+	dst_out=${tmp}
+	tmp=${src_outfifo}
+	src_outfifo=${dst_outfifo}
+	dst_outfifo=${tmp}
+	tmp=${src_qmp}
+	src_qmp=${dst_qmp}
+	dst_qmp=${tmp}
 
 	return $ret
 }
@@ -289,8 +290,8 @@ run_panic ()
 	qmp=$(mktemp -u -t panic-qmp.XXXXXXXXXX)
 
 	# start VM stopped so we don't miss any events
-	eval "$@" -chardev socket,id=mon1,path=${qmp},server=on,wait=off \
-		-mon chardev=mon1,mode=control -S &
+	eval "$@" -chardev socket,id=mon,path=${qmp},server=on,wait=off \
+		-mon chardev=mon,mode=control -S &
 
 	panic_event_count=$(qmp_events ${qmp} | jq -c 'select(.event == "GUEST_PANICKED")' | wc -l)
 	if [ "$panic_event_count" -lt 1 ]; then
Signature · 1 line
2.42.0
#14 lore raw

PATCH v3 6/8 migration: Add quiet migration support

In reply to Nicholas Piggin

Console output required to support migration becomes quite noisy when doing lots of migrations. Provide a migrate_quiet() call that suppresses console output and doesn't log a message.

---
 lib/migrate.c         | 12 ++++++++++++
 lib/migrate.h         |  1 +
 scripts/arch-run.bash |  4 ++--
 3 files changed, 15 insertions(+), 2 deletions(-)
lib/migrate.c +12 -0
index b7721659..4e0ab516 100644
@@ -18,6 +18,18 @@ void migrate(void)
 	report_info("Migration complete");
 }
 
+/*
+ * Like migrate() but supporess output and logs, useful for intensive
+ * migration stress testing without polluting logs. Test cases should
+ * provide relevant information about migration in failure reports.
+ */
+void migrate_quiet(void)
+{
+	puts("Now migrate the VM (quiet)\n");
+	(void)getchar();
+}
+
+
 /*
  * Initiate migration and wait for it to complete.
  * If this function is called more than once, it is a no-op.
lib/migrate.h +1 -0
index 2af06a72..95b9102b 100644
@@ -7,4 +7,5 @@
  */
 
 void migrate(void);
+void migrate_quiet(void);
 void migrate_once(void);
scripts/arch-run.bash +2 -2
index 0b45eb61..29cf9b0c 100644
@@ -152,7 +152,7 @@ run_migration ()
 		-chardev socket,id=mon,path=${src_qmp},server=on,wait=off \
 		-mon chardev=mon,mode=control > ${src_outfifo} &
 	live_pid=$!
-	cat ${src_outfifo} | tee ${src_out} &
+	cat ${src_outfifo} | tee ${src_out} | grep -v "Now migrate the VM (quiet)" &
 
 	# The test must prompt the user to migrate, so wait for the "migrate"
 	# keyword
@@ -200,7 +200,7 @@ do_migration ()
 		-mon chardev=mon,mode=control -incoming unix:${dst_incoming} \
 		< <(cat ${dst_infifo}) > ${dst_outfifo} &
 	incoming_pid=$!
-	cat ${dst_outfifo} | tee ${dst_out} &
+	cat ${dst_outfifo} | tee ${dst_out} | grep -v "Now migrate the VM (quiet)" &
 
 	# The test must prompt the user to migrate, so wait for the "migrate" keyword
 	while ! grep -q -i "Now migrate the VM" < ${src_out} ; do
Signature · 1 line
2.42.0
#16 lore raw

PATCH v3 7/8 Add common/ directory for architecture-independent tests

In reply to Nicholas Piggin

x86/sieve.c is used by s390x, arm, and riscv via symbolic link. Make a new directory common/ for architecture-independent tests and move sieve.c here.

---
 arm/sieve.c    |  2 +-
 common/sieve.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 riscv/sieve.c  |  2 +-
 s390x/sieve.c  |  2 +-
 x86/sieve.c    | 52 +-------------------------------------------------
 5 files changed, 55 insertions(+), 54 deletions(-)
 create mode 100644 common/sieve.c
 mode change 100644 => 120000 x86/sieve.c
arm/sieve.c +1 -1
index 8f14a5c3..fe299f30 120000
@@ -1 +1 @@
-../x86/sieve.c
\ No newline at end of file
+../common/sieve.c
\ No newline at end of file
common/sieve.c new file +51 -0

51 lines not on this page · raw message

riscv/sieve.c +1 -1
index 8f14a5c3..fe299f30 120000
@@ -1 +1 @@
-../x86/sieve.c
\ No newline at end of file
+../common/sieve.c
\ No newline at end of file
s390x/sieve.c +1 -1
index 8f14a5c3..fe299f30 120000
@@ -1 +1 @@
-../x86/sieve.c
\ No newline at end of file
+../common/sieve.c
\ No newline at end of file
x86/sieve.c deleted +0 -51

51 lines not on this page · raw message

x86/sieve.c new file +1 -0
new file mode 120000
index 00000000..fe299f30
@@ -0,0 +1 @@
+../common/sieve.c
\ No newline@end of file
Signature · 1 line
2.42.0
#18 lore raw

PATCH v3 8/8 migration: add a migration selftest

In reply to Nicholas Piggin

Add a selftest for migration support in guest library and test harness code. It performs migrations a tight loop to irritate races and bugs in the test harness code.

Include the test in arm, s390, powerpc.

---
This has flushed out several bugs in developing the multi migration test harness code already.

Thanks,
Nick

 arm/Makefile.common          |  1 +
 arm/selftest-migration.c     |  1 +
 arm/unittests.cfg            |  6 ++++++
 common/selftest-migration.c  | 34 ++++++++++++++++++++++++++++++++++
 powerpc/Makefile.common      |  1 +
 powerpc/selftest-migration.c |  1 +
 powerpc/unittests.cfg        |  4 ++++
 s390x/Makefile               |  1 +
 s390x/selftest-migration.c   |  1 +
 s390x/unittests.cfg          |  4 ++++
 10 files changed, 54 insertions(+)
 create mode 120000 arm/selftest-migration.c
 create mode 100644 common/selftest-migration.c
 create mode 120000 powerpc/selftest-migration.c
 create mode 120000 s390x/selftest-migration.c
arm/Makefile.common +1 -0
index f828dbe0..f107c478 100644
@@ -5,6 +5,7 @@
 #
 
 tests-common  = $(TEST_DIR)/selftest.$(exe)
+tests-common += $(TEST_DIR)/selftest-migration.$(exe)
 tests-common += $(TEST_DIR)/spinlock-test.$(exe)
 tests-common += $(TEST_DIR)/pci-test.$(exe)
 tests-common += $(TEST_DIR)/pmu.$(exe)
arm/selftest-migration.c new file +1 -0
new file mode 120000
index 00000000..bd1eb266
@@ -0,0 +1 @@
+../common/selftest-migration.c
\ No newline at end of file
arm/unittests.cfg +6 -0
index fe601cbb..1ffd9a82 100644
@@ -55,6 +55,12 @@ smp = $MAX_SMP
 extra_params = -append 'smp'
 groups = selftest
 
+# Test migration
+[selftest-migration]
+file = selftest-migration.flat
+groups = selftest migration
+
+arch = arm64
 # Test PCI emulation
 [pci-test]
 file = pci-test.flat
common/selftest-migration.c new file +34 -0
new file mode 100644
index 00000000..f70c505f
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Machine independent migration tests
+ *
+ * This is just a very simple test that is intended to stress the migration
+ * support in the test harness. This could be expanded to test more guest
+ * library code, but architecture-specific tests should be used to test
+ * migration of tricky machine state.
+ */
+#include <libcflat.h>
+#include <migrate.h>
+
+#if defined(__arm__) || defined(__aarch64__)
+/* arm can only call getchar 15 times */
+#define NR_MIGRATIONS 15
+#else
+#define NR_MIGRATIONS 100
+#endif
+
+int main(int argc, char **argv)
+{
+	int i = 0;
+
+	report_prefix_push("migration");
+
+	for (i = 0; i < NR_MIGRATIONS; i++)
+		migrate_quiet();
+
+	report(true, "simple harness stress test");
+
+	report_prefix_pop();
+
+	return report_summary();
+}
powerpc/Makefile.common +1 -0
index eb88398d..da4a7bbb 100644
@@ -6,6 +6,7 @@
 
 tests-common = \
 	$(TEST_DIR)/selftest.elf \
+	$(TEST_DIR)/selftest-migration.elf \
 	$(TEST_DIR)/spapr_hcall.elf \
 	$(TEST_DIR)/rtas.elf \
 	$(TEST_DIR)/emulator.elf \
powerpc/selftest-migration.c new file +1 -0
new file mode 120000
index 00000000..bd1eb266
@@ -0,0 +1 @@
+../common/selftest-migration.c
\ No newline at end of file
powerpc/unittests.cfg +4 -0
index e71140aa..7ce57de0 100644
@@ -36,6 +36,10 @@ smp = 2
 extra_params = -m 256 -append 'setup smp=2 mem=256'
 groups = selftest
 
+[selftest-migration]
+file = selftest-migration.elf
+groups = selftest migration
+
 [spapr_hcall]
 file = spapr_hcall.elf
 
s390x/Makefile +1 -0
index b72f7578..344d46d6 100644
@@ -1,4 +1,5 @@
 tests = $(TEST_DIR)/selftest.elf
+tests += $(TEST_DIR)/selftest-migration.elf
 tests += $(TEST_DIR)/intercept.elf
 tests += $(TEST_DIR)/emulator.elf
 tests += $(TEST_DIR)/sieve.elf
s390x/selftest-migration.c new file +1 -0
new file mode 120000
index 00000000..bd1eb266
@@ -0,0 +1 @@
+../common/selftest-migration.c
\ No newline@end of file
s390x/unittests.cfg +4 -0
index f5024b6e..a7ad522c 100644
@@ -24,6 +24,10 @@ groups = selftest
 # please keep the kernel cmdline in sync with $(TEST_DIR)/selftest.parmfile
 extra_params = -append 'test 123'
 
+[selftest-migration]
+file = selftest-migration.elf
+groups = selftest migration
+
 [intercept]
 file = intercept.elf
 
Signature · 1 line
2.42.0
Reviewed-by on v3 1/8
#2 lore raw

Re: [kvm-unit-tests PATCH v3 1/8] arch-run: Fix TRAP handler recursion to remove temporary files properly

In reply to Nicholas Piggin
Quoted 50 lines · Nicholas Piggin

Migration files were not being removed when the QEMU process is interrupted (e.g., with ^C). This is becaus the SIGINT propagates to the bash TRAP handler, which recursively TRAPs due to the 'kill 0' in the handler. This eventually crashes bash.

This can be observed by interrupting a long-running test program that is run with MIGRATION=yes, /tmp/mig-helper-* files remain afterwards.

Removing TRAP recursion solves this problem and allows the EXIT handler to run and clean up the files.

This also moves the trap handler before temp file creation, and expands the name variables at trap-time rather than install-time, which closes the small race between creation trap handler install.

---
  scripts/arch-run.bash | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
scripts/arch-run.bash +3 -4
index d0864360..11d47a85 100644
@@ -129,6 +129,9 @@ run_migration ()
  		return 77
  	fi
 
+	trap 'trap - TERM ; kill 0 ; exit 2' INT TERM
+	trap 'rm -f ${migout1} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
+
  	migsock=$(mktemp -u -t mig-helper-socket.XXXXXXXXXX)
  	migout1=$(mktemp -t mig-helper-stdout1.XXXXXXXXXX)
  	qmp1=$(mktemp -u -t mig-helper-qmp1.XXXXXXXXXX)
@@ -137,9 +140,6 @@ run_migration ()
  	qmpout1=/dev/null
  	qmpout2=/dev/null
 
-	trap 'kill 0; exit 2' INT TERM
-	trap 'rm -f ${migout1} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
-
  	eval "$@" -chardev socket,id=mon1,path=${qmp1},server=on,wait=off \
  		-mon chardev=mon1,mode=control | tee ${migout1} &
  	live_pid=`jobs -l %+ | grep "eval" | awk '{print$2}'`
@@ -209,11 +209,11 @@ run_panic ()
  		return 77
  	fi
 
-	qmp=$(mktemp -u -t panic-qmp.XXXXXXXXXX)

scripts/arch-run.bash run_panic () · line 219

Quoted 10 lines
scripts/arch-run.bash +3 -2
-
-	trap 'kill 0; exit 2' INT TERM
+	trap 'trap - TERM ; kill 0 ; exit 2' INT TERM
  	trap 'rm -f ${qmp}' RETURN EXIT
 
+	qmp=$(mktemp -u -t panic-qmp.XXXXXXXXXX)
+
  	# start VM stopped so we don't miss any events
  	eval "$@" -chardev socket,id=mon1,path=${qmp},server=on,wait=off \
  		-mon chardev=mon1,mode=control -S &
#4 lore raw

Re: [kvm-unit-tests PATCH v3 2/8] arch-run: Clean up initrd cleanup

In reply to Nicholas Piggin
Quoted 16 lines · Nicholas Piggin

Rather than put a big script into the trap handler, have it call a function.

---
  scripts/arch-run.bash | 13 ++++++++++++-
  1 file changed, 12 insertions(+), 1 deletion(-)
scripts/arch-run.bash +0 -0
index 11d47a85..1e903e83 100644
@@ -269,10 +269,21 @@ search_qemu_binary ()
  	export PATH=$save_path
  }
 

scripts/arch-run.bash search_qemu_binary () · line 281

Quoted 10 lines
scripts/arch-run.bash +10 -0
+initrd_cleanup ()
+{
+	rm -f $KVM_UNIT_TESTS_ENV
+	if [ "$KVM_UNIT_TESTS_ENV_OLD" ]; then
+		export KVM_UNIT_TESTS_ENV="$KVM_UNIT_TESTS_ENV_OLD"
+	else
+		unset KVM_UNIT_TESTS_ENV
+		unset KVM_UNIT_TESTS_ENV_OLD
+	fi
+}

Looking at the original code below, shouldn't this rather unset KVM_UNIT_TESTS_ENV_OLD after the "fi" statement?

Thomas

Quoted 8 lines
scripts/arch-run.bash +1 -1
  initrd_create ()
  {
  	if [ "$ENVIRON_DEFAULT" = "yes" ]; then
-		trap_exit_push 'rm -f $KVM_UNIT_TESTS_ENV; [ "$KVM_UNIT_TESTS_ENV_OLD" ] && export KVM_UNIT_TESTS_ENV="$KVM_UNIT_TESTS_ENV_OLD" || unset KVM_UNIT_TESTS_ENV; unset KVM_UNIT_TESTS_ENV_OLD'
+		trap_exit_push 'initrd_cleanup'
  		[ -f "$KVM_UNIT_TESTS_ENV" ] && export KVM_UNIT_TESTS_ENV_OLD="$KVM_UNIT_TESTS_ENV"
  		export KVM_UNIT_TESTS_ENV=$(mktemp)
  		env_params
Reviewed-by on v3 3/8
#7 lore raw

Re: [kvm-unit-tests PATCH v3 3/8] migration: use a more robust way to wait for background job

In reply to Nicholas Piggin
Quoted 55 lines · Nicholas Piggin

Starting a pipeline of jobs in the background does not seem to have a simple way to reliably find the pid of a particular process in the pipeline (because not all processes are started when the shell continues to execute).

The way PID of QEMU is derived can result in a failure waiting on a PID that is not running. This is easier to hit with subsequent multiple-migration support. Changing this to use $! by swapping the pipeline for a fifo is more robust.

---
  scripts/arch-run.bash | 15 +++++++++++----
  1 file changed, 11 insertions(+), 4 deletions(-)
scripts/arch-run.bash +7 -4
index 1e903e83..3689d7c2 100644
@@ -130,19 +130,22 @@ run_migration ()
  	fi
 
  	trap 'trap - TERM ; kill 0 ; exit 2' INT TERM
-	trap 'rm -f ${migout1} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
+	trap 'rm -f ${migout1} ${migout_fifo1} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
 
  	migsock=$(mktemp -u -t mig-helper-socket.XXXXXXXXXX)
  	migout1=$(mktemp -t mig-helper-stdout1.XXXXXXXXXX)
+	migout_fifo1=$(mktemp -u -t mig-helper-fifo-stdout1.XXXXXXXXXX)
  	qmp1=$(mktemp -u -t mig-helper-qmp1.XXXXXXXXXX)
  	qmp2=$(mktemp -u -t mig-helper-qmp2.XXXXXXXXXX)
  	fifo=$(mktemp -u -t mig-helper-fifo.XXXXXXXXXX)
  	qmpout1=/dev/null
  	qmpout2=/dev/null
 
+	mkfifo ${migout_fifo1}
  	eval "$@" -chardev socket,id=mon1,path=${qmp1},server=on,wait=off \
-		-mon chardev=mon1,mode=control | tee ${migout1} &
-	live_pid=`jobs -l %+ | grep "eval" | awk '{print$2}'`
+		-mon chardev=mon1,mode=control > ${migout_fifo1} &
+	live_pid=$!
+	cat ${migout_fifo1} | tee ${migout1} &
 
  	# We have to use cat to open the named FIFO, because named FIFO's, unlike
  	# pipes, will block on open() until the other end is also opened, and that
@@ -150,7 +153,7 @@ run_migration ()
  	mkfifo ${fifo}
  	eval "$@" -chardev socket,id=mon2,path=${qmp2},server=on,wait=off \
  		-mon chardev=mon2,mode=control -incoming unix:${migsock} < <(cat ${fifo}) &
-	incoming_pid=`jobs -l %+ | awk '{print$2}'`
+	incoming_pid=$!
 
  	# The test must prompt the user to migrate, so wait for the "migrate" keyword
  	while ! grep -q -i "Now migrate the VM" < ${migout1} ; do

scripts/arch-run.bash run_migration () · line 176

Quoted 10 lines
scripts/arch-run.bash +4 -0
@@ -164,6 +167,10 @@ run_migration ()
  		sleep 1
  	done
 
+	# Wait until the destination has created the incoming and qmp sockets
+	while ! [ -S ${migsock} ] ; do sleep 0.1 ; done
+	while ! [ -S ${qmp2} ] ; do sleep 0.1 ; done
+
  	qmp ${qmp1} '"migrate", "arguments": { "uri": "unix:'${migsock}'" }' > ${qmpout1}
 
  	# Wait for the migration to complete
#15 lore raw

Re: [kvm-unit-tests PATCH v3 6/8] migration: Add quiet migration support

In reply to Nicholas Piggin
Quoted 16 lines · Nicholas Piggin

Console output required to support migration becomes quite noisy when doing lots of migrations. Provide a migrate_quiet() call that suppresses console output and doesn't log a message.

---
  lib/migrate.c         | 12 ++++++++++++
  lib/migrate.h         |  1 +
  scripts/arch-run.bash |  4 ++--
  3 files changed, 15 insertions(+), 2 deletions(-)
lib/migrate.c +0 -0
index b7721659..4e0ab516 100644

lib/migrate.c void migrate(void) · line 22

Quoted 5 lines
lib/migrate.c +2 -0
@@ -18,6 +18,18 @@ void migrate(void)
  	report_info("Migration complete");
  }
 
+/*
+ * Like migrate() but supporess output and logs, useful for intensive

s/supporess/suppress/

lib/migrate.c void migrate(void) · line 32

Quoted 10 lines
lib/migrate.c +10 -0
+ * migration stress testing without polluting logs. Test cases should
+ * provide relevant information about migration in failure reports.
+ */
+void migrate_quiet(void)
+{
+	puts("Now migrate the VM (quiet)\n");
+	(void)getchar();
+}
+
+

Remove one empty line, please!

Quoted 27 lines
lib/migrate.c +0 -0
  /*
   * Initiate migration and wait for it to complete.
   * If this function is called more than once, it is a no-op.
lib/migrate.h +1 -0
index 2af06a72..95b9102b 100644
@@ -7,4 +7,5 @@
   */
 
  void migrate(void);
+void migrate_quiet(void);
  void migrate_once(void);
scripts/arch-run.bash +1 -1
index 0b45eb61..29cf9b0c 100644
@@ -152,7 +152,7 @@ run_migration ()
  		-chardev socket,id=mon,path=${src_qmp},server=on,wait=off \
  		-mon chardev=mon,mode=control > ${src_outfifo} &
  	live_pid=$!
-	cat ${src_outfifo} | tee ${src_out} &
+	cat ${src_outfifo} | tee ${src_out} | grep -v "Now migrate the VM (quiet)" &
 
  	# The test must prompt the user to migrate, so wait for the "migrate"
  	# keyword

scripts/arch-run.bash do_migration () · line 206

Quoted 8 lines
scripts/arch-run.bash +1 -1
@@ -200,7 +200,7 @@ do_migration ()
  		-mon chardev=mon,mode=control -incoming unix:${dst_incoming} \
  		< <(cat ${dst_infifo}) > ${dst_outfifo} &
  	incoming_pid=$!
-	cat ${dst_outfifo} | tee ${dst_out} &
+	cat ${dst_outfifo} | tee ${dst_out} | grep -v "Now migrate the VM (quiet)" &
 
  	# The test must prompt the user to migrate, so wait for the "migrate" keyword
  	while ! grep -q -i "Now migrate the VM" < ${src_out} ; do

Thomas

#9 lore raw

Re: [kvm-unit-tests PATCH v3 4/8] migration: Support multiple migrations

In reply to Nicholas Piggin
Quoted 7 lines · Nicholas Piggin

Support multiple migrations by flipping dest file/socket variables to source after the migration is complete, ready to start again. A new destination is created if the test outputs the migrate line again. Test cases may now switch to calling migrate() one or more times.

---

...

Quoted 80 lines
scripts/arch-run.bash +49 -6
index 3689d7c2..a914ba17 100644
@@ -129,12 +129,16 @@ run_migration ()
  		return 77
  	fi
 
+	migcmdline=$@
+
  	trap 'trap - TERM ; kill 0 ; exit 2' INT TERM
-	trap 'rm -f ${migout1} ${migout_fifo1} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
+	trap 'rm -f ${migout1} ${migout2} ${migout_fifo1} ${migout_fifo2} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
 
  	migsock=$(mktemp -u -t mig-helper-socket.XXXXXXXXXX)
  	migout1=$(mktemp -t mig-helper-stdout1.XXXXXXXXXX)
  	migout_fifo1=$(mktemp -u -t mig-helper-fifo-stdout1.XXXXXXXXXX)
+	migout2=$(mktemp -t mig-helper-stdout2.XXXXXXXXXX)
+	migout_fifo2=$(mktemp -u -t mig-helper-fifo-stdout2.XXXXXXXXXX)
  	qmp1=$(mktemp -u -t mig-helper-qmp1.XXXXXXXXXX)
  	qmp2=$(mktemp -u -t mig-helper-qmp2.XXXXXXXXXX)
  	fifo=$(mktemp -u -t mig-helper-fifo.XXXXXXXXXX)
@@ -142,18 +146,61 @@ run_migration ()
  	qmpout2=/dev/null
 
  	mkfifo ${migout_fifo1}
-	eval "$@" -chardev socket,id=mon1,path=${qmp1},server=on,wait=off \
+	mkfifo ${migout_fifo2}
+
+	eval "$migcmdline" \
+		-chardev socket,id=mon1,path=${qmp1},server=on,wait=off \
  		-mon chardev=mon1,mode=control > ${migout_fifo1} &
  	live_pid=$!
  	cat ${migout_fifo1} | tee ${migout1} &
 
-	# We have to use cat to open the named FIFO, because named FIFO's, unlike
-	# pipes, will block on open() until the other end is also opened, and that
-	# totally breaks QEMU...
+	# The test must prompt the user to migrate, so wait for the "migrate"
+	# keyword
+	while ! grep -q -i "Now migrate the VM" < ${migout1} ; do
+		if ! ps -p ${live_pid} > /dev/null ; then
+			echo "ERROR: Test exit before migration point." >&2
+			qmp ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
+			return 3
+		fi
+		sleep 0.1
+	done
+
+	# This starts the first source QEMU in advance of the test reaching the
+	# migration point, since we expect at least one migration. Subsequent
+	# sources are started as the test hits migrate keywords.
+	do_migration || return $?
+
+	while ps -p ${live_pid} > /dev/null ; do
+		# Wait for EXIT or further migrations
+		if ! grep -q -i "Now migrate the VM" < ${migout1} ; then
+			sleep 0.1
+		else
+			do_migration || return $?
+		fi
+	done
+
+	wait ${live_pid}
+	ret=$?
+
+	while (( $(jobs -r | wc -l) > 0 )); do
+		sleep 0.1
+	done
+
+	return $ret
+}
+
+do_migration ()
+{
+	# We have to use cat to open the named FIFO, because named FIFO's,
+	# unlike pipes, will block on open() until the other end is also
+	# opened, and that totally breaks QEMU...
  	mkfifo ${fifo}
-	eval "$@" -chardev socket,id=mon2,path=${qmp2},server=on,wait=off \

scripts/arch-run.bash run_migration () · line 206

Quoted 10 lines
scripts/arch-run.bash +5 -1
-		-mon chardev=mon2,mode=control -incoming unix:${migsock} < <(cat ${fifo}) &
+	eval "$migcmdline" \
+		-chardev socket,id=mon2,path=${qmp2},server=on,wait=off \
+		-mon chardev=mon2,mode=control -incoming unix:${migsock} \
+		< <(cat ${fifo}) > ${migout_fifo2} &
  	incoming_pid=$!
+	cat ${migout_fifo2} | tee ${migout2} &
 
  	# The test must prompt the user to migrate, so wait for the "migrate" keyword
  	while ! grep -q -i "Now migrate the VM" < ${migout1} ; do

So the old check for the "migrate" keyword is also still around? Why do we need to wait on two spots for the "Now mirgrate..." string now?

Thomas

Quoted 57 lines
scripts/arch-run.bash +26 -6
@@ -164,7 +211,7 @@ run_migration ()
  			qmp ${qmp2} '"quit"'> ${qmpout2} 2>/dev/null
  			return 3
  		fi
-		sleep 1
+		sleep 0.1
  	done
 
  	# Wait until the destination has created the incoming and qmp sockets
@@ -176,7 +223,7 @@ run_migration ()
  	# Wait for the migration to complete
  	migstatus=`qmp ${qmp1} '"query-migrate"' | grep return`
  	while ! grep -q '"completed"' <<<"$migstatus" ; do
-		sleep 1
+		sleep 0.1
  		if ! migstatus=`qmp ${qmp1} '"query-migrate"'`; then
  			echo "ERROR: Querying migration state failed." >&2
  			echo > ${fifo}
@@ -192,14 +239,34 @@ run_migration ()
  			return 2
  		fi
  	done
+
  	qmp ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
+
+	# keypress to dst so getchar completes and test continues
  	echo > ${fifo}
-	wait $incoming_pid
+	rm ${fifo}
+
+	# Ensure the incoming socket is removed, ready for next destination
+	if [ -S ${migsock} ] ; then
+		echo "ERROR: Incoming migration socket not removed after migration." >& 2
+		qmp ${qmp2} '"quit"'> ${qmpout2} 2>/dev/null
+		return 2
+	fi
+
+	wait ${live_pid}
  	ret=$?
 
-	while (( $(jobs -r | wc -l) > 0 )); do
-		sleep 0.5
-	done
+	# Now flip the variables because dest becomes source
+	live_pid=${incoming_pid}
+	tmp=${migout1}
+	migout1=${migout2}
+	migout2=${tmp}
+	tmp=${migout_fifo1}
+	migout_fifo1=${migout_fifo2}
+	migout_fifo2=${tmp}
+	tmp=${qmp1}
+	qmp1=${qmp2}
+	qmp2=${tmp}
 
  	return $ret
  }
#5 lore raw

Re: [kvm-unit-tests PATCH v3 2/8] arch-run: Clean up initrd cleanup

In reply to Thomas Huth
Quoted 17 lines · Thomas Huth
Quoted 16 lines · Nicholas Piggin

Rather than put a big script into the trap handler, have it call a function.

---
  scripts/arch-run.bash | 13 ++++++++++++-
  1 file changed, 12 insertions(+), 1 deletion(-)
scripts/arch-run.bash +0 -0
index 11d47a85..1e903e83 100644
@@ -269,10 +269,21 @@ search_qemu_binary ()
  	export PATH=$save_path
  }
 
Quoted 13 lines

scripts/arch-run.bash search_qemu_binary () · line 281

Quoted 10 lines
scripts/arch-run.bash +10 -0
+initrd_cleanup ()
+{
+	rm -f $KVM_UNIT_TESTS_ENV
+	if [ "$KVM_UNIT_TESTS_ENV_OLD" ]; then
+		export KVM_UNIT_TESTS_ENV="$KVM_UNIT_TESTS_ENV_OLD"
+	else
+		unset KVM_UNIT_TESTS_ENV
+		unset KVM_UNIT_TESTS_ENV_OLD
+	fi
+}

Looking at the original code below, shouldn't this rather unset KVM_UNIT_TESTS_ENV_OLD after the "fi" statement?

Yes good catch.

Thanks,
Nick

Acked-by on v3 5/8
#13 lore raw

Re: [kvm-unit-tests PATCH v3 5/8] arch-run: rename migration variables

In reply to Nicholas Piggin
Quoted 8 lines · Nicholas Piggin

Using 1 and 2 for source and destination is confusing, particularly now with multiple migrations that flip between them. Do a rename pass to tidy things up.

---
  scripts/arch-run.bash | 115 +++++++++++++++++++++---------------------
  1 file changed, 58 insertions(+), 57 deletions(-)
Reviewed-by on v3 7/8
#17 lore raw

Re: [kvm-unit-tests PATCH v3 7/8] Add common/ directory for architecture-independent tests

In reply to Nicholas Piggin
Quoted 12 lines · Nicholas Piggin

x86/sieve.c is used by s390x, arm, and riscv via symbolic link. Make a new directory common/ for architecture-independent tests and move sieve.c here.

---
  arm/sieve.c    |  2 +-
  common/sieve.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
  riscv/sieve.c  |  2 +-
  s390x/sieve.c  |  2 +-
  x86/sieve.c    | 52 +-------------------------------------------------
  5 files changed, 55 insertions(+), 54 deletions(-)
Quoted 2 lines

create mode 100644 common/sieve.c
mode change 100644 => 120000 x86/sieve.c

Reviewed-by on v3 8/8
#19 lore raw

Re: [kvm-unit-tests PATCH v3 8/8] migration: add a migration selftest

In reply to Nicholas Piggin
Quoted 2 lines · Nicholas Piggin

Add a selftest for migration support in guest library and test harness code. It performs migrations a tight loop to irritate races and bugs in

"*in* a tight loop" ?

Quoted 54 lines

the test harness code.

Include the test in arm, s390, powerpc.

---
This has flushed out several bugs in developing the multi migration test harness code already.

Thanks,
Nick

  arm/Makefile.common          |  1 +
  arm/selftest-migration.c     |  1 +
  arm/unittests.cfg            |  6 ++++++
  common/selftest-migration.c  | 34 ++++++++++++++++++++++++++++++++++
  powerpc/Makefile.common      |  1 +
  powerpc/selftest-migration.c |  1 +
  powerpc/unittests.cfg        |  4 ++++
  s390x/Makefile               |  1 +
  s390x/selftest-migration.c   |  1 +
  s390x/unittests.cfg          |  4 ++++
  10 files changed, 54 insertions(+)

create mode 120000 arm/selftest-migration.c
create mode 100644 common/selftest-migration.c
create mode 120000 powerpc/selftest-migration.c
create mode 120000 s390x/selftest-migration.c

arm/Makefile.common +1 -0
index f828dbe0..f107c478 100644
@@ -5,6 +5,7 @@
  #
 
  tests-common  = $(TEST_DIR)/selftest.$(exe)
+tests-common += $(TEST_DIR)/selftest-migration.$(exe)
  tests-common += $(TEST_DIR)/spinlock-test.$(exe)
  tests-common += $(TEST_DIR)/pci-test.$(exe)
  tests-common += $(TEST_DIR)/pmu.$(exe)
arm/selftest-migration.c new file +1 -0
new file mode 120000
index 00000000..bd1eb266
@@ -0,0 +1 @@
+../common/selftest-migration.c
\ No newline at end of file
arm/unittests.cfg +0 -0
index fe601cbb..1ffd9a82 100644

arm/unittests.cfg smp = $MAX_SMP · line 63

Quoted 9 lines
arm/unittests.cfg +6 -0
@@ -55,6 +55,12 @@ smp = $MAX_SMP
  extra_params = -append 'smp'
  groups = selftest
 
+# Test migration
+[selftest-migration]
+file = selftest-migration.flat
+groups = selftest migration
+
+arch = arm64

Please swap the last two lines!

arm/unittests.cfg smp = $MAX_SMP · line 66

Quoted 3 lines
arm/unittests.cfg +0 -0
  # Test PCI emulation
  [pci-test]
  file = pci-test.flat

With the nits fixed:

#10 lore raw

Re: [kvm-unit-tests PATCH v3 4/8] migration: Support multiple migrations

In reply to Thomas Huth
Quoted 97 lines · Thomas Huth
Quoted 7 lines · Nicholas Piggin

Support multiple migrations by flipping dest file/socket variables to source after the migration is complete, ready to start again. A new destination is created if the test outputs the migrate line again. Test cases may now switch to calling migrate() one or more times.

---

...

Quoted 88 lines
scripts/arch-run.bash +54 -7
index 3689d7c2..a914ba17 100644
@@ -129,12 +129,16 @@ run_migration ()
  		return 77
  	fi
 
+	migcmdline=$@
+
  	trap 'trap - TERM ; kill 0 ; exit 2' INT TERM
-	trap 'rm -f ${migout1} ${migout_fifo1} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
+	trap 'rm -f ${migout1} ${migout2} ${migout_fifo1} ${migout_fifo2} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
 
  	migsock=$(mktemp -u -t mig-helper-socket.XXXXXXXXXX)
  	migout1=$(mktemp -t mig-helper-stdout1.XXXXXXXXXX)
  	migout_fifo1=$(mktemp -u -t mig-helper-fifo-stdout1.XXXXXXXXXX)
+	migout2=$(mktemp -t mig-helper-stdout2.XXXXXXXXXX)
+	migout_fifo2=$(mktemp -u -t mig-helper-fifo-stdout2.XXXXXXXXXX)
  	qmp1=$(mktemp -u -t mig-helper-qmp1.XXXXXXXXXX)
  	qmp2=$(mktemp -u -t mig-helper-qmp2.XXXXXXXXXX)
  	fifo=$(mktemp -u -t mig-helper-fifo.XXXXXXXXXX)
@@ -142,18 +146,61 @@ run_migration ()
  	qmpout2=/dev/null
 
  	mkfifo ${migout_fifo1}
-	eval "$@" -chardev socket,id=mon1,path=${qmp1},server=on,wait=off \
+	mkfifo ${migout_fifo2}
+
+	eval "$migcmdline" \
+		-chardev socket,id=mon1,path=${qmp1},server=on,wait=off \
  		-mon chardev=mon1,mode=control > ${migout_fifo1} &
  	live_pid=$!
  	cat ${migout_fifo1} | tee ${migout1} &
 
-	# We have to use cat to open the named FIFO, because named FIFO's, unlike
-	# pipes, will block on open() until the other end is also opened, and that
-	# totally breaks QEMU...
+	# The test must prompt the user to migrate, so wait for the "migrate"
+	# keyword
+	while ! grep -q -i "Now migrate the VM" < ${migout1} ; do
+		if ! ps -p ${live_pid} > /dev/null ; then
+			echo "ERROR: Test exit before migration point." >&2
+			qmp ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
+			return 3
+		fi
+		sleep 0.1
+	done
+
+	# This starts the first source QEMU in advance of the test reaching the
+	# migration point, since we expect at least one migration. Subsequent
+	# sources are started as the test hits migrate keywords.
+	do_migration || return $?
+
+	while ps -p ${live_pid} > /dev/null ; do
+		# Wait for EXIT or further migrations
+		if ! grep -q -i "Now migrate the VM" < ${migout1} ; then
+			sleep 0.1
+		else
+			do_migration || return $?
+		fi
+	done
+
+	wait ${live_pid}
+	ret=$?
+
+	while (( $(jobs -r | wc -l) > 0 )); do
+		sleep 0.1
+	done
+
+	return $ret
+}
+
+do_migration ()
+{
+	# We have to use cat to open the named FIFO, because named FIFO's,
+	# unlike pipes, will block on open() until the other end is also
+	# opened, and that totally breaks QEMU...
  	mkfifo ${fifo}
-	eval "$@" -chardev socket,id=mon2,path=${qmp2},server=on,wait=off \
-		-mon chardev=mon2,mode=control -incoming unix:${migsock} < <(cat ${fifo}) &
+	eval "$migcmdline" \
+		-chardev socket,id=mon2,path=${qmp2},server=on,wait=off \
+		-mon chardev=mon2,mode=control -incoming unix:${migsock} \
+		< <(cat ${fifo}) > ${migout_fifo2} &
  	incoming_pid=$!
+	cat ${migout_fifo2} | tee ${migout2} &
 
Quoted 4 lines

scripts/arch-run.bash run_migration () · line 206

Quoted 2 lines
scripts/arch-run.bash +0 -0
  	# The test must prompt the user to migrate, so wait for the "migrate" keyword
  	while ! grep -q -i "Now migrate the VM" < ${migout1} ; do

So the old check for the "migrate" keyword is also still around?

It's just the comment is staleish, it only checks "Now migrate...".

Quoted 2 lines

Why do we
need to wait on two spots for the "Now mirgrate..." string now?

So that the it ensures we do one migration, subsequent ones are optional.

I was thinking we could just remove that, and possibly even remove the MIGRATION=yes/no paths and always just use the same code here. But that's for another time.

Actually there is some weirdness here. There are *three* spots where it waits for migration. The first one in run_migration can be removed, because it can call do_migration right away to start up the destination qemu process ahead of the first migration message as-per comment. I'll respin with that change.

Thanks,
Nick

#11 lore raw

Re: [kvm-unit-tests PATCH v3 4/8] migration: Support multiple migrations

Quoted 111 lines · Nicholas Piggin
Quoted 97 lines · Thomas Huth
Quoted 7 lines · Nicholas Piggin

Support multiple migrations by flipping dest file/socket variables to source after the migration is complete, ready to start again. A new destination is created if the test outputs the migrate line again. Test cases may now switch to calling migrate() one or more times.

---

...

Quoted 88 lines
scripts/arch-run.bash +54 -7
index 3689d7c2..a914ba17 100644
@@ -129,12 +129,16 @@ run_migration ()
   		return 77
   	fi
 
+	migcmdline=$@
+
   	trap 'trap - TERM ; kill 0 ; exit 2' INT TERM
-	trap 'rm -f ${migout1} ${migout_fifo1} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
+	trap 'rm -f ${migout1} ${migout2} ${migout_fifo1} ${migout_fifo2} ${migsock} ${qmp1} ${qmp2} ${fifo}' RETURN EXIT
 
   	migsock=$(mktemp -u -t mig-helper-socket.XXXXXXXXXX)
   	migout1=$(mktemp -t mig-helper-stdout1.XXXXXXXXXX)
   	migout_fifo1=$(mktemp -u -t mig-helper-fifo-stdout1.XXXXXXXXXX)
+	migout2=$(mktemp -t mig-helper-stdout2.XXXXXXXXXX)
+	migout_fifo2=$(mktemp -u -t mig-helper-fifo-stdout2.XXXXXXXXXX)
   	qmp1=$(mktemp -u -t mig-helper-qmp1.XXXXXXXXXX)
   	qmp2=$(mktemp -u -t mig-helper-qmp2.XXXXXXXXXX)
   	fifo=$(mktemp -u -t mig-helper-fifo.XXXXXXXXXX)
@@ -142,18 +146,61 @@ run_migration ()
   	qmpout2=/dev/null
 
   	mkfifo ${migout_fifo1}
-	eval "$@" -chardev socket,id=mon1,path=${qmp1},server=on,wait=off \
+	mkfifo ${migout_fifo2}
+
+	eval "$migcmdline" \
+		-chardev socket,id=mon1,path=${qmp1},server=on,wait=off \
   		-mon chardev=mon1,mode=control > ${migout_fifo1} &
   	live_pid=$!
   	cat ${migout_fifo1} | tee ${migout1} &
 
-	# We have to use cat to open the named FIFO, because named FIFO's, unlike
-	# pipes, will block on open() until the other end is also opened, and that
-	# totally breaks QEMU...
+	# The test must prompt the user to migrate, so wait for the "migrate"
+	# keyword
+	while ! grep -q -i "Now migrate the VM" < ${migout1} ; do
+		if ! ps -p ${live_pid} > /dev/null ; then
+			echo "ERROR: Test exit before migration point." >&2
+			qmp ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
+			return 3
+		fi
+		sleep 0.1
+	done
+
+	# This starts the first source QEMU in advance of the test reaching the
+	# migration point, since we expect at least one migration. Subsequent
+	# sources are started as the test hits migrate keywords.
+	do_migration || return $?
+
+	while ps -p ${live_pid} > /dev/null ; do
+		# Wait for EXIT or further migrations
+		if ! grep -q -i "Now migrate the VM" < ${migout1} ; then
+			sleep 0.1
+		else
+			do_migration || return $?
+		fi
+	done
+
+	wait ${live_pid}
+	ret=$?
+
+	while (( $(jobs -r | wc -l) > 0 )); do
+		sleep 0.1
+	done
+
+	return $ret
+}
+
+do_migration ()
+{
+	# We have to use cat to open the named FIFO, because named FIFO's,
+	# unlike pipes, will block on open() until the other end is also
+	# opened, and that totally breaks QEMU...
   	mkfifo ${fifo}
-	eval "$@" -chardev socket,id=mon2,path=${qmp2},server=on,wait=off \
-		-mon chardev=mon2,mode=control -incoming unix:${migsock} < <(cat ${fifo}) &
+	eval "$migcmdline" \
+		-chardev socket,id=mon2,path=${qmp2},server=on,wait=off \
+		-mon chardev=mon2,mode=control -incoming unix:${migsock} \
+		< <(cat ${fifo}) > ${migout_fifo2} &
   	incoming_pid=$!
+	cat ${migout_fifo2} | tee ${migout2} &
 
Quoted 4 lines

scripts/arch-run.bash run_migration () · line 206

Quoted 2 lines
scripts/arch-run.bash +0 -0
   	# The test must prompt the user to migrate, so wait for the "migrate" keyword
   	while ! grep -q -i "Now migrate the VM" < ${migout1} ; do

So the old check for the "migrate" keyword is also still around?

It's just the comment is staleish, it only checks "Now migrate...".

Quoted 2 lines

Why do we
need to wait on two spots for the "Now mirgrate..." string now?

So that the it ensures we do one migration, subsequent ones are optional.

Quoted 6 lines

I was thinking we could just remove that, and possibly even remove the MIGRATION=yes/no paths and always just use the same code here. But that's for another time.

Actually there is some weirdness here. There are *three* spots where it waits for migration.

Yes, that's what I meant (I considered your two new additions like one spot ;-))

Quoted 4 lines

The first one in run_migration
can be removed, because it can call do_migration right away to start up the destination qemu process ahead of the first migration message as-per comment. I'll respin with that change.

Thanks, that sounds good!

Thomas