Skip to contentSkip to conversation

selftests: Fix separate output directory builds

PATCH In review Muhammad Usama Anjum 4 participants 15 messages Jan 19, 2022 – Jan 20, 2022

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

v2 cover selftests: Fix separate output directory builds

Build of several selftests fail if separate output directory is specified by the following methods:
1) make -C tools/testing/selftests O=<build_dir>
2) export KBUILD_OUTPUT="build_dir"; make -C tools/testing/selftests

Build fails because of several reasons:
1) The kernel headers aren't found.
2) The path of output objects is wrong and hence unaccessible.

These problems can be solved by:
1) Including the correct path of uapi header files
2) By setting the BUILD variable correctly inside Makefile

Following different build scenarios have been tested after making these changes to verify that nothing gets broken with these changes:
make -C tools/testing/selftests
make -C tools/testing/selftests/futex
make -C tools/testing/selftests/kvm
make -C tools/testing/selftests/landlock
make -C tools/testing/selftests/net
make -C tools/testing/selftests/net/mptcp
make -C tools/testing/selftests/vm
make -C tools/testing/selftests O=build
make -C tools/testing/selftests o=/opt/build
export KBUILD_OUTPUT="/opt/build"; make -C tools/testing/selftests export KBUILD_OUTPUT="build"; make -C tools/testing/selftests cd <any_dir>; make -C <src_path>/tools/testing/selftests
cd <any_dir>; make -C <src_path>/tools/testing/selftests O=build

---
Changes in V2:
Revert the excessive cleanup which was breaking the individual
test build.

Muhammad Usama Anjum (10):
selftests: set the BUILD variable to absolute path
selftests: Add and export a kernel uapi headers path
selftests: Correct the headers install path
selftests: futex: Add the uapi headers include variable
selftests: kvm: Add the uapi headers include variable
selftests: landlock: Add the uapi headers include variable
selftests: net: Add the uapi headers include variable
selftests: mptcp: Add the uapi headers include variable
selftests: vm: Add the uapi headers include variable
selftests: vm: remove dependecy from internal kernel macros

 tools/testing/selftests/Makefile              | 32 +++++++++++++------
 .../selftests/futex/functional/Makefile       |  5 ++-
 tools/testing/selftests/kvm/Makefile          |  2 +-
 tools/testing/selftests/landlock/Makefile     |  2 +-
 tools/testing/selftests/net/Makefile          |  2 +-
 tools/testing/selftests/net/mptcp/Makefile    |  2 +-
 tools/testing/selftests/vm/Makefile           |  2 +-
 tools/testing/selftests/vm/userfaultfd.c      |  3 ++
 8 files changed, 32 insertions(+), 18 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/10 selftests: set the BUILD variable to absolute path

The build of kselftests fails if relative path is specified through KBUILD_OUTPUT or O=<path> method. BUILD variable is used to determine the path of the output objects. When make is run from other directories with relative paths, the exact path of the build objects is ambiguous and build fails.

make[1]: Entering directory '/home/usama/repos/kernel/linux_mainline2/tools/testing/selftests/alsa'
gcc mixer-test.c -L/usr/lib/x86_64-linux-gnu -lasound -o build/kselftest/alsa/mixer-test
/usr/bin/ld: cannot open output file build/kselftest/alsa/mixer-test

Set the BUILD variable to the absolute path of the output directory. Make the logic readable and easy to follow. Use spaces instead of tabs for indentation as if with tab indentation is considered recipe in make.

---
 tools/testing/selftests/Makefile | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
tools/testing/selftests/Makefile +17 -9
index d08fe4cfe811..a7b63860b7bc 100644
@@ -114,19 +114,27 @@ ifdef building_out_of_srctree
 override LDFLAGS =
 endif
 
-ifneq ($(O),)
-	BUILD := $(O)/kselftest
+top_srcdir ?= ../../..
+
+ifeq ("$(origin O)", "command line")
+  KBUILD_OUTPUT := $(O)
+endif
+
+ifneq ($(KBUILD_OUTPUT),)
+  # Make's built-in functions such as $(abspath ...), $(realpath ...) cannot
+  # expand a shell special character '~'. We use a somewhat tedious way here.
+  abs_objtree := $(shell cd $(top_srcdir) && mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && pwd)
+  $(if $(abs_objtree),, \
+    $(error failed to create output directory "$(KBUILD_OUTPUT)"))
+  # $(realpath ...) resolves symlinks
+  abs_objtree := $(realpath $(abs_objtree))
+  BUILD := $(abs_objtree)/kselftest
 else
