Skip to contentSkip to conversation

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

PATCH Superseded Hamza Mahfooz 2 participants 2 messages Jul 10 – Jul 11

Subsystems (2)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

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

This region is unused after mark_rodata_ro() and takes up, up to 2 MiB of memory. So, free it in mark_rodata_ro().

---
v3: guard __start_bss_decrypted{,_gap} with CONFIG_AMD_MEM_ENCRYPT.

---
 arch/x86/include/asm/sections.h | 1 +
 arch/x86/kernel/vmlinux.lds.S   | 2 ++
 arch/x86/mm/init_64.c           | 8 ++++++++
 3 files changed, 11 insertions(+)
arch/x86/include/asm/sections.h +1 -0
index 30e8ee7006f9..1d6589bc0f68 100644
@@ -8,6 +8,7 @@
 extern char __relocate_kernel_start[], __relocate_kernel_end[];
 extern char __brk_base[], __brk_limit[];
 extern char __end_rodata_aligned[];
+extern char __start_bss_decrypted_gap[];
 
 #if defined(CONFIG_X86_64)
 extern char __end_rodata_hpage_align[];
arch/x86/kernel/vmlinux.lds.S +2 -0
index 74e336d7f9dd..e9856d947370 100644
@@ -88,6 +88,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/init_64.c +8 -0
index d57f29ca23a5..c34973ac3656 100644
@@ -1409,6 +1409,10 @@ void mark_rodata_ro(void)
 	unsigned long end = (unsigned long)__end_rodata_hpage_align;
 	unsigned long text_end = PFN_ALIGN(_etext);
 	unsigned long rodata_end = PFN_ALIGN(__end_rodata);
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+	unsigned long bss_decrypted_gap_start = PFN_ALIGN(__start_bss_decrypted_gap);
+	unsigned long bss_decrypted_gap_stop = PFN_ALIGN(__start_bss_decrypted);
+#endif
 	unsigned long all_end;
 
 	printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
@@ -1446,6 +1450,10 @@ void mark_rodata_ro(void)
 				(void *)text_end, (void *)rodata_start);
 	free_kernel_image_pages("unused kernel image (rodata/data gap)",
 				(void *)rodata_end, (void *)_sdata);
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+	free_kernel_image_pages("unused kernel image (bss_decrypted gap)",
+				(void *)bss_decrypted_gap_start, (void *)bss_decrypted_gap_stop);
+#endif
 }
 
 /*
Signature · 1 line
2.55.0
maintainer
#1 lore raw

arch/x86/mm/init_64.c void mark_rodata_ro(void) · line 1456

Quoted 8 lines · Hamza Mahfooz
arch/x86/mm/init_64.c +4 -0
@@ -1446,6 +1450,10 @@ void mark_rodata_ro(void)
 				(void *)text_end, (void *)rodata_start);
 	free_kernel_image_pages("unused kernel image (rodata/data gap)",
 				(void *)rodata_end, (void *)_sdata);
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+	free_kernel_image_pages("unused kernel image (bss_decrypted gap)",
+				(void *)bss_decrypted_gap_start, (void *)bss_decrypted_gap_stop);
+#endif

If you're going to add the ugly ifdeffery gunk, why don't you push it into the

#define BSS_DECRYPTED

place so that you don't need to do any of that freeing when CONFIG_AMD_MEM_ENCRYPT is not enabled?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette