Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Jan Kiszka
On 2013-08-25 11:07, Arthur Chunqi Li wrote:
> On Sun, Aug 25, 2013 at 4:53 PM, Jan Kiszka  wrote:
>> On 2013-08-25 10:41, Arthur Chunqi Li wrote:
>>> On Sun, Aug 25, 2013 at 4:18 PM, Abel Gordon  wrote:
>>>>
>>>>
>>>> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:54:13 AM:
>>>>
>>>>> From: Jan Kiszka 
>>>>> To: Abel Gordon/Haifa/IBM@IBMIL,
>>>>> Cc: g...@redhat.com, kvm , pbonz...@redhat.com,
>>>>> "李春奇 "  
>>>>> Date: 25/08/2013 10:54 AM
>>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
>>>> timer
>>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>>
>>>>> On 2013-08-25 09:50, Abel Gordon wrote:
>>>>>>
>>>>>>
>>>>>> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:43:12 AM:
>>>>>>
>>>>>>> From: Jan Kiszka 
>>>>>>> To: Abel Gordon/Haifa/IBM@IBMIL,
>>>>>>> Cc: g...@redhat.com, kvm@vger.kernel.org, kvm-ow...@vger.kernel.org,
>>>>>>> pbonz...@redhat.com, "李春奇 " 
>>>>>>> Date: 25/08/2013 10:43 AM
>>>>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
>>>>>> timer
>>>>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>>>>
>>>>>>> On 2013-08-25 09:37, Abel Gordon wrote:
>>>>>>>>
>>>>>>>>
>>>>>>>>> From: Jan Kiszka 
>>>>>>>>> To: "李春奇 "  ,
>>>>>>>>> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
>>>>>>>>> Date: 25/08/2013 09:44 AM
>>>>>>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX
>>>> preemption
>>>>>>>> timer
>>>>>>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>>>>>>
>>>>>>>>> On 2013-08-24 20:44, root 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 
>>>>>>>>>> ---
>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
>>>>>>>>> *vcpu, struct vmcs12 *vmcs12)
>>>>>>>>>>(vmcs_config.pin_based_exec_ctrl |
>>>>>>>>>> vmcs12->pin_based_vm_exec_control));
>>>>>>>>>>
>>>>>>>>>> -   if (vmcs12->pin_based_vm_exec_control &
>>>>>>>> PIN_BASED_VMX_PREEMPTION_TIMER)
>>>>>>>>>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>>>>>>> -  vmcs12->vmx_preemption_timer_value);
>>>>>>>>>> +   if (vmcs12->pin_based_vm_exec_control &
>>>>>>>>> PIN_BASED_VMX_PREEMPTION_TIMER) {
>>>>>>>>>> +  if (vmcs12->vm_exit_controls &
>>>>>>>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
>>>>>>>>>> + vmcs12->vmx_preemption_timer_value =
>>>>>>>>>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
>>>>>>>>>> +  else
>>>>>>>>>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>>>>>>> +   vmcs12->vmx_preemption_timer_value);
>>>>>>>>>> +   }
>>>>>>>>>
>>>>>>>>> This is not correct. We still need to set the vmcs to
>>>>>>>>> 

Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Arthur Chunqi Li
On Sun, Aug 25, 2013 at 4:53 PM, Jan Kiszka  wrote:
> On 2013-08-25 10:41, Arthur Chunqi Li wrote:
>> On Sun, Aug 25, 2013 at 4:18 PM, Abel Gordon  wrote:
>>>
>>>
>>> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:54:13 AM:
>>>
>>>> From: Jan Kiszka 
>>>> To: Abel Gordon/Haifa/IBM@IBMIL,
>>>> Cc: g...@redhat.com, kvm , pbonz...@redhat.com,
>>>> "李春奇 "  
>>>> Date: 25/08/2013 10:54 AM
>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
>>> timer
>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>
>>>> On 2013-08-25 09:50, Abel Gordon wrote:
>>>>>
>>>>>
>>>>> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:43:12 AM:
>>>>>
>>>>>> From: Jan Kiszka 
>>>>>> To: Abel Gordon/Haifa/IBM@IBMIL,
>>>>>> Cc: g...@redhat.com, kvm@vger.kernel.org, kvm-ow...@vger.kernel.org,
>>>>>> pbonz...@redhat.com, "李春奇 " 
>>>>>> Date: 25/08/2013 10:43 AM
>>>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
>>>>> timer
>>>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>>>
>>>>>> On 2013-08-25 09:37, Abel Gordon wrote:
>>>>>>>
>>>>>>>
>>>>>>>> From: Jan Kiszka 
>>>>>>>> To: "李春奇 "  ,
>>>>>>>> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
>>>>>>>> Date: 25/08/2013 09:44 AM
>>>>>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX
>>> preemption
>>>>>>> timer
>>>>>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>>>>>
>>>>>>>> On 2013-08-24 20:44, root 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 
>>>>>>>>> ---
>>>>>>>
>>>>>>>>>
>>>>>>>>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
>>>>>>>> *vcpu, struct vmcs12 *vmcs12)
>>>>>>>>>(vmcs_config.pin_based_exec_ctrl |
>>>>>>>>> vmcs12->pin_based_vm_exec_control));
>>>>>>>>>
>>>>>>>>> -   if (vmcs12->pin_based_vm_exec_control &
>>>>>>> PIN_BASED_VMX_PREEMPTION_TIMER)
>>>>>>>>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>>>>>> -  vmcs12->vmx_preemption_timer_value);
>>>>>>>>> +   if (vmcs12->pin_based_vm_exec_control &
>>>>>>>> PIN_BASED_VMX_PREEMPTION_TIMER) {
>>>>>>>>> +  if (vmcs12->vm_exit_controls &
>>>>>>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
>>>>>>>>> + vmcs12->vmx_preemption_timer_value =
>>>>>>>>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
>>>>>>>>> +  else
>>>>>>>>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>>>>>> +   vmcs12->vmx_preemption_timer_value);
>>>>>>>>> +   }
>>>>>>>>
>>>>>>>> This is not correct. We still need to set the vmcs to
>>>>>>>> vmx_preemption_timer_value. The difference is that, on exit from L2,
>>>>>>>> vmx_preemption_timer_value has to be updated according to the saved
>>>>>>>> hardware state. The corresponding code is missing in your patch so
>>>>> far.
>>>>>>>
>>>>>>> I think som

Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Jan Kiszka
On 2013-08-25 10:41, Arthur Chunqi Li wrote:
> On Sun, Aug 25, 2013 at 4:18 PM, Abel Gordon  wrote:
>>
>>
>> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:54:13 AM:
>>
>>> From: Jan Kiszka 
>>> To: Abel Gordon/Haifa/IBM@IBMIL,
>>> Cc: g...@redhat.com, kvm , pbonz...@redhat.com,
>>> "李春奇 "  
>>> Date: 25/08/2013 10:54 AM
>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
>> timer
>>> Sent by: kvm-ow...@vger.kernel.org
>>>
>>> On 2013-08-25 09:50, Abel Gordon wrote:
>>>>
>>>>
>>>> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:43:12 AM:
>>>>
>>>>> From: Jan Kiszka 
>>>>> To: Abel Gordon/Haifa/IBM@IBMIL,
>>>>> Cc: g...@redhat.com, kvm@vger.kernel.org, kvm-ow...@vger.kernel.org,
>>>>> pbonz...@redhat.com, "李春奇 " 
>>>>> Date: 25/08/2013 10:43 AM
>>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
>>>> timer
>>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>>
>>>>> On 2013-08-25 09:37, Abel Gordon wrote:
>>>>>>
>>>>>>
>>>>>>> From: Jan Kiszka 
>>>>>>> To: "李春奇 "  ,
>>>>>>> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
>>>>>>> Date: 25/08/2013 09:44 AM
>>>>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX
>> preemption
>>>>>> timer
>>>>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>>>>
>>>>>>> On 2013-08-24 20:44, root 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 
>>>>>>>> ---
>>>>>>
>>>>>>>>
>>>>>>>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
>>>>>>> *vcpu, struct vmcs12 *vmcs12)
>>>>>>>>(vmcs_config.pin_based_exec_ctrl |
>>>>>>>> vmcs12->pin_based_vm_exec_control));
>>>>>>>>
>>>>>>>> -   if (vmcs12->pin_based_vm_exec_control &
>>>>>> PIN_BASED_VMX_PREEMPTION_TIMER)
>>>>>>>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>>>>> -  vmcs12->vmx_preemption_timer_value);
>>>>>>>> +   if (vmcs12->pin_based_vm_exec_control &
>>>>>>> PIN_BASED_VMX_PREEMPTION_TIMER) {
>>>>>>>> +  if (vmcs12->vm_exit_controls &
>>>>>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
>>>>>>>> + vmcs12->vmx_preemption_timer_value =
>>>>>>>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
>>>>>>>> +  else
>>>>>>>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>>>>> +   vmcs12->vmx_preemption_timer_value);
>>>>>>>> +   }
>>>>>>>
>>>>>>> This is not correct. We still need to set the vmcs to
>>>>>>> vmx_preemption_timer_value. The difference is that, on exit from L2,
>>>>>>> vmx_preemption_timer_value has to be updated according to the saved
>>>>>>> hardware state. The corresponding code is missing in your patch so
>>>> far.
>>>>>>
>>>>>> I think something else maybe be missing here: assuming L0 handles
>> exits
>>>>>> for L2 without involving L1 (e.g. external interrupts or ept
>>>> violations),
>>>>>> then, we may spend some cycles in L0 handling these exits. Note L1 is
>>>> not
>>>>>> aware of these exits and from L1 perspective L2 was running 

Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Arthur Chunqi Li
On Sun, Aug 25, 2013 at 4:18 PM, Abel Gordon  wrote:
>
>
> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:54:13 AM:
>
>> From: Jan Kiszka 
>> To: Abel Gordon/Haifa/IBM@IBMIL,
>> Cc: g...@redhat.com, kvm , pbonz...@redhat.com,
>> "李春奇 "  
>> Date: 25/08/2013 10:54 AM
>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
> timer
>> Sent by: kvm-ow...@vger.kernel.org
>>
>> On 2013-08-25 09:50, Abel Gordon wrote:
>> >
>> >
>> > kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:43:12 AM:
>> >
>> >> From: Jan Kiszka 
>> >> To: Abel Gordon/Haifa/IBM@IBMIL,
>> >> Cc: g...@redhat.com, kvm@vger.kernel.org, kvm-ow...@vger.kernel.org,
>> >> pbonz...@redhat.com, "李春奇 " 
>> >> Date: 25/08/2013 10:43 AM
>> >> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
>> > timer
>> >> Sent by: kvm-ow...@vger.kernel.org
>> >>
>> >> On 2013-08-25 09:37, Abel Gordon wrote:
>> >>>
>> >>>
>> >>>> From: Jan Kiszka 
>> >>>> To: "李春奇 "  ,
>> >>>> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
>> >>>> Date: 25/08/2013 09:44 AM
>> >>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX
> preemption
>> >>> timer
>> >>>> Sent by: kvm-ow...@vger.kernel.org
>> >>>>
>> >>>> On 2013-08-24 20:44, root 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 
>> >>>>> ---
>> >>>
>> >>>>>
>> >>>>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
>> >>>> *vcpu, struct vmcs12 *vmcs12)
>> >>>>>(vmcs_config.pin_based_exec_ctrl |
>> >>>>> vmcs12->pin_based_vm_exec_control));
>> >>>>>
>> >>>>> -   if (vmcs12->pin_based_vm_exec_control &
>> >>> PIN_BASED_VMX_PREEMPTION_TIMER)
>> >>>>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>> >>>>> -  vmcs12->vmx_preemption_timer_value);
>> >>>>> +   if (vmcs12->pin_based_vm_exec_control &
>> >>>> PIN_BASED_VMX_PREEMPTION_TIMER) {
>> >>>>> +  if (vmcs12->vm_exit_controls &
>> >>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
>> >>>>> + vmcs12->vmx_preemption_timer_value =
>> >>>>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
>> >>>>> +  else
>> >>>>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>> >>>>> +   vmcs12->vmx_preemption_timer_value);
>> >>>>> +   }
>> >>>>
>> >>>> This is not correct. We still need to set the vmcs to
>> >>>> vmx_preemption_timer_value. The difference is that, on exit from L2,
>> >>>> vmx_preemption_timer_value has to be updated according to the saved
>> >>>> hardware state. The corresponding code is missing in your patch so
>> > far.
>> >>>
>> >>> I think something else maybe be missing here: assuming L0 handles
> exits
>> >>> for L2 without involving L1 (e.g. external interrupts or ept
>> > violations),
>> >>> then, we may spend some cycles in L0 handling these exits. Note L1 is
>> > not
>> >>> aware of these exits and from L1 perspective L2 was running on the
> CPU.
>> >>> That means that we may need to reduce these cycles spent at
>> >>> L0 from the preemtion timer or emulate a preemption timer exit to
>> >>> force a transition to L1 instead of resuming L2.
>> >>
>> >> That's precisely what the logic I 

Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Abel Gordon


