Commit 27b26701 authored by Liam R. Howlett's avatar Liam R. Howlett Committed by Andrew Morton

ipc/shm: introduce new do_vma_munmap() to munmap

The shm already has the vma iterator in position for a write. 
do_vmi_munmap() searches for the correct position and aligns the write, so
it is not the right function to use in this case.

The shm VMA tree modification is similar to the brk munmap situation, the
vma iterator is in position and the VMA is already known.  This patch
generalizes the brk munmap function do_brk_munmap() to be used for any
other callers with the vma iterator already in position to munmap a VMA.

Link: https://lkml.kernel.org/r/20230126212049.980501-1-Liam.Howlett@oracle.comSigned-off-by: default avatarLiam R. Howlett <Liam.Howlett@oracle.com>
Reported-by: default avatarSven Schnelle <svens@linux.ibm.com>
  Link: https://lore.kernel.org/linux-mm/yt9dh6wec21a.fsf@linux.ibm.com/
Cc: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent d60beb1f
...@@ -2922,6 +2922,9 @@ extern int do_munmap(struct mm_struct *, unsigned long, size_t, ...@@ -2922,6 +2922,9 @@ extern int do_munmap(struct mm_struct *, unsigned long, size_t,
extern int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior); extern int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior);
#ifdef CONFIG_MMU #ifdef CONFIG_MMU
extern int do_vma_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,
unsigned long start, unsigned long end,
struct list_head *uf, bool downgrade);
extern int __mm_populate(unsigned long addr, unsigned long len, extern int __mm_populate(unsigned long addr, unsigned long len,
int ignore_errors); int ignore_errors);
static inline void mm_populate(unsigned long addr, unsigned long len) static inline void mm_populate(unsigned long addr, unsigned long len)
......
...@@ -1786,8 +1786,8 @@ long ksys_shmdt(char __user *shmaddr) ...@@ -1786,8 +1786,8 @@ long ksys_shmdt(char __user *shmaddr)
*/ */
file = vma->vm_file; file = vma->vm_file;
size = i_size_read(file_inode(vma->vm_file)); size = i_size_read(file_inode(vma->vm_file));
do_vmi_munmap(&vmi, mm, vma->vm_start, do_vma_munmap(&vmi, vma, vma->vm_start, vma->vm_end,
vma->vm_end - vma->vm_start, NULL, false); NULL, false);
/* /*
* We discovered the size of the shm segment, so * We discovered the size of the shm segment, so
* break out of here and fall through to the next * break out of here and fall through to the next
...@@ -1810,9 +1810,10 @@ long ksys_shmdt(char __user *shmaddr) ...@@ -1810,9 +1810,10 @@ long ksys_shmdt(char __user *shmaddr)
/* finding a matching vma now does not alter retval */ /* finding a matching vma now does not alter retval */
if ((vma->vm_ops == &shm_vm_ops) && if ((vma->vm_ops == &shm_vm_ops) &&
((vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) && ((vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) &&
(vma->vm_file == file)) (vma->vm_file == file)) {
do_vmi_munmap(&vmi, mm, vma->vm_start, do_vma_munmap(&vmi, vma, vma->vm_start, vma->vm_end,
vma->vm_end - vma->vm_start, NULL, false); NULL, false);
}
vma = vma_next(&vmi); vma = vma_next(&vmi);
} }
......
...@@ -180,9 +180,6 @@ static int check_brk_limits(unsigned long addr, unsigned long len) ...@@ -180,9 +180,6 @@ static int check_brk_limits(unsigned long addr, unsigned long len)
return mlock_future_check(current->mm, current->mm->def_flags, len); return mlock_future_check(current->mm, current->mm->def_flags, len);
} }
static int do_brk_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,
unsigned long newbrk, unsigned long oldbrk,
struct list_head *uf);
static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma, static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma,
unsigned long addr, unsigned long request, unsigned long flags); unsigned long addr, unsigned long request, unsigned long flags);
SYSCALL_DEFINE1(brk, unsigned long, brk) SYSCALL_DEFINE1(brk, unsigned long, brk)
...@@ -236,7 +233,7 @@ SYSCALL_DEFINE1(brk, unsigned long, brk) ...@@ -236,7 +233,7 @@ SYSCALL_DEFINE1(brk, unsigned long, brk)
/* /*
* Always allow shrinking brk. * Always allow shrinking brk.
* do_brk_munmap() may downgrade mmap_lock to read. * do_vma_munmap() may downgrade mmap_lock to read.
*/ */
if (brk <= mm->brk) { if (brk <= mm->brk) {
int ret; int ret;
...@@ -248,11 +245,11 @@ SYSCALL_DEFINE1(brk, unsigned long, brk) ...@@ -248,11 +245,11 @@ SYSCALL_DEFINE1(brk, unsigned long, brk)
goto out; /* mapping intersects with an existing non-brk vma. */ goto out; /* mapping intersects with an existing non-brk vma. */
/* /*
* mm->brk must be protected by write mmap_lock. * mm->brk must be protected by write mmap_lock.
* do_brk_munmap() may downgrade the lock, so update it * do_vma_munmap() may downgrade the lock, so update it
* before calling do_brk_munmap(). * before calling do_vma_munmap().
*/ */
mm->brk = brk; mm->brk = brk;
ret = do_brk_munmap(&vmi, brkvma, newbrk, oldbrk, &uf); ret = do_vma_munmap(&vmi, brkvma, newbrk, oldbrk, &uf, true);
if (ret == 1) { if (ret == 1) {
downgraded = true; downgraded = true;
goto success; goto success;
...@@ -2951,26 +2948,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, ...@@ -2951,26 +2948,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
} }
/* /*
* brk_munmap() - Unmap a full or partial vma. * do_vma_munmap() - Unmap a full or partial vma.
* @vmi: The vma iterator * @vmi: The vma iterator pointing at the vma
* @vma: The vma to be modified * @vma: The first vma to be munmapped
* @newbrk: the start of the address to unmap * @start: the start of the address to unmap
* @oldbrk: The end of the address to unmap * @end: The end of the address to unmap
* @uf: The userfaultfd list_head * @uf: The userfaultfd list_head
* @downgrade: Attempt to downgrade or not
* *
* Returns: 1 on success. * Returns: 0 on success and not downgraded, 1 on success and downgraded.
* unmaps a partial VMA mapping. Does not handle alignment, downgrades lock if * unmaps a VMA mapping when the vma iterator is already in position.
* possible. * Does not handle alignment.
*/ */
static int do_brk_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, int do_vma_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,
unsigned long newbrk, unsigned long oldbrk, unsigned long start, unsigned long end,
struct list_head *uf) struct list_head *uf, bool downgrade)
{ {
struct mm_struct *mm = vma->vm_mm; struct mm_struct *mm = vma->vm_mm;
int ret; int ret;
arch_unmap(mm, newbrk, oldbrk); arch_unmap(mm, start, end);
ret = do_vmi_align_munmap(vmi, vma, mm, newbrk, oldbrk, uf, true); ret = do_vmi_align_munmap(vmi, vma, mm, start, end, uf, downgrade);
validate_mm_mt(mm); validate_mm_mt(mm);
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment