numa.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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] &&
  381. (node->size >=
  382. ms->numa_state->hmat_cache[node->node_id][node->level - 1]->size)) {
  383. error_setg(errp, "Invalid size=%" PRIu64 ", the size of level=%" PRIu8
  384. " should be less than the size(%" PRIu64 ") of "
  385. "level=%u", node->size, node->level,
  386. ms->numa_state->hmat_cache[node->node_id]
  387. [node->level - 1]->size,
  388. node->level - 1);
  389. return;
  390. }
  391. if ((node->level < HMAT_LB_LEVELS - 1) &&
  392. ms->numa_state->hmat_cache[node->node_id][node->level + 1] &&
  393. (node->size <=
  394. ms->numa_state->hmat_cache[node->node_id][node->level + 1]->size)) {
  395. error_setg(errp, "Invalid size=%" PRIu64 ", the size of level=%" PRIu8
  396. " should be larger than the size(%" PRIu64 ") of "
  397. "level=%u", node->size, node->level,
  398. ms->numa_state->hmat_cache[node->node_id]
  399. [node->level + 1]->size,
  400. node->level + 1);
  401. return;
  402. }
  403. hmat_cache = g_malloc0(sizeof(*hmat_cache));
  404. memcpy(hmat_cache, node, sizeof(*hmat_cache));
  405. ms->numa_state->hmat_cache[node->node_id][node->level] = hmat_cache;
  406. }
  407. void set_numa_options(MachineState *ms, NumaOptions *object, Error **errp)
  408. {
  409. if (!ms->numa_state) {
  410. error_setg(errp, "NUMA is not supported by this machine-type");
  411. return;
  412. }
  413. switch (object->type) {
  414. case NUMA_OPTIONS_TYPE_NODE:
  415. parse_numa_node(ms, &object->u.node, errp);
  416. break;
  417. case NUMA_OPTIONS_TYPE_DIST:
  418. parse_numa_distance(ms, &object->u.dist, errp);
  419. break;
  420. case NUMA_OPTIONS_TYPE_CPU:
  421. if (!object->u.cpu.has_node_id) {
  422. error_setg(errp, "Missing mandatory node-id property");
  423. return;
  424. }
  425. if (!ms->numa_state->nodes[object->u.cpu.node_id].present) {
  426. error_setg(errp, "Invalid node-id=%" PRId64 ", NUMA node must be "
  427. "defined with -numa node,nodeid=ID before it's used with "
  428. "-numa cpu,node-id=ID", object->u.cpu.node_id);
  429. return;
  430. }
  431. machine_set_cpu_numa_node(ms,
  432. qapi_NumaCpuOptions_base(&object->u.cpu),
  433. errp);
  434. break;
  435. case NUMA_OPTIONS_TYPE_HMAT_LB:
  436. if (!ms->numa_state->hmat_enabled) {
  437. error_setg(errp, "ACPI Heterogeneous Memory Attribute Table "
  438. "(HMAT) is disabled, enable it with -machine hmat=on "
  439. "before using any of hmat specific options");
  440. return;
  441. }
  442. parse_numa_hmat_lb(ms->numa_state, &object->u.hmat_lb, errp);
  443. break;
  444. case NUMA_OPTIONS_TYPE_HMAT_CACHE:
  445. if (!ms->numa_state->hmat_enabled) {
  446. error_setg(errp, "ACPI Heterogeneous Memory Attribute Table "
  447. "(HMAT) is disabled, enable it with -machine hmat=on "
  448. "before using any of hmat specific options");
  449. return;
  450. }
  451. parse_numa_hmat_cache(ms, &object->u.hmat_cache, errp);
  452. break;
  453. default:
  454. abort();
  455. }
  456. }
  457. static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
  458. {
  459. NumaOptions *object = NULL;
  460. MachineState *ms = MACHINE(opaque);
  461. Error *err = NULL;
  462. Visitor *v = opts_visitor_new(opts);
  463. visit_type_NumaOptions(v, NULL, &object, errp);
  464. visit_free(v);
  465. if (!object) {
  466. return -1;
  467. }
  468. /* Fix up legacy suffix-less format */
  469. if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
  470. const char *mem_str = qemu_opt_get(opts, "mem");
  471. qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
  472. }
  473. set_numa_options(ms, object, &err);
  474. qapi_free_NumaOptions(object);
  475. if (err) {
  476. error_propagate(errp, err);
  477. return -1;
  478. }
  479. return 0;
  480. }
  481. /* If all node pair distances are symmetric, then only distances
  482. * in one direction are enough. If there is even one asymmetric
  483. * pair, though, then all distances must be provided. The
  484. * distance from a node to itself is always NUMA_DISTANCE_MIN,
  485. * so providing it is never necessary.
  486. */
  487. static void validate_numa_distance(MachineState *ms)
  488. {
  489. int src, dst;
  490. bool is_asymmetrical = false;
  491. int nb_numa_nodes = ms->numa_state->num_nodes;
  492. NodeInfo *numa_info = ms->numa_state->nodes;
  493. for (src = 0; src < nb_numa_nodes; src++) {
  494. for (dst = src; dst < nb_numa_nodes; dst++) {
  495. if (numa_info[src].distance[dst] == 0 &&
  496. numa_info[dst].distance[src] == 0) {
  497. if (src != dst) {
  498. error_report("The distance between node %d and %d is "
  499. "missing, at least one distance value "
  500. "between each nodes should be provided.",
  501. src, dst);
  502. exit(EXIT_FAILURE);
  503. }
  504. }
  505. if (numa_info[src].distance[dst] != 0 &&
  506. numa_info[dst].distance[src] != 0 &&
  507. numa_info[src].distance[dst] !=
  508. numa_info[dst].distance[src]) {
  509. is_asymmetrical = true;
  510. }
  511. }
  512. }
  513. if (is_asymmetrical) {
  514. for (src = 0; src < nb_numa_nodes; src++) {
  515. for (dst = 0; dst < nb_numa_nodes; dst++) {
  516. if (src != dst && numa_info[src].distance[dst] == 0) {
  517. error_report("At least one asymmetrical pair of "
  518. "distances is given, please provide distances "
  519. "for both directions of all node pairs.");
  520. exit(EXIT_FAILURE);
  521. }
  522. }
  523. }
  524. }
  525. }
  526. static void complete_init_numa_distance(MachineState *ms)
  527. {
  528. int src, dst;
  529. NodeInfo *numa_info = ms->numa_state->nodes;
  530. /* Fixup NUMA distance by symmetric policy because if it is an
  531. * asymmetric distance table, it should be a complete table and
  532. * there would not be any missing distance except local node, which
  533. * is verified by validate_numa_distance above.
  534. */
  535. for (src = 0; src < ms->numa_state->num_nodes; src++) {
  536. for (dst = 0; dst < ms->numa_state->num_nodes; dst++) {
  537. if (numa_info[src].distance[dst] == 0) {
  538. if (src == dst) {
  539. numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
  540. } else {
  541. numa_info[src].distance[dst] = numa_info[dst].distance[src];
  542. }
  543. }
  544. }
  545. }
  546. }
  547. void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
  548. int nb_nodes, ram_addr_t size)
  549. {
  550. int i;
  551. uint64_t usedmem = 0;
  552. /* Align each node according to the alignment
  553. * requirements of the machine class
  554. */
  555. for (i = 0; i < nb_nodes - 1; i++) {
  556. nodes[i].node_mem = (size / nb_nodes) &
  557. ~((1 << mc->numa_mem_align_shift) - 1);
  558. usedmem += nodes[i].node_mem;
  559. }
  560. nodes[i].node_mem = size - usedmem;
  561. }
  562. void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
  563. int nb_nodes, ram_addr_t size)
  564. {
  565. int i;
  566. uint64_t usedmem = 0, node_mem;
  567. uint64_t granularity = size / nb_nodes;
  568. uint64_t propagate = 0;
  569. for (i = 0; i < nb_nodes - 1; i++) {
  570. node_mem = (granularity + propagate) &
  571. ~((1 << mc->numa_mem_align_shift) - 1);
  572. propagate = granularity + propagate - node_mem;
  573. nodes[i].node_mem = node_mem;
  574. usedmem += node_mem;
  575. }
  576. nodes[i].node_mem = size - usedmem;
  577. }
  578. static void numa_init_memdev_container(MachineState *ms, MemoryRegion *ram)
  579. {
  580. int i;
  581. uint64_t addr = 0;
  582. for (i = 0; i < ms->numa_state->num_nodes; i++) {
  583. uint64_t size = ms->numa_state->nodes[i].node_mem;
  584. HostMemoryBackend *backend = ms->numa_state->nodes[i].node_memdev;
  585. if (!backend) {
  586. continue;
  587. }
  588. MemoryRegion *seg = machine_consume_memdev(ms, backend);
  589. memory_region_add_subregion(ram, addr, seg);
  590. addr += size;
  591. }
  592. }
  593. void numa_complete_configuration(MachineState *ms)
  594. {
  595. int i;
  596. MachineClass *mc = MACHINE_GET_CLASS(ms);
  597. NodeInfo *numa_info = ms->numa_state->nodes;
  598. /*
  599. * If memory hotplug is enabled (slot > 0) or memory devices are enabled
  600. * (ms->maxram_size > ram_size) but without '-numa' options explicitly on
  601. * CLI, guests will break.
  602. *
  603. * Windows: won't enable memory hotplug without SRAT table at all
  604. *
  605. * Linux: if QEMU is started with initial memory all below 4Gb
  606. * and no SRAT table present, guest kernel will use nommu DMA ops,
  607. * which breaks 32bit hw drivers when memory is hotplugged and
  608. * guest tries to use it with that drivers.
  609. *
  610. * Enable NUMA implicitly by adding a new NUMA node automatically.
  611. *
  612. * Or if MachineClass::auto_enable_numa is true and no NUMA nodes,
  613. * assume there is just one node with whole RAM.
  614. */
  615. if (ms->numa_state->num_nodes == 0 &&
  616. ((ms->ram_slots && mc->auto_enable_numa_with_memhp) ||
  617. (ms->maxram_size > ms->ram_size && mc->auto_enable_numa_with_memdev) ||
  618. mc->auto_enable_numa)) {
  619. NumaNodeOptions node = { };
  620. parse_numa_node(ms, &node, &error_abort);
  621. numa_info[0].node_mem = ram_size;
  622. }
  623. assert(max_numa_nodeid <= MAX_NODES);
  624. /* No support for sparse NUMA node IDs yet: */
  625. for (i = max_numa_nodeid - 1; i >= 0; i--) {
  626. /* Report large node IDs first, to make mistakes easier to spot */
  627. if (!numa_info[i].present) {
  628. error_report("numa: Node ID missing: %d", i);
  629. exit(1);
  630. }
  631. }
  632. /* This must be always true if all nodes are present: */
  633. assert(ms->numa_state->num_nodes == max_numa_nodeid);
  634. if (ms->numa_state->num_nodes > 0) {
  635. uint64_t numa_total;
  636. if (ms->numa_state->num_nodes > MAX_NODES) {
  637. ms->numa_state->num_nodes = MAX_NODES;
  638. }
  639. /* If no memory size is given for any node, assume the default case
  640. * and distribute the available memory equally across all nodes
  641. */
  642. for (i = 0; i < ms->numa_state->num_nodes; i++) {
  643. if (numa_info[i].node_mem != 0) {
  644. break;
  645. }
  646. }
  647. if (i == ms->numa_state->num_nodes) {
  648. assert(mc->numa_auto_assign_ram);
  649. mc->numa_auto_assign_ram(mc, numa_info,
  650. ms->numa_state->num_nodes, ram_size);
  651. if (!qtest_enabled()) {
  652. warn_report("Default splitting of RAM between nodes is deprecated,"
  653. " Use '-numa node,memdev' to explictly define RAM"
  654. " allocation per node");
  655. }
  656. }
  657. numa_total = 0;
  658. for (i = 0; i < ms->numa_state->num_nodes; i++) {
  659. numa_total += numa_info[i].node_mem;
  660. }
  661. if (numa_total != ram_size) {
  662. error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
  663. " should equal RAM size (0x" RAM_ADDR_FMT ")",
  664. numa_total, ram_size);
  665. exit(1);
  666. }
  667. if (!numa_uses_legacy_mem() && mc->default_ram_id) {
  668. if (ms->ram_memdev_id) {
  669. error_report("'-machine memory-backend' and '-numa memdev'"
  670. " properties are mutually exclusive");
  671. exit(1);
  672. }
  673. ms->ram = g_new(MemoryRegion, 1);
  674. memory_region_init(ms->ram, OBJECT(ms), mc->default_ram_id,
  675. ram_size);
  676. numa_init_memdev_container(ms, ms->ram);
  677. }
  678. /* QEMU needs at least all unique node pair distances to build
  679. * the whole NUMA distance table. QEMU treats the distance table
  680. * as symmetric by default, i.e. distance A->B == distance B->A.
  681. * Thus, QEMU is able to complete the distance table
  682. * initialization even though only distance A->B is provided and
  683. * distance B->A is not. QEMU knows the distance of a node to
  684. * itself is always 10, so A->A distances may be omitted. When
  685. * the distances of two nodes of a pair differ, i.e. distance
  686. * A->B != distance B->A, then that means the distance table is
  687. * asymmetric. In this case, the distances for both directions
  688. * of all node pairs are required.
  689. */
  690. if (ms->numa_state->have_numa_distance) {
  691. /* Validate enough NUMA distance information was provided. */
  692. validate_numa_distance(ms);
  693. /* Validation succeeded, now fill in any missing distances. */
  694. complete_init_numa_distance(ms);
  695. }
  696. }
  697. }
  698. void parse_numa_opts(MachineState *ms)
  699. {
  700. qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, &error_fatal);
  701. }
  702. void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
  703. {
  704. int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
  705. if (node_id == CPU_UNSET_NUMA_NODE_ID) {
  706. /* due to bug in libvirt, it doesn't pass node-id from props on
  707. * device_add as expected, so we have to fix it up here */
  708. if (slot->props.has_node_id) {
  709. object_property_set_int(OBJECT(dev), "node-id",
  710. slot->props.node_id, errp);
  711. }
  712. } else if (node_id != slot->props.node_id) {
  713. error_setg(errp, "invalid node-id, must be %"PRId64,
  714. slot->props.node_id);
  715. }
  716. }
  717. static void numa_stat_memory_devices(NumaNodeMem node_mem[])
  718. {
  719. MemoryDeviceInfoList *info_list = qmp_memory_device_list();
  720. MemoryDeviceInfoList *info;
  721. PCDIMMDeviceInfo *pcdimm_info;
  722. VirtioPMEMDeviceInfo *vpi;
  723. VirtioMEMDeviceInfo *vmi;
  724. for (info = info_list; info; info = info->next) {
  725. MemoryDeviceInfo *value = info->value;
  726. if (value) {
  727. switch (value->type) {
  728. case MEMORY_DEVICE_INFO_KIND_DIMM:
  729. case MEMORY_DEVICE_INFO_KIND_NVDIMM:
  730. pcdimm_info = value->type == MEMORY_DEVICE_INFO_KIND_DIMM ?
  731. value->u.dimm.data : value->u.nvdimm.data;
  732. node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
  733. node_mem[pcdimm_info->node].node_plugged_mem +=
  734. pcdimm_info->size;
  735. break;
  736. case MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM:
  737. vpi = value->u.virtio_pmem.data;
  738. /* TODO: once we support numa, assign to right node */
  739. node_mem[0].node_mem += vpi->size;
  740. node_mem[0].node_plugged_mem += vpi->size;
  741. break;
  742. case MEMORY_DEVICE_INFO_KIND_VIRTIO_MEM:
  743. vmi = value->u.virtio_mem.data;
  744. node_mem[vmi->node].node_mem += vmi->size;
  745. node_mem[vmi->node].node_plugged_mem += vmi->size;
  746. break;
  747. default:
  748. g_assert_not_reached();
  749. }
  750. }
  751. }
  752. qapi_free_MemoryDeviceInfoList(info_list);
  753. }
  754. void query_numa_node_mem(NumaNodeMem node_mem[], MachineState *ms)
  755. {
  756. int i;
  757. if (ms->numa_state == NULL || ms->numa_state->num_nodes <= 0) {
  758. return;
  759. }
  760. numa_stat_memory_devices(node_mem);
  761. for (i = 0; i < ms->numa_state->num_nodes; i++) {
  762. node_mem[i].node_mem += ms->numa_state->nodes[i].node_mem;
  763. }
  764. }
  765. void ram_block_notifier_add(RAMBlockNotifier *n)
  766. {
  767. QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
  768. }
  769. void ram_block_notifier_remove(RAMBlockNotifier *n)
  770. {
  771. QLIST_REMOVE(n, next);
  772. }
  773. void ram_block_notify_add(void *host, size_t size)
  774. {
  775. RAMBlockNotifier *notifier;
  776. QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
  777. notifier->ram_block_added(notifier, host, size);
  778. }
  779. }
  780. void ram_block_notify_remove(void *host, size_t size)
  781. {
  782. RAMBlockNotifier *notifier;
  783. QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
  784. notifier->ram_block_removed(notifier, host, size);
  785. }
  786. }