[PATCH bpf-next 2/6] bpf: Defer trampoline creation instruction sync
From: Xu Kuohai
Date: Sun Dec 06 2026 - 04:51:38 EST
From: Xu Kuohai <xukuohai@xxxxxxxxxx>
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@xxxxxxxxxx>
---
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