Skip to contentSkip to conversation

KVM: selftests: Improve sea_to_user test

PATCH In review Jiaqi Yan 3 participants 4 messages Jan 30 – Aug 17

Subsystems (3)
lore
Share & tools
lore

Jiaqi Yan

author
#0 lore raw

PATCH 1/1 KVM: selftests: Improve sea_to_user test

Several improvments to the test for KVM_EXIT_ARM_SEA:

- Refactor run_vm to catch GUEST_FAIL, instead of causing confusing unhandled MMIO kvm exit.

- Sync far_invalid to guest.

- Exit test with KSFT_SKIP or KSFT_FAIL when should.

- Add comment about VM backing memory type.

---
 .../testing/selftests/kvm/arm64/sea_to_user.c | 94 +++++++++++--------
 1 file changed, 53 insertions(+), 41 deletions(-)
tools/testing/selftests/kvm/arm64/sea_to_user.c +53 -41
index 573dd790aeb8e..4a3511fa1f940 100644
@@ -12,6 +12,11 @@
  * including the notrigger feature. Otherwise the test will be skipped.
  * The under-test platform's APEI should be unable to claim SEA. Otherwise
  * the test will also be skipped.
+ *
+ * The VM backing memory is tied to HugeTLB 1G hugepage so far. Make sure
+ * there are more than 4 1G hugepage on the system. They can be allocated
+ * at runtime by:
+ *   echo 4 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
  */
 
 #include <signal.h>
@@ -98,11 +103,15 @@ static void write_einj_entry(const char *einj_path, uint64_t val)
 
 static void inject_uer(uint64_t paddr)
 {
-	if (access("/sys/firmware/acpi/tables/EINJ", R_OK) == -1)
-		ksft_test_result_skip("EINJ table no available in firmware");
+	if (access("/sys/firmware/acpi/tables/EINJ", R_OK) == -1) {
+		ksft_test_result_skip("EINJ table not available in firmware\n");
+		exit(KSFT_SKIP);
+	}
 
-	if (access(EINJ_ETYPE, R_OK | W_OK) == -1)
-		ksft_test_result_skip("EINJ module probably not loaded?");
+	if (access(EINJ_ETYPE, R_OK | W_OK) == -1) {
+		ksft_test_result_skip("EINJ module probably not loaded?\n");
+		exit(KSFT_SKIP);
+	}
 
 	write_einj_entry(EINJ_ETYPE, ERROR_TYPE_MEMORY_UER);
 	write_einj_entry(EINJ_FLAGS, MASK_MEMORY_UER);
@@ -123,12 +132,13 @@ static void sigbus_signal_handler(int sig, siginfo_t *si, void *v)
 	ksft_print_msg("SIGBUS (%d) received, dumping siginfo...\n", sig);
 	ksft_print_msg("si_signo=%d, si_errno=%d, si_code=%d, si_addr=%p\n",
 		       si->si_signo, si->si_errno, si->si_code, si->si_addr);
-	if (si->si_code == BUS_MCEERR_AR)
+	if (si->si_code == BUS_MCEERR_AR) {
 		ksft_test_result_skip("SEA is claimed by host APEI\n");
-	else
+		exit(KSFT_SKIP);
+	} else {
 		ksft_test_result_fail("Exit with signal unhandled\n");
-
-	exit(0);
+		exit(KSFT_FAIL);
+	}
 }
 
 static void setup_sigbus_handler(void)
