[PATCH] riscv: KGDB: Do not inline arch_kgdb_breakpoint()

WangYuli posted 1 patch 9 months ago
There is a newer version of this series
arch/riscv/include/asm/kgdb.h | 13 +------------
arch/riscv/kernel/kgdb.c      |  8 ++++++++
2 files changed, 9 insertions(+), 12 deletions(-)
[PATCH] riscv: KGDB: Do not inline arch_kgdb_breakpoint()
Posted by WangYuli 9 months ago
The arch_kgdb_breakpoint() function defines the kgdb_compiled_break
symbol using inline assembly.

There's a potential issue where the compiler might inline
arch_kgdb_breakpoint(), which would then define the kgdb_breakinst
symbol multiple times, leading to fail to link vmlinux.o.

This isn't merely a potential compilation problem. The intent here
is to determine the global symbol address of kgdb_compiled_break,
and if this function is inlined multiple times, it would logically
be a grave error.

Fixes: fe89bd2be866 ("riscv: Add KGDB support")
Co-developed-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: WangYuli <wangyuli@uniontech.com>
---
 arch/riscv/include/asm/kgdb.h | 13 +------------
 arch/riscv/kernel/kgdb.c      |  8 ++++++++
 2 files changed, 9 insertions(+), 12 deletions(-)

diff --git a/arch/riscv/include/asm/kgdb.h b/arch/riscv/include/asm/kgdb.h
index 46677daf708b..566ab1bc66fa 100644
--- a/arch/riscv/include/asm/kgdb.h
+++ b/arch/riscv/include/asm/kgdb.h
@@ -17,20 +17,9 @@
 #define BREAK_INSTR_SIZE	4
 #endif
 
-#ifndef	__ASSEMBLY__
-
+extern void arch_kgdb_breakpoint(void);
 extern unsigned long kgdb_compiled_break;
 
-static inline void arch_kgdb_breakpoint(void)
-{
-	asm(".global kgdb_compiled_break\n"
-	    ".option norvc\n"
-	    "kgdb_compiled_break: ebreak\n"
-	    ".option rvc\n");
-}
-
-#endif /* !__ASSEMBLY__ */
-
 #define DBG_REG_ZERO "zero"
 #define DBG_REG_RA "ra"
 #define DBG_REG_SP "sp"
diff --git a/arch/riscv/kernel/kgdb.c b/arch/riscv/kernel/kgdb.c
index 2e0266ae6bd7..5d1ce8dacaf5 100644
--- a/arch/riscv/kernel/kgdb.c
+++ b/arch/riscv/kernel/kgdb.c
@@ -254,6 +254,14 @@ void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
 	regs->epc = pc;
 }
 
+noinline void arch_kgdb_breakpoint(void)
+{
+	asm(".global kgdb_compiled_break\n"
+	    ".option norvc\n"
+	    "kgdb_compiled_break: ebreak\n"
+	    ".option rvc\n");
+}
+
 void kgdb_arch_handle_qxfer_pkt(char *remcom_in_buffer,
 				char *remcom_out_buffer)
 {
-- 
2.49.0
Re: [PATCH] riscv: KGDB: Do not inline arch_kgdb_breakpoint()
Posted by Samuel Holland 8 months, 4 weeks ago
On 2025-03-22 1:28 AM, WangYuli wrote:
> The arch_kgdb_breakpoint() function defines the kgdb_compiled_break
> symbol using inline assembly.
> 
> There's a potential issue where the compiler might inline
> arch_kgdb_breakpoint(), which would then define the kgdb_breakinst
> symbol multiple times, leading to fail to link vmlinux.o.
> 
> This isn't merely a potential compilation problem. The intent here
> is to determine the global symbol address of kgdb_compiled_break,
> and if this function is inlined multiple times, it would logically
> be a grave error.
> 
> Fixes: fe89bd2be866 ("riscv: Add KGDB support")
> Co-developed-by: Huacai Chen <chenhuacai@loongson.cn>
> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
> Signed-off-by: WangYuli <wangyuli@uniontech.com>
> ---
>  arch/riscv/include/asm/kgdb.h | 13 +------------
>  arch/riscv/kernel/kgdb.c      |  8 ++++++++
>  2 files changed, 9 insertions(+), 12 deletions(-)
> 
> diff --git a/arch/riscv/include/asm/kgdb.h b/arch/riscv/include/asm/kgdb.h
> index 46677daf708b..566ab1bc66fa 100644
> --- a/arch/riscv/include/asm/kgdb.h
> +++ b/arch/riscv/include/asm/kgdb.h
> @@ -17,20 +17,9 @@
>  #define BREAK_INSTR_SIZE	4
>  #endif
>  
> -#ifndef	__ASSEMBLY__
> -
> +extern void arch_kgdb_breakpoint(void);
>  extern unsigned long kgdb_compiled_break;
>  
> -static inline void arch_kgdb_breakpoint(void)
> -{
> -	asm(".global kgdb_compiled_break\n"
> -	    ".option norvc\n"
> -	    "kgdb_compiled_break: ebreak\n"
> -	    ".option rvc\n");
> -}
> -
> -#endif /* !__ASSEMBLY__ */

Why remove the __ASSEMBLY__ check? This allows the header to be included from
assembly source files by excluding the C constructs. It is still needed after
moving arch_kgdb_breakpoint()

> -
>  #define DBG_REG_ZERO "zero"
>  #define DBG_REG_RA "ra"
>  #define DBG_REG_SP "sp"
> diff --git a/arch/riscv/kernel/kgdb.c b/arch/riscv/kernel/kgdb.c
> index 2e0266ae6bd7..5d1ce8dacaf5 100644
> --- a/arch/riscv/kernel/kgdb.c
> +++ b/arch/riscv/kernel/kgdb.c
> @@ -254,6 +254,14 @@ void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
>  	regs->epc = pc;
>  }
>  
> +noinline void arch_kgdb_breakpoint(void)
> +{
> +	asm(".global kgdb_compiled_break\n"
> +	    ".option norvc\n"
> +	    "kgdb_compiled_break: ebreak\n"
> +	    ".option rvc\n");

This is a separate issue, but using ".option rvc" here is a bug. It will
unconditionally enable the C extension for the rest of the file, even if the
kernel is being built with CONFIG_RISCV_ISA_C=n. This function needs to use
.option push/.option pop.

Regards,
Samuel

> +}
> +
>  void kgdb_arch_handle_qxfer_pkt(char *remcom_in_buffer,
>  				char *remcom_out_buffer)
>  {