Skip to content

CPU pinning and NUMA alignment for KVM guests — when, why, and how to wire it

How to pin a KVM guest's vCPUs to specific host pCPUs and align its memory to a single NUMA node — the cases where it doubles performance, and the ones where it does nothing.

A 32-core, 256 GB host with two NUMA nodes (16 cores + 128 GB each) runs a PostgreSQL VM that spans both sockets — 24 vCPUs scheduled across all 32 host cores, 96 GiB of guest memory backed by pages allocated from whichever NUMA node had room when the VM booted. p99 query latency is 2.3x what the same workload showed on bare metal. The host has plenty of spare capacity. The problem is not contention; it is that every memory access from the VM might cross the QPI link, and every CPU steal-event bounces threads between sockets. The fix is CPU pinning plus NUMA alignment. This article is when to apply it, when it does nothing, and how to wire it cleanly.

How to verify

Identify the host’s NUMA topology and confirm the guest’s current placement:

# Host topology
numactl --hardware
# available: 2 nodes (0-1)
# node 0 cpus: 0 1 2 3 4 5 6 7 16 17 18 19 20 21 22 23
# node 0 size: 128841 MB
# node 1 cpus: 8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31
# node 1 size: 128974 MB
# node distances: 10 (local) 21 (remote)

# Where are the guest's vCPUs running right now?
virsh vcpuinfo db-prod
# VCPU:     0
# CPU:      14
# State:    running
# CPU time: 9123.4s
# CPU Affinity: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy  (all 32 host CPUs)

# Where are the guest's memory pages?
virsh numatune db-prod
# numa_mode  : strict
# numa_nodeset: -

If CPU Affinity shows y across all host CPUs and numa_nodeset is blank, the guest is free-floating — the scheduler may park vCPUs on whichever pCPU is idle and memory is allocated wherever it lands. For a latency-sensitive guest, that is the default and it is the worst case.

What’s happening

A modern multi-socket server is not a single uniform memory pool. Each CPU socket has its own memory controller and its own slice of “local” RAM. Accessing RAM attached to the other socket goes through the inter-socket interconnect (QPI on Intel, Infinity Fabric on AMD), and that’s slower — typically 1.5x to 2x the latency of local access — and contended with every other cross-socket transaction.

When a VM’s vCPUs and memory are not aligned to a single NUMA node:

  • A vCPU running on a node-0 core may be touching memory the host allocated on node 1. Every cache miss is a remote DRAM read.
  • The Linux scheduler can migrate a vCPU between sockets to balance load. Each migration cold-starts the L1/L2/L3 caches.
  • Memory accessed by multiple vCPUs (a database buffer pool) is “split” across nodes — some hits are local, some are remote, and the variance shows up as p99 jitter.

The fix has three parts. CPU pinning maps each vCPU to a specific pCPU (or a small set on the same NUMA node) so the scheduler stops migrating it. Memory pinning with numatune tells the host to allocate the guest’s memory from a specific NUMA node. Topology exposure through <numa> and <cputune> in the domain XML tells the guest kernel about its NUMA layout so guest applications can also optimize their own thread/memory placement.

CPU pinning is not free. The vCPU loses scheduler flexibility — if another tenant needs that pCPU, there is contention. For a quiet host with one or two latency-sensitive guests, pinning is a clear win. For a noisy host where many guests share pCPUs, pinning is the wrong tool and you should use cpuset quotas instead.

