Add support for MAP_BELOW_HINT to mmap by restricting high_limit to addr
when the flag is enabled.

Signed-off-by: Charlie Jenkins <char...@rivosinc.com>
---
 arch/loongarch/mm/mmap.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/arch/loongarch/mm/mmap.c b/arch/loongarch/mm/mmap.c
index 889030985135..66a5badf849b 100644
--- a/arch/loongarch/mm/mmap.c
+++ b/arch/loongarch/mm/mmap.c
@@ -70,6 +70,13 @@ static unsigned long arch_get_unmapped_area_common(struct 
file *filp,
                info.flags = VM_UNMAPPED_AREA_TOPDOWN;
                info.low_limit = PAGE_SIZE;
                info.high_limit = mm->mmap_base;
+               if (flags & MAP_BELOW_HINT)
+                       /*
+                        * Subtract (STACK_TOP - mm->mmap_base) to get random
+                        * offset defined in mmap_base() in mm/util.c
+                        */
+                       info.high_limit = MIN(mm->mmap_base,
+                                             (addr + len) - (STACK_TOP - 
mm->mmap_base))
                addr = vm_unmapped_area(&info);
 
                if (!(addr & ~PAGE_MASK))
@@ -84,7 +91,11 @@ static unsigned long arch_get_unmapped_area_common(struct 
file *filp,
        }
 
        info.low_limit = mm->mmap_base;
+
        info.high_limit = TASK_SIZE;
+       if (flags & MAP_BELOW_HINT)
+               info.high_limit = MIN(info.high_limit, addr + len);
+
        return vm_unmapped_area(&info);
 }
 

-- 
2.45.0


Reply via email to