numa.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 "sysemu/numa.h"
  26. #include "exec/cpu-common.h"
  27. #include "exec/ramlist.h"
  28. #include "qemu/bitmap.h"
  29. #include "qom/cpu.h"
  30. #include "qemu/error-report.h"
  31. #include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
  32. #include "qapi-visit.h"
  33. #include "qapi/opts-visitor.h"
  34. #include "hw/boards.h"
  35. #include "sysemu/hostmem.h"
  36. #include "qmp-commands.h"
  37. #include "hw/mem/pc-dimm.h"
  38. #include "qemu/option.h"
  39. #include "qemu/config-file.h"
  40. #include "qemu/cutils.h"
  41. QemuOptsList qemu_numa_opts = {
  42. .name = "numa",
  43. .implied_opt_name = "type",
  44. .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
  45. .desc = { { 0 } } /* validated with OptsVisitor */
  46. };
  47. static int have_memdevs = -1;
  48. static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
  49. * For all nodes, nodeid < max_numa_nodeid
  50. */
  51. int nb_numa_nodes;
  52. bool have_numa_distance;
  53. NodeInfo numa_info[MAX_NODES];
  54. void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
  55. {
  56. struct numa_addr_range *range;
  57. /*
  58. * Memory-less nodes can come here with 0 size in which case,
  59. * there is nothing to do.
  60. */
  61. if (!size) {
  62. return;
  63. }
  64. range = g_malloc0(sizeof(*range));
  65. range->mem_start = addr;
  66. range->mem_end = addr + size - 1;
  67. QLIST_INSERT_HEAD(&numa_info[node].addr, range, entry);
  68. }
  69. void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
  70. {
  71. struct numa_addr_range *range, *next;
  72. QLIST_FOREACH_SAFE(range, &numa_info[node].addr, entry, next) {
  73. if (addr == range->mem_start && (addr + size - 1) == range->mem_end) {
  74. QLIST_REMOVE(range, entry);
  75. g_free(range);
  76. return;
  77. }
  78. }
  79. }
  80. static void numa_set_mem_ranges(void)
  81. {
  82. int i;
  83. ram_addr_t mem_start = 0;
  84. /*
  85. * Deduce start address of each node and use it to store
  86. * the address range info in numa_info address range list
  87. */
  88. for (i = 0; i < nb_numa_nodes; i++) {
  89. numa_set_mem_node_id(mem_start, numa_info[i].node_mem, i);
  90. mem_start += numa_info[i].node_mem;
  91. }
  92. }
  93. /*
  94. * Check if @addr falls under NUMA @node.
  95. */
  96. static bool numa_addr_belongs_to_node(ram_addr_t addr, uint32_t node)
  97. {
  98. struct numa_addr_range *range;
  99. QLIST_FOREACH(range, &numa_info[node].addr, entry) {
  100. if (addr >= range->mem_start && addr <= range->mem_end) {
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. /*
  107. * Given an address, return the index of the NUMA node to which the
  108. * address belongs to.
  109. */
  110. uint32_t numa_get_node(ram_addr_t addr, Error **errp)
  111. {
  112. uint32_t i;
  113. /* For non NUMA configurations, check if the addr falls under node 0 */
  114. if (!nb_numa_nodes) {
  115. if (numa_addr_belongs_to_node(addr, 0)) {
  116. return 0;
  117. }
  118. }
  119. for (i = 0; i < nb_numa_nodes; i++) {
  120. if (numa_addr_belongs_to_node(addr, i)) {
  121. return i;
  122. }
  123. }
  124. error_setg(errp, "Address 0x" RAM_ADDR_FMT " doesn't belong to any "
  125. "NUMA node", addr);
  126. return -1;
  127. }
  128. static void parse_numa_node(MachineState *ms, NumaNodeOptions *node,
  129. Error **errp)
  130. {
  131. uint16_t nodenr;
  132. uint16List *cpus = NULL;
  133. MachineClass *mc = MACHINE_GET_CLASS(ms);
  134. if (node->has_nodeid) {
  135. nodenr = node->nodeid;
  136. } else {
  137. nodenr = nb_numa_nodes;
  138. }
  139. if (nodenr >= MAX_NODES) {
  140. error_setg(errp, "Max number of NUMA nodes reached: %"
  141. PRIu16 "", nodenr);
  142. return;
  143. }
  144. if (numa_info[nodenr].present) {
  145. error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
  146. return;
  147. }
  148. if (!mc->cpu_index_to_instance_props) {
  149. error_report("NUMA is not supported by this machine-type");
  150. exit(1);
  151. }
  152. for (cpus = node->cpus; cpus; cpus = cpus->next) {
  153. CpuInstanceProperties props;
  154. if (cpus->value >= max_cpus) {
  155. error_setg(errp,
  156. "CPU index (%" PRIu16 ")"
  157. " should be smaller than maxcpus (%d)",
  158. cpus->value, max_cpus);
  159. return;
  160. }
  161. props = mc->cpu_index_to_instance_props(ms, cpus->value);
  162. props.node_id = nodenr;
  163. props.has_node_id = true;
  164. machine_set_cpu_numa_node(ms, &props, &error_fatal);
  165. }
  166. if (node->has_mem && node->has_memdev) {
  167. error_setg(errp, "cannot specify both mem= and memdev=");
  168. return;
  169. }
  170. if (have_memdevs == -1) {
  171. have_memdevs = node->has_memdev;
  172. }
  173. if (node->has_memdev != have_memdevs) {
  174. error_setg(errp, "memdev option must be specified for either "
  175. "all or no nodes");
  176. return;
  177. }
  178. if (node->has_mem) {
  179. numa_info[nodenr].node_mem = node->mem;
  180. }
  181. if (node->has_memdev) {
  182. Object *o;
  183. o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
  184. if (!o) {
  185. error_setg(errp, "memdev=%s is ambiguous", node->memdev);
  186. return;
  187. }
  188. object_ref(o);
  189. numa_info[nodenr].node_mem = object_property_get_uint(o, "size", NULL);
  190. numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
  191. }
  192. numa_info[nodenr].present = true;
  193. max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
  194. nb_numa_nodes++;
  195. }
  196. static void parse_numa_distance(NumaDistOptions *dist, Error **errp)
  197. {
  198. uint16_t src = dist->src;
  199. uint16_t dst = dist->dst;
  200. uint8_t val = dist->val;
  201. if (src >= MAX_NODES || dst >= MAX_NODES) {
  202. error_setg(errp,
  203. "Invalid node %d, max possible could be %d",
  204. MAX(src, dst), MAX_NODES);
  205. return;
  206. }
  207. if (!numa_info[src].present || !numa_info[dst].present) {
  208. error_setg(errp, "Source/Destination NUMA node is missing. "
  209. "Please use '-numa node' option to declare it first.");
  210. return;
  211. }
  212. if (val < NUMA_DISTANCE_MIN) {
  213. error_setg(errp, "NUMA distance (%" PRIu8 ") is invalid, "
  214. "it shouldn't be less than %d.",
  215. val, NUMA_DISTANCE_MIN);
  216. return;
  217. }
  218. if (src == dst && val != NUMA_DISTANCE_MIN) {
  219. error_setg(errp, "Local distance of node %d should be %d.",
  220. src, NUMA_DISTANCE_MIN);
  221. return;
  222. }
  223. numa_info[src].distance[dst] = val;
  224. have_numa_distance = true;
  225. }
  226. static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
  227. {
  228. NumaOptions *object = NULL;
  229. MachineState *ms = opaque;
  230. Error *err = NULL;
  231. {
  232. Visitor *v = opts_visitor_new(opts);
  233. visit_type_NumaOptions(v, NULL, &object, &err);
  234. visit_free(v);
  235. }
  236. if (err) {
  237. goto end;
  238. }
  239. /* Fix up legacy suffix-less format */
  240. if ((object->type == NUMA_OPTIONS_TYPE_NODE) && object->u.node.has_mem) {
  241. const char *mem_str = qemu_opt_get(opts, "mem");
  242. qemu_strtosz_MiB(mem_str, NULL, &object->u.node.mem);
  243. }
  244. switch (object->type) {
  245. case NUMA_OPTIONS_TYPE_NODE:
  246. parse_numa_node(ms, &object->u.node, &err);
  247. if (err) {
  248. goto end;
  249. }
  250. break;
  251. case NUMA_OPTIONS_TYPE_DIST:
  252. parse_numa_distance(&object->u.dist, &err);
  253. if (err) {
  254. goto end;
  255. }
  256. break;
  257. case NUMA_OPTIONS_TYPE_CPU:
  258. if (!object->u.cpu.has_node_id) {
  259. error_setg(&err, "Missing mandatory node-id property");
  260. goto end;
  261. }
  262. if (!numa_info[object->u.cpu.node_id].present) {
  263. error_setg(&err, "Invalid node-id=%" PRId64 ", NUMA node must be "
  264. "defined with -numa node,nodeid=ID before it's used with "
  265. "-numa cpu,node-id=ID", object->u.cpu.node_id);
  266. goto end;
  267. }
  268. machine_set_cpu_numa_node(ms, qapi_NumaCpuOptions_base(&object->u.cpu),
  269. &err);
  270. break;
  271. default:
  272. abort();
  273. }
  274. end:
  275. qapi_free_NumaOptions(object);
  276. if (err) {
  277. error_report_err(err);
  278. return -1;
  279. }
  280. return 0;
  281. }
  282. /* If all node pair distances are symmetric, then only distances
  283. * in one direction are enough. If there is even one asymmetric
  284. * pair, though, then all distances must be provided. The
  285. * distance from a node to itself is always NUMA_DISTANCE_MIN,
  286. * so providing it is never necessary.
  287. */
  288. static void validate_numa_distance(void)
  289. {
  290. int src, dst;
  291. bool is_asymmetrical = false;
  292. for (src = 0; src < nb_numa_nodes; src++) {
  293. for (dst = src; dst < nb_numa_nodes; dst++) {
  294. if (numa_info[src].distance[dst] == 0 &&
  295. numa_info[dst].distance[src] == 0) {
  296. if (src != dst) {
  297. error_report("The distance between node %d and %d is "
  298. "missing, at least one distance value "
  299. "between each nodes should be provided.",
  300. src, dst);
  301. exit(EXIT_FAILURE);
  302. }
  303. }
  304. if (numa_info[src].distance[dst] != 0 &&
  305. numa_info[dst].distance[src] != 0 &&
  306. numa_info[src].distance[dst] !=
  307. numa_info[dst].distance[src]) {
  308. is_asymmetrical = true;
  309. }
  310. }
  311. }
  312. if (is_asymmetrical) {
  313. for (src = 0; src < nb_numa_nodes; src++) {
  314. for (dst = 0; dst < nb_numa_nodes; dst++) {
  315. if (src != dst && numa_info[src].distance[dst] == 0) {
  316. error_report("At least one asymmetrical pair of "
  317. "distances is given, please provide distances "
  318. "for both directions of all node pairs.");
  319. exit(EXIT_FAILURE);
  320. }
  321. }
  322. }
  323. }
  324. }
  325. static void complete_init_numa_distance(void)
  326. {
  327. int src, dst;
  328. /* Fixup NUMA distance by symmetric policy because if it is an
  329. * asymmetric distance table, it should be a complete table and
  330. * there would not be any missing distance except local node, which
  331. * is verified by validate_numa_distance above.
  332. */
  333. for (src = 0; src < nb_numa_nodes; src++) {
  334. for (dst = 0; dst < nb_numa_nodes; dst++) {
  335. if (numa_info[src].distance[dst] == 0) {
  336. if (src == dst) {
  337. numa_info[src].distance[dst] = NUMA_DISTANCE_MIN;
  338. } else {
  339. numa_info[src].distance[dst] = numa_info[dst].distance[src];
  340. }
  341. }
  342. }
  343. }
  344. }
  345. void numa_legacy_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
  346. int nb_nodes, ram_addr_t size)
  347. {
  348. int i;
  349. uint64_t usedmem = 0;
  350. /* Align each node according to the alignment
  351. * requirements of the machine class
  352. */
  353. for (i = 0; i < nb_nodes - 1; i++) {
  354. nodes[i].node_mem = (size / nb_nodes) &
  355. ~((1 << mc->numa_mem_align_shift) - 1);
  356. usedmem += nodes[i].node_mem;
  357. }
  358. nodes[i].node_mem = size - usedmem;
  359. }
  360. void numa_default_auto_assign_ram(MachineClass *mc, NodeInfo *nodes,
  361. int nb_nodes, ram_addr_t size)
  362. {
  363. int i;
  364. uint64_t usedmem = 0, node_mem;
  365. uint64_t granularity = size / nb_nodes;
  366. uint64_t propagate = 0;
  367. for (i = 0; i < nb_nodes - 1; i++) {
  368. node_mem = (granularity + propagate) &
  369. ~((1 << mc->numa_mem_align_shift) - 1);
  370. propagate = granularity + propagate - node_mem;
  371. nodes[i].node_mem = node_mem;
  372. usedmem += node_mem;
  373. }
  374. nodes[i].node_mem = size - usedmem;
  375. }
  376. void parse_numa_opts(MachineState *ms)
  377. {
  378. int i;
  379. MachineClass *mc = MACHINE_GET_CLASS(ms);
  380. if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, ms, NULL)) {
  381. exit(1);
  382. }
  383. /*
  384. * If memory hotplug is enabled (slots > 0) but without '-numa'
  385. * options explicitly on CLI, guestes will break.
  386. *
  387. * Windows: won't enable memory hotplug without SRAT table at all
  388. *
  389. * Linux: if QEMU is started with initial memory all below 4Gb
  390. * and no SRAT table present, guest kernel will use nommu DMA ops,
  391. * which breaks 32bit hw drivers when memory is hotplugged and
  392. * guest tries to use it with that drivers.
  393. *
  394. * Enable NUMA implicitly by adding a new NUMA node automatically.
  395. */
  396. if (ms->ram_slots > 0 && nb_numa_nodes == 0 &&
  397. mc->auto_enable_numa_with_memhp) {
  398. NumaNodeOptions node = { };
  399. parse_numa_node(ms, &node, NULL);
  400. }
  401. assert(max_numa_nodeid <= MAX_NODES);
  402. /* No support for sparse NUMA node IDs yet: */
  403. for (i = max_numa_nodeid - 1; i >= 0; i--) {
  404. /* Report large node IDs first, to make mistakes easier to spot */
  405. if (!numa_info[i].present) {
  406. error_report("numa: Node ID missing: %d", i);
  407. exit(1);
  408. }
  409. }
  410. /* This must be always true if all nodes are present: */
  411. assert(nb_numa_nodes == max_numa_nodeid);
  412. if (nb_numa_nodes > 0) {
  413. uint64_t numa_total;
  414. if (nb_numa_nodes > MAX_NODES) {
  415. nb_numa_nodes = MAX_NODES;
  416. }
  417. /* If no memory size is given for any node, assume the default case
  418. * and distribute the available memory equally across all nodes
  419. */
  420. for (i = 0; i < nb_numa_nodes; i++) {
  421. if (numa_info[i].node_mem != 0) {
  422. break;
  423. }
  424. }
  425. if (i == nb_numa_nodes) {
  426. assert(mc->numa_auto_assign_ram);
  427. mc->numa_auto_assign_ram(mc, numa_info, nb_numa_nodes, ram_size);
  428. }
  429. numa_total = 0;
  430. for (i = 0; i < nb_numa_nodes; i++) {
  431. numa_total += numa_info[i].node_mem;
  432. }
  433. if (numa_total != ram_size) {
  434. error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
  435. " should equal RAM size (0x" RAM_ADDR_FMT ")",
  436. numa_total, ram_size);
  437. exit(1);
  438. }
  439. for (i = 0; i < nb_numa_nodes; i++) {
  440. QLIST_INIT(&numa_info[i].addr);
  441. }
  442. numa_set_mem_ranges();
  443. /* QEMU needs at least all unique node pair distances to build
  444. * the whole NUMA distance table. QEMU treats the distance table
  445. * as symmetric by default, i.e. distance A->B == distance B->A.
  446. * Thus, QEMU is able to complete the distance table
  447. * initialization even though only distance A->B is provided and
  448. * distance B->A is not. QEMU knows the distance of a node to
  449. * itself is always 10, so A->A distances may be omitted. When
  450. * the distances of two nodes of a pair differ, i.e. distance
  451. * A->B != distance B->A, then that means the distance table is
  452. * asymmetric. In this case, the distances for both directions
  453. * of all node pairs are required.
  454. */
  455. if (have_numa_distance) {
  456. /* Validate enough NUMA distance information was provided. */
  457. validate_numa_distance();
  458. /* Validation succeeded, now fill in any missing distances. */
  459. complete_init_numa_distance();
  460. }
  461. } else {
  462. numa_set_mem_node_id(0, ram_size, 0);
  463. }
  464. }
  465. void numa_cpu_pre_plug(const CPUArchId *slot, DeviceState *dev, Error **errp)
  466. {
  467. int node_id = object_property_get_int(OBJECT(dev), "node-id", &error_abort);
  468. if (node_id == CPU_UNSET_NUMA_NODE_ID) {
  469. /* due to bug in libvirt, it doesn't pass node-id from props on
  470. * device_add as expected, so we have to fix it up here */
  471. if (slot->props.has_node_id) {
  472. object_property_set_int(OBJECT(dev), slot->props.node_id,
  473. "node-id", errp);
  474. }
  475. } else if (node_id != slot->props.node_id) {
  476. error_setg(errp, "node-id=%d must match numa node specified "
  477. "with -numa option", node_id);
  478. }
  479. }
  480. static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
  481. const char *name,
  482. uint64_t ram_size)
  483. {
  484. if (mem_path) {
  485. #ifdef __linux__
  486. Error *err = NULL;
  487. memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
  488. mem_path, &err);
  489. if (err) {
  490. error_report_err(err);
  491. if (mem_prealloc) {
  492. exit(1);
  493. }
  494. /* Legacy behavior: if allocation failed, fall back to
  495. * regular RAM allocation.
  496. */
  497. memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
  498. }
  499. #else
  500. fprintf(stderr, "-mem-path not supported on this host\n");
  501. exit(1);
  502. #endif
  503. } else {
  504. memory_region_init_ram_nomigrate(mr, owner, name, ram_size, &error_fatal);
  505. }
  506. vmstate_register_ram_global(mr);
  507. }
  508. void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
  509. const char *name,
  510. uint64_t ram_size)
  511. {
  512. uint64_t addr = 0;
  513. int i;
  514. if (nb_numa_nodes == 0 || !have_memdevs) {
  515. allocate_system_memory_nonnuma(mr, owner, name, ram_size);
  516. return;
  517. }
  518. memory_region_init(mr, owner, name, ram_size);
  519. for (i = 0; i < nb_numa_nodes; i++) {
  520. uint64_t size = numa_info[i].node_mem;
  521. HostMemoryBackend *backend = numa_info[i].node_memdev;
  522. if (!backend) {
  523. continue;
  524. }
  525. MemoryRegion *seg = host_memory_backend_get_memory(backend,
  526. &error_fatal);
  527. if (memory_region_is_mapped(seg)) {
  528. char *path = object_get_canonical_path_component(OBJECT(backend));
  529. error_report("memory backend %s is used multiple times. Each "
  530. "-numa option must use a different memdev value.",
  531. path);
  532. exit(1);
  533. }
  534. host_memory_backend_set_mapped(backend, true);
  535. memory_region_add_subregion(mr, addr, seg);
  536. vmstate_register_ram_global(seg);
  537. addr += size;
  538. }
  539. }
  540. static void numa_stat_memory_devices(NumaNodeMem node_mem[])
  541. {
  542. MemoryDeviceInfoList *info_list = NULL;
  543. MemoryDeviceInfoList **prev = &info_list;
  544. MemoryDeviceInfoList *info;
  545. PCDIMMDeviceInfo *pcdimm_info;
  546. qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
  547. for (info = info_list; info; info = info->next) {
  548. MemoryDeviceInfo *value = info->value;
  549. if (value) {
  550. switch (value->type) {
  551. case MEMORY_DEVICE_INFO_KIND_DIMM: {
  552. pcdimm_info = value->u.dimm.data;
  553. node_mem[pcdimm_info->node].node_mem += pcdimm_info->size;
  554. if (pcdimm_info->hotpluggable && pcdimm_info->hotplugged) {
  555. node_mem[pcdimm_info->node].node_plugged_mem +=
  556. pcdimm_info->size;
  557. }
  558. break;
  559. }
  560. default:
  561. break;
  562. }
  563. }
  564. }
  565. qapi_free_MemoryDeviceInfoList(info_list);
  566. }
  567. void query_numa_node_mem(NumaNodeMem node_mem[])
  568. {
  569. int i;
  570. if (nb_numa_nodes <= 0) {
  571. return;
  572. }
  573. numa_stat_memory_devices(node_mem);
  574. for (i = 0; i < nb_numa_nodes; i++) {
  575. node_mem[i].node_mem += numa_info[i].node_mem;
  576. }
  577. }
  578. static int query_memdev(Object *obj, void *opaque)
  579. {
  580. MemdevList **list = opaque;
  581. MemdevList *m = NULL;
  582. if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
  583. m = g_malloc0(sizeof(*m));
  584. m->value = g_malloc0(sizeof(*m->value));
  585. m->value->id = object_property_get_str(obj, "id", NULL);
  586. m->value->has_id = !!m->value->id;
  587. m->value->size = object_property_get_uint(obj, "size",
  588. &error_abort);
  589. m->value->merge = object_property_get_bool(obj, "merge",
  590. &error_abort);
  591. m->value->dump = object_property_get_bool(obj, "dump",
  592. &error_abort);
  593. m->value->prealloc = object_property_get_bool(obj,
  594. "prealloc",
  595. &error_abort);
  596. m->value->policy = object_property_get_enum(obj,
  597. "policy",
  598. "HostMemPolicy",
  599. &error_abort);
  600. object_property_get_uint16List(obj, "host-nodes",
  601. &m->value->host_nodes,
  602. &error_abort);
  603. m->next = *list;
  604. *list = m;
  605. }
  606. return 0;
  607. }
  608. MemdevList *qmp_query_memdev(Error **errp)
  609. {
  610. Object *obj = object_get_objects_root();
  611. MemdevList *list = NULL;
  612. object_child_foreach(obj, query_memdev, &list);
  613. return list;
  614. }
  615. void ram_block_notifier_add(RAMBlockNotifier *n)
  616. {
  617. QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
  618. }
  619. void ram_block_notifier_remove(RAMBlockNotifier *n)
  620. {
  621. QLIST_REMOVE(n, next);
  622. }
  623. void ram_block_notify_add(void *host, size_t size)
  624. {
  625. RAMBlockNotifier *notifier;
  626. QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
  627. notifier->ram_block_added(notifier, host, size);
  628. }
  629. }
  630. void ram_block_notify_remove(void *host, size_t size)
  631. {
  632. RAMBlockNotifier *notifier;
  633. QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
  634. notifier->ram_block_removed(notifier, host, size);
  635. }
  636. }