Skip to contentSkip to conversation

selftests: target selection fixes for cross and LLVM builds

PATCH RESEND In review Sasha Levin 3 participants 9 messages Nov 5 – Nov 23

Subsystems (4)
lore
Share & tools
lore

Sasha Levin

author maintainer
#0 lore raw

cover selftests: target selection fixes for cross and LLVM builds

A few suites select their tests from the build host instead of the target, or depend on the host C library in ways that break cross builds:

- set-anon-vma-name-test fails instead of skipping on kernels built without CONFIG_ANON_VMA_NAME.

- kexec only recognizes the ppc64le spelling of PowerPC and decides about the x86-64 jump test from the build host.

- size links a custom _start against the static C library, which fails on ARM with clang and on LoongArch; it now uses nolibc.

The patches are independent of each other and apply on mm-nonmm-unstable.

Since the original posting:

- "selftests/prctl: build generated tests for the target architecture" is dropped. "selftests/prctl: select the tests by ARCH alone" in mm-nonmm-unstable replaces it.

- "selftests/thermal: allow cross builds of the Intel tests" is dropped, it went in through the thermal tree as ff4194e54f84.

- The size patch has Thomas's Reviewed-by.

Original posting:
https://lore.kernel.org/r/[email protected]

Sasha Levin (3):
selftests/prctl: skip the anonymous VMA naming test when unsupported selftests/kexec: select tests using the target compiler selftests/size: use nolibc startup and syscall wrappers

 tools/testing/selftests/kexec/Makefile        | 26 ++++++++-------
 .../selftests/prctl/set-anon-vma-name-test.c  |  3 ++
 tools/testing/selftests/size/Makefile         |  9 ++++-
 tools/testing/selftests/size/get_size.c       | 33 +++++--------------
 4 files changed, 34 insertions(+), 37 deletions(-)

base-commit: 72afe7762a29508ae89831540347c1be3b7ba621

Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH RESEND 1/3 selftests/prctl: skip the anonymous VMA naming test when unsupported

PR_SET_VMA_ANON_NAME is only implemented when CONFIG_ANON_VMA_NAME is enabled. Without it, set_anon_vma_name() returns -EINVAL for every request, so set-anon-vma-name-test reports a failure on kernels that simply do not provide the feature:

# renaming: Expected rename_vma(...) (-22) >= 0
not ok 1 vma.renaming

Probe the interface in the fixture setup by clearing the name of the anonymous mapping, which succeeds whenever the feature is available, and skip the test when the kernel rejects the request with EINVAL.

Fixes 5290586186d2 ("selftests: prctl: Add new prctl test for PR_SET_VMA action")
---
 tools/testing/selftests/prctl/set-anon-vma-name-test.c | 3 +++
 1 file changed, 3 insertions(+)
tools/testing/selftests/prctl/set-anon-vma-name-test.c +3 -0
index 5f9589534da8e..d3854ccb2a7a3 100644
@@ -93,6 +93,9 @@ FIXTURE_SETUP(vma) {
 					MAP_PRIVATE, self->fd_not_anon, 0);
 	ASSERT_NE(self->ptr_not_anon, MAP_FAILED);
 	close(self->fd_not_anon);
