2
0

numa.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. QemuOptsList qemu_numa_opts = {
  41. .name = "numa",
  42. .implied_opt_name = "type",
  43. .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
  44. .desc = { { 0 } } /* validated with OptsVisitor */
  45. };
  46. static int have_memdevs = -1;
  47. static int max_numa_nodeid; /* Highest specified NUMA node ID, plus one.
  48. * For all nodes, nodeid < max_numa_nodeid
  49. */
  50. int nb_numa_nodes;
  51. NodeInfo numa_info[MAX_NODES];
  52. void numa_set_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
  53. {
  54. struct numa_addr_range *range;
  55. /*
  56. * Memory-less nodes can come here with 0 size in which case,
  57. * there is nothing to do.
  58. */
  59. if (!size) {
  60. return;
  61. }
  62. range = g_malloc0(sizeof(*range));
  63. range->mem_start = addr;
  64. range->mem_end = addr + size - 1;
  65. QLIST_INSERT_HEAD(&numa_info[node].addr, range, entry);
  66. }
  67. void numa_unset_mem_node_id(ram_addr_t addr, uint64_t size, uint32_t node)
  68. {
  69. struct numa_addr_range *range, *next;
  70. QLIST_FOREACH_SAFE(range, &numa_info[node].addr, entry, next) {
  71. if (addr == range->mem_start && (addr + size - 1) == range->mem_end) {
  72. QLIST_REMOVE(range, entry);
  73. g_free(range);
  74. return;
  75. }
  76. }
  77. }
  78. static void numa_set_mem_ranges(void)
  79. {
  80. int i;
  81. ram_addr_t mem_start = 0;
  82. /*
  83. * Deduce start address of each node and use it to store
  84. * the address range info in numa_info address range list
  85. */
  86. for (i = 0; i < nb_numa_nodes; i++) {
  87. numa_set_mem_node_id(mem_start, numa_info[i].node_mem, i);
  88. mem_start += numa_info[i].node_mem;
  89. }
  90. }
  91. /*
  92. * Check if @addr falls under NUMA @node.
  93. */
  94. static bool numa_addr_belongs_to_node(ram_addr_t addr, uint32_t node)
  95. {
  96. struct numa_addr_range *range;
  97. QLIST_FOREACH(range, &numa_info[node].addr, entry) {
  98. if (addr >= range->mem_start && addr <= range->mem_end) {
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. /*
  105. * Given an address, return the index of the NUMA node to which the
  106. * address belongs to.
  107. */
  108. uint32_t numa_get_node(ram_addr_t addr, Error **errp)
  109. {
  110. uint32_t i;
  111. /* For non NUMA configurations, check if the addr falls under node 0 */
  112. if (!nb_numa_nodes) {
  113. if (numa_addr_belongs_to_node(addr, 0)) {
  114. return 0;
  115. }
  116. }
  117. for (i = 0; i < nb_numa_nodes; i++) {
  118. if (numa_addr_belongs_to_node(addr, i)) {
  119. return i;
  120. }
  121. }
  122. error_setg(errp, "Address 0x" RAM_ADDR_FMT " doesn't belong to any "
  123. "NUMA node", addr);
  124. return -1;
  125. }
  126. static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
  127. {
  128. uint16_t nodenr;
  129. uint16List *cpus = NULL;
  130. if (node->has_nodeid) {
  131. nodenr = node->nodeid;
  132. } else {
  133. nodenr = nb_numa_nodes;
  134. }
  135. if (nodenr >= MAX_NODES) {
  136. error_setg(errp, "Max number of NUMA nodes reached: %"
  137. PRIu16 "", nodenr);
  138. return;
  139. }
  140. if (numa_info[nodenr].present) {
  141. error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
  142. return;
  143. }
  144. for (cpus = node->cpus; cpus; cpus = cpus->next) {
  145. if (cpus->value >= max_cpus) {
  146. error_setg(errp,
  147. "CPU index (%" PRIu16 ")"
  148. " should be smaller than maxcpus (%d)",
  149. cpus->value, max_cpus);
  150. return;
  151. }
  152. bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
  153. }
  154. if (node->has_mem && node->has_memdev) {
  155. error_setg(errp, "qemu: cannot specify both mem= and memdev=");
  156. return;
  157. }
  158. if (have_memdevs == -1) {
  159. have_memdevs = node->has_memdev;
  160. }
  161. if (node->has_memdev != have_memdevs) {
  162. error_setg(errp, "qemu: memdev option must be specified for either "
  163. "all or no nodes");
  164. return;
  165. }
  166. if (node->has_mem) {
  167. uint64_t mem_size = node->mem;
  168. const char *mem_str = qemu_opt_get(opts, "mem");
  169. /* Fix up legacy suffix-less format */
  170. if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
  171. mem_size <<= 20;
  172. }
  173. numa_info[nodenr].node_mem = mem_size;
  174. }
  175. if (node->has_memdev) {
  176. Object *o;
  177. o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
  178. if (!o) {
  179. error_setg(errp, "memdev=%s is ambiguous", node->memdev);
  180. return;
  181. }
  182. object_ref(o);
  183. numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
  184. numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
  185. }
  186. numa_info[nodenr].present = true;
  187. max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
  188. }
  189. static int parse_numa(void *opaque, QemuOpts *opts, Error **errp)
  190. {
  191. NumaOptions *object = NULL;
  192. Error *err = NULL;
  193. {
  194. Visitor *v = opts_visitor_new(opts);
  195. visit_type_NumaOptions(v, NULL, &object, &err);
  196. visit_free(v);
  197. }
  198. if (err) {
  199. goto end;
  200. }
  201. switch (object->type) {
  202. case NUMA_OPTIONS_TYPE_NODE:
  203. numa_node_parse(&object->u.node, opts, &err);
  204. if (err) {
  205. goto end;
  206. }
  207. nb_numa_nodes++;
  208. break;
  209. default:
  210. abort();
  211. }
  212. end:
  213. qapi_free_NumaOptions(object);
  214. if (err) {
  215. error_report_err(err);
  216. return -1;
  217. }
  218. return 0;
  219. }
  220. static char *enumerate_cpus(unsigned long *cpus, int max_cpus)
  221. {
  222. int cpu;
  223. bool first = true;
  224. GString *s = g_string_new(NULL);
  225. for (cpu = find_first_bit(cpus, max_cpus);
  226. cpu < max_cpus;
  227. cpu = find_next_bit(cpus, max_cpus, cpu + 1)) {
  228. g_string_append_printf(s, "%s%d", first ? "" : " ", cpu);
  229. first = false;
  230. }
  231. return g_string_free(s, FALSE);
  232. }
  233. static void validate_numa_cpus(void)
  234. {
  235. int i;
  236. unsigned long *seen_cpus = bitmap_new(max_cpus);
  237. for (i = 0; i < nb_numa_nodes; i++) {
  238. if (bitmap_intersects(seen_cpus, numa_info[i].node_cpu, max_cpus)) {
  239. bitmap_and(seen_cpus, seen_cpus,
  240. numa_info[i].node_cpu, max_cpus);
  241. error_report("CPU(s) present in multiple NUMA nodes: %s",
  242. enumerate_cpus(seen_cpus, max_cpus));
  243. g_free(seen_cpus);
  244. exit(EXIT_FAILURE);
  245. }
  246. bitmap_or(seen_cpus, seen_cpus,
  247. numa_info[i].node_cpu, max_cpus);
  248. }
  249. if (!bitmap_full(seen_cpus, max_cpus)) {
  250. char *msg;
  251. bitmap_complement(seen_cpus, seen_cpus, max_cpus);
  252. msg = enumerate_cpus(seen_cpus, max_cpus);
  253. error_report("warning: CPU(s) not present in any NUMA nodes: %s", msg);
  254. error_report("warning: All CPU(s) up to maxcpus should be described "
  255. "in NUMA config");
  256. g_free(msg);
  257. }
  258. g_free(seen_cpus);
  259. }
  260. void parse_numa_opts(MachineClass *mc)
  261. {
  262. int i;
  263. for (i = 0; i < MAX_NODES; i++) {
  264. numa_info[i].node_cpu = bitmap_new(max_cpus);
  265. }
  266. if (qemu_opts_foreach(qemu_find_opts("numa"), parse_numa, NULL, NULL)) {
  267. exit(1);
  268. }
  269. assert(max_numa_nodeid <= MAX_NODES);
  270. /* No support for sparse NUMA node IDs yet: */
  271. for (i = max_numa_nodeid - 1; i >= 0; i--) {
  272. /* Report large node IDs first, to make mistakes easier to spot */
  273. if (!numa_info[i].present) {
  274. error_report("numa: Node ID missing: %d", i);
  275. exit(1);
  276. }
  277. }
  278. /* This must be always true if all nodes are present: */
  279. assert(nb_numa_nodes == max_numa_nodeid);
  280. if (nb_numa_nodes > 0) {
  281. uint64_t numa_total;
  282. if (nb_numa_nodes > MAX_NODES) {
  283. nb_numa_nodes = MAX_NODES;
  284. }
  285. /* If no memory size is given for any node, assume the default case
  286. * and distribute the available memory equally across all nodes
  287. */
  288. for (i = 0; i < nb_numa_nodes; i++) {
  289. if (numa_info[i].node_mem != 0) {
  290. break;
  291. }
  292. }
  293. if (i == nb_numa_nodes) {
  294. uint64_t usedmem = 0;
  295. /* Align each node according to the alignment
  296. * requirements of the machine class
  297. */
  298. for (i = 0; i < nb_numa_nodes - 1; i++) {
  299. numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
  300. ~((1 << mc->numa_mem_align_shift) - 1);
  301. usedmem += numa_info[i].node_mem;
  302. }
  303. numa_info[i].node_mem = ram_size - usedmem;
  304. }
  305. numa_total = 0;
  306. for (i = 0; i < nb_numa_nodes; i++) {
  307. numa_total += numa_info[i].node_mem;
  308. }
  309. if (numa_total != ram_size) {
  310. error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
  311. " should equal RAM size (0x" RAM_ADDR_FMT ")",
  312. numa_total, ram_size);
  313. exit(1);
  314. }
  315. for (i = 0; i < nb_numa_nodes; i++) {
  316. QLIST_INIT(&numa_info[i].addr);
  317. }
  318. numa_set_mem_ranges();
  319. for (i = 0; i < nb_numa_nodes; i++) {
  320. if (!bitmap_empty(numa_info[i].node_cpu, max_cpus)) {
  321. break;
  322. }
  323. }
  324. /* Historically VCPUs were assigned in round-robin order to NUMA
  325. * nodes. However it causes issues with guest not handling it nice
  326. * in case where cores/threads from a multicore CPU appear on
  327. * different nodes. So allow boards to override default distribution
  328. * rule grouping VCPUs by socket so that VCPUs from the same socket
  329. * would be on the same node.
  330. */
  331. if (i == nb_numa_nodes) {
  332. for (i = 0; i < max_cpus; i++) {
  333. unsigned node_id = i % nb_numa_nodes;
  334. if (mc->cpu_index_to_socket_id) {
  335. node_id = mc->cpu_index_to_socket_id(i) % nb_numa_nodes;
  336. }
  337. set_bit(i, numa_info[node_id].node_cpu);
  338. }
  339. }
  340. validate_numa_cpus();
  341. } else {
  342. numa_set_mem_node_id(0, ram_size, 0);
  343. }
  344. }
  345. void numa_post_machine_init(void)
  346. {
  347. CPUState *cpu;
  348. int i;
  349. CPU_FOREACH(cpu) {
  350. for (i = 0; i < nb_numa_nodes; i++) {
  351. assert(cpu->cpu_index < max_cpus);
  352. if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
  353. cpu->numa_node = i;
  354. }
  355. }
  356. }
  357. }
  358. static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
  359. const char *name,
  360. uint64_t ram_size)
  361. {
  362. if (mem_path) {
  363. #ifdef __linux__
  364. Error *err = NULL;
  365. memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
  366. mem_path, &err);
  367. if (err) {
  368. error_report_err(err);
  369. if (mem_prealloc) {
  370. exit(1);
  371. }
  372. /* Legacy behavior: if allocation failed, fall back to
  373. * regular RAM allocation.
  374. */
  375. memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
  376. }
  377. #else
  378. fprintf(stderr, "-mem-path not supported on this host\n");
  379. exit(1);
  380. #endif
  381. } else {
  382. memory_region_init_ram(mr, owner, name, ram_size, &error_fatal);
  383. }
  384. vmstate_register_ram_global(mr);
  385. }
  386. void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
  387. const char *name,
  388. uint64_t ram_size)
  389. {
  390. uint64_t addr = 0;
  391. int i;
  392. if (nb_numa_nodes == 0 || !have_memdevs) {
  393. allocate_system_memory_nonnuma(mr, owner, name, ram_size);
  394. return;
  395. }
  396. memory_region_init(mr, owner, name, ram_size);
  397. for (i = 0; i < MAX_NODES; i++) {
  398. uint64_t size = numa_info[i].node_mem;
  399. HostMemoryBackend *backend = numa_info[i].node_memdev;
  400. if (!backend) {
  401. continue;
  402. }
  403. MemoryRegion *seg = host_memory_backend_get_memory(backend,
  404. &error_fatal);
  405. if (memory_region_is_mapped(seg)) {
  406. char *path = object_get_canonical_path_component(OBJECT(backend));
  407. error_report("memory backend %s is used multiple times. Each "
  408. "-numa option must use a different memdev value.",
  409. path);
  410. exit(1);
  411. }
  412. host_memory_backend_set_mapped(backend, true);
  413. memory_region_add_subregion(mr, addr, seg);
  414. vmstate_register_ram_global(seg);
  415. addr += size;
  416. }
  417. }
  418. static void numa_stat_memory_devices(uint64_t node_mem[])
  419. {
  420. MemoryDeviceInfoList *info_list = NULL;
  421. MemoryDeviceInfoList **prev = &info_list;
  422. MemoryDeviceInfoList *info;
  423. qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
  424. for (info = info_list; info; info = info->next) {
  425. MemoryDeviceInfo *value = info->value;
  426. if (value) {
  427. switch (value->type) {
  428. case MEMORY_DEVICE_INFO_KIND_DIMM:
  429. node_mem[value->u.dimm.data->node] += value->u.dimm.data->size;
  430. break;
  431. default:
  432. break;
  433. }
  434. }
  435. }
  436. qapi_free_MemoryDeviceInfoList(info_list);
  437. }
  438. void query_numa_node_mem(uint64_t node_mem[])
  439. {
  440. int i;
  441. if (nb_numa_nodes <= 0) {
  442. return;
  443. }
  444. numa_stat_memory_devices(node_mem);
  445. for (i = 0; i < nb_numa_nodes; i++) {
  446. node_mem[i] += numa_info[i].node_mem;
  447. }
  448. }
  449. static int query_memdev(Object *obj, void *opaque)
  450. {
  451. MemdevList **list = opaque;
  452. MemdevList *m = NULL;
  453. if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
  454. m = g_malloc0(sizeof(*m));
  455. m->value = g_malloc0(sizeof(*m->value));
  456. m->value->id = object_property_get_str(obj, "id", NULL);
  457. m->value->has_id = !!m->value->id;
  458. m->value->size = object_property_get_int(obj, "size",
  459. &error_abort);
  460. m->value->merge = object_property_get_bool(obj, "merge",
  461. &error_abort);
  462. m->value->dump = object_property_get_bool(obj, "dump",
  463. &error_abort);
  464. m->value->prealloc = object_property_get_bool(obj,
  465. "prealloc",
  466. &error_abort);
  467. m->value->policy = object_property_get_enum(obj,
  468. "policy",
  469. "HostMemPolicy",
  470. &error_abort);
  471. object_property_get_uint16List(obj, "host-nodes",
  472. &m->value->host_nodes,
  473. &error_abort);
  474. m->next = *list;
  475. *list = m;
  476. }
  477. return 0;
  478. }
  479. MemdevList *qmp_query_memdev(Error **errp)
  480. {
  481. Object *obj = object_get_objects_root();
  482. MemdevList *list = NULL;
  483. object_child_foreach(obj, query_memdev, &list);
  484. return list;
  485. }
  486. int numa_get_node_for_cpu(int idx)
  487. {
  488. int i;
  489. assert(idx < max_cpus);
  490. for (i = 0; i < nb_numa_nodes; i++) {
  491. if (test_bit(idx, numa_info[i].node_cpu)) {
  492. break;
  493. }
  494. }
  495. return i;
  496. }
  497. void ram_block_notifier_add(RAMBlockNotifier *n)
  498. {
  499. QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next);
  500. }
  501. void ram_block_notifier_remove(RAMBlockNotifier *n)
  502. {
  503. QLIST_REMOVE(n, next);
  504. }
  505. void ram_block_notify_add(void *host, size_t size)
  506. {
  507. RAMBlockNotifier *notifier;
  508. QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
  509. notifier->ram_block_added(notifier, host, size);
  510. }
  511. }
  512. void ram_block_notify_remove(void *host, size_t size)
  513. {
  514. RAMBlockNotifier *notifier;
  515. QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) {
  516. notifier->ram_block_removed(notifier, host, size);
  517. }
  518. }