Skip to contentSkip to conversation

bpf: Introduce global functions

PATCH Superseded Alexei Starovoitov 7 participants 31 messages Jan 8, 2020 – Jan 15, 2020

Subsystems (8)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Alexei Starovoitov

author maintainer
#0 lore raw

cover bpf: Introduce global functions

Introduce static vs global functions and function by function verification. This is another step toward dynamic re-linking (or replacement) of global functions. See patch 3 for details. The rest are supporting patches.

Alexei Starovoitov (6):
libbpf: Sanitize BTF_KIND_FUNC linkage
libbpf: Collect static vs global info about functions
bpf: Introduce function-by-function verification selftests/bpf: Add fexit-to-skb test for global funcs selftests/bpf: Add a test for a large global function selftests/bpf: Modify a test to check global functions

 include/linux/bpf.h                           |   7 +-
 include/linux/bpf_verifier.h                  |   7 +-
 include/uapi/linux/btf.h                      |   6 +
 kernel/bpf/btf.c                              | 147 ++++++++---
 kernel/bpf/verifier.c                         | 228 ++++++++++++++----
 tools/include/uapi/linux/btf.h                |   6 +
 tools/lib/bpf/libbpf.c                        | 150 +++++++++++-
 .../bpf/prog_tests/bpf_verif_scale.c          |   2 +
 .../selftests/bpf/prog_tests/fexit_bpf2bpf.c  |   1 +
 .../selftests/bpf/progs/fexit_bpf2bpf.c       |  15 ++
 tools/testing/selftests/bpf/progs/pyperf.h    |   9 +-
 .../selftests/bpf/progs/pyperf_global.c       |   5 +
 .../selftests/bpf/progs/test_pkt_access.c     |  28 +++
 .../selftests/bpf/progs/test_xdp_noinline.c   |   4 +-
 14 files changed, 528 insertions(+), 87 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/pyperf_global.c
Signature · 1 line
2.23.0
author maintainer
#26 lore raw

PATCH 5/6 selftests/bpf: Add a test for a large global function

test results:
pyperf50 with always_inlined the same function five times: processed 46378 insns pyperf50 with global function: processed 6102 insns

---
 tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c | 2 ++
 tools/testing/selftests/bpf/progs/pyperf.h               | 9 +++++++--
 tools/testing/selftests/bpf/progs/pyperf_global.c        | 5 +++++
 3 files changed, 14 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/pyperf_global.c
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +2 -0
index 9486c13af6b2..e9f2f12ba06b 100644
@@ -48,6 +48,8 @@ void test_bpf_verif_scale(void)
 		{ "test_verif_scale2.o", BPF_PROG_TYPE_SCHED_CLS },
 		{ "test_verif_scale3.o", BPF_PROG_TYPE_SCHED_CLS },
 
+		{ "pyperf_global.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
+
 		/* full unroll by llvm */
 		{ "pyperf50.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
 		{ "pyperf100.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
tools/testing/selftests/bpf/progs/pyperf.h +7 -2
index 71d383cc9b85..e186899954e9 100644
@@ -154,7 +154,12 @@ struct {
 	__uint(value_size, sizeof(long long) * 127);
 } stackmap SEC(".maps");
 
-static __always_inline int __on_event(struct pt_regs *ctx)
+#ifdef GLOBAL_FUNC
+__attribute__((noinline))
+#else
+static __always_inline
+#endif
+int __on_event(struct bpf_raw_tracepoint_args *ctx)
 {
 	uint64_t pid_tgid = bpf_get_current_pid_tgid();
 	pid_t pid = (pid_t)(pid_tgid >> 32);
@@ -254,7 +259,7 @@ static __always_inline int __on_event(struct pt_regs *ctx)
 }
 
 SEC("raw_tracepoint/kfree_skb")
-int on_event(struct pt_regs* ctx)
+int on_event(struct bpf_raw_tracepoint_args* ctx)
 {
 	int i, ret = 0;
 	ret |= __on_event(ctx);
tools/testing/selftests/bpf/progs/pyperf_global.c new file +5 -0
new file mode 100644
index 000000000000..079e78a7562b
@@ -0,0 +1,5 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#define STACK_MAX_LEN 50
+#define GLOBAL_FUNC
+#include "pyperf.h"
Signature · 1 line
2.23.0
author maintainer
#5 lore raw

PATCH 2/6 libbpf: Collect static vs global info about functions

In reply to Alexei Starovoitov

Collect static vs global information about BPF functions from ELF file and improve BTF with this additional info if llvm is too old and doesn't emit it on its own.

---
 tools/lib/bpf/libbpf.c | 115 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 111 insertions(+), 4 deletions(-)
tools/lib/bpf/libbpf.c +111 -4
index f72b3ed6c34b..2a4417d03ee2 100644
@@ -294,6 +294,12 @@ struct extern_desc {
 
 static LIST_HEAD(bpf_objects_list);
 
+struct func_desc {
+	int insn_idx;
+	int sec_idx;
+	enum btf_func_linkage linkage;
+};
+
 struct bpf_object {
 	char name[BPF_OBJ_NAME_LEN];
 	char license[64];
@@ -304,6 +310,8 @@ struct bpf_object {
 	struct bpf_map *maps;
 	size_t nr_maps;
 	size_t maps_cap;
+	struct func_desc *funcs;
+	int nr_funcs;
 
 	char *kconfig;
 	struct extern_desc *externs;
@@ -313,6 +321,7 @@ struct bpf_object {
 	bool loaded;
 	bool has_pseudo_calls;
 	bool relaxed_core_relocs;
+	bool llvm_emits_func_linkage;
 
 	/*
 	 * Information when doing elf related work. Only valid if fd
@@ -526,10 +535,10 @@ bpf_object__init_prog_names(struct bpf_object *obj)
 				continue;
 			if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
 				continue;
-
 			name = elf_strptr(obj->efile.elf,
 					  obj->efile.strtabidx,
 					  sym.st_name);
+
 			if (!name) {
 				pr_warn("failed to get sym name string for prog %s\n",
 					prog->section_name);
@@ -2232,6 +2241,34 @@ static int cmp_externs(const void *_a, const void *_b)
 	return strcmp(a->name, b->name);
 }
 
+static int bpf_object__record_func_info(struct bpf_object *obj, GElf_Sym *sym)
+{
+	int bind = GELF_ST_BIND(sym->st_info);
+	bool static_func = bind == STB_LOCAL;
+	struct func_desc *f;
+
+	/*
+	 * Cannot check obj->caps.btf_func_linkage during elf open phase.
+	 * Otherwise could have skipped collection of this info.
+	 */
+
+	f = obj->funcs;
+	f = reallocarray(f, obj->nr_funcs + 1, sizeof(*f));
+	if (!f)
+		return -ENOMEM;
+	obj->funcs = f;
+	f = &f[obj->nr_funcs];
+	memset(f, 0, sizeof(*f));
+	obj->nr_funcs++;
+
+	f->insn_idx = sym->st_value / 8;
+	f->linkage = static_func ? BTF_FUNC_STATIC : BTF_FUNC_GLOBAL;
+	f->sec_idx = sym->st_shndx;
+	pr_debug("Func at insn %d sec %d linkage %d\n",
+		 f->insn_idx, f->sec_idx, f->linkage);
+	return 0;
+}
+
 static int bpf_object__collect_externs(struct bpf_object *obj)
 {
 	const struct btf_type *t;
@@ -2258,6 +2295,12 @@ static int bpf_object__collect_externs(struct bpf_object *obj)
 
 		if (!gelf_getsym(obj->efile.symbols, i, &sym))
 			return -LIBBPF_ERRNO__FORMAT;
+		if (GELF_ST_TYPE(sym.st_info) == STT_FUNC) {
+			int err = bpf_object__record_func_info(obj, &sym);
+
+			if (err)
+				return err;
+		}
 		if (!sym_is_extern(&sym))
 			continue;
 		ext_name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
@@ -3145,9 +3188,62 @@ check_btf_ext_reloc_err(struct bpf_program *prog, int err,
 	return 0;
 }
 
+static int btf_ext_improve_func_info(struct bpf_object *obj,
+				     struct bpf_program *prog,
+				     __u32 sec_idx)
+{
+	struct bpf_func_info_min *info;
+	struct btf_type *t;
+	struct func_desc *f;
+	const char *name;
+	int i, j;
+
+	if (!obj->caps.btf_func_linkage || obj->llvm_emits_func_linkage)
+		/*
+		 * If kernel doesn't understand func linkage or llvm emits
+		 * it already into BTF then don't try to improve BTF based on
+		 * ELF info.
+		 */
+		return 0;
+
+	info = prog->func_info;
+	for (i = 0; i < prog->func_info_cnt; i++) {
+		for (j = 0; j < obj->nr_funcs; j++) {
+			f = &obj->funcs[j];
+			if (f->insn_idx != info->insn_off ||
+			    f->sec_idx != sec_idx)
+				continue;
+			t = (void *)btf__type_by_id(obj->btf, info->type_id);
+			if (!t)
+				return -EINVAL;
+			name = btf__name_by_offset(obj->btf, t->name_off);
+			if (!name)
+				return -EINVAL;
+			pr_debug("Func '%s' at insn %d btf_id %d linkage in BTF %d in ELF %d\n",
+				 name, info->insn_off, info->type_id,
+				 BTF_INFO_VLEN(t->info), f->linkage);
+			if (BTF_INFO_VLEN(t->info) == f->linkage) {
+				if (BTF_INFO_VLEN(t->info)) {
+					/* llvm emits func linkage. Don't touch BTF */
+					obj->llvm_emits_func_linkage = true;
+					return 0;
+				}
+			} else {
+				/* improve BTF with static vs global */
+				t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0,
+						       f->linkage);
+			}
+			break;
+		}
+		info = ((void *)info) + prog->func_info_rec_size;
+	}
+	return 0;
+}
+
 static int
 bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj,
-			  const char *section_name,  __u32 insn_offset)
+			  const char *section_name,  __u32 insn_offset,
+			  __u32 sec_idx)
 {
 	int err;
 
@@ -3168,6 +3264,15 @@ bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj,
 						       "bpf_func_info");
 
 		prog->func_info_rec_size = btf_ext__func_info_rec_size(obj->btf_ext);
+		if (!insn_offset) {
+			/*
+			 * improve BTF_KIND_FUNC when func_info is allocated
+			 * first time. Don't touch it during relocation.
+			 */
+			err = btf_ext_improve_func_info(obj, prog, sec_idx);
+			if (err)
+				return err;
+		}
 	}
 
 	if (!insn_offset || prog->line_info) {
@@ -4308,7 +4413,8 @@ bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
 		if (obj->btf_ext) {
 			err = bpf_program_reloc_btf_ext(prog, obj,
 							text->section_name,
-							prog->insns_cnt);
+							prog->insns_cnt,
+							text->idx);
 			if (err)
 				return err;
 		}
@@ -4336,7 +4442,8 @@ bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
 
 	if (obj->btf_ext) {
 		err = bpf_program_reloc_btf_ext(prog, obj,
-						prog->section_name, 0);
+						prog->section_name, 0,
+						prog->idx);
 		if (err)
 			return err;
 	}
Signature · 1 line
2.23.0
author maintainer
#1 lore raw

PATCH 1/6 libbpf: Sanitize BTF_KIND_FUNC linkage

In reply to Alexei Starovoitov

In case kernel doesn't support static/global/extern liknage of BTF_KIND_FUNC sanitize BTF produced by llvm.

---
 tools/include/uapi/linux/btf.h |  6 ++++++
 tools/lib/bpf/libbpf.c         | 35 +++++++++++++++++++++++++++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)
tools/include/uapi/linux/btf.h +6 -0
index 1a2898c482ee..5a667107ad2c 100644
@@ -146,6 +146,12 @@ enum {
 	BTF_VAR_GLOBAL_EXTERN = 2,
 };
 
+enum btf_func_linkage {
+	BTF_FUNC_STATIC = 0,
+	BTF_FUNC_GLOBAL = 1,
+	BTF_FUNC_EXTERN = 2,
+};
+
 /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe
  * additional information related to the variable such as its linkage.
  */
tools/lib/bpf/libbpf.c +34 -1
index 7513165b104f..f72b3ed6c34b 100644
@@ -166,6 +166,8 @@ struct bpf_capabilities {
 	__u32 btf_datasec:1;
 	/* BPF_F_MMAPABLE is supported for arrays */
 	__u32 array_mmap:1;
+	/* static/global/extern is supported for BTF_KIND_FUNC */
+	__u32 btf_func_linkage:1;
 };
 
 enum reloc_type {
@@ -1817,13 +1819,14 @@ static bool section_have_execinstr(struct bpf_object *obj, int idx)
 
 static void bpf_object__sanitize_btf(struct bpf_object *obj)
 {
+	bool has_func_linkage = obj->caps.btf_func_linkage;
 	bool has_datasec = obj->caps.btf_datasec;
 	bool has_func = obj->caps.btf_func;
 	struct btf *btf = obj->btf;
 	struct btf_type *t;
 	int i, j, vlen;
 
-	if (!obj->btf || (has_func && has_datasec))
+	if (!obj->btf || (has_func && has_datasec && has_func_linkage))
 		return;
 
 	for (i = 1; i <= btf__get_nr_types(btf); i++) {
@@ -1871,6 +1874,9 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj)
 		} else if (!has_func && btf_is_func(t)) {
 			/* replace FUNC with TYPEDEF */
 			t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
+		} else if (!has_func_linkage && btf_is_func(t)) {
+			/* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
+			t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0);
 		}
 	}
 }
@@ -2804,6 +2810,32 @@ static int bpf_object__probe_btf_func(struct bpf_object *obj)
 	return 0;
 }
 
+static int bpf_object__probe_btf_func_linkage(struct bpf_object *obj)
+{
+	static const char strs[] = "\0int\0x\0a";
+	/* static void x(int a) {} */
+	__u32 types[] = {
+		/* int */
+		BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+		/* FUNC_PROTO */                                /* [2] */
+		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
+		BTF_PARAM_ENC(7, 1),
+		/* FUNC x BTF_FUNC_GLOBAL */                    /* [3] */
+		BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 1), 2),
+	};
+	int btf_fd;
+
+	btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types),
+				      strs, sizeof(strs));
+	if (btf_fd >= 0) {
+		obj->caps.btf_func_linkage = 1;
+		close(btf_fd);
+		return 1;
+	}
+
+	return 0;
+}
+
 static int bpf_object__probe_btf_datasec(struct bpf_object *obj)
 {
 	static const char strs[] = "\0x\0.data";
@@ -2859,6 +2891,7 @@ bpf_object__probe_caps(struct bpf_object *obj)
 		bpf_object__probe_name,
 		bpf_object__probe_global_data,
 		bpf_object__probe_btf_func,
+		bpf_object__probe_btf_func_linkage,
 		bpf_object__probe_btf_datasec,
 		bpf_object__probe_array_mmap,
 	};
