1
2
3
4
5#include <linux/kernel.h>
6#include <linux/errno.h>
7#include <linux/topology.h>
8#include <linux/memblock.h>
9#include <asm/dma.h>
10
11#include "numa_internal.h"
12
13static int emu_nid_to_phys[MAX_NUMNODES];
14static char *emu_cmdline __initdata;
15
16int __init numa_emu_cmdline(char *str)
17{
18 emu_cmdline = str;
19 return 0;
20}
21
22static int __init emu_find_memblk_by_nid(int nid, const struct numa_meminfo *mi)
23{
24 int i;
25
26 for (i = 0; i < mi->nr_blks; i++)
27 if (mi->blk[i].nid == nid)
28 return i;
29 return -ENOENT;
30}
31
32static u64 __init mem_hole_size(u64 start, u64 end)
33{
34 unsigned long start_pfn = PFN_UP(start);
35 unsigned long end_pfn = PFN_DOWN(end);
36
37 if (start_pfn < end_pfn)
38 return PFN_PHYS(absent_pages_in_range(start_pfn, end_pfn));
39 return 0;
40}
41
42
43
44
45
46static int __init emu_setup_memblk(struct numa_meminfo *ei,
47 struct numa_meminfo *pi,
48 int nid, int phys_blk, u64 size)
49{
50 struct numa_memblk *eb = &ei->blk[ei->nr_blks];
51 struct numa_memblk *pb = &pi->blk[phys_blk];
52
53 if (ei->nr_blks >= NR_NODE_MEMBLKS) {
54 pr_err("NUMA: Too many emulated memblks, failing emulation\n");
55 return -EINVAL;
56 }
57
58 ei->nr_blks++;
59 eb->start = pb->start;
60 eb->end = pb->start + size;
61 eb->nid = nid;
62
63 if (emu_nid_to_phys[nid] == NUMA_NO_NODE)
64 emu_nid_to_phys[nid] = pb->nid;
65
66 pb->start += size;
67 if (pb->start >= pb->end) {
68 WARN_ON_ONCE(pb->start > pb->end);
69 numa_remove_memblk_from(phys_blk, pi);
70 }
71
72 printk(KERN_INFO "Faking node %d at [mem %#018Lx-%#018Lx] (%LuMB)\n",
73 nid, eb->start, eb->end - 1, (eb->end - eb->start) >> 20);
74 return 0;
75}
76
77
78
79
80
81
82
83static int __init split_nodes_interleave(struct numa_meminfo *ei,
84 struct numa_meminfo *pi,
85 u64 addr, u64 max_addr, int nr_nodes)
86{
87 nodemask_t physnode_mask = numa_nodes_parsed;
88 u64 size;
89 int big;
90 int nid = 0;
91 int i, ret;
92
93 if (nr_nodes <= 0)
94 return -1;
95 if (nr_nodes > MAX_NUMNODES) {
96 pr_info("numa=fake=%d too large, reducing to %d\n",
97 nr_nodes, MAX_NUMNODES);
98 nr_nodes = MAX_NUMNODES;
99 }
100
101
102
103
104
105 size = max_addr - addr - mem_hole_size(addr, max_addr);
106 size = PFN_PHYS((unsigned long)(size >> PAGE_SHIFT) / nr_nodes);
107
108
109
110
111
112 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
113 FAKE_NODE_MIN_SIZE;
114
115 size &= FAKE_NODE_MIN_HASH_MASK;
116 if (!size) {
117 pr_err("Not enough memory for each node. "
118 "NUMA emulation disabled.\n");
119 return -1;
120 }
121
122
123
124
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
146
147
148 while (end - start - mem_hole_size(start, end) < size) {
149 end += FAKE_NODE_MIN_SIZE;
150 if (end > limit) {
151 end = limit;
152 break;
153 }
154 }
155
156
157
158
159
160
161 if (end < dma32_end && dma32_end - end -
162 mem_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
163 end = dma32_end;
164
165
166
167
168
169
170 if (limit - end - mem_hole_size(end, limit) < size)
171 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
185
186
187static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
188{
189 u64 end = start + size;
190
191 while (end - start - mem_hole_size(start, end) < size) {
192 end += FAKE_NODE_MIN_SIZE;
193 if (end > max_addr) {
194 end = max_addr;
195 break;
196 }
197 }
198 return end;
199}
200
201static u64 uniform_size(u64 max_addr, u64 base, u64 hole, int nr_nodes)
202{
203 unsigned long max_pfn = PHYS_PFN(max_addr);
204 unsigned long base_pfn = PHYS_PFN(base);
205 unsigned long hole_pfns = PHYS_PFN(hole);
206
207 return PFN_PHYS((max_pfn - base_pfn - hole_pfns) / nr_nodes);
208}
209
210
211
212
213
214
215
216static int __init split_nodes_size_interleave_uniform(struct numa_meminfo *ei,
217 struct numa_meminfo *pi,
218 u64 addr, u64 max_addr, u64 size,
219 int nr_nodes, struct numa_memblk *pblk,
220 int nid)
221{
222 nodemask_t physnode_mask = numa_nodes_parsed;
223 int i, ret, uniform = 0;
224 u64 min_size;
225
226 if ((!size && !nr_nodes) || (nr_nodes && !pblk))
227 return -1;
228
229
230
231
232
233
234
235
236
237
238
239 if (!nr_nodes)
240 nr_nodes = MAX_NUMNODES;
241 else {
242 nodes_clear(physnode_mask);
243 node_set(pblk->nid, physnode_mask);
244 uniform = 1;
245 }
246
247 if (uniform) {
248 min_size = uniform_size(max_addr, addr, 0, nr_nodes);
249 size = min_size;
250 } else {
251
252
253
254
255
256
257
258 min_size = uniform_size(max_addr, addr,
259 mem_hole_size(addr, max_addr), nr_nodes);
260 }
261 min_size = ALIGN(max(min_size, FAKE_NODE_MIN_SIZE), FAKE_NODE_MIN_SIZE);
262 if (size < min_size) {
263 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
264 size >> 20, min_size >> 20);
265 size = min_size;
266 }
267 size = ALIGN_DOWN(size, FAKE_NODE_MIN_SIZE);
268
269
270
271
272
273 while (nodes_weight(physnode_mask)) {
274 for_each_node_mask(i, physnode_mask) {
275 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
276 u64 start, limit, end;
277 int phys_blk;
278
279 phys_blk = emu_find_memblk_by_nid(i, pi);
280 if (phys_blk < 0) {
281 node_clear(i, physnode_mask);
282 continue;
283 }
284
285 start = pi->blk[phys_blk].start;
286 limit = pi->blk[phys_blk].end;
287
288 if (uniform)
289 end = start + size;
290 else
291 end = find_end_of_node(start, limit, size);
292
293
294
295
296
297 if (end < dma32_end && dma32_end - end -
298 mem_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
299 end = dma32_end;
300
301
302
303
304
305
306 if ((limit - end - mem_hole_size(end, limit) < size)
307 && !uniform)
308 end = limit;
309
310 ret = emu_setup_memblk(ei, pi, nid++ % MAX_NUMNODES,
311 phys_blk,
312 min(end, limit) - start);
313 if (ret < 0)
314 return ret;
315 }
316 }
317 return nid;
318}
319
320static int __init split_nodes_size_interleave(struct numa_meminfo *ei,
321 struct numa_meminfo *pi,
322 u64 addr, u64 max_addr, u64 size)
323{
324 return split_nodes_size_interleave_uniform(ei, pi, addr, max_addr, size,
325 0, NULL, 0);
326}
327
328static int __init setup_emu2phys_nid(int *dfl_phys_nid)
329{
330 int i, max_emu_nid = 0;
331
332 *dfl_phys_nid = NUMA_NO_NODE;
333 for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++) {
334 if (emu_nid_to_phys[i] != NUMA_NO_NODE) {
335 max_emu_nid = i;
336 if (*dfl_phys_nid == NUMA_NO_NODE)
337 *dfl_phys_nid = emu_nid_to_phys[i];
338 }
339 }
340
341 return max_emu_nid;
342}
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371void __init numa_emulation(struct numa_meminfo *numa_meminfo, int numa_dist_cnt)
372{
373 static struct numa_meminfo ei __initdata;
374 static struct numa_meminfo pi __initdata;
375 const u64 max_addr = PFN_PHYS(max_pfn);
376 u8 *phys_dist = NULL;
377 size_t phys_size = numa_dist_cnt * numa_dist_cnt * sizeof(phys_dist[0]);
378 int max_emu_nid, dfl_phys_nid;
379 int i, j, ret;
380
381 if (!emu_cmdline)
382 goto no_emu;
383
384 memset(&ei, 0, sizeof(ei));
385 pi = *numa_meminfo;
386
387 for (i = 0; i < MAX_NUMNODES; i++)
388 emu_nid_to_phys[i] = NUMA_NO_NODE;
389
390
391
392
393
394
395 if (strchr(emu_cmdline, 'U')) {
396 nodemask_t physnode_mask = numa_nodes_parsed;
397 unsigned long n;
398 int nid = 0;
399
400 n = simple_strtoul(emu_cmdline, &emu_cmdline, 0);
401 ret = -1;
402 for_each_node_mask(i, physnode_mask) {
403
404
405
406
407
408
409
410
411 ret = split_nodes_size_interleave_uniform(&ei, &pi,
412 pi.blk[0].start, pi.blk[0].end, 0,
413 n, &pi.blk[0], nid);
414 if (ret < 0)
415 break;
416 if (ret < n) {
417 pr_info("%s: phys: %d only got %d of %ld nodes, failing\n",
418 __func__, i, ret, n);
419 ret = -1;
420 break;
421 }
422 nid = ret;
423 }
424 } else if (strchr(emu_cmdline, 'M') || strchr(emu_cmdline, 'G')) {
425 u64 size;
426
427 size = memparse(emu_cmdline, &emu_cmdline);
428 ret = split_nodes_size_interleave(&ei, &pi, 0, max_addr, size);
429 } else {
430 unsigned long n;
431
432 n = simple_strtoul(emu_cmdline, &emu_cmdline, 0);
433 ret = split_nodes_interleave(&ei, &pi, 0, max_addr, n);
434 }
435 if (*emu_cmdline == ':')
436 emu_cmdline++;
437
438 if (ret < 0)
439 goto no_emu;
440
441 if (numa_cleanup_meminfo(&ei) < 0) {
442 pr_warn("NUMA: Warning: constructed meminfo invalid, disabling emulation\n");
443 goto no_emu;
444 }
445
446
447 if (numa_dist_cnt) {
448 u64 phys;
449
450 phys = memblock_find_in_range(0, PFN_PHYS(max_pfn_mapped),
451 phys_size, PAGE_SIZE);
452 if (!phys) {
453 pr_warn("NUMA: Warning: can't allocate copy of distance table, disabling emulation\n");
454 goto no_emu;
455 }
456 memblock_reserve(phys, phys_size);
457 phys_dist = __va(phys);
458
459 for (i = 0; i < numa_dist_cnt; i++)
460 for (j = 0; j < numa_dist_cnt; j++)
461 phys_dist[i * numa_dist_cnt + j] =
462 node_distance(i, j);
463 }
464
465
466
467
468
469 max_emu_nid = setup_emu2phys_nid(&dfl_phys_nid);
470
471
472 *numa_meminfo = ei;
473
474
475 nodes_clear(numa_nodes_parsed);
476 for (i = 0; i < ARRAY_SIZE(ei.blk); i++)
477 if (ei.blk[i].start != ei.blk[i].end &&
478 ei.blk[i].nid != NUMA_NO_NODE)
479 node_set(ei.blk[i].nid, numa_nodes_parsed);
480
481
482
483
484
485
486 for (i = 0; i < ARRAY_SIZE(__apicid_to_node); i++) {
487 if (__apicid_to_node[i] == NUMA_NO_NODE)
488 continue;
489 for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
490 if (__apicid_to_node[i] == emu_nid_to_phys[j])
491 break;
492 __apicid_to_node[i] = j < ARRAY_SIZE(emu_nid_to_phys) ? j : 0;
493 }
494
495
496 for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
497 if (emu_nid_to_phys[i] == NUMA_NO_NODE)
498 emu_nid_to_phys[i] = dfl_phys_nid;
499
500
501 numa_reset_distance();
502 for (i = 0; i < max_emu_nid + 1; i++) {
503 for (j = 0; j < max_emu_nid + 1; j++) {
504 int physi = emu_nid_to_phys[i];
505 int physj = emu_nid_to_phys[j];
506 int dist;
507
508 if (get_option(&emu_cmdline, &dist) == 2)
509 ;
510 else if (physi >= numa_dist_cnt || physj >= numa_dist_cnt)
511 dist = physi == physj ?
512 LOCAL_DISTANCE : REMOTE_DISTANCE;
513 else
514 dist = phys_dist[physi * numa_dist_cnt + physj];
515
516 numa_set_distance(i, j, dist);
517 }
518 }
519
520
521 if (phys_dist)
522 memblock_free(__pa(phys_dist), phys_size);
523 return;
524
525no_emu:
526
527 for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
528 emu_nid_to_phys[i] = i;
529}
530
531#ifndef CONFIG_DEBUG_PER_CPU_MAPS
532void numa_add_cpu(int cpu)
533{
534 int physnid, nid;
535
536 nid = early_cpu_to_node(cpu);
537 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
538
539 physnid = emu_nid_to_phys[nid];
540
541
542
543
544
545 for_each_online_node(nid)
546 if (emu_nid_to_phys[nid] == physnid)
547 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
548}
549
550void numa_remove_cpu(int cpu)
551{
552 int i;
553
554 for_each_online_node(i)
555 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
556}
557#else
558static void numa_set_cpumask(int cpu, bool enable)
559{
560 int nid, physnid;
561
562 nid = early_cpu_to_node(cpu);
563 if (nid == NUMA_NO_NODE) {
564
565 return;
566 }
567
568 physnid = emu_nid_to_phys[nid];
569
570 for_each_online_node(nid) {
571 if (emu_nid_to_phys[nid] != physnid)
572 continue;
573
574 debug_cpumask_set_cpu(cpu, nid, enable);
575 }
576}
577
578void numa_add_cpu(int cpu)
579{
580 numa_set_cpumask(cpu, true);
581}
582
583void numa_remove_cpu(int cpu)
584{
585 numa_set_cpumask(cpu, false);
586}
587#endif
588