From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7963FC83F22 for ; Thu, 17 Jul 2025 08:49:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 086668D0015; Thu, 17 Jul 2025 04:49:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 037C38D0011; Thu, 17 Jul 2025 04:49:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB9E58D0015; Thu, 17 Jul 2025 04:49:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A9A1D8D0015 for ; Thu, 17 Jul 2025 04:49:03 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 7F9CA14067C for ; Thu, 17 Jul 2025 08:49:03 +0000 (UTC) X-FDA: 83673131766.20.CDC6EF9 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by imf13.hostedemail.com (Postfix) with ESMTP id C10F020005 for ; Thu, 17 Jul 2025 08:49:01 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b=dWFD09ri; dkim=pass header.d=linutronix.de header.s=2020e header.b=iYbpEPZM; spf=pass (imf13.hostedemail.com: domain of t-8ch@linutronix.de designates 193.142.43.55 as permitted sender) smtp.mailfrom=t-8ch@linutronix.de; dmarc=pass (policy=none) header.from=linutronix.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1752742142; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QRrnBoH8IFFRjQzyw//nS3EiPMvLn5TdGq4dYEYIlJg=; b=ocbiGRBSK6+/QYEjFP/8Yqj/k1rqJkQjjjWNJcOFzuf23EomgMwaIa7JNAoto/tX7WtSIr Q+9qD54IEOk5f3LTe2T6ZS/aFLmSCx355m4vDjFxlqZkJcehrX0hFcFk6yI1Ik9O9jhW49 9vHAgeqbIHQHMist7qi+vH6ZKzoIZYE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752742142; a=rsa-sha256; cv=none; b=eRcdYWJlDd1GknrBK971kUAs0WW2z+AyEnSbs71uL26/u7PFT+CWJI/BP0JOU9DRM2R4KH CYtCg+svKCuhGA8yyKhDS/BfM6FKvQtn4cbyyXA3jitgglJWqIK5TAFRLusmqVGTCkAekZ JwZ72gUpt2kKxso0lEI4q1lnZm8LIek= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b=dWFD09ri; dkim=pass header.d=linutronix.de header.s=2020e header.b=iYbpEPZM; spf=pass (imf13.hostedemail.com: domain of t-8ch@linutronix.de designates 193.142.43.55 as permitted sender) smtp.mailfrom=t-8ch@linutronix.de; dmarc=pass (policy=none) header.from=linutronix.de From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1752742140; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QRrnBoH8IFFRjQzyw//nS3EiPMvLn5TdGq4dYEYIlJg=; b=dWFD09riE4O4+tZjIAy3cTk4M3sVWb5h8/ILsyaeqeEdqCqkgho5ZFfoKgaxrWjZ+asFn+ ByYwC5aZ9i5Q/ZhdRB9tKzakPfrtG6P9wjdX3b9kLtZRPN1sBvmqeSSSoo4kBYqi+ek43s TIBuzJAR0k6euvl3OQVP4TFknbVpXjXiRCSX+uxATx7CQNAThIPaHO0V+Qht46YUdybipO /v4Fjb4Vpe2PambZaOYX/jIWSfdUP4x+5HjzEONzlfJTw16k7yRm4WkXYDaYCbNeScHpKL lJCW3gOQEgjFFRWsf3Ffu7gVojL1IqRL8YC3RbASjBfB70Tc46TnYCaJAC27vQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1752742140; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QRrnBoH8IFFRjQzyw//nS3EiPMvLn5TdGq4dYEYIlJg=; b=iYbpEPZMe/vqrYdB5bRCpT8IjAvhzc4BJ9D9ik//CupsJl2UzOYHlpo8v+/8U00zmS8llO sYN2CUYEXI+HBtBw== Date: Thu, 17 Jul 2025 10:48:16 +0200 Subject: [PATCH v5 14/15] kunit: uapi: Introduce preinit executable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20250717-kunit-kselftests-v5-14-442b711cde2e@linutronix.de> References: <20250717-kunit-kselftests-v5-0-442b711cde2e@linutronix.de> In-Reply-To: <20250717-kunit-kselftests-v5-0-442b711cde2e@linutronix.de> To: Masahiro Yamada , Nathan Chancellor , Andrew Morton , Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Brendan Higgins , David Gow , Rae Moar , Shuah Khan , Jonathan Corbet , Nicolas Schier , Kees Cook , Alexander Viro , Christian Brauner , Jan Kara , Christoph Hellwig , Luis Chamberlain Cc: Christophe Leroy , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-doc@vger.kernel.org, workflows@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Nicolas Schier X-Developer-Signature: v=1; a=ed25519-sha256; t=1752742130; l=4791; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=rsRst4GAUSvhO7BiyO660CynxR9JGWccwNT8lkt95Wk=; b=7dAV+lyPvW4Xt9wGzS6ae0Dt5BYPoxtZ/NSDbgwLz5AEC5rxIHcjV1gwBzTHFSJP+u1XG6RWh DRZ0Mhl86nyDlbvkt2qSCmxOUhu50WR2b7u/wwforIfclfyjskegjFf X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= X-Rspamd-Queue-Id: C10F020005 X-Stat-Signature: k85ocgug6uyxofmwy8s1iwxi18sftwyf X-Rspam-User: X-Rspamd-Server: rspam07 X-HE-Tag: 1752742141-484831 X-HE-Meta: U2FsdGVkX1+St/76hK3AC6tZv+sMQI18eFJH8loI9X+WG0V+GSnaOGzAPlqpIUWMtwCyjmfzH64ctX0gN7Cb0Mj/V7mRdRJdKQkZke9O3oScbF0eYUhJbCPaVPb9NtzPj14pweAi28oYZ2rXkIBKDBMAivdWe3bV7NcamjuugPhP7+cV9xmE39imocdO8MDCmIQUpHUoOXzVmzxb+zCKhrxyM1+PdrRpV5IcFKiCfg3//YUECIVzfpGW7HSFqnspwYePjOFcRCIBstqf3A2tX/YHZFQcyKDYBGz/dSPy9opo8cORjGYLfb1H1OngrfTMe3gAh7uOsKgh129ZdgCsIezIFiXqFz8nytdmPboVLw2AoENvfsmw0mFtVQBdCPrWqvs+4bNn5/8yCcLk9JXReAfhCulfI0ASJEkx6Qq7qlQwBHkBHiqJjVTAxmfRSWnW1qhNTbfoNHu7I/wWudbaJpKzJSiDqPIcljmWIiirWgbDiM7L2bK1dieShATI5UiN8YNJZDH08sOQhJ71dcWM/z5g+BSmliMu+8OPUI8QCD69lkz291ewYtxnGtT26us8SToTMDKWgWX/ftLKBdRg7CtznJeRm1BnGAuR/ONfsD+DsQV07ZnF+vUVEIn1wR+ue5XGRkE7dKuN5glC+Hop49KelX6fA9pe81xImsxgS2quU/pU5C/xxLsTR83EQlFJZs0k68Y7t/79OSUfkTFd9rbZRMHI0hbQMq3UcgYBXD3OOpEng+ttKmwOBgqoXSlNgWYnuLwfVb9aOpzhVUlUszFmCAfM0FO/5tDsQ9vPu4KBYwl1GDG7In5q/gpfLX4sH3cZuIXV2eNZCGN63dH2pVTLi2vV27YuGKPYXCEPNJF9y+lFuNHTtIJyg7jWEsQfnuLMChFVnJkDCe5s5OUv/611b/pP/2FTYZ+3FY52m4cdLQHaMvjbtMoeL+qcggnfunvsbm5LVX556dfjlKo XkwqKjol ftrGdHb+76gHdNss231AW1SuZU3iNq2iyC2/7F+WgorbtToeSZ6X6FzhTODVWbaUcUCw1vnrjkhc3VS1Md3ael33QKtcDVc3NoLWrJcwRdl8TPnBfAbNyErOgy2v+XmiwKzbF X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: UAPI selftests may expect a "normal" userspace environment. For example the normal kernel API pseudo-filesystems should be mounted. This could be done from kernel code but it is non-idiomatic. Add a preinit userspace executable which performs these setup steps before running the final test executable. This preinit executable is only ever run from the kernel. Give it access to autoconf.h and kconfig.h to adapt itself to the tested kernel. Signed-off-by: Thomas Weißschuh Reviewed-by: David Gow --- MAINTAINERS | 1 + lib/kunit/Makefile | 7 ++++++ lib/kunit/kunit-uapi.c | 9 +++++-- lib/kunit/uapi-preinit.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-) diff --git a/MAINTAINERS b/MAINTAINERS index b1405f0a0e638d1654d9dc9e51d784ddc838cf5b..e81dfa180ab374ef91c7a45e546e6e9a8f454fa7 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13546,6 +13546,7 @@ S: Maintained F: include/kunit/uapi.h F: lib/kunit/kunit-example-uapi.c F: lib/kunit/kunit-uapi.c +F: lib/kunit/uapi-preinit.c KVM PARAVIRT (KVM/paravirt) M: Paolo Bonzini diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 2434470e998525e5667ebc207d11659835b5e888..e52a31b0852caef5669f11f6059c29ce5911aa43 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -14,8 +14,15 @@ kunit-objs += test.o \ device.o \ platform.o +userprogs += uapi-preinit +uapi-preinit-userccflags += -static $(NOLIBC_USERCFLAGS) \ + -include include/generated/autoconf.h \ + -include $(srctree)/tools/include/linux/kconfig.h obj-$(CONFIG_KUNIT_UAPI) += kunit-uapi.o +CFLAGS_kunit-uapi.o := -Wa,-I$(obj) +$(obj)/kunit-uapi.o: $(obj)/uapi-preinit + ifeq ($(CONFIG_KUNIT_DEBUGFS),y) kunit-objs += debugfs.o endif diff --git a/lib/kunit/kunit-uapi.c b/lib/kunit/kunit-uapi.c index 24356f68eca0df07032fb8a9e92daffde01b21d3..382a2bfee3c78a6e042a56a9ccdefcd55b5265a1 100644 --- a/lib/kunit/kunit-uapi.c +++ b/lib/kunit/kunit-uapi.c @@ -25,6 +25,8 @@ #define KSFT_XPASS 3 #define KSFT_SKIP 4 +KUNIT_UAPI_EMBED_BLOB(kunit_uapi_preinit, "uapi-preinit"); + static struct vfsmount *kunit_uapi_mount_ramfs(void) { struct file_system_type *type; @@ -146,7 +148,7 @@ static int kunit_uapi_user_mode_thread_init(void *data) kernel_sigaction(SIGABRT, SIG_DFL); complete(&ctx->setup_done); - ctx->exec_err = kernel_execve(ctx->executable, argv, NULL); + ctx->exec_err = kernel_execve(kbasename(kunit_uapi_preinit.path), argv, NULL); if (!ctx->exec_err) return 0; do_exit(0); @@ -255,7 +257,10 @@ static int kunit_uapi_run_executable(struct kunit *test, if (IS_ERR(mnt)) return PTR_ERR(mnt); - err = kunit_uapi_write_executable(mnt, executable); + err = kunit_uapi_write_executable(mnt, &kunit_uapi_preinit); + + if (!err) + err = kunit_uapi_write_executable(mnt, executable); if (!err) err = kunit_uapi_run_executable_in_mount(test, exe_name, mnt); diff --git a/lib/kunit/uapi-preinit.c b/lib/kunit/uapi-preinit.c new file mode 100644 index 0000000000000000000000000000000000000000..81182039965a8c93aebb2d5d76f4113bfef277a6 --- /dev/null +++ b/lib/kunit/uapi-preinit.c @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit Userspace environment setup. + * + * Copyright (C) 2025, Linutronix GmbH. + * Author: Thomas Weißschuh + * + * This is *userspace* code. + */ + +#include +#include + +#include "../../tools/testing/selftests/kselftest.h" + +static int setup_api_mount(const char *target, const char *fstype) +{ + int ret; + + ret = mkdir(target, 0755); + if (ret && errno != EEXIST) + return -errno; + + ret = mount("none", target, fstype, 0, NULL); + if (ret && errno != EBUSY) + return -errno; + + return 0; +} + +static void exit_failure(const char *stage, int err) +{ + /* If preinit fails synthesize a failed test report. */ + ksft_print_header(); + ksft_set_plan(1); + ksft_test_result_fail("Failed during test setup: %s: %s\n", stage, strerror(-err)); + ksft_finished(); +} + +int main(int argc, char **argv, char **envp) +{ + int ret; + + ret = setup_api_mount("/proc", "proc"); + if (ret) + exit_failure("mount /proc", ret); + + ret = setup_api_mount("/sys", "sysfs"); + if (ret) + exit_failure("mount /sys", ret); + + if (IS_ENABLED(CONFIG_DEVTMPFS)) { + ret = setup_api_mount("/dev", "devtmpfs"); + if (ret) + exit_failure("mount /dev", ret); + } + + ret = execve(argv[0], argv, envp); + if (ret) + exit_failure("execve", ret); + + return 0; +} -- 2.50.0