Re: [PATCH v2 rebased 5/9] powerpc/mm: Call radix__arch_get_unmapped_area() from arch_get_unmapped_area()

2021-11-30 Thread Nicholas Piggin
Excerpts from Christophe Leroy's message of November 26, 2021 3:52 am:
> Instead of setting mm->get_unmapped_area() to either
> arch_get_unmapped_area() or radix__arch_get_unmapped_area(),
> always set it to arch_get_unmapped_area() and call
> radix__arch_get_unmapped_area() from there when radix is enabled.
> 
> To keep radix__arch_get_unmapped_area() static, move it to slice.c
> 
> Do the same with radix__arch_get_unmapped_area_topdown()

Same comment with this one really. It would be better if we can
reuse generic code.

Thanks,
Nick


[PATCH v2 rebased 5/9] powerpc/mm: Call radix__arch_get_unmapped_area() from arch_get_unmapped_area()

2021-11-25 Thread Christophe Leroy
Instead of setting mm->get_unmapped_area() to either
arch_get_unmapped_area() or radix__arch_get_unmapped_area(),
always set it to arch_get_unmapped_area() and call
radix__arch_get_unmapped_area() from there when radix is enabled.

To keep radix__arch_get_unmapped_area() static, move it to slice.c

Do the same with radix__arch_get_unmapped_area_topdown()

Signed-off-by: Christophe Leroy 
---
 arch/powerpc/mm/book3s64/slice.c | 104 ++
 arch/powerpc/mm/mmap.c   | 123 ---
 2 files changed, 104 insertions(+), 123 deletions(-)

diff --git a/arch/powerpc/mm/book3s64/slice.c b/arch/powerpc/mm/book3s64/slice.c
index 4c3e9601fdf6..99742dde811c 100644
--- a/arch/powerpc/mm/book3s64/slice.c
+++ b/arch/powerpc/mm/book3s64/slice.c
@@ -639,12 +639,113 @@ unsigned long slice_get_unmapped_area(unsigned long 
addr, unsigned long len,
 }
 EXPORT_SYMBOL_GPL(slice_get_unmapped_area);
 
+/*
+ * Same function as generic code used only for radix, because we don't need to 
overload
+ * the generic one. But we will have to duplicate, because hash select
+ * HAVE_ARCH_UNMAPPED_AREA
+ */
+static unsigned long
+radix__arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned 
long len,
+ unsigned long pgoff, unsigned long flags)
+{
+   struct mm_struct *mm = current->mm;
+   struct vm_area_struct *vma;
+   int fixed = (flags & MAP_FIXED);
+   unsigned long high_limit;
+   struct vm_unmapped_area_info info;
+
+   high_limit = DEFAULT_MAP_WINDOW;
+   if (addr >= high_limit || (fixed && (addr + len > high_limit)))
+   high_limit = TASK_SIZE;
+
+   if (len > high_limit)
+   return -ENOMEM;
+
+   if (fixed) {
+   if (addr > high_limit - len)
+   return -ENOMEM;
+   return addr;
+   }
+
+   if (addr) {
+   addr = PAGE_ALIGN(addr);
+   vma = find_vma(mm, addr);
+   if (high_limit - len >= addr && addr >= mmap_min_addr &&
+   (!vma || addr + len <= vm_start_gap(vma)))
+   return addr;
+   }
+
+   info.flags = 0;
+   info.length = len;
+   info.low_limit = mm->mmap_base;
+   info.high_limit = high_limit;
+   info.align_mask = 0;
+
+   return vm_unmapped_area();
+}
+
+static unsigned long
+radix__arch_get_unmapped_area_topdown(struct file *filp, const unsigned long 
addr0,
+ const unsigned long len, const unsigned 
long pgoff,
+ const unsigned long flags)
+{
+   struct vm_area_struct *vma;
+   struct mm_struct *mm = current->mm;
+   unsigned long addr = addr0;
+   int fixed = (flags & MAP_FIXED);
+   unsigned long high_limit;
+   struct vm_unmapped_area_info info;
+
+   high_limit = DEFAULT_MAP_WINDOW;
+   if (addr >= high_limit || (fixed && (addr + len > high_limit)))
+   high_limit = TASK_SIZE;
+
+   if (len > high_limit)
+   return -ENOMEM;
+
+   if (fixed) {
+   if (addr > high_limit - len)
+   return -ENOMEM;
+   return addr;
+   }
+
+   if (addr) {
+   addr = PAGE_ALIGN(addr);
+   vma = find_vma(mm, addr);
+   if (high_limit - len >= addr && addr >= mmap_min_addr &&
+   (!vma || addr + len <= vm_start_gap(vma)))
+   return addr;
+   }
+
+   info.flags = VM_UNMAPPED_AREA_TOPDOWN;
+   info.length = len;
+   info.low_limit = max(PAGE_SIZE, mmap_min_addr);
+   info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW);
+   info.align_mask = 0;
+
+   addr = vm_unmapped_area();
+   if (!(addr & ~PAGE_MASK))
+   return addr;
+   VM_BUG_ON(addr != -ENOMEM);
+
+   /*
+* A failed mmap() very likely causes application failure,
+* so fall back to the bottom-up function here. This scenario
+* can happen with large stack limits and large mmap()
+* allocations.
+*/
+   return radix__arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
+}
+
 unsigned long arch_get_unmapped_area(struct file *filp,
 unsigned long addr,
 unsigned long len,
 unsigned long pgoff,
 unsigned long flags)
 {
+   if (radix_enabled())
+   return radix__arch_get_unmapped_area(filp, addr, len, pgoff, 
flags);
+
return slice_get_unmapped_area(addr, len, flags,
   
mm_ctx_user_psize(>mm->context), 0);
 }
@@ -655,6 +756,9 @@ unsigned long arch_get_unmapped_area_topdown(struct file 
*filp,
 const unsigned long pgoff,
 const