-	ifneq ($(KBUILD_OUTPUT),)
-		BUILD := $(KBUILD_OUTPUT)/kselftest
-	else
-		BUILD := $(shell pwd)
-		DEFAULT_INSTALL_HDR_PATH := 1
-	endif
+  BUILD := $(CURDIR)
+  DEFAULT_INSTALL_HDR_PATH := 1
 endif
 
 # Prepare for headers install
-top_srcdir ?= ../../..
 include $(top_srcdir)/scripts/subarch.include
 ARCH           ?= $(SUBARCH)
 export KSFT_KHDR_INSTALL_DONE := 1
Signature · 1 line
2.30.2
#2 lore raw

PATCH v2 2/10 selftests: Add and export a kernel uapi headers path

In reply to Muhammad Usama Anjum

Kernel uapi headers can be present at different paths depending upon how the build was invoked. It becomes impossible for the tests to include the correct headers directory. Set and export KHDR_INCLUDES variable to make it possible for sub make files to include the header files.

---
 tools/testing/selftests/Makefile | 4 ++++
 1 file changed, 4 insertions(+)
tools/testing/selftests/Makefile +4 -0
index a7b63860b7bc..21f983dfd047 100644
@@ -129,8 +129,11 @@ ifneq ($(KBUILD_OUTPUT),)
   # $(realpath ...) resolves symlinks
   abs_objtree := $(realpath $(abs_objtree))
   BUILD := $(abs_objtree)/kselftest
+  KHDR_INCLUDES := -I${abs_objtree}/usr/include
 else
   BUILD := $(CURDIR)
+  abs_srctree := $(shell cd $(top_srcdir) && pwd)
+  KHDR_INCLUDES := -I${abs_srctree}/usr/include
   DEFAULT_INSTALL_HDR_PATH := 1
 endif
 
@@ -139,6 +142,7 @@ include $(top_srcdir)/scripts/subarch.include
 ARCH           ?= $(SUBARCH)
 export KSFT_KHDR_INSTALL_DONE := 1
 export BUILD
+export KHDR_INCLUDES
 
 # set default goal to all, so make without a target runs all, even when
 # all isn't the first target in the file.
Signature · 1 line
2.30.2
#3 lore raw

PATCH v2 3/10 selftests: Correct the headers install path

In reply to Muhammad Usama Anjum

uapi headers should be installed at the top of the object tree, "<obj_tree>/usr/include". There is no need for kernel headers to be present at kselftest build directory, "<obj_tree>/kselftest/usr/ include" as well. This duplication can be avoided by correctly specifying the INSTALL_HDR_PATH.

---
 tools/testing/selftests/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/Makefile +1 -1
