Skip to contentSkip to conversation

KVM: selftests: Use $(SRCARCH) instead of $(ARCH)

PATCH In review David Matlack 2 participants 4 messages Apr 30, 2025 – May 7, 2025

Subsystems (5)
KVM Kbuild Kselftest RISC-V +1 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: selftests: Use $(SRCARCH) instead of $(ARCH)

Use $(SRCARCH) in Makefile.kvm instead of $(ARCH). The former may have been set on the command line and thus make will ignore the variable assignment to convert x86_64 to x86.

Introduce $(SRCARCH) rather than just reverting commit 9af04539d474 ("KVM: selftests: Override ARCH for x86_64 instead of using ARCH_DIR") to keep KVM selftests consistent with the top-level kernel Makefile, which uses $(SRCARCH) for the exact same purpose.

While here, drop the comment about the top-level selftests allowing ARCH=x86_64. The kernel itself allows/expects ARCH=x86_64 so it's reasonable to expect the KVM selftests to handle it as well.

Fixes 9af04539d474 ("KVM: selftests: Override ARCH for x86_64 instead of using ARCH_DIR")
---
 tools/testing/selftests/kvm/Makefile     | 10 +++++----
 tools/testing/selftests/kvm/Makefile.kvm | 26 ++++++++++++------------
 2 files changed, 19 insertions(+), 17 deletions(-)
tools/testing/selftests/kvm/Makefile +6 -4
index 20af35a91d6f..02bf061c51f8 100644
@@ -1,13 +1,15 @@
 # SPDX-License-Identifier: GPL-2.0-only
 top_srcdir = ../../../..
+
 include $(top_srcdir)/scripts/subarch.include
-ARCH            ?= $(SUBARCH)
+ARCH 	?= $(SUBARCH)
+SRCARCH := $(ARCH)
 
-ifeq ($(ARCH),$(filter $(ARCH),arm64 s390 riscv x86 x86_64))
-# Top-level selftests allows ARCH=x86_64 :-(
 ifeq ($(ARCH),x86_64)
-	ARCH := x86
+        SRCARCH := x86
 endif
+
+ifeq ($(SRCARCH),$(filter $(SRCARCH),arm64 s390 riscv x86))
 include Makefile.kvm
 else
 # Empty targets for unsupported architectures
tools/testing/selftests/kvm/Makefile.kvm +13 -13
index f62b0a5aba35..065029b35355 100644
@@ -193,10 +193,10 @@ TEST_GEN_PROGS_riscv += steal_time
 SPLIT_TESTS += arch_timer
 SPLIT_TESTS += get-reg-list
 
-TEST_PROGS += $(TEST_PROGS_$(ARCH))
-TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(ARCH))
-TEST_GEN_PROGS_EXTENDED += $(TEST_GEN_PROGS_EXTENDED_$(ARCH))
-LIBKVM += $(LIBKVM_$(ARCH))
+TEST_PROGS += $(TEST_PROGS_$(SRCARCH))
+TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(SRCARCH))
+TEST_GEN_PROGS_EXTENDED += $(TEST_GEN_PROGS_EXTENDED_$(SRCARCH))
+LIBKVM += $(LIBKVM_$(SRCARCH))
 
 OVERRIDE_TARGETS = 1
 
@@ -208,24 +208,24 @@ include ../lib.mk
 INSTALL_HDR_PATH = $(top_srcdir)/usr
 LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/
 LINUX_TOOL_INCLUDE = $(top_srcdir)/tools/include
-LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include
+LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(SRCARCH)/include
 CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
 	-Wno-gnu-variable-sized-type-not-at-end -MD -MP -DCONFIG_64BIT \
 	-fno-builtin-memcmp -fno-builtin-memcpy \
 	-fno-builtin-memset -fno-builtin-strnlen \
 	-fno-stack-protector -fno-PIE -fno-strict-aliasing \
 	-I$(LINUX_TOOL_INCLUDE) -I$(LINUX_TOOL_ARCH_INCLUDE) \
-	-I$(LINUX_HDR_PATH) -Iinclude -I$(<D) -Iinclude/$(ARCH) \
+	-I$(LINUX_HDR_PATH) -Iinclude -I$(<D) -Iinclude/$(SRCARCH) \
 	-I ../rseq -I.. $(EXTRA_CFLAGS) $(KHDR_INCLUDES)
