1 /* Common code for 32 and 64-bit NUMA */
2 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/init.h>
6 #include <linux/bootmem.h>
7 #include <linux/memblock.h>
8 #include <linux/mmzone.h>
9 #include <linux/ctype.h>
10 #include <linux/module.h>
11 #include <linux/nodemask.h>
12 #include <linux/sched.h>
13 #include <linux/topology.h>
16 #include <asm/proto.h>
19 #include <asm/amd_nb.h>
21 #include "numa_internal.h"
23 int __initdata numa_off;
24 nodemask_t numa_nodes_parsed __initdata;
26 struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
27 EXPORT_SYMBOL(node_data);
29 static struct numa_meminfo numa_meminfo
30 #ifndef CONFIG_MEMORY_HOTPLUG
35 static int numa_distance_cnt;
36 static u8 *numa_distance;
38 static __init int numa_setup(char *opt)
42 if (!strncmp(opt, "off", 3))
44 #ifdef CONFIG_NUMA_EMU
45 if (!strncmp(opt, "fake=", 5))
46 numa_emu_cmdline(opt + 5);
48 #ifdef CONFIG_ACPI_NUMA
49 if (!strncmp(opt, "noacpi", 6))
54 early_param("numa", numa_setup);
57 * apicid, cpu, node mappings
59 s16 __apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = {
60 [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
63 int __cpuinit numa_cpu_node(int cpu)
65 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
67 if (apicid != BAD_APICID)
68 return __apicid_to_node[apicid];
72 cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
73 EXPORT_SYMBOL(node_to_cpumask_map);
76 * Map cpu index to node index
78 DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
79 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
81 void __cpuinit numa_set_node(int cpu, int node)
83 int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
85 /* early setting, no percpu area yet */
86 if (cpu_to_node_map) {
87 cpu_to_node_map[cpu] = node;
91 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
92 if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
93 printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
98 per_cpu(x86_cpu_to_node_map, cpu) = node;
100 if (node != NUMA_NO_NODE)
101 set_cpu_numa_node(cpu, node);
104 void __cpuinit numa_clear_node(int cpu)
106 numa_set_node(cpu, NUMA_NO_NODE);
110 * Allocate node_to_cpumask_map based on number of available nodes
111 * Requires node_possible_map to be valid.
113 * Note: node_to_cpumask() is not valid until after this is done.
114 * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
116 void __init setup_node_to_cpumask_map(void)
118 unsigned int node, num = 0;
120 /* setup nr_node_ids if not done yet */
121 if (nr_node_ids == MAX_NUMNODES) {
122 for_each_node_mask(node, node_possible_map)
124 nr_node_ids = num + 1;
127 /* allocate the map */
128 for (node = 0; node < nr_node_ids; node++)
129 alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
131 /* cpumask_of_node() will now work */
132 pr_debug("Node to cpumask map for %d nodes\n", nr_node_ids);
135 static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
136 struct numa_meminfo *mi)
138 /* ignore zero length blks */
142 /* whine about and ignore invalid blks */
143 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
144 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
149 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
150 pr_err("NUMA: too many memblk ranges\n");
154 mi->blk[mi->nr_blks].start = start;
155 mi->blk[mi->nr_blks].end = end;
156 mi->blk[mi->nr_blks].nid = nid;
162 * numa_remove_memblk_from - Remove one numa_memblk from a numa_meminfo
163 * @idx: Index of memblk to remove
164 * @mi: numa_meminfo to remove memblk from
166 * Remove @idx'th numa_memblk from @mi by shifting @mi->blk[] and
167 * decrementing @mi->nr_blks.
169 void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
172 memmove(&mi->blk[idx], &mi->blk[idx + 1],
173 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
178 * numa_add_memblk - Add one numa_memblk to numa_meminfo
179 * @nid: NUMA node ID of the new memblk
180 * @start: Start address of the new memblk
181 * @end: End address of the new memblk
183 * Add a new memblk to the default numa_meminfo.
186 * 0 on success, -errno on failure.
188 int __init numa_add_memblk(int nid, u64 start, u64 end)
190 return numa_add_memblk_to(nid, start, end, &numa_meminfo);
194 /* Initialize bootmem allocator for a node */
195 static void __init setup_node_bootmem(int nid, u64 start, u64 end)
197 const u64 nd_low = PFN_PHYS(MAX_DMA_PFN);
198 const u64 nd_high = PFN_PHYS(max_pfn_mapped);
199 const size_t nd_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
204 * Don't confuse VM with a node that doesn't have the
205 * minimum amount of memory:
207 if (end && (end - start) < NODE_MIN_SIZE)
210 start = roundup(start, ZONE_ALIGN);
212 printk(KERN_INFO "Initmem setup node %d %016Lx-%016Lx\n",
216 * Try to allocate node data on local node and then fall back to
217 * all nodes. Never allocate in DMA zone.
219 nd_pa = memblock_x86_find_in_range_node(nid, nd_low, nd_high,
220 nd_size, SMP_CACHE_BYTES);
221 if (nd_pa == MEMBLOCK_ERROR)
222 nd_pa = memblock_find_in_range(nd_low, nd_high,
223 nd_size, SMP_CACHE_BYTES);
224 if (nd_pa == MEMBLOCK_ERROR) {
225 pr_err("Cannot find %zu bytes in node %d\n", nd_size, nid);
228 memblock_x86_reserve_range(nd_pa, nd_pa + nd_size, "NODE_DATA");
230 /* report and initialize */
231 printk(KERN_INFO " NODE_DATA [%016Lx - %016Lx]\n",
232 nd_pa, nd_pa + nd_size - 1);
233 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
235 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nid, tnid);
237 node_data[nid] = __va(nd_pa);
238 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
239 NODE_DATA(nid)->node_id = nid;
240 NODE_DATA(nid)->node_start_pfn = start >> PAGE_SHIFT;
241 NODE_DATA(nid)->node_spanned_pages = (end - start) >> PAGE_SHIFT;
243 node_set_online(nid);
247 * numa_cleanup_meminfo - Cleanup a numa_meminfo
248 * @mi: numa_meminfo to clean up
250 * Sanitize @mi by merging and removing unncessary memblks. Also check for
251 * conflicts and clear unused memblks.
254 * 0 on success, -errno on failure.
256 int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
259 const u64 high = PFN_PHYS(max_pfn);
262 for (i = 0; i < mi->nr_blks; i++) {
263 struct numa_memblk *bi = &mi->blk[i];
265 /* make sure all blocks are inside the limits */
266 bi->start = max(bi->start, low);
267 bi->end = min(bi->end, high);
269 /* and there's no empty block */
270 if (bi->start >= bi->end) {
271 numa_remove_memblk_from(i--, mi);
275 for (j = i + 1; j < mi->nr_blks; j++) {
276 struct numa_memblk *bj = &mi->blk[j];
280 * See whether there are overlapping blocks. Whine
281 * about but allow overlaps of the same nid. They
282 * will be merged below.
284 if (bi->end > bj->start && bi->start < bj->end) {
285 if (bi->nid != bj->nid) {
286 pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
287 bi->nid, bi->start, bi->end,
288 bj->nid, bj->start, bj->end);
291 pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
292 bi->nid, bi->start, bi->end,
297 * Join together blocks on the same node, holes
298 * between which don't overlap with memory on other
301 if (bi->nid != bj->nid)
303 start = max(min(bi->start, bj->start), low);
304 end = min(max(bi->end, bj->end), high);
305 for (k = 0; k < mi->nr_blks; k++) {
306 struct numa_memblk *bk = &mi->blk[k];
308 if (bi->nid == bk->nid)
310 if (start < bk->end && end > bk->start)
315 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%Lx,%Lx)\n",
316 bi->nid, bi->start, bi->end, bj->start, bj->end,
320 numa_remove_memblk_from(j--, mi);
324 for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
325 mi->blk[i].start = mi->blk[i].end = 0;
326 mi->blk[i].nid = NUMA_NO_NODE;
333 * Set nodes, which have memory in @mi, in *@nodemask.
335 static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
336 const struct numa_meminfo *mi)
340 for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
341 if (mi->blk[i].start != mi->blk[i].end &&
342 mi->blk[i].nid != NUMA_NO_NODE)
343 node_set(mi->blk[i].nid, *nodemask);
347 * numa_reset_distance - Reset NUMA distance table
349 * The current table is freed. The next numa_set_distance() call will
352 void __init numa_reset_distance(void)
354 size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]);
356 /* numa_distance could be 1LU marking allocation failure, test cnt */
357 if (numa_distance_cnt)
358 memblock_x86_free_range(__pa(numa_distance),
359 __pa(numa_distance) + size);
360 numa_distance_cnt = 0;
361 numa_distance = NULL; /* enable table creation */
364 static int __init numa_alloc_distance(void)
366 nodemask_t nodes_parsed;
371 /* size the new table and allocate it */
372 nodes_parsed = numa_nodes_parsed;
373 numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
375 for_each_node_mask(i, nodes_parsed)
378 size = cnt * cnt * sizeof(numa_distance[0]);
380 phys = memblock_find_in_range(0, PFN_PHYS(max_pfn_mapped),
382 if (phys == MEMBLOCK_ERROR) {
383 pr_warning("NUMA: Warning: can't allocate distance table!\n");
384 /* don't retry until explicitly reset */
385 numa_distance = (void *)1LU;
388 memblock_x86_reserve_range(phys, phys + size, "NUMA DIST");
390 numa_distance = __va(phys);
391 numa_distance_cnt = cnt;
393 /* fill with the default distances */
394 for (i = 0; i < cnt; i++)
395 for (j = 0; j < cnt; j++)
396 numa_distance[i * cnt + j] = i == j ?
397 LOCAL_DISTANCE : REMOTE_DISTANCE;
398 printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
405 * numa_set_distance - Set NUMA distance from one NUMA to another
406 * @from: the 'from' node to set distance
407 * @to: the 'to' node to set distance
408 * @distance: NUMA distance
410 * Set the distance from node @from to @to to @distance. If distance table
411 * doesn't exist, one which is large enough to accommodate all the currently
412 * known nodes will be created.
414 * If such table cannot be allocated, a warning is printed and further
415 * calls are ignored until the distance table is reset with
416 * numa_reset_distance().
418 * If @from or @to is higher than the highest known node at the time of
419 * table creation or @distance doesn't make sense, the call is ignored.
420 * This is to allow simplification of specific NUMA config implementations.
422 void __init numa_set_distance(int from, int to, int distance)
424 if (!numa_distance && numa_alloc_distance() < 0)
427 if (from >= numa_distance_cnt || to >= numa_distance_cnt) {
428 printk_once(KERN_DEBUG "NUMA: Debug: distance out of bound, from=%d to=%d distance=%d\n",
433 if ((u8)distance != distance ||
434 (from == to && distance != LOCAL_DISTANCE)) {
435 pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
440 numa_distance[from * numa_distance_cnt + to] = distance;
444 int __node_distance(int from, int to)
446 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
447 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
448 return numa_distance[from * numa_distance_cnt + to];
450 EXPORT_SYMBOL(__node_distance);
453 * Sanity check to catch more bad NUMA configurations (they are amazingly
454 * common). Make sure the nodes cover all memory.
456 static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
458 u64 numaram, e820ram;
462 for (i = 0; i < mi->nr_blks; i++) {
463 u64 s = mi->blk[i].start >> PAGE_SHIFT;
464 u64 e = mi->blk[i].end >> PAGE_SHIFT;
466 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
467 if ((s64)numaram < 0)
471 e820ram = max_pfn - (memblock_x86_hole_size(0,
472 PFN_PHYS(max_pfn)) >> PAGE_SHIFT);
473 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
474 if ((s64)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
475 printk(KERN_ERR "NUMA: nodes only cover %LuMB of your %LuMB e820 RAM. Not used.\n",
476 (numaram << PAGE_SHIFT) >> 20,
477 (e820ram << PAGE_SHIFT) >> 20);
483 static int __init numa_register_memblks(struct numa_meminfo *mi)
487 /* Account for nodes with cpus and no memory */
488 node_possible_map = numa_nodes_parsed;
489 numa_nodemask_from_meminfo(&node_possible_map, mi);
490 if (WARN_ON(nodes_empty(node_possible_map)))
493 for (i = 0; i < mi->nr_blks; i++)
494 memblock_x86_register_active_regions(mi->blk[i].nid,
495 mi->blk[i].start >> PAGE_SHIFT,
496 mi->blk[i].end >> PAGE_SHIFT);
498 /* for out of order entries */
500 if (!numa_meminfo_cover_memory(mi))
503 /* Finally register nodes. */
504 for_each_node_mask(nid, node_possible_map) {
505 u64 start = PFN_PHYS(max_pfn);
508 for (i = 0; i < mi->nr_blks; i++) {
509 if (nid != mi->blk[i].nid)
511 start = min(mi->blk[i].start, start);
512 end = max(mi->blk[i].end, end);
516 setup_node_bootmem(nid, start, end);
523 * There are unfortunately some poorly designed mainboards around that
524 * only connect memory to a single CPU. This breaks the 1:1 cpu->node
525 * mapping. To avoid this fill in the mapping for all possible CPUs,
526 * as the number of CPUs is not known yet. We round robin the existing
529 void __init numa_init_array(void)
533 rr = first_node(node_online_map);
534 for (i = 0; i < nr_cpu_ids; i++) {
535 if (early_cpu_to_node(i) != NUMA_NO_NODE)
537 numa_set_node(i, rr);
538 rr = next_node(rr, node_online_map);
539 if (rr == MAX_NUMNODES)
540 rr = first_node(node_online_map);
544 static int __init numa_init(int (*init_func)(void))
549 for (i = 0; i < MAX_LOCAL_APIC; i++)
550 set_apicid_to_node(i, NUMA_NO_NODE);
552 nodes_clear(numa_nodes_parsed);
553 nodes_clear(node_possible_map);
554 nodes_clear(node_online_map);
555 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
556 remove_all_active_ranges();
557 numa_reset_distance();
562 ret = numa_cleanup_meminfo(&numa_meminfo);
566 numa_emulation(&numa_meminfo, numa_distance_cnt);
568 ret = numa_register_memblks(&numa_meminfo);
572 for (i = 0; i < nr_cpu_ids; i++) {
573 int nid = early_cpu_to_node(i);
575 if (nid == NUMA_NO_NODE)
577 if (!node_online(nid))
585 * dummy_numa_init - Fallback dummy NUMA init
587 * Used if there's no underlying NUMA architecture, NUMA initialization
588 * fails, or NUMA is disabled on the command line.
590 * Must online at least one node and add memory blocks that cover all
591 * allowed memory. This function must not fail.
593 static int __init dummy_numa_init(void)
595 printk(KERN_INFO "%s\n",
596 numa_off ? "NUMA turned off" : "No NUMA configuration found");
597 printk(KERN_INFO "Faking a node at %016Lx-%016Lx\n",
598 0LLU, PFN_PHYS(max_pfn));
600 node_set(0, numa_nodes_parsed);
601 numa_add_memblk(0, 0, PFN_PHYS(max_pfn));
607 * x86_numa_init - Initialize NUMA
609 * Try each configured NUMA initialization method until one succeeds. The
610 * last fallback is dummy single node config encomapssing whole memory and
613 void __init x86_numa_init(void)
616 #ifdef CONFIG_ACPI_NUMA
617 if (!numa_init(x86_acpi_numa_init))
620 #ifdef CONFIG_AMD_NUMA
621 if (!numa_init(amd_numa_init))
626 numa_init(dummy_numa_init);
629 static __init int find_near_online_node(int node)
632 int min_val = INT_MAX;
635 for_each_online_node(n) {
636 val = node_distance(node, n);
648 * Setup early cpu_to_node.
650 * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
651 * and apicid_to_node[] tables have valid entries for a CPU.
652 * This means we skip cpu_to_node[] initialisation for NUMA
653 * emulation and faking node case (when running a kernel compiled
654 * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
655 * is already initialized in a round robin manner at numa_init_array,
656 * prior to this call, and this initialization is good enough
657 * for the fake NUMA cases.
659 * Called before the per_cpu areas are setup.
661 void __init init_cpu_to_node(void)
664 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
666 BUG_ON(cpu_to_apicid == NULL);
668 for_each_possible_cpu(cpu) {
669 int node = numa_cpu_node(cpu);
671 if (node == NUMA_NO_NODE)
673 if (!node_online(node))
674 node = find_near_online_node(node);
675 numa_set_node(cpu, node);
679 #ifndef CONFIG_DEBUG_PER_CPU_MAPS
681 # ifndef CONFIG_NUMA_EMU
682 void __cpuinit numa_add_cpu(int cpu)
684 cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
687 void __cpuinit numa_remove_cpu(int cpu)
689 cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
691 # endif /* !CONFIG_NUMA_EMU */
693 #else /* !CONFIG_DEBUG_PER_CPU_MAPS */
695 int __cpu_to_node(int cpu)
697 if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
699 "cpu_to_node(%d): usage too early!\n", cpu);
701 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
703 return per_cpu(x86_cpu_to_node_map, cpu);
705 EXPORT_SYMBOL(__cpu_to_node);
708 * Same function as cpu_to_node() but used if called before the
709 * per_cpu areas are setup.
711 int early_cpu_to_node(int cpu)
713 if (early_per_cpu_ptr(x86_cpu_to_node_map))
714 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
716 if (!cpu_possible(cpu)) {
718 "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
722 return per_cpu(x86_cpu_to_node_map, cpu);
725 void debug_cpumask_set_cpu(int cpu, int node, bool enable)
727 struct cpumask *mask;
730 if (node == NUMA_NO_NODE) {
731 /* early_cpu_to_node() already emits a warning and trace */
734 mask = node_to_cpumask_map[node];
736 pr_err("node_to_cpumask_map[%i] NULL\n", node);
742 cpumask_set_cpu(cpu, mask);
744 cpumask_clear_cpu(cpu, mask);
746 cpulist_scnprintf(buf, sizeof(buf), mask);
747 printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
748 enable ? "numa_add_cpu" : "numa_remove_cpu",
753 # ifndef CONFIG_NUMA_EMU
754 static void __cpuinit numa_set_cpumask(int cpu, bool enable)
756 debug_cpumask_set_cpu(cpu, early_cpu_to_node(cpu), enable);
759 void __cpuinit numa_add_cpu(int cpu)
761 numa_set_cpumask(cpu, true);
764 void __cpuinit numa_remove_cpu(int cpu)
766 numa_set_cpumask(cpu, false);
768 # endif /* !CONFIG_NUMA_EMU */
771 * Returns a pointer to the bitmask of CPUs on Node 'node'.
773 const struct cpumask *cpumask_of_node(int node)
775 if (node >= nr_node_ids) {
777 "cpumask_of_node(%d): node > nr_node_ids(%d)\n",
780 return cpu_none_mask;
782 if (node_to_cpumask_map[node] == NULL) {
784 "cpumask_of_node(%d): no node_to_cpumask_map!\n",
787 return cpu_online_mask;
789 return node_to_cpumask_map[node];
791 EXPORT_SYMBOL(cpumask_of_node);
793 #endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
795 #if defined(CONFIG_X86_64) && defined(CONFIG_MEMORY_HOTPLUG)
796 int memory_add_physaddr_to_nid(u64 start)
798 struct numa_meminfo *mi = &numa_meminfo;
799 int nid = mi->blk[0].nid;
802 for (i = 0; i < mi->nr_blks; i++)
803 if (mi->blk[i].start <= start && mi->blk[i].end > start)
804 nid = mi->blk[i].nid;
807 EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);