Skip to contentSkip to conversation

selftests: kvm: make syncregs more reliable on s390

PATCH In review Christian Borntraeger 2 participants 5 messages Oct 10, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: kvm: make syncregs more reliable on s390

similar to commit 2c57da356800 ("selftests: kvm: fix sync_regs_test with newer gccs") and commit 204c91eff798a ("KVM: selftests: do not blindly clobber registers in guest asm") we better do not rely on gcc leaving r11 untouched. We can write the simple ucall inline and have the guest code completely as small assembler function.

---
 .../testing/selftests/kvm/s390x/sync_regs_test.c  | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/s390x/sync_regs_test.c +9 -6
index d5290b4ad636..04d6abe68961 100644
@@ -25,12 +25,15 @@
 
 static void guest_code(void)
 {
-	register u64 stage asm("11") = 0;
-
-	for (;;) {
-		GUEST_SYNC(0);
-		asm volatile ("ahi %0,1" : : "r"(stage));
-	}
+	/*
+	 * we embed diag 501 here instead of a ucall as the called function
+	 * could mess with the content of r11 when doing the hypercall
+	 */
+	asm volatile (
+		"0:	diag 0,0,0x501\n"
+		"	ahi 11,1\n"
+		"	j 0b\n"
+	);
 }
 
 #define REG_COMPARE(reg) \
Signature · 1 line
2.21.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Christian Borntraeger

similar to commit 2c57da356800 ("selftests: kvm: fix sync_regs_test with newer gccs") and commit 204c91eff798a ("KVM: selftests: do not blindly clobber registers in guest asm") we better do not rely on gcc leaving r11 untouched. We can write the simple ucall inline and have the guest code completely as small assembler function.

---
 .../testing/selftests/kvm/s390x/sync_regs_test.c  | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/s390x/sync_regs_test.c +0 -0
index d5290b4ad636..04d6abe68961 100644
@@ -25,12 +25,15 @@
 
 static void guest_code(void)

tools/testing/selftests/kvm/s390x/sync_regs_test.c line 30

Quoted 10 lines
tools/testing/selftests/kvm/s390x/sync_regs_test.c +3 -6
 {
-	register u64 stage asm("11") = 0;
-
-	for (;;) {
-		GUEST_SYNC(0);
-		asm volatile ("ahi %0,1" : : "r"(stage));
-	}
+	/*
+	 * we embed diag 501 here instead of a ucall as the called function
+	 * could mess with the content of r11 when doing the hypercall

As far as I understood the issue on x86, it's not the called function that is messing with the counter register (since r11 is not volatile between function calls), but the compiler shuffled it around here between the GUEST_SYNC and the inline assembler code. So I'd prefer a comment like in the x86 version:

/*
* We embed diag 501 here instead of doing a ucall to avoid that
* the compiler reshuffles registers before calling the function. */

?

With such an updated comment:

Quoted 7 lines
tools/testing/selftests/kvm/s390x/sync_regs_test.c +6 -0
+	 */
+	asm volatile (
+		"0:	diag 0,0,0x501\n"
+		"	ahi 11,1\n"
+		"	j 0b\n"
+	);
 }
#2 lore raw
Quoted 28 lines · Thomas Huth
Quoted 27 lines · Christian Borntraeger

similar to commit 2c57da356800 ("selftests: kvm: fix sync_regs_test with newer gccs") and commit 204c91eff798a ("KVM: selftests: do not blindly clobber registers in guest asm") we better do not rely on gcc leaving r11 untouched. We can write the simple ucall inline and have the guest code completely as small assembler function.

---
 .../testing/selftests/kvm/s390x/sync_regs_test.c  | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/s390x/sync_regs_test.c +1 -6
index d5290b4ad636..04d6abe68961 100644
@@ -25,12 +25,15 @@
 
 static void guest_code(void)
 {
-	register u64 stage asm("11") = 0;
-
-	for (;;) {
-		GUEST_SYNC(0);
-		asm volatile ("ahi %0,1" : : "r"(stage));
-	}
+	/*
Quoted 8 lines

tools/testing/selftests/kvm/s390x/sync_regs_test.c line 30

Quoted 2 lines
tools/testing/selftests/kvm/s390x/sync_regs_test.c +2 -0
+	 * we embed diag 501 here instead of a ucall as the called function
+	 * could mess with the content of r11 when doing the hypercall

As far as I understood the issue on x86, it's not the called function that is messing with the counter register (since r11 is not volatile between function calls), but the compiler shuffled it around here between the GUEST_SYNC and the inline assembler code. So I'd prefer a comment like in the x86 version:

It is actually both.
The called function is GUEST_SYNC (which is ucall).
r11 is call-saved, so when we return from GUEST_SYNC r11 is as it is supposed to be. The problem is that this function (ucall) is allowed to use r11 for anything as long as it restores it before returning. As we do the guest exit in ucall then the test code could see some random value for r11.

Quoted 7 lines

/*
* We embed diag 501 here instead of doing a ucall to avoid that
* the compiler reshuffles registers before calling the function. */

?

What about

Quoted 18 lines

/*
* We embed diag 501 here instead of doing a ucall to avoid that
* the compiler has messed with r11 at the time of the ucall. */

With such an updated comment:

Quoted 7 lines
tools/testing/selftests/kvm/s390x/sync_regs_test.c +6 -0
+	 */
+	asm volatile (
+		"0:	diag 0,0,0x501\n"
+		"	ahi 11,1\n"
+		"	j 0b\n"
+	);
 }
