Skip to content

Commit f530ee9

Browse files
kirylIngo Molnar
authored andcommitted
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: 34bbb00 ("x86/boot/compressed: Enable 5-level paging during decompression stage") Reported-by: Aaron Lu <[email protected]> Signed-off-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 7575e5a commit f530ee9

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

arch/x86/boot/compressed/ident_map_64.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ static void *alloc_pgt_page(void *context)
5959
return NULL;
6060
}
6161

62+
/* Consumed more tables than expected? */
63+
if (pages->pgt_buf_offset == BOOT_PGT_SIZE_WARN) {
64+
debug_putstr("pgt_buf running low in " __FILE__ "\n");
65+
debug_putstr("Need to raise BOOT_PGT_SIZE?\n");
66+
debug_putaddr(pages->pgt_buf_offset);
67+
debug_putaddr(pages->pgt_buf_size);
68+
}
69+
6270
entry = pages->pgt_buf + pages->pgt_buf_offset;
6371
pages->pgt_buf_offset += PAGE_SIZE;
6472

arch/x86/include/asm/boot.h

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,40 @@
4040
#ifdef CONFIG_X86_64
4141
# define BOOT_STACK_SIZE 0x4000
4242

43+
/*
44+
* Used by decompressor's startup_32() to allocate page tables for identity
45+
* mapping of the 4G of RAM in 4-level paging mode:
46+
* - 1 level4 table;
47+
* - 1 level3 table;
48+
* - 4 level2 table that maps everything with 2M pages;
49+
*
50+
* The additional level5 table needed for 5-level paging is allocated from
51+
* trampoline_32bit memory.
52+
*/
4353
# define BOOT_INIT_PGT_SIZE (6*4096)
44-
# ifdef CONFIG_RANDOMIZE_BASE
54+
4555
/*
46-
* Assuming all cross the 512GB boundary:
47-
* 1 page for level4
48-
* (2+2)*4 pages for kernel, param, cmd_line, and randomized kernel
49-
* 2 pages for first 2M (video RAM: CONFIG_X86_VERBOSE_BOOTUP).
50-
* Total is 19 pages.
56+
* Total number of page tables kernel_add_identity_map() can allocate,
57+
* including page tables consumed by startup_32().
58+
*
59+
* Worst-case scenario:
60+
* - 5-level paging needs 1 level5 table;
61+
* - KASLR needs to map kernel, boot_params, cmdline and randomized kernel,
62+
* assuming all of them cross 256T boundary:
63+
* + 4*2 level4 table;
64+
* + 4*2 level3 table;
65+
* + 4*2 level2 table;
66+
* - X86_VERBOSE_BOOTUP needs to map the first 2M (video RAM):
67+
* + 1 level4 table;
68+
* + 1 level3 table;
69+
* + 1 level2 table;
70+
* Total: 28 tables
71+
*
72+
* Add 4 spare table in case decompressor touches anything beyond what is
73+
* accounted above. Warn if it happens.
5174
*/
52-
# ifdef CONFIG_X86_VERBOSE_BOOTUP
53-
# define BOOT_PGT_SIZE (19*4096)
54-
# else /* !CONFIG_X86_VERBOSE_BOOTUP */
55-
# define BOOT_PGT_SIZE (17*4096)
56-
# endif
57-
# else /* !CONFIG_RANDOMIZE_BASE */
58-
# define BOOT_PGT_SIZE BOOT_INIT_PGT_SIZE
59-
# endif
75+
# define BOOT_PGT_SIZE_WARN (28*4096)
76+
# define BOOT_PGT_SIZE (32*4096)
6077

6178
#else /* !CONFIG_X86_64 */
6279
# define BOOT_STACK_SIZE 0x1000

0 commit comments

Comments
 (0)