Skip to contentSkip to conversation

kunit: tool: Move qemu architecture dependency checks into a function

RFC PATCH New David Gow 1 participant 2 messages Jul 31, 2025

Subsystems (3)
lore
Share & tools
lore

David Gow

author
#0 lore raw

RFC PATCH 1/2 kunit: tool: Move qemu architecture dependency checks into a function

Currently the RISC-V qemu architecture config has some code to check for the presence of the BIOS files before loading, printing a message and quitting if it's not present.

However, this prevents us from loading the architecture file even just to inspect it if the dependencies are not present. Instead, have kunit.py look for a check_dependencies function, and call it if present only when the architecture config is being used.

This is necessary for future changes which enumerate or automatically select an architecture.

---
 tools/testing/kunit/kunit_kernel.py       |  5 +++++
 tools/testing/kunit/qemu_configs/riscv.py | 10 ++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)
tools/testing/kunit/kunit_kernel.py +5 -0
index 260d8d9aa1db..c3201a76da24 100644
@@ -230,6 +230,11 @@ def _get_qemu_ops(config_path: str,
 	assert isinstance(spec.loader, importlib.abc.Loader)
 	spec.loader.exec_module(config)
 
+	# Check for any per-architecture dependencies
+	if hasattr(config, 'check_dependencies'):
+		if not config.check_dependencies():
+			raise ValueError('Missing dependencies for ' + config_path)
+
 	if not hasattr(config, 'QEMU_ARCH'):
 		raise ValueError('qemu_config module missing "QEMU_ARCH": ' + config_path)
 	params: qemu_config.QemuArchParams = config.QEMU_ARCH
tools/testing/kunit/qemu_configs/riscv.py +6 -4
index c87758030ff7..3c271d1005d9 100644
@@ -6,10 +6,12 @@ import sys
 OPENSBI_FILE = 'opensbi-riscv64-generic-fw_dynamic.bin'
 OPENSBI_PATH = '/usr/share/qemu/' + OPENSBI_FILE
 
-if not os.path.isfile(OPENSBI_PATH):
-	print('\n\nOpenSBI bios was not found in "' + OPENSBI_PATH + '".\n'
-	      'Please ensure that qemu-system-riscv is installed, or edit the path in "qemu_configs/riscv.py"\n')
-	sys.exit()
+def check_dependencies() -> bool:
+    if not os.path.isfile(OPENSBI_PATH):
+	    print('\n\nOpenSBI bios was not found in "' + OPENSBI_PATH + '".\n'
+	          'Please ensure that qemu-system-riscv is installed, or edit the path in "qemu_configs/riscv.py"\n')
+	    return False
+    return True
 
 QEMU_ARCH = QemuArchParams(linux_arch='riscv',
 			   kconfig='''
Signature · 1 line
2.50.1.552.g942d659e1b-goog
author
#1 lore raw

RFC PATCH 2/2 kunit: tool: Automatically pick a default architecture if none is specified

Currently, kunit.py will default to 'um' (User-Mode Linux) if no specific --arch option is given. However, UML is only available on x86-based architectures, so kunit.py fails by default on anything else.

Instead, for non-x86 architectures, enumerate the qemu configs looking for one which matches the current architecture. This currently uses uname -m, which doesn't always match the kernel/kunit's architecture name (e.g., aarch64 versus arm64), so look at both the kernel and qemu architecture names in the qemu config until one matches.

With this change ./tools/testing/kunit/kunit.py run should function out-of-the-box on most non-x86 architectures, assuming qemu is installed.

---
 tools/testing/kunit/kunit.py           |  2 +-
 tools/testing/kunit/kunit_kernel.py    | 43 ++++++++++++++++++++------
 tools/testing/kunit/kunit_tool_test.py | 10 +++---
 3 files changed, 40 insertions(+), 15 deletions(-)
tools/testing/kunit/kunit.py +1 -1
index 7f9ae55fd6d5..188bb7f2802f 100755
@@ -351,7 +351,7 @@ def add_common_opts(parser: argparse.ArgumentParser) -> None:
 				  'string passed to the ARCH make param, '
 				  'e.g. i386, x86_64, arm, um, etc. Non-UML '
 				  'architectures run on QEMU.'),
-			    type=str, default='um', metavar='ARCH')
+			    type=str, metavar='ARCH')
 
 	parser.add_argument('--cross_compile',
 			    help=('Sets make\'s CROSS_COMPILE variable; it should '
tools/testing/kunit/kunit_kernel.py +34 -9

59 lines not on this page · raw message

tools/testing/kunit/kunit_tool_test.py +5 -5
index bbba921e0eac..a3dd456d62cd 100755
@@ -747,7 +747,7 @@ class KUnitMainTest(unittest.TestCase):
 		self.mock_linux_init.assert_called_once_with('.kunit',
 						kunitconfig_paths=['mykunitconfig'],
 						kconfig_add=None,
-						arch='um',
+						arch=None,
 						cross_compile=None,
 						qemu_config_path=None,
 						extra_qemu_args=[])
@@ -758,7 +758,7 @@ class KUnitMainTest(unittest.TestCase):
 		self.mock_linux_init.assert_called_once_with('.kunit',
 						kunitconfig_paths=['mykunitconfig'],
 						kconfig_add=None,
-						arch='um',
+						arch=None,
 						cross_compile=None,
 						qemu_config_path=None,
 						extra_qemu_args=[])
@@ -769,7 +769,7 @@ class KUnitMainTest(unittest.TestCase):
 		self.mock_linux_init.assert_called_once_with('.kunit',
 						kunitconfig_paths=[kunit_kernel.ALL_TESTS_CONFIG_PATH, 'mykunitconfig'],
 						kconfig_add=None,
-						arch='um',
+						arch=None,
 						cross_compile=None,
 						qemu_config_path=None,
 						extra_qemu_args=[])
@@ -783,7 +783,7 @@ class KUnitMainTest(unittest.TestCase):
 		mock_linux_init.assert_called_once_with('.kunit',
 							kunitconfig_paths=['mykunitconfig', 'other'],
 							kconfig_add=None,
-							arch='um',
+							arch=None,
 							cross_compile=None,
 							qemu_config_path=None,
 							extra_qemu_args=[])
@@ -794,7 +794,7 @@ class KUnitMainTest(unittest.TestCase):
 		self.mock_linux_init.assert_called_once_with('.kunit',
 						kunitconfig_paths=[],
 						kconfig_add=['CONFIG_KASAN=y', 'CONFIG_KCSAN=y'],
-						arch='um',
+						arch=None,
 						cross_compile=None,
 						qemu_config_path=None,
 						extra_qemu_args=[])
Signature · 1 line
2.50.1.552.g942d659e1b-goog