Skip to contentSkip to conversation

powerpc: Test SPR persistency during migration

PATCH Superseded Thomas Huth 2 participants 5 messages Mar 14, 2017 – Mar 20, 2017 KVM

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover powerpc: Test SPR persistency during migration

Here's a KVM-unit-test to check the persistency of SPRs during migration. First patch adds a helper script for doing migration tests in the kvm-unit-test framework, and the second patch contains the SPR tester.

v2:
- Addressed review feedback from v1
- Now using cpu_relax() instead of asm volatile ("nop")

Thomas Huth (2):
Add the possibility to do simple migration tests
powerpc: Add Special Purpose Register persistency test

 powerpc/Makefile.common          |   3 +-
 powerpc/cstart64.S               |   2 +
 powerpc/run                      |   4 +
 powerpc/sprs.c                   | 304 +++++++++++++++++++++++++++++++++++++++
 powerpc/unittests.cfg            |   5 +
 scripts/qemu-migration-helper.sh |  68 +++++++++
 scripts/runtime.bash             |   3 +
 7 files changed, 388 insertions(+), 1 deletion(-)
 create mode 100644 powerpc/sprs.c
 create mode 100755 scripts/qemu-migration-helper.sh
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/2 Add the possibility to do simple migration tests

To be able to do simple migration tests with kvm-unit-tests, too, add a helper script that does all the necessary work: Start two instances of QEMU (source and destination) with QMP sockets for sending commands to them, then trigger the migration from one instance to the other and finally signal the end of the migration to the guest by injecting an NMI.
This helper script is now used automatically for powerpc tests if the test is put into the "migration" group in the unittests.cfg file.

---
 powerpc/run                      |  4 +++
 scripts/qemu-migration-helper.sh | 68 ++++++++++++++++++++++++++++++++++++++++
 scripts/runtime.bash             |  3 ++
 3 files changed, 75 insertions(+)
 create mode 100755 scripts/qemu-migration-helper.sh
powerpc/run +4 -0
index 6269abb..f1528ed 100755
@@ -41,6 +41,10 @@ if ! $qemu -machine '?' 2>&1 | grep 'pseries' > /dev/null; then
 	exit 2
 fi
 
+if [ "$MIGRATION" = "yes" ]; then
+	qemu="scripts/qemu-migration-helper.sh $qemu"
+fi
+
 M='-machine pseries'
 M+=",accel=$ACCEL"
 command="$qemu -nodefaults $M -bios $FIRMWARE"
scripts/qemu-migration-helper.sh new file +68 -0

68 lines not on this page · raw message

scripts/runtime.bash +3 -0
index 9c1bc3b..f58eb4e 100644
@@ -98,6 +98,9 @@ function run()
     }
 
     cmdline=$(get_cmdline $kernel)
+    if grep -qw "migration" <<<$groups ; then
+        cmdline="MIGRATION=yes $cmdline"
+    fi
     if [ "$verbose" = "yes" ]; then
         echo $cmdline
     fi
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [kvm-unit-tests PATCH v2 1/2] Add the possibility to do simple migration tests

2017-03-14 11:23+0100, Thomas Huth:

Quoted 23 lines

To be able to do simple migration tests with kvm-unit-tests, too, add a helper script that does all the necessary work: Start two instances of QEMU (source and destination) with QMP sockets for sending commands to them, then trigger the migration from one instance to the other and finally signal the end of the migration to the guest by injecting an NMI.
This helper script is now used automatically for powerpc tests if the test is put into the "migration" group in the unittests.cfg file.

---
 powerpc/run                      |  4 +++
 scripts/qemu-migration-helper.sh | 68 ++++++++++++++++++++++++++++++++++++++++
 scripts/runtime.bash             |  3 ++
 3 files changed, 75 insertions(+)
 create mode 100755 scripts/qemu-migration-helper.sh
powerpc/run +0 -0
index 6269abb..f1528ed 100755

powerpc/run if ! $qemu -machine '?' 2>&1 | grep 'pseries' > /dev/null; then · line 46

Quoted 6 lines
powerpc/run +3 -0
@@ -41,6 +41,10 @@ if ! $qemu -machine '?' 2>&1 | grep 'pseries' > /dev/null; then
 	exit 2
 fi
 
+if [ "$MIGRATION" = "yes" ]; then
+	qemu="scripts/qemu-migration-helper.sh $qemu"
+fi

I think this script could be useful for other architectures as well ... what about adding migration_cmd to scripts/arch-run.bash

  migration_cmd ()
  {
  	if [ "$MIGRATION" = "yes" ]; then
  		echo "scripts/qemu-migration-helper.sh"
  	fi
  }

and using only

  command="$(timeout_cmd) $(migration_cmd) $command"

in arch scripts?

scripts/qemu-migration-helper.sh

