Skip to contentSkip to conversation

x86/boot/compressed: Reserve more memory for page tables

PATCH Applied Kirill A. Shutemov 3 participants 7 messages Sep 14, 2023 – Sep 17, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

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.

Fixes 34bbb0009f3b ("x86/boot/compressed: Enable 5-level paging during decompression stage")
---
 arch/x86/include/asm/boot.h | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)
arch/x86/include/asm/boot.h +12 -15
index 9191280d9ea3..aaf1b2fc6ede 100644
@@ -40,23 +40,20 @@
 #ifdef CONFIG_X86_64
 # define BOOT_STACK_SIZE	0x4000
 
-# 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.
+ * Used by decompressor's startup_32() to allocate page tables for identity
+ * mapping of the 4G of RAM in 4-level paging mode.
+ *
+ * The additional page table needed for 5-level paging is allocated from
+ * trampoline_32bit memory.
  */
-#  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_INIT_PGT_SIZE	(6*4096)
+
+/*
+ * Total number of page table kernel_add_identity_map() can allocate,
+ * including page tables consumed by startup_32().
+ */
+# define BOOT_PGT_SIZE		(32*4096)
 
 #else /* !CONFIG_X86_64 */
 # define BOOT_STACK_SIZE	0x1000
Signature · 1 line
2.41.0
#1 lore raw
The following commit has been merged into the x86/urgent branch of tip:

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     7362132a6408d23244b95025ea85dd3dc41e5332
Gitweb:        https://git.kernel.org/tip/7362132a6408d23244b95025ea85dd3dc41e5332
Author:        Kirill A. Shutemov <[email protected]>
AuthorDate:    Thu, 14 Sep 2023 15:30:01 +03:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Thu, 14 Sep 2023 16:12:28 +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.

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.

Fixes 34bbb0009f3b ("x86/boot/compressed: Enable 5-level paging during decompression stage")
Cc Linus Torvalds <[email protected]>
---
 arch/x86/include/asm/boot.h | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)
arch/x86/include/asm/boot.h +11 -14
index 4ae1433..d2caae2 100644
@@ -40,23 +40,20 @@
 #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.
+ *
+ * The additional page 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 table kernel_add_identity_map() can allocate,
+ * including page tables consumed by startup_32().
  */
-#  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		(32*4096)
 
 #else /* !CONFIG_X86_64 */
 # define BOOT_STACK_SIZE	0x1000
#2 lore raw
In reply to Kirill A. Shutemov

Review

Quoted 5 lines · Kirill A. Shutemov
(quoted diff) +5 -0
+/*
+ * Total number of page table kernel_add_identity_map() can allocate,
+ * including page tables consumed by startup_32().
+ */
+# define BOOT_PGT_SIZE		(32*4096)

I agree that needing to know this in advance *exactly* is troublesome.

But I do think that we should preserve the comment about the worst-case scenario. Also, I thought this was triggered by unaccepted memory. Am I remembering it wrong? How was it in play?

Either way, I think your general approach here is sound. But let's add one little tweak to at least warn when we're getting close to the limit.
Now that nobody has to worry about the limit for the immediate future
it's a guarantee that in the long term someone will plow through it accidentally.

Let's add a soft warning when we're nearing the limit so that there's a chance to catch these things in the future.

arch/x86/boot/compressed/ident_map_64.c +7 -0
index bcc956c17872..5dfb6a31bbb1 100644
@@ -59,6 +59,13 @@ static void *alloc_pgt_page(void *context)
 		return NULL;
 	}
 