Jan Kiszka  wrote on 25/08/2013 11:27:22 AM:

> From: Jan Kiszka 
> To: Abel Gordon/Haifa/IBM@IBMIL,
> Cc: g...@redhat.com, kvm , pbonz...@redhat.com,
> "李春奇 " 
> Date: 25/08/2013 11:27 AM
> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
timer
>
> On 2013-08-25 10:25, Jan Kiszka wrote:
> > On 2013-08-25 10:18, Abel Gordon wrote:
> >>
> >>
> >> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:54:13 AM:
> >>
> >>> From: Jan Kiszka 
> >>> To: Abel Gordon/Haifa/IBM@IBMIL,
> >>> Cc: g...@redhat.com, kvm , pbonz...@redhat.com,
> >>> "李春奇 "  
> >>> Date: 25/08/2013 10:54 AM
> >>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX
preemption
> >> timer
> >>> Sent by: kvm-ow...@vger.kernel.org
> >>>
> >>> On 2013-08-25 09:50, Abel Gordon wrote:
> >>>>
> >>>>
> >>>> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:43:12 AM:
> >>>>
> >>>>> From: Jan Kiszka 
> >>>>> To: Abel Gordon/Haifa/IBM@IBMIL,
> >>>>> Cc: g...@redhat.com, kvm@vger.kernel.org,
kvm-ow...@vger.kernel.org,
> >>>>> pbonz...@redhat.com, "李春奇 " 
> >>>>> Date: 25/08/2013 10:43 AM
> >>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX
preemption
> >>>> timer
> >>>>> Sent by: kvm-ow...@vger.kernel.org
> >>>>>
> >>>>> On 2013-08-25 09:37, Abel Gordon wrote:
> >>>>>>
> >>>>>>
> >>>>>>> From: Jan Kiszka 
> >>>>>>> To: "李春奇 "  ,
> >>>>>>> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
> >>>>>>> Date: 25/08/2013 09:44 AM
> >>>>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX
> >> preemption
> >>>>>> timer
> >>>>>>> Sent by: kvm-ow...@vger.kernel.org
> >>>>>>>
> >>>>>>> On 2013-08-24 20:44, root 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 
> >>>>>>>> ---
> >>>>>>
> >>>>>>>>
> >>>>>>>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct
kvm_vcpu
> >>>>>>> *vcpu, struct vmcs12 *vmcs12)
> >>>>>>>>(vmcs_config.pin_based_exec_ctrl |
> >>>>>>>> vmcs12->pin_based_vm_exec_control));
> >>>>>>>>
> >>>>>>>> -   if (vmcs12->pin_based_vm_exec_control &
> >>>>>> PIN_BASED_VMX_PREEMPTION_TIMER)
> >>>>>>>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
> >>>>>>>> -  vmcs12->vmx_preemption_timer_value);
> >>>>>>>> +   if (vmcs12->pin_based_vm_exec_control &
> >>>>>>> PIN_BASED_VMX_PREEMPTION_TIMER) {
> >>>>>>>> +  if (vmcs12->vm_exit_controls &
> >>>>>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
> >>>>>>>> + vmcs12->vmx_preemption_timer_value =
> >>>>>>>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
> >>>>>>>> +  else
> >>>>>>>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
> >>>>>>>> +   vmcs12->vmx_preemption_timer_value);
> >>>>>>>> +   }
> >>>>>>>
> >>>>>>> This is not correct. We still need to set the vmcs to
> >>>>>>> vmx_preemption_timer_value. The difference is that,

Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Jan Kiszka
On 2013-08-25 10:25, Jan Kiszka wrote:
> On 2013-08-25 10:18, Abel Gordon wrote:
>>
>>
>> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:54:13 AM:
>>
>>> From: Jan Kiszka 
>>> To: Abel Gordon/Haifa/IBM@IBMIL,
>>> Cc: g...@redhat.com, kvm , pbonz...@redhat.com,
>>> "李春奇 "  
>>> Date: 25/08/2013 10:54 AM
>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
>> timer
>>> Sent by: kvm-ow...@vger.kernel.org
>>>
>>> On 2013-08-25 09:50, Abel Gordon wrote:
>>>>
>>>>
>>>> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:43:12 AM:
>>>>
>>>>> From: Jan Kiszka 
>>>>> To: Abel Gordon/Haifa/IBM@IBMIL,
>>>>> Cc: g...@redhat.com, kvm@vger.kernel.org, kvm-ow...@vger.kernel.org,
>>>>> pbonz...@redhat.com, "李春奇 " 
>>>>> Date: 25/08/2013 10:43 AM
>>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
>>>> timer
>>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>>
>>>>> On 2013-08-25 09:37, Abel Gordon wrote:
>>>>>>
>>>>>>
>>>>>>> From: Jan Kiszka 
>>>>>>> To: "李春奇 "  ,
>>>>>>> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
>>>>>>> Date: 25/08/2013 09:44 AM
>>>>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX
>> preemption
>>>>>> timer
>>>>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>>>>
>>>>>>> On 2013-08-24 20:44, root 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 
>>>>>>>> ---
>>>>>>
>>>>>>>>
>>>>>>>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
>>>>>>> *vcpu, struct vmcs12 *vmcs12)
>>>>>>>>(vmcs_config.pin_based_exec_ctrl |
>>>>>>>> vmcs12->pin_based_vm_exec_control));
>>>>>>>>
>>>>>>>> -   if (vmcs12->pin_based_vm_exec_control &
>>>>>> PIN_BASED_VMX_PREEMPTION_TIMER)
>>>>>>>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>>>>> -  vmcs12->vmx_preemption_timer_value);
>>>>>>>> +   if (vmcs12->pin_based_vm_exec_control &
>>>>>>> PIN_BASED_VMX_PREEMPTION_TIMER) {
>>>>>>>> +  if (vmcs12->vm_exit_controls &
>>>>>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
>>>>>>>> + vmcs12->vmx_preemption_timer_value =
>>>>>>>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
>>>>>>>> +  else
>>>>>>>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>>>>> +   vmcs12->vmx_preemption_timer_value);
>>>>>>>> +   }
>>>>>>>
>>>>>>> This is not correct. We still need to set the vmcs to
>>>>>>> vmx_preemption_timer_value. The difference is that, on exit from L2,
>>>>>>> vmx_preemption_timer_value has to be updated according to the saved
>>>>>>> hardware state. The corresponding code is missing in your patch so
>>>> far.
>>>>>>
>>>>>> I think something else maybe be missing here: assuming L0 handles
>> exits
>>>>>> for L2 without involving L1 (e.g. external interrupts or ept
>>>> violations),
>>>>>> then, we may spend some cycles in L0 handling these exits. Note L1 is
>>>> not
>>>>>> aware of these exits and from L1 perspective L2 was running on the
>> CPU.
&

Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Jan Kiszka
On 2013-08-25 10:18, Abel Gordon wrote:
> 
> 
> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:54:13 AM:
> 
>> From: Jan Kiszka 
>> To: Abel Gordon/Haifa/IBM@IBMIL,
>> Cc: g...@redhat.com, kvm , pbonz...@redhat.com,
>> "李春奇 "  
>> Date: 25/08/2013 10:54 AM
>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
> timer
>> Sent by: kvm-ow...@vger.kernel.org
>>
>> On 2013-08-25 09:50, Abel Gordon wrote:
>>>
>>>
>>> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:43:12 AM:
>>>
>>>> From: Jan Kiszka 
>>>> To: Abel Gordon/Haifa/IBM@IBMIL,
>>>> Cc: g...@redhat.com, kvm@vger.kernel.org, kvm-ow...@vger.kernel.org,
>>>> pbonz...@redhat.com, "李春奇 " 
>>>> Date: 25/08/2013 10:43 AM
>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
>>> timer
>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>
>>>> On 2013-08-25 09:37, Abel Gordon wrote:
>>>>>
>>>>>
>>>>>> From: Jan Kiszka 
>>>>>> To: "李春奇 "  ,
>>>>>> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
>>>>>> Date: 25/08/2013 09:44 AM
>>>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX
> preemption
>>>>> timer
>>>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>>>
>>>>>> On 2013-08-24 20:44, root 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 
>>>>>>> ---
>>>>>
>>>>>>>
>>>>>>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
>>>>>> *vcpu, struct vmcs12 *vmcs12)
>>>>>>>(vmcs_config.pin_based_exec_ctrl |
>>>>>>> vmcs12->pin_based_vm_exec_control));
>>>>>>>
>>>>>>> -   if (vmcs12->pin_based_vm_exec_control &
>>>>> PIN_BASED_VMX_PREEMPTION_TIMER)
>>>>>>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>>>> -  vmcs12->vmx_preemption_timer_value);
>>>>>>> +   if (vmcs12->pin_based_vm_exec_control &
>>>>>> PIN_BASED_VMX_PREEMPTION_TIMER) {
>>>>>>> +  if (vmcs12->vm_exit_controls &
>>>>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
>>>>>>> + vmcs12->vmx_preemption_timer_value =
>>>>>>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
>>>>>>> +  else
>>>>>>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>>>> +   vmcs12->vmx_preemption_timer_value);
>>>>>>> +   }
>>>>>>
>>>>>> This is not correct. We still need to set the vmcs to
>>>>>> vmx_preemption_timer_value. The difference is that, on exit from L2,
>>>>>> vmx_preemption_timer_value has to be updated according to the saved
>>>>>> hardware state. The corresponding code is missing in your patch so
>>> far.
>>>>>
>>>>> I think something else maybe be missing here: assuming L0 handles
> exits
>>>>> for L2 without involving L1 (e.g. external interrupts or ept
>>> violations),
>>>>> then, we may spend some cycles in L0 handling these exits. Note L1 is
>>> not
>>>>> aware of these exits and from L1 perspective L2 was running on the
> CPU.
>>>>> That means that we may need to reduce these cycles spent at
>>>>> L0 from the preemtion timer or emulate a preemption timer exit to
>>>>> force a transition to L1 instead of resuming L2.
>>>>
>>>> That's precisely what the logic I described should achieve: reload the
>>>> value we saved on L2 exit on r

Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Abel Gordon


kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:54:13 AM:

> From: Jan Kiszka 
> To: Abel Gordon/Haifa/IBM@IBMIL,
> Cc: g...@redhat.com, kvm , pbonz...@redhat.com,
> "李春奇 "  
> Date: 25/08/2013 10:54 AM
> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
timer
> Sent by: kvm-ow...@vger.kernel.org
>
> On 2013-08-25 09:50, Abel Gordon wrote:
> >
> >
> > kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:43:12 AM:
> >
> >> From: Jan Kiszka 
> >> To: Abel Gordon/Haifa/IBM@IBMIL,
> >> Cc: g...@redhat.com, kvm@vger.kernel.org, kvm-ow...@vger.kernel.org,
> >> pbonz...@redhat.com, "李春奇 " 
> >> Date: 25/08/2013 10:43 AM
> >> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
> > timer
> >> Sent by: kvm-ow...@vger.kernel.org
> >>
> >> On 2013-08-25 09:37, Abel Gordon wrote:
> >>>
> >>>
> >>>> From: Jan Kiszka 
> >>>> To: "李春奇 "  ,
> >>>> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
> >>>> Date: 25/08/2013 09:44 AM
> >>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX
preemption
> >>> timer
> >>>> Sent by: kvm-ow...@vger.kernel.org
> >>>>
> >>>> On 2013-08-24 20:44, root 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 
> >>>>> ---
> >>>
> >>>>>
> >>>>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
> >>>> *vcpu, struct vmcs12 *vmcs12)
> >>>>>(vmcs_config.pin_based_exec_ctrl |
> >>>>> vmcs12->pin_based_vm_exec_control));
> >>>>>
> >>>>> -   if (vmcs12->pin_based_vm_exec_control &
> >>> PIN_BASED_VMX_PREEMPTION_TIMER)
> >>>>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
> >>>>> -  vmcs12->vmx_preemption_timer_value);
> >>>>> +   if (vmcs12->pin_based_vm_exec_control &
> >>>> PIN_BASED_VMX_PREEMPTION_TIMER) {
> >>>>> +  if (vmcs12->vm_exit_controls &
> >>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
> >>>>> + vmcs12->vmx_preemption_timer_value =
> >>>>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
> >>>>> +  else
> >>>>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
> >>>>> +   vmcs12->vmx_preemption_timer_value);
> >>>>> +   }
> >>>>
> >>>> This is not correct. We still need to set the vmcs to
> >>>> vmx_preemption_timer_value. The difference is that, on exit from L2,
> >>>> vmx_preemption_timer_value has to be updated according to the saved
> >>>> hardware state. The corresponding code is missing in your patch so
> > far.
> >>>
> >>> I think something else maybe be missing here: assuming L0 handles
exits
> >>> for L2 without involving L1 (e.g. external interrupts or ept
> > violations),
> >>> then, we may spend some cycles in L0 handling these exits. Note L1 is
> > not
> >>> aware of these exits and from L1 perspective L2 was running on the
CPU.
> >>> That means that we may need to reduce these cycles spent at
> >>> L0 from the preemtion timer or emulate a preemption timer exit to
> >>> force a transition to L1 instead of resuming L2.
> >>
> >> That's precisely what the logic I described should achieve: reload the
> >> value we saved on L2 exit on reentry.
> >
> > But don't you think we should also reduce the cycles spent at L0 from
the
> > preemption timer ? I mean, if we spent X cycles at L0 handling a L2
exit
> > which was not forwarded to L1, then, before we resume L2,
> > the preemption timer should be: (previous_value_on_exit - X).
> > If (previous

Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Jan Kiszka
On 2013-08-25 10:04, Abel Gordon wrote:
> 
> 
> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:55:24 AM:
> 
>> From: Arthur Chunqi Li 
>> To: Abel Gordon/Haifa/IBM@IBMIL,
>> Cc: Jan Kiszka , Gleb Natapov ,
>> kvm , kvm-ow...@vger.kernel.org, Paolo Bonzini
>> 
>> Date: 25/08/2013 10:55 AM
>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
> timer
>> Sent by: kvm-ow...@vger.kernel.org
>>
>> On Sun, Aug 25, 2013 at 3:50 PM, Abel Gordon  wrote:
>>>
>>>
>>> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:43:12 AM:
>>>
>>>> From: Jan Kiszka 
>>>> To: Abel Gordon/Haifa/IBM@IBMIL,
>>>> Cc: g...@redhat.com, kvm@vger.kernel.org, kvm-ow...@vger.kernel.org,
>>>> pbonz...@redhat.com, "李春奇 " 
>>>> Date: 25/08/2013 10:43 AM
>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
>>> timer
>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>
>>>> On 2013-08-25 09:37, Abel Gordon wrote:
>>>>>
>>>>>
>>>>>> From: Jan Kiszka 
>>>>>> To: "李春奇 "  ,
>>>>>> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
>>>>>> Date: 25/08/2013 09:44 AM
>>>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX
> preemption
>>>>> timer
>>>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>>>
>>>>>> On 2013-08-24 20:44, root 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 
>>>>>>> ---
>>>>>
>>>>>>>
>>>>>>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
>>>>>> *vcpu, struct vmcs12 *vmcs12)
>>>>>>>(vmcs_config.pin_based_exec_ctrl |
>>>>>>> vmcs12->pin_based_vm_exec_control));
>>>>>>>
>>>>>>> -   if (vmcs12->pin_based_vm_exec_control &
>>>>> PIN_BASED_VMX_PREEMPTION_TIMER)
>>>>>>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>>>> -  vmcs12->vmx_preemption_timer_value);
>>>>>>> +   if (vmcs12->pin_based_vm_exec_control &
>>>>>> PIN_BASED_VMX_PREEMPTION_TIMER) {
>>>>>>> +  if (vmcs12->vm_exit_controls &
>>>>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
>>>>>>> + vmcs12->vmx_preemption_timer_value =
>>>>>>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
>>>>>>> +  else
>>>>>>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>>>> +   vmcs12->vmx_preemption_timer_value);
>>>>>>> +   }
>>>>>>
>>>>>> This is not correct. We still need to set the vmcs to
>>>>>> vmx_preemption_timer_value. The difference is that, on exit from
> L2,
>>>>>> vmx_preemption_timer_value has to be updated according to the saved
>>>>>> hardware state. The corresponding code is missing in your patch so
>>> far.
>>>>>
>>>>> I think something else maybe be missing here: assuming L0 handles
> exits
>>>>> for L2 without involving L1 (e.g. external interrupts or ept
>>> violations),
>>>>> then, we may spend some cycles in L0 handling these exits. Note L1
> is
>>> not
>>>>> aware of these exits and from L1 perspective L2 was running on the
> CPU.
>>>>> That means that we may need to reduce these cycles spent at
>>>>> L0 from the preemtion timer or emulate a preemption timer exit to
>>>>> force a transition to L1 instead of resuming L2.
>>>>
>>>> That's precisely what the logic I described should achieve: reload the
>>>> value we saved on L2 exit on reentry.
>>>
>>> But don't you think we should also reduce the cycles spent at L0 from
> the
>>> preemption timer ? I mean, if we spent X cycles at L0 handling a L2
> exit
>>> which was not forwarded to L1, then, before we resume L2,
>>> the preemption timer should be: (previous_value_on_exit - X).
>>> If (previous_value_on_exit - X) < 0, then we should force ("emulate") a
>>> preemption timer exit between L2 and L1.
>> Sorry, I previously misunderstand your comments. But why should we
>> need to exclude cycles in L0 from L2 preemption value? These cycles
>> are not spent by L2 and it should not be on L2.
> 
> L1 asked the "hardware" (emulated by L0) to run L2 and force an exit
> after "Y" cycles. Now, in practice, we may spend "X" cycles at L0 handling
> exits without switching to L1. That means that from L1 perspective L2
> was running all these X cycles. L1 should assume that the instructions per
> cycle
> the CPU executed decreased but the cycles were spent. That's why I believe
> you should take in account these X cycles.
> 

Now I get it. There is likely some truth in this as the reference clock
for the preemption timer, the TSC, isn't stopped for L1/L2 while running
in L0. And the SDM demands the countdown to be proportional to that clock.

Jan




signature.asc
Description: OpenPGP digital signature


Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Abel Gordon


kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:55:24 AM:

> From: Arthur Chunqi Li 
> To: Abel Gordon/Haifa/IBM@IBMIL,
> Cc: Jan Kiszka , Gleb Natapov ,
> kvm , kvm-ow...@vger.kernel.org, Paolo Bonzini
> 
> Date: 25/08/2013 10:55 AM
> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
timer
> Sent by: kvm-ow...@vger.kernel.org
>
> On Sun, Aug 25, 2013 at 3:50 PM, Abel Gordon  wrote:
> >
> >
> > kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:43:12 AM:
> >
> >> From: Jan Kiszka 
> >> To: Abel Gordon/Haifa/IBM@IBMIL,
> >> Cc: g...@redhat.com, kvm@vger.kernel.org, kvm-ow...@vger.kernel.org,
> >> pbonz...@redhat.com, "李春奇 " 
> >> Date: 25/08/2013 10:43 AM
> >> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
> > timer
> >> Sent by: kvm-ow...@vger.kernel.org
> >>
> >> On 2013-08-25 09:37, Abel Gordon wrote:
> >> >
> >> >
> >> >> From: Jan Kiszka 
> >> >> To: "李春奇 "  ,
> >> >> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
> >> >> Date: 25/08/2013 09:44 AM
> >> >> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX
preemption
> >> > timer
> >> >> Sent by: kvm-ow...@vger.kernel.org
> >> >>
> >> >> On 2013-08-24 20:44, root 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 
> >> >>> ---
> >> >
> >> >>>
> >> >>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
> >> >> *vcpu, struct vmcs12 *vmcs12)
> >> >>>(vmcs_config.pin_based_exec_ctrl |
> >> >>> vmcs12->pin_based_vm_exec_control));
> >> >>>
> >> >>> -   if (vmcs12->pin_based_vm_exec_control &
> >> > PIN_BASED_VMX_PREEMPTION_TIMER)
> >> >>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
> >> >>> -  vmcs12->vmx_preemption_timer_value);
> >> >>> +   if (vmcs12->pin_based_vm_exec_control &
> >> >> PIN_BASED_VMX_PREEMPTION_TIMER) {
> >> >>> +  if (vmcs12->vm_exit_controls &
> >> > VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
> >> >>> + vmcs12->vmx_preemption_timer_value =
> >> >>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
> >> >>> +  else
> >> >>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
> >> >>> +   vmcs12->vmx_preemption_timer_value);
> >> >>> +   }
> >> >>
> >> >> This is not correct. We still need to set the vmcs to
> >> >> vmx_preemption_timer_value. The difference is that, on exit from
L2,
> >> >> vmx_preemption_timer_value has to be updated according to the saved
> >> >> hardware state. The corresponding code is missing in your patch so
> > far.
> >> >
> >> > I think something else maybe be missing here: assuming L0 handles
exits
> >> > for L2 without involving L1 (e.g. external interrupts or ept
> > violations),
> >> > then, we may spend some cycles in L0 handling these exits. Note L1
is
> > not
> >> > aware of these exits and from L1 perspective L2 was running on the
CPU.
> >> > That means that we may need to reduce these cycles spent at
> >> > L0 from the preemtion timer or emulate a preemption timer exit to
> >> > force a transition to L1 instead of resuming L2.
> >>
> >> That's precisely what the logic I described should achieve: reload the
> >> value we saved on L2 exit on reentry.
> >
> > But don't you think we should also reduce the cycles spent at L0 from
the
> > preemption timer ? I mean, if we spent X cycles at L0 handling a L2
exit
> > which was not forwarded to L1, then, before we resume L2,
> > the preemption timer should be: (previous_value_on_exit - X).
> > If (previous_value_on_exit - X) < 0, then we should force ("emulate") a
> > preemption timer exit between L2 and L1.
> Sorry, I previously misunderstand your comments. But why should we
> need to exclude cycles in L0 from L2 preemption value? These cycles
> are not spent by L2 and it should not be on L2.

