Skip to content

Commit 0b46b75

Browse files
qmonnetborkmann
authored andcommitted
libbpf: Add LIBBPF_DEPRECATED_SINCE macro for scheduling API deprecations
Introduce a macro LIBBPF_DEPRECATED_SINCE(major, minor, message) to prepare the deprecation of two API functions. This macro marks functions as deprecated when libbpf's version reaches the values passed as an argument. As part of this change libbpf_version.h header is added with recorded major (LIBBPF_MAJOR_VERSION) and minor (LIBBPF_MINOR_VERSION) libbpf version macros. They are now part of libbpf public API and can be relied upon by user code. libbpf_version.h is installed system-wide along other libbpf public headers. Due to this new build-time auto-generated header, in-kernel applications relying on libbpf (resolve_btfids, bpftool, bpf_preload) are updated to include libbpf's output directory as part of a list of include search paths. Better fix would be to use libbpf's make_install target to install public API headers, but that clean up is left out as a future improvement. The build changes were tested by building kernel (with KBUILD_OUTPUT and O= specified explicitly), bpftool, libbpf, selftests/bpf, and resolve_btfids builds. No problems were detected. Note that because of the constraints of the C preprocessor we have to write a few lines of macro magic for each version used to prepare deprecation (0.6 for now). Also, use LIBBPF_DEPRECATED_SINCE() to schedule deprecation of btf__get_from_id() and btf__load(), which are replaced by btf__load_from_kernel_by_id() and btf__load_into_kernel(), respectively, starting from future libbpf v0.6. This is part of libbpf 1.0 effort ([0]). [0] Closes: libbpf/libbpf#278 Co-developed-by: Quentin Monnet <[email protected]> Co-developed-by: Andrii Nakryiko <[email protected]> Signed-off-by: Quentin Monnet <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 006a509 commit 0b46b75

File tree

7 files changed

+53
-13
lines changed

7 files changed

+53
-13
lines changed

kernel/bpf/preload/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ LIBBPF_OUT = $(abspath $(obj))
1010
$(LIBBPF_A):
1111
$(Q)$(MAKE) -C $(LIBBPF_SRCS) O=$(LIBBPF_OUT)/ OUTPUT=$(LIBBPF_OUT)/ $(LIBBPF_OUT)/libbpf.a
1212

13-
userccflags += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi \
13+
userccflags += -I$(LIBBPF_OUT) -I $(srctree)/tools/include/ \
14+
-I $(srctree)/tools/include/uapi \
1415
-I $(srctree)/tools/lib/ -Wno-unused-result
1516

1617
userprogs := bpf_preload_umd
1718

18-
clean-files := $(userprogs) bpf_helper_defs.h FEATURE-DUMP.libbpf staticobjs/ feature/
19+
clean-files := $(userprogs) libbpf_version.h bpf_helper_defs.h FEATURE-DUMP.libbpf staticobjs/ feature/
20+
21+
$(obj)/iterators/iterators.o: $(LIBBPF_A)
1922

2023
bpf_preload_umd-objs := iterators/iterators.o
2124
bpf_preload_umd-userldlibs := $(LIBBPF_A) -lelf -lz

tools/bpf/bpftool/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
6060
CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
6161
CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
6262
-I$(if $(OUTPUT),$(OUTPUT),.) \
63+
$(if $(LIBBPF_OUTPUT),-I$(LIBBPF_OUTPUT)) \
6364
-I$(srctree)/kernel/bpf/ \
6465
-I$(srctree)/tools/include \
6566
-I$(srctree)/tools/include/uapi \
@@ -137,7 +138,10 @@ endif
137138
BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
138139

139140
BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o xlated_dumper.o btf_dumper.o disasm.o)
141+
$(BOOTSTRAP_OBJS): $(LIBBPF_BOOTSTRAP)
142+
140143
OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
144+
$(OBJS): $(LIBBPF)
141145

