From patchwork Tue Mar 29 12:39:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12794760 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 A7FB7C433F5 for ; Tue, 29 Mar 2022 12:40:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 36F008D0016; Tue, 29 Mar 2022 08:40:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F6998D0015; Tue, 29 Mar 2022 08:40:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 16FD88D0016; Tue, 29 Mar 2022 08:40:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0112.hostedemail.com [216.40.44.112]) by kanga.kvack.org (Postfix) with ESMTP id 05D188D0015 for ; Tue, 29 Mar 2022 08:40:58 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id BD9381828A446 for ; Tue, 29 Mar 2022 12:40:57 +0000 (UTC) X-FDA: 79297383354.23.8C26860 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf02.hostedemail.com (Postfix) with ESMTP id 311AA8003A for ; Tue, 29 Mar 2022 12:40:56 +0000 (UTC) Received: by mail-ed1-f74.google.com with SMTP id s9-20020a50d489000000b00418d556edbdso10959631edi.4 for ; Tue, 29 Mar 2022 05:40:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=6/sayBfyjsH2Y3AfJUr4VjD2mcwzYPMTZLT8of1IREg=; b=gPCDqH0qCHmbSNoYXOMeBTfYfmSmbXUFr3zh1f5ZPo0Pb3cPpOC3T9Li2Ba/s3wfmz gQ2bEXLw+QpWVfUvoSRzIkAV0HGDefVdPLjvrmeyUVW1CwUFY/o/0iTuMpy00qfMhXYD S7Ny7FKu+E+vuPhMe33YJ17U98elZnTICW7Rf7enJip6PmyGcM/IKGLtw9meI2L8r4T/ 1B+1Mwp5rxGR/FQmQuUKPOmSqvjvULvO7aaLABE+y2HIRD0KCdYz/+lXqsDIyraIiLZx Zx5IqS0hdEQnvzCuaM2/3M5V3KZQK9XBZNOZyH5gNz855mbOM3DEDHGoM3IbScEoxq6l Nn2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=6/sayBfyjsH2Y3AfJUr4VjD2mcwzYPMTZLT8of1IREg=; b=OxITBAPn6972oY26bhPMgmKoOQfQYfR26NtXqUzGHcj0SR3c3NoaT4E8KUUPcbny5h PHH/2LcqvsL96aKHk4iZNrt1mK7FlfM1z2dK8/yXlDrEHnlZSTV/KzeXW1iEzX1ROUAC j4r76XMUdYXkeBOwPkpDQ6RBpDX+CK3WO3QZuCk8x4VNKeobXouBQb/uS1XU0kDcJM+N M8YqJ0OnGnyywb9si7lXvJFvMPMd56M5twJBaVlmFDXpzA0z8cBhRT7s5vVOJmyJ2Uvt WyhHuhAai2dmrCV/nYvz3mBEXm7oBqZcZR6+5R4KDGgJ4QfWPre2C9xCcQ/OggOpO10N 72gA== X-Gm-Message-State: AOAM5324LeJlTG9GGNZEC7AZZZFmDehrPqALVxVcHZNAufZdpZ0X+cvz N8D+YUO5ZWnpoCiqVheXVA0eMDF+Rf8= X-Google-Smtp-Source: ABdhPJz19CPV3rmf1QsGMB2W43cqP02JXhPWr4JS53hrOaKJGQqZA3fyKoTvfV9B2lQS0qWbgSPFKMg0F4A= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:36eb:759:798f:98c3]) (user=glider job=sendgmr) by 2002:a17:906:40da:b0:6ce:51b:a593 with SMTP id a26-20020a17090640da00b006ce051ba593mr34588142ejk.604.1648557654844; Tue, 29 Mar 2022 05:40:54 -0700 (PDT) Date: Tue, 29 Mar 2022 14:39:39 +0200 In-Reply-To: <20220329124017.737571-1-glider@google.com> Message-Id: <20220329124017.737571-11-glider@google.com> Mime-Version: 1.0 References: <20220329124017.737571-1-glider@google.com> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog Subject: [PATCH v2 10/48] x86: kmsan: pgtable: reduce vmalloc space From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , 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 , linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 311AA8003A X-Rspam-User: Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=gPCDqH0q; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3Vv5CYgYKCHcbgdYZmbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--glider.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3Vv5CYgYKCHcbgdYZmbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--glider.bounces.google.com X-Stat-Signature: tsb7z3d5fnthotni3its8byaoyow5ds6 X-HE-Tag: 1648557656-44519 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 Link: https://linux-review.googlesource.com/id/I9d8b7f0a88a639f1263bc693cbd5c136626f7efd --- arch/x86/include/asm/pgtable_64_types.h | 41 ++++++++++++++++++++++++- arch/x86/mm/init_64.c | 2 +- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/pgtable_64_types.h b/arch/x86/include/asm/pgtable_64_types.h index 91ac106545703..7f15d43754a34 100644 --- a/arch/x86/include/asm/pgtable_64_types.h +++ b/arch/x86/include/asm/pgtable_64_types.h @@ -139,7 +139,46 @@ 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) +#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 96d34ebb20a9e..fcea37beb3911 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;