-ifeq ($(ARCH),s390)
+ifeq ($(SRCARCH),s390)
 	CFLAGS += -march=z10
 endif
-ifeq ($(ARCH),x86)
+ifeq ($(SRCARCH),x86)
 ifeq ($(shell echo "void foo(void) { }" | $(CC) -march=x86-64-v2 -x c - -c -o /dev/null 2>/dev/null; echo "$$?"),0)
 	CFLAGS += -march=x86-64-v2
 endif
 endif
-ifeq ($(ARCH),arm64)
+ifeq ($(SRCARCH),arm64)
 tools_dir := $(top_srcdir)/tools
 arm64_tools_dir := $(tools_dir)/arch/arm64/tools/
 
@@ -259,7 +259,7 @@ LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S))
 LIBKVM_STRING_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING))
 LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ)
 SPLIT_TEST_GEN_PROGS := $(patsubst %, $(OUTPUT)/%, $(SPLIT_TESTS))
-SPLIT_TEST_GEN_OBJ := $(patsubst %, $(OUTPUT)/$(ARCH)/%.o, $(SPLIT_TESTS))
+SPLIT_TEST_GEN_OBJ := $(patsubst %, $(OUTPUT)/$(SRCARCH)/%.o, $(SPLIT_TESTS))
 
 TEST_GEN_OBJ = $(patsubst %, %.o, $(TEST_GEN_PROGS))
 TEST_GEN_OBJ += $(patsubst %, %.o, $(TEST_GEN_PROGS_EXTENDED))
@@ -268,7 +268,7 @@ TEST_DEP_FILES += $(patsubst %.o, %.d, $(LIBKVM_OBJS))
 TEST_DEP_FILES += $(patsubst %.o, %.d, $(SPLIT_TEST_GEN_OBJ))
 -include $(TEST_DEP_FILES)
 
-$(shell mkdir -p $(sort $(OUTPUT)/$(ARCH) $(dir $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ))))
+$(shell mkdir -p $(sort $(OUTPUT)/$(SRCARCH) $(dir $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ))))
 
 $(filter-out $(SPLIT_TEST_GEN_PROGS), $(TEST_GEN_PROGS)) \
 $(TEST_GEN_PROGS_EXTENDED): %: %.o
@@ -276,9 +276,9 @@ $(TEST_GEN_PROGS_EXTENDED): %: %.o
 $(TEST_GEN_OBJ): $(OUTPUT)/%.o: %.c
 	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
 
-$(SPLIT_TEST_GEN_PROGS): $(OUTPUT)/%: $(OUTPUT)/%.o $(OUTPUT)/$(ARCH)/%.o
+$(SPLIT_TEST_GEN_PROGS): $(OUTPUT)/%: $(OUTPUT)/%.o $(OUTPUT)/$(SRCARCH)/%.o
 	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@
-$(SPLIT_TEST_GEN_OBJ): $(OUTPUT)/$(ARCH)/%.o: $(ARCH)/%.c
+$(SPLIT_TEST_GEN_OBJ): $(OUTPUT)/$(SRCARCH)/%.o: $(SRCARCH)/%.c
 	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
 
 EXTRA_CLEAN += $(GEN_HDRS) \

base-commit: 45eb29140e68ffe8e93a5471006858a018480a45

Signature · 1 line
2.49.0.906.g1f30a19c02-goog
#1 lore raw
Quoted 4 lines · David Matlack

Use $(SRCARCH) in Makefile.kvm instead of $(ARCH). The former may have been set on the command line and thus make will ignore the variable assignment to convert x86_64 to x86.

Blegh I meant to say "The latter ...".

#2 lore raw
In reply to David Matlack
Quoted 9 lines · David Matlack

Use $(SRCARCH) in Makefile.kvm instead of $(ARCH). The former may have been set on the command line and thus make will ignore the variable assignment to convert x86_64 to x86.

Introduce $(SRCARCH) rather than just reverting commit 9af04539d474 ("KVM: selftests: Override ARCH for x86_64 instead of using ARCH_DIR") to keep KVM selftests consistent with the top-level kernel Makefile, which uses $(SRCARCH) for the exact same purpose.