L1 asked the "hardware" (emulated by L0) to run L2 and force an exit
after "Y" cycles. Now, in practice, we may spend "X" cycles at L0 handling
exits without switching to L1. That means that from L1 perspective L2
was running all these X cycles. L1 should assume that the instructions per
cycle
the CPU executed decreased but the cycles were spent. That's why I believe
you should take in account these X cycles.

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Arthur Chunqi Li
On Sun, Aug 25, 2013 at 3:50 PM, Abel Gordon  wrote:
>
>
> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:43:12 AM:
>
>> From: Jan Kiszka 
>> To: Abel Gordon/Haifa/IBM@IBMIL,
>> Cc: g...@redhat.com, kvm@vger.kernel.org, kvm-ow...@vger.kernel.org,
>> pbonz...@redhat.com, "李春奇 " 
>> Date: 25/08/2013 10:43 AM
>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
> timer
>> Sent by: kvm-ow...@vger.kernel.org
>>
>> On 2013-08-25 09:37, Abel Gordon wrote:
>> >
>> >
>> >> From: Jan Kiszka 
>> >> To: "李春奇 "  ,
>> >> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
>> >> Date: 25/08/2013 09:44 AM
>> >> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
>> > timer
>> >> Sent by: kvm-ow...@vger.kernel.org
>> >>
>> >> On 2013-08-24 20:44, root 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 
>> >>> ---
>> >
>> >>>
>> >>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
>> >> *vcpu, struct vmcs12 *vmcs12)
>> >>>(vmcs_config.pin_based_exec_ctrl |
>> >>> vmcs12->pin_based_vm_exec_control));
>> >>>
>> >>> -   if (vmcs12->pin_based_vm_exec_control &
>> > PIN_BASED_VMX_PREEMPTION_TIMER)
>> >>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>> >>> -  vmcs12->vmx_preemption_timer_value);
>> >>> +   if (vmcs12->pin_based_vm_exec_control &
>> >> PIN_BASED_VMX_PREEMPTION_TIMER) {
>> >>> +  if (vmcs12->vm_exit_controls &
>> > VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
>> >>> + vmcs12->vmx_preemption_timer_value =
>> >>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
>> >>> +  else
>> >>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>> >>> +   vmcs12->vmx_preemption_timer_value);
>> >>> +   }
>> >>
>> >> This is not correct. We still need to set the vmcs to
>> >> vmx_preemption_timer_value. The difference is that, on exit from L2,
>> >> vmx_preemption_timer_value has to be updated according to the saved
>> >> hardware state. The corresponding code is missing in your patch so
> far.
>> >
>> > I think something else maybe be missing here: assuming L0 handles exits
>> > for L2 without involving L1 (e.g. external interrupts or ept
> violations),
>> > then, we may spend some cycles in L0 handling these exits. Note L1 is
> not
>> > aware of these exits and from L1 perspective L2 was running on the CPU.
>> > That means that we may need to reduce these cycles spent at
>> > L0 from the preemtion timer or emulate a preemption timer exit to
>> > force a transition to L1 instead of resuming L2.
>>
>> That's precisely what the logic I described should achieve: reload the
>> value we saved on L2 exit on reentry.
>
> But don't you think we should also reduce the cycles spent at L0 from the
> preemption timer ? I mean, if we spent X cycles at L0 handling a L2 exit
> which was not forwarded to L1, then, before we resume L2,
> the preemption timer should be: (previous_value_on_exit - X).
> If (previous_value_on_exit - X) < 0, then we should force ("emulate") a
> preemption timer exit between L2 and L1.
Sorry, I previously misunderstand your comments. But why should we
need to exclude cycles in L0 from L2 preemption value? These cycles
are not spent by L2 and it should not be on L2.

Arthur
>
>
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Jan Kiszka
On 2013-08-25 09:50, Abel Gordon wrote:
> 
> 
> kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:43:12 AM:
> 
>> From: Jan Kiszka 
>> To: Abel Gordon/Haifa/IBM@IBMIL,
>> Cc: g...@redhat.com, kvm@vger.kernel.org, kvm-ow...@vger.kernel.org,
>> pbonz...@redhat.com, "李春奇 " 
>> Date: 25/08/2013 10:43 AM
>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
> timer
>> Sent by: kvm-ow...@vger.kernel.org
>>
>> On 2013-08-25 09:37, Abel Gordon wrote:
>>>
>>>
>>>> From: Jan Kiszka 
>>>> To: "李春奇 "  ,
>>>> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
>>>> Date: 25/08/2013 09:44 AM
>>>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
>>> timer
>>>> Sent by: kvm-ow...@vger.kernel.org
>>>>
>>>> On 2013-08-24 20:44, root 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 
>>>>> ---
>>>
>>>>>
>>>>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
>>>> *vcpu, struct vmcs12 *vmcs12)
>>>>>(vmcs_config.pin_based_exec_ctrl |
>>>>> vmcs12->pin_based_vm_exec_control));
>>>>>
>>>>> -   if (vmcs12->pin_based_vm_exec_control &
>>> PIN_BASED_VMX_PREEMPTION_TIMER)
>>>>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>> -  vmcs12->vmx_preemption_timer_value);
>>>>> +   if (vmcs12->pin_based_vm_exec_control &
>>>> PIN_BASED_VMX_PREEMPTION_TIMER) {
>>>>> +  if (vmcs12->vm_exit_controls &
>>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
>>>>> + vmcs12->vmx_preemption_timer_value =
>>>>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
>>>>> +  else
>>>>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>>>> +   vmcs12->vmx_preemption_timer_value);
>>>>> +   }
>>>>
>>>> This is not correct. We still need to set the vmcs to
>>>> vmx_preemption_timer_value. The difference is that, on exit from L2,
>>>> vmx_preemption_timer_value has to be updated according to the saved
>>>> hardware state. The corresponding code is missing in your patch so
> far.
>>>
>>> I think something else maybe be missing here: assuming L0 handles exits
>>> for L2 without involving L1 (e.g. external interrupts or ept
> violations),
>>> then, we may spend some cycles in L0 handling these exits. Note L1 is
> not
>>> aware of these exits and from L1 perspective L2 was running on the CPU.
>>> That means that we may need to reduce these cycles spent at
>>> L0 from the preemtion timer or emulate a preemption timer exit to
>>> force a transition to L1 instead of resuming L2.
>>
>> That's precisely what the logic I described should achieve: reload the
>> value we saved on L2 exit on reentry.
> 
> But don't you think we should also reduce the cycles spent at L0 from the
> preemption timer ? I mean, if we spent X cycles at L0 handling a L2 exit
> which was not forwarded to L1, then, before we resume L2,
> the preemption timer should be: (previous_value_on_exit - X).
> If (previous_value_on_exit - X) < 0, then we should force ("emulate") a
> preemption timer exit between L2 and L1.

We ask the hardware to save the value of the preemption on L2 exit. This
value will be exposed to L1 (if it asked for saving as well) and/or be
written back to the hardware on L2 reenty (unless L1 had a chance to run
and modified it). So the time spent in L0 is implicitly subtracted.

Jan

PS: You had kvm-owner in CC.



signature.asc
Description: OpenPGP digital signature


Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Abel Gordon


kvm-ow...@vger.kernel.org wrote on 25/08/2013 10:43:12 AM:

> From: Jan Kiszka 
> To: Abel Gordon/Haifa/IBM@IBMIL,
> Cc: g...@redhat.com, kvm@vger.kernel.org, kvm-ow...@vger.kernel.org,
> pbonz...@redhat.com, "李春奇 " 
> Date: 25/08/2013 10:43 AM
> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
timer
> Sent by: kvm-ow...@vger.kernel.org
>
> On 2013-08-25 09:37, Abel Gordon wrote:
> >
> >
> >> From: Jan Kiszka 
> >> To: "李春奇 "  ,
> >> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
> >> Date: 25/08/2013 09:44 AM
> >> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
> > timer
> >> Sent by: kvm-ow...@vger.kernel.org
> >>
> >> On 2013-08-24 20:44, root 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 
> >>> ---
> >
> >>>
> >>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
> >> *vcpu, struct vmcs12 *vmcs12)
> >>>(vmcs_config.pin_based_exec_ctrl |
> >>> vmcs12->pin_based_vm_exec_control));
> >>>
> >>> -   if (vmcs12->pin_based_vm_exec_control &
> > PIN_BASED_VMX_PREEMPTION_TIMER)
> >>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
> >>> -  vmcs12->vmx_preemption_timer_value);
> >>> +   if (vmcs12->pin_based_vm_exec_control &
> >> PIN_BASED_VMX_PREEMPTION_TIMER) {
> >>> +  if (vmcs12->vm_exit_controls &
> > VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
> >>> + vmcs12->vmx_preemption_timer_value =
> >>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
> >>> +  else
> >>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
> >>> +   vmcs12->vmx_preemption_timer_value);
> >>> +   }
> >>
> >> This is not correct. We still need to set the vmcs to
> >> vmx_preemption_timer_value. The difference is that, on exit from L2,
> >> vmx_preemption_timer_value has to be updated according to the saved
> >> hardware state. The corresponding code is missing in your patch so
far.
> >
> > I think something else maybe be missing here: assuming L0 handles exits
> > for L2 without involving L1 (e.g. external interrupts or ept
violations),
> > then, we may spend some cycles in L0 handling these exits. Note L1 is
not
> > aware of these exits and from L1 perspective L2 was running on the CPU.
> > That means that we may need to reduce these cycles spent at
> > L0 from the preemtion timer or emulate a preemption timer exit to
> > force a transition to L1 instead of resuming L2.
>
> That's precisely what the logic I described should achieve: reload the
> value we saved on L2 exit on reentry.

