perf build: Properly guard libbpf includes
Subsystems (2)
Ian Rogers
reviewerPATCH v2 1/2 perf build: Properly guard libbpf includes
Including libbpf header files should be guarded by HAVE_LIBBPF_SUPPORT. In bpf_counter.h, move the skeleton utilities under HAVE_BPF_SKEL.
--- tools/perf/builtin-trace.c | 2 + tools/perf/util/bpf_counter.h | 85 ++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 41 deletions(-)
tools/perf/builtin-trace.c +2 -0
@@ -16,7 +16,9 @@#include "util/record.h" #include <api/fs/tracing_path.h> +#ifdef HAVE_LIBBPF_SUPPORT #include <bpf/bpf.h> +#endif #include "util/bpf_map.h" #include "util/rlimit.h" #include "builtin.h"
tools/perf/util/bpf_counter.h +44 -41
104 lines not on this page · raw message
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Ian Rogers
reviewerPATCH v2 2/2 perf build: Fix build error when NO_LIBBPF=1
The $(LIBBPF) target should only be a dependency of prepare if the static version of libbpf is needed. Add a new LIBBPF_STATIC variable that is set by Makefile.config. Use LIBBPF_STATIC to determine whether the CFLAGS, etc. need updating and for adding $(LIBBPF) as a prepare dependency.
As Makefile.config isn't loaded for "clean" as a target, always set LIBBPF_OUTPUT regardless of whether it is needed for $(LIBBPF). This is done to minimize conditional logic for $(LIBBPF)-clean.
This issue and an original fix was reported by Mike Leach in:
https://lore.kernel.org/lkml/[email protected]/
--- tools/perf/Makefile.config | 2 ++ tools/perf/Makefile.perf | 21 ++++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-)
tools/perf/Makefile.config +2 -0
@@ -602,6 +602,8 @@ ifndef NO_LIBELF dummy := $(error Error: No libbpf devel library found, please install libbpf-devel); endif else + # Libbpf will be built as a static library from tools/lib/bpf. + LIBBPF_STATIC := 1 CFLAGS += -DHAVE_LIBBPF_BTF__LOAD_FROM_KERNEL_BY_ID CFLAGS += -DHAVE_LIBBPF_BPF_PROG_LOAD CFLAGS += -DHAVE_LIBBPF_BPF_OBJECT__NEXT_PROGRAM
tools/perf/Makefile.perf +12 -9
@@ -303,10 +303,12 @@ ifneq ($(OUTPUT),) else LIBBPF_OUTPUT = $(CURDIR)/libbpf endif -LIBBPF_DESTDIR = $(LIBBPF_OUTPUT) -LIBBPF_INCLUDE = $(LIBBPF_DESTDIR)/include -LIBBPF = $(LIBBPF_OUTPUT)/libbpf.a -CFLAGS += -I$(LIBBPF_OUTPUT)/include +ifdef LIBBPF_STATIC + LIBBPF_DESTDIR = $(LIBBPF_OUTPUT) + LIBBPF_INCLUDE = $(LIBBPF_DESTDIR)/include + LIBBPF = $(LIBBPF_OUTPUT)/libbpf.a + CFLAGS += -I$(LIBBPF_OUTPUT)/include +endif ifneq ($(OUTPUT),) LIBSUBCMD_OUTPUT = $(abspath $(OUTPUT))/libsubcmd
@@ -393,10 +395,8 @@ endif export PERL_PATH PERFLIBS = $(LIBAPI) $(LIBPERF) $(LIBSUBCMD) $(LIBSYMBOL) -ifndef NO_LIBBPF - ifndef LIBBPF_DYNAMIC - PERFLIBS += $(LIBBPF) - endif +ifdef LIBBPF_STATIC + PERFLIBS += $(LIBBPF) endif # We choose to avoid "if .. else if .. else .. endif endif"
@@ -756,12 +756,15 @@ prepare: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h archheaders $(drm_ioc $(arch_errno_name_array) \ $(sync_file_range_arrays) \ $(LIBAPI) \ - $(LIBBPF) \ $(LIBPERF) \ $(LIBSUBCMD) \ $(LIBSYMBOL) \ bpf-skel +ifdef LIBBPF_STATIC +prepare: $(LIBBPF) +endif + $(OUTPUT)%.o: %.c prepare FORCE $(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=$(build-dir) $@
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Re: [PATCH v2 2/2] perf build: Fix build error when NO_LIBBPF=1
Quoted 87 lines · Ian Rogers
The $(LIBBPF) target should only be a dependency of prepare if the static version of libbpf is needed. Add a new
LIBBPF_STATICvariable that is set by Makefile.config. UseLIBBPF_STATICto determine whether the CFLAGS, etc. need updating and for adding $(LIBBPF) as a prepare dependency.As Makefile.config isn't loaded for "clean" as a target, always set
LIBBPF_OUTPUTregardless of whether it is needed for $(LIBBPF). This is done to minimize conditional logic for $(LIBBPF)-clean.This issue and an original fix was reported by Mike Leach in:
https://lore.kernel.org/lkml/[email protected]/Fixes 746bd29e348f ("perf build: Use tools/lib headers from install path")Reported-by Mike Leach <[email protected]>Signed-off-by Ian Rogers <[email protected]>--- tools/perf/Makefile.config | 2 ++ tools/perf/Makefile.perf | 21 ++++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-)tools/perf/Makefile.config +2
-0@@ -602,6 +602,8 @@ ifndef NO_LIBELF dummy := $(error Error: No libbpf devel library found, please install libbpf-devel); endif else + # Libbpf will be built as a static library from tools/lib/bpf. + LIBBPF_STATIC := 1 CFLAGS += -DHAVE_LIBBPF_BTF__LOAD_FROM_KERNEL_BY_ID CFLAGS += -DHAVE_LIBBPF_BPF_PROG_LOAD CFLAGS += -DHAVE_LIBBPF_BPF_OBJECT__NEXT_PROGRAMtools/perf/Makefile.perf +12
-10@@ -303,10 +303,12 @@ ifneq ($(OUTPUT),) else LIBBPF_OUTPUT = $(CURDIR)/libbpf endif -LIBBPF_DESTDIR = $(LIBBPF_OUTPUT) -LIBBPF_INCLUDE = $(LIBBPF_DESTDIR)/include -LIBBPF = $(LIBBPF_OUTPUT)/libbpf.a -CFLAGS += -I$(LIBBPF_OUTPUT)/include +ifdef LIBBPF_STATIC + LIBBPF_DESTDIR = $(LIBBPF_OUTPUT) + LIBBPF_INCLUDE = $(LIBBPF_DESTDIR)/include + LIBBPF = $(LIBBPF_OUTPUT)/libbpf.a + CFLAGS += -I$(LIBBPF_OUTPUT)/include +endif ifneq ($(OUTPUT),) LIBSUBCMD_OUTPUT = $(abspath $(OUTPUT))/libsubcmd@@ -393,10 +395,8 @@ endif export PERL_PATH PERFLIBS = $(LIBAPI) $(LIBPERF) $(LIBSUBCMD) $(LIBSYMBOL) -ifndef NO_LIBBPF - ifndef LIBBPF_DYNAMIC - PERFLIBS += $(LIBBPF) - endif +ifdef LIBBPF_STATIC + PERFLIBS += $(LIBBPF) endif # We choose to avoid "if .. else if .. else .. endif endif"@@ -756,12 +756,15 @@ prepare: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h archheaders $(drm_ioc $(arch_errno_name_array) \ $(sync_file_range_arrays) \ $(LIBAPI) \ - $(LIBBPF) \ $(LIBPERF) \ $(LIBSUBCMD) \ $(LIBSYMBOL) \ bpf-skel +ifdef LIBBPF_STATIC +prepare: $(LIBBPF) +endif + $(OUTPUT)%.o: %.c prepare FORCE $(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=$(build-dir) $@ --
Quoted 2 lines
2.39.0.314.g84b9a713c41-goog
Signature · 3 lines
Mike Leach Principal Engineer, ARM Ltd. Manchester Design Centre. UK
Arnaldo Carvalho de Melo
maintainerEm Fri, Jan 06, 2023 at 06:25:36AM -0800, Ian Rogers escreveu:
Quoted 7 lines
Including libbpf header files should be guarded by
HAVE_LIBBPF_SUPPORT. Inbpf_counter.h, move the skeleton utilities underHAVE_BPF_SKEL.Fixes d6a735ef3277 ("perf bpf_counter: Move common functions to bpf_counter.h")Reported-by Mike Leach <[email protected]>Signed-off-by Ian Rogers <[email protected]>
Can this be done in a way that reduces patch size?
- Arnaldo
Quoted 132 lines
--- tools/perf/builtin-trace.c | 2 + tools/perf/util/bpf_counter.h | 85 ++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 41 deletions(-)tools/perf/builtin-trace.c +2
-0@@ -16,7 +16,9 @@#include "util/record.h" #include <api/fs/tracing_path.h> +#ifdef HAVE_LIBBPF_SUPPORT #include <bpf/bpf.h> +#endif #include "util/bpf_map.h" #include "util/rlimit.h" #include "builtin.h" tools/perf/util/bpf_counter.h +44
-41@@ -4,9 +4,12 @@#include <linux/list.h> #include <sys/resource.h> + +#ifdef HAVE_LIBBPF_SUPPORT #include <bpf/bpf.h> #include <bpf/btf.h> #include <bpf/libbpf.h> +#endif struct evsel ;struct target ;@@ -42,6 +45,47 @@ int bpf_counter__read(struct evsel *evsel);void bpf_counter__destroy (struct evsel *evsel);int bpf_counter__install_pe (struct evsel *evsel,int cpu_map_idx,int fd); +static inline __u32 bpf_link_get_id (int fd) +{ +struct bpf_link_info link_info = { .id =0 , }; +__u32 link_info_len =sizeof (link_info); + +bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); +return link_info.id; +} + +static inline __u32 bpf_link_get_prog_id (int fd) +{ +struct bpf_link_info link_info = { .id =0 , }; +__u32 link_info_len =sizeof (link_info); + +bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); +return link_info.prog_id; +} + +static inline __u32 bpf_map_get_id (int fd) +{ +struct bpf_map_info map_info = { .id =0 , }; +__u32 map_info_len =sizeof (map_info); + +bpf_obj_get_info_by_fd (fd, &map_info, &map_info_len); +return map_info.id; +} + +/* trigger the leader program on a cpu */ +static inline int bperf_trigger_reading (int prog_fd,int cpu) +{ +DECLARE_LIBBPF_OPTS (bpf_test_run_opts, opts, + .ctx_in = NULL, + .ctx_size_in = 0, + .flags = BPF_F_TEST_RUN_ON_CPU, + .cpu = cpu, + .retval = 0, + ); + +return bpf_prog_test_run_opts (prog_fd, &opts); +} +#else /* HAVE_BPF_SKEL */ #include <linux/err.h> @@ -87,45 +131,4 @@ static inline void set_max_rlimit(void) setrlimit(RLIMIT_MEMLOCK, &rinf); } -static inline __u32 bpf_link_get_id (int fd) -{ -struct bpf_link_info link_info = { .id =0 , }; -__u32 link_info_len =sizeof (link_info); - -bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); -return link_info.id; -} - -static inline __u32 bpf_link_get_prog_id (int fd) -{ -struct bpf_link_info link_info = { .id =0 , }; -__u32 link_info_len =sizeof (link_info); - -bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); -return link_info.prog_id; -} - -static inline __u32 bpf_map_get_id (int fd) -{ -struct bpf_map_info map_info = { .id =0 , }; -__u32 map_info_len =sizeof (map_info); - -bpf_obj_get_info_by_fd (fd, &map_info, &map_info_len); -return map_info.id; -} - -/* trigger the leader program on a cpu */ -static inline int bperf_trigger_reading (int prog_fd,int cpu) -{ -DECLARE_LIBBPF_OPTS (bpf_test_run_opts, opts, - .ctx_in = NULL, - .ctx_size_in = 0, - .flags = BPF_F_TEST_RUN_ON_CPU, - .cpu = cpu, - .retval = 0, - ); - -return bpf_prog_test_run_opts (prog_fd, &opts); -} -#endif /* __PERF_BPF_COUNTER_H */ Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Signature · 1 line
- Arnaldo
Mike Leach
Tested-byQuoted 138 lines · Arnaldo Carvalho de Melo
Em Fri, Jan 06, 2023 at 06:25:36AM -0800, Ian Rogers escreveu:
Quoted 7 lines
Including libbpf header files should be guarded by
HAVE_LIBBPF_SUPPORT. Inbpf_counter.h, move the skeleton utilities underHAVE_BPF_SKEL.Fixes d6a735ef3277 ("perf bpf_counter: Move common functions to bpf_counter.h")Reported-by Mike Leach <[email protected]>Signed-off-by Ian Rogers <[email protected]>Can this be done in a way that reduces patch size?
- Arnaldo
Quoted 124 lines
--- tools/perf/builtin-trace.c | 2 + tools/perf/util/bpf_counter.h | 85 ++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 41 deletions(-)tools/perf/builtin-trace.c +2
-0@@ -16,7 +16,9 @@#include "util/record.h" #include <api/fs/tracing_path.h> +#ifdef HAVE_LIBBPF_SUPPORT #include <bpf/bpf.h> +#endif #include "util/bpf_map.h" #include "util/rlimit.h" #include "builtin.h" tools/perf/util/bpf_counter.h +44
-36@@ -4,9 +4,12 @@#include <linux/list.h> #include <sys/resource.h> + +#ifdef HAVE_LIBBPF_SUPPORT #include <bpf/bpf.h> #include <bpf/btf.h> #include <bpf/libbpf.h> +#endif struct evsel ;struct target ;@@ -42,6 +45,47 @@ int bpf_counter__read(struct evsel *evsel);void bpf_counter__destroy (struct evsel *evsel);int bpf_counter__install_pe (struct evsel *evsel,int cpu_map_idx,int fd); +static inline __u32 bpf_link_get_id (int fd) +{ +struct bpf_link_info link_info = { .id =0 , }; +__u32 link_info_len =sizeof (link_info); + +bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); +return link_info.id; +} + +static inline __u32 bpf_link_get_prog_id (int fd) +{ +struct bpf_link_info link_info = { .id =0 , }; +__u32 link_info_len =sizeof (link_info); + +bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); +return link_info.prog_id; +} + +static inline __u32 bpf_map_get_id (int fd) +{ +struct bpf_map_info map_info = { .id =0 , }; +__u32 map_info_len =sizeof (map_info); + +bpf_obj_get_info_by_fd (fd, &map_info, &map_info_len); +return map_info.id; +} + +/* trigger the leader program on a cpu */ +static inline int bperf_trigger_reading (int prog_fd,int cpu) +{ +DECLARE_LIBBPF_OPTS (bpf_test_run_opts, opts, + .ctx_in = NULL, + .ctx_size_in = 0, + .flags = BPF_F_TEST_RUN_ON_CPU, + .cpu = cpu, + .retval = 0, + ); + +return bpf_prog_test_run_opts (prog_fd, &opts); +} +#else /* HAVE_BPF_SKEL */ #include <linux/err.h> @@ -87,45 +131,4 @@ static inline void set_max_rlimit(void)setrlimit (RLIMIT_MEMLOCK , &rinf); } -static inline __u32 bpf_link_get_id (int fd) -{ -struct bpf_link_info link_info = { .id =0 , }; -__u32 link_info_len =sizeof (link_info); - -bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); -return link_info.id; -} - -static inline __u32 bpf_link_get_prog_id (int fd) -{ -struct bpf_link_info link_info = { .id =0 , }; -__u32 link_info_len =sizeof (link_info); - -bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); -return link_info.prog_id; -} - -static inline __u32 bpf_map_get_id (int fd) -{ -struct bpf_map_info map_info = { .id =0 , }; -__u32 map_info_len =sizeof (map_info); - -bpf_obj_get_info_by_fd (fd, &map_info, &map_info_len); -return map_info.id; -} - -/* trigger the leader program on a cpu */ -static inline int bperf_trigger_reading (int prog_fd,int cpu) -{ - DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, - .ctx_in = NULL, - .ctx_size_in = 0, - .flags = BPF_F_TEST_RUN_ON_CPU, - .cpu = cpu, - .retval = 0,
Quoted 9 lines
Quoted 8 lines
tools/perf/util/bpf_counter.h +0
-6- ); - -return bpf_prog_test_run_opts (prog_fd, &opts); -} -#endif /* __PERF_BPF_COUNTER_H */ --2.39.0.314.g84b9a713c41-goog
Quoted 3 lines
--
- Arnaldo
Signature · 3 lines
Mike Leach Principal Engineer, ARM Ltd. Manchester Design Centre. UK
Ian Rogers
reviewerQuoted 8 lines · Mike Leach
Quoted 6 lines · Arnaldo Carvalho de Melo
Em Fri, Jan 06, 2023 at 06:25:36AM -0800, Ian Rogers escreveu:
Quoted 4 lines
Including libbpf header files should be guarded by
HAVE_LIBBPF_SUPPORT. Inbpf_counter.h, move the skeleton utilities underHAVE_BPF_SKEL.
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Fixes d6a735ef3277 ("perf bpf_counter: Move common functions to bpf_counter.h")Reported-by Mike Leach <[email protected]>Signed-off-by Ian Rogers <[email protected]>Can this be done in a way that reduces patch size?
- Arnaldo
Done in v3. Thanks,
Ian
Quoted 146 lines
Quoted 125 lines
Quoted 125 lines
--- tools/perf/builtin-trace.c | 2 + tools/perf/util/bpf_counter.h | 85 ++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 41 deletions(-)tools/perf/builtin-trace.c +2
-0@@ -16,7 +16,9 @@#include "util/record.h" #include <api/fs/tracing_path.h> +#ifdef HAVE_LIBBPF_SUPPORT #include <bpf/bpf.h> +#endif #include "util/bpf_map.h" #include "util/rlimit.h" #include "builtin.h" tools/perf/util/bpf_counter.h +44
-36@@ -4,9 +4,12 @@#include <linux/list.h> #include <sys/resource.h> + +#ifdef HAVE_LIBBPF_SUPPORT #include <bpf/bpf.h> #include <bpf/btf.h> #include <bpf/libbpf.h> +#endif struct evsel ;struct target ;@@ -42,6 +45,47 @@ int bpf_counter__read(struct evsel *evsel);void bpf_counter__destroy (struct evsel *evsel);int bpf_counter__install_pe (struct evsel *evsel,int cpu_map_idx,int fd); +static inline __u32 bpf_link_get_id (int fd) +{ +struct bpf_link_info link_info = { .id =0 , }; +__u32 link_info_len =sizeof (link_info); + +bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); +return link_info.id; +} + +static inline __u32 bpf_link_get_prog_id (int fd) +{ +struct bpf_link_info link_info = { .id =0 , }; +__u32 link_info_len =sizeof (link_info); + +bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); +return link_info.prog_id; +} + +static inline __u32 bpf_map_get_id (int fd) +{ +struct bpf_map_info map_info = { .id =0 , }; +__u32 map_info_len =sizeof (map_info); + +bpf_obj_get_info_by_fd (fd, &map_info, &map_info_len); +return map_info.id; +} + +/* trigger the leader program on a cpu */ +static inline int bperf_trigger_reading (int prog_fd,int cpu) +{ +DECLARE_LIBBPF_OPTS (bpf_test_run_opts, opts, + .ctx_in = NULL, + .ctx_size_in = 0, + .flags = BPF_F_TEST_RUN_ON_CPU, + .cpu = cpu, + .retval = 0, + ); + +return bpf_prog_test_run_opts (prog_fd, &opts); +} +#else /* HAVE_BPF_SKEL */ #include <linux/err.h> @@ -87,45 +131,4 @@ static inline void set_max_rlimit(void)setrlimit (RLIMIT_MEMLOCK , &rinf); } -static inline __u32 bpf_link_get_id (int fd) -{ -struct bpf_link_info link_info = { .id =0 , }; -__u32 link_info_len =sizeof (link_info); - -bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); -return link_info.id; -} - -static inline __u32 bpf_link_get_prog_id (int fd) -{ -struct bpf_link_info link_info = { .id =0 , }; -__u32 link_info_len =sizeof (link_info); - -bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); -return link_info.prog_id; -} - -static inline __u32 bpf_map_get_id (int fd) -{ -struct bpf_map_info map_info = { .id =0 , }; -__u32 map_info_len =sizeof (map_info); - -bpf_obj_get_info_by_fd (fd, &map_info, &map_info_len); -return map_info.id; -} - -/* trigger the leader program on a cpu */ -static inline int bperf_trigger_reading (int prog_fd,int cpu) -{ - DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, - .ctx_in = NULL, - .ctx_size_in = 0, - .flags = BPF_F_TEST_RUN_ON_CPU, - .cpu = cpu, - .retval = 0,Quoted 9 lines
Quoted 8 lines
tools/perf/util/bpf_counter.h +0
-6- ); - -return bpf_prog_test_run_opts (prog_fd, &opts); -} -#endif /* __PERF_BPF_COUNTER_H */ --2.39.0.314.g84b9a713c41-goog
Tested-by Mike Leach <[email protected]>Quoted 3 lines
--
- Arnaldo
--
Mike Leach
Principal Engineer, ARM Ltd.
Manchester Design Centre. UK