1 // SPDX-License-Identifier: GPL-2.0
3 * NUMA support for s390
5 * NUMA emulation (aka fake NUMA) distributes the available memory to nodes
6 * without using real topology information about the physical memory of the
9 * It distributes the available CPUs to nodes while respecting the original
10 * machine topology information. This is done by trying to avoid to separate
11 * CPUs which reside on the same book or even on the same MC.
13 * Because the current Linux scheduler code requires a stable cpu to node
14 * mapping, cores are pinned to nodes when the first CPU thread is set online.
16 * Copyright IBM Corp. 2015
19 #define KMSG_COMPONENT "numa_emu"
20 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22 #include <linux/kernel.h>
23 #include <linux/cpumask.h>
24 #include <linux/memblock.h>
25 #include <linux/node.h>
26 #include <linux/memory.h>
27 #include <linux/slab.h>
29 #include <asm/topology.h>
30 #include "numa_mode.h"
33 /* Distances between the different system components */
41 /* Node distance reported to common code */
42 #define EMU_NODE_DIST 10
44 /* Node ID for free (not yet pinned) cores */
45 #define NODE_ID_FREE -1
47 /* Different levels of toptree */
48 enum toptree_level
{CORE
, MC
, BOOK
, DRAWER
, NODE
, TOPOLOGY
};
50 /* The two toptree IDs */
51 enum {TOPTREE_ID_PHYS
, TOPTREE_ID_NUMA
};
53 /* Number of NUMA nodes */
54 static int emu_nodes
= 1;
55 /* NUMA stripe size */
56 static unsigned long emu_size
;
59 * Node to core pinning information updates are protected by
60 * "sched_domains_mutex".
63 s32 to_node_id
[CONFIG_NR_CPUS
]; /* Pinned core to node mapping */
64 int total
; /* Total number of pinned cores */
65 int per_node_target
; /* Cores per node without extra cores */
66 int per_node
[MAX_NUMNODES
]; /* Number of cores pinned to node */
70 * Pin a core to a node
72 static void pin_core_to_node(int core_id
, int node_id
)
74 if (emu_cores
->to_node_id
[core_id
] == NODE_ID_FREE
) {
75 emu_cores
->per_node
[node_id
]++;
76 emu_cores
->to_node_id
[core_id
] = node_id
;
79 WARN_ON(emu_cores
->to_node_id
[core_id
] != node_id
);
84 * Number of pinned cores of a node
86 static int cores_pinned(struct toptree
*node
)
88 return emu_cores
->per_node
[node
->id
];
92 * ID of the node where the core is pinned (or NODE_ID_FREE)
94 static int core_pinned_to_node_id(struct toptree
*core
)
96 return emu_cores
->to_node_id
[core
->id
];
100 * Number of cores in the tree that are not yet pinned
102 static int cores_free(struct toptree
*tree
)
104 struct toptree
*core
;
107 toptree_for_each(core
, tree
, CORE
) {
108 if (core_pinned_to_node_id(core
) == NODE_ID_FREE
)
115 * Return node of core
117 static struct toptree
*core_node(struct toptree
*core
)
119 return core
->parent
->parent
->parent
->parent
;
123 * Return drawer of core
125 static struct toptree
*core_drawer(struct toptree
*core
)
127 return core
->parent
->parent
->parent
;
131 * Return book of core
133 static struct toptree
*core_book(struct toptree
*core
)
135 return core
->parent
->parent
;
141 static struct toptree
*core_mc(struct toptree
*core
)
147 * Distance between two cores
149 static int dist_core_to_core(struct toptree
*core1
, struct toptree
*core2
)
151 if (core_drawer(core1
)->id
!= core_drawer(core2
)->id
)
153 if (core_book(core1
)->id
!= core_book(core2
)->id
)
155 if (core_mc(core1
)->id
!= core_mc(core2
)->id
)
157 /* Same core or sibling on same MC */
162 * Distance of a node to a core
164 static int dist_node_to_core(struct toptree
*node
, struct toptree
*core
)
166 struct toptree
*core_node
;
167 int dist_min
= DIST_MAX
;
169 toptree_for_each(core_node
, node
, CORE
)
170 dist_min
= min(dist_min
, dist_core_to_core(core_node
, core
));
171 return dist_min
== DIST_MAX
? DIST_EMPTY
: dist_min
;
175 * Unify will delete empty nodes, therefore recreate nodes.
177 static void toptree_unify_tree(struct toptree
*tree
)
182 for (nid
= 0; nid
< emu_nodes
; nid
++)
183 toptree_get_child(tree
, nid
);
187 * Find the best/nearest node for a given core and ensure that no node
188 * gets more than "emu_cores->per_node_target + extra" cores.
190 static struct toptree
*node_for_core(struct toptree
*numa
, struct toptree
*core
,
193 struct toptree
*node
, *node_best
= NULL
;
194 int dist_cur
, dist_best
, cores_target
;
196 cores_target
= emu_cores
->per_node_target
+ extra
;
197 dist_best
= DIST_MAX
;
199 toptree_for_each(node
, numa
, NODE
) {
200 /* Already pinned cores must use their nodes */
201 if (core_pinned_to_node_id(core
) == node
->id
) {
205 /* Skip nodes that already have enough cores */
206 if (cores_pinned(node
) >= cores_target
)
208 dist_cur
= dist_node_to_core(node
, core
);
209 if (dist_cur
< dist_best
) {
210 dist_best
= dist_cur
;
218 * Find the best node for each core with respect to "extra" core count
220 static void toptree_to_numa_single(struct toptree
*numa
, struct toptree
*phys
,
223 struct toptree
*node
, *core
, *tmp
;
225 toptree_for_each_safe(core
, tmp
, phys
, CORE
) {
226 node
= node_for_core(numa
, core
, extra
);
229 toptree_move(core
, node
);
230 pin_core_to_node(core
->id
, node
->id
);
235 * Move structures of given level to specified NUMA node
237 static void move_level_to_numa_node(struct toptree
*node
, struct toptree
*phys
,
238 enum toptree_level level
, bool perfect
)
240 int cores_free
, cores_target
= emu_cores
->per_node_target
;
241 struct toptree
*cur
, *tmp
;
243 toptree_for_each_safe(cur
, tmp
, phys
, level
) {
244 cores_free
= cores_target
- toptree_count(node
, CORE
);
246 if (cores_free
== toptree_count(cur
, CORE
))
247 toptree_move(cur
, node
);
249 if (cores_free
>= toptree_count(cur
, CORE
))
250 toptree_move(cur
, node
);
256 * Move structures of a given level to NUMA nodes. If "perfect" is specified
257 * move only perfectly fitting structures. Otherwise move also smaller
258 * than needed structures.
260 static void move_level_to_numa(struct toptree
*numa
, struct toptree
*phys
,
261 enum toptree_level level
, bool perfect
)
263 struct toptree
*node
;
265 toptree_for_each(node
, numa
, NODE
)
266 move_level_to_numa_node(node
, phys
, level
, perfect
);
270 * For the first run try to move the big structures
272 static void toptree_to_numa_first(struct toptree
*numa
, struct toptree
*phys
)
274 struct toptree
*core
;
276 /* Always try to move perfectly fitting structures first */
277 move_level_to_numa(numa
, phys
, DRAWER
, true);
278 move_level_to_numa(numa
, phys
, DRAWER
, false);
279 move_level_to_numa(numa
, phys
, BOOK
, true);
280 move_level_to_numa(numa
, phys
, BOOK
, false);
281 move_level_to_numa(numa
, phys
, MC
, true);
282 move_level_to_numa(numa
, phys
, MC
, false);
283 /* Now pin all the moved cores */
284 toptree_for_each(core
, numa
, CORE
)
285 pin_core_to_node(core
->id
, core_node(core
)->id
);
289 * Allocate new topology and create required nodes
291 static struct toptree
*toptree_new(int id
, int nodes
)
293 struct toptree
*tree
;
296 tree
= toptree_alloc(TOPOLOGY
, id
);
299 for (nid
= 0; nid
< nodes
; nid
++) {
300 if (!toptree_get_child(tree
, nid
))
305 panic("NUMA emulation could not allocate topology");
309 * Allocate and initialize core to node mapping
311 static void __ref
create_core_to_node_map(void)
315 emu_cores
= memblock_alloc(sizeof(*emu_cores
), 8);
317 panic("%s: Failed to allocate %zu bytes align=0x%x\n",
318 __func__
, sizeof(*emu_cores
), 8);
319 for (i
= 0; i
< ARRAY_SIZE(emu_cores
->to_node_id
); i
++)
320 emu_cores
->to_node_id
[i
] = NODE_ID_FREE
;
324 * Move cores from physical topology into NUMA target topology
325 * and try to keep as much of the physical topology as possible.
327 static struct toptree
*toptree_to_numa(struct toptree
*phys
)
329 static int first
= 1;
330 struct toptree
*numa
;
333 cores_total
= emu_cores
->total
+ cores_free(phys
);
334 emu_cores
->per_node_target
= cores_total
/ emu_nodes
;
335 numa
= toptree_new(TOPTREE_ID_NUMA
, emu_nodes
);
337 toptree_to_numa_first(numa
, phys
);
340 toptree_to_numa_single(numa
, phys
, 0);
341 toptree_to_numa_single(numa
, phys
, 1);
342 toptree_unify_tree(numa
);
344 WARN_ON(cpumask_weight(&phys
->mask
));
349 * Create a toptree out of the physical topology that we got from the hypervisor
351 static struct toptree
*toptree_from_topology(void)
353 struct toptree
*phys
, *node
, *drawer
, *book
, *mc
, *core
;
354 struct cpu_topology_s390
*top
;
357 phys
= toptree_new(TOPTREE_ID_PHYS
, 1);
359 for_each_cpu(cpu
, &cpus_with_topology
) {
360 top
= &cpu_topology
[cpu
];
361 node
= toptree_get_child(phys
, 0);
362 drawer
= toptree_get_child(node
, top
->drawer_id
);
363 book
= toptree_get_child(drawer
, top
->book_id
);
364 mc
= toptree_get_child(book
, top
->socket_id
);
365 core
= toptree_get_child(mc
, smp_get_base_cpu(cpu
));
366 if (!drawer
|| !book
|| !mc
|| !core
)
367 panic("NUMA emulation could not allocate memory");
368 cpumask_set_cpu(cpu
, &core
->mask
);
369 toptree_update_mask(mc
);
375 * Add toptree core to topology and create correct CPU masks
377 static void topology_add_core(struct toptree
*core
)
379 struct cpu_topology_s390
*top
;
382 for_each_cpu(cpu
, &core
->mask
) {
383 top
= &cpu_topology
[cpu
];
384 cpumask_copy(&top
->thread_mask
, &core
->mask
);
385 cpumask_copy(&top
->core_mask
, &core_mc(core
)->mask
);
386 cpumask_copy(&top
->book_mask
, &core_book(core
)->mask
);
387 cpumask_copy(&top
->drawer_mask
, &core_drawer(core
)->mask
);
388 cpumask_set_cpu(cpu
, &node_to_cpumask_map
[core_node(core
)->id
]);
389 top
->node_id
= core_node(core
)->id
;
394 * Apply toptree to topology and create CPU masks
396 static void toptree_to_topology(struct toptree
*numa
)
398 struct toptree
*core
;
401 /* Clear all node masks */
402 for (i
= 0; i
< MAX_NUMNODES
; i
++)
403 cpumask_clear(&node_to_cpumask_map
[i
]);
405 /* Rebuild all masks */
406 toptree_for_each(core
, numa
, CORE
)
407 topology_add_core(core
);
411 * Show the node to core mapping
413 static void print_node_to_core_map(void)
417 if (!numa_debug_enabled
)
419 printk(KERN_DEBUG
"NUMA node to core mapping\n");
420 for (nid
= 0; nid
< emu_nodes
; nid
++) {
421 printk(KERN_DEBUG
" node %3d: ", nid
);
422 for (cid
= 0; cid
< ARRAY_SIZE(emu_cores
->to_node_id
); cid
++) {
423 if (emu_cores
->to_node_id
[cid
] == nid
)
424 printk(KERN_CONT
"%d ", cid
);
426 printk(KERN_CONT
"\n");
430 static void pin_all_possible_cpus(void)
432 int core_id
, node_id
, cpu
;
433 static int initialized
;
437 print_node_to_core_map();
439 for_each_possible_cpu(cpu
) {
440 core_id
= smp_get_base_cpu(cpu
);
441 if (emu_cores
->to_node_id
[core_id
] != NODE_ID_FREE
)
443 pin_core_to_node(core_id
, node_id
);
444 cpu_topology
[cpu
].node_id
= node_id
;
445 node_id
= (node_id
+ 1) % emu_nodes
;
447 print_node_to_core_map();
452 * Transfer physical topology into a NUMA topology and modify CPU masks
453 * according to the NUMA topology.
455 * Must be called with "sched_domains_mutex" lock held.
457 static void emu_update_cpu_topology(void)
459 struct toptree
*phys
, *numa
;
461 if (emu_cores
== NULL
)
462 create_core_to_node_map();
463 phys
= toptree_from_topology();
464 numa
= toptree_to_numa(phys
);
466 toptree_to_topology(numa
);
468 pin_all_possible_cpus();
472 * If emu_size is not set, use CONFIG_EMU_SIZE. Then round to minimum
473 * alignment (needed for memory hotplug).
475 static unsigned long emu_setup_size_adjust(unsigned long size
)
477 unsigned long size_new
;
479 size
= size
? : CONFIG_EMU_SIZE
;
480 size_new
= roundup(size
, memory_block_size_bytes());
481 if (size_new
== size
)
483 pr_warn("Increasing memory stripe size from %ld MB to %ld MB\n",
484 size
>> 20, size_new
>> 20);
489 * If we have not enough memory for the specified nodes, reduce the node count.
491 static int emu_setup_nodes_adjust(int nodes
)
495 nodes_max
= memblock
.memory
.total_size
/ emu_size
;
496 nodes_max
= max(nodes_max
, 1);
497 if (nodes_max
>= nodes
)
499 pr_warn("Not enough memory for %d nodes, reducing node count\n", nodes
);
506 static void emu_setup(void)
510 emu_size
= emu_setup_size_adjust(emu_size
);
511 emu_nodes
= emu_setup_nodes_adjust(emu_nodes
);
512 for (nid
= 0; nid
< emu_nodes
; nid
++)
513 node_set(nid
, node_possible_map
);
514 pr_info("Creating %d nodes with memory stripe size %ld MB\n",
515 emu_nodes
, emu_size
>> 20);
519 * Return node id for given page number
521 static int emu_pfn_to_nid(unsigned long pfn
)
523 return (pfn
/ (emu_size
>> PAGE_SHIFT
)) % emu_nodes
;
529 static unsigned long emu_align(void)
535 * Return distance between two nodes
537 static int emu_distance(int node1
, int node2
)
539 return (node1
!= node2
) * EMU_NODE_DIST
;
543 * Define callbacks for generic s390 NUMA infrastructure
545 const struct numa_mode numa_mode_emu
= {
548 .update_cpu_topology
= emu_update_cpu_topology
,
549 .__pfn_to_nid
= emu_pfn_to_nid
,
551 .distance
= emu_distance
,
555 * Kernel parameter: emu_nodes=<n>
557 static int __init
early_parse_emu_nodes(char *p
)
561 if (!p
|| kstrtoint(p
, 0, &count
) != 0 || count
<= 0)
563 emu_nodes
= min(count
, MAX_NUMNODES
);
566 early_param("emu_nodes", early_parse_emu_nodes
);
569 * Kernel parameter: emu_size=[<n>[k|M|G|T]]
571 static int __init
early_parse_emu_size(char *p
)
574 emu_size
= memparse(p
, NULL
);
577 early_param("emu_size", early_parse_emu_size
);