4 #include <linux/kernel.h>
5 #include <linux/errno.h>
6 #include <linux/topology.h>
7 #include <linux/memblock.h>
8 #include <linux/bootmem.h>
11 #include "numa_internal.h"
13 static int emu_nid_to_phys[MAX_NUMNODES];
14 static char *emu_cmdline __initdata;
16 void __init numa_emu_cmdline(char *str)
21 static int __init emu_find_memblk_by_nid(int nid, const struct numa_meminfo *mi)
25 for (i = 0; i < mi->nr_blks; i++)
26 if (mi->blk[i].nid == nid)
31 static u64 __init mem_hole_size(u64 start, u64 end)
33 unsigned long start_pfn = PFN_UP(start);
34 unsigned long end_pfn = PFN_DOWN(end);
36 if (start_pfn < end_pfn)
37 return PFN_PHYS(absent_pages_in_range(start_pfn, end_pfn));
42 * Sets up nid to range from @start to @end. The return value is -errno if
43 * something went wrong, 0 otherwise.
45 static int __init emu_setup_memblk(struct numa_meminfo *ei,
46 struct numa_meminfo *pi,
47 int nid, int phys_blk, u64 size)
49 struct numa_memblk *eb = &ei->blk[ei->nr_blks];
50 struct numa_memblk *pb = &pi->blk[phys_blk];
52 if (ei->nr_blks >= NR_NODE_MEMBLKS) {
53 pr_err("NUMA: Too many emulated memblks, failing emulation\n");
58 eb->start = pb->start;
59 eb->end = pb->start + size;
62 if (emu_nid_to_phys[nid] == NUMA_NO_NODE)
63 emu_nid_to_phys[nid] = nid;
66 if (pb->start >= pb->end) {
67 WARN_ON_ONCE(pb->start > pb->end);
68 numa_remove_memblk_from(phys_blk, pi);
71 printk(KERN_INFO "Faking node %d at [mem %#018Lx-%#018Lx] (%LuMB)\n",
72 nid, eb->start, eb->end - 1, (eb->end - eb->start) >> 20);
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.
80 static int __init split_nodes_interleave(struct numa_meminfo *ei,
81 struct numa_meminfo *pi,
82 u64 addr, u64 max_addr, int nr_nodes)
84 nodemask_t physnode_mask = NODE_MASK_NONE;
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;
99 * Calculate target node size. x86_32 freaks on __udivdi3() so do
100 * the division in ulong number of pages and convert back.
102 size = max_addr - addr - mem_hole_size(addr, max_addr);
103 size = PFN_PHYS((unsigned long)(size >> PAGE_SHIFT) / nr_nodes);
106 * Calculate the number of big nodes that can be allocated as a result
107 * of consolidating the remainder.
109 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
112 size &= FAKE_NODE_MIN_HASH_MASK;
114 pr_err("Not enough memory for each node. "
115 "NUMA emulation disabled.\n");
119 for (i = 0; i < pi->nr_blks; i++)
120 node_set(pi->blk[i].nid, physnode_mask);
123 * Continue to fill physical nodes with fake nodes until there is no
124 * memory left on any of them.
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;
132 phys_blk = emu_find_memblk_by_nid(i, pi);
134 node_clear(i, physnode_mask);
137 start = pi->blk[phys_blk].start;
138 limit = pi->blk[phys_blk].end;
142 end += FAKE_NODE_MIN_SIZE;
145 * Continue to add memory to this fake node if its
146 * non-reserved memory is less than the per-node size.
148 while (end - start - mem_hole_size(start, end) < size) {
149 end += FAKE_NODE_MIN_SIZE;
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.
161 if (end < dma32_end && dma32_end - end -
162 mem_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
166 * If there won't be enough non-reserved memory for the
167 * next node, this one must extend to the end of the
170 if (limit - end - mem_hole_size(end, limit) < size)
173 ret = emu_setup_memblk(ei, pi, nid++ % nr_nodes,
175 min(end, limit) - start);
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.
187 static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
189 u64 end = start + size;
191 while (end - start - mem_hole_size(start, end) < size) {
192 end += FAKE_NODE_MIN_SIZE;
193 if (end > max_addr) {
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.
205 static int __init split_nodes_size_interleave(struct numa_meminfo *ei,
206 struct numa_meminfo *pi,
207 u64 addr, u64 max_addr, u64 size)
209 nodemask_t physnode_mask = NODE_MASK_NONE;
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).
222 min_size = (max_addr - addr - mem_hole_size(addr, max_addr)) / MAX_NUMNODES;
223 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);
232 size &= FAKE_NODE_MIN_HASH_MASK;
234 for (i = 0; i < pi->nr_blks; i++)
235 node_set(pi->blk[i].nid, physnode_mask);
238 * Fill physical nodes with fake nodes of size until there is no memory
239 * left on any of them.
241 while (nodes_weight(physnode_mask)) {
242 for_each_node_mask(i, physnode_mask) {
243 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
244 u64 start, limit, end;
247 phys_blk = emu_find_memblk_by_nid(i, pi);
249 node_clear(i, physnode_mask);
252 start = pi->blk[phys_blk].start;
253 limit = pi->blk[phys_blk].end;
255 end = find_end_of_node(start, limit, size);
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.
261 if (end < dma32_end && dma32_end - end -
262 mem_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
266 * If there won't be enough non-reserved memory for the
267 * next node, this one must extend to the end of the
270 if (limit - end - mem_hole_size(end, limit) < size)
273 ret = emu_setup_memblk(ei, pi, nid++ % MAX_NUMNODES,
275 min(end, limit) - start);
283 int __init setup_emu2phys_nid(int *dfl_phys_nid)
285 int i, max_emu_nid = 0;
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) {
291 if (*dfl_phys_nid == NUMA_NO_NODE)
292 *dfl_phys_nid = emu_nid_to_phys[i];
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
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.
309 * On success, the following modifications are made.
311 * - @numa_meminfo is updated to reflect the emulated nodes.
313 * - __apicid_to_node[] is updated such that APIC IDs are mapped to the
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.
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().
323 * If emulation is not enabled or fails, emu_nid_to_phys[] is filled with
324 * identity mapping and no other modification is made.
326 void __init numa_emulation(struct numa_meminfo *numa_meminfo, int numa_dist_cnt)
328 static struct numa_meminfo ei __initdata;
329 static struct numa_meminfo pi __initdata;
330 const u64 max_addr = PFN_PHYS(max_pfn);
331 u8 *phys_dist = NULL;
332 size_t phys_size = numa_dist_cnt * numa_dist_cnt * sizeof(phys_dist[0]);
333 int max_emu_nid, dfl_phys_nid;
339 memset(&ei, 0, sizeof(ei));
342 for (i = 0; i < MAX_NUMNODES; i++)
343 emu_nid_to_phys[i] = NUMA_NO_NODE;
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.
350 if (strchr(emu_cmdline, 'M') || strchr(emu_cmdline, 'G')) {
353 size = memparse(emu_cmdline, &emu_cmdline);
354 ret = split_nodes_size_interleave(&ei, &pi, 0, max_addr, size);
358 n = simple_strtoul(emu_cmdline, &emu_cmdline, 0);
359 ret = split_nodes_interleave(&ei, &pi, 0, max_addr, n);
361 if (*emu_cmdline == ':')
367 if (numa_cleanup_meminfo(&ei) < 0) {
368 pr_warning("NUMA: Warning: constructed meminfo invalid, disabling emulation\n");
372 /* copy the physical distance table */
376 phys = memblock_find_in_range(0, PFN_PHYS(max_pfn_mapped),
377 phys_size, PAGE_SIZE);
379 pr_warning("NUMA: Warning: can't allocate copy of distance table, disabling emulation\n");
382 memblock_reserve(phys, phys_size);
383 phys_dist = __va(phys);
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] =
392 * Determine the max emulated nid and the default phys nid to use
393 * for unmapped nodes.
395 max_emu_nid = setup_emu2phys_nid(&dfl_phys_nid);
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.
405 for (i = 0; i < ARRAY_SIZE(__apicid_to_node); i++) {
406 if (__apicid_to_node[i] == NUMA_NO_NODE)
408 for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
409 if (__apicid_to_node[i] == emu_nid_to_phys[j])
411 __apicid_to_node[i] = j < ARRAY_SIZE(emu_nid_to_phys) ? j : 0;
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)
417 emu_nid_to_phys[i] = dfl_phys_nid;
419 /* transform distance table */
420 numa_reset_distance();
421 for (i = 0; i < max_emu_nid + 1; i++) {
422 for (j = 0; j < max_emu_nid + 1; j++) {
423 int physi = emu_nid_to_phys[i];
424 int physj = emu_nid_to_phys[j];
427 if (get_option(&emu_cmdline, &dist) == 2)
429 else if (physi >= numa_dist_cnt || physj >= numa_dist_cnt)
430 dist = physi == physj ?
431 LOCAL_DISTANCE : REMOTE_DISTANCE;
433 dist = phys_dist[physi * numa_dist_cnt + physj];
435 numa_set_distance(i, j, dist);
439 /* free the copied physical distance table */
441 memblock_free(__pa(phys_dist), phys_size);
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;
450 #ifndef CONFIG_DEBUG_PER_CPU_MAPS
451 void numa_add_cpu(int cpu)
455 nid = early_cpu_to_node(cpu);
456 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
458 physnid = emu_nid_to_phys[nid];
461 * Map the cpu to each emulated node that is allocated on the physical
462 * node of the cpu's apic id.
464 for_each_online_node(nid)
465 if (emu_nid_to_phys[nid] == physnid)
466 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
469 void numa_remove_cpu(int cpu)
473 for_each_online_node(i)
474 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
476 #else /* !CONFIG_DEBUG_PER_CPU_MAPS */
477 static void numa_set_cpumask(int cpu, bool enable)
481 nid = early_cpu_to_node(cpu);
482 if (nid == NUMA_NO_NODE) {
483 /* early_cpu_to_node() already emits a warning and trace */
487 physnid = emu_nid_to_phys[nid];
489 for_each_online_node(nid) {
490 if (emu_nid_to_phys[nid] != physnid)
493 debug_cpumask_set_cpu(cpu, nid, enable);
497 void numa_add_cpu(int cpu)
499 numa_set_cpumask(cpu, true);
502 void numa_remove_cpu(int cpu)
504 numa_set_cpumask(cpu, false);
506 #endif /* !CONFIG_DEBUG_PER_CPU_MAPS */