x86/sev: Apply RMP table fixups for kexec.
PATCH 1/1 x86/sev: Apply RMP table fixups for kexec.
From: Ashish Kalra <[email protected]>
RMP table start and end physical range may not be aligned to 2MB in the e820 tables causing fatal RMP page faults during kexec boot when new page allocations are done in the same 2MB page as the RMP table. Check if RMP table start and end physical range in e820_table is not aligned to 2MB and in that case use e820__range_update() to map this range to reserved.
Override e820__memory_setup_default() to check and apply these RMP table fixups in e820_table before e820_table is used to setup e280_table_firmware and e820_table_kexec.
--- arch/x86/virt/svm/sev.c | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)
arch/x86/virt/svm/sev.c +52 -0
@@ -65,6 +65,8 @@ static u64 probed_rmp_base, probed_rmp_size;static struct rmpentry *rmptable__ro_after_init ;static u64 rmptable_max_pfn__ro_after_init ; +static char *__init snp_rmptable_e820_fixup (void ); +static LIST_HEAD (snp_leaked_pages_list);static DEFINE_SPINLOCK (snp_leaked_pages_list_lock);
@@ -160,9 +162,59 @@ bool snp_probe_rmptable_info(void)pr_info ("RMP table physical range [0x%016llx - 0x%016llx]\n" , probed_rmp_base, probed_rmp_base + probed_rmp_size -1 ); +/* +* Override e820__memory_setup_default() to do any RMP table fixups +* for kexec if required. +*/ + x86_init.resources.memory_setup = snp_rmptable_e820_fixup; +return true; } +/* +* Override e820__memory_setup_default() to do any RMP table fixups +* in e820_table before e820_table_firmware and e820_table_kexec +* are setup. +*/ +static char *__init snp_rmptable_e820_fixup (void ) +{ +/* Populate e820_table from BIOS-supplied e820 map */ +char *p =e820__memory_setup_default (); +u64 pa; + +/* +* RMP table start & end physical range may not be aligned to 2MB in the +* e820 tables causing fatal RMP page faults during kexec boot when new +* page allocations are done in the same 2MB page as the RMP table. +* Check if RMP table start & end physical range in e820_table is not aligned +* to 2MB and in that case use e820__range_update() to map this range to reserved, +* e820__range_update() nicely handles partial range update and also +* merges any consecutive ranges of the same type. +* Need to override e820__memory_setup_default() to check and apply +* fixups in e820_table before e820_table is used to setup +* e280_table_firmware and e820_table_kexec. +*/ + pa = probed_rmp_base; +if (!IS_ALIGNED (pa,PMD_SIZE )) { + pa =ALIGN_DOWN (pa,PMD_SIZE ); +if (e820__mapped_any (pa, pa +PMD_SIZE ,E820_TYPE_RAM )) { +pr_info ("Reserving start of RMP table on a 2MB boundary [0x%016llx]\n" , pa); +e820__range_update (pa,PMD_SIZE ,E820_TYPE_RAM ,E820_TYPE_RESERVED ); + } + } + + pa = probed_rmp_base + probed_rmp_size; +if (!IS_ALIGNED (pa,PMD_SIZE )) { + pa =ALIGN_DOWN (pa,PMD_SIZE ); +if (e820__mapped_any (pa, pa +PMD_SIZE ,E820_TYPE_RAM )) { +pr_info ("Reserving end of RMP table on a 2MB boundary [0x%016llx]\n" , pa); +e820__range_update (pa,PMD_SIZE ,E820_TYPE_RAM ,E820_TYPE_RESERVED ); + } + } + +return p; +} +/* * Do the necessary preparations which are verified by the firmware as * described in the SNP_INIT_EX firmware command description in the SNP
Signature · 1 line
2.34.1
Quoted 82 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
RMP table start and end physical range may not be aligned to 2MB in the e820 tables causing fatal RMP page faults during kexec boot when new page allocations are done in the same 2MB page as the RMP table. Check if RMP table start and end physical range in
e820_tableis not aligned to 2MB and in that case usee820__range_update()to map this range to reserved.Override
e820__memory_setup_default()to check and apply these RMP table fixups ine820_tablebeforee820_tableis used to setupe280_table_firmwareande820_table_kexec.Fixes c3b86e61b756 ("x86/cpufeatures: Enable/unmask SEV-SNP CPU feature")Signed-off-by Ashish Kalra <[email protected]>--- arch/x86/virt/svm/sev.c | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)arch/x86/virt/svm/sev.c +45
-0@@ -65,6 +65,8 @@ static u64 probed_rmp_base, probed_rmp_size;static struct rmpentry *rmptable__ro_after_init ;static u64 rmptable_max_pfn__ro_after_init ; +static char *__init snp_rmptable_e820_fixup (void ); +static LIST_HEAD (snp_leaked_pages_list);static DEFINE_SPINLOCK (snp_leaked_pages_list_lock);@@ -160,9 +162,59 @@ bool snp_probe_rmptable_info(void)pr_info ("RMP table physical range [0x%016llx - 0x%016llx]\n" , probed_rmp_base, probed_rmp_base + probed_rmp_size -1 ); +/* +* Override e820__memory_setup_default() to do any RMP table fixups +* for kexec if required. +*/ + x86_init.resources.memory_setup = snp_rmptable_e820_fixup; +return true; } +/* +* Override e820__memory_setup_default() to do any RMP table fixups +* in e820_table before e820_table_firmware and e820_table_kexec +* are setup. +*/ +static char *__init snp_rmptable_e820_fixup (void ) +{ +/* Populate e820_table from BIOS-supplied e820 map */ +char *p =e820__memory_setup_default (); +u64 pa; + +/* +* RMP table start & end physical range may not be aligned to 2MB in the +* e820 tables causing fatal RMP page faults during kexec boot when new +* page allocations are done in the same 2MB page as the RMP table. +* Check if RMP table start & end physical range in e820_table is not aligned +* to 2MB and in that case use e820__range_update() to map this range to reserved, +* e820__range_update() nicely handles partial range update and also +* merges any consecutive ranges of the same type. +* Need to override e820__memory_setup_default() to check and apply +* fixups in e820_table before e820_table is used to setup +* e280_table_firmware and e820_table_kexec. +*/ + pa = probed_rmp_base; +if (!IS_ALIGNED (pa,PMD_SIZE )) { + pa =ALIGN_DOWN (pa,PMD_SIZE ); +if (e820__mapped_any (pa, pa +PMD_SIZE ,E820_TYPE_RAM )) { +pr_info ("Reserving start of RMP table on a 2MB boundary [0x%016llx]\n" , pa); +e820__range_update (pa,PMD_SIZE ,E820_TYPE_RAM ,E820_TYPE_RESERVED ); + } + } + + pa = probed_rmp_base + probed_rmp_size; +if (!IS_ALIGNED (pa,PMD_SIZE )) { + pa =ALIGN_DOWN (pa,PMD_SIZE ); +if (e820__mapped_any (pa, pa +PMD_SIZE ,E820_TYPE_RAM )) { +pr_info ("Reserving end of RMP table on a 2MB boundary [0x%016llx]\n" , pa);
arch/x86/virt/svm/sev.c bool snp_probe_rmptable_info(void) · line 220
Quoted 10 lines
arch/x86/virt/svm/sev.c +7
-0+e820__range_update (pa,PMD_SIZE ,E820_TYPE_RAM ,E820_TYPE_RESERVED ); + } + } + +return p; +} +/* * Do the necessary preparations which are verified by the firmware as * described in the SNP_INIT_EX firmware command description in the SNP
Tested this patch, it fixes the kexec issue reported. Thank you.
From: Borislav Petkov <[email protected]>
Quoted 4 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
RMP table start and end physical range may not be aligned to 2MB in the e820 tables
This already sounds fishy. Why may the range not be aligned? This is BIOS, right? And BIOS can be fixed to align them properly.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 10 lines · On 4/2/24 09:45, [email protected] wrote:
From: Borislav Petkov <[email protected]>
Quoted 4 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
RMP table start and end physical range may not be aligned to 2MB in the e820 tables
This already sounds fishy. Why may the range not be aligned? This is BIOS, right? And BIOS can be fixed to align them properly.
There's no requirement from a hardware/RMP usage perspective that requires a 2MB alignment, so BIOS is not doing anything wrong. The problem occurs because kexec is initially using 2MB mappings that overlap the start and/or end of the RMP which then results in an RMP fault when memory within one of those 2MB mappings, that is not part of the RMP, is referenced.
Additionally, we have BIOSes out there since Milan that don't do this 2MB alignment. And do you really trust that BIOS will do this properly all the time?
I think it needs to be checked and mitigated in the kernel.
Thanks,
Tom
Quoted 1 line
From: Borislav Petkov <[email protected]>
Quoted 5 lines · Tom Lendacky
There's no requirement from a hardware/RMP usage perspective that requires a 2MB alignment, so BIOS is not doing anything wrong. The problem occurs because kexec is initially using 2MB mappings that overlap the start and/or end of the RMP which then results in an RMP fault when memory within one of those 2MB mappings, that is not part of the RMP, is referenced.
Then this explanation is misleading. And that whole bla about alignment is nonsense either.
Quoted 3 lines
Additionally, we have BIOSes out there since Milan that don't do this 2MB alignment. And do you really trust that BIOS will do this properly all the time?
I don't trust the BIOS to do anything properly.
So why isn't the fix for this simply to reserve the space for the RMP table to start at 2M page - even if it doesn't - and to cover the last chunk *also* with a 2M page and be done with it?
Not this silly overriding dance.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 11 lines · On 4/2/2024 11:34 AM, [email protected] wrote:
From: Borislav Petkov <[email protected]>
Quoted 5 lines · Tom Lendacky
There's no requirement from a hardware/RMP usage perspective that requires a 2MB alignment, so BIOS is not doing anything wrong. The problem occurs because kexec is initially using 2MB mappings that overlap the start and/or end of the RMP which then results in an RMP fault when memory within one of those 2MB mappings, that is not part of the RMP, is referenced.
Then this explanation is misleading. And that whole bla about alignment is nonsense either.
Quoted 8 lines
Quoted 3 lines
Additionally, we have BIOSes out there since Milan that don't do this 2MB alignment. And do you really trust that BIOS will do this properly all the time?
I don't trust the BIOS to do anything properly.
So why isn't the fix for this simply to reserve the space for the RMP table to start at 2M page - even if it doesn't - and to cover the last chunk *also* with a 2M page and be done with it?
But, it is the BIOS which reserves the space for the RMP table.
And if you mean the reservation in the kernel page tables (directmap) then that will not help as kexec uses it's own identity mapped page tables.
Quoted 2 lines
Not this silly overriding dance.
This overriding dance is required to fixup all the three kexec tables, as there is no interface/API available to do modifications/fixups in all the three possible kexec tables.
Thanks, Ashish
Quoted 2 lines
Thx.
Quoted 22 lines · Ashish
Quoted 11 lines · On 4/2/2024 11:34 AM, [email protected] wrote:
From: Borislav Petkov <[email protected]>
Quoted 5 lines · Tom Lendacky
There's no requirement from a hardware/RMP usage perspective that requires a 2MB alignment, so BIOS is not doing anything wrong. The problem occurs because kexec is initially using 2MB mappings that overlap the start and/or end of the RMP which then results in an RMP fault when memory within one of those 2MB mappings, that is not part of the RMP, is referenced.
Then this explanation is misleading. And that whole bla about alignment is nonsense either.
Quoted 8 lines
Quoted 3 lines
Additionally, we have BIOSes out there since Milan that don't do this 2MB alignment. And do you really trust that BIOS will do this properly all the time?
I don't trust the BIOS to do anything properly.
So why isn't the fix for this simply to reserve the space for the RMP table to start at 2M page - even if it doesn't - and to cover the last chunk *also* with a 2M page and be done with it?
Quoted 9 lines
But, it is the BIOS which reserves the space for the RMP table.
And if you mean the reservation in the kernel page tables (directmap) then that will not help as kexec uses it's own identity mapped page tables.
Quoted 2 lines
Not this silly overriding dance.
This overriding dance is required to fixup all the three kexec tables, as there is no interface/API available to do modifications/fixups in all the three possible kexec tables.
I meant the three e820 tables out of which two are used for kexec, the e820_table_firmware and e820_table_kexec.
Thanks, Ashish
Quoted 3 lines
Quoted 3 lines
Thx.
Borislav Petkov
maintainerQuoted 3 lines · Ashish
Quoted 3 lines
And if you mean the reservation in the kernel page tables (directmap) then that will not help as kexec uses it's own identity mapped page tables.
So how hard would it be to *always* reserve the chunk of memory at the 2M boundary where the RMP table starts and up to the 2M boundary where the RMP table ends?
In *every* kernel.
By default.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 9 lines · Borislav Petkov
Quoted 3 lines · Ashish
Quoted 3 lines
And if you mean the reservation in the kernel page tables (directmap) then that will not help as kexec uses it's own identity mapped page tables.
So how hard would it be to *always* reserve the chunk of memory at the 2M boundary where the RMP table starts and up to the 2M boundary where the RMP table ends?
In *every* kernel.
But, that's what the e820 table fixup is doing, it is reserving the chunk of memory at the 2M boundary where the RMP table starts and ends in the e820. Kexec is referring to the e820 table(s) for placing it's segments in memory for the kexec boot.
Thanks, Ashish
Quoted 5 lines
By default.
Thx.
Borislav Petkov
maintainerQuoted 1 line · Ashish
But, that's what the e820 table fixup is doing,
And can it be doing it in snp_probe_rmptable_info() where all the RMP table stuff is being done?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 4 lines · Borislav Petkov
Quoted 1 line · Ashish
But, that's what the e820 table fixup is doing,
And can it be doing it in
snp_probe_rmptable_info()where all the RMP table stuff is being done?
The e820 fixups has to be done on both e820_table_kexec and e820_table_firmware (to handle both kexec variants) and this has to be done when e820_table is used to populate both e820_table_kexec and e820_table_firmware and this is done in e820__memory_setup().
But, snp_probe_rmptable_info() is called as part of early_cpu_init() -> early_identify_cpu() which is called earlier than e820__memory_setup() and e820 tables are setup much later e820__memory_setup().
And we can't do this in snp_rmptable_init() as e820_table_firmware can't be fixed at that point and by that time this table has been mapped into sysfs (/sys/firmware) which is used by kexec -c variant.
The function e820__memory_setup() has an optional platform quirk available to override/pick up the firmware/bootloader e820 map, and that's why i am using this platform specific quirk to override it with the SNP specific RMP table e820 fixup function which internally calls e820__memory_setup_default() to pass the firmware/bootloader e820 map (set in e820_table) and then add RMP table fixups on top of it.
Thanks, Ashish
Borislav Petkov
maintainerQuoted 3 lines · Ashish
And we can't do this in
snp_rmptable_init()ase820_table_firmwarecan't be fixed at that point and by that time this table has been mapped into sysfs (/sys/firmware) which is used by kexec -c variant.
Well, you have to do something here because if snp_rmptable_init() late-disables SNP, your RMP table fixups are moot and invalid.
Which means, your RMP table fixups need to happen at the *very* *late* step after we know that SNP is enabled and won't get disabled anymore.
I.e., in snp_rmptable_init().
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 11 lines · Borislav Petkov
Quoted 3 lines · Ashish
And we can't do this in
snp_rmptable_init()ase820_table_firmwarecan't be fixed at that point and by that time this table has been mapped into sysfs (/sys/firmware) which is used by kexec -c variant.Well, you have to do something here because if
snp_rmptable_init()late-disables SNP, your RMP table fixups are moot and invalid.Which means, your RMP table fixups need to happen at the *very* *late* step after we know that SNP is enabled and won't get disabled anymore.
I.e., in
snp_rmptable_init().
The main issue with doing that in snp_rmptable_init() is that there is no e820 API interfaces available to update the e820_table_kexec and e820_table_firmware and e820_table_firmware has already been exposed to sysfs.
The e820 API only exports e820__range_update() which *only* fixes e820_table.
The important point to note here is that in most cases BIOS would have reserved RMP table start and end aligned to 2M boundary and setup the e820 table which the BIOS passes to the kernel as such, so even if the kernel does not enable SNP or disables SNP later, these reservations will remain aligned as such. So what we are doing here in-kernel fixups is doing the same alignment fixups which the BIOS would have done. The summary here is that e820 table adjustments for RMP table done either by BIOS and/or kernel will exist/remain even if SNP is not enabled by the kernel.
Thanks, Ashish
Quoted 12 lines · Ashish
Quoted 10 lines · Borislav Petkov
Quoted 3 lines · Ashish
And we can't do this in
snp_rmptable_init()ase820_table_firmwarecan't be fixed at that point and by that time this table has been mapped into sysfs (/sys/firmware) which is used by kexec -c variant.Well, you have to do something here because if
snp_rmptable_init()late-disables SNP, your RMP table fixups are moot and invalid.Which means, your RMP table fixups need to happen at the *very* *late* step after we know that SNP is enabled and won't get disabled anymore.
Quoted 8 lines
Quoted 1 line
I.e., in
snp_rmptable_init().The main issue with doing that in
snp_rmptable_init()is that there is no e820 API interfaces available to update thee820_table_kexecande820_table_firmwareande820_table_firmwarehas already been exposed to sysfs.The e820 API only exports
e820__range_update()which *only* fixese820_table.The important point to note here is that in most cases BIOS would have reserved RMP table start and end aligned to 2M boundary and setup the e820 table which the BIOS passes to the kernel as such, so even if the kernel does not enable SNP or disables SNP later, these reservations will remain aligned as such. So what we are doing here in-kernel fixups is doing the same alignment fixups which the BIOS would have done. The summary here is that e820 table adjustments for RMP table done either by BIOS and/or kernel will exist/remain even if SNP is not enabled by the kernel.
Again, to reiterate here, RMP table memory is reserved by BIOS regardless of the kernel enabling SNP (and also passed on the e820 map to ensure that kernel does not map anything in that memory), so any adjustments/fixups on top of that reserved memory should not matter, after all we don't free this reserved RMP table memory if kernel does not enable SNP.
Thanks, Ashish
Borislav Petkov
maintainerQuoted 4 lines · Ashish
The main issue with doing that in
snp_rmptable_init()is that there is no e820 API interfaces available to update thee820_table_kexecande820_table_firmwareande820_table_firmwarehas already been exposed to sysfs.
And?
You can't change it later? Tried?
Quoted 6 lines
The e820 API only exports
e820__range_update()which *only* fixese820_table.The important point to note here is that in most cases BIOS would have reserved RMP table start and end aligned to 2M boundary and setup the e820 table which the BIOS passes to the kernel as such,
So what was this "RMP table start and end physical range may not be aligned to 2MB" in your commit message?
/me is completely confused now.
Or does "most cases" mean that there can be cases where the RMP table placement in the BIOS is not 2M aligned and then the kexec kernel could try to allocate from within that chunk and there's RMP faults?
And you want to allocate those chunks up to the 2M boundary unconditionally, regardless of SNP enablement?
Now look at your original commit message and tell me how much of what came out on this thread, was in it?
Not a lot, I'd say...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 8 lines · Borislav Petkov
Quoted 4 lines · Ashish
The main issue with doing that in
snp_rmptable_init()is that there is no e820 API interfaces available to update thee820_table_kexecande820_table_firmwareande820_table_firmwarehas already been exposed to sysfs.And?
You can't change it later? Tried?
The main issue is there is no API interface available to do that, i will need to add new API interfaces to update the e820_table_kexec and e820_table_firmware and then will that be acceptable for a use case which can be handled via a platform specific quirk ?
Quoted 6 lines
Quoted 6 lines
The e820 API only exports
e820__range_update()which *only* fixese820_table.The important point to note here is that in most cases BIOS would have reserved RMP table start and end aligned to 2M boundary and setup the e820 table which the BIOS passes to the kernel as such,
Quoted 7 lines
So what was this "RMP table start and end physical range may not be aligned to 2MB" in your commit message?
/me is completely confused now.Or does "most cases" mean that there can be cases where the RMP table placement in the BIOS is not 2M aligned and then the kexec kernel could try to allocate from within that chunk and there's RMP faults?
Yes exactly, that's what the above comment means.
That's why the above commit message says "may".
Quoted 3 lines
And you want to allocate those chunks up to the 2M boundary unconditionally, regardless of SNP enablement?
My point is that we always keep the RMP table memory reserved regardless of SNP enablement, so these are simply fixups/adjustments on top of that reservation.
Thanks, Ashish
Quoted 5 lines
Now look at your original commit message and tell me how much of what came out on this thread, was in it?
Not a lot, I'd say...
Borislav Petkov
maintainerQuoted 4 lines · Ashish
The main issue is there is no API interface available to do that, i will need to add new API interfaces to update the
e820_table_kexecande820_table_firmwareand then will that be acceptable for a use case which can be handled via a platform specific quirk ?
I can't review a patch without seeing it first, sorry.
Quoted 3 lines
Yes exactly, that's what the above comment means.
That's why the above commit message says "may".
Yah, *never* use "may" in commit messages. This is internal speak and doesn't belong in the kernel. Your commit message need to explain stuff fully and properly. Otherwise I'll keep asking until it does.
Quoted 3 lines
My point is that we always keep the RMP table memory reserved regardless of SNP enablement, so these are simply fixups/adjustments on top of that reservation.
Yes.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 33 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
RMP table start and end physical range may not be aligned to 2MB in the e820 tables causing fatal RMP page faults during kexec boot when new page allocations are done in the same 2MB page as the RMP table. Check if RMP table start and end physical range in
e820_tableis not aligned to 2MB and in that case usee820__range_update()to map this range to reserved.Override
e820__memory_setup_default()to check and apply these RMP table fixups ine820_tablebeforee820_tableis used to setupe280_table_firmwareande820_table_kexec.Fixes c3b86e61b756 ("x86/cpufeatures: Enable/unmask SEV-SNP CPU feature")Signed-off-by Ashish Kalra <[email protected]>--- arch/x86/virt/svm/sev.c | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)arch/x86/virt/svm/sev.c +2
-0@@ -65,6 +65,8 @@ static u64 probed_rmp_base, probed_rmp_size;static struct rmpentry *rmptable__ro_after_init ;static u64 rmptable_max_pfn__ro_after_init ; +static char *__init snp_rmptable_e820_fixup (void ); +static LIST_HEAD (snp_leaked_pages_list);static DEFINE_SPINLOCK (snp_leaked_pages_list_lock);
arch/x86/virt/svm/sev.c bool snp_probe_rmptable_info(void) · line 169
Quoted 8 lines
arch/x86/virt/svm/sev.c +5
-0@@ -160,9 +162,59 @@ bool snp_probe_rmptable_info(void)pr_info ("RMP table physical range [0x%016llx - 0x%016llx]\n" , probed_rmp_base, probed_rmp_base + probed_rmp_size -1 ); +/* +* Override e820__memory_setup_default() to do any RMP table fixups +* for kexec if required. +*/ + x86_init.resources.memory_setup = snp_rmptable_e820_fixup;
This produces a build warning:
WARNING: modpost: vmlinux: section mismatch in reference: snp_probe_rmptable_info+0x95 (section: .text) -> x86_init (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: snp_probe_rmptable_info+0x99 (section: .text) -> snp_rmptable_e820_fixup (section: .init.text)
Thanks,
Tom
Quoted 3 lines
arch/x86/virt/svm/sev.c +1
-0+return true; }
Quoted 42 lines · Tom Lendacky
Quoted 31 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
RMP table start and end physical range may not be aligned to 2MB in the e820 tables causing fatal RMP page faults during kexec boot when new page allocations are done in the same 2MB page as the RMP table. Check if RMP table start and end physical range in
e820_tableis not aligned to 2MB and in that case usee820__range_update()to map this range to reserved.Override
e820__memory_setup_default()to check and apply these RMP table fixups ine820_tablebeforee820_tableis used to setupe280_table_firmwareande820_table_kexec.Fixes c3b86e61b756 ("x86/cpufeatures: Enable/unmask SEV-SNP CPU feature")Signed-off-by Ashish Kalra <[email protected]>---
arch/x86/virt/svm/sev.c| 52 +++++++++++++++++++++++++++++++++++++++++1 file changed, 52 insertions(+)arch/x86/virt/svm/sev.c +1
-0@@ -65,6 +65,8 @@ static u64 probed_rmp_base, probed_rmp_size; static struct rmpentry *rmptable __ro_after_init; static u64 rmptable_max_pfn __ro_after_init; +static char *__init snp_rmptable_e820_fixup(void); + static LIST_HEAD(snp_leaked_pages_list); static DEFINE_SPINLOCK(snp_leaked_pages_list_lock); @@ -160,9 +162,59 @@ bool snp_probe_rmptable_info(void)Quoted 7 lines
pr_info("RMP table physical range [0x%016llx - 0x%016llx]\n",
probed_rmp_base,probed_rmp_base+probed_rmp_size- 1); + /*
+ * Overridee820__memory_setup_default()to do any RMP table fixups + * for kexec if required.
+ */
+ x86_init.resources.memory_setup =snp_rmptable_e820_fixup;This produces a build warning:
Quoted 3 lines
WARNING: modpost: vmlinux: section mismatch in reference:
snp_probe_rmptable_info+0x95(section: .text) ->x86_init(section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference:snp_probe_rmptable_info+0x99(section: .text) ->snp_rmptable_e820_fixup(section: .init.text)
Oh, so this requires snp_probe_rmptable_info() to be fixed to use the __init macro.
I believe that snp_probe_rmptable_info() should be anyway using the __init macro and this fix for snp_probe_rmptable_info() needs to be sent as a separate patch and regardless of this patch getting merged or not.
Thanks, Ashish
Quoted 6 lines
Thanks,
TomQuoted 3 lines
+
return true;
}
Quoted 52 lines · Ashish
Quoted 43 lines · Tom Lendacky
Quoted 32 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
RMP table start and end physical range may not be aligned to 2MB in the e820 tables causing fatal RMP page faults during kexec boot when new page allocations are done in the same 2MB page as the RMP table. Check if RMP table start and end physical range in
e820_tableis not aligned to 2MB and in that case usee820__range_update()to map this range to reserved.Override
e820__memory_setup_default()to check and apply these RMP table fixups ine820_tablebeforee820_tableis used to setupe280_table_firmwareande820_table_kexec.Fixes c3b86e61b756 ("x86/cpufeatures: Enable/unmask SEV-SNP CPUfeature")
Signed-off-by Ashish Kalra <[email protected]>---
arch/x86/virt/svm/sev.c| 52 +++++++++++++++++++++++++++++++++++++++++1 file changed, 52 insertions(+)arch/x86/virt/svm/sev.c +1
-0@@ -65,6 +65,8 @@ static u64 probed_rmp_base, probed_rmp_size; static struct rmpentry *rmptable __ro_after_init; static u64 rmptable_max_pfn __ro_after_init; +static char *__init snp_rmptable_e820_fixup(void); + static LIST_HEAD(snp_leaked_pages_list); static DEFINE_SPINLOCK(snp_leaked_pages_list_lock); @@ -160,9 +162,59 @@ bool snp_probe_rmptable_info(void)Quoted 7 lines
pr_info("RMP table physical range [0x%016llx - 0x%016llx]\n",
probed_rmp_base,probed_rmp_base+probed_rmp_size- 1); + /*
+ * Overridee820__memory_setup_default()to do any RMP table fixups + * for kexec if required.
+ */
+ x86_init.resources.memory_setup =snp_rmptable_e820_fixup;This produces a build warning:
Quoted 7 lines
WARNING: modpost: vmlinux: section mismatch in reference:
snp_probe_rmptable_info+0x95(section: .text) ->x86_init(section:
.init.data)
WARNING: modpost: vmlinux: section mismatch in reference:snp_probe_rmptable_info+0x99(section: .text) ->snp_rmptable_e820_fixup(section: .init.text)
Quoted 6 lines
Oh, so this requires
snp_probe_rmptable_info()to be fixed to use the__initmacro.I believe that
snp_probe_rmptable_info()should be anyway using the__initmacro and this fix forsnp_probe_rmptable_info()needs to be sent as a separate patch and regardless of this patch getting merged or not.
I think you'll hit issues with:
bsp_determine_snp() -> //non-__init
snp_probe_rmptable_info() //__init
and bsp_determine_snp() sticks around as a function pointer assigned to cpuinfo_x86 so I don't think you can use __init there.
So might need to just drop __init from snp_rmptable_e820_fixup().
-Mike
Quoted 9 lines
Thanks, Ashish
Quoted 6 lines
Thanks,
TomQuoted 3 lines
+
return true;
}
Quoted 59 lines · Michael Roth
Quoted 50 lines · Ashish
Quoted 42 lines · Tom Lendacky
Quoted 29 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
RMP table start and end physical range may not be aligned to 2MB in the e820 tables causing fatal RMP page faults during kexec boot when new page allocations are done in the same 2MB page as the RMP table. Check if RMP table start and end physical range in
e820_tableis not aligned to 2MB and in that case usee820__range_update()to map this range to reserved.Override
e820__memory_setup_default()to check and apply these RMP table fixups ine820_tablebeforee820_tableis used to setupe280_table_firmwareande820_table_kexec.Fixes c3b86e61b756 ("x86/cpufeatures: Enable/unmask SEV-SNP CPUfeature")
Signed-off-by Ashish Kalra <[email protected]>--- arch/x86/virt/svm/sev.c | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)arch/x86/virt/svm/sev.c +1
-0@@ -65,6 +65,8 @@ static u64 probed_rmp_base, probed_rmp_size; static struct rmpentry *rmptable __ro_after_init; static u64 rmptable_max_pfn __ro_after_init; +static char *__init snp_rmptable_e820_fixup(void); +arch/x86/virt/svm/sev.c static u64 probed_rmp_base, probed_rmp_size;
Quoted 10 lines
arch/x86/virt/svm/sev.c +4
-0static LIST_HEAD(snp_leaked_pages_list); static DEFINE_SPINLOCK(snp_leaked_pages_list_lock); @@ -160,9 +162,59 @@ bool snp_probe_rmptable_info(void) pr_info("RMP table physical range [0x%016llx - 0x%016llx]\n", probed_rmp_base, probed_rmp_base + probed_rmp_size - 1); + /* + * Override e820__memory_setup_default() to do any RMP table fixups + * for kexec if required. + */ + x86_init.resources.memory_setup = snp_rmptable_e820_fixup;This produces a build warning:
Quoted 7 lines
WARNING: modpost: vmlinux: section mismatch in reference:
snp_probe_rmptable_info+0x95(section: .text) ->x86_init(section:
.init.data)
WARNING: modpost: vmlinux: section mismatch in reference:snp_probe_rmptable_info+0x99(section: .text) ->snp_rmptable_e820_fixup(section: .init.text)Quoted 6 lines
Oh, so this requires
snp_probe_rmptable_info()to be fixed to use the__initmacro.I believe that
snp_probe_rmptable_info()should be anyway using the__initmacro and this fix forsnp_probe_rmptable_info()needs to be sent as a separate patch and regardless of this patch getting merged or not.I think you'll hit issues with:
Quoted 7 lines
bsp_determine_snp() -> //non-__init snp_probe_rmptable_info() //__initand
bsp_determine_snp()sticks around as a function pointer assigned tocpuinfo_x86so I don't think you can use__initthere.So might need to just drop
__initfromsnp_rmptable_e820_fixup().
Actually, that will not help as snp_probe_rmptable_info() is *also* accessing x86_init.resources.memory_setup
Thanks, Ashish
Quoted 9 lines
Quoted 9 lines
Thanks, Ashish
Quoted 6 lines
Thanks,
TomQuoted 3 lines
+
return true;
}
Quoted 64 lines · Ashish
Quoted 62 lines · Michael Roth
Quoted 50 lines · Ashish
Quoted 42 lines · Tom Lendacky
Quoted 32 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
RMP table start and end physical range may not be aligned to 2MB in the e820 tables causing fatal RMP page faults during kexec boot when new page allocations are done in the same 2MB page as the RMP table. Check if RMP table start and end physical range in
e820_tableis not aligned to 2MB and in that case usee820__range_update()to map this range to reserved.Override
e820__memory_setup_default()to check and apply these RMP table fixups ine820_tablebeforee820_tableis used to setupe280_table_firmwareande820_table_kexec.Fixes c3b86e61b756 ("x86/cpufeatures: Enable/unmask SEV-SNP CPUfeature")
Signed-off-by Ashish Kalra <[email protected]>---
arch/x86/virt/svm/sev.c| 52 +++++++++++++++++++++++++++++++++++++++++1 file changed, 52 insertions(+)arch/x86/virt/svm/sev.c +1
-0@@ -65,6 +65,8 @@ static u64 probed_rmp_base, probed_rmp_size; static struct rmpentry *rmptable __ro_after_init; static u64 rmptable_max_pfn __ro_after_init; +static char *__init snp_rmptable_e820_fixup(void); + static LIST_HEAD(snp_leaked_pages_list); static DEFINE_SPINLOCK(snp_leaked_pages_list_lock); @@ -160,9 +162,59 @@ bool snp_probe_rmptable_info(void)Quoted 7 lines
pr_info("RMP table physical range [0x%016llx - 0x%016llx]\n",
probed_rmp_base,probed_rmp_base+probed_rmp_size- 1); + /*
+ * Overridee820__memory_setup_default()to do any RMP table fixups + * for kexec if required.
+ */
+ x86_init.resources.memory_setup =snp_rmptable_e820_fixup;This produces a build warning:
Quoted 7 lines
WARNING: modpost: vmlinux: section mismatch in reference:
snp_probe_rmptable_info+0x95(section: .text) ->x86_init(section:
.init.data)
WARNING: modpost: vmlinux: section mismatch in reference:snp_probe_rmptable_info+0x99(section: .text) ->snp_rmptable_e820_fixup(section: .init.text)Quoted 6 lines
Oh, so this requires
snp_probe_rmptable_info()to be fixed to use the__initmacro.I believe that
snp_probe_rmptable_info()should be anyway using the__initmacro and this fix forsnp_probe_rmptable_info()needs to be sent as a separate patch and regardless of this patch getting merged or not.I think you'll hit issues with:
bsp_determine_snp()-> //non-__init
snp_probe_rmptable_info()//__init
Quoted 8 lines
Quoted 4 lines
and
bsp_determine_snp()sticks around as a function pointer assigned tocpuinfo_x86so I don't think you can use__initthere.So might need to just drop
__initfromsnp_rmptable_e820_fixup().Actually, that will not help as
snp_probe_rmptable_info()is *also* accessing x86_init.resources.memory_setup
What if we flipped the whole flow? Borislav is adding a CC_ATTR to indicate HOST_SEV_SNP support, we don't need to clear X86_FEATURE_SEV_SNP at this phase (or at all for that matter). snp_probe_rmptable_info() can be done later during kernel init, once e820 has been parsed.
One way of doing this would be to override x86_init.resources.memory_setup() to do both snp_probe_rmptable_info() and snp_rmptable_e820_fixup().
What do you think?
Hello Jeremi,
Quoted 76 lines · Jeremi Piotrowski
Quoted 63 lines · Ashish
Quoted 62 lines · Michael Roth
Quoted 50 lines · Ashish
Quoted 42 lines · Tom Lendacky
Quoted 29 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
RMP table start and end physical range may not be aligned to 2MB in the e820 tables causing fatal RMP page faults during kexec boot when new page allocations are done in the same 2MB page as the RMP table. Check if RMP table start and end physical range in
e820_tableis not aligned to 2MB and in that case usee820__range_update()to map this range to reserved.Override
e820__memory_setup_default()to check and apply these RMP table fixups ine820_tablebeforee820_tableis used to setupe280_table_firmwareande820_table_kexec.Fixes c3b86e61b756 ("x86/cpufeatures: Enable/unmask SEV-SNP CPUfeature")
Signed-off-by Ashish Kalra <[email protected]>--- arch/x86/virt/svm/sev.c | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)arch/x86/virt/svm/sev.c +1
-0@@ -65,6 +65,8 @@ static u64 probed_rmp_base, probed_rmp_size; static struct rmpentry *rmptable __ro_after_init; static u64 rmptable_max_pfn __ro_after_init; +static char *__init snp_rmptable_e820_fixup(void); +arch/x86/virt/svm/sev.c static u64 probed_rmp_base, probed_rmp_size;
Quoted 10 lines
arch/x86/virt/svm/sev.c +4
-0static LIST_HEAD(snp_leaked_pages_list); static DEFINE_SPINLOCK(snp_leaked_pages_list_lock); @@ -160,9 +162,59 @@ bool snp_probe_rmptable_info(void) pr_info("RMP table physical range [0x%016llx - 0x%016llx]\n", probed_rmp_base, probed_rmp_base + probed_rmp_size - 1); + /* + * Override e820__memory_setup_default() to do any RMP table fixups + * for kexec if required. + */ + x86_init.resources.memory_setup = snp_rmptable_e820_fixup;This produces a build warning:
Quoted 7 lines
WARNING: modpost: vmlinux: section mismatch in reference:
snp_probe_rmptable_info+0x95(section: .text) ->x86_init(section:
.init.data)
WARNING: modpost: vmlinux: section mismatch in reference:snp_probe_rmptable_info+0x99(section: .text) ->snp_rmptable_e820_fixup(section: .init.text)Quoted 6 lines
Oh, so this requires
snp_probe_rmptable_info()to be fixed to use the__initmacro.I believe that
snp_probe_rmptable_info()should be anyway using the__initmacro and this fix forsnp_probe_rmptable_info()needs to be sent as a separate patch and regardless of this patch getting merged or not.I think you'll hit issues with:
bsp_determine_snp() -> //non-__init snp_probe_rmptable_info() //__initQuoted 7 lines
Quoted 4 lines
and
bsp_determine_snp()sticks around as a function pointer assigned tocpuinfo_x86so I don't think you can use__initthere.So might need to just drop
__initfromsnp_rmptable_e820_fixup().Actually, that will not help as
snp_probe_rmptable_info()is *also* accessing x86_init.resources.memory_setupWhat if we flipped the whole flow? Borislav is adding a
CC_ATTRto indicateHOST_SEV_SNPsupport, we don't need to clearX86_FEATURE_SEV_SNPat this phase (or at all for that matter).snp_probe_rmptable_info()can be done later during kernel init, once e820 has been parsed.
Quoted 4 lines
One way of doing this would be to override
x86_init.resources.memory_setup()to do bothsnp_probe_rmptable_info()andsnp_rmptable_e820_fixup().What do you think?
I like this idea of overriding x86_init.resources_memory_setup() to do both snp_probe_rmptable_info() and snp_rmptable_e820_fixup().
This callback seems to be meant for platform specific memory setup and SNP enabled RMP table platforms seem to be a good candidate to use this callback to probe RMP table, sanitize it and then apply e820 fixups or adjustments specific to the RMP table.
Additionally. setup_arch() -> e820__memory_setup() initializes all the three e820 tables (including the two tables used for kexec - e820_table_kexec and e820_table_firmware) and then there is *no* e820 API functions available to update e820_table_kexec and e820_table_firmware. Even if i add new e820 API interfaces to allow e820 memmap updates to e820_table_kexec and e820_table_firmware, before setup_arch() returns it calls e820__reserve_resources() which exposes the e820_table_firmware to sysfs (/sys/firmware/memmap) which is then used directly by kexec-tools.
I don't see any callbacks i can use to call any newly added e820 API to update e820_table_firmware before it is exposed to sysfs between setup_arch()->e820__memory_setup() and setup_arch()->e820__reserve_resources().
For the same reason, i tried to use initcalls to invoke snp_rmptable_e820_fixup() but then initcalls happen after setup_arch() and by that time e820_table_firmware has already been exposed to sysfs.
That's why i see x86_init.resources_memory_setup() an ideal callback to probe RMP table itself and then apply any RMP table platform specific quirks.
Thanks, Ashish