+
+	if (rename_vma((unsigned long)self->ptr_anon, AREA_SIZE, NULL) == -EINVAL)
+		SKIP(return, "PR_SET_VMA_ANON_NAME is not supported");
 }
 
 FIXTURE_TEARDOWN(vma) {
Signature · 1 line
2.53.0
author maintainer
#3 lore raw

PATCH RESEND 2/3 selftests/kexec: select tests using the target compiler

In reply to Sasha Levin

The kexec Makefile recognizes PowerPC only through the ppc64le spelling of ARCH, so the canonical ARCH=powerpc used for cross builds omits the tests. The x86 jump test is selected with IS_64_BIT from scripts/Makefile.arch, which describes the build host rather than the compiler being used. On other architectures the Makefile includes no lib.mk rules at all, so installation and runlist generation fail with missing targets.

Select x86 and PowerPC from the target ARCH when it is given and from the host's SUBARCH otherwise, without assigning ARCH, so that lib.mk keeps choosing the compiler's native clang target. Ask the selected compiler, including USERCFLAGS, whether it targets a 64-bit x86 ABI and whether it targets little-endian 64-bit PowerPC. lib.mk expands the generated program list before it appends USERCFLAGS to CFLAGS, so the flags are passed explicitly to keep both probes consistent. If the compiler cannot be run, select the jump test so that the build fails instead of silently dropping it. Include lib.mk unconditionally so that unsupported targets build, install and emit an empty runlist.

Fixes 65e38e32a959 ("selftests/kexec: Enable secureboot tests for PowerPC")
Fixes 7615b94b6371 ("selftests/kexec: Add x86_64 selftest for kexec-jump and exception handling")
---
 tools/testing/selftests/kexec/Makefile | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
tools/testing/selftests/kexec/Makefile +14 -12
index 874cfdd3b75b3..97d5dd10208a1 100644
@@ -1,20 +1,22 @@
 # SPDX-License-Identifier: GPL-2.0-only
 # Makefile for kexec tests
 
-ARCH ?= $(shell uname -m 2>/dev/null || echo not)
-ARCH_PROCESSED := $(shell echo $(ARCH) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+top_srcdir = ../../../..
+include $(top_srcdir)/scripts/subarch.include
+ARCH_PROCESSED := $(shell echo $(or $(ARCH),$(SUBARCH)) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
 
-ifeq ($(ARCH_PROCESSED),$(filter $(ARCH_PROCESSED),x86 ppc64le))
-TEST_PROGS := test_kexec_load.sh test_kexec_file_load.sh
-TEST_FILES := kexec_common_lib.sh
+PROBE_CC = $(filter-out -fuse-ld=%,$(CC)) $(CFLAGS) $(USERCFLAGS) -E -P -x c -
+CC_IS_64BIT = $(shell echo __SIZEOF_LONG__ | $(PROBE_CC) || echo 8)
+PPC64LE = $(shell printf '\043if defined(__powerpc64__) && __BYTE_ORDER__ == \
+	__ORDER_LITTLE_ENDIAN__\n1\n\043endif\n' | $(PROBE_CC))
 
-include ../../../scripts/Makefile.arch
-
-ifeq ($(IS_64_BIT)$(ARCH_PROCESSED),1x86)
-TEST_PROGS += test_kexec_jump.sh
-TEST_GEN_PROGS := test_kexec_jump
+ifeq ($(ARCH_PROCESSED),x86)
+TEST_PROGS = test_kexec_load.sh test_kexec_file_load.sh \
+	$(if $(filter 8,$(CC_IS_64BIT)),test_kexec_jump.sh)
+TEST_GEN_PROGS = $(if $(filter 8,$(CC_IS_64BIT)),test_kexec_jump)
+else ifneq (,$(filter powerpc ppc64le,$(ARCH_PROCESSED)))
+TEST_PROGS = $(if $(filter 1,$(PPC64LE)),test_kexec_load.sh test_kexec_file_load.sh)
 endif
+TEST_FILES = $(if $(TEST_PROGS),kexec_common_lib.sh)
 
 include ../lib.mk
-
-endif
Signature · 1 line
2.53.0
author maintainer
#6 lore raw

PATCH RESEND 3/3 selftests/size: use nolibc startup and syscall wrappers

In reply to Sasha Levin

get_size provides its own _start and is linked statically against the C library with -nostartfiles. That combination assumes startup and TLS details that vary between architectures and C library versions: errno lives in thread-local storage that is never set up, and the static link expects symbols from the skipped startup files. CI builds have failed with undefined _init/_fini on ARM with clang and an undefined _DYNAMIC on LoongArch.

Build the test with the in-tree nolibc instead. nolibc provides the startup code, so the test returns from main(), and it provides the write() and syscall() wrappers without the TLS errno problem. A failing sysinfo() is now reported as a TAP failure instead of crashing the test. Keep the static link and the existing TAP output.

Since the test no longer uses the system C library headers, the kernel UAPI headers have to be installed before it is built. Add the headers target as an order-only prerequisite, as nolibc-test does, so that a standalone build installs them. Link libgcc only when the selected compiler is GCC, which needs it for division helpers on ARM, and detect that from the compiler rather than from LLVM so that CC=clang builds do not try to link a libgcc that clang does not provide.

nolibc does not support arc, csky, microblaze, nios2 or xtensa, so the test can no longer be built for those architectures. This conversion also makes the separate i386 vDSO workaround proposed for get_size unnecessary.

Fixes 3ce51050fadd ("selftest: size: Add size test for Linux kernel")
Cc [email protected] # 6.18.x
---
 tools/testing/selftests/size/Makefile   |  9 ++++++-
 tools/testing/selftests/size/get_size.c | 33 +++++++------------------
 2 files changed, 17 insertions(+), 25 deletions(-)
tools/testing/selftests/size/Makefile +8 -1
index b87facc00a6e9..884b42d7fd0e2 100644
@@ -1,6 +1,13 @@
 # SPDX-License-Identifier: GPL-2.0-only
-CFLAGS := -static -ffreestanding -nostartfiles -s
+CFLAGS = -Os -static -nostdlib -nostdinc -ffreestanding -fno-stack-protector -s
+CFLAGS += -isystem $(top_srcdir)/tools/include/nolibc $(KHDR_INCLUDES)
 
 TEST_GEN_PROGS := get_size
 
 include ../lib.mk
+
+CC_IS_GCC := $(shell printf '\043if defined(__GNUC__) && !defined(__clang__)\nGCC\n\043endif\n' | \
+	$(CC) $(CFLAGS) -E -P -x c - 2>/dev/null)
+LDLIBS += $(if $(filter GCC,$(CC_IS_GCC)),-lgcc)
+
+$(OUTPUT)/get_size: | headers
tools/testing/selftests/size/get_size.c +9 -24
index 2980b1a63366b..ada6a271711a7 100644
@@ -11,26 +11,12 @@
  * own execution.  It also attempts to have as few dependencies
  * on kernel features as possible.
  *
- * It should be statically linked, with startup libs avoided.  It uses
- * no library calls except the syscall() function for the following 3
- * syscalls:
- *   sysinfo(), write(), and _exit()
- *
- * For output, it avoids printf (which in some C libraries
- * has large external dependencies) by  implementing it's own
- * number output and print routines, and using __builtin_strlen()
- *
- * The test may crash if any of the above syscalls fails because in some
- * libc implementations (e.g. the GNU C Library) errno is saved in
- * thread-local storage, which does not get initialized due to avoiding
- * startup libs.
+ * It is statically linked with nolibc startup and syscall wrappers.
+ * For output, it avoids printf by implementing its own print routines.
  */
 
-#include <sys/sysinfo.h>
-#include <unistd.h>
-#include <sys/syscall.h>
-
-#define STDOUT_FILENO 1
+#include <nolibc.h>
+#include <linux/sysinfo.h>
 
 static int print(const char *s)
 {
@@ -39,7 +25,7 @@ static int print(const char *s)
 	while (s[len] != '\0')
 		len++;
 
-	return syscall(SYS_write, STDOUT_FILENO, s, len);
+	return write(STDOUT_FILENO, s, len);
 }
 
 static inline char *num_to_str(unsigned long num, char *buf, int len)
@@ -80,8 +66,7 @@ static int print_k_value(const char *s, unsigned long num, unsigned long units)
 	return ccode;
 }
 
-/* this program has no main(), as startup libraries are not used */
-void _start(void)
+int main(void)
 {
 	int ccode;
 	struct sysinfo info;
@@ -91,12 +76,12 @@ void _start(void)
 	print("TAP version 13\n");
 	print("# Testing system size.\n");
 
-	ccode = syscall(SYS_sysinfo, &info);
+	ccode = syscall(__NR_sysinfo, &info);
 	if (ccode < 0) {
 		print("not ok 1");
 		print(test_name);
 		print(" ---\n reason: \"could not get sysinfo\"\n ...\n");
-		syscall(SYS_exit, ccode);
+		return 1;
 	}
 	print("ok 1");
 	print(test_name);
@@ -112,5 +97,5 @@ void _start(void)
 	print(" ...\n");
 	print("1..1\n");
 
-	syscall(SYS_exit, 0);
+	return 0;
 }
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH RESEND 2/3] selftests/kexec: select tests using the target compiler

In reply to Sasha Levin
Sashiko has reviewed this patch and found no issues.
Quoted 19 lines

The kexec Makefile recognizes PowerPC only through the ppc64le spelling of ARCH, so the canonical ARCH=powerpc used for cross builds omits the tests. The x86 jump test is selected with IS_64_BIT from scripts/Makefile.arch, which describes the build host rather than the compiler being used. On other architectures the Makefile includes no lib.mk rules at all, so installation and runlist generation fail with missing targets.

Select x86 and PowerPC from the target ARCH when it is given and from the host's SUBARCH otherwise, without assigning ARCH, so that lib.mk keeps choosing the compiler's native clang target. Ask the selected compiler, including USERCFLAGS, whether it targets a 64-bit x86 ABI and whether it targets little-endian 64-bit PowerPC. lib.mk expands the generated program list before it appends USERCFLAGS to CFLAGS, so the flags are passed explicitly to keep both probes consistent. If the compiler cannot be run, select the jump test so that the build fails instead of silently dropping it. Include lib.mk unconditionally so that unsupported targets build, install and emit an empty runlist.

Quoted 3 lines
Fixes 65e38e32a959 ("selftests/kexec: Enable secureboot tests for PowerPC")
Fixes 7615b94b6371 ("selftests/kexec: Add x86_64 selftest for kexec-jump and exception handling")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#8 lore raw
In reply to Sasha Levin

Hi Andrew, Shuah,

Ping on this series. All three patches still apply to current mainline and to next-20261120.

KernelCI still hits what 2/3 and 3/3 fix: the 32-bit arm clang builds fail size on the undefined _init and _fini, the loongarch builds fail it on _DYNAMIC, and the powerpc builds skip kexec.

Signature · 2 lines
Thanks,
Sasha
tree notice
#2 lore raw

+ selftests-prctl-skip-the-anonymous-vma-naming-test-when-unsupported.patch added to mm-nonmm-unstable branch

In reply to Sasha Levin
The patch titled Subject: selftests/prctl: skip the anonymous VMA naming test when unsupported has been added to the -mm mm-nonmm-unstable branch. Its filename is…

The patch titled
Subject: selftests/prctl: skip the anonymous VMA naming test when unsupported
has been added to the -mm mm-nonmm-unstable branch. Its filename is
selftests-prctl-skip-the-anonymous-vma-naming-test-when-unsupported.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/selftests-prctl-skip-the-anonymous-vma-naming-test-when-unsupported.patch

This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Sasha Levin <[email protected]>
Subject: selftests/prctl: skip the anonymous VMA naming test when unsupported
Date: Thu, 05 Nov 2026 07:56:59 -0500

Patch series "selftests: target selection fixes for cross and LLVM builds".

A few suites select their tests from the build host instead of the target, or depend on the host C library in ways that break cross builds:

- set-anon-vma-name-test fails instead of skipping on kernels built without CONFIG_ANON_VMA_NAME.

- kexec only recognizes the ppc64le spelling of PowerPC and decides about the x86-64 jump test from the build host.

- size links a custom _start against the static C library, which fails on ARM with clang and on LoongArch; it now uses nolibc.

The patches are independent of each other and apply on mm-nonmm-unstable.

This patch (of 3):

PR_SET_VMA_ANON_NAME is only implemented when CONFIG_ANON_VMA_NAME is enabled. Without it, set_anon_vma_name() returns -EINVAL for every request, so set-anon-vma-name-test reports a failure on kernels that simply do not provide the feature:

# renaming: Expected rename_vma(...) (-22) >= 0
not ok 1 vma.renaming

Probe the interface in the fixture setup by clearing the name of the anonymous mapping, which succeeds whenever the feature is available, and skip the test when the kernel rejects the request with EINVAL.

Fixes 5290586186d2 ("selftests: prctl: Add new prctl test for PR_SET_VMA action")
Cc Shuah Khan <[email protected]>
Cc Ivan Orlov <[email protected]>
Cc Thomas Weißschuh <[email protected]>
 tools/testing/selftests/prctl/set-anon-vma-name-test.c |    3 +++
 1 file changed, 3 insertions(+)
tools/testing/selftests/prctl/set-anon-vma-name-test.c renamed from tools/testing/selftests/prctl/set-anon-vma-name-test.c~selftests-prctl-skip-the-anonymous-vma-naming-test-when-unsupported +3 -0
@@ -93,6 +93,9 @@ FIXTURE_SETUP(vma) {
 					MAP_PRIVATE, self->fd_not_anon, 0);
 	ASSERT_NE(self->ptr_not_anon, MAP_FAILED);
 	close(self->fd_not_anon);
+
+	if (rename_vma((unsigned long)self->ptr_anon, AREA_SIZE, NULL) == -EINVAL)
+		SKIP(return, "PR_SET_VMA_ANON_NAME is not supported");
 }
 
 FIXTURE_TEARDOWN(vma) {

_

Patches currently in -mm which might be from [email protected] are

selftests-proc-cast-the-procmap_query-address-through-uintptr_t.patch selftests-mm-fix-pointer-width-warnings-on-32-bit-architectures.patch selftests-mm-uffd-wp-mremap-keep-the-thp-sizes-in-unsigned-long.patch kselftest-alloc_tag-use-the-ll64-types-on-powerpc64.patch selftests-proc-search-the-kernel-headers-before-the-tools-copies.patch selftests-prctl-select-the-tests-by-arch-alone.patch selftests-prctl-skip-the-anonymous-vma-naming-test-when-unsupported.patch selftests-kexec-select-tests-using-the-target-compiler.patch selftests-size-use-nolibc-startup-and-syscall-wrappers.patch

tree notice
#5 lore raw

+ selftests-kexec-select-tests-using-the-target-compiler.patch added to mm-nonmm-unstable branch

In reply to Sasha Levin
The patch titled Subject: selftests/kexec: select tests using the target compiler has been added to the -mm mm-nonmm-unstable branch. Its filename is…

The patch titled
Subject: selftests/kexec: select tests using the target compiler
has been added to the -mm mm-nonmm-unstable branch. Its filename is
selftests-kexec-select-tests-using-the-target-compiler.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/selftests-kexec-select-tests-using-the-target-compiler.patch

This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Sasha Levin <[email protected]>
Subject: selftests/kexec: select tests using the target compiler
Date: Thu, 05 Nov 2026 07:57:00 -0500

The kexec Makefile recognizes PowerPC only through the ppc64le spelling of ARCH, so the canonical ARCH=powerpc used for cross builds omits the tests. The x86 jump test is selected with IS_64_BIT from scripts/Makefile.arch, which describes the build host rather than the compiler being used. On other architectures the Makefile includes no lib.mk rules at all, so installation and runlist generation fail with missing targets.

Select x86 and PowerPC from the target ARCH when it is given and from the host's SUBARCH otherwise, without assigning ARCH, so that lib.mk keeps choosing the compiler's native clang target. Ask the selected compiler, including USERCFLAGS, whether it targets a 64-bit x86 ABI and whether it targets little-endian 64-bit PowerPC. lib.mk expands the generated program list before it appends USERCFLAGS to CFLAGS, so the flags are passed explicitly to keep both probes consistent. If the compiler cannot be run, select the jump test so that the build fails instead of silently dropping it. Include lib.mk unconditionally so that unsupported targets build, install and emit an empty runlist.

Fixes 65e38e32a959 ("selftests/kexec: Enable secureboot tests for PowerPC")
Fixes 7615b94b6371 ("selftests/kexec: Add x86_64 selftest for kexec-jump and exception handling")
Cc Shuah Khan <[email protected]>
Cc David Woodhouse <[email protected]>
Cc R Nageswara Sastry <[email protected]>
Cc Thomas Weißschuh <[email protected]>
 tools/testing/selftests/kexec/Makefile |   30 ++++++++++++-----------
 1 file changed, 16 insertions(+), 14 deletions(-)
tools/testing/selftests/kexec/Makefile renamed from tools/testing/selftests/kexec/Makefile~selftests-kexec-select-tests-using-the-target-compiler +16 -14
@@ -1,20 +1,22 @@
 # SPDX-License-Identifier: GPL-2.0-only
 # Makefile for kexec tests
 
-ARCH ?= $(shell uname -m 2>/dev/null || echo not)
-ARCH_PROCESSED := $(shell echo $(ARCH) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
-
-ifeq ($(ARCH_PROCESSED),$(filter $(ARCH_PROCESSED),x86 ppc64le))
-TEST_PROGS := test_kexec_load.sh test_kexec_file_load.sh
-TEST_FILES := kexec_common_lib.sh
-
-include ../../../scripts/Makefile.arch
-
-ifeq ($(IS_64_BIT)$(ARCH_PROCESSED),1x86)
-TEST_PROGS += test_kexec_jump.sh
-TEST_GEN_PROGS := test_kexec_jump
+top_srcdir = ../../../..
+include $(top_srcdir)/scripts/subarch.include
+ARCH_PROCESSED := $(shell echo $(or $(ARCH),$(SUBARCH)) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+
+PROBE_CC = $(filter-out -fuse-ld=%,$(CC)) $(CFLAGS) $(USERCFLAGS) -E -P -x c -
+CC_IS_64BIT = $(shell echo __SIZEOF_LONG__ | $(PROBE_CC) || echo 8)
+PPC64LE = $(shell printf '\043if defined(__powerpc64__) && __BYTE_ORDER__ == \
+	__ORDER_LITTLE_ENDIAN__\n1\n\043endif\n' | $(PROBE_CC))
+
+ifeq ($(ARCH_PROCESSED),x86)
+TEST_PROGS = test_kexec_load.sh test_kexec_file_load.sh \
+	$(if $(filter 8,$(CC_IS_64BIT)),test_kexec_jump.sh)
+TEST_GEN_PROGS = $(if $(filter 8,$(CC_IS_64BIT)),test_kexec_jump)
+else ifneq (,$(filter powerpc ppc64le,$(ARCH_PROCESSED)))
+TEST_PROGS = $(if $(filter 1,$(PPC64LE)),test_kexec_load.sh test_kexec_file_load.sh)
 endif
+TEST_FILES = $(if $(TEST_PROGS),kexec_common_lib.sh)
 
 include ../lib.mk
-
-endif

_

Patches currently in -mm which might be from [email protected] are

selftests-proc-cast-the-procmap_query-address-through-uintptr_t.patch selftests-mm-fix-pointer-width-warnings-on-32-bit-architectures.patch selftests-mm-uffd-wp-mremap-keep-the-thp-sizes-in-unsigned-long.patch kselftest-alloc_tag-use-the-ll64-types-on-powerpc64.patch selftests-proc-search-the-kernel-headers-before-the-tools-copies.patch selftests-prctl-select-the-tests-by-arch-alone.patch selftests-prctl-skip-the-anonymous-vma-naming-test-when-unsupported.patch selftests-kexec-select-tests-using-the-target-compiler.patch selftests-size-use-nolibc-startup-and-syscall-wrappers.patch

tree notice
#7 lore raw

+ selftests-size-use-nolibc-startup-and-syscall-wrappers.patch added to mm-nonmm-unstable branch

In reply to Sasha Levin
The patch titled Subject: selftests/size: use nolibc startup and syscall wrappers has been added to the -mm mm-nonmm-unstable branch. Its filename is…

The patch titled
Subject: selftests/size: use nolibc startup and syscall wrappers
has been added to the -mm mm-nonmm-unstable branch. Its filename is
selftests-size-use-nolibc-startup-and-syscall-wrappers.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/selftests-size-use-nolibc-startup-and-syscall-wrappers.patch

This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Sasha Levin <[email protected]>
Subject: selftests/size: use nolibc startup and syscall wrappers
Date: Thu, 05 Nov 2026 07:57:01 -0500

get_size provides its own _start and is linked statically against the C library with -nostartfiles. That combination assumes startup and TLS details that vary between architectures and C library versions: errno lives in thread-local storage that is never set up, and the static link expects symbols from the skipped startup files. CI builds have failed with undefined _init/_fini on ARM with clang and an undefined _DYNAMIC on LoongArch.

Build the test with the in-tree nolibc instead. nolibc provides the startup code, so the test returns from main(), and it provides the write() and syscall() wrappers without the TLS errno problem. A failing sysinfo() is now reported as a TAP failure instead of crashing the test. Keep the static link and the existing TAP output.

Since the test no longer uses the system C library headers, the kernel UAPI headers have to be installed before it is built. Add the headers target as an order-only prerequisite, as nolibc-test does, so that a standalone build installs them. Link libgcc only when the selected compiler is GCC, which needs it for division helpers on ARM, and detect that from the compiler rather than from LLVM so that CC=clang builds do not try to link a libgcc that clang does not provide.

nolibc does not support arc, csky, microblaze, nios2 or xtensa, so the test can no longer be built for those architectures. This conversion also makes the separate i386 vDSO workaround proposed for get_size unnecessary.

 tools/testing/selftests/size/Makefile   |    9 +++++-
 tools/testing/selftests/size/get_size.c |   33 ++++++----------------
 2 files changed, 17 insertions(+), 25 deletions(-)
tools/testing/selftests/size/get_size.c renamed from tools/testing/selftests/size/get_size.c~selftests-size-use-nolibc-startup-and-syscall-wrappers +9 -24
@@ -11,26 +11,12 @@
  * own execution.  It also attempts to have as few dependencies
  * on kernel features as possible.
  *
- * It should be statically linked, with startup libs avoided.  It uses
- * no library calls except the syscall() function for the following 3
- * syscalls:
- *   sysinfo(), write(), and _exit()
- *
- * For output, it avoids printf (which in some C libraries
- * has large external dependencies) by  implementing it's own
- * number output and print routines, and using __builtin_strlen()
- *
- * The test may crash if any of the above syscalls fails because in some
- * libc implementations (e.g. the GNU C Library) errno is saved in
- * thread-local storage, which does not get initialized due to avoiding
- * startup libs.
+ * It is statically linked with nolibc startup and syscall wrappers.
+ * For output, it avoids printf by implementing its own print routines.
  */
 
-#include <sys/sysinfo.h>
-#include <unistd.h>
-#include <sys/syscall.h>
-
-#define STDOUT_FILENO 1
+#include <nolibc.h>
+#include <linux/sysinfo.h>
 
 static int print(const char *s)
 {
@@ -39,7 +25,7 @@ static int print(const char *s)
 	while (s[len] != '\0')
 		len++;
 
-	return syscall(SYS_write, STDOUT_FILENO, s, len);
+	return write(STDOUT_FILENO, s, len);
 }
 
 static inline char *num_to_str(unsigned long num, char *buf, int len)
@@ -80,8 +66,7 @@ static int print_k_value(const char *s,
 	return ccode;
 }
 
-/* this program has no main(), as startup libraries are not used */
-void _start(void)
+int main(void)
 {
 	int ccode;
 	struct sysinfo info;
@@ -91,12 +76,12 @@ void _start(void)
 	print("TAP version 13\n");
 	print("# Testing system size.\n");
 
-	ccode = syscall(SYS_sysinfo, &info);
+	ccode = syscall(__NR_sysinfo, &info);
 	if (ccode < 0) {
 		print("not ok 1");
 		print(test_name);
 		print(" ---\n reason: \"could not get sysinfo\"\n ...\n");
-		syscall(SYS_exit, ccode);
+		return 1;
 	}
 	print("ok 1");
 	print(test_name);
@@ -112,5 +97,5 @@ void _start(void)
 	print(" ...\n");
 	print("1..1\n");
 
-	syscall(SYS_exit, 0);
+	return 0;
 }
tools/testing/selftests/size/Makefile renamed from tools/testing/selftests/size/Makefile~selftests-size-use-nolibc-startup-and-syscall-wrappers +8 -1
@@ -1,6 +1,13 @@
 # SPDX-License-Identifier: GPL-2.0-only
-CFLAGS := -static -ffreestanding -nostartfiles -s
+CFLAGS = -Os -static -nostdlib -nostdinc -ffreestanding -fno-stack-protector -s
+CFLAGS += -isystem $(top_srcdir)/tools/include/nolibc $(KHDR_INCLUDES)
 
 TEST_GEN_PROGS := get_size
 
 include ../lib.mk
+
+CC_IS_GCC := $(shell printf '\043if defined(__GNUC__) && !defined(__clang__)\nGCC\n\043endif\n' | \
+	$(CC) $(CFLAGS) -E -P -x c - 2>/dev/null)
+LDLIBS += $(if $(filter GCC,$(CC_IS_GCC)),-lgcc)
+
+$(OUTPUT)/get_size: | headers

_

Patches currently in -mm which might be from [email protected] are

selftests-proc-cast-the-procmap_query-address-through-uintptr_t.patch selftests-mm-fix-pointer-width-warnings-on-32-bit-architectures.patch selftests-mm-uffd-wp-mremap-keep-the-thp-sizes-in-unsigned-long.patch kselftest-alloc_tag-use-the-ll64-types-on-powerpc64.patch selftests-proc-search-the-kernel-headers-before-the-tools-copies.patch selftests-prctl-select-the-tests-by-arch-alone.patch selftests-prctl-skip-the-anonymous-vma-naming-test-when-unsupported.patch selftests-kexec-select-tests-using-the-target-compiler.patch selftests-size-use-nolibc-startup-and-syscall-wrappers.patch