2022-07-08 16:41:27

by Guillaume Tucker

[permalink] [raw]
Subject: [PATCH 0/4] Fix kselftest build with sub-directory

Earlier attempts to get "make O=build kselftest-all" to work were
not successful as they made undesirable changes to some functions
in the top-level Makefile. This series takes a different
approach by removing the root cause of the problem within
kselftest, which is when the sub-Makefile tries to install kernel
headers "backwards" by calling make with the top-level Makefile.
The actual issue comes from the fact that $(srctree) is ".." when
building in a sub-directory with "O=build" which then obviously
makes "-C $(top_srcdir)" point outside of the real source tree.

With this series, the generic kselftest targets work as expected
from the top level with or without a build directory e.g.:

$ make kselftest-all

$ make O=build kselftest-all

Then in order to build using the sub-Makefile explicitly, the
headers have to be installed first. This is arguably a valid
requirement to have when building a tool from a sub-Makefile.
For example, "make -C tools/testing/nvdimm/" fails in a similar
way until <asm/rwonce.h> has been generated by a kernel build.

Guillaume Tucker (4):
selftests: drop khdr make target
selftests: stop using KSFT_KHDR_INSTALL
selftests: drop KSFT_KHDR_INSTALL make target
Makefile: add headers_install to kselftest targets

Makefile | 4 +-
tools/testing/selftests/Makefile | 28 +-------------
tools/testing/selftests/arm64/mte/Makefile | 1 -
tools/testing/selftests/arm64/signal/Makefile | 1 -
.../selftests/arm64/signal/test_signals.h | 4 +-
.../selftests/drivers/s390x/uvdevice/Makefile | 1 -
.../selftests/futex/functional/Makefile | 1 -
tools/testing/selftests/kvm/Makefile | 1 -
tools/testing/selftests/landlock/Makefile | 1 -
tools/testing/selftests/lib.mk | 38 -------------------
tools/testing/selftests/net/Makefile | 1 -
tools/testing/selftests/net/mptcp/Makefile | 1 -
tools/testing/selftests/tc-testing/Makefile | 1 -
tools/testing/selftests/vm/Makefile | 1 -
14 files changed, 5 insertions(+), 79 deletions(-)

--
2.30.2


2022-07-08 16:43:21

by Guillaume Tucker

[permalink] [raw]
Subject: [PATCH 1/4] selftests: drop khdr make target

Drop the "khdr" make target as it fails when the build directory is a
sub-directory of the source tree. Rely on the "headers_install"
target to have been run first instead.

For example, here's a typical error this patch is addressing:

$ make O=build -j32 kselftest-gen_tar
make[1]: Entering directory '/home/kernelci/linux/build'
make --no-builtin-rules INSTALL_HDR_PATH=/home/kernelci/linux/build/usr \
ARCH=x86 -C ../../.. headers_install
make[3]: Entering directory '/home/kernelci/linux'
Makefile:1022: ../scripts/Makefile.extrawarn: No such file or directory

The source directory is determined in the top-level Makefile as ".."
relatively to the "build" directory, but then the kselftest Makefile
switches to "-C ../../.." so "../scripts" then points one level higher
than the source tree e.g. "linux/../scripts" - which fails obviously.
There is no other use-case in the kernel tree where a sub-directory
Makefile tries to call a top-level make target, and it appears this
isn't really a valid thing to do.

Signed-off-by: Guillaume Tucker <[email protected]>
---
tools/testing/selftests/Makefile | 27 ++-------------------------
1 file changed, 2 insertions(+), 25 deletions(-)

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index de11992dc577..619451e82863 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -151,30 +151,7 @@ export KHDR_INCLUDES
# all isn't the first target in the file.
.DEFAULT_GOAL := all

