Commit 30afc8c3 authored by Yajun Deng's avatar Yajun Deng Committed by Andrew Morton

mm/mmap: simplify vma link and unlink

The file parameter in the __remove_shared_vm_struct is no longer used,
remove it.

These functions vma_link() and mmap_region() have some of the same code,
introduce vma_link_file() helper function to simplify the code.

Link: https://lkml.kernel.org/r/20240110084622.2425927-1-yajun.deng@linux.devSigned-off-by: default avatarYajun Deng <yajun.deng@linux.dev>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 0040f2c5
...@@ -105,7 +105,7 @@ void vma_set_page_prot(struct vm_area_struct *vma) ...@@ -105,7 +105,7 @@ void vma_set_page_prot(struct vm_area_struct *vma)
* Requires inode->i_mapping->i_mmap_rwsem * Requires inode->i_mapping->i_mmap_rwsem
*/ */
static void __remove_shared_vm_struct(struct vm_area_struct *vma, static void __remove_shared_vm_struct(struct vm_area_struct *vma,
struct file *file, struct address_space *mapping) struct address_space *mapping)
{ {
if (vma_is_shared_maywrite(vma)) if (vma_is_shared_maywrite(vma))
mapping_unmap_writable(mapping); mapping_unmap_writable(mapping);
...@@ -126,7 +126,7 @@ void unlink_file_vma(struct vm_area_struct *vma) ...@@ -126,7 +126,7 @@ void unlink_file_vma(struct vm_area_struct *vma)
if (file) { if (file) {
struct address_space *mapping = file->f_mapping; struct address_space *mapping = file->f_mapping;
i_mmap_lock_write(mapping); i_mmap_lock_write(mapping);
__remove_shared_vm_struct(vma, file, mapping); __remove_shared_vm_struct(vma, mapping);
i_mmap_unlock_write(mapping); i_mmap_unlock_write(mapping);
} }
} }
...@@ -392,26 +392,30 @@ static void __vma_link_file(struct vm_area_struct *vma, ...@@ -392,26 +392,30 @@ static void __vma_link_file(struct vm_area_struct *vma,
flush_dcache_mmap_unlock(mapping); flush_dcache_mmap_unlock(mapping);
} }
static void vma_link_file(struct vm_area_struct *vma)
{
struct file *file = vma->vm_file;
struct address_space *mapping;
if (file) {
mapping = file->f_mapping;
i_mmap_lock_write(mapping);
__vma_link_file(vma, mapping);
i_mmap_unlock_write(mapping);
}
}
static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma) static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma)
{ {
VMA_ITERATOR(vmi, mm, 0); VMA_ITERATOR(vmi, mm, 0);
struct address_space *mapping = NULL;
vma_iter_config(&vmi, vma->vm_start, vma->vm_end); vma_iter_config(&vmi, vma->vm_start, vma->vm_end);
if (vma_iter_prealloc(&vmi, vma)) if (vma_iter_prealloc(&vmi, vma))
return -ENOMEM; return -ENOMEM;
vma_start_write(vma); vma_start_write(vma);
vma_iter_store(&vmi, vma); vma_iter_store(&vmi, vma);
vma_link_file(vma);
if (vma->vm_file) {
mapping = vma->vm_file->f_mapping;
i_mmap_lock_write(mapping);
__vma_link_file(vma, mapping);
i_mmap_unlock_write(mapping);
}
mm->map_count++; mm->map_count++;
validate_mm(mm); validate_mm(mm);
return 0; return 0;
...@@ -519,10 +523,9 @@ static inline void vma_complete(struct vma_prepare *vp, ...@@ -519,10 +523,9 @@ static inline void vma_complete(struct vma_prepare *vp,
} }
if (vp->remove && vp->file) { if (vp->remove && vp->file) {
__remove_shared_vm_struct(vp->remove, vp->file, vp->mapping); __remove_shared_vm_struct(vp->remove, vp->mapping);
if (vp->remove2) if (vp->remove2)
__remove_shared_vm_struct(vp->remove2, vp->file, __remove_shared_vm_struct(vp->remove2, vp->mapping);
vp->mapping);
} else if (vp->insert) { } else if (vp->insert) {
/* /*
* split_vma has split insert from vma, and needs * split_vma has split insert from vma, and needs
...@@ -2891,16 +2894,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, ...@@ -2891,16 +2894,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
vma_start_write(vma); vma_start_write(vma);
vma_iter_store(&vmi, vma); vma_iter_store(&vmi, vma);
mm->map_count++; mm->map_count++;
if (vma->vm_file) { vma_link_file(vma);
i_mmap_lock_write(vma->vm_file->f_mapping);
if (vma_is_shared_maywrite(vma))
mapping_allow_writable(vma->vm_file->f_mapping);
flush_dcache_mmap_lock(vma->vm_file->f_mapping);
vma_interval_tree_insert(vma, &vma->vm_file->f_mapping->i_mmap);
flush_dcache_mmap_unlock(vma->vm_file->f_mapping);
i_mmap_unlock_write(vma->vm_file->f_mapping);
}
/* /*
* vma_merge() calls khugepaged_enter_vma() either, the below * vma_merge() calls khugepaged_enter_vma() either, the below
......
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