[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen staging-4.7] x86/vmx: Factor locate_msr_entry() out of vmx_find_msr() and vmx_add_msr()



commit 87dba8038bcb401ffc23ec226860b7b3ec2c3943
Author:     Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
AuthorDate: Mon May 7 11:57:00 2018 +0100
Commit:     Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
CommitDate: Tue Aug 14 17:27:59 2018 +0100

    x86/vmx: Factor locate_msr_entry() out of vmx_find_msr() and vmx_add_msr()
    
    Instead of having multiple algorithms searching the MSR lists, implement a
    single one.  It has the semantics required by vmx_add_msr(), to identify the
    position in which an MSR should live, if it isn't already present.
    
    There will be a marginal improvement for vmx_find_msr() by avoiding the
    function pointer calls to vmx_msr_entry_key_cmp(), and a major improvement 
for
    vmx_add_msr() by using a binary search instead of a linear search.
    
    Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
    Reviewed-by: Roger Pau Monné <roger.pau@xxxxxxxxxx>
    Acked-by: Kevin Tian <kevin.tian@xxxxxxxxx>
    (cherry picked from commit 4d94828cf11104256dccea1fa7762f00575dfaa0)
---
 xen/arch/x86/hvm/vmx/vmcs.c | 44 +++++++++++++++++++++++++++++++++-----------
 1 file changed, 33 insertions(+), 11 deletions(-)

diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c
index e924685e9d..8020e10546 100644
--- a/xen/arch/x86/hvm/vmx/vmcs.c
+++ b/xen/arch/x86/hvm/vmx/vmcs.c
@@ -1332,12 +1332,37 @@ static int construct_vmcs(struct vcpu *v)
     return rc;
 }
 
+/*
+ * Search an MSR list looking for an MSR entry, or the slot in which it should
+ * live (to keep the data sorted) if an entry is not found.
+ *
+ * The return pointer is guaranteed to be bounded by start and end.  However,
+ * it may point at end, and may be invalid for the caller to dereference.
+ */
+static struct vmx_msr_entry *locate_msr_entry(
+    struct vmx_msr_entry *start, struct vmx_msr_entry *end, uint32_t msr)
+{
+    while ( start < end )
+    {
+        struct vmx_msr_entry *mid = start + (end - start) / 2;
+
+        if ( msr < mid->index )
+            end = mid;
+        else if ( msr > mid->index )
+            start = mid + 1;
+        else
+            return mid;
+    }
+
+    return start;
+}
+
 struct vmx_msr_entry *vmx_find_msr(uint32_t msr, enum vmx_msr_list_type type)
 {
     struct vcpu *curr = current;
     struct arch_vmx_struct *vmx = &curr->arch.hvm_vmx;
-    struct vmx_msr_entry *start = NULL;
-    unsigned int i, total;
+    struct vmx_msr_entry *start = NULL, *ent, *end;
+    unsigned int total;
 
     switch ( type )
     {
@@ -1358,13 +1383,10 @@ struct vmx_msr_entry *vmx_find_msr(uint32_t msr, enum 
vmx_msr_list_type type)
     if ( !start )
         return NULL;
 
-    for ( i = 0; i < total; i++ )
-    {
-        if ( start[i].index == msr )
-            return &start[i];
-    }
+    end = start + total;
+    ent = locate_msr_entry(start, end, msr);
 
-    return NULL;
+    return ((ent < end) && (ent->index == msr)) ? ent : NULL;
 }
 
 int vmx_add_msr(uint32_t msr, enum vmx_msr_list_type type)
@@ -1416,10 +1438,10 @@ int vmx_add_msr(uint32_t msr, enum vmx_msr_list_type 
type)
 
     start = *ptr;
     end   = start + total;
+    ent   = locate_msr_entry(start, end, msr);
 
-    for ( ent = start; ent < end && ent->index <= msr; ++ent )
-        if ( ent->index == msr )
-            return 0;
+    if ( (ent < end) && (ent->index == msr) )
+        return 0;
 
     if ( total == (PAGE_SIZE / sizeof(*ent)) )
         return -ENOSPC;
--
generated by git-patchbot for /home/xen/git/xen.git#staging-4.7

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/xen-changelog

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.