bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/16] kbuild: support 'userprogs' syntax
@ 2020-04-23  7:39 Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 01/16] Documentation: kbuild: fix the section title format Masahiro Yamada
                   ` (16 more replies)
  0 siblings, 17 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild
  Cc: bpf, Sam Ravnborg, Masahiro Yamada, Alexei Starovoitov,
	Andrii Nakryiko, Christian Brauner, Daniel Borkmann,
	David S. Miller, Jakub Kicinski, John Fastabend, Jonathan Corbet,
	KP Singh, Martin KaFai Lau, Michal Marek, Song Liu,
	Tomas Winkler, Yonghong Song, linux-doc, linux-kernel, netdev


Several Makefiles use 'hostprogs' for building the code for
the host architecture is not appropriate.

This is just because Kbuild does not provide the syntax to do it.

This series introduce 'userprogs' syntax and use it from
sample and bpf Makefiles.

Sam worked on this in 2014.
https://lkml.org/lkml/2014/7/13/154

He used 'uapiprogs-y' but I just thought the meaning of
"UAPI programs" is unclear.

Naming is one the most difficult parts of this.

I chose 'userprogs'.
Anothor choice I had in my mind was 'targetprogs'.

If you can test this series quickly by
'make allmodconfig samples/'

When building objects for userspace, [U] is displayed.

masahiro@oscar:~/workspace/linux$ make allmodconfig samples/
  [snip]
  AR      samples/vfio-mdev/built-in.a
  CC [M]  samples/vfio-mdev/mtty.o
  CC [M]  samples/vfio-mdev/mdpy.o
  CC [M]  samples/vfio-mdev/mdpy-fb.o
  CC [M]  samples/vfio-mdev/mbochs.o
  AR      samples/mei/built-in.a
  CC [U]  samples/mei/mei-amt-version
  CC [U]  samples/auxdisplay/cfag12864b-example
  CC [M]  samples/configfs/configfs_sample.o
  CC [M]  samples/connector/cn_test.o
  CC [U]  samples/connector/ucon
  CC [M]  samples/ftrace/ftrace-direct.o
  CC [M]  samples/ftrace/ftrace-direct-too.o
  CC [M]  samples/ftrace/ftrace-direct-modify.o
  CC [M]  samples/ftrace/sample-trace-array.o
  CC [U]  samples/hidraw/hid-example
  CC [M]  samples/hw_breakpoint/data_breakpoint.o
  CC [M]  samples/kdb/kdb_hello.o
  CC [M]  samples/kfifo/bytestream-example.o
  CC [M]  samples/kfifo/dma-example.o
  CC [M]  samples/kfifo/inttype-example.o
  CC [M]  samples/kfifo/record-example.o
  CC [M]  samples/kobject/kobject-example.o
  CC [M]  samples/kobject/kset-example.o
  CC [M]  samples/kprobes/kprobe_example.o
  CC [M]  samples/kprobes/kretprobe_example.o
  CC [M]  samples/livepatch/livepatch-sample.o
  CC [M]  samples/livepatch/livepatch-shadow-mod.o
  CC [M]  samples/livepatch/livepatch-shadow-fix1.o
  CC [M]  samples/livepatch/livepatch-shadow-fix2.o
  CC [M]  samples/livepatch/livepatch-callbacks-demo.o
  CC [M]  samples/livepatch/livepatch-callbacks-mod.o
  CC [M]  samples/livepatch/livepatch-callbacks-busymod.o
  CC [M]  samples/rpmsg/rpmsg_client_sample.o
  CC [U]  samples/seccomp/bpf-fancy.o
  CC [U]  samples/seccomp/bpf-helper.o
  LD [U]  samples/seccomp/bpf-fancy
  CC [U]  samples/seccomp/dropper
  CC [U]  samples/seccomp/bpf-direct
  CC [U]  samples/seccomp/user-trap
  CC [U]  samples/timers/hpet_example
  CC [M]  samples/trace_events/trace-events-sample.o
  CC [M]  samples/trace_printk/trace-printk.o
  CC [U]  samples/uhid/uhid-example
  CC [M]  samples/v4l/v4l2-pci-skeleton.o
  CC [U]  samples/vfs/test-fsmount
  CC [U]  samples/vfs/test-statx
samples/vfs/test-statx.c:24:15: warning: ‘struct foo’ declared inside parameter list will not be visible outside of this definition or declaration
   24 | #define statx foo
      |               ^~~
  CC [U]  samples/watchdog/watchdog-simple
  AR      samples/built-in.a



Masahiro Yamada (15):
  Documentation: kbuild: fix the section title format
  Revert "objtool: Skip samples subdirectory"
  kbuild: add infrastructure to build userspace programs
  net: bpfilter: use 'userprogs' syntax to build bpfilter_umh
  samples: seccomp: build sample programs for target architecture
  kbuild: doc: document the new syntax 'userprogs'
  samples: uhid: build sample program for target architecture
  samples: hidraw: build sample program for target architecture
  samples: connector: build sample program for target architecture
  samples: vfs: build sample programs for target architecture
  samples: pidfd: build sample program for target architecture
  samples: mei: build sample program for target architecture
  samples: auxdisplay: use 'userprogs' syntax
  samples: timers: use 'userprogs' syntax
  samples: watchdog: use 'userprogs' syntax

Sam Ravnborg (1):
  samples: uhid: fix warnings in uhid-example

 Documentation/kbuild/makefiles.rst | 185 +++++++++++++++++++++--------
 Makefile                           |  11 +-
 net/bpfilter/Makefile              |  11 +-
 samples/Kconfig                    |  26 +++-
 samples/Makefile                   |   5 +-
 samples/auxdisplay/Makefile        |  11 +-
 samples/connector/Makefile         |  12 +-
 samples/hidraw/Makefile            |   9 +-
 samples/mei/Makefile               |   9 +-
 samples/pidfd/Makefile             |   8 +-
 samples/seccomp/Makefile           |  42 +------
 samples/timers/Makefile            |  17 +--
 samples/uhid/.gitignore            |   2 +
 samples/uhid/Makefile              |   9 +-
 samples/uhid/uhid-example.c        |   4 +-
 samples/vfs/Makefile               |  11 +-
 samples/watchdog/Makefile          |  10 +-
 scripts/Makefile.build             |   5 +
 scripts/Makefile.clean             |   2 +-
 scripts/Makefile.userprogs         |  44 +++++++
 20 files changed, 258 insertions(+), 175 deletions(-)
 create mode 100644 samples/uhid/.gitignore
 create mode 100644 scripts/Makefile.userprogs

-- 
2.25.1


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

* [PATCH 01/16] Documentation: kbuild: fix the section title format
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 02/16] Revert "objtool: Skip samples subdirectory" Masahiro Yamada
                   ` (15 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild
  Cc: bpf, Sam Ravnborg, Masahiro Yamada, Jonathan Corbet,
	Michal Marek, linux-doc, linux-kernel

Make it consistent with the other sections.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Documentation/kbuild/makefiles.rst | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/Documentation/kbuild/makefiles.rst b/Documentation/kbuild/makefiles.rst
index 04d5c01a2e99..b80257a03830 100644
--- a/Documentation/kbuild/makefiles.rst
+++ b/Documentation/kbuild/makefiles.rst
@@ -1241,7 +1241,8 @@ When kbuild executes, the following steps are followed (roughly):
 	will be displayed with "make KBUILD_VERBOSE=0".
 
 
---- 6.9 Preprocessing linker scripts
+6.9 Preprocessing linker scripts
+--------------------------------
 
 	When the vmlinux image is built, the linker script
 	arch/$(ARCH)/kernel/vmlinux.lds is used.
-- 
2.25.1


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

* [PATCH 02/16] Revert "objtool: Skip samples subdirectory"
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 01/16] Documentation: kbuild: fix the section title format Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-24 20:32   ` Josh Poimboeuf
  2020-04-23  7:39 ` [PATCH 03/16] kbuild: add infrastructure to build userspace programs Masahiro Yamada
                   ` (14 subsequent siblings)
  16 siblings, 1 reply; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild
  Cc: bpf, Sam Ravnborg, Masahiro Yamada, Josh Poimboeuf, linux-kernel

This reverts commit 8728497895794d1f207a836e02dae762ad175d56.

This directory contains no object.

Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 samples/Makefile | 1 -
 1 file changed, 1 deletion(-)

diff --git a/samples/Makefile b/samples/Makefile
index f8f847b4f61f..5ce50ef0f2b2 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -1,6 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 # Makefile for Linux samples code
-OBJECT_FILES_NON_STANDARD := y
 
 obj-$(CONFIG_SAMPLE_ANDROID_BINDERFS)	+= binderfs/
 obj-$(CONFIG_SAMPLE_CONFIGFS)		+= configfs/
-- 
2.25.1


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

* [PATCH 03/16] kbuild: add infrastructure to build userspace programs
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 01/16] Documentation: kbuild: fix the section title format Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 02/16] Revert "objtool: Skip samples subdirectory" Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-23 15:15   ` Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 04/16] net: bpfilter: use 'userprogs' syntax to build bpfilter_umh Masahiro Yamada
                   ` (13 subsequent siblings)
  16 siblings, 1 reply; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild
  Cc: bpf, Sam Ravnborg, Masahiro Yamada, Alexei Starovoitov,
	Andrii Nakryiko, Daniel Borkmann, John Fastabend, KP Singh,
	Martin KaFai Lau, Michal Marek, Song Liu, Yonghong Song,
	linux-kernel, netdev

Kbuild supports the infrastructure to build host programs, but there
was no support to build userspace programs for the target architecture
(i.e. the same architecture as the kernel).

Sam Ravnborg worked on this a long time ago.

  https://lkml.org/lkml/2014/7/13/154

But, it was not merged. One problem at that time was, there was no
good way to know whether $(CC) can link standalone programs. In fact,
pre-built kernel.org toolchains [1] do not provide libc.

Now, we can handle this cleanly because the compiler capability is
evaluated at the Kconfig time. If $(CC) cannot link standalone programs,
the relevant options are hidden by 'depends on CC_CAN_LINK'.

The implementation just mimics scripts/Makefile.host

The userspace programs are compiled with the same flags as the host
programs. In addition, it uses -m32 or -m64 if it is found in
$(KBUILD_CFLAGS).

This new syntax has at least two usecases.

- Sample programs

  Several userspace programs under samples/ include UAPI headers
  installed in usr/include. Most of them were previously built for
  the host architecture just to use 'hostprogs' syntax.

  However, 'make headers' always works for the target architecture.
  This caused the arch mismatch in cross-compiling. To fix this
  distortion, sample code should be built for the target architecture.

- Bpfilter

  net/bpfilter/Makefile compiles bpfilter_umh as the user mode helper,
  and embeds it into the kernel code. Currently, it overrides HOSTCC
  with CC to use the 'hostprogs' syntax. This hack should go away.

[1]: https://mirrors.edge.kernel.org/pub/tools/crosstool/
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Makefile                   | 11 +++++++---
 scripts/Makefile.build     |  5 +++++
 scripts/Makefile.clean     |  2 +-
 scripts/Makefile.userprogs | 44 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 58 insertions(+), 4 deletions(-)
 create mode 100644 scripts/Makefile.userprogs

diff --git a/Makefile b/Makefile
index 49b2709ff44e..f20597820131 100644
--- a/Makefile
+++ b/Makefile
@@ -406,9 +406,11 @@ else
 HOSTCC	= gcc
 HOSTCXX	= g++
 endif
-KBUILD_HOSTCFLAGS   := -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 \
-		-fomit-frame-pointer -std=gnu89 $(HOST_LFS_CFLAGS) \
-		$(HOSTCFLAGS)
+
+export KBUILD_USERCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
+			      -O2 -fomit-frame-pointer -std=gnu89
+
+KBUILD_HOSTCFLAGS   := $(KBUILD_USERCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
 KBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS)
 KBUILD_HOSTLDFLAGS  := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
 KBUILD_HOSTLDLIBS   := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
@@ -937,6 +939,9 @@ ifeq ($(CONFIG_RELR),y)
 LDFLAGS_vmlinux	+= --pack-dyn-relocs=relr
 endif
 
+# Align the bit size of userspace programs with the kernel
+KBUILD_USERCFLAGS += $(filter -m32 -m64, $(KBUILD_CFLAGS))
+
 # make the checker run with the right architecture
 CHECKFLAGS += --arch=$(ARCH)
 
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 9fcbfac15d1d..94f2f7016172 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -50,6 +50,11 @@ ifneq ($(hostprogs)$(hostcxxlibs-y)$(hostcxxlibs-m),)
 include scripts/Makefile.host
 endif
 
+# Do not include userprogs rules unless needed
+ifneq ($(userprogs),)
+include scripts/Makefile.userprogs
+endif
+
 ifndef obj
 $(warning kbuild: Makefile.build is included improperly)
 endif
diff --git a/scripts/Makefile.clean b/scripts/Makefile.clean
index 075f0cc2d8d7..e2c76122319d 100644
--- a/scripts/Makefile.clean
+++ b/scripts/Makefile.clean
@@ -29,7 +29,7 @@ subdir-ymn	:= $(addprefix $(obj)/,$(subdir-ymn))
 
 __clean-files	:= $(extra-y) $(extra-m) $(extra-)       \
 		   $(always) $(always-y) $(always-m) $(always-) $(targets) $(clean-files)   \
-		   $(hostprogs) $(hostprogs-y) $(hostprogs-m) $(hostprogs-) \
+		   $(hostprogs) $(hostprogs-y) $(hostprogs-m) $(hostprogs-) $(userprogs) \
 		   $(hostcxxlibs-y) $(hostcxxlibs-m)
 
 __clean-files   := $(filter-out $(no-clean-files), $(__clean-files))
diff --git a/scripts/Makefile.userprogs b/scripts/Makefile.userprogs
new file mode 100644
index 000000000000..0d987085819b
--- /dev/null
+++ b/scripts/Makefile.userprogs
@@ -0,0 +1,44 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Build userspace programs for the target system
+#
+
+userprogs 	:= $(sort $(userprogs))
+
+# Executables compiled from a single .c file
+user-csingle	:= $(foreach m, $(userprogs), $(if $($(m)-objs),,$(m)))
+
+# C executables linked based on several .o files
+user-cmulti	:= $(foreach m, $(userprogs), $(if $($(m)-objs),$(m)))
+
+# Object (.o) files compiled from .c files
+user-cobjs	:= $(foreach m, $(userprogs), $($(m)-objs))
+
+user-csingle	:= $(addprefix $(obj)/, $(user-csingle))
+user-cmulti	:= $(addprefix $(obj)/, $(user-cmulti))
+user-cobjs	:= $(addprefix $(obj)/, $(user-cobjs))
+
+user_c_flags	= -Wp,-MMD,$(depfile) $(KBUILD_USERCFLAGS) $(user-ccflags) \
+			$($(target-stem)-ccflags)
+
+# Create an executable from a single .c file
+quiet_cmd_user_cc_c = CC [U]  $@
+      cmd_user_cc_c = $(CC) $(user_c_flags) -o $@ $<
+$(user-csingle): $(obj)/%: $(src)/%.c FORCE
+	$(call if_changed_dep,user_cc_c)
+
+# Link an executable based on list of .o files
+quiet_cmd_user_ld = LD [U]  $@
+      cmd_user_ld = $(CC) -o $@ $(addprefix $(obj)/, $($(target-stem)-objs)) \
+                      $($(target-stem)-ldlibs)
+$(user-cmulti): FORCE
+	$(call if_changed,user_ld)
+$(call multi_depend, $(user-cmulti), , -objs)
+
+# Create .o file from a .c file
+quiet_cmd_user_cc_o_c = CC [U]  $@
+      cmd_user_cc_o_c = $(CC) $(user_c_flags) -c -o $@ $<
+$(user-cobjs): $(obj)/%.o: $(src)/%.c FORCE
+	$(call if_changed_dep,user_cc_o_c)
+
+targets += $(user-csingle) $(user-cmulti) $(user-cobjs)
-- 
2.25.1


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

* [PATCH 04/16] net: bpfilter: use 'userprogs' syntax to build bpfilter_umh
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (2 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 03/16] kbuild: add infrastructure to build userspace programs Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-28  1:46   ` kbuild test robot
  2020-06-08 11:56   ` Michal Kubecek
  2020-04-23  7:39 ` [PATCH 05/16] samples: seccomp: build sample programs for target architecture Masahiro Yamada
                   ` (12 subsequent siblings)
  16 siblings, 2 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild
  Cc: bpf, Sam Ravnborg, Masahiro Yamada, Alexei Starovoitov,
	Andrii Nakryiko, Daniel Borkmann, David S. Miller,
	Jakub Kicinski, John Fastabend, KP Singh, Martin KaFai Lau,
	Song Liu, Yonghong Song, linux-kernel, netdev

The user mode helper should be compiled for the same architecture as
the kernel.

This Makefile reuses the 'hostprogs' syntax by overriding HOSTCC with CC.

Now that Kbuild provides the syntax 'userprogs', use it to fix the
Makefile mess.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 net/bpfilter/Makefile | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/net/bpfilter/Makefile b/net/bpfilter/Makefile
index 36580301da70..6ee650c6badb 100644
--- a/net/bpfilter/Makefile
+++ b/net/bpfilter/Makefile
@@ -3,17 +3,14 @@
 # Makefile for the Linux BPFILTER layer.
 #
 
-hostprogs := bpfilter_umh
+userprogs := bpfilter_umh
 bpfilter_umh-objs := main.o
-KBUILD_HOSTCFLAGS += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi
-HOSTCC := $(CC)
+user-ccflags += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi
 
-ifeq ($(CONFIG_BPFILTER_UMH), y)
-# builtin bpfilter_umh should be compiled with -static
+# builtin bpfilter_umh should be linked with -static
 # since rootfs isn't mounted at the time of __init
 # function is called and do_execv won't find elf interpreter
-KBUILD_HOSTLDFLAGS += -static
-endif
+bpfilter_umh-ldflags += -static
 
 $(obj)/bpfilter_umh_blob.o: $(obj)/bpfilter_umh
 
-- 
2.25.1


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

* [PATCH 05/16] samples: seccomp: build sample programs for target architecture
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (3 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 04/16] net: bpfilter: use 'userprogs' syntax to build bpfilter_umh Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 06/16] kbuild: doc: document the new syntax 'userprogs' Masahiro Yamada
                   ` (11 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild
  Cc: bpf, Sam Ravnborg, Masahiro Yamada, Alexei Starovoitov,
	Andrii Nakryiko, Daniel Borkmann, John Fastabend, KP Singh,
	Martin KaFai Lau, Song Liu, Yonghong Song, linux-kernel, netdev

These userspace programs include UAPI headers exported to usr/include/.
'make headers' always works for the target architecture (i.e. the same
architecture as the kernel), so the sample programs must be built for
the target as well. Kbuild now supports the 'userprogs' syntax to
describe it cleanly.

I also guarded the CONFIG option by 'depends on CC_CAN_LINK' because
$(CC) may not necessarily provide libc.

The 'ifndef CROSS_COMPILE' is no longer needed.

BTW, the -m31 for s390 is left-over code. Commit 5a79859ae0f3 ("s390:
remove 31 bit support") killed it.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 samples/Kconfig          |  2 +-
 samples/seccomp/Makefile | 42 +++-------------------------------------
 2 files changed, 4 insertions(+), 40 deletions(-)

diff --git a/samples/Kconfig b/samples/Kconfig
index 9d236c346de5..8949e9646125 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -126,7 +126,7 @@ config SAMPLE_PIDFD
 
 config SAMPLE_SECCOMP
 	bool "Build seccomp sample code"
-	depends on SECCOMP_FILTER && HEADERS_INSTALL
+	depends on SECCOMP_FILTER && CC_CAN_LINK && HEADERS_INSTALL
 	help
 	  Build samples of seccomp filters using various methods of
 	  BPF filter construction.
diff --git a/samples/seccomp/Makefile b/samples/seccomp/Makefile
index 89279e8b87df..19a7b1125ddf 100644
--- a/samples/seccomp/Makefile
+++ b/samples/seccomp/Makefile
@@ -1,44 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
-ifndef CROSS_COMPILE
-hostprogs := bpf-fancy dropper bpf-direct user-trap
+userprogs := bpf-fancy dropper bpf-direct user-trap
 
-HOSTCFLAGS_bpf-fancy.o += -I$(objtree)/usr/include
-HOSTCFLAGS_bpf-fancy.o += -idirafter $(objtree)/include
-HOSTCFLAGS_bpf-helper.o += -I$(objtree)/usr/include
-HOSTCFLAGS_bpf-helper.o += -idirafter $(objtree)/include
 bpf-fancy-objs := bpf-fancy.o bpf-helper.o
 
-HOSTCFLAGS_dropper.o += -I$(objtree)/usr/include
-HOSTCFLAGS_dropper.o += -idirafter $(objtree)/include
-dropper-objs := dropper.o
+user-ccflags += -I usr/include
 
-HOSTCFLAGS_bpf-direct.o += -I$(objtree)/usr/include
-HOSTCFLAGS_bpf-direct.o += -idirafter $(objtree)/include
-bpf-direct-objs := bpf-direct.o
-
-HOSTCFLAGS_user-trap.o += -I$(objtree)/usr/include
-HOSTCFLAGS_user-trap.o += -idirafter $(objtree)/include
-user-trap-objs := user-trap.o
-
-# Try to match the kernel target.
-ifndef CONFIG_64BIT
-
-# s390 has -m31 flag to build 31 bit binaries
-ifndef CONFIG_S390
-MFLAG = -m32
-else
-MFLAG = -m31
-endif
-
-HOSTCFLAGS_bpf-direct.o += $(MFLAG)
-HOSTCFLAGS_dropper.o += $(MFLAG)
-HOSTCFLAGS_bpf-helper.o += $(MFLAG)
-HOSTCFLAGS_bpf-fancy.o += $(MFLAG)
-HOSTCFLAGS_user-trap.o += $(MFLAG)
-HOSTLDLIBS_bpf-direct += $(MFLAG)
-HOSTLDLIBS_bpf-fancy += $(MFLAG)
-HOSTLDLIBS_dropper += $(MFLAG)
-HOSTLDLIBS_user-trap += $(MFLAG)
-endif
-always-y := $(hostprogs)
-endif
+always-y := $(userprogs)
-- 
2.25.1


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

* [PATCH 06/16] kbuild: doc: document the new syntax 'userprogs'
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (4 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 05/16] samples: seccomp: build sample programs for target architecture Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 07/16] samples: uhid: fix warnings in uhid-example Masahiro Yamada
                   ` (10 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild
  Cc: bpf, Sam Ravnborg, Masahiro Yamada, Alexei Starovoitov,
	Andrii Nakryiko, Daniel Borkmann, John Fastabend,
	Jonathan Corbet, KP Singh, Martin KaFai Lau, Michal Marek,
	Song Liu, Yonghong Song, linux-doc, linux-kernel, netdev

Kbuild now supports the syntax 'userprogs' to compile userspace
programs for the same architecture as the kernel.

Insert the section '5 Userspace Program support' to explain it.

I copy-pasted '4 Host Program support' and fixed it up.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Documentation/kbuild/makefiles.rst | 184 +++++++++++++++++++++--------
 1 file changed, 136 insertions(+), 48 deletions(-)

diff --git a/Documentation/kbuild/makefiles.rst b/Documentation/kbuild/makefiles.rst
index b80257a03830..251e5431276e 100644
--- a/Documentation/kbuild/makefiles.rst
+++ b/Documentation/kbuild/makefiles.rst
@@ -29,31 +29,37 @@ This document describes the Linux kernel Makefiles.
 	   --- 4.4 Controlling compiler options for host programs
 	   --- 4.5 When host programs are actually built
 
-	=== 5 Kbuild clean infrastructure
-
-	=== 6 Architecture Makefiles
-	   --- 6.1 Set variables to tweak the build to the architecture
-	   --- 6.2 Add prerequisites to archheaders:
-	   --- 6.3 Add prerequisites to archprepare:
-	   --- 6.4 List directories to visit when descending
-	   --- 6.5 Architecture-specific boot images
-	   --- 6.6 Building non-kbuild targets
-	   --- 6.7 Commands useful for building a boot image
-	   --- 6.8 Custom kbuild commands
-	   --- 6.9 Preprocessing linker scripts
-	   --- 6.10 Generic header files
-	   --- 6.11 Post-link pass
-
-	=== 7 Kbuild syntax for exported headers
-		--- 7.1 no-export-headers
-		--- 7.2 generic-y
-		--- 7.3 generated-y
-		--- 7.4 mandatory-y
-
-	=== 8 Kbuild Variables
-	=== 9 Makefile language
-	=== 10 Credits
-	=== 11 TODO
+	=== 5 Userspace Program support
+	   --- 5.1 Simple Userspace Program
+	   --- 5.2 Composite Userspace Programs
+	   --- 5.3 Controlling compiler options for userspace programs
+	   --- 5.4 When userspace programs are actually built
+
+	=== 6 Kbuild clean infrastructure
+
+	=== 7 Architecture Makefiles
+	   --- 7.1 Set variables to tweak the build to the architecture
+	   --- 7.2 Add prerequisites to archheaders:
+	   --- 7.3 Add prerequisites to archprepare:
+	   --- 7.4 List directories to visit when descending
+	   --- 7.5 Architecture-specific boot images
+	   --- 7.6 Building non-kbuild targets
+	   --- 7.7 Commands useful for building a boot image
+	   --- 7.8 Custom kbuild commands
+	   --- 7.9 Preprocessing linker scripts
+	   --- 7.10 Generic header files
+	   --- 7.11 Post-link pass
+
+	=== 8 Kbuild syntax for exported headers
+		--- 8.1 no-export-headers
+		--- 8.2 generic-y
+		--- 8.3 generated-y
+		--- 8.4 mandatory-y
+
+	=== 9 Kbuild Variables
+	=== 10 Makefile language
+	=== 11 Credits
+	=== 12 TODO
 
 1 Overview
 ==========
@@ -732,7 +738,89 @@ Both possibilities are described in the following.
 	This will tell kbuild to build lxdialog even if not referenced in
 	any rule.
 
-5 Kbuild clean infrastructure
+5 Userspace Program support
+===========================
+
+Just like host programs, Kbuild also supports building userspace executables
+for the target architecture (i.e. the same architecture as you are building
+the kernel for).
+
+The syntax is quite similar. The difference is to use "userprogs" instead of
+"hostprogs".
+
+5.1 Simple Userspace Program
+----------------------------
+
+	The following line tells kbuild that the program bpf-direct shall be
+	built for the target architecture.
+
+	Example::
+
+		userprogs := bpf-direct
+
+	Kbuild assumes in the above example that bpf-direct is made from a
+	single C source file named bpf-direct.c located in the same directory
+	as the Makefile.
+
+5.2 Composite Userspace Programs
+--------------------------------
+
+	Userspace programs can be made up based on composite objects.
+	The syntax used to define composite objects for userspace programs is
+	similar to the syntax used for kernel objects.
+	$(<executable>-objs) lists all objects used to link the final
+	executable.
+
+	Example::
+
+		#samples/seccomp/Makefile
+		userprogs      := bpf-fancy
+		bpf-fancy-objs := bpf-fancy.o bpf-helper.o
+
+	Objects with extension .o are compiled from the corresponding .c
+	files. In the above example, bpf-fancy.c is compiled to bpf-fancy.o
+	and bpf-helper.c is compiled to bpf-helper.o.
+
+	Finally, the two .o files are linked to the executable, bpf-fancy.
+	Note: The syntax <executable>-y is not permitted for userspace programs.
+
+5.3 Controlling compiler options for userspace programs
+-------------------------------------------------------
+
+	When compiling userspace programs, it is possible to set specific flags.
+	The programs will always be compiled utilising $(CC) passed
+	the options specified in $(KBUILD_USERCFLAGS).
+	To set flags that will take effect for all userspace programs created
+	in that Makefile, use the variable user-ccflags.
+
+	Example::
+
+		# samples/seccomp/Makefile
+		user-ccflags += -I usr/include
+
+	To set specific flags for a single file the following construction
+	is used:
+
+	Example::
+
+		bpf-helper-ccflags += -I user/include
+
+	It is also possible to specify additional options to the linker.
+
+	Example::
+
+		# net/bpfilter/Makefile
+		bpfilter_umh-ldflags += -static
+
+	When linking bpfilter_umh-ldflags, it will be passed the extra option
+	-static.
+
+5.4 When userspace programs are actually built
+----------------------------------------------
+
+	Same as "When host programs are actually built".
+
+6 Kbuild clean infrastructure
 =============================
 
 "make clean" deletes most generated files in the obj tree where the kernel
@@ -790,7 +878,7 @@ is not operational at that point.
 Note 2: All directories listed in core-y, libs-y, drivers-y and net-y will
 be visited during "make clean".
 
-6 Architecture Makefiles
+7 Architecture Makefiles
 ========================
 
 The top level Makefile sets up the environment and does the preparation,
@@ -820,7 +908,7 @@ When kbuild executes, the following steps are followed (roughly):
    - Preparing initrd images and the like
 
 
-6.1 Set variables to tweak the build to the architecture
+7.1 Set variables to tweak the build to the architecture
 --------------------------------------------------------
 
     LDFLAGS
@@ -967,7 +1055,7 @@ When kbuild executes, the following steps are followed (roughly):
 	KBUILD_VMLINUX_LIBS together specify all the object files used to
 	link vmlinux.
 
-6.2 Add prerequisites to archheaders
+7.2 Add prerequisites to archheaders
 ------------------------------------
 
 	The archheaders: rule is used to generate header files that
@@ -977,7 +1065,7 @@ When kbuild executes, the following steps are followed (roughly):
 	architecture itself.
 
 
-6.3 Add prerequisites to archprepare
+7.3 Add prerequisites to archprepare
 ------------------------------------
 
 	The archprepare: rule is used to list prerequisites that need to be
@@ -995,7 +1083,7 @@ When kbuild executes, the following steps are followed (roughly):
 	generating offset header files.
 
 
-6.4 List directories to visit when descending
+7.4 List directories to visit when descending
 ---------------------------------------------
 
 	An arch Makefile cooperates with the top Makefile to define variables
@@ -1030,7 +1118,7 @@ When kbuild executes, the following steps are followed (roughly):
 		drivers-$(CONFIG_OPROFILE)  += arch/sparc64/oprofile/
 
 
-6.5 Architecture-specific boot images
+7.5 Architecture-specific boot images
 -------------------------------------
 
 	An arch Makefile specifies goals that take the vmlinux file, compress
@@ -1085,7 +1173,7 @@ When kbuild executes, the following steps are followed (roughly):
 
 	When "make" is executed without arguments, bzImage will be built.
 
-6.6 Building non-kbuild targets
+7.6 Building non-kbuild targets
 -------------------------------
 
     extra-y
@@ -1108,7 +1196,7 @@ When kbuild executes, the following steps are followed (roughly):
 	In this example, extra-y is used to list object files that
 	shall be built, but shall not be linked as part of built-in.a.
 
-6.7 Commands useful for building a boot image
+7.7 Commands useful for building a boot image
 ---------------------------------------------
 
     Kbuild provides a few macros that are useful when building a
@@ -1211,7 +1299,7 @@ When kbuild executes, the following steps are followed (roughly):
 		targets += $(dtb-y)
 		DTC_FLAGS ?= -p 1024
 
-6.8 Custom kbuild commands
+7.8 Custom kbuild commands
 --------------------------
 
 	When kbuild is executing with KBUILD_VERBOSE=0, then only a shorthand
@@ -1241,7 +1329,7 @@ When kbuild executes, the following steps are followed (roughly):
 	will be displayed with "make KBUILD_VERBOSE=0".
 
 
-6.9 Preprocessing linker scripts
+7.9 Preprocessing linker scripts
 --------------------------------
 
 	When the vmlinux image is built, the linker script
@@ -1274,7 +1362,7 @@ When kbuild executes, the following steps are followed (roughly):
 	The kbuild infrastructure for `*lds` files is used in several
 	architecture-specific files.
 
-6.10 Generic header files
+7.10 Generic header files
 -------------------------
 
 	The directory include/asm-generic contains the header files
@@ -1283,7 +1371,7 @@ When kbuild executes, the following steps are followed (roughly):
 	to list the file in the Kbuild file.
 	See "7.2 generic-y" for further info on syntax etc.
 
-6.11 Post-link pass
+7.11 Post-link pass
 -------------------
 
 	If the file arch/xxx/Makefile.postlink exists, this makefile
@@ -1299,7 +1387,7 @@ When kbuild executes, the following steps are followed (roughly):
 	For example, powerpc uses this to check relocation sanity of
 	the linked vmlinux file.
 
-7 Kbuild syntax for exported headers
+8 Kbuild syntax for exported headers
 ------------------------------------
 
 The kernel includes a set of headers that is exported to userspace.
@@ -1319,14 +1407,14 @@ A Kbuild file may be defined under arch/<arch>/include/uapi/asm/ and
 arch/<arch>/include/asm/ to list asm files coming from asm-generic.
 See subsequent chapter for the syntax of the Kbuild file.
 
-7.1 no-export-headers
+8.1 no-export-headers
 ---------------------
 
 	no-export-headers is essentially used by include/uapi/linux/Kbuild to
 	avoid exporting specific headers (e.g. kvm.h) on architectures that do
 	not support it. It should be avoided as much as possible.
 
-7.2 generic-y
+8.2 generic-y
 -------------
 
 	If an architecture uses a verbatim copy of a header from
@@ -1356,7 +1444,7 @@ See subsequent chapter for the syntax of the Kbuild file.
 
 			#include <asm-generic/termios.h>
 
-7.3 generated-y
+8.3 generated-y
 ---------------
 
 	If an architecture generates other header files alongside generic-y
@@ -1370,7 +1458,7 @@ See subsequent chapter for the syntax of the Kbuild file.
 			#arch/x86/include/asm/Kbuild
 			generated-y += syscalls_32.h
 
-7.4 mandatory-y
+8.4 mandatory-y
 ---------------
 
 	mandatory-y is essentially used by include/(uapi/)asm-generic/Kbuild
@@ -1380,7 +1468,7 @@ See subsequent chapter for the syntax of the Kbuild file.
 	in arch/$(ARCH)/include/(uapi/)/asm, Kbuild will automatically generate
 	a wrapper of the asm-generic one.
 
-8 Kbuild Variables
+9 Kbuild Variables
 ==================
 
 The top Makefile exports the following variables:
@@ -1438,8 +1526,8 @@ The top Makefile exports the following variables:
 	command.
 
 
-9 Makefile language
-===================
+10 Makefile language
+====================
 
 The kernel Makefiles are designed to be run with GNU Make.  The Makefiles
 use only the documented features of GNU Make, but they do use many
@@ -1458,7 +1546,7 @@ time the left-hand side is used.
 There are some cases where "=" is appropriate.  Usually, though, ":="
 is the right choice.
 
-10 Credits
+11 Credits
 ==========
 
 - Original version made by Michael Elizabeth Chastain, <mailto:mec@shout.net>
@@ -1466,7 +1554,7 @@ is the right choice.
 - Updates by Sam Ravnborg <sam@ravnborg.org>
 - Language QA by Jan Engelhardt <jengelh@gmx.de>
 
-11 TODO
+12 TODO
 =======
 
 - Describe how kbuild supports shipped files with _shipped.
-- 
2.25.1


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

* [PATCH 07/16] samples: uhid: fix warnings in uhid-example
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (5 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 06/16] kbuild: doc: document the new syntax 'userprogs' Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 08/16] samples: uhid: build sample program for target architecture Masahiro Yamada
                   ` (9 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild; +Cc: bpf, Sam Ravnborg, Masahiro Yamada, linux-kernel

From: Sam Ravnborg <sam@ravnborg.org>

Fix warnings seen when building for 32-bit architecture.

Use "%xd" for arguments of type size_t to fix the warnings.

Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

This is the same as Sam's patch in 2014.
https://lkml.org/lkml/2014/7/13/152


 samples/uhid/uhid-example.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/samples/uhid/uhid-example.c b/samples/uhid/uhid-example.c
index b72d645ce828..015cb06a241e 100644
--- a/samples/uhid/uhid-example.c
+++ b/samples/uhid/uhid-example.c
@@ -165,7 +165,7 @@ static int uhid_write(int fd, const struct uhid_event *ev)
 		fprintf(stderr, "Cannot write to uhid: %m\n");
 		return -errno;
 	} else if (ret != sizeof(*ev)) {
-		fprintf(stderr, "Wrong size written to uhid: %ld != %lu\n",
+		fprintf(stderr, "Wrong size written to uhid: %zd != %zu\n",
 			ret, sizeof(ev));
 		return -EFAULT;
 	} else {
@@ -236,7 +236,7 @@ static int event(int fd)
 		fprintf(stderr, "Cannot read uhid-cdev: %m\n");
 		return -errno;
 	} else if (ret != sizeof(ev)) {
-		fprintf(stderr, "Invalid size read from uhid-dev: %ld != %lu\n",
+		fprintf(stderr, "Invalid size read from uhid-dev: %zd != %zu\n",
 			ret, sizeof(ev));
 		return -EFAULT;
 	}
-- 
2.25.1


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

* [PATCH 08/16] samples: uhid: build sample program for target architecture
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (6 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 07/16] samples: uhid: fix warnings in uhid-example Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 09/16] samples: hidraw: " Masahiro Yamada
                   ` (8 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild; +Cc: bpf, Sam Ravnborg, Masahiro Yamada, linux-kernel

This userspace program includes UAPI headers exported to usr/include/.
'make headers' always works for the target architecture (i.e. the same
architecture as the kernel), so the sample program must be built for
the target as well. Kbuild now supports the 'userprogs' syntax to
describe it cleanly.

Add the entry to samples/Makefile to put this into the build bot
coverage.

I also added the CONFIG option guarded by 'depends on CC_CAN_LINK'
because $(CC) may not necessarily provide libc.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 samples/Kconfig         | 6 ++++++
 samples/Makefile        | 1 +
 samples/uhid/.gitignore | 2 ++
 samples/uhid/Makefile   | 9 +++------
 4 files changed, 12 insertions(+), 6 deletions(-)
 create mode 100644 samples/uhid/.gitignore

diff --git a/samples/Kconfig b/samples/Kconfig
index 8949e9646125..ff9126ef1c79 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -131,6 +131,12 @@ config SAMPLE_SECCOMP
 	  Build samples of seccomp filters using various methods of
 	  BPF filter construction.
 
+config SAMPLE_UHID
+	bool "Build UHID sample code"
+	depends on CC_CAN_LINK && HEADERS_INSTALL
+	help
+	  Build UHID sample program.
+
 config SAMPLE_VFIO_MDEV_MTTY
 	tristate "Build VFIO mtty example mediated device sample code -- loadable modules only"
 	depends on VFIO_MDEV_DEVICE && m
diff --git a/samples/Makefile b/samples/Makefile
index 5ce50ef0f2b2..bdc168405452 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -19,6 +19,7 @@ obj-$(CONFIG_SAMPLE_TRACE_EVENTS)	+= trace_events/
 obj-$(CONFIG_SAMPLE_TRACE_PRINTK)	+= trace_printk/
 obj-$(CONFIG_SAMPLE_FTRACE_DIRECT)	+= ftrace/
 obj-$(CONFIG_SAMPLE_TRACE_ARRAY)	+= ftrace/
+subdir-$(CONFIG_SAMPLE_UHID)		+= uhid
 obj-$(CONFIG_VIDEO_PCI_SKELETON)	+= v4l/
 obj-y					+= vfio-mdev/
 subdir-$(CONFIG_SAMPLE_VFS)		+= vfs
diff --git a/samples/uhid/.gitignore b/samples/uhid/.gitignore
new file mode 100644
index 000000000000..0e0a5a929f5d
--- /dev/null
+++ b/samples/uhid/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+/uhid-example
diff --git a/samples/uhid/Makefile b/samples/uhid/Makefile
index 5f44ea40d6d5..1dc58c40298c 100644
--- a/samples/uhid/Makefile
+++ b/samples/uhid/Makefile
@@ -1,8 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
-# List of programs to build
-hostprogs := uhid-example
+userprogs := uhid-example
+always-y := $(userprogs)
 
-# Tell kbuild to always build the programs
-always-y := $(hostprogs)
-
-HOSTCFLAGS_uhid-example.o += -I$(objtree)/usr/include
+user-ccflags += -I usr/include
-- 
2.25.1


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

* [PATCH 09/16] samples: hidraw: build sample program for target architecture
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (7 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 08/16] samples: uhid: build sample program for target architecture Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 10/16] samples: connector: " Masahiro Yamada
                   ` (7 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild; +Cc: bpf, Sam Ravnborg, Masahiro Yamada, linux-kernel

This userspace program includes UAPI headers exported to usr/include/.
'make headers' always works for the target architecture (i.e. the same
architecture as the kernel), so the sample program must be built for
the target as well. Kbuild now supports the 'userprogs' syntax to
describe it cleanly.

I also guarded the CONFIG option by 'depends on CC_CAN_LINK' because
$(CC) may not necessarily provide libc.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 samples/Kconfig         | 2 +-
 samples/hidraw/Makefile | 9 +++------
 2 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/samples/Kconfig b/samples/Kconfig
index ff9126ef1c79..2e0ef2cc1aa8 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -118,7 +118,7 @@ config SAMPLE_CONNECTOR
 
 config SAMPLE_HIDRAW
 	bool "hidraw sample"
-	depends on HEADERS_INSTALL
+	depends on CC_CAN_LINK && HEADERS_INSTALL
 
 config SAMPLE_PIDFD
 	bool "pidfd sample"
diff --git a/samples/hidraw/Makefile b/samples/hidraw/Makefile
index 8bd25f77671f..752ed03cdb64 100644
--- a/samples/hidraw/Makefile
+++ b/samples/hidraw/Makefile
@@ -1,8 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
-# List of programs to build
-hostprogs := hid-example
-always-y := $(hostprogs)
+userprogs := hid-example
+always-y := $(userprogs)
 
-HOSTCFLAGS_hid-example.o += -I$(objtree)/usr/include
-
-all: hid-example
+user-ccflags += -I usr/include
-- 
2.25.1


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

* [PATCH 10/16] samples: connector: build sample program for target architecture
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (8 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 09/16] samples: hidraw: " Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 11/16] samples: vfs: build sample programs " Masahiro Yamada
                   ` (6 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild; +Cc: bpf, Sam Ravnborg, Masahiro Yamada, linux-kernel

This userspace program includes UAPI headers exported to usr/include/.
'make headers' always works for the target architecture (i.e. the same
architecture as the kernel), so the sample program must be built for
the target as well. Kbuild now supports the 'userprogs' syntax to
describe it cleanly.

$(CC) can always compile cn_text.o since it is the kenrel-space code,
but building ucon requires libc.

I guarded it by:

  always-$(CONFIG_CC_CAN_LINK) := $(userprogs)

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 samples/connector/Makefile | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/samples/connector/Makefile b/samples/connector/Makefile
index b785cbde5ffa..7b5117e96fd0 100644
--- a/samples/connector/Makefile
+++ b/samples/connector/Makefile
@@ -1,13 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_SAMPLE_CONNECTOR) += cn_test.o
 
-# List of programs to build
-hostprogs := ucon
-always-y := $(hostprogs)
+userprogs := ucon
+always-$(CONFIG_CC_CAN_LINK) := $(userprogs)
 
-HOSTCFLAGS_ucon.o += -I$(objtree)/usr/include
-
-all: modules
-
-modules clean:
-	$(MAKE) -C ../.. M=$(CURDIR) $@
+user-ccflags += -I usr/include
-- 
2.25.1


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

* [PATCH 11/16] samples: vfs: build sample programs for target architecture
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (9 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 10/16] samples: connector: " Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 12/16] samples: pidfd: build sample program " Masahiro Yamada
                   ` (5 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild; +Cc: bpf, Sam Ravnborg, Masahiro Yamada, linux-kernel

These userspace programs include UAPI headers exported to usr/include/.
'make headers' always works for the target architecture (i.e. the same
architecture as the kernel), so the sample programs must be built for
the target as well. Kbuild now supports the 'userprogs' syntax to
describe it cleanly.

I also guarded the CONFIG option by 'depends on CC_CAN_LINK' because
$(CC) may not necessarily provide libc.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 samples/Kconfig      |  2 +-
 samples/vfs/Makefile | 11 +++--------
 2 files changed, 4 insertions(+), 9 deletions(-)

diff --git a/samples/Kconfig b/samples/Kconfig
index 2e0ef2cc1aa8..b8beb6fd75b4 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -184,7 +184,7 @@ config SAMPLE_ANDROID_BINDERFS
 
 config SAMPLE_VFS
 	bool "Build example programs that use new VFS system calls"
-	depends on HEADERS_INSTALL
+	depends on CC_CAN_LINK && HEADERS_INSTALL
 	help
 	  Build example userspace programs that use new VFS system calls such
 	  as mount API and statx().  Note that this is restricted to the x86
diff --git a/samples/vfs/Makefile b/samples/vfs/Makefile
index 65acdde5c117..f3831466c1bb 100644
--- a/samples/vfs/Makefile
+++ b/samples/vfs/Makefile
@@ -1,10 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
-# List of programs to build
-hostprogs := \
-	test-fsmount \
-	test-statx
+userprogs := test-fsmount test-statx
+always-y := $(userprogs)
 
-always-y := $(hostprogs)
-
-HOSTCFLAGS_test-fsmount.o += -I$(objtree)/usr/include
-HOSTCFLAGS_test-statx.o += -I$(objtree)/usr/include
+user-ccflags += -I usr/include
-- 
2.25.1


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

* [PATCH 12/16] samples: pidfd: build sample program for target architecture
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (10 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 11/16] samples: vfs: build sample programs " Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 13/16] samples: mei: " Masahiro Yamada
                   ` (4 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild
  Cc: bpf, Sam Ravnborg, Masahiro Yamada, Christian Brauner, linux-kernel

This userspace program includes UAPI headers exported to usr/include/.
'make headers' always works for the target architecture (i.e. the same
architecture as the kernel), so the sample program must be built for
the target as well. Kbuild now supports the 'userprogs' syntax to
describe it cleanly.

I also guarded the CONFIG option by 'depends on CC_CAN_LINK' because
$(CC) may not necessarily provide libc.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 samples/Kconfig        | 2 +-
 samples/pidfd/Makefile | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/samples/Kconfig b/samples/Kconfig
index b8beb6fd75b4..b8113555aa6c 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -122,7 +122,7 @@ config SAMPLE_HIDRAW
 
 config SAMPLE_PIDFD
 	bool "pidfd sample"
-	depends on HEADERS_INSTALL
+	depends on CC_CAN_LINK && HEADERS_INSTALL
 
 config SAMPLE_SECCOMP
 	bool "Build seccomp sample code"
diff --git a/samples/pidfd/Makefile b/samples/pidfd/Makefile
index ee2979849d92..0191902647ca 100644
--- a/samples/pidfd/Makefile
+++ b/samples/pidfd/Makefile
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 
-hostprogs := pidfd-metadata
-always-y := $(hostprogs)
-HOSTCFLAGS_pidfd-metadata.o += -I$(objtree)/usr/include
-all: pidfd-metadata
+usertprogs := pidfd-metadata
+always-y := $(userprogs)
+
+user-ccflags += -I usr/include
-- 
2.25.1


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

* [PATCH 13/16] samples: mei: build sample program for target architecture
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (11 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 12/16] samples: pidfd: build sample program " Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 14/16] samples: auxdisplay: use 'userprogs' syntax Masahiro Yamada
                   ` (3 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild
  Cc: bpf, Sam Ravnborg, Masahiro Yamada, Tomas Winkler, linux-kernel

This userspace program includes UAPI headers exported to usr/include/.
'make headers' always works for the target architecture (i.e. the same
architecture as the kernel), so the sample program must be built for
the target as well. Kbuild now supports the 'userprogs' syntax to
describe it cleanly.

I also guarded the CONFIG option by 'depends on CC_CAN_LINK' because
$(CC) may not necessarily provide libc.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 samples/Kconfig      | 1 +
 samples/mei/Makefile | 9 +++------
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/samples/Kconfig b/samples/Kconfig
index b8113555aa6c..b9dafee5d3af 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -193,6 +193,7 @@ config SAMPLE_VFS
 config SAMPLE_INTEL_MEI
 	bool "Build example program working with intel mei driver"
 	depends on INTEL_MEI
+	depends on CC_CAN_LINK && HEADERS_INSTALL
 	help
 	  Build a sample program to work with mei device.
 
diff --git a/samples/mei/Makefile b/samples/mei/Makefile
index f5b9d02be2cd..ced5532cbbee 100644
--- a/samples/mei/Makefile
+++ b/samples/mei/Makefile
@@ -1,10 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 # Copyright (c) 2012-2019, Intel Corporation. All rights reserved.
 
-hostprogs := mei-amt-version
+userprogs := mei-amt-version
+always-y := $(userprogs)
 
-HOSTCFLAGS_mei-amt-version.o += -I$(objtree)/usr/include
-
-always-y := $(hostprogs)
-
-all: mei-amt-version
+user-ccflags += -I usr/include
-- 
2.25.1


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

* [PATCH 14/16] samples: auxdisplay: use 'userprogs' syntax
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (12 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 13/16] samples: mei: " Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-23 11:50   ` Miguel Ojeda
  2020-04-23  7:39 ` [PATCH 15/16] samples: timers: " Masahiro Yamada
                   ` (2 subsequent siblings)
  16 siblings, 1 reply; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild; +Cc: bpf, Sam Ravnborg, Masahiro Yamada, linux-kernel

Kbuild now supports the 'userprogs' syntax to describe the build rules
of userspace programs for the target architecture (i.e. the same
architecture as the kernel).

Add the entry to samples/Makefile to put this into the build bot
coverage.

I also added the CONFIG option guarded by 'depends on CC_CAN_LINK'
because $(CC) may not necessarily provide libc.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 samples/Kconfig             |  4 ++++
 samples/Makefile            |  1 +
 samples/auxdisplay/Makefile | 11 ++---------
 3 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/samples/Kconfig b/samples/Kconfig
index b9dafee5d3af..cdb0091e4734 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -6,6 +6,10 @@ menuconfig SAMPLES
 
 if SAMPLES
 
+config SAMPLE_AUXDISPLAY
+	bool "auxdisplay sample"
+	depends on CC_CAN_LINK
+
 config SAMPLE_TRACE_EVENTS
 	tristate "Build trace_events examples -- loadable modules only"
 	depends on EVENT_TRACING && m
diff --git a/samples/Makefile b/samples/Makefile
index bdc168405452..0c43b5d34b15 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 # Makefile for Linux samples code
 
+subdir-$(CONFIG_SAMPLE_AUXDISPLAY)	+= auxdisplay
 obj-$(CONFIG_SAMPLE_ANDROID_BINDERFS)	+= binderfs/
 obj-$(CONFIG_SAMPLE_CONFIGFS)		+= configfs/
 obj-$(CONFIG_SAMPLE_CONNECTOR)		+= connector/
diff --git a/samples/auxdisplay/Makefile b/samples/auxdisplay/Makefile
index 0273bab27233..dbdf939af94a 100644
--- a/samples/auxdisplay/Makefile
+++ b/samples/auxdisplay/Makefile
@@ -1,10 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0
-CC := $(CROSS_COMPILE)gcc
-CFLAGS := -I../../usr/include
-
-PROGS := cfag12864b-example
-
-all: $(PROGS)
-
-clean:
-	rm -fr $(PROGS)
+userprogs := cfag12864b-example
+always-y := $(userprogs)
-- 
2.25.1


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

* [PATCH 15/16] samples: timers: use 'userprogs' syntax
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (13 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 14/16] samples: auxdisplay: use 'userprogs' syntax Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-23  7:39 ` [PATCH 16/16] samples: watchdog: " Masahiro Yamada
  2020-04-25 11:53 ` [PATCH 00/16] kbuild: support " Sam Ravnborg
  16 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild; +Cc: bpf, Sam Ravnborg, Masahiro Yamada, linux-kernel

Kbuild now supports the 'userprogs' syntax to describe the build rules
of userspace programs for the target architecture (i.e. the same
architecture as the kernel).

Add the entry to samples/Makefile to put this into the build bot
coverage.

I also added the CONFIG option guarded by 'depends on CC_CAN_LINK'
because $(CC) may not necessarily provide libc.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 samples/Kconfig         |  4 ++++
 samples/Makefile        |  1 +
 samples/timers/Makefile | 17 +++--------------
 3 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/samples/Kconfig b/samples/Kconfig
index cdb0091e4734..55548a487d3c 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -135,6 +135,10 @@ config SAMPLE_SECCOMP
 	  Build samples of seccomp filters using various methods of
 	  BPF filter construction.
 
+config SAMPLE_TIMER
+	bool "Build timer sample code"
+	depends on CC_CAN_LINK && HEADERS_INSTALL
+
 config SAMPLE_UHID
 	bool "Build UHID sample code"
 	depends on CC_CAN_LINK && HEADERS_INSTALL
diff --git a/samples/Makefile b/samples/Makefile
index 0c43b5d34b15..042208326689 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -16,6 +16,7 @@ subdir-$(CONFIG_SAMPLE_PIDFD)		+= pidfd
 obj-$(CONFIG_SAMPLE_QMI_CLIENT)		+= qmi/
 obj-$(CONFIG_SAMPLE_RPMSG_CLIENT)	+= rpmsg/
 subdir-$(CONFIG_SAMPLE_SECCOMP)		+= seccomp
+subdir-$(CONFIG_SAMPLE_TIMER)		+= timers
 obj-$(CONFIG_SAMPLE_TRACE_EVENTS)	+= trace_events/
 obj-$(CONFIG_SAMPLE_TRACE_PRINTK)	+= trace_printk/
 obj-$(CONFIG_SAMPLE_FTRACE_DIRECT)	+= ftrace/
diff --git a/samples/timers/Makefile b/samples/timers/Makefile
index f9fa07460802..c5f46e8caa80 100644
--- a/samples/timers/Makefile
+++ b/samples/timers/Makefile
@@ -1,16 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
-ifndef CROSS_COMPILE
-uname_M := $(shell uname -m 2>/dev/null || echo not)
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+userprogs := hpet_example
+always-y := $(userprogs)
 
-ifeq ($(ARCH),x86)
-CC := $(CROSS_COMPILE)gcc
-PROGS := hpet_example
-
-all: $(PROGS)
-
-clean:
-	rm -fr $(PROGS)
-
-endif
-endif
+user-ccflags += -I usr/include
-- 
2.25.1


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

* [PATCH 16/16] samples: watchdog: use 'userprogs' syntax
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (14 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 15/16] samples: timers: " Masahiro Yamada
@ 2020-04-23  7:39 ` Masahiro Yamada
  2020-04-25 11:53 ` [PATCH 00/16] kbuild: support " Sam Ravnborg
  16 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23  7:39 UTC (permalink / raw)
  To: linux-kbuild; +Cc: bpf, Sam Ravnborg, Masahiro Yamada, linux-kernel

Kbuild now supports the 'userprogs' syntax to describe the build rules
of userspace programs for the target architecture (i.e. the same
architecture as the kernel).

Add the entry to samples/Makefile to put this into the build bot
coverage.

I also added the CONFIG option guarded by 'depends on CC_CAN_LINK'
because $(CC) may not necessarily provide libc.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 samples/Kconfig           |  3 +++
 samples/Makefile          |  1 +
 samples/watchdog/Makefile | 10 ++--------
 3 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/samples/Kconfig b/samples/Kconfig
index 55548a487d3c..4aa89d24a19e 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -205,5 +205,8 @@ config SAMPLE_INTEL_MEI
 	help
 	  Build a sample program to work with mei device.
 
+config SAMPLE_WATCHDOG
+	bool "watchdog sample"
+	depends on CC_CAN_LINK
 
 endif # SAMPLES
diff --git a/samples/Makefile b/samples/Makefile
index 042208326689..29c66aadd954 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -26,3 +26,4 @@ obj-$(CONFIG_VIDEO_PCI_SKELETON)	+= v4l/
 obj-y					+= vfio-mdev/
 subdir-$(CONFIG_SAMPLE_VFS)		+= vfs
 obj-$(CONFIG_SAMPLE_INTEL_MEI)		+= mei/
+subdir-$(CONFIG_SAMPLE_WATCHDOG)	+= watchdog
diff --git a/samples/watchdog/Makefile b/samples/watchdog/Makefile
index a9430fa60253..17384cfb387e 100644
--- a/samples/watchdog/Makefile
+++ b/samples/watchdog/Makefile
@@ -1,9 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0
-CC := $(CROSS_COMPILE)gcc
-PROGS := watchdog-simple
-
-all: $(PROGS)
-
-clean:
-	rm -fr $(PROGS)
-
+userprogs := watchdog-simple
+always-y := $(userprogs)
-- 
2.25.1


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

* Re: [PATCH 14/16] samples: auxdisplay: use 'userprogs' syntax
  2020-04-23  7:39 ` [PATCH 14/16] samples: auxdisplay: use 'userprogs' syntax Masahiro Yamada
@ 2020-04-23 11:50   ` Miguel Ojeda
  2020-04-24  2:59     ` Masahiro Yamada
  0 siblings, 1 reply; 31+ messages in thread
From: Miguel Ojeda @ 2020-04-23 11:50 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: Linux Kbuild mailing list, bpf, Sam Ravnborg, linux-kernel

Hi Masahiro,

On Thu, Apr 23, 2020 at 9:41 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> Kbuild now supports the 'userprogs' syntax to describe the build rules
> of userspace programs for the target architecture (i.e. the same
> architecture as the kernel).
>
> Add the entry to samples/Makefile to put this into the build bot
> coverage.
>
> I also added the CONFIG option guarded by 'depends on CC_CAN_LINK'
> because $(CC) may not necessarily provide libc.
>
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Thanks for this! Looks nice. I guess you take all patches for the
samples/ changes through your tree?

Acked-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>

Cheers,
Miguel

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

* Re: [PATCH 03/16] kbuild: add infrastructure to build userspace programs
  2020-04-23  7:39 ` [PATCH 03/16] kbuild: add infrastructure to build userspace programs Masahiro Yamada
@ 2020-04-23 15:15   ` Masahiro Yamada
  0 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-23 15:15 UTC (permalink / raw)
  To: Linux Kbuild mailing list
  Cc: bpf, Sam Ravnborg, Alexei Starovoitov, Andrii Nakryiko,
	Daniel Borkmann, John Fastabend, KP Singh, Martin KaFai Lau,
	Michal Marek, Song Liu, Yonghong Song, Linux Kernel Mailing List,
	Networking

On Thu, Apr 23, 2020 at 4:40 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> Kbuild supports the infrastructure to build host programs, but there
> was no support to build userspace programs for the target architecture
> (i.e. the same architecture as the kernel).
>
> Sam Ravnborg worked on this a long time ago.
>
>   https://lkml.org/lkml/2014/7/13/154
>
> But, it was not merged. One problem at that time was, there was no
> good way to know whether $(CC) can link standalone programs. In fact,
> pre-built kernel.org toolchains [1] do not provide libc.
>
> Now, we can handle this cleanly because the compiler capability is
> evaluated at the Kconfig time. If $(CC) cannot link standalone programs,
> the relevant options are hidden by 'depends on CC_CAN_LINK'.
>
> The implementation just mimics scripts/Makefile.host
>
> The userspace programs are compiled with the same flags as the host
> programs. In addition, it uses -m32 or -m64 if it is found in
> $(KBUILD_CFLAGS).
>
> This new syntax has at least two usecases.
>
> - Sample programs
>
>   Several userspace programs under samples/ include UAPI headers
>   installed in usr/include. Most of them were previously built for
>   the host architecture just to use 'hostprogs' syntax.
>
>   However, 'make headers' always works for the target architecture.
>   This caused the arch mismatch in cross-compiling. To fix this
>   distortion, sample code should be built for the target architecture.
>
> - Bpfilter
>
>   net/bpfilter/Makefile compiles bpfilter_umh as the user mode helper,
>   and embeds it into the kernel code. Currently, it overrides HOSTCC
>   with CC to use the 'hostprogs' syntax. This hack should go away.
>
> [1]: https://mirrors.edge.kernel.org/pub/tools/crosstool/
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
>
>  Makefile                   | 11 +++++++---
>  scripts/Makefile.build     |  5 +++++
>  scripts/Makefile.clean     |  2 +-
>  scripts/Makefile.userprogs | 44 ++++++++++++++++++++++++++++++++++++++
>  4 files changed, 58 insertions(+), 4 deletions(-)
>  create mode 100644 scripts/Makefile.userprogs
>
> diff --git a/Makefile b/Makefile
> index 49b2709ff44e..f20597820131 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -406,9 +406,11 @@ else
>  HOSTCC = gcc
>  HOSTCXX        = g++
>  endif
> -KBUILD_HOSTCFLAGS   := -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 \
> -               -fomit-frame-pointer -std=gnu89 $(HOST_LFS_CFLAGS) \
> -               $(HOSTCFLAGS)
> +
> +export KBUILD_USERCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
> +                             -O2 -fomit-frame-pointer -std=gnu89
> +
> +KBUILD_HOSTCFLAGS   := $(KBUILD_USERCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
>  KBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS)
>  KBUILD_HOSTLDFLAGS  := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
>  KBUILD_HOSTLDLIBS   := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
> @@ -937,6 +939,9 @@ ifeq ($(CONFIG_RELR),y)
>  LDFLAGS_vmlinux        += --pack-dyn-relocs=relr
>  endif
>
> +# Align the bit size of userspace programs with the kernel
> +KBUILD_USERCFLAGS += $(filter -m32 -m64, $(KBUILD_CFLAGS))
> +
>  # make the checker run with the right architecture
>  CHECKFLAGS += --arch=$(ARCH)
>
> diff --git a/scripts/Makefile.build b/scripts/Makefile.build
> index 9fcbfac15d1d..94f2f7016172 100644
> --- a/scripts/Makefile.build
> +++ b/scripts/Makefile.build
> @@ -50,6 +50,11 @@ ifneq ($(hostprogs)$(hostcxxlibs-y)$(hostcxxlibs-m),)
>  include scripts/Makefile.host
>  endif
>
> +# Do not include userprogs rules unless needed
> +ifneq ($(userprogs),)
> +include scripts/Makefile.userprogs
> +endif
> +
>  ifndef obj
>  $(warning kbuild: Makefile.build is included improperly)
>  endif
> diff --git a/scripts/Makefile.clean b/scripts/Makefile.clean
> index 075f0cc2d8d7..e2c76122319d 100644
> --- a/scripts/Makefile.clean
> +++ b/scripts/Makefile.clean
> @@ -29,7 +29,7 @@ subdir-ymn    := $(addprefix $(obj)/,$(subdir-ymn))
>
>  __clean-files  := $(extra-y) $(extra-m) $(extra-)       \
>                    $(always) $(always-y) $(always-m) $(always-) $(targets) $(clean-files)   \
> -                  $(hostprogs) $(hostprogs-y) $(hostprogs-m) $(hostprogs-) \
> +                  $(hostprogs) $(hostprogs-y) $(hostprogs-m) $(hostprogs-) $(userprogs) \
>                    $(hostcxxlibs-y) $(hostcxxlibs-m)
>
>  __clean-files   := $(filter-out $(no-clean-files), $(__clean-files))
> diff --git a/scripts/Makefile.userprogs b/scripts/Makefile.userprogs
> new file mode 100644
> index 000000000000..0d987085819b
> --- /dev/null
> +++ b/scripts/Makefile.userprogs
> @@ -0,0 +1,44 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +#
> +# Build userspace programs for the target system
> +#
> +
> +userprogs      := $(sort $(userprogs))
> +
> +# Executables compiled from a single .c file
> +user-csingle   := $(foreach m, $(userprogs), $(if $($(m)-objs),,$(m)))
> +
> +# C executables linked based on several .o files
> +user-cmulti    := $(foreach m, $(userprogs), $(if $($(m)-objs),$(m)))
> +
> +# Object (.o) files compiled from .c files
> +user-cobjs     := $(foreach m, $(userprogs), $($(m)-objs))
> +
> +user-csingle   := $(addprefix $(obj)/, $(user-csingle))
> +user-cmulti    := $(addprefix $(obj)/, $(user-cmulti))
> +user-cobjs     := $(addprefix $(obj)/, $(user-cobjs))
> +
> +user_c_flags   = -Wp,-MMD,$(depfile) $(KBUILD_USERCFLAGS) $(user-ccflags) \
> +                       $($(target-stem)-ccflags)
> +
> +# Create an executable from a single .c file
> +quiet_cmd_user_cc_c = CC [U]  $@
> +      cmd_user_cc_c = $(CC) $(user_c_flags) -o $@ $<


                  $($(target-stem)-ldlibs)

is needed here too.



> +$(user-csingle): $(obj)/%: $(src)/%.c FORCE
> +       $(call if_changed_dep,user_cc_c)
> +
> +# Link an executable based on list of .o files
> +quiet_cmd_user_ld = LD [U]  $@
> +      cmd_user_ld = $(CC) -o $@ $(addprefix $(obj)/, $($(target-stem)-objs)) \
> +                      $($(target-stem)-ldlibs)
> +$(user-cmulti): FORCE
> +       $(call if_changed,user_ld)
> +$(call multi_depend, $(user-cmulti), , -objs)
> +
> +# Create .o file from a .c file
> +quiet_cmd_user_cc_o_c = CC [U]  $@
> +      cmd_user_cc_o_c = $(CC) $(user_c_flags) -c -o $@ $<
> +$(user-cobjs): $(obj)/%.o: $(src)/%.c FORCE
> +       $(call if_changed_dep,user_cc_o_c)
> +
> +targets += $(user-csingle) $(user-cmulti) $(user-cobjs)
> --
> 2.25.1
>


-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH 14/16] samples: auxdisplay: use 'userprogs' syntax
  2020-04-23 11:50   ` Miguel Ojeda
@ 2020-04-24  2:59     ` Masahiro Yamada
  0 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-24  2:59 UTC (permalink / raw)
  To: Miguel Ojeda; +Cc: Linux Kbuild mailing list, bpf, Sam Ravnborg, linux-kernel

Hi Miguel,

On Thu, Apr 23, 2020 at 8:50 PM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> Hi Masahiro,
>
> On Thu, Apr 23, 2020 at 9:41 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
> >
> > Kbuild now supports the 'userprogs' syntax to describe the build rules
> > of userspace programs for the target architecture (i.e. the same
> > architecture as the kernel).
> >
> > Add the entry to samples/Makefile to put this into the build bot
> > coverage.
> >
> > I also added the CONFIG option guarded by 'depends on CC_CAN_LINK'
> > because $(CC) may not necessarily provide libc.
> >
> > Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
>
> Thanks for this! Looks nice. I guess you take all patches for the
> samples/ changes through your tree?

Yes, I will take all to my tree
since this series is mostly Makefile changes.



> Acked-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
>
> Cheers,
> Miguel



-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH 02/16] Revert "objtool: Skip samples subdirectory"
  2020-04-23  7:39 ` [PATCH 02/16] Revert "objtool: Skip samples subdirectory" Masahiro Yamada
@ 2020-04-24 20:32   ` Josh Poimboeuf
  2020-04-29  2:43     ` Masahiro Yamada
  0 siblings, 1 reply; 31+ messages in thread
From: Josh Poimboeuf @ 2020-04-24 20:32 UTC (permalink / raw)
  To: Masahiro Yamada; +Cc: linux-kbuild, bpf, Sam Ravnborg, linux-kernel

On Thu, Apr 23, 2020 at 04:39:15PM +0900, Masahiro Yamada wrote:
> This reverts commit 8728497895794d1f207a836e02dae762ad175d56.
> 
> This directory contains no object.
> 
> Cc: Josh Poimboeuf <jpoimboe@redhat.com>
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  samples/Makefile | 1 -
>  1 file changed, 1 deletion(-)
> 
> diff --git a/samples/Makefile b/samples/Makefile
> index f8f847b4f61f..5ce50ef0f2b2 100644
> --- a/samples/Makefile
> +++ b/samples/Makefile
> @@ -1,6 +1,5 @@
>  # SPDX-License-Identifier: GPL-2.0
>  # Makefile for Linux samples code
> -OBJECT_FILES_NON_STANDARD := y
>  
>  obj-$(CONFIG_SAMPLE_ANDROID_BINDERFS)	+= binderfs/
>  obj-$(CONFIG_SAMPLE_CONFIGFS)		+= configfs/
> -- 
> 2.25.1

Hm, somehow I was thinking this would work recursively for
subdirectories.  Anyway, you're right:

Acked-by: Josh Poimboeuf <jpoimboe@redhat.com>

-- 
Josh


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

* Re: [PATCH 00/16] kbuild: support 'userprogs' syntax
  2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
                   ` (15 preceding siblings ...)
  2020-04-23  7:39 ` [PATCH 16/16] samples: watchdog: " Masahiro Yamada
@ 2020-04-25 11:53 ` Sam Ravnborg
  2020-04-29  2:38   ` Masahiro Yamada
  16 siblings, 1 reply; 31+ messages in thread
From: Sam Ravnborg @ 2020-04-25 11:53 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, bpf, Alexei Starovoitov, Andrii Nakryiko,
	Christian Brauner, Daniel Borkmann, David S. Miller,
	Jakub Kicinski, John Fastabend, Jonathan Corbet, KP Singh,
	Martin KaFai Lau, Michal Marek, Song Liu, Tomas Winkler,
	Yonghong Song, linux-doc, linux-kernel, netdev

Hi Masahiro

On Thu, Apr 23, 2020 at 04:39:13PM +0900, Masahiro Yamada wrote:
> 
> Several Makefiles use 'hostprogs' for building the code for
> the host architecture is not appropriate.
> 
> This is just because Kbuild does not provide the syntax to do it.
> 
> This series introduce 'userprogs' syntax and use it from
> sample and bpf Makefiles.
> 
> Sam worked on this in 2014.
> https://lkml.org/lkml/2014/7/13/154

I wonder how you managed to dig that up, but thanks for the reference.

Back then we would fail buiulding without any libc - you have solved
this nicely in this patch-set.

> 
> He used 'uapiprogs-y' but I just thought the meaning of
> "UAPI programs" is unclear.
> 
> Naming is one the most difficult parts of this.
> 
> I chose 'userprogs'.
> Anothor choice I had in my mind was 'targetprogs'.
> 
> If you can test this series quickly by
> 'make allmodconfig samples/'
> 
> When building objects for userspace, [U] is displayed.
> 
> masahiro@oscar:~/workspace/linux$ make allmodconfig samples/
>   [snip]
>   AR      samples/vfio-mdev/built-in.a
>   CC [M]  samples/vfio-mdev/mtty.o
...

> 
> 
> Masahiro Yamada (15):
>   Documentation: kbuild: fix the section title format
>   Revert "objtool: Skip samples subdirectory"
>   kbuild: add infrastructure to build userspace programs
>   net: bpfilter: use 'userprogs' syntax to build bpfilter_umh
>   samples: seccomp: build sample programs for target architecture
>   kbuild: doc: document the new syntax 'userprogs'
>   samples: uhid: build sample program for target architecture
>   samples: hidraw: build sample program for target architecture
>   samples: connector: build sample program for target architecture
>   samples: vfs: build sample programs for target architecture
>   samples: pidfd: build sample program for target architecture
>   samples: mei: build sample program for target architecture
>   samples: auxdisplay: use 'userprogs' syntax
>   samples: timers: use 'userprogs' syntax
>   samples: watchdog: use 'userprogs' syntax
Nice work!
All patches are:
Acked-by: Sam Ravnborg <sam@ravnborg.org>

> 
> Sam Ravnborg (1):
>   samples: uhid: fix warnings in uhid-example
> 
>  Documentation/kbuild/makefiles.rst | 185 +++++++++++++++++++++--------
>  Makefile                           |  11 +-
>  net/bpfilter/Makefile              |  11 +-
>  samples/Kconfig                    |  26 +++-
>  samples/Makefile                   |   5 +-
>  samples/auxdisplay/Makefile        |  11 +-
>  samples/connector/Makefile         |  12 +-
>  samples/hidraw/Makefile            |   9 +-
>  samples/mei/Makefile               |   9 +-
>  samples/pidfd/Makefile             |   8 +-
>  samples/seccomp/Makefile           |  42 +------
>  samples/timers/Makefile            |  17 +--
>  samples/uhid/.gitignore            |   2 +
>  samples/uhid/Makefile              |   9 +-
>  samples/uhid/uhid-example.c        |   4 +-
>  samples/vfs/Makefile               |  11 +-
>  samples/watchdog/Makefile          |  10 +-
>  scripts/Makefile.build             |   5 +
>  scripts/Makefile.clean             |   2 +-
>  scripts/Makefile.userprogs         |  44 +++++++
>  20 files changed, 258 insertions(+), 175 deletions(-)
>  create mode 100644 samples/uhid/.gitignore
>  create mode 100644 scripts/Makefile.userprogs
> 
> -- 
> 2.25.1

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

* Re: [PATCH 04/16] net: bpfilter: use 'userprogs' syntax to build bpfilter_umh
  2020-04-23  7:39 ` [PATCH 04/16] net: bpfilter: use 'userprogs' syntax to build bpfilter_umh Masahiro Yamada
@ 2020-04-28  1:46   ` kbuild test robot
  2020-04-28  5:13     ` Masahiro Yamada
  2020-06-08 11:56   ` Michal Kubecek
  1 sibling, 1 reply; 31+ messages in thread
From: kbuild test robot @ 2020-04-28  1:46 UTC (permalink / raw)
  To: Masahiro Yamada, linux-kbuild
  Cc: kbuild-all, bpf, Sam Ravnborg, Masahiro Yamada,
	Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jakub Kicinski, John Fastabend, KP Singh

[-- Attachment #1: Type: text/plain, Size: 1206 bytes --]

Hi Masahiro,

I love your patch! Yet something to improve:

[auto build test ERROR on kbuild/for-next]
[cannot apply to linus/master v5.7-rc3 next-20200424]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Masahiro-Yamada/kbuild-support-userprogs-syntax/20200426-114001
base:   https://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild.git for-next
config: i386-allyesconfig (attached as .config)
compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kbuild test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   /usr/bin/ld: i386 architecture of input file `net/bpfilter/main.o' is incompatible with i386:x86-64 output
>> collect2: error: ld returned 1 exit status

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 72224 bytes --]

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

* Re: [PATCH 04/16] net: bpfilter: use 'userprogs' syntax to build bpfilter_umh
  2020-04-28  1:46   ` kbuild test robot
@ 2020-04-28  5:13     ` Masahiro Yamada
  0 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-28  5:13 UTC (permalink / raw)
  To: kbuild test robot
  Cc: Linux Kbuild mailing list, kbuild-all, bpf, Sam Ravnborg,
	Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Jakub Kicinski, John Fastabend, KP Singh

On Tue, Apr 28, 2020 at 10:46 AM kbuild test robot <lkp@intel.com> wrote:
>
> Hi Masahiro,
>
> I love your patch! Yet something to improve:
>
> [auto build test ERROR on kbuild/for-next]
> [cannot apply to linus/master v5.7-rc3 next-20200424]
> [if your patch is applied to the wrong git tree, please drop us a note to help
> improve the system. BTW, we also suggest to use '--base' option to specify the
> base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
>
> url:    https://github.com/0day-ci/linux/commits/Masahiro-Yamada/kbuild-support-userprogs-syntax/20200426-114001
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild.git for-next
> config: i386-allyesconfig (attached as .config)
> compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0
> reproduce:
>         # save the attached .config to linux build tree
>         make ARCH=i386
>
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kbuild test robot <lkp@intel.com>
>
> All errors (new ones prefixed by >>):
>
>    /usr/bin/ld: i386 architecture of input file `net/bpfilter/main.o' is incompatible with i386:x86-64 output
> >> collect2: error: ld returned 1 exit status
>

Thanks.
-m32/-m64 is missing in the link time of the bpfilter_umh.
I will fix it soon.



-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH 00/16] kbuild: support 'userprogs' syntax
  2020-04-25 11:53 ` [PATCH 00/16] kbuild: support " Sam Ravnborg
@ 2020-04-29  2:38   ` Masahiro Yamada
  0 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-29  2:38 UTC (permalink / raw)
  To: Sam Ravnborg
  Cc: Linux Kbuild mailing list, bpf, Alexei Starovoitov,
	Andrii Nakryiko, Christian Brauner, Daniel Borkmann,
	David S. Miller, Jakub Kicinski, John Fastabend, Jonathan Corbet,
	KP Singh, Martin KaFai Lau, Michal Marek, Song Liu,
	Tomas Winkler, Yonghong Song, open list:DOCUMENTATION,
	Linux Kernel Mailing List, Networking

Hi Sam,

On Sat, Apr 25, 2020 at 8:53 PM Sam Ravnborg <sam@ravnborg.org> wrote:
>
> Hi Masahiro
>
> On Thu, Apr 23, 2020 at 04:39:13PM +0900, Masahiro Yamada wrote:
> >
> > Several Makefiles use 'hostprogs' for building the code for
> > the host architecture is not appropriate.
> >
> > This is just because Kbuild does not provide the syntax to do it.
> >
> > This series introduce 'userprogs' syntax and use it from
> > sample and bpf Makefiles.
> >
> > Sam worked on this in 2014.
> > https://lkml.org/lkml/2014/7/13/154
>
> I wonder how you managed to dig that up, but thanks for the reference.


I just remembered your work back in 2014.

I did not remember the patch title exactly,
but I searched for 'From: Sam Ravnborg' and
'To: linux-kbuild@vger.kernel.org' in my mail box.




-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH 02/16] Revert "objtool: Skip samples subdirectory"
  2020-04-24 20:32   ` Josh Poimboeuf
@ 2020-04-29  2:43     ` Masahiro Yamada
  0 siblings, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-04-29  2:43 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: Linux Kbuild mailing list, bpf, Sam Ravnborg, Linux Kernel Mailing List

On Sat, Apr 25, 2020 at 5:32 AM Josh Poimboeuf <jpoimboe@redhat.com> wrote:
>
> On Thu, Apr 23, 2020 at 04:39:15PM +0900, Masahiro Yamada wrote:
> > This reverts commit 8728497895794d1f207a836e02dae762ad175d56.
> >
> > This directory contains no object.
> >
> > Cc: Josh Poimboeuf <jpoimboe@redhat.com>
> > Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> > ---
> >
> >  samples/Makefile | 1 -
> >  1 file changed, 1 deletion(-)
> >
> > diff --git a/samples/Makefile b/samples/Makefile
> > index f8f847b4f61f..5ce50ef0f2b2 100644
> > --- a/samples/Makefile
> > +++ b/samples/Makefile
> > @@ -1,6 +1,5 @@
> >  # SPDX-License-Identifier: GPL-2.0
> >  # Makefile for Linux samples code
> > -OBJECT_FILES_NON_STANDARD := y
> >
> >  obj-$(CONFIG_SAMPLE_ANDROID_BINDERFS)        += binderfs/
> >  obj-$(CONFIG_SAMPLE_CONFIGFS)                += configfs/
> > --
> > 2.25.1
>
> Hm, somehow I was thinking this would work recursively for
> subdirectories.  Anyway, you're right:
>
> Acked-by: Josh Poimboeuf <jpoimboe@redhat.com>
>
> --
> Josh
>

Applied to linux-kbuild.


-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH 04/16] net: bpfilter: use 'userprogs' syntax to build bpfilter_umh
  2020-04-23  7:39 ` [PATCH 04/16] net: bpfilter: use 'userprogs' syntax to build bpfilter_umh Masahiro Yamada
  2020-04-28  1:46   ` kbuild test robot
@ 2020-06-08 11:56   ` Michal Kubecek
  2020-06-08 13:32     ` Masahiro Yamada
  2020-06-30  6:30     ` Masahiro Yamada
  1 sibling, 2 replies; 31+ messages in thread
From: Michal Kubecek @ 2020-06-08 11:56 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, bpf, Sam Ravnborg, Alexei Starovoitov,
	Andrii Nakryiko, Daniel Borkmann, David S. Miller,
	Jakub Kicinski, John Fastabend, KP Singh, Martin KaFai Lau,
	Song Liu, Yonghong Song, linux-kernel, netdev

On Thu, Apr 23, 2020 at 04:39:17PM +0900, Masahiro Yamada wrote:
> The user mode helper should be compiled for the same architecture as
> the kernel.
> 
> This Makefile reuses the 'hostprogs' syntax by overriding HOSTCC with CC.
> 
> Now that Kbuild provides the syntax 'userprogs', use it to fix the
> Makefile mess.
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> Reported-by: kbuild test robot <lkp@intel.com>
> ---
> 
>  net/bpfilter/Makefile | 11 ++++-------
>  1 file changed, 4 insertions(+), 7 deletions(-)
> 
> diff --git a/net/bpfilter/Makefile b/net/bpfilter/Makefile
> index 36580301da70..6ee650c6badb 100644
> --- a/net/bpfilter/Makefile
> +++ b/net/bpfilter/Makefile
> @@ -3,17 +3,14 @@
>  # Makefile for the Linux BPFILTER layer.
>  #
>  
> -hostprogs := bpfilter_umh
> +userprogs := bpfilter_umh
>  bpfilter_umh-objs := main.o
> -KBUILD_HOSTCFLAGS += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi
> -HOSTCC := $(CC)
> +user-ccflags += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi
>  
> -ifeq ($(CONFIG_BPFILTER_UMH), y)
> -# builtin bpfilter_umh should be compiled with -static
> +# builtin bpfilter_umh should be linked with -static
>  # since rootfs isn't mounted at the time of __init
>  # function is called and do_execv won't find elf interpreter
> -KBUILD_HOSTLDFLAGS += -static
> -endif
> +bpfilter_umh-ldflags += -static
>  
>  $(obj)/bpfilter_umh_blob.o: $(obj)/bpfilter_umh

Hello,

I just noticed that this patch (now in mainline as commit 8a2cc0505cc4)
drops the test if CONFIG_BPFILTER_UMH is "y" so that -static is now
passed to the linker even if bpfilter_umh is built as a module which
wasn't the case in v5.7.

This is not mentioned in the commit message and the comment still says
"*builtin* bpfilter_umh should be linked with -static" so this change
doesn't seem to be intentional. Did I miss something?

Michal Kubecek

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

* Re: [PATCH 04/16] net: bpfilter: use 'userprogs' syntax to build bpfilter_umh
  2020-06-08 11:56   ` Michal Kubecek
@ 2020-06-08 13:32     ` Masahiro Yamada
  2020-06-30  6:30     ` Masahiro Yamada
  1 sibling, 0 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-06-08 13:32 UTC (permalink / raw)
  To: Michal Kubecek
  Cc: Linux Kbuild mailing list, bpf, Sam Ravnborg, Alexei Starovoitov,
	Andrii Nakryiko, Daniel Borkmann, David S. Miller,
	Jakub Kicinski, John Fastabend, KP Singh, Martin KaFai Lau,
	Song Liu, Yonghong Song, Linux Kernel Mailing List, Networking

On Mon, Jun 8, 2020 at 8:56 PM Michal Kubecek <mkubecek@suse.cz> wrote:
>
> On Thu, Apr 23, 2020 at 04:39:17PM +0900, Masahiro Yamada wrote:
> > The user mode helper should be compiled for the same architecture as
> > the kernel.
> >
> > This Makefile reuses the 'hostprogs' syntax by overriding HOSTCC with CC.
> >
> > Now that Kbuild provides the syntax 'userprogs', use it to fix the
> > Makefile mess.
> >
> > Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> > Reported-by: kbuild test robot <lkp@intel.com>
> > ---
> >
> >  net/bpfilter/Makefile | 11 ++++-------
> >  1 file changed, 4 insertions(+), 7 deletions(-)
> >
> > diff --git a/net/bpfilter/Makefile b/net/bpfilter/Makefile
> > index 36580301da70..6ee650c6badb 100644
> > --- a/net/bpfilter/Makefile
> > +++ b/net/bpfilter/Makefile
> > @@ -3,17 +3,14 @@
> >  # Makefile for the Linux BPFILTER layer.
> >  #
> >
> > -hostprogs := bpfilter_umh
> > +userprogs := bpfilter_umh
> >  bpfilter_umh-objs := main.o
> > -KBUILD_HOSTCFLAGS += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi
> > -HOSTCC := $(CC)
> > +user-ccflags += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi
> >
> > -ifeq ($(CONFIG_BPFILTER_UMH), y)
> > -# builtin bpfilter_umh should be compiled with -static
> > +# builtin bpfilter_umh should be linked with -static
> >  # since rootfs isn't mounted at the time of __init
> >  # function is called and do_execv won't find elf interpreter
> > -KBUILD_HOSTLDFLAGS += -static
> > -endif
> > +bpfilter_umh-ldflags += -static
> >
> >  $(obj)/bpfilter_umh_blob.o: $(obj)/bpfilter_umh
>
> Hello,
>
> I just noticed that this patch (now in mainline as commit 8a2cc0505cc4)
> drops the test if CONFIG_BPFILTER_UMH is "y" so that -static is now
> passed to the linker even if bpfilter_umh is built as a module which
> wasn't the case in v5.7.
>
> This is not mentioned in the commit message and the comment still says
> "*builtin* bpfilter_umh should be linked with -static" so this change
> doesn't seem to be intentional. Did I miss something?
>
> Michal Kubecek


Sorry. ifeq was accidentally dropped.
I will restore it.

-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH 04/16] net: bpfilter: use 'userprogs' syntax to build bpfilter_umh
  2020-06-08 11:56   ` Michal Kubecek
  2020-06-08 13:32     ` Masahiro Yamada
@ 2020-06-30  6:30     ` Masahiro Yamada
  2020-06-30  8:57       ` Michal Kubecek
  2020-06-30 16:47       ` Alexei Starovoitov
  1 sibling, 2 replies; 31+ messages in thread
From: Masahiro Yamada @ 2020-06-30  6:30 UTC (permalink / raw)
  To: Michal Kubecek, Alexei Starovoitov
  Cc: Linux Kbuild mailing list, bpf, Sam Ravnborg, Andrii Nakryiko,
	Daniel Borkmann, David S. Miller, Jakub Kicinski, John Fastabend,
	KP Singh, Martin KaFai Lau, Song Liu, Yonghong Song,
	Linux Kernel Mailing List, Networking

Hi Michal, Alexei,

On Mon, Jun 8, 2020 at 8:56 PM Michal Kubecek <mkubecek@suse.cz> wrote:
>
> On Thu, Apr 23, 2020 at 04:39:17PM +0900, Masahiro Yamada wrote:
> > The user mode helper should be compiled for the same architecture as
> > the kernel.
> >
> > This Makefile reuses the 'hostprogs' syntax by overriding HOSTCC with CC.
> >
> > Now that Kbuild provides the syntax 'userprogs', use it to fix the
> > Makefile mess.
> >
> > Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> > Reported-by: kbuild test robot <lkp@intel.com>
> > ---
> >
> >  net/bpfilter/Makefile | 11 ++++-------
> >  1 file changed, 4 insertions(+), 7 deletions(-)
> >
> > diff --git a/net/bpfilter/Makefile b/net/bpfilter/Makefile
> > index 36580301da70..6ee650c6badb 100644
> > --- a/net/bpfilter/Makefile
> > +++ b/net/bpfilter/Makefile
> > @@ -3,17 +3,14 @@
> >  # Makefile for the Linux BPFILTER layer.
> >  #
> >
> > -hostprogs := bpfilter_umh
> > +userprogs := bpfilter_umh
> >  bpfilter_umh-objs := main.o
> > -KBUILD_HOSTCFLAGS += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi
> > -HOSTCC := $(CC)
> > +user-ccflags += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi
> >
> > -ifeq ($(CONFIG_BPFILTER_UMH), y)
> > -# builtin bpfilter_umh should be compiled with -static
> > +# builtin bpfilter_umh should be linked with -static
> >  # since rootfs isn't mounted at the time of __init
> >  # function is called and do_execv won't find elf interpreter
> > -KBUILD_HOSTLDFLAGS += -static
> > -endif
> > +bpfilter_umh-ldflags += -static
> >
> >  $(obj)/bpfilter_umh_blob.o: $(obj)/bpfilter_umh
>
> Hello,
>
> I just noticed that this patch (now in mainline as commit 8a2cc0505cc4)
> drops the test if CONFIG_BPFILTER_UMH is "y" so that -static is now
> passed to the linker even if bpfilter_umh is built as a module which
> wasn't the case in v5.7.
>
> This is not mentioned in the commit message and the comment still says
> "*builtin* bpfilter_umh should be linked with -static" so this change
> doesn't seem to be intentional. Did I miss something?
>
> Michal Kubecek

I was away for a while from this because I saw long discussion in
"net/bpfilter: Remove this broken and apparently unmaintained"


Please let me resume this topic now.


The original behavior of linking umh was like this:
  - If CONFIG_BPFILTER_UMH=y, bpfilter_umh was linked with -static
  - If CONFIG_BPFILTER_UMH=m, bpfilter_umh was linked without -static



Restoring the original behavior will add more complexity because
now we have CONFIG_CC_CAN_LINK and CONFIG_CC_CAN_LINK_STATIC
since commit b1183b6dca3e0d5

If CONFIG_BPFILTER_UMH=y, we need to check CONFIG_CC_CAN_LINK_STATIC.
If CONFIG_BPFILTER_UMH=m, we need to check CONFIG_CC_CAN_LINK.
This would make the Kconfig dependency logic too complicated.


To make it simpler, I'd like to suggest two options.



Idea 1:

  Always use -static irrespective of whether
  CONFIG_BPFILTER_UMH is y or m.

  Add two more lines to clarify this
  in the comment in net/bpfilter/Makefile:

  # builtin bpfilter_umh should be linked with -static
  # since rootfs isn't mounted at the time of __init
  # function is called and do_execv won't find elf interpreter.
  # Static linking is not required when bpfilter is modular, but
  # we always pass -static to keep the 'depends on' in Kconfig simple.



Idea 2:

   Allow umh to become only modular,
   and drop -static flag entirely.

   If you look at net/bpfilter/Kconfig,
   BPFILTER_UMH already has 'default m'.
   So, I assume the most expected use-case
   is modular.

   My suggestion is to replace 'default m' with 'depends on m'.

   config BPFILTER_UMH
           tristate "bpfilter kernel module with user mode helper"
           depends on CC_CAN_LINK
           depends on m

   Then BPFILTER_UMH will be restricted to either m or n.
   Link umh dynamically because we can expect rootfs
   is already mounted for the module case.






Comments are appreciated.


-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH 04/16] net: bpfilter: use 'userprogs' syntax to build bpfilter_umh
  2020-06-30  6:30     ` Masahiro Yamada
@ 2020-06-30  8:57       ` Michal Kubecek
  2020-06-30 16:47       ` Alexei Starovoitov
  1 sibling, 0 replies; 31+ messages in thread
From: Michal Kubecek @ 2020-06-30  8:57 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: Alexei Starovoitov, Linux Kbuild mailing list, bpf, Sam Ravnborg,
	Andrii Nakryiko, Daniel Borkmann, David S. Miller,
	Jakub Kicinski, John Fastabend, KP Singh, Martin KaFai Lau,
	Song Liu, Yonghong Song, Linux Kernel Mailing List, Networking

On Tue, Jun 30, 2020 at 03:30:04PM +0900, Masahiro Yamada wrote:
> On Mon, Jun 8, 2020 at 8:56 PM Michal Kubecek <mkubecek@suse.cz> wrote:
> >
> > I just noticed that this patch (now in mainline as commit 8a2cc0505cc4)
> > drops the test if CONFIG_BPFILTER_UMH is "y" so that -static is now
> > passed to the linker even if bpfilter_umh is built as a module which
> > wasn't the case in v5.7.
> >
> > This is not mentioned in the commit message and the comment still says
> > "*builtin* bpfilter_umh should be linked with -static" so this change
> > doesn't seem to be intentional. Did I miss something?
> 
> I was away for a while from this because I saw long discussion in
> "net/bpfilter: Remove this broken and apparently unmaintained"
> 
> 
> Please let me resume this topic now.
> 
> 
> The original behavior of linking umh was like this:
>   - If CONFIG_BPFILTER_UMH=y, bpfilter_umh was linked with -static
>   - If CONFIG_BPFILTER_UMH=m, bpfilter_umh was linked without -static
> 
> 
> 
> Restoring the original behavior will add more complexity because
> now we have CONFIG_CC_CAN_LINK and CONFIG_CC_CAN_LINK_STATIC
> since commit b1183b6dca3e0d5
> 
> If CONFIG_BPFILTER_UMH=y, we need to check CONFIG_CC_CAN_LINK_STATIC.
> If CONFIG_BPFILTER_UMH=m, we need to check CONFIG_CC_CAN_LINK.
> This would make the Kconfig dependency logic too complicated.
> 
> 
> To make it simpler, I'd like to suggest two options.
> 
> 
> 
> Idea 1:
> 
>   Always use -static irrespective of whether
>   CONFIG_BPFILTER_UMH is y or m.
> 
>   Add two more lines to clarify this
>   in the comment in net/bpfilter/Makefile:
> 
>   # builtin bpfilter_umh should be linked with -static
>   # since rootfs isn't mounted at the time of __init
>   # function is called and do_execv won't find elf interpreter.
>   # Static linking is not required when bpfilter is modular, but
>   # we always pass -static to keep the 'depends on' in Kconfig simple.

I wouldn't be very happy with this solution as that would mean adding an
extra build dependency which we don't really need. We might even
consider disabling CONFIG_BPFILTER_UMH instead.

> Idea 2:
> 
>    Allow umh to become only modular,
>    and drop -static flag entirely.
> 
>    If you look at net/bpfilter/Kconfig,
>    BPFILTER_UMH already has 'default m'.
>    So, I assume the most expected use-case
>    is modular.
> 
>    My suggestion is to replace 'default m' with 'depends on m'.
> 
>    config BPFILTER_UMH
>            tristate "bpfilter kernel module with user mode helper"
>            depends on CC_CAN_LINK
>            depends on m
> 
>    Then BPFILTER_UMH will be restricted to either m or n.
>    Link umh dynamically because we can expect rootfs
>    is already mounted for the module case.

This wouldn't be a problem for me or openSUSE kernels as we already have
CONFIG_BPFILTER_UMH=m. But I can't speak for others, I'm not sure if
there are some use cases requiring CONFIG_BPFILTER_UMH=y.

Michal

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

* Re: [PATCH 04/16] net: bpfilter: use 'userprogs' syntax to build bpfilter_umh
  2020-06-30  6:30     ` Masahiro Yamada
  2020-06-30  8:57       ` Michal Kubecek
@ 2020-06-30 16:47       ` Alexei Starovoitov
  1 sibling, 0 replies; 31+ messages in thread
From: Alexei Starovoitov @ 2020-06-30 16:47 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: Michal Kubecek, Alexei Starovoitov, Linux Kbuild mailing list,
	bpf, Sam Ravnborg, Andrii Nakryiko, Daniel Borkmann,
	David S. Miller, Jakub Kicinski, John Fastabend, KP Singh,
	Martin KaFai Lau, Song Liu, Yonghong Song,
	Linux Kernel Mailing List, Networking

On Tue, Jun 30, 2020 at 03:30:04PM +0900, Masahiro Yamada wrote:
> Hi Michal, Alexei,
> 
> On Mon, Jun 8, 2020 at 8:56 PM Michal Kubecek <mkubecek@suse.cz> wrote:
> >
> > On Thu, Apr 23, 2020 at 04:39:17PM +0900, Masahiro Yamada wrote:
> > > The user mode helper should be compiled for the same architecture as
> > > the kernel.
> > >
> > > This Makefile reuses the 'hostprogs' syntax by overriding HOSTCC with CC.
> > >
> > > Now that Kbuild provides the syntax 'userprogs', use it to fix the
> > > Makefile mess.
> > >
> > > Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> > > Reported-by: kbuild test robot <lkp@intel.com>
> > > ---
> > >
> > >  net/bpfilter/Makefile | 11 ++++-------
> > >  1 file changed, 4 insertions(+), 7 deletions(-)
> > >
> > > diff --git a/net/bpfilter/Makefile b/net/bpfilter/Makefile
> > > index 36580301da70..6ee650c6badb 100644
> > > --- a/net/bpfilter/Makefile
> > > +++ b/net/bpfilter/Makefile
> > > @@ -3,17 +3,14 @@
> > >  # Makefile for the Linux BPFILTER layer.
> > >  #
> > >
> > > -hostprogs := bpfilter_umh
> > > +userprogs := bpfilter_umh
> > >  bpfilter_umh-objs := main.o
> > > -KBUILD_HOSTCFLAGS += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi
> > > -HOSTCC := $(CC)
> > > +user-ccflags += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi
> > >
> > > -ifeq ($(CONFIG_BPFILTER_UMH), y)
> > > -# builtin bpfilter_umh should be compiled with -static
> > > +# builtin bpfilter_umh should be linked with -static
> > >  # since rootfs isn't mounted at the time of __init
> > >  # function is called and do_execv won't find elf interpreter
> > > -KBUILD_HOSTLDFLAGS += -static
> > > -endif
> > > +bpfilter_umh-ldflags += -static
> > >
> > >  $(obj)/bpfilter_umh_blob.o: $(obj)/bpfilter_umh
> >
> > Hello,
> >
> > I just noticed that this patch (now in mainline as commit 8a2cc0505cc4)
> > drops the test if CONFIG_BPFILTER_UMH is "y" so that -static is now
> > passed to the linker even if bpfilter_umh is built as a module which
> > wasn't the case in v5.7.
> >
> > This is not mentioned in the commit message and the comment still says
> > "*builtin* bpfilter_umh should be linked with -static" so this change
> > doesn't seem to be intentional. Did I miss something?
> >
> > Michal Kubecek
> 
> I was away for a while from this because I saw long discussion in
> "net/bpfilter: Remove this broken and apparently unmaintained"
> 
> 
> Please let me resume this topic now.
> 
> 
> The original behavior of linking umh was like this:
>   - If CONFIG_BPFILTER_UMH=y, bpfilter_umh was linked with -static
>   - If CONFIG_BPFILTER_UMH=m, bpfilter_umh was linked without -static

That was done to make sure both static and dynamic linking work.
For production -static is necessary.
For debugging of usermode blob dynamic is beneficial.

> Restoring the original behavior will add more complexity because
> now we have CONFIG_CC_CAN_LINK and CONFIG_CC_CAN_LINK_STATIC
> since commit b1183b6dca3e0d5
> 
> If CONFIG_BPFILTER_UMH=y, we need to check CONFIG_CC_CAN_LINK_STATIC.
> If CONFIG_BPFILTER_UMH=m, we need to check CONFIG_CC_CAN_LINK.
> This would make the Kconfig dependency logic too complicated.

Currently I'm working on adding bpf_iter to use 'user mode driver'
(old user mode blob) facility on top of Eric's patches.
So there will be quite a bit more complexity to build system.
Folks who don't want to deal with -static requirement should
just disable the feature.

> To make it simpler, I'd like to suggest two options.
> 
> 
> 
> Idea 1:
> 
>   Always use -static irrespective of whether
>   CONFIG_BPFILTER_UMH is y or m.

I don't think it's making it much simpler.
It's a tiny change to makefile.
I could be missing something.
Requiring -static for =y and =m is fine.

>   Add two more lines to clarify this
>   in the comment in net/bpfilter/Makefile:
> 
>   # builtin bpfilter_umh should be linked with -static
>   # since rootfs isn't mounted at the time of __init
>   # function is called and do_execv won't find elf interpreter.
>   # Static linking is not required when bpfilter is modular, but
>   # we always pass -static to keep the 'depends on' in Kconfig simple.
> 
> 
> 
> Idea 2:
> 
>    Allow umh to become only modular,
>    and drop -static flag entirely.

absolutely not.
Both =y and =m are mandatory.

> 
>    If you look at net/bpfilter/Kconfig,
>    BPFILTER_UMH already has 'default m'.
>    So, I assume the most expected use-case
>    is modular.

The default for BPFILTER is =N.
Distros should NOT be turning that to =y

Same thing with upcoming bpf_iter. It will default to =n.

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

end of thread, other threads:[~2020-06-30 16:47 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-23  7:39 [PATCH 00/16] kbuild: support 'userprogs' syntax Masahiro Yamada
2020-04-23  7:39 ` [PATCH 01/16] Documentation: kbuild: fix the section title format Masahiro Yamada
2020-04-23  7:39 ` [PATCH 02/16] Revert "objtool: Skip samples subdirectory" Masahiro Yamada
2020-04-24 20:32   ` Josh Poimboeuf
2020-04-29  2:43     ` Masahiro Yamada
2020-04-23  7:39 ` [PATCH 03/16] kbuild: add infrastructure to build userspace programs Masahiro Yamada
2020-04-23 15:15   ` Masahiro Yamada
2020-04-23  7:39 ` [PATCH 04/16] net: bpfilter: use 'userprogs' syntax to build bpfilter_umh Masahiro Yamada
2020-04-28  1:46   ` kbuild test robot
2020-04-28  5:13     ` Masahiro Yamada
2020-06-08 11:56   ` Michal Kubecek
2020-06-08 13:32     ` Masahiro Yamada
2020-06-30  6:30     ` Masahiro Yamada
2020-06-30  8:57       ` Michal Kubecek
2020-06-30 16:47       ` Alexei Starovoitov
2020-04-23  7:39 ` [PATCH 05/16] samples: seccomp: build sample programs for target architecture Masahiro Yamada
2020-04-23  7:39 ` [PATCH 06/16] kbuild: doc: document the new syntax 'userprogs' Masahiro Yamada
2020-04-23  7:39 ` [PATCH 07/16] samples: uhid: fix warnings in uhid-example Masahiro Yamada
2020-04-23  7:39 ` [PATCH 08/16] samples: uhid: build sample program for target architecture Masahiro Yamada
2020-04-23  7:39 ` [PATCH 09/16] samples: hidraw: " Masahiro Yamada
2020-04-23  7:39 ` [PATCH 10/16] samples: connector: " Masahiro Yamada
2020-04-23  7:39 ` [PATCH 11/16] samples: vfs: build sample programs " Masahiro Yamada
2020-04-23  7:39 ` [PATCH 12/16] samples: pidfd: build sample program " Masahiro Yamada
2020-04-23  7:39 ` [PATCH 13/16] samples: mei: " Masahiro Yamada
2020-04-23  7:39 ` [PATCH 14/16] samples: auxdisplay: use 'userprogs' syntax Masahiro Yamada
2020-04-23 11:50   ` Miguel Ojeda
2020-04-24  2:59     ` Masahiro Yamada
2020-04-23  7:39 ` [PATCH 15/16] samples: timers: " Masahiro Yamada
2020-04-23  7:39 ` [PATCH 16/16] samples: watchdog: " Masahiro Yamada
2020-04-25 11:53 ` [PATCH 00/16] kbuild: support " Sam Ravnborg
2020-04-29  2:38   ` Masahiro Yamada

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).