The procedure

  1. Decide which guests get pinned. Candidates: databases, low-latency message brokers, anything where p99 matters and the workload regularly saturates 2+ vCPUs. Not candidates: web frontends, CI runners, anything that is bursty and idle most of the time.

  2. Reserve host CPUs for the host itself. Leave at least cores 0-1 (and their hyperthread siblings) for the host — kernel threads, the qemu I/O thread, the network stack. Do not pin a guest to a core the host also uses, or you fight the host’s own work.

  3. Pin vCPUs to pCPUs on a single NUMA node. For a guest that wants 8 vCPUs aligned to node 0:

    <vcpu placement='static'>8</vcpu>
    <cputune>
      <vcpupin vcpu='0' cpuset='2'/>
      <vcpupin vcpu='1' cpuset='3'/>
      <vcpupin vcpu='2' cpuset='4'/>
      <vcpupin vcpu='3' cpuset='5'/>
      <vcpupin vcpu='4' cpuset='6'/>
      <vcpupin vcpu='5' cpuset='7'/>
      <vcpupin vcpu='6' cpuset='18'/>   <!-- hyperthread sibling of 2 -->
      <vcpupin vcpu='7' cpuset='19'/>
      <emulatorpin cpuset='2-7,18-19'/>
    </cputune>

    Note that pCPUs 2-7 and their hyperthread siblings 18-23 are all on node 0 in our example topology. Pinning vcpu 0 to pcpu 2 and vcpu 6 to pcpu 18 keeps the hyperthread pair on the same NUMA node and lets the guest see a coherent topology.

  4. Pin memory to the same NUMA node.

    <numatune>
      <memory mode='strict' nodeset='0'/>
    </numatune>

    mode='strict' fails the allocation if node 0 doesn’t have room — better to fail at boot than silently spill to node 1. Use mode='preferred' only if the workload tolerates occasional spillover.

  5. Expose the NUMA topology to the guest. A guest that knows it’s a single-NUMA-node VM lets its kernel and applications make smart decisions:

    <cpu mode='host-passthrough'>
      <topology sockets='1' cores='4' threads='2'/>
      <numa>
        <cell id='0' cpus='0-7' memory='8388608' unit='KiB'/>
      </numa>
    </cpu>

    mode='host-passthrough' exposes the actual host CPU model — important because guest applications that detect AVX-512, AES-NI, or specific microarchitecture features can use them. Without host-passthrough, the guest sees the generic qemu64 CPU model and skips those code paths.

  6. Apply and verify. virsh define the new XML, restart the guest. Then:

    virsh vcpuinfo db-prod
    # CPU Affinity: --yyyyyy-----------yyyyyyy------  (only node-0 cores)
    
    virsh numatune db-prod
    # numa_mode  : strict
    # numa_nodeset: 0
    
    # Inside the guest
    numactl --hardware
    # available: 1 node (0)
    # node 0 cpus: 0 1 2 3 4 5 6 7
    # node 0 size: 8192 MB
  7. Measure. Run the workload that hurt before. p99 should drop, often by 30-50%. CPU steal inside the guest should drop to near-zero. If neither moves, the guest wasn’t NUMA-bound — undo the pinning and look elsewhere (storage I/O, network, garbage collection in the application).

Common pitfalls

  1. Pinning a guest larger than a single NUMA node. A guest with 24 vCPUs on a host with 16-core nodes does not fit in one node. You have two options: split the guest across both nodes (using two <cell> entries and matching numatune nodesets) and accept the cross-socket cost, or shrink the guest. There is no magic option where 24 vCPUs fit in a 16-core node.

  2. Pinning without leaving CPUs for the host. Pin a guest to cores 0-15 on a 16-core node and the host kernel ends up time-sharing the cores with the qemu emulator thread, which negates the win. Always leave 1-2 cores per node for the host.

  3. mode='strict' on a host without enough free node-local memory. The guest fails to start. Boot the guest first, check numactl --hardware for free pages per node, then commit to strict. If memory is tight, use mode='preferred' and accept some spillover.

  4. CPU hot-plug breaks pinning. Adding a vCPU live to a pinned guest does not auto-pin the new vCPU; it lands on whichever pCPU the scheduler picks. Live-add a vCPU only if you vcpupin it immediately after.

  5. Forgetting hyperthread siblings. Pinning vcpu 0 to pcpu 2 without also pinning the SMT-sibling thread (pcpu 18 on Intel typically) means the guest’s vCPU 0 and an unrelated host task share the same physical core. For latency-sensitive guests, pin entire sibling pairs — or disable SMT in the BIOS for hosts that run a small number of pinned guests.

Operational notes

Pinning + NUMA alignment is most effective on hosts with 2+ sockets and guests whose working set is large enough that cache misses matter. On a single-socket host, NUMA alignment is a no-op (one node). On a tiny guest that fits in L3 cache, pinning matters less than CPU model selection.

We operate KVM estates with per-guest topology profiles — pinned + NUMA-aligned for databases and brokers, free-floating with cpuset quotas for everything else — and the placement rationale is documented per guest. See Managed Cloud Operations.