blob: 1116c90af9ebd3ae77202c587beebeeae1fa54cf [file] [log] [blame]
Wanlong Gao96d0e262014-05-14 17:43:05 +08001/*
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
Peter Maydelld38ea872016-01-29 17:50:05 +000025#include "qemu/osdep.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020026#include "sysemu/numa.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080027#include "exec/cpu-common.h"
Paolo Bonzini0987d732016-12-21 00:31:36 +080028#include "exec/ramlist.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080029#include "qemu/bitmap.h"
30#include "qom/cpu.h"
Wanlong Gao2b631ec2014-05-14 17:43:06 +080031#include "qemu/error-report.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010032#include "qapi/error.h"
Wanlong Gao00421092014-05-14 17:43:08 +080033#include "qapi/opts-visitor.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060034#include "qapi/qapi-commands-misc.h"
35#include "qapi/qapi-visit-misc.h"
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +080036#include "hw/boards.h"
Paolo Bonzini7febe362014-05-14 17:43:17 +080037#include "sysemu/hostmem.h"
zhanghailiang5b009e42014-11-04 19:49:30 +080038#include "hw/mem/pc-dimm.h"
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -020039#include "qemu/option.h"
40#include "qemu/config-file.h"
Igor Mammedovcc001882017-10-12 11:39:58 +020041#include "qemu/cutils.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080042
Wanlong Gao00421092014-05-14 17:43:08 +080043QemuOptsList qemu_numa_opts = {
44 .name = "numa",
45 .implied_opt_name = "type",
46 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
47 .desc = { { 0 } } /* validated with OptsVisitor */
48};
49
Paolo Bonzini7febe362014-05-14 17:43:17 +080050static int have_memdevs = -1;
Eduardo Habkost25712ff2015-02-08 16:51:19 -020051static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
52 * For all nodes, nodeid < max_numa_nodeid
53 */
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020054int nb_numa_nodes;
He Chen0f203432017-04-27 10:35:58 +080055bool have_numa_distance;
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020056NodeInfo numa_info[MAX_NODES];
Paolo Bonzini7febe362014-05-14 17:43:17 +080057
Bharata B Raoe75e2a12015-06-29 13:50:27 +053058
Igor Mammedov64c2a8f2017-05-10 13:29:49 +020059static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
Igor Mammedovcc001882017-10-12 11:39:58 +020060 Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +080061{
Wanlong Gao00421092014-05-14 17:43:08 +080062 uint16_t nodenr;
63 uint16List *cpus = NULL;
Igor Mammedov64c2a8f2017-05-10 13:29:49 +020064 MachineClass *mc = MACHINE_GET_CLASS(ms);
Wanlong Gao96d0e262014-05-14 17:43:05 +080065
Wanlong Gao00421092014-05-14 17:43:08 +080066 if (node->has_nodeid) {
67 nodenr = node->nodeid;
68 } else {
69 nodenr = nb_numa_nodes;
70 }
71
72 if (nodenr >= MAX_NODES) {
73 error_setg(errp, "Max number of NUMA nodes reached: %"
Gonglei01bbbcf2015-02-25 12:22:30 +080074 PRIu16 "", nodenr);
Wanlong Gao96d0e262014-05-14 17:43:05 +080075 return;
76 }
77
Eduardo Habkost1945b9d2014-06-26 18:33:19 -030078 if (numa_info[nodenr].present) {
79 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
80 return;
81 }
82
David Hildenbrand81ce6aa2018-02-27 12:02:55 +010083 if (!mc->cpu_index_to_instance_props || !mc->get_default_cpu_node_id) {
Igor Mammedov64c2a8f2017-05-10 13:29:49 +020084 error_report("NUMA is not supported by this machine-type");
85 exit(1);
86 }
Wanlong Gao00421092014-05-14 17:43:08 +080087 for (cpus = node->cpus; cpus; cpus = cpus->next) {
Igor Mammedov7c88e652017-05-10 13:29:50 +020088 CpuInstanceProperties props;
Eduardo Habkost8979c942015-02-09 17:28:52 -020089 if (cpus->value >= max_cpus) {
90 error_setg(errp,
91 "CPU index (%" PRIu16 ")"
92 " should be smaller than maxcpus (%d)",
93 cpus->value, max_cpus);
Wanlong Gao00421092014-05-14 17:43:08 +080094 return;
Wanlong Gao96d0e262014-05-14 17:43:05 +080095 }
Igor Mammedov7c88e652017-05-10 13:29:50 +020096 props = mc->cpu_index_to_instance_props(ms, cpus->value);
97 props.node_id = nodenr;
98 props.has_node_id = true;
99 machine_set_cpu_numa_node(ms, &props, &error_fatal);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800100 }
101
Paolo Bonzini7febe362014-05-14 17:43:17 +0800102 if (node->has_mem && node->has_memdev) {
Ishani Chughd0e31a12017-04-13 21:44:39 +0530103 error_setg(errp, "cannot specify both mem= and memdev=");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800104 return;
105 }
106
107 if (have_memdevs == -1) {
108 have_memdevs = node->has_memdev;
109 }
110 if (node->has_memdev != have_memdevs) {
Ishani Chughd0e31a12017-04-13 21:44:39 +0530111 error_setg(errp, "memdev option must be specified for either "
Gonglei01bbbcf2015-02-25 12:22:30 +0800112 "all or no nodes");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800113 return;
114 }
115
Wanlong Gao00421092014-05-14 17:43:08 +0800116 if (node->has_mem) {
Igor Mammedovcc001882017-10-12 11:39:58 +0200117 numa_info[nodenr].node_mem = node->mem;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800118 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800119 if (node->has_memdev) {
120 Object *o;
121 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
122 if (!o) {
123 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
124 return;
125 }
126
127 object_ref(o);
Marc-André Lureau61d7c142017-06-07 20:36:30 +0400128 numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800129 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
130 }
Eduardo Habkost1af878e2014-06-26 18:33:18 -0300131 numa_info[nodenr].present = true;
132 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
Dou Liyang7b8be492017-11-14 10:34:01 +0800133 nb_numa_nodes++;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800134}
135
He Chen0f203432017-04-27 10:35:58 +0800136static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
137{
138 uint16_t src = dist->src;
139 uint16_t dst = dist->dst;
140 uint8_t val = dist->val;
141
142 if (src >= MAX_NODES || dst >= MAX_NODES) {
143 error_setg(errp,
Eduardo Habkostf8922912017-05-30 15:40:13 -0300144 "Invalid node %d, max possible could be %d",
He Chen0f203432017-04-27 10:35:58 +0800145 MAX(src, dst), MAX_NODES);
146 return;
147 }
148
149 if (!numa_info[src].present || !numa_info[dst].present) {
150 error_setg(errp, "Source/Destination NUMA node is missing. "
151 "Please use '-numa node' option to declare it first.");
152 return;
153 }
154
155 if (val < NUMA_DISTANCE_MIN) {
156 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
157 "it shouldn't be less than %d.",
158 val, NUMA_DISTANCE_MIN);
159 return;
160 }
161
162 if (src == dst && val != NUMA_DISTANCE_MIN) {
163 error_setg(errp, "Local distance of node %d should be %d.",
164 src, NUMA_DISTANCE_MIN);
165 return;
166 }
167
168 numa_info[src].distance[dst] = val;
169 have_numa_distance = true;
170}
171
Markus Armbruster28d0de72015-03-13 13:35:14 +0100172static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800173{
Wanlong Gao00421092014-05-14 17:43:08 +0800174 NumaOptions *object = NULL;
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200175 MachineState *ms = opaque;
Wanlong Gao00421092014-05-14 17:43:08 +0800176 Error *err = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800177
Wanlong Gao00421092014-05-14 17:43:08 +0800178 {
Eric Blake09204ea2016-06-09 10:48:36 -0600179 Visitor *v = opts_visitor_new(opts);
180 visit_type_NumaOptions(v, NULL, &object, &err);
181 visit_free(v);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800182 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800183
Wanlong Gao00421092014-05-14 17:43:08 +0800184 if (err) {
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200185 goto end;
Wanlong Gao00421092014-05-14 17:43:08 +0800186 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800187
Igor Mammedovcc001882017-10-12 11:39:58 +0200188 /* Fix up legacy suffix-less format */
189 if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
190 const char *mem_str = qemu_opt_get(opts, "mem");
191 qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
192 }
193
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600194 switch (object->type) {
Markus Armbrusterd081a492017-02-21 21:46:26 +0100195 case NUMA_OPTIONS_TYPE_NODE:
Igor Mammedovcc001882017-10-12 11:39:58 +0200196 parse_numa_node(ms, &object->u.node, &err);
Wanlong Gao00421092014-05-14 17:43:08 +0800197 if (err) {
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200198 goto end;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800199 }
Wanlong Gao00421092014-05-14 17:43:08 +0800200 break;
He Chen0f203432017-04-27 10:35:58 +0800201 case NUMA_OPTIONS_TYPE_DIST:
202 parse_numa_distance(&object->u.dist, &err);
203 if (err) {
204 goto end;
205 }
206 break;
Igor Mammedov419fcde2017-05-10 13:30:01 +0200207 case NUMA_OPTIONS_TYPE_CPU:
208 if (!object->u.cpu.has_node_id) {
209 error_setg(&err, "Missing mandatory node-id property");
210 goto end;
211 }
212 if (!numa_info[object->u.cpu.node_id].present) {
213 error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be "
214 "defined with -numa node,nodeid=ID before it's used with "
215 "-numa cpu,node-id=ID", object->u.cpu.node_id);
216 goto end;
217 }
218
219 machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu),
220 &err);
221 break;
Wanlong Gao00421092014-05-14 17:43:08 +0800222 default:
223 abort();
Wanlong Gao96d0e262014-05-14 17:43:05 +0800224 }
Wanlong Gao00421092014-05-14 17:43:08 +0800225
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200226end:
Eric Blake96a16162016-02-23 14:14:33 -0700227 qapi_free_NumaOptions(object);
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200228 if (err) {
229 error_report_err(err);
230 return -1;
231 }
Wanlong Gao00421092014-05-14 17:43:08 +0800232
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200233 return 0;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800234}
235
He Chen0f203432017-04-27 10:35:58 +0800236/* If all node pair distances are symmetric, then only distances
237 * in one direction are enough. If there is even one asymmetric
238 * pair, though, then all distances must be provided. The
239 * distance from a node to itself is always NUMA_DISTANCE_MIN,
240 * so providing it is never necessary.
241 */
242static void validate_numa_distance(void)
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200243{
He Chen0f203432017-04-27 10:35:58 +0800244 int src, dst;
245 bool is_asymmetrical = false;
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200246
He Chen0f203432017-04-27 10:35:58 +0800247 for (src = 0; src < nb_numa_nodes; src++) {
248 for (dst = src; dst < nb_numa_nodes; dst++) {
249 if (numa_info[src].distance[dst] == 0 &&
250 numa_info[dst].distance[src] == 0) {
251 if (src != dst) {
252 error_report("The distance between node %d and %d is "
253 "missing, at least one distance value "
254 "between each nodes should be provided.",
255 src, dst);
256 exit(EXIT_FAILURE);
257 }
258 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200259
He Chen0f203432017-04-27 10:35:58 +0800260 if (numa_info[src].distance[dst] != 0 &&
261 numa_info[dst].distance[src] != 0 &&
262 numa_info[src].distance[dst] !=
263 numa_info[dst].distance[src]) {
264 is_asymmetrical = true;
265 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200266 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200267 }
Eduardo Habkost549fc542015-02-09 17:35:04 -0200268
He Chen0f203432017-04-27 10:35:58 +0800269 if (is_asymmetrical) {
270 for (src = 0; src < nb_numa_nodes; src++) {
271 for (dst = 0; dst < nb_numa_nodes; dst++) {
272 if (src != dst && numa_info[src].distance[dst] == 0) {
273 error_report("At least one asymmetrical pair of "
274 "distances is given, please provide distances "
275 "for both directions of all node pairs.");
276 exit(EXIT_FAILURE);
277 }
278 }
279 }
Eduardo Habkost549fc542015-02-09 17:35:04 -0200280 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200281}
282
He Chen0f203432017-04-27 10:35:58 +0800283static void complete_init_numa_distance(void)
284{
285 int src, dst;
286
287 /* Fixup NUMA distance by symmetric policy because if it is an
288 * asymmetric distance table, it should be a complete table and
289 * there would not be any missing distance except local node, which
290 * is verified by validate_numa_distance above.
291 */
292 for (src = 0; src < nb_numa_nodes; src++) {
293 for (dst = 0; dst < nb_numa_nodes; dst++) {
294 if (numa_info[src].distance[dst] == 0) {
295 if (src == dst) {
296 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
297 } else {
298 numa_info[src].distance[dst] = numa_info[dst].distance[src];
299 }
300 }
301 }
302 }
303}
304
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200305void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
306 int nb_nodes, ram_addr_t size)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800307{
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300308 int i;
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200309 uint64_t usedmem = 0;
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300310
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200311 /* Align each node according to the alignment
312 * requirements of the machine class
313 */
314
315 for (i = 0; i < nb_nodes - 1; i++) {
316 nodes[i].node_mem = (size / nb_nodes) &
317 ~((1 << mc->numa_mem_align_shift) - 1);
318 usedmem += nodes[i].node_mem;
Igor Mammedovcdda2012016-11-18 12:02:54 +0100319 }
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200320 nodes[i].node_mem = size - usedmem;
321}
Igor Mammedovcdda2012016-11-18 12:02:54 +0100322
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200323void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
324 int nb_nodes, ram_addr_t size)
325{
326 int i;
327 uint64_t usedmem = 0, node_mem;
328 uint64_t granularity = size / nb_nodes;
329 uint64_t propagate = 0;
330
331 for (i = 0; i < nb_nodes - 1; i++) {
332 node_mem = (granularity + propagate) &
333 ~((1 << mc->numa_mem_align_shift) - 1);
334 propagate = granularity + propagate - node_mem;
335 nodes[i].node_mem = node_mem;
336 usedmem += node_mem;
337 }
338 nodes[i].node_mem = size - usedmem;
339}
340
Igor Mammedovea089ee2017-05-10 13:29:45 +0200341void parse_numa_opts(MachineState *ms)
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -0200342{
343 int i;
Igor Mammedovea089ee2017-05-10 13:29:45 +0200344 MachineClass *mc = MACHINE_GET_CLASS(ms);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800345
Igor Mammedov64c2a8f2017-05-10 13:29:49 +0200346 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, NULL)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800347 exit(1);
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300348 }
349
Dou Liyang7b8be492017-11-14 10:34:01 +0800350 /*
351 * If memory hotplug is enabled (slots > 0) but without '-numa'
352 * options explicitly on CLI, guestes will break.
353 *
354 * Windows: won't enable memory hotplug without SRAT table at all
355 *
356 * Linux: if QEMU is started with initial memory all below 4Gb
357 * and no SRAT table present, guest kernel will use nommu DMA ops,
358 * which breaks 32bit hw drivers when memory is hotplugged and
359 * guest tries to use it with that drivers.
360 *
361 * Enable NUMA implicitly by adding a new NUMA node automatically.
362 */
363 if (ms->ram_slots > 0 && nb_numa_nodes == 0 &&
364 mc->auto_enable_numa_with_memhp) {
365 NumaNodeOptions node = { };
366 parse_numa_node(ms, &node, NULL);
367 }
368
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300369 assert(max_numa_nodeid <= MAX_NODES);
370
371 /* No support for sparse NUMA node IDs yet: */
372 for (i = max_numa_nodeid - 1; i >= 0; i--) {
373 /* Report large node IDs first, to make mistakes easier to spot */
374 if (!numa_info[i].present) {
375 error_report("numa: Node ID missing: %d", i);
376 exit(1);
377 }
378 }
379
380 /* This must be always true if all nodes are present: */
381 assert(nb_numa_nodes == max_numa_nodeid);
382
Wanlong Gao96d0e262014-05-14 17:43:05 +0800383 if (nb_numa_nodes > 0) {
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800384 uint64_t numa_total;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800385
386 if (nb_numa_nodes > MAX_NODES) {
387 nb_numa_nodes = MAX_NODES;
388 }
389
Michael S. Tsirkin9851d0f2014-06-24 08:50:30 +0300390 /* If no memory size is given for any node, assume the default case
Wanlong Gao96d0e262014-05-14 17:43:05 +0800391 * and distribute the available memory equally across all nodes
392 */
393 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800394 if (numa_info[i].node_mem != 0) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800395 break;
396 }
397 }
398 if (i == nb_numa_nodes) {
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200399 assert(mc->numa_auto_assign_ram);
400 mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800401 }
402
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800403 numa_total = 0;
404 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800405 numa_total += numa_info[i].node_mem;
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800406 }
407 if (numa_total != ram_size) {
Hu Taoc68233a2014-08-04 16:16:09 +0800408 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
409 " should equal RAM size (0x" RAM_ADDR_FMT ")",
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800410 numa_total, ram_size);
411 exit(1);
412 }
413
He Chen0f203432017-04-27 10:35:58 +0800414 /* QEMU needs at least all unique node pair distances to build
415 * the whole NUMA distance table. QEMU treats the distance table
416 * as symmetric by default, i.e. distance A->B == distance B->A.
417 * Thus, QEMU is able to complete the distance table
418 * initialization even though only distance A->B is provided and
419 * distance B->A is not. QEMU knows the distance of a node to
420 * itself is always 10, so A->A distances may be omitted. When
421 * the distances of two nodes of a pair differ, i.e. distance
422 * A->B != distance B->A, then that means the distance table is
423 * asymmetric. In this case, the distances for both directions
424 * of all node pairs are required.
425 */
426 if (have_numa_distance) {
427 /* Validate enough NUMA distance information was provided. */
428 validate_numa_distance();
429
430 /* Validation succeeded, now fill in any missing distances. */
431 complete_init_numa_distance();
432 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800433 }
434}
435
Igor Mammedova0ceb642017-05-30 18:23:56 +0200436void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
437{
Igor Mammedova0ceb642017-05-30 18:23:56 +0200438 int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
439
Igor Mammedova0ceb642017-05-30 18:23:56 +0200440 if (node_id == CPU_UNSET_NUMA_NODE_ID) {
441 /* due to bug in libvirt, it doesn't pass node-id from props on
442 * device_add as expected, so we have to fix it up here */
Igor Mammedovd41f3e72017-05-30 18:23:58 +0200443 if (slot->props.has_node_id) {
444 object_property_set_int(OBJECT(dev), slot->props.node_id,
445 "node-id", errp);
446 }
447 } else if (node_id != slot->props.node_id) {
Igor Mammedova0ceb642017-05-30 18:23:56 +0200448 error_setg(errp, "node-id=%d must match numa node specified "
449 "with -numa option", node_id);
450 }
451}
452
Paolo Bonzini7febe362014-05-14 17:43:17 +0800453static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
454 const char *name,
455 uint64_t ram_size)
456{
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800457 if (mem_path) {
458#ifdef __linux__
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800459 Error *err = NULL;
Haozhong Zhang98376842017-12-11 15:28:04 +0800460 memory_region_init_ram_from_file(mr, owner, name, ram_size, 0, false,
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800461 mem_path, &err);
Igor Mammedovc3ba3092014-06-17 12:17:05 +0200462 if (err) {
Markus Armbruster29b762f2015-02-10 15:06:23 +0100463 error_report_err(err);
Luiz Capitulinofae947b2016-01-22 09:15:01 -0500464 if (mem_prealloc) {
465 exit(1);
466 }
Marcelo Tosattie85687f2018-01-15 18:17:01 -0200467 error_report("falling back to regular RAM allocation.");
Luiz Capitulinofae947b2016-01-22 09:15:01 -0500468
469 /* Legacy behavior: if allocation failed, fall back to
470 * regular RAM allocation.
471 */
Peter Maydell1cfe48c2017-07-07 15:42:49 +0100472 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800473 }
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800474#else
475 fprintf(stderr, "-mem-path not supported on this host\n");
476 exit(1);
477#endif
478 } else {
Peter Maydell1cfe48c2017-07-07 15:42:49 +0100479 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800480 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800481 vmstate_register_ram_global(mr);
482}
483
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800484void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
485 const char *name,
486 uint64_t ram_size)
487{
Paolo Bonzini7febe362014-05-14 17:43:17 +0800488 uint64_t addr = 0;
489 int i;
490
491 if (nb_numa_nodes == 0 || !have_memdevs) {
492 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
493 return;
494 }
495
496 memory_region_init(mr, owner, name, ram_size);
Dou Liyangf51878b2017-08-22 15:45:36 +0800497 for (i = 0; i < nb_numa_nodes; i++) {
Paolo Bonzini7febe362014-05-14 17:43:17 +0800498 uint64_t size = numa_info[i].node_mem;
499 HostMemoryBackend *backend = numa_info[i].node_memdev;
500 if (!backend) {
501 continue;
502 }
Markus Armbruster007b0652015-09-11 15:04:45 +0200503 MemoryRegion *seg = host_memory_backend_get_memory(backend,
504 &error_fatal);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800505
Hu Tao0462fae2014-06-30 18:28:15 +0800506 if (memory_region_is_mapped(seg)) {
507 char *path = object_get_canonical_path_component(OBJECT(backend));
508 error_report("memory backend %s is used multiple times. Each "
509 "-numa option must use a different memdev value.",
510 path);
511 exit(1);
512 }
513
Greg Kurz0b217572016-07-19 10:28:35 +0200514 host_memory_backend_set_mapped(backend, true);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800515 memory_region_add_subregion(mr, addr, seg);
516 vmstate_register_ram_global(seg);
517 addr += size;
518 }
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800519}
Hu Tao76b5d852014-06-16 18:05:41 +0800520
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200521static void numa_stat_memory_devices(NumaNodeMem node_mem[])
zhanghailiang5b009e42014-11-04 19:49:30 +0800522{
Haozhong Zhang52c95ca2018-03-11 11:02:11 +0800523 MemoryDeviceInfoList *info_list = qmp_pc_dimm_device_list();
zhanghailiang5b009e42014-11-04 19:49:30 +0800524 MemoryDeviceInfoList *info;
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200525 PCDIMMDeviceInfo *pcdimm_info;
zhanghailiang5b009e42014-11-04 19:49:30 +0800526
zhanghailiang5b009e42014-11-04 19:49:30 +0800527 for (info = info_list; info; info = info->next) {
528 MemoryDeviceInfo *value = info->value;
529
530 if (value) {
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600531 switch (value->type) {
Haozhong Zhang6388e182018-03-11 11:02:12 +0800532 case MEMORY_DEVICE_INFO_KIND_DIMM:
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200533 pcdimm_info = value->u.dimm.data;
Haozhong Zhang6388e182018-03-11 11:02:12 +0800534 break;
535
536 case MEMORY_DEVICE_INFO_KIND_NVDIMM:
537 pcdimm_info = value->u.nvdimm.data;
538 break;
539
540 default:
541 pcdimm_info = NULL;
542 break;
543 }
544
545 if (pcdimm_info) {
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200546 node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
547 if (pcdimm_info->hotpluggable && pcdimm_info->hotplugged) {
548 node_mem[pcdimm_info->node].node_plugged_mem +=
549 pcdimm_info->size;
550 }
zhanghailiang5b009e42014-11-04 19:49:30 +0800551 }
552 }
553 }
554 qapi_free_MemoryDeviceInfoList(info_list);
555}
556
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200557void query_numa_node_mem(NumaNodeMem node_mem[])
zhanghailiang5b009e42014-11-04 19:49:30 +0800558{
559 int i;
560
561 if (nb_numa_nodes <= 0) {
562 return;
563 }
564
565 numa_stat_memory_devices(node_mem);
566 for (i = 0; i < nb_numa_nodes; i++) {
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200567 node_mem[i].node_mem += numa_info[i].node_mem;
zhanghailiang5b009e42014-11-04 19:49:30 +0800568 }
569}
570
Hu Tao76b5d852014-06-16 18:05:41 +0800571static int query_memdev(Object *obj, void *opaque)
572{
573 MemdevList **list = opaque;
Chen Fanb0e90182014-08-18 14:46:33 +0800574 MemdevList *m = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800575
576 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
Chen Fanb0e90182014-08-18 14:46:33 +0800577 m = g_malloc0(sizeof(*m));
Hu Tao76b5d852014-06-16 18:05:41 +0800578
579 m->value = g_malloc0(sizeof(*m->value));
580
Igor Mammedove1ff3c62017-01-10 13:53:15 +0100581 m->value->id = object_property_get_str(obj, "id", NULL);
582 m->value->has_id = !!m->value->id;
583
Marc-André Lureau61d7c142017-06-07 20:36:30 +0400584 m->value->size = object_property_get_uint(obj, "size",
585 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800586 m->value->merge = object_property_get_bool(obj, "merge",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100587 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800588 m->value->dump = object_property_get_bool(obj, "dump",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100589 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800590 m->value->prealloc = object_property_get_bool(obj,
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100591 "prealloc",
592 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800593 m->value->policy = object_property_get_enum(obj,
594 "policy",
Daniel P. Berrangea3590da2015-05-27 16:07:56 +0100595 "HostMemPolicy",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100596 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800597 object_property_get_uint16List(obj, "host-nodes",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100598 &m->value->host_nodes,
599 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800600
601 m->next = *list;
602 *list = m;
603 }
604
605 return 0;
Hu Tao76b5d852014-06-16 18:05:41 +0800606}
607
608MemdevList *qmp_query_memdev(Error **errp)
609{
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100610 Object *obj = object_get_objects_root();
Chen Fanecaf54a2014-08-18 14:46:35 +0800611 MemdevList *list = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800612
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100613 object_child_foreach(obj, query_memdev, &list);
Hu Tao76b5d852014-06-16 18:05:41 +0800614 return list;
Hu Tao76b5d852014-06-16 18:05:41 +0800615}
Igor Mammedov6bea1dd2016-10-05 17:51:23 +0200616
Paolo Bonzini0987d732016-12-21 00:31:36 +0800617void ram_block_notifier_add(RAMBlockNotifier *n)
618{
619 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
620}
621
622void ram_block_notifier_remove(RAMBlockNotifier *n)
623{
624 QLIST_REMOVE(n, next);
625}
626
627void ram_block_notify_add(void *host, size_t size)
628{
629 RAMBlockNotifier *notifier;
630
631 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
632 notifier->ram_block_added(notifier, host, size);
633 }
634}
635
636void ram_block_notify_remove(void *host, size_t size)
637{
638 RAMBlockNotifier *notifier;
639
640 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
641 notifier->ram_block_removed(notifier, host, size);
642 }
643}