Can KVM CPU assignment count differ from physical hosts CPU count?

I have read this question. I knew already that I could for example, have a quad core machine with four guests each having two vCPUs. As they don't all be require 100% CPU usage all the time, the scheduler would handle this for me.

My question is about how this relates to a fail-over or migration situation;

If host1 has two dual-core CPUs, and I assign guest1 four vCPUs (so it accessed all four physical cores), what will happen if I try and migrate it to host2 which only has one dual-core CPU?

Can qemu-kvm emulate more vCPUs than there are physical? Or would I have to shut down the virtual machine, change the CPU assignment, migrate it, and then boot it back up (so no live migration)?

Many thanks.


Yes, you can configure and run more virtual CPUs than physical cores available. This is an idle Windows Server 2008 (x86, German) KVM guest showing 10 CPUs on a 4-core single-CPU Intel X3210 machine:

Task manager showing 10 CPUs

But you only should do so when you know, what you are doing:

For one, this will be accompanied by the overhead of timing / synchronizing more virtual CPUs without the benefit of having additional execution units to run the load on. The even more significant overhead is that your host will have to perform context switches more often than necessary whenever the number of threads scheduled for immediate execution is higher than the number of physical CPUs available. The guest OS's thread scheduler would work against the Hypervisor's scheduler leading to a serious performance degradation.

So if you've migrated a VM to a host with a fewer number of pCPUs, you should offline a number of vCPUs to match the number of your available pCPUs. At least with Linux it could be done without restarting by toggling /sys/devices/system/cpu/cpu<X>/online.

Another theoretical approach to reduce the number of CPUs on a running system would involve the use of CPU hot-plugging, but OS support for this feature is rather sparse and KVM would not allow for online reconfiguration of the number of CPUs as well.