Quoted 9 lines
powerpc/run +1 -0
+
 M='-machine pseries'
 M+=",accel=$ACCEL"
 command="$qemu -nodefaults $M -bios $FIRMWARE"
scripts/qemu-migration-helper.sh new file +0 -0
new file mode 100755
index 0000000..5842026

This doesn't seem to work with standalone tests, because they don't bundle scripts/qemu-migration-helper.sh ...

Any reason why we couldn't turn it into a

  qemu-migration-helper () {
    ...
  }

wrapper (likely inside scripts/arch-run.bash), so it would get sourced into the standalone test?

Quoted 30 lines
scripts/qemu-migration-helper.sh new file +29 -0
@@ -0,0 +1,68 @@
+#!/bin/bash
+# This script runs two instances of QEMU and then migrates the guest from one
+# instance to the other. The end of the migration is signalled to the guest by
+# injecting an NMI.
+
+if ! command -v nc >/dev/null 2>&1; then
+	echo "$0 needs nc (netcat)"
+	exit 1
+fi
+
+qemu=$1
+shift
+
+if ! command -v "$qemu" >/dev/null 2>&1; then
+	echo "The first parameter must be pointing to the QEMU executable"
+	exit 1
+fi
+
+migsock=`mktemp -u -t mig-helper-socket.XXXXXXXXXX`
+stdout1=`mktemp -t mig-helper-stdout1.XXXXXXXXXX`
+stdout2=`mktemp -t mig-helper-stdout2.XXXXXXXXXX`
+qmpout1=`mktemp -t mig-helper-qmpout1.XXXXXXXXXX`
+qmpout2=`mktemp -t mig-helper-qmpout2.XXXXXXXXXX`
+qmp1=`mktemp -u -t mig-helper-qmp1.XXXXXXXXXX`
+qmp2=`mktemp -u -t mig-helper-qmp2.XXXXXXXXXX`
+
+cleanup()
+{
+	rm -f ${stdout1} ${stdout2} ${migsock}

scripts/qemu-migration-helper.sh line 39

Quoted 10 lines
scripts/qemu-migration-helper.sh new file +10 -0
+	rm -f ${qmpout1} ${qmpout2} ${qmp1} ${qmp2}
+}
+trap cleanup EXIT
+
+qmp_cmd()
+{
+	echo '{ "execute": "qmp_capabilities" }{ "execute":' "$2" '}' | nc -U $1
+}
+
+$qemu $* -chardev socket,id=mon1,path=${qmp1},server,nowait \

I have a bad feeling about $*, please use "$@" instead.

The difference can be seen in:

  # f () { g $*; }
  # g () {echo $#; }
  # f 1 2; f "1 2"
  2
  2
  # f () { g "$@"; }
  # f 1 2; f "1 2"
  2
  1
Quoted 15 lines
scripts/qemu-migration-helper.sh new file +15 -0
+	 -mon chardev=mon1,mode=control > ${stdout1} &
+
+$qemu $* -chardev socket,id=mon2,path=${qmp2},server,nowait \
+	 -mon chardev=mon2,mode=control -incoming unix:${migsock} > ${stdout2} &
+
+# The test must prompt the user to migrate, so wait for the "migrate" keyword
+while ! grep -q -i "migrate" < ${stdout1} ; do
+	sleep 1
+done
+
+qmp_cmd ${qmp1} '"migrate", "arguments": { "uri": "unix:'${migsock}'" }' > ${qmpout1}
+
+# Wait for the migration to complete
+migstatus=`qmp_cmd ${qmp1} '"query-migrate"' | grep return`
+while ! grep -q '"completed"' <<<"$migstatus" ; do

scripts/qemu-migration-helper.sh line 64

Quoted 10 lines
scripts/qemu-migration-helper.sh new file +10 -0
+	sleep 1
+	migstatus=`qmp_cmd ${qmp1} '"query-migrate"' | grep return`
+	if grep -q '"failed"' <<<"$migstatus" ; then
+		echo "ERROR: Migration failed."
+		exit 1
+	fi
+done
+qmp_cmd ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
+
+qmp_cmd ${qmp2} '"inject-nmi"'> ${qmpout2}

qmpout* seems unused ... couldn't we just redirect to /dev/null?

scripts/qemu-migration-helper.sh line 68

Quoted 4 lines
scripts/qemu-migration-helper.sh new file +4 -0
+
+wait
+
+cat ${stdout1} ${stdout2}

The test might hang and we would never see its output ...
I think that stdout1 could be duplicated using a crazy technique similar to what run_qemu() uses. And do we need to capture stdout2 at all?

Thanks.

#3 lore raw

Re: [kvm-unit-tests PATCH v2 1/2] Add the possibility to do simple migration tests

Quoted 41 lines · Radim Krčmář

2017-03-14 11:23+0100, Thomas Huth:

Quoted 23 lines

To be able to do simple migration tests with kvm-unit-tests, too, add a helper script that does all the necessary work: Start two instances of QEMU (source and destination) with QMP sockets for sending commands to them, then trigger the migration from one instance to the other and finally signal the end of the migration to the guest by injecting an NMI.
This helper script is now used automatically for powerpc tests if the test is put into the "migration" group in the unittests.cfg file.

---
 powerpc/run                      |  4 +++
 scripts/qemu-migration-helper.sh | 68 ++++++++++++++++++++++++++++++++++++++++
 scripts/runtime.bash             |  3 ++
 3 files changed, 75 insertions(+)
 create mode 100755 scripts/qemu-migration-helper.sh
powerpc/run +0 -0
index 6269abb..f1528ed 100755

powerpc/run if ! $qemu -machine '?' 2>&1 | grep 'pseries' > /dev/null; then · line 46

Quoted 6 lines
powerpc/run +3 -0
@@ -41,6 +41,10 @@ if ! $qemu -machine '?' 2>&1 | grep 'pseries' > /dev/null; then
 	exit 2
 fi
 
+if [ "$MIGRATION" = "yes" ]; then
+	qemu="scripts/qemu-migration-helper.sh $qemu"
+fi

I think this script could be useful for other architectures as well ... what about adding migration_cmd to scripts/arch-run.bash

  migration_cmd ()
  {
  	if [ "$MIGRATION" = "yes" ]; then
  		echo "scripts/qemu-migration-helper.sh"
  	fi
  }
Quoted 5 lines

and using only

  command="$(timeout_cmd) $(migration_cmd) $command"

in arch scripts?

Yes, sounds like a good idea, I'll change my patch accordingly.

Quoted 13 lines

scripts/qemu-migration-helper.sh

Quoted 9 lines
powerpc/run +1 -0
+
 M='-machine pseries'
 M+=",accel=$ACCEL"
 command="$qemu -nodefaults $M -bios $FIRMWARE"
scripts/qemu-migration-helper.sh new file +0 -0
new file mode 100755
index 0000000..5842026

This doesn't seem to work with standalone tests, because they don't bundle scripts/qemu-migration-helper.sh ...

Quoted 8 lines

Any reason why we couldn't turn it into a

  qemu-migration-helper () {
    ...
  }

wrapper (likely inside scripts/arch-run.bash), so it would get sourced into the standalone test?

Oh, well, I've completely forgot about that standalone test feature ... I'll have a look...

Quoted 34 lines
Quoted 34 lines
scripts/qemu-migration-helper.sh new file +33 -0
@@ -0,0 +1,68 @@
+#!/bin/bash
+# This script runs two instances of QEMU and then migrates the guest from one
+# instance to the other. The end of the migration is signalled to the guest by
+# injecting an NMI.
+
+if ! command -v nc >/dev/null 2>&1; then
+	echo "$0 needs nc (netcat)"
+	exit 1
+fi
+
+qemu=$1
+shift
+
+if ! command -v "$qemu" >/dev/null 2>&1; then
+	echo "The first parameter must be pointing to the QEMU executable"
+	exit 1
+fi
+
+migsock=`mktemp -u -t mig-helper-socket.XXXXXXXXXX`
+stdout1=`mktemp -t mig-helper-stdout1.XXXXXXXXXX`
+stdout2=`mktemp -t mig-helper-stdout2.XXXXXXXXXX`
+qmpout1=`mktemp -t mig-helper-qmpout1.XXXXXXXXXX`
+qmpout2=`mktemp -t mig-helper-qmpout2.XXXXXXXXXX`
+qmp1=`mktemp -u -t mig-helper-qmp1.XXXXXXXXXX`
+qmp2=`mktemp -u -t mig-helper-qmp2.XXXXXXXXXX`
+
+cleanup()
+{
+	rm -f ${stdout1} ${stdout2} ${migsock}
+	rm -f ${qmpout1} ${qmpout2} ${qmp1} ${qmp2}
+}
+trap cleanup EXIT
+
Quoted 8 lines

scripts/qemu-migration-helper.sh line 39

Quoted 6 lines
scripts/qemu-migration-helper.sh new file +6 -0
+qmp_cmd()
+{
+	echo '{ "execute": "qmp_capabilities" }{ "execute":' "$2" '}' | nc -U $1
+}
+
+$qemu $* -chardev socket,id=mon1,path=${qmp1},server,nowait \

I have a bad feeling about $*, please use "$@" instead.

Sure, will do.