Quoted 6 lines

While here, drop the comment about the top-level selftests allowing ARCH=x86_64. The kernel itself allows/expects ARCH=x86_64 so it's reasonable to expect the KVM selftests to handle it as well.

Fixes 9af04539d474 ("KVM: selftests: Override ARCH for x86_64 instead of using ARCH_DIR")

If this approach seems reasonable I can also send another patch to share the definitions of $(ARCH) and $(SRCARCH) with the top-level Makefile so that we don't need any custom Makefile code in KVM selftests for this.

e.g.

From: David Matlack <[email protected]>
Date: Thu, 1 May 2025 10:30:26 -0700
Subject: [PATCH v2] kbuild: Share $(ARCH) and $(SRCARCH) with tools/

Pull out the definitions for $(ARCH), $(SRCARCH), and $(SUBARCH) into a scripts/arch.include and use it to reduce duplication in Makefiles under tools/.

---
 Makefile                                  | 28 +++--------------------
 scripts/{subarch.include => arch.include} | 24 +++++++++++++++++++
 tools/include/nolibc/Makefile             |  5 +---
 tools/testing/selftests/Makefile          |  3 +--
 tools/testing/selftests/kvm/Makefile      |  8 +------
 tools/testing/selftests/nolibc/Makefile   |  5 +---
 6 files changed, 31 insertions(+), 42 deletions(-)
 rename scripts/{subarch.include => arch.include} (61%)
Makefile +3 -25
index c91afd55099e..7a10252ddbe1 100644
@@ -380,8 +380,6 @@ KERNELRELEASE = $(call read-file, $(objtree)/include/config/kernel.release)
 KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)
 export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION
 
-include $(srctree)/scripts/subarch.include
-
 # Cross compiling and selecting different set of gcc/bin-utils
 # ---------------------------------------------------------------------------
 #
@@ -400,32 +398,12 @@ include $(srctree)/scripts/subarch.include
 # Alternatively CROSS_COMPILE can be set in the environment.
 # Default value for CROSS_COMPILE is not to prefix executables
 # Note: Some architectures assign CROSS_COMPILE in their arch/*/Makefile
-ARCH		?= $(SUBARCH)
+
+# Import definitions for SUBARCH, ARCH, and SRCARCH.
+include $(srctree)/scripts/arch.include
 
 # Architecture as present in compile.h
 UTS_MACHINE 	:= $(ARCH)
-SRCARCH 	:= $(ARCH)
-
-# Additional ARCH settings for x86
-ifeq ($(ARCH),i386)
-        SRCARCH := x86
-endif
-ifeq ($(ARCH),x86_64)
-        SRCARCH := x86
-endif
-
-# Additional ARCH settings for sparc
-ifeq ($(ARCH),sparc32)
-       SRCARCH := sparc
-endif
-ifeq ($(ARCH),sparc64)
-       SRCARCH := sparc
-endif
-
-# Additional ARCH settings for parisc
-ifeq ($(ARCH),parisc64)
-       SRCARCH := parisc
-endif
 
 export cross_compiling :=
 ifneq ($(SRCARCH),$(SUBARCH))
scripts/arch.include renamed from scripts/subarch.include +24 -0
similarity index 61%
rename from scripts/subarch.include
rename to scripts/arch.include
index c4592d59d69b..545f731140df 100644
@@ -11,3 +11,27 @@ SUBARCH := $(shell uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/ \
 				  -e s/ppc.*/powerpc/ -e s/mips.*/mips/ \
 				  -e s/sh[234].*/sh/ -e s/aarch64.*/arm64/ \
 				  -e s/riscv.*/riscv/ -e s/loongarch.*/loongarch/)
+
+ARCH		?= $(SUBARCH)
+SRCARCH 	:= $(ARCH)
+
+# Additional ARCH settings for x86
+ifeq ($(ARCH),i386)
+        SRCARCH := x86
+endif
+ifeq ($(ARCH),x86_64)
+        SRCARCH := x86
+endif
+
+# Additional ARCH settings for sparc
+ifeq ($(ARCH),sparc32)
+       SRCARCH := sparc
+endif
+ifeq ($(ARCH),sparc64)
+       SRCARCH := sparc
+endif
+
+# Additional ARCH settings for parisc
+ifeq ($(ARCH),parisc64)
+       SRCARCH := parisc
+endif
tools/include/nolibc/Makefile +1 -4
index f9702877ac21..9a78fe77205f 100644
@@ -8,10 +8,7 @@ srctree := $(patsubst %/tools/include/,%,$(dir $(CURDIR)))
 endif
 
 # when run as make -C tools/ nolibc_<foo> the arch is not set
