public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [PATCH common/qemu-server v6 0/5] vCPU binding for qemu guests
@ 2026-12-03 10:52 Elias Huhsovitz
  2026-12-03 10:52 ` [PATCH common v6 1/5] syscall: add sched_setaffinity Elias Huhsovitz
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Elias Huhsovitz @ 2026-12-03 10:52 UTC (permalink / raw)
  To: pve-devel; +Cc: Elias Huhsovitz

This series adds a convinient way to achive vCPU:CPU binding in PVE.
It is configurable using the the new key `cpu-binding`.

2 functional modes are added:

mode=one-to-one
---------------
Bind each vCPU to its own host CPU, chosen from the pool configured by
affinity. The configured affinity string acts as an upper bound on the
host CPUs that can be used for binding. If the guest has N vCPUs and
the affinity pool has M host CPUs, each vCPU is fixed to a single one
of the M host CPUs (M >= N must be true). Each vCPU is fixed to a
single host CPU, it does not float within the pool.

An empty affinity string means all host CPUs will be considered. On a
host with 16 cores, that pins an N-vCPU VM to host CPUs 0, 1, ..., N-1.

Examples:

Example 1: 4-vCPU guest with affinity=4-9:
The resulting mapping would be:
vCPU0:CPU4
vCPU1:CPU5
vCPU2:CPU6
vCPU3:CPU7

Example 2: 4-vCPU guest with affinity=0,2:
The resulting mapping would be:
vCPU0:CPU0
vCPU1:CPU2

mode=numa
---------------
Bind the set of vCPUs of each vNUMA node to the set of CPUs of a host
NUMA node. The CPU bindings are determined by the numa[N] configuration
of the guest. If no numa[N] config is explicitly provided, then for each
socket a numa[N] node is created. Each guest vNUMA node's vCPUs form a
group that may run on any CPU of the assigned host NUMA node. The host
scheduler is free to migrate the vCPUs within the host node. This is
the intended trade-off: memory allocated by a vCPU stays local to the
host node it runs on (preserving NUMA memory locality), while the
scheduler still has room to balance load across the node's cores and
hyperthreads.

Examples:

numa0: cpus=0-1,hostnodes=0,memory=2048,policy=bind
Allows vCPUs 0-1 to execute on all CPUs of host NUMA node 0

numa1: cpus=6-7,hostnodes=3,memory=2048,policy=bind
Allows vCPUs 6-7 to execute on all CPUs of host NUMA node 3

numa:1, no numa[N], sockets: 2, cores: 2, total vCPUs: 4
Allows vCPUs 0-1 to execute on all CPUs of host NUMA node 0
Allows vCPUs 2-3 to execute on all CPUs of host NUMA node 1

Reproducibility
------------------
To make vCPU placement reproduceable, the config is deterministically
generated using the 3 configuration keys:
* affinity
* cpu-binding
* numa[N] config keys

Two VMs with the same config on the same host
should get the same placement. (unless there is some randomness that I
am unaware of)

Basic usage example
--------------
# One vCPU executes on exactly one host cpu (under affinity mask)
qm set <vmid> --cpu-binding mode=one-to-one
qm set <vmid> --affinity 4-7

# NUMA binding (explicit):
#Guest NUMA 0 -> host NUMA 2, guest NUMA 1 -> host NUMA 3
qm set <vmid> --numa 1
qm set <vmid> --cpu-binding mode=numa
qm set <vmid> --numa0 cpus=0-1,hostnodes=0,memory=1024,policy=bind
qm set <vmid> --numa1 cpus=2-3,hostnodes=1,memory=1024,policy=bind

I plan on adding implicit NUMA binding in the future, e.g.,
# NUMA binding (implicit):
# for each socket: one numa node is created and bound
# e.g., 2 sockets: numa0:host-numa0, numa1:host-numa:1
qm set <vmid> --numa 1

So that a user can simply enable numa bindign mode and automatically
have a sensible config utilizing the numa capeability of his hardware.

But for this I would like to refactor the Memory.pm first to unify the
how the numa[N] config is created and used. This will be provided in
a subsequent patch, or the next version of this series(if a need one is
needed).

Validation runs at VM start. Binding itself runs after QEMU spawns the
vCPU threads (The VM is frozen at start, until the affinity of the
vCPUs has been set)

What is not in this PATCH
----------------------------
* memory_binding: This is best handled by qemu directly and already
possible via, e.g.:

numa0: cpus=0-1,hostnodes=1,memory=2048,policy=bind
numa1: cpus=2-3,hostnodes=2,memory=2048,policy=bind
numa2: cpus=4-5,hostnodes=3,memory=2048,policy=bind
numa3: cpus=6-7,hostnodes=4,memory=2048,policy=bind

What we need is a convinient way to automatically apply the correct
memory bindingings for the numa node. This is will be handled in a
subsequent patch.

* Qemu I/O Thread control/binding

* CPU Isolation

I plan on adding most of the features requested in the forum post [2].

Changes v5->v6
--------------
* Unify numa configuration parsing in Memory.pm. This makes the numa
  pinning option more robust and allows for numa based cpu-binding with
  automatic numa config generation.

Changes v4->v5
--------------
* Refactor code: rust --> perl.
* Drop the toplogy tree data structure. IMO this only makes sense in a
  typed language and might come back in the future if need it in the
  future (e.g. for complex automatic pinning strategies).
* always start qemu using taskset if affinity is configured.
  (i.e. current implementation on master)
* support cpu hotplug.
* Hold off on the "allow-smt-siblings" option for now.
* comment why starting the vm in frozen state is necesarry.
* die when vm is unable to be unfrozen.

Changes v3->v4
-----------------
* Drop the manual modes (manual, numa-manual).
* All modes respect the affnity setting.
* numa mode utilizes the numa[N] config.
* Freeze guest until sched_setaffinity has finished to avoid using
non-specified host cpus during startup sequence.
* Build cpu toplogy from sysfs instead of hwloc [1].

7. Previous versions
---------------------
v5: https://lore.proxmox.com/pve-devel/20261124131858.135785-1-e.huhsovitz@proxmox.com/
v4: https://lore.proxmox.com/pve-devel/20261111152027.260117-1-e.huhsovitz@proxmox.com/
v3: https://lore.proxmox.com/pve-devel/20261022092227.58871-1-e.huhsovitz@proxmox.com/
v2: https://lore.proxmox.com/pve-devel/20260921095404.61552-1-e.huhsovitz@proxmox.com/
v1: https://lore.proxmox.com/pve-devel/20260217114813.2063770-1-d.csapak@proxmox.com/

v1 was originally introduced by Dominik C.

8. References
-------------
[0] https://lore.proxmox.com/pve-devel/4f5ed842-1699-45d6-895b-e5fb664bd195@proxmox.com/
[1] https://hwloc.readthedocs.io/en/hwloc-2.15.0/doxygen/html/index.html
[2] https://forum.proxmox.com/threads/hey-proxmox-community-lets-talk-about-resources-isolation.124256/

Thank you to whoever reviews this! :)

Summary of changes
------------------
pve-common:

Elias Huhsovitz (2):
  syscall: add sched_setaffinity
  cpuset: generalize to improve usability for sysfs cpusets.

 src/PVE/CpuSet.pm  | 76 +++++++++++++++++++++++++++++++++++++---------
 src/PVE/Syscall.pm |  1 +
 2 files changed, 62 insertions(+), 15 deletions(-)


qemu-server:

Elias Huhsovitz (3):
  helpers: return integer values for parse_number_sets
  memory: unify NUMA config parsing and resolution
  cpu: add affinity module

 src/PVE/QemuServer.pm             |  44 ++-
 src/PVE/QemuServer/CPUAffinity.pm | 220 +++++++++++++++
 src/PVE/QemuServer/Helpers.pm     |   6 +-
 src/PVE/QemuServer/Makefile       |   1 +
 src/PVE/QemuServer/Memory.pm      | 444 ++++++++++++++++--------------
 src/test/run_memory_tests.pl      |  31 ++-
 6 files changed, 530 insertions(+), 216 deletions(-)
 create mode 100644 src/PVE/QemuServer/CPUAffinity.pm


Summary over all repositories:
  8 files changed, 592 insertions(+), 231 deletions(-)

-- 
Generated by murpp 0.12.0




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-03 11:34 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 10:52 [PATCH common/qemu-server v6 0/5] vCPU binding for qemu guests Elias Huhsovitz
2026-12-03 10:52 ` [PATCH common v6 1/5] syscall: add sched_setaffinity Elias Huhsovitz
2026-12-03 10:52 ` [PATCH common v6 2/5] cpuset: generalize to improve usability for sysfs cpusets Elias Huhsovitz
2026-12-03 10:52 ` [PATCH qemu-server v6 3/5] helpers: return integer values for parse_number_sets Elias Huhsovitz
2026-12-03 10:52 ` [PATCH qemu-server v6 4/5] memory: unify NUMA config parsing and resolution Elias Huhsovitz
2026-12-03 11:34   ` Elias Huhsovitz
2026-12-03 10:53 ` [PATCH qemu-server v6 5/5] cpu: add affinity module Elias Huhsovitz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal