blob: a6d55308660f7e50317356fa3da8ae6ec175490f [file] [log] [blame]
Tejun Heob8ef9172011-02-22 11:10:08 +01001/*
2 * NUMA emulation
3 */
4#include <linux/kernel.h>
5#include <linux/errno.h>
6#include <linux/topology.h>
7#include <linux/memblock.h>
Tejun Heo1b7e03e2011-05-02 17:24:48 +02008#include <linux/bootmem.h>
Tejun Heob8ef9172011-02-22 11:10:08 +01009#include <asm/dma.h>
10
11#include "numa_internal.h"
12
Paul Gortmaker148f9bb2013-06-18 18:23:59 -040013static int emu_nid_to_phys[MAX_NUMNODES];
Tejun Heob8ef9172011-02-22 11:10:08 +010014static char *emu_cmdline __initdata;
15
16void __init numa_emu_cmdline(char *str)
17{
18 emu_cmdline = str;
19}
20
21static int __init emu_find_memblk_by_nid(int nid, const struct numa_meminfo *mi)
22{
23 int i;
24
25 for (i = 0; i < mi->nr_blks; i++)
26 if (mi->blk[i].nid == nid)
27 return i;
28 return -ENOENT;
29}
30
Jiri Kosinae37aade2012-02-28 16:16:33 +010031static u64 __init mem_hole_size(u64 start, u64 end)
Tejun Heo474b8812011-07-12 11:16:04 +020032{
33 unsigned long start_pfn = PFN_UP(start);
34 unsigned long end_pfn = PFN_DOWN(end);
35
36 if (start_pfn < end_pfn)
37 return PFN_PHYS(absent_pages_in_range(start_pfn, end_pfn));
38 return 0;
39}
40
Tejun Heob8ef9172011-02-22 11:10:08 +010041/*
42 * Sets up nid to range from @start to @end. The return value is -errno if
43 * something went wrong, 0 otherwise.
44 */
45static int __init emu_setup_memblk(struct numa_meminfo *ei,
46 struct numa_meminfo *pi,
47 int nid, int phys_blk, u64 size)
48{
49 struct numa_memblk *eb = &ei->blk[ei->nr_blks];
50 struct numa_memblk *pb = &pi->blk[phys_blk];
51
52 if (ei->nr_blks >= NR_NODE_MEMBLKS) {
53 pr_err("NUMA: Too many emulated memblks, failing emulation\n");
54 return -EINVAL;
55 }
56
57 ei->nr_blks++;
58 eb->start = pb->start;
59 eb->end = pb->start + size;
60 eb->nid = nid;
61
62 if (emu_nid_to_phys[nid] == NUMA_NO_NODE)
Andrea Arcangelid71b5a72012-03-21 16:34:16 -070063 emu_nid_to_phys[nid] = nid;
Tejun Heob8ef9172011-02-22 11:10:08 +010064
65 pb->start += size;
66 if (pb->start >= pb->end) {
67 WARN_ON_ONCE(pb->start > pb->end);
68 numa_remove_memblk_from(phys_blk, pi);
69 }
70
Bjorn Helgaas365811d2012-05-29 15:06:29 -070071 printk(KERN_INFO "Faking node %d at [mem %#018Lx-%#018Lx] (%LuMB)\n",
72 nid, eb->start, eb->end - 1, (eb->end - eb->start) >> 20);
Tejun Heob8ef9172011-02-22 11:10:08 +010073 return 0;
74}
75
76/*
77 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
78 * to max_addr. The return value is the number of nodes allocated.
79 */
80static int __init split_nodes_interleave(struct numa_meminfo *ei,
81 struct numa_meminfo *pi,
82 u64 addr, u64 max_addr, int nr_nodes)
83{
84 nodemask_t physnode_mask = NODE_MASK_NONE;
85 u64 size;
86 int big;
87 int nid = 0;
88 int i, ret;
89
90 if (nr_nodes <= 0)
91 return -1;
92 if (nr_nodes > MAX_NUMNODES) {
93 pr_info("numa=fake=%d too large, reducing to %d\n",
94 nr_nodes, MAX_NUMNODES);
95 nr_nodes = MAX_NUMNODES;
96 }
97
Tejun Heo1b7e03e2011-05-02 17:24:48 +020098 /*
99 * Calculate target node size. x86_32 freaks on __udivdi3() so do
100 * the division in ulong number of pages and convert back.
101 */
Tejun Heo474b8812011-07-12 11:16:04 +0200102 size = max_addr - addr - mem_hole_size(addr, max_addr);
Tejun Heo1b7e03e2011-05-02 17:24:48 +0200103 size = PFN_PHYS((unsigned long)(size >> PAGE_SHIFT) / nr_nodes);
104
Tejun Heob8ef9172011-02-22 11:10:08 +0100105 /*
106 * Calculate the number of big nodes that can be allocated as a result
107 * of consolidating the remainder.
108 */
109 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
110 FAKE_NODE_MIN_SIZE;
111
112 size &= FAKE_NODE_MIN_HASH_MASK;
113 if (!size) {
114 pr_err("Not enough memory for each node. "
115 "NUMA emulation disabled.\n");
116 return -1;
117 }
118
119 for (i = 0; i < pi->nr_blks; i++)
120 node_set(pi->blk[i].nid, physnode_mask);
121
122 /*
123 * Continue to fill physical nodes with fake nodes until there is no
124 * memory left on any of them.
125 */
126 while (nodes_weight(physnode_mask)) {
127 for_each_node_mask(i, physnode_mask) {
128 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
129 u64 start, limit, end;
130 int phys_blk;
131
132 phys_blk = emu_find_memblk_by_nid(i, pi);
133 if (phys_blk < 0) {
134 node_clear(i, physnode_mask);
135 continue;
136 }
137 start = pi->blk[phys_blk].start;
138 limit = pi->blk[phys_blk].end;
139 end = start + size;
140
141 if (nid < big)
142 end += FAKE_NODE_MIN_SIZE;
143
144 /*
145 * Continue to add memory to this fake node if its
146 * non-reserved memory is less than the per-node size.
147 */
Tejun Heo474b8812011-07-12 11:16:04 +0200148 while (end - start - mem_hole_size(start, end) < size) {
Tejun Heob8ef9172011-02-22 11:10:08 +0100149 end += FAKE_NODE_MIN_SIZE;
150 if (end > limit) {
151 end = limit;
152 break;
153 }
154 }
155
156 /*
157 * If there won't be at least FAKE_NODE_MIN_SIZE of
158 * non-reserved memory in ZONE_DMA32 for the next node,
159 * this one must extend to the boundary.
160 */
161 if (end < dma32_end && dma32_end - end -
Tejun Heo474b8812011-07-12 11:16:04 +0200162 mem_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
Tejun Heob8ef9172011-02-22 11:10:08 +0100163 end = dma32_end;
164
165 /*
166 * If there won't be enough non-reserved memory for the
167 * next node, this one must extend to the end of the
168 * physical node.
169 */
Tejun Heo474b8812011-07-12 11:16:04 +0200170 if (limit - end - mem_hole_size(end, limit) < size)
Tejun Heob8ef9172011-02-22 11:10:08 +0100171 end = limit;
172
173 ret = emu_setup_memblk(ei, pi, nid++ % nr_nodes,
174 phys_blk,
175 min(end, limit) - start);
176 if (ret < 0)
177 return ret;
178 }
179 }
180 return 0;
181}
182
183/*
184 * Returns the end address of a node so that there is at least `size' amount of
185 * non-reserved memory or `max_addr' is reached.
186 */
187static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
188{
189 u64 end = start + size;
190
Tejun Heo474b8812011-07-12 11:16:04 +0200191 while (end - start - mem_hole_size(start, end) < size) {
Tejun Heob8ef9172011-02-22 11:10:08 +0100192 end += FAKE_NODE_MIN_SIZE;
193 if (end > max_addr) {
194 end = max_addr;
195 break;
196 }
197 }
198 return end;
199}
200
201/*
202 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
203 * `addr' to `max_addr'. The return value is the number of nodes allocated.
204 */
205static int __init split_nodes_size_interleave(struct numa_meminfo *ei,
206 struct numa_meminfo *pi,
207 u64 addr, u64 max_addr, u64 size)
208{
209 nodemask_t physnode_mask = NODE_MASK_NONE;
210 u64 min_size;
211 int nid = 0;
212 int i, ret;
213
214 if (!size)
215 return -1;
216 /*
217 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
218 * increased accordingly if the requested size is too small. This
219 * creates a uniform distribution of node sizes across the entire
220 * machine (but not necessarily over physical nodes).
221 */
Tejun Heo474b8812011-07-12 11:16:04 +0200222 min_size = (max_addr - addr - mem_hole_size(addr, max_addr)) / MAX_NUMNODES;
Tejun Heob8ef9172011-02-22 11:10:08 +0100223 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
224 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
225 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
226 FAKE_NODE_MIN_HASH_MASK;
227 if (size < min_size) {
228 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
229 size >> 20, min_size >> 20);
230 size = min_size;
231 }
232 size &= FAKE_NODE_MIN_HASH_MASK;
233
234 for (i = 0; i < pi->nr_blks; i++)
235 node_set(pi->blk[i].nid, physnode_mask);
236
237 /*
238 * Fill physical nodes with fake nodes of size until there is no memory
239 * left on any of them.
240 */
241 while (nodes_weight(physnode_mask)) {
242 for_each_node_mask(i, physnode_mask) {
Tejun Heo1b7e03e2011-05-02 17:24:48 +0200243 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
Tejun Heob8ef9172011-02-22 11:10:08 +0100244 u64 start, limit, end;
245 int phys_blk;
246
247 phys_blk = emu_find_memblk_by_nid(i, pi);
248 if (phys_blk < 0) {
249 node_clear(i, physnode_mask);
250 continue;
251 }
252 start = pi->blk[phys_blk].start;
253 limit = pi->blk[phys_blk].end;
254
255 end = find_end_of_node(start, limit, size);
256 /*
257 * If there won't be at least FAKE_NODE_MIN_SIZE of
258 * non-reserved memory in ZONE_DMA32 for the next node,
259 * this one must extend to the boundary.
260 */
261 if (end < dma32_end && dma32_end - end -
Tejun Heo474b8812011-07-12 11:16:04 +0200262 mem_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
Tejun Heob8ef9172011-02-22 11:10:08 +0100263 end = dma32_end;
264
265 /*
266 * If there won't be enough non-reserved memory for the
267 * next node, this one must extend to the end of the
268 * physical node.
269 */
Tejun Heo474b8812011-07-12 11:16:04 +0200270 if (limit - end - mem_hole_size(end, limit) < size)
Tejun Heob8ef9172011-02-22 11:10:08 +0100271 end = limit;
272
273 ret = emu_setup_memblk(ei, pi, nid++ % MAX_NUMNODES,
274 phys_blk,
275 min(end, limit) - start);
276 if (ret < 0)
277 return ret;
278 }
279 }
280 return 0;
281}
282
Wei Yang158f4242017-07-08 09:30:57 +0800283int __init setup_emu2phys_nid(int *dfl_phys_nid)
284{
285 int i, max_emu_nid = 0;
286
287 *dfl_phys_nid = NUMA_NO_NODE;
288 for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++) {
289 if (emu_nid_to_phys[i] != NUMA_NO_NODE) {
290 max_emu_nid = i;
291 if (*dfl_phys_nid == NUMA_NO_NODE)
292 *dfl_phys_nid = emu_nid_to_phys[i];
293 }
294 }
295
296 return max_emu_nid;
297}
298
Tejun Heo90e6b672011-02-22 11:10:08 +0100299/**
300 * numa_emulation - Emulate NUMA nodes
301 * @numa_meminfo: NUMA configuration to massage
302 * @numa_dist_cnt: The size of the physical NUMA distance table
303 *
304 * Emulate NUMA nodes according to the numa=fake kernel parameter.
305 * @numa_meminfo contains the physical memory configuration and is modified
306 * to reflect the emulated configuration on success. @numa_dist_cnt is
307 * used to determine the size of the physical distance table.
308 *
309 * On success, the following modifications are made.
310 *
311 * - @numa_meminfo is updated to reflect the emulated nodes.
312 *
313 * - __apicid_to_node[] is updated such that APIC IDs are mapped to the
314 * emulated nodes.
315 *
316 * - NUMA distance table is rebuilt to represent distances between emulated
317 * nodes. The distances are determined considering how emulated nodes
318 * are mapped to physical nodes and match the actual distances.
319 *
320 * - emu_nid_to_phys[] reflects how emulated nodes are mapped to physical
321 * nodes. This is used by numa_add_cpu() and numa_remove_cpu().
322 *
323 * If emulation is not enabled or fails, emu_nid_to_phys[] is filled with
324 * identity mapping and no other modification is made.
Tejun Heob8ef9172011-02-22 11:10:08 +0100325 */
326void __init numa_emulation(struct numa_meminfo *numa_meminfo, int numa_dist_cnt)
327{
328 static struct numa_meminfo ei __initdata;
329 static struct numa_meminfo pi __initdata;
Tejun Heo1b7e03e2011-05-02 17:24:48 +0200330 const u64 max_addr = PFN_PHYS(max_pfn);
Tejun Heob8ef9172011-02-22 11:10:08 +0100331 u8 *phys_dist = NULL;
Yinghai Luce003332011-03-02 11:22:14 +0100332 size_t phys_size = numa_dist_cnt * numa_dist_cnt * sizeof(phys_dist[0]);
Tejun Heo56396e62011-03-11 10:33:31 +0100333 int max_emu_nid, dfl_phys_nid;
Tejun Heob8ef9172011-02-22 11:10:08 +0100334 int i, j, ret;
335
336 if (!emu_cmdline)
337 goto no_emu;
338
339 memset(&ei, 0, sizeof(ei));
340 pi = *numa_meminfo;
341
342 for (i = 0; i < MAX_NUMNODES; i++)
343 emu_nid_to_phys[i] = NUMA_NO_NODE;
344
345 /*
346 * If the numa=fake command-line contains a 'M' or 'G', it represents
347 * the fixed node size. Otherwise, if it is just a single number N,
348 * split the system RAM into N fake nodes.
349 */
350 if (strchr(emu_cmdline, 'M') || strchr(emu_cmdline, 'G')) {
351 u64 size;
352
353 size = memparse(emu_cmdline, &emu_cmdline);
354 ret = split_nodes_size_interleave(&ei, &pi, 0, max_addr, size);
355 } else {
356 unsigned long n;
357
Peter Zijlstra94c0dd32012-04-18 19:04:17 +0200358 n = simple_strtoul(emu_cmdline, &emu_cmdline, 0);
Tejun Heob8ef9172011-02-22 11:10:08 +0100359 ret = split_nodes_interleave(&ei, &pi, 0, max_addr, n);
360 }
Peter Zijlstra94c0dd32012-04-18 19:04:17 +0200361 if (*emu_cmdline == ':')
362 emu_cmdline++;
Tejun Heob8ef9172011-02-22 11:10:08 +0100363
364 if (ret < 0)
365 goto no_emu;
366
367 if (numa_cleanup_meminfo(&ei) < 0) {
368 pr_warning("NUMA: Warning: constructed meminfo invalid, disabling emulation\n");
369 goto no_emu;
370 }
371
Yinghai Luce003332011-03-02 11:22:14 +0100372 /* copy the physical distance table */
Tejun Heob8ef9172011-02-22 11:10:08 +0100373 if (numa_dist_cnt) {
Tejun Heob8ef9172011-02-22 11:10:08 +0100374 u64 phys;
375
Tejun Heo1b7e03e2011-05-02 17:24:48 +0200376 phys = memblock_find_in_range(0, PFN_PHYS(max_pfn_mapped),
Yinghai Luce003332011-03-02 11:22:14 +0100377 phys_size, PAGE_SIZE);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200378 if (!phys) {
Tejun Heob8ef9172011-02-22 11:10:08 +0100379 pr_warning("NUMA: Warning: can't allocate copy of distance table, disabling emulation\n");
380 goto no_emu;
381 }
Tejun Heo24aa0782011-07-12 11:16:06 +0200382 memblock_reserve(phys, phys_size);
Tejun Heob8ef9172011-02-22 11:10:08 +0100383 phys_dist = __va(phys);
384
385 for (i = 0; i < numa_dist_cnt; i++)
386 for (j = 0; j < numa_dist_cnt; j++)
387 phys_dist[i * numa_dist_cnt + j] =
388 node_distance(i, j);
389 }
390
Tejun Heo56396e62011-03-11 10:33:31 +0100391 /*
392 * Determine the max emulated nid and the default phys nid to use
393 * for unmapped nodes.
394 */
Wei Yang158f4242017-07-08 09:30:57 +0800395 max_emu_nid = setup_emu2phys_nid(&dfl_phys_nid);
Tejun Heo078a1982011-03-04 16:32:02 +0100396
Tejun Heob8ef9172011-02-22 11:10:08 +0100397 /* commit */
398 *numa_meminfo = ei;
399
400 /*
401 * Transform __apicid_to_node table to use emulated nids by
402 * reverse-mapping phys_nid. The maps should always exist but fall
403 * back to zero just in case.
404 */
405 for (i = 0; i < ARRAY_SIZE(__apicid_to_node); i++) {
406 if (__apicid_to_node[i] == NUMA_NO_NODE)
407 continue;
408 for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
409 if (__apicid_to_node[i] == emu_nid_to_phys[j])
410 break;
411 __apicid_to_node[i] = j < ARRAY_SIZE(emu_nid_to_phys) ? j : 0;
412 }
413
414 /* make sure all emulated nodes are mapped to a physical node */
415 for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
416 if (emu_nid_to_phys[i] == NUMA_NO_NODE)
Tejun Heo078a1982011-03-04 16:32:02 +0100417 emu_nid_to_phys[i] = dfl_phys_nid;
Tejun Heob8ef9172011-02-22 11:10:08 +0100418
Tejun Heo56396e62011-03-11 10:33:31 +0100419 /* transform distance table */
Tejun Heob8ef9172011-02-22 11:10:08 +0100420 numa_reset_distance();
Tejun Heo56396e62011-03-11 10:33:31 +0100421 for (i = 0; i < max_emu_nid + 1; i++) {
422 for (j = 0; j < max_emu_nid + 1; j++) {
Tejun Heob8ef9172011-02-22 11:10:08 +0100423 int physi = emu_nid_to_phys[i];
424 int physj = emu_nid_to_phys[j];
425 int dist;
426
Peter Zijlstra94c0dd32012-04-18 19:04:17 +0200427 if (get_option(&emu_cmdline, &dist) == 2)
428 ;
429 else if (physi >= numa_dist_cnt || physj >= numa_dist_cnt)
Tejun Heob8ef9172011-02-22 11:10:08 +0100430 dist = physi == physj ?
431 LOCAL_DISTANCE : REMOTE_DISTANCE;
432 else
433 dist = phys_dist[physi * numa_dist_cnt + physj];
434
435 numa_set_distance(i, j, dist);
436 }
437 }
Yinghai Luce003332011-03-02 11:22:14 +0100438
439 /* free the copied physical distance table */
440 if (phys_dist)
Tejun Heo24aa0782011-07-12 11:16:06 +0200441 memblock_free(__pa(phys_dist), phys_size);
Tejun Heob8ef9172011-02-22 11:10:08 +0100442 return;
443
444no_emu:
445 /* No emulation. Build identity emu_nid_to_phys[] for numa_add_cpu() */
446 for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
447 emu_nid_to_phys[i] = i;
448}
449
450#ifndef CONFIG_DEBUG_PER_CPU_MAPS
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400451void numa_add_cpu(int cpu)
Tejun Heob8ef9172011-02-22 11:10:08 +0100452{
453 int physnid, nid;
454
Yinghai Lu51b361b2011-03-04 14:49:28 +0100455 nid = early_cpu_to_node(cpu);
Tejun Heob8ef9172011-02-22 11:10:08 +0100456 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
457
458 physnid = emu_nid_to_phys[nid];
459
460 /*
461 * Map the cpu to each emulated node that is allocated on the physical
462 * node of the cpu's apic id.
463 */
464 for_each_online_node(nid)
465 if (emu_nid_to_phys[nid] == physnid)
466 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
467}
468
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400469void numa_remove_cpu(int cpu)
Tejun Heob8ef9172011-02-22 11:10:08 +0100470{
471 int i;
472
473 for_each_online_node(i)
474 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
475}
476#else /* !CONFIG_DEBUG_PER_CPU_MAPS */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400477static void numa_set_cpumask(int cpu, bool enable)
Tejun Heob8ef9172011-02-22 11:10:08 +0100478{
David Rientjes7a6c6542011-04-20 19:19:13 -0700479 int nid, physnid;
Tejun Heob8ef9172011-02-22 11:10:08 +0100480
481 nid = early_cpu_to_node(cpu);
482 if (nid == NUMA_NO_NODE) {
483 /* early_cpu_to_node() already emits a warning and trace */
484 return;
485 }
486
487 physnid = emu_nid_to_phys[nid];
488
David Rientjes7a6c6542011-04-20 19:19:13 -0700489 for_each_online_node(nid) {
Tejun Heob8ef9172011-02-22 11:10:08 +0100490 if (emu_nid_to_phys[nid] != physnid)
491 continue;
492
David Rientjes7a6c6542011-04-20 19:19:13 -0700493 debug_cpumask_set_cpu(cpu, nid, enable);
Tejun Heob8ef9172011-02-22 11:10:08 +0100494 }
495}
496
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400497void numa_add_cpu(int cpu)
Tejun Heob8ef9172011-02-22 11:10:08 +0100498{
David Rientjes7a6c6542011-04-20 19:19:13 -0700499 numa_set_cpumask(cpu, true);
Tejun Heob8ef9172011-02-22 11:10:08 +0100500}
501
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400502void numa_remove_cpu(int cpu)
Tejun Heob8ef9172011-02-22 11:10:08 +0100503{
David Rientjes7a6c6542011-04-20 19:19:13 -0700504 numa_set_cpumask(cpu, false);
Tejun Heob8ef9172011-02-22 11:10:08 +0100505}
506#endif /* !CONFIG_DEBUG_PER_CPU_MAPS */