2
0

numa.c 31 KB

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