But don't you think we should also reduce the cycles spent at L0 from the
preemption timer ? I mean, if we spent X cycles at L0 handling a L2 exit
which was not forwarded to L1, then, before we resume L2,
the preemption timer should be: (previous_value_on_exit - X).
If (previous_value_on_exit - X) < 0, then we should force ("emulate") a
preemption timer exit between L2 and L1.


--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Arthur Chunqi Li
On Sun, Aug 25, 2013 at 3:44 PM, Jan Kiszka  wrote:
> On 2013-08-25 09:37, Arthur Chunqi Li wrote:
>> On Sun, Aug 25, 2013 at 3:28 PM, Jan Kiszka  wrote:
>>> On 2013-08-25 09:24, Arthur Chunqi Li wrote:
 On Sun, Aug 25, 2013 at 2:44 PM, Jan Kiszka  wrote:
> On 2013-08-24 20:44, root 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 
>> ---
>>  arch/x86/kvm/vmx.c |   30 +-
>>  1 file changed, 25 insertions(+), 5 deletions(-)
>>
>> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
>> index 57b4e12..9579409 100644
>> --- a/arch/x86/kvm/vmx.c
>> +++ b/arch/x86/kvm/vmx.c
>> @@ -2204,7 +2204,8 @@ 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;
>>   nested_vmx_exit_ctls_high |= (VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
>> VM_EXIT_LOAD_IA32_EFER);
>
> In the absence of VM_EXIT_SAVE_VMX_PREEMPTION_TIMER, you need to hide
> PIN_BASED_VMX_PREEMPTION_TIMER from the guest as we cannot emulate its
> behavior properly in that case.
>> Besides, we need to test that in the absence of
>> PIN_BASED_VMX_PREEMPTION_TIMER, we need to hide
>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER, though this should not happen
>> according to Intel SDM.
>
> If the SDM guarantees this for us, we don't need such a safety measure.
> Otherwise, it should be added, yes.
The SDM has such description (see 26.2.1.2):

If “activate VMX-preemption timer” VM-execution control is 0, the
“save VMX-preemption timer value” VM-exit
control must also be 0.

It doesn't tell us if these two flags are consistent when getting them
from related MSR (IA32_VMX_PINBASED_CTLS and IA32_VMX_EXIT_CTLS). So I
think the check is needed here.

Arthur
>
> Jan
>
>
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Jan Kiszka
On 2013-08-25 09:37, Arthur Chunqi Li wrote:
> On Sun, Aug 25, 2013 at 3:28 PM, Jan Kiszka  wrote:
>> On 2013-08-25 09:24, Arthur Chunqi Li wrote:
>>> On Sun, Aug 25, 2013 at 2:44 PM, Jan Kiszka  wrote:
 On 2013-08-24 20:44, root 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 
> ---
>  arch/x86/kvm/vmx.c |   30 +-
>  1 file changed, 25 insertions(+), 5 deletions(-)
>
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> index 57b4e12..9579409 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -2204,7 +2204,8 @@ 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;
>   nested_vmx_exit_ctls_high |= (VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
> VM_EXIT_LOAD_IA32_EFER);

 In the absence of VM_EXIT_SAVE_VMX_PREEMPTION_TIMER, you need to hide
 PIN_BASED_VMX_PREEMPTION_TIMER from the guest as we cannot emulate its
 behavior properly in that case.
> Besides, we need to test that in the absence of
> PIN_BASED_VMX_PREEMPTION_TIMER, we need to hide
> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER, though this should not happen
> according to Intel SDM.

If the SDM guarantees this for us, we don't need such a safety measure.
Otherwise, it should be added, yes.

Jan




signature.asc
Description: OpenPGP digital signature


Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Arthur Chunqi Li
On Sun, Aug 25, 2013 at 3:37 PM, Abel Gordon  wrote:
>
>
>> From: Jan Kiszka 
>> To: "李春奇 "  ,
>> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
>> Date: 25/08/2013 09:44 AM
>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
> timer
>> Sent by: kvm-ow...@vger.kernel.org
>>
>> On 2013-08-24 20:44, root 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 
>> > ---
>
>> >
>> > @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
>> *vcpu, struct vmcs12 *vmcs12)
>> >(vmcs_config.pin_based_exec_ctrl |
>> > vmcs12->pin_based_vm_exec_control));
>> >
>> > -   if (vmcs12->pin_based_vm_exec_control &
> PIN_BASED_VMX_PREEMPTION_TIMER)
>> > -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>> > -  vmcs12->vmx_preemption_timer_value);
>> > +   if (vmcs12->pin_based_vm_exec_control &
>> PIN_BASED_VMX_PREEMPTION_TIMER) {
>> > +  if (vmcs12->vm_exit_controls &
> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
>> > + vmcs12->vmx_preemption_timer_value =
>> > +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
>> > +  else
>> > + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>> > +   vmcs12->vmx_preemption_timer_value);
>> > +   }
>>
>> This is not correct. We still need to set the vmcs to
>> vmx_preemption_timer_value. The difference is that, on exit from L2,
>> vmx_preemption_timer_value has to be updated according to the saved
>> hardware state. The corresponding code is missing in your patch so far.
>
> I think something else maybe be missing here: assuming L0 handles exits
> for L2 without involving L1 (e.g. external interrupts or ept violations),
> then, we may spend some cycles in L0 handling these exits. Note L1 is not
> aware of these exits and from L1 perspective L2 was running on the CPU.
> That means that we may need to reduce these cycles spent at
> L0 from the preemtion timer or emulate a preemption timer exit to
> force a transition to L1 instead of resuming L2.
My solution is setting "save preemption value" feature of L2 if L2
sets "vmx preemption timer" feature, thus external interrupts (or
others) will save the exact value in L2's vmcs, and the resume of L2
will load the value in L2's vmcs. Thus cycles of handling these vmexit
in L0 will not affect L2's preemption value.

Arthur
>
>
>
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Jan Kiszka
On 2013-08-25 09:37, Abel Gordon wrote:
> 
> 
>> From: Jan Kiszka 
>> To: "李春奇 "  ,
>> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
>> Date: 25/08/2013 09:44 AM
>> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
> timer
>> Sent by: kvm-ow...@vger.kernel.org
>>
>> On 2013-08-24 20:44, root 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 
>>> ---
> 
>>>
>>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
>> *vcpu, struct vmcs12 *vmcs12)
>>>(vmcs_config.pin_based_exec_ctrl |
>>> vmcs12->pin_based_vm_exec_control));
>>>
>>> -   if (vmcs12->pin_based_vm_exec_control &
> PIN_BASED_VMX_PREEMPTION_TIMER)
>>> -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>> -  vmcs12->vmx_preemption_timer_value);
>>> +   if (vmcs12->pin_based_vm_exec_control &
>> PIN_BASED_VMX_PREEMPTION_TIMER) {
>>> +  if (vmcs12->vm_exit_controls &
> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
>>> + vmcs12->vmx_preemption_timer_value =
>>> +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
>>> +  else
>>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>> +   vmcs12->vmx_preemption_timer_value);
>>> +   }
>>
>> This is not correct. We still need to set the vmcs to
>> vmx_preemption_timer_value. The difference is that, on exit from L2,
>> vmx_preemption_timer_value has to be updated according to the saved
>> hardware state. The corresponding code is missing in your patch so far.
> 
> I think something else maybe be missing here: assuming L0 handles exits
> for L2 without involving L1 (e.g. external interrupts or ept violations),
> then, we may spend some cycles in L0 handling these exits. Note L1 is not
> aware of these exits and from L1 perspective L2 was running on the CPU.
> That means that we may need to reduce these cycles spent at
> L0 from the preemtion timer or emulate a preemption timer exit to
> force a transition to L1 instead of resuming L2.

That's precisely what the logic I described should achieve: reload the
value we saved on L2 exit on reentry.

Jan




signature.asc
Description: OpenPGP digital signature


Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Arthur Chunqi Li
On Sun, Aug 25, 2013 at 3:28 PM, Jan Kiszka  wrote:
> On 2013-08-25 09:24, Arthur Chunqi Li wrote:
>> On Sun, Aug 25, 2013 at 2:44 PM, Jan Kiszka  wrote:
>>> On 2013-08-24 20:44, root 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 
 ---
  arch/x86/kvm/vmx.c |   30 +-
  1 file changed, 25 insertions(+), 5 deletions(-)

 diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
 index 57b4e12..9579409 100644
 --- a/arch/x86/kvm/vmx.c
 +++ b/arch/x86/kvm/vmx.c
 @@ -2204,7 +2204,8 @@ 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;
   nested_vmx_exit_ctls_high |= (VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
 VM_EXIT_LOAD_IA32_EFER);
