From: Hamza Mahfooz <hamzamahfooz@linux.microsoft.com>
To: x86@kernel.org
Cc: Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
"H. Peter Anvin" <hpa@zytor.com>,
Andy Lutomirski <luto@kernel.org>,
Peter Zijlstra <peterz@infradead.org>, Rong Xu <xur@google.com>,
linux-kernel@vger.kernel.org,
Hamza Mahfooz <hamzamahfooz@linux.microsoft.com>
Subject: [PATCH v8] x86/mm/64: free the gap between BSS_MAIN and BSS_DECRYPTED
Date: Mon, 23 Nov 2026 20:06:35 -0500 [thread overview]
Message-ID: <20261124010635.1299890-1-hamzamahfooz@linux.microsoft.com> (raw)
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.
Signed-off-by: Hamza Mahfooz <hamzamahfooz@linux.microsoft.com>
---
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(-)
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 2438b89a4620..fae895638d06 100644
--- a/arch/x86/kernel/vmlinux.lds.S
+++ b/arch/x86/kernel/vmlinux.lds.S
@@ -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 = .; \
diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c
index 2f8c32173972..357e4389a5e4 100644
--- a/arch/x86/mm/mem_encrypt_amd.c
+++ b/arch/x86/mm/mem_encrypt_amd.c
@@ -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;
--
2.56.1
reply other threads:[~2026-11-24 1:06 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124010635.1299890-1-hamzamahfooz@linux.microsoft.com \
--to=hamzamahfooz@linux.microsoft.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
--cc=xur@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®