Skip to content

Commit 6345d24

Browse files
committed
mm: Fix boot crash in mm_alloc()
Thomas Gleixner reports that we now have a boot crash triggered by CONFIG_CPUMASK_OFFSTACK=y: BUG: unable to handle kernel NULL pointer dereference at (null) IP: [<c11ae035>] find_next_bit+0x55/0xb0 Call Trace: [<c11addda>] cpumask_any_but+0x2a/0x70 [<c102396b>] flush_tlb_mm+0x2b/0x80 [<c1022705>] pud_populate+0x35/0x50 [<c10227ba>] pgd_alloc+0x9a/0xf0 [<c103a3fc>] mm_init+0xec/0x120 [<c103a7a3>] mm_alloc+0x53/0xd0 which was introduced by commit de03c72 ("mm: convert mm->cpu_vm_cpumask into cpumask_var_t"), and is due to wrong ordering of mm_init() vs mm_init_cpumask Thomas wrote a patch to just fix the ordering of initialization, but I hate the new double allocation in the fork path, so I ended up instead doing some more radical surgery to clean it all up. Reported-by: Thomas Gleixner <tglx@linutronix.de> Reported-by: Ingo Molnar <mingo@elte.hu> Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com> Cc: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent cab0d85 commit 6345d24

4 files changed

Lines changed: 23 additions & 36 deletions

File tree

‎include/linux/mm_types.h‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ struct mm_struct {
264264

265265
struct linux_binfmt *binfmt;
266266

267+
cpumask_var_t cpu_vm_mask_var;
268+
267269
/* Architecture-specific MM context */
268270
mm_context_t context;
269271

@@ -311,10 +313,18 @@ struct mm_struct {
311313
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
312314
pgtable_t pmd_huge_pte; /* protected by page_table_lock */
313315
#endif
314-
315-
cpumask_var_t cpu_vm_mask_var;
316+
#ifdef CONFIG_CPUMASK_OFFSTACK
317+
struct cpumask cpumask_allocation;
318+
#endif
316319
};
317320

321+
static inline void mm_init_cpumask(struct mm_struct *mm)
322+
{
323+
#ifdef CONFIG_CPUMASK_OFFSTACK
324+
mm->cpu_vm_mask_var = &mm->cpumask_allocation;
325+
#endif
326+
}
327+
318328
/* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
319329
static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
320330
{

‎include/linux/sched.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2194,7 +2194,6 @@ static inline void mmdrop(struct mm_struct * mm)
21942194
if (unlikely(atomic_dec_and_test(&mm->mm_count)))
21952195
__mmdrop(mm);
21962196
}
2197-
extern int mm_init_cpumask(struct mm_struct *mm, struct mm_struct *oldmm);
21982197

21992198
/* mmput gets rid of the mappings and all user-space */
22002199
extern void mmput(struct mm_struct *);

‎init/main.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ asmlinkage void __init start_kernel(void)
487487
printk(KERN_NOTICE "%s", linux_banner);
488488
setup_arch(&command_line);
489489
mm_init_owner(&init_mm, &init_task);
490+
mm_init_cpumask(&init_mm);
490491
setup_command_line(command_line);
491492
setup_nr_cpu_ids();
492493
setup_per_cpu_areas();
@@ -510,7 +511,6 @@ asmlinkage void __init start_kernel(void)
510511
sort_main_extable();
511512
trap_init();
512513
mm_init();
513-
BUG_ON(mm_init_cpumask(&init_mm, 0));
514514

515515
/*
516516
* Set up the scheduler prior starting any interrupts (such as the

‎kernel/fork.c‎

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -484,20 +484,6 @@ static void mm_init_aio(struct mm_struct *mm)
484484
#endif
485485
}
486486

487-
int mm_init_cpumask(struct mm_struct *mm, struct mm_struct *oldmm)
488-
{
489-
#ifdef CONFIG_CPUMASK_OFFSTACK
490-
if (!alloc_cpumask_var(&mm->cpu_vm_mask_var, GFP_KERNEL))
491-
return -ENOMEM;
492-
493-
if (oldmm)
494-
cpumask_copy(mm_cpumask(mm), mm_cpumask(oldmm));
495-
else
496-
memset(mm_cpumask(mm), 0, cpumask_size());
497-
#endif
498-
return 0;
499-
}
500-
501487
static struct mm_struct * mm_init(struct mm_struct * mm, struct task_struct *p)
502488
{
503489
atomic_set(&mm->mm_users, 1);
@@ -538,17 +524,8 @@ struct mm_struct * mm_alloc(void)
538524
return NULL;
539525

540526
memset(mm, 0, sizeof(*mm));
541-
mm = mm_init(mm, current);
542-
if (!mm)
543-
return NULL;
544-
545-
if (mm_init_cpumask(mm, NULL)) {
546-
mm_free_pgd(mm);
547-
free_mm(mm);
548-
return NULL;
549-
}
550-
551-
return mm;
527+
mm_init_cpumask(mm);
528+
return mm_init(mm, current);
552529
}
553530

554531
/*
@@ -559,7 +536,6 @@ struct mm_struct * mm_alloc(void)
559536
void __mmdrop(struct mm_struct *mm)
560537
{
561538
BUG_ON(mm == &init_mm);
562-
free_cpumask_var(mm->cpu_vm_mask_var);
563539
mm_free_pgd(mm);
564540
destroy_context(mm);
565541
mmu_notifier_mm_destroy(mm);
@@ -753,6 +729,7 @@ struct mm_struct *dup_mm(struct task_struct *tsk)
753729
goto fail_nomem;
754730

755731
memcpy(mm, oldmm, sizeof(*mm));
732+
mm_init_cpumask(mm);
756733

757734
/* Initializing for Swap token stuff */
758735
mm->token_priority = 0;
@@ -765,9 +742,6 @@ struct mm_struct *dup_mm(struct task_struct *tsk)
765742
if (!mm_init(mm, tsk))
766743
goto fail_nomem;
767744

768-
if (mm_init_cpumask(mm, oldmm))
769-
goto fail_nocpumask;
770-
771745
if (init_new_context(tsk, mm))
772746
goto fail_nocontext;
773747

@@ -794,9 +768,6 @@ struct mm_struct *dup_mm(struct task_struct *tsk)
794768
return NULL;
795769

796770
fail_nocontext:
797-
free_cpumask_var(mm->cpu_vm_mask_var);
798-
799-
fail_nocpumask:
800771
/*
801772
* If init_new_context() failed, we cannot use mmput() to free the mm
802773
* because it calls destroy_context()
@@ -1591,6 +1562,13 @@ void __init proc_caches_init(void)
15911562
fs_cachep = kmem_cache_create("fs_cache",
15921563
sizeof(struct fs_struct), 0,
15931564
SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
1565+
/*
1566+
* FIXME! The "sizeof(struct mm_struct)" currently includes the
1567+
* whole struct cpumask for the OFFSTACK case. We could change
1568+
* this to *only* allocate as much of it as required by the
1569+
* maximum number of CPU's we can ever have. The cpumask_allocation
1570+
* is at the end of the structure, exactly for that reason.
1571+
*/
15941572
mm_cachep = kmem_cache_create("mm_struct",
15951573
sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
15961574
SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);

0 commit comments

Comments
 (0)