From: Gleb Natapov <gleb@redhat.com>
To: Arthur Chunqi Li <yzt356@gmail.com>
Cc: kvm <kvm@vger.kernel.org>, Jan Kiszka <jan.kiszka@web.de>,
Paolo Bonzini <pbonzini@redhat.com>,
Yang Zhang <yang.z.zhang@intel.com>
Subject: Re: [PATCH v4] KVM: nVMX: Fully support of nested VMX preemption timer
Date: Mon, 16 Sep 2013 09:20:09 +0300 [thread overview]
Message-ID: <20130916062009.GF17294@redhat.com> (raw)
In-Reply-To: <CABpY8MLyhJB0Xsg=eypJYmmzaTKkxehk9=tO8aqCUdBKJ0beng@mail.gmail.com>
On Mon, Sep 16, 2013 at 01:49:41PM +0800, Arthur Chunqi Li wrote:
> On Sun, Sep 15, 2013 at 8:31 PM, Gleb Natapov <gleb@redhat.com> wrote:
> > On Fri, Sep 06, 2013 at 10:04:51AM +0800, Arthur Chunqi Li wrote:
> >> This patch contains the following two changes:
> >> 1. Fix the bug in nested preemption timer support. If vmexit L2->L0
> >> with some reasons not emulated by L1, preemption timer value should
> >> be save in such exits.
> >> 2. Add support of "Save VMX-preemption timer value" VM-Exit controls
> >> to nVMX.
> >>
> >> With this patch, nested VMX preemption timer features are fully
> >> supported.
> >>
> >> Signed-off-by: Arthur Chunqi Li <yzt356@gmail.com>
> >> ---
> >> ChangeLog to v3:
> >> Move nested_adjust_preemption_timer to the latest place just before vmenter.
> >> Some minor changes.
> >>
> >> arch/x86/include/uapi/asm/msr-index.h | 1 +
> >> arch/x86/kvm/vmx.c | 49 +++++++++++++++++++++++++++++++--
> >> 2 files changed, 48 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/arch/x86/include/uapi/asm/msr-index.h b/arch/x86/include/uapi/asm/msr-index.h
> >> index bb04650..b93e09a 100644
> >> --- a/arch/x86/include/uapi/asm/msr-index.h
> >> +++ b/arch/x86/include/uapi/asm/msr-index.h
> >> @@ -536,6 +536,7 @@
> >>
> >> /* MSR_IA32_VMX_MISC bits */
> >> #define MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS (1ULL << 29)
> >> +#define MSR_IA32_VMX_MISC_PREEMPTION_TIMER_SCALE 0x1F
> >> /* AMD-V MSRs */
> >>
> >> #define MSR_VM_CR 0xc0010114
> >> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> >> index 1f1da43..f364d16 100644
> >> --- a/arch/x86/kvm/vmx.c
> >> +++ b/arch/x86/kvm/vmx.c
> >> @@ -374,6 +374,8 @@ struct nested_vmx {
> >> */
> >> struct page *apic_access_page;
> >> u64 msr_ia32_feature_control;
> >> + /* Set if vmexit is L2->L1 */
> >> + bool nested_vmx_exit;
> > Do not see why it is needed, see bellow.
> >
> >> };
> >>
> >> #define POSTED_INTR_ON 0
> >> @@ -2204,7 +2206,17 @@ static __init void nested_vmx_setup_ctls_msrs(void)
> >> #ifdef CONFIG_X86_64
> >> VM_EXIT_HOST_ADDR_SPACE_SIZE |
> >> #endif
> >> - VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
> >> + VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT |
> >> + VM_EXIT_SAVE_VMX_PREEMPTION_TIMER;
> >> + if (!(nested_vmx_pinbased_ctls_high &
> >> + PIN_BASED_VMX_PREEMPTION_TIMER) ||
> >> + !(nested_vmx_exit_ctls_high &
> >> + VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)) {
> >> + nested_vmx_exit_ctls_high &=
> >> + (~VM_EXIT_SAVE_VMX_PREEMPTION_TIMER);
> >> + nested_vmx_pinbased_ctls_high &=
> >> + (~PIN_BASED_VMX_PREEMPTION_TIMER);
> >> + }
> >> nested_vmx_exit_ctls_high |= (VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
> >> VM_EXIT_LOAD_IA32_EFER);
> >>
> >> @@ -6707,6 +6719,24 @@ static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
> >> *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
> >> }
> >>
> >> +static void nested_adjust_preemption_timer(struct kvm_vcpu *vcpu)
> >> +{
> >> + u64 delta_tsc_l1;
> >> + u32 preempt_val_l1, preempt_val_l2, preempt_scale;
> >> +
> >> + preempt_scale = native_read_msr(MSR_IA32_VMX_MISC) &
> >> + MSR_IA32_VMX_MISC_PREEMPTION_TIMER_SCALE;
> >> + preempt_val_l2 = vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
> >> + delta_tsc_l1 = kvm_x86_ops->read_l1_tsc(vcpu,
> >> + native_read_tsc()) - vcpu->arch.last_guest_tsc;
> >> + preempt_val_l1 = delta_tsc_l1 >> preempt_scale;
> >> + if (preempt_val_l2 <= preempt_val_l1)
> >> + preempt_val_l2 = 0;
> >> + else
> >> + preempt_val_l2 -= preempt_val_l1;
> >> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, preempt_val_l2);
> >> +}
> >> +
> >> /*
> >> * The guest has exited. See if we can fix it or if we need userspace
> >> * assistance.
> >> @@ -6736,9 +6766,11 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)
> >> vmx->nested.nested_run_pending = 0;
> >>
> >> if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
> >> + vmx->nested.nested_vmx_exit = true;
> >> nested_vmx_vmexit(vcpu);
> >> return 1;
> >> }
> >> + vmx->nested.nested_vmx_exit = false;
> >>
> >> if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
> >> vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
> >> @@ -7132,6 +7164,8 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
> >> debugctlmsr = get_debugctlmsr();
> >>
> >> vmx->__launched = vmx->loaded_vmcs->launched;
> >> + if (is_guest_mode(vcpu) && !(vmx->nested.nested_vmx_exit))
> > How is_guest_mode() and nested_vmx_exi can be both true? The only place
> > nested_vmx_exit is set to true is just before call to
> > nested_vmx_vmexit(). The firs thing nested_vmx_vmexit() does is makes
> > is_guest_mode() false. To enter guest mode again at least one other
> > vmexit from L1 to L0 is needed at which point nested_vmx_exit will be
> > reset to false again.
> >
> > If you want to avoid calling nested_adjust_preemption_timer() during
> > vmlaunch/vmresume emulation (and it looks like this is what you are
> > trying to achieve here) you can check nested_run_pending.
> Besides vmlaunch/vmresume emulation, every exit from L2->L1 should not
> call nested_adjust_preemption_timer(), this function is just used to
> adjust preemption timer when L2->L0->L2. Can nested_run_pending
> distinguish this?
>
On L2->L1 exit is_guest_mode() will be false during vmx_vcpu_run().
On L1->L2 entry nested_run_pending will be true. On L0->L2 entry
is_guest_mode() == true && nested_run_pending == false.
--
Gleb.
prev parent reply other threads:[~2013-09-16 6:20 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-06 2:04 [PATCH v4] KVM: nVMX: Fully support of nested VMX preemption timer Arthur Chunqi Li
2013-09-13 17:15 ` Paolo Bonzini
2013-09-14 7:44 ` Jan Kiszka
2013-09-15 7:59 ` Arthur Chunqi Li
2013-09-16 5:42 ` Arthur Chunqi Li
2013-09-16 7:18 ` Jan Kiszka
2013-09-16 7:44 ` Gleb Natapov
2013-09-16 8:58 ` Paolo Bonzini
2013-09-16 9:09 ` Gleb Natapov
2013-09-16 9:49 ` Paolo Bonzini
2013-09-16 10:44 ` Gleb Natapov
2013-09-16 10:50 ` Paolo Bonzini
2013-09-15 12:31 ` Gleb Natapov
2013-09-16 5:49 ` Arthur Chunqi Li
2013-09-16 6:20 ` Gleb Natapov [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130916062009.GF17294@redhat.com \
--to=gleb@redhat.com \
--cc=jan.kiszka@web.de \
--cc=kvm@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=yang.z.zhang@intel.com \
--cc=yzt356@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.