linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 2/4] selftests: Fix seccomp to support relocatable build (O=objdir)
@ 2020-03-05  0:36 Shuah Khan
  2020-03-05  2:20 ` Kees Cook
  0 siblings, 1 reply; 5+ messages in thread
From: Shuah Khan @ 2020-03-05  0:36 UTC (permalink / raw)
  To: shuah, keescook, luto, wad, daniel, kafai, yhs, andriin, gregkh, tglx
  Cc: Shuah Khan, khilman, mpe, linux-kselftest, linux-kernel, netdev, bpf

Fix seccomp relocatable builds. This is a simple fix to use the
right lib.mk variable TEST_CUSTOM_PROGS to continue to do custom
build to preserve dependency on kselftest_harness.h local header.
This change applies cutom rule to seccomp_bpf seccomp_benchmark
for a simpler logic. 

Uses $(OUTPUT) defined in lib.mk to handle build relocation.

The following use-cases work with this change:

In seccomp directory:
make all and make clean

From top level from main Makefile:
make kselftest-install O=objdir ARCH=arm64 HOSTCC=gcc \
 CROSS_COMPILE=aarch64-linux-gnu- TARGETS=seccomp

Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
---
 tools/testing/selftests/seccomp/Makefile | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/tools/testing/selftests/seccomp/Makefile b/tools/testing/selftests/seccomp/Makefile
index 1760b3e39730..355bcbc0394a 100644
--- a/tools/testing/selftests/seccomp/Makefile
+++ b/tools/testing/selftests/seccomp/Makefile
@@ -1,17 +1,16 @@
 # SPDX-License-Identifier: GPL-2.0
-all:
-
-include ../lib.mk
+CFLAGS += -Wl,-no-as-needed -Wall
+LDFLAGS += -lpthread
 
 .PHONY: all clean
 
-BINARIES := seccomp_bpf seccomp_benchmark
-CFLAGS += -Wl,-no-as-needed -Wall
+include ../lib.mk
+
+# OUTPUT set by lib.mk
+TEST_CUSTOM_PROGS := $(OUTPUT)/seccomp_bpf $(OUTPUT)/seccomp_benchmark
 
-seccomp_bpf: seccomp_bpf.c ../kselftest_harness.h
-	$(CC) $(CFLAGS) $(LDFLAGS) $< -lpthread -o $@
+$(TEST_CUSTOM_PROGS): ../kselftest_harness.h
 
-TEST_PROGS += $(BINARIES)
-EXTRA_CLEAN := $(BINARIES)
+all: $(TEST_CUSTOM_PROGS)
 
-all: $(BINARIES)
+EXTRA_CLEAN := $(TEST_CUSTOM_PROGS)
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/4] selftests: Fix seccomp to support relocatable build (O=objdir)
  2020-03-05  0:36 [PATCH v2 2/4] selftests: Fix seccomp to support relocatable build (O=objdir) Shuah Khan
@ 2020-03-05  2:20 ` Kees Cook
  2020-03-05 16:41   ` Shuah Khan
  0 siblings, 1 reply; 5+ messages in thread
From: Kees Cook @ 2020-03-05  2:20 UTC (permalink / raw)
  To: Shuah Khan
  Cc: shuah, luto, wad, daniel, kafai, yhs, andriin, gregkh, tglx,
	khilman, mpe, linux-kselftest, linux-kernel, netdev, bpf

On Wed, Mar 04, 2020 at 05:36:27PM -0700, Shuah Khan wrote:
> Fix seccomp relocatable builds. This is a simple fix to use the
> right lib.mk variable TEST_CUSTOM_PROGS to continue to do custom
> build to preserve dependency on kselftest_harness.h local header.
> This change applies cutom rule to seccomp_bpf seccomp_benchmark
> for a simpler logic. 
> 
> Uses $(OUTPUT) defined in lib.mk to handle build relocation.
> 
> The following use-cases work with this change:
> 
> In seccomp directory:
> make all and make clean

This works.

> 
> From top level from main Makefile:
> make kselftest-install O=objdir ARCH=arm64 HOSTCC=gcc \
>  CROSS_COMPILE=aarch64-linux-gnu- TARGETS=seccomp

This fails for me:

$ make kselftest-install O=objdir ARCH=arm64 HOSTCC=gcc CROSS_COMPILE=aarch64-linux-gnu- TARGETS=seccomp
make[1]: Entering directory '/home/kees/src/linux/objdir'
make --no-builtin-rules INSTALL_HDR_PATH=$BUILD/usr \
        ARCH=arm64 -C ../../.. headers_install
make[4]: ../scripts/Makefile.build: No such file or directory
make[4]: *** No rule to make target '../scripts/Makefile.build'.  Stop.
make[3]: *** [Makefile:501: scripts_basic] Error 2
make[2]: *** [Makefile:151: khdr] Error 2
make[1]: *** [/home/kees/src/linux/Makefile:1221: kselftest-install] Error 2
make[1]: Leaving directory '/home/kees/src/linux/objdir'
make: *** [Makefile:180: sub-make] Error 2

(My "objdir" is empty)

If I remove O=objdir everything is fine. And see below...

> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
> ---
>  tools/testing/selftests/seccomp/Makefile | 19 +++++++++----------
>  1 file changed, 9 insertions(+), 10 deletions(-)
> 
> diff --git a/tools/testing/selftests/seccomp/Makefile b/tools/testing/selftests/seccomp/Makefile
> index 1760b3e39730..355bcbc0394a 100644
> --- a/tools/testing/selftests/seccomp/Makefile
> +++ b/tools/testing/selftests/seccomp/Makefile
> @@ -1,17 +1,16 @@
>  # SPDX-License-Identifier: GPL-2.0
> -all:
> -
> -include ../lib.mk
> +CFLAGS += -Wl,-no-as-needed -Wall
> +LDFLAGS += -lpthread
>  
>  .PHONY: all clean
>  
> -BINARIES := seccomp_bpf seccomp_benchmark
> -CFLAGS += -Wl,-no-as-needed -Wall
> +include ../lib.mk
> +
> +# OUTPUT set by lib.mk
> +TEST_CUSTOM_PROGS := $(OUTPUT)/seccomp_bpf $(OUTPUT)/seccomp_benchmark
>  
> -seccomp_bpf: seccomp_bpf.c ../kselftest_harness.h
> -	$(CC) $(CFLAGS) $(LDFLAGS) $< -lpthread -o $@
> +$(TEST_CUSTOM_PROGS): ../kselftest_harness.h
>  
> -TEST_PROGS += $(BINARIES)
> -EXTRA_CLEAN := $(BINARIES)
> +all: $(TEST_CUSTOM_PROGS)
>  
> -all: $(BINARIES)
> +EXTRA_CLEAN := $(TEST_CUSTOM_PROGS)
> -- 
> 2.20.1
> 

Instead of the TEST_CUSTOM_PROGS+all dance, you can just add an explicit
dependency, with the final seccomp/Makefile looking like this:


# SPDX-License-Identifier: GPL-2.0
CFLAGS += -Wl,-no-as-needed -Wall
LDFLAGS += -lpthread

TEST_GEN_PROGS := seccomp_bpf seccomp_benchmark

include ../lib.mk

# Additional dependencies
$(OUTPUT)/seccomp_bpf: ../kselftest_harness.h


(Though this fails in the same way as above when run from the top-level
directory.)

-Kees

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/4] selftests: Fix seccomp to support relocatable build (O=objdir)
  2020-03-05  2:20 ` Kees Cook
