/
exchange.c
1955 lines (1647 loc) · 54.5 KB
/
exchange.c
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
/*
* Exchange two in-use pages. Page flags and page->mapping are exchanged
* as well. Only anonymous pages are supported.
*
* Copyright (C) 2016 NVIDIA, Zi Yan <ziy@nvidia.com>
*
* This work is licensed under the terms of the GNU GPL, version 2.
*/
#include <linux/syscalls.h>
#include <linux/migrate.h>
#include <linux/exchange.h>
#include <linux/security.h>
#include <linux/cpuset.h>
#include <linux/hugetlb.h>
#include <linux/mm_inline.h>
#include <linux/page_idle.h>
#include <linux/page-flags.h>
#include <linux/ksm.h>
#include <linux/memcontrol.h>
#include <linux/balloon_compaction.h>
#include <linux/buffer_head.h>
#include <linux/fs.h> /* buffer_migrate_page */
#include <linux/backing-dev.h>
#include <linux/sched/mm.h>
#include "internal.h"
/*
* Move a list of individual pages
*/
struct pages_to_node {
unsigned long from_addr;
int from_status;
unsigned long to_addr;
int to_status;
};
struct page_flags {
unsigned int page_error :1;
unsigned int page_referenced:1;
unsigned int page_uptodate:1;
unsigned int page_active:1;
unsigned int page_unevictable:1;
unsigned int page_checked:1;
unsigned int page_mappedtodisk:1;
unsigned int page_dirty:1;
unsigned int page_is_young:1;
unsigned int page_is_idle:1;
unsigned int page_swapcache:1;
unsigned int page_writeback:1;
unsigned int page_private:1;
unsigned int page_doublemap:1;
unsigned int __pad:2;
};
static void pr_dump_page(struct page *page, const char *msg)
{
pr_debug("dump: %s page(%p): 0x%lx,"
"count: %d, mapcount: %d, mapping: %p, index: %#lx, flags: %#lx(%pGp), %s, order: %d"
", %s"
"\n",
msg,
page,
page_to_pfn(page),
page_ref_count(page),
PageSlab(page)?0:page_mapcount(page),
page->mapping, page_to_pgoff(page),
page->flags, &page->flags,
PageCompound(page)?"compound_page":"single_page",
compound_order(page),
PageDirty(page)?"dirty":"clean"
);
}
static void exchange_page(char *to, char *from)
{
u64 tmp;
int i;
for (i = 0; i < PAGE_SIZE; i += sizeof(tmp)) {
tmp = *((u64*)(from + i));
*((u64*)(from + i)) = *((u64*)(to + i));
*((u64*)(to + i)) = tmp;
}
}
static inline void exchange_highpage(struct page *to, struct page *from)
{
char *vfrom, *vto;
vfrom = kmap_atomic(from);
vto = kmap_atomic(to);
exchange_page(vto, vfrom);
kunmap_atomic(vto);
kunmap_atomic(vfrom);
}
static void __exchange_gigantic_page(struct page *dst, struct page *src,
int nr_pages)
{
int i;
struct page *dst_base = dst;
struct page *src_base = src;
for (i = 0; i < nr_pages; ) {
cond_resched();
exchange_highpage(dst, src);
i++;
dst = mem_map_next(dst, dst_base, i);
src = mem_map_next(src, src_base, i);
}
}
static void exchange_huge_page(struct page *dst, struct page *src)
{
int i;
int nr_pages;
if (PageHuge(src)) {
/* hugetlbfs page */
struct hstate *h = page_hstate(src);
nr_pages = pages_per_huge_page(h);
if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) {
__exchange_gigantic_page(dst, src, nr_pages);
return;
}
} else {
/* thp page */
BUG_ON(!PageTransHuge(src));
nr_pages = hpage_nr_pages(src);
}
for (i = 0; i < nr_pages; i++) {
exchange_highpage(dst + i, src + i);
}
}
/*
* Copy the page to its new location without polluting cache
*/
static void exchange_page_flags(struct page *to_page, struct page *from_page)
{
int from_cpupid, to_cpupid;
struct page_flags from_page_flags = {0}, to_page_flags = {0};
struct mem_cgroup *to_memcg = page_memcg(to_page),
*from_memcg = page_memcg(from_page);
from_cpupid = page_cpupid_xchg_last(from_page, -1);
from_page_flags.page_error = PageError(from_page);
if (from_page_flags.page_error)
ClearPageError(from_page);
from_page_flags.page_referenced = TestClearPageReferenced(from_page);
from_page_flags.page_uptodate = PageUptodate(from_page);
ClearPageUptodate(from_page);
from_page_flags.page_active = TestClearPageActive(from_page);
from_page_flags.page_unevictable = TestClearPageUnevictable(from_page);
from_page_flags.page_checked = PageChecked(from_page);
if (from_page_flags.page_checked)
ClearPageChecked(from_page);
from_page_flags.page_mappedtodisk = PageMappedToDisk(from_page);
ClearPageMappedToDisk(from_page);
from_page_flags.page_dirty = PageDirty(from_page);
ClearPageDirty(from_page);
from_page_flags.page_is_young = test_and_clear_page_young(from_page);
from_page_flags.page_is_idle = page_is_idle(from_page);
clear_page_idle(from_page);
from_page_flags.page_swapcache = PageSwapCache(from_page);
/*from_page_flags.page_private = PagePrivate(from_page);*/
/*ClearPagePrivate(from_page);*/
from_page_flags.page_writeback = test_clear_page_writeback(from_page);
from_page_flags.page_doublemap = PageDoubleMap(from_page);
to_cpupid = page_cpupid_xchg_last(to_page, -1);
to_page_flags.page_error = PageError(to_page);
if (to_page_flags.page_error)
ClearPageError(to_page);
to_page_flags.page_referenced = TestClearPageReferenced(to_page);
to_page_flags.page_uptodate = PageUptodate(to_page);
ClearPageUptodate(to_page);
to_page_flags.page_active = TestClearPageActive(to_page);
to_page_flags.page_unevictable = TestClearPageUnevictable(to_page);
to_page_flags.page_checked = PageChecked(to_page);
if (to_page_flags.page_checked)
ClearPageChecked(to_page);
to_page_flags.page_mappedtodisk = PageMappedToDisk(to_page);
ClearPageMappedToDisk(to_page);
to_page_flags.page_dirty = PageDirty(to_page);
ClearPageDirty(to_page);
to_page_flags.page_is_young = test_and_clear_page_young(to_page);
to_page_flags.page_is_idle = page_is_idle(to_page);
clear_page_idle(to_page);
to_page_flags.page_swapcache = PageSwapCache(to_page);
/*to_page_flags.page_private = PagePrivate(to_page);*/
/*ClearPagePrivate(to_page);*/
to_page_flags.page_writeback = test_clear_page_writeback(to_page);
to_page_flags.page_doublemap = PageDoubleMap(to_page);
/* set to_page */
if (from_page_flags.page_error)
SetPageError(to_page);
if (from_page_flags.page_referenced)
SetPageReferenced(to_page);
if (from_page_flags.page_uptodate)
SetPageUptodate(to_page);
if (from_page_flags.page_active) {
VM_BUG_ON_PAGE(from_page_flags.page_unevictable, from_page);
SetPageActive(to_page);
} else if (from_page_flags.page_unevictable)
SetPageUnevictable(to_page);
if (from_page_flags.page_checked)
SetPageChecked(to_page);
if (from_page_flags.page_mappedtodisk)
SetPageMappedToDisk(to_page);
/* Move dirty on pages not done by migrate_page_move_mapping() */
if (from_page_flags.page_dirty)
SetPageDirty(to_page);
if (from_page_flags.page_is_young)
set_page_young(to_page);
if (from_page_flags.page_is_idle)
set_page_idle(to_page);
if (from_page_flags.page_doublemap)
SetPageDoubleMap(to_page);
/* set from_page */
if (to_page_flags.page_error)
SetPageError(from_page);
if (to_page_flags.page_referenced)
SetPageReferenced(from_page);
if (to_page_flags.page_uptodate)
SetPageUptodate(from_page);
if (to_page_flags.page_active) {
VM_BUG_ON_PAGE(to_page_flags.page_unevictable, from_page);
SetPageActive(from_page);
} else if (to_page_flags.page_unevictable)
SetPageUnevictable(from_page);
if (to_page_flags.page_checked)
SetPageChecked(from_page);
if (to_page_flags.page_mappedtodisk)
SetPageMappedToDisk(from_page);
/* Move dirty on pages not done by migrate_page_move_mapping() */
if (to_page_flags.page_dirty)
SetPageDirty(from_page);
if (to_page_flags.page_is_young)
set_page_young(from_page);
if (to_page_flags.page_is_idle)
set_page_idle(from_page);
if (to_page_flags.page_doublemap)
SetPageDoubleMap(from_page);
/*
* Copy NUMA information to the new page, to prevent over-eager
* future migrations of this same page.
*/
page_cpupid_xchg_last(to_page, from_cpupid);
page_cpupid_xchg_last(from_page, to_cpupid);
ksm_exchange_page(to_page, from_page);
/*
* Please do not reorder this without considering how mm/ksm.c's
* get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
*/
ClearPageSwapCache(to_page);
ClearPageSwapCache(from_page);
if (from_page_flags.page_swapcache)
SetPageSwapCache(to_page);
if (to_page_flags.page_swapcache)
SetPageSwapCache(from_page);
#ifdef CONFIG_PAGE_OWNER
/* exchange page owner */
BUG();
#endif
/* exchange mem cgroup */
to_page->mem_cgroup = from_memcg;
from_page->mem_cgroup = to_memcg;
}
/*
* Replace the page in the mapping.
*
* The number of remaining references must be:
* 1 for anonymous pages without a mapping
* 2 for pages with a mapping
* 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
*/
static int exchange_page_move_mapping(struct address_space *to_mapping,
struct address_space *from_mapping,
struct page *to_page, struct page *from_page,
struct buffer_head *to_head, struct buffer_head *from_head,
enum migrate_mode mode,
int to_extra_count, int from_extra_count)
{
int to_expected_count = 1 + to_extra_count,
from_expected_count = 1 + from_extra_count;
unsigned long from_page_index = from_page->index;
unsigned long to_page_index = to_page->index;
int to_swapbacked = PageSwapBacked(to_page),
from_swapbacked = PageSwapBacked(from_page);
struct address_space *to_mapping_value = to_page->mapping,
*from_mapping_value = from_page->mapping;
VM_BUG_ON_PAGE(to_mapping != page_mapping(to_page), to_page);
VM_BUG_ON_PAGE(from_mapping != page_mapping(from_page), from_page);
VM_BUG_ON(PageCompound(from_page) != PageCompound(to_page));
if (!to_mapping) {
/* Anonymous page without mapping */
if (page_count(to_page) != to_expected_count)
return -EAGAIN;
}
if (!from_mapping) {
/* Anonymous page without mapping */
if (page_count(from_page) != from_expected_count)
return -EAGAIN;
}
/* both are anonymous pages */
if (!from_mapping && !to_mapping) {
/* from_page */
from_page->index = to_page_index;
from_page->mapping = to_mapping_value;
ClearPageSwapBacked(from_page);
if (to_swapbacked)
SetPageSwapBacked(from_page);
/* to_page */
to_page->index = from_page_index;
to_page->mapping = from_mapping_value;
ClearPageSwapBacked(to_page);
if (from_swapbacked)
SetPageSwapBacked(to_page);
} else if (!from_mapping && to_mapping) { /* from is anonymous, to is file-backed */
struct zone *from_zone, *to_zone;
void **to_pslot;
int dirty;
from_zone = page_zone(from_page);
to_zone = page_zone(to_page);
spin_lock_irq(&to_mapping->tree_lock);
to_pslot = radix_tree_lookup_slot(&to_mapping->page_tree, page_index(to_page));
to_expected_count += 1 + page_has_private(to_page);
if (page_count(to_page) != to_expected_count ||
radix_tree_deref_slot_protected(to_pslot, &to_mapping->tree_lock)
!= to_page) {
spin_unlock_irq(&to_mapping->tree_lock);
return -EAGAIN;
}
if (!page_ref_freeze(to_page, to_expected_count)) {
spin_unlock_irq(&to_mapping->tree_lock);
pr_debug("cannot freeze page count\n");
return -EAGAIN;
}
if (((mode & MIGRATE_MODE_MASK) == MIGRATE_ASYNC) && to_head &&
!buffer_migrate_lock_buffers(to_head, mode)) {
page_ref_unfreeze(to_page, to_expected_count);
spin_unlock_irq(&to_mapping->tree_lock);
pr_debug("cannot lock buffer head\n");
return -EAGAIN;
}
/*
* Now we know that no one else is looking at the page:
* no turning back from here.
*/
ClearPageSwapBacked(from_page);
ClearPageSwapBacked(to_page);
/* from_page */
from_page->index = to_page_index;
from_page->mapping = to_mapping_value;
/* to_page */
to_page->index = from_page_index;
to_page->mapping = from_mapping_value;
get_page(from_page); /* add cache reference */
if (to_swapbacked)
__SetPageSwapBacked(from_page);
else
VM_BUG_ON_PAGE(PageSwapCache(to_page), to_page);
if (from_swapbacked)
__SetPageSwapBacked(to_page);
else
VM_BUG_ON_PAGE(PageSwapCache(from_page), from_page);
dirty = PageDirty(to_page);
radix_tree_replace_slot(&to_mapping->page_tree, to_pslot, from_page);
/* drop cache reference */
page_ref_unfreeze(to_page, to_expected_count - 1);
spin_unlock(&to_mapping->tree_lock);
/*
* If moved to a different zone then also account
* the page for that zone. Other VM counters will be
* taken care of when we establish references to the
* new page and drop references to the old page.
*
* Note that anonymous pages are accounted for
* via NR_FILE_PAGES and NR_ANON_MAPPED if they
* are mapped to swap space.
*/
if (to_zone != from_zone) {
__dec_node_state(to_zone->zone_pgdat, NR_FILE_PAGES);
__inc_node_state(from_zone->zone_pgdat, NR_FILE_PAGES);
if (PageSwapBacked(to_page) && !PageSwapCache(to_page)) {
__dec_node_state(to_zone->zone_pgdat, NR_SHMEM);
__inc_node_state(from_zone->zone_pgdat, NR_SHMEM);
}
if (dirty && mapping_cap_account_dirty(to_mapping)) {
__dec_node_state(to_zone->zone_pgdat, NR_FILE_DIRTY);
__dec_zone_state(to_zone, NR_ZONE_WRITE_PENDING);
__inc_node_state(from_zone->zone_pgdat, NR_FILE_DIRTY);
__inc_zone_state(from_zone, NR_ZONE_WRITE_PENDING);
}
}
local_irq_enable();
} else {
/* from is file-backed to is anonymous: fold this to the case above */
/* both are file-backed */
BUG();
}
return MIGRATEPAGE_SUCCESS;
}
static int exchange_from_to_pages(struct page *to_page, struct page *from_page,
enum migrate_mode mode)
{
int rc = -EBUSY;
struct address_space *to_page_mapping, *from_page_mapping;
struct buffer_head *to_head = NULL, *to_bh = NULL;
#ifdef CONFIG_PAGE_MIGRATION_PROFILE
u64 timestamp;
#endif
VM_BUG_ON_PAGE(!PageLocked(from_page), from_page);
VM_BUG_ON_PAGE(!PageLocked(to_page), to_page);
/* copy page->mapping not use page_mapping() */
to_page_mapping = page_mapping(to_page);
from_page_mapping = page_mapping(from_page);
/* from_page has to be anonymous page */
BUG_ON(from_page_mapping);
BUG_ON(PageWriteback(from_page));
/* writeback has to finish */
BUG_ON(PageWriteback(to_page));
pr_dump_page(from_page, "exchange anonymous page: from ");
/* to_page is anonymous */
if (!to_page_mapping) {
pr_dump_page(to_page, "exchange anonymous page: to ");
exchange_mappings:
/* actual page mapping exchange */
rc = exchange_page_move_mapping(to_page_mapping, from_page_mapping,
to_page, from_page, NULL, NULL, mode, 0, 0);
} else {
if (to_page_mapping->a_ops->migratepage == buffer_migrate_page) {
pr_dump_page(to_page, "exchange has migratepage: to ");
if (!page_has_buffers(to_page))
goto exchange_mappings;
to_head = page_buffers(to_page);
rc = exchange_page_move_mapping(to_page_mapping,
from_page_mapping, to_page, from_page,
to_head, NULL, mode, 0, 0);
if (rc != MIGRATEPAGE_SUCCESS)
return rc;
/*
* In the async case, migrate_page_move_mapping locked the buffers
* with an IRQ-safe spinlock held. In the sync case, the buffers
* need to be locked now
*/
if ((mode & MIGRATE_MODE_MASK) != MIGRATE_ASYNC)
BUG_ON(!buffer_migrate_lock_buffers(to_head, mode));
ClearPagePrivate(to_page);
set_page_private(from_page, page_private(to_page));
set_page_private(to_page, 0);
/* transfer private page count */
put_page(to_page);
get_page(from_page);
to_bh = to_head;
do {
set_bh_page(to_bh, from_page, bh_offset(to_bh));
to_bh = to_bh->b_this_page;
} while (to_bh != to_head);
SetPagePrivate(from_page);
to_bh = to_head;
} else if (!to_page_mapping->a_ops->migratepage) {
/* fallback_migrate_page */
pr_dump_page(to_page, "exchange no migratepage: to ");
if (PageDirty(to_page)) {
if ((mode & MIGRATE_MODE_MASK) != MIGRATE_SYNC)
return -EBUSY;
return writeout(to_page_mapping, to_page);
}
if (page_has_private(to_page) &&
!try_to_release_page(to_page, GFP_KERNEL))
return -EAGAIN;
goto exchange_mappings;
}
}
#ifdef CONFIG_PAGE_MIGRATION_PROFILE
timestamp = rdtsc();
current->move_pages_breakdown.change_page_mapping_cycles += timestamp -
current->move_pages_breakdown.last_timestamp;
current->move_pages_breakdown.last_timestamp = timestamp;
#endif
/* actual page data exchange */
if (rc != MIGRATEPAGE_SUCCESS)
return rc;
rc = -EFAULT;
if (mode & MIGRATE_MT)
rc = exchange_page_mthread(to_page, from_page,
hpage_nr_pages(from_page));
if (rc) {
if (PageHuge(from_page) || PageTransHuge(from_page))
exchange_huge_page(to_page, from_page);
else
exchange_highpage(to_page, from_page);
rc = 0;
}
/*
* 1. buffer_migrate_page:
* private flag should be transferred from to_page to from_page
*
* 2. anon<->anon, fallback_migrate_page:
* both have none private flags or to_page's is cleared.
* */
VM_BUG_ON(!((page_has_private(from_page) && !page_has_private(to_page)) ||
(!page_has_private(from_page) && !page_has_private(to_page))));
exchange_page_flags(to_page, from_page);
#ifdef CONFIG_PAGE_MIGRATION_PROFILE
timestamp = rdtsc();
current->move_pages_breakdown.copy_page_cycles += timestamp -
current->move_pages_breakdown.last_timestamp;
current->move_pages_breakdown.last_timestamp = timestamp;
#endif
pr_dump_page(from_page, "after exchange: from ");
pr_dump_page(to_page, "after exchange: to ");
if (to_bh) {
VM_BUG_ON(to_bh != to_head);
do {
unlock_buffer(to_bh);
put_bh(to_bh);
to_bh = to_bh->b_this_page;
} while (to_bh != to_head);
}
return rc;
}
static int unmap_and_exchange(struct page *from_page,
struct page *to_page, enum migrate_mode mode)
{
int rc = -EAGAIN;
struct anon_vma *from_anon_vma = NULL;
struct anon_vma *to_anon_vma = NULL;
/*bool is_from_lru = !__PageMovable(from_page);*/
/*bool is_to_lru = !__PageMovable(to_page);*/
int from_page_was_mapped = 0;
int to_page_was_mapped = 0;
int from_page_count = 0, to_page_count = 0;
int from_map_count = 0, to_map_count = 0;
unsigned long from_flags, to_flags;
pgoff_t from_index, to_index;
struct address_space *from_mapping, *to_mapping;
#ifdef CONFIG_PAGE_MIGRATION_PROFILE
u64 timestamp;
#endif
if (!trylock_page(from_page)) {
if ((mode & MIGRATE_MODE_MASK) == MIGRATE_ASYNC)
goto out;
lock_page(from_page);
}
if (!trylock_page(to_page)) {
if ((mode & MIGRATE_MODE_MASK) == MIGRATE_ASYNC)
goto out;
lock_page(to_page);
}
#ifdef CONFIG_PAGE_MIGRATION_PROFILE
timestamp = rdtsc();
current->move_pages_breakdown.lock_page_cycles += timestamp -
current->move_pages_breakdown.last_timestamp;
current->move_pages_breakdown.last_timestamp = timestamp;
#endif
/* from_page is supposed to be an anonymous page */
VM_BUG_ON_PAGE(PageWriteback(from_page), from_page);
if (PageWriteback(to_page)) {
/*
* Only in the case of a full synchronous migration is it
* necessary to wait for PageWriteback. In the async case,
* the retry loop is too short and in the sync-light case,
* the overhead of stalling is too much
*/
if ((mode & MIGRATE_MODE_MASK) != MIGRATE_SYNC) {
rc = -EBUSY;
goto out_unlock;
}
wait_on_page_writeback(to_page);
}
/*
* By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
* we cannot notice that anon_vma is freed while we migrates a page.
* This get_anon_vma() delays freeing anon_vma pointer until the end
* of migration. File cache pages are no problem because of page_lock()
* File Caches may use write_page() or lock_page() in migration, then,
* just care Anon page here.
*
* Only page_get_anon_vma() understands the subtleties of
* getting a hold on an anon_vma from outside one of its mms.
* But if we cannot get anon_vma, then we won't need it anyway,
* because that implies that the anon page is no longer mapped
* (and cannot be remapped so long as we hold the page lock).
*/
if (PageAnon(from_page) && !PageKsm(from_page))
from_anon_vma = page_get_anon_vma(from_page);
if (PageAnon(to_page) && !PageKsm(to_page))
to_anon_vma = page_get_anon_vma(to_page);
/*if (unlikely(!is_from_lru)) {*/
/*VM_BUG_ON_PAGE(1, from_page);*/
/*goto out_unlock_both;*/
/*}*/
/*if (unlikely(!is_to_lru)) {*/
/*pr_debug("exchange non-lru to_page\n");*/
/*goto out_unlock_both;*/
/*}*/
from_page_count = page_count(from_page);
from_map_count = page_mapcount(from_page);
to_page_count = page_count(to_page);
to_map_count = page_mapcount(to_page);
from_flags = from_page->flags;
to_flags = to_page->flags;
from_mapping = from_page->mapping;
to_mapping = to_page->mapping;
from_index = from_page->index;
to_index = to_page->index;
/*
* Corner case handling:
* 1. When a new swap-cache page is read into, it is added to the LRU
* and treated as swapcache but it has no rmap yet.
* Calling try_to_unmap() against a page->mapping==NULL page will
* trigger a BUG. So handle it here.
* 2. An orphaned page (see truncate_complete_page) might have
* fs-private metadata. The page can be picked up due to memory
* offlining. Everywhere else except page reclaim, the page is
* invisible to the vm, so the page can not be migrated. So try to
* free the metadata, so the page can be freed.
*/
if (!from_page->mapping) {
VM_BUG_ON_PAGE(PageAnon(from_page), from_page);
if (page_has_private(from_page)) {
try_to_free_buffers(from_page);
goto out_unlock_both;
}
} else if (page_mapped(from_page)) {
/* Establish migration ptes */
VM_BUG_ON_PAGE(PageAnon(from_page) && !PageKsm(from_page) &&
!from_anon_vma, from_page);
try_to_unmap(from_page,
TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
from_page_was_mapped = 1;
}
if (!to_page->mapping) {
VM_BUG_ON_PAGE(PageAnon(to_page), to_page);
if (page_has_private(to_page)) {
try_to_free_buffers(to_page);
goto out_unlock_both_remove_from_migration_pte;
}
} else if (page_mapped(to_page)) {
/* Establish migration ptes */
VM_BUG_ON_PAGE(PageAnon(to_page) && !PageKsm(to_page) &&
!to_anon_vma, to_page);
try_to_unmap(to_page,
TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
to_page_was_mapped = 1;
}
#ifdef CONFIG_PAGE_MIGRATION_PROFILE
timestamp = rdtsc();
current->move_pages_breakdown.unmap_page_cycles += timestamp -
current->move_pages_breakdown.last_timestamp;
current->move_pages_breakdown.last_timestamp = timestamp;
#endif
if (!page_mapped(from_page) && !page_mapped(to_page)) {
rc = exchange_from_to_pages(to_page, from_page, mode);
pr_debug("exchange_from_to_pages from: %lx, to %lx: %d\n", page_to_pfn(from_page), page_to_pfn(to_page), rc);
}
/* In remove_migration_ptes(), page_walk_vma() assumes
* from_page and to_page have the same index.
* Thus, we restore old_page->index here.
* Here to_page is the old_page.
*/
if (to_page_was_mapped) {
if (rc == MIGRATEPAGE_SUCCESS)
swap(to_page->index, to_index);
remove_migration_ptes(to_page,
rc == MIGRATEPAGE_SUCCESS ? from_page : to_page, false);
if (rc == MIGRATEPAGE_SUCCESS)
swap(to_page->index, to_index);
}
out_unlock_both_remove_from_migration_pte:
if (from_page_was_mapped) {
if (rc == MIGRATEPAGE_SUCCESS)
swap(from_page->index, from_index);
remove_migration_ptes(from_page,
rc == MIGRATEPAGE_SUCCESS ? to_page : from_page, false);
if (rc == MIGRATEPAGE_SUCCESS)
swap(from_page->index, from_index);
}
#ifdef CONFIG_PAGE_MIGRATION_PROFILE
timestamp = rdtsc();
current->move_pages_breakdown.remove_migration_ptes_cycles += timestamp -
current->move_pages_breakdown.last_timestamp;
current->move_pages_breakdown.last_timestamp = timestamp;
#endif
if (rc == MIGRATEPAGE_SUCCESS) {
if (from_page_count != page_count(to_page) ||
to_page_count != page_count(from_page) ||
from_map_count != page_mapcount(to_page) ||
to_map_count != page_mapcount(from_page)) {
if (page_mapping(from_page) &&
!page_mapping(from_page)->a_ops->migratepage &&
to_page_count == page_count(from_page) + 1 &&
to_map_count == page_mapcount(from_page) &&
from_page_count == page_count(to_page) &&
from_map_count == page_mapcount(to_page)) {
} else if ((PageWaiters(from_page)?
to_page_count < page_count(from_page):
to_page_count == page_count(from_page))&&
to_map_count == page_mapcount(from_page) &&
(PageWaiters(to_page)?
from_page_count < page_count(to_page):
from_page_count == page_count(to_page) )&&
from_map_count == page_mapcount(to_page)) {
} else {
pr_debug("anon<->file: from_page_was_mapped: %d, to_page_was_mapped: %d\n",
from_page_was_mapped, to_page_was_mapped);
pr_debug("before: from_page_count: %d, from_map_count: %d, from_flags: %#lx(%pGp), from_mapping: %p, "
"to_page_count: %d, to_map_count: %d, to_flags: %#lx(%pGp), to_mapping: %p\n",
from_page_count, from_map_count, from_flags, &from_flags, from_mapping,
to_page_count, to_map_count, to_flags, &to_flags, to_mapping);
pr_dump_page(from_page, "after exchange: from");
pr_dump_page(to_page, "after exchange: to");
}
}
} else {
if (from_page_count != page_count(from_page) ||
to_page_count != page_count(to_page) ||
from_map_count != page_mapcount(from_page) ||
to_map_count != page_mapcount(to_page)) {
if (page_mapping(to_page) &&
!page_mapping(to_page)->a_ops->migratepage &&
to_page_count == page_count(to_page) + 1 &&
to_map_count == page_mapcount(to_page) &&
from_page_count == page_count(from_page) &&
from_map_count == page_mapcount(from_page)) {
} else if ((PageWaiters(to_page)?
to_page_count < page_count(to_page):
to_page_count == page_count(to_page) )&&
to_map_count == page_mapcount(to_page) &&
(PageWaiters(from_page)?
from_page_count < page_count(from_page):
from_page_count == page_count(from_page) )&&
from_map_count == page_mapcount(from_page)) {
} else {
pr_debug("anon<->file: from_page_was_mapped: %d, to_page_was_mapped: %d, rc: %d\n",
from_page_was_mapped, to_page_was_mapped, rc);
pr_debug("before: from_page_count: %d, from_map_count: %d, from_flags: %#lx(%pGp), from_mapping: %p, "
"to_page_count: %d, to_map_count: %d, to_flags: %#lx(%pGp), to_mapping: %p\n",
from_page_count, from_map_count, from_flags, &from_flags, from_mapping,
to_page_count, to_map_count, to_flags, &to_flags, to_mapping);
pr_dump_page(from_page, "exchange failed: from");
pr_dump_page(to_page, "exchange failed: to");
}
}
}
out_unlock_both:
if (to_anon_vma)
put_anon_vma(to_anon_vma);
unlock_page(to_page);
out_unlock:
/* Drop an anon_vma reference if we took one */
if (from_anon_vma)
put_anon_vma(from_anon_vma);
unlock_page(from_page);
out:
return rc;
}
static bool can_be_exchanged(struct page *from, struct page *to)
{
if (PageCompound(from) != PageCompound(to))
return false;
if (PageHuge(from) != PageHuge(to))
return false;
if (PageHuge(from) || PageHuge(to))
return false;
if (compound_order(from) != compound_order(to))
return false;
return true;
}
/*
* Exchange pages in the exchange_list
*
* Caller should release the exchange_list resource.
*
* */
int exchange_pages(struct list_head *exchange_list,
enum migrate_mode mode,
int reason)
{
struct exchange_page_info *one_pair, *one_pair2;
int failed = 0;
#ifdef CONFIG_PAGE_MIGRATION_PROFILE
u64 timestamp;
timestamp = rdtsc();
current->move_pages_breakdown.enter_unmap_and_move_cycles += timestamp -
current->move_pages_breakdown.last_timestamp;
current->move_pages_breakdown.last_timestamp = timestamp;
#endif
list_for_each_entry_safe(one_pair, one_pair2, exchange_list, list) {
struct page *from_page = one_pair->from_page;
struct page *to_page = one_pair->to_page;
int rc;
int retry = 0;
again:
if (page_count(from_page) == 1) {
/* page was freed from under us. So we are done */
ClearPageActive(from_page);
ClearPageUnevictable(from_page);
put_page(from_page);
dec_node_page_state(from_page, NR_ISOLATED_ANON +
page_is_file_cache(from_page));
if (page_count(to_page) == 1) {
ClearPageActive(to_page);
ClearPageUnevictable(to_page);
put_page(to_page);
} else
goto putback_to_page;
continue;
}
if (page_count(to_page) == 1) {
/* page was freed from under us. So we are done */
ClearPageActive(to_page);
ClearPageUnevictable(to_page);
put_page(to_page);
dec_node_page_state(to_page, NR_ISOLATED_ANON +
page_is_file_cache(to_page));
dec_node_page_state(from_page, NR_ISOLATED_ANON +
page_is_file_cache(from_page));
putback_lru_page(from_page);
continue;
}
/* TODO: compound page not supported */
if (!can_be_exchanged(from_page, to_page) ||
page_mapping(from_page)
/* allow to_page to be file-backed page */
/*|| page_mapping(to_page)*/
) {
++failed;
goto putback;
}
rc = unmap_and_exchange(from_page, to_page, mode);
if (rc == -EAGAIN && retry < 3) {
++retry;
goto again;
}
if (rc != MIGRATEPAGE_SUCCESS)
++failed;
putback:
dec_node_page_state(from_page, NR_ISOLATED_ANON +
page_is_file_cache(from_page));
putback_lru_page(from_page);
#ifdef CONFIG_PAGE_MIGRATION_PROFILE
timestamp = rdtsc();
current->move_pages_breakdown.putback_old_page_cycles += timestamp -
current->move_pages_breakdown.last_timestamp;
current->move_pages_breakdown.last_timestamp = timestamp;
#endif
putback_to_page:
/*if (!__PageMovable(to_page)) {*/
dec_node_page_state(to_page, NR_ISOLATED_ANON +
page_is_file_cache(to_page));
putback_lru_page(to_page);
/*} else {*/
/*putback_movable_page(to_page);*/
/*}*/
#ifdef CONFIG_PAGE_MIGRATION_PROFILE
timestamp = rdtsc();
current->move_pages_breakdown.putback_new_page_cycles += timestamp -