Commit 0d39e266 authored by Andrey Ryabinin's avatar Andrey Ryabinin Committed by Thomas Gleixner

x86/kasan: Panic if there is not enough memory to boot

Currently KASAN doesn't panic in case it don't have enough memory
to boot. Instead, it crashes in some random place:

 kernel BUG at arch/x86/mm/physaddr.c:27!

 RIP: 0010:__phys_addr+0x268/0x276
 Call Trace:
  kasan_populate_shadow+0x3f2/0x497
  kasan_init+0x12e/0x2b2
  setup_arch+0x2825/0x2a2c
  start_kernel+0xc8/0x15f4
  x86_64_start_reservations+0x2a/0x2c
  x86_64_start_kernel+0x72/0x75
  secondary_startup_64+0xa5/0xb0

Use memblock_virt_alloc_try_nid() for allocations without failure
fallback. It will panic with an out of memory message.
Reported-by: default avatarkernel test robot <xiaolong.ye@intel.com>
Signed-off-by: default avatarAndrey Ryabinin <aryabinin@virtuozzo.com>
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Acked-by: default avatarDmitry Vyukov <dvyukov@google.com>
Cc: kasan-dev@googlegroups.com
Cc: Alexander Potapenko <glider@google.com>
Cc: lkp@01.org
Link: https://lkml.kernel.org/r/20180110153602.18919-1-aryabinin@virtuozzo.com
parent b8b9ce4b
...@@ -21,10 +21,14 @@ extern struct range pfn_mapped[E820_MAX_ENTRIES]; ...@@ -21,10 +21,14 @@ extern struct range pfn_mapped[E820_MAX_ENTRIES];
static p4d_t tmp_p4d_table[PTRS_PER_P4D] __initdata __aligned(PAGE_SIZE); static p4d_t tmp_p4d_table[PTRS_PER_P4D] __initdata __aligned(PAGE_SIZE);
static __init void *early_alloc(size_t size, int nid) static __init void *early_alloc(size_t size, int nid, bool panic)
{ {
return memblock_virt_alloc_try_nid_nopanic(size, size, if (panic)
__pa(MAX_DMA_ADDRESS), BOOTMEM_ALLOC_ACCESSIBLE, nid); return memblock_virt_alloc_try_nid(size, size,
__pa(MAX_DMA_ADDRESS), BOOTMEM_ALLOC_ACCESSIBLE, nid);
else
return memblock_virt_alloc_try_nid_nopanic(size, size,
__pa(MAX_DMA_ADDRESS), BOOTMEM_ALLOC_ACCESSIBLE, nid);
} }
static void __init kasan_populate_pmd(pmd_t *pmd, unsigned long addr, static void __init kasan_populate_pmd(pmd_t *pmd, unsigned long addr,
...@@ -38,14 +42,14 @@ static void __init kasan_populate_pmd(pmd_t *pmd, unsigned long addr, ...@@ -38,14 +42,14 @@ static void __init kasan_populate_pmd(pmd_t *pmd, unsigned long addr,
if (boot_cpu_has(X86_FEATURE_PSE) && if (boot_cpu_has(X86_FEATURE_PSE) &&
((end - addr) == PMD_SIZE) && ((end - addr) == PMD_SIZE) &&
IS_ALIGNED(addr, PMD_SIZE)) { IS_ALIGNED(addr, PMD_SIZE)) {
p = early_alloc(PMD_SIZE, nid); p = early_alloc(PMD_SIZE, nid, false);
if (p && pmd_set_huge(pmd, __pa(p), PAGE_KERNEL)) if (p && pmd_set_huge(pmd, __pa(p), PAGE_KERNEL))
return; return;
else if (p) else if (p)
memblock_free(__pa(p), PMD_SIZE); memblock_free(__pa(p), PMD_SIZE);
} }
p = early_alloc(PAGE_SIZE, nid); p = early_alloc(PAGE_SIZE, nid, true);
pmd_populate_kernel(&init_mm, pmd, p); pmd_populate_kernel(&init_mm, pmd, p);
} }
...@@ -57,7 +61,7 @@ static void __init kasan_populate_pmd(pmd_t *pmd, unsigned long addr, ...@@ -57,7 +61,7 @@ static void __init kasan_populate_pmd(pmd_t *pmd, unsigned long addr,
if (!pte_none(*pte)) if (!pte_none(*pte))
continue; continue;
p = early_alloc(PAGE_SIZE, nid); p = early_alloc(PAGE_SIZE, nid, true);
entry = pfn_pte(PFN_DOWN(__pa(p)), PAGE_KERNEL); entry = pfn_pte(PFN_DOWN(__pa(p)), PAGE_KERNEL);
set_pte_at(&init_mm, addr, pte, entry); set_pte_at(&init_mm, addr, pte, entry);
} while (pte++, addr += PAGE_SIZE, addr != end); } while (pte++, addr += PAGE_SIZE, addr != end);
...@@ -75,14 +79,14 @@ static void __init kasan_populate_pud(pud_t *pud, unsigned long addr, ...@@ -75,14 +79,14 @@ static void __init kasan_populate_pud(pud_t *pud, unsigned long addr,
if (boot_cpu_has(X86_FEATURE_GBPAGES) && if (boot_cpu_has(X86_FEATURE_GBPAGES) &&
((end - addr) == PUD_SIZE) && ((end - addr) == PUD_SIZE) &&
IS_ALIGNED(addr, PUD_SIZE)) { IS_ALIGNED(addr, PUD_SIZE)) {
p = early_alloc(PUD_SIZE, nid); p = early_alloc(PUD_SIZE, nid, false);
if (p && pud_set_huge(pud, __pa(p), PAGE_KERNEL)) if (p && pud_set_huge(pud, __pa(p), PAGE_KERNEL))
return; return;
else if (p) else if (p)
memblock_free(__pa(p), PUD_SIZE); memblock_free(__pa(p), PUD_SIZE);
} }
p = early_alloc(PAGE_SIZE, nid); p = early_alloc(PAGE_SIZE, nid, true);
pud_populate(&init_mm, pud, p); pud_populate(&init_mm, pud, p);
} }
...@@ -101,7 +105,7 @@ static void __init kasan_populate_p4d(p4d_t *p4d, unsigned long addr, ...@@ -101,7 +105,7 @@ static void __init kasan_populate_p4d(p4d_t *p4d, unsigned long addr,
unsigned long next; unsigned long next;
if (p4d_none(*p4d)) { if (p4d_none(*p4d)) {
void *p = early_alloc(PAGE_SIZE, nid); void *p = early_alloc(PAGE_SIZE, nid, true);
p4d_populate(&init_mm, p4d, p); p4d_populate(&init_mm, p4d, p);
} }
...@@ -122,7 +126,7 @@ static void __init kasan_populate_pgd(pgd_t *pgd, unsigned long addr, ...@@ -122,7 +126,7 @@ static void __init kasan_populate_pgd(pgd_t *pgd, unsigned long addr,
unsigned long next; unsigned long next;
if (pgd_none(*pgd)) { if (pgd_none(*pgd)) {
p = early_alloc(PAGE_SIZE, nid); p = early_alloc(PAGE_SIZE, nid, true);
pgd_populate(&init_mm, pgd, p); pgd_populate(&init_mm, pgd, p);
} }
......
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