blob: 1d7c49ad431b2d39b0138ada28b92517245f3b04 [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"
David Hildenbrand2cc0e2e2018-04-23 18:51:16 +020039#include "hw/mem/memory-device.h"
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -020040#include "qemu/option.h"
41#include "qemu/config-file.h"
Igor Mammedovcc001882017-10-12 11:39:58 +020042#include "qemu/cutils.h"
Wanlong Gao96d0e262014-05-14 17:43:05 +080043
Wanlong Gao00421092014-05-14 17:43:08 +080044QemuOptsList qemu_numa_opts = {
45 .name = "numa",
46 .implied_opt_name = "type",
47 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
48 .desc = { { 0 } } /* validated with OptsVisitor */
49};
50
Paolo Bonzini7febe362014-05-14 17:43:17 +080051static int have_memdevs = -1;
Eduardo Habkost25712ff2015-02-08 16:51:19 -020052static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
53 * For all nodes, nodeid < max_numa_nodeid
54 */
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020055int nb_numa_nodes;
He Chen0f203432017-04-27 10:35:58 +080056bool have_numa_distance;
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020057NodeInfo numa_info[MAX_NODES];
Paolo Bonzini7febe362014-05-14 17:43:17 +080058
Bharata B Raoe75e2a12015-06-29 13:50:27 +053059
Igor Mammedov64c2a8f2017-05-10 13:29:49 +020060static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
Igor Mammedovcc001882017-10-12 11:39:58 +020061 Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +080062{
Markus Armbrustera22528b2018-10-17 10:26:39 +020063 Error *err = NULL;
Wanlong Gao00421092014-05-14 17:43:08 +080064 uint16_t nodenr;
65 uint16List *cpus = NULL;
Igor Mammedov64c2a8f2017-05-10 13:29:49 +020066 MachineClass *mc = MACHINE_GET_CLASS(ms);
Wanlong Gao96d0e262014-05-14 17:43:05 +080067
Wanlong Gao00421092014-05-14 17:43:08 +080068 if (node->has_nodeid) {
69 nodenr = node->nodeid;
70 } else {
71 nodenr = nb_numa_nodes;
72 }
73
74 if (nodenr >= MAX_NODES) {
75 error_setg(errp, "Max number of NUMA nodes reached: %"
Gonglei01bbbcf2015-02-25 12:22:30 +080076 PRIu16 "", nodenr);
Wanlong Gao96d0e262014-05-14 17:43:05 +080077 return;
78 }
79
Eduardo Habkost1945b9d2014-06-26 18:33:19 -030080 if (numa_info[nodenr].present) {
81 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
82 return;
83 }
84
David Hildenbrand81ce6aa2018-02-27 12:02:55 +010085 if (!mc->cpu_index_to_instance_props || !mc->get_default_cpu_node_id) {
Markus Armbrustera22528b2018-10-17 10:26:39 +020086 error_setg(errp, "NUMA is not supported by this machine-type");
87 return;
Igor Mammedov64c2a8f2017-05-10 13:29:49 +020088 }
Wanlong Gao00421092014-05-14 17:43:08 +080089 for (cpus = node->cpus; cpus; cpus = cpus->next) {
Igor Mammedov7c88e652017-05-10 13:29:50 +020090 CpuInstanceProperties props;
Eduardo Habkost8979c942015-02-09 17:28:52 -020091 if (cpus->value >= max_cpus) {
92 error_setg(errp,
93 "CPU index (%" PRIu16 ")"
94 " should be smaller than maxcpus (%d)",
95 cpus->value, max_cpus);
Wanlong Gao00421092014-05-14 17:43:08 +080096 return;
Wanlong Gao96d0e262014-05-14 17:43:05 +080097 }
Igor Mammedov7c88e652017-05-10 13:29:50 +020098 props = mc->cpu_index_to_instance_props(ms, cpus->value);
99 props.node_id = nodenr;
100 props.has_node_id = true;
Markus Armbrustera22528b2018-10-17 10:26:39 +0200101 machine_set_cpu_numa_node(ms, &props, &err);
102 if (err) {
103 error_propagate(errp, err);
104 return;
105 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800106 }
107
Paolo Bonzini7febe362014-05-14 17:43:17 +0800108 if (node->has_mem && node->has_memdev) {
Ishani Chughd0e31a12017-04-13 21:44:39 +0530109 error_setg(errp, "cannot specify both mem= and memdev=");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800110 return;
111 }
112
113 if (have_memdevs == -1) {
114 have_memdevs = node->has_memdev;
115 }
116 if (node->has_memdev != have_memdevs) {
Ishani Chughd0e31a12017-04-13 21:44:39 +0530117 error_setg(errp, "memdev option must be specified for either "
Gonglei01bbbcf2015-02-25 12:22:30 +0800118 "all or no nodes");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800119 return;
120 }
121
Wanlong Gao00421092014-05-14 17:43:08 +0800122 if (node->has_mem) {
Igor Mammedovcc001882017-10-12 11:39:58 +0200123 numa_info[nodenr].node_mem = node->mem;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800124 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800125 if (node->has_memdev) {
126 Object *o;
127 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
128 if (!o) {
129 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
130 return;
131 }
132
133 object_ref(o);
Marc-André Lureau61d7c142017-06-07 20:36:30 +0400134 numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800135 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
136 }
Eduardo Habkost1af878e2014-06-26 18:33:18 -0300137 numa_info[nodenr].present = true;
138 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
Dou Liyang7b8be492017-11-14 10:34:01 +0800139 nb_numa_nodes++;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800140}
141
He Chen0f203432017-04-27 10:35:58 +0800142static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
143{
144 uint16_t src = dist->src;
145 uint16_t dst = dist->dst;
146 uint8_t val = dist->val;
147
148 if (src >= MAX_NODES || dst >= MAX_NODES) {
Igor Mammedov74f38e92018-05-16 17:06:14 +0200149 error_setg(errp, "Parameter '%s' expects an integer between 0 and %d",
150 src >= MAX_NODES ? "src" : "dst", MAX_NODES - 1);
He Chen0f203432017-04-27 10:35:58 +0800151 return;
152 }
153
154 if (!numa_info[src].present || !numa_info[dst].present) {
155 error_setg(errp, "Source/Destination NUMA node is missing. "
156 "Please use '-numa node' option to declare it first.");
157 return;
158 }
159
160 if (val < NUMA_DISTANCE_MIN) {
161 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
162 "it shouldn't be less than %d.",
163 val, NUMA_DISTANCE_MIN);
164 return;
165 }
166
167 if (src == dst && val != NUMA_DISTANCE_MIN) {
168 error_setg(errp, "Local distance of node %d should be %d.",
169 src, NUMA_DISTANCE_MIN);
170 return;
171 }
172
173 numa_info[src].distance[dst] = val;
174 have_numa_distance = true;
175}
176
Igor Mammedov3319b4e2018-05-04 10:37:40 +0200177static
178void set_numa_options(MachineState *ms, NumaOptions *object, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800179{
Wanlong Gao00421092014-05-14 17:43:08 +0800180 Error *err = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800181
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600182 switch (object->type) {
Markus Armbrusterd081a492017-02-21 21:46:26 +0100183 case NUMA_OPTIONS_TYPE_NODE:
Igor Mammedovcc001882017-10-12 11:39:58 +0200184 parse_numa_node(ms, &object->u.node, &err);
Wanlong Gao00421092014-05-14 17:43:08 +0800185 if (err) {
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200186 goto end;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800187 }
Wanlong Gao00421092014-05-14 17:43:08 +0800188 break;
He Chen0f203432017-04-27 10:35:58 +0800189 case NUMA_OPTIONS_TYPE_DIST:
190 parse_numa_distance(&object->u.dist, &err);
191 if (err) {
192 goto end;
193 }
194 break;
Igor Mammedov419fcde2017-05-10 13:30:01 +0200195 case NUMA_OPTIONS_TYPE_CPU:
196 if (!object->u.cpu.has_node_id) {
197 error_setg(&err, "Missing mandatory node-id property");
198 goto end;
199 }
200 if (!numa_info[object->u.cpu.node_id].present) {
201 error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be "
202 "defined with -numa node,nodeid=ID before it's used with "
203 "-numa cpu,node-id=ID", object->u.cpu.node_id);
204 goto end;
205 }
206
207 machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu),
208 &err);
209 break;
Wanlong Gao00421092014-05-14 17:43:08 +0800210 default:
211 abort();
Wanlong Gao96d0e262014-05-14 17:43:05 +0800212 }
Wanlong Gao00421092014-05-14 17:43:08 +0800213
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200214end:
Igor Mammedov3319b4e2018-05-04 10:37:40 +0200215 error_propagate(errp, err);
216}
217
218int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
219{
220 NumaOptions *object = NULL;
221 MachineState *ms = MACHINE(opaque);
222 Error *err = NULL;
223 Visitor *v = opts_visitor_new(opts);
224
225 visit_type_NumaOptions(v, NULL, &object, &err);
226 visit_free(v);
227 if (err) {
228 goto end;
229 }
230
231 /* Fix up legacy suffix-less format */
232 if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
233 const char *mem_str = qemu_opt_get(opts, "mem");
234 qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
235 }
236
237 set_numa_options(ms, object, &err);
238
239end:
Eric Blake96a16162016-02-23 14:14:33 -0700240 qapi_free_NumaOptions(object);
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200241 if (err) {
242 error_report_err(err);
243 return -1;
244 }
Wanlong Gao00421092014-05-14 17:43:08 +0800245
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200246 return 0;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800247}
248
He Chen0f203432017-04-27 10:35:58 +0800249/* If all node pair distances are symmetric, then only distances
250 * in one direction are enough. If there is even one asymmetric
251 * pair, though, then all distances must be provided. The
252 * distance from a node to itself is always NUMA_DISTANCE_MIN,
253 * so providing it is never necessary.
254 */
255static void validate_numa_distance(void)
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200256{
He Chen0f203432017-04-27 10:35:58 +0800257 int src, dst;
258 bool is_asymmetrical = false;
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200259
He Chen0f203432017-04-27 10:35:58 +0800260 for (src = 0; src < nb_numa_nodes; src++) {
261 for (dst = src; dst < nb_numa_nodes; dst++) {
262 if (numa_info[src].distance[dst] == 0 &&
263 numa_info[dst].distance[src] == 0) {
264 if (src != dst) {
265 error_report("The distance between node %d and %d is "
266 "missing, at least one distance value "
267 "between each nodes should be provided.",
268 src, dst);
269 exit(EXIT_FAILURE);
270 }
271 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200272
He Chen0f203432017-04-27 10:35:58 +0800273 if (numa_info[src].distance[dst] != 0 &&
274 numa_info[dst].distance[src] != 0 &&
275 numa_info[src].distance[dst] !=
276 numa_info[dst].distance[src]) {
277 is_asymmetrical = true;
278 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200279 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200280 }
Eduardo Habkost549fc542015-02-09 17:35:04 -0200281
He Chen0f203432017-04-27 10:35:58 +0800282 if (is_asymmetrical) {
283 for (src = 0; src < nb_numa_nodes; src++) {
284 for (dst = 0; dst < nb_numa_nodes; dst++) {
285 if (src != dst && numa_info[src].distance[dst] == 0) {
286 error_report("At least one asymmetrical pair of "
287 "distances is given, please provide distances "
288 "for both directions of all node pairs.");
289 exit(EXIT_FAILURE);
290 }
291 }
292 }
Eduardo Habkost549fc542015-02-09 17:35:04 -0200293 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200294}
295
He Chen0f203432017-04-27 10:35:58 +0800296static void complete_init_numa_distance(void)
297{
298 int src, dst;
299
300 /* Fixup NUMA distance by symmetric policy because if it is an
301 * asymmetric distance table, it should be a complete table and
302 * there would not be any missing distance except local node, which
303 * is verified by validate_numa_distance above.
304 */
305 for (src = 0; src < nb_numa_nodes; src++) {
306 for (dst = 0; dst < nb_numa_nodes; dst++) {
307 if (numa_info[src].distance[dst] == 0) {
308 if (src == dst) {
309 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
310 } else {
311 numa_info[src].distance[dst] = numa_info[dst].distance[src];
312 }
313 }
314 }
315 }
316}
317
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200318void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
319 int nb_nodes, ram_addr_t size)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800320{
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300321 int i;
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200322 uint64_t usedmem = 0;
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300323
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200324 /* Align each node according to the alignment
325 * requirements of the machine class
326 */
327
328 for (i = 0; i < nb_nodes - 1; i++) {
329 nodes[i].node_mem = (size / nb_nodes) &
330 ~((1 << mc->numa_mem_align_shift) - 1);
331 usedmem += nodes[i].node_mem;
Igor Mammedovcdda2012016-11-18 12:02:54 +0100332 }
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200333 nodes[i].node_mem = size - usedmem;
334}
Igor Mammedovcdda2012016-11-18 12:02:54 +0100335
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200336void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
337 int nb_nodes, ram_addr_t size)
338{
339 int i;
340 uint64_t usedmem = 0, node_mem;
341 uint64_t granularity = size / nb_nodes;
342 uint64_t propagate = 0;
343
344 for (i = 0; i < nb_nodes - 1; i++) {
345 node_mem = (granularity + propagate) &
346 ~((1 << mc->numa_mem_align_shift) - 1);
347 propagate = granularity + propagate - node_mem;
348 nodes[i].node_mem = node_mem;
349 usedmem += node_mem;
350 }
351 nodes[i].node_mem = size - usedmem;
352}
353
Igor Mammedov7a3099f2018-05-04 10:37:39 +0200354void numa_complete_configuration(MachineState *ms)
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -0200355{
356 int i;
Igor Mammedovea089ee2017-05-10 13:29:45 +0200357 MachineClass *mc = MACHINE_GET_CLASS(ms);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800358
Dou Liyang7b8be492017-11-14 10:34:01 +0800359 /*
360 * If memory hotplug is enabled (slots > 0) but without '-numa'
361 * options explicitly on CLI, guestes will break.
362 *
363 * Windows: won't enable memory hotplug without SRAT table at all
364 *
365 * Linux: if QEMU is started with initial memory all below 4Gb
366 * and no SRAT table present, guest kernel will use nommu DMA ops,
367 * which breaks 32bit hw drivers when memory is hotplugged and
368 * guest tries to use it with that drivers.
369 *
370 * Enable NUMA implicitly by adding a new NUMA node automatically.
371 */
372 if (ms->ram_slots > 0 && nb_numa_nodes == 0 &&
373 mc->auto_enable_numa_with_memhp) {
374 NumaNodeOptions node = { };
Markus Armbrustera22528b2018-10-17 10:26:39 +0200375 parse_numa_node(ms, &node, &error_abort);
Dou Liyang7b8be492017-11-14 10:34:01 +0800376 }
377
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300378 assert(max_numa_nodeid <= MAX_NODES);
379
380 /* No support for sparse NUMA node IDs yet: */
381 for (i = max_numa_nodeid - 1; i >= 0; i--) {
382 /* Report large node IDs first, to make mistakes easier to spot */
383 if (!numa_info[i].present) {
384 error_report("numa: Node ID missing: %d", i);
385 exit(1);
386 }
387 }
388
389 /* This must be always true if all nodes are present: */
390 assert(nb_numa_nodes == max_numa_nodeid);
391
Wanlong Gao96d0e262014-05-14 17:43:05 +0800392 if (nb_numa_nodes > 0) {
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800393 uint64_t numa_total;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800394
395 if (nb_numa_nodes > MAX_NODES) {
396 nb_numa_nodes = MAX_NODES;
397 }
398
Michael S. Tsirkin9851d0f2014-06-24 08:50:30 +0300399 /* If no memory size is given for any node, assume the default case
Wanlong Gao96d0e262014-05-14 17:43:05 +0800400 * and distribute the available memory equally across all nodes
401 */
402 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800403 if (numa_info[i].node_mem != 0) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800404 break;
405 }
406 }
407 if (i == nb_numa_nodes) {
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200408 assert(mc->numa_auto_assign_ram);
409 mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800410 }
411
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800412 numa_total = 0;
413 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800414 numa_total += numa_info[i].node_mem;
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800415 }
416 if (numa_total != ram_size) {
Hu Taoc68233a2014-08-04 16:16:09 +0800417 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
418 " should equal RAM size (0x" RAM_ADDR_FMT ")",
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800419 numa_total, ram_size);
420 exit(1);
421 }
422
He Chen0f203432017-04-27 10:35:58 +0800423 /* QEMU needs at least all unique node pair distances to build
424 * the whole NUMA distance table. QEMU treats the distance table
425 * as symmetric by default, i.e. distance A->B == distance B->A.
426 * Thus, QEMU is able to complete the distance table
427 * initialization even though only distance A->B is provided and
428 * distance B->A is not. QEMU knows the distance of a node to
429 * itself is always 10, so A->A distances may be omitted. When
430 * the distances of two nodes of a pair differ, i.e. distance
431 * A->B != distance B->A, then that means the distance table is
432 * asymmetric. In this case, the distances for both directions
433 * of all node pairs are required.
434 */
435 if (have_numa_distance) {
436 /* Validate enough NUMA distance information was provided. */
437 validate_numa_distance();
438
439 /* Validation succeeded, now fill in any missing distances. */
440 complete_init_numa_distance();
441 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800442 }
443}
444
Igor Mammedov7a3099f2018-05-04 10:37:39 +0200445void parse_numa_opts(MachineState *ms)
446{
447 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, NULL)) {
448 exit(1);
449 }
450}
451
Igor Mammedovf3be6782018-05-04 10:37:48 +0200452void qmp_set_numa_node(NumaOptions *cmd, Error **errp)
453{
454 if (!runstate_check(RUN_STATE_PRECONFIG)) {
455 error_setg(errp, "The command is permitted only in '%s' state",
456 RunState_str(RUN_STATE_PRECONFIG));
457 return;
458 }
459
460 set_numa_options(MACHINE(qdev_get_machine()), cmd, errp);
461}
462
Igor Mammedova0ceb642017-05-30 18:23:56 +0200463void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
464{
Igor Mammedova0ceb642017-05-30 18:23:56 +0200465 int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
466
Igor Mammedova0ceb642017-05-30 18:23:56 +0200467 if (node_id == CPU_UNSET_NUMA_NODE_ID) {
468 /* due to bug in libvirt, it doesn't pass node-id from props on
469 * device_add as expected, so we have to fix it up here */
Igor Mammedovd41f3e72017-05-30 18:23:58 +0200470 if (slot->props.has_node_id) {
471 object_property_set_int(OBJECT(dev), slot->props.node_id,
472 "node-id", errp);
473 }
474 } else if (node_id != slot->props.node_id) {
Igor Mammedova0ceb642017-05-30 18:23:56 +0200475 error_setg(errp, "node-id=%d must match numa node specified "
476 "with -numa option", node_id);
477 }
478}
479
Paolo Bonzini7febe362014-05-14 17:43:17 +0800480static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
481 const char *name,
482 uint64_t ram_size)
483{
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800484 if (mem_path) {
485#ifdef __linux__
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800486 Error *err = NULL;
Junyan Hecbfc0172018-07-18 15:47:58 +0800487 memory_region_init_ram_from_file(mr, owner, name, ram_size, 0, 0,
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800488 mem_path, &err);
Igor Mammedovc3ba3092014-06-17 12:17:05 +0200489 if (err) {
Markus Armbruster29b762f2015-02-10 15:06:23 +0100490 error_report_err(err);
Luiz Capitulinofae947b2016-01-22 09:15:01 -0500491 if (mem_prealloc) {
492 exit(1);
493 }
Marcelo Tosattie85687f2018-01-15 18:17:01 -0200494 error_report("falling back to regular RAM allocation.");
Luiz Capitulinofae947b2016-01-22 09:15:01 -0500495
496 /* Legacy behavior: if allocation failed, fall back to
497 * regular RAM allocation.
498 */
David Gibson6233b672018-04-19 17:19:11 +1000499 mem_path = NULL;
Peter Maydell1cfe48c2017-07-07 15:42:49 +0100500 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800501 }
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800502#else
503 fprintf(stderr, "-mem-path not supported on this host\n");
504 exit(1);
505#endif
506 } else {
Peter Maydell1cfe48c2017-07-07 15:42:49 +0100507 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800508 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800509 vmstate_register_ram_global(mr);
510}
511
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800512void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
513 const char *name,
514 uint64_t ram_size)
515{
Paolo Bonzini7febe362014-05-14 17:43:17 +0800516 uint64_t addr = 0;
517 int i;
518
519 if (nb_numa_nodes == 0 || !have_memdevs) {
520 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
521 return;
522 }
523
524 memory_region_init(mr, owner, name, ram_size);
Dou Liyangf51878b2017-08-22 15:45:36 +0800525 for (i = 0; i < nb_numa_nodes; i++) {
Paolo Bonzini7febe362014-05-14 17:43:17 +0800526 uint64_t size = numa_info[i].node_mem;
527 HostMemoryBackend *backend = numa_info[i].node_memdev;
528 if (!backend) {
529 continue;
530 }
David Hildenbrand7943e972018-06-19 15:41:36 +0200531 MemoryRegion *seg = host_memory_backend_get_memory(backend);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800532
Hu Tao0462fae2014-06-30 18:28:15 +0800533 if (memory_region_is_mapped(seg)) {
534 char *path = object_get_canonical_path_component(OBJECT(backend));
535 error_report("memory backend %s is used multiple times. Each "
536 "-numa option must use a different memdev value.",
537 path);
538 exit(1);
539 }
540
Greg Kurz0b217572016-07-19 10:28:35 +0200541 host_memory_backend_set_mapped(backend, true);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800542 memory_region_add_subregion(mr, addr, seg);
543 vmstate_register_ram_global(seg);
544 addr += size;
545 }
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800546}
Hu Tao76b5d852014-06-16 18:05:41 +0800547
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200548static void numa_stat_memory_devices(NumaNodeMem node_mem[])
zhanghailiang5b009e42014-11-04 19:49:30 +0800549{
David Hildenbrand2cc0e2e2018-04-23 18:51:16 +0200550 MemoryDeviceInfoList *info_list = qmp_memory_device_list();
zhanghailiang5b009e42014-11-04 19:49:30 +0800551 MemoryDeviceInfoList *info;
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200552 PCDIMMDeviceInfo *pcdimm_info;
zhanghailiang5b009e42014-11-04 19:49:30 +0800553
zhanghailiang5b009e42014-11-04 19:49:30 +0800554 for (info = info_list; info; info = info->next) {
555 MemoryDeviceInfo *value = info->value;
556
557 if (value) {
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600558 switch (value->type) {
Haozhong Zhang6388e182018-03-11 11:02:12 +0800559 case MEMORY_DEVICE_INFO_KIND_DIMM:
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200560 pcdimm_info = value->u.dimm.data;
Haozhong Zhang6388e182018-03-11 11:02:12 +0800561 break;
562
563 case MEMORY_DEVICE_INFO_KIND_NVDIMM:
564 pcdimm_info = value->u.nvdimm.data;
565 break;
566
567 default:
568 pcdimm_info = NULL;
569 break;
570 }
571
572 if (pcdimm_info) {
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200573 node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
David Hildenbrand178003e2018-06-22 16:40:45 +0200574 node_mem[pcdimm_info->node].node_plugged_mem +=
575 pcdimm_info->size;
zhanghailiang5b009e42014-11-04 19:49:30 +0800576 }
577 }
578 }
579 qapi_free_MemoryDeviceInfoList(info_list);
580}
581
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200582void query_numa_node_mem(NumaNodeMem node_mem[])
zhanghailiang5b009e42014-11-04 19:49:30 +0800583{
584 int i;
585
586 if (nb_numa_nodes <= 0) {
587 return;
588 }
589
590 numa_stat_memory_devices(node_mem);
591 for (i = 0; i < nb_numa_nodes; i++) {
Vadim Galitsyn31959e82017-08-29 17:30:20 +0200592 node_mem[i].node_mem += numa_info[i].node_mem;
zhanghailiang5b009e42014-11-04 19:49:30 +0800593 }
594}
595
Hu Tao76b5d852014-06-16 18:05:41 +0800596static int query_memdev(Object *obj, void *opaque)
597{
598 MemdevList **list = opaque;
Chen Fanb0e90182014-08-18 14:46:33 +0800599 MemdevList *m = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800600
601 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
Chen Fanb0e90182014-08-18 14:46:33 +0800602 m = g_malloc0(sizeof(*m));
Hu Tao76b5d852014-06-16 18:05:41 +0800603
604 m->value = g_malloc0(sizeof(*m->value));
605
Paolo Bonzini29de4ec2018-04-30 11:48:18 +0200606 m->value->id = object_get_canonical_path_component(obj);
Igor Mammedove1ff3c62017-01-10 13:53:15 +0100607 m->value->has_id = !!m->value->id;
608
Marc-André Lureau61d7c142017-06-07 20:36:30 +0400609 m->value->size = object_property_get_uint(obj, "size",
610 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800611 m->value->merge = object_property_get_bool(obj, "merge",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100612 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800613 m->value->dump = object_property_get_bool(obj, "dump",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100614 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800615 m->value->prealloc = object_property_get_bool(obj,
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100616 "prealloc",
617 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800618 m->value->policy = object_property_get_enum(obj,
619 "policy",
Daniel P. Berrangea3590da2015-05-27 16:07:56 +0100620 "HostMemPolicy",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100621 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800622 object_property_get_uint16List(obj, "host-nodes",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100623 &m->value->host_nodes,
624 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800625
626 m->next = *list;
627 *list = m;
628 }
629
630 return 0;
Hu Tao76b5d852014-06-16 18:05:41 +0800631}
632
633MemdevList *qmp_query_memdev(Error **errp)
634{
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100635 Object *obj = object_get_objects_root();
Chen Fanecaf54a2014-08-18 14:46:35 +0800636 MemdevList *list = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800637
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100638 object_child_foreach(obj, query_memdev, &list);
Hu Tao76b5d852014-06-16 18:05:41 +0800639 return list;
Hu Tao76b5d852014-06-16 18:05:41 +0800640}
Igor Mammedov6bea1dd2016-10-05 17:51:23 +0200641
Paolo Bonzini0987d732016-12-21 00:31:36 +0800642void ram_block_notifier_add(RAMBlockNotifier *n)
643{
644 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
645}
646
647void ram_block_notifier_remove(RAMBlockNotifier *n)
648{
649 QLIST_REMOVE(n, next);
650}
651
652void ram_block_notify_add(void *host, size_t size)
653{
654 RAMBlockNotifier *notifier;
655
656 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
657 notifier->ram_block_added(notifier, host, size);
658 }
659}
660
661void ram_block_notify_remove(void *host, size_t size)
662{
663 RAMBlockNotifier *notifier;
664
665 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
666 notifier->ram_block_removed(notifier, host, size);
667 }
668}