-ifeq ($(ARCH),)
-include $(srctree)/scripts/subarch.include
-ARCH = $(SUBARCH)
-endif
+include $(srctree)/scripts/arch.include
 
 # OUTPUT is only set when run from the main makefile, otherwise
 # it defaults to this nolibc directory.
tools/testing/selftests/Makefile +1 -2
index c77c8c8e3d9b..827ce9bf3e50 100644
@@ -190,8 +190,7 @@ else
 endif
 
 # Prepare for headers install
-include $(top_srcdir)/scripts/subarch.include
-ARCH           ?= $(SUBARCH)
+include $(top_srcdir)/scripts/arch.include
 export BUILD
 export KHDR_INCLUDES
 
tools/testing/selftests/kvm/Makefile +1 -7
index 02bf061c51f8..503530d671e9 100644
@@ -1,13 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 top_srcdir = ../../../..
 
-include $(top_srcdir)/scripts/subarch.include
-ARCH 	?= $(SUBARCH)
-SRCARCH := $(ARCH)
-
-ifeq ($(ARCH),x86_64)
-        SRCARCH := x86
-endif
+include $(top_srcdir)/scripts/arch.include
 
 ifeq ($(SRCARCH),$(filter $(SRCARCH),arm64 s390 riscv x86))
 include Makefile.kvm
tools/testing/selftests/nolibc/Makefile +1 -4
index 58bcbbd029bc..13555d418c78 100644
@@ -18,10 +18,7 @@ else
 objtree ?= $(srctree)
 endif
 
-ifeq ($(ARCH),)
-include $(srctree)/scripts/subarch.include
-ARCH = $(SUBARCH)
-endif
+include $(srctree)/scripts/arch.include
 
 cc-option = $(call __cc-option, $(CC),$(CLANG_CROSS_FLAGS),$(1),$(2))
 

base-commit: 45eb29140e68ffe8e93a5471006858a018480a45
prerequisite-patch-id: 3bae97c9e1093148763235f47a84fa040b512d04

Signature · 1 line
2.49.0.906.g1f30a19c02-goog
#3 lore raw
Quoted 23 lines · David Matlack
Quoted 9 lines · David Matlack

Use $(SRCARCH) in Makefile.kvm instead of $(ARCH). The former may have been set on the command line and thus make will ignore the variable assignment to convert x86_64 to x86.

Introduce $(SRCARCH) rather than just reverting commit 9af04539d474 ("KVM: selftests: Override ARCH for x86_64 instead of using ARCH_DIR") to keep KVM selftests consistent with the top-level kernel Makefile, which uses $(SRCARCH) for the exact same purpose.

Quoted 6 lines

While here, drop the comment about the top-level selftests allowing ARCH=x86_64. The kernel itself allows/expects ARCH=x86_64 so it's reasonable to expect the KVM selftests to handle it as well.

Fixes 9af04539d474 ("KVM: selftests: Override ARCH for x86_64 instead of using ARCH_DIR")

If this approach seems reasonable I can also send another patch to share the definitions of $(ARCH) and $(SRCARCH) with the top-level Makefile so that we don't need any custom Makefile code in KVM selftests for this.

e.g.

Quoted 7 lines

From: David Matlack <[email protected]>
Date: Thu, 1 May 2025 10:30:26 -0700
Subject: [PATCH v2] kbuild: Share $(ARCH) and $(SRCARCH) with tools/

Pull out the definitions for $(ARCH), $(SRCARCH), and $(SUBARCH) into a scripts/arch.include and use it to reduce duplication in Makefiles under tools/.

Yes, please. However, SRCARCH might be going away in the kernel's Makefile[*]. I recommend holding off on anything until that discussion is fully resolved.

[*] https://lore.kernel.org/all/[email protected]