> bpf: Mark the BPF trampoline helpers notrace

TL;DR:
- Possible unbounded BPF trampoline recursion if powerpc local_add helpers
  stay unmarked notrace
- Same possible recursion on arm64 via unmarked __percpu_add_case_64() used by
  percpu_ref helpers

> diff --git a/arch/x86/include/asm/local.h b/arch/x86/include/asm/local.h
> --- a/arch/x86/include/asm/local.h
> +++ b/arch/x86/include/asm/local.h
> @@ -16,7 +16,7 @@ typedef struct {
>  #define local_read(l)        atomic_long_read(&(l)->a)
>  #define local_set(l, i)      atomic_long_set(&(l)->a, (i))
>
> -static inline void local_inc(local_t *l)
> +static inline void notrace local_inc(local_t *l)

The commit message says local_inc() and local_add() are marked "on x86",
because u64_stats_inc() and u64_stats_add() reach them from
bpf_prog_inc_misses_counter() and __update_prog_stats().

Should the powerpc equivalents be covered as well?  powerpc64 also
supports BPF trampolines and has its own plain __inline__ helpers in
arch/powerpc/include/asm/local.h:

#define LOCAL_OP_RETURN(op, c_op)                                     \
static __inline__ long local_##op##_return(long a, local_t *l)        \
...
#define local_inc_return(l)             local_add_return(1LL, l)
#define local_inc(l)                    local_inc_return(l)

On 64-bit, local64_inc() and local64_add() map to local_inc() and
local_add() (include/asm-generic/local64.h), so:

__bpf_prog_enter_recur()
  bpf_prog_inc_misses_counter()
    u64_stats_inc()
      local64_inc()
        local_add_return()

__bpf_prog_exit*()
  __update_prog_stats()
    u64_stats_inc()/u64_stats_add()
      local_add_return()/local_add()

Nothing in this series touches arch/powerpc/include/asm/local.h, and a
later commit in the series (compiler_types: Move notrace from inline to
__always_inline) removes the implied notrace from plain inline.  If an
out-of-line local_add_return() gets a patchable entry, attaching to it
would make __bpf_prog_enter_recur() reject the program and call
bpf_prog_inc_misses_counter(), which re-enters the trampoline, without
bound.

loongarch looks similar one level down: local_inc()/local_add() are
macros for atomic_long_inc()/atomic_long_add(), ending in
arch_atomic64_add(), a plain static inline in
arch/loongarch/include/asm/atomic-amo.h.

Should the powerpc local_add()/local_add_return() helpers be marked
notrace together with the x86 ones?

[ ... ]

> diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h
> --- a/include/linux/percpu-refcount.h
> +++ b/include/linux/percpu-refcount.h

[ ... ]

> @@ -195,7 +195,7 @@ static inline bool __ref_is_percpu(struct percpu_ref *ref,
>   *
>   * This function is safe to call as long as @ref is between init and exit.
>   */
> -static inline void percpu_ref_get_many(struct percpu_ref *ref, unsigned long 
> nr)
> +static inline void notrace percpu_ref_get_many(struct percpu_ref *ref, 
> unsigned long nr)
>  {
>       unsigned long __percpu *percpu_count;
>

This marks percpu_ref_get_many() and percpu_ref_put_many() notrace because
__bpf_tramp_enter() and __bpf_tramp_exit() call them.  On arm64, though,
the per-CPU op they use is still a plain inline helper that nothing in the
series marks:

this_cpu_add()
  this_cpu_add_8()
    __percpu_add_case_64()

and this_cpu_sub() is this_cpu_add() with a negated value.  In
arch/arm64/include/asm/percpu.h the helper is generated as:

#define __PERCPU_OP_CASE(w, sfx, name, sz, op_llsc, op_lse)           \
static inline void                                                    \
__percpu_##name##_case_##sz(void __percpu *pcp, unsigned long val)    \

The arm64 trampoline calls __bpf_tramp_enter() before the fentry programs
when BPF_TRAMP_F_CALL_ORIG is set, so:

arm64 trampoline
  __bpf_tramp_enter()
    percpu_ref_get()
      percpu_ref_get_many()
        this_cpu_add()
          __percpu_add_case_64()

and likewise __bpf_tramp_exit() -> percpu_ref_put_many() -> this_cpu_sub().

The only later commit in the series touching arch/arm64/include/asm/percpu.h
is "arm64: Mark helpers called from noinstr code notrace", which marks only
__kern_my_cpu_offset().  Its changelog says clang emits an out-of-line copy
of that helper in an arm64 defconfig build, and __percpu_add_case_64() has a
longer ALTERNATIVE asm body, so an out-of-line copy seems at least as
plausible.

Once "compiler_types: Move notrace from inline to __always_inline" lands,
such a copy gets a patchable entry.  An fexit or fmod_ret program attached
to it would recurse:

trampoline -> __bpf_tramp_enter() -> __percpu_add_case_64() -> trampoline
-> __bpf_tramp_enter() -> ...

This happens before any __bpf_prog_enter*() recursion check runs.  On x86
this_cpu_add() is an asm macro, so this is specific to arm64.

Should the arm64 __percpu_*_case_*() helpers, or at least the add case used
here, be marked notrace as part of this commit?


---
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/46577721107

Reply via email to