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