#3 lore raw
Quoted 40 lines · Christian Borntraeger
Quoted 28 lines · Thomas Huth
Quoted 27 lines · Christian Borntraeger

similar to commit 2c57da356800 ("selftests: kvm: fix sync_regs_test with newer gccs") and commit 204c91eff798a ("KVM: selftests: do not blindly clobber registers in guest asm") we better do not rely on gcc leaving r11 untouched. We can write the simple ucall inline and have the guest code completely as small assembler function.

---
 .../testing/selftests/kvm/s390x/sync_regs_test.c  | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/s390x/sync_regs_test.c +1 -6
index d5290b4ad636..04d6abe68961 100644
@@ -25,12 +25,15 @@
 
 static void guest_code(void)
 {
-	register u64 stage asm("11") = 0;
-
-	for (;;) {
-		GUEST_SYNC(0);
-		asm volatile ("ahi %0,1" : : "r"(stage));
-	}
+	/*
Quoted 8 lines

tools/testing/selftests/kvm/s390x/sync_regs_test.c line 30

Quoted 2 lines
tools/testing/selftests/kvm/s390x/sync_regs_test.c +2 -0
+	 * we embed diag 501 here instead of a ucall as the called function
+	 * could mess with the content of r11 when doing the hypercall

As far as I understood the issue on x86, it's not the called function that is messing with the counter register (since r11 is not volatile between function calls), but the compiler shuffled it around here between the GUEST_SYNC and the inline assembler code. So I'd prefer a comment like in the x86 version:

Quoted 6 lines

It is actually both.
The called function is GUEST_SYNC (which is ucall).
r11 is call-saved, so when we return from GUEST_SYNC r11 is as it is supposed to be. The problem is that this function (ucall) is allowed to use r11 for anything as long as it restores it before returning. As we do the guest exit in ucall then the test code could see some random value for r11.

Oh, right!

Quoted 5 lines
Quoted 5 lines

/*
* We embed diag 501 here instead of doing a ucall to avoid that
* the compiler reshuffles registers before calling the function. */

Quoted 7 lines
Quoted 1 line

?

What about

Quoted 4 lines

/*
* We embed diag 501 here instead of doing a ucall to avoid that
* the compiler has messed with r11 at the time of the ucall. */

Sounds good!

Thanks,
Thomas

author Applied
#4 lore raw
Quoted 57 lines · Thomas Huth
Quoted 40 lines · Christian Borntraeger
Quoted 28 lines · Thomas Huth
Quoted 27 lines · Christian Borntraeger

similar to commit 2c57da356800 ("selftests: kvm: fix sync_regs_test with newer gccs") and commit 204c91eff798a ("KVM: selftests: do not blindly clobber registers in guest asm") we better do not rely on gcc leaving r11 untouched. We can write the simple ucall inline and have the guest code completely as small assembler function.

---
 .../testing/selftests/kvm/s390x/sync_regs_test.c  | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/s390x/sync_regs_test.c +1 -6
index d5290b4ad636..04d6abe68961 100644
@@ -25,12 +25,15 @@
 
 static void guest_code(void)
 {
-	register u64 stage asm("11") = 0;
-
-	for (;;) {
-		GUEST_SYNC(0);
-		asm volatile ("ahi %0,1" : : "r"(stage));
-	}
+	/*
Quoted 8 lines

tools/testing/selftests/kvm/s390x/sync_regs_test.c line 30

Quoted 2 lines
tools/testing/selftests/kvm/s390x/sync_regs_test.c +2 -0
+	 * we embed diag 501 here instead of a ucall as the called function
+	 * could mess with the content of r11 when doing the hypercall

As far as I understood the issue on x86, it's not the called function that is messing with the counter register (since r11 is not volatile between function calls), but the compiler shuffled it around here between the GUEST_SYNC and the inline assembler code. So I'd prefer a comment like in the x86 version:

Quoted 6 lines

It is actually both.
The called function is GUEST_SYNC (which is ucall).
r11 is call-saved, so when we return from GUEST_SYNC r11 is as it is supposed to be. The problem is that this function (ucall) is allowed to use r11 for anything as long as it restores it before returning. As we do the guest exit in ucall then the test code could see some random value for r11.

Oh, right!

Quoted 7 lines
Quoted 6 lines

/*
* We embed diag 501 here instead of doing a ucall to avoid that
* the compiler reshuffles registers before calling the function. */

?

Quoted 7 lines
Quoted 5 lines

What about

Quoted 4 lines

/*
* We embed diag 501 here instead of doing a ucall to avoid that
* the compiler has messed with r11 at the time of the ucall. */

Sounds good!

applied.