Skip to content

Commit 0bce547

Browse files
image-dragonKernel Patches Daemon
authored andcommitted
bpf: add kfunc bpf_fsession_cookie for TRACING SESSION
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]>
1 parent 7db1fff commit 0bce547

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,7 @@ struct bpf_prog {
17361736
enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */
17371737
call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */
17381738
call_get_func_ip:1, /* Do we call get_func_ip() */
1739+
call_session_cookie:1, /* Do we call bpf_fsession_cookie() */
17391740
tstamp_type_access:1, /* Accessed __sk_buff->tstamp_type */
17401741
sleepable:1; /* BPF program is sleepable */
17411742
enum bpf_prog_type type; /* Type of BPF program */

kernel/bpf/verifier.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12294,6 +12294,7 @@ enum special_kfunc_type {
1229412294
KF_bpf_task_work_schedule_signal,
1229512295
KF_bpf_task_work_schedule_resume,
1229612296
KF_bpf_tracing_is_exit,
12297+
KF_bpf_fsession_cookie,
1229712298
};
1229812299

1229912300
BTF_ID_LIST(special_kfunc_list)
@@ -12367,6 +12368,7 @@ BTF_ID(func, __bpf_trap)
1236712368
BTF_ID(func, bpf_task_work_schedule_signal)
1236812369
BTF_ID(func, bpf_task_work_schedule_resume)
1236912370
BTF_ID(func, bpf_tracing_is_exit)
12371+
BTF_ID(func, bpf_fsession_cookie)
1237012372

1237112373
static bool is_task_work_add_kfunc(u32 func_id)
1237212374
{
@@ -12422,7 +12424,8 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,
1242212424
bool arg_mem_size = false;
1242312425

1242412426
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])
12427+
meta->func_id == special_kfunc_list[KF_bpf_tracing_is_exit] ||
12428+
meta->func_id == special_kfunc_list[KF_bpf_fsession_cookie])
1242612429
return KF_ARG_PTR_TO_CTX;
1242712430

1242812431
/* In this function, we verify the kfunc's BTF as per the argument type,
@@ -13915,7 +13918,8 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1391513918
}
1391613919
}
1391713920

13918-
if (meta.func_id == special_kfunc_list[KF_bpf_session_cookie]) {
13921+
if (meta.func_id == special_kfunc_list[KF_bpf_session_cookie] ||
13922+
meta.func_id == special_kfunc_list[KF_bpf_fsession_cookie]) {
1391913923
meta.r0_size = sizeof(u64);
1392013924
meta.r0_rdonly = false;
1392113925
}
@@ -14196,6 +14200,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1419614200
return err;
1419714201
}
1419814202

14203+
if (meta.func_id == special_kfunc_list[KF_bpf_fsession_cookie])
14204+
env->prog->call_session_cookie = true;
14205+
1419914206
return 0;
1420014207
}
1420114208

@@ -22025,6 +22032,15 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2202522032
insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);
2202622033
insn_buf[5] = BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 1);
2202722034
*cnt = 6;
22035+
} else if (desc->func_id == special_kfunc_list[KF_bpf_fsession_cookie]) {
22036+
/* Load nr_args from ctx - 8 */
22037+
insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8);
22038+
/* add rax, 2 */
22039+
insn_buf[1] = BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 2);
22040+
insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3);
22041+
insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1);
22042+
insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);
22043+
*cnt = 5;
2202822044
}
2202922045

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

kernel/trace/bpf_trace.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3370,10 +3370,20 @@ __bpf_kfunc bool bpf_tracing_is_exit(void *ctx)
33703370
return ((u64 *)ctx)[nr_args + 1] & 1;
33713371
}
33723372

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+
33733382
__bpf_kfunc_end_defs();
33743383

33753384
BTF_KFUNCS_START(tracing_kfunc_set_ids)
33763385
BTF_ID_FLAGS(func, bpf_tracing_is_exit, KF_FASTCALL)
3386+
BTF_ID_FLAGS(func, bpf_fsession_cookie, KF_FASTCALL)
33773387
BTF_KFUNCS_END(tracing_kfunc_set_ids)
33783388

33793389
static int bpf_tracing_filter(const struct bpf_prog *prog, u32 kfunc_id)

0 commit comments

Comments
 (0)