[PATCH bpf-next 1/6] bpf: Defer instruction sync to the end of load time

From: Xu Kuohai

Date: Sun Dec 06 2026 - 04:51:33 EST


From: Xu Kuohai <xukuohai@xxxxxxxxxx>

Instruction sync is currently performed at JIT time. For a program
with multiple subprogs, this means one sync per subprog JIT, while
only the last one is needed. Instruction sync is a heavy operation on
some architectures, like arm64, which broadcasts IPIs to all online
CPUs to flush the pipeline.

So defer instruction sync to the end of load time. Two weak functions
are introduced for architectures to override:

- bpf_arch_text_copy_nosync(): Copy JITed text without instruction sync.
- bpf_arch_text_synchronize(): Perform the deferred sync.

Signed-off-by: Xu Kuohai <xukuohai@xxxxxxxxxx>
---
include/linux/bpf.h | 2 ++
kernel/bpf/core.c | 17 ++++++++++++++++-
2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 4f8d419fd824..058630e00dda 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -4505,6 +4505,8 @@ void bpf_arch_poke_desc_update(struct bpf_jit_poke_descriptor *poke,
struct bpf_prog *new, struct bpf_prog *old);

void *bpf_arch_text_copy(void *dst, void *src, size_t len);
+void *bpf_arch_text_copy_nosync(void *dst, void *src, size_t len);
+void bpf_arch_text_synchronize(void);
int bpf_arch_text_invalidate(void *dst, size_t len);

struct btf_id_set;
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 4249abbf6ae4..e3b7879992c7 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -194,6 +194,10 @@ void bpf_prog_jit_attempt_done(struct bpf_prog *prog)

bpf_free_kfunc_tab(prog->aux->kfunc_tab);
prog->aux->kfunc_tab = NULL;
+
+ /* Synchronize instructions for all online CPUs */
+ if (prog->jited)
+ bpf_arch_text_synchronize();
}

/* The jit engine is responsible to provide an array
@@ -1559,7 +1563,8 @@ int bpf_jit_binary_pack_finalize(struct bpf_binary_header *ro_header,
{
void *ptr;

- ptr = bpf_arch_text_copy(ro_header, rw_header, rw_header->size);
+ /* Synchronization is deferred to bpf_prog_jit_attempt_done() */
+ ptr = bpf_arch_text_copy_nosync(ro_header, rw_header, rw_header->size);

kvfree(rw_header);

@@ -4072,6 +4077,16 @@ void * __weak bpf_arch_text_copy(void *dst, void *src, size_t len)
return ERR_PTR(-ENOTSUPP);
}

+void * __weak bpf_arch_text_copy_nosync(void *dst, void *src, size_t len)
+{
+ /* Fall back to sync copy if the arch does not override this hook */
+ return bpf_arch_text_copy(dst, src, len);
+}
+
+void __weak bpf_arch_text_synchronize(void)
+{
+}
+
int __weak bpf_arch_text_invalidate(void *dst, size_t len)
{
return -ENOTSUPP;
--
2.47.3