@@ -158,7 +168,6 @@ static void expect_sea_handler(struct ex_regs *regs)
 {
 	u64 esr = read_sysreg(esr_el1);
 	u64 far = read_sysreg(far_el1);
-	bool expect_far_invalid = far_invalid;
 
 	GUEST_PRINTF("Handling Guest SEA\n");
 	GUEST_PRINTF("ESR_EL1=%#lx, FAR_EL1=%#lx\n", esr, far);
@@ -166,7 +175,7 @@ static void expect_sea_handler(struct ex_regs *regs)
 	GUEST_ASSERT_EQ(ESR_ELx_EC(esr), ESR_ELx_EC_DABT_CUR);
 	GUEST_ASSERT_EQ(esr & ESR_ELx_FSC_TYPE, ESR_ELx_FSC_EXTABT);
 
-	if (expect_far_invalid) {
+	if (far_invalid) {
 		GUEST_ASSERT_EQ(esr & ESR_ELx_FnV, ESR_ELx_FnV);
 		GUEST_PRINTF("Guest observed garbage value in FAR\n");
 	} else {
@@ -185,25 +194,19 @@ static void vcpu_inject_sea(struct kvm_vcpu *vcpu)
 	vcpu_events_set(vcpu, &events);
 }
 
-static void run_vm(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
+static void validate_kvm_exit_arm_sea(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
 {
-	struct ucall uc;
-	bool guest_done = false;
 	struct kvm_run *run = vcpu->run;
 	u64 esr;
 
-	/* Resume the vCPU after error injection to consume the error. */
-	vcpu_run(vcpu);
+	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_ARM_SEA);
 
-	ksft_print_msg("Dump kvm_run info about KVM_EXIT_%s\n",
-		       exit_reason_str(run->exit_reason));
+	ksft_print_msg("Dumping kvm_run as arm_sea:\n");
 	ksft_print_msg("kvm_run.arm_sea: esr=%#llx, flags=%#llx\n",
 		       run->arm_sea.esr, run->arm_sea.flags);
 	ksft_print_msg("kvm_run.arm_sea: gva=%#llx, gpa=%#llx\n",
 		       run->arm_sea.gva, run->arm_sea.gpa);
 
-	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_ARM_SEA);
-
 	esr = run->arm_sea.esr;
 	TEST_ASSERT_EQ(ESR_ELx_EC(esr), ESR_ELx_EC_DABT_LOW);
 	TEST_ASSERT_EQ(esr & ESR_ELx_FSC_TYPE, ESR_ELx_FSC_EXTABT);
@@ -211,39 +214,48 @@ static void run_vm(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
 	TEST_ASSERT_EQ((esr & ESR_ELx_INST_SYNDROME), 0);
 	TEST_ASSERT_EQ(esr & ESR_ELx_VNCR, 0);
 
-	if (!(esr & ESR_ELx_FnV)) {
-		ksft_print_msg("Expect gva to match given FnV bit is 0\n");
+	far_invalid = esr & ESR_ELx_FnV;
+	sync_global_to_guest(vm, far_invalid);
+
+	if (!far_invalid) {
+		ksft_print_msg("Expect gva to match\n");
 		TEST_ASSERT_EQ(run->arm_sea.gva, EINJ_GVA);
 	}
 
 	if (run->arm_sea.flags & KVM_EXIT_ARM_SEA_FLAG_GPA_VALID) {
-		ksft_print_msg("Expect gpa to match given KVM_EXIT_ARM_SEA_FLAG_GPA_VALID is set\n");
+		ksft_print_msg("Expect gpa to match\n");
 		TEST_ASSERT_EQ(run->arm_sea.gpa, einj_gpa & PAGE_ADDR_MASK);
 	}
+}
 
-	far_invalid = esr & ESR_ELx_FnV;
-
-	/* Inject a SEA into guest and expect handled in SEA handler. */
-	vcpu_inject_sea(vcpu);
+static void run_vm(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
+{
+	struct ucall uc;
+	bool guest_done = false;
 
 	/* Expect the guest to reach GUEST_DONE gracefully. */
 	do {
 		vcpu_run(vcpu);
-		switch (get_ucall(vcpu, &uc)) {
-		case UCALL_PRINTF:
-			ksft_print_msg("From guest: %s", uc.buffer);
-			break;
-		case UCALL_DONE:
-			ksft_print_msg("Guest done gracefully!\n");
-			guest_done = 1;
-			break;
-		case UCALL_ABORT:
-			ksft_print_msg("Guest aborted!\n");
-			guest_done = 1;
-			REPORT_GUEST_ASSERT(uc);
-			break;
-		default:
-			TEST_FAIL("Unexpected ucall: %lu\n", uc.cmd);
+		if (vcpu->run->exit_reason == KVM_EXIT_ARM_SEA) {
+			validate_kvm_exit_arm_sea(vm, vcpu);
+			vcpu_inject_sea(vcpu);
+		} else {
+			switch (get_ucall(vcpu, &uc)) {
+			case UCALL_PRINTF:
+				ksft_print_msg("From guest: %s", uc.buffer);
+				break;
+			case UCALL_DONE:
+				ksft_print_msg("Guest done gracefully!\n");
+				guest_done = 1;
+				break;
+			case UCALL_ABORT:
+				ksft_print_msg("Guest aborted!\n");
+				guest_done = 1;
+				REPORT_GUEST_ASSERT(uc);
+				break;
+			default:
+				TEST_FAIL("Unexpected ucall: %lu\n", uc.cmd);
+			}
 		}
 	} while (!guest_done);
 }
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
maintainer
#1 lore raw
Quoted 8 lines · Jiaqi Yan

Several improvments to the test for KVM_EXIT_ARM_SEA:

- Refactor run_vm to catch GUEST_FAIL, instead of causing confusing unhandled MMIO kvm exit.

- Sync far_invalid to guest.

Quoted 8 lines

- Exit test with KSFT_SKIP or KSFT_FAIL when should.

- Add comment about VM backing memory type.

---
 .../testing/selftests/kvm/arm64/sea_to_user.c | 94 +++++++++++--------
 1 file changed, 53 insertions(+), 41 deletions(-)

Overall, this test is still pretty broken.

For example, on my Altra box:

maz@filthy-habits:~$ ./sea_to_user 
Random seed: 0x6b8b4567
# Mapped 0x40000 pages: gva=0x80000000 to gpa=0xff80000000
# Before EINJect: data=0xbaadcafe
# EINJ_GVA=0x81234bad, einj_gpa=0xff81234bad, einj_hva=0xffff41234bad, einj_hpa=0xbad
ok 1 # SKIP EINJ table not available in firmware

Well, not quite. EINJ *is* available, it is just that this test, contrary to *all* the other tests, requires some insanely high privileges. But the test is making stupid assumption.

But that's not all:

maz@filthy-habits:~$ sudo ./sea_to_user 
Random seed: 0x6b8b4567
# Mapped 0x40000 pages: gva=0x80000000 to gpa=0xff80000000
# Before EINJect: data=0xbaadcafe
# EINJ_GVA=0x81234bad, einj_gpa=0xff81234bad, einj_hva=0xffff41234bad, einj_hpa=0x80041234bad
# echo 0x10 > /sys/kernel/debug/apei/einj/error_type - done
# echo 0x2 > /sys/kernel/debug/apei/einj/flags - done
# echo 0x80041234bad > /sys/kernel/debug/apei/einj/param1 - done
# echo 0xffffffffffffffff > /sys/kernel/debug/apei/einj/param2 - done
# echo 0x1 > /sys/kernel/debug/apei/einj/notrigger - done
sh: 1: echo: echo: I/O error
Bail out! Failed to write EINJ entry: Success (0)
# Totals: pass:0 fail:0 xfail:0 xpass:0 skip:0 error:0

Failed? Or Success? Who knows...

But frankly, the whole "embedded shell script" is disgusting. What is wrong with driving the injection from the test itself, rather than this access()+popen()? popen() itself is a liability (see the man page for system()), and should never be used for this sort of things.

tools/testing/selftests/kvm/arm64/sea_to_user.c line 17

Quoted 12 lines
tools/testing/selftests/kvm/arm64/sea_to_user.c +3 -0
index 573dd790aeb8e..4a3511fa1f940 100644
@@ -12,6 +12,11 @@
  * including the notrigger feature. Otherwise the test will be skipped.
  * The under-test platform's APEI should be unable to claim SEA. Otherwise
  * the test will also be skipped.
+ *
+ * The VM backing memory is tied to HugeTLB 1G hugepage so far. Make sure
+ * there are more than 4 1G hugepage on the system. They can be allocated

Why *more than*? Isn't that *at least* instead?

tools/testing/selftests/kvm/arm64/sea_to_user.c line 20

Quoted 3 lines
tools/testing/selftests/kvm/arm64/sea_to_user.c +2 -0
+ * at runtime by:
+ *   echo 4 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
  */

Why can't you provide an adequate diagnostic instead of this stuff:

maz@filthy-habits:~$ ./sea_to_user 
Random seed: 0x6b8b4567
==== Test Assertion Failure ====
  include/kvm_syscalls.h:58: mem != MAP_FAILED
  pid=887 tid=887 errno=12 - Cannot allocate memory
     1	0x0000000000405b4f: __kvm_mmap at kvm_syscalls.h:58 (discriminator 3)
     2	 (inlined by) kvm_mmap at kvm_syscalls.h:65 (discriminator 3)
     3	 (inlined by) vm_mem_add at kvm_util.c:1036 (discriminator 3)
     4	0x0000000000402373: vm_create_with_sea_handler at sea_to_user.c:290
     5	 (inlined by) main at sea_to_user.c:336
     6	0x0000ffffb0dc229b: ?? ??:0
     7	0x0000ffffb0dc237b: ?? ??:0
     8	0x00000000004027ef: _start at ??:?
  mmap() failed, rc: -1 errno: 12 (Cannot allocate memory)

What is preventing you from not requiring huge pages? And not requiring 4kB as the base page size?

Given how broken this is, I'm likely to disable this test until you fix it for real.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#2 lore raw
Quoted 93 lines · Marc Zyngier
Quoted 8 lines · Jiaqi Yan

Several improvments to the test for KVM_EXIT_ARM_SEA:

- Refactor run_vm to catch GUEST_FAIL, instead of causing confusing unhandled MMIO kvm exit.

- Sync far_invalid to guest.

Quoted 8 lines

- Exit test with KSFT_SKIP or KSFT_FAIL when should.

- Add comment about VM backing memory type.

---
  .../testing/selftests/kvm/arm64/sea_to_user.c | 94 +++++++++++--------
  1 file changed, 53 insertions(+), 41 deletions(-)

Overall, this test is still pretty broken.

For example, on my Altra box:

maz@filthy-habits:~$ ./sea_to_user
Random seed: 0x6b8b4567
# Mapped 0x40000 pages: gva=0x80000000 to gpa=0xff80000000
# Before EINJect: data=0xbaadcafe
# EINJ_GVA=0x81234bad, einj_gpa=0xff81234bad, einj_hva=0xffff41234bad, einj_hpa=0xbad
ok 1 # SKIP EINJ table not available in firmware

Well, not quite. EINJ *is* available, it is just that this test, contrary to *all* the other tests, requires some insanely high privileges. But the test is making stupid assumption.

But that's not all:

maz@filthy-habits:~$ sudo ./sea_to_user
Random seed: 0x6b8b4567
# Mapped 0x40000 pages: gva=0x80000000 to gpa=0xff80000000
# Before EINJect: data=0xbaadcafe
# EINJ_GVA=0x81234bad, einj_gpa=0xff81234bad, einj_hva=0xffff41234bad, einj_hpa=0x80041234bad
# echo 0x10 > /sys/kernel/debug/apei/einj/error_type - done
# echo 0x2 > /sys/kernel/debug/apei/einj/flags - done
# echo 0x80041234bad > /sys/kernel/debug/apei/einj/param1 - done
# echo 0xffffffffffffffff > /sys/kernel/debug/apei/einj/param2 - done
# echo 0x1 > /sys/kernel/debug/apei/einj/notrigger - done
sh: 1: echo: echo: I/O error
Bail out! Failed to write EINJ entry: Success (0)
# Totals: pass:0 fail:0 xfail:0 xpass:0 skip:0 error:0

Failed? Or Success? Who knows...

But frankly, the whole "embedded shell script" is disgusting. What is wrong with driving the injection from the test itself, rather than this access()+popen()? popen() itself is a liability (see the man page for system()), and should never be used for this sort of things.

tools/testing/selftests/kvm/arm64/sea_to_user.c line 17

Quoted 12 lines
tools/testing/selftests/kvm/arm64/sea_to_user.c +3 -0
index 573dd790aeb8e..4a3511fa1f940 100644
@@ -12,6 +12,11 @@
   * including the notrigger feature. Otherwise the test will be skipped.
   * The under-test platform's APEI should be unable to claim SEA. Otherwise
   * the test will also be skipped.
+ *
+ * The VM backing memory is tied to HugeTLB 1G hugepage so far. Make sure
+ * there are more than 4 1G hugepage on the system. They can be allocated

Why *more than*? Isn't that *at least* instead?

tools/testing/selftests/kvm/arm64/sea_to_user.c line 20

Quoted 3 lines
tools/testing/selftests/kvm/arm64/sea_to_user.c +2 -0
+ * at runtime by:
+ *   echo 4 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
   */

Why can't you provide an adequate diagnostic instead of this stuff:

maz@filthy-habits:~$ ./sea_to_user
Random seed: 0x6b8b4567
==== Test Assertion Failure ====
   include/kvm_syscalls.h:58: mem != MAP_FAILED
   pid=887 tid=887 errno=12 - Cannot allocate memory
      1	0x0000000000405b4f: __kvm_mmap at kvm_syscalls.h:58 (discriminator 3)
      2	 (inlined by) kvm_mmap at kvm_syscalls.h:65 (discriminator 3)
      3	 (inlined by) vm_mem_add at kvm_util.c:1036 (discriminator 3)
      4	0x0000000000402373: vm_create_with_sea_handler at sea_to_user.c:290
      5	 (inlined by) main at sea_to_user.c:336
      6	0x0000ffffb0dc229b: ?? ??:0
      7	0x0000ffffb0dc237b: ?? ??:0
      8	0x00000000004027ef: _start at ??:?
   mmap() failed, rc: -1 errno: 12 (Cannot allocate memory)
Quoted 5 lines

What is preventing you from not requiring huge pages? And not requiring 4kB as the base page size?

Given how broken this is, I'm likely to disable this test until you fix it for real.

Pls help review the expect output:

# selftests: kvm: sea_to_user
# Random seed: 0x6b8b4567
# # Mapped 0x40000 pages: gva=0x80000000 to gpa=0xff80000000
# # Before EINJect: data=0xbaadcafe
# # EINJ_GVA=0x81234bad, einj_gpa=0xff81234bad, einj_hva=0xffff41234bad, einj_hpa=0x2141234bad
# 1..0 # SKIP EINJ debugfs interface not available; run as root with CONFIG_ACPI_APEI_EINJ and firmware EINJ support ok 1 selftests: kvm: sea_to_user # SKIP
# 1 skipped test(s) detected. Consider enabling relevant config options to improve coverage.
# Totals: pass:0 fail:0 xfail:0 xpass:0 skip:1 error:0

, and the diff behind:

tools/testing/selftests/kvm/arm64/sea_to_user.c +19 -12
index a23d74ed45035..7e86cbeb55291 100644
@@ -86,25 +86,32 @@ static u64 translate_hva_to_hpa(unsigned long hva)
 
 static void write_einj_entry(const char *einj_path, u64 val)
 {
-	char cmd[256] = {0};
-	FILE *cmdfile = NULL;
+	char buf[32];
+	int fd, len, ret;
 
-	sprintf(cmd, "echo %#lx > %s", val, einj_path);
-	cmdfile = popen(cmd, "r");
+	fd = open(einj_path, O_WRONLY);
+	if (fd < 0)
+		ksft_exit_fail_perror(einj_path);
 
-	if (pclose(cmdfile) == 0)
-		ksft_print_msg("echo %#lx > %s - done\n", val, einj_path);
-	else
-		ksft_exit_fail_perror("Failed to write EINJ entry");
+	len = snprintf(buf, sizeof(buf), "%#lx\n", val);
+	ret = write(fd, buf, len);
+	if (ret != len)
+		ksft_exit_fail_perror(einj_path);
+
+	close(fd);
+	ksft_print_msg("%#lx > %s - done\n", val, einj_path);
 }
 
 static void inject_uer(u64 hpa)
 {
-	if (access("/sys/firmware/acpi/tables/EINJ", R_OK) == -1)
-		ksft_test_result_skip("EINJ table no available in firmware");
-
+	/*
+	 * Reading the ACPI EINJ table requires CAP_SYS_ADMIN, so a plain
+	 * access() check gives a false negative for unprivileged runs. Probe
+	 * the debugfs EINJ interface instead: it is only present when both
+	 * firmware and the kernel EINJ driver support error injection.
+	 */
 	if (access(EINJ_ETYPE, R_OK | W_OK) == -1)
-		ksft_test_result_skip("EINJ module probably not loaded?");
+		ksft_exit_skip("EINJ debugfs interface not available; run as root with CONFIG_ACPI_APEI_EINJ and firmware EINJ support\n");
 
 	write_einj_entry(EINJ_ETYPE, ERROR_TYPE_MEMORY_UER);
 	write_einj_entry(EINJ_FLAGS, MASK_MEMORY_UER);
Quoted 5 lines

Thanks,

	M.
maintainer
#3 lore raw
Quoted 168 lines · Like Xu
Quoted 93 lines · Marc Zyngier
Quoted 8 lines · Jiaqi Yan

Several improvments to the test for KVM_EXIT_ARM_SEA:

- Refactor run_vm to catch GUEST_FAIL, instead of causing confusing unhandled MMIO kvm exit.

- Sync far_invalid to guest.

Quoted 8 lines

- Exit test with KSFT_SKIP or KSFT_FAIL when should.

- Add comment about VM backing memory type.

---
  .../testing/selftests/kvm/arm64/sea_to_user.c | 94 +++++++++++--------
  1 file changed, 53 insertions(+), 41 deletions(-)

Overall, this test is still pretty broken.

For example, on my Altra box:

maz@filthy-habits:~$ ./sea_to_user
Random seed: 0x6b8b4567
# Mapped 0x40000 pages: gva=0x80000000 to gpa=0xff80000000
# Before EINJect: data=0xbaadcafe
# EINJ_GVA=0x81234bad, einj_gpa=0xff81234bad, einj_hva=0xffff41234bad, einj_hpa=0xbad
ok 1 # SKIP EINJ table not available in firmware

Well, not quite. EINJ *is* available, it is just that this test, contrary to *all* the other tests, requires some insanely high privileges. But the test is making stupid assumption.

But that's not all:

maz@filthy-habits:~$ sudo ./sea_to_user
Random seed: 0x6b8b4567
# Mapped 0x40000 pages: gva=0x80000000 to gpa=0xff80000000
# Before EINJect: data=0xbaadcafe
# EINJ_GVA=0x81234bad, einj_gpa=0xff81234bad, einj_hva=0xffff41234bad, einj_hpa=0x80041234bad
# echo 0x10 > /sys/kernel/debug/apei/einj/error_type - done
# echo 0x2 > /sys/kernel/debug/apei/einj/flags - done
# echo 0x80041234bad > /sys/kernel/debug/apei/einj/param1 - done
# echo 0xffffffffffffffff > /sys/kernel/debug/apei/einj/param2 - done
# echo 0x1 > /sys/kernel/debug/apei/einj/notrigger - done
sh: 1: echo: echo: I/O error
Bail out! Failed to write EINJ entry: Success (0)
# Totals: pass:0 fail:0 xfail:0 xpass:0 skip:0 error:0

Failed? Or Success? Who knows...

But frankly, the whole "embedded shell script" is disgusting. What is wrong with driving the injection from the test itself, rather than this access()+popen()? popen() itself is a liability (see the man page for system()), and should never be used for this sort of things.

tools/testing/selftests/kvm/arm64/sea_to_user.c line 17

Quoted 12 lines
tools/testing/selftests/kvm/arm64/sea_to_user.c +3 -0
index 573dd790aeb8e..4a3511fa1f940 100644
@@ -12,6 +12,11 @@
   * including the notrigger feature. Otherwise the test will be skipped.
   * The under-test platform's APEI should be unable to claim SEA. Otherwise
   * the test will also be skipped.
+ *
+ * The VM backing memory is tied to HugeTLB 1G hugepage so far. Make sure
+ * there are more than 4 1G hugepage on the system. They can be allocated

Why *more than*? Isn't that *at least* instead?

tools/testing/selftests/kvm/arm64/sea_to_user.c line 20

Quoted 3 lines
tools/testing/selftests/kvm/arm64/sea_to_user.c +2 -0
+ * at runtime by:
+ *   echo 4 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
   */

Why can't you provide an adequate diagnostic instead of this stuff:

maz@filthy-habits:~$ ./sea_to_user
Random seed: 0x6b8b4567
==== Test Assertion Failure ====
   include/kvm_syscalls.h:58: mem != MAP_FAILED
   pid=887 tid=887 errno=12 - Cannot allocate memory
      1	0x0000000000405b4f: __kvm_mmap at kvm_syscalls.h:58 (discriminator 3)
      2	 (inlined by) kvm_mmap at kvm_syscalls.h:65 (discriminator 3)
      3	 (inlined by) vm_mem_add at kvm_util.c:1036 (discriminator 3)
      4	0x0000000000402373: vm_create_with_sea_handler at sea_to_user.c:290
      5	 (inlined by) main at sea_to_user.c:336
      6	0x0000ffffb0dc229b: ?? ??:0
      7	0x0000ffffb0dc237b: ?? ??:0
      8	0x00000000004027ef: _start at ??:?
   mmap() failed, rc: -1 errno: 12 (Cannot allocate memory)
Quoted 5 lines

What is preventing you from not requiring huge pages? And not requiring 4kB as the base page size?

Given how broken this is, I'm likely to disable this test until you fix it for real.

Pls help review the expect output:

# selftests: kvm: sea_to_user
# Random seed: 0x6b8b4567
# # Mapped 0x40000 pages: gva=0x80000000 to gpa=0xff80000000 # # Before EINJect: data=0xbaadcafe
# # EINJ_GVA=0x81234bad, einj_gpa=0xff81234bad, einj_hva=0xffff41234bad, einj_hpa=0x2141234bad
# 1..0 # SKIP EINJ debugfs interface not available; run as root with CONFIG_ACPI_APEI_EINJ and firmware EINJ support
ok 1 selftests: kvm: sea_to_user # SKIP
# 1 skipped test(s) detected. Consider enabling relevant config options to improve coverage.
# Totals: pass:0 fail:0 xfail:0 xpass:0 skip:1 error:0

, and the diff behind:

tools/testing/selftests/kvm/arm64/sea_to_user.c +19 -12
index a23d74ed45035..7e86cbeb55291 100644
@@ -86,25 +86,32 @@ static u64 translate_hva_to_hpa(unsigned long hva)
 
 static void write_einj_entry(const char *einj_path, u64 val)
 {
-	char cmd[256] = {0};
-	FILE *cmdfile = NULL;
+	char buf[32];
+	int fd, len, ret;
 
-	sprintf(cmd, "echo %#lx > %s", val, einj_path);
-	cmdfile = popen(cmd, "r");
+	fd = open(einj_path, O_WRONLY);
+	if (fd < 0)
+		ksft_exit_fail_perror(einj_path);
 
-	if (pclose(cmdfile) == 0)
-		ksft_print_msg("echo %#lx > %s - done\n", val, einj_path);
-	else
-		ksft_exit_fail_perror("Failed to write EINJ entry");
+	len = snprintf(buf, sizeof(buf), "%#lx\n", val);
+	ret = write(fd, buf, len);
+	if (ret != len)
+		ksft_exit_fail_perror(einj_path);
+
+	close(fd);
+	ksft_print_msg("%#lx > %s - done\n", val, einj_path);
 }
 
 static void inject_uer(u64 hpa)
 {
-	if (access("/sys/firmware/acpi/tables/EINJ", R_OK) == -1)
-		ksft_test_result_skip("EINJ table no available in firmware");
-
+	/*
+	 * Reading the ACPI EINJ table requires CAP_SYS_ADMIN, so a plain
+	 * access() check gives a false negative for unprivileged runs. Probe
+	 * the debugfs EINJ interface instead: it is only present when both
+	 * firmware and the kernel EINJ driver support error injection.
+	 */
 	if (access(EINJ_ETYPE, R_OK | W_OK) == -1)
-		ksft_test_result_skip("EINJ module probably not loaded?");
+		ksft_exit_skip("EINJ debugfs interface not available;
 run as root with CONFIG_ACPI_APEI_EINJ and firmware EINJ support\n");
 
 	write_einj_entry(EINJ_ETYPE, ERROR_TYPE_MEMORY_UER);
Quoted 1 line

write_einj_entry(EINJ_FLAGS, MASK_MEMORY_UER);

This seems better, although I still think the access() thing is wrong. We should catch errno after the open() and work out what when wrong. Also, the dependency on debugfs is really horrible.

Anyway, since this is an improvement, please post a standalone patch that can be reviewed/merged, and add separate patches to check for the huge pages.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.