[PATCH bpf-next 3/6] arm64: patching: Add nosync helpers for instruction copy and set

From: Xu Kuohai

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


From: Xu Kuohai <xukuohai@xxxxxxxxxx>

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@xxxxxxxxxx>
---
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