Skip to contentSkip to conversation

bpf: support proper non-jit func info

PATCH Applied Yonghong Song 3 participants 6 messages Nov 25, 2018 – Nov 27, 2018

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

cover bpf: support proper non-jit func info

Commit 838e96904ff3 ("bpf: Introduce bpf_func_info") added bpf func info support. The userspace is able to get better ksym's for bpf programs with jit, and is able to print out func prototypes.

For a program containing func-to-func calls, the existing implementation returns user specified number of function calls and BTF types if jit is enabled. If the jit is not enabled, it only returns the type for the main function.

This is undesirable. Interpreter may still be used and we should keep feature identical regardless of whether jit is enabled or not.
This patch fixed this discrepancy.

The following example shows bpftool output for the bpf program in selftests test_btf_haskv.o when jit is disabled:

  $ bpftool prog dump xlated id 1490
  int _dummy_tracepoint(struct dummy_tracepoint_args * arg):
     0: (85) call pc+2#__bpf_prog_run_args32
     1: (b7) r0 = 0
     2: (95) exit
  int test_long_fname_1(struct dummy_tracepoint_args * arg):
     3: (85) call pc+1#__bpf_prog_run_args32
     4: (95) exit
  int test_long_fname_2(struct dummy_tracepoint_args * arg):
     5: (b7) r2 = 0
     6: (63) *(u32 *)(r10 -4) = r2
     7: (79) r1 = *(u64 *)(r1 +8)
     8: (15) if r1 == 0x0 goto pc+9
     9: (bf) r2 = r10
    10: (07) r2 += -4
    11: (18) r1 = map[id:1173]
    13: (85) call bpf_map_lookup_elem#77088
    14: (15) if r0 == 0x0 goto pc+3
    15: (61) r1 = *(u32 *)(r0 +4)
    16: (07) r1 += 1
    17: (63) *(u32 *)(r0 +4) = r1
    18: (95) exit
  $ bpftool prog dump jited id 1490
    no instructions returned

Yonghong Song (2):
bpf: btf: support proper non-jit func info tools/bpf: change selftest test_btf for both jit and non-jit

 include/linux/bpf.h                    |  6 ++-
 include/linux/bpf_verifier.h           |  1 -
 kernel/bpf/core.c                      |  3 +-
 kernel/bpf/syscall.c                   | 33 ++++------------
 kernel/bpf/verifier.c                  | 55 ++++++++++++++++++--------
 tools/testing/selftests/bpf/test_btf.c | 33 ++--------------
 6 files changed, 55 insertions(+), 76 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 bpf: btf: support proper non-jit func info

Commit 838e96904ff3 ("bpf: Introduce bpf_func_info") added bpf func info support. The userspace is able to get better ksym's for bpf programs with jit, and is able to print out func prototypes.

For a program containing func-to-func calls, the existing implementation returns user specified number of function calls and BTF types if jit is enabled. If the jit is not enabled, it only returns the type for the main function.

This is undesirable. Interpreter may still be used and we should keep feature identical regardless of whether jit is enabled or not.
This patch fixed this discrepancy.

Fixes 838e96904ff3 ("bpf: Introduce bpf_func_info")
---
 include/linux/bpf.h          |  6 ++--
 include/linux/bpf_verifier.h |  1 -
 kernel/bpf/core.c            |  3 +-
 kernel/bpf/syscall.c         | 33 ++++++----------------
 kernel/bpf/verifier.c        | 55 +++++++++++++++++++++++++-----------
 5 files changed, 52 insertions(+), 46 deletions(-)
