This patch reverted the meaning of the addr parameter in the mmap syscall
change from the previous commit b5b4287accd7 ("riscv: mm: Use hint address
in mmap if available") from patch[1] which treats hint addr + size as the
upper bound of the mmap return address. Result in ENOMEM error caused when
hint address + size is not big enough.

Thus, this patch makes the behavior of mmap syscall to align with x86,
arm64, powerpc by only limiting the address space to DEFAULT_MAP_WINDOW
which is defined as not larger than 47-bit. If a user program wants to
use sv57 address space, it can use mmap with a hint address larger than
BIT(47) as it is already documented in x86 and arm64.

[1] 
https://lore.kernel.org/linux-riscv/20240130-use_mmap_hint_address-v3-0-8a655cfa8...@rivosinc.com/

Signed-off-by: Yangyu Chen <c...@cyyself.name>
---
 arch/riscv/include/asm/processor.h | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)

diff --git a/arch/riscv/include/asm/processor.h 
b/arch/riscv/include/asm/processor.h
index 8702b8721a27..faf3e230ab24 100644
--- a/arch/riscv/include/asm/processor.h
+++ b/arch/riscv/include/asm/processor.h
@@ -13,22 +13,17 @@
 #include <vdso/processor.h>
 
 #include <asm/ptrace.h>
+#include <asm/mman.h>
 
-/*
- * 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_compat_task() ||                 \
-           ((_addr + len) > BIT(VA_BITS - 1)))                 \
+       if (((_addr + len) > DEFAULT_MAP_WINDOW) ||             \
+           ((flags) & MAP_FIXED))                              \
                mmap_end = STACK_TOP_MAX;                       \
        else                                                    \
-               mmap_end = (_addr + len);                       \
+               mmap_end = DEFAULT_MAP_WINDOW;                  \
        mmap_end;                                               \
 })
 
@@ -38,11 +33,10 @@
        typeof(addr) _addr = (addr);                            \
        typeof(base) _base = (base);                            \
        unsigned long rnd_gap = DEFAULT_MAP_WINDOW - (_base);   \
-       if ((_addr) == 0 || is_compat_task() ||                 \
-           ((_addr + len) > BIT(VA_BITS - 1)))                 \
-               mmap_base = (_base);                            \
+       if ((_addr + len) > DEFAULT_MAP_WINDOW)                 \
+               mmap_base = (STACK_TOP_MAX - rnd_gap);          \
        else                                                    \
-               mmap_base = (_addr + len) - rnd_gap;            \
+               mmap_base = (_base);                            \
        mmap_base;                                              \
 })
 
-- 
2.45.2


Reply via email to