blob: d753687dec195487f3424cfc8b13b920c41c6273 [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"
32#include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
Wanlong Gao00421092014-05-14 17:43:08 +080033#include "qapi-visit.h"
34#include "qapi/opts-visitor.h"
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +080035#include "hw/boards.h"
Paolo Bonzini7febe362014-05-14 17:43:17 +080036#include "sysemu/hostmem.h"
Hu Tao76b5d852014-06-16 18:05:41 +080037#include "qmp-commands.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"
Wanlong Gao96d0e262014-05-14 17:43:05 +080041
Wanlong Gao00421092014-05-14 17:43:08 +080042QemuOptsList 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
Paolo Bonzini7febe362014-05-14 17:43:17 +080049static int have_memdevs = -1;
Eduardo Habkost25712ff2015-02-08 16:51:19 -020050static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
51 * For all nodes, nodeid < max_numa_nodeid
52 */
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020053int nb_numa_nodes;
He Chen0f203432017-04-27 10:35:58 +080054bool have_numa_distance;
Eduardo Habkostde1a7c82015-02-08 16:51:18 -020055NodeInfo numa_info[MAX_NODES];
Paolo Bonzini7febe362014-05-14 17:43:17 +080056
Bharata B Raofa9ea812015-06-29 13:50:25 +053057void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
58{
Bharata B Rao672558d2015-07-09 20:57:36 +053059 struct numa_addr_range *range;
Bharata B Raofa9ea812015-06-29 13:50:25 +053060
Bharata B Raoabafabd2015-06-29 13:50:26 +053061 /*
62 * Memory-less nodes can come here with 0 size in which case,
63 * there is nothing to do.
64 */
65 if (!size) {
66 return;
67 }
68
Bharata B Rao672558d2015-07-09 20:57:36 +053069 range = g_malloc0(sizeof(*range));
Bharata B Raofa9ea812015-06-29 13:50:25 +053070 range->mem_start = addr;
71 range->mem_end = addr + size - 1;
72 QLIST_INSERT_HEAD(&numa_info[node].addr, range, entry);
73}
74
75void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
76{
77 struct numa_addr_range *range, *next;
78
79 QLIST_FOREACH_SAFE(range, &numa_info[node].addr, entry, next) {
80 if (addr == range->mem_start && (addr + size - 1) == range->mem_end) {
81 QLIST_REMOVE(range, entry);
82 g_free(range);
83 return;
84 }
85 }
86}
87
Bharata B Raoabafabd2015-06-29 13:50:26 +053088static void numa_set_mem_ranges(void)
89{
90 int i;
91 ram_addr_t mem_start = 0;
92
93 /*
94 * Deduce start address of each node and use it to store
95 * the address range info in numa_info address range list
96 */
97 for (i = 0; i < nb_numa_nodes; i++) {
98 numa_set_mem_node_id(mem_start, numa_info[i].node_mem, i);
99 mem_start += numa_info[i].node_mem;
100 }
101}
102
Bharata B Raoe75e2a12015-06-29 13:50:27 +0530103/*
104 * Check if @addr falls under NUMA @node.
105 */
106static bool numa_addr_belongs_to_node(ram_addr_t addr, uint32_t node)
107{
108 struct numa_addr_range *range;
109
110 QLIST_FOREACH(range, &numa_info[node].addr, entry) {
111 if (addr >= range->mem_start && addr <= range->mem_end) {
112 return true;
113 }
114 }
115 return false;
116}
117
118/*
119 * Given an address, return the index of the NUMA node to which the
120 * address belongs to.
121 */
122uint32_t numa_get_node(ram_addr_t addr, Error **errp)
123{
124 uint32_t i;
125
126 /* For non NUMA configurations, check if the addr falls under node 0 */
127 if (!nb_numa_nodes) {
128 if (numa_addr_belongs_to_node(addr, 0)) {
129 return 0;
130 }
131 }
132
133 for (i = 0; i < nb_numa_nodes; i++) {
134 if (numa_addr_belongs_to_node(addr, i)) {
135 return i;
136 }
137 }
138
139 error_setg(errp, "Address 0x" RAM_ADDR_FMT " doesn't belong to any "
140 "NUMA node", addr);
141 return -1;
142}
143
He Chen0f203432017-04-27 10:35:58 +0800144static void parse_numa_node(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800145{
Wanlong Gao00421092014-05-14 17:43:08 +0800146 uint16_t nodenr;
147 uint16List *cpus = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800148
Wanlong Gao00421092014-05-14 17:43:08 +0800149 if (node->has_nodeid) {
150 nodenr = node->nodeid;
151 } else {
152 nodenr = nb_numa_nodes;
153 }
154
155 if (nodenr >= MAX_NODES) {
156 error_setg(errp, "Max number of NUMA nodes reached: %"
Gonglei01bbbcf2015-02-25 12:22:30 +0800157 PRIu16 "", nodenr);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800158 return;
159 }
160
Eduardo Habkost1945b9d2014-06-26 18:33:19 -0300161 if (numa_info[nodenr].present) {
162 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
163 return;
164 }
165
Wanlong Gao00421092014-05-14 17:43:08 +0800166 for (cpus = node->cpus; cpus; cpus = cpus->next) {
Eduardo Habkost8979c942015-02-09 17:28:52 -0200167 if (cpus->value >= max_cpus) {
168 error_setg(errp,
169 "CPU index (%" PRIu16 ")"
170 " should be smaller than maxcpus (%d)",
171 cpus->value, max_cpus);
Wanlong Gao00421092014-05-14 17:43:08 +0800172 return;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800173 }
Wanlong Gao00421092014-05-14 17:43:08 +0800174 bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800175 }
176
Paolo Bonzini7febe362014-05-14 17:43:17 +0800177 if (node->has_mem && node->has_memdev) {
Gonglei01bbbcf2015-02-25 12:22:30 +0800178 error_setg(errp, "qemu: cannot specify both mem= and memdev=");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800179 return;
180 }
181
182 if (have_memdevs == -1) {
183 have_memdevs = node->has_memdev;
184 }
185 if (node->has_memdev != have_memdevs) {
186 error_setg(errp, "qemu: memdev option must be specified for either "
Gonglei01bbbcf2015-02-25 12:22:30 +0800187 "all or no nodes");
Paolo Bonzini7febe362014-05-14 17:43:17 +0800188 return;
189 }
190
Wanlong Gao00421092014-05-14 17:43:08 +0800191 if (node->has_mem) {
192 uint64_t mem_size = node->mem;
193 const char *mem_str = qemu_opt_get(opts, "mem");
194 /* Fix up legacy suffix-less format */
195 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
196 mem_size <<= 20;
197 }
198 numa_info[nodenr].node_mem = mem_size;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800199 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800200 if (node->has_memdev) {
201 Object *o;
202 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
203 if (!o) {
204 error_setg(errp, "memdev=%s is ambiguous", node->memdev);
205 return;
206 }
207
208 object_ref(o);
209 numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
210 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
211 }
Eduardo Habkost1af878e2014-06-26 18:33:18 -0300212 numa_info[nodenr].present = true;
213 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800214}
215
He Chen0f203432017-04-27 10:35:58 +0800216static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
217{
218 uint16_t src = dist->src;
219 uint16_t dst = dist->dst;
220 uint8_t val = dist->val;
221
222 if (src >= MAX_NODES || dst >= MAX_NODES) {
223 error_setg(errp,
224 "Invalid node %" PRIu16
225 ", max possible could be %" PRIu16,
226 MAX(src, dst), MAX_NODES);
227 return;
228 }
229
230 if (!numa_info[src].present || !numa_info[dst].present) {
231 error_setg(errp, "Source/Destination NUMA node is missing. "
232 "Please use '-numa node' option to declare it first.");
233 return;
234 }
235
236 if (val < NUMA_DISTANCE_MIN) {
237 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
238 "it shouldn't be less than %d.",
239 val, NUMA_DISTANCE_MIN);
240 return;
241 }
242
243 if (src == dst && val != NUMA_DISTANCE_MIN) {
244 error_setg(errp, "Local distance of node %d should be %d.",
245 src, NUMA_DISTANCE_MIN);
246 return;
247 }
248
249 numa_info[src].distance[dst] = val;
250 have_numa_distance = true;
251}
252
Markus Armbruster28d0de72015-03-13 13:35:14 +0100253static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800254{
Wanlong Gao00421092014-05-14 17:43:08 +0800255 NumaOptions *object = NULL;
256 Error *err = NULL;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800257
Wanlong Gao00421092014-05-14 17:43:08 +0800258 {
Eric Blake09204ea2016-06-09 10:48:36 -0600259 Visitor *v = opts_visitor_new(opts);
260 visit_type_NumaOptions(v, NULL, &object, &err);
261 visit_free(v);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800262 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800263
Wanlong Gao00421092014-05-14 17:43:08 +0800264 if (err) {
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200265 goto end;
Wanlong Gao00421092014-05-14 17:43:08 +0800266 }
Wanlong Gao96d0e262014-05-14 17:43:05 +0800267
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600268 switch (object->type) {
Markus Armbrusterd081a492017-02-21 21:46:26 +0100269 case NUMA_OPTIONS_TYPE_NODE:
He Chen0f203432017-04-27 10:35:58 +0800270 parse_numa_node(&object->u.node, opts, &err);
Wanlong Gao00421092014-05-14 17:43:08 +0800271 if (err) {
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200272 goto end;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800273 }
274 nb_numa_nodes++;
Wanlong Gao00421092014-05-14 17:43:08 +0800275 break;
He Chen0f203432017-04-27 10:35:58 +0800276 case NUMA_OPTIONS_TYPE_DIST:
277 parse_numa_distance(&object->u.dist, &err);
278 if (err) {
279 goto end;
280 }
281 break;
Wanlong Gao00421092014-05-14 17:43:08 +0800282 default:
283 abort();
Wanlong Gao96d0e262014-05-14 17:43:05 +0800284 }
Wanlong Gao00421092014-05-14 17:43:08 +0800285
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200286end:
Eric Blake96a16162016-02-23 14:14:33 -0700287 qapi_free_NumaOptions(object);
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200288 if (err) {
289 error_report_err(err);
290 return -1;
291 }
Wanlong Gao00421092014-05-14 17:43:08 +0800292
Marc-André Lureau157e94e2016-07-13 02:39:13 +0200293 return 0;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800294}
295
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200296static char *enumerate_cpus(unsigned long *cpus, int max_cpus)
297{
298 int cpu;
299 bool first = true;
300 GString *s = g_string_new(NULL);
301
302 for (cpu = find_first_bit(cpus, max_cpus);
303 cpu < max_cpus;
304 cpu = find_next_bit(cpus, max_cpus, cpu + 1)) {
305 g_string_append_printf(s, "%s%d", first ? "" : " ", cpu);
306 first = false;
307 }
308 return g_string_free(s, FALSE);
309}
310
311static void validate_numa_cpus(void)
312{
313 int i;
Igor Mammedovcdda2012016-11-18 12:02:54 +0100314 unsigned long *seen_cpus = bitmap_new(max_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200315
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200316 for (i = 0; i < nb_numa_nodes; i++) {
Igor Mammedovcdda2012016-11-18 12:02:54 +0100317 if (bitmap_intersects(seen_cpus, numa_info[i].node_cpu, max_cpus)) {
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200318 bitmap_and(seen_cpus, seen_cpus,
Igor Mammedovcdda2012016-11-18 12:02:54 +0100319 numa_info[i].node_cpu, max_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200320 error_report("CPU(s) present in multiple NUMA nodes: %s",
Daniel P. Berrangea8f15a22015-08-26 12:17:12 +0100321 enumerate_cpus(seen_cpus, max_cpus));
Igor Mammedovcdda2012016-11-18 12:02:54 +0100322 g_free(seen_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200323 exit(EXIT_FAILURE);
324 }
325 bitmap_or(seen_cpus, seen_cpus,
Igor Mammedovcdda2012016-11-18 12:02:54 +0100326 numa_info[i].node_cpu, max_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200327 }
Eduardo Habkost549fc542015-02-09 17:35:04 -0200328
329 if (!bitmap_full(seen_cpus, max_cpus)) {
330 char *msg;
331 bitmap_complement(seen_cpus, seen_cpus, max_cpus);
332 msg = enumerate_cpus(seen_cpus, max_cpus);
333 error_report("warning: CPU(s) not present in any NUMA nodes: %s", msg);
334 error_report("warning: All CPU(s) up to maxcpus should be described "
335 "in NUMA config");
336 g_free(msg);
337 }
Igor Mammedovcdda2012016-11-18 12:02:54 +0100338 g_free(seen_cpus);
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200339}
340
He Chen0f203432017-04-27 10:35:58 +0800341/* If all node pair distances are symmetric, then only distances
342 * in one direction are enough. If there is even one asymmetric
343 * pair, though, then all distances must be provided. The
344 * distance from a node to itself is always NUMA_DISTANCE_MIN,
345 * so providing it is never necessary.
346 */
347static void validate_numa_distance(void)
348{
349 int src, dst;
350 bool is_asymmetrical = false;
351
352 for (src = 0; src < nb_numa_nodes; src++) {
353 for (dst = src; dst < nb_numa_nodes; dst++) {
354 if (numa_info[src].distance[dst] == 0 &&
355 numa_info[dst].distance[src] == 0) {
356 if (src != dst) {
357 error_report("The distance between node %d and %d is "
358 "missing, at least one distance value "
359 "between each nodes should be provided.",
360 src, dst);
361 exit(EXIT_FAILURE);
362 }
363 }
364
365 if (numa_info[src].distance[dst] != 0 &&
366 numa_info[dst].distance[src] != 0 &&
367 numa_info[src].distance[dst] !=
368 numa_info[dst].distance[src]) {
369 is_asymmetrical = true;
370 }
371 }
372 }
373
374 if (is_asymmetrical) {
375 for (src = 0; src < nb_numa_nodes; src++) {
376 for (dst = 0; dst < nb_numa_nodes; dst++) {
377 if (src != dst && numa_info[src].distance[dst] == 0) {
378 error_report("At least one asymmetrical pair of "
379 "distances is given, please provide distances "
380 "for both directions of all node pairs.");
381 exit(EXIT_FAILURE);
382 }
383 }
384 }
385 }
386}
387
388static void complete_init_numa_distance(void)
389{
390 int src, dst;
391
392 /* Fixup NUMA distance by symmetric policy because if it is an
393 * asymmetric distance table, it should be a complete table and
394 * there would not be any missing distance except local node, which
395 * is verified by validate_numa_distance above.
396 */
397 for (src = 0; src < nb_numa_nodes; src++) {
398 for (dst = 0; dst < nb_numa_nodes; dst++) {
399 if (numa_info[src].distance[dst] == 0) {
400 if (src == dst) {
401 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
402 } else {
403 numa_info[src].distance[dst] = numa_info[dst].distance[src];
404 }
405 }
406 }
407 }
408}
409
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200410void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
411 int nb_nodes, ram_addr_t size)
412{
413 int i;
414 uint64_t usedmem = 0;
415
416 /* Align each node according to the alignment
417 * requirements of the machine class
418 */
419
420 for (i = 0; i < nb_nodes - 1; i++) {
421 nodes[i].node_mem = (size / nb_nodes) &
422 ~((1 << mc->numa_mem_align_shift) - 1);
423 usedmem += nodes[i].node_mem;
424 }
425 nodes[i].node_mem = size - usedmem;
426}
427
428void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
429 int nb_nodes, ram_addr_t size)
430{
431 int i;
432 uint64_t usedmem = 0, node_mem;
433 uint64_t granularity = size / nb_nodes;
434 uint64_t propagate = 0;
435
436 for (i = 0; i < nb_nodes - 1; i++) {
437 node_mem = (granularity + propagate) &
438 ~((1 << mc->numa_mem_align_shift) - 1);
439 propagate = granularity + propagate - node_mem;
440 nodes[i].node_mem = node_mem;
441 usedmem += node_mem;
442 }
443 nodes[i].node_mem = size - usedmem;
444}
445
Igor Mammedov57924bc2015-03-19 17:09:21 +0000446void parse_numa_opts(MachineClass *mc)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800447{
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300448 int i;
449
Igor Mammedovcdda2012016-11-18 12:02:54 +0100450 for (i = 0; i < MAX_NODES; i++) {
451 numa_info[i].node_cpu = bitmap_new(max_cpus);
452 }
453
Markus Armbruster28d0de72015-03-13 13:35:14 +0100454 if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, NULL, NULL)) {
Eduardo Habkost7dcd1d72015-02-08 16:51:20 -0200455 exit(1);
456 }
457
Eduardo Habkost12d6e462014-06-26 18:33:20 -0300458 assert(max_numa_nodeid <= MAX_NODES);
459
460 /* No support for sparse NUMA node IDs yet: */
461 for (i = max_numa_nodeid - 1; i >= 0; i--) {
462 /* Report large node IDs first, to make mistakes easier to spot */
463 if (!numa_info[i].present) {
464 error_report("numa: Node ID missing: %d", i);
465 exit(1);
466 }
467 }
468
469 /* This must be always true if all nodes are present: */
470 assert(nb_numa_nodes == max_numa_nodeid);
471
Wanlong Gao96d0e262014-05-14 17:43:05 +0800472 if (nb_numa_nodes > 0) {
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800473 uint64_t numa_total;
Wanlong Gao96d0e262014-05-14 17:43:05 +0800474
475 if (nb_numa_nodes > MAX_NODES) {
476 nb_numa_nodes = MAX_NODES;
477 }
478
Michael S. Tsirkin9851d0f2014-06-24 08:50:30 +0300479 /* If no memory size is given for any node, assume the default case
Wanlong Gao96d0e262014-05-14 17:43:05 +0800480 * and distribute the available memory equally across all nodes
481 */
482 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800483 if (numa_info[i].node_mem != 0) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800484 break;
485 }
486 }
487 if (i == nb_numa_nodes) {
Laurent Vivier3bfe5712017-05-02 18:29:55 +0200488 assert(mc->numa_auto_assign_ram);
489 mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800490 }
491
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800492 numa_total = 0;
493 for (i = 0; i < nb_numa_nodes; i++) {
Wanlong Gao8c859012014-05-14 17:43:07 +0800494 numa_total += numa_info[i].node_mem;
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800495 }
496 if (numa_total != ram_size) {
Hu Taoc68233a2014-08-04 16:16:09 +0800497 error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
498 " should equal RAM size (0x" RAM_ADDR_FMT ")",
Wanlong Gao2b631ec2014-05-14 17:43:06 +0800499 numa_total, ram_size);
500 exit(1);
501 }
502
Wanlong Gao96d0e262014-05-14 17:43:05 +0800503 for (i = 0; i < nb_numa_nodes; i++) {
Bharata B Raofa9ea812015-06-29 13:50:25 +0530504 QLIST_INIT(&numa_info[i].addr);
505 }
506
Bharata B Raoabafabd2015-06-29 13:50:26 +0530507 numa_set_mem_ranges();
508
Bharata B Raofa9ea812015-06-29 13:50:25 +0530509 for (i = 0; i < nb_numa_nodes; i++) {
Igor Mammedovcdda2012016-11-18 12:02:54 +0100510 if (!bitmap_empty(numa_info[i].node_cpu, max_cpus)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800511 break;
512 }
513 }
Igor Mammedov57924bc2015-03-19 17:09:21 +0000514 /* Historically VCPUs were assigned in round-robin order to NUMA
515 * nodes. However it causes issues with guest not handling it nice
516 * in case where cores/threads from a multicore CPU appear on
517 * different nodes. So allow boards to override default distribution
518 * rule grouping VCPUs by socket so that VCPUs from the same socket
519 * would be on the same node.
Wanlong Gao96d0e262014-05-14 17:43:05 +0800520 */
521 if (i == nb_numa_nodes) {
522 for (i = 0; i < max_cpus; i++) {
Igor Mammedov57924bc2015-03-19 17:09:21 +0000523 unsigned node_id = i % nb_numa_nodes;
524 if (mc->cpu_index_to_socket_id) {
525 node_id = mc->cpu_index_to_socket_id(i) % nb_numa_nodes;
526 }
527
528 set_bit(i, numa_info[node_id].node_cpu);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800529 }
530 }
Eduardo Habkost3ef71972015-02-09 17:32:04 -0200531
532 validate_numa_cpus();
He Chen0f203432017-04-27 10:35:58 +0800533
534 /* QEMU needs at least all unique node pair distances to build
535 * the whole NUMA distance table. QEMU treats the distance table
536 * as symmetric by default, i.e. distance A->B == distance B->A.
537 * Thus, QEMU is able to complete the distance table
538 * initialization even though only distance A->B is provided and
539 * distance B->A is not. QEMU knows the distance of a node to
540 * itself is always 10, so A->A distances may be omitted. When
541 * the distances of two nodes of a pair differ, i.e. distance
542 * A->B != distance B->A, then that means the distance table is
543 * asymmetric. In this case, the distances for both directions
544 * of all node pairs are required.
545 */
546 if (have_numa_distance) {
547 /* Validate enough NUMA distance information was provided. */
548 validate_numa_distance();
549
550 /* Validation succeeded, now fill in any missing distances. */
551 complete_init_numa_distance();
552 }
Bharata B Raoabafabd2015-06-29 13:50:26 +0530553 } else {
554 numa_set_mem_node_id(0, ram_size, 0);
Wanlong Gao96d0e262014-05-14 17:43:05 +0800555 }
556}
557
Eduardo Habkostdde11112015-02-08 16:51:22 -0200558void numa_post_machine_init(void)
Wanlong Gao96d0e262014-05-14 17:43:05 +0800559{
560 CPUState *cpu;
561 int i;
562
563 CPU_FOREACH(cpu) {
564 for (i = 0; i < nb_numa_nodes; i++) {
Igor Mammedovcdda2012016-11-18 12:02:54 +0100565 assert(cpu->cpu_index < max_cpus);
Wanlong Gao8c859012014-05-14 17:43:07 +0800566 if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
Wanlong Gao96d0e262014-05-14 17:43:05 +0800567 cpu->numa_node = i;
568 }
569 }
570 }
571}
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800572
Paolo Bonzini7febe362014-05-14 17:43:17 +0800573static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
574 const char *name,
575 uint64_t ram_size)
576{
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800577 if (mem_path) {
578#ifdef __linux__
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800579 Error *err = NULL;
Paolo Bonzinidbcb8982014-06-10 19:15:24 +0800580 memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800581 mem_path, &err);
Igor Mammedovc3ba3092014-06-17 12:17:05 +0200582 if (err) {
Markus Armbruster29b762f2015-02-10 15:06:23 +0100583 error_report_err(err);
Luiz Capitulinofae947b2016-01-22 09:15:01 -0500584 if (mem_prealloc) {
585 exit(1);
586 }
587
588 /* Legacy behavior: if allocation failed, fall back to
589 * regular RAM allocation.
590 */
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200591 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini7f56e742014-05-14 17:43:20 +0800592 }
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800593#else
594 fprintf(stderr, "-mem-path not supported on this host\n");
595 exit(1);
596#endif
597 } else {
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200598 memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
Paolo Bonzini0b183fc2014-05-14 17:43:19 +0800599 }
Paolo Bonzini7febe362014-05-14 17:43:17 +0800600 vmstate_register_ram_global(mr);
601}
602
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800603void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
604 const char *name,
605 uint64_t ram_size)
606{
Paolo Bonzini7febe362014-05-14 17:43:17 +0800607 uint64_t addr = 0;
608 int i;
609
610 if (nb_numa_nodes == 0 || !have_memdevs) {
611 allocate_system_memory_nonnuma(mr, owner, name, ram_size);
612 return;
613 }
614
615 memory_region_init(mr, owner, name, ram_size);
616 for (i = 0; i < MAX_NODES; i++) {
Paolo Bonzini7febe362014-05-14 17:43:17 +0800617 uint64_t size = numa_info[i].node_mem;
618 HostMemoryBackend *backend = numa_info[i].node_memdev;
619 if (!backend) {
620 continue;
621 }
Markus Armbruster007b0652015-09-11 15:04:45 +0200622 MemoryRegion *seg = host_memory_backend_get_memory(backend,
623 &error_fatal);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800624
Hu Tao0462fae2014-06-30 18:28:15 +0800625 if (memory_region_is_mapped(seg)) {
626 char *path = object_get_canonical_path_component(OBJECT(backend));
627 error_report("memory backend %s is used multiple times. Each "
628 "-numa option must use a different memdev value.",
629 path);
630 exit(1);
631 }
632
Greg Kurz0b217572016-07-19 10:28:35 +0200633 host_memory_backend_set_mapped(backend, true);
Paolo Bonzini7febe362014-05-14 17:43:17 +0800634 memory_region_add_subregion(mr, addr, seg);
635 vmstate_register_ram_global(seg);
636 addr += size;
637 }
Paolo Bonzinidfabb8b2014-05-14 17:43:15 +0800638}
Hu Tao76b5d852014-06-16 18:05:41 +0800639
zhanghailiang5b009e42014-11-04 19:49:30 +0800640static void numa_stat_memory_devices(uint64_t node_mem[])
641{
642 MemoryDeviceInfoList *info_list = NULL;
643 MemoryDeviceInfoList **prev = &info_list;
644 MemoryDeviceInfoList *info;
645
646 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
647 for (info = info_list; info; info = info->next) {
648 MemoryDeviceInfo *value = info->value;
649
650 if (value) {
Eric Blake1fd5d4f2015-10-26 16:34:59 -0600651 switch (value->type) {
zhanghailiang5b009e42014-11-04 19:49:30 +0800652 case MEMORY_DEVICE_INFO_KIND_DIMM:
Eric Blake32bafa82016-03-17 16:48:37 -0600653 node_mem[value->u.dimm.data->node] += value->u.dimm.data->size;
zhanghailiang5b009e42014-11-04 19:49:30 +0800654 break;
655 default:
656 break;
657 }
658 }
659 }
660 qapi_free_MemoryDeviceInfoList(info_list);
661}
662
663void query_numa_node_mem(uint64_t node_mem[])
664{
665 int i;
666
667 if (nb_numa_nodes <= 0) {
668 return;
669 }
670
671 numa_stat_memory_devices(node_mem);
672 for (i = 0; i < nb_numa_nodes; i++) {
673 node_mem[i] += numa_info[i].node_mem;
674 }
675}
676
Hu Tao76b5d852014-06-16 18:05:41 +0800677static int query_memdev(Object *obj, void *opaque)
678{
679 MemdevList **list = opaque;
Chen Fanb0e90182014-08-18 14:46:33 +0800680 MemdevList *m = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800681
682 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
Chen Fanb0e90182014-08-18 14:46:33 +0800683 m = g_malloc0(sizeof(*m));
Hu Tao76b5d852014-06-16 18:05:41 +0800684
685 m->value = g_malloc0(sizeof(*m->value));
686
Igor Mammedove1ff3c62017-01-10 13:53:15 +0100687 m->value->id = object_property_get_str(obj, "id", NULL);
688 m->value->has_id = !!m->value->id;
689
Hu Tao76b5d852014-06-16 18:05:41 +0800690 m->value->size = object_property_get_int(obj, "size",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100691 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800692 m->value->merge = object_property_get_bool(obj, "merge",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100693 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800694 m->value->dump = object_property_get_bool(obj, "dump",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100695 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800696 m->value->prealloc = object_property_get_bool(obj,
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100697 "prealloc",
698 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800699 m->value->policy = object_property_get_enum(obj,
700 "policy",
Daniel P. Berrangea3590da2015-05-27 16:07:56 +0100701 "HostMemPolicy",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100702 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800703 object_property_get_uint16List(obj, "host-nodes",
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100704 &m->value->host_nodes,
705 &error_abort);
Hu Tao76b5d852014-06-16 18:05:41 +0800706
707 m->next = *list;
708 *list = m;
709 }
710
711 return 0;
Hu Tao76b5d852014-06-16 18:05:41 +0800712}
713
714MemdevList *qmp_query_memdev(Error **errp)
715{
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100716 Object *obj = object_get_objects_root();
Chen Fanecaf54a2014-08-18 14:46:35 +0800717 MemdevList *list = NULL;
Hu Tao76b5d852014-06-16 18:05:41 +0800718
Markus Armbruster2f6f8262015-11-23 09:35:31 +0100719 object_child_foreach(obj, query_memdev, &list);
Hu Tao76b5d852014-06-16 18:05:41 +0800720 return list;
Hu Tao76b5d852014-06-16 18:05:41 +0800721}
Igor Mammedov6bea1dd2016-10-05 17:51:23 +0200722
723int numa_get_node_for_cpu(int idx)
724{
725 int i;
726
Igor Mammedovcdda2012016-11-18 12:02:54 +0100727 assert(idx < max_cpus);
728
Igor Mammedov6bea1dd2016-10-05 17:51:23 +0200729 for (i = 0; i < nb_numa_nodes; i++) {
730 if (test_bit(idx, numa_info[i].node_cpu)) {
731 break;
732 }
733 }
734 return i;
735}
Paolo Bonzini0987d732016-12-21 00:31:36 +0800736
737void ram_block_notifier_add(RAMBlockNotifier *n)
738{
739 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
740}
741
742void ram_block_notifier_remove(RAMBlockNotifier *n)
743{
744 QLIST_REMOVE(n, next);
745}
746
747void ram_block_notify_add(void *host, size_t size)
748{
749 RAMBlockNotifier *notifier;
750
751 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
752 notifier->ram_block_added(notifier, host, size);
753 }
754}
755
756void ram_block_notify_remove(void *host, size_t size)
757{
758 RAMBlockNotifier *notifier;
759
760 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
761 notifier->ram_block_removed(notifier, host, size);
762 }
763}