include/linux/bpf.h +4 -2
index 7f0e225bf630..e82b7039fc66 100644
@@ -299,7 +299,8 @@ struct bpf_prog_aux {
 	u32 max_pkt_offset;
 	u32 stack_depth;
 	u32 id;
-	u32 func_cnt;
+	u32 func_cnt; /* used by non-func prog as the number of func progs */
+	u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
 	bool offload_requested;
 	struct bpf_prog **func;
 	void *jit_data; /* JIT specific data. arch dependent */
@@ -317,7 +318,8 @@ struct bpf_prog_aux {
 #endif
 	struct bpf_prog_offload *offload;
 	struct btf *btf;
-	u32 type_id; /* type id for this prog/func */
+	struct bpf_func_info *func_info;
+	u32 func_info_cnt;
 	union {
 		struct work_struct work;
 		struct rcu_head	rcu;
include/linux/bpf_verifier.h +0 -1
index 204382f46fd8..11f5df1092d9 100644
@@ -204,7 +204,6 @@ static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)
 struct bpf_subprog_info {
 	u32 start; /* insn idx of function entry point */
 	u16 stack_depth; /* max. stack depth used by this function */
-	u32 type_id; /* btf type_id for this subprog */
 };
 
 /* single container for all structs
kernel/bpf/core.c +2 -1
index 16d77012ad3e..002d67c62c8b 100644
@@ -411,7 +411,8 @@ static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
 
 	/* prog->aux->name will be ignored if full btf name is available */
 	if (prog->aux->btf) {
-		type = btf_type_by_id(prog->aux->btf, prog->aux->type_id);
+		type = btf_type_by_id(prog->aux->btf,
+				      prog->aux->func_info[prog->aux->func_idx].type_id);
 		func_name = btf_name_by_offset(prog->aux->btf, type->name_off);
 		snprintf(sym, (size_t)(end - sym), "_%s", func_name);
 		return;
kernel/bpf/syscall.c +8 -25
index 998377808102..85cbeec06e50 100644
@@ -1214,6 +1214,7 @@ static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
 		bpf_prog_free_id(prog, do_idr_lock);
 		bpf_prog_kallsyms_del_all(prog);
 		btf_put(prog->aux->btf);
+		kvfree(prog->aux->func_info);
 
 		call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
 	}
@@ -2219,46 +2220,28 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
 	}
 
 	if (prog->aux->btf) {
+		u32 krec_size = sizeof(struct bpf_func_info);
 		u32 ucnt, urec_size;
 
 		info.btf_id = btf_id(prog->aux->btf);
 
 		ucnt = info.func_info_cnt;
-		info.func_info_cnt = prog->aux->func_cnt ? : 1;
+		info.func_info_cnt = prog->aux->func_info_cnt;
 		urec_size = info.func_info_rec_size;
-		info.func_info_rec_size = sizeof(struct bpf_func_info);
+		info.func_info_rec_size = krec_size;
 		if (ucnt) {
 			/* expect passed-in urec_size is what the kernel expects */
 			if (urec_size != info.func_info_rec_size)
 				return -EINVAL;
 
 			if (bpf_dump_raw_ok()) {
-				struct bpf_func_info kern_finfo;
 				char __user *user_finfo;
-				u32 i, insn_offset;
 
 				user_finfo = u64_to_user_ptr(info.func_info);
-				if (prog->aux->func_cnt) {
-					ucnt = min_t(u32, info.func_info_cnt, ucnt);
-					insn_offset = 0;
-					for (i = 0; i < ucnt; i++) {
-						kern_finfo.insn_offset = insn_offset;
-						kern_finfo.type_id = prog->aux->func[i]->aux->type_id;
-						if (copy_to_user(user_finfo, &kern_finfo,
-								 sizeof(kern_finfo)))
-							return -EFAULT;
-
-						/* func[i]->len holds the prog len */
-						insn_offset += prog->aux->func[i]->len;
-						user_finfo += urec_size;
-					}
-				} else {
-					kern_finfo.insn_offset = 0;
-					kern_finfo.type_id = prog->aux->type_id;
-					if (copy_to_user(user_finfo, &kern_finfo,
-							 sizeof(kern_finfo)))
-						return -EFAULT;
-				}
+				ucnt = min_t(u32, info.func_info_cnt, ucnt);
+				if (copy_to_user(user_finfo, prog->aux->func_info,
+						 krec_size * ucnt))
+					return -EFAULT;
 			} else {
 				info.func_info_cnt = 0;
 			}
kernel/bpf/verifier.c +38 -17

131 lines not on this page · raw message

Signature · 1 line
2.17.1
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH bpf-next 1/2] bpf: btf: support proper non-jit func info

Quoted 10 lines · Yonghong Song

Commit 838e96904ff3 ("bpf: Introduce bpf_func_info") added bpf func info support. The userspace is able to get better ksym's for bpf programs with jit, and is able to print out func prototypes.

For a program containing func-to-func calls, the existing implementation returns user specified number of function calls and BTF types if jit is enabled. If the jit is not enabled, it only returns the type for the main function.

Quoted 4 lines

This is undesirable. Interpreter may still be used and we should keep feature identical regardless of whether jit is enabled or not.
This patch fixed this discrepancy.

#3 lore raw

PATCH 2/2 tools/bpf: change selftest test_btf for both jit and non-jit

In reply to Yonghong Song

The selftest test_btf is changed to test both jit and non-jit. The test result should be the same regardless of whether jit is enabled or not.

---
 tools/testing/selftests/bpf/test_btf.c | 33 +++-----------------------
 1 file changed, 3 insertions(+), 30 deletions(-)
tools/testing/selftests/bpf/test_btf.c +3 -30
index bcbda7037840..b4c8725b3004 100644
@@ -29,7 +29,6 @@
 static uint32_t pass_cnt;
 static uint32_t error_cnt;
 static uint32_t skip_cnt;
-static bool jit_enabled;
 
 #define CHECK(condition, format...) ({					\
 	int __ret = !!(condition);					\
@@ -65,24 +64,6 @@ static int __base_pr(const char *format, ...)
 	return err;
 }
 
-static bool is_jit_enabled(void)
-{
-	const char *jit_sysctl = "/proc/sys/net/core/bpf_jit_enable";
-	bool enabled = false;
-	int sysctl_fd;
-
-	sysctl_fd = open(jit_sysctl, 0, O_RDONLY);
-	if (sysctl_fd != -1) {
-		char tmpc;
-
-		if (read(sysctl_fd, &tmpc, sizeof(tmpc)) == 1)
-			enabled = (tmpc != '0');
-		close(sysctl_fd);
-	}
-
-	return enabled;
-}
-
 #define BTF_INFO_ENC(kind, root, vlen)			\
 	((!!(root) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
 
@@ -2547,8 +2528,8 @@ static int do_test_file(unsigned int test_num)
 		  test->btf_kv_notfound))
 		goto done;
 
-	if (!jit_enabled || !has_btf_ext)
-		goto skip_jit;
+	if (!has_btf_ext)
+		goto skip;
 
 	/* get necessary program info */
 	info_len = sizeof(struct bpf_prog_info);
@@ -2636,7 +2617,7 @@ static int do_test_file(unsigned int test_num)
 		finfo = (void *)finfo + rec_size;
 	}
 
