xref: /openbmc/linux/arch/x86/mm/numa.c (revision db921483)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Common code for 32 and 64-bit NUMA */
3 #include <linux/acpi.h>
4 #include <linux/kernel.h>
5 #include <linux/mm.h>
6 #include <linux/string.h>
7 #include <linux/init.h>
8 #include <linux/memblock.h>
9 #include <linux/mmzone.h>
10 #include <linux/ctype.h>
11 #include <linux/nodemask.h>
12 #include <linux/sched.h>
13 #include <linux/topology.h>
14 #include <linux/sort.h>
15 
16 #include <asm/e820/api.h>
17 #include <asm/proto.h>
18 #include <asm/dma.h>
19 #include <asm/amd_nb.h>
20 
21 #include "numa_internal.h"
22 
23 int numa_off;
24 nodemask_t numa_nodes_parsed __initdata;
25 
26 struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
27 EXPORT_SYMBOL(node_data);
28 
29 static struct numa_meminfo numa_meminfo __initdata_or_meminfo;
30 static struct numa_meminfo numa_reserved_meminfo __initdata_or_meminfo;
31 
32 static int numa_distance_cnt;
33 static u8 *numa_distance;
34 
numa_setup(char * opt)35 static __init int numa_setup(char *opt)
36 {
37 	if (!opt)
38 		return -EINVAL;
39 	if (!strncmp(opt, "off", 3))
40 		numa_off = 1;
41 	if (!strncmp(opt, "fake=", 5))
42 		return numa_emu_cmdline(opt + 5);
43 	if (!strncmp(opt, "noacpi", 6))
44 		disable_srat();
45 	if (!strncmp(opt, "nohmat", 6))
46 		disable_hmat();
47 	return 0;
48 }
49 early_param("numa", numa_setup);
50 
51 /*
52  * apicid, cpu, node mappings
53  */
54 s16 __apicid_to_node[MAX_LOCAL_APIC] = {
55 	[0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
56 };
57 
numa_cpu_node(int cpu)58 int numa_cpu_node(int cpu)
59 {
60 	int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
61 
62 	if (apicid != BAD_APICID)
63 		return __apicid_to_node[apicid];
64 	return NUMA_NO_NODE;
65 }
66 
67 cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
68 EXPORT_SYMBOL(node_to_cpumask_map);
69 
70 /*
71  * Map cpu index to node index
72  */
73 DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
74 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
75 
numa_set_node(int cpu,int node)76 void numa_set_node(int cpu, int node)
77 {
78 	int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
79 
80 	/* early setting, no percpu area yet */
81 	if (cpu_to_node_map) {
82 		cpu_to_node_map[cpu] = node;
83 		return;
84 	}
85 
86 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
87 	if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
88 		printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
89 		dump_stack();
90 		return;
91 	}
92 #endif
93 	per_cpu(x86_cpu_to_node_map, cpu) = node;
94 
95 	set_cpu_numa_node(cpu, node);
96 }
97 
numa_clear_node(int cpu)98 void numa_clear_node(int cpu)
99 {
100 	numa_set_node(cpu, NUMA_NO_NODE);
101 }
102 
103 /*
104  * Allocate node_to_cpumask_map based on number of available nodes
105  * Requires node_possible_map to be valid.
106  *
107  * Note: cpumask_of_node() is not valid until after this is done.
108  * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
109  */
setup_node_to_cpumask_map(void)110 void __init setup_node_to_cpumask_map(void)
111 {
112 	unsigned int node;
113 
114 	/* setup nr_node_ids if not done yet */
115 	if (nr_node_ids == MAX_NUMNODES)
116 		setup_nr_node_ids();
117 
118 	/* allocate the map */
119 	for (node = 0; node < nr_node_ids; node++)
120 		alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
121 
122 	/* cpumask_of_node() will now work */
123 	pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids);
124 }
125 
numa_add_memblk_to(int nid,u64 start,u64 end,struct numa_meminfo * mi)126 static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
127 				     struct numa_meminfo *mi)
128 {
129 	/* ignore zero length blks */
130 	if (start == end)
131 		return 0;
132 
133 	/* whine about and ignore invalid blks */
134 	if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
135 		pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
136 			nid, start, end - 1);
137 		return 0;
138 	}
139 
140 	if (mi->nr_blks >= NR_NODE_MEMBLKS) {
141 		pr_err("too many memblk ranges\n");
142 		return -EINVAL;
143 	}
144 
145 	mi->blk[mi->nr_blks].start = start;
146 	mi->blk[mi->nr_blks].end = end;
147 	mi->blk[mi->nr_blks].nid = nid;
148 	mi->nr_blks++;
149 	return 0;
150 }
151 
152 /**
153  * numa_remove_memblk_from - Remove one numa_memblk from a numa_meminfo
154  * @idx: Index of memblk to remove
155  * @mi: numa_meminfo to remove memblk from
156  *
157  * Remove @idx'th numa_memblk from @mi by shifting @mi->blk[] and
158  * decrementing @mi->nr_blks.
159  */
numa_remove_memblk_from(int idx,struct numa_meminfo * mi)160 void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
161 {
162 	mi->nr_blks--;
163 	memmove(&mi->blk[idx], &mi->blk[idx + 1],
164 		(mi->nr_blks - idx) * sizeof(mi->blk[0]));
165 }
166 
167 /**
168  * numa_move_tail_memblk - Move a numa_memblk from one numa_meminfo to another
169  * @dst: numa_meminfo to append block to
170  * @idx: Index of memblk to remove
171  * @src: numa_meminfo to remove memblk from
172  */
numa_move_tail_memblk(struct numa_meminfo * dst,int idx,struct numa_meminfo * src)173 static void __init numa_move_tail_memblk(struct numa_meminfo *dst, int idx,
174 					 struct numa_meminfo *src)
175 {
176 	dst->blk[dst->nr_blks++] = src->blk[idx];
177 	numa_remove_memblk_from(idx, src);
178 }
179 
180 /**
181  * numa_add_memblk - Add one numa_memblk to numa_meminfo
182  * @nid: NUMA node ID of the new memblk
183  * @start: Start address of the new memblk
184  * @end: End address of the new memblk
185  *
186  * Add a new memblk to the default numa_meminfo.
187  *
188  * RETURNS:
189  * 0 on success, -errno on failure.
190  */
numa_add_memblk(int nid,u64 start,u64 end)191 int __init numa_add_memblk(int nid, u64 start, u64 end)
192 {
193 	return numa_add_memblk_to(nid, start, end, &numa_meminfo);
194 }
195 
196 /* Allocate NODE_DATA for a node on the local memory */
alloc_node_data(int nid)197 static void __init alloc_node_data(int nid)
198 {
199 	const size_t nd_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
200 	u64 nd_pa;
201 	void *nd;
202 	int tnid;
203 
204 	/*
205 	 * Allocate node data.  Try node-local memory and then any node.
206 	 * Never allocate in DMA zone.
207 	 */
208 	nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
209 	if (!nd_pa) {
210 		pr_err("Cannot find %zu bytes in any node (initial node: %d)\n",
211 		       nd_size, nid);
212 		return;
213 	}
214 	nd = __va(nd_pa);
215 
216 	/* report and initialize */
217 	printk(KERN_INFO "NODE_DATA(%d) allocated [mem %#010Lx-%#010Lx]\n", nid,
218 	       nd_pa, nd_pa + nd_size - 1);
219 	tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
220 	if (tnid != nid)
221 		printk(KERN_INFO "    NODE_DATA(%d) on node %d\n", nid, tnid);
222 
223 	node_data[nid] = nd;
224 	memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
225 
226 	node_set_online(nid);
227 }
228 
229 /**
230  * numa_cleanup_meminfo - Cleanup a numa_meminfo
231  * @mi: numa_meminfo to clean up
232  *
233  * Sanitize @mi by merging and removing unnecessary memblks.  Also check for
234  * conflicts and clear unused memblks.
235  *
236  * RETURNS:
237  * 0 on success, -errno on failure.
238  */
numa_cleanup_meminfo(struct numa_meminfo * mi)239 int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
240 {
241 	const u64 low = 0;
242 	const u64 high = PFN_PHYS(max_pfn);
243 	int i, j, k;
244 
245 	/* first, trim all entries */
246 	for (i = 0; i < mi->nr_blks; i++) {
247 		struct numa_memblk *bi = &mi->blk[i];
248 
249 		/* move / save reserved memory ranges */
250 		if (!memblock_overlaps_region(&memblock.memory,
251 					bi->start, bi->end - bi->start)) {
252 			numa_move_tail_memblk(&numa_reserved_meminfo, i--, mi);
253 			continue;
254 		}
255 
256 		/* make sure all non-reserved blocks are inside the limits */
257 		bi->start = max(bi->start, low);
258 
259 		/* preserve info for non-RAM areas above 'max_pfn': */
260 		if (bi->end > high) {
261 			numa_add_memblk_to(bi->nid, high, bi->end,
262 					   &numa_reserved_meminfo);
263 			bi->end = high;
264 		}
265 
266 		/* and there's no empty block */
267 		if (bi->start >= bi->end)
268 			numa_remove_memblk_from(i--, mi);
269 	}
270 
271 	/* merge neighboring / overlapping entries */
272 	for (i = 0; i < mi->nr_blks; i++) {
273 		struct numa_memblk *bi = &mi->blk[i];
274 
275 		for (j = i + 1; j < mi->nr_blks; j++) {
276 			struct numa_memblk *bj = &mi->blk[j];
277 			u64 start, end;
278 
279 			/*
280 			 * See whether there are overlapping blocks.  Whine
281 			 * about but allow overlaps of the same nid.  They
282 			 * will be merged below.
283 			 */
284 			if (bi->end > bj->start && bi->start < bj->end) {
285 				if (bi->nid != bj->nid) {
286 					pr_err("node %d [mem %#010Lx-%#010Lx] overlaps with node %d [mem %#010Lx-%#010Lx]\n",
287 					       bi->nid, bi->start, bi->end - 1,
288 					       bj->nid, bj->start, bj->end - 1);
289 					return -EINVAL;
290 				}
291 				pr_warn("Warning: node %d [mem %#010Lx-%#010Lx] overlaps with itself [mem %#010Lx-%#010Lx]\n",
292 					bi->nid, bi->start, bi->end - 1,
293 					bj->start, bj->end - 1);
294 			}
295 
296 			/*
297 			 * Join together blocks on the same node, holes
298 			 * between which don't overlap with memory on other
299 			 * nodes.
300 			 */
301 			if (bi->nid != bj->nid)
302 				continue;
303 			start = min(bi->start, bj->start);
304 			end = max(bi->end, bj->end);
305 			for (k = 0; k < mi->nr_blks; k++) {
306 				struct numa_memblk *bk = &mi->blk[k];
307 
308 				if (bi->nid == bk->nid)
309 					continue;
310 				if (start < bk->end && end > bk->start)
311 					break;
312 			}
313 			if (k < mi->nr_blks)
314 				continue;
315 			printk(KERN_INFO "NUMA: Node %d [mem %#010Lx-%#010Lx] + [mem %#010Lx-%#010Lx] -> [mem %#010Lx-%#010Lx]\n",
316 			       bi->nid, bi->start, bi->end - 1, bj->start,
317 			       bj->end - 1, start, end - 1);
318 			bi->start = start;
319 			bi->end = end;
320 			numa_remove_memblk_from(j--, mi);
321 		}
322 	}
323 
324 	/* clear unused ones */
325 	for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
326 		mi->blk[i].start = mi->blk[i].end = 0;
327 		mi->blk[i].nid = NUMA_NO_NODE;
328 	}
329 
330 	return 0;
331 }
332 
333 /*
334  * Set nodes, which have memory in @mi, in *@nodemask.
335  */
numa_nodemask_from_meminfo(nodemask_t * nodemask,const struct numa_meminfo * mi)336 static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
337 					      const struct numa_meminfo *mi)
338 {
339 	int i;
340 
341 	for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
342 		if (mi->blk[i].start != mi->blk[i].end &&
343 		    mi->blk[i].nid != NUMA_NO_NODE)
344 			node_set(mi->blk[i].nid, *nodemask);
345 }
346 
347 /**
348  * numa_reset_distance - Reset NUMA distance table
349  *
350  * The current table is freed.  The next numa_set_distance() call will
351  * create a new one.
352  */
numa_reset_distance(void)353 void __init numa_reset_distance(void)
354 {
355 	size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]);
356 
357 	/* numa_distance could be 1LU marking allocation failure, test cnt */
358 	if (numa_distance_cnt)
359 		memblock_free(numa_distance, size);
360 	numa_distance_cnt = 0;
361 	numa_distance = NULL;	/* enable table creation */
362 }
363 
numa_alloc_distance(void)364 static int __init numa_alloc_distance(void)
365 {
366 	nodemask_t nodes_parsed;
367 	size_t size;
368 	int i, j, cnt = 0;
369 	u64 phys;
370 
371 	/* size the new table and allocate it */
372 	nodes_parsed = numa_nodes_parsed;
373 	numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
374 
375 	for_each_node_mask(i, nodes_parsed)
376 		cnt = i;
377 	cnt++;
378 	size = cnt * cnt * sizeof(numa_distance[0]);
379 
380 	phys = memblock_phys_alloc_range(size, PAGE_SIZE, 0,
381 					 PFN_PHYS(max_pfn_mapped));
382 	if (!phys) {
383 		pr_warn("Warning: can't allocate distance table!\n");
384 		/* don't retry until explicitly reset */
385 		numa_distance = (void *)1LU;
386 		return -ENOMEM;
387 	}
388 
389 	numa_distance = __va(phys);
390 	numa_distance_cnt = cnt;
391 
392 	/* fill with the default distances */
393 	for (i = 0; i < cnt; i++)
394 		for (j = 0; j < cnt; j++)
395 			numa_distance[i * cnt + j] = i == j ?
396 				LOCAL_DISTANCE : REMOTE_DISTANCE;
397 	printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
398 
399 	return 0;
400 }
401 
402 /**
403  * numa_set_distance - Set NUMA distance from one NUMA to another
404  * @from: the 'from' node to set distance
405  * @to: the 'to'  node to set distance
406  * @distance: NUMA distance
407  *
408  * Set the distance from node @from to @to to @distance.  If distance table
409  * doesn't exist, one which is large enough to accommodate all the currently
410  * known nodes will be created.
411  *
412  * If such table cannot be allocated, a warning is printed and further
413  * calls are ignored until the distance table is reset with
414  * numa_reset_distance().
415  *
416  * If @from or @to is higher than the highest known node or lower than zero
417  * at the time of table creation or @distance doesn't make sense, the call
418  * is ignored.
419  * This is to allow simplification of specific NUMA config implementations.
420  */
numa_set_distance(int from,int to,int distance)421 void __init numa_set_distance(int from, int to, int distance)
422 {
423 	if (!numa_distance && numa_alloc_distance() < 0)
424 		return;
425 
426 	if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
427 			from < 0 || to < 0) {
428 		pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
429 			     from, to, distance);
430 		return;
431 	}
432 
433 	if ((u8)distance != distance ||
434 	    (from == to && distance != LOCAL_DISTANCE)) {
435 		pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
436 			     from, to, distance);
437 		return;
438 	}
439 
440 	numa_distance[from * numa_distance_cnt + to] = distance;
441 }
442 
__node_distance(int from,int to)443 int __node_distance(int from, int to)
444 {
445 	if (from >= numa_distance_cnt || to >= numa_distance_cnt)
446 		return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
447 	return numa_distance[from * numa_distance_cnt + to];
448 }
449 EXPORT_SYMBOL(__node_distance);
450 
451 /*
452  * Sanity check to catch more bad NUMA configurations (they are amazingly
453  * common).  Make sure the nodes cover all memory.
454  */
numa_meminfo_cover_memory(const struct numa_meminfo * mi)455 static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
456 {
457 	u64 numaram, e820ram;
458 	int i;
459 
460 	numaram = 0;
461 	for (i = 0; i < mi->nr_blks; i++) {
462 		u64 s = mi->blk[i].start >> PAGE_SHIFT;
463 		u64 e = mi->blk[i].end >> PAGE_SHIFT;
464 		numaram += e - s;
465 		numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
466 		if ((s64)numaram < 0)
467 			numaram = 0;
468 	}
469 
470 	e820ram = max_pfn - absent_pages_in_range(0, max_pfn);
471 
472 	/* We seem to lose 3 pages somewhere. Allow 1M of slack. */
473 	if ((s64)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
474 		printk(KERN_ERR "NUMA: nodes only cover %LuMB of your %LuMB e820 RAM. Not used.\n",
475 		       (numaram << PAGE_SHIFT) >> 20,
476 		       (e820ram << PAGE_SHIFT) >> 20);
477 		return false;
478 	}
479 	return true;
480 }
481 
482 /*
483  * Mark all currently memblock-reserved physical memory (which covers the
484  * kernel's own memory ranges) as hot-unswappable.
485  */
numa_clear_kernel_node_hotplug(void)486 static void __init numa_clear_kernel_node_hotplug(void)
487 {
488 	nodemask_t reserved_nodemask = NODE_MASK_NONE;
489 	struct memblock_region *mb_region;
490 	int i;
491 
492 	/*
493 	 * We have to do some preprocessing of memblock regions, to
494 	 * make them suitable for reservation.
495 	 *
496 	 * At this time, all memory regions reserved by memblock are
497 	 * used by the kernel, but those regions are not split up
498 	 * along node boundaries yet, and don't necessarily have their
499 	 * node ID set yet either.
500 	 *
501 	 * So iterate over all memory known to the x86 architecture,
502 	 * and use those ranges to set the nid in memblock.reserved.
503 	 * This will split up the memblock regions along node
504 	 * boundaries and will set the node IDs as well.
505 	 */
506 	for (i = 0; i < numa_meminfo.nr_blks; i++) {
507 		struct numa_memblk *mb = numa_meminfo.blk + i;
508 		int ret;
509 
510 		ret = memblock_set_node(mb->start, mb->end - mb->start, &memblock.reserved, mb->nid);
511 		WARN_ON_ONCE(ret);
512 	}
513 
514 	/*
515 	 * Now go over all reserved memblock regions, to construct a
516 	 * node mask of all kernel reserved memory areas.
517 	 *
518 	 * [ Note, when booting with mem=nn[kMG] or in a kdump kernel,
519 	 *   numa_meminfo might not include all memblock.reserved
520 	 *   memory ranges, because quirks such as trim_snb_memory()
521 	 *   reserve specific pages for Sandy Bridge graphics. ]
522 	 */
523 	for_each_reserved_mem_region(mb_region) {
524 		int nid = memblock_get_region_node(mb_region);
525 
526 		if (nid != MAX_NUMNODES)
527 			node_set(nid, reserved_nodemask);
528 	}
529 
530 	/*
531 	 * Finally, clear the MEMBLOCK_HOTPLUG flag for all memory
532 	 * belonging to the reserved node mask.
533 	 *
534 	 * Note that this will include memory regions that reside
535 	 * on nodes that contain kernel memory - entire nodes
536 	 * become hot-unpluggable:
537 	 */
538 	for (i = 0; i < numa_meminfo.nr_blks; i++) {
539 		struct numa_memblk *mb = numa_meminfo.blk + i;
540 
541 		if (!node_isset(mb->nid, reserved_nodemask))
542 			continue;
543 
544 		memblock_clear_hotplug(mb->start, mb->end - mb->start);
545 	}
546 }
547 
numa_register_memblks(struct numa_meminfo * mi)548 static int __init numa_register_memblks(struct numa_meminfo *mi)
549 {
550 	int i, nid;
551 
552 	/* Account for nodes with cpus and no memory */
553 	node_possible_map = numa_nodes_parsed;
554 	numa_nodemask_from_meminfo(&node_possible_map, mi);
555 	if (WARN_ON(nodes_empty(node_possible_map)))
556 		return -EINVAL;
557 
558 	for (i = 0; i < mi->nr_blks; i++) {
559 		struct numa_memblk *mb = &mi->blk[i];
560 		memblock_set_node(mb->start, mb->end - mb->start,
561 				  &memblock.memory, mb->nid);
562 	}
563 
564 	/*
565 	 * At very early time, the kernel have to use some memory such as
566 	 * loading the kernel image. We cannot prevent this anyway. So any
567 	 * node the kernel resides in should be un-hotpluggable.
568 	 *
569 	 * And when we come here, alloc node data won't fail.
570 	 */
571 	numa_clear_kernel_node_hotplug();
572 
573 	/*
574 	 * If sections array is gonna be used for pfn -> nid mapping, check
575 	 * whether its granularity is fine enough.
576 	 */
577 	if (IS_ENABLED(NODE_NOT_IN_PAGE_FLAGS)) {
578 		unsigned long pfn_align = node_map_pfn_alignment();
579 
580 		if (pfn_align && pfn_align < PAGES_PER_SECTION) {
581 			pr_warn("Node alignment %LuMB < min %LuMB, rejecting NUMA config\n",
582 				PFN_PHYS(pfn_align) >> 20,
583 				PFN_PHYS(PAGES_PER_SECTION) >> 20);
584 			return -EINVAL;
585 		}
586 	}
587 	if (!numa_meminfo_cover_memory(mi))
588 		return -EINVAL;
589 
590 	/* Finally register nodes. */
591 	for_each_node_mask(nid, node_possible_map) {
592 		u64 start = PFN_PHYS(max_pfn);
593 		u64 end = 0;
594 
595 		for (i = 0; i < mi->nr_blks; i++) {
596 			if (nid != mi->blk[i].nid)
597 				continue;
598 			start = min(mi->blk[i].start, start);
599 			end = max(mi->blk[i].end, end);
600 		}
601 
602 		if (start >= end)
603 			continue;
604 
605 		alloc_node_data(nid);
606 	}
607 
608 	/* Dump memblock with node info and return. */
609 	memblock_dump_all();
610 	return 0;
611 }
612 
613 /*
614  * There are unfortunately some poorly designed mainboards around that
615  * only connect memory to a single CPU. This breaks the 1:1 cpu->node
616  * mapping. To avoid this fill in the mapping for all possible CPUs,
617  * as the number of CPUs is not known yet. We round robin the existing
618  * nodes.
619  */
numa_init_array(void)620 static void __init numa_init_array(void)
621 {
622 	int rr, i;
623 
624 	rr = first_node(node_online_map);
625 	for (i = 0; i < nr_cpu_ids; i++) {
626 		if (early_cpu_to_node(i) != NUMA_NO_NODE)
627 			continue;
628 		numa_set_node(i, rr);
629 		rr = next_node_in(rr, node_online_map);
630 	}
631 }
632 
numa_init(int (* init_func)(void))633 static int __init numa_init(int (*init_func)(void))
634 {
635 	int i;
636 	int ret;
637 
638 	for (i = 0; i < MAX_LOCAL_APIC; i++)
639 		set_apicid_to_node(i, NUMA_NO_NODE);
640 
641 	nodes_clear(numa_nodes_parsed);
642 	nodes_clear(node_possible_map);
643 	nodes_clear(node_online_map);
644 	memset(&numa_meminfo, 0, sizeof(numa_meminfo));
645 	WARN_ON(memblock_set_node(0, ULLONG_MAX, &memblock.memory,
646 				  MAX_NUMNODES));
647 	WARN_ON(memblock_set_node(0, ULLONG_MAX, &memblock.reserved,
648 				  MAX_NUMNODES));
649 	/* In case that parsing SRAT failed. */
650 	WARN_ON(memblock_clear_hotplug(0, ULLONG_MAX));
651 	numa_reset_distance();
652 
653 	ret = init_func();
654 	if (ret < 0)
655 		return ret;
656 
657 	/*
658 	 * We reset memblock back to the top-down direction
659 	 * here because if we configured ACPI_NUMA, we have
660 	 * parsed SRAT in init_func(). It is ok to have the
661 	 * reset here even if we did't configure ACPI_NUMA
662 	 * or acpi numa init fails and fallbacks to dummy
663 	 * numa init.
664 	 */
665 	memblock_set_bottom_up(false);
666 
667 	ret = numa_cleanup_meminfo(&numa_meminfo);
668 	if (ret < 0)
669 		return ret;
670 
671 	numa_emulation(&numa_meminfo, numa_distance_cnt);
672 
673 	ret = numa_register_memblks(&numa_meminfo);
674 	if (ret < 0)
675 		return ret;
676 
677 	for (i = 0; i < nr_cpu_ids; i++) {
678 		int nid = early_cpu_to_node(i);
679 
680 		if (nid == NUMA_NO_NODE)
681 			continue;
682 		if (!node_online(nid))
683 			numa_clear_node(i);
684 	}
685 	numa_init_array();
686 
687 	return 0;
688 }
689 
690 /**
691  * dummy_numa_init - Fallback dummy NUMA init
692  *
693  * Used if there's no underlying NUMA architecture, NUMA initialization
694  * fails, or NUMA is disabled on the command line.
695  *
696  * Must online at least one node and add memory blocks that cover all
697  * allowed memory.  This function must not fail.
698  */
dummy_numa_init(void)699 static int __init dummy_numa_init(void)
700 {
701 	printk(KERN_INFO "%s\n",
702 	       numa_off ? "NUMA turned off" : "No NUMA configuration found");
703 	printk(KERN_INFO "Faking a node at [mem %#018Lx-%#018Lx]\n",
704 	       0LLU, PFN_PHYS(max_pfn) - 1);
705 
706 	node_set(0, numa_nodes_parsed);
707 	numa_add_memblk(0, 0, PFN_PHYS(max_pfn));
708 
709 	return 0;
710 }
711 
712 /**
713  * x86_numa_init - Initialize NUMA
714  *
715  * Try each configured NUMA initialization method until one succeeds.  The
716  * last fallback is dummy single node config encompassing whole memory and
717  * never fails.
718  */
x86_numa_init(void)719 void __init x86_numa_init(void)
720 {
721 	if (!numa_off) {
722 #ifdef CONFIG_ACPI_NUMA
723 		if (!numa_init(x86_acpi_numa_init))
724 			return;
725 #endif
726 #ifdef CONFIG_AMD_NUMA
727 		if (!numa_init(amd_numa_init))
728 			return;
729 #endif
730 	}
731 
732 	numa_init(dummy_numa_init);
733 }
734 
735 
736 /*
737  * A node may exist which has one or more Generic Initiators but no CPUs and no
738  * memory.
739  *
740  * This function must be called after init_cpu_to_node(), to ensure that any
741  * memoryless CPU nodes have already been brought online, and before the
742  * node_data[nid] is needed for zone list setup in build_all_zonelists().
743  *
744  * When this function is called, any nodes containing either memory and/or CPUs
745  * will already be online and there is no need to do anything extra, even if
746  * they also contain one or more Generic Initiators.
747  */
init_gi_nodes(void)748 void __init init_gi_nodes(void)
749 {
750 	int nid;
751 
752 	/*
753 	 * Exclude this node from
754 	 * bringup_nonboot_cpus
755 	 *  cpu_up
756 	 *   __try_online_node
757 	 *    register_one_node
758 	 * because node_subsys is not initialized yet.
759 	 * TODO remove dependency on node_online
760 	 */
761 	for_each_node_state(nid, N_GENERIC_INITIATOR)
762 		if (!node_online(nid))
763 			node_set_online(nid);
764 }
765 
766 /*
767  * Setup early cpu_to_node.
768  *
769  * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
770  * and apicid_to_node[] tables have valid entries for a CPU.
771  * This means we skip cpu_to_node[] initialisation for NUMA
772  * emulation and faking node case (when running a kernel compiled
773  * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
774  * is already initialized in a round robin manner at numa_init_array,
775  * prior to this call, and this initialization is good enough
776  * for the fake NUMA cases.
777  *
778  * Called before the per_cpu areas are setup.
779  */
init_cpu_to_node(void)780 void __init init_cpu_to_node(void)
781 {
782 	int cpu;
783 	u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
784 
785 	BUG_ON(cpu_to_apicid == NULL);
786 
787 	for_each_possible_cpu(cpu) {
788 		int node = numa_cpu_node(cpu);
789 
790 		if (node == NUMA_NO_NODE)
791 			continue;
792 
793 		/*
794 		 * Exclude this node from
795 		 * bringup_nonboot_cpus
796 		 *  cpu_up
797 		 *   __try_online_node
798 		 *    register_one_node
799 		 * because node_subsys is not initialized yet.
800 		 * TODO remove dependency on node_online
801 		 */
802 		if (!node_online(node))
803 			node_set_online(node);
804 
805 		numa_set_node(cpu, node);
806 	}
807 }
808 
809 #ifndef CONFIG_DEBUG_PER_CPU_MAPS
810 
811 # ifndef CONFIG_NUMA_EMU
numa_add_cpu(int cpu)812 void numa_add_cpu(int cpu)
813 {
814 	cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
815 }
816 
numa_remove_cpu(int cpu)817 void numa_remove_cpu(int cpu)
818 {
819 	cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
820 }
821 # endif	/* !CONFIG_NUMA_EMU */
822 
823 #else	/* !CONFIG_DEBUG_PER_CPU_MAPS */
824 
__cpu_to_node(int cpu)825 int __cpu_to_node(int cpu)
826 {
827 	if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
828 		printk(KERN_WARNING
829 			"cpu_to_node(%d): usage too early!\n", cpu);
830 		dump_stack();
831 		return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
832 	}
833 	return per_cpu(x86_cpu_to_node_map, cpu);
834 }
835 EXPORT_SYMBOL(__cpu_to_node);
836 
837 /*
838  * Same function as cpu_to_node() but used if called before the
839  * per_cpu areas are setup.
840  */
early_cpu_to_node(int cpu)841 int early_cpu_to_node(int cpu)
842 {
843 	if (early_per_cpu_ptr(x86_cpu_to_node_map))
844 		return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
845 
846 	if (!cpu_possible(cpu)) {
847 		printk(KERN_WARNING
848 			"early_cpu_to_node(%d): no per_cpu area!\n", cpu);
849 		dump_stack();
850 		return NUMA_NO_NODE;
851 	}
852 	return per_cpu(x86_cpu_to_node_map, cpu);
853 }
854 
debug_cpumask_set_cpu(int cpu,int node,bool enable)855 void debug_cpumask_set_cpu(int cpu, int node, bool enable)
856 {
857 	struct cpumask *mask;
858 
859 	if (node == NUMA_NO_NODE) {
860 		/* early_cpu_to_node() already emits a warning and trace */
861 		return;
862 	}
863 	mask = node_to_cpumask_map[node];
864 	if (!cpumask_available(mask)) {
865 		pr_err("node_to_cpumask_map[%i] NULL\n", node);
866 		dump_stack();
867 		return;
868 	}
869 
870 	if (enable)
871 		cpumask_set_cpu(cpu, mask);
872 	else
873 		cpumask_clear_cpu(cpu, mask);
874 
875 	printk(KERN_DEBUG "%s cpu %d node %d: mask now %*pbl\n",
876 		enable ? "numa_add_cpu" : "numa_remove_cpu",
877 		cpu, node, cpumask_pr_args(mask));
878 	return;
879 }
880 
881 # ifndef CONFIG_NUMA_EMU
numa_set_cpumask(int cpu,bool enable)882 static void numa_set_cpumask(int cpu, bool enable)
883 {
884 	debug_cpumask_set_cpu(cpu, early_cpu_to_node(cpu), enable);
885 }
886 
numa_add_cpu(int cpu)887 void numa_add_cpu(int cpu)
888 {
889 	numa_set_cpumask(cpu, true);
890 }
891 
numa_remove_cpu(int cpu)892 void numa_remove_cpu(int cpu)
893 {
894 	numa_set_cpumask(cpu, false);
895 }
896 # endif	/* !CONFIG_NUMA_EMU */
897 
898 /*
899  * Returns a pointer to the bitmask of CPUs on Node 'node'.
900  */
cpumask_of_node(int node)901 const struct cpumask *cpumask_of_node(int node)
902 {
903 	if ((unsigned)node >= nr_node_ids) {
904 		printk(KERN_WARNING
905 			"cpumask_of_node(%d): (unsigned)node >= nr_node_ids(%u)\n",
906 			node, nr_node_ids);
907 		dump_stack();
908 		return cpu_none_mask;
909 	}
910 	if (!cpumask_available(node_to_cpumask_map[node])) {
911 		printk(KERN_WARNING
912 			"cpumask_of_node(%d): no node_to_cpumask_map!\n",
913 			node);
914 		dump_stack();
915 		return cpu_online_mask;
916 	}
917 	return node_to_cpumask_map[node];
918 }
919 EXPORT_SYMBOL(cpumask_of_node);
920 
921 #endif	/* !CONFIG_DEBUG_PER_CPU_MAPS */
922 
923 #ifdef CONFIG_NUMA_KEEP_MEMINFO
meminfo_to_nid(struct numa_meminfo * mi,u64 start)924 static int meminfo_to_nid(struct numa_meminfo *mi, u64 start)
925 {
926 	int i;
927 
928 	for (i = 0; i < mi->nr_blks; i++)
929 		if (mi->blk[i].start <= start && mi->blk[i].end > start)
930 			return mi->blk[i].nid;
931 	return NUMA_NO_NODE;
932 }
933 
phys_to_target_node(phys_addr_t start)934 int phys_to_target_node(phys_addr_t start)
935 {
936 	int nid = meminfo_to_nid(&numa_meminfo, start);
937 
938 	/*
939 	 * Prefer online nodes, but if reserved memory might be
940 	 * hot-added continue the search with reserved ranges.
941 	 */
942 	if (nid != NUMA_NO_NODE)
943 		return nid;
944 
945 	return meminfo_to_nid(&numa_reserved_meminfo, start);
946 }
947 EXPORT_SYMBOL_GPL(phys_to_target_node);
948 
memory_add_physaddr_to_nid(u64 start)949 int memory_add_physaddr_to_nid(u64 start)
950 {
951 	int nid = meminfo_to_nid(&numa_meminfo, start);
952 
953 	if (nid == NUMA_NO_NODE)
954 		nid = numa_meminfo.blk[0].nid;
955 	return nid;
956 }
957 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
958 
959 #endif
960 
cmp_memblk(const void * a,const void * b)961 static int __init cmp_memblk(const void *a, const void *b)
962 {
963 	const struct numa_memblk *ma = *(const struct numa_memblk **)a;
964 	const struct numa_memblk *mb = *(const struct numa_memblk **)b;
965 
966 	return (ma->start > mb->start) - (ma->start < mb->start);
967 }
968 
969 static struct numa_memblk *numa_memblk_list[NR_NODE_MEMBLKS] __initdata;
970 
971 /**
972  * numa_fill_memblks - Fill gaps in numa_meminfo memblks
973  * @start: address to begin fill
974  * @end: address to end fill
975  *
976  * Find and extend numa_meminfo memblks to cover the physical
977  * address range @start-@end
978  *
979  * RETURNS:
980  * 0		  : Success
981  * NUMA_NO_MEMBLK : No memblks exist in address range @start-@end
982  */
983 
numa_fill_memblks(u64 start,u64 end)984 int __init numa_fill_memblks(u64 start, u64 end)
985 {
986 	struct numa_memblk **blk = &numa_memblk_list[0];
987 	struct numa_meminfo *mi = &numa_meminfo;
988 	int count = 0;
989 	u64 prev_end;
990 
991 	/*
992 	 * Create a list of pointers to numa_meminfo memblks that
993 	 * overlap start, end. The list is used to make in-place
994 	 * changes that fill out the numa_meminfo memblks.
995 	 */
996 	for (int i = 0; i < mi->nr_blks; i++) {
997 		struct numa_memblk *bi = &mi->blk[i];
998 
999 		if (memblock_addrs_overlap(start, end - start, bi->start,
1000 					   bi->end - bi->start)) {
1001 			blk[count] = &mi->blk[i];
1002 			count++;
1003 		}
1004 	}
1005 	if (!count)
1006 		return NUMA_NO_MEMBLK;
1007 
1008 	/* Sort the list of pointers in memblk->start order */
1009 	sort(&blk[0], count, sizeof(blk[0]), cmp_memblk, NULL);
1010 
1011 	/* Make sure the first/last memblks include start/end */
1012 	blk[0]->start = min(blk[0]->start, start);
1013 	blk[count - 1]->end = max(blk[count - 1]->end, end);
1014 
1015 	/*
1016 	 * Fill any gaps by tracking the previous memblks
1017 	 * end address and backfilling to it if needed.
1018 	 */
1019 	prev_end = blk[0]->end;
1020 	for (int i = 1; i < count; i++) {
1021 		struct numa_memblk *curr = blk[i];
1022 
1023 		if (prev_end >= curr->start) {
1024 			if (prev_end < curr->end)
1025 				prev_end = curr->end;
1026 		} else {
1027 			curr->start = prev_end;
1028 			prev_end = curr->end;
1029 		}
1030 	}
1031 	return 0;
1032 }
1033