diff mbox series

[bpf-next,1/3] samples: bpf: Fix cross-compiling error by using bootstrap bpftool

Message ID 20220712030813.865410-2-pulehui@huawei.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Use lightweigt version of bpftool | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 5 maintainers not CCed: haoluo@google.com song@kernel.org martin.lau@linux.dev jolsa@kernel.org sdf@google.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 23 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Kernel LATEST on ubuntu-latest with llvm-15
bpf/vmtest-bpf-next-VM_Test-1 success Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Kernel LATEST on z15 with gcc

Commit Message

Pu Lehui July 12, 2022, 3:08 a.m. UTC
Currently, when cross compiling bpf samples, the host side cannot
use arch-specific bpftool to generate vmlinux.h or skeleton. Since
samples/bpf use bpftool for vmlinux.h, skeleton, and static linking
only, we can use lightweight bootstrap version of bpftool to handle
these, and it's always host-native.

Signed-off-by: Pu Lehui <pulehui@huawei.com>
Suggested-by: Andrii Nakryiko <andrii@kernel.org>
---
 samples/bpf/Makefile | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

Comments

Quentin Monnet July 12, 2022, 10:11 a.m. UTC | #1
On 12/07/2022 04:08, Pu Lehui wrote:
> Currently, when cross compiling bpf samples, the host side cannot
> use arch-specific bpftool to generate vmlinux.h or skeleton. Since
> samples/bpf use bpftool for vmlinux.h, skeleton, and static linking
> only, we can use lightweight bootstrap version of bpftool to handle
> these, and it's always host-native.
> 
> Signed-off-by: Pu Lehui <pulehui@huawei.com>
> Suggested-by: Andrii Nakryiko <andrii@kernel.org>
> ---
>  samples/bpf/Makefile | 16 +++++++++++-----
>  1 file changed, 11 insertions(+), 5 deletions(-)
> 
> diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
> index 5002a5b9a7da..57012b8259d2 100644
> --- a/samples/bpf/Makefile
> +++ b/samples/bpf/Makefile
> @@ -282,12 +282,18 @@ $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU
>  
>  BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
>  BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
> -BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool
> +BPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool
> +ifeq ($(CROSS_COMPILE),)
>  $(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
> -	    $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \
> -		OUTPUT=$(BPFTOOL_OUTPUT)/ \
> -		LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \
> -		LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/
> +	$(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../		\
> +		OUTPUT=$(BPFTOOL_OUTPUT)/ 					\
> +		LIBBPF_BOOTSTRAP_OUTPUT=$(LIBBPF_OUTPUT)/ 			\
> +		LIBBPF_BOOTSTRAP_DESTDIR=$(LIBBPF_DESTDIR)/ bootstrap
> +else
> +$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)

Thanks for this! Just trying to fully understand the details here. When
cross-compiling, you leave aside the dependency on target-arch-libbpf,
so that "make -C <bpftool-dir> bootstrap" rebuilds its own host-arch
libbpf, is this correct?

> +	$(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ 		\
> +		OUTPUT=$(BPFTOOL_OUTPUT)/ bootstrap
> +endif
>  
>  $(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
>  	$(call msg,MKDIR,$@)
Pu Lehui July 12, 2022, 11:32 a.m. UTC | #2
On 2022/7/12 18:11, Quentin Monnet wrote:
> On 12/07/2022 04:08, Pu Lehui wrote:
>> Currently, when cross compiling bpf samples, the host side cannot
>> use arch-specific bpftool to generate vmlinux.h or skeleton. Since
>> samples/bpf use bpftool for vmlinux.h, skeleton, and static linking
>> only, we can use lightweight bootstrap version of bpftool to handle
>> these, and it's always host-native.
>>
>> Signed-off-by: Pu Lehui <pulehui@huawei.com>
>> Suggested-by: Andrii Nakryiko <andrii@kernel.org>
>> ---
>>   samples/bpf/Makefile | 16 +++++++++++-----
>>   1 file changed, 11 insertions(+), 5 deletions(-)
>>
>> diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
>> index 5002a5b9a7da..57012b8259d2 100644
>> --- a/samples/bpf/Makefile
>> +++ b/samples/bpf/Makefile
>> @@ -282,12 +282,18 @@ $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU
>>   
>>   BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
>>   BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
>> -BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool
>> +BPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool
>> +ifeq ($(CROSS_COMPILE),)
>>   $(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
>> -	    $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \
>> -		OUTPUT=$(BPFTOOL_OUTPUT)/ \
>> -		LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \
>> -		LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/
>> +	$(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../		\
>> +		OUTPUT=$(BPFTOOL_OUTPUT)/ 					\
>> +		LIBBPF_BOOTSTRAP_OUTPUT=$(LIBBPF_OUTPUT)/ 			\
>> +		LIBBPF_BOOTSTRAP_DESTDIR=$(LIBBPF_DESTDIR)/ bootstrap
>> +else
>> +$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
> 
> Thanks for this! Just trying to fully understand the details here. When
> cross-compiling, you leave aside the dependency on target-arch-libbpf,
> so that "make -C <bpftool-dir> bootstrap" rebuilds its own host-arch
> libbpf, is this correct?
> 

You're right. libbpf may does get out-of-sync. So the best way is to 
compile both arch-specific libbpf simultaneously, and then attach to 
bpftool. But it will make this job more complicated. Could we just add 
back $(LIBBPF) to handle this?

>> +	$(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ 		\
>> +		OUTPUT=$(BPFTOOL_OUTPUT)/ bootstrap
>> +endif
>>   
>>   $(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
>>   	$(call msg,MKDIR,$@)
> 
> .
>
Andrii Nakryiko July 13, 2022, 6:50 p.m. UTC | #3
On Tue, Jul 12, 2022 at 4:32 AM Pu Lehui <pulehui@huawei.com> wrote:
>
>
>
> On 2022/7/12 18:11, Quentin Monnet wrote:
> > On 12/07/2022 04:08, Pu Lehui wrote:
> >> Currently, when cross compiling bpf samples, the host side cannot
> >> use arch-specific bpftool to generate vmlinux.h or skeleton. Since
> >> samples/bpf use bpftool for vmlinux.h, skeleton, and static linking
> >> only, we can use lightweight bootstrap version of bpftool to handle
> >> these, and it's always host-native.
> >>
> >> Signed-off-by: Pu Lehui <pulehui@huawei.com>
> >> Suggested-by: Andrii Nakryiko <andrii@kernel.org>
> >> ---
> >>   samples/bpf/Makefile | 16 +++++++++++-----
> >>   1 file changed, 11 insertions(+), 5 deletions(-)
> >>
> >> diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
> >> index 5002a5b9a7da..57012b8259d2 100644
> >> --- a/samples/bpf/Makefile
> >> +++ b/samples/bpf/Makefile
> >> @@ -282,12 +282,18 @@ $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU
> >>
> >>   BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
> >>   BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
> >> -BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool
> >> +BPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool
> >> +ifeq ($(CROSS_COMPILE),)
> >>   $(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
> >> -        $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \
> >> -            OUTPUT=$(BPFTOOL_OUTPUT)/ \
> >> -            LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \
> >> -            LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/
> >> +    $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../             \
> >> +            OUTPUT=$(BPFTOOL_OUTPUT)/                                       \
> >> +            LIBBPF_BOOTSTRAP_OUTPUT=$(LIBBPF_OUTPUT)/                       \
> >> +            LIBBPF_BOOTSTRAP_DESTDIR=$(LIBBPF_DESTDIR)/ bootstrap
> >> +else
> >> +$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
> >
> > Thanks for this! Just trying to fully understand the details here. When
> > cross-compiling, you leave aside the dependency on target-arch-libbpf,
> > so that "make -C <bpftool-dir> bootstrap" rebuilds its own host-arch
> > libbpf, is this correct?
> >
>
> You're right. libbpf may does get out-of-sync. So the best way is to
> compile both arch-specific libbpf simultaneously, and then attach to
> bpftool. But it will make this job more complicated. Could we just add
> back $(LIBBPF) to handle this?
>

Maybe let's keep it simple and let bpftool's Makefile deal with
cross-compile issue and building its own libbpf? So just request
bootstrap, but not try to share libbpf between samples/bpf and
bpftool? Especially that is this "samples", such complexity in
Makefile seems like a micro-optimization.

> >> +    $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../             \
> >> +            OUTPUT=$(BPFTOOL_OUTPUT)/ bootstrap
> >> +endif
> >>
> >>   $(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
> >>      $(call msg,MKDIR,$@)
> >
> > .
> >
diff mbox series

Patch

diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 5002a5b9a7da..57012b8259d2 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -282,12 +282,18 @@  $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU
 
 BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
 BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
-BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool
+BPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool
+ifeq ($(CROSS_COMPILE),)
 $(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
-	    $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \
-		OUTPUT=$(BPFTOOL_OUTPUT)/ \
-		LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \
-		LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/
+	$(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../		\
+		OUTPUT=$(BPFTOOL_OUTPUT)/ 					\
+		LIBBPF_BOOTSTRAP_OUTPUT=$(LIBBPF_OUTPUT)/ 			\
+		LIBBPF_BOOTSTRAP_DESTDIR=$(LIBBPF_DESTDIR)/ bootstrap
+else
+$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
+	$(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ 		\
+		OUTPUT=$(BPFTOOL_OUTPUT)/ bootstrap
+endif
 
 $(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
 	$(call msg,MKDIR,$@)