Signature · 1 line
2.23.0
author maintainer
#11 lore raw

PATCH 3/6 bpf: Introduce function-by-function verification

In reply to Alexei Starovoitov

New llvm and old llvm with libbpf help produce BTF that distinguish global and static functions. Unlike arguments of static function the arguments of global functions cannot be removed or optimized away by llvm. The compiler has to use exactly the arguments specified in a function prototype. The argument type information allows the verifier validate each global function independently. For now only supported argument types are pointer to context and scalars. In the future pointers to structures, sizes, pointer to packet data can be supported as well. Consider the following example:

static int f1(int ...)
{
...
}

int f3(int b);

int f2(int a)
{
f1(a) + f3(a);
}

int f3(int b)
{
...
}

int main(...)
{
f1(...) + f2(...) + f3(...);
}

The verifier will start its safety checks from the first global function f2(). It will recursively descend into f1() because it's static. Then it will check that arguments match for the f3() invocation inside f2(). It will not descend into f3(). It will finish f2() that has to be successfully verified for all possible values of 'a'. Then it will proceed with f3(). That function also has to be safe for all possible values of 'b'. Then it will start subprog 0 (which is main() function). It will recursively descend into f1() and will skip full check of f2() and f3(), since they are global. The order of processing global functions doesn't affect safety, since all global functions must be proven safe based on their arguments only.

Such function by function verification can drastically improve speed of the verification and reduce complexity.

Note that the stack limit of 512 still applies to the call chain regardless whether functions were static or global. The nested level of 8 also still applies. The same recursion prevention checks are in place as well.

The type information and static/global kind is preserved after the verification hence in the above example global function f2() and f3() can be replaced later by equivalent functions with the same types that are loaded and verified later without affecting safety of this main() program. Such replacement (re-linking) of global functions is a subject of future patches.

---
 include/linux/bpf.h          |   7 +-
 include/linux/bpf_verifier.h |   7 +-
 include/uapi/linux/btf.h     |   6 +
 kernel/bpf/btf.c             | 147 +++++++++++++++++-----
 kernel/bpf/verifier.c        | 228 +++++++++++++++++++++++++++--------
 5 files changed, 317 insertions(+), 78 deletions(-)
include/linux/bpf.h +6 -1
index b14e51d56a82..ceb5b6c13abc 100644
@@ -558,6 +558,7 @@ static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
 #endif
 
 struct bpf_func_info_aux {
+	u32 linkage;
 	bool unreliable;
 };
 
@@ -1006,7 +1007,11 @@ int btf_distill_func_proto(struct bpf_verifier_log *log,
 			   const char *func_name,
 			   struct btf_func_model *m);
 
-int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog);
+struct bpf_reg_state;
+int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
+			     struct bpf_reg_state *regs);
+int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
+			  struct bpf_reg_state *reg);
 
 struct bpf_prog *bpf_prog_by_id(u32 id);
 
include/linux/bpf_verifier.h +5 -2
index 26e40de9ef55..b668d3ba2f11 100644
@@ -304,11 +304,13 @@ struct bpf_insn_aux_data {
 	u64 map_key_state; /* constant (32 bit) key tracking for maps */
 	int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
 	int sanitize_stack_off; /* stack slot to be cleared */
-	bool seen; /* this insn was processed by the verifier */
+	u32 seen; /* this insn was processed by the verifier at env->pass_cnt */
 	bool zext_dst; /* this insn zero extends dst reg */
 	u8 alu_state; /* used in combination with alu_limit */
-	bool prune_point;
+
+	/* below fields are initialized once */
 	unsigned int orig_idx; /* original instruction index */
+	bool prune_point;
 };
 
 #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
@@ -379,6 +381,7 @@ struct bpf_verifier_env {
 		int *insn_stack;
 		int cur_stack;
 	} cfg;
+	u32 pass_cnt; /* number of times do_check() was called */
 	u32 subprog_cnt;
 	/* number of instructions analyzed by the verifier */
 	u32 prev_insn_processed, insn_processed;
include/uapi/linux/btf.h +6 -0
index 1a2898c482ee..5a667107ad2c 100644
@@ -146,6 +146,12 @@ enum {
 	BTF_VAR_GLOBAL_EXTERN = 2,
 };
 