>>>
>>> In the absence of VM_EXIT_SAVE_VMX_PREEMPTION_TIMER, you need to hide
>>> PIN_BASED_VMX_PREEMPTION_TIMER from the guest as we cannot emulate its
>>> behavior properly in that case.
Besides, we need to test that in the absence of
PIN_BASED_VMX_PREEMPTION_TIMER, we need to hide
VM_EXIT_SAVE_VMX_PREEMPTION_TIMER, though this should not happen
according to Intel SDM.
>>>

 @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, 
 struct vmcs12 *vmcs12)
   (vmcs_config.pin_based_exec_ctrl |
vmcs12->pin_based_vm_exec_control));

 - if (vmcs12->pin_based_vm_exec_control & 
 PIN_BASED_VMX_PREEMPTION_TIMER)
 - vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
 -  vmcs12->vmx_preemption_timer_value);
 + if (vmcs12->pin_based_vm_exec_control & 
 PIN_BASED_VMX_PREEMPTION_TIMER) {
 + if (vmcs12->vm_exit_controls & 
 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
 + vmcs12->vmx_preemption_timer_value =
 + vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
 + else
 + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
 + vmcs12->vmx_preemption_timer_value);
 + }
>>>
>>> This is not correct. We still need to set the vmcs to
>>> vmx_preemption_timer_value. The difference is that, on exit from L2,
>>> vmx_preemption_timer_value has to be updated according to the saved
>>> hardware state. The corresponding code is missing in your patch so far.
>>>

   /*
* Whether page-faults are trapped is determined by a combination of
 @@ -7690,7 +7696,11 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, 
 struct vmcs12 *vmcs12)
* we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
* bits are further modified by vmx_set_efer() below.
*/
 - vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
 + if (vmcs12->pin_based_vm_exec_control & 
 PIN_BASED_VMX_PREEMPTION_TIMER)
 + vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl |
 + VM_EXIT_SAVE_VMX_PREEMPTION_TIMER);
 + else
 + vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
>>>
>>> Let's prepare the value for VM_EXIT_CONTROLS in a local variable first,
>>> then write it to the vmcs.
>>>

   /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
* emulated by vmx_set_efer(), below.
 @@ -7912,6 +7922,16 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, 
 bool launch)
   }

   /*
 +  * If L2 support PIN_BASED_VMX_PREEMPTION_TIMER, L0 must support
 +  * VM_EXIT_SAVE_VMX_PREEMPTION_TIMER.
 +  */
 + if ((vmcs12->pin_based_vm_exec_control & 
 PIN_BASED_VMX_PREEMPTION_TIMER) &&
 + !(nested_vmx_exit_ctls_high & 
 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)) {
 + nested_vmx_failValid(vcpu, 
 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
 + return 1;
 + }
>>>
>>> Nope, the guest is free to run the preemption timer without saving on
>>> exits. It may have a valid use case for this, e.g. that it will always
>>> reprogram it on entry.
>> Here "!(nested_vmx_exit_ctls_high &
>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)" is used to detect if hardware
>> support "save preemption timer" feature, which means if L2 supports
>

Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Abel Gordon


> From: Jan Kiszka 
> To: "李春奇 "  ,
> Cc: kvm@vger.kernel.org, g...@redhat.com, pbonz...@redhat.com
> Date: 25/08/2013 09:44 AM
> Subject: Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption
timer
> Sent by: kvm-ow...@vger.kernel.org
>
> On 2013-08-24 20:44, root 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 
> > ---

> >
> > @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu
> *vcpu, struct vmcs12 *vmcs12)
> >(vmcs_config.pin_based_exec_ctrl |
> > vmcs12->pin_based_vm_exec_control));
> >
> > -   if (vmcs12->pin_based_vm_exec_control &
PIN_BASED_VMX_PREEMPTION_TIMER)
> > -  vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
> > -  vmcs12->vmx_preemption_timer_value);
> > +   if (vmcs12->pin_based_vm_exec_control &
> PIN_BASED_VMX_PREEMPTION_TIMER) {
> > +  if (vmcs12->vm_exit_controls &
VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
> > + vmcs12->vmx_preemption_timer_value =
> > +vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
> > +  else
> > + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
> > +   vmcs12->vmx_preemption_timer_value);
> > +   }
>
> This is not correct. We still need to set the vmcs to
> vmx_preemption_timer_value. The difference is that, on exit from L2,
> vmx_preemption_timer_value has to be updated according to the saved
> hardware state. The corresponding code is missing in your patch so far.

I think something else maybe be missing here: assuming L0 handles exits
for L2 without involving L1 (e.g. external interrupts or ept violations),
then, we may spend some cycles in L0 handling these exits. Note L1 is not
aware of these exits and from L1 perspective L2 was running on the CPU.
That means that we may need to reduce these cycles spent at
L0 from the preemtion timer or emulate a preemption timer exit to
force a transition to L1 instead of resuming L2.



--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Jan Kiszka
On 2013-08-25 09:24, Arthur Chunqi Li wrote:
> On Sun, Aug 25, 2013 at 2:44 PM, Jan Kiszka  wrote:
>> On 2013-08-24 20:44, root 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 
>>> ---
>>>  arch/x86/kvm/vmx.c |   30 +-
>>>  1 file changed, 25 insertions(+), 5 deletions(-)
>>>
>>> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
>>> index 57b4e12..9579409 100644
>>> --- a/arch/x86/kvm/vmx.c
>>> +++ b/arch/x86/kvm/vmx.c
>>> @@ -2204,7 +2204,8 @@ 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;
>>>   nested_vmx_exit_ctls_high |= (VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
>>> VM_EXIT_LOAD_IA32_EFER);
>>
>> In the absence of VM_EXIT_SAVE_VMX_PREEMPTION_TIMER, you need to hide
>> PIN_BASED_VMX_PREEMPTION_TIMER from the guest as we cannot emulate its
>> behavior properly in that case.
>>
>>>
>>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, 
>>> struct vmcs12 *vmcs12)
>>>   (vmcs_config.pin_based_exec_ctrl |
>>>vmcs12->pin_based_vm_exec_control));
>>>
>>> - if (vmcs12->pin_based_vm_exec_control & 
>>> PIN_BASED_VMX_PREEMPTION_TIMER)
>>> - vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>> -  vmcs12->vmx_preemption_timer_value);
>>> + if (vmcs12->pin_based_vm_exec_control & 
>>> PIN_BASED_VMX_PREEMPTION_TIMER) {
>>> + if (vmcs12->vm_exit_controls & 
>>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
>>> + vmcs12->vmx_preemption_timer_value =
>>> + vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
>>> + else
>>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>>> + vmcs12->vmx_preemption_timer_value);
>>> + }
>>
>> This is not correct. We still need to set the vmcs to
>> vmx_preemption_timer_value. The difference is that, on exit from L2,
>> vmx_preemption_timer_value has to be updated according to the saved
>> hardware state. The corresponding code is missing in your patch so far.
>>
>>>
>>>   /*
>>>* Whether page-faults are trapped is determined by a combination of
>>> @@ -7690,7 +7696,11 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, 
>>> struct vmcs12 *vmcs12)
>>>* we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
>>>* bits are further modified by vmx_set_efer() below.
>>>*/
>>> - vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
>>> + if (vmcs12->pin_based_vm_exec_control & 
>>> PIN_BASED_VMX_PREEMPTION_TIMER)
>>> + vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl |
>>> + VM_EXIT_SAVE_VMX_PREEMPTION_TIMER);
>>> + else
>>> + vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
>>
>> Let's prepare the value for VM_EXIT_CONTROLS in a local variable first,
>> then write it to the vmcs.
>>
>>>
>>>   /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
>>>* emulated by vmx_set_efer(), below.
>>> @@ -7912,6 +7922,16 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, 
>>> bool launch)
>>>   }
>>>
>>>   /*
>>> +  * If L2 support PIN_BASED_VMX_PREEMPTION_TIMER, L0 must support
>>> +  * VM_EXIT_SAVE_VMX_PREEMPTION_TIMER.
>>> +  */
>>> + if ((vmcs12->pin_based_vm_exec_control & 
>>> PIN_BASED_VMX_PREEMPTION_TIMER) &&
>>> + !(nested_vmx_exit_ctls_high & 
>>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)) {
>>> + nested_vmx_failValid(vcpu, 
>>> VMXERR_ENTRY_INVALID_CONTROL_FIELD);
>>> + return 1;
>>> + }
>>
>> Nope, the guest is free to run the preemption timer without saving on
>> exits. It may have a valid use case for this, e.g. that it will always
>> reprogram it on entry.
> Here "!(nested_vmx_exit_ctls_high &
> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)" is used to detect if hardware
> support "save preemption timer" feature, which means if L2 supports
> pinbased vmx preemption timer, host must support "save preemption
> timer" feature.

Sorry, parsed the code incorrectly.

> Though nested_vmx_exit_ctls_* is used for nested env,
> but it can also used to reflect the host's feature. Here is what I
> discuss with you yesterday, and we can also get the feature via
> "rdmsr" here to avoid the con

Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-25 Thread Arthur Chunqi Li
On Sun, Aug 25, 2013 at 2:44 PM, Jan Kiszka  wrote:
> On 2013-08-24 20:44, root 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 
>> ---
>>  arch/x86/kvm/vmx.c |   30 +-
>>  1 file changed, 25 insertions(+), 5 deletions(-)
>>
>> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
>> index 57b4e12..9579409 100644
>> --- a/arch/x86/kvm/vmx.c
>> +++ b/arch/x86/kvm/vmx.c
>> @@ -2204,7 +2204,8 @@ 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;
>>   nested_vmx_exit_ctls_high |= (VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
>> VM_EXIT_LOAD_IA32_EFER);
>
> In the absence of VM_EXIT_SAVE_VMX_PREEMPTION_TIMER, you need to hide
> PIN_BASED_VMX_PREEMPTION_TIMER from the guest as we cannot emulate its
> behavior properly in that case.
>
>>
>> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, 
>> struct vmcs12 *vmcs12)
>>   (vmcs_config.pin_based_exec_ctrl |
>>vmcs12->pin_based_vm_exec_control));
>>
>> - if (vmcs12->pin_based_vm_exec_control & PIN_BASED_VMX_PREEMPTION_TIMER)
>> - vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>> -  vmcs12->vmx_preemption_timer_value);
>> + if (vmcs12->pin_based_vm_exec_control & 
>> PIN_BASED_VMX_PREEMPTION_TIMER) {
>> + if (vmcs12->vm_exit_controls & 
>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
>> + vmcs12->vmx_preemption_timer_value =
>> + vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
>> + else
>> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
>> + vmcs12->vmx_preemption_timer_value);
>> + }
>
> This is not correct. We still need to set the vmcs to
> vmx_preemption_timer_value. The difference is that, on exit from L2,
> vmx_preemption_timer_value has to be updated according to the saved
> hardware state. The corresponding code is missing in your patch so far.
>
>>
>>   /*
>>* Whether page-faults are trapped is determined by a combination of
>> @@ -7690,7 +7696,11 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, 
>> struct vmcs12 *vmcs12)
>>* we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
>>* bits are further modified by vmx_set_efer() below.
>>*/
>> - vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
>> + if (vmcs12->pin_based_vm_exec_control & PIN_BASED_VMX_PREEMPTION_TIMER)
>> + vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl |
>> + VM_EXIT_SAVE_VMX_PREEMPTION_TIMER);
>> + else
>> + vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
>
> Let's prepare the value for VM_EXIT_CONTROLS in a local variable first,
> then write it to the vmcs.
>
>>
>>   /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
>>* emulated by vmx_set_efer(), below.
>> @@ -7912,6 +7922,16 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool 
>> launch)
>>   }
>>
>>   /*
>> +  * If L2 support PIN_BASED_VMX_PREEMPTION_TIMER, L0 must support
>> +  * VM_EXIT_SAVE_VMX_PREEMPTION_TIMER.
>> +  */
>> + if ((vmcs12->pin_based_vm_exec_control & 
>> PIN_BASED_VMX_PREEMPTION_TIMER) &&
>> + !(nested_vmx_exit_ctls_high & 
>> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)) {
>> + nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
>> + return 1;
>> + }
>
> Nope, the guest is free to run the preemption timer without saving on
> exits. It may have a valid use case for this, e.g. that it will always
> reprogram it on entry.
Here "!(nested_vmx_exit_ctls_high &
VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)" is used to detect if hardware
support "save preemption timer" feature, which means if L2 supports
pinbased vmx preemption timer, host must support "save preemption
timer" feature. Though nested_vmx_exit_ctls_* is used for nested env,
but it can also used to reflect the host's feature. Here is what I
discuss with you yesterday, and we can also get the feature via
"rdmsr" here to avoid the confusion.

Arthur
>
>> +
>> + /*
>>* We're finally done with prerequisite checking, and can start with
>>* the nested entry.
>>*/
>>
>
> Jan
>
--
To unsubscribe from this list: send the line 

Re: [PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-24 Thread Jan Kiszka
On 2013-08-24 20:44, root 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 
> ---
>  arch/x86/kvm/vmx.c |   30 +-
>  1 file changed, 25 insertions(+), 5 deletions(-)
> 
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> index 57b4e12..9579409 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -2204,7 +2204,8 @@ 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;
>   nested_vmx_exit_ctls_high |= (VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
> VM_EXIT_LOAD_IA32_EFER);

In the absence of VM_EXIT_SAVE_VMX_PREEMPTION_TIMER, you need to hide
PIN_BASED_VMX_PREEMPTION_TIMER from the guest as we cannot emulate its
behavior properly in that case.

>  
> @@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, 
> struct vmcs12 *vmcs12)
>   (vmcs_config.pin_based_exec_ctrl |
>vmcs12->pin_based_vm_exec_control));
>  
> - if (vmcs12->pin_based_vm_exec_control & PIN_BASED_VMX_PREEMPTION_TIMER)
> - vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
> -  vmcs12->vmx_preemption_timer_value);
> + if (vmcs12->pin_based_vm_exec_control & PIN_BASED_VMX_PREEMPTION_TIMER) 
> {
> + if (vmcs12->vm_exit_controls & 
> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
> + vmcs12->vmx_preemption_timer_value =
> + vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
> + else
> + vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
> + vmcs12->vmx_preemption_timer_value);
> + }

This is not correct. We still need to set the vmcs to
vmx_preemption_timer_value. The difference is that, on exit from L2,
vmx_preemption_timer_value has to be updated according to the saved
hardware state. The corresponding code is missing in your patch so far.

>  
>   /*
>* Whether page-faults are trapped is determined by a combination of
> @@ -7690,7 +7696,11 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, 
> struct vmcs12 *vmcs12)
>* we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
>* bits are further modified by vmx_set_efer() below.
>*/
> - vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
> + if (vmcs12->pin_based_vm_exec_control & PIN_BASED_VMX_PREEMPTION_TIMER)
> + vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl |
> + VM_EXIT_SAVE_VMX_PREEMPTION_TIMER);
> + else
> + vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);

Let's prepare the value for VM_EXIT_CONTROLS in a local variable first,
then write it to the vmcs.

>  
>   /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
>* emulated by vmx_set_efer(), below.
> @@ -7912,6 +7922,16 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool 
> launch)
>   }
>  
>   /*
> +  * If L2 support PIN_BASED_VMX_PREEMPTION_TIMER, L0 must support
> +  * VM_EXIT_SAVE_VMX_PREEMPTION_TIMER.
> +  */
> + if ((vmcs12->pin_based_vm_exec_control & 
> PIN_BASED_VMX_PREEMPTION_TIMER) &&
> + !(nested_vmx_exit_ctls_high & 
> VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)) {
> + nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
> + return 1;
> + }

Nope, the guest is free to run the preemption timer without saving on
exits. It may have a valid use case for this, e.g. that it will always
reprogram it on entry.

> +
> + /*
>* We're finally done with prerequisite checking, and can start with
>* the nested entry.
>*/
> 

Jan



signature.asc
Description: OpenPGP digital signature


[PATCH] KVM: nVMX: Fully support of nested VMX preemption timer

2013-08-24 Thread root
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 
---
 arch/x86/kvm/vmx.c |   30 +-
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 57b4e12..9579409 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -2204,7 +2204,8 @@ 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;
nested_vmx_exit_ctls_high |= (VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  VM_EXIT_LOAD_IA32_EFER);
 
@@ -7578,9 +7579,14 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct 
vmcs12 *vmcs12)
(vmcs_config.pin_based_exec_ctrl |
 vmcs12->pin_based_vm_exec_control));
 
-   if (vmcs12->pin_based_vm_exec_control & PIN_BASED_VMX_PREEMPTION_TIMER)
-   vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
-vmcs12->vmx_preemption_timer_value);
+   if (vmcs12->pin_based_vm_exec_control & PIN_BASED_VMX_PREEMPTION_TIMER) 
{
+   if (vmcs12->vm_exit_controls & 
VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
+   vmcs12->vmx_preemption_timer_value =
+   vmcs_read32(VMX_PREEMPTION_TIMER_VALUE);
+   else
+   vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
+   vmcs12->vmx_preemption_timer_value);
+   }
 
/*
 * Whether page-faults are trapped is determined by a combination of
@@ -7690,7 +7696,11 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct 
vmcs12 *vmcs12)
 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
 * bits are further modified by vmx_set_efer() below.
 */
-   vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
+   if (vmcs12->pin_based_vm_exec_control & PIN_BASED_VMX_PREEMPTION_TIMER)
+   vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl |
+   VM_EXIT_SAVE_VMX_PREEMPTION_TIMER);
+   else
+   vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
 
/* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
 * emulated by vmx_set_efer(), below.
@@ -7912,6 +7922,16 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool 
launch)
}
 
/*
+* If L2 support PIN_BASED_VMX_PREEMPTION_TIMER, L0 must support
+* VM_EXIT_SAVE_VMX_PREEMPTION_TIMER.
+*/
+   if ((vmcs12->pin_based_vm_exec_control & 
PIN_BASED_VMX_PREEMPTION_TIMER) &&
+   !(nested_vmx_exit_ctls_high & 
VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)) {
+   nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
+   return 1;
+   }
+
+   /*
 * We're finally done with prerequisite checking, and can start with
 * the nested entry.
 */
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html