From nobody Mon May 11 00:10:33 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E895C433EF for ; Wed, 20 Apr 2022 20:30:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382150AbiDTUdV (ORCPT ); Wed, 20 Apr 2022 16:33:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382138AbiDTUdR (ORCPT ); Wed, 20 Apr 2022 16:33:17 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C647344C4 for ; Wed, 20 Apr 2022 13:30:30 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-2ec1914e315so25258447b3.23 for ; Wed, 20 Apr 2022 13:30:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=AXD6tg0Y/lSZ4l/KU4T2EXjDq6mZdsK/9IDg33E4+DA=; b=qwZe0Qz42RUbQQDExper1fKktpd02nJILCYCAaAF8IT4RrvRn/Sgg/K9iLi6C9d7Z7 CefqGeUGfmTZbYxwBHWGjNzcPIwDcG1ruhL9E9q+tm34m3EuhQNo7gZvDOKouClwoZwY Anzy9OvgJPS2lFEb4dWeSSQIQfrw2IjnOqYj7JjPWTfuc5L2zQnT8wLJyqrH+mnyK7xH G22pVzEcPGxVfrM7mWqUfhQ79H3/SU2vp+9AqtdaFjJxzH8Qb01Dibznk5cJn+qtZ6bK Ekr6iFjyoSNuuV1noVL1hNL35cwbkREvHwc6PkuWBQ6lXmZmfsOSnHU91c4vAHo0a4ed BsuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=AXD6tg0Y/lSZ4l/KU4T2EXjDq6mZdsK/9IDg33E4+DA=; b=lMq/fI4/u4zhzQMKzYGkhqcHZBllO8fd9BP3d8q/x1NWqrlyNIVcCaZRechBwzQ0pH d/NL7wwWgTeDx0W7fO09Q66MaXRXEipRSoZQAjhguubjHW9X3JUIueNgjVjwKTEqSbGx axXe+Oh2SwkPC+NdZ3CyhxkOuP188oMGJl1FWvdYhtm6ktRrE2KpQQ5sl34ruRdwBnOR UA3j18cc+MMSZWbRfBIgjuwAwgaEhp/z0t1/hst2fa2wfkCEOvOrl1X3IfBCXzI6loi8 Dwjkm9qI9vNG9j6G8TYn5wmsr4eyybGEbCCEf/zYq4zt/EQBr589tcKNJp/HFGabRCGK zsZA== X-Gm-Message-State: AOAM531+5PzkdVrMU2Qz0Bd9vNIryhgg51F0k/tCLOv1ovJG9lLws9/E hAoX6Qn9p1j8zs082r7Muar3nBrLE9YK7w== X-Google-Smtp-Source: ABdhPJyeZNWg0oaUVoRVWM+O13/EhmeQRvWqCylEwV+94Me2sT9LP1a8LKqYUGIgbvH6iw+XwaE92QePTs6rjg== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:ccc:f7fa:7a7e:1435]) (user=dlatypov job=sendgmr) by 2002:a25:bc04:0:b0:641:cfe9:ff38 with SMTP id i4-20020a25bc04000000b00641cfe9ff38mr21647645ybh.213.1650486629286; Wed, 20 Apr 2022 13:30:29 -0700 (PDT) Date: Wed, 20 Apr 2022 13:30:20 -0700 Message-Id: <20220420203020.1412886-1-dlatypov@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [PATCH] kunit: tool: stop using a shell to run kernel under QEMU From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Note: this potentially breaks custom qemu_configs if people are using them! But the fix for them is simple, don't specify multiple arguments in one string and don't add on a redundant ''. It feels a bit iffy to be using a shell in the first place. There's the usual shenanigans where people could pass in arbitrary shell commands via --kernel_arg (since we're just adding '' around the kernel_cmdline) or via a custom qemu_config. This isn't too much of a concern given the nature of this script (and the qemu_config file is in python, you can do w/e you want already). But it does have some other drawbacks. One example of a kunit-specific pain point: If the relevant qemu binary is missing, we get output like this: > /bin/sh: line 1: qemu-system-aarch64: command not found This in turn results in our KTAP parser complaining about missing/invalid KTAP, but we don't directly show the error! It's even more annoying to debug when you consider --raw_output only shows KUnit output by default, i.e. you need --raw_output=3Dall to see it. Whereas directly invoking the binary, Python will raise a FileNotFoundError for us, which is a noisier but more clear. Making this change requires * splitting parameters like ['-m 256'] into ['-m', '256'] in kunit/qemu_configs/*.py * change [''] to [] in kunit/qemu_configs/*.py since otherwise QEMU fails w/ 'Device needs media, but drive is empty' * dropping explicit quoting of the kernel cmdline * using shlex.quote() when we print what command we're running so the user can copy-paste and run it Signed-off-by: Daniel Latypov Reviewed-by: Brendan Higgins --- tools/testing/kunit/kunit_kernel.py | 18 ++++++++++-------- tools/testing/kunit/qemu_configs/alpha.py | 2 +- tools/testing/kunit/qemu_configs/arm.py | 2 +- tools/testing/kunit/qemu_configs/arm64.py | 2 +- tools/testing/kunit/qemu_configs/i386.py | 2 +- tools/testing/kunit/qemu_configs/powerpc.py | 2 +- tools/testing/kunit/qemu_configs/riscv.py | 6 +++--- tools/testing/kunit/qemu_configs/s390.py | 4 ++-- tools/testing/kunit/qemu_configs/sparc.py | 2 +- tools/testing/kunit/qemu_configs/x86_64.py | 2 +- 10 files changed, 22 insertions(+), 20 deletions(-) diff --git a/tools/testing/kunit/kunit_kernel.py b/tools/testing/kunit/kuni= t_kernel.py index 483f78e15ce9..1b9c4922a675 100644 --- a/tools/testing/kunit/kunit_kernel.py +++ b/tools/testing/kunit/kunit_kernel.py @@ -11,6 +11,7 @@ import importlib.util import logging import subprocess import os +import shlex import shutil import signal import threading @@ -118,16 +119,17 @@ class LinuxSourceTreeOperationsQemu(LinuxSourceTreeOp= erations): '-nodefaults', '-m', '1024', '-kernel', kernel_path, - '-append', '\'' + ' '.join(params + [self._kernel_command_line]) + '\'= ', + '-append', ' '.join(params + [self._kernel_command_line]), '-no-reboot', '-nographic', - '-serial stdio'] + self._extra_qemu_params - print('Running tests with:\n$', ' '.join(qemu_command)) - return subprocess.Popen(' '.join(qemu_command), - stdin=3Dsubprocess.PIPE, - stdout=3Dsubprocess.PIPE, - stderr=3Dsubprocess.STDOUT, - text=3DTrue, shell=3DTrue, errors=3D'backslashreplace') + '-serial', 'stdio'] + self._extra_qemu_params + # Note: shlex.join() does what we want, but requires python 3.8+. + print('Running tests with:\n$', ' '.join(shlex.quote(arg) for arg in qem= u_command)) + return subprocess.Popen(qemu_command, + stdin=3Dsubprocess.PIPE, + stdout=3Dsubprocess.PIPE, + stderr=3Dsubprocess.STDOUT, + text=3DTrue, errors=3D'backslashreplace') =20 class LinuxSourceTreeOperationsUml(LinuxSourceTreeOperations): """An abstraction over command line operations performed on a source tree= .""" diff --git a/tools/testing/kunit/qemu_configs/alpha.py b/tools/testing/kuni= t/qemu_configs/alpha.py index 5d0c0cff03bd..3ac846e03a6b 100644 --- a/tools/testing/kunit/qemu_configs/alpha.py +++ b/tools/testing/kunit/qemu_configs/alpha.py @@ -7,4 +7,4 @@ CONFIG_SERIAL_8250_CONSOLE=3Dy''', qemu_arch=3D'alpha', kernel_path=3D'arch/alpha/boot/vmlinux', kernel_command_line=3D'console=3DttyS0', - extra_qemu_params=3D['']) + extra_qemu_params=3D[]) diff --git a/tools/testing/kunit/qemu_configs/arm.py b/tools/testing/kunit/= qemu_configs/arm.py index b9c2a35e0296..db2160200566 100644 --- a/tools/testing/kunit/qemu_configs/arm.py +++ b/tools/testing/kunit/qemu_configs/arm.py @@ -10,4 +10,4 @@ CONFIG_SERIAL_AMBA_PL011_CONSOLE=3Dy''', qemu_arch=3D'arm', kernel_path=3D'arch/arm/boot/zImage', kernel_command_line=3D'console=3DttyAMA0', - extra_qemu_params=3D['-machine virt']) + extra_qemu_params=3D['-machine', 'virt']) diff --git a/tools/testing/kunit/qemu_configs/arm64.py b/tools/testing/kuni= t/qemu_configs/arm64.py index 517c04459f47..67d04064f785 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=3Dy''', qemu_arch=3D'aarch64', kernel_path=3D'arch/arm64/boot/Image.gz', kernel_command_line=3D'console=3DttyAMA0', - extra_qemu_params=3D['-machine virt', '-cpu cortex-a57']) + extra_qemu_params=3D['-machine', 'virt', '-cpu', 'cortex-a57']) diff --git a/tools/testing/kunit/qemu_configs/i386.py b/tools/testing/kunit= /qemu_configs/i386.py index aed3ffd3937d..52b80be40e4b 100644 --- a/tools/testing/kunit/qemu_configs/i386.py +++ b/tools/testing/kunit/qemu_configs/i386.py @@ -7,4 +7,4 @@ CONFIG_SERIAL_8250_CONSOLE=3Dy''', qemu_arch=3D'x86_64', kernel_path=3D'arch/x86/boot/bzImage', kernel_command_line=3D'console=3DttyS0', - extra_qemu_params=3D['']) + extra_qemu_params=3D[]) diff --git a/tools/testing/kunit/qemu_configs/powerpc.py b/tools/testing/ku= nit/qemu_configs/powerpc.py index 35e9de24f0db..6c901149726b 100644 --- a/tools/testing/kunit/qemu_configs/powerpc.py +++ b/tools/testing/kunit/qemu_configs/powerpc.py @@ -9,4 +9,4 @@ CONFIG_HVC_CONSOLE=3Dy''', qemu_arch=3D'ppc64', kernel_path=3D'vmlinux', kernel_command_line=3D'console=3DttyS0', - extra_qemu_params=3D['-M pseries', '-cpu power8']) + extra_qemu_params=3D['-M', ' pseries', '-cpu', 'power8']) diff --git a/tools/testing/kunit/qemu_configs/riscv.py b/tools/testing/kuni= t/qemu_configs/riscv.py index 9e528087cd7c..b882fde39435 100644 --- a/tools/testing/kunit/qemu_configs/riscv.py +++ b/tools/testing/kunit/qemu_configs/riscv.py @@ -26,6 +26,6 @@ CONFIG_SERIAL_EARLYCON_RISCV_SBI=3Dy''', kernel_path=3D'arch/riscv/boot/Image', kernel_command_line=3D'console=3DttyS0', extra_qemu_params=3D[ - '-machine virt', - '-cpu rv64', - '-bios opensbi-riscv64-generic-fw_dynamic.bin']) + '-machine', 'virt', + '-cpu', 'rv64', + '-bios', 'opensbi-riscv64-generic-fw_dynamic.bin']) diff --git a/tools/testing/kunit/qemu_configs/s390.py b/tools/testing/kunit= /qemu_configs/s390.py index e310bd521113..98fa4fb60c0a 100644 --- a/tools/testing/kunit/qemu_configs/s390.py +++ b/tools/testing/kunit/qemu_configs/s390.py @@ -10,5 +10,5 @@ CONFIG_MODULES=3Dy''', kernel_path=3D'arch/s390/boot/bzImage', kernel_command_line=3D'console=3DttyS0', extra_qemu_params=3D[ - '-machine s390-ccw-virtio', - '-cpu qemu',]) + '-machine', 's390-ccw-virtio', + '-cpu', 'qemu',]) diff --git a/tools/testing/kunit/qemu_configs/sparc.py b/tools/testing/kuni= t/qemu_configs/sparc.py index 27f474e7ad6e..e975c4331a7c 100644 --- a/tools/testing/kunit/qemu_configs/sparc.py +++ b/tools/testing/kunit/qemu_configs/sparc.py @@ -7,4 +7,4 @@ CONFIG_SERIAL_8250_CONSOLE=3Dy''', qemu_arch=3D'sparc', kernel_path=3D'arch/sparc/boot/zImage', kernel_command_line=3D'console=3DttyS0 mem=3D256M', - extra_qemu_params=3D['-m 256']) + extra_qemu_params=3D['-m', '256']) diff --git a/tools/testing/kunit/qemu_configs/x86_64.py b/tools/testing/kun= it/qemu_configs/x86_64.py index 77ab1aeee8a3..dc7949076863 100644 --- a/tools/testing/kunit/qemu_configs/x86_64.py +++ b/tools/testing/kunit/qemu_configs/x86_64.py @@ -7,4 +7,4 @@ CONFIG_SERIAL_8250_CONSOLE=3Dy''', qemu_arch=3D'x86_64', kernel_path=3D'arch/x86/boot/bzImage', kernel_command_line=3D'console=3DttyS0', - extra_qemu_params=3D['']) + extra_qemu_params=3D[]) base-commit: 59729170afcd4900e08997a482467ffda8d88c7f --=20 2.36.0.rc0.470.gd361397f0d-goog