-skip_jit:
+skip:
 	fprintf(stderr, "OK");
 
 done:
@@ -3270,12 +3251,6 @@ static int do_test_func_type(int test_num)
 		err = -1;
 		goto done;
 	}
-	if (!jit_enabled) {
-		skip_cnt++;
-		fprintf(stderr, "SKIPPED, please enable sysctl bpf_jit_enable\n");
-		err = 0;
-		goto done;
-	}
 
 	/* get necessary lens */
 	info_len = sizeof(struct bpf_prog_info);
@@ -3452,8 +3427,6 @@ int main(int argc, char **argv)
 	if (args.always_log)
 		libbpf_set_print(__base_pr, __base_pr, __base_pr);
 
-	jit_enabled = is_jit_enabled();
-
 	if (args.raw_test)
 		err |= test_raw();
 
Signature · 1 line
2.17.1
Acked-by on v1 2/2
#4 lore raw

Re: [PATCH bpf-next 2/2] tools/bpf: change selftest test_btf for both jit and non-jit

Quoted 3 lines · Yonghong Song

The selftest test_btf is changed to test both jit and non-jit. The test result should be the same regardless of whether jit is enabled or not.

#5 lore raw
In reply to Yonghong Song
Quoted 34 lines · Yonghong Song

Commit 838e96904ff3 ("bpf: Introduce bpf_func_info") added bpf func info support. The userspace is able to get better ksym's for bpf programs with jit, and is able to print out func prototypes.

For a program containing func-to-func calls, the existing implementation returns user specified number of function calls and BTF types if jit is enabled. If the jit is not enabled, it only returns the type for the main function.

This is undesirable. Interpreter may still be used and we should keep feature identical regardless of whether jit is enabled or not.
This patch fixed this discrepancy.

The following example shows bpftool output for the bpf program in selftests test_btf_haskv.o when jit is disabled:

  $ bpftool prog dump xlated id 1490
  int _dummy_tracepoint(struct dummy_tracepoint_args * arg):
     0: (85) call pc+2#__bpf_prog_run_args32
     1: (b7) r0 = 0
     2: (95) exit
  int test_long_fname_1(struct dummy_tracepoint_args * arg):
     3: (85) call pc+1#__bpf_prog_run_args32
     4: (95) exit
  int test_long_fname_2(struct dummy_tracepoint_args * arg):
     5: (b7) r2 = 0
     6: (63) *(u32 *)(r10 -4) = r2
     7: (79) r1 = *(u64 *)(r1 +8)
     8: (15) if r1 == 0x0 goto pc+9
     9: (bf) r2 = r10
    10: (07) r2 += -4
    11: (18) r1 = map[id:1173]
Quoted 8 lines
    13: (85) call bpf_map_lookup_elem#77088
    14: (15) if r0 == 0x0 goto pc+3
    15: (61) r1 = *(u32 *)(r0 +4)
    16: (07) r1 += 1
    17: (63) *(u32 *)(r0 +4) = r1
    18: (95) exit
  $ bpftool prog dump jited id 1490
    no instructions returned

Applied to bpf-next. Thanks