Skip to content

Commit 169e265

Browse files
namhyungKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
bpf/perf: Call perf_prepare_sample() before bpf_prog_run()
When the BPF program calls bpf_cast_to_kern_ctx(), it assumes the program will access perf sample data directly and call perf_prepare_sample() to make sure the sample data is populated. Signed-off-by: Namhyung Kim <[email protected]>
1 parent 53bdda5 commit 169e265

File tree

3 files changed

+5
-0
lines changed

3 files changed

+5
-0
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,7 @@ struct bpf_prog {
13411341
enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */
13421342
call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */
13431343
call_get_func_ip:1, /* Do we call get_func_ip() */
1344+
call_cast_kctx:1, /* Do we call bpf_cast_to_kern_ctx() */
13441345
tstamp_type_access:1; /* Accessed __sk_buff->tstamp_type */
13451346
enum bpf_prog_type type; /* Type of BPF program */
13461347
enum bpf_attach_type expected_attach_type; /* For some prog types */

kernel/bpf/verifier.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9236,6 +9236,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
92369236
regs[BPF_REG_0].type = PTR_TO_BTF_ID | PTR_TRUSTED;
92379237
regs[BPF_REG_0].btf = desc_btf;
92389238
regs[BPF_REG_0].btf_id = meta.ret_btf_id;
9239+
env->prog->call_cast_kctx = 1;
92399240
} else if (meta.func_id == special_kfunc_list[KF_bpf_rdonly_cast]) {
92409241
ret_t = btf_type_by_id(desc_btf, meta.arg_constant.value);
92419242
if (!ret_t || !btf_type_is_struct(ret_t)) {

kernel/events/core.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10332,6 +10332,7 @@ static void bpf_overflow_handler(struct perf_event *event,
1033210332
.event = event,
1033310333
};
1033410334
struct bpf_prog *prog;
10335+
struct perf_event_header dummy;
1033510336
int ret = 0;
1033610337

1033710338
ctx.regs = perf_arch_bpf_user_pt_regs(regs);
@@ -10346,6 +10347,8 @@ static void bpf_overflow_handler(struct perf_event *event,
1034610347
data->callchain = perf_callchain(event, regs);
1034710348
data->sample_flags |= PERF_SAMPLE_CALLCHAIN;
1034810349
}
10350+
if (prog->call_cast_kctx)
10351+
perf_prepare_sample(&dummy, data, event, regs);
1034910352

1035010353
ret = bpf_prog_run(prog, &ctx);
1035110354
}

0 commit comments

Comments
 (0)