x86/sev: Convert shared memory back to private on kexec
authorAshish Kalra <ashish.kalra@amd.com>
Thu, 1 Aug 2024 19:14:50 +0000 (19:14 +0000)
committerBorislav Petkov (AMD) <bp@alien8.de>
Mon, 28 Oct 2024 17:06:54 +0000 (18:06 +0100)
commit3074152e56c9b0f9b9c67edfbc08b371db050b6d
treea1e706b42546a83ef7c09837a3b2543bdfa9e421
parent2a783066b6f5f5250b838d2acfc716561d2a66e0
x86/sev: Convert shared memory back to private on kexec

SNP guests allocate shared buffers to perform I/O. It is done by
allocating pages normally from the buddy allocator and converting them
to shared with set_memory_decrypted().

The second, kexec-ed, kernel has no idea what memory is converted this
way. It only sees E820_TYPE_RAM.

Accessing shared memory via private mapping will cause unrecoverable RMP
page-faults.

On kexec, walk direct mapping and convert all shared memory back to
private. It makes all RAM private again and second kernel may use it
normally. Additionally, for SNP guests, convert all bss decrypted
section pages back to private.

The conversion occurs in two steps: stopping new conversions and
unsharing all memory. In the case of normal kexec, the stopping of
conversions takes place while scheduling is still functioning. This
allows for waiting until any ongoing conversions are finished. The
second step is carried out when all CPUs except one are inactive and
interrupts are disabled. This prevents any conflicts with code that may
access shared memory.

Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Ashish Kalra <ashish.kalra@amd.com>
Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
Link: https://lore.kernel.org/r/05a8c15fb665dbb062b04a8cb3d592a63f235937.1722520012.git.ashish.kalra@amd.com
arch/x86/coco/sev/core.c
arch/x86/include/asm/sev.h
arch/x86/mm/mem_encrypt_amd.c