xref: /openbmc/qemu/hw/core/numa.c (revision 59a3a1c0)
1 /*
2  * NUMA parameter parsing routines
3  *
4  * Copyright (c) 2014 Fujitsu Ltd.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "sysemu/hostmem.h"
27 #include "sysemu/numa.h"
28 #include "sysemu/sysemu.h"
29 #include "exec/cpu-common.h"
30 #include "exec/ramlist.h"
31 #include "qemu/bitmap.h"
32 #include "qemu/error-report.h"
33 #include "qapi/error.h"
34 #include "qapi/opts-visitor.h"
35 #include "qapi/qapi-visit-machine.h"
36 #include "sysemu/qtest.h"
37 #include "qom/cpu.h"
38 #include "hw/mem/pc-dimm.h"
39 #include "migration/vmstate.h"
40 #include "hw/boards.h"
41 #include "hw/mem/memory-device.h"
42 #include "qemu/option.h"
43 #include "qemu/config-file.h"
44 #include "qemu/cutils.h"
45 
46 QemuOptsList qemu_numa_opts = {
47     .name = "numa",
48     .implied_opt_name = "type",
49     .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
50     .desc = { { 0 } } /* validated with OptsVisitor */
51 };
52 
53 static int have_memdevs;
54 static int have_mem;
55 static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
56                              * For all nodes, nodeid < max_numa_nodeid
57                              */
58 int nb_numa_nodes;
59 bool have_numa_distance;
60 NodeInfo numa_info[MAX_NODES];
61 
62 
63 static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
64                             Error **errp)
65 {
66     Error *err = NULL;
67     uint16_t nodenr;
68     uint16List *cpus = NULL;
69     MachineClass *mc = MACHINE_GET_CLASS(ms);
70     unsigned int max_cpus = ms->smp.max_cpus;
71 
72     if (node->has_nodeid) {
73         nodenr = node->nodeid;
74     } else {
75         nodenr = nb_numa_nodes;
76     }
77 
78     if (nodenr >= MAX_NODES) {
79         error_setg(errp, "Max number of NUMA nodes reached: %"
80                    PRIu16 "", nodenr);
81         return;
82     }
83 
84     if (numa_info[nodenr].present) {
85         error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
86         return;
87     }
88 
89     if (!mc->cpu_index_to_instance_props || !mc->get_default_cpu_node_id) {
90         error_setg(errp, "NUMA is not supported by this machine-type");
91         return;
92     }
93     for (cpus = node->cpus; cpus; cpus = cpus->next) {
94         CpuInstanceProperties props;
95         if (cpus->value >= max_cpus) {
96             error_setg(errp,
97                        "CPU index (%" PRIu16 ")"
98                        " should be smaller than maxcpus (%d)",
99                        cpus->value, max_cpus);
100             return;
101         }
102         props = mc->cpu_index_to_instance_props(ms, cpus->value);
103         props.node_id = nodenr;
104         props.has_node_id = true;
105         machine_set_cpu_numa_node(ms, &props, &err);
106         if (err) {
107             error_propagate(errp, err);
108             return;
109         }
110     }
111 
112     have_memdevs = have_memdevs ? : node->has_memdev;
113     have_mem = have_mem ? : node->has_mem;
114     if ((node->has_mem && have_memdevs) || (node->has_memdev && have_mem)) {
115         error_setg(errp, "numa configuration should use either mem= or memdev=,"
116                    "mixing both is not allowed");
117         return;
118     }
119 
120     if (node->has_mem) {
121         numa_info[nodenr].node_mem = node->mem;
122         if (!qtest_enabled()) {
123             warn_report("Parameter -numa node,mem is deprecated,"
124                         " use -numa node,memdev instead");
125         }
126     }
127     if (node->has_memdev) {
128         Object *o;
129         o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
130         if (!o) {
131             error_setg(errp, "memdev=%s is ambiguous", node->memdev);
132             return;
133         }
134 
135         object_ref(o);
136         numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL);
137         numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
138     }
139     numa_info[nodenr].present = true;
140     max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
141     nb_numa_nodes++;
142 }
143 
144 static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
145 {
146     uint16_t src = dist->src;
147     uint16_t dst = dist->dst;
148     uint8_t val = dist->val;
149 
150     if (src >= MAX_NODES || dst >= MAX_NODES) {
151         error_setg(errp, "Parameter '%s' expects an integer between 0 and %d",
152                    src >= MAX_NODES ? "src" : "dst", MAX_NODES - 1);
153         return;
154     }
155 
156     if (!numa_info[src].present || !numa_info[dst].present) {
157         error_setg(errp, "Source/Destination NUMA node is missing. "
158                    "Please use '-numa node' option to declare it first.");
159         return;
160     }
161 
162     if (val < NUMA_DISTANCE_MIN) {
163         error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
164                    "it shouldn't be less than %d.",
165                    val, NUMA_DISTANCE_MIN);
166         return;
167     }
168 
169     if (src == dst && val != NUMA_DISTANCE_MIN) {
170         error_setg(errp, "Local distance of node %d should be %d.",
171                    src, NUMA_DISTANCE_MIN);
172         return;
173     }
174 
175     numa_info[src].distance[dst] = val;
176     have_numa_distance = true;
177 }
178 
179 void set_numa_options(MachineState *ms, NumaOptions *object, Error **errp)
180 {
181     Error *err = NULL;
182 
183     switch (object->type) {
184     case NUMA_OPTIONS_TYPE_NODE:
185         parse_numa_node(ms, &object->u.node, &err);
186         if (err) {
187             goto end;
188         }
189         break;
190     case NUMA_OPTIONS_TYPE_DIST:
191         parse_numa_distance(&object->u.dist, &err);
192         if (err) {
193             goto end;
194         }
195         break;
196     case NUMA_OPTIONS_TYPE_CPU:
197         if (!object->u.cpu.has_node_id) {
198             error_setg(&err, "Missing mandatory node-id property");
199             goto end;
200         }
201         if (!numa_info[object->u.cpu.node_id].present) {
202             error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be "
203                 "defined with -numa node,nodeid=ID before it's used with "
204                 "-numa cpu,node-id=ID", object->u.cpu.node_id);
205             goto end;
206         }
207 
208         machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu),
209                                   &err);
210         break;
211     default:
212         abort();
213     }
214 
215 end:
216     error_propagate(errp, err);
217 }
218 
219 static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
220 {
221     NumaOptions *object = NULL;
222     MachineState *ms = MACHINE(opaque);
223     Error *err = NULL;
224     Visitor *v = opts_visitor_new(opts);
225 
226     visit_type_NumaOptions(v, NULL, &object, &err);
227     visit_free(v);
228     if (err) {
229         goto end;
230     }
231 
232     /* Fix up legacy suffix-less format */
233     if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
234         const char *mem_str = qemu_opt_get(opts, "mem");
235         qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
236     }
237 
238     set_numa_options(ms, object, &err);
239 
240 end:
241     qapi_free_NumaOptions(object);
242     if (err) {
243         error_propagate(errp, err);
244         return -1;
245     }
246 
247     return 0;
248 }
249 
250 /* If all node pair distances are symmetric, then only distances
251  * in one direction are enough. If there is even one asymmetric
252  * pair, though, then all distances must be provided. The
253  * distance from a node to itself is always NUMA_DISTANCE_MIN,
254  * so providing it is never necessary.
255  */
256 static void validate_numa_distance(void)
257 {
258     int src, dst;
259     bool is_asymmetrical = false;
260 
261     for (src = 0; src < nb_numa_nodes; src++) {
262         for (dst = src; dst < nb_numa_nodes; dst++) {
263             if (numa_info[src].distance[dst] == 0 &&
264                 numa_info[dst].distance[src] == 0) {
265                 if (src != dst) {
266                     error_report("The distance between node %d and %d is "
267                                  "missing, at least one distance value "
268                                  "between each nodes should be provided.",
269                                  src, dst);
270                     exit(EXIT_FAILURE);
271                 }
272             }
273 
274             if (numa_info[src].distance[dst] != 0 &&
275                 numa_info[dst].distance[src] != 0 &&
276                 numa_info[src].distance[dst] !=
277                 numa_info[dst].distance[src]) {
278                 is_asymmetrical = true;
279             }
280         }
281     }
282 
283     if (is_asymmetrical) {
284         for (src = 0; src < nb_numa_nodes; src++) {
285             for (dst = 0; dst < nb_numa_nodes; dst++) {
286                 if (src != dst && numa_info[src].distance[dst] == 0) {
287                     error_report("At least one asymmetrical pair of "
288                             "distances is given, please provide distances "
289                             "for both directions of all node pairs.");
290                     exit(EXIT_FAILURE);
291                 }
292             }
293         }
294     }
295 }
296 
297 static void complete_init_numa_distance(void)
298 {
299     int src, dst;
300 
301     /* Fixup NUMA distance by symmetric policy because if it is an
302      * asymmetric distance table, it should be a complete table and
303      * there would not be any missing distance except local node, which
304      * is verified by validate_numa_distance above.
305      */
306     for (src = 0; src < nb_numa_nodes; src++) {
307         for (dst = 0; dst < nb_numa_nodes; dst++) {
308             if (numa_info[src].distance[dst] == 0) {
309                 if (src == dst) {
310                     numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
311                 } else {
312                     numa_info[src].distance[dst] = numa_info[dst].distance[src];
313                 }
314             }
315         }
316     }
317 }
318 
319 void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
320                                  int nb_nodes, ram_addr_t size)
321 {
322     int i;
323     uint64_t usedmem = 0;
324 
325     /* Align each node according to the alignment
326      * requirements of the machine class
327      */
328 
329     for (i = 0; i < nb_nodes - 1; i++) {
330         nodes[i].node_mem = (size / nb_nodes) &
331                             ~((1 << mc->numa_mem_align_shift) - 1);
332         usedmem += nodes[i].node_mem;
333     }
334     nodes[i].node_mem = size - usedmem;
335 }
336 
337 void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
338                                   int nb_nodes, ram_addr_t size)
339 {
340     int i;
341     uint64_t usedmem = 0, node_mem;
342     uint64_t granularity = size / nb_nodes;
343     uint64_t propagate = 0;
344 
345     for (i = 0; i < nb_nodes - 1; i++) {
346         node_mem = (granularity + propagate) &
347                    ~((1 << mc->numa_mem_align_shift) - 1);
348         propagate = granularity + propagate - node_mem;
349         nodes[i].node_mem = node_mem;
350         usedmem += node_mem;
351     }
352     nodes[i].node_mem = size - usedmem;
353 }
354 
355 void numa_complete_configuration(MachineState *ms)
356 {
357     int i;
358     MachineClass *mc = MACHINE_GET_CLASS(ms);
359 
360     /*
361      * If memory hotplug is enabled (slots > 0) but without '-numa'
362      * options explicitly on CLI, guestes will break.
363      *
364      *   Windows: won't enable memory hotplug without SRAT table at all
365      *
366      *   Linux: if QEMU is started with initial memory all below 4Gb
367      *   and no SRAT table present, guest kernel will use nommu DMA ops,
368      *   which breaks 32bit hw drivers when memory is hotplugged and
369      *   guest tries to use it with that drivers.
370      *
371      * Enable NUMA implicitly by adding a new NUMA node automatically.
372      */
373     if (ms->ram_slots > 0 && nb_numa_nodes == 0 &&
374         mc->auto_enable_numa_with_memhp) {
375             NumaNodeOptions node = { };
376             parse_numa_node(ms, &node, &error_abort);
377     }
378 
379     assert(max_numa_nodeid <= MAX_NODES);
380 
381     /* No support for sparse NUMA node IDs yet: */
382     for (i = max_numa_nodeid - 1; i >= 0; i--) {
383         /* Report large node IDs first, to make mistakes easier to spot */
384         if (!numa_info[i].present) {
385             error_report("numa: Node ID missing: %d", i);
386             exit(1);
387         }
388     }
389 
390     /* This must be always true if all nodes are present: */
391     assert(nb_numa_nodes == max_numa_nodeid);
392 
393     if (nb_numa_nodes > 0) {
394         uint64_t numa_total;
395 
396         if (nb_numa_nodes > MAX_NODES) {
397             nb_numa_nodes = MAX_NODES;
398         }
399 
400         /* If no memory size is given for any node, assume the default case
401          * and distribute the available memory equally across all nodes
402          */
403         for (i = 0; i < nb_numa_nodes; i++) {
404             if (numa_info[i].node_mem != 0) {
405                 break;
406             }
407         }
408         if (i == nb_numa_nodes) {
409             assert(mc->numa_auto_assign_ram);
410             mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
411             if (!qtest_enabled()) {
412                 warn_report("Default splitting of RAM between nodes is deprecated,"
413                             " Use '-numa node,memdev' to explictly define RAM"
414                             " allocation per node");
415             }
416         }
417 
418         numa_total = 0;
419         for (i = 0; i < nb_numa_nodes; i++) {
420             numa_total += numa_info[i].node_mem;
421         }
422         if (numa_total != ram_size) {
423             error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
424                          " should equal RAM size (0x" RAM_ADDR_FMT ")",
425                          numa_total, ram_size);
426             exit(1);
427         }
428 
429         /* QEMU needs at least all unique node pair distances to build
430          * the whole NUMA distance table. QEMU treats the distance table
431          * as symmetric by default, i.e. distance A->B == distance B->A.
432          * Thus, QEMU is able to complete the distance table
433          * initialization even though only distance A->B is provided and
434          * distance B->A is not. QEMU knows the distance of a node to
435          * itself is always 10, so A->A distances may be omitted. When
436          * the distances of two nodes of a pair differ, i.e. distance
437          * A->B != distance B->A, then that means the distance table is
438          * asymmetric. In this case, the distances for both directions
439          * of all node pairs are required.
440          */
441         if (have_numa_distance) {
442             /* Validate enough NUMA distance information was provided. */
443             validate_numa_distance();
444 
445             /* Validation succeeded, now fill in any missing distances. */
446             complete_init_numa_distance();
447         }
448     }
449 }
450 
451 void parse_numa_opts(MachineState *ms)
452 {
453     qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, &error_fatal);
454 }
455 
456 void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
457 {
458     int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
459 
460     if (node_id == CPU_UNSET_NUMA_NODE_ID) {
461         /* due to bug in libvirt, it doesn't pass node-id from props on
462          * device_add as expected, so we have to fix it up here */
463         if (slot->props.has_node_id) {
464             object_property_set_int(OBJECT(dev), slot->props.node_id,
465                                     "node-id", errp);
466         }
467     } else if (node_id != slot->props.node_id) {
468         error_setg(errp, "invalid node-id, must be %"PRId64,
469                    slot->props.node_id);
470     }
471 }
472 
473 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
474                                            const char *name,
475                                            uint64_t ram_size)
476 {
477     if (mem_path) {
478 #ifdef __linux__
479         Error *err = NULL;
480         memory_region_init_ram_from_file(mr, owner, name, ram_size, 0, 0,
481                                          mem_path, &err);
482         if (err) {
483             error_report_err(err);
484             if (mem_prealloc) {
485                 exit(1);
486             }
487             warn_report("falling back to regular RAM allocation");
488             error_printf("This is deprecated. Make sure that -mem-path "
489                          " specified path has sufficient resources to allocate"
490                          " -m specified RAM amount");
491             /* Legacy behavior: if allocation failed, fall back to
492              * regular RAM allocation.
493              */
494             mem_path = NULL;
495             memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
496         }
497 #else
498         fprintf(stderr, "-mem-path not supported on this host\n");
499         exit(1);
500 #endif
501     } else {
502         memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
503     }
504     vmstate_register_ram_global(mr);
505 }
506 
507 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
508                                           const char *name,
509                                           uint64_t ram_size)
510 {
511     uint64_t addr = 0;
512     int i;
513 
514     if (nb_numa_nodes == 0 || !have_memdevs) {
515         allocate_system_memory_nonnuma(mr, owner, name, ram_size);
516         return;
517     }
518 
519     memory_region_init(mr, owner, name, ram_size);
520     for (i = 0; i < nb_numa_nodes; i++) {
521         uint64_t size = numa_info[i].node_mem;
522         HostMemoryBackend *backend = numa_info[i].node_memdev;
523         if (!backend) {
524             continue;
525         }
526         MemoryRegion *seg = host_memory_backend_get_memory(backend);
527 
528         if (memory_region_is_mapped(seg)) {
529             char *path = object_get_canonical_path_component(OBJECT(backend));
530             error_report("memory backend %s is used multiple times. Each "
531                          "-numa option must use a different memdev value.",
532                          path);
533             g_free(path);
534             exit(1);
535         }
536 
537         host_memory_backend_set_mapped(backend, true);
538         memory_region_add_subregion(mr, addr, seg);
539         vmstate_register_ram_global(seg);
540         addr += size;
541     }
542 }
543 
544 static void numa_stat_memory_devices(NumaNodeMem node_mem[])
545 {
546     MemoryDeviceInfoList *info_list = qmp_memory_device_list();
547     MemoryDeviceInfoList *info;
548     PCDIMMDeviceInfo     *pcdimm_info;
549     VirtioPMEMDeviceInfo *vpi;
550 
551     for (info = info_list; info; info = info->next) {
552         MemoryDeviceInfo *value = info->value;
553 
554         if (value) {
555             switch (value->type) {
556             case MEMORY_DEVICE_INFO_KIND_DIMM:
557             case MEMORY_DEVICE_INFO_KIND_NVDIMM:
558                 pcdimm_info = value->type == MEMORY_DEVICE_INFO_KIND_DIMM ?
559                               value->u.dimm.data : value->u.nvdimm.data;
560                 node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
561                 node_mem[pcdimm_info->node].node_plugged_mem +=
562                     pcdimm_info->size;
563                 break;
564             case MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM:
565                 vpi = value->u.virtio_pmem.data;
566                 /* TODO: once we support numa, assign to right node */
567                 node_mem[0].node_mem += vpi->size;
568                 node_mem[0].node_plugged_mem += vpi->size;
569                 break;
570             default:
571                 g_assert_not_reached();
572             }
573         }
574     }
575     qapi_free_MemoryDeviceInfoList(info_list);
576 }
577 
578 void query_numa_node_mem(NumaNodeMem node_mem[])
579 {
580     int i;
581 
582     if (nb_numa_nodes <= 0) {
583         return;
584     }
585 
586     numa_stat_memory_devices(node_mem);
587     for (i = 0; i < nb_numa_nodes; i++) {
588         node_mem[i].node_mem += numa_info[i].node_mem;
589     }
590 }
591 
592 void ram_block_notifier_add(RAMBlockNotifier *n)
593 {
594     QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
595 }
596 
597 void ram_block_notifier_remove(RAMBlockNotifier *n)
598 {
599     QLIST_REMOVE(n, next);
600 }
601 
602 void ram_block_notify_add(void *host, size_t size)
603 {
604     RAMBlockNotifier *notifier;
605 
606     QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
607         notifier->ram_block_added(notifier, host, size);
608     }
609 }
610 
611 void ram_block_notify_remove(void *host, size_t size)
612 {
613     RAMBlockNotifier *notifier;
614 
615     QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
616         notifier->ram_block_removed(notifier, host, size);
617     }
618 }
619