index 21f983dfd047..80e5498eab92 100644
@@ -167,7 +167,7 @@ khdr:
 ifeq (1,$(DEFAULT_INSTALL_HDR_PATH))
 	$(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install
 else
-	$(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$BUILD/usr \
+	$(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$(abs_objtree)/usr \
 		ARCH=$(ARCH) -C $(top_srcdir) headers_install
 endif
 
Signature · 1 line
2.30.2
#4 lore raw

PATCH v2 4/10 selftests: futex: Add the uapi headers include variable

In reply to Muhammad Usama Anjum

Out of tree build of this test fails if relative path of the output directory is specified. KBUILD_OUTPUT also doesn't point to the correct directory when relative path is used. Thus out of tree builds fail. Remove the un-needed include paths and use KHDR_INCLUDES to correctly reach the headers.

---
Changes in V2:
Revert the excessive cleanup which was breaking the individual
test build.

---
 tools/testing/selftests/futex/functional/Makefile | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/futex/functional/Makefile +2 -3
index 5cc38de9d8ea..2a12b174cb04 100644
@@ -1,7 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
-INCLUDES := -I../include -I../../ -I../../../../../usr/include/ \
-	-I$(KBUILD_OUTPUT)/kselftest/usr/include
-CFLAGS := $(CFLAGS) -g -O2 -Wall -D_GNU_SOURCE -pthread $(INCLUDES)
+INCLUDES := -I../include -I../../ -I../../../../../usr/include/
+CFLAGS := $(CFLAGS) -g -O2 -Wall -D_GNU_SOURCE -pthread $(INCLUDES) $(KHDR_INCLUDES)
 LDLIBS := -lpthread -lrt
 
 HEADERS := \
Signature · 1 line
2.30.2
#5 lore raw

PATCH v2 5/10 selftests: kvm: Add the uapi headers include variable

In reply to Muhammad Usama Anjum

Out of tree build of this test fails if relative path of the output directory is specified. Add KHDR_INCLUDES to correctly reach the headers.

---
Changes in V2:
Revert the excessive cleanup which was breaking the individual
test build.

---
 tools/testing/selftests/kvm/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/Makefile +1 -1
index ee8cf2149824..556da71c33b8 100644
@@ -147,7 +147,7 @@ endif
 CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
 	-fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \
 	-I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \
-	-I$(<D) -Iinclude/$(UNAME_M) -I.. $(EXTRA_CFLAGS)
+	-I$(<D) -Iinclude/$(UNAME_M) -I.. $(EXTRA_CFLAGS) $(KHDR_INCLUDES)
 
 no-pie-option := $(call try-run, echo 'int main() { return 0; }' | \
         $(CC) -Werror -no-pie -x c - -o "$$TMP", -no-pie)
Signature · 1 line
2.30.2
#6 lore raw

PATCH v2 6/10 selftests: landlock: Add the uapi headers include variable

In reply to Muhammad Usama Anjum

Out of tree build of this test fails if relative path of the output directory is specified. Add the KHDR_INCLUDES to correctly reach the headers.

---
Changes in V2:
Revert the excessive cleanup which was breaking the individual
test build.

---
 tools/testing/selftests/landlock/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/landlock/Makefile +1 -1
index a99596ca9882..0b0049e133bb 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 
-CFLAGS += -Wall -O2
+CFLAGS += -Wall -O2 $(KHDR_INCLUDES)
 
 src_test := $(wildcard *_test.c)
 
Signature · 1 line
2.30.2
maintainer Reviewed-by on v2 6/10
#7 lore raw

Re: [PATCH V2 06/10] selftests: landlock: Add the uapi headers include variable

Quoted 18 lines · Muhammad Usama Anjum

Out of tree build of this test fails if relative path of the output directory is specified. Add the KHDR_INCLUDES to correctly reach the headers.

---
Changes in V2:
Revert the excessive cleanup which was breaking the individual
test build.

---
  tools/testing/selftests/landlock/Makefile | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/landlock/Makefile +0 -0
index a99596ca9882..0b0049e133bb 100644

tools/testing/selftests/landlock/Makefile line 3

Quoted 4 lines
tools/testing/selftests/landlock/Makefile +1 -1
@@ -1,6 +1,6 @@
  # SPDX-License-Identifier: GPL-2.0
 
-CFLAGS += -Wall -O2
+CFLAGS += -Wall -O2 $(KHDR_INCLUDES)

It works for me, but I'm wondering if a missing -I path could cause any issue (cf. -I$(khdr_dir) bellow in this file). My GCC and clang ignore such non-existent paths unless -Wmissing-include-dirs is used, which would print a warning on your CI, but I guess that's OK.

Quoted 3 lines
tools/testing/selftests/landlock/Makefile +0 -0
  src_test := $(wildcard *_test.c)
 
#8 lore raw

PATCH v2 7/10 selftests: net: Add the uapi headers include variable

In reply to Muhammad Usama Anjum

Out of tree build of this test fails if relative path of the output directory is specified. Add the KHDR_INCLUDES to correctly reach the headers.

---
Changes in V2:
Revert the excessive cleanup which was breaking the individual
test build.

---
 tools/testing/selftests/net/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/net/Makefile +1 -1
index 9897fa9ab953..0b1488616c55 100644
@@ -2,7 +2,7 @@
 # Makefile for net selftests
 
 CFLAGS =  -Wall -Wl,--no-as-needed -O2 -g
-CFLAGS += -I../../../../usr/include/
+CFLAGS += -I../../../../usr/include/ $(KHDR_INCLUDES)
 
 TEST_PROGS := run_netsocktests run_afpackettests test_bpf.sh netdevice.sh \
 	      rtnetlink.sh xfrm_policy.sh test_blackhole_dev.sh
Signature · 1 line
2.30.2
#9 lore raw

PATCH v2 8/10 selftests: mptcp: Add the uapi headers include variable

In reply to Muhammad Usama Anjum

Out of tree build of this test fails if relative path of the output directory is specified. Add the KHDR_INCLUDES to correctly reach the headers.

---
Changes in V2:
Revert the excessive cleanup which was breaking the individual
test build.

---
 tools/testing/selftests/net/mptcp/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/net/mptcp/Makefile +1 -1
index 0356c4501c99..f905d5358e68 100644
@@ -3,7 +3,7 @@
 top_srcdir = ../../../../..
 KSFT_KHDR_INSTALL := 1
 
-CFLAGS =  -Wall -Wl,--no-as-needed -O2 -g  -I$(top_srcdir)/usr/include
+CFLAGS =  -Wall -Wl,--no-as-needed -O2 -g -I$(top_srcdir)/usr/include $(KHDR_INCLUDES)
 
 TEST_PROGS := mptcp_connect.sh pm_netlink.sh mptcp_join.sh diag.sh \
 	      simult_flows.sh mptcp_sockopt.sh
Signature · 1 line
2.30.2
Reviewed-by on v2 8/10
#10 lore raw

Re: [PATCH V2 08/10] selftests: mptcp: Add the uapi headers include variable

Hi Muhammad,

Quoted 9 lines · Muhammad Usama Anjum

Out of tree build of this test fails if relative path of the output directory is specified. Add the KHDR_INCLUDES to correctly reach the headers.

---
Changes in V2:
Revert the excessive cleanup which was breaking the individual
test build.

Thank you for the v2, it looks safer that way and it no longer breaks our CI!

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net
#11 lore raw

PATCH v2 9/10 selftests: vm: Add the uapi headers include variable

In reply to Muhammad Usama Anjum

Out of tree build of this test fails if relative path of the output directory is specified. Add the KHDR_INCLUDES to correctly reach the headers.

---
Changes in V2:
Revert the excessive cleanup which was breaking the individual
test build.

---
 tools/testing/selftests/vm/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/vm/Makefile +1 -1
index 7d100a7dc462..96714d2d49dc 100644
@@ -23,7 +23,7 @@ MACHINE ?= $(shell echo $(uname_M) | sed -e 's/aarch64.*/arm64/' -e 's/ppc64.*/p
 # LDLIBS.
 MAKEFLAGS += --no-builtin-rules
 
-CFLAGS = -Wall -I ../../../../usr/include $(EXTRA_CFLAGS)
+CFLAGS = -Wall -I ../../../../usr/include $(EXTRA_CFLAGS) $(KHDR_INCLUDES)
 LDLIBS = -lrt -lpthread
 TEST_GEN_FILES = compaction_test
 TEST_GEN_FILES += gup_test
Signature · 1 line
2.30.2
Tested-by on v2 9/10
#12 lore raw

Re: [PATCH V2 09/10] selftests: vm: Add the uapi headers include variable

Thanks for fixing this! It has been annoying me for a while but I never found the time to investigate properly. Feel free to add:

Quoted 28 lines · AEDT Muhammad Usama Anjum

Out of tree build of this test fails if relative path of the output directory is specified. Add the KHDR_INCLUDES to correctly reach the headers.

---
Changes in V2:
Revert the excessive cleanup which was breaking the individual
test build.

---
 tools/testing/selftests/vm/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/vm/Makefile +1 -1
index 7d100a7dc462..96714d2d49dc 100644
@@ -23,7 +23,7 @@ MACHINE ?= $(shell echo $(uname_M) | sed -e 's/aarch64.*/arm64/' -e 's/ppc64.*/p
 # LDLIBS.
 MAKEFLAGS += --no-builtin-rules
 
-CFLAGS = -Wall -I ../../../../usr/include $(EXTRA_CFLAGS)
+CFLAGS = -Wall -I ../../../../usr/include $(EXTRA_CFLAGS) $(KHDR_INCLUDES)
 LDLIBS = -lrt -lpthread
 TEST_GEN_FILES = compaction_test
 TEST_GEN_FILES += gup_test
#13 lore raw

PATCH v2 10/10 selftests: vm: remove dependecy from internal kernel macros

In reply to Muhammad Usama Anjum

The defination of swap() is used from kernel's internal header when this test is built in source tree. The build fails when this test is built out of source tree as defination of swap() isn't found. Selftests shouldn't depend on kernel's internal header files. They can only depend on uapi header files. Add the defination of swap() to fix the build error:

gcc -Wall -I/linux_mainline2/build/usr/include -no-pie userfaultfd.c -lrt -lpthread -o /linux_mainline2/build/kselftest/vm/userfaultfd
userfaultfd.c: In function ‘userfaultfd_stress’:
userfaultfd.c:1530:3: warning: implicit declaration of function ‘swap’; did you mean ‘swab’? [-Wimplicit-function-declaration]
1530 | swap(area_src, area_dst);
| ^~~~
| swab
/usr/bin/ld: /tmp/cclUUH7V.o: in function `userfaultfd_stress':
userfaultfd.c:(.text+0x4d64): undefined reference to `swap'
/usr/bin/ld: userfaultfd.c:(.text+0x4d82): undefined reference to `swap'
collect2: error: ld returned 1 exit status

Fixes 2c769ed7137a ("tools/testing/selftests/vm/userfaultfd.c: use swap() to make code cleaner")
---
 tools/testing/selftests/vm/userfaultfd.c | 3 +++
 1 file changed, 3 insertions(+)
tools/testing/selftests/vm/userfaultfd.c +3 -0
index d3fd24f9fae8..d2480ab93037 100644
@@ -119,6 +119,9 @@ struct uffd_stats {
 				 ~(unsigned long)(sizeof(unsigned long long) \
 						  -  1)))
 
+#define swap(a, b) \
+	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+
 const char *examples =
     "# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
     "./userfaultfd anon 100 99999\n\n"
Signature · 1 line
2.30.2
Reviewed-by on v2 10/10
#14 lore raw

Re: [PATCH V2 10/10] selftests: vm: remove dependecy from internal kernel macros

Quoted 39 lines · AEDT Muhammad Usama Anjum

The defination of swap() is used from kernel's internal header when this test is built in source tree. The build fails when this test is built out of source tree as defination of swap() isn't found. Selftests shouldn't depend on kernel's internal header files. They can only depend on uapi header files. Add the defination of swap() to fix the build error:

gcc -Wall -I/linux_mainline2/build/usr/include -no-pie userfaultfd.c -lrt -lpthread -o /linux_mainline2/build/kselftest/vm/userfaultfd
userfaultfd.c: In function ‘userfaultfd_stress’:
userfaultfd.c:1530:3: warning: implicit declaration of function ‘swap’; did you mean ‘swab’? [-Wimplicit-function-declaration]
1530 | swap(area_src, area_dst);
| ^~~~
| swab
/usr/bin/ld: /tmp/cclUUH7V.o: in function `userfaultfd_stress':
userfaultfd.c:(.text+0x4d64): undefined reference to `swap'
/usr/bin/ld: userfaultfd.c:(.text+0x4d82): undefined reference to `swap'
collect2: error: ld returned 1 exit status

Fixes 2c769ed7137a ("tools/testing/selftests/vm/userfaultfd.c: use swap() to make code cleaner")
---
 tools/testing/selftests/vm/userfaultfd.c | 3 +++
 1 file changed, 3 insertions(+)
tools/testing/selftests/vm/userfaultfd.c +3 -0
index d3fd24f9fae8..d2480ab93037 100644
@@ -119,6 +119,9 @@ struct uffd_stats {
 				 ~(unsigned long)(sizeof(unsigned long long) \
 						  -  1)))
 
+#define swap(a, b) \
+	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+
 const char *examples =
     "# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
     "./userfaultfd anon 100 99999\n\n"