Skip to contentSkip to conversation

include: riscv_asm: Optimize csr_xyz() macros to reduce stack usage

PATCH Applied Bo Gan 2 participants 2 messages Dec 16, 2025 – Dec 27, 2025 RISC-V

lore
Share & tools
lore

Bo Gan

author
#0 lore raw

PATCH 1/1 include: riscv_asm: Optimize csr_xyz() macros to reduce stack usage

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()")
---
 include/sbi/riscv_asm.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
include/sbi/riscv_asm.h +6 -6
index ef48dc89..0cf3fc37 100644
@@ -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)                   \
Signature · 7 lines
2.34.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 1/1 Applied
#1 lore raw
Quoted 8 lines · Bo Gan

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.

Quoted 6 lines

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()")

LGTM.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 70 lines
---
 include/sbi/riscv_asm.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
include/sbi/riscv_asm.h +6 -7
index ef48dc89..0cf3fc37 100644
@@ -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
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi