Skip to content

Commit

Permalink
mm: use helper functions for allocating and freeing vm_area structs
Browse files Browse the repository at this point in the history
The vm_area_struct is one of the most fundamental memory management
objects, but the management of it is entirely open-coded evertwhere,
ranging from allocation and freeing (using kmem_cache_[z]alloc and
kmem_cache_free) to initializing all the fields.

We want to unify this in order to end up having some unified
initialization of the vmas, and the first step to this is to at least
have basic allocation functions.

Right now those functions are literally just wrappers around the
kmem_cache_*() calls.  This is a purely mechanical conversion:

    # new vma:
    kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL) -> vm_area_alloc()

    # copy old vma
    kmem_cache_alloc(vm_area_cachep, GFP_KERNEL) -> vm_area_dup(old)

    # free vma
    kmem_cache_free(vm_area_cachep, vma) -> vm_area_free(vma)

to the point where the old vma passed in to the vm_area_dup() function
isn't even used yet (because I've left all the old manual initialization
alone).

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Park Ju Hyung <qkrwngud825@gmail.com>
Signed-off-by: kyvangka1610 <kyvangka2002@gmail.com>
  • Loading branch information
torvalds authored and Kyvangka1610 committed Jan 8, 2021
1 parent 52c56be commit e7ba4d7
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 194 deletions.
4 changes: 2 additions & 2 deletions arch/ia64/kernel/perfmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -2278,7 +2278,7 @@ pfm_smpl_buffer_alloc(struct task_struct *task, struct file *filp, pfm_context_t
DPRINT(("smpl_buf @%p\n", smpl_buf));

/* allocate vma */
vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
vma = vm_area_alloc();
if (!vma) {
DPRINT(("Cannot allocate vma\n"));
goto error_kmem;
Expand Down Expand Up @@ -2346,7 +2346,7 @@ pfm_smpl_buffer_alloc(struct task_struct *task, struct file *filp, pfm_context_t
return 0;

error:
kmem_cache_free(vm_area_cachep, vma);
vm_area_free(vma);
error_kmem:
pfm_rvfree(smpl_buf, size);

Expand Down
8 changes: 4 additions & 4 deletions arch/ia64/mm/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ ia64_init_addr_space (void)
* the problem. When the process attempts to write to the register backing store
* for the first time, it will get a SEGFAULT in this case.
*/
vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
vma = vm_area_alloc();
if (vma) {
INIT_LIST_HEAD(&vma->anon_vma_chain);
vma->vm_mm = current->mm;
Expand All @@ -125,15 +125,15 @@ ia64_init_addr_space (void)
down_write(&current->mm->mmap_sem);
if (insert_vm_struct(current->mm, vma)) {
up_write(&current->mm->mmap_sem);
kmem_cache_free(vm_area_cachep, vma);
vm_area_free(vma);
return;
}
up_write(&current->mm->mmap_sem);
}

/* map NaT-page at address zero to speed up speculative dereferencing of NULL: */
if (!(current->personality & MMAP_PAGE_ZERO)) {
vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
vma = vm_area_alloc();
if (vma) {
INIT_LIST_HEAD(&vma->anon_vma_chain);
vma->vm_mm = current->mm;
Expand All @@ -144,7 +144,7 @@ ia64_init_addr_space (void)
down_write(&current->mm->mmap_sem);
if (insert_vm_struct(current->mm, vma)) {
up_write(&current->mm->mmap_sem);
kmem_cache_free(vm_area_cachep, vma);
vm_area_free(vma);
return;
}
up_write(&current->mm->mmap_sem);
Expand Down
4 changes: 2 additions & 2 deletions fs/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ static int __bprm_mm_init(struct linux_binprm *bprm)
struct vm_area_struct *vma = NULL;
struct mm_struct *mm = bprm->mm;

bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
bprm->vma = vma = vm_area_alloc();
if (!vma)
return -ENOMEM;

Expand Down Expand Up @@ -326,7 +326,7 @@ static int __bprm_mm_init(struct linux_binprm *bprm)
up_write(&mm->mmap_sem);
err_free:
bprm->vma = NULL;
kmem_cache_free(vm_area_cachep, vma);
vm_area_free(vma);
return err;
}

Expand Down
4 changes: 3 additions & 1 deletion include/linux/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *,
* mmap() functions).
*/

extern struct kmem_cache *vm_area_cachep;
struct vm_area_struct *vm_area_alloc(void);
struct vm_area_struct *vm_area_dup(struct vm_area_struct *);
void vm_area_free(struct vm_area_struct *);

#ifndef CONFIG_MMU
extern struct rb_root nommu_region_tree;
Expand Down
Loading

0 comments on commit e7ba4d7

Please sign in to comment.