Skip to content

Commit 41f727f

Browse files
Vladimir Davydovtorvalds
authored andcommitted
fork/exec: cleanup mm initialization
mm initialization on fork/exec is spread all over the place, which makes the code look inconsistent. We have mm_init(), which is supposed to init/nullify mm's internals, but it doesn't init all the fields it should: - on fork ->mmap,mm_rb,vmacache_seqnum,map_count,mm_cpumask,locked_vm are zeroed in dup_mmap(); - on fork ->pmd_huge_pte is zeroed in dup_mm(), immediately before calling mm_init(); - ->cpu_vm_mask_var ptr is initialized by mm_init_cpumask(), which is called before mm_init() on both fork and exec; - ->context is initialized by init_new_context(), which is called after mm_init() on both fork and exec; Let's consolidate all the initializations in mm_init() to make the code look cleaner. Signed-off-by: Vladimir Davydov <vdavydov@parallels.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: David Rientjes <rientjes@google.com> Cc: Christoph Lameter <cl@linux.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8f053ac commit 41f727f

3 files changed

Lines changed: 21 additions & 31 deletions

File tree

‎fs/exec.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,6 @@ static int bprm_mm_init(struct linux_binprm *bprm)
368368
if (!mm)
369369
goto err;
370370

371-
err = init_new_context(current, mm);
372-
if (err)
373-
goto err;
374-
375371
err = __bprm_mm_init(bprm);
376372
if (err)
377373
goto err;

‎include/linux/mm_types.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ static inline void mm_init_cpumask(struct mm_struct *mm)
461461
#ifdef CONFIG_CPUMASK_OFFSTACK
462462
mm->cpu_vm_mask_var = &mm->cpumask_allocation;
463463
#endif
464+
cpumask_clear(mm->cpu_vm_mask_var);
464465
}
465466

466467
/* Future-safe accessor for struct mm_struct's cpu_vm_mask. */

‎kernel/fork.c‎

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,6 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
374374
*/
375375
down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
376376

377-
mm->locked_vm = 0;
378-
mm->mmap = NULL;
379-
mm->vmacache_seqnum = 0;
380-
mm->map_count = 0;
381-
cpumask_clear(mm_cpumask(mm));
382-
mm->mm_rb = RB_ROOT;
383377
rb_link = &mm->mm_rb.rb_node;
384378
rb_parent = NULL;
385379
pprev = &mm->mmap;
@@ -538,17 +532,27 @@ static void mm_init_aio(struct mm_struct *mm)
538532

539533
static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
540534
{
535+
mm->mmap = NULL;
536+
mm->mm_rb = RB_ROOT;
537+
mm->vmacache_seqnum = 0;
541538
atomic_set(&mm->mm_users, 1);
542539
atomic_set(&mm->mm_count, 1);
543540
init_rwsem(&mm->mmap_sem);
544541
INIT_LIST_HEAD(&mm->mmlist);
545542
mm->core_state = NULL;
546543
atomic_long_set(&mm->nr_ptes, 0);
544+
mm->map_count = 0;
545+
mm->locked_vm = 0;
547546
memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
548547
spin_lock_init(&mm->page_table_lock);
548+
mm_init_cpumask(mm);
549549
mm_init_aio(mm);
550550
mm_init_owner(mm, p);
551+
mmu_notifier_mm_init(mm);
551552
clear_tlb_flush_pending(mm);
553+
#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
554+
mm->pmd_huge_pte = NULL;
555+
#endif
552556

553557
if (current->mm) {
554558
mm->flags = current->mm->flags & MMF_INIT_MASK;
@@ -558,11 +562,17 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
558562
mm->def_flags = 0;
559563
}
560564

561-
if (likely(!mm_alloc_pgd(mm))) {
562-
mmu_notifier_mm_init(mm);
563-
return mm;
564-
}
565+
if (mm_alloc_pgd(mm))
566+
goto fail_nopgd;
567+
568+
if (init_new_context(p, mm))
569+
goto fail_nocontext;
565570

571+
return mm;
572+
573+
fail_nocontext:
574+
mm_free_pgd(mm);
575+
fail_nopgd:
566576
free_mm(mm);
567577
return NULL;
568578
}
@@ -596,7 +606,6 @@ struct mm_struct *mm_alloc(void)
596606
return NULL;
597607

598608
memset(mm, 0, sizeof(*mm));
599-
mm_init_cpumask(mm);
600609
return mm_init(mm, current);
601610
}
602611

@@ -828,17 +837,10 @@ static struct mm_struct *dup_mm(struct task_struct *tsk)
828837
goto fail_nomem;
829838

830839
memcpy(mm, oldmm, sizeof(*mm));
831-
mm_init_cpumask(mm);
832840

833-
#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
834-
mm->pmd_huge_pte = NULL;
835-
#endif
836841
if (!mm_init(mm, tsk))
837842
goto fail_nomem;
838843

839-
if (init_new_context(tsk, mm))
840-
goto fail_nocontext;
841-
842844
dup_mm_exe_file(oldmm, mm);
843845

844846
err = dup_mmap(mm, oldmm);
@@ -860,15 +862,6 @@ static struct mm_struct *dup_mm(struct task_struct *tsk)
860862

861863
fail_nomem:
862864
return NULL;
863-
864-
fail_nocontext:
865-
/*
866-
* If init_new_context() failed, we cannot use mmput() to free the mm
867-
* because it calls destroy_context()
868-
*/
869-
mm_free_pgd(mm);
870-
free_mm(mm);
871-
return NULL;
872865
}
873866

874867
static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)

0 commit comments

Comments
 (0)