142146
VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
143147
$(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \

tools/bpf/resolve_btfids/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ LIBBPF_SRC := $(srctree)/tools/lib/bpf/
2626
SUBCMD_SRC := $(srctree)/tools/lib/subcmd/
2727

2828
BPFOBJ := $(OUTPUT)/libbpf/libbpf.a
29+
LIBBPF_OUT := $(abspath $(dir $(BPFOBJ)))/
2930
SUBCMDOBJ := $(OUTPUT)/libsubcmd/libsubcmd.a
3031

3132
BINARY := $(OUTPUT)/resolve_btfids
@@ -41,11 +42,12 @@ $(SUBCMDOBJ): fixdep FORCE | $(OUTPUT)/libsubcmd
4142
$(Q)$(MAKE) -C $(SUBCMD_SRC) OUTPUT=$(abspath $(dir $@))/ $(abspath $@)
4243

4344
$(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT)/libbpf
44-
$(Q)$(MAKE) $(submake_extras) -C $(LIBBPF_SRC) OUTPUT=$(abspath $(dir $@))/ $(abspath $@)
45+
$(Q)$(MAKE) $(submake_extras) -C $(LIBBPF_SRC) OUTPUT=$(LIBBPF_OUT) $(abspath $@)
4546

4647
CFLAGS := -g \
4748
-I$(srctree)/tools/include \
4849
-I$(srctree)/tools/include/uapi \
50+
-I$(LIBBPF_OUT) \
4951
-I$(LIBBPF_SRC) \
5052
-I$(SUBCMD_SRC)
5153

@@ -54,7 +56,7 @@ LIBS = -lelf -lz
5456
export srctree OUTPUT CFLAGS Q
5557
include $(srctree)/tools/build/Makefile.include
5658

57-
$(BINARY_IN): fixdep FORCE | $(OUTPUT)
59+
$(BINARY_IN): $(BPFOBJ) fixdep FORCE | $(OUTPUT)
5860
$(Q)$(MAKE) $(build)=resolve_btfids
5961

6062
$(BINARY): $(BPFOBJ) $(SUBCMDOBJ) $(BINARY_IN)

tools/lib/bpf/Makefile

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ VERSION_SCRIPT := libbpf.map
88
LIBBPF_VERSION := $(shell \
99
grep -oE '^LIBBPF_([0-9.]+)' $(VERSION_SCRIPT) | \
1010
sort -rV | head -n1 | cut -d'_' -f2)
11-
LIBBPF_MAJOR_VERSION := $(firstword $(subst ., ,$(LIBBPF_VERSION)))
11+
LIBBPF_MAJOR_VERSION := $(word 1,$(subst ., ,$(LIBBPF_VERSION)))
12+
LIBBPF_MINOR_VERSION := $(word 2,$(subst ., ,$(LIBBPF_VERSION)))
1213

1314
MAKEFLAGS += --no-print-directory
1415

@@ -59,7 +60,8 @@ ifndef VERBOSE
5960
VERBOSE = 0
6061
endif
6162

62-
INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/include/uapi
63+
INCLUDES = -I$(if $(OUTPUT),$(OUTPUT),.) \
64+
-I$(srctree)/tools/include -I$(srctree)/tools/include/uapi
6365

6466
export prefix libdir src obj
6567

@@ -111,7 +113,9 @@ SHARED_OBJDIR := $(OUTPUT)sharedobjs/
111113
STATIC_OBJDIR := $(OUTPUT)staticobjs/
112114
BPF_IN_SHARED := $(SHARED_OBJDIR)libbpf-in.o
113115
BPF_IN_STATIC := $(STATIC_OBJDIR)libbpf-in.o
116+
VERSION_HDR := $(OUTPUT)libbpf_version.h
114117
BPF_HELPER_DEFS := $(OUTPUT)bpf_helper_defs.h
118+
BPF_GENERATED := $(BPF_HELPER_DEFS) $(VERSION_HDR)
115119

116120
LIB_TARGET := $(addprefix $(OUTPUT),$(LIB_TARGET))
117121
LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
@@ -136,7 +140,7 @@ all: fixdep
136140

137141
all_cmd: $(CMD_TARGETS) check
138142

139-
$(BPF_IN_SHARED): force $(BPF_HELPER_DEFS)
143+
$(BPF_IN_SHARED): force $(BPF_GENERATED)
140144
@(test -f ../../include/uapi/linux/bpf.h -a -f ../../../include/uapi/linux/bpf.h && ( \
141145
(diff -B ../../include/uapi/linux/bpf.h ../../../include/uapi/linux/bpf.h >/dev/null) || \
142146
echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h'" >&2 )) || true
@@ -154,13 +158,19 @@ $(BPF_IN_SHARED): force $(BPF_HELPER_DEFS)
154158
echo "Warning: Kernel ABI header at 'tools/include/uapi/linux/if_xdp.h' differs from latest version at 'include/uapi/linux/if_xdp.h'" >&2 )) || true
155159
$(Q)$(MAKE) $(build)=libbpf OUTPUT=$(SHARED_OBJDIR) CFLAGS="$(CFLAGS) $(SHLIB_FLAGS)"
156160

157-
$(BPF_IN_STATIC): force $(BPF_HELPER_DEFS)
161+
$(BPF_IN_STATIC): force $(BPF_GENERATED)
158162
$(Q)$(MAKE) $(build)=libbpf OUTPUT=$(STATIC_OBJDIR)
159163

160164
$(BPF_HELPER_DEFS): $(srctree)/tools/include/uapi/linux/bpf.h
161165
$(QUIET_GEN)$(srctree)/scripts/bpf_doc.py --header \
162166
--file $(srctree)/tools/include/uapi/linux/bpf.h > $(BPF_HELPER_DEFS)
163167

168+
$(VERSION_HDR): force
169+
$(QUIET_GEN)echo "/* This file was auto-generated. */" > $@
170+
@echo "" >> $@
171+
@echo "#define LIBBPF_MAJOR_VERSION $(LIBBPF_MAJOR_VERSION)" >> $@
172+
@echo "#define LIBBPF_MINOR_VERSION $(LIBBPF_MINOR_VERSION)" >> $@
173+
164174
$(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)
165175

166176
$(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN_SHARED) $(VERSION_SCRIPT)
@@ -224,10 +234,10 @@ install_lib: all_cmd
224234
cp -fpR $(LIB_FILE) $(DESTDIR)$(libdir_SQ)
225235

226236
INSTALL_HEADERS = bpf.h libbpf.h btf.h libbpf_common.h libbpf_legacy.h xsk.h \
227-
bpf_helpers.h $(BPF_HELPER_DEFS) bpf_tracing.h \
237+
bpf_helpers.h $(BPF_GENERATED) bpf_tracing.h \
228238
bpf_endian.h bpf_core_read.h skel_internal.h
229239

230-
install_headers: $(BPF_HELPER_DEFS)
240+
install_headers: $(BPF_GENERATED)
231241
$(call QUIET_INSTALL, headers) \
232242
$(foreach hdr,$(INSTALL_HEADERS), \
233243
$(call do_install,$(hdr),$(prefix)/include/bpf,644);)
@@ -240,7 +250,7 @@ install: install_lib install_pkgconfig install_headers
240250

241251
clean:
242252
$(call QUIET_CLEAN, libbpf) $(RM) -rf $(CMD_TARGETS) \
243-
*~ .*.d .*.cmd LIBBPF-CFLAGS $(BPF_HELPER_DEFS) \
253+
*~ .*.d .*.cmd LIBBPF-CFLAGS $(BPF_GENERATED) \
244254
$(SHARED_OBJDIR) $(STATIC_OBJDIR) \
245255
$(addprefix $(OUTPUT), \
246256
*.o *.a *.so *.so.$(LIBBPF_MAJOR_VERSION) *.pc)

tools/lib/bpf/btf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ LIBBPF_API struct btf *libbpf_find_kernel_btf(void);
5050

5151
LIBBPF_API struct btf *btf__load_from_kernel_by_id(__u32 id);
5252
LIBBPF_API struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf);
53+
LIBBPF_DEPRECATED_SINCE(0, 6, "use btf__load_from_kernel_by_id instead")
5354
LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf);
5455