-# Install headers here once for all tests. KSFT_KHDR_INSTALL_DONE
-# is used to avoid running headers_install from lib.mk.
-# Invoke headers install with --no-builtin-rules to avoid circular
-# dependency in "make kselftest" case. In this case, second level
-# make inherits builtin-rules which will use the rule generate
-# Makefile.o and runs into
-# "Circular Makefile.o <- prepare dependency dropped."
-# and headers_install fails and test compile fails.
-#
-# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile
-# invokes them as sub-makes and --no-builtin-rules is not necessary,
-# but doesn't cause any failures. Keep it simple and use the same
-# flags in both cases.
-# Local build cases: "make kselftest", "make -C" - headers are installed
-# in the default INSTALL_HDR_PATH usr/include.
-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=$(abs_objtree)/usr \
- ARCH=$(ARCH) -C $(top_srcdir) headers_install
-endif
-
-all: khdr
+all:
@ret=1; \
for TARGET in $(TARGETS); do \
BUILD_TARGET=$$BUILD/$$TARGET; \
@@ -274,4 +251,4 @@ clean:
$(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET clean;\
done;

-.PHONY: khdr all run_tests hotplug run_hotplug clean_hotplug run_pstore_crash install clean gen_tar
+.PHONY: all run_tests hotplug run_hotplug clean_hotplug run_pstore_crash install clean gen_tar
--
2.30.2

2022-07-08 16:43:53

by Guillaume Tucker

[permalink] [raw]
Subject: [PATCH 4/4] Makefile: add headers_install to kselftest targets

Add headers_install as a dependency to kselftest targets so that they
can be run directly from the top of the tree. The kselftest Makefile
used to try to call headers_install "backwards" but failed due to the
relative path not being consistent.

Now we can either run this directly:

$ make O=build kselftest-all

or this:

$ make O=build headers_install
$ make O=build -C tools/testing/selftest all

The same commands work as well when building directly in the source
tree (no O=) or any arbitrary path (relative or absolute).

Signed-off-by: Guillaume Tucker <[email protected]>
---
Makefile | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 1a6678d817bd..afc9d739ba44 100644
--- a/Makefile
+++ b/Makefile
@@ -1347,10 +1347,10 @@ tools/%: FORCE
# Kernel selftest

PHONY += kselftest
-kselftest:
+kselftest: headers_install
$(Q)$(MAKE) -C $(srctree)/tools/testing/selftests run_tests

-kselftest-%: FORCE
+kselftest-%: headers_install FORCE
$(Q)$(MAKE) -C $(srctree)/tools/testing/selftests $*

PHONY += kselftest-merge
--
2.30.2

2022-07-08 17:58:44

by Shuah Khan

[permalink] [raw]
Subject: Re: [PATCH 0/4] Fix kselftest build with sub-directory

On 7/8/22 10:23 AM, Guillaume Tucker wrote:
> Earlier attempts to get "make O=build kselftest-all" to work were
> not successful as they made undesirable changes to some functions
> in the top-level Makefile. This series takes a different
> approach by removing the root cause of the problem within
> kselftest, which is when the sub-Makefile tries to install kernel
> headers "backwards" by calling make with the top-level Makefile.
> The actual issue comes from the fact that $(srctree) is ".." when
> building in a sub-directory with "O=build" which then obviously
> makes "-C $(top_srcdir)" point outside of the real source tree.
>
> With this series, the generic kselftest targets work as expected
> from the top level with or without a build directory e.g.:
>
> $ make kselftest-all
>
> $ make O=build kselftest-all
>
> Then in order to build using the sub-Makefile explicitly, the
> headers have to be installed first. This is arguably a valid
> requirement to have when building a tool from a sub-Makefile.
> For example, "make -C tools/testing/nvdimm/" fails in a similar
> way until <asm/rwonce.h> has been generated by a kernel build.
>
> Guillaume Tucker (4):
> selftests: drop khdr make target
> selftests: stop using KSFT_KHDR_INSTALL
> selftests: drop KSFT_KHDR_INSTALL make target
> Makefile: add headers_install to kselftest targets
>

This takes us to back to the state before b2d35fa5fc80 added
khdr support. I reluctantly agreed to the change and it has
proven to be a problematic change. I would rather have had the
dependency stated that headers should be installed prior to
building tests - test build depends on kernel build anyway and
having dependency on headers having build isn't a huge deal.

So I am in favor of getting rid of khdr support. However, this
khdr support was a change originated from Linaro test ring. Undoing
this might have implication on their workflow.

I will pull them into the discussion so they are aware of it and
be prepared for this change.

thanks,
-- Shuah


2022-07-08 18:04:42

by Bird, Tim

[permalink] [raw]
Subject: RE: [PATCH 0/4] Fix kselftest build with sub-directory



> -----Original Message-----
> From: Shuah Khan <[email protected]>
>
> On 7/8/22 10:23 AM, Guillaume Tucker wrote:
> > Earlier attempts to get "make O=build kselftest-all" to work were
> > not successful as they made undesirable changes to some functions
> > in the top-level Makefile. This series takes a different
> > approach by removing the root cause of the problem within
> > kselftest, which is when the sub-Makefile tries to install kernel
> > headers "backwards" by calling make with the top-level Makefile.
> > The actual issue comes from the fact that $(srctree) is ".." when
> > building in a sub-directory with "O=build" which then obviously
> > makes "-C $(top_srcdir)" point outside of the real source tree.
> >
> > With this series, the generic kselftest targets work as expected
> > from the top level with or without a build directory e.g.:
> >
> > $ make kselftest-all
> >
> > $ make O=build kselftest-all
> >
> > Then in order to build using the sub-Makefile explicitly, the
> > headers have to be installed first. This is arguably a valid
> > requirement to have when building a tool from a sub-Makefile.
> > For example, "make -C tools/testing/nvdimm/" fails in a similar
> > way until <asm/rwonce.h> has been generated by a kernel build.
> >
> > Guillaume Tucker (4):
> > selftests: drop khdr make target
> > selftests: stop using KSFT_KHDR_INSTALL
> > selftests: drop KSFT_KHDR_INSTALL make target
> > Makefile: add headers_install to kselftest targets
> >
>
> This takes us to back to the state before b2d35fa5fc80 added
> khdr support. I reluctantly agreed to the change and it has
> proven to be a problematic change. I would rather have had the
> dependency stated that headers should be installed prior to
> building tests - test build depends on kernel build anyway and
> having dependency on headers having build isn't a huge deal.
>
> So I am in favor of getting rid of khdr support. However, this
> khdr support was a change originated from Linaro test ring. Undoing
> this might have implication on their workflow.
>
> I will pull them into the discussion so they are aware of it and
> be prepared for this change.

I ran into this bug quite a while ago. I reported it here:
https://lore.kernel.org/all/[email protected]/
it was fixed here:
https://lore.kernel.org/all/[email protected]/
but apparently reverting it was discussed, based on this:
https://lore.kernel.org/all/[email protected]/

I'm not sure what happened after that.

I was able to work around it by avoiding
putting the build directory inside the source tree.

I strongly support getting rid of the khdr stuff, as it's quite hard
to follow. I think my workflow would not be affected (but I should
run off and test it.)

Thanks for working on this Guillaume!
-- Tim

2022-07-11 12:33:37

by Anders Roxell

[permalink] [raw]
Subject: Re: [PATCH 0/4] Fix kselftest build with sub-directory

On Fri, 8 Jul 2022 at 19:14, Shuah Khan <[email protected]> wrote:
>
> On 7/8/22 10:23 AM, Guillaume Tucker wrote:
> > Earlier attempts to get "make O=build kselftest-all" to work were
> > not successful as they made undesirable changes to some functions
> > in the top-level Makefile. This series takes a different
> > approach by removing the root cause of the problem within
> > kselftest, which is when the sub-Makefile tries to install kernel
> > headers "backwards" by calling make with the top-level Makefile.
> > The actual issue comes from the fact that $(srctree) is ".." when
> > building in a sub-directory with "O=build" which then obviously
> > makes "-C $(top_srcdir)" point outside of the real source tree.
> >
> > With this series, the generic kselftest targets work as expected
> > from the top level with or without a build directory e.g.:
> >
> > $ make kselftest-all
> >
> > $ make O=build kselftest-all
> >
> > Then in order to build using the sub-Makefile explicitly, the
> > headers have to be installed first. This is arguably a valid
> > requirement to have when building a tool from a sub-Makefile.
> > For example, "make -C tools/testing/nvdimm/" fails in a similar
> > way until <asm/rwonce.h> has been generated by a kernel build.
> >
> > Guillaume Tucker (4):
> > selftests: drop khdr make target
> > selftests: stop using KSFT_KHDR_INSTALL
> > selftests: drop KSFT_KHDR_INSTALL make target
> > Makefile: add headers_install to kselftest targets
> >
>
> This takes us to back to the state before b2d35fa5fc80 added
> khdr support. I reluctantly agreed to the change and it has
> proven to be a problematic change. I would rather have had the
> dependency stated that headers should be installed prior to
> building tests - test build depends on kernel build anyway and
> having dependency on headers having build isn't a huge deal.

I agree that it's not a huge deal.

>
> So I am in favor of getting rid of khdr support. However, this
> khdr support was a change originated from Linaro test ring. Undoing
> this might have implication on their workflow.

It shouldn't be a problem.
I've been running these patches through a smoke test and it looks
good.

Tested-by: Anders Roxell <[email protected]>


Cheers,
Anders

>
> I will pull them into the discussion so they are aware of it and
> be prepared for this change.
>
> thanks,
> -- Shuah
>
>

2022-07-11 23:18:08

by Shuah Khan

[permalink] [raw]
Subject: Re: [PATCH 0/4] Fix kselftest build with sub-directory

On 7/11/22 6:13 AM, Anders Roxell wrote:
> On Fri, 8 Jul 2022 at 19:14, Shuah Khan <[email protected]> wrote:
>>
>> On 7/8/22 10:23 AM, Guillaume Tucker wrote:
>>> Earlier attempts to get "make O=build kselftest-all" to work were
>>> not successful as they made undesirable changes to some functions
>>> in the top-level Makefile. This series takes a different
>>> approach by removing the root cause of the problem within
>>> kselftest, which is when the sub-Makefile tries to install kernel
>>> headers "backwards" by calling make with the top-level Makefile.
>>> The actual issue comes from the fact that $(srctree) is ".." when
>>> building in a sub-directory with "O=build" which then obviously
>>> makes "-C $(top_srcdir)" point outside of the real source tree.
>>>
>>> With this series, the generic kselftest targets work as expected
>>> from the top level with or without a build directory e.g.:
>>>
>>> $ make kselftest-all
>>>
>>> $ make O=build kselftest-all
>>>
>>> Then in order to build using the sub-Makefile explicitly, the
>>> headers have to be installed first. This is arguably a valid
>>> requirement to have when building a tool from a sub-Makefile.
>>> For example, "make -C tools/testing/nvdimm/" fails in a similar
>>> way until <asm/rwonce.h> has been generated by a kernel build.
>>>
>>> Guillaume Tucker (4):
>>> selftests: drop khdr make target
>>> selftests: stop using KSFT_KHDR_INSTALL
>>> selftests: drop KSFT_KHDR_INSTALL make target
>>> Makefile: add headers_install to kselftest targets
>>>
>>
>> This takes us to back to the state before b2d35fa5fc80 added
>> khdr support. I reluctantly agreed to the change and it has
>> proven to be a problematic change. I would rather have had the
>> dependency stated that headers should be installed prior to
>> building tests - test build depends on kernel build anyway and
>> having dependency on headers having build isn't a huge deal.
>
> I agree that it's not a huge deal.
>
>>
>> So I am in favor of getting rid of khdr support. However, this
>> khdr support was a change originated from Linaro test ring. Undoing
>> this might have implication on their workflow.
>
> It shouldn't be a problem.
> I've been running these patches through a smoke test and it looks
> good.
>
> Tested-by: Anders Roxell <[email protected]>
>
>

Thank you Anders for confirming this isn't a problem for Linaro workflow
and testing.

Than you Guillaume for fixing the problem. I will apply these for 5.20-rc1.

thanks,
-- Shuah

2022-07-12 01:40:25

by Masahiro Yamada

[permalink] [raw]
Subject: Re: [PATCH 4/4] Makefile: add headers_install to kselftest targets

On Sat, Jul 9, 2022 at 1:23 AM Guillaume Tucker
<[email protected]> wrote:
>
> Add headers_install as a dependency to kselftest targets so that they
> can be run directly from the top of the tree. The kselftest Makefile
> used to try to call headers_install "backwards" but failed due to the
> relative path not being consistent.
>
> Now we can either run this directly:
>
> $ make O=build kselftest-all
>
> or this:
>
> $ make O=build headers_install
> $ make O=build -C tools/testing/selftest all
>
> The same commands work as well when building directly in the source
> tree (no O=) or any arbitrary path (relative or absolute).
>
> Signed-off-by: Guillaume Tucker <[email protected]>
> ---
> Makefile | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 1a6678d817bd..afc9d739ba44 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1347,10 +1347,10 @@ tools/%: FORCE
> # Kernel selftest
>
> PHONY += kselftest
> -kselftest:
> +kselftest: headers_install
> $(Q)$(MAKE) -C $(srctree)/tools/testing/selftests run_tests

Nit.
Please use 'headers' for in-kernel use of exportedI headers.

kselftest: headers


>
> -kselftest-%: FORCE
> +kselftest-%: headers_install FORCE
> $(Q)$(MAKE) -C $(srctree)/tools/testing/selftests $*

Ditto.

kselftest-%: headers FORCE


>
> PHONY += kselftest-merge
> --
> 2.30.2
>


--
Best Regards
Masahiro Yamada

2022-07-12 09:09:21

by Guillaume Tucker

[permalink] [raw]
Subject: Re: [PATCH 4/4] Makefile: add headers_install to kselftest targets

On 12/07/2022 02:35, Masahiro Yamada wrote:
> On Sat, Jul 9, 2022 at 1:23 AM Guillaume Tucker
> <[email protected]> wrote:
>>
>> Add headers_install as a dependency to kselftest targets so that they
>> can be run directly from the top of the tree. The kselftest Makefile
>> used to try to call headers_install "backwards" but failed due to the
>> relative path not being consistent.
>>
>> Now we can either run this directly:
>>
>> $ make O=build kselftest-all
>>
>> or this:
>>
>> $ make O=build headers_install
>> $ make O=build -C tools/testing/selftest all
>>
>> The same commands work as well when building directly in the source
>> tree (no O=) or any arbitrary path (relative or absolute).
>>
>> Signed-off-by: Guillaume Tucker <[email protected]>
>> ---
>> Makefile | 4 ++--
>> 1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/Makefile b/Makefile
>> index 1a6678d817bd..afc9d739ba44 100644
>> --- a/Makefile
>> +++ b/Makefile
>> @@ -1347,10 +1347,10 @@ tools/%: FORCE
>> # Kernel selftest
>>
>> PHONY += kselftest
>> -kselftest:
>> +kselftest: headers_install
>> $(Q)$(MAKE) -C $(srctree)/tools/testing/selftests run_tests
>
> Nit.
> Please use 'headers' for in-kernel use of exportedI headers.
>
> kselftest: headers
>
>
>>
>> -kselftest-%: FORCE
>> +kselftest-%: headers_install FORCE
>> $(Q)$(MAKE) -C $(srctree)/tools/testing/selftests $*
>
> Ditto.
>
> kselftest-%: headers FORCE

Thank you all for the reviews.

I've sent a v2 with this tweak and double-checked that the
kselftest build produced exactly the same result.

Best wishes,
Guillaume

2022-07-13 07:04:22

by Guillaume Tucker

[permalink] [raw]
Subject: Re: [PATCH 4/4] Makefile: add headers_install to kselftest targets

On 08/07/2022 18:23, Guillaume Tucker wrote:
>
> $ make O=build headers_install
> $ make O=build -C tools/testing/selftest all

Typo, it should be selftests:

$ make O=build -C tools/testing/selftests all

Thanks,
Guillaume