@ 2020-03-05 16:41   ` Shuah Khan
  2020-03-05 17:42     ` Kees Cook
  0 siblings, 1 reply; 5+ messages in thread
From: Shuah Khan @ 2020-03-05 16:41 UTC (permalink / raw)
  To: Kees Cook
  Cc: shuah, luto, wad, daniel, kafai, yhs, andriin, gregkh, tglx,
	khilman, mpe, linux-kselftest, linux-kernel, netdev, bpf,
	Shuah Khan

On 3/4/20 7:20 PM, Kees Cook wrote:
> On Wed, Mar 04, 2020 at 05:36:27PM -0700, Shuah Khan wrote:
>> Fix seccomp relocatable builds. This is a simple fix to use the
>> right lib.mk variable TEST_CUSTOM_PROGS to continue to do custom
>> build to preserve dependency on kselftest_harness.h local header.
>> This change applies cutom rule to seccomp_bpf seccomp_benchmark
>> for a simpler logic.
>>
>> Uses $(OUTPUT) defined in lib.mk to handle build relocation.
>>
>> The following use-cases work with this change:
>>
>> In seccomp directory:
>> make all and make clean
> 
> This works.
> 
>>
>>  From top level from main Makefile:
>> make kselftest-install O=objdir ARCH=arm64 HOSTCC=gcc \
>>   CROSS_COMPILE=aarch64-linux-gnu- TARGETS=seccomp
> 
> This fails for me:
> 
> $ make kselftest-install O=objdir ARCH=arm64 HOSTCC=gcc CROSS_COMPILE=aarch64-linux-gnu- TARGETS=seccomp
> make[1]: Entering directory '/home/kees/src/linux/objdir'
> make --no-builtin-rules INSTALL_HDR_PATH=$BUILD/usr \
>          ARCH=arm64 -C ../../.. headers_install
> make[4]: ../scripts/Makefile.build: No such file or directory
> make[4]: *** No rule to make target '../scripts/Makefile.build'.  Stop.
> make[3]: *** [Makefile:501: scripts_basic] Error 2
> make[2]: *** [Makefile:151: khdr] Error 2
> make[1]: *** [/home/kees/src/linux/Makefile:1221: kselftest-install] Error 2
> make[1]: Leaving directory '/home/kees/src/linux/objdir'
> make: *** [Makefile:180: sub-make] Error 2
> 
> (My "objdir" is empty)
> 
> If I remove O=objdir everything is fine. And see below...
> 
>> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
>> ---
>>   tools/testing/selftests/seccomp/Makefile | 19 +++++++++----------
>>   1 file changed, 9 insertions(+), 10 deletions(-)
>>
>> diff --git a/tools/testing/selftests/seccomp/Makefile b/tools/testing/selftests/seccomp/Makefile
>> index 1760b3e39730..355bcbc0394a 100644
>> --- a/tools/testing/selftests/seccomp/Makefile
>> +++ b/tools/testing/selftests/seccomp/Makefile
>> @@ -1,17 +1,16 @@
>>   # SPDX-License-Identifier: GPL-2.0
>> -all:
>> -
>> -include ../lib.mk
>> +CFLAGS += -Wl,-no-as-needed -Wall
>> +LDFLAGS += -lpthread
>>   
>>   .PHONY: all clean
>>   
>> -BINARIES := seccomp_bpf seccomp_benchmark
>> -CFLAGS += -Wl,-no-as-needed -Wall
>> +include ../lib.mk
>> +
>> +# OUTPUT set by lib.mk
>> +TEST_CUSTOM_PROGS := $(OUTPUT)/seccomp_bpf $(OUTPUT)/seccomp_benchmark
>>   
>> -seccomp_bpf: seccomp_bpf.c ../kselftest_harness.h
>> -	$(CC) $(CFLAGS) $(LDFLAGS) $< -lpthread -o $@
>> +$(TEST_CUSTOM_PROGS): ../kselftest_harness.h
>>   
>> -TEST_PROGS += $(BINARIES)
>> -EXTRA_CLEAN := $(BINARIES)
>> +all: $(TEST_CUSTOM_PROGS)
>>   
>> -all: $(BINARIES)
>> +EXTRA_CLEAN := $(TEST_CUSTOM_PROGS)
>> -- 
>> 2.20.1
>>
> 
> Instead of the TEST_CUSTOM_PROGS+all dance, you can just add an explicit
> dependency, with the final seccomp/Makefile looking like this:
> 
> 
> # SPDX-License-Identifier: GPL-2.0
> CFLAGS += -Wl,-no-as-needed -Wall
> LDFLAGS += -lpthread
> 
> TEST_GEN_PROGS := seccomp_bpf seccomp_benchmark
> 

TEST_CUSTOM_PROGS is for differentiating test programs that
can't use lib.mk generic rules. It is appropriate to use
for seccomp_bpf

> include ../lib.mk
> 
> # Additional dependencies
> $(OUTPUT)/seccomp_bpf: ../kselftest_harness.h
> 
> 
> (Though this fails in the same way as above when run from the top-level
> directory.)
> 

I didn't see this because I have been the same directory I used
for relocated cross-build kernel. :(

Thanks for testing this. I know the problem here. all is a dependency
for install step and $(OUTPUT) is referencing the objdir before it
gets created. It is a Makefile/lib.mk problem to fix.

I will do a separate patch for this. This will show up in any test
that is using $(OUTPUT) to relocate objects mainly the ones that
require custom build rule like seeccomp.

thanks,
-- Shuah


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/4] selftests: Fix seccomp to support relocatable build (O=objdir)
  2020-03-05 16:41   ` Shuah Khan
@ 2020-03-05 17:42     ` Kees Cook
  2020-03-10 23:11       ` Shuah Khan
  0 siblings, 1 reply; 5+ messages in thread
From: Kees Cook @ 2020-03-05 17:42 UTC (permalink / raw)
  To: Shuah Khan
  Cc: shuah, luto, wad, daniel, kafai, yhs, andriin, gregkh, tglx,
	khilman, mpe, linux-kselftest, linux-kernel, netdev, bpf

On Thu, Mar 05, 2020 at 09:41:34AM -0700, Shuah Khan wrote:
> On 3/4/20 7:20 PM, Kees Cook wrote:
> > Instead of the TEST_CUSTOM_PROGS+all dance, you can just add an explicit
> > dependency, with the final seccomp/Makefile looking like this:
> > 
> > 
> > # SPDX-License-Identifier: GPL-2.0
> > CFLAGS += -Wl,-no-as-needed -Wall
> > LDFLAGS += -lpthread
> > 
> > TEST_GEN_PROGS := seccomp_bpf seccomp_benchmark
> > 
> 
> TEST_CUSTOM_PROGS is for differentiating test programs that
> can't use lib.mk generic rules. It is appropriate to use
> for seccomp_bpf

I don't follow? This suggested Makefile works for me (i.e. it can use
the lib.mk generic rules since CFLAGS and LDFLAGS can be customized
first, and it just adds an additional dependency).

> > include ../lib.mk
> > 
> > # Additional dependencies
> > $(OUTPUT)/seccomp_bpf: ../kselftest_harness.h

BTW, I see a lot of other targets that use kselftest_harness.h appear to
be missing this Makefile dependency, but that's a different problem. :)

> > (Though this fails in the same way as above when run from the top-level
> > directory.)
> > 
> 
> I didn't see this because I have been the same directory I used
> for relocated cross-build kernel. :(
> 
> Thanks for testing this. I know the problem here. all is a dependency
> for install step and $(OUTPUT) is referencing the objdir before it
> gets created. It is a Makefile/lib.mk problem to fix.
> 
> I will do a separate patch for this. This will show up in any test
> that is using $(OUTPUT) to relocate objects mainly the ones that
> require custom build rule like seeccomp.

Okay, cool. It looked to me like it lost track of the top level source
directory (i.e. "make: entering $output" ... "can't find
../other/files")

Anyway, I look forward to the next version and I'll get it tested. :)
Thanks for fixing this! I really like having a top-level "make" command
that can extract a single selftest; that's very handy!

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/4] selftests: Fix seccomp to support relocatable build (O=objdir)
  2020-03-05 17:42     ` Kees Cook
