]> git.itanic.dy.fi Git - linux-stable/commitdiff
x86/boot/compressed: Reserve more memory for page tables
authorKirill A. Shutemov <kirill.shutemov@linux.intel.com>
Fri, 15 Sep 2023 07:02:21 +0000 (10:02 +0300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 23 Sep 2023 09:01:09 +0000 (11:01 +0200)
[ Upstream commit f530ee95b72e77b09c141c4b1a4b94d1199ffbd9 ]

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 <aaron.lu@intel.com>
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/r/20230915070221.10266-1-kirill.shutemov@linux.intel.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
arch/x86/boot/compressed/ident_map_64.c
arch/x86/include/asm/boot.h

index 39b2eded7bc2b633c8fafa2bce50ed788b054a83..f4a2e6d373b29a283c95f242d7acdeb346022562 100644 (file)
@@ -67,6 +67,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;
 
index 9191280d9ea3160d8d92db5e688fc34712b90bf8..215d37f7dde8a993295160eaa6e8f012beb05783 100644 (file)
 #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