[PATCH v2 14/20] mm: create __do_mmap() to take an mm_struct * arg

From: Anthony Yznaga
Date: Thu Apr 03 2025 - 22:22:35 EST


In preparation for mapping objects into an mshare region, create
__do_mmap() to allow mapping into a specified mm. There are no
functional changes otherwise.

Signed-off-by: Anthony Yznaga <anthony.yznaga@xxxxxxxxxx>
---
include/linux/mm.h | 16 ++++++++++++++++
mm/mmap.c | 10 +++++-----
mm/vma.c | 12 ++++++------
mm/vma.h | 2 +-
4 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index f06be2f20c20..9e64deae3d64 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3480,10 +3480,26 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
return __get_unmapped_area(file, addr, len, pgoff, flags, 0);
}

+#ifdef CONFIG_MMU
+unsigned long __do_mmap(struct file *file, unsigned long addr,
+ unsigned long len, unsigned long prot, unsigned long flags,
+ vm_flags_t vm_flags, unsigned long pgoff, unsigned long *populate,
+ struct list_head *uf, struct mm_struct *mm);
+static inline unsigned long do_mmap(struct file *file, unsigned long addr,
+ unsigned long len, unsigned long prot, unsigned long flags,
+ vm_flags_t vm_flags, unsigned long pgoff, unsigned long *populate,
+ struct list_head *uf)
+{
+ return __do_mmap(file, addr, len, prot, flags, vm_flags, pgoff,
+ populate, uf, current->mm);
+}
+#else
extern unsigned long do_mmap(struct file *file, unsigned long addr,
unsigned long len, unsigned long prot, unsigned long flags,
vm_flags_t vm_flags, unsigned long pgoff, unsigned long *populate,
struct list_head *uf);
+#endif
+
extern int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm,
unsigned long start, size_t len, struct list_head *uf,
bool unlock);
diff --git a/mm/mmap.c b/mm/mmap.c
index bd210aaf7ebd..0cc187a60a0f 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -278,7 +278,7 @@ static inline bool file_mmap_ok(struct file *file, struct inode *inode,
}

/**
- * do_mmap() - Perform a userland memory mapping into the current process
+ * __do_mmap() - Perform a userland memory mapping into the current process
* address space of length @len with protection bits @prot, mmap flags @flags
* (from which VMA flags will be inferred), and any additional VMA flags to
* apply @vm_flags. If this is a file-backed mapping then the file is specified
@@ -330,17 +330,17 @@ static inline bool file_mmap_ok(struct file *file, struct inode *inode,
* @uf: An optional pointer to a list head to track userfaultfd unmap events
* should unmapping events arise. If provided, it is up to the caller to manage
* this.
+ * @mm: The mm_struct
*
* Returns: Either an error, or the address at which the requested mapping has
* been performed.
*/
-unsigned long do_mmap(struct file *file, unsigned long addr,
+unsigned long __do_mmap(struct file *file, unsigned long addr,
unsigned long len, unsigned long prot,
unsigned long flags, vm_flags_t vm_flags,
unsigned long pgoff, unsigned long *populate,
- struct list_head *uf)
+ struct list_head *uf, struct mm_struct *mm)
{
- struct mm_struct *mm = current->mm;
int pkey = 0;

*populate = 0;
@@ -558,7 +558,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
vm_flags |= VM_NORESERVE;
}

- addr = mmap_region(file, addr, len, vm_flags, pgoff, uf);
+ addr = mmap_region(file, addr, len, vm_flags, pgoff, uf, mm);
if (!IS_ERR_VALUE(addr) &&
((vm_flags & VM_LOCKED) ||
(flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE))
diff --git a/mm/vma.c b/mm/vma.c
index 5cdc5612bfc1..9069b42edab6 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -2452,9 +2452,8 @@ static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma)

static unsigned long __mmap_region(struct file *file, unsigned long addr,
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
- struct list_head *uf)
+ struct list_head *uf, struct mm_struct *mm)
{
- struct mm_struct *mm = current->mm;
struct vm_area_struct *vma = NULL;
int error;
VMA_ITERATOR(vmi, mm, addr);
@@ -2521,18 +2520,19 @@ static unsigned long __mmap_region(struct file *file, unsigned long addr,
* the virtual page offset in memory of the anonymous mapping.
* @uf: Optionally, a pointer to a list head used for tracking userfaultfd unmap
* events.
+ * @mm: The mm struct
*
* Returns: Either an error, or the address at which the requested mapping has
* been performed.
*/
unsigned long mmap_region(struct file *file, unsigned long addr,
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
- struct list_head *uf)
+ struct list_head *uf, struct mm_struct *mm)
{
unsigned long ret;
bool writable_file_mapping = false;

- mmap_assert_write_locked(current->mm);
+ mmap_assert_write_locked(mm);

/* Check to see if MDWE is applicable. */
if (map_deny_write_exec(vm_flags, vm_flags))
@@ -2551,13 +2551,13 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
writable_file_mapping = true;
}

- ret = __mmap_region(file, addr, len, vm_flags, pgoff, uf);
+ ret = __mmap_region(file, addr, len, vm_flags, pgoff, uf, mm);

/* Clear our write mapping regardless of error. */
if (writable_file_mapping)
mapping_unmap_writable(file->f_mapping);

- validate_mm(current->mm);
+ validate_mm(mm);
return ret;
}

diff --git a/mm/vma.h b/mm/vma.h
index 7356ca5a22d3..a6db191e65cf 100644
--- a/mm/vma.h
+++ b/mm/vma.h
@@ -292,7 +292,7 @@ void mm_drop_all_locks(struct mm_struct *mm);

unsigned long mmap_region(struct file *file, unsigned long addr,
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
- struct list_head *uf);
+ struct list_head *uf, struct mm_struct *mm);

int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma,
unsigned long addr, unsigned long request, unsigned long flags);
--
2.43.5