* [PATCH] include: riscv_asm: Optimize csr_xyz() macros to reduce stack usage
@ 2025-12-16 5:25 Bo Gan
2025-12-27 9:09 ` Anup Patel
0 siblings, 1 reply; 2+ messages in thread
From: Bo Gan @ 2025-12-16 5:25 UTC (permalink / raw)
To: opensbi
When using debug builds, aka., DEBUG=1, csr_write_num() function can
trigger stack overflow. This is caused by the large amount of macro
expansion of csr_write(...), which, under debug builds, will generate
massive amount of stack variables (tested with GCC 13.2.0). The issue
is masked previously as we didn't have too many csr_write()'s before
commit 55296fd27c0c, but now, it does overflow the default 4KB stack.
The csr_read(relaxed) macros already use the "register" modifier to
optimize stack usage (perhaps unknowingly?), so this patch just
follows suit.
Fixes: 55296fd27c0c ("lib: Allow custom CSRs in csr_read_num() and csr_write_num()")
Signed-off-by: Bo Gan <ganboing@gmail.com>
---
include/sbi/riscv_asm.h | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/include/sbi/riscv_asm.h b/include/sbi/riscv_asm.h
index ef48dc89..0cf3fc37 100644
--- a/include/sbi/riscv_asm.h
+++ b/include/sbi/riscv_asm.h
@@ -83,7 +83,7 @@
#define csr_swap(csr, val) \
({ \
- unsigned long __v = (unsigned long)(val); \
+ register unsigned long __v = (unsigned long)(val); \
__asm__ __volatile__("csrrw %0, " __ASM_STR(csr) ", %1" \
: "=r"(__v) \
: "rK"(__v) \
@@ -111,7 +111,7 @@
#define csr_write(csr, val) \
({ \
- unsigned long __v = (unsigned long)(val); \
+ register unsigned long __v = (unsigned long)(val); \
__asm__ __volatile__("csrw " __ASM_STR(csr) ", %0" \
: \
: "rK"(__v) \
@@ -120,7 +120,7 @@
#define csr_read_set(csr, val) \
({ \
- unsigned long __v = (unsigned long)(val); \
+ register unsigned long __v = (unsigned long)(val); \
__asm__ __volatile__("csrrs %0, " __ASM_STR(csr) ", %1" \
: "=r"(__v) \
: "rK"(__v) \
@@ -130,7 +130,7 @@
#define csr_set(csr, val) \
({ \
- unsigned long __v = (unsigned long)(val); \
+ register unsigned long __v = (unsigned long)(val); \
__asm__ __volatile__("csrs " __ASM_STR(csr) ", %0" \
: \
: "rK"(__v) \
@@ -139,7 +139,7 @@
#define csr_read_clear(csr, val) \
({ \
- unsigned long __v = (unsigned long)(val); \
+ register unsigned long __v = (unsigned long)(val); \
__asm__ __volatile__("csrrc %0, " __ASM_STR(csr) ", %1" \
: "=r"(__v) \
: "rK"(__v) \
@@ -149,7 +149,7 @@
#define csr_clear(csr, val) \
({ \
- unsigned long __v = (unsigned long)(val); \
+ register unsigned long __v = (unsigned long)(val); \
__asm__ __volatile__("csrc " __ASM_STR(csr) ", %0" \
: \
: "rK"(__v) \
--
2.34.1
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] include: riscv_asm: Optimize csr_xyz() macros to reduce stack usage
2025-12-16 5:25 [PATCH] include: riscv_asm: Optimize csr_xyz() macros to reduce stack usage Bo Gan
@ 2025-12-27 9:09 ` Anup Patel
0 siblings, 0 replies; 2+ messages in thread
From: Anup Patel @ 2025-12-27 9:09 UTC (permalink / raw)
To: Bo Gan; +Cc: opensbi
On Tue, Dec 16, 2025 at 10:57 AM Bo Gan <ganboing@gmail.com> wrote:
>
> When using debug builds, aka., DEBUG=1, csr_write_num() function can
> trigger stack overflow. This is caused by the large amount of macro
> expansion of csr_write(...), which, under debug builds, will generate
> massive amount of stack variables (tested with GCC 13.2.0). The issue
> is masked previously as we didn't have too many csr_write()'s before
> commit 55296fd27c0c, but now, it does overflow the default 4KB stack.
>
> The csr_read(relaxed) macros already use the "register" modifier to
> optimize stack usage (perhaps unknowingly?), so this patch just
> follows suit.
>
> Fixes: 55296fd27c0c ("lib: Allow custom CSRs in csr_read_num() and csr_write_num()")
> Signed-off-by: Bo Gan <ganboing@gmail.com>
LGTM.
Reviewed-by: Anup Patel <anup@brainfault.org>
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
> ---
> include/sbi/riscv_asm.h | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/include/sbi/riscv_asm.h b/include/sbi/riscv_asm.h
> index ef48dc89..0cf3fc37 100644
> --- a/include/sbi/riscv_asm.h
> +++ b/include/sbi/riscv_asm.h
> @@ -83,7 +83,7 @@
>
> #define csr_swap(csr, val) \
> ({ \
> - unsigned long __v = (unsigned long)(val); \
> + register unsigned long __v = (unsigned long)(val); \
> __asm__ __volatile__("csrrw %0, " __ASM_STR(csr) ", %1" \
> : "=r"(__v) \
> : "rK"(__v) \
> @@ -111,7 +111,7 @@
>
> #define csr_write(csr, val) \
> ({ \
> - unsigned long __v = (unsigned long)(val); \
> + register unsigned long __v = (unsigned long)(val); \
> __asm__ __volatile__("csrw " __ASM_STR(csr) ", %0" \
> : \
> : "rK"(__v) \
> @@ -120,7 +120,7 @@
>
> #define csr_read_set(csr, val) \
> ({ \
> - unsigned long __v = (unsigned long)(val); \
> + register unsigned long __v = (unsigned long)(val); \
> __asm__ __volatile__("csrrs %0, " __ASM_STR(csr) ", %1" \
> : "=r"(__v) \
> : "rK"(__v) \
> @@ -130,7 +130,7 @@
>
> #define csr_set(csr, val) \
> ({ \
> - unsigned long __v = (unsigned long)(val); \
> + register unsigned long __v = (unsigned long)(val); \
> __asm__ __volatile__("csrs " __ASM_STR(csr) ", %0" \
> : \
> : "rK"(__v) \
> @@ -139,7 +139,7 @@
>
> #define csr_read_clear(csr, val) \
> ({ \
> - unsigned long __v = (unsigned long)(val); \
> + register unsigned long __v = (unsigned long)(val); \
> __asm__ __volatile__("csrrc %0, " __ASM_STR(csr) ", %1" \
> : "=r"(__v) \
> : "rK"(__v) \
> @@ -149,7 +149,7 @@
>
> #define csr_clear(csr, val) \
> ({ \
> - unsigned long __v = (unsigned long)(val); \
> + register unsigned long __v = (unsigned long)(val); \
> __asm__ __volatile__("csrc " __ASM_STR(csr) ", %0" \
> : \
> : "rK"(__v) \
> --
> 2.34.1
>
>
> --
> opensbi mailing list
> opensbi@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-12-27 9:09 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-16 5:25 [PATCH] include: riscv_asm: Optimize csr_xyz() macros to reduce stack usage Bo Gan
2025-12-27 9:09 ` Anup Patel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox