]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: virhostcpu: Extract filtering of the returned data from virHostCPUGetCPUID
authorPeter Krempa <pkrempa@redhat.com>
Mon, 25 Apr 2022 11:20:14 +0000 (13:20 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 25 Apr 2022 14:38:01 +0000 (16:38 +0200)
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@redhat.com>
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/util/virhostcpu.c

index 6be00a5b7662695f1f37e9fb081e91f72146e5ff..f07514a11bedc356116519f7be0be6cb9e37e9c0 100644 (file)
@@ -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);
         }
     }