* [PATCH bpf-next 0/6] bpf, arm64: Reduce instruction sync IPIs
@ 2026-12-06 9:23 Xu Kuohai
2026-12-06 9:23 ` [PATCH bpf-next 1/6] bpf: Defer instruction sync to the end of load time Xu Kuohai
` (5 more replies)
0 siblings, 6 replies; 11+ messages in thread
From: Xu Kuohai @ 2026-12-06 9:23 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, John Fastabend,
Puranjay Mohan, Catalin Marinas, Will Deacon, Mark Rutland
From: Xu Kuohai <xukuohai@huawei.com>
On arm64, instruction sync broadcasts IPIs to all online CPUs. A BPF
program with multiple subprograms can trigger this synchronization
once for every subprogram during loading.
So defer instruction sync until all JIT images have been created and
the program is ready to execute. Apply the same approach to trampoline
creation so struct_ops can sync multiple trampolines with one IPI
broadcast.
Also remove the synchronization performed when invalidating freed BPF
image, since the memory is no longer executable and new code is
synchronized before it can run.
Xu Kuohai (6):
bpf: Defer instruction sync to the end of load time
bpf: Defer trampoline creation instruction sync
arm64: patching: Add nosync helpers for instruction copy and set
bpf, arm64: Defer JIT instruction sync
bpf, arm64: Defer trampoline creation instruction sync
bpf, arm64: Remove instruction sync when invalidating freed memory
arch/arm64/include/asm/text-patching.h | 2 +
arch/arm64/kernel/patching.c | 63 +++++++++++++++++++++++---
arch/arm64/net/bpf_jit_comp.c | 22 +++++++--
include/linux/bpf.h | 6 +++
kernel/bpf/bpf_struct_ops.c | 4 ++
kernel/bpf/core.c | 17 ++++++-
kernel/bpf/trampoline.c | 3 ++
net/bpf/bpf_dummy_struct_ops.c | 3 ++
8 files changed, 108 insertions(+), 12 deletions(-)
--
2.47.3
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH bpf-next 1/6] bpf: Defer instruction sync to the end of load time
2026-12-06 9:23 [PATCH bpf-next 0/6] bpf, arm64: Reduce instruction sync IPIs Xu Kuohai
@ 2026-12-06 9:23 ` Xu Kuohai
2026-12-06 9:23 ` [PATCH bpf-next 2/6] bpf: Defer trampoline creation instruction sync Xu Kuohai
` (4 subsequent siblings)
5 siblings, 0 replies; 11+ messages in thread
From: Xu Kuohai @ 2026-12-06 9:23 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, John Fastabend,
Puranjay Mohan, Catalin Marinas, Will Deacon, Mark Rutland
From: Xu Kuohai <xukuohai@huawei.com>
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@huawei.com>
---
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
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH bpf-next 2/6] bpf: Defer trampoline creation instruction sync
2026-12-06 9:23 [PATCH bpf-next 0/6] bpf, arm64: Reduce instruction sync IPIs Xu Kuohai
2026-12-06 9:23 ` [PATCH bpf-next 1/6] bpf: Defer instruction sync to the end of load time Xu Kuohai
@ 2026-12-06 9:23 ` Xu Kuohai
2026-12-06 9:23 ` [PATCH bpf-next 3/6] arm64: patching: Add nosync helpers for instruction copy and set Xu Kuohai
` (3 subsequent siblings)
5 siblings, 0 replies; 11+ messages in thread
From: Xu Kuohai @ 2026-12-06 9:23 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, John Fastabend,
Puranjay Mohan, Catalin Marinas, Will Deacon, Mark Rutland
From: Xu Kuohai <xukuohai@huawei.com>
Like JIT, instruction sync is performed at trampoline creation time.
For struct_ops, which creates multiple trampolines at once, only the
final sync is needed. Defer the instruction sync to the time before
the trampoline is registered or linked, and document the requirement
on arch_prepare_bpf_trampoline().
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
include/linux/bpf.h | 4 ++++
kernel/bpf/bpf_struct_ops.c | 4 ++++
kernel/bpf/trampoline.c | 3 +++
net/bpf/bpf_dummy_struct_ops.c | 3 +++
4 files changed, 14 insertions(+)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 058630e00dda..7bceb2a34757 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1406,6 +1406,10 @@ struct bpf_tramp_run_ctx;
* fexit = a set of program to run after original function
*/
struct bpf_tramp_image;
+/*
+ * NOTE: The caller must call bpf_arch_text_synchronize() before executing or
+ * publishing the generated trampolines.
+ */
int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,
const struct btf_func_model *m, u32 flags,
struct bpf_tramp_nodes *tnodes,
diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c
index 6ac596c156c2..fee3280b680a 100644
--- a/kernel/bpf/bpf_struct_ops.c
+++ b/kernel/bpf/bpf_struct_ops.c
@@ -1133,6 +1133,10 @@ static long __bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
goto reset_unlock;
}
+ /* Synchronize all member trampolines before handing them to the subsystem. */
+ if (st_map->image_pages_cnt)
+ bpf_arch_text_synchronize();
+
if (st_map->map.map_flags & BPF_F_LINK) {
err = 0;
/* Let bpf_link handle registration & unregistration.
diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index 619297eae8c1..17656a548769 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -794,6 +794,9 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut
if (err)
goto out_free;
+ /* Synchronize the trampoline image before publishing it. */
+ bpf_arch_text_synchronize();
+
if (tr->cur_image)
/* progs already running at this address */
err = ops->modify_fentry(tr, orig_flags, im, lock_direct_mutex, data);
diff --git a/net/bpf/bpf_dummy_struct_ops.c b/net/bpf/bpf_dummy_struct_ops.c
index c724a6bc7b80..001c42816aa8 100644
--- a/net/bpf/bpf_dummy_struct_ops.c
+++ b/net/bpf/bpf_dummy_struct_ops.c
@@ -186,6 +186,9 @@ int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
err = arch_protect_bpf_trampoline(image, PAGE_SIZE);
if (err)
goto out;
+
+ bpf_arch_text_synchronize();
+
prog_ret = dummy_ops_call_op(image, args);
err = dummy_ops_copy_args(args);
--
2.47.3
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH bpf-next 3/6] arm64: patching: Add nosync helpers for instruction copy and set
2026-12-06 9:23 [PATCH bpf-next 0/6] bpf, arm64: Reduce instruction sync IPIs Xu Kuohai
2026-12-06 9:23 ` [PATCH bpf-next 1/6] bpf: Defer instruction sync to the end of load time Xu Kuohai
2026-12-06 9:23 ` [PATCH bpf-next 2/6] bpf: Defer trampoline creation instruction sync Xu Kuohai
@ 2026-12-06 9:23 ` Xu Kuohai
2026-12-06 10:12 ` sashiko-bot
2026-12-06 9:23 ` [PATCH bpf-next 4/6] bpf, arm64: Defer JIT instruction sync Xu Kuohai
` (2 subsequent siblings)
5 siblings, 1 reply; 11+ messages in thread
From: Xu Kuohai @ 2026-12-06 9:23 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, John Fastabend,
Puranjay Mohan, Catalin Marinas, Will Deacon, Mark Rutland
From: Xu Kuohai <xukuohai@huawei.com>
Add nosync variants of the arm64 instruction copy and set functions.
They will be used by the following patches to remove unnecessary IPIs.
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
arch/arm64/include/asm/text-patching.h | 2 +
arch/arm64/kernel/patching.c | 63 +++++++++++++++++++++++---
2 files changed, 58 insertions(+), 7 deletions(-)
diff --git a/arch/arm64/include/asm/text-patching.h b/arch/arm64/include/asm/text-patching.h
index 587bdb91ab7a..222566b020f2 100644
--- a/arch/arm64/include/asm/text-patching.h
+++ b/arch/arm64/include/asm/text-patching.h
@@ -9,7 +9,9 @@ int aarch64_insn_write(void *addr, u32 insn);
int aarch64_insn_write_literal_u64(void *addr, u64 val);
void *aarch64_insn_set(void *dst, u32 insn, size_t len);
+void *aarch64_insn_set_nosync(void *dst, u32 insn, size_t len);
void *aarch64_insn_copy(void *dst, void *src, size_t len);
+void *aarch64_insn_copy_nosync(void *dst, void *src, size_t len);
int aarch64_insn_patch_text_nosync(void *addr, u32 insn);
int aarch64_insn_patch_text(void *addrs[], u32 insns[], int cnt);
diff --git a/arch/arm64/kernel/patching.c b/arch/arm64/kernel/patching.c
index 09f019c6547a..5e28476db046 100644
--- a/arch/arm64/kernel/patching.c
+++ b/arch/arm64/kernel/patching.c
@@ -104,7 +104,8 @@ noinstr int aarch64_insn_write_literal_u64(void *addr, u64 val)
typedef void text_poke_f(void *dst, void *src, size_t patched, size_t len);
-static void *__text_poke(text_poke_f func, void *addr, void *src, size_t len)
+static void *__text_poke(text_poke_f func, void *addr, void *src, size_t len,
+ bool sync)
{
unsigned long flags;
size_t patched = 0;
@@ -126,7 +127,15 @@ static void *__text_poke(text_poke_f func, void *addr, void *src, size_t len)
}
raw_spin_unlock_irqrestore(&patch_lock, flags);
- flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len);
+ caches_clean_inval_pou((uintptr_t)addr, (uintptr_t)addr + len);
+ /*
+ * IPI all online CPUs to force them to refetch the new instructions.
+ * KGDB uses IPIs to round-up the secondary CPUs during the patching
+ * operation, so we don't need extra IPIs here. See the comments in
+ * flush_icache_range() in arch/arm64/include/asm/cacheflush.h.
+ */
+ if (sync && !in_dbg_master())
+ kick_all_cpus_sync();
return addr;
}
@@ -143,6 +152,15 @@ static void text_poke_memset(void *dst, void *src, size_t patched, size_t len)
memset32(dst, c, len / 4);
}
+static void *__aarch64_insn_copy(void *dst, void *src, size_t len, bool sync)
+{
+ /* A64 instructions must be word aligned */
+ if ((uintptr_t)dst & 0x3)
+ return NULL;
+
+ return __text_poke(text_poke_memcpy, dst, src, len, sync);
+}
+
/**
* aarch64_insn_copy - Copy instructions into (an unused part of) RX memory
* @dst: address to modify
@@ -153,11 +171,30 @@ static void text_poke_memset(void *dst, void *src, size_t patched, size_t len)
*/
noinstr void *aarch64_insn_copy(void *dst, void *src, size_t len)
{
- /* A64 instructions must be word aligned */
+ return __aarch64_insn_copy(dst, src, len, true);
+}
+
+/**
+ * aarch64_insn_copy_nosync - Copy instructions into RX memory, no sync
+ * @dst: address to modify
+ * @src: source of the copy
+ * @len: length to copy
+ *
+ * Same as aarch64_insn_copy(), but does not IPI other CPUs to refetch
+ * the new instructions. The caller must do the synchronization before
+ * the text is executed.
+ */
+noinstr void *aarch64_insn_copy_nosync(void *dst, void *src, size_t len)
+{
+ return __aarch64_insn_copy(dst, src, len, false);
+}
+
+static void *__aarch64_insn_set(void *dst, u32 insn, size_t len, bool sync)
+{
if ((uintptr_t)dst & 0x3)
return NULL;
- return __text_poke(text_poke_memcpy, dst, src, len);
+ return __text_poke(text_poke_memset, dst, &insn, len, sync);
}
/**
@@ -170,10 +207,22 @@ noinstr void *aarch64_insn_copy(void *dst, void *src, size_t len)
*/
noinstr void *aarch64_insn_set(void *dst, u32 insn, size_t len)
{
- if ((uintptr_t)dst & 0x3)
- return NULL;
+ return __aarch64_insn_set(dst, insn, len, true);
+}
- return __text_poke(text_poke_memset, dst, &insn, len);
+/**
+ * aarch64_insn_set_nosync - memset for RX memory regions, no sync
+ * @dst: address to modify
+ * @insn: value to set
+ * @len: length of memory region.
+ *
+ * Same as aarch64_insn_set(), but does not IPI other CPUs to refetch
+ * the new instructions. The caller must do the synchronization before
+ * the text is executed.
+ */
+noinstr void *aarch64_insn_set_nosync(void *dst, u32 insn, size_t len)
+{
+ return __aarch64_insn_set(dst, insn, len, false);
}
int __kprobes aarch64_insn_patch_text_nosync(void *addr, u32 insn)
--
2.47.3
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH bpf-next 4/6] bpf, arm64: Defer JIT instruction sync
2026-12-06 9:23 [PATCH bpf-next 0/6] bpf, arm64: Reduce instruction sync IPIs Xu Kuohai
` (2 preceding siblings ...)
2026-12-06 9:23 ` [PATCH bpf-next 3/6] arm64: patching: Add nosync helpers for instruction copy and set Xu Kuohai
@ 2026-12-06 9:23 ` Xu Kuohai
2026-12-06 10:14 ` sashiko-bot
2026-12-06 9:23 ` [PATCH bpf-next 5/6] bpf, arm64: Defer trampoline creation " Xu Kuohai
2026-12-06 9:23 ` [PATCH bpf-next 6/6] bpf, arm64: Remove instruction sync when invalidating freed memory Xu Kuohai
5 siblings, 1 reply; 11+ messages in thread
From: Xu Kuohai @ 2026-12-06 9:23 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, John Fastabend,
Puranjay Mohan, Catalin Marinas, Will Deacon, Mark Rutland
From: Xu Kuohai <xukuohai@huawei.com>
Override bpf_arch_text_copy_nosync() and bpf_arch_text_synchronize()
on arm64 to enable deferred instruction sync for JIT. Also switch the
JIT error path to the nosync text copy.
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
arch/arm64/net/bpf_jit_comp.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index 3ac287580d22..76d25947ace6 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -3231,8 +3231,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
prog->jited_len = 0;
}
if (header) {
- bpf_arch_text_copy(&ro_header->size, &header->size,
- sizeof(header->size));
+ bpf_arch_text_copy_nosync(&ro_header->size, &header->size,
+ sizeof(header->size));
bpf_jit_binary_pack_free(ro_header, header);
/* The private stack goes with the image, see out_off */
ro_header = NULL;
@@ -3288,6 +3288,18 @@ void *bpf_arch_text_copy(void *dst, void *src, size_t len)
return dst;
}
+void *bpf_arch_text_copy_nosync(void *dst, void *src, size_t len)
+{
+ if (!aarch64_insn_copy_nosync(dst, src, len))
+ return ERR_PTR(-EINVAL);
+ return dst;
+}
+
+void bpf_arch_text_synchronize(void)
+{
+ kick_all_cpus_sync();
+}
+
u64 bpf_jit_alloc_exec_limit(void)
{
return VMALLOC_END - VMALLOC_START;
--
2.47.3
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH bpf-next 5/6] bpf, arm64: Defer trampoline creation instruction sync
2026-12-06 9:23 [PATCH bpf-next 0/6] bpf, arm64: Reduce instruction sync IPIs Xu Kuohai
` (3 preceding siblings ...)
2026-12-06 9:23 ` [PATCH bpf-next 4/6] bpf, arm64: Defer JIT instruction sync Xu Kuohai
@ 2026-12-06 9:23 ` Xu Kuohai
2026-12-06 10:12 ` sashiko-bot
2026-12-06 9:23 ` [PATCH bpf-next 6/6] bpf, arm64: Remove instruction sync when invalidating freed memory Xu Kuohai
5 siblings, 1 reply; 11+ messages in thread
From: Xu Kuohai @ 2026-12-06 9:23 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, John Fastabend,
Puranjay Mohan, Catalin Marinas, Will Deacon, Mark Rutland
From: Xu Kuohai <xukuohai@huawei.com>
Use bpf_arch_text_copy_nosync() to copy trampoline images since the
generic code handles the instruction sync. Also return immediately on
trampoline generation failure, to avoid copying a discarded partial
image.
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
arch/arm64/net/bpf_jit_comp.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index 76d25947ace6..7b5519f1159c 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -4002,6 +4002,8 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,
if (ret)
goto out;
ret = prepare_trampoline(&ctx, im, tnodes, func_addr, m, &aaux, flags);
+ if (ret < 0)
+ goto out;
if (ret > 0 && validate_code(&ctx) < 0) {
ret = -EINVAL;
@@ -4011,7 +4013,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,
if (ret > 0)
ret *= AARCH64_INSN_SIZE;
- tmp = bpf_arch_text_copy(ro_image, image, size);
+ tmp = bpf_arch_text_copy_nosync(ro_image, image, size);
if (IS_ERR(tmp)) {
ret = PTR_ERR(tmp);
goto out;
--
2.47.3
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH bpf-next 6/6] bpf, arm64: Remove instruction sync when invalidating freed memory
2026-12-06 9:23 [PATCH bpf-next 0/6] bpf, arm64: Reduce instruction sync IPIs Xu Kuohai
` (4 preceding siblings ...)
2026-12-06 9:23 ` [PATCH bpf-next 5/6] bpf, arm64: Defer trampoline creation " Xu Kuohai
@ 2026-12-06 9:23 ` Xu Kuohai
2026-12-06 10:19 ` bot+bpf-ci
5 siblings, 1 reply; 11+ messages in thread
From: Xu Kuohai @ 2026-12-06 9:23 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, John Fastabend,
Puranjay Mohan, Catalin Marinas, Will Deacon, Mark Rutland
From: Xu Kuohai <xukuohai@huawei.com>
The freed BPF image is no longer usable. When its memory is reused,
the new image is copied and an instruction sync is performed. So the
sync on the free path is not needed. Remove it.
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
arch/arm64/net/bpf_jit_comp.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index 7b5519f1159c..605deb44ccff 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -353,7 +353,7 @@ static void jit_fill_hole(void *area, unsigned int size)
int bpf_arch_text_invalidate(void *dst, size_t len)
{
- if (!aarch64_insn_set(dst, AARCH64_BREAK_FAULT, len))
+ if (!aarch64_insn_set_nosync(dst, AARCH64_BREAK_FAULT, len))
return -EINVAL;
return 0;
--
2.47.3
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH bpf-next 5/6] bpf, arm64: Defer trampoline creation instruction sync
2026-12-06 9:23 ` [PATCH bpf-next 5/6] bpf, arm64: Defer trampoline creation " Xu Kuohai
@ 2026-12-06 10:12 ` sashiko-bot
0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-12-06 10:12 UTC (permalink / raw)
To: Xu Kuohai, bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] bpf, arm64: bpf_arch_text_synchronize() misses local isb() on
CPU migration
--
commit 3e973e1a645e674c6e02bd2b7e8f11e0c65b2a35
Author: Xu Kuohai <xukuohai@huawei.com>
bpf, arm64: Defer trampoline creation instruction sync
This commit switches arch_prepare_bpf_trampoline() to use
bpf_arch_text_copy_nosync() so instruction synchronization is deferred
to generic code, and returns immediately when prepare_trampoline()
fails.
> diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
> index 76d25947ace65..7b5519f1159cc 100644
> --- a/arch/arm64/net/bpf_jit_comp.c
> +++ b/arch/arm64/net/bpf_jit_comp.c
> @@ -4002,6 +4002,8 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,
[ ... ]
> if (ret > 0)
> ret *= AARCH64_INSN_SIZE;
>
> - tmp = bpf_arch_text_copy(ro_image, image, size);
> + tmp = bpf_arch_text_copy_nosync(ro_image, image, size);
[Severity: Medium]
Can task migration between bpf_arch_text_copy_nosync() and
bpf_arch_text_synchronize() leave the destination CPU without a context
synchronization event before it executes the new trampoline?
aarch64_insn_copy_nosync() runs caches_clean_inval_pou(), which emits
isb only on the local CPU. Because preemption is enabled and callers
sleep before bpf_arch_text_synchronize() (e.g. kvfree() here, or the
allocations in __bpf_struct_ops_map_update_elem()), the task can migrate:
CPU0 CPU1
(speculatively prefetches recycled
bpf_prog_pack ROX slot)
arch_prepare_bpf_trampoline()
bpf_arch_text_copy_nosync()
caches_clean_inval_pou()
isb (CPU0 only)
kvfree(image)
... (task migrates) ---------->
__switch_to() (dsb, no isb)
bpf_arch_text_synchronize()
kick_all_cpus_sync() (skips CPU1)
dummy_ops_call_op() <-- stale insns
On arm64, __switch_to() emits dsb(ish) without guaranteeing an isb, and
bpf_arch_text_synchronize() only calls kick_all_cpus_sync(). That calls
smp_call_function(), which skips the calling CPU and does not guarantee
a local isb on the sender (e.g. on GICv2, or when no remote IPI is sent).
If CPU1 then executes the new trampoline (such as dummy_ops_call_op()
right after bpf_arch_text_synchronize() in bpf_struct_ops_test_run()),
it can execute stale prefetched instructions from the recycled
bpf_prog_pack slot, trapping on AARCH64_BREAK_FAULT or running old code.
Disabling preemption around kick_all_cpus_sync() and issuing a local
isb() in bpf_arch_text_synchronize() would ensure the calling CPU is
synchronized as well.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796545068.git.xukuohai@huawei.com?part=5
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH bpf-next 3/6] arm64: patching: Add nosync helpers for instruction copy and set
2026-12-06 9:23 ` [PATCH bpf-next 3/6] arm64: patching: Add nosync helpers for instruction copy and set Xu Kuohai
@ 2026-12-06 10:12 ` sashiko-bot
0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-12-06 10:12 UTC (permalink / raw)
To: Xu Kuohai, bpf
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] arm64: missing local ISB on task migration before
kick_all_cpus_sync()
- [Low] arm64: __text_poke() duplicates flush_icache_range() and its
KGDB check
--
commit f0adbfc81bbc8b0254c57fd9e19c900c17d58113
Author: Xu Kuohai <xukuohai@huawei.com>
arm64: patching: Add nosync helpers for instruction copy and set
This commit adds aarch64_insn_copy_nosync() and aarch64_insn_set_nosync()
so callers can defer cross-CPU instruction synchronization instead of
sending an IPI on every poke.
> diff --git a/arch/arm64/kernel/patching.c b/arch/arm64/kernel/patching.c
> index 09f019c6547a0..5e28476db046c 100644
> --- a/arch/arm64/kernel/patching.c
> +++ b/arch/arm64/kernel/patching.c
[ ... ]
> @@ -126,7 +127,15 @@ static void *__text_poke(text_poke_f func, void *addr, void *src, size_t len)
> }
> raw_spin_unlock_irqrestore(&patch_lock, flags);
>
> - flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len);
> + caches_clean_inval_pou((uintptr_t)addr, (uintptr_t)addr + len);
> + /*
> + * IPI all online CPUs to force them to refetch the new instructions.
> + * KGDB uses IPIs to round-up the secondary CPUs during the patching
> + * operation, so we don't need extra IPIs here. See the comments in
> + * flush_icache_range() in arch/arm64/include/asm/cacheflush.h.
> + */
> + if (sync && !in_dbg_master())
> + kick_all_cpus_sync();
>
> return addr;
> }
[Severity: Low]
Can this call flush_icache_range() when sync is true and
caches_clean_inval_pou() otherwise, instead of duplicating
flush_icache_range() and its in_dbg_master() check here?
arch/arm64/include/asm/cacheflush.h already implements this exact
sequence in flush_icache_range(). Calling it directly keeps the KGDB
workaround in one place and avoids relying on a transitive include of
linux/kgdb.h in patching.c.
[ ... ]
> +/**
> + * aarch64_insn_copy_nosync - Copy instructions into RX memory, no sync
> + * @dst: address to modify
> + * @src: source of the copy
> + * @len: length to copy
> + *
> + * Same as aarch64_insn_copy(), but does not IPI other CPUs to refetch
> + * the new instructions. The caller must do the synchronization before
> + * the text is executed.
> + */
> +noinstr void *aarch64_insn_copy_nosync(void *dst, void *src, size_t len)
> +{
> + return __aarch64_insn_copy(dst, src, len, false);
> +}
[Severity: Medium]
Can a task that migrates between caches_clean_inval_pou() and
kick_all_cpus_sync() execute stale prefetched instructions on the new
CPU?
caches_clean_inval_pou() only executes isb on the local CPU. If the
caller migrates to another CPU via a voluntary switch from a thread
sharing the same mm, switch_mm() is skipped and __switch_to() issues
dsb(ish) without an unconditional isb().
CPU0 CPU1
aarch64_insn_copy_nosync()
caches_clean_inval_pou()
isb (CPU0 only)
... task migrates to CPU1 ...
__switch_to() (same mm, no isb)
kick_all_cpus_sync()
skips this_cpu (CPU1)
execute new text <-- stale prefetch
kick_all_cpus_sync() calls smp_call_function_many_cond(), which excludes
this_cpu, and sending the IPI does not guarantee a local isb() (for
example, GICv2 gic_ipi_send_mask() uses dmb(ishst) and writel_relaxed()).
Executing newly written text on CPU1, such as a reused BPF prog pack slot
that held AARCH64_BREAK_FAULT, can hit stale prefetched instructions and
cause unexpected BRK faults or corrupted execution.
The same preemptible window exists in __text_poke() when sync is true,
and aarch64_insn_set_nosync() shares this contract. Using on_each_cpu()
to run a local isb(), and documenting that callers of the nosync helpers
must also synchronize the local CPU, would cover every online CPU.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796545068.git.xukuohai@huawei.com?part=3
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH bpf-next 4/6] bpf, arm64: Defer JIT instruction sync
2026-12-06 9:23 ` [PATCH bpf-next 4/6] bpf, arm64: Defer JIT instruction sync Xu Kuohai
@ 2026-12-06 10:14 ` sashiko-bot
0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-12-06 10:14 UTC (permalink / raw)
To: Xu Kuohai, bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] bpf, arm64: bpf_arch_text_synchronize() omits local CPU ISB
after migration
--
commit 8b2bf7fd475c1e8e55dccee21aa58f89891e3c3e
Author: Xu Kuohai <xukuohai@huawei.com>
bpf, arm64: Defer JIT instruction sync
This commit implements bpf_arch_text_copy_nosync() and
bpf_arch_text_synchronize() on arm64 to support deferred JIT instruction
synchronization, and switches the JIT error path to the nosync copy.
> diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
> index 3ac287580d224..76d25947ace65 100644
> --- a/arch/arm64/net/bpf_jit_comp.c
> +++ b/arch/arm64/net/bpf_jit_comp.c
[ ... ]
> @@ -3288,6 +3288,18 @@ void *bpf_arch_text_copy(void *dst, void *src, size_t len)
> return dst;
> }
>
> +void *bpf_arch_text_copy_nosync(void *dst, void *src, size_t len)
> +{
> + if (!aarch64_insn_copy_nosync(dst, src, len))
> + return ERR_PTR(-EINVAL);
> + return dst;
> +}
> +
> +void bpf_arch_text_synchronize(void)
> +{
> + kick_all_cpus_sync();
> +}
[Severity: High]
Can bpf_arch_text_synchronize() return without executing an isb() on the
calling CPU if the task migrated after bpf_arch_text_copy_nosync()?
bpf_arch_text_copy_nosync() runs caches_clean_inval_pou(), which only
executes isb() on the CPU performing the copy. Because synchronization is
deferred across preemptible work, the task can migrate before calling
bpf_arch_text_synchronize():
CPU0 CPU1
bpf_arch_text_copy_nosync()
caches_clean_inval_pou() (isb)
... (task migrates to CPU1) ...
__switch_to() (dsb, no isb)
bpf_arch_text_synchronize()
kick_all_cpus_sync() (skips CPU1)
dummy_ops_call_op() <-- stale prefetch
On arm64, __switch_to() emits dsb(ish) without guaranteeing an isb() (as
documented in emit_pte_barriers()). kick_all_cpus_sync() executes
smp_mb() (dmb(ish)) and smp_call_function(), which clears this_cpu from
the target mask and skips SCF_RUN_LOCAL.
Sending the remote IPI does not guarantee an isb() on CPU1 either, for
example on GICv2 or when no remote IPI needs to be dispatched.
A caller such as bpf_struct_ops_test_run() can then immediately execute
newly patched code on CPU1 with stale prefetched instructions from a
reused bpf_prog_pack slot, hitting AARCH64_BREAK_FAULT (kernel oops) or
stale code from a freed program.
Executing isb() on the local CPU in the same non-migratable region as
kick_all_cpus_sync() (e.g. under preempt_disable(), or via on_each_cpu()
with an isb() callback) would close both migration windows.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796545068.git.xukuohai@huawei.com?part=4
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH bpf-next 6/6] bpf, arm64: Remove instruction sync when invalidating freed memory
2026-12-06 9:23 ` [PATCH bpf-next 6/6] bpf, arm64: Remove instruction sync when invalidating freed memory Xu Kuohai
@ 2026-12-06 10:19 ` bot+bpf-ci
0 siblings, 0 replies; 11+ messages in thread
From: bot+bpf-ci @ 2026-12-06 10:19 UTC (permalink / raw)
To: xukuohai, bpf, linux-kernel
Cc: ast, daniel, andrii, eddyz87, memxor, song, yonghong.song, jolsa,
emil, john.fastabend, puranjay, catalin.marinas, will,
mark.rutland, ast, andrii, daniel, martin.lau, eddyz87,
yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 1258 bytes --]
> bpf, arm64: Remove instruction sync when invalidating freed memory
TL;DR: Nit only: aarch64_insn_set() now has no callers after this change;
consider dropping it or making it skip the IPI instead of keeping both it and
aarch64_insn_set_nosync().
> diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
> index 7b5519f1159c..605deb44ccff 100644
> --- a/arch/arm64/net/bpf_jit_comp.c
> +++ b/arch/arm64/net/bpf_jit_comp.c
> @@ -353,7 +353,7 @@ static void jit_fill_hole(void *area, unsigned int size)
>
> int bpf_arch_text_invalidate(void *dst, size_t len)
> {
> - if (!aarch64_insn_set(dst, AARCH64_BREAK_FAULT, len))
> + if (!aarch64_insn_set_nosync(dst, AARCH64_BREAK_FAULT, len))
> return -EINVAL;
>
> return 0;
This isn't a bug, but now that bpf_arch_text_invalidate() no longer calls
aarch64_insn_set(), nothing else in the tree does either. Should
aarch64_insn_set() be dropped, or changed in place to skip the IPI,
instead of keeping both it and aarch64_insn_set_nosync()?
---
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/47636473460
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-12-06 10:19 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06 9:23 [PATCH bpf-next 0/6] bpf, arm64: Reduce instruction sync IPIs Xu Kuohai
2026-12-06 9:23 ` [PATCH bpf-next 1/6] bpf: Defer instruction sync to the end of load time Xu Kuohai
2026-12-06 9:23 ` [PATCH bpf-next 2/6] bpf: Defer trampoline creation instruction sync Xu Kuohai
2026-12-06 9:23 ` [PATCH bpf-next 3/6] arm64: patching: Add nosync helpers for instruction copy and set Xu Kuohai
2026-12-06 10:12 ` sashiko-bot
2026-12-06 9:23 ` [PATCH bpf-next 4/6] bpf, arm64: Defer JIT instruction sync Xu Kuohai
2026-12-06 10:14 ` sashiko-bot
2026-12-06 9:23 ` [PATCH bpf-next 5/6] bpf, arm64: Defer trampoline creation " Xu Kuohai
2026-12-06 10:12 ` sashiko-bot
2026-12-06 9:23 ` [PATCH bpf-next 6/6] bpf, arm64: Remove instruction sync when invalidating freed memory Xu Kuohai
2026-12-06 10:19 ` bot+bpf-ci
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox