mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Changbin Du <changbin.du@huawei.com>
To: Paul Walmsley <paul.walmsley@sifive.com>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexandre Ghiti <alex@ghiti.fr>,
	<linux-riscv@lists.infradead.org>, <linux-kernel@vger.kernel.org>,
	Changbin Du <changbin.du@huawei.com>
Subject: [PATCH] riscv: Prevent early kernel panic in instrumented apply_early_boot_alternatives
Date: Tue, 24 Jun 2025 19:30:42 +0800	[thread overview]
Message-ID: <20250624113042.2123140-1-changbin.du@huawei.com> (raw)

Under FTRACE=y, DYNAMIC_FTRACE=n, and RISCV_ALTERNATIVE_EARLY=n, the kernel
panics upon returning from _mcount() in the early boot path. This occurs
during _start_kernel() -> setup_vm() -> apply_early_boot_alternatives().

The CC_FLAGS_FTRACE is only removed from alternative.c when
CONFIG_RISCV_ALTERNATIVE_EARLY=y. Therefore, no function calls should be
made to alternative.c during early boot in this configuration.

Fixes: a35707c3d850 ("riscv: add memory-type errata for T-Head")
Signed-off-by: Changbin Du <changbin.du@huawei.com>
---
 arch/riscv/include/asm/alternative.h | 6 ++++++
 arch/riscv/kernel/alternative.c      | 4 ++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/arch/riscv/include/asm/alternative.h b/arch/riscv/include/asm/alternative.h
index 3c2b59b25017..c670b0cc55f4 100644
--- a/arch/riscv/include/asm/alternative.h
+++ b/arch/riscv/include/asm/alternative.h
@@ -31,7 +31,13 @@
 #define ALT_ALT_PTR(a)			__ALT_PTR(a, alt_offset)
 
 void __init apply_boot_alternatives(void);
+
+# ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
 void __init apply_early_boot_alternatives(void);
+# else
+static inline void apply_early_boot_alternatives(void) { }
+# endif
+
 void apply_module_alternatives(void *start, size_t length);
 
 void riscv_alternative_fix_offsets(void *alt_ptr, unsigned int len,
diff --git a/arch/riscv/kernel/alternative.c b/arch/riscv/kernel/alternative.c
index 7eb3cb1215c6..5406c3301627 100644
--- a/arch/riscv/kernel/alternative.c
+++ b/arch/riscv/kernel/alternative.c
@@ -205,6 +205,7 @@ void __init apply_boot_alternatives(void)
 	apply_vdso_alternatives();
 }
 
+#ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
 /*
  * apply_early_boot_alternatives() is called from setup_vm() with MMU-off.
  *
@@ -219,12 +220,11 @@ void __init apply_boot_alternatives(void)
  */
 void __init apply_early_boot_alternatives(void)
 {
-#ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
 	_apply_alternatives((struct alt_entry *)__alt_start,
 			    (struct alt_entry *)__alt_end,
 			    RISCV_ALTERNATIVES_EARLY_BOOT);
-#endif
 }
+#endif
 
 #ifdef CONFIG_MODULES
 void apply_module_alternatives(void *start, size_t length)
-- 
2.43.0


             reply	other threads:[~2025-06-24 11:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-24 11:30 Changbin Du [this message]
2025-06-24 12:54 ` Alexandre Ghiti
2025-06-25  3:10   ` duchangbin
2025-10-09  2:01     ` Paul Walmsley
2025-10-09  6:31       ` Vivian Wang
2025-10-09 23:33         ` Paul Walmsley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250624113042.2123140-1-changbin.du@huawei.com \
    --to=changbin.du@huawei.com \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®