x86/sev: ensure that RMP table fixups are reserved for memblock
PATCH 1/1 x86/sev: ensure that RMP table fixups are reserved for memblock
From: Ashish Kalra <[email protected]>
The BIOS reserves RMP table memory via e820 reservations, but since this can still lead to RMP page faults during kexec if the host tries to access memory within the same 2MB region, commit 400fea4b9651 ("x86/sev: Add callback to apply RMP table fixups for kexec") was added to adjust the e820 reservations for the RMP table so that the entire 2MB range at the start/end of the RMP table is marked reserved. The e820 reservations are then later passed to firmware via SNP_INIT where they get marked HV-Fixed.
The RMP table fixups are done after the e820 ranges have been added to memblock, allowing the fixup ranges to still be allocated and used by the system. The problem is that this memory range is now marked reserved in the e820 tables and during SNP initialization these reserved ranges are made HV-Fixed. This means that the pages cannot be used by an SNP guest, only by the hypervisor. However, the memory management subsystem does not make this distinction and can allocate one of those pages to an SNP guest. This will ultimately result in RMPUPDATE failures associated with the guest, causing it to fail to start or terminate when accessing the HV-Fixed page.
The issue is captured below with memblock=debug:
[ 0.000000] SEV-SNP: *** DEBUG: snp_probe_rmptable_info:352 - rmp_base=0x280d4800000, rmp_end=0x28357efffff ... [ 0.000000] BIOS-provided physical RAM map: ... [ 0.000000] BIOS-e820: [mem 0x00000280d4800000-0x0000028357efffff] reserved [ 0.000000] BIOS-e820: [mem 0x0000028357f00000-0x0000028357ffffff] usable ... ... [ 0.183593] memblock add: [0x0000028357f00000-0x0000028357ffffff] e820__memblock_setup+0x74/0xb0 ... [ 0.203179] MEMBLOCK configuration: [ 0.207057] memory size = 0x0000027d0d194000 reserved size = 0x0000000009ed2c00 [ 0.215299] memory.cnt = 0xb ... [ 0.311192] memory[0x9] [0x0000028357f00000-0x0000028357ffffff], 0x0000000000100000 bytes flags: 0x0 ... ... [ 0.419110] SEV-SNP: Reserving start/end of RMP table on a 2MB boundary [0x0000028357e00000] [ 0.428514] e820: update [mem 0x28357e00000-0x28357ffffff] usable ==> reserved [ 0.428517] e820: update [mem 0x28357e00000-0x28357ffffff] usable ==> reserved [ 0.428520] e820: update [mem 0x28357e00000-0x28357ffffff] usable ==> reserved ... ... [ 5.604051] MEMBLOCK configuration: [ 5.607922] memory size = 0x0000027d0d194000 reserved size = 0x0000000011faae02 [ 5.616163] memory.cnt = 0xe ... [ 5.754525] memory[0xc] [0x0000028357f00000-0x0000028357ffffff], 0x0000000000100000 bytes on node 0 flags: 0x0 ... ... [ 10.080295] Early memory node ranges[ 10.168065]
...
node 0: [mem 0x0000028357f00000-0x0000028357ffffff]
...
...
[ 8149.348948] SEV-SNP: RMPUPDATE failed for PFN 28357f7c, pg_level: 1, ret: 2
As shown above, the memblock allocations show 1MB after the end of the RMP as available for allocation, which is what the RMP table fixups has made reserved. This memory range subsequently gets allocated as SNP guest memory, resulting in an RMPUPDATE failure.
This can potentially be fixed by not reserving the memory range in the e820 table, but that causes kexec failures when using the KEXEC_FILE_LOAD syscall.
The solution is to use memblock_reserve() to mark the memory reserved for the system, ensuring that it cannot be allocated to an SNP guest.
Since HV-Fixed memory is still readable/writable by the host, this only ends up being a problem if the memory in this range requires a page state change, which generally will only happen when allocating memory in this range to be used for running SNP guests, which is now possible with the SNP hypervisor support in kernel 6.11.
For this reason, this patch is not being marked for stable, even though it fixes handling that was introduced in kernel 6.9.
--- arch/x86/virt/svm/sev.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/virt/svm/sev.c +2 -0
@@ -173,6 +173,8 @@ static void __init __snp_fixup_e820_tables(u64 pa)e820__range_update (pa,PMD_SIZE ,E820_TYPE_RAM ,E820_TYPE_RESERVED );e820__range_update_table (e820_table_kexec, pa,PMD_SIZE ,E820_TYPE_RAM ,E820_TYPE_RESERVED );e820__range_update_table (e820_table_firmware, pa,PMD_SIZE ,E820_TYPE_RAM ,E820_TYPE_RESERVED ); +if (!memblock_is_region_reserved (pa,PMD_SIZE )) +memblock_reserve (pa,PMD_SIZE ); } }
Signature · 1 line
2.34.1
Quoted 78 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
The BIOS reserves RMP table memory via e820 reservations, but since this can still lead to RMP page faults during kexec if the host tries to access memory within the same 2MB region, commit
400fea4b9651("x86/sev: Add callback to apply RMP table fixups for kexec") was added to adjust the e820 reservations for the RMP table so that the entire 2MB range at the start/end of the RMP table is marked reserved. The e820 reservations are then later passed to firmware viaSNP_INITwhere they get marked HV-Fixed.The RMP table fixups are done after the e820 ranges have been added to memblock, allowing the fixup ranges to still be allocated and used by the system. The problem is that this memory range is now marked reserved in the e820 tables and during SNP initialization these reserved ranges are made HV-Fixed. This means that the pages cannot be used by an SNP guest, only by the hypervisor. However, the memory management subsystem does not make this distinction and can allocate one of those pages to an SNP guest. This will ultimately result in RMPUPDATE failures associated with the guest, causing it to fail to start or terminate when accessing the HV-Fixed page.
The issue is captured below with memblock=debug:
[ 0.000000] SEV-SNP: *** DEBUG: snp_probe_rmptable_info:352 - rmp_base=0x280d4800000, rmp_end=0x28357efffff ... [ 0.000000] BIOS-provided physical RAM map: ... [ 0.000000] BIOS-e820: [mem 0x00000280d4800000-0x0000028357efffff] reserved [ 0.000000] BIOS-e820: [mem 0x0000028357f00000-0x0000028357ffffff] usable ... ... [ 0.183593] memblock add: [0x0000028357f00000-0x0000028357ffffff] e820__memblock_setup+0x74/0xb0 ... [ 0.203179] MEMBLOCK configuration: [ 0.207057] memory size = 0x0000027d0d194000 reserved size = 0x0000000009ed2c00 [ 0.215299] memory.cnt = 0xb ... [ 0.311192] memory[0x9] [0x0000028357f00000-0x0000028357ffffff], 0x0000000000100000 bytes flags: 0x0 ... ... [ 0.419110] SEV-SNP: Reserving start/end of RMP table on a 2MB boundary [0x0000028357e00000] [ 0.428514] e820: update [mem 0x28357e00000-0x28357ffffff] usable ==> reserved [ 0.428517] e820: update [mem 0x28357e00000-0x28357ffffff] usable ==> reserved [ 0.428520] e820: update [mem 0x28357e00000-0x28357ffffff] usable ==> reserved ... ... [ 5.604051] MEMBLOCK configuration: [ 5.607922] memory size = 0x0000027d0d194000 reserved size = 0x0000000011faae02 [ 5.616163] memory.cnt = 0xe ... [ 5.754525] memory[0xc] [0x0000028357f00000-0x0000028357ffffff], 0x0000000000100000 bytes on node 0 flags: 0x0 ... ... [ 10.080295] Early memory node ranges[ 10.168065]...
node 0: [mem 0x0000028357f00000-0x0000028357ffffff]
...
...[ 8149.348948] SEV-SNP: RMPUPDATE failed for PFN 28357f7c, pg_level: 1, ret: 2As shown above, the memblock allocations show 1MB after the end of the RMP as available for allocation, which is what the RMP table fixups has made reserved. This memory range subsequently gets allocated as SNP guest memory, resulting in an RMPUPDATE failure.
This can potentially be fixed by not reserving the memory range in the e820 table, but that causes kexec failures when using the
KEXEC_FILE_LOADsyscall.The solution is to use
memblock_reserve()to mark the memory reserved for the system, ensuring that it cannot be allocated to an SNP guest.Since HV-Fixed memory is still readable/writable by the host, this only ends up being a problem if the memory in this range requires a page state change, which generally will only happen when allocating memory in this range to be used for running SNP guests, which is now possible with the SNP hypervisor support in kernel 6.11.
Quoted 6 lines
For this reason, this patch is not being marked for stable, even though it fixes handling that was introduced in kernel 6.9.
Fixes 400fea4b9651 ("x86/sev: Add callback to apply RMP table fixups for kexec")Suggested-by Thomas Lendacky <[email protected]>Signed-off-by Ashish Kalra <[email protected]>
Quoted 17 lines
--- arch/x86/virt/svm/sev.c | 2 ++ 1 file changed, 2 insertions(+)arch/x86/virt/svm/sev.c +2
-0@@ -173,6 +173,8 @@ static void __init __snp_fixup_e820_tables(u64 pa)e820__range_update (pa,PMD_SIZE ,E820_TYPE_RAM ,E820_TYPE_RESERVED );e820__range_update_table (e820_table_kexec, pa,PMD_SIZE ,E820_TYPE_RAM ,E820_TYPE_RESERVED );e820__range_update_table (e820_table_firmware, pa,PMD_SIZE ,E820_TYPE_RAM ,E820_TYPE_RESERVED ); +if (!memblock_is_region_reserved (pa,PMD_SIZE )) +memblock_reserve (pa,PMD_SIZE ); } }
[tip: x86/urgent] x86/sev: Ensure that RMP table fixups are reserved
In reply to Ashish KalraThe following commit has been merged into the x86/urgent branch of tip:
The following commit has been merged into the x86/urgent branch of tip:
Commit-ID: 88a921aa3c6b006160d6a46a231b8b32227e8196 Gitweb: https://git.kernel.org/tip/88a921aa3c6b006160d6a46a231b8b32227e8196 Author: Ashish Kalra <[email protected]> AuthorDate: Thu, 15 Aug 2024 22:16:30 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Wed, 23 Oct 2024 12:34:06 +02:00
x86/sev: Ensure that RMP table fixups are reserved
The BIOS reserves RMP table memory via e820 reservations. This can still lead to RMP page faults during kexec if the host tries to access memory within the same 2MB region.
Commit
400fea4b9651 ("x86/sev: Add callback to apply RMP table fixups for kexec"
adjusts the e820 reservations for the RMP table so that the entire 2MB range at the start/end of the RMP table is marked reserved.
The e820 reservations are then passed to firmware via SNP_INIT where they get marked HV-Fixed.
The RMP table fixups are done after the e820 ranges have been added to memblock, allowing the fixup ranges to still be allocated and used by the system.
The problem is that this memory range is now marked reserved in the e820 tables and during SNP initialization these reserved ranges are marked as HV-Fixed. This means that the pages cannot be used by an SNP guest, only by the hypervisor.
However, the memory management subsystem does not make this distinction and can allocate one of those pages to an SNP guest. This will ultimately result in RMPUPDATE failures associated with the guest, causing it to fail to start or terminate when accessing the HV-Fixed page.
The issue is captured below with memblock=debug:
[ 0.000000] SEV-SNP: *** DEBUG: snp_probe_rmptable_info:352 - rmp_base=0x280d4800000, rmp_end=0x28357efffff ... [ 0.000000] BIOS-provided physical RAM map: ... [ 0.000000] BIOS-e820: [mem 0x00000280d4800000-0x0000028357efffff] reserved [ 0.000000] BIOS-e820: [mem 0x0000028357f00000-0x0000028357ffffff] usable ... ... [ 0.183593] memblock add: [0x0000028357f00000-0x0000028357ffffff] e820__memblock_setup+0x74/0xb0 ... [ 0.203179] MEMBLOCK configuration: [ 0.207057] memory size = 0x0000027d0d194000 reserved size = 0x0000000009ed2c00 [ 0.215299] memory.cnt = 0xb ... [ 0.311192] memory[0x9] [0x0000028357f00000-0x0000028357ffffff], 0x0000000000100000 bytes flags: 0x0 ... ... [ 0.419110] SEV-SNP: Reserving start/end of RMP table on a 2MB boundary [0x0000028357e00000] [ 0.428514] e820: update [mem 0x28357e00000-0x28357ffffff] usable ==> reserved [ 0.428517] e820: update [mem 0x28357e00000-0x28357ffffff] usable ==> reserved [ 0.428520] e820: update [mem 0x28357e00000-0x28357ffffff] usable ==> reserved ... ... [ 5.604051] MEMBLOCK configuration: [ 5.607922] memory size = 0x0000027d0d194000 reserved size = 0x0000000011faae02 [ 5.616163] memory.cnt = 0xe ... [ 5.754525] memory[0xc] [0x0000028357f00000-0x0000028357ffffff], 0x0000000000100000 bytes on node 0 flags: 0x0 ... ... [ 10.080295] Early memory node ranges[ 10.168065] ... node 0: [mem 0x0000028357f00000-0x0000028357ffffff] ... ... [ 8149.348948] SEV-SNP: RMPUPDATE failed for PFN 28357f7c, pg_level: 1, ret: 2
As shown above, the memblock allocations show 1MB after the end of the RMP as available for allocation, which is what the RMP table fixups have reserved. This memory range subsequently gets allocated as SNP guest memory, resulting in an RMPUPDATE failure.
This can potentially be fixed by not reserving the memory range in the e820 table, but that causes kexec failures when using the KEXEC_FILE_LOAD syscall.
The solution is to use memblock_reserve() to mark the memory reserved for the system, ensuring that it cannot be allocated to an SNP guest.
Since HV-Fixed memory is still readable/writable by the host, this only ends up being a problem if the memory in this range requires a page state change, which generally will only happen when allocating memory in this range to be used for running SNP guests, which is now possible with the SNP hypervisor support in kernel 6.11.
Backporter note:
Fixes tag points to a 6.9 change but as the last paragraph above explains, this whole thing can happen after 6.11 received SNP HV support, therefore backporting to 6.9 is not really necessary.
[ bp: Massage commit message. ]
--- arch/x86/virt/svm/sev.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/virt/svm/sev.c +2 -0
@@ -173,6 +173,8 @@ static void __init __snp_fixup_e820_tables(u64 pa)e820__range_update (pa,PMD_SIZE ,E820_TYPE_RAM ,E820_TYPE_RESERVED );e820__range_update_table (e820_table_kexec, pa,PMD_SIZE ,E820_TYPE_RAM ,E820_TYPE_RESERVED );e820__range_update_table (e820_table_firmware, pa,PMD_SIZE ,E820_TYPE_RAM ,E820_TYPE_RESERVED ); +if (!memblock_is_region_reserved (pa,PMD_SIZE )) +memblock_reserve (pa,PMD_SIZE ); } }