Skip to content

Commit ebf96ad

Browse files
image-dragonKernel Patches Daemon
authored andcommitted
bpf: add two kfunc for TRACE_SESSION
If TRACE_SESSION exists, we will use extra 8-bytes in the stack of the trampoline to store the flags that we needed, and the 8-bytes lie after the return value, which means ctx[nr_args + 1]. And we will store the flag "is_exit" to the first bit of it. Introduce the kfunc bpf_tracing_is_exit(), which is used to tell if it is fexit currently. Meanwhile, inline it in the verifier. Add the kfunc bpf_fsession_cookie(), which is similar to bpf_session_cookie() and return the address of the session cookie. The address of the session cookie is stored after session flags, which means ctx[nr_args + 2]. Inline this kfunc in the verifier too. Signed-off-by: Menglong Dong <[email protected]> Co-developed-by: Leon Hwang <[email protected]> Signed-off-by: Leon Hwang <[email protected]>
1 parent c71941f commit ebf96ad

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,7 @@ struct bpf_prog {
17441744
enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */
17451745
call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */
17461746
call_get_func_ip:1, /* Do we call get_func_ip() */
1747+
call_session_cookie:1, /* Do we call bpf_fsession_cookie() */
17471748
tstamp_type_access:1, /* Accessed __sk_buff->tstamp_type */
17481749
sleepable:1; /* BPF program is sleepable */
17491750
enum bpf_prog_type type; /* Type of BPF program */

kernel/bpf/verifier.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12290,6 +12290,8 @@ enum special_kfunc_type {
1229012290
KF___bpf_trap,
1229112291
KF_bpf_task_work_schedule_signal,
1229212292
KF_bpf_task_work_schedule_resume,
12293+
KF_bpf_tracing_is_exit,
12294+
KF_bpf_fsession_cookie,
1229312295
};
1229412296

1229512297
BTF_ID_LIST(special_kfunc_list)
@@ -12364,6 +12366,8 @@ BTF_ID(func, bpf_dynptr_file_discard)
1236412366
BTF_ID(func, __bpf_trap)
1236512367
BTF_ID(func, bpf_task_work_schedule_signal)
1236612368
BTF_ID(func, bpf_task_work_schedule_resume)
12369+
BTF_ID(func, bpf_tracing_is_exit)
12370+
BTF_ID(func, bpf_fsession_cookie)
1236712371

1236812372
static bool is_task_work_add_kfunc(u32 func_id)
1236912373
{
@@ -12418,7 +12422,9 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,
1241812422
struct bpf_reg_state *reg = &regs[regno];
1241912423
bool arg_mem_size = false;
1242012424

12421-
if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx])
12425+
if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx] ||
12426+
meta->func_id == special_kfunc_list[KF_bpf_tracing_is_exit] ||
12427+
meta->func_id == special_kfunc_list[KF_bpf_fsession_cookie])
1242212428
return KF_ARG_PTR_TO_CTX;
1242312429

1242412430
/* In this function, we verify the kfunc's BTF as per the argument type,
@@ -13916,7 +13922,8 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1391613922
}
1391713923
}
1391813924

13919-
if (meta.func_id == special_kfunc_list[KF_bpf_session_cookie]) {
13925+
if (meta.func_id == special_kfunc_list[KF_bpf_session_cookie] ||
13926+
meta.func_id == special_kfunc_list[KF_bpf_fsession_cookie]) {
1392013927
meta.r0_size = sizeof(u64);
1392113928
meta.r0_rdonly = false;
1392213929
}
@@ -14203,6 +14210,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1420314210
return err;
1420414211
}
1420514212

14213+
if (meta.func_id == special_kfunc_list[KF_bpf_fsession_cookie])
14214+
env->prog->call_session_cookie = true;
14215+
1420614216
return 0;
1420714217
}
1420814218

@@ -22041,6 +22051,25 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2204122051
desc->func_id == special_kfunc_list[KF_bpf_rdonly_cast]) {
2204222052
insn_buf[0] = BPF_MOV64_REG(BPF_REG_0, BPF_REG_1);
2204322053
*cnt = 1;
22054+
} else if (desc->func_id == special_kfunc_list[KF_bpf_tracing_is_exit]) {
22055+
/* Load nr_args from ctx - 8 */
22056+
insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8);
22057+
/* add rax, 1 */
22058+
insn_buf[1] = BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1);
22059+
insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3);
22060+
insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1);
22061+
insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);
22062+
insn_buf[5] = BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 1);
22063+
*cnt = 6;
22064+
} else if (desc->func_id == special_kfunc_list[KF_bpf_fsession_cookie]) {
22065+
/* Load nr_args from ctx - 8 */
22066+
insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8);
22067+
/* add rax, 2 */
22068+
insn_buf[1] = BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 2);
22069+
insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3);
22070+
insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1);
22071+
insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);
22072+
*cnt = 5;
2204422073
}
2204522074

