Skip to contentSkip to conversation

x86/mm/64: free the gap between BSS_MAIN and BSS_DECRYPTED

PATCH New Hamza Mahfooz 1 participant 1 message Nov 24

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

PATCH v8 1/1 x86/mm/64: free the gap between BSS_MAIN and BSS_DECRYPTED

With CONFIG_AMD_MEM_ENCRYPT=y, BSS_DECRYPTED puts .bss..decrypted on its own 2M-aligned range at the end of .bss. When SME or SEV is active, sme_postprocess_startup() clears the encryption mask for the whole range in the early page tables, one 2M page at a time. mem_encrypt_free_decrypted_mem() later frees the part of the range that isn't used, re-encrypting it first if needed.

However, nothing frees the padding that the 2M alignment inserts between the end of BSS_MAIN and __start_bss_decrypted. Kernels that support AMD memory encryption, e.g. distribution kernels or paravisor kernels running inside confidential VMs, still waste anywhere between 0 and 2M - 4K on every boot, on any CPU, depending on where BSS_MAIN happens to end. e.g., built with GCC 16.2:

.config _end - _text padding

  x86_64_defconfig + AMD_MEM_ENCRYPT         44.2M     616K
  Arch Linux 7.3.5-arch1-1                   60.2M    1160K
  OpenHCL paravisor, x64 CVM                 12.1M     196K

Mark the start of the padding with a new __start_bss_decrypted_gap symbol, it ends at __start_bss_decrypted. The start can't be derived from the existing symbols, as nothing marks the end of BSS_MAIN.

Free the padding at the beginning of mem_encrypt_free_decrypted_mem(), which runs on every CONFIG_AMD_MEM_ENCRYPT=y kernel, whether memory encryption is active or not. The padding lies outside of the range that sme_postprocess_startup() maps decrypted, so unlike the unused part of .bss..decrypted, it doesn't need to be re-encrypted.

---
v8:
- Go back to freeing the gap from mem_encrypt_free_decrypted_mem(), like v5 did. It is called on every CONFIG_AMD_MEM_ENCRYPT=y kernel, so there is no need for an #ifdef or for a !CONFIG_AMD_MEM_ENCRYPT variant of BSS_DECRYPTED, and only one new symbol is needed. (Boris)
- Free the gap before re-encrypting the unused part of .bss..decrypted, so that a set_memory_encrypted() failure can't leak it. (Sashiko)
- Explain the problem in the changelog, add measurements and move the testing notes to the appropriate location. (Boris)

Testing: x86_64_defconfig + AMD_MEM_ENCRYPT + kmemleak + debug_pagealloc, booted in QEMU/KVM with pti=on, pti=off and pti=on debug_pagealloc=on, and in QEMU/TCG emulating an Intel Skylake CPU, where PTI is enabled by default. Each boot ran two kmemleak scans without splats, and the W+X check passed:

Freeing unused kernel image (bss_decrypted gap) memory: 1948K Freeing unused decrypted memory: 2036K

Also, tested on a CoCo VM running on an AMD EPYC 7763v which generated the following print:

Freeing unused kernel image (bss_decrypted gap) memory: 184K

---
 arch/x86/include/asm/mem_encrypt.h | 3 ++-
 arch/x86/kernel/vmlinux.lds.S      | 2 ++
 arch/x86/mm/mem_encrypt_amd.c      | 6 ++++++
 3 files changed, 10 insertions(+), 1 deletion(-)
arch/x86/include/asm/mem_encrypt.h +2 -1
index ea6494628cb0..c9722be2808f 100644
@@ -112,7 +112,8 @@ void add_encrypt_protection_map(void);
 #define __sme_pa(x)		(__pa(x) | sme_me_mask)
 #define __sme_pa_nodebug(x)	(__pa_nodebug(x) | sme_me_mask)
 
-extern char __start_bss_decrypted[], __end_bss_decrypted[], __start_bss_decrypted_unused[];
+extern char __start_bss_decrypted_gap[], __start_bss_decrypted[],
+	__end_bss_decrypted[], __start_bss_decrypted_unused[];
 
 #endif	/* __ASSEMBLER__ */
 
arch/x86/kernel/vmlinux.lds.S +2 -0
index 2438b89a4620..fae895638d06 100644
@@ -94,6 +94,8 @@ const_cpu_current_top_of_stack = cpu_current_top_of_stack;
  * decrypted to avoid exposing more than we wish.
  */
 #define BSS_DECRYPTED						\
+	. = ALIGN(PAGE_SIZE);					\
+	__start_bss_decrypted_gap = .;				\
 	. = ALIGN(PMD_SIZE);					\
 	__start_bss_decrypted = .;				\
 	__pi___start_bss_decrypted = .;				\
arch/x86/mm/mem_encrypt_amd.c +6 -0
index 2f8c32173972..357e4389a5e4 100644
@@ -545,6 +545,12 @@ void __init mem_encrypt_free_decrypted_mem(void)
 	unsigned long vaddr, vaddr_end, npages;
 	int r;
 
+	vaddr = (unsigned long)__start_bss_decrypted_gap;
+	vaddr_end = (unsigned long)__start_bss_decrypted;
+
+	free_init_pages("unused kernel image (bss_decrypted gap)",
+			vaddr, vaddr_end);
+
 	vaddr = (unsigned long)__start_bss_decrypted_unused;
 	vaddr_end = (unsigned long)__end_bss_decrypted;
 	npages = (vaddr_end - vaddr) >> PAGE_SHIFT;
Signature · 1 line
2.56.1