-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathautotiering.patch
More file actions
8235 lines (8004 loc) · 232 KB
/
Copy pathautotiering.patch
File metadata and controls
8235 lines (8004 loc) · 232 KB
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/Makefile b/Makefile
index 6886f2290..2294e8a39 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@
VERSION = 5
PATCHLEVEL = 3
SUBLEVEL = 0
-EXTRAVERSION =
+EXTRAVERSION = -autotiering
NAME = Bobtail Squid
# *DOCUMENTATION*
diff --git a/drivers/acpi/hmat/hmat.c b/drivers/acpi/hmat/hmat.c
index 96b7d39a9..f86fe7130 100644
--- a/drivers/acpi/hmat/hmat.c
+++ b/drivers/acpi/hmat/hmat.c
@@ -14,14 +14,18 @@
#include <linux/init.h>
#include <linux/list.h>
#include <linux/list_sort.h>
+#include <linux/memory.h>
+#include <linux/mutex.h>
#include <linux/node.h>
#include <linux/sysfs.h>
-static __initdata u8 hmat_revision;
+static u8 hmat_revision;
-static __initdata LIST_HEAD(targets);
-static __initdata LIST_HEAD(initiators);
-static __initdata LIST_HEAD(localities);
+static LIST_HEAD(targets);
+static LIST_HEAD(initiators);
+static LIST_HEAD(localities);
+
+static DEFINE_MUTEX(target_lock);
/*
* The defined enum order is used to prioritize attributes to break ties when
@@ -36,11 +40,19 @@ enum locality_types {
static struct memory_locality *localities_types[4];
+struct target_cache {
+ struct list_head node;
+ struct node_cache_attrs cache_attrs;
+};
+
struct memory_target {
struct list_head node;
unsigned int memory_pxm;
unsigned int processor_pxm;
struct node_hmem_attrs hmem_attrs;
+ struct list_head caches;
+ struct node_cache_attrs cache_attrs;
+ bool registered;
};
struct memory_initiator {
@@ -53,7 +65,7 @@ struct memory_locality {
struct acpi_hmat_locality *hmat_loc;
};
-static __init struct memory_initiator *find_mem_initiator(unsigned int cpu_pxm)
+static struct memory_initiator *find_mem_initiator(unsigned int cpu_pxm)
{
struct memory_initiator *initiator;
@@ -63,7 +75,7 @@ static __init struct memory_initiator *find_mem_initiator(unsigned int cpu_pxm)
return NULL;
}
-static __init struct memory_target *find_mem_target(unsigned int mem_pxm)
+static struct memory_target *find_mem_target(unsigned int mem_pxm)
{
struct memory_target *target;
@@ -110,6 +122,7 @@ static __init void alloc_memory_target(unsigned int mem_pxm)
target->memory_pxm = mem_pxm;
target->processor_pxm = PXM_INVAL;
list_add_tail(&target->node, &targets);
+ INIT_LIST_HEAD(&target->caches);
}
static __init const char *hmat_data_type(u8 type)
@@ -148,7 +161,7 @@ static __init const char *hmat_data_type_suffix(u8 type)
}
}
-static __init u32 hmat_normalize(u16 entry, u64 base, u8 type)
+static u32 hmat_normalize(u16 entry, u64 base, u8 type)
{
u32 value;
@@ -183,7 +196,7 @@ static __init u32 hmat_normalize(u16 entry, u64 base, u8 type)
return value;
}
-static __init void hmat_update_target_access(struct memory_target *target,
+static void hmat_update_target_access(struct memory_target *target,
u8 type, u32 value)
{
switch (type) {
@@ -314,7 +327,8 @@ static __init int hmat_parse_cache(union acpi_subtable_headers *header,
const unsigned long end)
{
struct acpi_hmat_cache *cache = (void *)header;
- struct node_cache_attrs cache_attrs;
+ struct memory_target *target;
+ struct target_cache *tcache;
u32 attrs;
if (cache->header.length < sizeof(*cache)) {
@@ -328,37 +342,47 @@ static __init int hmat_parse_cache(union acpi_subtable_headers *header,
cache->memory_PD, cache->cache_size, attrs,
cache->number_of_SMBIOShandles);
- cache_attrs.size = cache->cache_size;
- cache_attrs.level = (attrs & ACPI_HMAT_CACHE_LEVEL) >> 4;
- cache_attrs.line_size = (attrs & ACPI_HMAT_CACHE_LINE_SIZE) >> 16;
+ target = find_mem_target(cache->memory_PD);
+ if (!target)
+ return 0;
+
+ tcache = kzalloc(sizeof(*tcache), GFP_KERNEL);
+ if (!tcache) {
+ pr_notice_once("Failed to allocate HMAT cache info\n");
+ return 0;
+ }
+
+ tcache->cache_attrs.size = cache->cache_size;
+ tcache->cache_attrs.level = (attrs & ACPI_HMAT_CACHE_LEVEL) >> 4;
+ tcache->cache_attrs.line_size = (attrs & ACPI_HMAT_CACHE_LINE_SIZE) >> 16;
switch ((attrs & ACPI_HMAT_CACHE_ASSOCIATIVITY) >> 8) {
case ACPI_HMAT_CA_DIRECT_MAPPED:
- cache_attrs.indexing = NODE_CACHE_DIRECT_MAP;
+ tcache->cache_attrs.indexing = NODE_CACHE_DIRECT_MAP;
break;
case ACPI_HMAT_CA_COMPLEX_CACHE_INDEXING:
- cache_attrs.indexing = NODE_CACHE_INDEXED;
+ tcache->cache_attrs.indexing = NODE_CACHE_INDEXED;
break;
case ACPI_HMAT_CA_NONE:
default:
- cache_attrs.indexing = NODE_CACHE_OTHER;
+ tcache->cache_attrs.indexing = NODE_CACHE_OTHER;
break;
}
switch ((attrs & ACPI_HMAT_WRITE_POLICY) >> 12) {
case ACPI_HMAT_CP_WB:
- cache_attrs.write_policy = NODE_CACHE_WRITE_BACK;
+ tcache->cache_attrs.write_policy = NODE_CACHE_WRITE_BACK;
break;
case ACPI_HMAT_CP_WT:
- cache_attrs.write_policy = NODE_CACHE_WRITE_THROUGH;
+ tcache->cache_attrs.write_policy = NODE_CACHE_WRITE_THROUGH;
break;
case ACPI_HMAT_CP_NONE:
default:
- cache_attrs.write_policy = NODE_CACHE_WRITE_OTHER;
+ tcache->cache_attrs.write_policy = NODE_CACHE_WRITE_OTHER;
break;
}
+ list_add_tail(&tcache->node, &target->caches);
- node_add_cache(pxm_to_node(cache->memory_PD), &cache_attrs);
return 0;
}
@@ -435,7 +459,7 @@ static __init int srat_parse_mem_affinity(union acpi_subtable_headers *header,
return 0;
}
-static __init u32 hmat_initiator_perf(struct memory_target *target,
+static u32 hmat_initiator_perf(struct memory_target *target,
struct memory_initiator *initiator,
struct acpi_hmat_locality *hmat_loc)
{
@@ -473,7 +497,7 @@ static __init u32 hmat_initiator_perf(struct memory_target *target,
hmat_loc->data_type);
}
-static __init bool hmat_update_best(u8 type, u32 value, u32 *best)
+static bool hmat_update_best(u8 type, u32 value, u32 *best)
{
bool updated = false;
@@ -517,7 +541,7 @@ static int initiator_cmp(void *priv, struct list_head *a, struct list_head *b)
return ia->processor_pxm - ib->processor_pxm;
}
-static __init void hmat_register_target_initiators(struct memory_target *target)
+static void hmat_register_target_initiators(struct memory_target *target)
{
static DECLARE_BITMAP(p_nodes, MAX_NUMNODES);
struct memory_initiator *initiator;
@@ -577,29 +601,80 @@ static __init void hmat_register_target_initiators(struct memory_target *target)
}
}
-static __init void hmat_register_target_perf(struct memory_target *target)
+static void hmat_register_target_cache(struct memory_target *target)
+{
+ unsigned mem_nid = pxm_to_node(target->memory_pxm);
+ struct target_cache *tcache;
+
+ list_for_each_entry(tcache, &target->caches, node)
+ node_add_cache(mem_nid, &tcache->cache_attrs);
+}
+
+static void hmat_register_target_perf(struct memory_target *target)
{
unsigned mem_nid = pxm_to_node(target->memory_pxm);
node_set_perf_attrs(mem_nid, &target->hmem_attrs, 0);
}
-static __init void hmat_register_targets(void)
+static void hmat_register_target(struct memory_target *target)
{
- struct memory_target *target;
+ if (!node_online(pxm_to_node(target->memory_pxm)))
+ return;
- list_for_each_entry(target, &targets, node) {
+ mutex_lock(&target_lock);
+ if (!target->registered) {
hmat_register_target_initiators(target);
+ hmat_register_target_cache(target);
hmat_register_target_perf(target);
+ target->registered = true;
}
+ mutex_unlock(&target_lock);
+}
+
+static void hmat_register_targets(void)
+{
+ struct memory_target *target;
+
+ list_for_each_entry(target, &targets, node)
+ hmat_register_target(target);
+}
+
+static int hmat_callback(struct notifier_block *self,
+ unsigned long action, void *arg)
+{
+ struct memory_target *target;
+ struct memory_notify *mnb = arg;
+ int pxm, nid = mnb->status_change_nid;
+
+ if (nid == NUMA_NO_NODE || action != MEM_ONLINE)
+ return NOTIFY_OK;
+
+ pxm = node_to_pxm(nid);
+ target = find_mem_target(pxm);
+ if (!target)
+ return NOTIFY_OK;
+
+ hmat_register_target(target);
+ return NOTIFY_OK;
}
+static struct notifier_block hmat_callback_nb = {
+ .notifier_call = hmat_callback,
+ .priority = 2,
+};
+
static __init void hmat_free_structures(void)
{
struct memory_target *target, *tnext;
struct memory_locality *loc, *lnext;
struct memory_initiator *initiator, *inext;
+ struct target_cache *tcache, *cnext;
list_for_each_entry_safe(target, tnext, &targets, node) {
+ list_for_each_entry_safe(tcache, cnext, &target->caches, node) {
+ list_del(&tcache->node);
+ kfree(tcache);
+ }
list_del(&target->node);
kfree(target);
}
@@ -658,6 +733,10 @@ static __init int hmat_init(void)
}
}
hmat_register_targets();
+
+ /* Keep the table and structures if the notifier may use them */
+ if (!register_hotmemory_notifier(&hmat_callback_nb))
+ return 0;
out_put:
hmat_free_structures();
acpi_put_table(tbl);
diff --git a/drivers/base/node.c b/drivers/base/node.c
index 75b7e6f65..9d5f0abf1 100644
--- a/drivers/base/node.c
+++ b/drivers/base/node.c
@@ -17,6 +17,7 @@
#include <linux/nodemask.h>
#include <linux/cpu.h>
#include <linux/device.h>
+#include <linux/list_sort.h>
#include <linux/pm_runtime.h>
#include <linux/swap.h>
#include <linux/slab.h>
@@ -74,6 +75,9 @@ struct node_access_nodes {
unsigned access;
#ifdef CONFIG_HMEM_REPORTING
struct node_hmem_attrs hmem_attrs;
+ struct list_head target_list;
+ struct list_head target_siblings;
+ int initiator_node;
#endif
};
#define to_access_nodes(dev) container_of(dev, struct node_access_nodes, dev)
@@ -102,6 +106,13 @@ static const struct attribute_group *node_access_node_groups[] = {
NULL,
};
+#define TERMINAL_NODE -1
+static int node_migration[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] = TERMINAL_NODE};
+static int node_demotion[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] = TERMINAL_NODE};
+static int node_promotion[MAX_NUMNODES] = {[0 ... MAX_NUMNODES - 1] = TERMINAL_NODE};
+static DEFINE_SPINLOCK(node_migration_lock);
+static DEFINE_SPINLOCK(node_kswapd_failure_lock);
+
static void node_remove_accesses(struct node *node)
{
struct node_access_nodes *c, *cnext;
@@ -117,6 +128,17 @@ static void node_access_release(struct device *dev)
kfree(to_access_nodes(dev));
}
+static struct node_access_nodes *node_find_access_node(struct node *node,
+ unsigned access)
+{
+ struct node_access_nodes *access_node;
+
+ list_for_each_entry(access_node, &node->access_list, list_node)
+ if (access_node->access == access)
+ return access_node;
+ return NULL;
+}
+
static struct node_access_nodes *node_init_node_access(struct node *node,
unsigned access)
{
@@ -139,6 +161,12 @@ static struct node_access_nodes *node_init_node_access(struct node *node,
if (dev_set_name(dev, "access%u", access))
goto free;
+#ifdef CONFIG_HMEM_REPORTING
+ INIT_LIST_HEAD(&access_node->target_list);
+ INIT_LIST_HEAD(&access_node->target_siblings);
+ access_node->initiator_node = -1;
+#endif
+
if (device_register(dev))
goto free_name;
@@ -175,6 +203,58 @@ static struct attribute *access_attrs[] = {
NULL,
};
+/*
+ * Sort priority: read latency, write latency, read bandwidth, write bandwidth,
+ * and finally capacity. We'll prefer to migrate to nodes with more plentiful
+ * memory.
+ */
+static int access_cmp(void *priv, struct list_head *a, struct list_head *b)
+{
+ struct node_access_nodes *access_a = container_of(a,
+ struct node_access_nodes,
+ target_siblings);
+ struct node_access_nodes *access_b = container_of(b,
+ struct node_access_nodes,
+ target_siblings);
+ struct node *node_a, *node_b;
+ struct sysinfo ia, ib;
+
+ if (access_a->hmem_attrs.read_latency != 0 &&
+ access_b->hmem_attrs.read_latency != 0 &&
+ access_a->hmem_attrs.read_latency !=
+ access_b->hmem_attrs.read_latency)
+ return access_a->hmem_attrs.read_latency -
+ access_b->hmem_attrs.read_latency;
+
+ if (access_a->hmem_attrs.write_latency != 0 &&
+ access_b->hmem_attrs.write_latency != 0 &&
+ access_a->hmem_attrs.write_latency !=
+ access_b->hmem_attrs.write_latency)
+ return access_a->hmem_attrs.write_latency -
+ access_b->hmem_attrs.write_latency;
+
+ if (access_a->hmem_attrs.read_bandwidth != 0 &&
+ access_b->hmem_attrs.read_bandwidth != 0 &&
+ access_a->hmem_attrs.read_bandwidth !=
+ access_b->hmem_attrs.read_bandwidth)
+ return access_b->hmem_attrs.read_bandwidth -
+ access_a->hmem_attrs.read_bandwidth;
+
+ if (access_a->hmem_attrs.write_bandwidth != 0 &&
+ access_b->hmem_attrs.write_bandwidth != 0 &&
+ access_a->hmem_attrs.write_bandwidth !=
+ access_b->hmem_attrs.write_bandwidth)
+ return access_b->hmem_attrs.write_bandwidth -
+ access_a->hmem_attrs.write_bandwidth;
+
+ node_a = to_node(access_a->dev.parent);
+ node_b = to_node(access_b->dev.parent);
+ si_meminfo_node(&ia, node_a->dev.id);
+ si_meminfo_node(&ib, node_b->dev.id);
+
+ return ib.totalram - ia.totalram;
+}
+
/**
* node_set_perf_attrs - Set the performance values for given access class
* @nid: Node identifier to be set
@@ -184,27 +264,57 @@ static struct attribute *access_attrs[] = {
void node_set_perf_attrs(unsigned int nid, struct node_hmem_attrs *hmem_attrs,
unsigned access)
{
- struct node_access_nodes *c;
- struct node *node;
- int i;
+ struct node_access_nodes *m, *c;
+ struct node *node, *initiator;
+ int i, cpu_nid;
if (WARN_ON_ONCE(!node_online(nid)))
return;
node = node_devices[nid];
- c = node_init_node_access(node, access);
- if (!c)
+ m = node_init_node_access(node, access);
+ if (!m)
return;
- c->hmem_attrs = *hmem_attrs;
+ m->hmem_attrs = *hmem_attrs;
for (i = 0; access_attrs[i] != NULL; i++) {
- if (sysfs_add_file_to_group(&c->dev.kobj, access_attrs[i],
+ if (sysfs_add_file_to_group(&m->dev.kobj, access_attrs[i],
"initiators")) {
pr_info("failed to add performance attribute to node %d\n",
nid);
break;
}
}
+
+ if (access != 0)
+ return;
+
+ cpu_nid = m->initiator_node;
+ if (cpu_nid < 0)
+ return;
+
+ initiator = node_devices[cpu_nid];
+ if (!initiator)
+ return;
+
+ c = node_find_access_node(initiator, access);
+ if (!c)
+ return;
+
+ list_add_tail(&m->target_siblings, &c->target_list);
+ list_sort(NULL, &c->target_list, access_cmp);
+
+ i = -1;
+ list_for_each_entry(m, &c->target_list, target_siblings) {
+ node = to_node(m->dev.parent);
+ if (i >= 0) {
+ node_demotion[i] = node->dev.id;
+ node_promotion[node->dev.id] = i;
+ node_migration[i] = node->dev.id;
+ node_migration[node->dev.id] = i;
+ }
+ i = node->dev.id;
+ }
}
/**
@@ -531,6 +641,249 @@ static ssize_t node_read_distance(struct device *dev,
}
static DEVICE_ATTR(distance, S_IRUGO, node_read_distance, NULL);
+static ssize_t kswapd_failures_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct pglist_data *pgdat = NODE_DATA(dev->id);
+ return sprintf(buf, "%d\n", pgdat->kswapd_failures);
+}
+
+static ssize_t migration_path_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ return sprintf(buf, "%d\n", node_migration[dev->id]);
+}
+
+static ssize_t promotion_path_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ return sprintf(buf, "%d\n", node_promotion[dev->id]);
+}
+
+static ssize_t demotion_path_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ return sprintf(buf, "%d\n", node_demotion[dev->id]);
+}
+
+static ssize_t kswapd_failures_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ int err, nid = dev->id;
+ struct pglist_data *pgdat = NODE_DATA(nid);
+ long nr_failures;
+
+ err = kstrtol(buf, 0, &nr_failures);
+ if (err)
+ return -EINVAL;
+
+ if (nr_failures < 0)
+ return -EINVAL;
+
+ spin_lock(&node_kswapd_failure_lock);
+ WRITE_ONCE(pgdat->kswapd_failures, nr_failures);
+ spin_unlock(&node_kswapd_failure_lock);
+
+ return count;
+}
+
+static ssize_t migration_path_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ int i, err, nid = dev->id;
+ nodemask_t visited = NODE_MASK_NONE;
+ long next;
+
+ err = kstrtol(buf, 0, &next);
+ if (err)
+ return -EINVAL;
+
+ if (next < 0) {
+ spin_lock(&node_migration_lock);
+ WRITE_ONCE(node_migration[nid], TERMINAL_NODE);
+ spin_unlock(&node_migration_lock);
+ return count;
+ }
+ if (next >= MAX_NUMNODES || !node_online(next))
+ return -EINVAL;
+
+ /*
+ * Follow the entire migration path from 'nid' through the point where
+ * we hit a TERMINAL_NODE.
+ *
+ * Don't allow loops migration cycles in the path.
+ */
+ node_set(nid, visited);
+ spin_lock(&node_migration_lock);
+ for (i = next; node_migration[i] != TERMINAL_NODE;
+ i = node_migration[i]) {
+ /* Fail if we have visited this node already */
+ if (node_test_and_set(i, visited)) {
+ spin_unlock(&node_migration_lock);
+ return -EINVAL;
+ }
+ }
+ WRITE_ONCE(node_migration[nid], next);
+
+ spin_unlock(&node_migration_lock);
+
+ return count;
+}
+
+static ssize_t demotion_path_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ int i, err, nid = dev->id;
+ nodemask_t visited = NODE_MASK_NONE;
+ long next;
+
+ err = kstrtol(buf, 0, &next);
+ if (err)
+ return -EINVAL;
+
+ if (next < 0) {
+ spin_lock(&node_migration_lock);
+ WRITE_ONCE(node_demotion[nid], TERMINAL_NODE);
+ spin_unlock(&node_migration_lock);
+ return count;
+ }
+ if (next >= MAX_NUMNODES || !node_online(next))
+ return -EINVAL;
+
+ /*
+ * Follow the entire migration path from 'nid' through the point where
+ * we hit a TERMINAL_NODE.
+ *
+ * Don't allow loops migration cycles in the path.
+ */
+ node_set(nid, visited);
+ spin_lock(&node_migration_lock);
+ for (i = next; node_demotion[i] != TERMINAL_NODE;
+ i = node_demotion[i]) {
+ /* Fail if we have visited this node already */
+ if (node_test_and_set(i, visited)) {
+ spin_unlock(&node_migration_lock);
+ return -EINVAL;
+ }
+ }
+
+ WRITE_ONCE(node_demotion[nid], next);
+
+ spin_unlock(&node_migration_lock);
+
+ return count;
+}
+
+static ssize_t promotion_path_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ int i, err, nid = dev->id;
+ nodemask_t visited = NODE_MASK_NONE;
+ long next;
+
+ err = kstrtol(buf, 0, &next);
+ if (err)
+ return -EINVAL;
+
+ if (next < 0) {
+ spin_lock(&node_migration_lock);
+ WRITE_ONCE(node_promotion[nid], TERMINAL_NODE);
+ spin_unlock(&node_migration_lock);
+ return count;
+ }
+ if (next >= MAX_NUMNODES || !node_online(next))
+ return -EINVAL;
+
+ /*
+ * Follow the entire migration path from 'nid' through the point where
+ * we hit a TERMINAL_NODE.
+ *
+ * Don't allow loops migration cycles in the path.
+ */
+ node_set(nid, visited);
+ spin_lock(&node_migration_lock);
+ for (i = next; node_promotion[i] != TERMINAL_NODE;
+ i = node_promotion[i]) {
+ /* Fail if we have visited this node already */
+ if (node_test_and_set(i, visited)) {
+ spin_unlock(&node_migration_lock);
+ return -EINVAL;
+ }
+ }
+ WRITE_ONCE(node_promotion[nid], next);
+
+ spin_unlock(&node_migration_lock);
+
+ return count;
+}
+static DEVICE_ATTR_RW(migration_path);
+static DEVICE_ATTR_RW(promotion_path);
+static DEVICE_ATTR_RW(demotion_path);
+static DEVICE_ATTR_RW(kswapd_failures);
+
+/**
+ * next_migration_node() - Get the next node in the migration path
+ * @current_node: The starting node to lookup the next node
+ *
+ * @returns: node id for next memory node in the migration path hierarchy from
+ * @current_node; -1 if @current_node is terminal or its migration
+ * node is not online.
+ */
+int next_migration_node(int current_node)
+{
+ int nid = READ_ONCE(node_migration[current_node]);
+
+ if (nid >= 0 && node_online(nid))
+ return nid;
+ return TERMINAL_NODE;
+}
+
+int next_promotion_node(int current_node)
+{
+ int nid = READ_ONCE(node_promotion[current_node]);
+
+ if (nid >= 0 && node_online(nid))
+ return nid;
+ return TERMINAL_NODE;
+}
+
+int next_demotion_node(int current_node)
+{
+ int nid = READ_ONCE(node_demotion[current_node]);
+
+ if (nid >= 0 && node_online(nid))
+ return nid;
+ return TERMINAL_NODE;
+}
+
+int is_top_node(int current_node) {
+ if (current_node == TERMINAL_NODE)
+ return false;
+
+ if (next_promotion_node(current_node) == TERMINAL_NODE)
+ return true;
+ else
+ return false;
+}
+
+int is_bottom_node(int current_node) {
+ if (current_node == TERMINAL_NODE)
+ return false;
+
+ if (next_demotion_node(current_node) == TERMINAL_NODE)
+ return true;
+ else
+ return false;
+}
+
static struct attribute *node_dev_attrs[] = {
&dev_attr_cpumap.attr,
&dev_attr_cpulist.attr,
@@ -538,6 +891,10 @@ static struct attribute *node_dev_attrs[] = {
&dev_attr_numastat.attr,
&dev_attr_distance.attr,
&dev_attr_vmstat.attr,
+ &dev_attr_migration_path.attr,
+ &dev_attr_promotion_path.attr,
+ &dev_attr_demotion_path.attr,
+ &dev_attr_kswapd_failures.attr,
NULL
};
ATTRIBUTE_GROUPS(node_dev);
@@ -714,6 +1071,9 @@ int register_memory_node_under_compute_node(unsigned int mem_nid,
if (ret)
goto err;
+#ifdef CONFIG_HMEM_REPORTING
+ target->initiator_node = cpu_nid;
+#endif
return 0;
err:
sysfs_remove_link_from_group(&initiator->dev.kobj, "targets",
diff --git a/include/linux/exchange.h b/include/linux/exchange.h
new file mode 100644
index 000000000..7a70ac29f
--- /dev/null
+++ b/include/linux/exchange.h
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_EXCHANGE_H
+#define _LINUX_EXCHANGE_H
+
+#include <linux/migrate.h>
+#include <linux/list.h>
+
+struct exchange_page_info {
+ struct page *from_page;
+ struct page *to_page;
+
+ struct anon_vma *from_anon_vma;
+ struct anon_vma *to_anon_vma;
+
+ int from_page_was_mapped;
+ int to_page_was_mapped;
+
+ pgoff_t from_index, to_index;
+
+ struct list_head list;
+};
+
+int exchange_two_pages(struct page *, struct page *, enum migrate_mode mode);
+int try_exchange_page(struct page *page, int dst_nid);
+int exchange_pages(struct list_head *, enum migrate_mode mode);
+int exchange_pages_concur(struct list_head *exchange_list,
+ enum migrate_mode mode);
+int exchange_pages_between_nodes_batch(const int from_nid, const int to_nid);
+int exchange_page_lists_mthread(struct page **to, struct page **from, int nr_pages);
+int exchange_page_mthread(struct page *to, struct page *from, int nr_pages);
+void wakeup_kexchanged(int node);
+
+#endif /* _LINUX_EXCHANGE_H */
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index edfca4278..50126c47c 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -101,7 +101,9 @@ long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
long freed);
bool isolate_huge_page(struct page *page, struct list_head *list);
void putback_active_hugepage(struct page *page);
-void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason);
+struct migrate_detail; /* hack for crazy include ordering */
+void move_hugetlb_state(struct page *oldpage, struct page *newpage,
+ struct migrate_detail *m_detail);
void free_huge_page(struct page *page);
void hugetlb_fix_reserve_counts(struct inode *inode);
extern struct mutex *hugetlb_fault_mutex_table;
@@ -192,7 +194,7 @@ static inline bool isolate_huge_page(struct page *page, struct list_head *list)
return false;
}
#define putback_active_hugepage(p) do {} while (0)
-#define move_hugetlb_state(old, new, reason) do {} while (0)
+#define move_hugetlb_state(old, new, m_detail) do {} while (0)
static inline unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
unsigned long address, unsigned long end, pgprot_t newprot)
diff --git a/include/linux/ksm.h b/include/linux/ksm.h
index e48b1e453..461793e87 100644
--- a/include/linux/ksm.h
+++ b/include/linux/ksm.h
@@ -53,6 +53,7 @@ struct page *ksm_might_need_to_copy(struct page *page,
void rmap_walk_ksm(struct page *page, struct rmap_walk_control *rwc);
void ksm_migrate_page(struct page *newpage, struct page *oldpage);
+void ksm_exchange_page(struct page *to_page, struct page *from_page);
bool reuse_ksm_page(struct page *page,
struct vm_area_struct *vma, unsigned long address);
@@ -88,6 +89,10 @@ static inline void rmap_walk_ksm(struct page *page,
static inline void ksm_migrate_page(struct page *newpage, struct page *oldpage)
{
}
+static inline void ksm_exchange_page(struct page *to_page,
+ struct page *from_page)
+{
+}
static inline bool reuse_ksm_page(struct page *page,
struct vm_area_struct *vma, unsigned long address)
{
diff --git a/include/linux/migrate.h b/include/linux/migrate.h
index 7f04754c7..9ad306396 100644
--- a/include/linux/migrate.h
+++ b/include/linux/migrate.h
@@ -25,9 +25,45 @@ enum migrate_reason {
MR_MEMPOLICY_MBIND,
MR_NUMA_MISPLACED,
MR_CONTIG_RANGE,
+ MR_DEMOTION,
+ MR_PROMOTION,
MR_TYPES
};
+enum migrate_fail_reason {
+ MFR_UNKNOWN,
+ MFR_DST_NODE_FULL,
+ MFR_NUMA_ISOLATE,
+ MFR_NOMEM_FAIL,
+ MFR_REFCOUNT_FAIL,
+ MFR_NR_REASONS
+};
+
+enum migrate_hmem_reason {
+ MR_HMEM_UNKNOWN,
+ MR_HMEM_DEMOTE,
+ MR_HMEM_LOCAL_PROMOTE,
+ MR_HMEM_REMOTE_PROMOTE,
+ MR_HMEM_MIGRATE,
+ MR_HMEM_NR_REASONS
+};
+
+enum migrate_hmem_fail_reason {
+ MR_HMEM_UNKNOWN_FAIL,
+ MR_HMEM_LOCAL_PROMOTE_FAIL,
+ MR_HMEM_REMOTE_PROMOTE_FAIL,
+ MR_HMEM_MIGRATE_FAIL,
+ MR_HMEM_NR_FAIL_REASONS
+};
+
+struct migrate_detail {
+ enum migrate_reason reason;
+ enum migrate_fail_reason fail_reason;
+ enum migrate_hmem_reason h_reason;
+ enum migrate_hmem_reason h_reason_orig;
+ enum migrate_hmem_fail_reason h_fail_reason;
+};
+
/* In mm/debug.c; also keep sync with include/trace/events/migrate.h */
extern const char *migrate_reason_names[MR_TYPES];
@@ -66,7 +102,8 @@ extern int migrate_page(struct address_space *mapping,
struct page *newpage, struct page *page,
enum migrate_mode mode);
extern int migrate_pages(struct list_head *l, new_page_t new, free_page_t free,
- unsigned long private, enum migrate_mode mode, int reason);
+ unsigned long private, enum migrate_mode mode,
+ struct migrate_detail *m_detail);
extern int isolate_movable_page(struct page *page, isolate_mode_t mode);
extern void putback_movable_page(struct page *page);
@@ -78,6 +115,8 @@ extern int migrate_huge_page_move_mapping(struct address_space *mapping,
struct page *newpage, struct page *page);
extern int migrate_page_move_mapping(struct address_space *mapping,
struct page *newpage, struct page *page, int extra_count);
+
+extern int copy_page_multithread(struct page *to, struct page *from, int nr_pages);
#else
static inline void putback_movable_pages(struct list_head *l) {}
@@ -104,6 +143,7 @@ static inline int migrate_huge_page_move_mapping(struct address_space *mapping,
return -ENOSYS;
}
+
#endif /* CONFIG_MIGRATION */
#ifdef CONFIG_COMPACTION
@@ -125,6 +165,17 @@ static inline void __ClearPageMovable(struct page *page)
extern bool pmd_trans_migrating(pmd_t pmd);
extern int migrate_misplaced_page(struct page *page,
struct vm_area_struct *vma, int node);
+extern int migrate_demote_mapping(struct page *page);
+extern int migrate_promote_mapping(struct page *page);
+
+extern int wakeup_kdemoted(int dst_cpu, struct page *fault_page);
+extern int try_demote_from_busy_node(struct page *fault_page, int busy_nid,
+ unsigned int mode);
+extern int try_demote_page_cache(struct pglist_data *pgdat, struct lruvec *lruvec);
+extern bool migrate_balanced_pgdat(struct pglist_data *pgdat, unsigned int order);
+extern void numamigrate_fail_reason(struct migrate_detail *m_detail,
+ enum migrate_hmem_reason h_reason);
+extern void numamigrate_reason(struct migrate_detail *m_detail, int src_nid, int dst_nid);
#else
static inline bool pmd_trans_migrating(pmd_t pmd)
{
@@ -135,6 +186,41 @@ static inline int migrate_misplaced_page(struct page *page,
{
return -EAGAIN; /* can't migrate now */
}
+static inline int migrate_demote_mapping(struct page *page)
+{
+ return -ENOSYS;
+}
+static inline int migrate_promote_mapping(struct page *page)
+{
+ return -ENOSYS;
+}
+static inline int wakeup_kdemoted(int dst_cpu, struct page *fault_page)
+{
+ return false;
+}
+static inline int try_demote_from_busy_node(struct page *fault_page, int busy_nid,
+ unsigned int mode)
+{
+ return false;
+}
+static inline int try_demote_page_cache(struct pglist_data *pgdat,
+ struct lruvec *lruvec)
+{
+ return false;
+}
+static inline bool migrate_balanced_pgdat(struct pglist_data *pgdat,
+ unsigned int order)
+{
+ return false;
+}
+static inline void numamigrate_fail_reason(struct migrate_detail *m_detail,
+ enum migrate_hmem_reason h_reason)
+{
+}
+static inline void numamigrate_reason(struct migrate_detail *m_detail,
+ int src_nid, int dst_nid)
+{
+}
#endif /* CONFIG_NUMA_BALANCING */
#if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
@@ -155,6 +241,13 @@ static inline int migrate_misplaced_transhuge_page(struct mm_struct *mm,
#endif /* CONFIG_NUMA_BALANCING && CONFIG_TRANSPARENT_HUGEPAGE*/
+#ifdef CONFIG_BLOCK
+bool buffer_migrate_lock_buffers(struct buffer_head *head,
+ enum migrate_mode mode);
+#endif
+
+int writeout(struct address_space *mapping, struct page *page);
+
#ifdef CONFIG_MIGRATION
/*
diff --git a/include/linux/migrate_mode.h b/include/linux/migrate_mode.h
index 883c99249..0b78c3461 100644
--- a/include/linux/migrate_mode.h
+++ b/include/linux/migrate_mode.h
@@ -16,7 +16,12 @@ enum migrate_mode {
MIGRATE_ASYNC,