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 "qemu/units.h" 27 #include "sysemu/hostmem.h" 28 #include "sysemu/numa.h" 29 #include "exec/cpu-common.h" 30 #include "exec/ramlist.h" 31 #include "qemu/error-report.h" 32 #include "qapi/error.h" 33 #include "qapi/opts-visitor.h" 34 #include "qapi/qapi-visit-machine.h" 35 #include "sysemu/qtest.h" 36 #include "hw/core/cpu.h" 37 #include "hw/mem/pc-dimm.h" 38 #include "hw/boards.h" 39 #include "hw/mem/memory-device.h" 40 #include "qemu/option.h" 41 #include "qemu/config-file.h" 42 #include "qemu/cutils.h" 43 44 QemuOptsList qemu_numa_opts = { 45 .name = "numa", 46 .implied_opt_name = "type", 47 .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head), 48 .desc = { { 0 } } /* validated with OptsVisitor */ 49 }; 50 51 static int have_memdevs; 52 bool numa_uses_legacy_mem(void) 53 { 54 return !have_memdevs; 55 } 56 57 static int have_mem; 58 static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one. 59 * For all nodes, nodeid < max_numa_nodeid 60 */ 61 62 static void parse_numa_node(MachineState *ms, NumaNodeOptions *node, 63 Error **errp) 64 { 65 Error *err = NULL; 66 uint16_t nodenr; 67 uint16List *cpus = NULL; 68 MachineClass *mc = MACHINE_GET_CLASS(ms); 69 unsigned int max_cpus = ms->smp.max_cpus; 70 NodeInfo *numa_info = ms->numa_state->nodes; 71 72 if (node->has_nodeid) { 73 nodenr = node->nodeid; 74 } else { 75 nodenr = ms->numa_state->num_nodes; 76 } 77 78 if (nodenr >= MAX_NODES) { 79 error_setg(errp, "Max number of NUMA nodes reached: %" 80 PRIu16 "", nodenr); 81 return; 82 } 83 84 if (numa_info[nodenr].present) { 85 error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr); 86 return; 87 } 88 89 /* 90 * If not set the initiator, set it to MAX_NODES. And if 91 * HMAT is enabled and this node has no cpus, QEMU will raise error. 92 */ 93 numa_info[nodenr].initiator = MAX_NODES; 94 if (node->has_initiator) { 95 if (!ms->numa_state->hmat_enabled) { 96 error_setg(errp, "ACPI Heterogeneous Memory Attribute Table " 97 "(HMAT) is disabled, enable it with -machine hmat=on " 98 "before using any of hmat specific options"); 99 return; 100 } 101 102 if (node->initiator >= MAX_NODES) { 103 error_report("The initiator id %" PRIu16 " expects an integer " 104 "between 0 and %d", node->initiator, 105 MAX_NODES - 1); 106 return; 107 } 108 109 numa_info[nodenr].initiator = node->initiator; 110 } 111 112 for (cpus = node->cpus; cpus; cpus = cpus->next) { 113 CpuInstanceProperties props; 114 if (cpus->value >= max_cpus) { 115 error_setg(errp, 116 "CPU index (%" PRIu16 ")" 117 " should be smaller than maxcpus (%d)", 118 cpus->value, max_cpus); 119 return; 120 } 121 props = mc->cpu_index_to_instance_props(ms, cpus->value); 122 props.node_id = nodenr; 123 props.has_node_id = true; 124 machine_set_cpu_numa_node(ms, &props, &err); 125 if (err) { 126 error_propagate(errp, err); 127 return; 128 } 129 } 130 131 have_memdevs = have_memdevs || node->memdev; 132 have_mem = have_mem || node->has_mem; 133 if ((node->has_mem && have_memdevs) || (node->memdev && have_mem)) { 134 error_setg(errp, "numa configuration should use either mem= or memdev=," 135 "mixing both is not allowed"); 136 return; 137 } 138 139 if (node->has_mem) { 140 if (!mc->numa_mem_supported) { 141 error_setg(errp, "Parameter -numa node,mem is not supported by this" 142 " machine type"); 143 error_append_hint(errp, "Use -numa node,memdev instead\n"); 144 return; 145 } 146 147 numa_info[nodenr].node_mem = node->mem; 148 if (!qtest_enabled()) { 149 warn_report("Parameter -numa node,mem is deprecated," 150 " use -numa node,memdev instead"); 151 } 152 } 153 if (node->memdev) { 154 Object *o; 155 o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL); 156 if (!o) { 157 error_setg(errp, "memdev=%s is ambiguous", node->memdev); 158 return; 159 } 160 161 object_ref(o); 162 numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL); 163 numa_info[nodenr].node_memdev = MEMORY_BACKEND(o); 164 } 165 166 numa_info[nodenr].present = true; 167 max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1); 168 ms->numa_state->num_nodes++; 169 } 170 171 static 172 void parse_numa_distance(MachineState *ms, NumaDistOptions *dist, Error **errp) 173 { 174 uint16_t src = dist->src; 175 uint16_t dst = dist->dst; 176 uint8_t val = dist->val; 177 NodeInfo *numa_info = ms->numa_state->nodes; 178 179 if (src >= MAX_NODES || dst >= MAX_NODES) { 180 error_setg(errp, "Parameter '%s' expects an integer between 0 and %d", 181 src >= MAX_NODES ? "src" : "dst", MAX_NODES - 1); 182 return; 183 } 184 185 if (!numa_info[src].present || !numa_info[dst].present) { 186 error_setg(errp, "Source/Destination NUMA node is missing. " 187 "Please use '-numa node' option to declare it first."); 188 return; 189 } 190 191 if (val < NUMA_DISTANCE_MIN) { 192 error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, " 193 "it shouldn't be less than %d.", 194 val, NUMA_DISTANCE_MIN); 195 return; 196 } 197 198 if (src == dst && val != NUMA_DISTANCE_MIN) { 199 error_setg(errp, "Local distance of node %d should be %d.", 200 src, NUMA_DISTANCE_MIN); 201 return; 202 } 203 204 numa_info[src].distance[dst] = val; 205 ms->numa_state->have_numa_distance = true; 206 } 207 208 void parse_numa_hmat_lb(NumaState *numa_state, NumaHmatLBOptions *node, 209 Error **errp) 210 { 211 int i, first_bit, last_bit; 212 uint64_t max_entry, temp_base, bitmap_copy; 213 NodeInfo *numa_info = numa_state->nodes; 214 HMAT_LB_Info *hmat_lb = 215 numa_state->hmat_lb[node->hierarchy][node->data_type]; 216 HMAT_LB_Data lb_data = {}; 217 HMAT_LB_Data *lb_temp; 218 219 /* Error checking */ 220 if (node->initiator > numa_state->num_nodes) { 221 error_setg(errp, "Invalid initiator=%d, it should be less than %d", 222 node->initiator, numa_state->num_nodes); 223 return; 224 } 225 if (node->target > numa_state->num_nodes) { 226 error_setg(errp, "Invalid target=%d, it should be less than %d", 227 node->target, numa_state->num_nodes); 228 return; 229 } 230 if (!numa_info[node->initiator].has_cpu) { 231 error_setg(errp, "Invalid initiator=%d, it isn't an " 232 "initiator proximity domain", node->initiator); 233 return; 234 } 235 if (!numa_info[node->target].present) { 236 error_setg(errp, "The target=%d should point to an existing node", 237 node->target); 238 return; 239 } 240 241 if (!hmat_lb) { 242 hmat_lb = g_malloc0(sizeof(*hmat_lb)); 243 numa_state->hmat_lb[node->hierarchy][node->data_type] = hmat_lb; 244 hmat_lb->list = g_array_new(false, true, sizeof(HMAT_LB_Data)); 245 } 246 hmat_lb->hierarchy = node->hierarchy; 247 hmat_lb->data_type = node->data_type; 248 lb_data.initiator = node->initiator; 249 lb_data.target = node->target; 250 251 if (node->data_type <= HMATLB_DATA_TYPE_WRITE_LATENCY) { 252 /* Input latency data */ 253 254 if (!node->has_latency) { 255 error_setg(errp, "Missing 'latency' option"); 256 return; 257 } 258 if (node->has_bandwidth) { 259 error_setg(errp, "Invalid option 'bandwidth' since " 260 "the data type is latency"); 261 return; 262 } 263 264 /* Detect duplicate configuration */ 265 for (i = 0; i < hmat_lb->list->len; i++) { 266 lb_temp = &g_array_index(hmat_lb->list, HMAT_LB_Data, i); 267 268 if (node->initiator == lb_temp->initiator && 269 node->target == lb_temp->target) { 270 error_setg(errp, "Duplicate configuration of the latency for " 271 "initiator=%d and target=%d", node->initiator, 272 node->target); 273 return; 274 } 275 } 276 277 hmat_lb->base = hmat_lb->base ? hmat_lb->base : UINT64_MAX; 278 279 if (node->latency) { 280 /* Calculate the temporary base and compressed latency */ 281 max_entry = node->latency; 282 temp_base = 1; 283 while (QEMU_IS_ALIGNED(max_entry, 10)) { 284 max_entry /= 10; 285 temp_base *= 10; 286 } 287 288 /* Calculate the max compressed latency */ 289 temp_base = MIN(hmat_lb->base, temp_base); 290 max_entry = node->latency / hmat_lb->base; 291 max_entry = MAX(hmat_lb->range_bitmap, max_entry); 292 293 /* 294 * For latency hmat_lb->range_bitmap record the max compressed 295 * latency which should be less than 0xFFFF (UINT16_MAX) 296 */ 297 if (max_entry >= UINT16_MAX) { 298 error_setg(errp, "Latency %" PRIu64 " between initiator=%d and " 299 "target=%d should not differ from previously entered " 300 "min or max values on more than %d", node->latency, 301 node->initiator, node->target, UINT16_MAX - 1); 302 return; 303 } else { 304 hmat_lb->base = temp_base; 305 hmat_lb->range_bitmap = max_entry; 306 } 307 308 /* 309 * Set lb_info_provided bit 0 as 1, 310 * latency information is provided 311 */ 312 numa_info[node->target].lb_info_provided |= BIT(0); 313 } 314 lb_data.data = node->latency; 315 } else if (node->data_type >= HMATLB_DATA_TYPE_ACCESS_BANDWIDTH) { 316 /* Input bandwidth data */ 317 if (!node->has_bandwidth) { 318 error_setg(errp, "Missing 'bandwidth' option"); 319 return; 320 } 321 if (node->has_latency) { 322 error_setg(errp, "Invalid option 'latency' since " 323 "the data type is bandwidth"); 324 return; 325 } 326 if (!QEMU_IS_ALIGNED(node->bandwidth, MiB)) { 327 error_setg(errp, "Bandwidth %" PRIu64 " between initiator=%d and " 328 "target=%d should be 1MB aligned", node->bandwidth, 329 node->initiator, node->target); 330 return; 331 } 332 333 /* Detect duplicate configuration */ 334 for (i = 0; i < hmat_lb->list->len; i++) { 335 lb_temp = &g_array_index(hmat_lb->list, HMAT_LB_Data, i); 336 337 if (node->initiator == lb_temp->initiator && 338 node->target == lb_temp->target) { 339 error_setg(errp, "Duplicate configuration of the bandwidth for " 340 "initiator=%d and target=%d", node->initiator, 341 node->target); 342 return; 343 } 344 } 345 346 hmat_lb->base = hmat_lb->base ? hmat_lb->base : 1; 347 348 if (node->bandwidth) { 349 /* Keep bitmap unchanged when bandwidth out of range */ 350 bitmap_copy = hmat_lb->range_bitmap; 351 bitmap_copy |= node->bandwidth; 352 first_bit = ctz64(bitmap_copy); 353 temp_base = UINT64_C(1) << first_bit; 354 max_entry = node->bandwidth / temp_base; 355 last_bit = 64 - clz64(bitmap_copy); 356 357 /* 358 * For bandwidth, first_bit record the base unit of bandwidth bits, 359 * last_bit record the last bit of the max bandwidth. The max 360 * compressed bandwidth should be less than 0xFFFF (UINT16_MAX) 361 */ 362 if ((last_bit - first_bit) > UINT16_BITS || 363 max_entry >= UINT16_MAX) { 364 error_setg(errp, "Bandwidth %" PRIu64 " between initiator=%d " 365 "and target=%d should not differ from previously " 366 "entered values on more than %d", node->bandwidth, 367 node->initiator, node->target, UINT16_MAX - 1); 368 return; 369 } else { 370 hmat_lb->base = temp_base; 371 hmat_lb->range_bitmap = bitmap_copy; 372 } 373 374 /* 375 * Set lb_info_provided bit 1 as 1, 376 * bandwidth information is provided 377 */ 378 numa_info[node->target].lb_info_provided |= BIT(1); 379 } 380 lb_data.data = node->bandwidth; 381 } else { 382 assert(0); 383 } 384 385 g_array_append_val(hmat_lb->list, lb_data); 386 } 387 388 void parse_numa_hmat_cache(MachineState *ms, NumaHmatCacheOptions *node, 389 Error **errp) 390 { 391 int nb_numa_nodes = ms->numa_state->num_nodes; 392 NodeInfo *numa_info = ms->numa_state->nodes; 393 NumaHmatCacheOptions *hmat_cache = NULL; 394 395 if (node->node_id >= nb_numa_nodes) { 396 error_setg(errp, "Invalid node-id=%" PRIu32 ", it should be less " 397 "than %d", node->node_id, nb_numa_nodes); 398 return; 399 } 400 401 if (numa_info[node->node_id].lb_info_provided != (BIT(0) | BIT(1))) { 402 error_setg(errp, "The latency and bandwidth information of " 403 "node-id=%" PRIu32 " should be provided before memory side " 404 "cache attributes", node->node_id); 405 return; 406 } 407 408 if (node->level < 1 || node->level >= HMAT_LB_LEVELS) { 409 error_setg(errp, "Invalid level=%" PRIu8 ", it should be larger than 0 " 410 "and less than or equal to %d", node->level, 411 HMAT_LB_LEVELS - 1); 412 return; 413 } 414 415 assert(node->associativity < HMAT_CACHE_ASSOCIATIVITY__MAX); 416 assert(node->policy < HMAT_CACHE_WRITE_POLICY__MAX); 417 if (ms->numa_state->hmat_cache[node->node_id][node->level]) { 418 error_setg(errp, "Duplicate configuration of the side cache for " 419 "node-id=%" PRIu32 " and level=%" PRIu8, 420 node->node_id, node->level); 421 return; 422 } 423 424 if ((node->level > 1) && 425 ms->numa_state->hmat_cache[node->node_id][node->level - 1] == NULL) { 426 error_setg(errp, "Cache level=%u shall be defined first", 427 node->level - 1); 428 return; 429 } 430 431 if ((node->level > 1) && 432 (node->size <= 433 ms->numa_state->hmat_cache[node->node_id][node->level - 1]->size)) { 434 error_setg(errp, "Invalid size=%" PRIu64 ", the size of level=%" PRIu8 435 " should be larger than the size(%" PRIu64 ") of " 436 "level=%u", node->size, node->level, 437 ms->numa_state->hmat_cache[node->node_id] 438 [node->level - 1]->size, 439 node->level - 1); 440 return; 441 } 442 443 if ((node->level < HMAT_LB_LEVELS - 1) && 444 ms->numa_state->hmat_cache[node->node_id][node->level + 1] && 445 (node->size >= 446 ms->numa_state->hmat_cache[node->node_id][node->level + 1]->size)) { 447 error_setg(errp, "Invalid size=%" PRIu64 ", the size of level=%" PRIu8 448 " should be less than the size(%" PRIu64 ") of " 449 "level=%u", node->size, node->level, 450 ms->numa_state->hmat_cache[node->node_id] 451 [node->level + 1]->size, 452 node->level + 1); 453 return; 454 } 455 456 hmat_cache = g_malloc0(sizeof(*hmat_cache)); 457 memcpy(hmat_cache, node, sizeof(*hmat_cache)); 458 ms->numa_state->hmat_cache[node->node_id][node->level] = hmat_cache; 459 } 460 461 void set_numa_options(MachineState *ms, NumaOptions *object, Error **errp) 462 { 463 if (!ms->numa_state) { 464 error_setg(errp, "NUMA is not supported by this machine-type"); 465 return; 466 } 467 468 switch (object->type) { 469 case NUMA_OPTIONS_TYPE_NODE: 470 parse_numa_node(ms, &object->u.node, errp); 471 break; 472 case NUMA_OPTIONS_TYPE_DIST: 473 parse_numa_distance(ms, &object->u.dist, errp); 474 break; 475 case NUMA_OPTIONS_TYPE_CPU: 476 if (!object->u.cpu.has_node_id) { 477 error_setg(errp, "Missing mandatory node-id property"); 478 return; 479 } 480 if (!ms->numa_state->nodes[object->u.cpu.node_id].present) { 481 error_setg(errp, "Invalid node-id=%" PRId64 ", NUMA node must be " 482 "defined with -numa node,nodeid=ID before it's used with " 483 "-numa cpu,node-id=ID", object->u.cpu.node_id); 484 return; 485 } 486 487 machine_set_cpu_numa_node(ms, 488 qapi_NumaCpuOptions_base(&object->u.cpu), 489 errp); 490 break; 491 case NUMA_OPTIONS_TYPE_HMAT_LB: 492 if (!ms->numa_state->hmat_enabled) { 493 error_setg(errp, "ACPI Heterogeneous Memory Attribute Table " 494 "(HMAT) is disabled, enable it with -machine hmat=on " 495 "before using any of hmat specific options"); 496 return; 497 } 498 499 parse_numa_hmat_lb(ms->numa_state, &object->u.hmat_lb, errp); 500 break; 501 case NUMA_OPTIONS_TYPE_HMAT_CACHE: 502 if (!ms->numa_state->hmat_enabled) { 503 error_setg(errp, "ACPI Heterogeneous Memory Attribute Table " 504 "(HMAT) is disabled, enable it with -machine hmat=on " 505 "before using any of hmat specific options"); 506 return; 507 } 508 509 parse_numa_hmat_cache(ms, &object->u.hmat_cache, errp); 510 break; 511 default: 512 abort(); 513 } 514 } 515 516 static int parse_numa(void *opaque, QemuOpts *opts, Error **errp) 517 { 518 NumaOptions *object = NULL; 519 MachineState *ms = MACHINE(opaque); 520 Error *err = NULL; 521 Visitor *v = opts_visitor_new(opts); 522 523 visit_type_NumaOptions(v, NULL, &object, errp); 524 visit_free(v); 525 if (!object) { 526 return -1; 527 } 528 529 /* Fix up legacy suffix-less format */ 530 if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) { 531 const char *mem_str = qemu_opt_get(opts, "mem"); 532 int ret = qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem); 533 534 if (ret < 0) { 535 error_setg_errno(&err, -ret, "could not parse memory size '%s'", 536 mem_str); 537 } 538 } 539 540 if (!err) { 541 set_numa_options(ms, object, &err); 542 } 543 544 qapi_free_NumaOptions(object); 545 if (err) { 546 error_propagate(errp, err); 547 return -1; 548 } 549 550 return 0; 551 } 552 553 /* If all node pair distances are symmetric, then only distances 554 * in one direction are enough. If there is even one asymmetric 555 * pair, though, then all distances must be provided. The 556 * distance from a node to itself is always NUMA_DISTANCE_MIN, 557 * so providing it is never necessary. 558 */ 559 static void validate_numa_distance(MachineState *ms) 560 { 561 int src, dst; 562 bool is_asymmetrical = false; 563 int nb_numa_nodes = ms->numa_state->num_nodes; 564 NodeInfo *numa_info = ms->numa_state->nodes; 565 566 for (src = 0; src < nb_numa_nodes; src++) { 567 for (dst = src; dst < nb_numa_nodes; dst++) { 568 if (numa_info[src].distance[dst] == 0 && 569 numa_info[dst].distance[src] == 0) { 570 if (src != dst) { 571 error_report("The distance between node %d and %d is " 572 "missing, at least one distance value " 573 "between each nodes should be provided.", 574 src, dst); 575 exit(EXIT_FAILURE); 576 } 577 } 578 579 if (numa_info[src].distance[dst] != 0 && 580 numa_info[dst].distance[src] != 0 && 581 numa_info[src].distance[dst] != 582 numa_info[dst].distance[src]) { 583 is_asymmetrical = true; 584 } 585 } 586 } 587 588 if (is_asymmetrical) { 589 for (src = 0; src < nb_numa_nodes; src++) { 590 for (dst = 0; dst < nb_numa_nodes; dst++) { 591 if (src != dst && numa_info[src].distance[dst] == 0) { 592 error_report("At least one asymmetrical pair of " 593 "distances is given, please provide distances " 594 "for both directions of all node pairs."); 595 exit(EXIT_FAILURE); 596 } 597 } 598 } 599 } 600 } 601 602 static void complete_init_numa_distance(MachineState *ms) 603 { 604 int src, dst; 605 NodeInfo *numa_info = ms->numa_state->nodes; 606 607 /* Fixup NUMA distance by symmetric policy because if it is an 608 * asymmetric distance table, it should be a complete table and 609 * there would not be any missing distance except local node, which 610 * is verified by validate_numa_distance above. 611 */ 612 for (src = 0; src < ms->numa_state->num_nodes; src++) { 613 for (dst = 0; dst < ms->numa_state->num_nodes; dst++) { 614 if (numa_info[src].distance[dst] == 0) { 615 if (src == dst) { 616 numa_info[src].distance[dst] = NUMA_DISTANCE_MIN; 617 } else { 618 numa_info[src].distance[dst] = numa_info[dst].distance[src]; 619 } 620 } 621 } 622 } 623 } 624 625 static void numa_init_memdev_container(MachineState *ms, MemoryRegion *ram) 626 { 627 int i; 628 uint64_t addr = 0; 629 630 for (i = 0; i < ms->numa_state->num_nodes; i++) { 631 uint64_t size = ms->numa_state->nodes[i].node_mem; 632 HostMemoryBackend *backend = ms->numa_state->nodes[i].node_memdev; 633 if (!backend) { 634 continue; 635 } 636 MemoryRegion *seg = machine_consume_memdev(ms, backend); 637 memory_region_add_subregion(ram, addr, seg); 638 addr += size; 639 } 640 } 641 642 void numa_complete_configuration(MachineState *ms) 643 { 644 int i; 645 MachineClass *mc = MACHINE_GET_CLASS(ms); 646 NodeInfo *numa_info = ms->numa_state->nodes; 647 648 /* 649 * If memory hotplug is enabled (slot > 0) or memory devices are enabled 650 * (ms->maxram_size > ms->ram_size) but without '-numa' options explicitly on 651 * CLI, guests will break. 652 * 653 * Windows: won't enable memory hotplug without SRAT table at all 654 * 655 * Linux: if QEMU is started with initial memory all below 4Gb 656 * and no SRAT table present, guest kernel will use nommu DMA ops, 657 * which breaks 32bit hw drivers when memory is hotplugged and 658 * guest tries to use it with that drivers. 659 * 660 * Enable NUMA implicitly by adding a new NUMA node automatically. 661 * 662 * Or if MachineClass::auto_enable_numa is true and no NUMA nodes, 663 * assume there is just one node with whole RAM. 664 */ 665 if (ms->numa_state->num_nodes == 0 && 666 ((ms->ram_slots && mc->auto_enable_numa_with_memhp) || 667 (ms->maxram_size > ms->ram_size && mc->auto_enable_numa_with_memdev) || 668 mc->auto_enable_numa)) { 669 NumaNodeOptions node = { }; 670 parse_numa_node(ms, &node, &error_abort); 671 numa_info[0].node_mem = ms->ram_size; 672 } 673 674 assert(max_numa_nodeid <= MAX_NODES); 675 676 /* No support for sparse NUMA node IDs yet: */ 677 for (i = max_numa_nodeid - 1; i >= 0; i--) { 678 /* Report large node IDs first, to make mistakes easier to spot */ 679 if (!numa_info[i].present) { 680 error_report("numa: Node ID missing: %d", i); 681 exit(1); 682 } 683 } 684 685 /* This must be always true if all nodes are present: */ 686 assert(ms->numa_state->num_nodes == max_numa_nodeid); 687 688 if (ms->numa_state->num_nodes > 0) { 689 uint64_t numa_total; 690 691 numa_total = 0; 692 for (i = 0; i < ms->numa_state->num_nodes; i++) { 693 numa_total += numa_info[i].node_mem; 694 } 695 if (numa_total != ms->ram_size) { 696 error_report("total memory for NUMA nodes (0x%" PRIx64 ")" 697 " should equal RAM size (0x" RAM_ADDR_FMT ")", 698 numa_total, ms->ram_size); 699 exit(1); 700 } 701 702 if (!numa_uses_legacy_mem() && mc->default_ram_id) { 703 if (ms->memdev) { 704 error_report("'-machine memory-backend' and '-numa memdev'" 705 " properties are mutually exclusive"); 706 exit(1); 707 } 708 ms->ram = g_new(MemoryRegion, 1); 709 memory_region_init(ms->ram, OBJECT(ms), mc->default_ram_id, 710 ms->ram_size); 711 numa_init_memdev_container(ms, ms->ram); 712 } 713 /* QEMU needs at least all unique node pair distances to build 714 * the whole NUMA distance table. QEMU treats the distance table 715 * as symmetric by default, i.e. distance A->B == distance B->A. 716 * Thus, QEMU is able to complete the distance table 717 * initialization even though only distance A->B is provided and 718 * distance B->A is not. QEMU knows the distance of a node to 719 * itself is always 10, so A->A distances may be omitted. When 720 * the distances of two nodes of a pair differ, i.e. distance 721 * A->B != distance B->A, then that means the distance table is 722 * asymmetric. In this case, the distances for both directions 723 * of all node pairs are required. 724 */ 725 if (ms->numa_state->have_numa_distance) { 726 /* Validate enough NUMA distance information was provided. */ 727 validate_numa_distance(ms); 728 729 /* Validation succeeded, now fill in any missing distances. */ 730 complete_init_numa_distance(ms); 731 } 732 } 733 } 734 735 void parse_numa_opts(MachineState *ms) 736 { 737 qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, &error_fatal); 738 } 739 740 void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp) 741 { 742 int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort); 743 744 if (node_id == CPU_UNSET_NUMA_NODE_ID) { 745 /* due to bug in libvirt, it doesn't pass node-id from props on 746 * device_add as expected, so we have to fix it up here */ 747 if (slot->props.has_node_id) { 748 object_property_set_int(OBJECT(dev), "node-id", 749 slot->props.node_id, errp); 750 } 751 } else if (node_id != slot->props.node_id) { 752 error_setg(errp, "invalid node-id, must be %"PRId64, 753 slot->props.node_id); 754 } 755 } 756 757 static void numa_stat_memory_devices(NumaNodeMem node_mem[]) 758 { 759 MemoryDeviceInfoList *info_list = qmp_memory_device_list(); 760 MemoryDeviceInfoList *info; 761 PCDIMMDeviceInfo *pcdimm_info; 762 VirtioPMEMDeviceInfo *vpi; 763 VirtioMEMDeviceInfo *vmi; 764 SgxEPCDeviceInfo *se; 765 766 for (info = info_list; info; info = info->next) { 767 MemoryDeviceInfo *value = info->value; 768 769 if (value) { 770 switch (value->type) { 771 case MEMORY_DEVICE_INFO_KIND_DIMM: 772 case MEMORY_DEVICE_INFO_KIND_NVDIMM: 773 pcdimm_info = value->type == MEMORY_DEVICE_INFO_KIND_DIMM ? 774 value->u.dimm.data : value->u.nvdimm.data; 775 node_mem[pcdimm_info->node].node_mem += pcdimm_info->size; 776 node_mem[pcdimm_info->node].node_plugged_mem += 777 pcdimm_info->size; 778 break; 779 case MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM: 780 vpi = value->u.virtio_pmem.data; 781 /* TODO: once we support numa, assign to right node */ 782 node_mem[0].node_mem += vpi->size; 783 node_mem[0].node_plugged_mem += vpi->size; 784 break; 785 case MEMORY_DEVICE_INFO_KIND_VIRTIO_MEM: 786 vmi = value->u.virtio_mem.data; 787 node_mem[vmi->node].node_mem += vmi->size; 788 node_mem[vmi->node].node_plugged_mem += vmi->size; 789 break; 790 case MEMORY_DEVICE_INFO_KIND_SGX_EPC: 791 se = value->u.sgx_epc.data; 792 node_mem[se->node].node_mem += se->size; 793 node_mem[se->node].node_plugged_mem = 0; 794 break; 795 default: 796 g_assert_not_reached(); 797 } 798 } 799 } 800 qapi_free_MemoryDeviceInfoList(info_list); 801 } 802 803 void query_numa_node_mem(NumaNodeMem node_mem[], MachineState *ms) 804 { 805 int i; 806 807 if (ms->numa_state == NULL || ms->numa_state->num_nodes <= 0) { 808 return; 809 } 810 811 numa_stat_memory_devices(node_mem); 812 for (i = 0; i < ms->numa_state->num_nodes; i++) { 813 node_mem[i].node_mem += ms->numa_state->nodes[i].node_mem; 814 } 815 } 816 817 static int ram_block_notify_add_single(RAMBlock *rb, void *opaque) 818 { 819 const ram_addr_t max_size = qemu_ram_get_max_length(rb); 820 const ram_addr_t size = qemu_ram_get_used_length(rb); 821 void *host = qemu_ram_get_host_addr(rb); 822 RAMBlockNotifier *notifier = opaque; 823 824 if (host) { 825 notifier->ram_block_added(notifier, host, size, max_size); 826 } 827 return 0; 828 } 829 830 static int ram_block_notify_remove_single(RAMBlock *rb, void *opaque) 831 { 832 const ram_addr_t max_size = qemu_ram_get_max_length(rb); 833 const ram_addr_t size = qemu_ram_get_used_length(rb); 834 void *host = qemu_ram_get_host_addr(rb); 835 RAMBlockNotifier *notifier = opaque; 836 837 if (host) { 838 notifier->ram_block_removed(notifier, host, size, max_size); 839 } 840 return 0; 841 } 842 843 void ram_block_notifier_add(RAMBlockNotifier *n) 844 { 845 QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next); 846 847 /* Notify about all existing ram blocks. */ 848 if (n->ram_block_added) { 849 qemu_ram_foreach_block(ram_block_notify_add_single, n); 850 } 851 } 852 853 void ram_block_notifier_remove(RAMBlockNotifier *n) 854 { 855 QLIST_REMOVE(n, next); 856 857 if (n->ram_block_removed) { 858 qemu_ram_foreach_block(ram_block_notify_remove_single, n); 859 } 860 } 861 862 void ram_block_notify_add(void *host, size_t size, size_t max_size) 863 { 864 RAMBlockNotifier *notifier; 865 RAMBlockNotifier *next; 866 867 QLIST_FOREACH_SAFE(notifier, &ram_list.ramblock_notifiers, next, next) { 868 if (notifier->ram_block_added) { 869 notifier->ram_block_added(notifier, host, size, max_size); 870 } 871 } 872 } 873 874 void ram_block_notify_remove(void *host, size_t size, size_t max_size) 875 { 876 RAMBlockNotifier *notifier; 877 RAMBlockNotifier *next; 878 879 QLIST_FOREACH_SAFE(notifier, &ram_list.ramblock_notifiers, next, next) { 880 if (notifier->ram_block_removed) { 881 notifier->ram_block_removed(notifier, host, size, max_size); 882 } 883 } 884 } 885 886 void ram_block_notify_resize(void *host, size_t old_size, size_t new_size) 887 { 888 RAMBlockNotifier *notifier; 889 RAMBlockNotifier *next; 890 891 QLIST_FOREACH_SAFE(notifier, &ram_list.ramblock_notifiers, next, next) { 892 if (notifier->ram_block_resized) { 893 notifier->ram_block_resized(notifier, host, old_size, new_size); 894 } 895 } 896 } 897