2204622075
if (env->insn_aux_data[insn_idx].arg_prog) {

kernel/trace/bpf_trace.c

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,12 +3356,65 @@ static const struct btf_kfunc_id_set bpf_kprobe_multi_kfunc_set = {
33563356
.filter = bpf_kprobe_multi_filter,
33573357
};
33583358

3359-
static int __init bpf_kprobe_multi_kfuncs_init(void)
3359+
__bpf_kfunc_start_defs();
3360+
3361+
__bpf_kfunc bool bpf_tracing_is_exit(void *ctx)
33603362
{
3361-
return register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_kprobe_multi_kfunc_set);
3363+
/* This helper call is inlined by verifier. */
3364+
u64 nr_args = ((u64 *)ctx)[-1];
3365+
3366+
/*
3367+
* ctx[nr_args + 1] is the session flags, and the last bit is
3368+
* is_exit.
3369+
*/
3370+
return ((u64 *)ctx)[nr_args + 1] & 1;
3371+
}
3372+
3373+
__bpf_kfunc u64 *bpf_fsession_cookie(void *ctx)
3374+
{
3375+
/* This helper call is inlined by verifier. */
3376+
u64 nr_args = ((u64 *)ctx)[-1];
3377+
3378+
/* ctx[nr_args + 2] is the session cookie address */
3379+
return (u64 *)((u64 *)ctx)[nr_args + 2];
3380+
}
3381+
3382+
__bpf_kfunc_end_defs();
3383+
3384+
BTF_KFUNCS_START(tracing_kfunc_set_ids)
3385+
BTF_ID_FLAGS(func, bpf_tracing_is_exit, KF_FASTCALL)
3386+
BTF_ID_FLAGS(func, bpf_fsession_cookie, KF_FASTCALL)
3387+
BTF_KFUNCS_END(tracing_kfunc_set_ids)
3388+
3389+
static int bpf_tracing_filter(const struct bpf_prog *prog, u32 kfunc_id)
3390+
{
3391+
if (!btf_id_set8_contains(&tracing_kfunc_set_ids, kfunc_id))
3392+
return 0;
3393+
3394+
if (prog->type != BPF_PROG_TYPE_TRACING ||
3395+
prog->expected_attach_type != BPF_TRACE_SESSION)
3396+
return -EINVAL;
3397+
3398+
return 0;
3399+
}
3400+
3401+
static const struct btf_kfunc_id_set bpf_tracing_kfunc_set = {
3402+
.owner = THIS_MODULE,
3403+
.set = &tracing_kfunc_set_ids,
3404+
.filter = bpf_tracing_filter,
3405+
};
3406+
3407+
static int __init bpf_trace_kfuncs_init(void)
3408+
{
3409+
int err = 0;
3410+
3411+
err = err ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_kprobe_multi_kfunc_set);
3412+
err = err ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_tracing_kfunc_set);
3413+
3414+
return err;
33623415
}
33633416

3364-
late_initcall(bpf_kprobe_multi_kfuncs_init);
3417+
late_initcall(bpf_trace_kfuncs_init);
33653418

33663419
typedef int (*copy_fn_t)(void *dst, const void *src, u32 size, struct task_struct *tsk);
33673420

0 commit comments

Comments
 (0)