Re: [PATCH 03/13] KVM: arm64: selftests: Add helper to check for VGICv3 support

Itaru Kitayama
2025-09-18T01:46:03Z

On Wed, Sep 17, 2025 at 02:20:33PM -0700, Oliver Upton wrote:
> Introduce a proper predicate for probing VGICv3 by performing a 'test'
> creation of the device on a dummy VM.
>
> Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
> ---
> tools/testing/selftests/kvm/arm64/arch_timer.c | 3 ++-
> .../selftests/kvm/arm64/arch_timer_edge_cases.c | 3 ++-
> tools/testing/selftests/kvm/arm64/vgic_irq.c | 3 ++-
> tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c | 4 ++--
> .../testing/selftests/kvm/arm64/vpmu_counter_access.c | 3 +--
> tools/testing/selftests/kvm/include/arm64/vgic.h | 1 +
> tools/testing/selftests/kvm/lib/arm64/vgic.c | 11 +++++++++++
> 7 files changed, 21 insertions(+), 7 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/arm64/arch_timer.c b/tools/testing/selftests/kvm/arm64/arch_timer.c
> index eeba1cc87ff8..aaf4285f832a 100644
> --- a/tools/testing/selftests/kvm/arm64/arch_timer.c
> +++ b/tools/testing/selftests/kvm/arm64/arch_timer.c
> @@ -184,6 +184,8 @@ struct kvm_vm *test_vm_create(void)
> unsigned int i;
> int nr_vcpus = test_args.nr_vcpus;
>
> + TEST_REQUIRE(kvm_supports_vgic_v3());
> +
> vm = vm_create_with_vcpus(nr_vcpus, guest_code, vcpus);
>
> vm_init_descriptor_tables(vm);
> @@ -205,7 +207,6 @@ struct kvm_vm *test_vm_create(void)
>
> test_init_timer_irq(vm);
> gic_fd = vgic_v3_setup(vm, nr_vcpus, 64);
> - __TEST_REQUIRE(gic_fd >= 0, "Failed to create vgic-v3");
>
> /* Make all the test's cmdline args visible to the guest */
> sync_global_to_guest(vm, test_args);
> diff --git a/tools/testing/selftests/kvm/arm64/arch_timer_edge_cases.c b/tools/testing/selftests/kvm/arm64/arch_timer_edge_cases.c
> index ce74d069cb7b..d349d80d8418 100644
> --- a/tools/testing/selftests/kvm/arm64/arch_timer_edge_cases.c
> +++ b/tools/testing/selftests/kvm/arm64/arch_timer_edge_cases.c
> @@ -952,7 +952,6 @@ static void test_vm_create(struct kvm_vm **vm, struct kvm_vcpu **vcpu,
>
> test_init_timer_irq(*vm, *vcpu);
> gic_fd = vgic_v3_setup(*vm, 1, 64);
> - __TEST_REQUIRE(gic_fd >= 0, "Failed to create vgic-v3");
>
> sync_global_to_guest(*vm, test_args);
> sync_global_to_guest(*vm, CVAL_MAX);
> @@ -1042,6 +1041,8 @@ int main(int argc, char *argv[])
> /* Tell stdout not to buffer its content */
> setbuf(stdout, NULL);
>
> + TEST_REQUIRE(kvm_supports_vgic_v3());
> +
> if (!parse_args(argc, argv))
> exit(KSFT_SKIP);
>
> diff --git a/tools/testing/selftests/kvm/arm64/vgic_irq.c b/tools/testing/selftests/kvm/arm64/vgic_irq.c
> index a09dd423c2d7..9fc9e8e44ecd 100644
> --- a/tools/testing/selftests/kvm/arm64/vgic_irq.c
> +++ b/tools/testing/selftests/kvm/arm64/vgic_irq.c
> @@ -752,7 +752,6 @@ static void test_vgic(uint32_t nr_irqs, bool level_sensitive, bool eoi_split)
> vcpu_args_set(vcpu, 1, args_gva);
>
> gic_fd = vgic_v3_setup(vm, 1, nr_irqs);
> - __TEST_REQUIRE(gic_fd >= 0, "Failed to create vgic-v3, skipping");
>
> vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT,
> guest_irq_handlers[args.eoi_split][args.level_sensitive]);
> @@ -802,6 +801,8 @@ int main(int argc, char **argv)
> int opt;
> bool eoi_split = false;
>
> + TEST_REQUIRE(kvm_supports_vgic_v3());
> +
> while ((opt = getopt(argc, argv, "hn:e:l:")) != -1) {
> switch (opt) {
> case 'n':
> diff --git a/tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c b/tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c
> index fc4fe52fb6f8..cc2b21d374af 100644
> --- a/tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c
> +++ b/tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c
> @@ -215,8 +215,6 @@ static void setup_test_data(void)
> static void setup_gic(void)
> {
> gic_fd = vgic_v3_setup(vm, test_data.nr_cpus, 64);
> - __TEST_REQUIRE(gic_fd >= 0, "Failed to create GICv3");
> -
> its_fd = vgic_its_setup(vm);
> }
>
> @@ -374,6 +372,8 @@ int main(int argc, char **argv)
> u32 nr_threads;
> int c;
>
> + TEST_REQUIRE(kvm_supports_vgic_v3());
> +
> while ((c = getopt(argc, argv, "hv:d:e:i:")) != -1) {
> switch (c) {
> case 'v':
> diff --git a/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c b/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c
> index a0c4ab839155..01f61657de45 100644
> --- a/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c
> +++ b/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c
> @@ -436,8 +436,6 @@ static void create_vpmu_vm(void *guest_code)
> vpmu_vm.vcpu = aarch64_vcpu_add(vpmu_vm.vm, 0, &init, guest_code);
> vcpu_init_descriptor_tables(vpmu_vm.vcpu);
> vpmu_vm.gic_fd = vgic_v3_setup(vpmu_vm.vm, 1, 64);
> - __TEST_REQUIRE(vpmu_vm.gic_fd >= 0,
> - "Failed to create vgic-v3, skipping");
>
> /* Make sure that PMUv3 support is indicated in the ID register */
> dfr0 = vcpu_get_reg(vpmu_vm.vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64DFR0_EL1));
> @@ -634,6 +632,7 @@ int main(void)
> uint64_t i, pmcr_n;
>
> TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_PMU_V3));
> + TEST_REQUIRE(kvm_supports_vgic_v3());
>
> pmcr_n = get_pmcr_n_limit();
> for (i = 0; i <= pmcr_n; i++) {
> diff --git a/tools/testing/selftests/kvm/include/arm64/vgic.h b/tools/testing/selftests/kvm/include/arm64/vgic.h
> index c481d0c00a5d..b858fa8195b4 100644
> --- a/tools/testing/selftests/kvm/include/arm64/vgic.h
> +++ b/tools/testing/selftests/kvm/include/arm64/vgic.h
> @@ -16,6 +16,7 @@
> ((uint64_t)(flags) << 12) | \
> index)
>
> +bool kvm_supports_vgic_v3(void);
> int vgic_v3_setup(struct kvm_vm *vm, unsigned int nr_vcpus, uint32_t nr_irqs);
>
> #define VGIC_MAX_RESERVED 1023
> diff --git a/tools/testing/selftests/kvm/lib/arm64/vgic.c b/tools/testing/selftests/kvm/lib/arm64/vgic.c
> index 64e793795563..661744c6532e 100644
> --- a/tools/testing/selftests/kvm/lib/arm64/vgic.c
> +++ b/tools/testing/selftests/kvm/lib/arm64/vgic.c
> @@ -15,6 +15,17 @@
> #include "gic.h"
> #include "gic_v3.h"
>
> +bool kvm_supports_vgic_v3(void)
> +{
> + struct kvm_vm *vm = vm_create_barebones();
> + int r;
> +
> + r = __kvm_test_create_device(vm, KVM_DEV_TYPE_ARM_VGIC_V3);
> + kvm_vm_free(vm);
> +
> + return !r;
> +}
> +
> /*
> * vGIC-v3 default host setup
> *
> --
> 2.47.3
>

Oliver
The arch_timer_edge_cases selftest does not return nor fails on FVP RevC.
I boot the system with kvm-arm.mode=nested.

Thanks,
Itaru.