From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.5 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 418A7FA3728 for ; Thu, 17 Oct 2019 01:25:35 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E511E2082C for ; Thu, 17 Oct 2019 01:25:34 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=axtens.net header.i=@axtens.net header.b="EK4oU0BZ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E511E2082C Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=axtens.net Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9975D8E000A; Wed, 16 Oct 2019 21:25:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9473E8E0001; Wed, 16 Oct 2019 21:25:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80E5C8E000A; Wed, 16 Oct 2019 21:25:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0101.hostedemail.com [216.40.44.101]) by kanga.kvack.org (Postfix) with ESMTP id 619A88E0001 for ; Wed, 16 Oct 2019 21:25:34 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with SMTP id 07DD2583F for ; Thu, 17 Oct 2019 01:25:34 +0000 (UTC) X-FDA: 76051534188.04.rate74_4da0d9a05ea32 X-HE-Tag: rate74_4da0d9a05ea32 X-Filterd-Recvd-Size: 6739 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Thu, 17 Oct 2019 01:25:33 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id e15so280773pgu.13 for ; Wed, 16 Oct 2019 18:25:33 -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=isMm51VtK/oRU6lGsD9w6jqnjg5iuBGo4uf1ljxv9U4=; b=EK4oU0BZG1V6UaQsnBNZnjXP8g8KxL/hhWkVyT8+14E8rqLSg3p2oglexNRbLfDRyD uEfvJrRT1xB9OLiujVi/H6ZYlpgtsIXX7v5bz1AeiKn7Cf5lHwzOGKals+WHChh0ctti rZVxpfdH6wI8CAMhz4odtR5tkZaQNKlfoegVk= 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=isMm51VtK/oRU6lGsD9w6jqnjg5iuBGo4uf1ljxv9U4=; b=DdjMeGdhnKYhcrARErqXrrUmlrcwDai7nghu2TI5IjXes0y9fEzUaBtdhwuTakAh1M 2nXQscCswcRnrAxMeqTeabSK1PM+lgoXnKlRE5fmOYVuBVdGYVDKK0guEoVmB3LjGYB+ jLO66DJBEOas+X8EiMA4i03vJus65F2Bdpn9T3CsFWj4Tt0ofmT7/si4yM8Wgun2H8e0 rhYFT8xek3DUYFtVQ8MO/k7x1wPG3JxtiaRGHatHWad5vWXV4Mo0SyuE4Mo/RBliW12i cIbYGeQthsCY/OHXx5YpbumspOaqs8OFn1kp7R3G3217CGSVycL9PKYu13+QkIHGYyJY fz0A== X-Gm-Message-State: APjAAAUlrxnDhuFcVJS7g9YtZuO3CANWx/8GMg0wBSHnTA1Jq9bpqmQR ElVFJc+vA39h1Uwc6e+uq5Jx4w== X-Google-Smtp-Source: APXvYqz8KIe0XQW6K5vnFa8nQQJeKdiESpPZJX7VNQmEzPXTQBBRxJ6FkEGke8hAC3oYssSRd5RjMQ== X-Received: by 2002:a63:d916:: with SMTP id r22mr1243029pgg.46.1571275532214; Wed, 16 Oct 2019 18:25:32 -0700 (PDT) Received: from localhost (ppp167-251-205.static.internode.on.net. [59.167.251.205]) by smtp.gmail.com with ESMTPSA id f89sm391422pje.20.2019.10.16.18.25.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Oct 2019 18:25:31 -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 v9 4/5] x86/kasan: support KASAN_VMALLOC Date: Thu, 17 Oct 2019 12:25:05 +1100 Message-Id: <20191017012506.28503-5-dja@axtens.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191017012506.28503-1-dja@axtens.net> References: <20191017012506.28503-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 arch/x86/Kconfig arch/x86/Kconfig index abe822d52167..92f5d5d5c78a 100644 --- arch/x86/Kconfig +++ arch/x86/Kconfig @@ -135,6 +135,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 arch/x86/mm/kasan_init_64.c arch/x86/mm/kasan_init_64.c index 296da58f3013..8f00f462709e 100644 --- arch/x86/mm/kasan_init_64.c +++ arch/x86/mm/kasan_init_64.c @@ -245,6 +245,51 @@ static void __init kasan_map_early_shadow(pgd_t *pgd= ) } while (pgd++, addr =3D next, addr !=3D end); } =20 +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 =3D p4d_offset(pgd, addr); + do { + next =3D p4d_addr_end(addr, end); + + if (p4d_none(*p4d)) { + p =3D early_alloc(PAGE_SIZE, nid, true); + p4d_populate(&init_mm, p4d, p); + } + } while (p4d++, addr =3D next, addr !=3D end); +} + +static void __init kasan_shallow_populate_pgds(void *start, void *end) +{ + unsigned long addr, next; + pgd_t *pgd; + void *p; + int nid =3D early_pfn_to_nid((unsigned long)start); + + addr =3D (unsigned long)start; + pgd =3D pgd_offset_k(addr); + do { + next =3D pgd_addr_end(addr, (unsigned long)end); + + if (pgd_none(*pgd)) { + p =3D 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 =3D next, addr !=3D (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 =3D (void *)round_up( (unsigned long)shadow_cpu_entry_end, PAGE_SIZE); =20 + /* + * 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 =20 kasan_populate_shadow((unsigned long)shadow_cpu_entry_begin, (unsigned long)shadow_cpu_entry_end, 0); --=20 2.20.1