Received: by 10.223.176.46 with SMTP id f43csp2668844wra; Mon, 22 Jan 2018 01:14:33 -0800 (PST) X-Google-Smtp-Source: AH8x227Ai3g6jjD/rnYPIAyZF5ExwruhBPO6gUjamr6mRaF5Y8ZHY6oNiD/M2++JzVMK+2MNPhKS X-Received: by 10.99.126.73 with SMTP id o9mr7105460pgn.429.1516612473635; Mon, 22 Jan 2018 01:14:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516612473; cv=none; d=google.com; s=arc-20160816; b=At9AyCKXt3lYGtmhk0Ebn5tqi44Aptxqq3qzhVkw+2SgB+FF8vPVeGZpXghAea60L7 kb9es72JCR8XNDWHnoaKBuiiYIEBHTuZI+UfsW5SC0hnWmU38yJcPKEPQ2UvJ3WPWB2M E0zatfHjeQpSnYMKVM9NfztxWx3UQG+H9tN/BSKfaeKhosdumEzXcb477U0E+j+R8yqo HVNu4Di0cSE37hseyTzm1usl55UsgPYBjzFn0tczJ7XZNqBHhcjiZC3855K7obSTgT+r oiEqFakW2qw05hcVVq6vwThdCZdNXYvlV8boOXImDRhZh+Y/qZEd0KxsiSTgjmyK/ftC rkpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=cgATOsOhPYX8jT06THP0IStubpCFlbpYRMGN7m4W8ZY=; b=eH0OM8ttieY74BpOZUR24+DjYr0kH+IgDHJgteWAgaK2A3byGYeK5U9lbx41/fUYlC 4eoYXOHizm738fPEcX5C1cbcZPyInPgahX/7eJ76CuTShYjfs1eXpbA20WKan8mDBq7M Y2edjPIxO6jiD9hlCf4bEMx1xQLtBPO/bGRgqRYKGbYL6OOm/bslZubTiuxVbB9penNk B51WbW/PthkdMIEydNTRjxJsIGq2n8Vb/skJ2mT3Ta8S5WXXjYAl9NplTvXNj5A/NJqZ MVbMmcM+BF4MU9Ejg6bKj0HSJiim6IQ57899aPtvNOgyCmQ1R+uqkE4jwY316fAkEEoq k1Bw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s135si13521944pgc.165.2018.01.22.01.14.19; Mon, 22 Jan 2018 01:14:33 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752883AbeAVIva (ORCPT + 99 others); Mon, 22 Jan 2018 03:51:30 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:33280 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752530AbeAVIv1 (ORCPT ); Mon, 22 Jan 2018 03:51:27 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id C430BEE6; Mon, 22 Jan 2018 08:51:26 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, kernel test robot , Andrey Ryabinin , Thomas Gleixner , Dmitry Vyukov , kasan-dev@googlegroups.com, Alexander Potapenko , lkp@01.org Subject: [PATCH 4.14 31/89] x86/kasan: Panic if there is not enough memory to boot Date: Mon, 22 Jan 2018 09:45:11 +0100 Message-Id: <20180122083957.744338997@linuxfoundation.org> X-Mailer: git-send-email 2.16.0 In-Reply-To: <20180122083954.683903493@linuxfoundation.org> References: <20180122083954.683903493@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Andrey Ryabinin commit 0d39e2669d7b0fefd2d8f9e7868ae669b364d9ba upstream. 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: kernel test robot Signed-off-by: Andrey Ryabinin Signed-off-by: Thomas Gleixner Acked-by: Dmitry Vyukov Cc: kasan-dev@googlegroups.com Cc: Alexander Potapenko Cc: lkp@01.org Link: https://lkml.kernel.org/r/20180110153602.18919-1-aryabinin@virtuozzo.com Signed-off-by: Greg Kroah-Hartman --- arch/x86/mm/kasan_init_64.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) --- a/arch/x86/mm/kasan_init_64.c +++ b/arch/x86/mm/kasan_init_64.c @@ -21,10 +21,14 @@ extern struct range pfn_mapped[E820_MAX_ 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, - __pa(MAX_DMA_ADDRESS), BOOTMEM_ALLOC_ACCESSIBLE, nid); + if (panic) + 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, @@ -38,14 +42,14 @@ static void __init kasan_populate_pmd(pm if (boot_cpu_has(X86_FEATURE_PSE) && ((end - 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)) return; else if (p) 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); } @@ -57,7 +61,7 @@ static void __init kasan_populate_pmd(pm if (!pte_none(*pte)) continue; - p = early_alloc(PAGE_SIZE, nid); + p = early_alloc(PAGE_SIZE, nid, true); entry = pfn_pte(PFN_DOWN(__pa(p)), PAGE_KERNEL); set_pte_at(&init_mm, addr, pte, entry); } while (pte++, addr += PAGE_SIZE, addr != end); @@ -75,14 +79,14 @@ static void __init kasan_populate_pud(pu if (boot_cpu_has(X86_FEATURE_GBPAGES) && ((end - 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)) return; else if (p) 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); } @@ -101,7 +105,7 @@ static void __init kasan_populate_p4d(p4 unsigned long next; 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); } @@ -122,7 +126,7 @@ static void __init kasan_populate_pgd(pg unsigned long next; if (pgd_none(*pgd)) { - p = early_alloc(PAGE_SIZE, nid); + p = early_alloc(PAGE_SIZE, nid, true); pgd_populate(&init_mm, pgd, p); }