[PATCH] tests/functional: Check for nested virtualization to run KVM tests

Philippe Mathieu-Daudé posted 1 patch 3 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260116070438.31869-1-philmd@linaro.org
Maintainers: Peter Maydell <peter.maydell@linaro.org>
tests/functional/aarch64/test_kvm.py | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
[PATCH] tests/functional: Check for nested virtualization to run KVM tests
Posted by Philippe Mathieu-Daudé 3 weeks ago
On Apple Silicon, nested virtualization starts to be usable
with M3 models and later. Check for the CPU model to avoid
failure on pre-M3:

  qemu-system-aarch64: unable to find CPU model 'cortex-a72'

Now tests are correctly skipped, i.e. on M1:

  ok 1 test_kvm.Aarch64VirtKVMTests.test_aarch64_nvhe_selftest # SKIP Nested Virtualization not available on Apple M1 Pro
  ok 2 test_kvm.Aarch64VirtKVMTests.test_aarch64_vhe_selftest # SKIP Nested Virtualization not available on Apple M1 Pro

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 tests/functional/aarch64/test_kvm.py | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/tests/functional/aarch64/test_kvm.py b/tests/functional/aarch64/test_kvm.py
index 7545f5ed554..b26c849ec67 100755
--- a/tests/functional/aarch64/test_kvm.py
+++ b/tests/functional/aarch64/test_kvm.py
@@ -29,8 +29,31 @@ class Aarch64VirtKVMTests(LinuxKernelTest):
     # base of tests
     KUT_BASE = "/usr/share/kvm-unit-tests/"
 
+    def require_nested_virtualization(self):
+        """
+        Requires the accelerator to support nested virtualization for the test
+        to continue
+
+        If the check fails, the test is canceled.
+        """
+        import platform, re, subprocess
+
+        if platform.system() != 'Darwin':
+            return
+        r = subprocess.run(['sysctl', '-n', 'machdep.cpu.brand_string'],
+                           text=True, capture_output=True)
+        if r.returncode != 0:
+            return
+        m = re.match(r"Apple M(\d+)( .*)?", r.stdout)
+        if m:
+            if int(m.group(1)) < 3:
+                self.skipTest("Nested Virtualization not available"
+                              " on %s" % r.stdout.strip())
+
     def _launch_guest(self, kvm_mode="nvhe"):
 
+        self.require_nested_virtualization()
+
         self.set_machine('virt')
         kernel_path = self.ASSET_KVM_TEST_KERNEL.fetch()
 
-- 
2.52.0


Re: [PATCH] tests/functional: Check for nested virtualization to run KVM tests
Posted by Daniel P. Berrangé 3 weeks ago
On Fri, Jan 16, 2026 at 08:04:38AM +0100, Philippe Mathieu-Daudé wrote:
> On Apple Silicon, nested virtualization starts to be usable
> with M3 models and later. Check for the CPU model to avoid
> failure on pre-M3:
> 
>   qemu-system-aarch64: unable to find CPU model 'cortex-a72'
> 
> Now tests are correctly skipped, i.e. on M1:
> 
>   ok 1 test_kvm.Aarch64VirtKVMTests.test_aarch64_nvhe_selftest # SKIP Nested Virtualization not available on Apple M1 Pro
>   ok 2 test_kvm.Aarch64VirtKVMTests.test_aarch64_vhe_selftest # SKIP Nested Virtualization not available on Apple M1 Pro
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>  tests/functional/aarch64/test_kvm.py | 23 +++++++++++++++++++++++
>  1 file changed, 23 insertions(+)
> 
> diff --git a/tests/functional/aarch64/test_kvm.py b/tests/functional/aarch64/test_kvm.py
> index 7545f5ed554..b26c849ec67 100755
> --- a/tests/functional/aarch64/test_kvm.py
> +++ b/tests/functional/aarch64/test_kvm.py
> @@ -29,8 +29,31 @@ class Aarch64VirtKVMTests(LinuxKernelTest):
>      # base of tests
>      KUT_BASE = "/usr/share/kvm-unit-tests/"
>  
> +    def require_nested_virtualization(self):
> +        """
> +        Requires the accelerator to support nested virtualization for the test
> +        to continue
> +
> +        If the check fails, the test is canceled.
> +        """
> +        import platform, re, subprocess
> +
> +        if platform.system() != 'Darwin':
> +            return
> +        r = subprocess.run(['sysctl', '-n', 'machdep.cpu.brand_string'],
> +                           text=True, capture_output=True)
> +        if r.returncode != 0:
> +            return
> +        m = re.match(r"Apple M(\d+)( .*)?", r.stdout)

I tend to prefer check_call/check_output over the plain 'run',
in this case check_output is suitble

    try
       data = subprocess.check_output([..args..], encoding='utf8')
    except CalledProcessError
       return
    m = re.match(r"Apple M(\d+)( .*)?", data)


> +        if m:
> +            if int(m.group(1)) < 3:
> +                self.skipTest("Nested Virtualization not available"
> +                              " on %s" % r.stdout.strip())

Please add this as a decorator in tests/functional/qemu_tests/decorators.py
called something like  "skipUnlessNestedVirtSupported".

> +
>      def _launch_guest(self, kvm_mode="nvhe"):
>  
> +        self.require_nested_virtualization()
> +
>          self.set_machine('virt')
>          kernel_path = self.ASSET_KVM_TEST_KERNEL.fetch()
>  
> -- 
> 2.52.0
> 
> 

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|