numa.c 29 KB

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