[libvirt] [PATCH 16/17] cpu_x86: Check vendor early

Jiri Denemark jdenemar at redhat.com
Sat May 14 08:30:35 UTC 2016


When searching for the best CPU model for CPUID data we can easily
ignore models with non-matching vendor before spending time on CPUID
data to virCPUDef conversion.

Signed-off-by: Jiri Denemark <jdenemar at redhat.com>
---
 src/cpu/cpu_x86.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c
index e7bd521..701486c 100644
--- a/src/cpu/cpu_x86.c
+++ b/src/cpu/cpu_x86.c
@@ -431,7 +431,7 @@ x86DataToCPUFeatures(virCPUDefPtr cpu,
 
 /* also removes bits corresponding to vendor string from data */
 static virCPUx86VendorPtr
-x86DataToVendor(virCPUx86Data *data,
+x86DataToVendor(const virCPUx86Data *data,
                 virCPUx86MapPtr map)
 {
     virCPUx86VendorPtr vendor = map->vendors;
@@ -1591,6 +1591,7 @@ x86Decode(virCPUDefPtr cpu,
     virCPUx86Data *copy = NULL;
     virCPUx86Data *features = NULL;
     const virCPUx86Data *cpuData = NULL;
+    virCPUx86VendorPtr vendor;
     size_t i;
     int rc;
 
@@ -1600,6 +1601,8 @@ x86Decode(virCPUDefPtr cpu,
     if (!data || !(map = virCPUx86GetMap()))
         return -1;
 
+    vendor = x86DataToVendor(data, map);
+
     for (candidate = map->models; candidate; candidate = candidate->next) {
         if (!cpuModelIsAllowed(candidate->name, models, nmodels)) {
             if (preferred && STREQ(candidate->name, preferred)) {
@@ -1620,19 +1623,19 @@ x86Decode(virCPUDefPtr cpu,
             continue;
         }
 
+        /* Both vendor and candidate->vendor are pointers to a single list of
+         * known vendors stored in the map.
+         */
+        if (vendor && candidate->vendor && vendor != candidate->vendor) {
+            VIR_DEBUG("CPU vendor %s of model %s differs from %s; ignoring",
+                      candidate->vendor->name, candidate->name, vendor->name);
+            continue;
+        }
+
         if (!(cpuCandidate = x86DataToCPU(data, candidate, map)))
             goto cleanup;
         cpuCandidate->type = cpu->type;
 
-        if (candidate->vendor && cpuCandidate->vendor &&
-            STRNEQ(candidate->vendor->name, cpuCandidate->vendor)) {
-            VIR_DEBUG("CPU vendor %s of model %s differs from %s; ignoring",
-                      candidate->vendor->name, candidate->name,
-                      cpuCandidate->vendor);
-            virCPUDefFree(cpuCandidate);
-            continue;
-        }
-
         if ((rc = x86DecodeUseCandidate(cpuModel, cpuCandidate, preferred,
                                         cpu->type == VIR_CPU_TYPE_HOST))) {
             virCPUDefFree(cpuModel);
@@ -1677,8 +1680,10 @@ x86Decode(virCPUDefPtr cpu,
             goto cleanup;
     }
 
+    if (vendor && VIR_STRDUP(cpu->vendor, vendor->name) < 0)
+        goto cleanup;
+
     cpu->model = cpuModel->model;
-    cpu->vendor = cpuModel->vendor;
     cpu->nfeatures = cpuModel->nfeatures;
     cpu->features = cpuModel->features;
     VIR_FREE(cpuModel);
-- 
2.8.2




More information about the libvir-list mailing list