diff mbox series

[bpf] samples: bpf: Fix vmlinux.h generation for XDP samples

Message ID 20210928041329.1735524-1-memxor@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [bpf] samples: bpf: Fix vmlinux.h generation for XDP samples | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf
netdev/subject_prefix success Link
netdev/cc_maintainers fail 1 blamed authors not CCed: ast@kernel.org; 12 maintainers not CCed: andrii@kernel.org ast@kernel.org kpsingh@kernel.org kuba@kernel.org hawk@kernel.org kafai@fb.com netdev@vger.kernel.org john.fastabend@gmail.com davem@davemloft.net daniel@iogearbox.net yhs@fb.com songliubraving@fb.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch warning WARNING: use relative pathname instead of absolute in changelog text
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link
bpf/vmtest-bpf-PR fail PR summary
bpf/vmtest-bpf fail VM_Test

Commit Message

Kumar Kartikeya Dwivedi Sept. 28, 2021, 4:13 a.m. UTC
Generate vmlinux.h only from the in-tree vmlinux, and remove enum
declarations that would cause a build failure in case of version
mismatches.

There are now two options when building the samples:
1. Compile the kernel to use in-tree vmlinux for vmlinux.h
2. Override VMLINUX_BTF for samples using something like this:
   make VMLINUX_BTF=/sys/kernel/btf/vmlinux -C samples/bpf

This change was tested with relative builds, e.g. cases like:
 * make O=build -C samples/bpf
 * make KBUILD_OUTPUT=build -C samples/bpf
 * make -C samples/bpf
 * cd samples/bpf && make

When a suitable VMLINUX_BTF is not found, the following message is
printed:
/home/kkd/src/linux/samples/bpf/Makefile:333: *** Cannot find a vmlinux
for VMLINUX_BTF at any of "  ./vmlinux", build the kernel or set
VMLINUX_BTF variable.  Stop.

Cc: Toke Høiland-Jørgensen <toke@redhat.com>
Fixes: 384b6b3bbf0d (samples: bpf: Add vmlinux.h generation support)
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 samples/bpf/Makefile                     | 13 ++++++-------
 samples/bpf/xdp_redirect_map_multi.bpf.c |  5 -----
 2 files changed, 6 insertions(+), 12 deletions(-)

--
2.33.0

Comments

Andrii Nakryiko Sept. 28, 2021, 4:36 a.m. UTC | #1
On Mon, Sep 27, 2021 at 9:16 PM Kumar Kartikeya Dwivedi
<memxor@gmail.com> wrote:
>
> Generate vmlinux.h only from the in-tree vmlinux, and remove enum
> declarations that would cause a build failure in case of version
> mismatches.
>
> There are now two options when building the samples:
> 1. Compile the kernel to use in-tree vmlinux for vmlinux.h
> 2. Override VMLINUX_BTF for samples using something like this:
>    make VMLINUX_BTF=/sys/kernel/btf/vmlinux -C samples/bpf
>
> This change was tested with relative builds, e.g. cases like:
>  * make O=build -C samples/bpf
>  * make KBUILD_OUTPUT=build -C samples/bpf
>  * make -C samples/bpf
>  * cd samples/bpf && make
>
> When a suitable VMLINUX_BTF is not found, the following message is
> printed:
> /home/kkd/src/linux/samples/bpf/Makefile:333: *** Cannot find a vmlinux
> for VMLINUX_BTF at any of "  ./vmlinux", build the kernel or set
> VMLINUX_BTF variable.  Stop.
>
> Cc: Toke Høiland-Jørgensen <toke@redhat.com>
> Fixes: 384b6b3bbf0d (samples: bpf: Add vmlinux.h generation support)
> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> ---
>  samples/bpf/Makefile                     | 13 ++++++-------
>  samples/bpf/xdp_redirect_map_multi.bpf.c |  5 -----
>  2 files changed, 6 insertions(+), 12 deletions(-)
>
> diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
> index 4dc20be5fb96..a05130e91403 100644
> --- a/samples/bpf/Makefile
> +++ b/samples/bpf/Makefile
> @@ -324,15 +324,9 @@ $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
>
>  VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)                           \
>                      $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)    \
> -                    ../../../../vmlinux                                \
> -                    /sys/kernel/btf/vmlinux                            \
> -                    /boot/vmlinux-$(shell uname -r)
> +                    ./vmlinux

