Skip to content

Commit e8d0133

Browse files
kkdwivediAlexei Starovoitov
authored andcommitted
bpf: Report may_goto timeout to BPF stderr
Begin reporting may_goto timeouts to BPF program's stderr stream. Acked-by: Eduard Zingerman <[email protected]> Reviewed-by: Emil Tsalapatis <[email protected]> Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent d7c431c commit e8d0133

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

kernel/bpf/core.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3168,6 +3168,22 @@ u64 __weak arch_bpf_timed_may_goto(void)
31683168
return 0;
31693169
}
31703170

3171+
static noinline void bpf_prog_report_may_goto_violation(void)
3172+
{
3173+
#ifdef CONFIG_BPF_SYSCALL
3174+
struct bpf_stream_stage ss;
3175+
struct bpf_prog *prog;
3176+
3177+
prog = bpf_prog_find_from_stack();
3178+
if (!prog)
3179+
return;
3180+
bpf_stream_stage(ss, prog, BPF_STDERR, ({
3181+
bpf_stream_printk(ss, "ERROR: Timeout detected for may_goto instruction\n");
3182+
bpf_stream_dump_stack(ss);
3183+
}));
3184+
#endif
3185+
}
3186+
31713187
u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *p)
31723188
{
31733189
u64 time = ktime_get_mono_fast_ns();
@@ -3178,8 +3194,10 @@ u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *p)
31783194
return BPF_MAX_TIMED_LOOPS;
31793195
}
31803196
/* Check if we've exhausted our time slice, and zero count. */
3181-
if (time - p->timestamp >= (NSEC_PER_SEC / 4))
3197+
if (unlikely(time - p->timestamp >= (NSEC_PER_SEC / 4))) {
3198+
bpf_prog_report_may_goto_violation();
31823199
return 0;
3200+
}
31833201
/* Refresh the count for the stack frame. */
31843202
return BPF_MAX_TIMED_LOOPS;
31853203
}

0 commit comments

Comments
 (0)