1 /* 2 * NUMA parameter parsing routines 3 * 4 * Copyright (c) 2014 Fujitsu Ltd. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25 #include "qemu/osdep.h" 26 #include "sysemu/hostmem.h" 27 #include "sysemu/numa.h" 28 #include "sysemu/sysemu.h" 29 #include "exec/cpu-common.h" 30 #include "exec/ramlist.h" 31 #include "qemu/bitmap.h" 32 #include "qemu/error-report.h" 33 #include "qapi/error.h" 34 #include "qapi/opts-visitor.h" 35 #include "qapi/qapi-visit-machine.h" 36 #include "sysemu/qtest.h" 37 #include "hw/core/cpu.h" 38 #include "hw/mem/pc-dimm.h" 39 #include "migration/vmstate.h" 40 #include "hw/boards.h" 41 #include "hw/mem/memory-device.h" 42 #include "qemu/option.h" 43 #include "qemu/config-file.h" 44 #include "qemu/cutils.h" 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 53 static int have_memdevs; 54 static int have_mem; 55 static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one. 56 * For all nodes, nodeid < max_numa_nodeid 57 */ 58 59 static void parse_numa_node(MachineState *ms, NumaNodeOptions *node, 60 Error **errp) 61 { 62 Error *err = NULL; 63 uint16_t nodenr; 64 uint16List *cpus = NULL; 65 MachineClass *mc = MACHINE_GET_CLASS(ms); 66 unsigned int max_cpus = ms->smp.max_cpus; 67 NodeInfo *numa_info = ms->numa_state->nodes; 68 69 if (node->has_nodeid) { 70 nodenr = node->nodeid; 71 } else { 72 nodenr = ms->numa_state->num_nodes; 73 } 74 75 if (nodenr >= MAX_NODES) { 76 error_setg(errp, "Max number of NUMA nodes reached: %" 77 PRIu16 "", nodenr); 78 return; 79 } 80 81 if (numa_info[nodenr].present) { 82 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr); 83 return; 84 } 85 86 if (!mc->cpu_index_to_instance_props || !mc->get_default_cpu_node_id) { 87 error_setg(errp, "NUMA is not supported by this machine-type"); 88 return; 89 } 90 for (cpus = node->cpus; cpus; cpus = cpus->next) { 91 CpuInstanceProperties props; 92 if (cpus->value >= max_cpus) { 93 error_setg(errp, 94 "CPU index (%" PRIu16 ")" 95 " should be smaller than maxcpus (%d)", 96 cpus->value, max_cpus); 97 return; 98 } 99 props = mc->cpu_index_to_instance_props(ms, cpus->value); 100 props.node_id = nodenr; 101 props.has_node_id = true; 102 machine_set_cpu_numa_node(ms, &props, &err); 103 if (err) { 104 error_propagate(errp, err); 105 return; 106 } 107 } 108 109 have_memdevs = have_memdevs ? : node->has_memdev; 110 have_mem = have_mem ? : node->has_mem; 111 if ((node->has_mem && have_memdevs) || (node->has_memdev && have_mem)) { 112 error_setg(errp, "numa configuration should use either mem= or memdev=," 113 "mixing both is not allowed"); 114 return; 115 } 116 117 if (node->has_mem) { 118 numa_info[nodenr].node_mem = node->mem; 119 if (!qtest_enabled()) { 120 warn_report("Parameter -numa node,mem is deprecated," 121 " use -numa node,memdev instead"); 122 } 123 } 124 if (node->has_memdev) { 125 Object *o; 126 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL); 127 if (!o) { 128 error_setg(errp, "memdev=%s is ambiguous", node->memdev); 129 return; 130 } 131 132 object_ref(o); 133 numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL); 134 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o); 135 } 136 137 /* 138 * If not set the initiator, set it to MAX_NODES. And if 139 * HMAT is enabled and this node has no cpus, QEMU will raise error. 140 */ 141 numa_info[nodenr].initiator = MAX_NODES; 142 if (node->has_initiator) { 143 if (!ms->numa_state->hmat_enabled) { 144 error_setg(errp, "ACPI Heterogeneous Memory Attribute Table " 145 "(HMAT) is disabled, enable it with -machine hmat=on " 146 "before using any of hmat specific options"); 147 return; 148 } 149 150 if (node->initiator >= MAX_NODES) { 151 error_report("The initiator id %" PRIu16 " expects an integer " 152 "between 0 and %d", node->initiator, 153 MAX_NODES - 1); 154 return; 155 } 156 157 numa_info[nodenr].initiator = node->initiator; 158 } 159 numa_info[nodenr].present = true; 160 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1); 161 ms->numa_state->num_nodes++; 162 } 163 164 static 165 void parse_numa_distance(MachineState *ms, NumaDistOptions *dist, Error **errp) 166 { 167 uint16_t src = dist->src; 168 uint16_t dst = dist->dst; 169 uint8_t val = dist->val; 170 NodeInfo *numa_info = ms->numa_state->nodes; 171 172 if (src >= MAX_NODES || dst >= MAX_NODES) { 173 error_setg(errp, "Parameter '%s' expects an integer between 0 and %d", 174 src >= MAX_NODES ? "src" : "dst", MAX_NODES - 1); 175 return; 176 } 177 178 if (!numa_info[src].present || !numa_info[dst].present) { 179 error_setg(errp, "Source/Destination NUMA node is missing. " 180 "Please use '-numa node' option to declare it first."); 181 return; 182 } 183 184 if (val < NUMA_DISTANCE_MIN) { 185 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, " 186 "it shouldn't be less than %d.", 187 val, NUMA_DISTANCE_MIN); 188 return; 189 } 190 191 if (src == dst && val != NUMA_DISTANCE_MIN) { 192 error_setg(errp, "Local distance of node %d should be %d.", 193 src, NUMA_DISTANCE_MIN); 194 return; 195 } 196 197 numa_info[src].distance[dst] = val; 198 ms->numa_state->have_numa_distance = true; 199 } 200 201 void set_numa_options(MachineState *ms, NumaOptions *object, Error **errp) 202 { 203 Error *err = NULL; 204 MachineClass *mc = MACHINE_GET_CLASS(ms); 205 206 if (!mc->numa_mem_supported) { 207 error_setg(errp, "NUMA is not supported by this machine-type"); 208 goto end; 209 } 210 211 switch (object->type) { 212 case NUMA_OPTIONS_TYPE_NODE: 213 parse_numa_node(ms, &object->u.node, &err); 214 if (err) { 215 goto end; 216 } 217 break; 218 case NUMA_OPTIONS_TYPE_DIST: 219 parse_numa_distance(ms, &object->u.dist, &err); 220 if (err) { 221 goto end; 222 } 223 break; 224 case NUMA_OPTIONS_TYPE_CPU: 225 if (!object->u.cpu.has_node_id) { 226 error_setg(&err, "Missing mandatory node-id property"); 227 goto end; 228 } 229 if (!ms->numa_state->nodes[object->u.cpu.node_id].present) { 230 error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be " 231 "defined with -numa node,nodeid=ID before it's used with " 232 "-numa cpu,node-id=ID", object->u.cpu.node_id); 233 goto end; 234 } 235 236 machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu), 237 &err); 238 break; 239 default: 240 abort(); 241 } 242 243 end: 244 error_propagate(errp, err); 245 } 246 247 static int parse_numa(void *opaque, QemuOpts *opts, Error **errp) 248 { 249 NumaOptions *object = NULL; 250 MachineState *ms = MACHINE(opaque); 251 Error *err = NULL; 252 Visitor *v = opts_visitor_new(opts); 253 254 visit_type_NumaOptions(v, NULL, &object, &err); 255 visit_free(v); 256 if (err) { 257 goto end; 258 } 259 260 /* Fix up legacy suffix-less format */ 261 if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) { 262 const char *mem_str = qemu_opt_get(opts, "mem"); 263 qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem); 264 } 265 266 set_numa_options(ms, object, &err); 267 268 end: 269 qapi_free_NumaOptions(object); 270 if (err) { 271 error_propagate(errp, err); 272 return -1; 273 } 274 275 return 0; 276 } 277 278 /* If all node pair distances are symmetric, then only distances 279 * in one direction are enough. If there is even one asymmetric 280 * pair, though, then all distances must be provided. The 281 * distance from a node to itself is always NUMA_DISTANCE_MIN, 282 * so providing it is never necessary. 283 */ 284 static void validate_numa_distance(MachineState *ms) 285 { 286 int src, dst; 287 bool is_asymmetrical = false; 288 int nb_numa_nodes = ms->numa_state->num_nodes; 289 NodeInfo *numa_info = ms->numa_state->nodes; 290 291 for (src = 0; src < nb_numa_nodes; src++) { 292 for (dst = src; dst < nb_numa_nodes; dst++) { 293 if (numa_info[src].distance[dst] == 0 && 294 numa_info[dst].distance[src] == 0) { 295 if (src != dst) { 296 error_report("The distance between node %d and %d is " 297 "missing, at least one distance value " 298 "between each nodes should be provided.", 299 src, dst); 300 exit(EXIT_FAILURE); 301 } 302 } 303 304 if (numa_info[src].distance[dst] != 0 && 305 numa_info[dst].distance[src] != 0 && 306 numa_info[src].distance[dst] != 307 numa_info[dst].distance[src]) { 308 is_asymmetrical = true; 309 } 310 } 311 } 312 313 if (is_asymmetrical) { 314 for (src = 0; src < nb_numa_nodes; src++) { 315 for (dst = 0; dst < nb_numa_nodes; dst++) { 316 if (src != dst && numa_info[src].distance[dst] == 0) { 317 error_report("At least one asymmetrical pair of " 318 "distances is given, please provide distances " 319 "for both directions of all node pairs."); 320 exit(EXIT_FAILURE); 321 } 322 } 323 } 324 } 325 } 326 327 static void complete_init_numa_distance(MachineState *ms) 328 { 329 int src, dst; 330 NodeInfo *numa_info = ms->numa_state->nodes; 331 332 /* Fixup NUMA distance by symmetric policy because if it is an 333 * asymmetric distance table, it should be a complete table and 334 * there would not be any missing distance except local node, which 335 * is verified by validate_numa_distance above. 336 */ 337 for (src = 0; src < ms->numa_state->num_nodes; src++) { 338 for (dst = 0; dst < ms->numa_state->num_nodes; dst++) { 339 if (numa_info[src].distance[dst] == 0) { 340 if (src == dst) { 341 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN; 342 } else { 343 numa_info[src].distance[dst] = numa_info[dst].distance[src]; 344 } 345 } 346 } 347 } 348 } 349 350 void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes, 351 int nb_nodes, ram_addr_t size) 352 { 353 int i; 354 uint64_t usedmem = 0; 355 356 /* Align each node according to the alignment 357 * requirements of the machine class 358 */ 359 360 for (i = 0; i < nb_nodes - 1; i++) { 361 nodes[i].node_mem = (size / nb_nodes) & 362 ~((1 << mc->numa_mem_align_shift) - 1); 363 usedmem += nodes[i].node_mem; 364 } 365 nodes[i].node_mem = size - usedmem; 366 } 367 368 void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes, 369 int nb_nodes, ram_addr_t size) 370 { 371 int i; 372 uint64_t usedmem = 0, node_mem; 373 uint64_t granularity = size / nb_nodes; 374 uint64_t propagate = 0; 375 376 for (i = 0; i < nb_nodes - 1; i++) { 377 node_mem = (granularity + propagate) & 378 ~((1 << mc->numa_mem_align_shift) - 1); 379 propagate = granularity + propagate - node_mem; 380 nodes[i].node_mem = node_mem; 381 usedmem += node_mem; 382 } 383 nodes[i].node_mem = size - usedmem; 384 } 385 386 void numa_complete_configuration(MachineState *ms) 387 { 388 int i; 389 MachineClass *mc = MACHINE_GET_CLASS(ms); 390 NodeInfo *numa_info = ms->numa_state->nodes; 391 392 /* 393 * If memory hotplug is enabled (slots > 0) but without '-numa' 394 * options explicitly on CLI, guestes will break. 395 * 396 * Windows: won't enable memory hotplug without SRAT table at all 397 * 398 * Linux: if QEMU is started with initial memory all below 4Gb 399 * and no SRAT table present, guest kernel will use nommu DMA ops, 400 * which breaks 32bit hw drivers when memory is hotplugged and 401 * guest tries to use it with that drivers. 402 * 403 * Enable NUMA implicitly by adding a new NUMA node automatically. 404 * 405 * Or if MachineClass::auto_enable_numa is true and no NUMA nodes, 406 * assume there is just one node with whole RAM. 407 */ 408 if (ms->numa_state->num_nodes == 0 && 409 ((ms->ram_slots > 0 && 410 mc->auto_enable_numa_with_memhp) || 411 mc->auto_enable_numa)) { 412 NumaNodeOptions node = { }; 413 parse_numa_node(ms, &node, &error_abort); 414 numa_info[0].node_mem = ram_size; 415 } 416 417 assert(max_numa_nodeid <= MAX_NODES); 418 419 /* No support for sparse NUMA node IDs yet: */ 420 for (i = max_numa_nodeid - 1; i >= 0; i--) { 421 /* Report large node IDs first, to make mistakes easier to spot */ 422 if (!numa_info[i].present) { 423 error_report("numa: Node ID missing: %d", i); 424 exit(1); 425 } 426 } 427 428 /* This must be always true if all nodes are present: */ 429 assert(ms->numa_state->num_nodes == max_numa_nodeid); 430 431 if (ms->numa_state->num_nodes > 0) { 432 uint64_t numa_total; 433 434 if (ms->numa_state->num_nodes > MAX_NODES) { 435 ms->numa_state->num_nodes = MAX_NODES; 436 } 437 438 /* If no memory size is given for any node, assume the default case 439 * and distribute the available memory equally across all nodes 440 */ 441 for (i = 0; i < ms->numa_state->num_nodes; i++) { 442 if (numa_info[i].node_mem != 0) { 443 break; 444 } 445 } 446 if (i == ms->numa_state->num_nodes) { 447 assert(mc->numa_auto_assign_ram); 448 mc->numa_auto_assign_ram(mc, numa_info, 449 ms->numa_state->num_nodes, ram_size); 450 if (!qtest_enabled()) { 451 warn_report("Default splitting of RAM between nodes is deprecated," 452 " Use '-numa node,memdev' to explictly define RAM" 453 " allocation per node"); 454 } 455 } 456 457 numa_total = 0; 458 for (i = 0; i < ms->numa_state->num_nodes; i++) { 459 numa_total += numa_info[i].node_mem; 460 } 461 if (numa_total != ram_size) { 462 error_report("total memory for NUMA nodes (0x%" PRIx64 ")" 463 " should equal RAM size (0x" RAM_ADDR_FMT ")", 464 numa_total, ram_size); 465 exit(1); 466 } 467 468 /* QEMU needs at least all unique node pair distances to build 469 * the whole NUMA distance table. QEMU treats the distance table 470 * as symmetric by default, i.e. distance A->B == distance B->A. 471 * Thus, QEMU is able to complete the distance table 472 * initialization even though only distance A->B is provided and 473 * distance B->A is not. QEMU knows the distance of a node to 474 * itself is always 10, so A->A distances may be omitted. When 475 * the distances of two nodes of a pair differ, i.e. distance 476 * A->B != distance B->A, then that means the distance table is 477 * asymmetric. In this case, the distances for both directions 478 * of all node pairs are required. 479 */ 480 if (ms->numa_state->have_numa_distance) { 481 /* Validate enough NUMA distance information was provided. */ 482 validate_numa_distance(ms); 483 484 /* Validation succeeded, now fill in any missing distances. */ 485 complete_init_numa_distance(ms); 486 } 487 } 488 } 489 490 void parse_numa_opts(MachineState *ms) 491 { 492 qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, &error_fatal); 493 } 494 495 void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp) 496 { 497 int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort); 498 499 if (node_id == CPU_UNSET_NUMA_NODE_ID) { 500 /* due to bug in libvirt, it doesn't pass node-id from props on 501 * device_add as expected, so we have to fix it up here */ 502 if (slot->props.has_node_id) { 503 object_property_set_int(OBJECT(dev), slot->props.node_id, 504 "node-id", errp); 505 } 506 } else if (node_id != slot->props.node_id) { 507 error_setg(errp, "invalid node-id, must be %"PRId64, 508 slot->props.node_id); 509 } 510 } 511 512 static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner, 513 const char *name, 514 uint64_t ram_size) 515 { 516 if (mem_path) { 517 #ifdef __linux__ 518 Error *err = NULL; 519 memory_region_init_ram_from_file(mr, owner, name, ram_size, 0, 0, 520 mem_path, &err); 521 if (err) { 522 error_report_err(err); 523 if (mem_prealloc) { 524 exit(1); 525 } 526 warn_report("falling back to regular RAM allocation"); 527 error_printf("This is deprecated. Make sure that -mem-path " 528 " specified path has sufficient resources to allocate" 529 " -m specified RAM amount\n"); 530 /* Legacy behavior: if allocation failed, fall back to 531 * regular RAM allocation. 532 */ 533 mem_path = NULL; 534 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal); 535 } 536 #else 537 fprintf(stderr, "-mem-path not supported on this host\n"); 538 exit(1); 539 #endif 540 } else { 541 memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal); 542 } 543 vmstate_register_ram_global(mr); 544 } 545 546 void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner, 547 const char *name, 548 uint64_t ram_size) 549 { 550 uint64_t addr = 0; 551 int i; 552 MachineState *ms = MACHINE(qdev_get_machine()); 553 554 if (ms->numa_state == NULL || 555 ms->numa_state->num_nodes == 0 || !have_memdevs) { 556 allocate_system_memory_nonnuma(mr, owner, name, ram_size); 557 return; 558 } 559 560 memory_region_init(mr, owner, name, ram_size); 561 for (i = 0; i < ms->numa_state->num_nodes; i++) { 562 uint64_t size = ms->numa_state->nodes[i].node_mem; 563 HostMemoryBackend *backend = ms->numa_state->nodes[i].node_memdev; 564 if (!backend) { 565 continue; 566 } 567 MemoryRegion *seg = host_memory_backend_get_memory(backend); 568 569 if (memory_region_is_mapped(seg)) { 570 char *path = object_get_canonical_path_component(OBJECT(backend)); 571 error_report("memory backend %s is used multiple times. Each " 572 "-numa option must use a different memdev value.", 573 path); 574 g_free(path); 575 exit(1); 576 } 577 578 host_memory_backend_set_mapped(backend, true); 579 memory_region_add_subregion(mr, addr, seg); 580 vmstate_register_ram_global(seg); 581 addr += size; 582 } 583 } 584 585 static void numa_stat_memory_devices(NumaNodeMem node_mem[]) 586 { 587 MemoryDeviceInfoList *info_list = qmp_memory_device_list(); 588 MemoryDeviceInfoList *info; 589 PCDIMMDeviceInfo *pcdimm_info; 590 VirtioPMEMDeviceInfo *vpi; 591 592 for (info = info_list; info; info = info->next) { 593 MemoryDeviceInfo *value = info->value; 594 595 if (value) { 596 switch (value->type) { 597 case MEMORY_DEVICE_INFO_KIND_DIMM: 598 case MEMORY_DEVICE_INFO_KIND_NVDIMM: 599 pcdimm_info = value->type == MEMORY_DEVICE_INFO_KIND_DIMM ? 600 value->u.dimm.data : value->u.nvdimm.data; 601 node_mem[pcdimm_info->node].node_mem += pcdimm_info->size; 602 node_mem[pcdimm_info->node].node_plugged_mem += 603 pcdimm_info->size; 604 break; 605 case MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM: 606 vpi = value->u.virtio_pmem.data; 607 /* TODO: once we support numa, assign to right node */ 608 node_mem[0].node_mem += vpi->size; 609 node_mem[0].node_plugged_mem += vpi->size; 610 break; 611 default: 612 g_assert_not_reached(); 613 } 614 } 615 } 616 qapi_free_MemoryDeviceInfoList(info_list); 617 } 618 619 void query_numa_node_mem(NumaNodeMem node_mem[], MachineState *ms) 620 { 621 int i; 622 623 if (ms->numa_state == NULL || ms->numa_state->num_nodes <= 0) { 624 return; 625 } 626 627 numa_stat_memory_devices(node_mem); 628 for (i = 0; i < ms->numa_state->num_nodes; i++) { 629 node_mem[i].node_mem += ms->numa_state->nodes[i].node_mem; 630 } 631 } 632 633 void ram_block_notifier_add(RAMBlockNotifier *n) 634 { 635 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next); 636 } 637 638 void ram_block_notifier_remove(RAMBlockNotifier *n) 639 { 640 QLIST_REMOVE(n, next); 641 } 642 643 void ram_block_notify_add(void *host, size_t size) 644 { 645 RAMBlockNotifier *notifier; 646 647 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { 648 notifier->ram_block_added(notifier, host, size); 649 } 650 } 651 652 void ram_block_notify_remove(void *host, size_t size) 653 { 654 RAMBlockNotifier *notifier; 655 656 QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { 657 notifier->ram_block_removed(notifier, host, size); 658 } 659 } 660