isn't this relative to samples/bpf subdirectory, so shouldn't it be
../../vmlinux?

>  VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
>
> -ifeq ($(VMLINUX_BTF),)
> -$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
> -endif
> -

[...]
Kumar Kartikeya Dwivedi Sept. 28, 2021, 4:51 a.m. UTC | #2
On Tue, Sep 28, 2021 at 10:06:09AM IST, Andrii Nakryiko wrote:
> On Mon, Sep 27, 2021 at 9:16 PM Kumar Kartikeya Dwivedi
> <memxor@gmail.com> wrote:
> >
> > Generate vmlinux.h only from the in-tree vmlinux, and remove enum
> > declarations that would cause a build failure in case of version
> > mismatches.
> >
> > There are now two options when building the samples:
> > 1. Compile the kernel to use in-tree vmlinux for vmlinux.h
> > 2. Override VMLINUX_BTF for samples using something like this:
> >    make VMLINUX_BTF=/sys/kernel/btf/vmlinux -C samples/bpf
> >
> > This change was tested with relative builds, e.g. cases like:
> >  * make O=build -C samples/bpf
> >  * make KBUILD_OUTPUT=build -C samples/bpf
> >  * make -C samples/bpf
> >  * cd samples/bpf && make
> >
> > When a suitable VMLINUX_BTF is not found, the following message is
> > printed:
> > /home/kkd/src/linux/samples/bpf/Makefile:333: *** Cannot find a vmlinux
> > for VMLINUX_BTF at any of "  ./vmlinux", build the kernel or set
> > VMLINUX_BTF variable.  Stop.
> >
> > Cc: Toke Høiland-Jørgensen <toke@redhat.com>
> > Fixes: 384b6b3bbf0d (samples: bpf: Add vmlinux.h generation support)
> > Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> > ---
> >  samples/bpf/Makefile                     | 13 ++++++-------
> >  samples/bpf/xdp_redirect_map_multi.bpf.c |  5 -----
> >  2 files changed, 6 insertions(+), 12 deletions(-)
> >
> > diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
> > index 4dc20be5fb96..a05130e91403 100644
> > --- a/samples/bpf/Makefile
> > +++ b/samples/bpf/Makefile
> > @@ -324,15 +324,9 @@ $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
> >
> >  VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)                           \
> >                      $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)    \
> > -                    ../../../../vmlinux                                \
> > -                    /sys/kernel/btf/vmlinux                            \
> > -                    /boot/vmlinux-$(shell uname -r)
> > +                    ./vmlinux
>
> isn't this relative to samples/bpf subdirectory, so shouldn't it be
> ../../vmlinux?
>

I was confused about this too; but it executes this when you call make:

# Trick to allow make to be run from this directory
all:
        $(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)

so it fails if the path isn't relative to kernel source root.

> >  VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
> >
> > -ifeq ($(VMLINUX_BTF),)
> > -$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
> > -endif
> > -
>
> [...]

