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