numa.c 11 KB

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