--
Kartikeya
Andrii Nakryiko Sept. 28, 2021, 5:28 a.m. UTC | #3
On Mon, Sep 27, 2021 at 9:51 PM Kumar Kartikeya Dwivedi
<memxor@gmail.com> wrote:
>
> On Tue, Sep 28, 2021 at 10:06:09AM IST, Andrii Nakryiko wrote:
> > On Mon, Sep 27, 2021 at 9:16 PM Kumar Kartikeya Dwivedi
> > <memxor@gmail.com> wrote:
> > >
> > > Generate vmlinux.h only from the in-tree vmlinux, and remove enum
> > > declarations that would cause a build failure in case of version
> > > mismatches.
> > >
> > > There are now two options when building the samples:
> > > 1. Compile the kernel to use in-tree vmlinux for vmlinux.h
> > > 2. Override VMLINUX_BTF for samples using something like this:
> > >    make VMLINUX_BTF=/sys/kernel/btf/vmlinux -C samples/bpf
> > >
> > > This change was tested with relative builds, e.g. cases like:
> > >  * make O=build -C samples/bpf
> > >  * make KBUILD_OUTPUT=build -C samples/bpf
> > >  * make -C samples/bpf
> > >  * cd samples/bpf && make
> > >
> > > When a suitable VMLINUX_BTF is not found, the following message is
> > > printed:
> > > /home/kkd/src/linux/samples/bpf/Makefile:333: *** Cannot find a vmlinux
> > > for VMLINUX_BTF at any of "  ./vmlinux", build the kernel or set
> > > VMLINUX_BTF variable.  Stop.
> > >
> > > Cc: Toke Høiland-Jørgensen <toke@redhat.com>
> > > Fixes: 384b6b3bbf0d (samples: bpf: Add vmlinux.h generation support)
> > > Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> > > ---
> > >  samples/bpf/Makefile                     | 13 ++++++-------
> > >  samples/bpf/xdp_redirect_map_multi.bpf.c |  5 -----
> > >  2 files changed, 6 insertions(+), 12 deletions(-)
> > >
> > > diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
> > > index 4dc20be5fb96..a05130e91403 100644
> > > --- a/samples/bpf/Makefile
> > > +++ b/samples/bpf/Makefile
> > > @@ -324,15 +324,9 @@ $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
> > >
> > >  VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)                           \
> > >                      $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)    \
> > > -                    ../../../../vmlinux                                \
> > > -                    /sys/kernel/btf/vmlinux                            \
> > > -                    /boot/vmlinux-$(shell uname -r)
> > > +                    ./vmlinux
> >
> > isn't this relative to samples/bpf subdirectory, so shouldn't it be
> > ../../vmlinux?
> >
>
> I was confused about this too; but it executes this when you call make:
>
> # Trick to allow make to be run from this directory
> all:
>         $(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)
>
> so it fails if the path isn't relative to kernel source root.

