diff mbox

[v2,1/2] KVM: nVMX: enhance allocate/free_vpid to handle shadow vpid

Message ID BLU436-SMTP2539135088523E893EC341A805C0@phx.gbl (mailing list archive)
State New, archived
Headers show

Commit Message

Wanpeng Li Sept. 15, 2015, 10:30 a.m. UTC
Enhance allocate/free_vid to handle shadow vpid.

Suggested-by: Wincy Van <fanwenyi0529@gmail.com>
Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
---
 arch/x86/kvm/vmx.c | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)

Comments

Paolo Bonzini Sept. 15, 2015, 12:54 p.m. UTC | #1
On 15/09/2015 12:30, Wanpeng Li wrote:
> +	if (!nested) {
> +		vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
> +		if (vpid < VMX_NR_VPIDS) {
>  		vmx->vpid = vpid;
>  		__set_bit(vpid, vmx_vpid_bitmap);
> +		}
> +	} else {
> +		vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
> +		if (vpid < VMX_NR_VPIDS) {
> +			vmx->nested.vpid02 = vpid;
> +			__set_bit(vpid, vmx_vpid_bitmap);
> +		}

Messy indentation, and a lot of duplicate code.  Can you instead have
(which I think was Jan's suggestion too):

static int allocate_vpid(void);
static void free_vpid(int vpid);

That said, I like the simple solution to the "too many VPIDs for each L1
VCPU" processor.

Paolo
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Wanpeng Li Sept. 16, 2015, 3:58 a.m. UTC | #2
On 9/15/15 8:54 PM, Paolo Bonzini wrote:
>
> On 15/09/2015 12:30, Wanpeng Li wrote:
>> +	if (!nested) {
>> +		vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
>> +		if (vpid < VMX_NR_VPIDS) {
>>   		vmx->vpid = vpid;
>>   		__set_bit(vpid, vmx_vpid_bitmap);
>> +		}
>> +	} else {
>> +		vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
>> +		if (vpid < VMX_NR_VPIDS) {
>> +			vmx->nested.vpid02 = vpid;
>> +			__set_bit(vpid, vmx_vpid_bitmap);
>> +		}
> Messy indentation, and a lot of duplicate code.  Can you instead have
> (which I think was Jan's suggestion too):
>
> static int allocate_vpid(void);
> static void free_vpid(int vpid);

I see, done in v3.

>
> That said, I like the simple solution to the "too many VPIDs for each L1
> VCPU" processor.

Thanks. :-)

Regards,
Wanpeng Li

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

Patch

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index da1590e..bd07d88 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -424,6 +424,8 @@  struct nested_vmx {
 	/* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
 	u64 vmcs01_debugctl;
 
+	u16 vpid02;
+
 	u32 nested_vmx_procbased_ctls_low;
 	u32 nested_vmx_procbased_ctls_high;
 	u32 nested_vmx_true_procbased_ctls_low;
@@ -4155,18 +4157,29 @@  static int alloc_identity_pagetable(struct kvm *kvm)
 	return r;
 }
 
-static void allocate_vpid(struct vcpu_vmx *vmx)
+static void allocate_vpid(struct vcpu_vmx *vmx, bool nested)
 {
 	int vpid;
 
-	vmx->vpid = 0;
 	if (!enable_vpid)
 		return;
+	if (!nested)
+		vmx->vpid = 0;
+	else
+		vmx->nested.vpid02 = 0;
 	spin_lock(&vmx_vpid_lock);
-	vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
-	if (vpid < VMX_NR_VPIDS) {
+	if (!nested) {
+		vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
+		if (vpid < VMX_NR_VPIDS) {
 		vmx->vpid = vpid;
 		__set_bit(vpid, vmx_vpid_bitmap);
+		}
+	} else {
+		vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
+		if (vpid < VMX_NR_VPIDS) {
+			vmx->nested.vpid02 = vpid;
+			__set_bit(vpid, vmx_vpid_bitmap);
+		}
 	}
 	spin_unlock(&vmx_vpid_lock);
 }
@@ -4178,6 +4191,12 @@  static void free_vpid(struct vcpu_vmx *vmx)
 	spin_lock(&vmx_vpid_lock);
 	if (vmx->vpid != 0)
 		__clear_bit(vmx->vpid, vmx_vpid_bitmap);
+	if (!nested) {
+		spin_unlock(&vmx_vpid_lock);
+		return;
+	}
+	if (vmx->nested.vpid02)
+		__clear_bit(vmx->nested.vpid02, vmx_vpid_bitmap);
 	spin_unlock(&vmx_vpid_lock);
 }
 
@@ -8509,7 +8528,7 @@  static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 	if (!vmx)
 		return ERR_PTR(-ENOMEM);
 
-	allocate_vpid(vmx);
+	allocate_vpid(vmx, false);
 
 	err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
 	if (err)
@@ -8557,8 +8576,10 @@  static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 			goto free_vmcs;
 	}
 
-	if (nested)
+	if (nested) {
 		nested_vmx_setup_ctls_msrs(vmx);
+		allocate_vpid(vmx, true);
+	}
 
 	vmx->nested.posted_intr_nv = -1;
 	vmx->nested.current_vmptr = -1ull;