5556
LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf);
57+
LIBBPF_DEPRECATED_SINCE(0, 6, "use btf__load_into_kernel instead")
5658
LIBBPF_API int btf__load(struct btf *btf);
5759
LIBBPF_API int btf__load_into_kernel(struct btf *btf);
5860
LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,

tools/lib/bpf/libbpf_common.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,32 @@
1010
#define __LIBBPF_LIBBPF_COMMON_H
1111

1212
#include <string.h>
13+
#include "libbpf_version.h"
1314

1415
#ifndef LIBBPF_API
1516
#define LIBBPF_API __attribute__((visibility("default")))
1617
#endif
1718

1819
#define LIBBPF_DEPRECATED(msg) __attribute__((deprecated(msg)))
1920

21+
/* Mark a symbol as deprecated when libbpf version is >= {major}.{minor} */
22+
#define LIBBPF_DEPRECATED_SINCE(major, minor, msg) \
23+
__LIBBPF_MARK_DEPRECATED_ ## major ## _ ## minor \
24+
(LIBBPF_DEPRECATED("libbpf v" # major "." # minor "+: " msg))
25+
26+
#define __LIBBPF_CURRENT_VERSION_GEQ(major, minor) \
27+
(LIBBPF_MAJOR_VERSION > (major) || \
28+
(LIBBPF_MAJOR_VERSION == (major) && LIBBPF_MINOR_VERSION >= (minor)))
29+
30+
/* Add checks for other versions below when planning deprecation of API symbols
31+
* with the LIBBPF_DEPRECATED_SINCE macro.
32+
*/
33+
#if __LIBBPF_CURRENT_VERSION_GEQ(0, 6)
34+
#define __LIBBPF_MARK_DEPRECATED_0_6(X) X
35+
#else
36+
#define __LIBBPF_MARK_DEPRECATED_0_6(X)
37+
#endif
38+
2039
/* Helper macro to declare and initialize libbpf options struct
2140
*
2241
* This dance with uninitialized declaration, followed by memset to zero,

tools/testing/selftests/bpf/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,14 +512,14 @@ $(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
512512
$(Q)$(CXX) $(CFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@
513513

514514
# Benchmark runner
515-
$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h
515+
$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h $(BPFOBJ)
516516
$(call msg,CC,,$@)
517517
$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
518518
$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
519519
$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
520520
$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
521521
$(OUTPUT)/perfbuf_bench.skel.h
522-
$(OUTPUT)/bench.o: bench.h testing_helpers.h
522+
$(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ)
523523
$(OUTPUT)/bench: LDLIBS += -lm
524524
$(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
525525
$(OUTPUT)/bench_count.o \

0 commit comments

Comments
 (0)