> bpf: Add sleepable version of bpf_get_stack in tracing_prog_func_proto and 
> tp_prog_func_proto

TL;DR: Commit message and a code comment only: they say
bpf_get_stack_sleepable() uses build_id_parse(), but it appears to use
build_id_parse_file(), and the bpf_spin_lock() rejection claim looks wrong,
since locks already reject helper calls.

> bpf: Add sleepable version of bpf_get_stack in tracing_prog_func_proto and 
> tp_prog_func_proto
>
> bpf_get_stack() has two implementations.  bpf_get_stack() passes
> may_fault == false and resolves build IDs with build_id_parse_nofault(),
> so an IP whose backing file page is not resident falls back to
> BPF_STACK_BUILD_ID_IP.  bpf_get_stack_sleepable() passes may_fault == true
> and uses build_id_parse(), which faults the page in and returns a real
> build ID.
>
> Only kprobe/uprobe programs are routed to the sleepable implementation
> today, so the other program types that can be sleepable keep losing
> build IDs for non-resident pages:
>
>  - tracing_prog_func_proto() serves BPF_TRACE_ITER, fentry/fexit/fmod_ret
>    and BPF_TRACE_RAW_TP programs, and falls back to
>    raw_tp_prog_func_proto() for BPF_FUNC_get_stack, which is also the
>    get_func_proto() of BPF_PROG_TYPE_RAW_TRACEPOINT and is reached by
>    BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_SYSCALL through the default cases
>    of bpf_lsm_func_proto() and syscall_prog_func_proto().
>
>  - tp_prog_func_proto() serves BPF_PROG_TYPE_TRACEPOINT programs.
>
> Route them to bpf_get_stack_sleepable() as well and mark the new
> prototypes might_sleep, following
> commit 9d02927fdf4e ("bpf: Mark faultable stack helpers as sleepable"),
> which did the same for bpf_get_task_stack() and uprobe programs.
>
> The verifier then rejects bpf_get_stack() from these program types
> whenever in_sleepable_context() is false: inside bpf_rcu_read_lock(),
> bpf_preempt_disable(), bpf_local_irq_save() and bpf_spin_lock() regions,
> and in callback subprograms running under rcu_read_lock().  This also
> covers calls that do not pass BPF_F_USER_BUILD_ID and would not sleep;
> such programs load today.  This is the same trade off that making the
> faultable stack helpers sleepable made for bpf_get_task_stack().
>
> Signed-off-by: Feng Yang <[email protected]>
>

Two statements here don't seem to match the code.

The changelog says bpf_get_stack_sleepable() "uses build_id_parse()", and
the new comment in bpf_get_stack_raw_tp_sleepable() says the slot "cannot
be held across the sleep that build_id_parse() may take".  Is that accurate?

With may_fault set, stack_map_get_build_id_offset() takes this path:

    if (may_fault && has_user_ctx) {
            stack_map_get_build_id_offset_sleepable(id_offs, trace_nr);
            return;
    }

and stack_map_get_build_id_offset_sleepable() calls build_id_parse_file(),
not build_id_parse().  The only build_id_parse() call is in
fetch_build_id(), reached from the mmap_read_trylock() loop that is never
entered when may_fault is set.  Should the changelog and the comment name
build_id_parse_file() instead?

The changelog also lists bpf_spin_lock() regions among those where
bpf_get_stack() is now rejected, and says such programs "load today".
Is that right?  While active_locks is nonzero, do_check_insn() already
rejects helper calls before check_helper_call() is reached:

    if (env->cur_state->active_locks) {
            ...
                    insn->imm != BPF_FUNC_spin_unlock &&
                    insn->imm != BPF_FUNC_kptr_xchg) ||
            ...
                    "function calls are not allowed while holding a lock\n");

so bpf_get_stack() inside a bpf_spin_lock() region is not accepted today
either, and this patch changes nothing for that case.  The user-visible
change appears limited to RCU, preempt-disabled and IRQ-disabled regions,
RCU callbacks, and non-sleepable async callbacks such as bpf_timer
callbacks.  Could the bpf_spin_lock() mention be dropped from the
changelog?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/41802507259

Reply via email to