+	if (pages->pgt_buf_offset >= pages->pgt_buf_size * 3 / 4) {
+		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;
 
#3 lore raw
Quoted 11 lines · Dave Hansen

Review

Quoted 5 lines · Kirill A. Shutemov
(quoted diff) +5 -0
+/*
+ * Total number of page table kernel_add_identity_map() can allocate,
+ * including page tables consumed by startup_32().
+ */
+# define BOOT_PGT_SIZE		(32*4096)

I agree that needing to know this in advance *exactly* is troublesome.

But I do think that we should preserve the comment about the worst-case scenario.

Want me to send v2 for that?

Quoted 2 lines

Also, I thought this was triggered by unaccepted memory. Am I remembering it wrong? How was it in play?

Unaccepted memory touched EFI system table. I was able to reproduce without unaccepted memory enabled: if get_rsdp_addr() takes efi_get_rsdp_addr() path. So it is not the root cause, just a trigger.

So we need several things to run into the problem:

- System supports 5-level paging and it is enabled;

- Decompressor takes control in 64-bit mode, so it uses page tables inherited from bootloader until initialize_identity_maps().

In initialize_identity_maps() kernel resets page tables, rebuilding them from scratch. Here we only map what is definitely required: kernel, cmdline, boot_patams, setup_data.

Entering in 32-bit mode would make startup_32() map the first 4G unconditionally, but in this setup we rely more on #PF to fill page table. It masks problem as we rarely need all four PMD tables.

- Make kernel touch at least one page per-gigabyte in the first 4G.

In our case, unaccepted memory path was the last straw: it triggered allocation of the fourth PMD table which failed.

We can increase the constant by one and it will work as long as nobody need anything beyond the first 4G (or any 1G-aligned 4G region where we've got loaded, I guess). I am not sure we can guarantee this with (potentially buggy) ACPI and EFI in the picture.

Quoted 2 lines

Either way, I think your general approach here is sound. But let's add one little tweak to at least warn when we're getting close to the limit.

Yeah, makes sense.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#4 lore raw

PATCH v2 1/1 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.

Fixes 34bbb0009f3b ("x86/boot/compressed: Enable 5-level paging during decompression stage")
---
 arch/x86/boot/compressed/ident_map_64.c |  8 +++++
 arch/x86/include/asm/boot.h             | 47 +++++++++++++++++--------
 2 files changed, 40 insertions(+), 15 deletions(-)
arch/x86/boot/compressed/ident_map_64.c +8 -0
index bcc956c17872..08f93b0401bb 100644
@@ -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;
 
arch/x86/include/asm/boot.h +32 -15

55 lines not on this page · raw message

Signature · 1 line
2.41.0
#5 lore raw
The following commit has been merged into the x86/urgent branch of tip:

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 <[email protected]>
AuthorDate:    Fri, 15 Sep 2023 10:02:21 +03:00
Committer:     Ingo Molnar <[email protected]>
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")
---
 arch/x86/boot/compressed/ident_map_64.c |  8 ++++-
 arch/x86/include/asm/boot.h             | 45 ++++++++++++++++--------
 2 files changed, 39 insertions(+), 14 deletions(-)
arch/x86/boot/compressed/ident_map_64.c +8 -0
index bcc956c..08f93b0 100644
@@ -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;
 
arch/x86/include/asm/boot.h +31 -14

54 lines not on this page · raw message

#6 lore raw
In reply to Kirill A. Shutemov
The following commit has been merged into the x86/urgent branch of tip:

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     f530ee95b72e77b09c141c4b1a4b94d1199ffbd9
Gitweb:        https://git.kernel.org/tip/f530ee95b72e77b09c141c4b1a4b94d1199ffbd9
Author:        Kirill A. Shutemov <[email protected]>
AuthorDate:    Fri, 15 Sep 2023 10:02:21 +03:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Sun, 17 Sep 2023 09:48:57 +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")
---
 arch/x86/boot/compressed/ident_map_64.c |  8 ++++-
 arch/x86/include/asm/boot.h             | 45 ++++++++++++++++--------
 2 files changed, 39 insertions(+), 14 deletions(-)
arch/x86/boot/compressed/ident_map_64.c +8 -0
index bcc956c..08f93b0 100644
@@ -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;
 
arch/x86/include/asm/boot.h +31 -14

54 lines not on this page · raw message