Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp2095579ybe; Tue, 3 Sep 2019 07:57:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqzCqOVGcac6uTWCC+DAQjQ1knfFaXYJ0x2kL45HoDU4TY4fqtpbYaFLO6UjduTuO81xMp+n X-Received: by 2002:a17:902:e48d:: with SMTP id cj13mr35292741plb.177.1567522660022; Tue, 03 Sep 2019 07:57:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567522660; cv=none; d=google.com; s=arc-20160816; b=w8bOEP1v5r8ZSuJN11Y2wIOHdc3OqDmg+ouLLyj2ApPTIER+cVBw+mMJlsq/TDpl87 st7ilKOeNsvB0Y53YkfKD5QodegF3DY2zIhwKn3lmzbDe1PqeCEIBQAybaOswkioHRF5 f9L3G+p5oc4pXJIZzAiCECGYXKrVGYPWzzuZgRE9pMgdIbLKgvN01id3upg+e94kW0ne Hy4d4RkCoO+dm2GH6ZD6O4TGHSt1BWBvUoYHfAJDb/cb3S6Wk1cdnsux4yQfi8FzrT4J m4KNU20UJfECaghsO9eqo4LaoeNxrCZDAtgKyKvle70rI+wizsGYIhS9LNgfu2W5D/1r C59w== 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=/Pi33Ab5H1THuUOlmFKCqF9xJSMT6s+LImLmp8toPnA=; b=ejoh4YFCnljztXCXP+ELD0DNQRJfKRZnZfThGP7ByH9iAqNLLEXuRKMt9qb/rn4bdO xrETkVtjyuUcUkaN05cy0yuzHH0gJpJ7e/TYVu6piZLo68vykq3i6M09jYCfIiixphvq BxIIElfxo5GMVPg/viyS2pLo3mNZYXqVSuQBtONvObPSqMVoq0G/OisGSdQVVsp9Z5vt 4L+h+sGYsgXZNMNVt6SJHdaaF4gutWFNQ++2mEpfY/h5H/RTiQAQoPpx248ARoQjsO9O dv1nk6Qsg/7PVE6w3wE+4Gf8NzvxnPdMcPm1fROLrLktjzAGPbuSica/MztfRxiqqoGt lPOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@axtens.net header.s=google header.b=TGcPmos1; 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 f15si15155567plm.292.2019.09.03.07.57.23; Tue, 03 Sep 2019 07:57:40 -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=TGcPmos1; 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 S1729687AbfICO4D (ORCPT + 99 others); Tue, 3 Sep 2019 10:56:03 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:42122 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727069AbfICO4C (ORCPT ); Tue, 3 Sep 2019 10:56:02 -0400 Received: by mail-pf1-f196.google.com with SMTP id w22so2494768pfi.9 for ; Tue, 03 Sep 2019 07:56:01 -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=/Pi33Ab5H1THuUOlmFKCqF9xJSMT6s+LImLmp8toPnA=; b=TGcPmos1Y0BnQsCiS8+ov03JSSxJfhQd7iQLyNg+WP8u0M+vi9O6utfre1w//CYiSN zlC86cvXjt0eacPKyxaTSO7rwcqc1E3Q8+jlLPjEUIYotJSNdqFzJVOc2IzoXq8CwxpV /WR4qATOVB2CFZzGc3/5lEGaCbhFocxYEkbdU= 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=/Pi33Ab5H1THuUOlmFKCqF9xJSMT6s+LImLmp8toPnA=; b=dUvsfO++3oJuLvOvn9jABFxmejkFU2lrUo3FFHMWZFdU6GSFcKU0G6QsoD+31XHML1 yX+1gF18XQ8aaOuN3+8fERQntbcL1cMmQh5ZMJEhMAPtbPD4PmeDRZNCPaQyFGKALtcU zKzp2g+my5CP+vhoddAqTXnIpnpfmdz5R5oio1rWemkHkxvqe60IqtvJhxYnUuqNgzE6 QNIVWTNXZBbTWynXFGVhas1S/S7N4SYiH/9QJb0QedCgW7hptxagfk0MxBdS1u/3GmHp 82t9BdbAhXgQl/no8ZEF99xuA35XxR3V/Z/XFQjLzzD9OLmOlf0zNzsKIAobCp1/Geak zzZA== X-Gm-Message-State: APjAAAXlDXW6yVnfhBiAhS2Aun55CB931S4cY0UEhgdi5bcUhH3u0C0e hKD6Px0qZRVRFoTGCnl2P7crfQ== X-Received: by 2002:a62:8749:: with SMTP id i70mr8363618pfe.12.1567522561420; Tue, 03 Sep 2019 07:56:01 -0700 (PDT) Received: from localhost (ppp167-251-205.static.internode.on.net. [59.167.251.205]) by smtp.gmail.com with ESMTPSA id h12sm18490529pgr.8.2019.09.03.07.55.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2019 07:56:00 -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, christophe.leroy@c-s.fr Cc: linuxppc-dev@lists.ozlabs.org, gor@linux.ibm.com, Daniel Axtens Subject: [PATCH v7 4/5] x86/kasan: support KASAN_VMALLOC Date: Wed, 4 Sep 2019 00:55:35 +1000 Message-Id: <20190903145536.3390-5-dja@axtens.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190903145536.3390-1-dja@axtens.net> References: <20190903145536.3390-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 --- v5: fix some checkpatch CHECK warnings. There are some that remain around lines ending with '(': I have not changed these because it's consistent with the rest of the file and it's not easy to see how to fix it without creating an overlong line or lots of temporary variables. v2: move from faulting in shadow pgds to prepopulating --- arch/x86/Kconfig | 1 + arch/x86/mm/kasan_init_64.c | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 2502f7f60c9c..300b4766ccfa 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..8f00f462709e 100644 --- a/arch/x86/mm/kasan_init_64.c +++ b/arch/x86/mm/kasan_init_64.c @@ -245,6 +245,51 @@ 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 +397,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