[PATCH v4] x86/mm/64: free the gap between BSS_MAIN and BSS_DECRYPTED

Hamza Mahfooz posted 1 patch 4 days, 14 hours ago
There is a newer version of this series
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(-)
[PATCH v4] x86/mm/64: free the gap between BSS_MAIN and BSS_DECRYPTED
Posted by Hamza Mahfooz 4 days, 14 hours ago
This region is unused and takes up, up to 2 MiB
of memory if AMD_MEM_ENCRYPT is enabled. So, free it in
mem_encrypt_free_decrypted_mem().

Signed-off-by: Hamza Mahfooz <hamzamahfooz@linux.microsoft.com>
---
 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(-)

diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_encrypt.h
index ea6494628cb0..c9722be2808f 100644
--- a/arch/x86/include/asm/mem_encrypt.h
+++ b/arch/x86/include/asm/mem_encrypt.h
@@ -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__ */
 
diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
index 4711a35e706c..179578a08da1 100644
--- a/arch/x86/kernel/vmlinux.lds.S
+++ b/arch/x86/kernel/vmlinux.lds.S
@@ -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 = .;				\
diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c
index 2f8c32173972..8d518571ae5c 100644
--- a/arch/x86/mm/mem_encrypt_amd.c
+++ b/arch/x86/mm/mem_encrypt_amd.c
@@ -566,4 +566,10 @@ void __init mem_encrypt_free_decrypted_mem(void)
 	}
 
 	free_init_pages("unused decrypted", vaddr, vaddr_end);
+
+	vaddr = PFN_ALIGN(__start_bss_decrypted_gap);
+	vaddr_end = PFN_ALIGN(__start_bss_decrypted);
+
+	free_kernel_image_pages("unused kernel image (bss_decrypted gap)",
+				(void *)vaddr, (void *)vaddr_end);
 }
-- 
2.55.0
Re: [PATCH v4] x86/mm/64: free the gap between BSS_MAIN and BSS_DECRYPTED
Posted by Dave Hansen 4 days, 14 hours ago
On 7/20/26 04:20, Hamza Mahfooz wrote:
> This region is unused and takes up, up to 2 MiB
> of memory if AMD_MEM_ENCRYPT is enabled. So, free it in
> mem_encrypt_free_decrypted_mem().

For something like this, I'd really appreciate if contributors bring
_some_ kind of mention of testing to the changelog.

A great addition to this would be a blurb that mentioned an increase in
MemFree on boot, or a note about the delta in dmesg on startup that
shows the patch working.

> diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
> index 4711a35e706c..179578a08da1 100644
> --- a/arch/x86/kernel/vmlinux.lds.S
> +++ b/arch/x86/kernel/vmlinux.lds.S
> @@ -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 = .;				\
> diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c
> index 2f8c32173972..8d518571ae5c 100644
> --- a/arch/x86/mm/mem_encrypt_amd.c
> +++ b/arch/x86/mm/mem_encrypt_amd.c
> @@ -566,4 +566,10 @@ void __init mem_encrypt_free_decrypted_mem(void)
>  	}
>  
>  	free_init_pages("unused decrypted", vaddr, vaddr_end);
> +
> +	vaddr = PFN_ALIGN(__start_bss_decrypted_gap);
> +	vaddr_end = PFN_ALIGN(__start_bss_decrypted);
> +
> +	free_kernel_image_pages("unused kernel image (bss_decrypted gap)",
> +				(void *)vaddr, (void *)vaddr_end);
>  }

What purpose do the two new PFN_ALIGN()s serve?
Re: [PATCH v4] x86/mm/64: free the gap between BSS_MAIN and BSS_DECRYPTED
Posted by Hamza Mahfooz 4 days, 10 hours ago
On Mon, Jul 20, 2026 at 05:10:45AM -0700, Dave Hansen wrote:
> On 7/20/26 04:20, Hamza Mahfooz wrote:
> > This region is unused and takes up, up to 2 MiB
> > of memory if AMD_MEM_ENCRYPT is enabled. So, free it in
> > mem_encrypt_free_decrypted_mem().
> 
> For something like this, I'd really appreciate if contributors bring
> _some_ kind of mention of testing to the changelog.
> 
> A great addition to this would be a blurb that mentioned an increase in
> MemFree on boot, or a note about the delta in dmesg on startup that
> shows the patch working.

Sure, I did test this on a CoCo VM FWIW and was able to verify that the
code actually ran and that MemFree was lower on boot.

> 
> > diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
> > index 4711a35e706c..179578a08da1 100644
> > --- a/arch/x86/kernel/vmlinux.lds.S
> > +++ b/arch/x86/kernel/vmlinux.lds.S
> > @@ -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 = .;				\
> > diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c
> > index 2f8c32173972..8d518571ae5c 100644
> > --- a/arch/x86/mm/mem_encrypt_amd.c
> > +++ b/arch/x86/mm/mem_encrypt_amd.c
> > @@ -566,4 +566,10 @@ void __init mem_encrypt_free_decrypted_mem(void)
> >  	}
> >  
> >  	free_init_pages("unused decrypted", vaddr, vaddr_end);
> > +
> > +	vaddr = PFN_ALIGN(__start_bss_decrypted_gap);
> > +	vaddr_end = PFN_ALIGN(__start_bss_decrypted);
> > +
> > +	free_kernel_image_pages("unused kernel image (bss_decrypted gap)",
> > +				(void *)vaddr, (void *)vaddr_end);
> >  }
> 
> What purpose do the two new PFN_ALIGN()s serve?

On closer examination they do seem redundant. I will retest without them
and respin if that is working fine.