+enum btf_func_linkage {
+	BTF_FUNC_STATIC = 0,
+	BTF_FUNC_GLOBAL = 1,
+	BTF_FUNC_EXTERN = 2,
+};
+
 /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe
  * additional information related to the variable such as its linkage.
  */
kernel/bpf/btf.c +120 -27

228 lines not on this page · raw message

kernel/bpf/verifier.c +180 -48

336 lines not on this page · raw message

Signature · 1 line
2.23.0
author maintainer
#29 lore raw

PATCH 6/6 selftests/bpf: Modify a test to check global functions

In reply to Alexei Starovoitov

Make two static functions in test_xdp_noinline.c global:
before: processed 2790 insns
after: processed 2598 insns

---
 tools/testing/selftests/bpf/progs/test_xdp_noinline.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/test_xdp_noinline.c +2 -2
index e88d7b9d65ab..f95bc1a17667 100644
@@ -86,7 +86,7 @@ u32 jhash(const void *key, u32 length, u32 initval)
 	return c;
 }
 
-static __attribute__ ((noinline))
+__attribute__ ((noinline))
 u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval)
 {
 	a += initval;
@@ -96,7 +96,7 @@ u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval)
 	return c;
 }
 
-static __attribute__ ((noinline))
+__attribute__ ((noinline))
 u32 jhash_2words(u32 a, u32 b, u32 initval)
 {
 	return __jhash_nwords(a, b, 0, initval + JHASH_INITVAL + (2 << 2));
Signature · 1 line
2.23.0
author maintainer
#24 lore raw

PATCH 4/6 selftests/bpf: Add fexit-to-skb test for global funcs

In reply to Alexei Starovoitov

Add simple fexit prog type to skb prog type test when subprogram is a global function.

---
 .../selftests/bpf/prog_tests/fexit_bpf2bpf.c  |  1 +
 .../selftests/bpf/progs/fexit_bpf2bpf.c       | 15 ++++++++++
 .../selftests/bpf/progs/test_pkt_access.c     | 28 +++++++++++++++++++
 3 files changed, 44 insertions(+)
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +1 -0
index b426bf2f97e4..7d3740d38965 100644
@@ -98,6 +98,7 @@ static void test_target_yes_callees(void)
 		"fexit/test_pkt_access",
 		"fexit/test_pkt_access_subprog1",
 		"fexit/test_pkt_access_subprog2",
+		"fexit/test_pkt_access_subprog3",
 	};
 	test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o",
 				  "./test_pkt_access.o",
tools/testing/selftests/bpf/progs/fexit_bpf2bpf.c +15 -0
index 2d211ee98a1c..81d7b4aaf79e 100644
@@ -79,4 +79,19 @@ int test_subprog2(struct args_subprog2 *ctx)
 	test_result_subprog2 = 1;
 	return 0;
 }
+
+__u64 test_result_subprog3 = 0;
+BPF_TRACE_3("fexit/test_pkt_access_subprog3", test_subprog3,
+	    int, val, struct sk_buff *, skb, int, ret)
+{
+	int len;
+
+	__builtin_preserve_access_index(({
+		len = skb->len;
+	}));
+	if (len != 74 || ret != 74 * val || val != 3)
+		return 0;
+	test_result_subprog3 = 1;
+	return 0;
+}
 char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/test_pkt_access.c +28 -0
index 3a7b4b607ed3..b77cebf71e66 100644
@@ -47,6 +47,32 @@ int test_pkt_access_subprog2(int val, volatile struct __sk_buff *skb)
 	return skb->len * val;
 }
 
+#define MAX_STACK (512 - 2 * 32)
+
+__attribute__ ((noinline))
+int get_skb_len(struct __sk_buff *skb)
+{
+	volatile char buf[MAX_STACK] = {};
+
+	return skb->len;
+}
+
+int get_skb_ifindex(int, struct __sk_buff *skb, int);
+
+__attribute__ ((noinline))
+int test_pkt_access_subprog3(int val, struct __sk_buff *skb)
+{
+	return get_skb_len(skb) * get_skb_ifindex(val, skb, 1);
+}
+
+__attribute__ ((noinline))
+int get_skb_ifindex(int val, struct __sk_buff *skb, int var)
+{
+	volatile char buf[MAX_STACK] = {};
+
+	return skb->ifindex * val * var;
+}
+
 SEC("classifier/test_pkt_access")
 int test_pkt_access(struct __sk_buff *skb)
 {
@@ -82,6 +108,8 @@ int test_pkt_access(struct __sk_buff *skb)
 		return TC_ACT_SHOT;
 	if (test_pkt_access_subprog2(2, skb) != skb->len * 2)
 		return TC_ACT_SHOT;
+	if (test_pkt_access_subprog3(3, skb) != skb->len * 3 * skb->ifindex)
+		return TC_ACT_SHOT;
 	if (tcp) {
 		if (((void *)(tcp) + 20) > data_end || proto != 6)
 			return TC_ACT_SHOT;
Signature · 1 line
2.23.0
#6 lore raw

Re: [PATCH bpf-next 2/6] libbpf: Collect static vs global info about functions

In reply to Alexei Starovoitov
Quoted 3 lines · Alexei Starovoitov

Collect static vs global information about BPF functions from ELF file and improve BTF with this additional info if llvm is too old and doesn't emit it on its own.

Has the support for this actually landed in LLVM yet? I tried grep'ing in the commit log and couldn't find anything...

[...]

tools/lib/bpf/libbpf.c struct bpf_object { · line 324

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -313,6 +321,7 @@ struct bpf_object {
 	bool loaded;
 	bool has_pseudo_calls;
 	bool relaxed_core_relocs;
+	bool llvm_emits_func_linkage;

Nit: s/llvm/compiler/? Presumably GCC will also support this at some point?

-Toke

#12 lore raw

Re: [PATCH bpf-next 3/6] bpf: Introduce function-by-function verification

In reply to Alexei Starovoitov
Quoted 49 lines · Alexei Starovoitov

New llvm and old llvm with libbpf help produce BTF that distinguish global and static functions. Unlike arguments of static function the arguments of global functions cannot be removed or optimized away by llvm. The compiler has to use exactly the arguments specified in a function prototype. The argument type information allows the verifier validate each global function independently. For now only supported argument types are pointer to context and scalars. In the future pointers to structures, sizes, pointer to packet data can be supported as well. Consider the following example:

static int f1(int ...)
{
...
}

int f3(int b);

int f2(int a)
{
f1(a) + f3(a);
}

int f3(int b)
{
...
}

int main(...)
{
f1(...) + f2(...) + f3(...);
}

The verifier will start its safety checks from the first global function f2(). It will recursively descend into f1() because it's static. Then it will check that arguments match for the f3() invocation inside f2(). It will not descend into f3(). It will finish f2() that has to be successfully verified for all possible values of 'a'. Then it will proceed with f3(). That function also has to be safe for all possible values of 'b'. Then it will start subprog 0 (which is main() function). It will recursively descend into f1() and will skip full check of f2() and f3(), since they are global. The order of processing global functions doesn't affect safety, since all global functions must be proven safe based on their arguments only.

Such function by function verification can drastically improve speed of the verification and reduce complexity.

Note that the stack limit of 512 still applies to the call chain regardless whether functions were static or global. The nested level of 8 also still applies. The same recursion prevention checks are in place as well.

Quoted 7 lines

The type information and static/global kind is preserved after the verification hence in the above example global function f2() and f3() can be replaced later by equivalent functions with the same types that are loaded and verified later without affecting safety of this main() program. Such replacement (re-linking) of global functions is a subject of future patches.

Great to see this progressing; and thanks for breaking things up, makes it much easier to follow along!

One question:

include/uapi/linux/btf.h enum { · line 153

Quoted 5 lines
include/uapi/linux/btf.h +5 -0
+enum btf_func_linkage {
+	BTF_FUNC_STATIC = 0,
+	BTF_FUNC_GLOBAL = 1,
+	BTF_FUNC_EXTERN = 2,
+};

What's supposed to happen with FUNC_EXTERN? That is specifically for the re-linking follow-up?

Quoted 8 lines

/* BTF_KIND_VAR is followed by a single "struct btf_var" to describe
* additional information related to the variable such as its linkage. */

kernel/bpf/btf.c +0 -0
index ed2075884724..e28ec89971ce 100644

kernel/bpf/btf.c static s32 btf_func_check_meta(struct btf_verifier_env *env, · line 2625

Quoted 7 lines
kernel/bpf/btf.c +2 -2
@@ -2621,8 +2621,8 @@ static s32 btf_func_check_meta(struct btf_verifier_env *env,
 		return -EINVAL;
 	}
 
-	if (btf_type_vlen(t)) {
-		btf_verifier_log_type(env, t, "vlen != 0");
+	if (btf_type_vlen(t) > BTF_FUNC_EXTERN) {
+		btf_verifier_log_type(env, t, "invalid func linkage");

This doesn't reject linkage==BTF_FUNC_EXTERN; so for this patch FUNC_EXTERN will be treated the same as FUNC_STATIC (it'll fail the is_global check below)? Or did I miss somewhere else where BTF_FUNC_EXTERN is rejected?

-Toke

#7 lore raw

Re: [PATCH bpf-next 2/6] libbpf: Collect static vs global info about functions

In reply to Toke Høiland-Jørgensen
Quoted 8 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Alexei Starovoitov

Collect static vs global information about BPF functions from ELF file and improve BTF with this additional info if llvm is too old and doesn't emit it on its own.

Has the support for this actually landed in LLVM yet? I tried grep'ing in the commit log and couldn't find anything...

It has not landed yet. The commit link is:

    https://reviews.llvm.org/D71638

I will try to land the patch in the next couple of days once this series of patch is merged or the principle of the patch is accepted.

Quoted 13 lines

[...]

tools/lib/bpf/libbpf.c struct bpf_object { · line 324

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -313,6 +321,7 @@ struct bpf_object {
  	bool loaded;
  	bool has_pseudo_calls;
  	bool relaxed_core_relocs;
+	bool llvm_emits_func_linkage;

Nit: s/llvm/compiler/? Presumably GCC will also support this at some point?

-Toke

Acked-by
#9 lore raw

Re: [PATCH bpf-next 2/6] libbpf: Collect static vs global info about functions

In reply to Toke Høiland-Jørgensen
Quoted 14 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Alexei Starovoitov

Collect static vs global information about BPF functions from ELF file and improve BTF with this additional info if llvm is too old and doesn't emit it on its own.

Has the support for this actually landed in LLVM yet? I tried grep'ing in the commit log and couldn't find anything...

[...]

Quoted 8 lines

tools/lib/bpf/libbpf.c struct bpf_object { · line 324

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -313,6 +321,7 @@ struct bpf_object {
 	bool loaded;
 	bool has_pseudo_calls;
 	bool relaxed_core_relocs;
+	bool llvm_emits_func_linkage;

Nit: s/llvm/compiler/? Presumably GCC will also support this at some point?

Echoing this nit (and other references to llvm). Otherwise,

#3 lore raw

Re: [PATCH bpf-next 1/6] libbpf: Sanitize BTF_KIND_FUNC linkage

In reply to Alexei Starovoitov
Quoted 56 lines · Alexei Starovoitov

In case kernel doesn't support static/global/extern liknage of BTF_KIND_FUNC sanitize BTF produced by llvm.

---
  tools/include/uapi/linux/btf.h |  6 ++++++
  tools/lib/bpf/libbpf.c         | 35 +++++++++++++++++++++++++++++++++-
  2 files changed, 40 insertions(+), 1 deletion(-)
tools/include/uapi/linux/btf.h +6 -0
index 1a2898c482ee..5a667107ad2c 100644
@@ -146,6 +146,12 @@ enum {
  	BTF_VAR_GLOBAL_EXTERN = 2,
  };
 
+enum btf_func_linkage {
+	BTF_FUNC_STATIC = 0,
+	BTF_FUNC_GLOBAL = 1,
+	BTF_FUNC_EXTERN = 2,
+};
+
  /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe
   * additional information related to the variable such as its linkage.
   */
tools/lib/bpf/libbpf.c +4 -1
index 7513165b104f..f72b3ed6c34b 100644
@@ -166,6 +166,8 @@ struct bpf_capabilities {
  	__u32 btf_datasec:1;
  	/* BPF_F_MMAPABLE is supported for arrays */
  	__u32 array_mmap:1;
+	/* static/global/extern is supported for BTF_KIND_FUNC */
+	__u32 btf_func_linkage:1;
  };
 
  enum reloc_type {
@@ -1817,13 +1819,14 @@ static bool section_have_execinstr(struct bpf_object *obj, int idx)
 
  static void bpf_object__sanitize_btf(struct bpf_object *obj)
  {
+	bool has_func_linkage = obj->caps.btf_func_linkage;
  	bool has_datasec = obj->caps.btf_datasec;
  	bool has_func = obj->caps.btf_func;
  	struct btf *btf = obj->btf;
  	struct btf_type *t;
  	int i, j, vlen;
 
-	if (!obj->btf || (has_func && has_datasec))
+	if (!obj->btf || (has_func && has_datasec && has_func_linkage))
  		return;
 
  	for (i = 1; i <= btf__get_nr_types(btf); i++) {

tools/lib/bpf/libbpf.c static void bpf_object__sanitize_btf(struct bpf_object *obj) · line 1879

Quoted 6 lines
tools/lib/bpf/libbpf.c +3 -0
@@ -1871,6 +1874,9 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj)
  		} else if (!has_func && btf_is_func(t)) {
  			/* replace FUNC with TYPEDEF */
  			t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
+		} else if (!has_func_linkage && btf_is_func(t)) {
+			/* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
+			t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0);

The comment says we only sanitize BTF_FUNC_GLOBAL here. Actually, it also sanitize BTF_FUNC_EXTERN.

Currently, in kernel/bpf/btf.c, we have
static int btf_check_all_types(struct btf_verifier_env *env)

{
		...
                 if (btf_type_is_func(t)) {
                         err = btf_func_check(env, t);
                         if (err)
                                 return err;
                 }
		...
}

btf_func_check() will ensure func btf_type->type is a func_proto and all arguments of func_proto has a name except void which is considered as varg.

For extern function, the argument name is lost in llvm/clang.

-bash-4.4$ cat test.c

extern int foo(int a);
int test() { return foo(5); }
-bash-4.4$
-bash-4.4$ clang -target bpf -O2 -g -S -emit-llvm test.c

!2 = !{}
!4 = !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !5,
flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2) !5 = !DISubroutineType(types: !6)
!6 = !{!7, !7}
!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)

To avoid kernel complaints, we need to sanitize in a different way. For example extern BTF_KIND_FUNC could be rewritten to a BTF_KIND_PTR to void.

Quoted 45 lines
tools/lib/bpf/libbpf.c +27 -0
  		}
  	}
  }
@@ -2804,6 +2810,32 @@ static int bpf_object__probe_btf_func(struct bpf_object *obj)
  	return 0;
  }
 
+static int bpf_object__probe_btf_func_linkage(struct bpf_object *obj)
+{
+	static const char strs[] = "\0int\0x\0a";
+	/* static void x(int a) {} */
+	__u32 types[] = {
+		/* int */
+		BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+		/* FUNC_PROTO */                                /* [2] */
+		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
+		BTF_PARAM_ENC(7, 1),
+		/* FUNC x BTF_FUNC_GLOBAL */                    /* [3] */
+		BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 1), 2),
+	};
+	int btf_fd;
+
+	btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types),
+				      strs, sizeof(strs));
+	if (btf_fd >= 0) {
+		obj->caps.btf_func_linkage = 1;
+		close(btf_fd);
+		return 1;
+	}
+
+	return 0;
+}
+
  static int bpf_object__probe_btf_datasec(struct bpf_object *obj)
  {
  	static const char strs[] = "\0x\0.data";
@@ -2859,6 +2891,7 @@ bpf_object__probe_caps(struct bpf_object *obj)
  		bpf_object__probe_name,
  		bpf_object__probe_global_data,
  		bpf_object__probe_btf_func,
+		bpf_object__probe_btf_func_linkage,
  		bpf_object__probe_btf_datasec,
  		bpf_object__probe_array_mmap,
  	};
#17 lore raw

Re: [PATCH bpf-next 3/6] bpf: Introduce function-by-function verification

In reply to Alexei Starovoitov
Quoted 12 lines · Alexei Starovoitov

New llvm and old llvm with libbpf help produce BTF that distinguish global and static functions. Unlike arguments of static function the arguments of global functions cannot be removed or optimized away by llvm. The compiler has to use exactly the arguments specified in a function prototype. The argument type information allows the verifier validate each global function independently. For now only supported argument types are pointer to context and scalars. In the future pointers to structures, sizes, pointer to packet data can be supported as well. Consider the following example:

[...]

Quoted 20 lines

The type information and static/global kind is preserved after the verification hence in the above example global function f2() and f3() can be replaced later by equivalent functions with the same types that are loaded and verified later without affecting safety of this main() program. Such replacement (re-linking) of global functions is a subject of future patches.

---

include/linux/bpf.h          |   7 +-
include/linux/bpf_verifier.h |   7 +-
include/uapi/linux/btf.h     |   6 +
kernel/bpf/btf.c             | 147 +++++++++++++++++-----
kernel/bpf/verifier.c        | 228 +++++++++++++++++++++++++++--------
5 files changed, 317 insertions(+), 78 deletions(-)
include/linux/bpf.h +0 -0
index b14e51d56a82..ceb5b6c13abc 100644

include/linux/bpf.h static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, · line 561

Quoted 4 lines
include/linux/bpf.h +1 -0
@@ -558,6 +558,7 @@ static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
 #endif
 
 struct bpf_func_info_aux {
+	u32 linkage;

How about we use u16 or even u8 for linkage? We are using BTF_INFO_VLEN() which is 16-bit long. Maybe we should save some bits for future extensions?

Quoted 16 lines
include/linux/bpf.h +5 -1
 	bool unreliable;
 };
 
@@ -1006,7 +1007,11 @@ int btf_distill_func_proto(struct bpf_verifier_log *log,
 			   const char *func_name,
 			   struct btf_func_model *m);
 
-int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog);
+struct bpf_reg_state;
+int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
+			     struct bpf_reg_state *regs);
+int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
+			  struct bpf_reg_state *reg);
 
 struct bpf_prog *bpf_prog_by_id(u32 id);
 

[...]

Quoted 25 lines
include/linux/bpf.h +4 -3
@@ -3535,11 +3537,12 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,
 static int btf_translate_to_vmlinux(struct bpf_verifier_log *log,
 				     struct btf *btf,
 				     const struct btf_type *t,
-				     enum bpf_prog_type prog_type)
+				     enum bpf_prog_type prog_type,
+				     int arg)
 {
 	const struct btf_member *prog_ctx_type, *kern_ctx_type;
 
-	prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type);
+	prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type, arg);
 	if (!prog_ctx_type)
 		return -ENOENT;
 	kern_ctx_type = prog_ctx_type + 1;
@@ -3700,7 +3703,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
 	info->btf_id = t->type;
 
 	if (tgt_prog) {
-		ret = btf_translate_to_vmlinux(log, btf, t, tgt_prog->type);
+		ret = btf_translate_to_vmlinux(log, btf, t, tgt_prog->type, arg);
 		if (ret > 0) {
 			info->btf_id = ret;
 			return true;

include/linux/bpf.h int btf_distill_func_proto(struct bpf_verifier_log *log, · line 4051

Quoted 7 lines
include/linux/bpf.h +3 -1
@@ -4043,11 +4046,10 @@ int btf_distill_func_proto(struct bpf_verifier_log *log,
 	return 0;
 }
 
-int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog)
+/* Compare BTF of a function with given bpf_reg_state */
+int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
+			     struct bpf_reg_state *reg)

I think we need more comments for the retval of btf_check_func_arg_match().

Quoted 7 lines
include/linux/bpf.h +0 -3
 {
-	struct bpf_verifier_state *st = env->cur_state;
-	struct bpf_func_state *func = st->frame[st->curframe];
-	struct bpf_reg_state *reg = func->regs;
 	struct bpf_verifier_log *log = &env->log;
 	struct bpf_prog *prog = env->prog;
 	struct btf *btf = prog->aux->btf;

[...]

Quoted 6 lines
include/linux/bpf.h +6 -0
+
+/* Convert BTF of a function into bpf_reg_state if possible */
+int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
+			  struct bpf_reg_state *reg)
+{
+	struct bpf_verifier_log *log = &env->log;

include/linux/bpf.h

Quoted 10 lines
include/linux/bpf.h +10 -0
+	struct bpf_prog *prog = env->prog;
+	struct btf *btf = prog->aux->btf;
+	const struct btf_param *args;
+	const struct btf_type *t;
+	u32 i, nargs, btf_id;
+	const char *tname;
+
+	if (!prog->aux->func_info ||
+	    prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) {
+		bpf_log(log, "Verifier bug\n");

IIUC, this should never happen? Maybe we need more details in the log, and maybe also WARN_ONCE()?

Quoted 14 lines
include/linux/bpf.h +14 -0
+		return -EFAULT;
+	}
+
+	btf_id = prog->aux->func_info[subprog].type_id;
+	if (!btf_id) {
+		bpf_log(log, "Global functions need valid BTF\n");
+		return -EINVAL;
+	}
+
+	t = btf_type_by_id(btf, btf_id);
+	if (!t || !btf_type_is_func(t)) {
+		bpf_log(log, "BTF of func#%d doesn't point to KIND_FUNC\n",
+			subprog);
+		return -EINVAL;

include/linux/bpf.h

Quoted 10 lines
include/linux/bpf.h +10 -0
+	}
+	tname = btf_name_by_offset(btf, t->name_off);
+
+	if (log->level & BPF_LOG_LEVEL)
+		bpf_log(log, "Validating %s() func#%d...\n",
+			tname, subprog);
+
+	if (prog->aux->func_info_aux[subprog].unreliable) {
+		bpf_log(log, "Verifier bug in function %s()\n", tname);
+		return -EFAULT;

Why -EFAULT instead of -EINVAL? I think we treat them the same? I guess this to highlight verifier bug vs. libbpf/llvm bug? How about we use WARN_ONCE() for all verifier bug?

Quoted 46 lines
include/linux/bpf.h +44 -0
+	}
+
+	t = btf_type_by_id(btf, t->type);
+	if (!t || !btf_type_is_func_proto(t)) {
+		bpf_log(log, "Invalid type of function %s()\n", tname);
+		return -EINVAL;
+	}
+	args = (const struct btf_param *)(t + 1);
+	nargs = btf_type_vlen(t);
+	if (nargs > 5) {
+		bpf_log(log, "Global function %s() with %d > 5 args. Buggy llvm.\n",
+			tname, nargs);
+		return -EINVAL;
+	}
+	/* check that function returns int */
+	t = btf_type_by_id(btf, t->type);
+	while (btf_type_is_modifier(t))
+		t = btf_type_by_id(btf, t->type);
+	if (!btf_type_is_int(t) && !btf_type_is_enum(t)) {
+		bpf_log(log,
+			"Global function %s() doesn't return scalar. Only those are supported.\n",
+			tname);
+		return -EINVAL;
+	}
+	/* Convert BTF function arguments into verifier types.
+	 * Only PTR_TO_CTX and SCALAR are supported atm.
+	 */
+	for (i = 0; i < nargs; i++) {
+		t = btf_type_by_id(btf, args[i].type);
+		while (btf_type_is_modifier(t))
+			t = btf_type_by_id(btf, t->type);
+		if (btf_type_is_int(t) || btf_type_is_enum(t)) {
+			reg[i + 1].type = SCALAR_VALUE;
+			continue;
+		}
+		if (btf_type_is_ptr(t) &&
+		    btf_get_prog_ctx_type(log, btf, t, prog->type, i)) {
+			reg[i + 1].type = PTR_TO_CTX;
+			continue;
+		}
+		bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n",
+			i, btf_kind_str[BTF_INFO_KIND(t->info)], tname);
+		return -EINVAL;
+	}
	return 0;
}

[...]

#28 lore raw

Re: [PATCH bpf-next 5/6] selftests/bpf: Add a test for a large global function

Quoted 11 lines · Song Liu
Quoted 8 lines · Alexei Starovoitov

test results:
pyperf50 with always_inlined the same function five times: processed 46378 insns pyperf50 with global function: processed 6102 insns

This is awesome!

Quoted 4 lines
Quoted 2 lines
#13 lore raw

Re: [PATCH bpf-next 3/6] bpf: Introduce function-by-function verification

In reply to Toke Høiland-Jørgensen
Quoted 64 lines · Toke Høiland-Jørgensen
Quoted 49 lines · Alexei Starovoitov

New llvm and old llvm with libbpf help produce BTF that distinguish global and static functions. Unlike arguments of static function the arguments of global functions cannot be removed or optimized away by llvm. The compiler has to use exactly the arguments specified in a function prototype. The argument type information allows the verifier validate each global function independently. For now only supported argument types are pointer to context and scalars. In the future pointers to structures, sizes, pointer to packet data can be supported as well. Consider the following example:

static int f1(int ...)
{
...
}

int f3(int b);

int f2(int a)
{
f1(a) + f3(a);
}

int f3(int b)
{
...
}

int main(...)
{
f1(...) + f2(...) + f3(...);
}

The verifier will start its safety checks from the first global function f2(). It will recursively descend into f1() because it's static. Then it will check that arguments match for the f3() invocation inside f2(). It will not descend into f3(). It will finish f2() that has to be successfully verified for all possible values of 'a'. Then it will proceed with f3(). That function also has to be safe for all possible values of 'b'. Then it will start subprog 0 (which is main() function). It will recursively descend into f1() and will skip full check of f2() and f3(), since they are global. The order of processing global functions doesn't affect safety, since all global functions must be proven safe based on their arguments only.

Such function by function verification can drastically improve speed of the verification and reduce complexity.

Note that the stack limit of 512 still applies to the call chain regardless whether functions were static or global. The nested level of 8 also still applies. The same recursion prevention checks are in place as well.

Quoted 7 lines

The type information and static/global kind is preserved after the verification hence in the above example global function f2() and f3() can be replaced later by equivalent functions with the same types that are loaded and verified later without affecting safety of this main() program. Such replacement (re-linking) of global functions is a subject of future patches.

Great to see this progressing; and thanks for breaking things up, makes it much easier to follow along!

One question:

Quoted 8 lines

include/uapi/linux/btf.h enum { · line 153

Quoted 5 lines
include/uapi/linux/btf.h +5 -0
+enum btf_func_linkage {
+	BTF_FUNC_STATIC = 0,
+	BTF_FUNC_GLOBAL = 1,
+	BTF_FUNC_EXTERN = 2,
+};

What's supposed to happen with FUNC_EXTERN? That is specifically for the re-linking follow-up?

I was thinking to complete the whole thing with re-linking and then send it, but llvm 10 feature cut off date is end of this week, so we have to land llvm bits asap. I'd like to land patch 1 with libbpf sanitization first before landing llvm. llvm release cadence is ~4 month and it would be sad to miss it. Note we will be able to tweak encoding if really necessary after next week. (BTF encoding gets fixed in ABI only after full kernel release). It's unlikely though. I think the encoding is good. I've played with few different variants and this one fits the best. FUNC_EXTERN encoding as 2 is kinda obvious when encoding for global vs static is selected. The kernel and libbpf will not be using FUNC_EXTERN yet, but llvm is tested to do the right thing already, so I think it's fine to add it to btf.h now.

As far as future plans when libbpf sees FUNC_EXTERN it will do the linking the way we discussed in the other thread. The kernel will support FUNC_EXTERN when we introduce dynamic libraries. A collection of bpf functions will be loaded into the kernel first (like libc.so) and later programs will have FUNC_EXTERN as part of their BTF to be resolved while loading. The func name to btf_id resolution will be done by libbpf. The kernel verifier will do the type checking on BTFs. So the kernel side of FUNC_EXTERN support will be minimal, but to your point below...

Quoted 4 lines

This doesn't reject linkage==BTF_FUNC_EXTERN; so for this patch FUNC_EXTERN will be treated the same as FUNC_STATIC (it'll fail the is_global check below)? Or did I miss somewhere else where BTF_FUNC_EXTERN is rejected?

... is absolutely correct. My bad. Added this bit too soon. Will remove. The kernel should accept FUNC_GLOBAL only in this patch set.

#10 lore raw

Re: [PATCH bpf-next 2/6] libbpf: Collect static vs global info about functions

In reply to Song Liu
Quoted 23 lines · Song Liu
Quoted 14 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Alexei Starovoitov

Collect static vs global information about BPF functions from ELF file and improve BTF with this additional info if llvm is too old and doesn't emit it on its own.

Has the support for this actually landed in LLVM yet? I tried grep'ing in the commit log and couldn't find anything...

[...]

Quoted 8 lines

tools/lib/bpf/libbpf.c struct bpf_object { · line 324

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -313,6 +321,7 @@ struct bpf_object {
 	bool loaded;
 	bool has_pseudo_calls;
 	bool relaxed_core_relocs;
+	bool llvm_emits_func_linkage;

Nit: s/llvm/compiler/? Presumably GCC will also support this at some point?

Quoted 1 line

Echoing this nit (and other references to llvm). Otherwise,

sure. will rename to compiler, but I think you folks are overly optimistic about gcc. Even basic stuff doesn't work yet. I doubt we will see BTF emitted by gcc this year.

#4 lore raw

Re: [PATCH bpf-next 1/6] libbpf: Sanitize BTF_KIND_FUNC linkage

In reply to Yonghong Song
Quoted 101 lines · Yonghong Song
Quoted 56 lines · Alexei Starovoitov

In case kernel doesn't support static/global/extern liknage of BTF_KIND_FUNC sanitize BTF produced by llvm.

---
  tools/include/uapi/linux/btf.h |  6 ++++++
  tools/lib/bpf/libbpf.c         | 35 +++++++++++++++++++++++++++++++++-
  2 files changed, 40 insertions(+), 1 deletion(-)
tools/include/uapi/linux/btf.h +6 -0
index 1a2898c482ee..5a667107ad2c 100644
@@ -146,6 +146,12 @@ enum {
  	BTF_VAR_GLOBAL_EXTERN = 2,
  };
 
+enum btf_func_linkage {
+	BTF_FUNC_STATIC = 0,
+	BTF_FUNC_GLOBAL = 1,
+	BTF_FUNC_EXTERN = 2,
+};
+
  /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe
   * additional information related to the variable such as its linkage.
   */
tools/lib/bpf/libbpf.c +4 -1
index 7513165b104f..f72b3ed6c34b 100644
@@ -166,6 +166,8 @@ struct bpf_capabilities {
  	__u32 btf_datasec:1;
  	/* BPF_F_MMAPABLE is supported for arrays */
  	__u32 array_mmap:1;
+	/* static/global/extern is supported for BTF_KIND_FUNC */
+	__u32 btf_func_linkage:1;
  };
 
  enum reloc_type {
@@ -1817,13 +1819,14 @@ static bool section_have_execinstr(struct bpf_object *obj, int idx)
 
  static void bpf_object__sanitize_btf(struct bpf_object *obj)
  {
+	bool has_func_linkage = obj->caps.btf_func_linkage;
  	bool has_datasec = obj->caps.btf_datasec;
  	bool has_func = obj->caps.btf_func;
  	struct btf *btf = obj->btf;
  	struct btf_type *t;
  	int i, j, vlen;
 
-	if (!obj->btf || (has_func && has_datasec))
+	if (!obj->btf || (has_func && has_datasec && has_func_linkage))
  		return;
 
  	for (i = 1; i <= btf__get_nr_types(btf); i++) {

tools/lib/bpf/libbpf.c static void bpf_object__sanitize_btf(struct bpf_object *obj) · line 1879

Quoted 6 lines
tools/lib/bpf/libbpf.c +3 -0
@@ -1871,6 +1874,9 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj)
  		} else if (!has_func && btf_is_func(t)) {
  			/* replace FUNC with TYPEDEF */
  			t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
+		} else if (!has_func_linkage && btf_is_func(t)) {
+			/* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
+			t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0);

The comment says we only sanitize BTF_FUNC_GLOBAL here. Actually, it also sanitize BTF_FUNC_EXTERN.

Currently, in kernel/bpf/btf.c, we have
static int btf_check_all_types(struct btf_verifier_env *env)

{
		...
                 if (btf_type_is_func(t)) {
                         err = btf_func_check(env, t);
                         if (err)
                                 return err;
                 }
		...
}

btf_func_check() will ensure func btf_type->type is a func_proto and all arguments of func_proto has a name except void which is considered as varg.

For extern function, the argument name is lost in llvm/clang.

-bash-4.4$ cat test.c

extern int foo(int a);
int test() { return foo(5); }

(quoted diff) +0 -2
-bash-4.4$
-bash-4.4$ clang -target bpf -O2 -g -S -emit-llvm test.c

!2 = !{}
!4 = !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !5,
flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2) !5 = !DISubroutineType(types: !6)
!6 = !{!7, !7}
!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)

Quoted 3 lines

To avoid kernel complaints, we need to sanitize in a different way. For example extern BTF_KIND_FUNC could be rewritten to a BTF_KIND_PTR to void.

Good point. I'll reword the comment and rename the test to btf_func_global, so it probes kernel for KIND_GLOBAL only and santizes only that bit. KIND_EXTERN sanitization is to be done later. Separate libbpf and kernel patches.

#18 lore raw

Re: [PATCH bpf-next 3/6] bpf: Introduce function-by-function verification

In reply to Song Liu
Quoted 37 lines · Song Liu
Quoted 12 lines · Alexei Starovoitov

New llvm and old llvm with libbpf help produce BTF that distinguish global and static functions. Unlike arguments of static function the arguments of global functions cannot be removed or optimized away by llvm. The compiler has to use exactly the arguments specified in a function prototype. The argument type information allows the verifier validate each global function independently. For now only supported argument types are pointer to context and scalars. In the future pointers to structures, sizes, pointer to packet data can be supported as well. Consider the following example:

[...]

Quoted 22 lines

The type information and static/global kind is preserved after the verification hence in the above example global function f2() and f3() can be replaced later by equivalent functions with the same types that are loaded and verified later without affecting safety of this main() program. Such replacement (re-linking) of global functions is a subject of future patches.

---

include/linux/bpf.h          |   7 +-
include/linux/bpf_verifier.h |   7 +-
include/uapi/linux/btf.h     |   6 +
kernel/bpf/btf.c             | 147 +++++++++++++++++-----
kernel/bpf/verifier.c        | 228 +++++++++++++++++++++++++++--------
5 files changed, 317 insertions(+), 78 deletions(-)
include/linux/bpf.h +0 -0
index b14e51d56a82..ceb5b6c13abc 100644
@@ -558,6 +558,7 @@ static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
 #endif
 
Quoted 5 lines

include/linux/bpf.h static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, · line 561

Quoted 2 lines
include/linux/bpf.h +1 -0
 struct bpf_func_info_aux {
+	u32 linkage;

How about we use u16 or even u8 for linkage? We are using BTF_INFO_VLEN() which is 16-bit long. Maybe we should save some bits for future extensions?

sure. u16 is fine.
Will also introduce btf_func_kind() helper to avoid misleading BTF_INFO_VLEN macro.

Quoted 6 lines

include/linux/bpf.h static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, · line 564

Quoted 4 lines
include/linux/bpf.h +3 -1
-int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog)
+/* Compare BTF of a function with given bpf_reg_state */
+int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
+			     struct bpf_reg_state *reg)

I think we need more comments for the retval of btf_check_func_arg_match().

sure.

Quoted 19 lines
Quoted 7 lines

{

include/linux/bpf.h +0 -3
-	struct bpf_verifier_state *st = env->cur_state;
-	struct bpf_func_state *func = st->frame[st->curframe];
-	struct bpf_reg_state *reg = func->regs;
	struct bpf_verifier_log *log = &env->log;
	struct bpf_prog *prog = env->prog;
	struct btf *btf = prog->aux->btf;

[...]

Quoted 11 lines
include/linux/bpf.h +11 -0
+
+/* Convert BTF of a function into bpf_reg_state if possible */
+int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
+			  struct bpf_reg_state *reg)
+{
+	struct bpf_verifier_log *log = &env->log;
+	struct bpf_prog *prog = env->prog;
+	struct btf *btf = prog->aux->btf;
+	const struct btf_param *args;
+	const struct btf_type *t;
+	u32 i, nargs, btf_id;
Quoted 8 lines

include/linux/bpf.h

Quoted 5 lines
include/linux/bpf.h +5 -0
+	const char *tname;
+
+	if (!prog->aux->func_info ||
+	    prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) {
+		bpf_log(log, "Verifier bug\n");

IIUC, this should never happen? Maybe we need more details in the log, and maybe also WARN_ONCE()?

Should never happen and I think it's pretty clear from the diff, since this function is called after == FUNC_GLOBAL check in the caller. I didn't add WARN to avoid wasting .text even more here.
Single 'if' above already feels a bit overly defensive.
It's not like other cases in the verifier where we have WARN_ONCE. Those are for complex things. Here it's one callsite and trivial control flow.

Quoted 5 lines

include/linux/bpf.h

Quoted 3 lines
include/linux/bpf.h +3 -0
+	if (prog->aux->func_info_aux[subprog].unreliable) {
+		bpf_log(log, "Verifier bug in function %s()\n", tname);
+		return -EFAULT;

Why -EFAULT instead of -EINVAL? I think we treat them the same?

EFAULT is a verifier bug like in all other places in the verifier where EFAULT is returned. EINVAL is normal error.

#19 lore raw

Re: [PATCH bpf-next 3/6] bpf: Introduce function-by-function verification

Quoted 88 lines · Alexei Starovoitov
Quoted 37 lines · Song Liu
Quoted 12 lines · Alexei Starovoitov

New llvm and old llvm with libbpf help produce BTF that distinguish global and static functions. Unlike arguments of static function the arguments of global functions cannot be removed or optimized away by llvm. The compiler has to use exactly the arguments specified in a function prototype. The argument type information allows the verifier validate each global function independently. For now only supported argument types are pointer to context and scalars. In the future pointers to structures, sizes, pointer to packet data can be supported as well. Consider the following example:

[...]

Quoted 22 lines

The type information and static/global kind is preserved after the verification hence in the above example global function f2() and f3() can be replaced later by equivalent functions with the same types that are loaded and verified later without affecting safety of this main() program. Such replacement (re-linking) of global functions is a subject of future patches.

---

include/linux/bpf.h          |   7 +-
include/linux/bpf_verifier.h |   7 +-
include/uapi/linux/btf.h     |   6 +
kernel/bpf/btf.c             | 147 +++++++++++++++++-----
kernel/bpf/verifier.c        | 228 +++++++++++++++++++++++++++--------
5 files changed, 317 insertions(+), 78 deletions(-)
include/linux/bpf.h +0 -0
index b14e51d56a82..ceb5b6c13abc 100644
@@ -558,6 +558,7 @@ static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
 #endif
 
Quoted 5 lines

include/linux/bpf.h static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, · line 561

Quoted 2 lines
include/linux/bpf.h +1 -0
 struct bpf_func_info_aux {
+	u32 linkage;

How about we use u16 or even u8 for linkage? We are using BTF_INFO_VLEN() which is 16-bit long. Maybe we should save some bits for future extensions?

sure. u16 is fine.
Will also introduce btf_func_kind() helper to avoid misleading BTF_INFO_VLEN macro.

Quoted 6 lines

include/linux/bpf.h static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, · line 564

Quoted 4 lines
include/linux/bpf.h +3 -1
-int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog)
+/* Compare BTF of a function with given bpf_reg_state */
+int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
+			     struct bpf_reg_state *reg)

I think we need more comments for the retval of btf_check_func_arg_match().

sure.

Quoted 19 lines
Quoted 7 lines

{

include/linux/bpf.h +0 -3
-	struct bpf_verifier_state *st = env->cur_state;
-	struct bpf_func_state *func = st->frame[st->curframe];
-	struct bpf_reg_state *reg = func->regs;
	struct bpf_verifier_log *log = &env->log;
	struct bpf_prog *prog = env->prog;
	struct btf *btf = prog->aux->btf;

[...]

Quoted 11 lines
include/linux/bpf.h +11 -0
+
+/* Convert BTF of a function into bpf_reg_state if possible */
+int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
+			  struct bpf_reg_state *reg)
+{
+	struct bpf_verifier_log *log = &env->log;
+	struct bpf_prog *prog = env->prog;
+	struct btf *btf = prog->aux->btf;
+	const struct btf_param *args;
+	const struct btf_type *t;
+	u32 i, nargs, btf_id;
Quoted 8 lines

include/linux/bpf.h

Quoted 5 lines
include/linux/bpf.h +5 -0
+	const char *tname;
+
+	if (!prog->aux->func_info ||
+	    prog->aux->func_info_aux[subprog].linkage != BTF_FUNC_GLOBAL) {
+		bpf_log(log, "Verifier bug\n");

IIUC, this should never happen? Maybe we need more details in the log, and maybe also WARN_ONCE()?

Quoted 6 lines

Should never happen and I think it's pretty clear from the diff, since this function is called after == FUNC_GLOBAL check in the caller. I didn't add WARN to avoid wasting .text even more here.
Single 'if' above already feels a bit overly defensive.
It's not like other cases in the verifier where we have WARN_ONCE. Those are for complex things. Here it's one callsite and trivial control flow.

Agreed. Current check is good enough.

Quoted 9 lines
Quoted 6 lines

include/linux/bpf.h

Quoted 4 lines
include/linux/bpf.h +3 -0
+	if (prog->aux->func_info_aux[subprog].unreliable) {
+		bpf_log(log, "Verifier bug in function %s()\n", tname);
+		return -EFAULT;

Why -EFAULT instead of -EINVAL? I think we treat them the same?

EFAULT is a verifier bug like in all other places in the verifier where EFAULT is returned. EINVAL is normal error.

Thanks for the explanation.

Song

#20 lore raw

Re: [PATCH bpf-next 3/6] bpf: Introduce function-by-function verification

In reply to Alexei Starovoitov

kernel/bpf/verifier.c static void free_states(struct bpf_verifier_env *env) · line 9534

Quoted 4 lines · Alexei Starovoitov
kernel/bpf/verifier.c +4 -0
+       if (subprog) {
+               ret = btf_prepare_func_args(env, subprog, regs);
+               if (ret)
+                       return ret;

noticed a memory leak here.
Will respin with all comments addressed soon.

#8 lore raw

Re: [PATCH bpf-next 2/6] libbpf: Collect static vs global info about functions

In reply to Yonghong Song
Quoted 7 lines · Yonghong Song
Quoted 6 lines · Toke Høiland-Jørgensen
Quoted 3 lines · Alexei Starovoitov

Collect static vs global information about BPF functions from ELF file and improve BTF with this additional info if llvm is too old and doesn't emit it on its own.

Quoted 8 lines
Quoted 2 lines

Has the support for this actually landed in LLVM yet? I tried grep'ing in the commit log and couldn't find anything...

It has not landed yet. The commit link is:

    https://reviews.llvm.org/D71638

I will try to land the patch in the next couple of days once this series of patch is merged or the principle of the patch is accepted.

OK. My next question was going to be whether you're planning to get this into LLVM master before the cutoff date for LLVM10, but I see Alexei already answered that in the other reply, so great!

-Toke

#14 lore raw

Re: [PATCH bpf-next 3/6] bpf: Introduce function-by-function verification

In reply to Alexei Starovoitov
Quoted 74 lines · Alexei Starovoitov
Quoted 64 lines · Toke Høiland-Jørgensen
Quoted 49 lines · Alexei Starovoitov

New llvm and old llvm with libbpf help produce BTF that distinguish global and static functions. Unlike arguments of static function the arguments of global functions cannot be removed or optimized away by llvm. The compiler has to use exactly the arguments specified in a function prototype. The argument type information allows the verifier validate each global function independently. For now only supported argument types are pointer to context and scalars. In the future pointers to structures, sizes, pointer to packet data can be supported as well. Consider the following example:

static int f1(int ...)
{
...
}

int f3(int b);

int f2(int a)
{
f1(a) + f3(a);
}

int f3(int b)
{
...
}

int main(...)
{
f1(...) + f2(...) + f3(...);
}

The verifier will start its safety checks from the first global function f2(). It will recursively descend into f1() because it's static. Then it will check that arguments match for the f3() invocation inside f2(). It will not descend into f3(). It will finish f2() that has to be successfully verified for all possible values of 'a'. Then it will proceed with f3(). That function also has to be safe for all possible values of 'b'. Then it will start subprog 0 (which is main() function). It will recursively descend into f1() and will skip full check of f2() and f3(), since they are global. The order of processing global functions doesn't affect safety, since all global functions must be proven safe based on their arguments only.

Such function by function verification can drastically improve speed of the verification and reduce complexity.

Note that the stack limit of 512 still applies to the call chain regardless whether functions were static or global. The nested level of 8 also still applies. The same recursion prevention checks are in place as well.

Quoted 7 lines

The type information and static/global kind is preserved after the verification hence in the above example global function f2() and f3() can be replaced later by equivalent functions with the same types that are loaded and verified later without affecting safety of this main() program. Such replacement (re-linking) of global functions is a subject of future patches.

Great to see this progressing; and thanks for breaking things up, makes it much easier to follow along!

One question:

Quoted 8 lines

include/uapi/linux/btf.h enum { · line 153

Quoted 5 lines
include/uapi/linux/btf.h +5 -0
+enum btf_func_linkage {
+	BTF_FUNC_STATIC = 0,
+	BTF_FUNC_GLOBAL = 1,
+	BTF_FUNC_EXTERN = 2,
+};

What's supposed to happen with FUNC_EXTERN? That is specifically for the re-linking follow-up?

Quoted 5 lines

I was thinking to complete the whole thing with re-linking and then send it, but llvm 10 feature cut off date is end of this week, so we have to land llvm bits asap. I'd like to land patch 1 with libbpf sanitization first before landing llvm. llvm release cadence is ~4 month and it would be sad to miss it.

Agreed, it would be sad to miss the cutoff!

Quoted 7 lines

Note we will be able to tweak encoding if really necessary after next week. (BTF encoding gets fixed in ABI only after full kernel release). It's unlikely though. I think the encoding is good. I've played with few different variants and this one fits the best. FUNC_EXTERN encoding as 2 is kinda obvious when encoding for global vs static is selected. The kernel and libbpf will not be using FUNC_EXTERN yet, but llvm is tested to do the right thing already, so I think it's fine to add it to btf.h now.

Sure, OK. Don't have any objections (or opinions on the encoding, really), just want to understand how this all fits together.

Quoted 7 lines

As far as future plans when libbpf sees FUNC_EXTERN it will do the linking the way we discussed in the other thread. The kernel will support FUNC_EXTERN when we introduce dynamic libraries. A collection of bpf functions will be loaded into the kernel first (like libc.so) and later programs will have FUNC_EXTERN as part of their BTF to be resolved while loading. The func name to btf_id resolution will be done by libbpf. The kernel verifier will do the type checking on BTFs.

Right, FUNC_EXTERN will be rejected by the kernel unless it's patched up with "target" btf_ids by libbpf before load? So it'll be FUNC_GLOBAL-linked functions that will be replaceable after the fact with the "dynamic re-linking" feature?

Quoted 10 lines

So the kernel side of FUNC_EXTERN support will be minimal, but to your point below...

Quoted 4 lines

This doesn't reject linkage==BTF_FUNC_EXTERN; so for this patch FUNC_EXTERN will be treated the same as FUNC_STATIC (it'll fail the is_global check below)? Or did I miss somewhere else where BTF_FUNC_EXTERN is rejected?

... is absolutely correct. My bad. Added this bit too soon. Will remove. The kernel should accept FUNC_GLOBAL only in this patch set.

Great, thanks!

-Toke

#15 lore raw

Re: [PATCH bpf-next 3/6] bpf: Introduce function-by-function verification

Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 8 lines

As far as future plans when libbpf sees FUNC_EXTERN it will do the linking the way we discussed in the other thread. The kernel will support FUNC_EXTERN when we introduce dynamic libraries. A collection of bpf functions will be loaded into the kernel first (like libc.so) and later programs will have FUNC_EXTERN as part of their BTF to be resolved while loading. The func name to btf_id resolution will be done by libbpf. The kernel verifier will do the type checking on BTFs.

Quoted 4 lines

Right, FUNC_EXTERN will be rejected by the kernel unless it's patched up with "target" btf_ids by libbpf before load? So it'll be FUNC_GLOBAL-linked functions that will be replaceable after the fact with the "dynamic re-linking" feature?

Right. When libbpf statically links two .o it will need to produce a combined BTF out of these two .o. That new BTF will not have FUNC_EXTERN anymore if they are resolved. When the kernel sees FUNC_EXTERN it's a directive for the kernel to resolve it. BPF program with FUNC_EXTERN references would be loadable, but not executable. Anyhow the extern work is not immediate. I don't think any of that is necessary for dynamic re-linking.

#16 lore raw

Re: [PATCH bpf-next 3/6] bpf: Introduce function-by-function verification

Quoted 15 lines · Alexei Starovoitov
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 8 lines

As far as future plans when libbpf sees FUNC_EXTERN it will do the linking the way we discussed in the other thread. The kernel will support FUNC_EXTERN when we introduce dynamic libraries. A collection of bpf functions will be loaded into the kernel first (like libc.so) and later programs will have FUNC_EXTERN as part of their BTF to be resolved while loading. The func name to btf_id resolution will be done by libbpf. The kernel verifier will do the type checking on BTFs.

Quoted 4 lines

Right, FUNC_EXTERN will be rejected by the kernel unless it's patched up with "target" btf_ids by libbpf before load? So it'll be FUNC_GLOBAL-linked functions that will be replaceable after the fact with the "dynamic re-linking" feature?

Quoted 6 lines

Right. When libbpf statically links two .o it will need to produce a combined BTF out of these two .o. That new BTF will not have FUNC_EXTERN anymore if they are resolved. When the kernel sees FUNC_EXTERN it's a directive for the kernel to resolve it. BPF program with FUNC_EXTERN references would be loadable, but not executable. Anyhow the extern work is not immediate. I don't think any of that is necessary for dynamic re-linking.

Right, makes sense. I'll just wait for your follow-up series for the dynamic re-linking, then. Thanks for the explanation :)

-Toke

#21 lore raw

Re: [PATCH bpf-next 3/6] bpf: Introduce function-by-function verification

In reply to Alexei Starovoitov
Quoted 11 lines · Alexei Starovoitov

New llvm and old llvm with libbpf help produce BTF that distinguish global and static functions. Unlike arguments of static function the arguments of global functions cannot be removed or optimized away by llvm. The compiler has to use exactly the arguments specified in a function prototype. The argument type information allows the verifier validate each global function independently. For now only supported argument types are pointer to context and scalars. In the future pointers to structures, sizes, pointer to packet data can be supported as well. Consider the following example:

kernel/bpf/btf.c static s32 btf_func_check_meta(struct btf_verifier_env *env, · line 2626

Quoted 8 lines
kernel/bpf/btf.c +2 -2
@@ -2621,8 +2621,8 @@ static s32 btf_func_check_meta(struct btf_verifier_env *env,
 		return -EINVAL;
 	}
 
-	if (btf_type_vlen(t)) {
-		btf_verifier_log_type(env, t, "vlen != 0");
+	if (btf_type_vlen(t) > BTF_FUNC_EXTERN) {
+		btf_verifier_log_type(env, t, "invalid func linkage");
 		return -EINVAL;

Sorry for bringing it up after the review:

This effectively teaches kernel about BTF_KIND_FUNC scope argument, right? Which means, if I take clang from the tip
(https://github.com/llvm/llvm-project/commit/fbb64aa69835c8e3e9efe0afc8a73058b5a0fb3c#diff-f191c05d1eb0a6ca0e89d7e7938d73d4)
and take 5.4 kernel, it will reject BTF because it now has a BTF_KIND_FUNC with global scope (any 'main' function is global and has non-zero vlen).

What's the general guidance on the situation where clang starts spitting out some BTF and released kernels reject it? Is there some list of flags I can pass to clang to not emit some of the BTF features? Or am I missing something?

#22 lore raw

Re: [PATCH bpf-next 3/6] bpf: Introduce function-by-function verification

Quoted 30 lines · Stanislav Fomichev
Quoted 11 lines · Alexei Starovoitov

New llvm and old llvm with libbpf help produce BTF that distinguish global and static functions. Unlike arguments of static function the arguments of global functions cannot be removed or optimized away by llvm. The compiler has to use exactly the arguments specified in a function prototype. The argument type information allows the verifier validate each global function independently. For now only supported argument types are pointer to context and scalars. In the future pointers to structures, sizes, pointer to packet data can be supported as well. Consider the following example:

kernel/bpf/btf.c static s32 btf_func_check_meta(struct btf_verifier_env *env, · line 2626

Quoted 8 lines
kernel/bpf/btf.c +2 -2
@@ -2621,8 +2621,8 @@ static s32 btf_func_check_meta(struct btf_verifier_env *env,
              return -EINVAL;
      }
 
-     if (btf_type_vlen(t)) {
-             btf_verifier_log_type(env, t, "vlen != 0");
+     if (btf_type_vlen(t) > BTF_FUNC_EXTERN) {
+             btf_verifier_log_type(env, t, "invalid func linkage");
              return -EINVAL;

Sorry for bringing it up after the review:

This effectively teaches kernel about BTF_KIND_FUNC scope argument, right? Which means, if I take clang from the tip
(https://github.com/llvm/llvm-project/commit/fbb64aa69835c8e3e9efe0afc8a73058b5a0fb3c#diff-f191c05d1eb0a6ca0e89d7e7938d73d4)
and take 5.4 kernel, it will reject BTF because it now has a BTF_KIND_FUNC with global scope (any 'main' function is global and has non-zero vlen).

Quoted 4 lines

What's the general guidance on the situation where clang starts spitting out some BTF and released kernels reject it? Is there some list of flags I can pass to clang to not emit some of the BTF features? Or am I missing something?

Isn't that the issue that 2d3eb67f64ec ("libbpf: Sanitize global functions") addresses by sanitizing those BTF_KIND_FUNC as static functions (with vlen=0)?

The general guidance is to have libbpf sanitize such BTF to make it compatible with old kernels.

#23 lore raw

Re: [PATCH bpf-next 3/6] bpf: Introduce function-by-function verification

Quoted 36 lines · Andrii Nakryiko
Quoted 30 lines · Stanislav Fomichev
Quoted 11 lines · Alexei Starovoitov

New llvm and old llvm with libbpf help produce BTF that distinguish global and static functions. Unlike arguments of static function the arguments of global functions cannot be removed or optimized away by llvm. The compiler has to use exactly the arguments specified in a function prototype. The argument type information allows the verifier validate each global function independently. For now only supported argument types are pointer to context and scalars. In the future pointers to structures, sizes, pointer to packet data can be supported as well. Consider the following example:

kernel/bpf/btf.c static s32 btf_func_check_meta(struct btf_verifier_env *env, · line 2626

Quoted 8 lines
kernel/bpf/btf.c +2 -2
@@ -2621,8 +2621,8 @@ static s32 btf_func_check_meta(struct btf_verifier_env *env,
              return -EINVAL;
      }
 
-     if (btf_type_vlen(t)) {
-             btf_verifier_log_type(env, t, "vlen != 0");
+     if (btf_type_vlen(t) > BTF_FUNC_EXTERN) {
+             btf_verifier_log_type(env, t, "invalid func linkage");
              return -EINVAL;

Sorry for bringing it up after the review:

This effectively teaches kernel about BTF_KIND_FUNC scope argument, right? Which means, if I take clang from the tip
(https://github.com/llvm/llvm-project/commit/fbb64aa69835c8e3e9efe0afc8a73058b5a0fb3c#diff-f191c05d1eb0a6ca0e89d7e7938d73d4)
and take 5.4 kernel, it will reject BTF because it now has a BTF_KIND_FUNC with global scope (any 'main' function is global and has non-zero vlen).

Quoted 4 lines

What's the general guidance on the situation where clang starts spitting out some BTF and released kernels reject it? Is there some list of flags I can pass to clang to not emit some of the BTF features? Or am I missing something?

Quoted 6 lines

Isn't that the issue that 2d3eb67f64ec ("libbpf: Sanitize global functions") addresses by sanitizing those BTF_KIND_FUNC as static functions (with vlen=0)?

The general guidance is to have libbpf sanitize such BTF to make it compatible with old kernels.

Ah, that was the missing piece, thank you!