[PATCH 4/7] util: virhostcpu: Extract filtering of the returned data from virHostCPUGetCPUID

Peter Krempa pkrempa at redhat.com
Mon Apr 25 13:28:28 UTC 2022


Move the filtering code into virHostCPUGetCPUIDFilterVolatile.

This also removes a safe but very questionable reuse of 'i' iterator in
the both the top level and nested loop. It's safe for now as the to
level loop will not iterate any more in the current state.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/util/virhostcpu.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)

diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c
index 6be00a5b76..f07514a11b 100644
--- a/src/util/virhostcpu.c
+++ b/src/util/virhostcpu.c
@@ -1316,6 +1316,32 @@ virHostCPUGetMSR(unsigned long index,
 }


+/**
+ * virHostCPUGetCPUIDFilterVolatile:
+ *
+ * Filters the 'kvm_cpuid2' struct and removes data which may change depending
+ * on the CPU core this was run on.
+ *
+ * Currently filtered fields:
+ * - local APIC ID
+ */
+static void
+virHostCPUGetCPUIDFilterVolatile(struct kvm_cpuid2 *kvm_cpuid)
+{
+    size_t i;
+
+    for (i = 0; i < kvm_cpuid->nent; ++i) {
+        struct kvm_cpuid_entry2 *entry = &kvm_cpuid->entries[i];
+
+        /* filter out local apic id */
+        if (entry->function == 0x01 && entry->index == 0x00)
+            entry->ebx &= 0x00ffffff;
+        if (entry->function == 0x0b)
+            entry->edx &= 0xffffff00;
+    }
+}
+
+
 struct kvm_cpuid2 *
 virHostCPUGetCPUID(void)
 {
@@ -1341,15 +1367,7 @@ virHostCPUGetCPUID(void)
         kvm_cpuid->nent = i;

         if (ioctl(fd, KVM_GET_SUPPORTED_CPUID, kvm_cpuid) == 0) {
-            /* filter out local apic id */
-            for (i = 0; i < kvm_cpuid->nent; ++i) {
-                struct kvm_cpuid_entry2 *entry = &kvm_cpuid->entries[i];
-                if (entry->function == 0x01 && entry->index == 0x00)
-                    entry->ebx &= 0x00ffffff;
-                if (entry->function == 0x0b)
-                    entry->edx &= 0xffffff00;
-            }
-
+            virHostCPUGetCPUIDFilterVolatile(kvm_cpuid);
             return g_steal_pointer(&kvm_cpuid);
         }
     }
-- 
2.35.1



More information about the libvir-list mailing list