Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4684599rdb; Fri, 15 Sep 2023 09:12:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHoO0E2oEakjj4fwH2LhaxuFi/tCO+uyNzRfRUZS2UCi4JxNqFMcLku2LxargSGePFWCE9O X-Received: by 2002:a05:6a20:7288:b0:135:7975:f55 with SMTP id o8-20020a056a20728800b0013579750f55mr2448735pzk.47.1694794369699; Fri, 15 Sep 2023 09:12:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694794369; cv=none; d=google.com; s=arc-20160816; b=l9UrPtj107Hms2xRKzzlTuJX26JGMkwFS3li92YKMixIql7jcr08O7ZEvl6DyeKKAl BHLJowEBNPnw4z/Wz5kgaaKuYlr5UtNPWI7kJ/1Tbvswx8yMUKYlIWGg9vag7OsQ9TXX zjDj4axjIfGQPDzARESNd9GASE4t42lJlk9F0kTlZ4aqGpUXPZzJwd6iT6Hw+gEMZxV5 akimFxicqAPs9XdXB6XNEpctI1fgSDh6hcbL9X7KHOJ+0trIfHpMMjy4RVD8UwbqfwD0 JqcEsJmy9f6yhngvWxULkTXJ/tQkIs0XH9EsTJYz71tPIKek8WpC6K775qH29wOd1XMg 5Nkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=5D+PB2+Xq1bmF0uQFk1Rfk1E0SNjFQGsUKNQTbL8vjI=; fh=DvJpOgpqesTAZlr3bqDFz3CtlbK9yGxVry+sEHnDWYk=; b=zvIWLs7IRSyQsM0GOFczbyU1dEz/524KZbm2pTPKkAAsfEYIAjUrkAk4vy9wWfFdgu TcCVy+6fvmVRm9twuKgKPZP5REjhvBAqL2XIxF8R5/Ldb58t3VnyhfMo36muMshww39J PSOUGIuziQY+LZ3abjBTbnWEq1wUZu2g+zYMYmJb5uxk0fh1FX6ftYzFkzOowAnrAdge 0e00rEyIl7q1btLEEQyEUL7/0sQl0NGGJRyCvBY0pYe33PZZEZZkwJQuABlrjftSvvEa FLBhQTV+Vjz1KOvIfY1qIyRfCgiJR9VgAdOLXqQEOUHCqaSwo5xGp3H/Gjc3eBLGlqPu ZAfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=JDhRrZvP; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id ij4-20020a170902ab4400b001b3d6d125f9si3436920plb.524.2023.09.15.09.12.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 09:12:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=JDhRrZvP; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 123D7839B9D5; Fri, 15 Sep 2023 01:29:20 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232454AbjIOI3R (ORCPT + 99 others); Fri, 15 Sep 2023 04:29:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232893AbjIOI3P (ORCPT ); Fri, 15 Sep 2023 04:29:15 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88EBD2D70; Fri, 15 Sep 2023 01:28:30 -0700 (PDT) Date: Fri, 15 Sep 2023 08:28:27 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694766508; h=from:from:sender:sender:reply-to: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5D+PB2+Xq1bmF0uQFk1Rfk1E0SNjFQGsUKNQTbL8vjI=; b=JDhRrZvPCASGh+mPXX31Uw59n5BNE8m1JOk8sf/RCOAwwDfWEH8bRm64VGi2gjCFofzA1W 4x7tUZtHDp/5Nm2dYCPxEqOy5dlGHLAoqAawzs8MeFczytbdg1dHY6Veo42ftgH/vv7idS M/ghNUJr2Ycwfsq5W4FnVJVIVAXjiQdYa80EvMiEsHXk7DbBIi3PpMAIiT6bF/QlvP8XI0 ltGSwV1RTbGh8KyOtrkO+DEqDYY4ck/XOPTI4/C/wYX9EEOWBxKYdHPq0g1inFBHTJZPs5 s8mHoK+ZiuHdEecPcYppnuWYqNeMOOfSkDCGOH6ufhUD3NMvdQljg95dSxEQwA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694766508; h=from:from:sender:sender:reply-to: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5D+PB2+Xq1bmF0uQFk1Rfk1E0SNjFQGsUKNQTbL8vjI=; b=ZBfDrVqjNn6i6tUBnjfmxoWZ0eiBrTM5dWMnPXt9w+Nb6ZACLJP8z1C3Iisim9gaInmaKp HIcCObaIkD2FtMBw== From: "tip-bot2 for Kirill A. Shutemov" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/urgent] x86/boot/compressed: Reserve more memory for page tables Cc: Aaron Lu , "Kirill A. Shutemov" , Ingo Molnar , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230915070221.10266-1-kirill.shutemov@linux.intel.com> References: <20230915070221.10266-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Message-ID: <169476650726.27769.5534132274690328507.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Fri, 15 Sep 2023 01:29:20 -0700 (PDT) The following commit has been merged into the x86/urgent branch of tip: Commit-ID: 2768c8ca5cc768568e4dfca291b26caa652127cb Gitweb: https://git.kernel.org/tip/2768c8ca5cc768568e4dfca291b26caa652127cb Author: Kirill A. Shutemov AuthorDate: Fri, 15 Sep 2023 10:02:21 +03:00 Committer: Ingo Molnar CommitterDate: Fri, 15 Sep 2023 10:22:24 +02:00 x86/boot/compressed: Reserve more memory for page tables The decompressor has a hard limit on the number of page tables it can allocate. This limit is defined at compile-time and will cause boot failure if it is reached. The kernel is very strict and calculates the limit precisely for the worst-case scenario based on the current configuration. However, it is easy to forget to adjust the limit when a new use-case arises. The worst-case scenario is rarely encountered during sanity checks. In the case of enabling 5-level paging, a use-case was overlooked. The limit needs to be increased by one to accommodate the additional level. This oversight went unnoticed until Aaron attempted to run the kernel via kexec with 5-level paging and unaccepted memory enabled. Update wost-case calculations to include 5-level paging. To address this issue, let's allocate some extra space for page tables. 128K should be sufficient for any use-case. The logic can be simplified by using a single value for all kernel configurations. [ Also add a warning, should this memory run low - by Dave Hansen. ] Fixes: 34bbb0009f3b ("x86/boot/compressed: Enable 5-level paging during decompression stage") Reported-by: Aaron Lu Signed-off-by: Kirill A. Shutemov Signed-off-by: Ingo Molnar Link: https://lore.kernel.org/r/20230915070221.10266-1-kirill.shutemov@linux.intel.com --- arch/x86/boot/compressed/ident_map_64.c | 8 ++++- arch/x86/include/asm/boot.h | 45 ++++++++++++++++-------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/arch/x86/boot/compressed/ident_map_64.c b/arch/x86/boot/compressed/ident_map_64.c index bcc956c..08f93b0 100644 --- a/arch/x86/boot/compressed/ident_map_64.c +++ b/arch/x86/boot/compressed/ident_map_64.c @@ -59,6 +59,14 @@ static void *alloc_pgt_page(void *context) return NULL; } + /* Consumed more tables than expected? */ + if (pages->pgt_buf_offset == BOOT_PGT_SIZE_WARN) { + debug_putstr("pgt_buf running low in " __FILE__ "\n"); + debug_putstr("Need to raise BOOT_PGT_SIZE?\n"); + debug_putaddr(pages->pgt_buf_offset); + debug_putaddr(pages->pgt_buf_size); + } + entry = pages->pgt_buf + pages->pgt_buf_offset; pages->pgt_buf_offset += PAGE_SIZE; diff --git a/arch/x86/include/asm/boot.h b/arch/x86/include/asm/boot.h index 4ae1433..b3a7cfb 100644 --- a/arch/x86/include/asm/boot.h +++ b/arch/x86/include/asm/boot.h @@ -40,23 +40,40 @@ #ifdef CONFIG_X86_64 # define BOOT_STACK_SIZE 0x4000 +/* + * Used by decompressor's startup_32() to allocate page tables for identity + * mapping of the 4G of RAM in 4-level paging mode: + * - 1 level4 table; + * - 1 level3 table; + * - 4 level2 table that maps everything with 2M pages; + * + * The additional level5 table needed for 5-level paging is allocated from + * trampoline_32bit memory. + */ # define BOOT_INIT_PGT_SIZE (6*4096) -# ifdef CONFIG_RANDOMIZE_BASE + /* - * Assuming all cross the 512GB boundary: - * 1 page for level4 - * (2+2)*4 pages for kernel, param, cmd_line, and randomized kernel - * 2 pages for first 2M (video RAM: CONFIG_X86_VERBOSE_BOOTUP). - * Total is 19 pages. + * Total number of page tables kernel_add_identity_map() can allocate, + * including page tables consumed by startup_32(). + * + * Worst-case scenario: + * - 5-level paging needs 1 level5 table; + * - KASLR needs to map kernel, boot_params, cmdline and randomized kernel, + * assuming all of them cross 256T boundary: + * + 4*2 level4 table; + * + 4*2 level3 table; + * + 4*2 level2 table; + * - X86_VERBOSE_BOOTUP needs to map the first 2M (video RAM): + * + 1 level4 table; + * + 1 level3 table; + * + 1 level2 table; + * Total: 28 tables + * + * Add 4 spare table in case decompressor touches anything beyond what is + * accounted above. Warn if it happens. */ -# ifdef CONFIG_X86_VERBOSE_BOOTUP -# define BOOT_PGT_SIZE (19*4096) -# else /* !CONFIG_X86_VERBOSE_BOOTUP */ -# define BOOT_PGT_SIZE (17*4096) -# endif -# else /* !CONFIG_RANDOMIZE_BASE */ -# define BOOT_PGT_SIZE BOOT_INIT_PGT_SIZE -# endif +# define BOOT_PGT_SIZE_WARN (28*4096) +# define BOOT_PGT_SIZE (32*4096) #else /* !CONFIG_X86_64 */ # define BOOT_STACK_SIZE 0x1000