Add Guest API & Guest Kernel support for SEV live migration.
v6 cover Add Guest API & Guest Kernel support for SEV live migration.
From: Ashish Kalra <[email protected]>
The series adds guest api and guest kernel support for SEV live migration.
The patch series introduces a new hypercall. The guest OS can use this hypercall to notify the page encryption status. If the page is encrypted with guest specific-key then we use SEV command during the migration. If page is not encrypted then fallback to default. This new hypercall is invoked using paravirt_ops.
This section descibes how the SEV live migration feature is negotiated between the host and guest, the host indicates this feature support via KVM_FEATURE_CPUID. The guest firmware (OVMF) detects this feature and sets a UEFI enviroment variable indicating OVMF support for live migration, the guest kernel also detects the host support for this feature via cpuid and in case of an EFI boot verifies if OVMF also supports this feature by getting the UEFI enviroment variable and if it set then enables live migration feature on host by writing to a custom MSR, if not booted under EFI, then it simply enables the feature by again writing to the custom MSR.
Changes since v5:
- Add detailed comments and explanation why SEV hypercalls need to be made before apply_alternative() and how adding kvm_sev_hypercall3() function is abstracted using the early_set_memory_XX() interfaces invoking the paravirt_ops (pv_ops).
- Reverting to the earlier kvm_sev_hypercall3() interface after feedback from Sean that inversion of KVM_HYPERCALL to VMMCALL is causing issues and not going to work.
Changes since v4:
- Split the guest kernel support for SEV live migration and kexec support for live migration into separate patches.
Changes since v3:
- Add Code style fixes as per review from Boris.
Changes since v2:
- Add guest api patch to this patchset.
- Replace KVM_HC_PAGE_ENC_STATUS hypercall with the more generic KVM_HC_MAP_GPA_RANGE hypercall.
- Add WARN_ONCE() messages if address lookup fails during kernel page table walk while issuing KVM_HC_MAP_GPA_RANGE hypercall.
Changes since v1:
- Avoid having an SEV specific variant of kvm_hypercall3() and instead invert the default to VMMCALL.
Ashish Kalra (4):
KVM: X86: Introduce KVM_HC_MAP_GPA_RANGE hypercall
KVM: x86: invert KVM_HYPERCALL to default to VMMCALL
EFI: Introduce the new AMD Memory Encryption GUID. x86/kvm: Add guest support for detecting and enabling SEV Live
Migration feature.
Ashish Kalra (3):
EFI: Introduce the new AMD Memory Encryption GUID. x86/kvm: Add guest support for detecting and enabling SEV Live
Migration feature.
x86/kvm: Add kexec support for SEV Live Migration.
Brijesh Singh (2):
x86/kvm: Add AMD SEV specific Hypercall3
mm: x86: Invoke hypercall when page encryption status is changed
arch/x86/include/asm/kvm_para.h | 12 +++ arch/x86/include/asm/mem_encrypt.h | 4 + arch/x86/include/asm/paravirt.h | 6 ++ arch/x86/include/asm/paravirt_types.h | 1 + arch/x86/include/asm/set_memory.h | 1 + arch/x86/kernel/kvm.c | 107 ++++++++++++++++++++++++++ arch/x86/kernel/paravirt.c | 1 + arch/x86/mm/mem_encrypt.c | 72 ++++++++++++++--- arch/x86/mm/pat/set_memory.c | 6 ++ include/linux/efi.h | 1 + 10 files changed, 202 insertions(+), 9 deletions(-)
Signature · 1 line
2.17.1
PATCH v6 1/5 x86/kvm: Add AMD SEV specific Hypercall3
From: Brijesh Singh <[email protected]>
KVM hypercall framework relies on alternative framework to patch the VMCALL -> VMMCALL on AMD platform. If a hypercall is made before apply_alternative() is called then it defaults to VMCALL. The approach works fine on non SEV guest. A VMCALL would causes #UD, and hypervisor will be able to decode the instruction and do the right things. But when SEV is active, guest memory is encrypted with guest key and hypervisor will not be able to decode the instruction bytes.
To highlight the need to provide this interface, capturing the flow of apply_alternatives() :setup_arch() call init_hypervisor_platform() which detects the hypervisor platform the kernel is running under and then the hypervisor specific initialization code can make early hypercalls. For example, KVM specific initialization in case of SEV will try to mark the "__bss_decrypted" section's encryption state via early page encryption status hypercalls.
Now, apply_alternatives() is called much later when setup_arch() calls check_bugs(), so we do need some kind of an early, pre-alternatives hypercall interface. Other cases of pre-alternatives hypercalls include marking per-cpu GHCB pages as decrypted on SEV-ES and per-cpu apf_reason, steal_time and kvm_apic_eoi as decrypted for SEV generally.
Add SEV specific hypercall3, it unconditionally uses VMMCALL. The hypercall will be used by the SEV guest to notify encrypted pages to the hypervisor.
This kvm_sev_hypercall3() function is abstracted and used as follows :
All these early hypercalls are made through early_set_memory_XX() interfaces, which in turn invoke pv_ops (paravirt_ops).
This early_set_memory_XX() -> pv_ops.mmu.notify_page_enc_status_changed() is a generic interface and can easily have SEV, TDX and any other future platform specific abstractions added to it.
Currently, pv_ops.mmu.notify_page_enc_status_changed() callback is setup to invoke kvm_sev_hypercall3() in case of SEV.
Similarly, in case of TDX, pv_ops.mmu.notify_page_enc_status_changed() can be setup to a TDX specific callback.
--- arch/x86/include/asm/kvm_para.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/x86/include/asm/kvm_para.h +12 -0
@@ -83,6 +83,18 @@ static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,return ret; } +static inline long kvm_sev_hypercall3 (unsigned int nr,unsigned long p1, +unsigned long p2,unsigned long p3) +{ +long ret; + + asmvolatile ("vmmcall" + :"=a" (ret) + :"a" (nr),"b" (p1),"c" (p2),"d" (p3) + :"memory" ); +return ret; +} +#ifdef CONFIG_KVM_GUEST void kvmclock_init (void );void kvmclock_disable (void );
Signature · 1 line
2.17.1
Re: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Quoted 86 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
KVM hypercall framework relies on alternative framework to patch the VMCALL -> VMMCALL on AMD platform. If a hypercall is made before
apply_alternative()is called then it defaults to VMCALL. The approach works fine on non SEV guest. A VMCALL would causes #UD, and hypervisor will be able to decode the instruction and do the right things. But when SEV is active, guest memory is encrypted with guest key and hypervisor will not be able to decode the instruction bytes.To highlight the need to provide this interface, capturing the flow of
apply_alternatives():setup_arch()callinit_hypervisor_platform()which detects the hypervisor platform the kernel is running under and then the hypervisor specific initialization code can make early hypercalls. For example, KVM specific initialization in case of SEV will try to mark the "__bss_decrypted" section's encryption state via early page encryption status hypercalls.Now,
apply_alternatives()is called much later whensetup_arch()callscheck_bugs(), so we do need some kind of an early, pre-alternatives hypercall interface. Other cases of pre-alternatives hypercalls include marking per-cpu GHCB pages as decrypted on SEV-ES and per-cpuapf_reason,steal_timeandkvm_apic_eoias decrypted for SEV generally.Add SEV specific hypercall3, it unconditionally uses VMMCALL. The hypercall will be used by the SEV guest to notify encrypted pages to the hypervisor.
This
kvm_sev_hypercall3()function is abstracted and used as follows :
All these early hypercalls are made throughearly_set_memory_XX()interfaces, which in turn invokepv_ops(paravirt_ops).This
early_set_memory_XX()->pv_ops.mmu.notify_page_enc_status_changed()is a generic interface and can easily have SEV, TDX and any other future platform specific abstractions added to it.Currently,
pv_ops.mmu.notify_page_enc_status_changed()callback is setup to invokekvm_sev_hypercall3()in case of SEV.Similarly, in case of TDX,
pv_ops.mmu.notify_page_enc_status_changed()can be setup to a TDX specific callback.Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc Joerg Roedel <[email protected]>Cc Borislav Petkov <[email protected]>Cc Tom Lendacky <[email protected]>Reviewed-by Steve Rutherford <[email protected]>Reviewed-by Venu Busireddy <[email protected]>Signed-off-by Brijesh Singh <[email protected]>Signed-off-by Ashish Kalra <[email protected]>--- arch/x86/include/asm/kvm_para.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)arch/x86/include/asm/kvm_para.h +12
-1@@ -83,6 +83,18 @@ static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,return ret; } +static inline long kvm_sev_hypercall3 (unsigned int nr,unsigned long p1, +unsigned long p2,unsigned long p3) +{ +long ret; + + asmvolatile ("vmmcall" + :"=a" (ret) + :"a" (nr),"b" (p1),"c" (p2),"d" (p3) + :"memory" ); +return ret; +} +#ifdef CONFIG_KVM_GUEST void kvmclock_init (void );void kvmclock_disable (void ); --
Quoted 2 lines
2.17.1
Looking at these threads, this patch either:
1) Needs review/approval from a maintainer that is interested or
2) Should flip back to using alternative (as suggested by Sean). In
particular: ALTERNATIVE("vmmcall", "vmcall", ALT_NOT(X86_FEATURE_VMMCALL)). My understanding is that the advantage of this is that (after calling apply alternatives) you get exactly the same behavior as before. But before apply alternatives, you get the desired flipped behavior. The previous patch changed the behavior after apply alternatives in a very slight manner (if feature flags were not set, you'd get a different instruction).
I personally don't have strong feelings on this decision, but this decision does need to be made for this patch series to move forward.
I'd also be curious to hear Sean's opinion on this since he was vocal about this previously.
Thanks,
Steve
Sean Christopherson
maintainerRe: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Quoted 11 lines · Steve Rutherford
Looking at these threads, this patch either:
1) Needs review/approval from a maintainer that is interested or
2) Should flip back to using alternative (as suggested by Sean). In
particular:ALTERNATIVE("vmmcall", "vmcall", ALT_NOT(X86_FEATURE_VMMCALL)). My understanding is that the advantage of this is that (after calling apply alternatives) you get exactly the same behavior as before. But before apply alternatives, you get the desired flipped behavior. The previous patch changed the behavior after apply alternatives in a very slight manner (if feature flags were not set, you'd get a different instruction).
Quoted 5 lines
I personally don't have strong feelings on this decision, but this decision does need to be made for this patch series to move forward.
I'd also be curious to hear Sean's opinion on this since he was vocal about this previously.
Pulling in Ashish's last email from the previous thread, which I failed to respond to.
https://lore.kernel.org/all/20210820133223.GA28059@ashkalra_ubuntu_server/T/#u
Quoted 14 lines · Ashish Kalra
Quoted 13 lines · Sean Christopherson
Quoted 6 lines · Ashish
Quoted 6 lines · Ashish
I think it makes more sense to stick to the original approach/patch, i.e., introducing a new private hypercall interface like
kvm_sev_hypercall3()and let early paravirtualized kernel code invoke this private hypercall interface wherever required.I don't like the idea of duplicating code just because the problem is tricky to solve. Right now it's just one function, but it could balloon to multiple in the future. Plus there's always the possibility of a new, pre-alternatives
kvm_hypercall()being added in generic code, at which point using an SEV-specific variant gets even uglier.
...
Quoted 17 lines
Now,
apply_alternatives()is called much later whensetup_arch()callscheck_bugs(), so we do need some kind of an early, pre-alternatives hypercall interface.Other cases of pre-alternatives hypercalls include marking per-cpu GHCB pages as decrypted on SEV-ES and per-cpu
apf_reason,steal_timeandkvm_apic_eoias decrypted for SEV generally.Actually using this
kvm_sev_hypercall3()function may be abstracted quite nicely. All these early hypercalls are made throughearly_set_memory_XX()interfaces, which in turn invokepv_ops.Now,
pv_opscan have this SEV/TDX specific abstractions.Currently,
pv_ops.mmu.notify_page_enc_status_changed()callback is setup tokvm_sev_hypercall3()in case of SEV.
Quoted 6 lines
Similarly, in case of TDX,
pv_ops.mmu.notify_page_enc_status_changed()can be setup to a TDX specific callback.Therefore, this
early_set_memory_XX()->pv_ops.mmu.notify_page_enc_status_changed()is a generic interface and can easily have SEV, TDX and any other future platform specific abstractions added to it.
Unless there's some fundamental technical hurdle I'm overlooking, if pv_ops can be configured early enough to handle this, then so can alternatives. Adding notify_page_enc_status_changed() may be necessary in the future, e.g. for TDX or SNP, but IMO that is orthogonal to adding a generic, 100% redundant helper.
I appreciate that simply swapping the default from VMCALL->VMMCALL is a bit dirty since it gives special meaning to the default value, but if that's the argument against reusing kvm_hypercall3() then we should solve the early alternatives problem, not fudge around it.
Re: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Hello Sean, Steve,
Quoted 12 lines · Sean Christopherson
Quoted 11 lines · Steve Rutherford
Looking at these threads, this patch either:
1) Needs review/approval from a maintainer that is interested or
2) Should flip back to using alternative (as suggested by Sean). In
particular:ALTERNATIVE("vmmcall", "vmcall", ALT_NOT(X86_FEATURE_VMMCALL)). My understanding is that the advantage of this is that (after calling apply alternatives) you get exactly the same behavior as before. But before apply alternatives, you get the desired flipped behavior. The previous patch changed the behavior after apply alternatives in a very slight manner (if feature flags were not set, you'd get a different instruction).
This is simply a Hack, i don't think this is a good approach to take forward.
Quoted 49 lines
Quoted 5 lines
I personally don't have strong feelings on this decision, but this decision does need to be made for this patch series to move forward.
I'd also be curious to hear Sean's opinion on this since he was vocal about this previously.
Pulling in Ashish's last email from the previous thread, which I failed to respond to.
Quoted 14 lines · Ashish Kalra
Quoted 13 lines · Sean Christopherson
Quoted 6 lines · Ashish
Quoted 6 lines · Ashish
I think it makes more sense to stick to the original approach/patch, i.e., introducing a new private hypercall interface like
kvm_sev_hypercall3()and let early paravirtualized kernel code invoke this private hypercall interface wherever required.I don't like the idea of duplicating code just because the problem is tricky to solve. Right now it's just one function, but it could balloon to multiple in the future. Plus there's always the possibility of a new, pre-alternatives
kvm_hypercall()being added in generic code, at which point using an SEV-specific variant gets even uglier....
Quoted 20 lines
Now,
apply_alternatives()is called much later whensetup_arch()callscheck_bugs(), so we do need some kind of an early, pre-alternatives hypercall interface.Other cases of pre-alternatives hypercalls include marking per-cpu GHCB pages as decrypted on SEV-ES and per-cpu
apf_reason,steal_timeandkvm_apic_eoias decrypted for SEV generally.Actually using this
kvm_sev_hypercall3()function may be abstracted quite nicely. All these early hypercalls are made throughearly_set_memory_XX()interfaces, which in turn invokepv_ops.Now,
pv_opscan have this SEV/TDX specific abstractions.Currently,
pv_ops.mmu.notify_page_enc_status_changed()callback is setup tokvm_sev_hypercall3()in case of SEV.Similarly, in case of TDX,
pv_ops.mmu.notify_page_enc_status_changed()can be setup to a TDX specific callback.
Quoted 7 lines
Quoted 3 lines
Therefore, this
early_set_memory_XX()->pv_ops.mmu.notify_page_enc_status_changed()is a generic interface and can easily have SEV, TDX and any other future platform specific abstractions added to it.Unless there's some fundamental technical hurdle I'm overlooking, if
pv_opscan be configured early enough to handle this, then so can alternatives.
Now, as i mentioned earlier, apply_alternatives() is only called boot CPU identification has been done which is a lot of support code which may be dependent on earlier setup_arch() code and then it does CPU mitigtion selections before patching alternatives, again which may have dependencies on previous code paths in setup_arch(), so i am not sure if we can call apply_alternatives() earlier.
Maybe for a guest kernel and virtualized boot enviroment, CPU identification may not be as complicated as for a physical host, but still it may have dependencies on earlier architecture specific boot code.
Quoted 2 lines
Adding
notify_page_enc_status_changed()may be necessary in the future, e.g. for TDX or SNP, but IMO that is orthogonal to adding a generic, 100% redundant helper.
If we have to do this in the future and as Sean mentioned ealier that vmcall needs to be fixed for TDX (as it will cause a #VE), then why not add this abstraction right now ?
Thanks,
Ashish
Quoted 4 lines
I appreciate that simply swapping the default from
VMCALL->VMMCALLis a bit dirty since it gives special meaning to the default value, but if that's the argument against reusingkvm_hypercall3()then we should solve the early alternatives problem, not fudge around it.
Sean Christopherson
maintainerRe: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Quoted 1 line · Ashish Kalra
This is simply a Hack, i don't think this is a good approach to take forward.
But a clever hack ;-)
Quoted 9 lines
Quoted 2 lines
Unless there's some fundamental technical hurdle I'm overlooking, if
pv_opscan be configured early enough to handle this, then so can alternatives.Now, as i mentioned earlier,
apply_alternatives()is only called boot CPU identification has been done which is a lot of support code which may be dependent on earliersetup_arch()code and then it does CPU mitigtion selections before patching alternatives, again which may have dependencies on previous code paths insetup_arch(), so i am not sure if we can callapply_alternatives()earlier.
apply_alternatives() is a generic helper that can work on any struct alt_instr array, e.g. KVM_HYPERCALL can put its alternative into a different section that's patched as soon as the VMM is identified.
Quoted 11 lines
Maybe for a guest kernel and virtualized boot enviroment, CPU identification may not be as complicated as for a physical host, but still it may have dependencies on earlier architecture specific boot code.
Quoted 2 lines
Adding
notify_page_enc_status_changed()may be necessary in the future, e.g. for TDX or SNP, but IMO that is orthogonal to adding a generic, 100% redundant helper.If we have to do this in the future and as Sean mentioned ealier that vmcall needs to be fixed for TDX (as it will cause a #VE), then why not add this abstraction right now ?
I'm not objecting to adding a PV op, I'm objecting to kvm_sev_hypercall3(). If others disagree and feel it's the way forward, I certainly won't stand in the way, but IMO it's unnecessary code duplication.
Borislav Petkov
maintainerRe: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Quoted 3 lines · Sean Christopherson
apply_alternatives()is a generic helper that can work on any structalt_instrarray, e.g.KVM_HYPERCALLcan put its alternative into a different section that's patched as soon as the VMM is identified.
Where exactly in the boot process you wanna move it?
As Ashish says, you need the boot_cpu_data bits properly set before it runs.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteSean Christopherson
maintainerRe: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Quoted 6 lines · Borislav Petkov
Quoted 3 lines · Sean Christopherson
apply_alternatives()is a generic helper that can work on any structalt_instrarray, e.g.KVM_HYPERCALLcan put its alternative into a different section that's patched as soon as the VMM is identified.Where exactly in the boot process you wanna move it?
init_hypervisor_platform(), after x86_init.hyper.init_platform() so that the PV support can set the desired feature flags. Since kvm_hypercall*() is only used by KVM guests, set_cpu_cap(c, X86_FEATURE_VMMCALL) can be moved out of early_init_amd/hygon() and into kvm_init_platform().
Quoted 2 lines
As Ashish says, you need the
boot_cpu_databits properly set before it runs.
Another option would be to refactor apply_alternatives() to allow the caller to provide a different feature check mechanism than boot_cpu_has(), which I think would let us drop X86_FEATURE_VMMCALL, X86_FEATURE_VMCALL, and X86_FEATURE_VMW_VMMCALL from cpufeatures. That might get more than a bit gross though.
But like I said, if others think I'm over-engineering this...
Borislav Petkov
maintainerRe: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Quoted 4 lines · Sean Christopherson
init_hypervisor_platform(), afterx86_init.hyper.init_platform()so that the PV support can set the desired feature flags. Since kvm_hypercall*() is only used by KVM guests,set_cpu_cap(c, X86_FEATURE_VMMCALL)can be moved out of early_init_amd/hygon() and intokvm_init_platform().
See below.
Quoted 5 lines
Another option would be to refactor
apply_alternatives()to allow the caller to provide a different feature check mechanism thanboot_cpu_has(), which I think would let us dropX86_FEATURE_VMMCALL,X86_FEATURE_VMCALL, andX86_FEATURE_VMW_VMMCALLfrom cpufeatures. That might get more than a bit gross though.
Uuuf.
So here's what I'm seeing (line numbers given to show when stuff happens):
start_kernel
|-> 953: setup_arch
|-> 794: early_cpu_init
|-> 936: init_hypervisor_platform
|
|-> 1134: check_bugs
|-> alternative_instructionsat line 794 setup_arch() calls early_cpu_init() which would end up setting X86_FEATURE_VMMCALL on an AMD guest, based on CPUID information.
init_hypervisor_platform() happens after that.
The alternatives patching happens in check_bugs() at line 1134. Which means, if one would consider moving the patching up, one would have to audit all the code between line 953 and 1134, whether it does set_cpu_cap() or some of the other helpers to set or clear bits in boot_cpu_data which controls the patching.
So for that I have only one thing to say: can'o'worms. We tried to move the memblock allocations placement in the boot process and generated at least 4 regressions. I'm still testing the fix for the fix for the 4th regression.
So moving stuff in the fragile boot process makes my hair stand up.
Refactoring apply_alternatives() to patch only for X86_FEATURE_VMMCALL and then patch again, I dunno, this stuff is fragile and it might cause some other similarly nasty fallout. And those are hard to debug because one does not see immediately when boot_cpu_data features are missing and functionality is behaving differently because of that.
So what's wrong with:
kvm_hypercall3:
if (cpu_feature_enabled(X86_FEATURE_VMMCALL)) return kvm_sev_hypercall3(...); /* rest of code */
?
Dunno we probably had that already in those old versions and maybe that was shot down for another reason but it should get you what you want without having to test the world and more for regressions possibly happening from disturbing the house of cards called x86 boot order.
IMHO, I'd say.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Hello Boris,
Quoted 60 lines · Borislav Petkov
Quoted 4 lines · Sean Christopherson
init_hypervisor_platform(), afterx86_init.hyper.init_platform()so that the PV support can set the desired feature flags. Since kvm_hypercall*() is only used by KVM guests,set_cpu_cap(c, X86_FEATURE_VMMCALL)can be moved out of early_init_amd/hygon() and intokvm_init_platform().See below.
Quoted 5 lines
Another option would be to refactor
apply_alternatives()to allow the caller to provide a different feature check mechanism thanboot_cpu_has(), which I think would let us dropX86_FEATURE_VMMCALL,X86_FEATURE_VMCALL, andX86_FEATURE_VMW_VMMCALLfrom cpufeatures. That might get more than a bit gross though.Uuuf.
So here's what I'm seeing (line numbers given to show when stuff happens):
start_kernel
|-> 953:setup_arch|-> 794: early_cpu_init |-> 936: init_hypervisor_platform | |-> 1134: check_bugs |-> alternative_instructionsat line 794
setup_arch()callsearly_cpu_init()which would end up settingX86_FEATURE_VMMCALLon an AMD guest, based on CPUID information.
init_hypervisor_platform()happens after that.The alternatives patching happens in
check_bugs()at line 1134. Which means, if one would consider moving the patching up, one would have to audit all the code between line 953 and 1134, whether it doesset_cpu_cap()or some of the other helpers to set or clear bits inboot_cpu_datawhich controls the patching.So for that I have only one thing to say: can'o'worms. We tried to move the memblock allocations placement in the boot process and generated at least 4 regressions. I'm still testing the fix for the fix for the 4th regression.
So moving stuff in the fragile boot process makes my hair stand up.
Refactoring
apply_alternatives()to patch only forX86_FEATURE_VMMCALLand then patch again, I dunno, this stuff is fragile and it might cause some other similarly nasty fallout. And those are hard to debug because one does not see immediately whenboot_cpu_datafeatures are missing and functionality is behaving differently because of that.So what's wrong with:
kvm_hypercall3:if (cpu_feature_enabled(X86_FEATURE_VMMCALL)) return kvm_sev_hypercall3(...); /* rest of code */
Quoted 9 lines
?
Dunno we probably had that already in those old versions and maybe that was shot down for another reason but it should get you what you want without having to test the world and more for regressions possibly happening from disturbing the house of cards called x86 boot order.
IMHO, I'd say.
Thanks for the above explanation.
If we have to do this:
if (cpu_feature_enabled(X86_FEATURE_VMMCALL))
return kvm_sev_hypercall3(...);
Then isn't it cleaner to simply do it via the paravirt_ops interface, i.e, pv_ops.mmu.notify_page_enc_status_changed() where the callback is only set when SEV and live migration feature are supported and invoked through early_set_memory_decrypted()/encrypted().
Another memory encryption platform can set it's callback accordingly.
Thanks,
Ashish
Quoted 5 lines
Signature · 4 lines
Regards/Gruss, Boris. https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpeople.kernel.org%2Ftglx%2Fnotes-about-netiquette&data=04%7C01%7Cashish.kalra%40amd.com%7C02217ac26c444833d50208d97dacb5f0%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637679003031781718%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=1oXxchRABGifVoLwnXwQxQ7%2F%2FZpwGLqpGdma4Yz5sjw%3D&reserved=0
Borislav Petkov
maintainerRe: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Quoted 6 lines · Ashish Kalra
Then isn't it cleaner to simply do it via the
paravirt_opsinterface, i.e,pv_ops.mmu.notify_page_enc_status_changed()where the callback is only set when SEV and live migration feature are supported and invoked through early_set_memory_decrypted()/encrypted().Another memory encryption platform can set it's callback accordingly.
Yeah, that sounds even cleaner to me.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Quoted 10 lines · Borislav Petkov
Quoted 6 lines · Ashish Kalra
Then isn't it cleaner to simply do it via the
paravirt_opsinterface, i.e,pv_ops.mmu.notify_page_enc_status_changed()where the callback is only set when SEV and live migration feature are supported and invoked through early_set_memory_decrypted()/encrypted().Another memory encryption platform can set it's callback accordingly.
Yeah, that sounds even cleaner to me.
If I'm not mistaken, this is what the patch set does now?
Re: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Quoted 12 lines · Steve Rutherford
Quoted 7 lines · Borislav Petkov
Quoted 7 lines · Ashish Kalra
Then isn't it cleaner to simply do it via the
paravirt_opsinterface, i.e,pv_ops.mmu.notify_page_enc_status_changed()where the callback is only set when SEV and live migration feature are supported and invoked through early_set_memory_decrypted()/encrypted().
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
Another memory encryption platform can set it's callback accordingly.
Yeah, that sounds even cleaner to me.
If I'm not mistaken, this is what the patch set does now?
Yes that’s what I mentioned to Boris.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Quoted 1 line · Ashish
Yes that’s what I mentioned to Boris.
Right, and as far as I'm concerned, the x86 bits look ok to me and I'm fine with this going through the kvm tree.
There will be a conflict with this:
https://lkml.kernel.org/r/[email protected]
resulting in:
arch/x86/kernel/kvm.c: In function ‘setup_efi_kvm_sev_migration’:arch/x86/kernel/kvm.c:563:7: error: implicit declaration of function ‘sev_active’; did you mean ‘cpu_active’? [-Werror=implicit-function-declaration]
563 | if (!sev_active() ||
| ^~~~~~~~~~
| cpu_activecc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:277: arch/x86/kernel/kvm.o] Error 1 make[2]: *** Waiting for unfinished jobs....
make[1]: *** [scripts/Makefile.build:540: arch/x86/kernel] Error 2
make: *** [Makefile:1868: arch/x86] Error 2
make: *** Waiting for unfinished jobs....
but Paolo and I will figure out what to do - I'll likely have a separate branch out which he can merge and that sev_active() will need to be converted to
if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
which is trivial.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Hello Paolo,
With reference to Boris's ack below, are you going to go ahead and queue this patch-set to kvm tree ?
Or do you want me to work on improving on or fixing anything on this patch-set?
Thanks,
Ashish
Quoted 33 lines · Borislav Petkov
Quoted 1 line · Ashish
Yes that’s what I mentioned to Boris.
Right, and as far as I'm concerned, the x86 bits look ok to me and I'm fine with this going through the kvm tree.
There will be a conflict with this:
resulting in:
arch/x86/kernel/kvm.c: In function ‘setup_efi_kvm_sev_migration’:arch/x86/kernel/kvm.c:563:7: error: implicit declaration of function ‘sev_active’; did you mean ‘cpu_active’? [-Werror=implicit-function-declaration]563 | if (!sev_active() || | ^~~~~~~~~~ | cpu_activecc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:277:arch/x86/kernel/kvm.o] Error 1 make[2]: *** Waiting for unfinished jobs....
make[1]: *** [scripts/Makefile.build:540:arch/x86/kernel] Error 2
make: *** [Makefile:1868:arch/x86] Error 2
make: *** Waiting for unfinished jobs....but Paolo and I will figure out what to do - I'll likely have a separate branch out which he can merge and that
sev_active()will need to be converted toif (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
which is trivial.
Thx.
Re: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
In reply to Borislav PetkovQuoted 33 lines · Borislav Petkov
Quoted 1 line · Ashish
Yes that’s what I mentioned to Boris.
Right, and as far as I'm concerned, the x86 bits look ok to me and I'm fine with this going through the kvm tree.
There will be a conflict with this:
https://lkml.kernel.org/r/[email protected]
resulting in:
arch/x86/kernel/kvm.c: In function ‘setup_efi_kvm_sev_migration’:arch/x86/kernel/kvm.c:563:7: error: implicit declaration of function ‘sev_active’; did you mean ‘cpu_active’? [-Werror=implicit-function-declaration]563 | if (!sev_active() || | ^~~~~~~~~~ | cpu_activecc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:277:arch/x86/kernel/kvm.o] Error 1 make[2]: *** Waiting for unfinished jobs....
make[1]: *** [scripts/Makefile.build:540:arch/x86/kernel] Error 2
make: *** [Makefile:1868:arch/x86] Error 2
make: *** Waiting for unfinished jobs....but Paolo and I will figure out what to do - I'll likely have a separate branch out which he can merge and that
sev_active()will need to be converted toif (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
which is trivial.
Quoted 7 lines
Thx.
--
Regards/Gruss,
Boris.
Hey All,
Bumping this thread again, since I believe these patches are good to go.
Let me know if there is anything I can do to help here, Thanks,
Steve
Paolo Bonzini
maintainerRe: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Quoted 12 lines · Steve Rutherford
arch/x86/kernel/kvm.c: In function ‘setup_efi_kvm_sev_migration’:arch/x86/kernel/kvm.c:563:7: error: implicit declaration of function ‘sev_active’; did you mean ‘cpu_active’? [-Werror=implicit-function-declaration]563 | if (!sev_active() || | ^~~~~~~~~~ | cpu_activecc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:277:arch/x86/kernel/kvm.o] Error 1 make[2]: *** Waiting for unfinished jobs....
make[1]: *** [scripts/Makefile.build:540:arch/x86/kernel] Error 2
make: *** [Makefile:1868:arch/x86] Error 2
make: *** Waiting for unfinished jobs....
Quoted 5 lines
but Paolo and I will figure out what to do - I'll likely have a separate branch out which he can merge and that
sev_active()will need to be converted toif (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
Hi Boris,
can I just merge these patches via the KVM tree, since we're close to the end of the merge window and the cc_platform_has series has been merged?
Thanks,
Paolo
Borislav Petkov
maintainerRe: [PATCH v6 1/5] x86/kvm: Add AMD SEV specific Hypercall3
Quoted 2 lines · Paolo Bonzini
can I just merge these patches via the KVM tree, since we're close to the end of the merge window and the
cc_platform_hasseries has been merged?
Yes, please. All the pending tip stuff from the last round is already upstream.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v6 2/5 mm: x86: Invoke hypercall when page encryption status is changed
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
Invoke a hypercall when a memory region is changed from encrypted -> decrypted and vice versa. Hypervisor needs to know the page encryption status during the guest migration.
--- arch/x86/include/asm/paravirt.h | 6 +++ arch/x86/include/asm/paravirt_types.h | 1 + arch/x86/include/asm/set_memory.h | 1 + arch/x86/kernel/paravirt.c | 1 + arch/x86/mm/mem_encrypt.c | 67 +++++++++++++++++++++++---- arch/x86/mm/pat/set_memory.c | 6 +++ 6 files changed, 73 insertions(+), 9 deletions(-)
arch/x86/include/asm/paravirt.h +6 -0
@@ -97,6 +97,12 @@ static inline void paravirt_arch_exit_mmap(struct mm_struct *mm)PVOP_VCALL1 (mmu.exit_mmap, mm); } +static inline void notify_page_enc_status_changed (unsigned long pfn, +int npages,bool enc) +{ +PVOP_VCALL3 (mmu.notify_page_enc_status_changed, pfn, npages, enc); +} +#ifdef CONFIG_PARAVIRT_XXL static inline void load_sp0 (unsigned long sp0) {
arch/x86/include/asm/paravirt_types.h +1 -0
@@ -168,6 +168,7 @@ struct pv_mmu_ops {/* Hook for intercepting the destruction of an mm_struct. */ void (*exit_mmap)(struct mm_struct *mm); +void (*notify_page_enc_status_changed)(unsigned long pfn,int npages,bool enc);#ifdef CONFIG_PARAVIRT_XXL struct paravirt_callee_save read_cr2;
arch/x86/include/asm/set_memory.h +1 -0
@@ -83,6 +83,7 @@ int set_pages_rw(struct page *page, int numpages);int set_direct_map_invalid_noflush (struct page *page);int set_direct_map_default_noflush (struct page *page);bool kernel_page_present (struct page *page); +void notify_range_enc_status_changed (unsigned long vaddr,int npages,bool enc);extern int kernel_set_to_readonly;
arch/x86/kernel/paravirt.c +1 -0
@@ -296,6 +296,7 @@ struct paravirt_patch_template pv_ops = { (void (*)(struct mmu_gather *, void *))tlb_remove_page, .mmu.exit_mmap = paravirt_nop, + .mmu.notify_page_enc_status_changed = paravirt_nop, #ifdef CONFIG_PARAVIRT_XXL .mmu.read_cr2 = __PV_IS_CALLEE_SAVE(native_read_cr2),
arch/x86/mm/mem_encrypt.c +58 -9
105 lines not on this page · raw message
arch/x86/mm/pat/set_memory.c +6 -0
@@ -2020,6 +2020,12 @@ static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc)*/ cpa_flush (&cpa,0 ); +/* +* Notify hypervisor that a given memory range is mapped encrypted +* or decrypted. +*/ +notify_range_enc_status_changed (addr, numpages, enc); +return ret; }
Signature · 1 line
2.17.1
PATCH v6 3/5 EFI: Introduce the new AMD Memory Encryption GUID.
In reply to Ashish KalraFrom: Ashish Kalra <[email protected]>
Introduce a new AMD Memory Encryption GUID which is currently used for defining a new UEFI environment variable which indicates UEFI/OVMF support for the SEV live migration feature. This variable is setup when UEFI/OVMF detects host/hypervisor support for SEV live migration and later this variable is read by the kernel using EFI runtime services to verify if OVMF supports the live migration feature.
--- include/linux/efi.h | 1 + 1 file changed, 1 insertion(+)
include/linux/efi.h +1 -0
@@ -362,6 +362,7 @@ void efi_native_runtime_setup(void);/* OEM GUIDs */ #define DELLEMC_EFI_RCI2_TABLE_GUID EFI_GUID(0x2d9f28a2, 0xa886, 0x456a, 0x97, 0xa8, 0xf1, 0x1e, 0xf2, 0x4f, 0xf4, 0x55) +#define AMD_SEV_MEM_ENCRYPT_GUID EFI_GUID(0x0cf29b71, 0x9e51, 0x433a, 0xa3, 0xb7, 0x81, 0xf3, 0xab, 0x16, 0xb8, 0x75) typedef struct { efi_guid_t guid;
Signature · 1 line
2.17.1
PATCH v6 4/5 x86/kvm: Add guest support for detecting and enabling SEV Live Migration feature.
In reply to Ashish KalraFrom: Ashish Kalra <[email protected]>
The guest support for detecting and enabling SEV Live migration feature uses the following logic :
- kvm_init_plaform() checks if its booted under the EFI
- If not EFI,
i) if kvm_para_has_feature(KVM_FEATURE_MIGRATION_CONTROL), issue a wrmsrl() to enable the SEV live migration support
- If EFI,
i) If kvm_para_has_feature(KVM_FEATURE_MIGRATION_CONTROL), read the UEFI variable which indicates OVMF support for live migration
ii) the variable indicates live migration is supported, issue a wrmsrl() to
enable the SEV live migration support
The EFI live migration check is done using a late_initcall() callback.
Also, ensure that _bss_decrypted section is marked as decrypted in the hypervisor's guest page encryption status tracking.
--- arch/x86/include/asm/mem_encrypt.h | 4 ++ arch/x86/kernel/kvm.c | 82 ++++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt.c | 5 ++ 3 files changed, 91 insertions(+)
arch/x86/include/asm/mem_encrypt.h +4 -0
@@ -43,6 +43,8 @@ void __init sme_enable(struct boot_params *bp);int __init early_set_memory_decrypted (unsigned long vaddr,unsigned long size);int __init early_set_memory_encrypted (unsigned long vaddr,unsigned long size); +void __init early_set_mem_enc_dec_hypercall (unsigned long vaddr,int npages, +bool enc);void __init mem_encrypt_free_decrypted_mem (void );
@@ -83,6 +85,8 @@ static inline int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size) { return 0; }static inline int __init early_set_memory_encrypted (unsigned long vaddr,unsigned long size) {return 0 ; } +static inline void __init +early_set_mem_enc_dec_hypercall (unsigned long vaddr,int npages,bool enc) {}static inline void mem_encrypt_free_decrypted_mem (void ) { }
arch/x86/kernel/kvm.c +82 -0
114 lines not on this page · raw message
arch/x86/mm/mem_encrypt.c +5 -0
@@ -409,6 +409,11 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)return early_set_memory_enc_dec (vaddr, size, true); } +void __init early_set_mem_enc_dec_hypercall (unsigned long vaddr,int npages,bool enc) +{ +notify_range_enc_status_changed (vaddr, npages, enc); +} +/* * SME and SEV are very similar but they are not the same, so there are * times that the kernel will need to distinguish between SME and SEV. The
Signature · 1 line
2.17.1
PATCH v6 5/5 x86/kvm: Add kexec support for SEV Live Migration.
In reply to Ashish KalraFrom: Ashish Kalra <[email protected]>
Reset the host's shared pages list related to kernel specific page encryption status settings before we load a new kernel by kexec. We cannot reset the complete shared pages list here as we need to retain the UEFI/OVMF firmware specific settings.
The host's shared pages list is maintained for the guest to keep track of all unencrypted guest memory regions, therefore we need to explicitly mark all shared pages as encrypted again before rebooting into the new guest kernel.
--- arch/x86/kernel/kvm.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
arch/x86/kernel/kvm.c +25 -0
@@ -869,10 +869,35 @@ static void __init kvm_init_platform(void)if (sev_active () &&kvm_para_has_feature (KVM_FEATURE_MIGRATION_CONTROL )) {unsigned long nr_pages; +int i; pv_ops.mmu.notify_page_enc_status_changed = kvm_sev_hc_page_enc_status; +/* +* Reset the host's shared pages list related to kernel +* specific page encryption status settings before we load a +* new kernel by kexec. Reset the page encryption status +* during early boot intead of just before kexec to avoid SMP +* races during kvm_pv_guest_cpu_reboot(). +* NOTE: We cannot reset the complete shared pages list +* here as we need to retain the UEFI/OVMF firmware +* specific settings. +*/ + +for (i =0 ; i < e820_table->nr_entries; i++) { +struct e820_entry *entry = &e820_table->entries[i]; + +if (entry->type !=E820_TYPE_RAM ) +continue ; + + nr_pages =DIV_ROUND_UP (entry->size,PAGE_SIZE ); + +kvm_sev_hypercall3 (KVM_HC_MAP_GPA_RANGE , entry->addr, + nr_pages, +KVM_MAP_GPA_RANGE_ENCRYPTED |KVM_MAP_GPA_RANGE_PAGE_SZ_4K ); + } +/* * Ensure that _bss_decrypted section is marked as decrypted in the * shared pages list.
Signature · 1 line
2.17.1
Quoted 8 lines · Ashish Kalra
adds guest api and guest kernel support for SEV live migration.
The patch series introduces a new hypercall. The guest OS can use this hypercall to notify the page encryption status. If the page is encrypted with guest specific-key then we use SEV command during the migration. If page is not encrypted then fallback to default. This new hypercall is invoked using
paravirt_ops.
Quoted 10 lines
This section descibes how the SEV live migration feature is negotiated between the host and guest, the host indicates this feature support via
KVM_FEATURE_CPUID. The guest firmware (OVMF) detects this feature and sets a UEFI enviroment variable indicating OVMF support for live migration, the guest kernel also detects the host support for this feature via cpuid and in case of an EFI boot verifies if OVMF also supports this feature by getting the UEFI enviroment variable and if it set then enables live migration feature on host by writing to a custom MSR, if not booted under EFI, then it simply enables the feature by again writing to the custom MSR.
Queued, thanks.
Paolo