Skip to contentSkip to conversation

riscv: Improve arch_get_mmap_end() macro

RFC PATCH In review Leonardo Bras 2 participants 3 messages Dec 22, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 riscv: Improve arch_get_mmap_end() macro

This macro caused me some confusion, which took some reviewer's time to make it clear, so I propose adding a short comment in code to avoid confusion in the future.

Also, added some improvements to the macro, such as removing the assumption of VA_USER_SV57 being the largest address space.

---
 arch/riscv/include/asm/processor.h | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/riscv/include/asm/processor.h +9 -3
index f19f861cda549..2278e2a8362af 100644
@@ -18,15 +18,21 @@
 #define DEFAULT_MAP_WINDOW	(UL(1) << (MMAP_VA_BITS - 1))
 #define STACK_TOP_MAX		TASK_SIZE_64
 
+/*
+ * addr is a hint to the maximum userspace address that mmap should provide, so
+ * this macro needs to return the largest address space available so that
+ * mmap_end < addr, being mmap_end the top of that address space.
+ * See Documentation/arch/riscv/vm-layout.rst for more details.
+ */
 #define arch_get_mmap_end(addr, len, flags)			\
 ({								\
 	unsigned long mmap_end;					\
 	typeof(addr) _addr = (addr);				\
 	if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
 		mmap_end = STACK_TOP_MAX;			\
-	else if ((_addr) >= VA_USER_SV57)			\
-		mmap_end = STACK_TOP_MAX;			\
-	else if ((((_addr) >= VA_USER_SV48)) && (VA_BITS >= VA_BITS_SV48)) \
+	else if (((_addr) >= VA_USER_SV57) && (VA_BITS >= VA_BITS_SV57)) \
+		mmap_end = VA_USER_SV57;			\
+	else if (((_addr) >= VA_USER_SV48) && (VA_BITS >= VA_BITS_SV48)) \
 		mmap_end = VA_USER_SV48;			\
 	else							\
 		mmap_end = VA_USER_SV39;			\
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 28 lines · Leonardo Bras

This macro caused me some confusion, which took some reviewer's time to make it clear, so I propose adding a short comment in code to avoid confusion in the future.

Also, added some improvements to the macro, such as removing the assumption of VA_USER_SV57 being the largest address space.

---
 arch/riscv/include/asm/processor.h | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/riscv/include/asm/processor.h +6 -0
index f19f861cda549..2278e2a8362af 100644
@@ -18,15 +18,21 @@
 #define DEFAULT_MAP_WINDOW     (UL(1) << (MMAP_VA_BITS - 1))
 #define STACK_TOP_MAX          TASK_SIZE_64
 
+/*
+ * addr is a hint to the maximum userspace address that mmap should provide, so
+ * this macro needs to return the largest address space available so that
+ * mmap_end < addr, being mmap_end the top of that address space.
+ * See Documentation/arch/riscv/vm-layout.rst for more details.
+ */
 #define arch_get_mmap_end(addr, len, flags)                    \

arch/riscv/include/asm/processor.h line 34

Quoted 10 lines
arch/riscv/include/asm/processor.h +2 -3
 ({                                                             \
        unsigned long mmap_end;                                 \
        typeof(addr) _addr = (addr);                            \
        if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
                mmap_end = STACK_TOP_MAX;                       \
-       else if ((_addr) >= VA_USER_SV57)                       \
-               mmap_end = STACK_TOP_MAX;                       \
-       else if ((((_addr) >= VA_USER_SV48)) && (VA_BITS >= VA_BITS_SV48)) \
+       else if (((_addr) >= VA_USER_SV57) && (VA_BITS >= VA_BITS_SV57)) \
+               mmap_end = VA_USER_SV57;                        \

It's clearer.

LGTM.

Quoted 7 lines
arch/riscv/include/asm/processor.h +1 -1
+       else if (((_addr) >= VA_USER_SV48) && (VA_BITS >= VA_BITS_SV48)) \
                mmap_end = VA_USER_SV48;                        \
        else                                                    \
                mmap_end = VA_USER_SV39;                        \
--

2.43.0

Signature · 2 lines
Best Regards
 Guo Ren
#2 lore raw
Quoted 41 lines · Guo Ren
Quoted 28 lines · Leonardo Bras

This macro caused me some confusion, which took some reviewer's time to make it clear, so I propose adding a short comment in code to avoid confusion in the future.

Also, added some improvements to the macro, such as removing the assumption of VA_USER_SV57 being the largest address space.

---
 arch/riscv/include/asm/processor.h | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/riscv/include/asm/processor.h +6 -0
index f19f861cda549..2278e2a8362af 100644
@@ -18,15 +18,21 @@
 #define DEFAULT_MAP_WINDOW     (UL(1) << (MMAP_VA_BITS - 1))
 #define STACK_TOP_MAX          TASK_SIZE_64
 
+/*
+ * addr is a hint to the maximum userspace address that mmap should provide, so
+ * this macro needs to return the largest address space available so that
+ * mmap_end < addr, being mmap_end the top of that address space.
+ * See Documentation/arch/riscv/vm-layout.rst for more details.
+ */
 #define arch_get_mmap_end(addr, len, flags)                    \

arch/riscv/include/asm/processor.h line 34

Quoted 10 lines
arch/riscv/include/asm/processor.h +2 -3
 ({                                                             \
        unsigned long mmap_end;                                 \
        typeof(addr) _addr = (addr);                            \
        if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
                mmap_end = STACK_TOP_MAX;                       \
-       else if ((_addr) >= VA_USER_SV57)                       \
-               mmap_end = STACK_TOP_MAX;                       \
-       else if ((((_addr) >= VA_USER_SV48)) && (VA_BITS >= VA_BITS_SV48)) \
+       else if (((_addr) >= VA_USER_SV57) && (VA_BITS >= VA_BITS_SV57)) \
+               mmap_end = VA_USER_SV57;                        \

It's clearer.

Quoted 2 lines

LGTM.

Thanks!
Leo

Quoted 14 lines
Quoted 8 lines
arch/riscv/include/asm/processor.h +1 -1
+       else if (((_addr) >= VA_USER_SV48) && (VA_BITS >= VA_BITS_SV48)) \
                mmap_end = VA_USER_SV48;                        \
        else                                                    \
                mmap_end = VA_USER_SV39;                        \
--

2.43.0

Signature · 2 lines
Best Regards
 Guo Ren