Skip to contentSkip to conversation

bpf, arm64/riscv: Remove redundant icache flush after pack allocator finalize

PATCH Superseded Puranjay Mohan 2 participants 5 messages Apr 13

Subsystems (6)
BPF RISC-V arm64 BPF JIT for arm64 +2 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Puranjay Mohan

author maintainer
#0 lore raw

cover bpf, arm64/riscv: Remove redundant icache flush after pack allocator finalize

When the BPF prog pack allocator was added for arm64 and riscv, the existing bpf_flush_icache() calls were retained after bpf_jit_binary_pack_finalize(). However, the finalize path copies the JITed code via architecture text patching routines (__text_poke on arm64, patch_text_nosync on riscv) that already perform a full flush_icache_range() internally. The subsequent bpf_flush_icache() repeats the same cache maintenance on the same range.

Remove the redundant flush and the now-unused bpf_flush_icache() definitions on both architectures.

Puranjay Mohan (2):
bpf, arm64: Remove redundant bpf_flush_icache() after pack allocator
finalize
bpf, riscv: Remove redundant bpf_flush_icache() after pack allocator
finalize

 arch/arm64/net/bpf_jit_comp.c | 11 -----------
 arch/riscv/net/bpf_jit.h      |  5 -----
 arch/riscv/net/bpf_jit_core.c |  7 -------
 3 files changed, 23 deletions(-)

base-commit: 71b500afd2f7336f5b6c6026f2af546fc079be26

Signature · 1 line
2.52.0
author maintainer
#1 lore raw

PATCH 1/2 bpf, arm64: Remove redundant bpf_flush_icache() after pack allocator finalize

bpf_flush_icache() calls flush_icache_range() to clean the data cache and invalidate the instruction cache for the JITed code region. However, since commit 1dad391daef1 ("bpf, arm64: use bpf_prog_pack for memory management"), this flush is redundant.

bpf_jit_binary_pack_finalize() copies the JITed instructions to the ROX region via bpf_arch_text_copy() -> aarch64_insn_copy() -> __text_poke(), and __text_poke() already calls flush_icache_range() on the written range. The subsequent bpf_flush_icache() repeats the same cache maintenance on an overlapping range, including an unnecessary second synchronous IPI to all CPUs via kick_all_cpus_sync().

Remove the redundant bpf_flush_icache() call and its now-unused definition.

Fixes 1dad391daef1 ("bpf, arm64: use bpf_prog_pack for memory management")
---
 arch/arm64/net/bpf_jit_comp.c | 11 -----------
 1 file changed, 11 deletions(-)
arch/arm64/net/bpf_jit_comp.c +0 -11
index adf84962d579..e88b0917adec 100644
@@ -1961,11 +1961,6 @@ static int validate_ctx(struct jit_ctx *ctx)
 	return 0;
 }
 
-static inline void bpf_flush_icache(void *start, void *end)
-{
-	flush_icache_range((unsigned long)start, (unsigned long)end);
-}
-
 static void priv_stack_init_guard(void __percpu *priv_stack_ptr, int alloc_size)
 {
 	int cpu, underflow_idx = (alloc_size - PRIV_STACK_GUARD_SZ) >> 3;
@@ -2204,12 +2199,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
 			prog = orig_prog;
 			goto out_off;
 		}
-		/*
-		 * The instructions have now been copied to the ROX region from
-		 * where they will execute. Now the data cache has to be cleaned to
-		 * the PoU and the I-cache has to be invalidated for the VAs.
-		 */
-		bpf_flush_icache(ro_header, ctx.ro_image + ctx.idx);
 	} else {
 		jit_data->ctx = ctx;
 		jit_data->ro_image = ro_image_ptr;
Signature · 1 line
2.52.0
reviewer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH bpf-next 1/2] bpf, arm64: Remove redundant bpf_flush_icache() after pack allocator finalize

Quoted 13 lines · Puranjay Mohan

bpf_flush_icache() calls flush_icache_range() to clean the data cache and invalidate the instruction cache for the JITed code region. However, since commit 1dad391daef1 ("bpf, arm64: use bpf_prog_pack for memory management"), this flush is redundant.

bpf_jit_binary_pack_finalize() copies the JITed instructions to the ROX region via bpf_arch_text_copy() -> aarch64_insn_copy() -> __text_poke(), and __text_poke() already calls flush_icache_range() on the written range. The subsequent bpf_flush_icache() repeats the same cache maintenance on an overlapping range, including an unnecessary second synchronous IPI to all CPUs via kick_all_cpus_sync().

Quoted 5 lines

Remove the redundant bpf_flush_icache() call and its now-unused definition.

Fixes 1dad391daef1 ("bpf, arm64: use bpf_prog_pack for memory management")

We can now remove "#include <asm/cacheflush.h>".

Other than that,

author maintainer
#3 lore raw

PATCH 2/2 bpf, riscv: Remove redundant bpf_flush_icache() after pack allocator finalize

In reply to Puranjay Mohan

bpf_flush_icache() calls flush_icache_range() to clean the data cache and invalidate the instruction cache for the JITed code region. However, since commit 48a8f78c50bd ("bpf, riscv: use prog pack allocator in the BPF JIT"), this flush is redundant.

bpf_jit_binary_pack_finalize() copies the JITed instructions to the ROX region via bpf_arch_text_copy() -> patch_text_nosync(), and patch_text_nosync() already calls flush_icache_range() on the written range. The subsequent bpf_flush_icache() repeats the same cache maintenance on an overlapping range.

Remove the redundant bpf_flush_icache() call and its now-unused definition.

Fixes 48a8f78c50bd ("bpf, riscv: use prog pack allocator in the BPF JIT")
---
 arch/riscv/net/bpf_jit.h      | 5 -----
 arch/riscv/net/bpf_jit_core.c | 7 -------
 2 files changed, 12 deletions(-)
arch/riscv/net/bpf_jit.h +0 -5
index 632ced07bca4..549537cad86b 100644
@@ -105,11 +105,6 @@ static inline void bpf_fill_ill_insns(void *area, unsigned int size)
 	memset(area, 0, size);
 }
 
-static inline void bpf_flush_icache(void *start, void *end)
-{
-	flush_icache_range((unsigned long)start, (unsigned long)end);
-}
-
 /* Emit a 4-byte riscv instruction. */
 static inline void emit(const u32 insn, struct rv_jit_context *ctx)
 {
arch/riscv/net/bpf_jit_core.c +0 -7
index b3581e926436..f7fd4afc3ca3 100644
@@ -183,13 +183,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
 			prog = orig_prog;
 			goto out_offset;
 		}
-		/*
-		 * The instructions have now been copied to the ROX region from
-		 * where they will execute.
-		 * Write any modified data cache blocks out to memory and
-		 * invalidate the corresponding blocks in the instruction cache.
-		 */
-		bpf_flush_icache(jit_data->ro_header, ctx->ro_insns + ctx->ninsns);
 		for (i = 0; i < prog->len; i++)
 			ctx->offset[i] = ninsns_rvoff(ctx->offset[i]);
 		bpf_prog_fill_jited_linfo(prog, ctx->offset);
Signature · 1 line
2.52.0
reviewer Acked-by on v1 2/2
#4 lore raw

Re: [PATCH bpf-next 2/2] bpf, riscv: Remove redundant bpf_flush_icache() after pack allocator finalize

Quoted 12 lines · Puranjay Mohan

bpf_flush_icache() calls flush_icache_range() to clean the data cache and invalidate the instruction cache for the JITed code region. However, since commit 48a8f78c50bd ("bpf, riscv: use prog pack allocator in the BPF JIT"), this flush is redundant.

bpf_jit_binary_pack_finalize() copies the JITed instructions to the ROX region via bpf_arch_text_copy() -> patch_text_nosync(), and patch_text_nosync() already calls flush_icache_range() on the written range. The subsequent bpf_flush_icache() repeats the same cache maintenance on an overlapping range.

Quoted 5 lines

Remove the redundant bpf_flush_icache() call and its now-unused definition.

Fixes 48a8f78c50bd ("bpf, riscv: use prog pack allocator in the BPF JIT")

ditto on #include <asm/cacheflush.h>.