Commit ca674a03 authored by Alexei Starovoitov's avatar Alexei Starovoitov
Browse files

Merge branch 'bpf-support-bpf_get_func_arg-for-bpf_trace_raw_tp'

Menglong Dong says:

====================
bpf: support bpf_get_func_arg() for BPF_TRACE_RAW_TP

Support bpf_get_func_arg() for BPF_TRACE_RAW_TP by getting the function
argument count from "prog->aux->attach_func_proto" during verifier inline.

Changes v5 -> v4:
* some format adjustment in the 1st patch
* v4: https://lore.kernel.org/bpf/20260120073046.324342-1-dongml2@chinatelecom.cn/

Changes v4 -> v3:
* fix the error of using bpf_get_func_arg() for BPF_TRACE_ITER
* v3: https://lore.kernel.org/bpf/20260119023732.130642-1-dongml2@chinatelecom.cn/

Changes v3 -> v2:
* remove unnecessary NULL checking for prog->aux->attach_func_proto
* v2: https://lore.kernel.org/bpf/20260116071739.121182-1-dongml2@chinatelecom.cn/

Changes v2 -> v1:
* for nr_args, skip first 'void *__data' argument in btf_trace_##name
  typedef
* check the result4 and result5 in the selftests
* v1: https://lore.kernel.org/bpf/20260116035024.98214-1-dongml2@chinatelecom.cn/
====================

Link: https://patch.msgid.link/20260121044348.113201-1-dongml2@chinatelecom.cn


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents ba335bf3 1ed79776
Loading
Loading
Loading
Loading
+18 −4
Original line number Diff line number Diff line
@@ -23741,8 +23741,15 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
		/* Implement bpf_get_func_arg inline. */
		if (prog_type == BPF_PROG_TYPE_TRACING &&
		    insn->imm == BPF_FUNC_get_func_arg) {
			if (eatype == BPF_TRACE_RAW_TP) {
				int nr_args = btf_type_vlen(prog->aux->attach_func_proto);
				/* skip 'void *__data' in btf_trace_##name() and save to reg0 */
				insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, nr_args - 1);
			} else {
				/* Load nr_args from ctx - 8 */
				insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8);
			}
			insn_buf[1] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6);
			insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3);
			insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1);
@@ -23794,8 +23801,15 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
		/* Implement get_func_arg_cnt inline. */
		if (prog_type == BPF_PROG_TYPE_TRACING &&
		    insn->imm == BPF_FUNC_get_func_arg_cnt) {
			if (eatype == BPF_TRACE_RAW_TP) {
				int nr_args = btf_type_vlen(prog->aux->attach_func_proto);
				/* skip 'void *__data' in btf_trace_##name() and save to reg0 */
				insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, nr_args - 1);
			} else {
				/* Load nr_args from ctx - 8 */
				insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8);
			}
			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1);
			if (!new_prog)
+8 −2
Original line number Diff line number Diff line
@@ -1734,11 +1734,17 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
	case BPF_FUNC_d_path:
		return &bpf_d_path_proto;
	case BPF_FUNC_get_func_arg:
		return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL;
		if (bpf_prog_has_trampoline(prog) ||
		    prog->expected_attach_type == BPF_TRACE_RAW_TP)
			return &bpf_get_func_arg_proto;
		return NULL;
	case BPF_FUNC_get_func_ret:
		return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
	case BPF_FUNC_get_func_arg_cnt:
		return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
		if (bpf_prog_has_trampoline(prog) ||
		    prog->expected_attach_type == BPF_TRACE_RAW_TP)
			return &bpf_get_func_arg_cnt_proto;
		return NULL;
	case BPF_FUNC_get_attach_cookie:
		if (prog->type == BPF_PROG_TYPE_TRACING &&
		    prog->expected_attach_type == BPF_TRACE_RAW_TP)
+3 −0
Original line number Diff line number Diff line
@@ -33,11 +33,14 @@ void test_get_func_args_test(void)

	ASSERT_EQ(topts.retval >> 16, 1, "test_run");
	ASSERT_EQ(topts.retval & 0xffff, 1234 + 29, "test_run");
	ASSERT_OK(trigger_module_test_read(1), "trigger_read");

	ASSERT_EQ(skel->bss->test1_result, 1, "test1_result");
	ASSERT_EQ(skel->bss->test2_result, 1, "test2_result");
	ASSERT_EQ(skel->bss->test3_result, 1, "test3_result");
	ASSERT_EQ(skel->bss->test4_result, 1, "test4_result");
	ASSERT_EQ(skel->bss->test5_result, 1, "test5_result");
	ASSERT_EQ(skel->bss->test6_result, 1, "test6_result");

cleanup:
	get_func_args_test__destroy(skel);
+44 −0
Original line number Diff line number Diff line
@@ -121,3 +121,47 @@ int BPF_PROG(fexit_test, int _a, int *_b, int _ret)
	test4_result &= err == 0 && ret == 1234;
	return 0;
}

__u64 test5_result = 0;
SEC("tp_btf/bpf_testmod_fentry_test1_tp")
int BPF_PROG(tp_test1)
{
	__u64 cnt = bpf_get_func_arg_cnt(ctx);
	__u64 a = 0, z = 0;
	__s64 err;

	test5_result = cnt == 1;

	err = bpf_get_func_arg(ctx, 0, &a);
	test5_result &= err == 0 && ((int) a == 1);

	/* not valid argument */
	err = bpf_get_func_arg(ctx, 1, &z);
	test5_result &= err == -EINVAL;

	return 0;
}

__u64 test6_result = 0;
SEC("tp_btf/bpf_testmod_fentry_test2_tp")
int BPF_PROG(tp_test2)
{
	__u64 cnt = bpf_get_func_arg_cnt(ctx);
	__u64 a = 0, b = 0, z = 0;
	__s64 err;

	test6_result = cnt == 2;

	/* valid arguments */
	err = bpf_get_func_arg(ctx, 0, &a);
	test6_result &= err == 0 && (int) a == 2;

	err = bpf_get_func_arg(ctx, 1, &b);
	test6_result &= err == 0 && b == 3;

	/* not valid argument */
	err = bpf_get_func_arg(ctx, 2, &z);
	test6_result &= err == -EINVAL;

	return 0;
}
+10 −0
Original line number Diff line number Diff line
@@ -63,6 +63,16 @@ BPF_TESTMOD_DECLARE_TRACE(bpf_testmod_test_writable_bare,
	sizeof(struct bpf_testmod_test_writable_ctx)
);

DECLARE_TRACE(bpf_testmod_fentry_test1,
	TP_PROTO(int a),
	TP_ARGS(a)
);

DECLARE_TRACE(bpf_testmod_fentry_test2,
	TP_PROTO(int a, u64 b),
	TP_ARGS(a, b)
);

#endif /* _BPF_TESTMOD_EVENTS_H */

#undef TRACE_INCLUDE_PATH
Loading