2
0

numa.c 31 KB

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