public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Elias Huhsovitz <e.huhsovitz@proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Elias Huhsovitz <e.huhsovitz@proxmox.com>
Subject: [PATCH qemu-server v6 4/5] memory: unify NUMA config parsing and resolution
Date: Thu,  3 Dec 2026 11:52:59 +0100	[thread overview]
Message-ID: <20261203105300.70656-5-e.huhsovitz@proxmox.com> (raw)
In-Reply-To: <20261203105300.70656-1-e.huhsovitz@proxmox.com>

Currently NUMA configuration is read and interpreted at each call site.
Validation is scattered across differente paths, leading to inconsistent
rules when enabling NUMA with different additional options, such as
hugepages or memory hotplug.

Introduce a single resolver, `get_effective_numa_cfg()`, that parses all
custom numa[N] entries, auto-completes omitted memory and CPU
assignments, generates a default topology when none is configured, and
validates the result in one place . All three call sites now consume the
resolved node list instead of re-parsing the raw config.

As a side effect, users may now under-specify their NUMA config: omitted
memory values are distributed across the remaining nodes, and a fully
omitted CPU assignment is derived from sockets and cores.

Signed-off-by: Elias Huhsovitz <e.huhsovitz@proxmox.com>
---
 src/PVE/QemuServer/Memory.pm | 444 +++++++++++++++++++----------------
 src/test/run_memory_tests.pl |  31 ++-
 2 files changed, 264 insertions(+), 211 deletions(-)

diff --git a/src/PVE/QemuServer/Memory.pm b/src/PVE/QemuServer/Memory.pm
index 2dbf2dc7..36b2865a 100644
--- a/src/PVE/QemuServer/Memory.pm
+++ b/src/PVE/QemuServer/Memory.pm
@@ -3,11 +3,12 @@ package PVE::QemuServer::Memory;
 use strict;
 use warnings;
 
+use PVE::CpuSet;
 use PVE::JSONSchema qw(parse_property_string);
 use PVE::Tools qw(run_command lock_file lock_file_full file_read_firstline dir_glob_foreach);
 use PVE::Exception qw(raise raise_param_exc);
 
-use PVE::QemuServer::Helpers qw(parse_number_sets);
+use PVE::QemuServer::Helpers;
 use PVE::QemuServer::Monitor qw(mon_cmd);
 use PVE::QemuServer::QMPHelpers qw(qemu_devicedel qemu_objectdel);
 
@@ -15,6 +16,7 @@ use base qw(Exporter);
 
 our @EXPORT_OK = qw(
     get_current_memory
+    get_effective_numa_cfg
 );
 
 our $MAX_NUMA = 8;
@@ -23,8 +25,9 @@ my $numa_fmt = {
     cpus => {
         type => "string",
         pattern => qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/,
-        description => "CPUs accessing this NUMA node.",
+        description => "vCPUs accessing this NUMA node.",
         format_description => "id[-id];...",
+        optional => 1,
     },
     memory => {
         type => "number",
@@ -64,15 +67,6 @@ our $numadesc = {
 };
 PVE::JSONSchema::register_standard_option("pve-qm-numanode", $numadesc);
 
-sub parse_numa {
-    my ($data) = @_;
-
-    my $res = parse_property_string($numa_fmt, $data);
-    $res->{cpus} = parse_number_sets($res->{cpus}) if defined($res->{cpus});
-    $res->{hostnodes} = parse_number_sets($res->{hostnodes}) if defined($res->{hostnodes});
-    return $res;
-}
-
 my $STATICMEM = 1024;
 
 our $memory_fmt = {
@@ -98,9 +92,7 @@ sub parse_memory {
 
     return { current => $memory_fmt->{current}->{default} } if !defined($value);
 
-    my $res = PVE::JSONSchema::parse_property_string($memory_fmt, $value);
-
-    return $res;
+    return PVE::JSONSchema::parse_property_string($memory_fmt, $value);
 }
 
 my sub get_max_mem {
@@ -145,17 +137,196 @@ sub get_current_memory {
     return $memory->{current};
 }
 
-sub get_numa_node_list {
+my sub get_static_memory {
+    my ($conf, $sockets, $hotplug, $memory) = @_;
+
+    return $memory if !$hotplug;
+
+    my $static_memory = $STATICMEM;
+    if ($conf->{hugepages} && $conf->{hugepages} == 1024) {
+        $static_memory *= $sockets;
+    }
+
+    return $static_memory;
+}
+
+# Split $total into $count integer parts that sum to exactly $total.
+# The first ($total % $count) parts get one extra unit.
+my sub distribute_evenly {
+    my ($total, $count) = @_;
+
+    my $base = int($total / $count);
+    my $remainder = $total - $base * $count;
+
+    return map { $base + ($_ < $remainder ? 1 : 0) } (0 .. $count - 1);
+}
+
+my sub parse_numa_node {
+    my ($id, $entry) = @_;
+
+    my $numa = parse_property_string($numa_fmt, $entry);
+
+    my $cpus = defined($numa->{cpus}) ? PVE::CpuSet->from_str($numa->{cpus}, ';') : undef;
+
+    my $hostnodes;
+    if (defined($numa->{hostnodes})) {
+        die "numa$id: policy is required when hostnodes is set\n"
+            if !defined($numa->{policy});
+        $hostnodes = PVE::CpuSet->from_str($numa->{hostnodes}, ';');
+    } elsif (defined($numa->{policy})) {
+        die "numa$id: policy requires hostnodes\n";
+    }
+
+    return {
+        id => $id,
+        cpus => $cpus,
+        memory => $numa->{memory},
+        hostnodes => $hostnodes,
+        policy => $numa->{policy},
+    };
+}
+
+my sub parse_numa_cfg {
     my ($conf) = @_;
-    my @numa_map;
-    for (my $i = 0; $i < $MAX_NUMA; $i++) {
-        my $entry = $conf->{"numa$i"} or next;
-        my $numa = parse_numa($entry) or next;
-        push @numa_map, $i;
+
+    my $nodes = [];
+    for my $id (0 .. $MAX_NUMA - 1) {
+        my $entry = $conf->{"numa$id"} or next;
+        my $node = parse_numa_node($id, $entry);
+        # Custom nodes need an explicit host binding for hugepages. Generated nodes
+        # use the implicit 1:1 mapping and never pass through here.
+        die "numa hostnodes need to be defined to use hugepages\n"
+            if $conf->{hugepages} && !$node->{hostnodes};
+        push @$nodes, $node;
     }
-    return @numa_map if @numa_map;
+
+    return $nodes;
+}
+
+my sub gen_basic_numa_cfg {
+    my ($sockets, $cores, $mem) = @_;
+
+    # Do not use distribute_evenly here, to match legacy behaviour
+    my $mem_per_node = $mem / $sockets;
+    my $nodes = [];
+    for my $id (0 .. $sockets - 1) {
+        my $start = $cores * $id;
+        push @$nodes,
+            {
+                id => $id,
+                cpus => PVE::CpuSet->from_range($start, $start + $cores - 1),
+                memory => $mem_per_node,
+                hostnodes => undef,
+                policy => undef,
+                generated => 1,
+            };
+    }
+
+    return $nodes;
+}
+
+my sub complete_numa_memory {
+    my ($nodes, $mem) = @_;
+
+    my $omitted = [grep { !defined($_->{memory}) } @$nodes];
+    return if !@$omitted;
+
+    my $explicit_sum = 0;
+    for my $node (@$nodes) {
+        $explicit_sum += $node->{memory} if defined($node->{memory});
+    }
+
+    my $remaining = $mem - $explicit_sum;
+    die "cannot auto-complete NUMA memory: remaining budget ($remaining) is not positive\n"
+        if $remaining <= 0;
+
+    my @shares = distribute_evenly($remaining, scalar(@$omitted));
+    for my $i (0 .. $#$omitted) {
+        $omitted->[$i]->{memory} = $shares[$i];
+    }
+}
+
+my sub complete_numa_cpus {
+    my ($nodes, $sockets, $cores) = @_;
+
+    my $omitted = [grep { !defined($_->{cpus}) } @$nodes];
+    return if !@$omitted;
+    die "cannot auto-complete NUMA cpus: mix of explicit and omitted cpus\n"
+        if @$omitted != @$nodes;
+    die "cannot auto-complete NUMA cpus: node count must equal sockets ($sockets)\n"
+        if @$nodes != $sockets;
+
+    for my $i (0 .. $#$nodes) {
+        my $start = $cores * $i;
+        $nodes->[$i]->{cpus} = PVE::CpuSet->from_range($start, $start + $cores - 1);
+    }
+}
+
+my sub validate_numa_cfg {
+    my ($nodes, $sockets, $cores, $mem, $hugepages) = @_;
+
+    my $expected_id = 0;
+    my $max_vcpus = $sockets * $cores;
+    my $mem_sum = 0;
+    my %seen;
+    for my $node (@$nodes) {
+        die "NUMA node IDs must be sequential starting from 0 (missing $expected_id)\n"
+            if $node->{id} != $expected_id;
+        $expected_id++;
+
+        die "numa$node->{id}: memory is not set\n" if !defined($node->{memory});
+        die "numa$node->{id}: memory must be positive\n" if $node->{memory} <= 0;
+        $mem_sum += $node->{memory};
+        die "numa$node->{id}: cpus is not set\n" if !defined($node->{cpus});
+
+        for my $cpu ($node->{cpus}->members_unsorted()) {
+            die "vCPU $cpu is assigned to multiple NUMA nodes\n" if $seen{$cpu}++;
+            die "vCPU $cpu exceeds max vCPUs ($max_vcpus)\n" if $cpu >= $max_vcpus;
+        }
+
+        if (defined($node->{hostnodes})) {
+            die
+                "numa$node->{id}: exactly one host node per NUMA node is required when hugepages are enabled\n"
+                if $hugepages && $node->{hostnodes}->size() != 1;
+            for my $host_node ($node->{hostnodes}->members_unsorted()) {
+                die "host NUMA node $host_node doesn't exist\n"
+                    if !host_numanode_exists($host_node);
+            }
+        } elsif ($hugepages) {
+            # Generated nodes use the implicit 1:1 mapping.
+            die "host NUMA node $node->{id} doesn't exist\n"
+                if !host_numanode_exists($node->{id});
+        }
+    }
+
+    # legacy behaviour allows for fractional memory
+    die "total NUMA memory ($mem_sum) must equal memory budget ($mem)\n"
+        if abs($mem_sum - $mem) > 1e-6;
+}
+
+sub get_effective_numa_cfg {
+    my ($conf, $mem_budget, $auto_complete) = @_;
+    # auto_complete=1 should not hurt any existing guests.
+    # therefore we could set auto_complete=1 and set auto_complete=0
+    # as opt-out in the numa config.
+    return [] if !$conf->{numa};
+
+    my $nodes = parse_numa_cfg($conf);
     my $sockets = $conf->{sockets} || 1;
-    return (0 .. ($sockets - 1));
+    my $cores = $conf->{cores} || 1;
+
+    if ($auto_complete) {
+        if (!@$nodes) {
+            $nodes = gen_basic_numa_cfg($sockets, $cores, $mem_budget);
+        } else {
+            complete_numa_memory($nodes, $mem_budget);
+            complete_numa_cpus($nodes, $sockets, $cores);
+        }
+    }
+
+    validate_numa_cfg($nodes, $sockets, $cores, $mem_budget, $conf->{hugepages});
+
+    return $nodes;
 }
 
 sub host_numanode_exists {
@@ -164,22 +335,18 @@ sub host_numanode_exists {
     return -d "/sys/devices/system/node/node$id/";
 }
 
-# only valid when numa nodes map to a single host node
-sub get_numa_guest_to_host_map {
-    my ($conf) = @_;
-    my $map = {};
-    for (my $i = 0; $i < $MAX_NUMA; $i++) {
-        my $entry = $conf->{"numa$i"} or next;
-        my $numa = parse_numa($entry) or next;
-        $map->{$i} = print_numa_hostnodes($numa->{hostnodes});
-    }
-    return $map if %$map;
-    my $sockets = $conf->{sockets} || 1;
-    return { map { $_ => $_ } (0 .. ($sockets - 1)) };
+# Resolved single host NUMA node for hugepages allocation. Generated nodes without
+# an explicit binding map 1:1 by guest node ID.
+my sub get_hugepages_numa_host_node {
+    my ($node) = @_;
+
+    return $node->{id} if !defined($node->{hostnodes});
+
+    return ($node->{hostnodes}->members())[0];
 }
 
-sub foreach_dimm {
-    my ($conf, $vmid, $memory, $static_memory, $func) = @_;
+my sub foreach_dimm {
+    my ($conf, $vmid, $nodes, $memory, $static_memory, $func) = @_;
 
     my $dimm_id = 0;
     my $current_size = $static_memory;
@@ -193,15 +360,13 @@ sub foreach_dimm {
 
     return if $current_size == $memory;
 
-    my @numa_map = get_numa_node_list($conf);
-
     for (my $j = 0; $j < 8; $j++) {
         for (my $i = 0; $i < 32; $i++) {
             my $name = "dimm${dimm_id}";
             $dimm_id++;
-            my $numanode = $numa_map[$i % @numa_map];
+            my $node = $nodes->[$i % @$nodes];
             $current_size += $dimm_size;
-            &$func($conf, $vmid, $name, $dimm_size, $numanode, $current_size, $memory);
+            $func->($conf, $vmid, $name, $dimm_size, $node, $current_size, $memory);
             return $current_size if $current_size >= $memory;
         }
         $dimm_size *= 2;
@@ -222,34 +387,30 @@ sub qemu_memory_hotplug {
     $value = $newmem->{current};
 
     my $sockets = $conf->{sockets} || 1;
-    my $static_memory = $STATICMEM;
-    $static_memory = $static_memory * $sockets
-        if ($conf->{hugepages} && $conf->{hugepages} == 1024);
+    my $static_memory = get_static_memory($conf, $sockets, 1, $memory);
 
     die "memory can't be lower than $static_memory MB" if $value < $static_memory;
     my $MAX_MEM = get_max_mem($conf);
     die "you cannot add more memory than max mem $MAX_MEM MB!\n" if $value > $MAX_MEM;
 
     if ($value > $memory) {
-
-        my $numa_hostmap;
+        my $nodes = get_effective_numa_cfg($conf, $static_memory, 1);
 
         foreach_dimm(
             $conf,
             $vmid,
+            $nodes,
             $value,
             $static_memory,
             sub {
-                my ($conf, $vmid, $name, $dimm_size, $numanode, $current_size, $memory) = @_;
+                my ($conf, $vmid, $name, $dimm_size, $node, $current_size, $memory) = @_;
 
                 return if $current_size <= get_current_memory($conf->{memory});
 
                 if ($conf->{hugepages}) {
-                    $numa_hostmap = get_numa_guest_to_host_map($conf) if !$numa_hostmap;
-
                     my $hugepages_size = hugepages_size($conf, $dimm_size);
                     my $path = hugepages_mount_path($hugepages_size);
-                    my $host_numanode = $numa_hostmap->{$numanode};
+                    my $host_numanode = get_hugepages_numa_host_node($node);
                     my $hugepages_topology->{$hugepages_size}->{$host_numanode} =
                         hugepages_nr($dimm_size, $hugepages_size);
 
@@ -299,7 +460,7 @@ sub qemu_memory_hotplug {
                         driver => "pc-dimm",
                         id => "$name",
                         memdev => "mem-$name",
-                        node => $numanode,
+                        node => $node->{id},
                     );
                 };
                 if (my $err = $@) {
@@ -368,113 +529,44 @@ sub config {
     my ($conf, $vmid, $sockets, $cores, $hotplug, $virtiofs_enabled, $cmd, $machine_flags) = @_;
 
     my $memory = get_current_memory($conf->{memory});
-    my $static_memory = 0;
+    my $static_memory = get_static_memory($conf, $sockets, $hotplug, $memory);
 
     if ($hotplug) {
         die "NUMA needs to be enabled for memory hotplug\n" if !$conf->{numa};
         my $MAX_MEM = get_max_mem($conf);
         die "Total memory is bigger than ${MAX_MEM}MB\n" if $memory > $MAX_MEM;
 
-        for (my $i = 0; $i < $MAX_NUMA; $i++) {
-            die "cannot enable memory hotplugging with custom NUMA topology\n"
-                if $conf->{"numa$i"};
-        }
-
-        my $sockets = $conf->{sockets} || 1;
-
-        $static_memory = $STATICMEM;
-        $static_memory = $static_memory * $sockets
-            if ($conf->{hugepages} && $conf->{hugepages} == 1024);
+        die "cannot enable memory hotplugging with custom NUMA topology\n" if $conf->{numa0};
 
-        die "minimum memory must be ${static_memory}MB\n" if ($memory < $static_memory);
+        die "minimum memory must be ${static_memory}MB\n" if $memory < $static_memory;
         push @$cmd, '-m', "size=${static_memory},slots=255,maxmem=${MAX_MEM}M";
-
     } else {
-
-        $static_memory = $memory;
         push @$cmd, '-m', $static_memory;
     }
 
     die "numa needs to be enabled to use hugepages" if $conf->{hugepages} && !$conf->{numa};
-
     die "Memory hotplug does not work in combination with virtio-fs.\n"
         if $hotplug && $virtiofs_enabled;
 
-    if ($conf->{numa}) {
-
-        my $numa_totalmemory = undef;
-        for (my $i = 0; $i < $MAX_NUMA; $i++) {
-            next if !$conf->{"numa$i"};
-            my $numa = parse_numa($conf->{"numa$i"});
-            next if !$numa;
-            # memory
-            die "missing NUMA node$i memory value\n" if !$numa->{memory};
-            my $numa_memory = $numa->{memory};
-            $numa_totalmemory += $numa_memory;
-
-            my $memdev = $virtiofs_enabled ? "virtiofs-mem$i" : "ram-node$i";
-            my $mem_object = print_mem_object($conf, $memdev, $numa_memory);
-
-            # cpus
-            my $cpulists = $numa->{cpus};
-            die "missing NUMA node$i cpus\n" if !defined($cpulists);
-            my $cpus = join(
-                ',cpus=',
-                map {
-                    my ($start, $end) = @$_;
-                    defined($end) ? "$start-$end" : $start
-                } @$cpulists,
-            );
-
-            # hostnodes
-            my $hostnodelists = $numa->{hostnodes};
-            if (defined($hostnodelists)) {
-
-                my $hostnodes = print_numa_hostnodes($hostnodelists);
-
-                # policy - note that the value 'default' is not exposed, because it can't be used in
-                # combination with host-nodes:
-                # > kvm: host-nodes must be empty for policy default, or you should explicitly
-                # > specify a policy other than default
-                my $policy = $numa->{policy};
-                die "you need to define a policy for hostnode $hostnodes\n" if !$policy;
-
-                # QEMU splits object options at commas, so each range needs its own host-nodes key
-                my $host_node_ranges = join(
-                    ',host-nodes=',
-                    map {
-                        my ($start, $end) = @$_;
-                        defined($end) ? "$start-$end" : $start
-                    } @$hostnodelists,
-                );
-                $mem_object .= ",host-nodes=$host_node_ranges,policy=$policy";
-            } else {
-                die "numa hostnodes need to be defined to use hugepages" if $conf->{hugepages};
-            }
-
-            push @$cmd, '-object', $mem_object;
-            push @$cmd, '-numa', "node,nodeid=$i,cpus=$cpus,memdev=$memdev";
-        }
+    my $nodes = get_effective_numa_cfg($conf, $static_memory, 1);
 
-        die "total memory for NUMA nodes must be equal to vm static memory\n"
-            if $numa_totalmemory && $numa_totalmemory != $static_memory;
+    if (@$nodes) {
+        for my $node (@$nodes) {
+            my $node_id = $node->{id};
+            my $memdev = $virtiofs_enabled ? "virtiofs-mem$node_id" : "ram-node$node_id";
+            my $mem_object = print_mem_object($conf, $memdev, $node->{memory});
 
-        #if no custom tology, we split memory and cores across numa nodes
-        if (!$numa_totalmemory) {
-            my $numa_memory = ($static_memory / $sockets);
-
-            for (my $i = 0; $i < $sockets; $i++) {
-                die "host NUMA node$i doesn't exist\n"
-                    if !host_numanode_exists($i) && $conf->{hugepages};
+            if (defined($node->{hostnodes})) {
+                for my $range (split(',', $node->{hostnodes}->short_string())) {
+                    $mem_object .= ",host-nodes=$range";
+                }
+                $mem_object .= ",policy=$node->{policy}";
+            }
 
-                my $cpus = ($cores * $i);
-                $cpus .= "-" . ($cpus + $cores - 1) if $cores > 1;
+            my $cpus = join(',cpus=', split(',', $node->{cpus}->short_string()));
 
-                my $memdev = $virtiofs_enabled ? "virtiofs-mem$i" : "ram-node$i";
-                my $mem_object = print_mem_object($conf, $memdev, $numa_memory);
-                push @$cmd, '-object', $mem_object;
-                push @$cmd, '-numa', "node,nodeid=$i,cpus=$cpus,memdev=$memdev";
-            }
+            push @$cmd, '-object', $mem_object;
+            push @$cmd, '-numa', "node,nodeid=$node_id,cpus=$cpus,memdev=$memdev";
         }
     } elsif ($virtiofs_enabled) {
         # kvm: '-machine memory-backend' and '-numa memdev' properties are mutually exclusive
@@ -486,15 +578,16 @@ sub config {
         foreach_dimm(
             $conf,
             $vmid,
+            $nodes,
             $memory,
             $static_memory,
             sub {
-                my ($conf, $vmid, $name, $dimm_size, $numanode, $current_size, $memory) = @_;
+                my ($conf, $vmid, $name, $dimm_size, $node, $current_size, $memory) = @_;
 
                 my $mem_object = print_mem_object($conf, "mem-$name", $dimm_size);
 
                 push @$cmd, "-object", $mem_object;
-                push @$cmd, "-device", "pc-dimm,id=$name,memdev=mem-$name,node=$numanode";
+                push @$cmd, "-device", "pc-dimm,id=$name,memdev=mem-$name,node=$node->{id}";
 
                 die "memory size ($memory) must be aligned to $dimm_size for hotplugging\n"
                     if $current_size > $memory;
@@ -520,23 +613,6 @@ sub print_mem_object {
 
 }
 
-sub print_numa_hostnodes {
-    my ($hostnodelists) = @_;
-
-    my $hostnodes;
-    foreach my $hostnoderange (@$hostnodelists) {
-        my ($start, $end) = @$hostnoderange;
-        $hostnodes .= ',' if defined($hostnodes);
-        $hostnodes .= $start;
-        $hostnodes .= "-$end" if defined($end);
-        $end //= $start;
-        for (my $i = $start; $i <= $end; ++$i) {
-            die "host NUMA node$i doesn't exist\n" if !host_numanode_exists($i);
-        }
-    }
-    return $hostnodes;
-}
-
 sub hugepages_mount {
 
     my $mountdata = PVE::ProcFSTools::parse_proc_mounts();
@@ -619,70 +695,37 @@ sub hugepages_size {
 sub hugepages_topology {
     my ($conf, $hotplug) = @_;
 
-    my $hugepages_topology = {};
-
     return if !$conf->{numa};
 
     my $memory = get_current_memory($conf->{memory});
-    my $static_memory = 0;
     my $sockets = $conf->{sockets} || 1;
-    my $numa_custom_topology = undef;
-
-    if ($hotplug) {
-        $static_memory = $STATICMEM;
-        $static_memory = $static_memory * $sockets
-            if ($conf->{hugepages} && $conf->{hugepages} == 1024);
-    } else {
-        $static_memory = $memory;
-    }
-
-    #custom numa topology
-    for (my $i = 0; $i < $MAX_NUMA; $i++) {
-        next if !$conf->{"numa$i"};
-        my $numa = parse_numa($conf->{"numa$i"});
-        next if !$numa;
+    my $static_memory = get_static_memory($conf, $sockets, $hotplug, $memory);
 
-        $numa_custom_topology = 1;
-        my $numa_memory = $numa->{memory};
-        my $hostnodelists = $numa->{hostnodes};
-        my $hostnodes = print_numa_hostnodes($hostnodelists);
+    my $nodes = get_effective_numa_cfg($conf, $static_memory, 1);
 
-        die "more than 1 hostnode value in numa node is not supported when hugepages are enabled"
-            if $hostnodes !~ m/^\d+$/;
+    my $hugepages_topology = {};
+    for my $node (@$nodes) {
+        my $numa_memory = $node->{memory};
         my $hugepages_size = hugepages_size($conf, $numa_memory);
-        $hugepages_topology->{$hugepages_size}->{$hostnodes} +=
+        my $host_node = get_hugepages_numa_host_node($node);
+        $hugepages_topology->{$hugepages_size}->{$host_node} +=
             hugepages_nr($numa_memory, $hugepages_size);
-
-    }
-
-    #if no custom numa tology, we split memory and cores across numa nodes
-    if (!$numa_custom_topology) {
-
-        my $numa_memory = ($static_memory / $sockets);
-
-        for (my $i = 0; $i < $sockets; $i++) {
-
-            my $hugepages_size = hugepages_size($conf, $numa_memory);
-            $hugepages_topology->{$hugepages_size}->{$i} +=
-                hugepages_nr($numa_memory, $hugepages_size);
-        }
     }
 
     if ($hotplug) {
-        my $numa_hostmap = get_numa_guest_to_host_map($conf);
-
         foreach_dimm(
             $conf,
             undef,
+            $nodes,
             $memory,
             $static_memory,
             sub {
-                my ($conf, undef, $name, $dimm_size, $numanode, $current_size, $memory) = @_;
+                my ($conf, undef, $name, $dimm_size, $node, $current_size, $memory) = @_;
 
-                $numanode = $numa_hostmap->{$numanode};
+                my $host_node = get_hugepages_numa_host_node($node);
 
                 my $hugepages_size = hugepages_size($conf, $dimm_size);
-                $hugepages_topology->{$hugepages_size}->{$numanode} +=
+                $hugepages_topology->{$hugepages_size}->{$host_node} +=
                     hugepages_nr($dimm_size, $hugepages_size);
             },
         );
@@ -824,4 +867,3 @@ sub hugepages_update_locked {
     return $res;
 }
 1;
-
diff --git a/src/test/run_memory_tests.pl b/src/test/run_memory_tests.pl
index 97b89961..90f6a1bb 100755
--- a/src/test/run_memory_tests.pl
+++ b/src/test/run_memory_tests.pl
@@ -31,19 +31,23 @@ for my $test (@$tests) {
 
     subtest "host nodes $hostnodes" => sub {
         my $numa_config = "cpus=0,memory=1024,hostnodes=$hostnodes,policy=bind";
-        my $numa = PVE::QemuServer::Memory::parse_numa($numa_config);
-        is(
-            PVE::QemuServer::Memory::print_numa_hostnodes($numa->{hostnodes}),
-            $expected,
-            'preserve node and range separators',
-        );
-
-        my $conf = {
+        my $base_conf = {
             numa => 1,
             memory => 1024,
-            hugepages => 2,
+            sockets => 1,
+            cores => 1,
             numa0 => $numa_config,
         };
+
+        # Without hugepages, multi-node bindings are valid; inspect the parsed set.
+        my $nodes = PVE::QemuServer::Memory::get_effective_numa_cfg($base_conf, 1, 1, 1024, 1);
+        is(
+            $nodes->[0]->{hostnodes}->short_string(),
+            $expected,
+            'hostnodes normalize to a canonical CpuSet rendering',
+        );
+
+        my $conf = { %$base_conf, hugepages => 2 };
         my $topology = eval { PVE::QemuServer::Memory::hugepages_topology($conf, 0); };
         if ($single_node) {
             is($@, '', 'accept a single host node for hugepages');
@@ -51,11 +55,18 @@ for my $test (@$tests) {
         } else {
             like(
                 $@,
-                qr/^more than 1 hostnode value in numa node is not supported when hugepages are enabled/,
+                qr/exactly one host node per NUMA node is required when hugepages are enabled/,
                 'reject host node lists and ranges for hugepages',
             );
         }
     };
 }
 
+subtest 'generated topology with hugepages' => sub {
+    my $conf = { numa => 1, memory => 1024, sockets => 1, cores => 1, hugepages => 2 };
+    my $topology = eval { PVE::QemuServer::Memory::hugepages_topology($conf, 0); };
+    is($@, '', 'generated nodes use the implicit 1:1 host mapping');
+    is_deeply($topology, { 2 => { 0 => 512 } }, 'allocate on host node 0');
+};
+
 done_testing();
-- 
2.47.3





  parent reply	other threads:[~2026-12-03 10:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Elias Huhsovitz [this message]
2026-12-03 11:34   ` [PATCH qemu-server v6 4/5] memory: unify NUMA config parsing and resolution Elias Huhsovitz
2026-12-03 10:53 ` [PATCH qemu-server v6 5/5] cpu: add affinity module Elias Huhsovitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203105300.70656-5-e.huhsovitz@proxmox.com \
    --to=e.huhsovitz@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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