1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
/*
* NUMA support for s390
*
* NUMA emulation (aka fake NUMA) distributes the available memory to nodes
* without using real topology information about the physical memory of the
* machine.
*
* It distributes the available CPUs to nodes while respecting the original
* machine topology information. This is done by trying to avoid to separate
* CPUs which reside on the same book or even on the same MC.
*
* Because the current Linux scheduler code requires a stable cpu to node
* mapping, cores are pinned to nodes when the first CPU thread is set online.
*
* Copyright IBM Corp. 2015
*/
#define KMSG_COMPONENT "numa_emu"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
#include <linux/kernel.h>
#include <linux/cpumask.h>
#include <linux/memblock.h>
#include <linux/node.h>
#include <linux/memory.h>
#include <linux/slab.h>
#include <asm/smp.h>
#include <asm/topology.h>
#include "numa_mode.h"
#include "toptree.h"
/* Distances between the different system components */
#define DIST_EMPTY 0
#define DIST_CORE 1
#define DIST_MC 2
#define DIST_BOOK 3
#define DIST_DRAWER 4
#define DIST_MAX 5
/* Node distance reported to common code */
#define EMU_NODE_DIST 10
/* Node ID for free (not yet pinned) cores */
#define NODE_ID_FREE -1
/* Different levels of toptree */
enum toptree_level {CORE, MC, BOOK, DRAWER, NODE, TOPOLOGY};
/* The two toptree IDs */
enum {TOPTREE_ID_PHYS, TOPTREE_ID_NUMA};
/* Number of NUMA nodes */
static int emu_nodes = 1;
/* NUMA stripe size */
static unsigned long emu_size;
/*
* Node to core pinning information updates are protected by
* "sched_domains_mutex".
*/
static struct {
s32 to_node_id[CONFIG_NR_CPUS]; /* Pinned core to node mapping */
int total; /* Total number of pinned cores */
int per_node_target; /* Cores per node without extra cores */
int per_node[MAX_NUMNODES]; /* Number of cores pinned to node */
} *emu_cores;
/*
* Pin a core to a node
*/
static void pin_core_to_node(int core_id, int node_id)
{
if (emu_cores->to_node_id[core_id] == NODE_ID_FREE) {
emu_cores->per_node[node_id]++;
emu_cores->to_node_id[core_id] = node_id;
emu_cores->total++;
} else {
WARN_ON(emu_cores->to_node_id[core_id] != node_id);
}
}
/*
* Number of pinned cores of a node
*/
static int cores_pinned(struct toptree *node)
{
return emu_cores->per_node[node->id];
}
/*
* ID of the node where the core is pinned (or NODE_ID_FREE)
*/
static int core_pinned_to_node_id(struct toptree *core)
{
return emu_cores->to_node_id[core->id];
}
/*
* Number of cores in the tree that are not yet pinned
*/
static int cores_free(struct toptree *tree)
{
struct toptree *core;
int count = 0;
toptree_for_each(core, tree, CORE) {
if (core_pinned_to_node_id(core) == NODE_ID_FREE)
count++;
}
return count;
}
/*
* Return node of core
*/
static struct toptree *core_node(struct toptree *core)
{
return core->parent->parent->parent->parent;
}
/*
* Return drawer of core
*/
static struct toptree *core_drawer(struct toptree *core)
{
return core->parent->parent->parent;
}
/*
* Return book of core
*/
static struct toptree *core_book(struct toptree *core)
{
return core->parent->parent;
}
/*
* Return mc of core
*/
static struct toptree *core_mc(struct toptree *core)
{
return core->parent;
}
/*
* Distance between two cores
*/
static int dist_core_to_core(struct toptree *core1, struct toptree *core2)
{
if (core_drawer(core1)->id != core_drawer(core2)->id)
return DIST_DRAWER;
if (core_book(core1)->id != core_book(core2)->id)
return DIST_BOOK;
if (core_mc(core1)->id != core_mc(core2)->id)
return DIST_MC;
/* Same core or sibling on same MC */
return DIST_CORE;
}
/*
* Distance of a node to a core
*/
static int dist_node_to_core(struct toptree *node, struct toptree *core)
{
struct toptree *core_node;
int dist_min = DIST_MAX;
toptree_for_each(core_node, node, CORE)
dist_min = min(dist_min, dist_core_to_core(core_node, core));
return dist_min == DIST_MAX ? DIST_EMPTY : dist_min;
}
/*
* Unify will delete empty nodes, therefore recreate nodes.
*/
static void toptree_unify_tree(struct toptree *tree)
{
int nid;
toptree_unify(tree);
for (nid = 0; nid < emu_nodes; nid++)
toptree_get_child(tree, nid);
}
/*
* Find the best/nearest node for a given core and ensure that no node
* gets more than "emu_cores->per_node_target + extra" cores.
*/
static struct toptree *node_for_core(struct toptree *numa, struct toptree *core,
int extra)
{
struct toptree *node, *node_best = NULL;
int dist_cur, dist_best, cores_target;
cores_target = emu_cores->per_node_target + extra;
dist_best = DIST_MAX;
node_best = NULL;
toptree_for_each(node, numa, NODE) {
/* Already pinned cores must use their nodes */
if (core_pinned_to_node_id(core) == node->id) {
node_best = node;
break;
}
/* Skip nodes that already have enough cores */
if (cores_pinned(node) >= cores_target)
continue;
dist_cur = dist_node_to_core(node, core);
if (dist_cur < dist_best) {
dist_best = dist_cur;
node_best = node;
}
}
return node_best;
}
/*
* Find the best node for each core with respect to "extra" core count
*/
static void toptree_to_numa_single(struct toptree *numa, struct toptree *phys,
int extra)
{
struct toptree *node, *core, *tmp;
toptree_for_each_safe(core, tmp, phys, CORE) {
node = node_for_core(numa, core, extra);
if (!node)
return;
toptree_move(core, node);
pin_core_to_node(core->id, node->id);
}
}
/*
* Move structures of given level to specified NUMA node
*/
static void move_level_to_numa_node(struct toptree *node, struct toptree *phys,
enum toptree_level level, bool perfect)
{
int cores_free, cores_target = emu_cores->per_node_target;
struct toptree *cur, *tmp;
toptree_for_each_safe(cur, tmp, phys, level) {
cores_free = cores_target - toptree_count(node, CORE);
if (perfect) {
if (cores_free == toptree_count(cur, CORE))
toptree_move(cur, node);
} else {
if (cores_free >= toptree_count(cur, CORE))
toptree_move(cur, node);
}
}
}
/*
* Move structures of a given level to NUMA nodes. If "perfect" is specified
* move only perfectly fitting structures. Otherwise move also smaller
* than needed structures.
*/
static void move_level_to_numa(struct toptree *numa, struct toptree *phys,
enum toptree_level level, bool perfect)
{
struct toptree *node;
toptree_for_each(node, numa, NODE)
move_level_to_numa_node(node, phys, level, perfect);
}
/*
* For the first run try to move the big structures
*/
static void toptree_to_numa_first(struct toptree *numa, struct toptree *phys)
{
struct toptree *core;
/* Always try to move perfectly fitting structures first */
move_level_to_numa(numa, phys, DRAWER, true);
move_level_to_numa(numa, phys,
|