Skip to content

Commit 05fe721

Browse files
image-dragonKernel Patches Daemon
authored andcommitted
bpf: add kfunc bpf_tracing_is_exit 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. Signed-off-by: Menglong Dong <[email protected]> Co-developed-by: Leon Hwang <[email protected]> Signed-off-by: Leon Hwang <[email protected]>
1 parent 4ffdcc3 commit 05fe721

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

kernel/bpf/verifier.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12293,6 +12293,7 @@ enum special_kfunc_type {
1229312293
KF___bpf_trap,
1229412294
KF_bpf_task_work_schedule_signal,
1229512295
KF_bpf_task_work_schedule_resume,
12296+
KF_bpf_tracing_is_exit,
1229612297
};
1229712298

1229812299
BTF_ID_LIST(special_kfunc_list)
@@ -12365,6 +12366,7 @@ BTF_ID(func, bpf_res_spin_unlock_irqrestore)
1236512366
BTF_ID(func, __bpf_trap)
1236612367
BTF_ID(func, bpf_task_work_schedule_signal)
1236712368
BTF_ID(func, bpf_task_work_schedule_resume)
12369+
BTF_ID(func, bpf_tracing_is_exit)
1236812370

1236912371
static bool is_task_work_add_kfunc(u32 func_id)
1237012372
{
@@ -12419,7 +12421,8 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,
1241912421
struct bpf_reg_state *reg = &regs[regno];
1242012422
bool arg_mem_size = false;
1242112423

12422-
if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx])
12424+
if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx] ||
12425+
meta->func_id == special_kfunc_list[KF_bpf_tracing_is_exit])
1242312426
return KF_ARG_PTR_TO_CTX;
1242412427

1242512428
/* In this function, we verify the kfunc's BTF as per the argument type,
@@ -22012,6 +22015,16 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2201222015
desc->func_id == special_kfunc_list[KF_bpf_rdonly_cast]) {
2201322016
insn_buf[0] = BPF_MOV64_REG(BPF_REG_0, BPF_REG_1);
2201422017
*cnt = 1;
22018+
} else if (desc->func_id == special_kfunc_list[KF_bpf_tracing_is_exit]) {
22019+
/* Load nr_args from ctx - 8 */
22020+
insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8);
22021+
/* add rax, 1 */
22022+
insn_buf[1] = BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1);
22023+
insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3);
22024+
insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1);
22025+
insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);
22026+
insn_buf[5] = BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 1);
22027+
*cnt = 6;
2201522028
}
2201622029

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

kernel/trace/bpf_trace.c

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,12 +3356,55 @@ 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)
3362+
{
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_end_defs();
3374+
3375+
BTF_KFUNCS_START(tracing_kfunc_set_ids)
3376+
BTF_ID_FLAGS(func, bpf_tracing_is_exit, KF_FASTCALL)
3377+
BTF_KFUNCS_END(tracing_kfunc_set_ids)
3378+
3379+
static int bpf_tracing_filter(const struct bpf_prog *prog, u32 kfunc_id)
33603380
{
3361-
return register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_kprobe_multi_kfunc_set);
3381+
if (!btf_id_set8_contains(&tracing_kfunc_set_ids, kfunc_id))
3382+
return 0;
3383+
3384+
if (prog->type != BPF_PROG_TYPE_TRACING ||
3385+
prog->expected_attach_type != BPF_TRACE_SESSION)
3386+
return -EINVAL;
3387+
3388+
return 0;
3389+
}
3390+
3391+
static const struct btf_kfunc_id_set bpf_tracing_kfunc_set = {
3392+
.owner = THIS_MODULE,
3393+
.set = &tracing_kfunc_set_ids,
3394+
.filter = bpf_tracing_filter,
3395+
};
3396+
3397+
static int __init bpf_trace_kfuncs_init(void)
3398+
{
3399+
int err = 0;
3400+
3401+
err = err ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_kprobe_multi_kfunc_set);
3402+
err = err ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_tracing_kfunc_set);
3403+
3404+
return err;
33623405
}
33633406

3364-
late_initcall(bpf_kprobe_multi_kfuncs_init);
3407+
late_initcall(bpf_trace_kfuncs_init);
33653408

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

0 commit comments

Comments
 (0)