bpf_arch_ibpb() issues the BPF JIT-spraying predictor flush on every online CPU via on_each_cpu(). On systems with isolated/nohz_full CPUs, an (unprivileged) cBPF load/free therefore delivers IPIs and IBPBs to CPUs that never run BPF, causing latency spikes on cores dedicated to latency-sensitive workloads (the IPI steal plus a branch-predictor wipe that slows the victim afterwards). Use the per-CPU bpf_prog_ran flag introduced in the previous patch to flush only the CPUs that have executed a BPF program since the last flush. This is sound since a stale prediction for a reused JIT chunk can only exist on a CPU that executed the code previously placed in that chunk. Fixes: a3af84b0fa00 ("x86/bugs: Enable IBPB flush on BPF JIT allocation") Signed-off-by: Viktor Malik --- arch/x86/kernel/cpu/bugs.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index 4ced10c6ba0d..08eac5cf2c32 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -2253,12 +2253,18 @@ static void __init spectre_v2_update_mitigation(void) #ifdef CONFIG_BPF_JIT static void __bpf_arch_ibpb(void *unused) { + WRITE_ONCE(*raw_cpu_ptr(&bpf_prog_ran), false); write_ibpb(); } +static bool bpf_ibpb_needed(int cpu, void *unused) +{ + return READ_ONCE(per_cpu(bpf_prog_ran, cpu)); +} + void bpf_arch_ibpb(void) { - on_each_cpu(__bpf_arch_ibpb, NULL, 1); + on_each_cpu_cond(bpf_ibpb_needed, __bpf_arch_ibpb, NULL, 1); } static bool __init cpu_wants_ibpb_bpf(void) -- 2.56.1