Skip to content

Commit 3928d4f

Browse files
committed
mm: use helper functions for allocating and freeing vm_area structs
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>
1 parent 191a3af commit 3928d4f

File tree

7 files changed

+44
-27
lines changed

7 files changed

+44
-27
lines changed

arch/ia64/kernel/perfmon.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,7 +2278,7 @@ pfm_smpl_buffer_alloc(struct task_struct *task, struct file *filp, pfm_context_t
22782278
DPRINT(("smpl_buf @%p\n", smpl_buf));
22792279

22802280
/* allocate vma */
2281-
vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2281+
vma = vm_area_alloc();
22822282
if (!vma) {
22832283
DPRINT(("Cannot allocate vma\n"));
22842284
goto error_kmem;
@@ -2346,7 +2346,7 @@ pfm_smpl_buffer_alloc(struct task_struct *task, struct file *filp, pfm_context_t
23462346
return 0;
23472347

23482348
error:
2349-
kmem_cache_free(vm_area_cachep, vma);
2349+
vm_area_free(vma);
23502350
error_kmem:
23512351
pfm_rvfree(smpl_buf, size);
23522352

arch/ia64/mm/init.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ ia64_init_addr_space (void)
114114
* the problem. When the process attempts to write to the register backing store
115115
* for the first time, it will get a SEGFAULT in this case.
116116
*/
117-
vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
117+
vma = vm_area_alloc();
118118
if (vma) {
119119
INIT_LIST_HEAD(&vma->anon_vma_chain);
120120
vma->vm_mm = current->mm;
@@ -125,15 +125,15 @@ ia64_init_addr_space (void)
125125
down_write(&current->mm->mmap_sem);
126126
if (insert_vm_struct(current->mm, vma)) {
127127
up_write(&current->mm->mmap_sem);
128-
kmem_cache_free(vm_area_cachep, vma);
128+
vm_area_free(vma);
129129
return;
130130
}
131131
up_write(&current->mm->mmap_sem);
132132
}
133133

134134
/* map NaT-page at address zero to speed up speculative dereferencing of NULL: */
135135
if (!(current->personality & MMAP_PAGE_ZERO)) {
136-
vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
136+
vma = vm_area_alloc();
137137
if (vma) {
138138
INIT_LIST_HEAD(&vma->anon_vma_chain);
139139
vma->vm_mm = current->mm;
@@ -144,7 +144,7 @@ ia64_init_addr_space (void)
144144
down_write(&current->mm->mmap_sem);
145145
if (insert_vm_struct(current->mm, vma)) {
146146
up_write(&current->mm->mmap_sem);
147-
kmem_cache_free(vm_area_cachep, vma);
147+
vm_area_free(vma);
148148
return;
149149
}
150150
up_write(&current->mm->mmap_sem);

fs/exec.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ static int __bprm_mm_init(struct linux_binprm *bprm)
290290
struct vm_area_struct *vma = NULL;
291291
struct mm_struct *mm = bprm->mm;
292292

293-
bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
293+
bprm->vma = vma = vm_area_alloc();
294294
if (!vma)
295295
return -ENOMEM;
296296

@@ -326,7 +326,7 @@ static int __bprm_mm_init(struct linux_binprm *bprm)
326326
up_write(&mm->mmap_sem);
327327
err_free:
328328
bprm->vma = NULL;
329-
kmem_cache_free(vm_area_cachep, vma);
329+
vm_area_free(vma);
330330
return err;
331331
}
332332

include/linux/mm.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *,
155155
* mmap() functions).
156156
*/
157157

158-
extern struct kmem_cache *vm_area_cachep;
158+
struct vm_area_struct *vm_area_alloc(void);
159+
struct vm_area_struct *vm_area_dup(struct vm_area_struct *);
160+
void vm_area_free(struct vm_area_struct *);
159161

160162
#ifndef CONFIG_MMU
161163
extern struct rb_root nommu_region_tree;

kernel/fork.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,26 @@ struct kmem_cache *files_cachep;
303303
struct kmem_cache *fs_cachep;
304304

305305
/* SLAB cache for vm_area_struct structures */
306-
struct kmem_cache *vm_area_cachep;
306+
static struct kmem_cache *vm_area_cachep;
307307

308308
/* SLAB cache for mm_struct structures (tsk->mm) */
309309
static struct kmem_cache *mm_cachep;
310310

311+
struct vm_area_struct *vm_area_alloc(void)
312+
{
313+
return kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
314+
}
315+
316+
struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)
317+
{
318+
return kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
319+
}
320+
321+
void vm_area_free(struct vm_area_struct *vma)
322+
{
323+
kmem_cache_free(vm_area_cachep, vma);
324+
}
325+
311326
static void account_kernel_stack(struct task_struct *tsk, int account)
312327
{
313328
void *stack = task_stack_page(tsk);
@@ -455,7 +470,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
455470
goto fail_nomem;
456471
charge = len;
457472
}
458-
tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
473+
tmp = vm_area_dup(mpnt);
459474
if (!tmp)
460475
goto fail_nomem;
461476
*tmp = *mpnt;
@@ -539,7 +554,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
539554
fail_nomem_anon_vma_fork:
540555
mpol_put(vma_policy(tmp));
541556
fail_nomem_policy:
542-
kmem_cache_free(vm_area_cachep, tmp);
557+
vm_area_free(tmp);
543558
fail_nomem:
544559
retval = -ENOMEM;
545560
vm_unacct_memory(charge);

mm/mmap.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
182182
if (vma->vm_file)
183183
fput(vma->vm_file);
184184
mpol_put(vma_policy(vma));
185-
kmem_cache_free(vm_area_cachep, vma);
185+
vm_area_free(vma);
186186
return next;
187187
}
188188

@@ -911,7 +911,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
911911
anon_vma_merge(vma, next);
912912
mm->map_count--;
913913
mpol_put(vma_policy(next));
914-
kmem_cache_free(vm_area_cachep, next);
914+
vm_area_free(next);
915915
/*
916916
* In mprotect's case 6 (see comments on vma_merge),
917917
* we must remove another next too. It would clutter
@@ -1729,7 +1729,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
17291729
* specific mapper. the address has already been validated, but
17301730
* not unmapped, but the maps are removed from the list.
17311731
*/
1732-
vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1732+
vma = vm_area_alloc();
17331733
if (!vma) {
17341734
error = -ENOMEM;
17351735
goto unacct_error;
@@ -1832,7 +1832,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
18321832
if (vm_flags & VM_DENYWRITE)
18331833
allow_write_access(file);
18341834
free_vma:
1835-
kmem_cache_free(vm_area_cachep, vma);
1835+
vm_area_free(vma);
18361836
unacct_error:
18371837
if (charged)
18381838
vm_unacct_memory(charged);
@@ -2620,7 +2620,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
26202620
return err;
26212621
}
26222622

2623-
new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
2623+
new = vm_area_dup(vma);
26242624
if (!new)
26252625
return -ENOMEM;
26262626

@@ -2669,7 +2669,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
26692669
out_free_mpol:
26702670
mpol_put(vma_policy(new));
26712671
out_free_vma:
2672-
kmem_cache_free(vm_area_cachep, new);
2672+
vm_area_free(new);
26732673
return err;
26742674
}
26752675

