> compiler_types: Move notrace from inline to __always_inline
TL;DR:
- notrace in compiler_attributes.h may break the powerpc boot wrapper build
(unverified)
- dropping notrace from inline may allow ftrace recursion on microblaze,
x86-32 and other architectures (unlikely)
> diff --git a/include/linux/compiler_attributes.h
> b/include/linux/compiler_attributes.h
> --- a/include/linux/compiler_attributes.h
> +++ b/include/linux/compiler_attributes.h
[ ... ]
> -#define __always_inline inline
> __attribute__((__always_inline__))
> +#define __always_inline inline
> __attribute__((__always_inline__)) notrace
Is notrace defined everywhere compiler_attributes.h is used?
notrace is defined in compiler_types.h, and its definition depends on
CC_USING_HOTPATCH / CC_USING_PATCHABLE_FUNCTION_ENTRY. The powerpc boot
wrapper only gets compiler_attributes.h:
arch/powerpc/boot/Makefile:
BOOTCFLAGS += -include $(srctree)/include/linux/compiler_attributes.h
and deliberately does not -include compiler_types.h (see commit
77433830ed16 ("powerpc: boot: include compiler_attributes.h")).
With CONFIG_KERNEL_XZ=y, arch/powerpc/boot/decompress.c includes
lib/decompress_unxz.c, which pulls in lib/xz/xz_dec_lzma2.c:
static __always_inline void rc_normalize(struct rc_dec *rc)
This now preprocesses to:
static inline __attribute__((__always_inline__)) notrace void
rc_normalize(...)
Nothing under arch/powerpc/boot/, lib/xz/ or lib/decompress_unxz.c defines
notrace, so should decompress.o fail to compile? skiroot_defconfig sets
CONFIG_KERNEL_XZ=y. I could not compile this to confirm.
The comment at the top of compiler_attributes.h also says that attributes
which depend on a configuration option or on other attributes should be
defined elsewhere, e.g. compiler_types.h.
Would it work to leave this definition alone and add notrace to
__always_inline in compiler_types.h after notrace is defined?
Alternatively, the powerpc boot wrapper could get its own empty notrace
definition.
> diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
> --- a/include/linux/compiler_types.h
> +++ b/include/linux/compiler_types.h
[ ... ]
> -#define inline inline __gnu_inline __inline_maybe_unused notrace
> +#define inline inline __gnu_inline __inline_maybe_unused
Can the removal of notrace here cause recursion on microblaze?
ftrace_test_recursion_trylock() also reads preempt_count():
ftrace_test_recursion_trylock()
trace_test_and_set_recursion()
trace_get_context_bit()
interrupt_context_level()
preempt_count()
On microblaze, preempt_count() reads current_thread_info()->preempt_count,
and current_thread_info() is still a plain inline:
arch/microblaze/include/asm/thread_info.h:
static inline struct thread_info *current_thread_info(void)
fs/pstore/ftrace.c is built with CC_FLAGS_FTRACE, and pstore_ftrace_call()
calls ftrace_test_recursion_trylock() on an ops without
FTRACE_OPS_FL_RECURSION. If the compiler emits an out-of-line copy of
current_thread_info() in that object, the copy now has an mcount call:
mcount -> pstore_ftrace_call() -> ftrace_test_recursion_trylock()
-> current_thread_info() -> mcount
and the recursion is unbounded because protection is not yet held.
The neighbouring microblaze irqflags helpers are already 'static inline
notrace'. The trigger needs the compiler to keep a one-instruction helper
out of line, which is unlikely, but the series applied the same standard to
the equivalent helpers on csky, mips and sh. Should current_thread_info()
on microblaze be marked notrace too?
Similarly, the riscv patch in this series marks the arch_local_irq_*()
helpers notrace because ct_idle_exit() and cpu_idle_poll() call
raw_local_irq_save()/raw_local_irq_restore()/raw_local_irq_enable() while
RCU is not watching. Both are generic code in traced files
(kernel/context_tracking.c and kernel/sched/idle.c).
Several other architectures that select HAVE_FUNCTION_TRACER still define
these helpers as plain inline, so their out-of-line copies become
traceable:
arch/arm/include/asm/irqflags.h: arch_local_irq_save/enable/disable
arch/loongarch/include/asm/irqflags.h
arch/mips/include/asm/irqflags.h
arch/csky/include/asm/irqflags.h
arch/parisc/include/asm/irqflags.h
arch/xtensa/include/asm/irqflags.h
For example:
arch/loongarch/include/asm/irqflags.h:
static inline void arch_local_irq_enable(void)
s390 noinstr entry code has a similar gap: __do_pgm_check() calls the
plain-inline set_pt_regs_flag() before irqentry_enter().
The commit message says the previous patches mark notrace "the inline
helpers that relied on inline implying notrace", but the noinstr audit
covered only arm64, powerpc and riscv. Only x86_64 has objtool noinstr
validation.
Were arm, loongarch, mips, csky, parisc, xtensa and s390 checked, for
example with a clang build as was done for arm64 and riscv? If not, should
their irqflags helpers get the same notrace treatment?
A subsystem pattern also flags the following as potentially concerning. On
x86-32, HAVE_DYNAMIC_FTRACE_WITH_ARGS is only selected for X86_64, so
ftrace_get_regs() ends up calling the generic plain-inline helper:
include/linux/ftrace.h:
static inline struct pt_regs *arch_ftrace_get_regs(struct ftrace_regs
*fregs)
{
return &arch_ftrace_regs(fregs)->regs;
}
The x86 kprobe_ftrace_handler() calls it before taking recursion
protection:
arch/x86/kernel/kprobes/ftrace.c:kprobe_ftrace_handler() {
struct pt_regs *regs = ftrace_get_regs(fregs);
...
bit = ftrace_test_recursion_trylock(ip, parent_ip);
arch/x86/kernel/kprobes/ftrace.o is built with ftrace flags, and
kprobe_ftrace_ops and kprobe_ipmodify_ops set only FTRACE_OPS_FL_SAVE_REGS
(and IPMODIFY), not FTRACE_OPS_FL_RECURSION. Once inline no longer implies
notrace, an out-of-line copy of arch_ftrace_get_regs() in that object is a
traceable function called from an ftrace callback before recursion
protection.
The helper is a single address computation and is almost certainly inlined,
and recursion would also need a kprobe placed on the out-of-line copy
itself, so this seems very unlikely in practice. Would making the generic
arch_ftrace_get_regs() __always_inline, as the arch-specific versions
already are, remove the question?
---
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