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