From patchwork Mon Sep 5 12:24:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12966040 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 A11CAC54EE9 for ; Mon, 5 Sep 2022 12:25:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 36FFE8D0070; Mon, 5 Sep 2022 08:25:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 31F9E8D0050; Mon, 5 Sep 2022 08:25:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1C01D8D0070; Mon, 5 Sep 2022 08:25:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0B25D8D0050 for ; Mon, 5 Sep 2022 08:25:24 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D65921C6BE5 for ; Mon, 5 Sep 2022 12:25:23 +0000 (UTC) X-FDA: 79877952126.10.79950CF Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf30.hostedemail.com (Postfix) with ESMTP id 92CD280084 for ; Mon, 5 Sep 2022 12:25:23 +0000 (UTC) Received: by mail-wm1-f74.google.com with SMTP id ay21-20020a05600c1e1500b003a6271a9718so5338866wmb.0 for ; Mon, 05 Sep 2022 05:25:23 -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:subject:date; bh=PTsTfo/gJDV7iQzWmhln0w3yo3kBxDNSc9M3IZOTBoE=; b=d8oJ2o++JW9bXOqfouyBbl7WkyEV3VHUYF8d1z29wCxZ/PqYypXaFeFJ33XAooBYV2 6QnDZVdh2UZrOxf24TEY+bGaDzLoSOuz+Mt4ecgWbx2NculB+imyaiX0qfEphxqAWKD1 MHVx9LHlo5v7i4izd070T9QRcm38bjy4cTn++9ZxoWkJAy94o2rmHDnSmxn1cm1XXL0g GWA9ByBagZwIeOuTs+8qckPWsft9wpwkXGKmsbytmxjY70ngsRdz0sLz9vhk+xnrQMLC 4mcD/+64uPOb4iGYTejlWfkFKopWWvVqG7mTec9Li6rv7o2hJYEbpzlaF9yp4URO3JXm fw8A== 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:subject:date; bh=PTsTfo/gJDV7iQzWmhln0w3yo3kBxDNSc9M3IZOTBoE=; b=UgpOT3GYWUrdBu3t30/cpLkekCVSG5wcCiAUOQmd4tDaGpAfpy1gq3ULmvKzY8d26d //hslJBV9tDBD7BrCRtx7wKPNyd15FPvOFMWxpE80qTZPnxRPIUTln79CbIiCJisHXVI CoKZ04W/V6cPM2cT+CY8or0SJBEMZT1uVnXaBXV3MZAHKuwlZRLzAxBQUHTn9eDO2CeF lZsd1SDojKZqNtNUIEhVc9ii58q2outZe5jINMP1MtmPCRYBlENn5z3osORirw73jqIa 30oNNlLgA7DVmaFH43su98sQjxOjCMBqKrGGjOr2C6G/ykWcdi2fj+PQ1AApXeQ9Mu9m JC/Q== X-Gm-Message-State: ACgBeo3wmPYDT0ziZohYS5oK5agUMWkgAGQyxo0erFvzuy4kOWiFRLKa 0zMg2JFSVw8e/hBqIE/nEiOq+vzsnPE= X-Google-Smtp-Source: AA6agR4AUwxgeiq6J6CG95MolO6yz3zXmWIf61Vrp856Wu6spDXPfrQJz5uys7cjFGIfk6d+Qot0Uv3+LWE= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:b808:8d07:ab4a:554c]) (user=glider job=sendgmr) by 2002:adf:e74d:0:b0:226:d514:8c29 with SMTP id c13-20020adfe74d000000b00226d5148c29mr22189326wrn.664.1662380722163; Mon, 05 Sep 2022 05:25:22 -0700 (PDT) Date: Mon, 5 Sep 2022 14:24:17 +0200 In-Reply-To: <20220905122452.2258262-1-glider@google.com> Mime-Version: 1.0 References: <20220905122452.2258262-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220905122452.2258262-10-glider@google.com> Subject: [PATCH v6 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-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=d8oJ2o++; spf=pass (imf30.hostedemail.com: domain of 3suoVYwYKCP0lqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3suoVYwYKCP0lqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662380723; a=rsa-sha256; cv=none; b=JRVUQfL1nTp0hzmlOekCTMtRcsPzcdHV7s676Q55AbPyj1tBoDCb4Pj0YK72drVFnrszkA DmQtx8wqcMvHeeMW3ykWCLRvpAuHMaqiIgHoFdZ0bQQzvpBbETS4HW806hE18P2RIOD9Cg EIehrH4hUpzx8r9wfaVwn4CqLHVylmc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662380723; 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=PTsTfo/gJDV7iQzWmhln0w3yo3kBxDNSc9M3IZOTBoE=; b=JinxDlQHiD51qnRUh+A2I21Hsd1YnYfz74AyyBgANhjWjVrCgk1QtN1gysQaQO9g1emYdt lg6cFTbe+TzirjQeVSF7XzMjT2MsGqHfvnQp2szmMr8rTAUkmvcbx5ByZ05pzYpH6awjvl qLEwOIQvMr6OCRmdzlP10/mSxNkaJB4= X-Rspamd-Server: rspam02 X-Rspam-User: Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=d8oJ2o++; spf=pass (imf30.hostedemail.com: domain of 3suoVYwYKCP0lqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3suoVYwYKCP0lqnijwlttlqj.htrqnsz2-rrp0fhp.twl@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: 9ghhwq58a47i5o7rysejbg83crnnu3mi X-Rspamd-Queue-Id: 92CD280084 X-HE-Tag: 1662380723-78675 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;