Introduce a 'jit_required' bitfield flag at the end of the flags group in struct bpf_prog. This bit tracks whether a program strictly requires the JIT compiler. Set this flag to 1 when a kfunc call is added at the end of bpf_add_kfunc_call(). In __bpf_prog_select_runtime(), check with fp->jit_required rather than bpf_prog_has_kfunc_call() to unify the logic. Suggested-by: Alexei Starovoitov Suggested-by: KaFai Wan Signed-off-by: Tiezhu Yang --- include/linux/bpf.h | 3 ++- kernel/bpf/core.c | 3 +-- kernel/bpf/verifier.c | 2 ++ 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index ba09795e0bfd..4e2b059d71f3 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1880,7 +1880,8 @@ struct bpf_prog { call_get_func_ip:1, /* Do we call get_func_ip() */ call_session_cookie:1, /* Do we call bpf_session_cookie() */ tstamp_type_access:1, /* Accessed __sk_buff->tstamp_type */ - sleepable:1; /* BPF program is sleepable */ + sleepable:1, /* BPF program is sleepable */ + jit_required:1; /* program strictly requires JIT compiler */ enum bpf_prog_type type; /* Type of BPF program */ enum bpf_attach_type expected_attach_type; /* For some prog types */ u32 len; /* Number of filter blocks */ diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 649cce41e13f..5fcd19ccb41a 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2619,8 +2619,7 @@ struct bpf_prog *__bpf_prog_select_runtime(struct bpf_verifier_env *env, struct if (fp->bpf_func) goto finalize; - if (IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON) || - bpf_prog_has_kfunc_call(fp)) + if (IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON) || fp->jit_required) jit_needed = true; if (!bpf_prog_select_interpreter(fp)) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 25aea4271cd0..f496b45b9da4 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -2765,6 +2765,8 @@ int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset) desc->func_model = func_model; sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), kfunc_desc_cmp_by_id_off, NULL); + + env->prog->jit_required = 1; return 0; } -- 2.42.0 When an architecture implements bpf_jit_inlines_helper_call() (such as LoongArch, ARM64, PowerPC, and RISC-V), the verifier skips rewriting the helper call offset (insn->imm) during the bpf_do_misc_fixups() phase, because the helper is expected to be inlined by the JIT compiler. As a result, insn->imm remains as the raw helper enum ID. However, if JIT is disabled at runtime (net.core.bpf_jit_enable=0) or if the JIT compilation later dynamically fails (e.g., due to OOM), the core BPF subsystem falls back to the BPF interpreter. When the interpreter executes (__bpf_call_base + insn->imm) with the unpatched raw helper ID, it jumps into an unaligned invalid address space, triggering an instruction alignment fault or a memory access panic. Fix this by setting 'jit_required' to 1 when helper call rewriting is skipped for JIT inlining. During runtime selection, if JIT compilation is not available, explicitly reject loading with -ENOTSUPP based on this flag to safely prevent the kernel panic. Fixes: 2ddec2c80b44 ("riscv, bpf: inline bpf_get_smp_processor_id()") Suggested-by: Alexei Starovoitov Suggested-by: KaFai Wan Signed-off-by: Tiezhu Yang --- kernel/bpf/fixups.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index 12a8a4eb757f..94e0457a0aa3 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -1841,8 +1841,10 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env) } /* Skip inlining the helper call if the JIT does it. */ - if (bpf_jit_inlines_helper_call(insn->imm)) + if (bpf_jit_inlines_helper_call(insn->imm)) { + prog->jit_required = 1; goto next_insn; + } if (insn->imm == BPF_FUNC_get_route_realm) prog->dst_needed = 1; -- 2.42.0