Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1455716ybl; Wed, 14 Aug 2019 17:43:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqyjkO1efhcvXGYSrRgXCZ/J44t479DoFIW9yTMHh7nKHCjb9tqlg5XASUi9jFnJtG3tWZIa X-Received: by 2002:a17:90a:d3cf:: with SMTP id d15mr585429pjw.34.1565829789028; Wed, 14 Aug 2019 17:43:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565829789; cv=none; d=google.com; s=arc-20160816; b=xEvwSsG2Cg8gRSVpYjzZPKyiph0DmGQtr12jU7cp4XPt250ECubR/iXmBmsfCGRzN3 cap/d/ry3pXyV5ADJ9eYHmz8bp5zN3/gXTQbARLFNoVGzA/iwETMKhImTE4DYSsB3/kT 8BtVOmWqeSttBwJEOFvlwhBplW0J1L3ff+X/YOOJevCsGwV/ReX3LcguHuUBKhYVhUqL +y+w8QuC5UibeGBOek9+RW3pO+EFoq8GZ0xsq17QFlZajI5b/3zJ7AxzIqIlJLOmyixD 5UcM7E3pqB7Q/r7mPCI+aUoCqEBesO2zrPCCHBSAnR0Fyld4cSrwdNS04/dn2tXaSarq KhbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=UahEbtdXwZNAU+KwZPeJAGjgE2lYz0Dt6XczqvKwLUQ=; b=Xnt2ZSF4VOsskKaK1rr4o41fd5aj2GovRDHAMYvjFjn19coXN8y83Pr/o/LOJhvM1+ Qyz87rAs/9X/Jy4euVW6E0XVo6g9LAhx1UAwDzEjBIK8Ca8PQL/h/sBng4p/3ljiny2p 2R9Oes1ACtRXsTBqvAKtr+n7kmREXT1E7+Djh+LOyTJdn40HXnQj3uEVQ18orU3yVD2+ 6/HVC+OZYSeBBmtK3AZTPwf2rmUsPAhqk47OzROoD7iMZ7gNvIfQl687qE/oDi8FjJfu BEtK3N4wg5de3s9ppkxrFPudpTV+IFv46r1VvFk1Z3injc3VN2+cLqhobrzvWELEqs5Q uQgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@axtens.net header.s=google header.b=EzjEg35m; 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 g24si795043pgh.416.2019.08.14.17.42.25; Wed, 14 Aug 2019 17:43:09 -0700 (PDT) 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; dkim=pass header.i=@axtens.net header.s=google header.b=EzjEg35m; 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 S1729259AbfHOARJ (ORCPT + 99 others); Wed, 14 Aug 2019 20:17:09 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:40517 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729093AbfHOARI (ORCPT ); Wed, 14 Aug 2019 20:17:08 -0400 Received: by mail-pf1-f196.google.com with SMTP id w16so363607pfn.7 for ; Wed, 14 Aug 2019 17:17:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UahEbtdXwZNAU+KwZPeJAGjgE2lYz0Dt6XczqvKwLUQ=; b=EzjEg35mc1bbHgqD1Ww7viar/jC2gY1ttaK6kS4nEhAVQAc9DPpjV5s9r/dHqVkDvP 1aJjS12y4rJXR3CnBHGnF9uwmU85X95N5a/RVv5vtVMrGT/jXYcs+Npf3JRUlkGUst7Z Y16NXi5EAZRKf1theyPlNghDSgylNqksp2oaU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UahEbtdXwZNAU+KwZPeJAGjgE2lYz0Dt6XczqvKwLUQ=; b=rIk1x0rgDSKc6QH0H27oPg1D26eHvjMecKY8gHuk29C9oT46KOY3PmNuQh9ItxYBBl XY2j7/VG21u8Fw0WahP3JAmRfGkDPEjcin6fmVieJSWP0n04Gc3dPLtAZ90LSiKos/G2 wz39APpB841PtSAiYnf8c7xcQ80jhk/wFkfNUtHbW5Kq8wXb7iK2z9xfwX6WXekzxnLP b2rUgiuY03F58JLiuPalLuahHtFcR/5bL+N2UepUp17wZzVn3sXDRNUsV00eQ60r/2KT /dgHv6WSwzJdzoud++IIOQHAoYSls8NhdaTlIALrlRchHbpEMf1csfZwjMEbzlErQs2b xalg== X-Gm-Message-State: APjAAAVlxeW0aQQPRtEiLq1IYamN1HSFmLx4XVlWXBUJLI1NzZJtIbBM SEPy2s+nA9LkjAMhd3ovJM759A== X-Received: by 2002:aa7:9609:: with SMTP id q9mr2568209pfg.232.1565828228148; Wed, 14 Aug 2019 17:17:08 -0700 (PDT) Received: from localhost (ppp167-251-205.static.internode.on.net. [59.167.251.205]) by smtp.gmail.com with ESMTPSA id g11sm821630pgu.11.2019.08.14.17.17.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2019 17:17:07 -0700 (PDT) From: Daniel Axtens To: kasan-dev@googlegroups.com, linux-mm@kvack.org, x86@kernel.org, aryabinin@virtuozzo.com, glider@google.com, luto@kernel.org, linux-kernel@vger.kernel.org, mark.rutland@arm.com, dvyukov@google.com Cc: linuxppc-dev@lists.ozlabs.org, gor@linux.ibm.com, Daniel Axtens Subject: [PATCH v4 3/3] x86/kasan: support KASAN_VMALLOC Date: Thu, 15 Aug 2019 10:16:36 +1000 Message-Id: <20190815001636.12235-4-dja@axtens.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190815001636.12235-1-dja@axtens.net> References: <20190815001636.12235-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In the case where KASAN directly allocates memory to back vmalloc space, don't map the early shadow page over it. We prepopulate pgds/p4ds for the range that would otherwise be empty. This is required to get it synced to hardware on boot, allowing the lower levels of the page tables to be filled dynamically. Acked-by: Dmitry Vyukov Signed-off-by: Daniel Axtens --- v2: move from faulting in shadow pgds to prepopulating --- arch/x86/Kconfig | 1 + arch/x86/mm/kasan_init_64.c | 61 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 222855cc0158..40562cc3771f 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -134,6 +134,7 @@ config X86 select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if X86_64 + select HAVE_ARCH_KASAN_VMALLOC if X86_64 select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT diff --git a/arch/x86/mm/kasan_init_64.c b/arch/x86/mm/kasan_init_64.c index 296da58f3013..2f57c4ddff61 100644 --- a/arch/x86/mm/kasan_init_64.c +++ b/arch/x86/mm/kasan_init_64.c @@ -245,6 +245,52 @@ static void __init kasan_map_early_shadow(pgd_t *pgd) } while (pgd++, addr = next, addr != end); } +static void __init kasan_shallow_populate_p4ds(pgd_t *pgd, + unsigned long addr, + unsigned long end, + int nid) +{ + p4d_t *p4d; + unsigned long next; + void *p; + + p4d = p4d_offset(pgd, addr); + do { + next = p4d_addr_end(addr, end); + + if (p4d_none(*p4d)) { + p = early_alloc(PAGE_SIZE, nid, true); + p4d_populate(&init_mm, p4d, p); + } + } while (p4d++, addr = next, addr != end); +} + +static void __init kasan_shallow_populate_pgds(void *start, void *end) +{ + unsigned long addr, next; + pgd_t *pgd; + void *p; + int nid = early_pfn_to_nid((unsigned long)start); + + addr = (unsigned long)start; + pgd = pgd_offset_k(addr); + do { + next = pgd_addr_end(addr, (unsigned long)end); + + if (pgd_none(*pgd)) { + p = early_alloc(PAGE_SIZE, nid, true); + pgd_populate(&init_mm, pgd, p); + } + + /* + * we need to populate p4ds to be synced when running in + * four level mode - see sync_global_pgds_l4() + */ + kasan_shallow_populate_p4ds(pgd, addr, next, nid); + } while (pgd++, addr = next, addr != (unsigned long)end); +} + + #ifdef CONFIG_KASAN_INLINE static int kasan_die_handler(struct notifier_block *self, unsigned long val, @@ -352,9 +398,24 @@ void __init kasan_init(void) shadow_cpu_entry_end = (void *)round_up( (unsigned long)shadow_cpu_entry_end, PAGE_SIZE); + /* + * If we're in full vmalloc mode, don't back vmalloc space with early + * shadow pages. Instead, prepopulate pgds/p4ds so they are synced to + * the global table and we can populate the lower levels on demand. + */ +#ifdef CONFIG_KASAN_VMALLOC + kasan_shallow_populate_pgds( + kasan_mem_to_shadow((void *)PAGE_OFFSET + MAXMEM), + kasan_mem_to_shadow((void *)VMALLOC_END)); + + kasan_populate_early_shadow( + kasan_mem_to_shadow((void *)VMALLOC_END + 1), + shadow_cpu_entry_begin); +#else kasan_populate_early_shadow( kasan_mem_to_shadow((void *)PAGE_OFFSET + MAXMEM), shadow_cpu_entry_begin); +#endif kasan_populate_shadow((unsigned long)shadow_cpu_entry_begin, (unsigned long)shadow_cpu_entry_end, 0); -- 2.20.1