From patchwork Fri Aug 26 15:07:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956182 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 707E4ECAAD5 for ; Fri, 26 Aug 2022 15:08:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0957B940010; Fri, 26 Aug 2022 11:08:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 043F1940007; Fri, 26 Aug 2022 11:08:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB1CD940010; Fri, 26 Aug 2022 11:08:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C7963940007 for ; Fri, 26 Aug 2022 11:08:39 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id AA57C40EF0 for ; Fri, 26 Aug 2022 15:08:39 +0000 (UTC) X-FDA: 79842075558.27.5D4EDBC Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf25.hostedemail.com (Postfix) with ESMTP id 6B664A0026 for ; Fri, 26 Aug 2022 15:08:39 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id sd6-20020a1709076e0600b0073315809fb5so720191ejc.10 for ; Fri, 26 Aug 2022 08:08:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=lZyR+/TbY71+6irDHrQVY8i8lV0LAE0DR0x5Sx//ziQ=; b=Ofrs8PCMHX+W0ceEYWnDzVx1uzp/bWz8GLaWKdo+wAqOx3sPZu6pI92iq0IULzGOQ8 VJ09OsHIUk+49s1paxcvdc/dQgoZURGCS4X6UxVQKGmyCn/fzomHAQt3pDibyyvopGw+ A204SQq3udS4olps/yoQNs/HdBDLMH/XYtFo5t7S3WbS0/Mi6jEQiCtdTGIheelU/NKQ ODyynPxQ+JC42TN6qhou33wAGXEzHzmJj/qEXAgKq4QXYccBrT+Ih7NpvsAuafykD+5Y +aYVMqZk7TNUIRRwqC7Wdqslm1Z8gXmVvziP4uuhA49TmwX3KTg2b8vw2WCFS2T1Wo1D S3QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=lZyR+/TbY71+6irDHrQVY8i8lV0LAE0DR0x5Sx//ziQ=; b=DVKwQ5v9bBuWGlSjdfBuRpkD1UQvHUXteOmbL0f2hmvKdsBTqf1ggSdZIQ4ofA3w8q AfxtDy34gNcEkfi0r0YWbZfz8QePbEthnkD9h7ymDBTEKGRh5zabPJFQeiWAKxp+it88 d9TFWmCMSaPAX1oqY6R+rBmJsL10vMGyMqgGM3JilBaPw9TCN+5ttZCgVX1mW5/S8CXz lxE4UE6UqpgnJ/hFzjKkXcw0YPSJlNtoG5E95KN6Mzju1HKy/jyf2odDoJnQY87lwJcu bB6van+ZGNqfOt41V0misQv0Af3NZuyKd6W2wHbvKmwuLU3aEqpvgqJ6Sdjj+8qx8uid +VNw== X-Gm-Message-State: ACgBeo2Jvv0xELJnh9UCKqr9BUv6ryjrZx3e/TkxH7Iuzi5DcobTWtGk wEAew2G2IM+AQAHxVPkCztH+VxbrlUA= X-Google-Smtp-Source: AA6agR6DZmZvABSNd8NirMJa2iJguxZBNB8hGvX5VWCga9N1u2wq6DvLGXRRSlQxms71XTCN2etlpU9v9zU= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:906:f88f:b0:731:463d:4b15 with SMTP id lg15-20020a170906f88f00b00731463d4b15mr5703796ejb.299.1661526518263; Fri, 26 Aug 2022 08:08:38 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:32 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-10-glider@google.com> Subject: [PATCH v5 09/44] x86: kmsan: pgtable: reduce vmalloc space From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , Andrew Morton , Andrey Konovalov , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Christoph Hellwig , Christoph Lameter , David Rientjes , Dmitry Vyukov , Eric Dumazet , Greg Kroah-Hartman , Herbert Xu , Ilya Leoshkevich , Ingo Molnar , Jens Axboe , Joonsoo Kim , Kees Cook , Marco Elver , Mark Rutland , Matthew Wilcox , "Michael S. Tsirkin" , Pekka Enberg , Peter Zijlstra , Petr Mladek , Steven Rostedt , Thomas Gleixner , Vasily Gorbik , Vegard Nossum , Vlastimil Babka , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526519; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=lZyR+/TbY71+6irDHrQVY8i8lV0LAE0DR0x5Sx//ziQ=; b=A7FvIGxMpJ85X/LlFkz5nsCKxMyE6A7JH0RIluDlQ31AvU0oJPNl/ywfYlFRQ8Gf0D3gAo OoduAu7tlN0y8Hha7PP3o/MPBVbkG589Tmj+eaOj+z7Ar6LGKlJGkydbdMKxa/sSYpYVX2 AEroYeHeufWAWBpr9+RDFIIaS2BEMFg= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Ofrs8PCM; spf=pass (imf25.hostedemail.com: domain of 39uEIYwYKCPsjolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=39uEIYwYKCPsjolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526519; a=rsa-sha256; cv=none; b=mz2iXcZNHw4xyd6bOBero2cy/wYCsk79p6UCQJDDP0tair83eDHE6bsAHqu3eHmL34lWnd JvJin3gkpcffi4SqV+O3jh2ErwQ3oRGtmZOCgOEhpD98UnUQTTwz2W/o2vYoPe1xDP1HLX Xn1bRfqFZwNpFn9N+56yD1Zr6rtjw5o= X-Rspam-User: Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Ofrs8PCM; spf=pass (imf25.hostedemail.com: domain of 39uEIYwYKCPsjolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=39uEIYwYKCPsjolghujrrjoh.frpolqx0-ppnydfn.ruj@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam05 X-Stat-Signature: 4tue1w1hoomoridmj6eaziggixwpydgi X-Rspamd-Queue-Id: 6B664A0026 X-HE-Tag: 1661526519-846051 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: KMSAN is going to use 3/4 of existing vmalloc space to hold the metadata, therefore we lower VMALLOC_END to make sure vmalloc() doesn't allocate past the first 1/4. Signed-off-by: Alexander Potapenko --- v2: -- added x86: to the title v5: -- add comment for VMEMORY_END Link: https://linux-review.googlesource.com/id/I9d8b7f0a88a639f1263bc693cbd5c136626f7efd --- arch/x86/include/asm/pgtable_64_types.h | 47 ++++++++++++++++++++++++- arch/x86/mm/init_64.c | 2 +- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/pgtable_64_types.h b/arch/x86/include/asm/pgtable_64_types.h index 70e360a2e5fb7..04f36063ad546 100644 --- a/arch/x86/include/asm/pgtable_64_types.h +++ b/arch/x86/include/asm/pgtable_64_types.h @@ -139,7 +139,52 @@ extern unsigned int ptrs_per_p4d; # define VMEMMAP_START __VMEMMAP_BASE_L4 #endif /* CONFIG_DYNAMIC_MEMORY_LAYOUT */ -#define VMALLOC_END (VMALLOC_START + (VMALLOC_SIZE_TB << 40) - 1) +/* + * End of the region for which vmalloc page tables are pre-allocated. + * For non-KMSAN builds, this is the same as VMALLOC_END. + * For KMSAN builds, VMALLOC_START..VMEMORY_END is 4 times bigger than + * VMALLOC_START..VMALLOC_END (see below). + */ +#define VMEMORY_END (VMALLOC_START + (VMALLOC_SIZE_TB << 40) - 1) + +#ifndef CONFIG_KMSAN +#define VMALLOC_END VMEMORY_END +#else +/* + * In KMSAN builds vmalloc area is four times smaller, and the remaining 3/4 + * are used to keep the metadata for virtual pages. The memory formerly + * belonging to vmalloc area is now laid out as follows: + * + * 1st quarter: VMALLOC_START to VMALLOC_END - new vmalloc area + * 2nd quarter: KMSAN_VMALLOC_SHADOW_START to + * VMALLOC_END+KMSAN_VMALLOC_SHADOW_OFFSET - vmalloc area shadow + * 3rd quarter: KMSAN_VMALLOC_ORIGIN_START to + * VMALLOC_END+KMSAN_VMALLOC_ORIGIN_OFFSET - vmalloc area origins + * 4th quarter: KMSAN_MODULES_SHADOW_START to KMSAN_MODULES_ORIGIN_START + * - shadow for modules, + * KMSAN_MODULES_ORIGIN_START to + * KMSAN_MODULES_ORIGIN_START + MODULES_LEN - origins for modules. + */ +#define VMALLOC_QUARTER_SIZE ((VMALLOC_SIZE_TB << 40) >> 2) +#define VMALLOC_END (VMALLOC_START + VMALLOC_QUARTER_SIZE - 1) + +/* + * vmalloc metadata addresses are calculated by adding shadow/origin offsets + * to vmalloc address. + */ +#define KMSAN_VMALLOC_SHADOW_OFFSET VMALLOC_QUARTER_SIZE +#define KMSAN_VMALLOC_ORIGIN_OFFSET (VMALLOC_QUARTER_SIZE << 1) + +#define KMSAN_VMALLOC_SHADOW_START (VMALLOC_START + KMSAN_VMALLOC_SHADOW_OFFSET) +#define KMSAN_VMALLOC_ORIGIN_START (VMALLOC_START + KMSAN_VMALLOC_ORIGIN_OFFSET) + +/* + * The shadow/origin for modules are placed one by one in the last 1/4 of + * vmalloc space. + */ +#define KMSAN_MODULES_SHADOW_START (VMALLOC_END + KMSAN_VMALLOC_ORIGIN_OFFSET + 1) +#define KMSAN_MODULES_ORIGIN_START (KMSAN_MODULES_SHADOW_START + MODULES_LEN) +#endif /* CONFIG_KMSAN */ #define MODULES_VADDR (__START_KERNEL_map + KERNEL_IMAGE_SIZE) /* The module sections ends with the start of the fixmap */ diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index 0fe690ebc269b..39b6bfcaa0ed4 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -1287,7 +1287,7 @@ static void __init preallocate_vmalloc_pages(void) unsigned long addr; const char *lvl; - for (addr = VMALLOC_START; addr <= VMALLOC_END; addr = ALIGN(addr + 1, PGDIR_SIZE)) { + for (addr = VMALLOC_START; addr <= VMEMORY_END; addr = ALIGN(addr + 1, PGDIR_SIZE)) { pgd_t *pgd = pgd_offset_k(addr); p4d_t *p4d; pud_t *pud;