* [PATCH] KVM: nVMX: remove side effects from nested_vmx_exit_reflected
@ 2020-03-17 18:00 Paolo Bonzini
2020-03-18 10:52 ` Vitaly Kuznetsov
2020-03-19 20:11 ` Krish Sadhukhan
0 siblings, 2 replies; 5+ messages in thread
From: Paolo Bonzini @ 2020-03-17 18:00 UTC (permalink / raw)
To: linux-kernel, kvm; +Cc: vkuznets, sean.j.christopherson
The name of nested_vmx_exit_reflected suggests that it's purely
a test, but it actually marks VMCS12 pages as dirty. Move this to
vmx_handle_exit, observing that the initial nested_run_pending check in
nested_vmx_exit_reflected is pointless---nested_run_pending has just
been cleared in vmx_vcpu_run and won't be set until handle_vmlaunch
or handle_vmresume.
Suggested-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
arch/x86/kvm/vmx/nested.c | 18 ++----------------
arch/x86/kvm/vmx/nested.h | 1 +
arch/x86/kvm/vmx/vmx.c | 19 +++++++++++++++++--
3 files changed, 20 insertions(+), 18 deletions(-)
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 8578513907d7..4ff859c99946 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -3527,7 +3527,7 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
}
-static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
+void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
{
struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
gfn_t gfn;
@@ -5543,8 +5543,7 @@ bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
struct vcpu_vmx *vmx = to_vmx(vcpu);
struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
- if (vmx->nested.nested_run_pending)
- return false;
+ WARN_ON_ONCE(vmx->nested.nested_run_pending);
if (unlikely(vmx->fail)) {
trace_kvm_nested_vmenter_failed(
@@ -5553,19 +5552,6 @@ bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
return true;
}
- /*
- * The host physical addresses of some pages of guest memory
- * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
- * Page). The CPU may write to these pages via their host
- * physical address while L2 is running, bypassing any
- * address-translation-based dirty tracking (e.g. EPT write
- * protection).
- *
- * Mark them dirty on every exit from L2 to prevent them from
- * getting out of sync with dirty tracking.
- */
- nested_mark_vmcs12_pages_dirty(vcpu);
-
trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
vmcs_readl(EXIT_QUALIFICATION),
vmx->idt_vectoring_info,
diff --git a/arch/x86/kvm/vmx/nested.h b/arch/x86/kvm/vmx/nested.h
index 21d36652f213..f70968b76d33 100644
--- a/arch/x86/kvm/vmx/nested.h
+++ b/arch/x86/kvm/vmx/nested.h
@@ -33,6 +33,7 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
u32 vmx_instruction_info, bool wr, int len, gva_t *ret);
void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu);
+void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu);
bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
int size);
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index b447d66f44e6..07299a957d4a 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -5851,8 +5851,23 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,
if (vmx->emulation_required)
return handle_invalid_guest_state(vcpu);
- if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
- return nested_vmx_reflect_vmexit(vcpu, exit_reason);
+ if (is_guest_mode(vcpu)) {
+ /*
+ * The host physical addresses of some pages of guest memory
+ * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
+ * Page). The CPU may write to these pages via their host
+ * physical address while L2 is running, bypassing any
+ * address-translation-based dirty tracking (e.g. EPT write
+ * protection).
+ *
+ * Mark them dirty on every exit from L2 to prevent them from
+ * getting out of sync with dirty tracking.
+ */
+ nested_mark_vmcs12_pages_dirty(vcpu);
+
+ if (nested_vmx_exit_reflected(vcpu, exit_reason))
+ return nested_vmx_reflect_vmexit(vcpu, exit_reason);
+ }
if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
dump_vmcs();
--
1.8.3.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] KVM: nVMX: remove side effects from nested_vmx_exit_reflected
2020-03-17 18:00 [PATCH] KVM: nVMX: remove side effects from nested_vmx_exit_reflected Paolo Bonzini
@ 2020-03-18 10:52 ` Vitaly Kuznetsov
2020-03-18 10:59 ` Paolo Bonzini
2020-03-19 20:11 ` Krish Sadhukhan
1 sibling, 1 reply; 5+ messages in thread
From: Vitaly Kuznetsov @ 2020-03-18 10:52 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: sean.j.christopherson, linux-kernel, kvm
Paolo Bonzini <pbonzini@redhat.com> writes:
> The name of nested_vmx_exit_reflected suggests that it's purely
> a test, but it actually marks VMCS12 pages as dirty. Move this to
> vmx_handle_exit, observing that the initial nested_run_pending check in
> nested_vmx_exit_reflected is pointless---nested_run_pending has just
> been cleared in vmx_vcpu_run and won't be set until handle_vmlaunch
> or handle_vmresume.
>
> Suggested-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> arch/x86/kvm/vmx/nested.c | 18 ++----------------
> arch/x86/kvm/vmx/nested.h | 1 +
> arch/x86/kvm/vmx/vmx.c | 19 +++++++++++++++++--
> 3 files changed, 20 insertions(+), 18 deletions(-)
>
> diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
> index 8578513907d7..4ff859c99946 100644
> --- a/arch/x86/kvm/vmx/nested.c
> +++ b/arch/x86/kvm/vmx/nested.c
> @@ -3527,7 +3527,7 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
> }
>
>
> -static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
> +void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
> {
> struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
> gfn_t gfn;
> @@ -5543,8 +5543,7 @@ bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
> struct vcpu_vmx *vmx = to_vmx(vcpu);
> struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
>
> - if (vmx->nested.nested_run_pending)
> - return false;
> + WARN_ON_ONCE(vmx->nested.nested_run_pending);
>
> if (unlikely(vmx->fail)) {
> trace_kvm_nested_vmenter_failed(
> @@ -5553,19 +5552,6 @@ bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
> return true;
> }
>
> - /*
> - * The host physical addresses of some pages of guest memory
> - * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
> - * Page). The CPU may write to these pages via their host
> - * physical address while L2 is running, bypassing any
> - * address-translation-based dirty tracking (e.g. EPT write
> - * protection).
> - *
> - * Mark them dirty on every exit from L2 to prevent them from
> - * getting out of sync with dirty tracking.
> - */
> - nested_mark_vmcs12_pages_dirty(vcpu);
> -
> trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
> vmcs_readl(EXIT_QUALIFICATION),
> vmx->idt_vectoring_info,
> diff --git a/arch/x86/kvm/vmx/nested.h b/arch/x86/kvm/vmx/nested.h
> index 21d36652f213..f70968b76d33 100644
> --- a/arch/x86/kvm/vmx/nested.h
> +++ b/arch/x86/kvm/vmx/nested.h
> @@ -33,6 +33,7 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
> int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
> u32 vmx_instruction_info, bool wr, int len, gva_t *ret);
> void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu);
> +void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu);
> bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
> int size);
>
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index b447d66f44e6..07299a957d4a 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -5851,8 +5851,23 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,
> if (vmx->emulation_required)
> return handle_invalid_guest_state(vcpu);
>
> - if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
> - return nested_vmx_reflect_vmexit(vcpu, exit_reason);
> + if (is_guest_mode(vcpu)) {
> + /*
> + * The host physical addresses of some pages of guest memory
> + * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
> + * Page). The CPU may write to these pages via their host
> + * physical address while L2 is running, bypassing any
> + * address-translation-based dirty tracking (e.g. EPT write
> + * protection).
> + *
> + * Mark them dirty on every exit from L2 to prevent them from
> + * getting out of sync with dirty tracking.
> + */
> + nested_mark_vmcs12_pages_dirty(vcpu);
> +
> + if (nested_vmx_exit_reflected(vcpu, exit_reason))
> + return nested_vmx_reflect_vmexit(vcpu, exit_reason);
> + }
>
> if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
> dump_vmcs();
The only functional difference seems to be that we're now doing
nested_mark_vmcs12_pages_dirty() in vmx->fail case too and this seems
superfluous: we failed to enter L2 so 'special' pages should remain
intact (right?) but this should be an uncommon case.
Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>
--
Vitaly
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] KVM: nVMX: remove side effects from nested_vmx_exit_reflected
2020-03-18 10:52 ` Vitaly Kuznetsov
@ 2020-03-18 10:59 ` Paolo Bonzini
2020-03-18 15:12 ` Sean Christopherson
0 siblings, 1 reply; 5+ messages in thread
From: Paolo Bonzini @ 2020-03-18 10:59 UTC (permalink / raw)
To: Vitaly Kuznetsov; +Cc: sean.j.christopherson, linux-kernel, kvm
On 18/03/20 11:52, Vitaly Kuznetsov wrote:
> The only functional difference seems to be that we're now doing
> nested_mark_vmcs12_pages_dirty() in vmx->fail case too and this seems
> superfluous: we failed to enter L2 so 'special' pages should remain
> intact (right?) but this should be an uncommon case.
>
> Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>
I'm not entirely sure if the PID could be written before the processor
decrees a vmfail. It doesn't really hurt anyway as you say though.
Thanks for the review!
Paolo
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] KVM: nVMX: remove side effects from nested_vmx_exit_reflected
2020-03-18 10:59 ` Paolo Bonzini
@ 2020-03-18 15:12 ` Sean Christopherson
0 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2020-03-18 15:12 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: Vitaly Kuznetsov, linux-kernel, kvm
On Wed, Mar 18, 2020 at 11:59:13AM +0100, Paolo Bonzini wrote:
> On 18/03/20 11:52, Vitaly Kuznetsov wrote:
> > The only functional difference seems to be that we're now doing
> > nested_mark_vmcs12_pages_dirty() in vmx->fail case too and this seems
> > superfluous: we failed to enter L2 so 'special' pages should remain
> > intact (right?) but this should be an uncommon case.
> >
> > Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>
>
> I'm not entirely sure if the PID could be written before the processor
> decrees a vmfail. It doesn't really hurt anyway as you say though.
I would expect that writing special pages on VM-Fail would be classified
as a CPU bug.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] KVM: nVMX: remove side effects from nested_vmx_exit_reflected
2020-03-17 18:00 [PATCH] KVM: nVMX: remove side effects from nested_vmx_exit_reflected Paolo Bonzini
2020-03-18 10:52 ` Vitaly Kuznetsov
@ 2020-03-19 20:11 ` Krish Sadhukhan
1 sibling, 0 replies; 5+ messages in thread
From: Krish Sadhukhan @ 2020-03-19 20:11 UTC (permalink / raw)
To: Paolo Bonzini, linux-kernel, kvm; +Cc: vkuznets, sean.j.christopherson
On 3/17/20 11:00 AM, Paolo Bonzini wrote:
> The name of nested_vmx_exit_reflected suggests that it's purely
> a test, but it actually marks VMCS12 pages as dirty. Move this to
> vmx_handle_exit, observing that the initial nested_run_pending check in
> nested_vmx_exit_reflected is pointless---nested_run_pending has just
> been cleared in vmx_vcpu_run and won't be set until handle_vmlaunch
> or handle_vmresume.
>
> Suggested-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> arch/x86/kvm/vmx/nested.c | 18 ++----------------
> arch/x86/kvm/vmx/nested.h | 1 +
> arch/x86/kvm/vmx/vmx.c | 19 +++++++++++++++++--
> 3 files changed, 20 insertions(+), 18 deletions(-)
>
> diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
> index 8578513907d7..4ff859c99946 100644
> --- a/arch/x86/kvm/vmx/nested.c
> +++ b/arch/x86/kvm/vmx/nested.c
> @@ -3527,7 +3527,7 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
> }
>
>
> -static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
> +void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
> {
> struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
> gfn_t gfn;
> @@ -5543,8 +5543,7 @@ bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
> struct vcpu_vmx *vmx = to_vmx(vcpu);
> struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
>
> - if (vmx->nested.nested_run_pending)
> - return false;
> + WARN_ON_ONCE(vmx->nested.nested_run_pending);
>
> if (unlikely(vmx->fail)) {
> trace_kvm_nested_vmenter_failed(
> @@ -5553,19 +5552,6 @@ bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
> return true;
> }
>
> - /*
> - * The host physical addresses of some pages of guest memory
> - * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
> - * Page). The CPU may write to these pages via their host
> - * physical address while L2 is running, bypassing any
> - * address-translation-based dirty tracking (e.g. EPT write
> - * protection).
> - *
> - * Mark them dirty on every exit from L2 to prevent them from
> - * getting out of sync with dirty tracking.
> - */
> - nested_mark_vmcs12_pages_dirty(vcpu);
> -
> trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
> vmcs_readl(EXIT_QUALIFICATION),
> vmx->idt_vectoring_info,
> diff --git a/arch/x86/kvm/vmx/nested.h b/arch/x86/kvm/vmx/nested.h
> index 21d36652f213..f70968b76d33 100644
> --- a/arch/x86/kvm/vmx/nested.h
> +++ b/arch/x86/kvm/vmx/nested.h
> @@ -33,6 +33,7 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
> int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
> u32 vmx_instruction_info, bool wr, int len, gva_t *ret);
> void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu);
> +void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu);
> bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
> int size);
>
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index b447d66f44e6..07299a957d4a 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -5851,8 +5851,23 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,
> if (vmx->emulation_required)
> return handle_invalid_guest_state(vcpu);
>
> - if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
> - return nested_vmx_reflect_vmexit(vcpu, exit_reason);
> + if (is_guest_mode(vcpu)) {
> + /*
> + * The host physical addresses of some pages of guest memory
> + * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
> + * Page). The CPU may write to these pages via their host
> + * physical address while L2 is running, bypassing any
> + * address-translation-based dirty tracking (e.g. EPT write
> + * protection).
> + *
> + * Mark them dirty on every exit from L2 to prevent them from
> + * getting out of sync with dirty tracking.
> + */
> + nested_mark_vmcs12_pages_dirty(vcpu);
> +
> + if (nested_vmx_exit_reflected(vcpu, exit_reason))
> + return nested_vmx_reflect_vmexit(vcpu, exit_reason);
> + }
>
> if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
> dump_vmcs();
Reviewed-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-03-19 20:11 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-17 18:00 [PATCH] KVM: nVMX: remove side effects from nested_vmx_exit_reflected Paolo Bonzini
2020-03-18 10:52 ` Vitaly Kuznetsov
2020-03-18 10:59 ` Paolo Bonzini
2020-03-18 15:12 ` Sean Christopherson
2020-03-19 20:11 ` Krish Sadhukhan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).