mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tamir Duberstein <tamird@gmail.com>
To: Brendan Higgins <brendan.higgins@linux.dev>,
	 David Gow <davidgow@google.com>, Rae Moar <rmoar@google.com>
Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com,
	 linux-kernel@vger.kernel.org,
	Tamir Duberstein <tamird@gmail.com>
Subject: [PATCH 2/2] kunit: enable hardware acceleration when available
Date: Fri, 25 Oct 2024 17:03:54 -0400	[thread overview]
Message-ID: <20241025-kunit-qemu-accel-macos-v1-2-2f30c26192d4@gmail.com> (raw)
In-Reply-To: <20241025-kunit-qemu-accel-macos-v1-0-2f30c26192d4@gmail.com>

Use KVM or HVF if supported by the QEMU binary and available on the
system.

This produces a nice improvement on my Apple M3 Pro running macOS 14.7:

Before:
./tools/testing/kunit/kunit.py exec --arch arm64
[HH:MM:SS] Elapsed time: 10.145s

After:
./tools/testing/kunit/kunit.py exec --arch arm64
[HH:MM:SS] Elapsed time: 1.773s

Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
 tools/testing/kunit/kunit_kernel.py       | 26 +++++++++++++++++++++++++-
 tools/testing/kunit/qemu_configs/arm64.py |  2 +-
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/tools/testing/kunit/kunit_kernel.py b/tools/testing/kunit/kunit_kernel.py
index 61931c4926fd6645f2c62dd13f9842a432ec4167..10cacf5a3c443bacd6c074647e4bddfc31599cf8 100644
--- a/tools/testing/kunit/kunit_kernel.py
+++ b/tools/testing/kunit/kunit_kernel.py
@@ -116,7 +116,8 @@ class LinuxSourceTreeOperationsQemu(LinuxSourceTreeOperations):
 
 	def start(self, params: List[str], build_dir: str) -> subprocess.Popen:
 		kernel_path = os.path.join(build_dir, self._kernel_path)
-		qemu_command = ['qemu-system-' + self._qemu_arch,
+		qemu_binary = 'qemu-system-' + self._qemu_arch
+		qemu_command = [qemu_binary,
 				'-nodefaults',
 				'-m', '1024',
 				'-kernel', kernel_path,
@@ -124,6 +125,29 @@ class LinuxSourceTreeOperationsQemu(LinuxSourceTreeOperations):
 				'-no-reboot',
 				'-nographic',
 				'-serial', self._serial] + self._extra_qemu_params
+		accelerators = {
+			line.strip()
+			for line in subprocess.check_output([qemu_binary, "-accel", "help"], text=True).splitlines()
+			if line and line.islower()
+		}
+		if 'kvm' in accelerators:
+			try:
+				with open('/dev/kvm', 'rb+'):
+					qemu_command.extend(['-accel', 'kvm'])
+			except OSError as e:
+				print(e)
+		elif 'hvf' in accelerators:
+			try:
+				for line in subprocess.check_output(['sysctl', 'kern.hv_support'], text=True).splitlines():
+					if not line:
+						continue
+					key, value = line.split(':')
+					if key == 'kern.hv_support' and bool(value):
+						qemu_command.extend(['-accel', 'hvf'])
+						break
+			except subprocess.CalledProcessError as e:
+				print(e)
+
 		# Note: shlex.join() does what we want, but requires python 3.8+.
 		print('Running tests with:\n$', ' '.join(shlex.quote(arg) for arg in qemu_command))
 		return subprocess.Popen(qemu_command,
diff --git a/tools/testing/kunit/qemu_configs/arm64.py b/tools/testing/kunit/qemu_configs/arm64.py
index d3ff27024755411441f910799be30399295c9541..5c44d3a87e6dd2cd6b086138186a277a1473585b 100644
--- a/tools/testing/kunit/qemu_configs/arm64.py
+++ b/tools/testing/kunit/qemu_configs/arm64.py
@@ -9,4 +9,4 @@ CONFIG_SERIAL_AMBA_PL011_CONSOLE=y''',
 			   qemu_arch='aarch64',
 			   kernel_path='arch/arm64/boot/Image.gz',
 			   kernel_command_line='console=ttyAMA0',
-			   extra_qemu_params=['-machine', 'virt', '-cpu', 'max,pauth-impdef=on'])
+			   extra_qemu_params=['-machine', 'virt', '-cpu', 'max'])

-- 
2.47.0


  parent reply	other threads:[~2024-10-25 21:04 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-25 21:03 [PATCH 0/2] kunit: enable hardware virtualization Tamir Duberstein
2024-10-25 21:03 ` [PATCH 1/2] kunit: add fallback for os.sched_getaffinity Tamir Duberstein
2024-10-25 21:03 ` Tamir Duberstein [this message]
2024-11-01 13:52   ` [PATCH 2/2] kunit: enable hardware acceleration when available Alyssa Ross
2024-11-01 14:49     ` Tamir Duberstein
2024-11-01 15:23       ` Thomas Weißschuh
2024-11-02 11:10         ` Tamir Duberstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241025-kunit-qemu-accel-macos-v1-2-2f30c26192d4@gmail.com \
    --to=tamird@gmail.com \
    --cc=brendan.higgins@linux.dev \
    --cc=davidgow@google.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=rmoar@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®