KVM: selftests: arm64: Improve diagnostics from set_id_regs
v10 cover KVM: selftests: arm64: Improve diagnostics from set_id_regs
While debugging issues related to aarch64 only systems I ran into speedbumps due to the lack of detail in the results reported when the guest register read and reset value preservation tests were run, they generated an immediately fatal assert without indicating which register was being tested. Update these tests to non-fatally report a standard kselftest result per register, making it much easier to see what the problem being reported is.
A similar issue exists with the validation of the individual bitfields in registers due to the use of immediately fatal asserts. Update those asserts to be standard kselftest reports.
---
Changes in v10:
- Use __vcpu_set_reg() when testing valid writes to avoid asserts.
- Use local variable for reg_id more.
- Link to v9: https://patch.msgid.link/20261103-kvm-arm64-set-id-regs-aarch64-v9-0-ab32bc79fa9c@kernel.org
Changes in v9:
- Rebase onto v7.4-rc1.
- Link to v8: https://patch.msgid.link/20260929-kvm-arm64-set-id-regs-aarch64-v8-0-58aabe2c2fac@kernel.org
Changes in v8:
- Convert another assert.
- Link to v7: https://patch.msgid.link/20260901-kvm-arm64-set-id-regs-aarch64-v7-0-7b065b0bda9a@kernel.org
Changes in v7:
- Rebase onto v7.3-rc1.
- Link to v6: https://patch.msgid.link/20260719-kvm-arm64-set-id-regs-aarch64-v6-0-724287f5f108@kernel.org
Changes in v6:
- Rebase onto v7.2-rc3.
- Drop changes to handling of aarch32 related registers on 64 bit only
systems, an in kernel solution is preferred for those.
- Use a define to count the non-test_regs registers.
- Link to v5: https://patch.msgid.link/20260317-kvm-arm64-set-id-regs-aarch64-v5-0-a60f2b956e22@kernel.org
Changes in v5:
- Rebase onto v7.0-rc1.
- Link to v4: https://patch.msgid.link/20260106-kvm-arm64-set-id-regs-aarch64-v4-0-c7ef4551afb3@kernel.org
Changes in v4:
- Correct check for 32 bit ID registers.
- Link to v3: https://patch.msgid.link/20251219-kvm-arm64-set-id-regs-aarch64-v3-0-bfa474ec3218@kernel.org
Changes in v3:
- Rebase onto v6.19-rc1.
- Link to v2: https://patch.msgid.link/20251114-kvm-arm64-set-id-regs-aarch64-v2-0-672f214f41bf@kernel.org
Changes in v2:
- Add a fix for spurious failures with 64 bit only guests.
- Link to v1: https://patch.msgid.link/20251030-kvm-arm64-set-id-regs-aarch64-v1-0-96fe0d2b178e@kernel.org
---
Mark Brown (3):
KVM: selftests: arm64: Report set_id_reg reads of test registers as tests
KVM: selftests: arm64: Report register reset tests individually
KVM: selftests: arm64: Make set_id_regs bitfield validity checks non-fatal
tools/testing/selftests/kvm/arm64/set_id_regs.c | 152 +++++++++++++++++------- 1 file changed, 111 insertions(+), 41 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm master
change-id: 20251028-kvm-arm64-set-id-regs-aarch64-ebb77969401c
Best regards,
Signature · 1 line
Mark Brown <[email protected]>
PATCH v10 1/3 KVM: selftests: arm64: Report set_id_reg reads of test registers as tests
Currently when we run guest code to validate that the values we wrote to the registers are seen by the guest we assert that these values match using a KVM selftests level assert, resulting in unclear diagnostics if the test fails. Replace this assert with reporting a kselftest test per register.
In order to support getting the names of the registers we repaint the array of ID_ registers to store the names and open code the rest.
--- tools/testing/selftests/kvm/arm64/set_id_regs.c | 83 +++++++++++++++++++------ 1 file changed, 64 insertions(+), 19 deletions(-)
tools/testing/selftests/kvm/arm64/set_id_regs.c +64 -19
@@ -44,6 +44,7 @@ struct reg_ftr_bits { };struct test_feature_reg { +const char *name;u32 reg;const struct reg_ftr_bits *ftr_bits; };
@@ -228,30 +229,32 @@ static const struct reg_ftr_bits ftr_id_aa64zfr0_el1[] = {#define TEST_REG (id, table) \ { \ - .reg = id, \ + .name = #id, \ + .reg = SYS_ ## id, \ .ftr_bits = &((table)[0]), \ }static struct test_feature_reg test_regs[] = { -TEST_REG (SYS_ID_AA64DFR0_EL1 , ftr_id_aa64dfr0_el1), -TEST_REG (SYS_ID_DFR0_EL1 , ftr_id_dfr0_el1), -TEST_REG (SYS_ID_AA64ISAR0_EL1 , ftr_id_aa64isar0_el1), -TEST_REG (SYS_ID_AA64ISAR1_EL1 , ftr_id_aa64isar1_el1), -TEST_REG (SYS_ID_AA64ISAR2_EL1 , ftr_id_aa64isar2_el1), -TEST_REG (SYS_ID_AA64ISAR3_EL1 , ftr_id_aa64isar3_el1), -TEST_REG (SYS_ID_AA64PFR0_EL1 , ftr_id_aa64pfr0_el1), -TEST_REG (SYS_ID_AA64PFR1_EL1 , ftr_id_aa64pfr1_el1), -TEST_REG (SYS_ID_AA64MMFR0_EL1 , ftr_id_aa64mmfr0_el1), -TEST_REG (SYS_ID_AA64MMFR1_EL1 , ftr_id_aa64mmfr1_el1), -TEST_REG (SYS_ID_AA64MMFR2_EL1 , ftr_id_aa64mmfr2_el1), -TEST_REG (SYS_ID_AA64MMFR3_EL1 , ftr_id_aa64mmfr3_el1), -TEST_REG (SYS_ID_AA64ZFR0_EL1 , ftr_id_aa64zfr0_el1), +TEST_REG (ID_AA64DFR0_EL1 , ftr_id_aa64dfr0_el1), +TEST_REG (ID_DFR0_EL1 , ftr_id_dfr0_el1), +TEST_REG (ID_AA64ISAR0_EL1 , ftr_id_aa64isar0_el1), +TEST_REG (ID_AA64ISAR1_EL1 , ftr_id_aa64isar1_el1), +TEST_REG (ID_AA64ISAR2_EL1 , ftr_id_aa64isar2_el1), +TEST_REG (ID_AA64ISAR3_EL1 , ftr_id_aa64isar3_el1), +TEST_REG (ID_AA64PFR0_EL1 , ftr_id_aa64pfr0_el1), +TEST_REG (ID_AA64PFR1_EL1 , ftr_id_aa64pfr1_el1), +TEST_REG (ID_AA64MMFR0_EL1 , ftr_id_aa64mmfr0_el1), +TEST_REG (ID_AA64MMFR1_EL1 , ftr_id_aa64mmfr1_el1), +TEST_REG (ID_AA64MMFR2_EL1 , ftr_id_aa64mmfr2_el1), +TEST_REG (ID_AA64MMFR3_EL1 , ftr_id_aa64mmfr3_el1), +TEST_REG (ID_AA64ZFR0_EL1 , ftr_id_aa64zfr0_el1), };#define GUEST_REG_SYNC (id) GUEST_SYNC_ARGS(0, id, read_sysreg_s(id), 0, 0);static void guest_code (void ) { +/* Registers in test_regs array */ GUEST_REG_SYNC (SYS_ID_AA64DFR0_EL1 );GUEST_REG_SYNC (SYS_ID_DFR0_EL1 );GUEST_REG_SYNC (SYS_ID_AA64ISAR0_EL1 );
@@ -265,6 +268,8 @@ static void guest_code(void)GUEST_REG_SYNC (SYS_ID_AA64MMFR2_EL1 );GUEST_REG_SYNC (SYS_ID_AA64MMFR3_EL1 );GUEST_REG_SYNC (SYS_ID_AA64ZFR0_EL1 ); + +/* Additional registers counted in NUM_EXTRA_REGS */ GUEST_REG_SYNC (SYS_MPIDR_EL1 );GUEST_REG_SYNC (SYS_CLIDR_EL1 );GUEST_REG_SYNC (SYS_CTR_EL0 );
@@ -275,6 +280,35 @@ static void guest_code(void)GUEST_DONE (); } +#define NUM_EXTRA_REGS 6 +#define GUEST_READ_TEST (ARRAY_SIZE(test_regs) + NUM_EXTRA_REGS) + +static const char *get_reg_name (u64 id) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (test_regs); i++) +if (test_regs[i].reg == id) +return test_regs[i].name; + +switch (id) { +case SYS_MPIDR_EL1 : +return "MPIDR_EL1" ; +case SYS_CLIDR_EL1 : +return "CLIDR_EL1" ; +case SYS_CTR_EL0 : +return "CTR_EL0" ; +case SYS_MIDR_EL1 : +return "MIDR_EL1" ; +case SYS_REVIDR_EL1 : +return "REVIDR_EL1" ; +case SYS_AIDR_EL1 : +return "AIDR_EL1" ; +default : +TEST_FAIL ("Unknown register" ); + } +} +/* Return a safe value to a given ftr_bits an ftr value */ u64 get_safe_value (const struct reg_ftr_bits *ftr_bits,u64 ftr) {
@@ -675,7 +709,8 @@ static void test_guest_reg_read(struct kvm_vcpu *vcpu)struct ucall uc;while (!done) { -u64 val; +u64 reg_id, expected_val, guest_val; +bool match;vcpu_run (vcpu);
@@ -684,11 +719,21 @@ static void test_guest_reg_read(struct kvm_vcpu *vcpu)REPORT_GUEST_ASSERT (uc);break ;case UCALL_SYNC : - val = test_reg_vals[encoding_to_range_idx (uc.args[2 ])]; - val =reset_mutable_bits (uc.args[2 ], val); + reg_id = uc.args[2 ]; + + expected_val = test_reg_vals[encoding_to_range_idx (reg_id)]; + expected_val =reset_mutable_bits (reg_id, expected_val);/* Make sure the written values are seen by guest */ -TEST_ASSERT_EQ (val,reset_mutable_bits (uc.args[2 ], uc.args[3 ])); + guest_val =reset_mutable_bits (reg_id, uc.args[3 ]); + + match = expected_val == guest_val; +if (!match) +ksft_print_msg ("%lx != %lx\n" , + expected_val, guest_val); +ksft_test_result (match, +"%s value seen in guest\n" , +get_reg_name (reg_id));break ;case UCALL_DONE : done = true;
@@ -941,7 +986,7 @@ int main(void)ksft_print_header (); - test_cnt =4 +MPAM_IDREG_TEST +MTE_IDREG_TEST ; + test_cnt =4 +MPAM_IDREG_TEST +MTE_IDREG_TEST +GUEST_READ_TEST ;for (i =0 ; i <ARRAY_SIZE (test_regs); i++)for (j =0 ; test_regs[i].ftr_bits[j].type !=FTR_END ; j++) test_cnt++;
Signature · 1 line
2.47.3
PATCH v10 2/3 KVM: selftests: arm64: Report register reset tests individually
In reply to Mark Brownset_id_regs tests that registers have their values preserved over reset. Currently it reports all registers in a single test with an instantly fatal assert which isn't great for diagnostics, it's hard to tell which register failed or if it's just one register. Change this to report each register as a separate test so that it's clear from the program output which registers have problems.
--- tools/testing/selftests/kvm/arm64/set_id_regs.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/arm64/set_id_regs.c +13 -6
@@ -821,13 +821,21 @@ static void test_vcpu_non_ftr_id_regs(struct kvm_vcpu *vcpu)static void test_assert_id_reg_unchanged (struct kvm_vcpu *vcpu,u32 encoding) {size_t idx =encoding_to_range_idx (encoding); -u64 observed; +u64 observed, expected; +bool pass; observed =vcpu_get_reg (vcpu,KVM_ARM64_SYS_REG (encoding)); -TEST_ASSERT_EQ (reset_mutable_bits (encoding, test_reg_vals[idx]), -reset_mutable_bits (encoding, observed)); + observed =reset_mutable_bits (encoding, observed); + expected =reset_mutable_bits (encoding, test_reg_vals[idx]); + pass = expected == observed; +if (!pass) +ksft_print_msg ("%lx != %lx\n" , expected, observed); +ksft_test_result (pass,"%s unchanged by reset\n" , +get_reg_name (encoding)); } +#define ID_REG_RESET_UNCHANGED_TEST (ARRAY_SIZE(test_regs) + NUM_EXTRA_REGS) +static void test_reset_preserves_id_regs (struct kvm_vcpu *vcpu) {/*
@@ -845,8 +853,6 @@ static void test_reset_preserves_id_regs(struct kvm_vcpu *vcpu)test_assert_id_reg_unchanged (vcpu,SYS_MIDR_EL1 );test_assert_id_reg_unchanged (vcpu,SYS_REVIDR_EL1 );test_assert_id_reg_unchanged (vcpu,SYS_AIDR_EL1 ); - -ksft_test_result_pass ("%s\n" , __func__); }/*
@@ -986,7 +992,8 @@ int main(void)ksft_print_header (); - test_cnt =4 +MPAM_IDREG_TEST +MTE_IDREG_TEST +GUEST_READ_TEST ; + test_cnt =3 +MPAM_IDREG_TEST +MTE_IDREG_TEST +GUEST_READ_TEST + +ID_REG_RESET_UNCHANGED_TEST ;for (i =0 ; i <ARRAY_SIZE (test_regs); i++)for (j =0 ; test_regs[i].ftr_bits[j].type !=FTR_END ; j++) test_cnt++;
Signature · 1 line
2.47.3
PATCH v10 3/3 KVM: selftests: arm64: Make set_id_regs bitfield validity checks non-fatal
In reply to Mark BrownCurrently when set_id_regs encounters a problem checking validation of writes to feature registers it uses an immediately fatal assert to report the problem. This is not idiomatic for kselftest, and it is also not great for usability. The affected bitfield is not clearly reported and further tests do not have their results reported.
Switch to using standard kselftest result reporting for the asserts we do, these are non-fatal asserts so allow the program to continue and the test names include the affected field.
--- tools/testing/selftests/kvm/arm64/set_id_regs.c | 52 +++++++++++++++++-------- 1 file changed, 35 insertions(+), 17 deletions(-)
tools/testing/selftests/kvm/arm64/set_id_regs.c +35 -17
@@ -417,12 +417,14 @@ u64 get_invalid_value(const struct reg_ftr_bits *ftr_bits, u64 ftr)return ftr; } -static u64 test_reg_set_success (struct kvm_vcpu *vcpu,u64 reg, -const struct reg_ftr_bits *ftr_bits) +static void test_reg_set_success (struct kvm_vcpu *vcpu,u64 reg, +const struct reg_ftr_bits *ftr_bits) {u8 shift = ftr_bits->shift;u64 mask = ftr_bits->mask;u64 val, new_val, ftr; +bool pass; +int ret; val =vcpu_get_reg (vcpu, reg); ftr = (val & mask) >> shift;
@@ -433,11 +435,17 @@ static u64 test_reg_set_success(struct kvm_vcpu *vcpu, u64 reg, val &= ~mask; val |= ftr; -vcpu_set_reg (vcpu, reg, val); - new_val =vcpu_get_reg (vcpu, reg); -TEST_ASSERT_EQ (new_val, val); - -return new_val; + ret =__vcpu_set_reg (vcpu, reg, val); +if (ret ==0 ) { + new_val =vcpu_get_reg (vcpu, reg); + pass = new_val == val; +if (!pass) +ksft_print_msg ("%lx != %lx\n" , new_val, val); + }else { +ksft_print_msg ("Write of 0x%lx failed: %d\n" , val, ret); + pass = false; + } +ksft_test_result (pass,"%s valid write succeeded\n" , ftr_bits->name); } static void test_reg_set_fail(struct kvm_vcpu *vcpu, u64 reg,
@@ -447,6 +455,7 @@ static void test_reg_set_fail(struct kvm_vcpu *vcpu, u64 reg,u64 mask = ftr_bits->mask;u64 val, old_val, ftr;int r; +bool match; val =vcpu_get_reg (vcpu, reg); ftr = (val & mask) >> shift;
@@ -459,11 +468,19 @@ static void test_reg_set_fail(struct kvm_vcpu *vcpu, u64 reg, val |= ftr; r =__vcpu_set_reg (vcpu, reg, val); -TEST_ASSERT (r <0 && errno ==EINVAL , -"Unexpected KVM_SET_ONE_REG error: r=%d, errno=%d" , r, errno); +if (r >=0 || errno !=EINVAL ) { +ksft_print_msg ("Unexpected KVM_SET_ONE_REG error: r=%d, errno=%d (%s)\n" , + r, errno,strerror (errno)); +ksft_test_result_fail ("%s invalid write rejected\n" , + ftr_bits->name); +return ; + } val =vcpu_get_reg (vcpu, reg); -TEST_ASSERT_EQ (val, old_val); + match = val == old_val; +if (!match) +ksft_print_msg ("%lx != %lx\n" , val, old_val); +ksft_test_result (match,"%s invalid write rejected\n" , ftr_bits->name); }static u64 test_reg_vals[KVM_ARM_FEATURE_ID_RANGE_SIZE ];
@@ -503,7 +520,11 @@ static void test_vm_ftr_id_regs(struct kvm_vcpu *vcpu, bool aarch64_only)for (int j =0 ; ftr_bits[j].type !=FTR_END ; j++) {/* Skip aarch32 reg on aarch64 only system, since they are RAZ/WI. */ if (aarch64_only &&sys_reg_CRm (reg_id) <4 ) { -ksft_test_result_skip ("%s on AARCH64 only system\n" , +ksft_print_msg ("%s on AARCH64 only system\n" , + ftr_bits[j].name); +ksft_test_result_skip ("%s invalid write rejected\n" , + ftr_bits[j].name); +ksft_test_result_skip ("%s valid write succeeded\n" , ftr_bits[j].name);continue ; }
@@ -512,11 +533,8 @@ static void test_vm_ftr_id_regs(struct kvm_vcpu *vcpu, bool aarch64_only)TEST_ASSERT_EQ (masks[idx] & ftr_bits[j].mask, ftr_bits[j].mask);test_reg_set_fail (vcpu, reg, &ftr_bits[j]); - - test_reg_vals[idx] =test_reg_set_success (vcpu, reg, - &ftr_bits[j]); - -ksft_test_result_pass ("%s\n" , ftr_bits[j].name); +test_reg_set_success (vcpu, reg, &ftr_bits[j]); + test_reg_vals[idx] =vcpu_get_reg (vcpu, reg); } } }
@@ -996,7 +1014,7 @@ int main(void)ID_REG_RESET_UNCHANGED_TEST ;for (i =0 ; i <ARRAY_SIZE (test_regs); i++)for (j =0 ; test_regs[i].ftr_bits[j].type !=FTR_END ; j++) - test_cnt++; + test_cnt +=2 ;ksft_set_plan (test_cnt);
Signature · 1 line
2.47.3
Quoted 9 lines · Mark Brown
While debugging issues related to aarch64 only systems I ran into speedbumps due to the lack of detail in the results reported when the guest register read and reset value preservation tests were run, they generated an immediately fatal assert without indicating which register was being tested. Update these tests to non-fatally report a standard kselftest result per register, making it much easier to see what the problem being reported is.
[...]
Applied to next, thanks!
[1/3] KVM: selftests: arm64: Report set_id_reg reads of test registers as tests
https://git.kernel.org/kvmarm/kvmarm/c/dce6b987cd12 [2/3] KVM: selftests: arm64: Report register reset tests individually https://git.kernel.org/kvmarm/kvmarm/c/3ce2c97cae1d [3/3] KVM: selftests: arm64: Make set_id_regs bitfield validity checks non-fatal https://git.kernel.org/kvmarm/kvmarm/c/62927ea99e33
--
Best,
Oliver