*word &= boot_cpu_data.x86_capability[wordnum];
 }
 
-static void do_host_cpuid(struct kvm_cpuid_entry2 *entry, u32 function,
-                          u32 index)
+static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_entry2 *entry,
+                                             int *nent, int maxnent,
+                                             u32 function, u32 index)
 {
+       if (*nent >= maxnent)
+               return NULL;
+       ++*nent;
+
        entry->function = function;
        entry->index = index;
        entry->flags = 0;
                entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
                break;
        }
+
+       return entry;
 }
 
 static int __do_cpuid_func_emulated(struct kvm_cpuid_entry2 *entry,
 
        r = -E2BIG;
 
-       if (WARN_ON(*nent >= maxnent))
+       if (WARN_ON(!do_host_cpuid(entry, nent, maxnent, function, 0)))
                goto out;
 
-       do_host_cpuid(entry, function, 0);
-       ++*nent;
-
        switch (function) {
        case 0:
                /* Limited to the highest leaf implemented in KVM. */
 
                entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
                for (t = 1; t < times; ++t) {
-                       if (*nent >= maxnent)
+                       if (!do_host_cpuid(&entry[t], nent, maxnent, function, 0))
                                goto out;
-
-                       do_host_cpuid(&entry[t], function, 0);
-                       ++*nent;
                }
                break;
        }
                        if (!cache_type)
                                break;
 
-                       if (*nent >= maxnent)
+                       if (!do_host_cpuid(&entry[i], nent, maxnent, function, i))
                                goto out;
-                       do_host_cpuid(&entry[i], function, i);
-                       ++*nent;
                }
                break;
        }
                do_cpuid_7_mask(entry);
 
                for (i = 1; i <= entry->eax; i++) {
-                       if (*nent >= maxnent)
+                       if (!do_host_cpuid(&entry[i], nent, maxnent, function, i))
                                goto out;
 
-                       do_host_cpuid(&entry[i], function, i);
-                       ++*nent;
-
                        do_cpuid_7_mask(&entry[i]);
                }
                break;
                 * added entry is zero.
                 */
                for (i = 1; entry[i - 1].ecx & 0xff00; ++i) {
-                       if (*nent >= maxnent)
+                       if (!do_host_cpuid(&entry[i], nent, maxnent, function, i))
                                goto out;
-
-                       do_host_cpuid(&entry[i], function, i);
-                       ++*nent;
                }
                break;
        }
                if (!supported)
                        break;
 
-               if (*nent >= maxnent)
+               if (!do_host_cpuid(&entry[1], nent, maxnent, function, 1))
                        goto out;
 
-               do_host_cpuid(&entry[1], function, 1);
-               ++*nent;
-
                entry[1].eax &= kvm_cpuid_D_1_eax_x86_features;
                cpuid_mask(&entry[1].eax, CPUID_D_1_EAX);
                if (entry[1].eax & (F(XSAVES)|F(XSAVEC)))
                        if (!(supported & BIT_ULL(idx)))
                                continue;
 
-                       if (*nent >= maxnent)
+                       if (!do_host_cpuid(&entry[i], nent, maxnent, function, idx))
                                goto out;
 
-                       do_host_cpuid(&entry[i], function, idx);
-                       ++*nent;
-
                        /*
                         * The @supported check above should have filtered out
                         * invalid sub-leafs as well as sub-leafs managed by
                        break;
 
                for (t = 1; t <= times; ++t) {
-                       if (*nent >= maxnent)
+                       if (!do_host_cpuid(&entry[t], nent, maxnent, function, t))
                                goto out;
-                       do_host_cpuid(&entry[t], function, t);
-                       ++*nent;
                }
                break;
        }