Quoted 19 lines
Quoted 19 lines
scripts/qemu-migration-helper.sh new file +19 -0
+	 -mon chardev=mon1,mode=control > ${stdout1} &
+
+$qemu $* -chardev socket,id=mon2,path=${qmp2},server,nowait \
+	 -mon chardev=mon2,mode=control -incoming unix:${migsock} > ${stdout2} &
+
+# The test must prompt the user to migrate, so wait for the "migrate" keyword
+while ! grep -q -i "migrate" < ${stdout1} ; do
+	sleep 1
+done
+
+qmp_cmd ${qmp1} '"migrate", "arguments": { "uri": "unix:'${migsock}'" }' > ${qmpout1}
+
+# Wait for the migration to complete
+migstatus=`qmp_cmd ${qmp1} '"query-migrate"' | grep return`
+while ! grep -q '"completed"' <<<"$migstatus" ; do
+	sleep 1
+	migstatus=`qmp_cmd ${qmp1} '"query-migrate"' | grep return`
+	if grep -q '"failed"' <<<"$migstatus" ; then
+		echo "ERROR: Migration failed."
Quoted 8 lines

scripts/qemu-migration-helper.sh line 64

Quoted 6 lines
scripts/qemu-migration-helper.sh new file +6 -0
+		exit 1
+	fi
+done
+qmp_cmd ${qmp1} '"quit"'> ${qmpout1} 2>/dev/null
+
+qmp_cmd ${qmp2} '"inject-nmi"'> ${qmpout2}

qmpout* seems unused ... couldn't we just redirect to /dev/null?

It's useful for debugging (you just got to make sure that it does not get deleted at the end)... but I could also set the variables to /dev/null instead, so the output gets written there by default.

Quoted 8 lines

scripts/qemu-migration-helper.sh line 68

Quoted 4 lines
scripts/qemu-migration-helper.sh new file +4 -0
+
+wait
+
+cat ${stdout1} ${stdout2}

The test might hang and we would never see its output ...
I think that stdout1 could be duplicated using a crazy technique similar to what run_qemu() uses. And do we need to capture stdout2 at all?

At least on ppc, we need both outputs, since the return code of the test is finally passed via the console output there. But I'll see whether I can come up with something better here instead...

Thanks for the review!

Thomas

#4 lore raw

PATCH v2 2/2 powerpc: Add Special Purpose Register persistency test

In reply to Thomas Huth

This test has two purposes: First, check whether the hypervisor can be destabilized by writing random values into the SPRs of the PowerPC CPU (this indeed revealed a bug last year, see CVE-2016-3044). Second, this test can be used to check whether the SPRs are synchronized properly between the KVM host CPU and QEMU, e.g. when migrating the VM from one QEMU instance to another.
The test first fills the various SPRs with some non-zero value, then reads the values back into a first array. It then either sleeps a short period of time (for testing without migration, in the hope that we're rescheduled on another host CPU), or it waits for a key or NMI (with the '-w' option) so that it is possible to migrate the VM before continuing. The test then finally reads the values from the SPRs back into another array and then compares them with the initial values.
Currently the test only supports the SPRs from the PowerISA v2.01 (PowerPC 970) and PowerISA v2.07 specification (i.e. POWER8 CPUs), but other versions should be pretty easy to add later.

---
 powerpc/Makefile.common |   3 +-
 powerpc/cstart64.S      |   2 +
 powerpc/sprs.c          | 304 ++++++++++++++++++++++++++++++++++++++++++++++++
 powerpc/unittests.cfg   |   5 +
 4 files changed, 313 insertions(+), 1 deletion(-)
 create mode 100644 powerpc/sprs.c
powerpc/Makefile.common +2 -1
index 37f8caa..92809a5 100644
@@ -9,7 +9,8 @@ tests-common = \
 	$(TEST_DIR)/spapr_hcall.elf \
 	$(TEST_DIR)/rtas.elf \
 	$(TEST_DIR)/emulator.elf \
-	$(TEST_DIR)/tm.elf
+	$(TEST_DIR)/tm.elf \
+	$(TEST_DIR)/sprs.elf
 
 tests-all = $(tests-common) $(tests)
 all: $(TEST_DIR)/boot_rom.bin $(tests-all)
powerpc/cstart64.S +2 -0
index 2204e3b..ec673b3 100644
@@ -247,6 +247,8 @@ call_handler:
 	.globl __start_interrupts
 __start_interrupts:
 
+VECTOR(0x100)
+VECTOR(0x200)
 VECTOR(0x300)
 VECTOR(0x400)
 VECTOR(0x500)
powerpc/sprs.c new file +304 -0

304 lines not on this page · raw message

powerpc/unittests.cfg +5 -0
index 20dbde6..fb6b70e 100644
@@ -59,3 +59,8 @@ file = tm.elf
 smp = 2,threads=2
 extra_params = -append "h_cede_tm"
 groups = nodefault,h_cede_tm
+
+[sprs]
+file = sprs.elf
+extra_params = -append '-w'
+groups = migration
Signature · 1 line
1.8.3.1