x86: vmx: Allow direct access to MSR_IA32_SPEC_CTRL
Quoted 8 lines · ----- [email protected] wrote:
Quoted 1 line · Konrad Rzeszutek Wilk
Quoted 1 line
To avoid the overhead of atomically saving and restoring the
MSR_IA32_SPEC_CTRLQuoted 1 line
Quoted 1 line
for guests that do not actually use the MSR, only
add_atomic_switch_msrwhen aQuoted 3 lines
Quoted 1 line
non-zero is written to it.
Quoted 8 lines
Quoted 4 lines
We tried this and found that it was about 3% slower that doing the old way of rdmsr and wrmsr.
But that was also with the host doing IBRS as well.
The common case will be that neither host nor guest are doing IBRS. Until the guest touches the MSR we do absolutely *nothing* with it, which is definitely the fastest option.
Windows use IBRS and Microsoft don't have any plans to switch to retpoline. Running a Windows guest should be a pretty common use-case no?
In addition, your handle of the first WRMSR intercept could be different. It could signal you to start doing the following:
1. Disable intercept on SPEC_CTRL MSR.
2. On VMEntry, Write vCPU SPEC_CTRL value into physical MSR.
3. On VMExit, read physical MSR into vCPU SPEC_CTRL value. (And if IBRS is used at host, also set physical SPEC_CTRL MSR here to 1)
That way, you will both have fastest option as long as guest don't use IBRS and also won't have the 3% performance hit compared to Konrad's proposal.
Am I missing something?
-Liran
Quoted 11 lines · Liran Alon
Windows use IBRS and Microsoft don't have any plans to switch to retpoline. Running a Windows guest should be a pretty common use-case no?
In addition, your handle of the first WRMSR intercept could be different. It could signal you to start doing the following:
1. Disable intercept onSPEC_CTRLMSR.
2. On VMEntry, Write vCPUSPEC_CTRLvalue into physical MSR.
3. On VMExit, read physical MSR into vCPUSPEC_CTRLvalue. (And if IBRS is used at host, also set physicalSPEC_CTRLMSR here to 1)
Quoted 4 lines
That way, you will both have fastest option as long as guest don't use IBRS and also won't have the 3% performance hit compared to Konrad's proposal.
Am I missing something?
Reads from the SPEC_CTRL MSR are strangely slow. I suspect a large part of the 3% speedup you observe is because in the above, the vmentry path doesn't need to *read* the host's value and store it; the host is expected to restore it for itself anyway?
I'd actually quite like to repeat the benchmark on the new fixed microcode, if anyone has it yet, to see if that read/swap slowness is still quite as excessive. I'm certainly not ruling this out, but I'm just a little wary of premature optimisation, and I'd like to make sure we have everything *else* in the KVM patches right first.
The fact that the save-and-restrict macros I have in the tip of my working tree at the moment are horrid and causing 0-day nastygrams, probably doesn't help persuade me to favour the approach ;)
... hm, the CPU actually has separate MSR save/restore lists for entry/exit, doesn't it? Is there any way to sanely make use of that and do the restoration manually on vmentry but let it be automatic on vmexit, by having it *only* in the guest's MSR-store area to be saved on exit and restored on exit, but *not* in the host's MSR-store area?
Reading the code and comparing with the SDM, I can't see where we're ever setting VM_EXIT_MSR_STORE_{ADDR,COUNT} except in the nested case...
Quoted 38 lines · David Woodhouse
Quoted 11 lines · Liran Alon
Windows use IBRS and Microsoft don't have any plans to switch to retpoline. Running a Windows guest should be a pretty common use-case no?
In addition, your handle of the first WRMSR intercept could be different. It could signal you to start doing the following:
1. Disable intercept onSPEC_CTRLMSR.
2. On VMEntry, Write vCPUSPEC_CTRLvalue into physical MSR.
3. On VMExit, read physical MSR into vCPUSPEC_CTRLvalue. (And if IBRS is used at host, also set physicalSPEC_CTRLMSR here to 1)Quoted 4 lines
That way, you will both have fastest option as long as guest don't use IBRS and also won't have the 3% performance hit compared to Konrad's proposal.
Am I missing something?
Reads from the
SPEC_CTRLMSR are strangely slow. I suspect a large part of the 3% speedup you observe is because in the above, the vmentry path doesn't need to *read* the host's value and store it; the host is expected to restore it for itself anyway?I'd actually quite like to repeat the benchmark on the new fixed microcode, if anyone has it yet, to see if that read/swap slowness is still quite as excessive. I'm certainly not ruling this out, but I'm just a little wary of premature optimisation, and I'd like to make sure we have everything *else* in the KVM patches right first.
The fact that the save-and-restrict macros I have in the tip of my working tree at the moment are horrid and causing 0-day nastygrams, probably doesn't help persuade me to favour the approach ;)
... hm, the CPU actually has separate MSR save/restore lists for entry/exit, doesn't it? Is there any way to sanely make use of that and do the restoration manually on vmentry but let it be automatic on vmexit, by having it *only* in the guest's MSR-store area to be saved on exit and restored on exit, but *not* in the host's MSR-store area?
Quoted 3 lines
Reading the code and comparing with the SDM, I can't see where we're ever setting VM_EXIT_MSR_STORE_{ADDR,COUNT} except in the nested case...
Hmmm ... you are probably right! I think all users of this interface always trap + update save area and never passthrough the MSR. That is why only LOAD is needed *so far*.
Okay, let me sort this out in v3 then.
Quoted 1 line
Amazon Development Center Germany GmbH
Berlin - Dresden - Aachen
main office: Krausenstr. 38, 10117 Berlin
Geschaeftsfuehrer: Dr. Ralf Herbrich, Christian Schlaeger
Ust-ID: DE289237879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B
Quoted 9 lines · KarimAllah Ahmed
Quoted 3 lines · David Woodhouse
Reading the code and comparing with the SDM, I can't see where we're ever setting VM_EXIT_MSR_STORE_{ADDR,COUNT} except in the nested case...
Hmmm ... you are probably right! I think all users of this interface always trap + update save area and never passthrough the MSR. That is why only LOAD is needed *so far*.
Okay, let me sort this out in v3 then.
I'm starting to think a variant of Ashok's patch might actually be the simpler approach, and not "premature optimisation". Especially if we need to support the !cpu_has_vmx_msr_bitmaps() case?
Start with vmx->spec_ctrl set to zero. When first touched, make it passthrough (but not atomically switched) and set a flag (e.g. "spec_ctrl_live") which triggers the 'restore_branch_speculation' and 'save_and_restrict_branch_speculation' behaviours. Except don't use those macros. Those can look something like
/* If this vCPU has touched SPEC_CTRL then restore its value if needed */ if (vmx->spec_ctrl_live && vmx->spec_ctrl) wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
/* vmentry is serialising on affected CPUs, so the conditional branch is safe */
... and, respectively, ...
/* If this vCPU has touched SPEC_CTRL then save its value and ensure we have zero */
if (vmx->spec_ctrl_live) {
rdmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
if (vmx->spec_ctrl)
wrmsrl(MSR_IA32_SPEC_CTRL, 0);}
Perhaps we can ditch the separate 'spec_ctrl_live' flag and check the pass-through MSR bitmap directly, in the case that it exists?
Paolo Bonzini
maintainerQuoted 10 lines · David Woodhouse
I'd actually quite like to repeat the benchmark on the new fixed microcode, if anyone has it yet, to see if that read/swap slowness is still quite as excessive. I'm certainly not ruling this out, but I'm just a little wary of premature optimisation, and I'd like to make sure we have everything *else* in the KVM patches right first.
The fact that the save-and-restrict macros I have in the tip of my working tree at the moment are horrid and causing 0-day nastygrams, probably doesn't help persuade me to favour the approach ;)
Quoted 5 lines
... hm, the CPU actually has separate MSR save/restore lists for entry/exit, doesn't it? Is there any way to sanely make use of that and do the restoration manually on vmentry but let it be automatic on vmexit, by having it *only* in the guest's MSR-store area to be saved on exit and restored on exit, but *not* in the host's MSR-store area?
Right now we don't even use the store-on-vmexit list at all, so the Simplest Patch That Can Possibly Work is definitely the one using rdmsr/wrmsr. It's not really premature optimization---though it doesn't hurt that it isn't awfully slow.
Paolo
Paolo Bonzini
maintainerQuoted 9 lines · David Woodhouse
Quoted 8 lines · KarimAllah Ahmed
Quoted 3 lines · David Woodhouse
Reading the code and comparing with the SDM, I can't see where we're ever setting VM_EXIT_MSR_STORE_{ADDR,COUNT} except in the nested case...
Hmmm ... you are probably right! I think all users of this interface always trap + update save area and never passthrough the MSR. That is why only LOAD is needed *so far*.
Quoted 5 lines
Quoted 1 line
Okay, let me sort this out in v3 then.
I'm starting to think a variant of Ashok's patch might actually be the simpler approach, and not "premature optimisation". Especially if we need to support the !cpu_has_vmx_msr_bitmaps() case?
That case is awfully slow anyway, it doesn't matter, but the direct-access flag would simply be always 0 if you have no MSR bitmaps.
Quoted 15 lines
Start with
vmx->spec_ctrlset to zero. When first touched, make it passthrough (but not atomically switched) and set a flag (e.g. "spec_ctrl_live") which triggers the 'restore_branch_speculation' and 'save_and_restrict_branch_speculation' behaviours. Except don't use those macros. Those can look something like/* If this vCPU has touched
SPEC_CTRLthen restore its value if needed */ if (vmx->spec_ctrl_live&&vmx->spec_ctrl)wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
/* vmentry is serialising on affected CPUs, so the conditional branch is safe */... and, respectively, ...
/* If this vCPU has touched
SPEC_CTRLthen save its value and ensure we have zero */
Quoted 8 lines
if (vmx->spec_ctrl_live) { rdmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl); if (vmx->spec_ctrl) wrmsrl(MSR_IA32_SPEC_CTRL, 0);}
Perhaps we can ditch the separate '
spec_ctrl_live' flag and check the pass-through MSR bitmap directly, in the case that it exists?
Probably a cache miss, or even a TLB miss if you're unlucky, so the separate flag is okay.
Paolo
Quoted 34 lines · David Woodhouse
Quoted 9 lines · KarimAllah Ahmed
Quoted 3 lines · David Woodhouse
Reading the code and comparing with the SDM, I can't see where we're ever setting VM_EXIT_MSR_STORE_{ADDR,COUNT} except in the nested case...
Hmmm ... you are probably right! I think all users of this interface always trap + update save area and never passthrough the MSR. That is why only LOAD is needed *so far*.
Okay, let me sort this out in v3 then.
I'm starting to think a variant of Ashok's patch might actually be the simpler approach, and not "premature optimisation". Especially if we need to support the !cpu_has_vmx_msr_bitmaps() case?
Start with
vmx->spec_ctrlset to zero. When first touched, make it passthrough (but not atomically switched) and set a flag (e.g. "spec_ctrl_live") which triggers the 'restore_branch_speculation' and 'save_and_restrict_branch_speculation' behaviours. Except don't use those macros. Those can look something like/* If this vCPU has touched
SPEC_CTRLthen restore its value if needed */ if (vmx->spec_ctrl_live&&vmx->spec_ctrl)wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
/* vmentry is serialising on affected CPUs, so the conditional branch is safe */... and, respectively, ...
/* If this vCPU has touched
SPEC_CTRLthen save its value and ensure we have zero */if (vmx->spec_ctrl_live) { rdmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl); if (vmx->spec_ctrl) wrmsrl(MSR_IA32_SPEC_CTRL, 0);
Quoted 5 lines
}
Perhaps we can ditch the separate '
spec_ctrl_live' flag and check the pass-through MSR bitmap directly, in the case that it exists?
Or the cpuid_flag as that would determine whether the MSR bitmap intercept is set or not.
Quoted 15 lines · David Woodhouse
Quoted 14 lines · Liran Alon
Windows use IBRS and Microsoft don't have any plans to switch to retpoline. Running a Windows guest should be a pretty common use-case no?
In addition, your handle of the first WRMSR intercept could be different. It could signal you to start doing the following:
1. Disable intercept onSPEC_CTRLMSR.
2. On VMEntry, Write vCPUSPEC_CTRLvalue into physical MSR.
3. On VMExit, read physical MSR into vCPUSPEC_CTRLvalue. (And if IBRS is used at host, also set physicalSPEC_CTRLMSR here to 1)That way, you will both have fastest option as long as guest don't use IBRS and also won't have the 3% performance hit compared to Konrad's proposal.
Quoted 6 lines
Quoted 1 line
Am I missing something?
Reads from the
SPEC_CTRLMSR are strangely slow. I suspect a large part of the 3% speedup you observe is because in the above, the vmentry path doesn't need to *read* the host's value and store it; the host is expected to restore it for itself anyway?
Yes for at least the purpose of correctness. That is based on what I have heard is that you when you transition to a higher ring you have to write 1, then write zero when you transition back to lower rings. That is it is like a knob.
But then I heard that on some CPUs it is more like reset button and just writting 1 to IBRS is fine. But again, correctness here.
Quoted 11 lines
I'd actually quite like to repeat the benchmark on the new fixed microcode, if anyone has it yet, to see if that read/swap slowness is still quite as excessive. I'm certainly not ruling this out, but I'm just a little wary of premature optimisation, and I'd like to make sure we have everything *else* in the KVM patches right first.
The fact that the save-and-restrict macros I have in the tip of my working tree at the moment are horrid and causing 0-day nastygrams, probably doesn't help persuade me to favour the approach ;)
Quoted 5 lines
... hm, the CPU actually has separate MSR save/restore lists for entry/exit, doesn't it? Is there any way to sanely make use of that and do the restoration manually on vmentry but let it be automatic on vmexit, by having it *only* in the guest's MSR-store area to be saved on exit and restored on exit, but *not* in the host's MSR-store area?
Oh. That sounds sounds interesting
Quoted 4 lines
Reading the code and comparing with the SDM, I can't see where we're ever setting VM_EXIT_MSR_STORE_{ADDR,COUNT} except in the nested case...
Right. We (well Daniel Kiper, CC-ed) implemented it for this and that is where we got the numbers.
Daniel, you OK posting it here? Granted with the caveats thta it won't even compile against upstream as it was based on a distro kernel.
Quoted 23 lines · Konrad Rzeszutek Wilk
Quoted 15 lines · David Woodhouse
Quoted 14 lines · Liran Alon
Windows use IBRS and Microsoft don't have any plans to switch to retpoline. Running a Windows guest should be a pretty common use-case no?
In addition, your handle of the first WRMSR intercept could be different. It could signal you to start doing the following:
1. Disable intercept onSPEC_CTRLMSR.
2. On VMEntry, Write vCPUSPEC_CTRLvalue into physical MSR.
3. On VMExit, read physical MSR into vCPUSPEC_CTRLvalue. (And if IBRS is used at host, also set physicalSPEC_CTRLMSR here to 1)That way, you will both have fastest option as long as guest don't use IBRS and also won't have the 3% performance hit compared to Konrad's proposal.
Quoted 6 lines
Quoted 1 line
Am I missing something?
Reads from the
SPEC_CTRLMSR are strangely slow. I suspect a large part of the 3% speedup you observe is because in the above, the vmentry path doesn't need to *read* the host's value and store it; the host is expected to restore it for itself anyway?
Quoted 23 lines
Yes for at least the purpose of correctness. That is based on what I have heard is that you when you transition to a higher ring you have to write 1, then write zero when you transition back to lower rings. That is it is like a knob.
But then I heard that on some CPUs it is more like reset button and just writting 1 to IBRS is fine. But again, correctness here.
Quoted 16 lines
I'd actually quite like to repeat the benchmark on the new fixed microcode, if anyone has it yet, to see if that read/swap slowness is still quite as excessive. I'm certainly not ruling this out, but I'm just a little wary of premature optimisation, and I'd like to make sure we have everything *else* in the KVM patches right first.
The fact that the save-and-restrict macros I have in the tip of my working tree at the moment are horrid and causing 0-day nastygrams, probably doesn't help persuade me to favour the approach ;)
... hm, the CPU actually has separate MSR save/restore lists for entry/exit, doesn't it? Is there any way to sanely make use of that and do the restoration manually on vmentry but let it be automatic on vmexit, by having it *only* in the guest's MSR-store area to be saved on exit and restored on exit, but *not* in the host's MSR-store area?
s/on exit and restored on exit/on exit and restored on entry/?
Additionally, AIUI there is no "host's MSR-store area".
Quoted 1 line
Oh. That sounds sounds interesting
That is possible but you have to split add_atomic_switch_msr() accordingly.
Quoted 9 lines
Quoted 3 lines
Reading the code and comparing with the SDM, I can't see where we're ever setting VM_EXIT_MSR_STORE_{ADDR,COUNT} except in the nested case...
Right. We (well Daniel Kiper, CC-ed) implemented it for this and that is where we got the numbers.
Daniel, you OK posting it here? Granted with the caveats thta it won't even compile against upstream as it was based on a distro kernel.
Please look below...
arch/x86/kvm/vmx.c +48 -8
@@ -162,8 +162,10 @@ module_param(ple_window_max, int, S_IRUGO);extern const ulong vmx_return; -#define NR_AUTOLOAD_MSRS 8 -#define VMCS02_POOL_SIZE 1 +#define NR_AUTOLOAD_MSRS 8 +#define NR_AUTOSTORE_MSRS NR_AUTOLOAD_MSRS + +#define VMCS02_POOL_SIZE 1struct vmcs {u32 revision_id;
@@ -504,6 +506,10 @@ struct vcpu_vmx {struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS ];struct vmx_msr_entry host[NR_AUTOLOAD_MSRS ]; } msr_autoload; +struct msr_autostore { +unsigned nr; +struct vmx_msr_entry guest[NR_AUTOSTORE_MSRS ]; + } msr_autostore;struct {int loaded;u16 fs_sel, gs_sel, ldt_sel;
@@ -1704,6 +1710,37 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, m->host[i].value = host_val; } +static void add_atomic_store_msr (struct vcpu_vmx *vmx,unsigned msr) +{ +unsigned i; +struct msr_autostore *m = &vmx->msr_autostore; + +for (i =0 ; i < m->nr; ++i) +if (m->guest[i].index == msr) +return ; + +if (i ==NR_AUTOSTORE_MSRS ) { +pr_err ("Not enough msr store entries. Can't add msr %x\n" , msr); +BUG (); + } + + m->guest[i].index = msr; +vmcs_write32 (VM_EXIT_MSR_STORE_COUNT , ++m->nr); +} + +static u64 get_msr_vmcs_store (struct vcpu_vmx *vmx,unsigned msr) +{ +unsigned i; +struct msr_autostore *m = &vmx->msr_autostore; + +for (i =0 ; i < m->nr; ++i) +if (m->guest[i].index == msr) +return m->guest[i].value; + +pr_err ("Can't find msr %x in VMCS store\n" , msr); +BUG (); +} +static void reload_tss (void ) {/*
@@ -4716,6 +4753,7 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)#endif vmcs_write32 (VM_EXIT_MSR_STORE_COUNT ,0 ); +vmcs_write64 (VM_EXIT_MSR_STORE_ADDR ,__pa (vmx->msr_autostore.guest));vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT ,0 );vmcs_write64 (VM_EXIT_MSR_LOAD_ADDR ,__pa (vmx->msr_autoload.host));vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT ,0 );
@@ -8192,8 +8230,11 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) vmx->__launched = vmx->loaded_vmcs->launched; -if (ibrs_inuse) -wrmsrl (MSR_IA32_SPEC_CTRL , vmx->spec_ctrl); +if (ibrs_inuse) { +add_atomic_switch_msr (vmx,MSR_IA32_SPEC_CTRL , vmx->spec_ctrl, +SPEC_CTRL_FEATURE_ENABLE_IBRS ); +add_atomic_store_msr (vmx,MSR_IA32_SPEC_CTRL ); + } asm(/* Store host registers */
@@ -8317,12 +8358,11 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)#endif ); -if (ibrs_inuse) { -rdmsrl (MSR_IA32_SPEC_CTRL , vmx->spec_ctrl); -wrmsrl (MSR_IA32_SPEC_CTRL ,SPEC_CTRL_FEATURE_ENABLE_IBRS ); - }stuff_RSB (); +if (ibrs_inuse) + vmx->spec_ctrl =get_msr_vmcs_store (vmx,MSR_IA32_SPEC_CTRL ); +/* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ if (debugctlmsr)update_debugctlmsr (debugctlmsr);
By the way, I saw in one version of patches that you use own rdmsrl()/wrmsrl() functions to save/restore IBRS instead of using common ones (I mean native_rdmsrl()/native_wrmsrl(), safe__rdmsrl()/safe_wrmsrl(), etc.) available in the kernel. Could you explain why do you do that?
Daniel
Quoted 73 lines · Daniel Kiper
Quoted 23 lines · Konrad Rzeszutek Wilk
Quoted 15 lines · David Woodhouse
Quoted 14 lines · Liran Alon
Windows use IBRS and Microsoft don't have any plans to switch to retpoline. Running a Windows guest should be a pretty common use-case no?
In addition, your handle of the first WRMSR intercept could be different. It could signal you to start doing the following:
1. Disable intercept onSPEC_CTRLMSR.
2. On VMEntry, Write vCPUSPEC_CTRLvalue into physical MSR.
3. On VMExit, read physical MSR into vCPUSPEC_CTRLvalue. (And if IBRS is used at host, also set physicalSPEC_CTRLMSR here to 1)That way, you will both have fastest option as long as guest don't use IBRS and also won't have the 3% performance hit compared to Konrad's proposal.
Quoted 6 lines
Quoted 1 line
Am I missing something?
Reads from the
SPEC_CTRLMSR are strangely slow. I suspect a large part of the 3% speedup you observe is because in the above, the vmentry path doesn't need to *read* the host's value and store it; the host is expected to restore it for itself anyway?Quoted 23 lines
Yes for at least the purpose of correctness. That is based on what I have heard is that you when you transition to a higher ring you have to write 1, then write zero when you transition back to lower rings. That is it is like a knob.
But then I heard that on some CPUs it is more like reset button and just writting 1 to IBRS is fine. But again, correctness here.
Quoted 16 lines
I'd actually quite like to repeat the benchmark on the new fixed microcode, if anyone has it yet, to see if that read/swap slowness is still quite as excessive. I'm certainly not ruling this out, but I'm just a little wary of premature optimisation, and I'd like to make sure we have everything *else* in the KVM patches right first.
The fact that the save-and-restrict macros I have in the tip of my working tree at the moment are horrid and causing 0-day nastygrams, probably doesn't help persuade me to favour the approach ;)
... hm, the CPU actually has separate MSR save/restore lists for entry/exit, doesn't it? Is there any way to sanely make use of that and do the restoration manually on vmentry but let it be automatic on vmexit, by having it *only* in the guest's MSR-store area to be saved on exit and restored on exit, but *not* in the host's MSR-store area?
s/on exit and restored on exit/on exit and restored on entry/?
Additionally, AIUI there is no "host's MSR-store area".
Quoted 1 line
Oh. That sounds sounds interesting
That is possible but you have to split
add_atomic_switch_msr()accordingly.Quoted 9 lines
Quoted 3 lines
Reading the code and comparing with the SDM, I can't see where we're ever setting VM_EXIT_MSR_STORE_{ADDR,COUNT} except in the nested case...
Right. We (well Daniel Kiper, CC-ed) implemented it for this and that is where we got the numbers.
Daniel, you OK posting it here? Granted with the caveats thta it won't even compile against upstream as it was based on a distro kernel.
Please look below...
arch/x86/kvm/vmx.c +0
-0
arch/x86/kvm/vmx.c module_param(ple_window_max, int, S_IRUGO); · line 168
Quoted 9 lines
arch/x86/kvm/vmx.c +4
-2@@ -162,8 +162,10 @@ module_param(ple_window_max, int, S_IRUGO);extern const ulong vmx_return; -#define NR_AUTOLOAD_MSRS 8 -#define VMCS02_POOL_SIZE 1 +#define NR_AUTOLOAD_MSRS 8 +#define NR_AUTOSTORE_MSRS NR_AUTOLOAD_MSRS + +#define VMCS02_POOL_SIZE 1
I think you accidentally resurrected VMCS02_POOL_SIZE.
Quoted 19 lines
arch/x86/kvm/vmx.c +6
-0struct vmcs {u32 revision_id;@@ -504,6 +506,10 @@ struct vcpu_vmx {struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS ];struct vmx_msr_entry host[NR_AUTOLOAD_MSRS ]; } msr_autoload; +struct msr_autostore { +unsigned nr; +struct vmx_msr_entry guest[NR_AUTOSTORE_MSRS ]; + } msr_autostore;struct {int loaded;u16 fs_sel, gs_sel, ldt_sel;@@ -1704,6 +1710,37 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, m->host[i].value = host_val; } +static void add_atomic_store_msr (struct vcpu_vmx *vmx,unsigned msr) +{
arch/x86/kvm/vmx.c static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, · line 1724
Quoted 10 lines
arch/x86/kvm/vmx.c +10
-0+unsigned i; +struct msr_autostore *m = &vmx->msr_autostore; + +for (i =0 ; i < m->nr; ++i) +if (m->guest[i].index == msr) +return ; + +if (i ==NR_AUTOSTORE_MSRS ) { +pr_err ("Not enough msr store entries. Can't add msr %x\n" , msr); +BUG ();
I wouldn't panic the host for this. add_atomic_switch_msr() just emits a warning for the equivalent condition. (Resetting the vCPU might be better in both cases.)
Quoted 7 lines
arch/x86/kvm/vmx.c +7
-0+ } + + m->guest[i].index = msr; +vmcs_write32 (VM_EXIT_MSR_STORE_COUNT , ++m->nr); +} + +static u64 get_msr_vmcs_store (struct vcpu_vmx *vmx,unsigned msr)
arch/x86/kvm/vmx.c static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, · line 1741
Quoted 10 lines
arch/x86/kvm/vmx.c +10
-0+{ +unsigned i; +struct msr_autostore *m = &vmx->msr_autostore; + +for (i =0 ; i < m->nr; ++i) +if (m->guest[i].index == msr) +return m->guest[i].value; + +pr_err ("Can't find msr %x in VMCS store\n" , msr); +BUG ();
Same comment as above.
Quoted 14 lines
arch/x86/kvm/vmx.c +3
-0+} +static void reload_tss (void ) {/* @@ -4716,6 +4753,7 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)#endif vmcs_write32 (VM_EXIT_MSR_STORE_COUNT ,0 ); +vmcs_write64 (VM_EXIT_MSR_STORE_ADDR ,__pa (vmx->msr_autostore.guest));vmcs_write32 (VM_EXIT_MSR_LOAD_COUNT ,0 );vmcs_write64 (VM_EXIT_MSR_LOAD_ADDR ,__pa (vmx->msr_autoload.host));vmcs_write32 (VM_ENTRY_MSR_LOAD_COUNT ,0 );
arch/x86/kvm/vmx.c static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) · line 8237
Quoted 10 lines
arch/x86/kvm/vmx.c +5
-2@@ -8192,8 +8230,11 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) vmx->__launched = vmx->loaded_vmcs->launched; -if (ibrs_inuse) -wrmsrl (MSR_IA32_SPEC_CTRL , vmx->spec_ctrl); +if (ibrs_inuse) { +add_atomic_switch_msr (vmx,MSR_IA32_SPEC_CTRL , vmx->spec_ctrl, +SPEC_CTRL_FEATURE_ENABLE_IBRS ); +add_atomic_store_msr (vmx,MSR_IA32_SPEC_CTRL ); + }
If ibrs_inuse can be cleared dynamically, perhaps there should be:
} else {
clear_autostore_msr(vmx, MSR_IA32_SPEC_CTRL);
}Quoted 20 lines
arch/x86/kvm/vmx.c +3
-4asm(/* Store host registers */ @@ -8317,12 +8358,11 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)#endif ); -if (ibrs_inuse) { -rdmsrl (MSR_IA32_SPEC_CTRL , vmx->spec_ctrl); -wrmsrl (MSR_IA32_SPEC_CTRL ,SPEC_CTRL_FEATURE_ENABLE_IBRS ); - }stuff_RSB (); +if (ibrs_inuse) + vmx->spec_ctrl =get_msr_vmcs_store (vmx,MSR_IA32_SPEC_CTRL ); +/* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ if (debugctlmsr)update_debugctlmsr (debugctlmsr);
Quoted 6 lines
By the way, I saw in one version of patches that you use own rdmsrl()/wrmsrl() functions to save/restore IBRS instead of using common ones (I mean native_rdmsrl()/native_wrmsrl(), safe__rdmsrl()/safe_wrmsrl(), etc.) available in the kernel. Could you explain why do you do that?
Daniel
What about vmcs02?
If ibrs_inuse can be set dynamically, you may need the following in nested_vmx_vmexit:
vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.nr);