[PATCH v4 2/2] riscv: patch: skip fixmap mapping when kernel text is already writable

From: Xiaofeng Yuan

Date: Sun Jul 19 2026 - 23:24:38 EST


Currently patch_map() always creates a temporary writable mapping via
fixmap for kernel text addresses, even when CONFIG_STRICT_MODULE_RWX
is disabled and the kernel text is already mapped with _PAGE_WRITE.

This is unnecessary overhead at best, and on minimal configurations
it can cause page faults.

Skip the fixmap path for kernel text when CONFIG_STRICT_MODULE_RWX
is not enabled, since the text pages are already writable in this case.

Signed-off-by: Xiaofeng Yuan <xiaofengmian@xxxxxxx>
---
v2: add commit description
v3: early return when !CONFIG_STRICT_MODULE_RWX (per Nam Cao's suggestion)
diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c
index 16b243376f..caef41d5ef 100644
--- a/arch/riscv/kernel/patch.c
+++ b/arch/riscv/kernel/patch.c
@@ -44,15 +44,16 @@ static __always_inline void *patch_map(void *addr, const unsigned int fixmap)
uintptr_t uintaddr = (uintptr_t) addr;
phys_addr_t phys;

+ if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
+ return addr;
+
if (core_kernel_text(uintaddr) || is_kernel_exittext(uintaddr)) {
phys = __pa_symbol(addr);
- } else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) {
+ } else {
struct page *page = vmalloc_to_page(addr);

BUG_ON(!page);
phys = page_to_phys(page) + offset_in_page(addr);
- } else {
- return addr;
}

return (void *)set_fixmap_offset(fixmap, phys);
--
2.43.0