@@ -2984,7 +2984,7 @@ static int do_brk_flags(unsigned long addr, unsigned long len, unsigned long fla
29842984
/*
29852985
* create a vma struct for an anonymous mapping
29862986
*/
2987-
vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2987+
vma = vm_area_alloc();
29882988
if (!vma) {
29892989
vm_unacct_memory(len >> PAGE_SHIFT);
29902990
return -ENOMEM;
@@ -3202,7 +3202,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
32023202
}
32033203
*need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
32043204
} else {
3205-
new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
3205+
new_vma = vm_area_dup(vma);
32063206
if (!new_vma)
32073207
goto out;
32083208
*new_vma = *vma;
@@ -3226,7 +3226,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
32263226
out_free_mempol:
32273227
mpol_put(vma_policy(new_vma));
32283228
out_free_vma:
3229-
kmem_cache_free(vm_area_cachep, new_vma);
3229+
vm_area_free(new_vma);
32303230
out:
32313231
return NULL;
32323232
}
@@ -3350,7 +3350,7 @@ static struct vm_area_struct *__install_special_mapping(
33503350
int ret;
33513351
struct vm_area_struct *vma;
33523352

3353-
vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
3353+
vma = vm_area_alloc();
33543354
if (unlikely(vma == NULL))
33553355
return ERR_PTR(-ENOMEM);
33563356

@@ -3376,7 +3376,7 @@ static struct vm_area_struct *__install_special_mapping(
33763376
return vma;
33773377

33783378
out:
3379-
kmem_cache_free(vm_area_cachep, vma);
3379+
vm_area_free(vma);
33803380
return ERR_PTR(ret);
33813381
}
33823382

mm/nommu.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
769769
if (vma->vm_file)
770770
fput(vma->vm_file);
771771
put_nommu_region(vma->vm_region);
772-
kmem_cache_free(vm_area_cachep, vma);
772+
vm_area_free(vma);
773773
}
774774

775775
/*
@@ -1204,7 +1204,7 @@ unsigned long do_mmap(struct file *file,
12041204
if (!region)
12051205
goto error_getting_region;
12061206

1207-
vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1207+
vma = vm_area_alloc();
12081208
if (!vma)
12091209
goto error_getting_vma;
12101210

@@ -1368,7 +1368,7 @@ unsigned long do_mmap(struct file *file,
13681368
kmem_cache_free(vm_region_jar, region);
13691369
if (vma->vm_file)
13701370
fput(vma->vm_file);
1371-
kmem_cache_free(vm_area_cachep, vma);
1371+
vm_area_free(vma);
13721372
return ret;
13731373

13741374
sharing_violation:
@@ -1469,7 +1469,7 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
14691469
if (!region)
14701470
return -ENOMEM;
14711471

1472-
new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1472+
new = vm_area_dup(vma);
14731473
if (!new) {
14741474
kmem_cache_free(vm_region_jar, region);
14751475
return -ENOMEM;

0 commit comments

Comments
 (0)