[RFC PATCH 2/5] mm: mremap: Use round_hint_to_min() for new_addr hints

From: Brian Geffon
Date: Tue Dec 10 2024 - 16:31:15 EST


MREMAP_DONTUNMAP allows for a get_unmapped_area() hint
when used without MREMAP_FIXED. This was behavior was introduced
inadvertently and should be rounded to the mmap_min_address like
in mmap(2).

Signed-off-by: Brian Geffon <bgeffon@xxxxxxxxxx>
---
include/linux/mm_inline.h | 14 ++++++++++++++
mm/mmap.c | 13 -------------
mm/mremap.c | 3 +++
3 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
index 1b6a917fffa4..863143ec5bb0 100644
--- a/include/linux/mm_inline.h
+++ b/include/linux/mm_inline.h
@@ -9,6 +9,7 @@
#include <linux/string.h>
#include <linux/userfaultfd_k.h>
#include <linux/swapops.h>
+#include <linux/security.h>

/**
* folio_is_file_lru - Should the folio be on a file LRU or anon LRU?
@@ -613,4 +614,17 @@ static inline bool vma_has_recency(struct vm_area_struct *vma)
return true;
}

+/*
+ * If a hint addr is less than mmap_min_addr change hint to be as
+ * low as possible but still greater than mmap_min_addr
+ */
+static inline unsigned long round_hint_to_min(unsigned long hint)
+{
+ hint &= PAGE_MASK;
+ if (((void *)hint != NULL) &&
+ (hint < mmap_min_addr))
+ return PAGE_ALIGN(mmap_min_addr);
+ return hint;
+}
+
#endif
diff --git a/mm/mmap.c b/mm/mmap.c
index d32b7e701058..04952ac21d58 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -215,19 +215,6 @@ SYSCALL_DEFINE1(brk, unsigned long, brk)
return origbrk;
}

-/*
- * If a hint addr is less than mmap_min_addr change hint to be as
- * low as possible but still greater than mmap_min_addr
- */
-static inline unsigned long round_hint_to_min(unsigned long hint)
-{
- hint &= PAGE_MASK;
- if (((void *)hint != NULL) &&
- (hint < mmap_min_addr))
- return PAGE_ALIGN(mmap_min_addr);
- return hint;
-}
-
bool mlock_future_ok(struct mm_struct *mm, unsigned long flags,
unsigned long bytes)
{
diff --git a/mm/mremap.c b/mm/mremap.c
index 62aec72bbe42..fdc1b0f1b38e 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -1109,6 +1109,9 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
goto out;
}

+ if (!(flags & MREMAP_FIXED))
+ new_addr = round_hint_to_min(new_addr);
+
if (flags & (MREMAP_FIXED | MREMAP_DONTUNMAP)) {
ret = mremap_to(addr, old_len, new_addr, new_len,
&locked, flags, &uf, &uf_unmap_early,
--
2.47.0.338.g60cca15819-goog