Hm... what if we do $(abspath ./vmlinux) here (and probably same for
$(O) and $(KBUILD_OUTPUT) just in case those are relative as well,
would that do the right thing here? The way it is right now it's
extremely confusing (and obviously ../../../../vmlinux never worked
and we never knew that).

>
> > >  VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
> > >
> > > -ifeq ($(VMLINUX_BTF),)
> > > -$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
> > > -endif
> > > -
> >
> > [...]
>
> --
> Kartikeya
Kumar Kartikeya Dwivedi Sept. 28, 2021, 5:44 a.m. UTC | #4
On Tue, Sep 28, 2021 at 10:58:01AM IST, Andrii Nakryiko wrote:
> On Mon, Sep 27, 2021 at 9:51 PM Kumar Kartikeya Dwivedi
> <memxor@gmail.com> wrote:
> >
> > On Tue, Sep 28, 2021 at 10:06:09AM IST, Andrii Nakryiko wrote:
> > > On Mon, Sep 27, 2021 at 9:16 PM Kumar Kartikeya Dwivedi
> > > <memxor@gmail.com> wrote:
> > > >
> > > > Generate vmlinux.h only from the in-tree vmlinux, and remove enum
> > > > declarations that would cause a build failure in case of version
> > > > mismatches.
> > > >
> > > > There are now two options when building the samples:
> > > > 1. Compile the kernel to use in-tree vmlinux for vmlinux.h
> > > > 2. Override VMLINUX_BTF for samples using something like this:
> > > >    make VMLINUX_BTF=/sys/kernel/btf/vmlinux -C samples/bpf
> > > >
> > > > This change was tested with relative builds, e.g. cases like:
> > > >  * make O=build -C samples/bpf
> > > >  * make KBUILD_OUTPUT=build -C samples/bpf
> > > >  * make -C samples/bpf
> > > >  * cd samples/bpf && make
> > > >
> > > > When a suitable VMLINUX_BTF is not found, the following message is
> > > > printed:
> > > > /home/kkd/src/linux/samples/bpf/Makefile:333: *** Cannot find a vmlinux
> > > > for VMLINUX_BTF at any of "  ./vmlinux", build the kernel or set
> > > > VMLINUX_BTF variable.  Stop.
> > > >
> > > > Cc: Toke Høiland-Jørgensen <toke@redhat.com>
> > > > Fixes: 384b6b3bbf0d (samples: bpf: Add vmlinux.h generation support)
> > > > Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> > > > ---
> > > >  samples/bpf/Makefile                     | 13 ++++++-------
> > > >  samples/bpf/xdp_redirect_map_multi.bpf.c |  5 -----
> > > >  2 files changed, 6 insertions(+), 12 deletions(-)
> > > >
> > > > diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
> > > > index 4dc20be5fb96..a05130e91403 100644
> > > > --- a/samples/bpf/Makefile
> > > > +++ b/samples/bpf/Makefile
> > > > @@ -324,15 +324,9 @@ $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
> > > >
> > > >  VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)                           \
> > > >                      $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)    \
> > > > -                    ../../../../vmlinux                                \
> > > > -                    /sys/kernel/btf/vmlinux                            \
> > > > -                    /boot/vmlinux-$(shell uname -r)
> > > > +                    ./vmlinux
> > >
> > > isn't this relative to samples/bpf subdirectory, so shouldn't it be
> > > ../../vmlinux?
> > >
> >
> > I was confused about this too; but it executes this when you call make:
> >
> > # Trick to allow make to be run from this directory
> > all:
> >         $(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)
> >
> > so it fails if the path isn't relative to kernel source root.
>
> Hm... what if we do $(abspath ./vmlinux) here (and probably same for
> $(O) and $(KBUILD_OUTPUT) just in case those are relative as well,
> would that do the right thing here? The way it is right now it's

Yeah, that works. I'll send a v2.

> extremely confusing (and obviously ../../../../vmlinux never worked
> and we never knew that).
>

Indeed.

> >
> > > >  VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
> > > >
> > > > -ifeq ($(VMLINUX_BTF),)
> > > > -$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
> > > > -endif
> > > > -
> > >
> > > [...]
> >
> > --
> > Kartikeya

--
Kartikeya
diff mbox series

Patch

diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 4dc20be5fb96..a05130e91403 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -324,15 +324,9 @@  $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h

 VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
 		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
-		     ../../../../vmlinux				\
-		     /sys/kernel/btf/vmlinux				\
-		     /boot/vmlinux-$(shell uname -r)
+		     ./vmlinux
 VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))

-ifeq ($(VMLINUX_BTF),)
-$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
-endif
-
 $(obj)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL)
 ifeq ($(VMLINUX_H),)
 	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
@@ -340,6 +334,11 @@  else
 	$(Q)cp "$(VMLINUX_H)" $@
 endif

+ifeq ($(VMLINUX_BTF),)
+	$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)",\
+		build the kernel or set VMLINUX_BTF variable)
+endif
+
 clean-files += vmlinux.h

 # Get Clang's default includes on this system, as opposed to those seen by
diff --git a/samples/bpf/xdp_redirect_map_multi.bpf.c b/samples/bpf/xdp_redirect_map_multi.bpf.c
index 8f59d430cb64..bb0a5a3bfcf0 100644
--- a/samples/bpf/xdp_redirect_map_multi.bpf.c
+++ b/samples/bpf/xdp_redirect_map_multi.bpf.c
@@ -5,11 +5,6 @@ 
 #include "xdp_sample.bpf.h"
 #include "xdp_sample_shared.h"

-enum {
-	BPF_F_BROADCAST		= (1ULL << 3),
-	BPF_F_EXCLUDE_INGRESS	= (1ULL << 4),
-};
-
 struct {
 	__uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
 	__uint(key_size, sizeof(int));