numa.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 "sysemu/sysemu.h"
  25. #include "exec/cpu-common.h"
  26. #include "qemu/bitmap.h"
  27. #include "qom/cpu.h"
  28. #include "qemu/error-report.h"
  29. #include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */
  30. #include "qapi-visit.h"
  31. #include "qapi/opts-visitor.h"
  32. #include "qapi/dealloc-visitor.h"
  33. #include "qapi/qmp/qerror.h"
  34. #include "hw/boards.h"
  35. #include "sysemu/hostmem.h"
  36. #include "qmp-commands.h"
  37. #include "hw/mem/pc-dimm.h"
  38. QemuOptsList qemu_numa_opts = {
  39. .name = "numa",
  40. .implied_opt_name = "type",
  41. .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
  42. .desc = { { 0 } } /* validated with OptsVisitor */
  43. };
  44. static int have_memdevs = -1;
  45. static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp)
  46. {
  47. uint16_t nodenr;
  48. uint16List *cpus = NULL;
  49. if (node->has_nodeid) {
  50. nodenr = node->nodeid;
  51. } else {
  52. nodenr = nb_numa_nodes;
  53. }
  54. if (nodenr >= MAX_NODES) {
  55. error_setg(errp, "Max number of NUMA nodes reached: %"
  56. PRIu16 "\n", nodenr);
  57. return;
  58. }
  59. if (numa_info[nodenr].present) {
  60. error_setg(errp, "Duplicate NUMA nodeid: %" PRIu16, nodenr);
  61. return;
  62. }
  63. for (cpus = node->cpus; cpus; cpus = cpus->next) {
  64. if (cpus->value > MAX_CPUMASK_BITS) {
  65. error_setg(errp, "CPU number %" PRIu16 " is bigger than %d",
  66. cpus->value, MAX_CPUMASK_BITS);
  67. return;
  68. }
  69. bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1);
  70. }
  71. if (node->has_mem && node->has_memdev) {
  72. error_setg(errp, "qemu: cannot specify both mem= and memdev=\n");
  73. return;
  74. }
  75. if (have_memdevs == -1) {
  76. have_memdevs = node->has_memdev;
  77. }
  78. if (node->has_memdev != have_memdevs) {
  79. error_setg(errp, "qemu: memdev option must be specified for either "
  80. "all or no nodes\n");
  81. return;
  82. }
  83. if (node->has_mem) {
  84. uint64_t mem_size = node->mem;
  85. const char *mem_str = qemu_opt_get(opts, "mem");
  86. /* Fix up legacy suffix-less format */
  87. if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
  88. mem_size <<= 20;
  89. }
  90. numa_info[nodenr].node_mem = mem_size;
  91. }
  92. if (node->has_memdev) {
  93. Object *o;
  94. o = object_resolve_path_type(node->memdev, TYPE_MEMORY_BACKEND, NULL);
  95. if (!o) {
  96. error_setg(errp, "memdev=%s is ambiguous", node->memdev);
  97. return;
  98. }
  99. object_ref(o);
  100. numa_info[nodenr].node_mem = object_property_get_int(o, "size", NULL);
  101. numa_info[nodenr].node_memdev = MEMORY_BACKEND(o);
  102. }
  103. numa_info[nodenr].present = true;
  104. max_numa_nodeid = MAX(max_numa_nodeid, nodenr + 1);
  105. }
  106. int numa_init_func(QemuOpts *opts, void *opaque)
  107. {
  108. NumaOptions *object = NULL;
  109. Error *err = NULL;
  110. {
  111. OptsVisitor *ov = opts_visitor_new(opts);
  112. visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err);
  113. opts_visitor_cleanup(ov);
  114. }
  115. if (err) {
  116. goto error;
  117. }
  118. switch (object->kind) {
  119. case NUMA_OPTIONS_KIND_NODE:
  120. numa_node_parse(object->node, opts, &err);
  121. if (err) {
  122. goto error;
  123. }
  124. nb_numa_nodes++;
  125. break;
  126. default:
  127. abort();
  128. }
  129. return 0;
  130. error:
  131. qerror_report_err(err);
  132. error_free(err);
  133. if (object) {
  134. QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
  135. visit_type_NumaOptions(qapi_dealloc_get_visitor(dv),
  136. &object, NULL, NULL);
  137. qapi_dealloc_visitor_cleanup(dv);
  138. }
  139. return -1;
  140. }
  141. void set_numa_nodes(void)
  142. {
  143. int i;
  144. assert(max_numa_nodeid <= MAX_NODES);
  145. /* No support for sparse NUMA node IDs yet: */
  146. for (i = max_numa_nodeid - 1; i >= 0; i--) {
  147. /* Report large node IDs first, to make mistakes easier to spot */
  148. if (!numa_info[i].present) {
  149. error_report("numa: Node ID missing: %d", i);
  150. exit(1);
  151. }
  152. }
  153. /* This must be always true if all nodes are present: */
  154. assert(nb_numa_nodes == max_numa_nodeid);
  155. if (nb_numa_nodes > 0) {
  156. uint64_t numa_total;
  157. if (nb_numa_nodes > MAX_NODES) {
  158. nb_numa_nodes = MAX_NODES;
  159. }
  160. /* If no memory size is given for any node, assume the default case
  161. * and distribute the available memory equally across all nodes
  162. */
  163. for (i = 0; i < nb_numa_nodes; i++) {
  164. if (numa_info[i].node_mem != 0) {
  165. break;
  166. }
  167. }
  168. if (i == nb_numa_nodes) {
  169. uint64_t usedmem = 0;
  170. /* On Linux, each node's border has to be 8MB aligned,
  171. * the final node gets the rest.
  172. */
  173. for (i = 0; i < nb_numa_nodes - 1; i++) {
  174. numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
  175. ~((1 << 23UL) - 1);
  176. usedmem += numa_info[i].node_mem;
  177. }
  178. numa_info[i].node_mem = ram_size - usedmem;
  179. }
  180. numa_total = 0;
  181. for (i = 0; i < nb_numa_nodes; i++) {
  182. numa_total += numa_info[i].node_mem;
  183. }
  184. if (numa_total != ram_size) {
  185. error_report("total memory for NUMA nodes (0x%" PRIx64 ")"
  186. " should equal RAM size (0x" RAM_ADDR_FMT ")",
  187. numa_total, ram_size);
  188. exit(1);
  189. }
  190. for (i = 0; i < nb_numa_nodes; i++) {
  191. if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
  192. break;
  193. }
  194. }
  195. /* assigning the VCPUs round-robin is easier to implement, guest OSes
  196. * must cope with this anyway, because there are BIOSes out there in
  197. * real machines which also use this scheme.
  198. */
  199. if (i == nb_numa_nodes) {
  200. for (i = 0; i < max_cpus; i++) {
  201. set_bit(i, numa_info[i % nb_numa_nodes].node_cpu);
  202. }
  203. }
  204. }
  205. }
  206. void set_numa_modes(void)
  207. {
  208. CPUState *cpu;
  209. int i;
  210. CPU_FOREACH(cpu) {
  211. for (i = 0; i < nb_numa_nodes; i++) {
  212. if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
  213. cpu->numa_node = i;
  214. }
  215. }
  216. }
  217. }
  218. static void allocate_system_memory_nonnuma(MemoryRegion *mr, Object *owner,
  219. const char *name,
  220. uint64_t ram_size)
  221. {
  222. if (mem_path) {
  223. #ifdef __linux__
  224. Error *err = NULL;
  225. memory_region_init_ram_from_file(mr, owner, name, ram_size, false,
  226. mem_path, &err);
  227. /* Legacy behavior: if allocation failed, fall back to
  228. * regular RAM allocation.
  229. */
  230. if (err) {
  231. qerror_report_err(err);
  232. error_free(err);
  233. memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
  234. }
  235. #else
  236. fprintf(stderr, "-mem-path not supported on this host\n");
  237. exit(1);
  238. #endif
  239. } else {
  240. memory_region_init_ram(mr, owner, name, ram_size, &error_abort);
  241. }
  242. vmstate_register_ram_global(mr);
  243. }
  244. void memory_region_allocate_system_memory(MemoryRegion *mr, Object *owner,
  245. const char *name,
  246. uint64_t ram_size)
  247. {
  248. uint64_t addr = 0;
  249. int i;
  250. if (nb_numa_nodes == 0 || !have_memdevs) {
  251. allocate_system_memory_nonnuma(mr, owner, name, ram_size);
  252. return;
  253. }
  254. memory_region_init(mr, owner, name, ram_size);
  255. for (i = 0; i < MAX_NODES; i++) {
  256. Error *local_err = NULL;
  257. uint64_t size = numa_info[i].node_mem;
  258. HostMemoryBackend *backend = numa_info[i].node_memdev;
  259. if (!backend) {
  260. continue;
  261. }
  262. MemoryRegion *seg = host_memory_backend_get_memory(backend, &local_err);
  263. if (local_err) {
  264. qerror_report_err(local_err);
  265. exit(1);
  266. }
  267. if (memory_region_is_mapped(seg)) {
  268. char *path = object_get_canonical_path_component(OBJECT(backend));
  269. error_report("memory backend %s is used multiple times. Each "
  270. "-numa option must use a different memdev value.",
  271. path);
  272. exit(1);
  273. }
  274. memory_region_add_subregion(mr, addr, seg);
  275. vmstate_register_ram_global(seg);
  276. addr += size;
  277. }
  278. }
  279. static void numa_stat_memory_devices(uint64_t node_mem[])
  280. {
  281. MemoryDeviceInfoList *info_list = NULL;
  282. MemoryDeviceInfoList **prev = &info_list;
  283. MemoryDeviceInfoList *info;
  284. qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
  285. for (info = info_list; info; info = info->next) {
  286. MemoryDeviceInfo *value = info->value;
  287. if (value) {
  288. switch (value->kind) {
  289. case MEMORY_DEVICE_INFO_KIND_DIMM:
  290. node_mem[value->dimm->node] += value->dimm->size;
  291. break;
  292. default:
  293. break;
  294. }
  295. }
  296. }
  297. qapi_free_MemoryDeviceInfoList(info_list);
  298. }
  299. void query_numa_node_mem(uint64_t node_mem[])
  300. {
  301. int i;
  302. if (nb_numa_nodes <= 0) {
  303. return;
  304. }
  305. numa_stat_memory_devices(node_mem);
  306. for (i = 0; i < nb_numa_nodes; i++) {
  307. node_mem[i] += numa_info[i].node_mem;
  308. }
  309. }
  310. static int query_memdev(Object *obj, void *opaque)
  311. {
  312. MemdevList **list = opaque;
  313. MemdevList *m = NULL;
  314. Error *err = NULL;
  315. if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
  316. m = g_malloc0(sizeof(*m));
  317. m->value = g_malloc0(sizeof(*m->value));
  318. m->value->size = object_property_get_int(obj, "size",
  319. &err);
  320. if (err) {
  321. goto error;
  322. }
  323. m->value->merge = object_property_get_bool(obj, "merge",
  324. &err);
  325. if (err) {
  326. goto error;
  327. }
  328. m->value->dump = object_property_get_bool(obj, "dump",
  329. &err);
  330. if (err) {
  331. goto error;
  332. }
  333. m->value->prealloc = object_property_get_bool(obj,
  334. "prealloc", &err);
  335. if (err) {
  336. goto error;
  337. }
  338. m->value->policy = object_property_get_enum(obj,
  339. "policy",
  340. HostMemPolicy_lookup,
  341. &err);
  342. if (err) {
  343. goto error;
  344. }
  345. object_property_get_uint16List(obj, "host-nodes",
  346. &m->value->host_nodes, &err);
  347. if (err) {
  348. goto error;
  349. }
  350. m->next = *list;
  351. *list = m;
  352. }
  353. return 0;
  354. error:
  355. g_free(m->value);
  356. g_free(m);
  357. return -1;
  358. }
  359. MemdevList *qmp_query_memdev(Error **errp)
  360. {
  361. Object *obj;
  362. MemdevList *list = NULL;
  363. obj = object_resolve_path("/objects", NULL);
  364. if (obj == NULL) {
  365. return NULL;
  366. }
  367. if (object_child_foreach(obj, query_memdev, &list) != 0) {
  368. goto error;
  369. }
  370. return list;
  371. error:
  372. qapi_free_MemdevList(list);
  373. return NULL;
  374. }