@ 2020-03-10 23:11       ` Shuah Khan
  0 siblings, 0 replies; 5+ messages in thread
From: Shuah Khan @ 2020-03-10 23:11 UTC (permalink / raw)
  To: Kees Cook
  Cc: shuah, luto, wad, daniel, kafai, yhs, andriin, gregkh, tglx,
	khilman, mpe, linux-kselftest, linux-kernel, netdev, bpf,
	skh >> Shuah Khan

On 3/5/20 10:42 AM, Kees Cook wrote:
> On Thu, Mar 05, 2020 at 09:41:34AM -0700, Shuah Khan wrote:
>> On 3/4/20 7:20 PM, Kees Cook wrote:
>>> Instead of the TEST_CUSTOM_PROGS+all dance, you can just add an explicit
>>> dependency, with the final seccomp/Makefile looking like this:
>>>
>>>
>>> # SPDX-License-Identifier: GPL-2.0
>>> CFLAGS += -Wl,-no-as-needed -Wall
>>> LDFLAGS += -lpthread
>>>
>>> TEST_GEN_PROGS := seccomp_bpf seccomp_benchmark
>>>
>>
>> TEST_CUSTOM_PROGS is for differentiating test programs that
>> can't use lib.mk generic rules. It is appropriate to use
>> for seccomp_bpf
> 
> I don't follow? This suggested Makefile works for me (i.e. it can use
> the lib.mk generic rules since CFLAGS and LDFLAGS can be customized
> first, and it just adds an additional dependency).
> 

Yeah. TEST_CUSTOM_PROGS isn't really needed for this custom case.
I can refine it and get rid of the dependency.

>>> include ../lib.mk
>>>
>>> # Additional dependencies
>>> $(OUTPUT)/seccomp_bpf: ../kselftest_harness.h
> 
> BTW, I see a lot of other targets that use kselftest_harness.h appear to
> be missing this Makefile dependency, but that's a different problem. :)
> 
>>> (Though this fails in the same way as above when run from the top-level
>>> directory.)
>>>
>>
>> I didn't see this because I have been the same directory I used
>> for relocated cross-build kernel. :(
>>
>> Thanks for testing this. I know the problem here. all is a dependency
>> for install step and $(OUTPUT) is referencing the objdir before it
>> gets created. It is a Makefile/lib.mk problem to fix.
>>

I was way off with my analysis. :(

>> I will do a separate patch for this. This will show up in any test
>> that is using $(OUTPUT) to relocate objects mainly the ones that
>> require custom build rule like seeccomp.
> 
> Okay, cool. It looked to me like it lost track of the top level source
> directory (i.e. "make: entering $output" ... "can't find
> ../other/files")
> 

Odd that you would have empty objdir in the cross-compile case.

In the cross-compile case, you would have cross-built kernel first in
the object directory. Your objdir won't be empty.

This is no different from kselftest build dependency on kernel build
even when srcdir=objdir

So for cross-build case, the following  is the workflow to build kernel
first and then the tests:

make O=/../objdir ARCH=arm64 HOSTCC=gcc CROSS_COMPILE=aarch64-linux-gnu- 
defconfig

make O=/../objdir ARCH=arm64 HOSTCC=gcc CROSS_COMPILE=aarch64-linux-gnu- all

make kselftest-install O=/../objdir ARCH=arm64 HOSTCC=gcc 
CROSS_COMPILE=aarch64-linux-gnu- TARGETS=seccomp

You can isolate a single test when you are do native build:

make kselftest-install O=/../objdir TARGETS=seccomp

The above won't fail even if objdir doesn't exist and/or empty.

thanks,
-- Shuah








^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-03-10 23:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-05  0:36 [PATCH v2 2/4] selftests: Fix seccomp to support relocatable build (O=objdir) Shuah Khan
2020-03-05  2:20 ` Kees Cook
2020-03-05 16:41   ` Shuah Khan
2020-03-05 17:42     ` Kees Cook
2020-03-10 23:11       ` Shuah Khan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).