Commit 420370f3 authored by Alexandre Ghiti's avatar Alexandre Ghiti Committed by Palmer Dabbelt

riscv: Check if the code to patch lies in the exit section

Otherwise we fall through to vmalloc_to_page() which panics since the
address does not lie in the vmalloc region.

Fixes: 043cb41a ("riscv: introduce interfaces to patch kernel code")
Signed-off-by: default avatarAlexandre Ghiti <alexghiti@rivosinc.com>
Reviewed-by: default avatarCharlie Jenkins <charlie@rivosinc.com>
Link: https://lore.kernel.org/r/20231214091926.203439-1-alexghiti@rivosinc.comSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
parent ed5b7cfd
...@@ -13,6 +13,7 @@ extern char _start_kernel[]; ...@@ -13,6 +13,7 @@ extern char _start_kernel[];
extern char __init_data_begin[], __init_data_end[]; extern char __init_data_begin[], __init_data_end[];
extern char __init_text_begin[], __init_text_end[]; extern char __init_text_begin[], __init_text_end[];
extern char __alt_start[], __alt_end[]; extern char __alt_start[], __alt_end[];
extern char __exittext_begin[], __exittext_end[];
static inline bool is_va_kernel_text(uintptr_t va) static inline bool is_va_kernel_text(uintptr_t va)
{ {
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <asm/fixmap.h> #include <asm/fixmap.h>
#include <asm/ftrace.h> #include <asm/ftrace.h>
#include <asm/patch.h> #include <asm/patch.h>
#include <asm/sections.h>
struct patch_insn { struct patch_insn {
void *addr; void *addr;
...@@ -25,6 +26,14 @@ struct patch_insn { ...@@ -25,6 +26,14 @@ struct patch_insn {
int riscv_patch_in_stop_machine = false; int riscv_patch_in_stop_machine = false;
#ifdef CONFIG_MMU #ifdef CONFIG_MMU
static inline bool is_kernel_exittext(uintptr_t addr)
{
return system_state < SYSTEM_RUNNING &&
addr >= (uintptr_t)__exittext_begin &&
addr < (uintptr_t)__exittext_end;
}
/* /*
* The fix_to_virt(, idx) needs a const value (not a dynamic variable of * The fix_to_virt(, idx) needs a const value (not a dynamic variable of
* reg-a0) or BUILD_BUG_ON failed with "idx >= __end_of_fixed_addresses". * reg-a0) or BUILD_BUG_ON failed with "idx >= __end_of_fixed_addresses".
...@@ -35,7 +44,7 @@ static __always_inline void *patch_map(void *addr, const unsigned int fixmap) ...@@ -35,7 +44,7 @@ static __always_inline void *patch_map(void *addr, const unsigned int fixmap)
uintptr_t uintaddr = (uintptr_t) addr; uintptr_t uintaddr = (uintptr_t) addr;
struct page *page; struct page *page;
if (core_kernel_text(uintaddr)) if (core_kernel_text(uintaddr) || is_kernel_exittext(uintaddr))
page = phys_to_page(__pa_symbol(addr)); page = phys_to_page(__pa_symbol(addr));
else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
page = vmalloc_to_page(addr); page = vmalloc_to_page(addr);
......
...@@ -29,10 +29,12 @@ SECTIONS ...@@ -29,10 +29,12 @@ SECTIONS
HEAD_TEXT_SECTION HEAD_TEXT_SECTION
INIT_TEXT_SECTION(PAGE_SIZE) INIT_TEXT_SECTION(PAGE_SIZE)
/* we have to discard exit text and such at runtime, not link time */ /* we have to discard exit text and such at runtime, not link time */
__exittext_begin = .;
.exit.text : .exit.text :
{ {
EXIT_TEXT EXIT_TEXT
} }
__exittext_end = .;
.text : { .text : {
_text = .; _text = .;
......
...@@ -69,10 +69,12 @@ SECTIONS ...@@ -69,10 +69,12 @@ SECTIONS
__soc_builtin_dtb_table_end = .; __soc_builtin_dtb_table_end = .;
} }
/* we have to discard exit text and such at runtime, not link time */ /* we have to discard exit text and such at runtime, not link time */
__exittext_begin = .;
.exit.text : .exit.text :
{ {
EXIT_TEXT EXIT_TEXT
} }
__exittext_end = .;
__init_text_end = .; __init_text_end = .;
. = ALIGN(SECTION_ALIGN); . = ALIGN(SECTION_ALIGN);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment