1
2
3
4
5
6
7
8
9
10#include <linux/swap.h>
11#include <linux/migrate.h>
12#include <linux/compaction.h>
13#include <linux/mm_inline.h>
14#include <linux/backing-dev.h>
15#include <linux/sysctl.h>
16#include <linux/sysfs.h>
17#include <linux/balloon_compaction.h>
18#include <linux/page-isolation.h>
19#include "internal.h"
20
21#ifdef CONFIG_COMPACTION
22static inline void count_compact_event(enum vm_event_item item)
23{
24 count_vm_event(item);
25}
26
27static inline void count_compact_events(enum vm_event_item item, long delta)
28{
29 count_vm_events(item, delta);
30}
31#else
32#define count_compact_event(item) do { } while (0)
33#define count_compact_events(item, delta) do { } while (0)
34#endif
35
36#if defined CONFIG_COMPACTION || defined CONFIG_CMA
37
38#define CREATE_TRACE_POINTS
39#include <trace/events/compaction.h>
40
41static unsigned long release_freepages(struct list_head *freelist)
42{
43 struct page *page, *next;
44 unsigned long count = 0;
45
46 list_for_each_entry_safe(page, next, freelist, lru) {
47 list_del(&page->lru);
48 __free_page(page);
49 count++;
50 }
51
52 return count;
53}
54
55static void map_pages(struct list_head *list)
56{
57 struct page *page;
58
59 list_for_each_entry(page, list, lru) {
60 arch_alloc_page(page, 0);
61 kernel_map_pages(page, 1, 1);
62 }
63}
64
65static inline bool migrate_async_suitable(int migratetype)
66{
67 return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
68}
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87static struct page *pageblock_pfn_to_page(unsigned long start_pfn,
88 unsigned long end_pfn, struct zone *zone)
89{
90 struct page *start_page;
91 struct page *end_page;
92
93
94 end_pfn--;
95
96 if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
97 return NULL;
98
99 start_page = pfn_to_page(start_pfn);
100
101 if (page_zone(start_page) != zone)
102 return NULL;
103
104 end_page = pfn_to_page(end_pfn);
105
106
107 if (page_zone_id(start_page) != page_zone_id(end_page))
108 return NULL;
109
110 return start_page;
111}
112
113#ifdef CONFIG_COMPACTION
114
115static inline bool isolation_suitable(struct compact_control *cc,
116 struct page *page)
117{
118 if (cc->ignore_skip_hint)
119 return true;
120
121 return !get_pageblock_skip(page);
122}
123
124
125
126
127
128
129static void __reset_isolation_suitable(struct zone *zone)
130{
131 unsigned long start_pfn = zone->zone_start_pfn;
132 unsigned long end_pfn = zone_end_pfn(zone);
133 unsigned long pfn;
134
135 zone->compact_cached_migrate_pfn[0] = start_pfn;
136 zone->compact_cached_migrate_pfn[1] = start_pfn;
137 zone->compact_cached_free_pfn = end_pfn;
138 zone->compact_blockskip_flush = false;
139
140
141 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
142 struct page *page;
143
144 cond_resched();
145
146 if (!pfn_valid(pfn))
147 continue;
148
149 page = pfn_to_page(pfn);
150 if (zone != page_zone(page))
151 continue;
152
153 clear_pageblock_skip(page);
154 }
155}
156
157void reset_isolation_suitable(pg_data_t *pgdat)
158{
159 int zoneid;
160
161 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
162 struct zone *zone = &pgdat->node_zones[zoneid];
163 if (!populated_zone(zone))
164 continue;
165
166
167 if (zone->compact_blockskip_flush)
168 __reset_isolation_suitable(zone);
169 }
170}
171
172
173
174
175
176static void update_pageblock_skip(struct compact_control *cc,
177 struct page *page, unsigned long nr_isolated,
178 bool migrate_scanner)
179{
180 struct zone *zone = cc->zone;
181 unsigned long pfn;
182
183 if (cc->ignore_skip_hint)
184 return;
185
186 if (!page)
187 return;
188
189 if (nr_isolated)
190 return;
191
192 set_pageblock_skip(page);
193
194 pfn = page_to_pfn(page);
195
196
197 if (migrate_scanner) {
198 if (cc->finished_update_migrate)
199 return;
200 if (pfn > zone->compact_cached_migrate_pfn[0])
201 zone->compact_cached_migrate_pfn[0] = pfn;
202 if (cc->mode != MIGRATE_ASYNC &&
203 pfn > zone->compact_cached_migrate_pfn[1])
204 zone->compact_cached_migrate_pfn[1] = pfn;
205 } else {
206 if (cc->finished_update_free)
207 return;
208 if (pfn < zone->compact_cached_free_pfn)
209 zone->compact_cached_free_pfn = pfn;
210 }
211}
212#else
213static inline bool isolation_suitable(struct compact_control *cc,
214 struct page *page)
215{
216 return true;
217}
218
219static void update_pageblock_skip(struct compact_control *cc,
220 struct page *page, unsigned long nr_isolated,
221 bool migrate_scanner)
222{
223}
224#endif
225
226
227
228
229
230
231
232
233
234static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
235 struct compact_control *cc)
236{
237 if (cc->mode == MIGRATE_ASYNC) {
238 if (!spin_trylock_irqsave(lock, *flags)) {
239 cc->contended = COMPACT_CONTENDED_LOCK;
240 return false;
241 }
242 } else {
243 spin_lock_irqsave(lock, *flags);
244 }
245
246 return true;
247}
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264static bool compact_unlock_should_abort(spinlock_t *lock,
265 unsigned long flags, bool *locked, struct compact_control *cc)
266{
267 if (*locked) {
268 spin_unlock_irqrestore(lock, flags);
269 *locked = false;
270 }
271
272 if (fatal_signal_pending(current)) {
273 cc->contended = COMPACT_CONTENDED_SCHED;
274 return true;
275 }
276
277 if (need_resched()) {
278 if (cc->mode == MIGRATE_ASYNC) {
279 cc->contended = COMPACT_CONTENDED_SCHED;
280 return true;
281 }
282 cond_resched();
283 }
284
285 return false;
286}
287
288
289
290
291
292
293
294
295
296
297static inline bool compact_should_abort(struct compact_control *cc)
298{
299
300 if (need_resched()) {
301 if (cc->mode == MIGRATE_ASYNC) {
302 cc->contended = COMPACT_CONTENDED_SCHED;
303 return true;
304 }
305
306 cond_resched();
307 }
308
309 return false;
310}
311
312
313static bool suitable_migration_target(struct page *page)
314{
315
316 if (PageBuddy(page)) {
317
318
319
320
321
322 if (page_order_unsafe(page) >= pageblock_order)
323 return false;
324 }
325
326
327 if (migrate_async_suitable(get_pageblock_migratetype(page)))
328 return true;
329
330
331 return false;
332}
333
334
335
336
337
338
339static unsigned long isolate_freepages_block(struct compact_control *cc,
340 unsigned long *start_pfn,
341 unsigned long end_pfn,
342 struct list_head *freelist,
343 bool strict)
344{
345 int nr_scanned = 0, total_isolated = 0;
346 struct page *cursor, *valid_page = NULL;
347 unsigned long flags = 0;
348 bool locked = false;
349 unsigned long blockpfn = *start_pfn;
350
351 cursor = pfn_to_page(blockpfn);
352
353
354 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
355 int isolated, i;
356 struct page *page = cursor;
357
358
359
360
361
362
363 if (!(blockpfn % SWAP_CLUSTER_MAX)
364 && compact_unlock_should_abort(&cc->zone->lock, flags,
365 &locked, cc))
366 break;
367
368 nr_scanned++;
369 if (!pfn_valid_within(blockpfn))
370 goto isolate_fail;
371
372 if (!valid_page)
373 valid_page = page;
374 if (!PageBuddy(page))
375 goto isolate_fail;
376
377
378
379
380
381
382
383
384 if (!locked) {
385
386
387
388
389
390
391
392
393 locked = compact_trylock_irqsave(&cc->zone->lock,
394 &flags, cc);
395 if (!locked)
396 break;
397
398
399 if (!PageBuddy(page))
400 goto isolate_fail;
401 }
402
403
404 isolated = split_free_page(page);
405 total_isolated += isolated;
406 for (i = 0; i < isolated; i++) {
407 list_add(&page->lru, freelist);
408 page++;
409 }
410
411
412 if (isolated) {
413 blockpfn += isolated - 1;
414 cursor += isolated - 1;
415 continue;
416 }
417
418isolate_fail:
419 if (strict)
420 break;
421 else
422 continue;
423
424 }
425
426
427 *start_pfn = blockpfn;
428
429 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
430
431
432
433
434
435
436 if (strict && blockpfn < end_pfn)
437 total_isolated = 0;
438
439 if (locked)
440 spin_unlock_irqrestore(&cc->zone->lock, flags);
441
442
443 if (blockpfn == end_pfn)
444 update_pageblock_skip(cc, valid_page, total_isolated, false);
445
446 count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
447 if (total_isolated)
448 count_compact_events(COMPACTISOLATED, total_isolated);
449 return total_isolated;
450}
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465unsigned long
466isolate_freepages_range(struct compact_control *cc,
467 unsigned long start_pfn, unsigned long end_pfn)
468{
469 unsigned long isolated, pfn, block_end_pfn;
470 LIST_HEAD(freelist);
471
472 pfn = start_pfn;
473 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
474
475 for (; pfn < end_pfn; pfn += isolated,
476 block_end_pfn += pageblock_nr_pages) {
477
478 unsigned long isolate_start_pfn = pfn;
479
480 block_end_pfn = min(block_end_pfn, end_pfn);
481
482
483
484
485
486
487 if (pfn >= block_end_pfn) {
488 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
489 block_end_pfn = min(block_end_pfn, end_pfn);
490 }
491
492 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
493 break;
494
495 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
496 block_end_pfn, &freelist, true);
497
498
499
500
501
502
503 if (!isolated)
504 break;
505
506
507
508
509
510
511 }
512
513
514 map_pages(&freelist);
515
516 if (pfn < end_pfn) {
517
518 release_freepages(&freelist);
519 return 0;
520 }
521
522
523 return pfn;
524}
525
526
527static void acct_isolated(struct zone *zone, struct compact_control *cc)
528{
529 struct page *page;
530 unsigned int count[2] = { 0, };
531
532 if (list_empty(&cc->migratepages))
533 return;
534
535 list_for_each_entry(page, &cc->migratepages, lru)
536 count[!!page_is_file_cache(page)]++;
537
538 mod_zone_page_state(zone, NR_ISOLATED_ANON, count[0]);
539 mod_zone_page_state(zone, NR_ISOLATED_FILE, count[1]);
540}
541
542
543static bool too_many_isolated(struct zone *zone)
544{
545 unsigned long active, inactive, isolated;
546
547 inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
548 zone_page_state(zone, NR_INACTIVE_ANON);
549 active = zone_page_state(zone, NR_ACTIVE_FILE) +
550 zone_page_state(zone, NR_ACTIVE_ANON);
551 isolated = zone_page_state(zone, NR_ISOLATED_FILE) +
552 zone_page_state(zone, NR_ISOLATED_ANON);
553
554 return isolated > (inactive + active) / 2;
555}
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575static unsigned long
576isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
577 unsigned long end_pfn, isolate_mode_t isolate_mode)
578{
579 struct zone *zone = cc->zone;
580 unsigned long nr_scanned = 0, nr_isolated = 0;
581 struct list_head *migratelist = &cc->migratepages;
582 struct lruvec *lruvec;
583 unsigned long flags = 0;
584 bool locked = false;
585 struct page *page = NULL, *valid_page = NULL;
586
587
588
589
590
591
592 while (unlikely(too_many_isolated(zone))) {
593
594 if (cc->mode == MIGRATE_ASYNC)
595 return 0;
596
597 congestion_wait(BLK_RW_ASYNC, HZ/10);
598
599 if (fatal_signal_pending(current))
600 return 0;
601 }
602
603 if (compact_should_abort(cc))
604 return 0;
605
606
607 for (; low_pfn < end_pfn; low_pfn++) {
608
609
610
611
612
613 if (!(low_pfn % SWAP_CLUSTER_MAX)
614 && compact_unlock_should_abort(&zone->lru_lock, flags,
615 &locked, cc))
616 break;
617
618 if (!pfn_valid_within(low_pfn))
619 continue;
620 nr_scanned++;
621
622 page = pfn_to_page(low_pfn);
623
624 if (!valid_page)
625 valid_page = page;
626
627
628
629
630
631
632
633 if (PageBuddy(page)) {
634 unsigned long freepage_order = page_order_unsafe(page);
635
636
637
638
639
640
641 if (freepage_order > 0 && freepage_order < MAX_ORDER)
642 low_pfn += (1UL << freepage_order) - 1;
643 continue;
644 }
645
646
647
648
649
650
651 if (!PageLRU(page)) {
652 if (unlikely(balloon_page_movable(page))) {
653 if (balloon_page_isolate(page)) {
654
655 goto isolate_success;
656 }
657 }
658 continue;
659 }
660
661
662
663
664
665
666
667
668
669
670
671 if (PageTransHuge(page)) {
672 if (!locked)
673 low_pfn = ALIGN(low_pfn + 1,
674 pageblock_nr_pages) - 1;
675 else
676 low_pfn += (1 << compound_order(page)) - 1;
677
678 continue;
679 }
680
681
682
683
684
685
686 if (!page_mapping(page) &&
687 page_count(page) > page_mapcount(page))
688 continue;
689
690
691 if (!locked) {
692 locked = compact_trylock_irqsave(&zone->lru_lock,
693 &flags, cc);
694 if (!locked)
695 break;
696
697
698 if (!PageLRU(page))
699 continue;
700 if (PageTransHuge(page)) {
701 low_pfn += (1 << compound_order(page)) - 1;
702 continue;
703 }
704 }
705
706 lruvec = mem_cgroup_page_lruvec(page, zone);
707
708
709 if (__isolate_lru_page(page, isolate_mode) != 0)
710 continue;
711
712 VM_BUG_ON_PAGE(PageTransCompound(page), page);
713
714
715 del_page_from_lru_list(page, lruvec, page_lru(page));
716
717isolate_success:
718 cc->finished_update_migrate = true;
719 list_add(&page->lru, migratelist);
720 cc->nr_migratepages++;
721 nr_isolated++;
722
723
724 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
725 ++low_pfn;
726 break;
727 }
728 }
729
730
731
732
733
734 if (unlikely(low_pfn > end_pfn))
735 low_pfn = end_pfn;
736
737 if (locked)
738 spin_unlock_irqrestore(&zone->lru_lock, flags);
739
740
741
742
743
744 if (low_pfn == end_pfn)
745 update_pageblock_skip(cc, valid_page, nr_isolated, true);
746
747 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
748
749 count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
750 if (nr_isolated)
751 count_compact_events(COMPACTISOLATED, nr_isolated);
752
753 return low_pfn;
754}
755
756
757
758
759
760
761
762
763
764
765
766unsigned long
767isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
768 unsigned long end_pfn)
769{
770 unsigned long pfn, block_end_pfn;
771
772
773 pfn = start_pfn;
774 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
775
776 for (; pfn < end_pfn; pfn = block_end_pfn,
777 block_end_pfn += pageblock_nr_pages) {
778
779 block_end_pfn = min(block_end_pfn, end_pfn);
780
781 if (!pageblock_pfn_to_page(pfn, block_end_pfn, cc->zone))
782 continue;
783
784 pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
785 ISOLATE_UNEVICTABLE);
786
787
788
789
790
791
792 if (!pfn) {
793 putback_movable_pages(&cc->migratepages);
794 cc->nr_migratepages = 0;
795 break;
796 }
797
798 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
799 break;
800 }
801 acct_isolated(cc->zone, cc);
802
803 return pfn;
804}
805
806#endif
807#ifdef CONFIG_COMPACTION
808
809
810
811
812static void isolate_freepages(struct compact_control *cc)
813{
814 struct zone *zone = cc->zone;
815 struct page *page;
816 unsigned long block_start_pfn;
817 unsigned long isolate_start_pfn;
818 unsigned long block_end_pfn;
819 unsigned long low_pfn;
820 int nr_freepages = cc->nr_freepages;
821 struct list_head *freelist = &cc->freepages;
822
823
824
825
826
827
828
829
830
831
832
833
834 isolate_start_pfn = cc->free_pfn;
835 block_start_pfn = cc->free_pfn & ~(pageblock_nr_pages-1);
836 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
837 zone_end_pfn(zone));
838 low_pfn = ALIGN(cc->migrate_pfn + 1, pageblock_nr_pages);
839
840
841
842
843
844
845 for (; block_start_pfn >= low_pfn && cc->nr_migratepages > nr_freepages;
846 block_end_pfn = block_start_pfn,
847 block_start_pfn -= pageblock_nr_pages,
848 isolate_start_pfn = block_start_pfn) {
849 unsigned long isolated;
850
851
852
853
854
855
856 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
857 && compact_should_abort(cc))
858 break;
859
860 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
861 zone);
862 if (!page)
863 continue;
864
865
866 if (!suitable_migration_target(page))
867 continue;
868
869
870 if (!isolation_suitable(cc, page))
871 continue;
872
873
874 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
875 block_end_pfn, freelist, false);
876 nr_freepages += isolated;
877
878
879
880
881
882
883
884
885
886
887 cc->free_pfn = (isolate_start_pfn < block_end_pfn) ?
888 isolate_start_pfn :
889 block_start_pfn - pageblock_nr_pages;
890
891
892
893
894
895
896
897 if (isolated)
898 cc->finished_update_free = true;
899
900
901
902
903
904 if (cc->contended)
905 break;
906 }
907
908
909 map_pages(freelist);
910
911
912
913
914
915 if (block_start_pfn < low_pfn)
916 cc->free_pfn = cc->migrate_pfn;
917
918 cc->nr_freepages = nr_freepages;
919}
920
921
922
923
924
925static struct page *compaction_alloc(struct page *migratepage,
926 unsigned long data,
927 int **result)
928{
929 struct compact_control *cc = (struct compact_control *)data;
930 struct page *freepage;
931
932
933
934
935
936 if (list_empty(&cc->freepages)) {
937 if (!cc->contended)
938 isolate_freepages(cc);
939
940 if (list_empty(&cc->freepages))
941 return NULL;
942 }
943
944 freepage = list_entry(cc->freepages.next, struct page, lru);
945 list_del(&freepage->lru);
946 cc->nr_freepages--;
947
948 return freepage;
949}
950
951
952
953
954
955
956static void compaction_free(struct page *page, unsigned long data)
957{
958 struct compact_control *cc = (struct compact_control *)data;
959
960 list_add(&page->lru, &cc->freepages);
961 cc->nr_freepages++;
962}
963
964
965typedef enum {
966 ISOLATE_ABORT,
967 ISOLATE_NONE,
968 ISOLATE_SUCCESS,
969} isolate_migrate_t;
970
971
972
973
974
975
976static isolate_migrate_t isolate_migratepages(struct zone *zone,
977 struct compact_control *cc)
978{
979 unsigned long low_pfn, end_pfn;
980 struct page *page;
981 const isolate_mode_t isolate_mode =
982 (cc->mode == MIGRATE_ASYNC ? ISOLATE_ASYNC_MIGRATE : 0);
983
984
985
986
987
988 low_pfn = cc->migrate_pfn;
989
990
991 end_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages);
992
993
994
995
996
997 for (; end_pfn <= cc->free_pfn;
998 low_pfn = end_pfn, end_pfn += pageblock_nr_pages) {
999
1000
1001
1002
1003
1004
1005 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1006 && compact_should_abort(cc))
1007 break;
1008
1009 page = pageblock_pfn_to_page(low_pfn, end_pfn, zone);
1010 if (!page)
1011 continue;
1012
1013
1014 if (!isolation_suitable(cc, page))
1015 continue;
1016
1017
1018
1019
1020
1021
1022 if (cc->mode == MIGRATE_ASYNC &&
1023 !migrate_async_suitable(get_pageblock_migratetype(page)))
1024 continue;
1025
1026
1027 low_pfn = isolate_migratepages_block(cc, low_pfn, end_pfn,
1028 isolate_mode);
1029
1030 if (!low_pfn || cc->contended)
1031 return ISOLATE_ABORT;
1032
1033
1034
1035
1036
1037
1038 break;
1039 }
1040
1041 acct_isolated(zone, cc);
1042
1043
1044
1045
1046
1047 cc->migrate_pfn = (end_pfn <= cc->free_pfn) ? low_pfn : cc->free_pfn;
1048
1049 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
1050}
1051
1052static int compact_finished(struct zone *zone, struct compact_control *cc,
1053 const int migratetype)
1054{
1055 unsigned int order;
1056 unsigned long watermark;
1057
1058 if (cc->contended || fatal_signal_pending(current))
1059 return COMPACT_PARTIAL;
1060
1061
1062 if (cc->free_pfn <= cc->migrate_pfn) {
1063
1064 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
1065 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
1066 zone->compact_cached_free_pfn = zone_end_pfn(zone);
1067
1068
1069
1070
1071
1072
1073
1074 if (!current_is_kswapd())
1075 zone->compact_blockskip_flush = true;
1076
1077 return COMPACT_COMPLETE;
1078 }
1079
1080
1081
1082
1083
1084 if (cc->order == -1)
1085 return COMPACT_CONTINUE;
1086
1087
1088 watermark = low_wmark_pages(zone);
1089 watermark += (1 << cc->order);
1090
1091 if (!zone_watermark_ok(zone, cc->order, watermark, 0, 0))
1092 return COMPACT_CONTINUE;
1093
1094
1095 for (order = cc->order; order < MAX_ORDER; order++) {
1096 struct free_area *area = &zone->free_area[order];
1097
1098
1099 if (!list_empty(&area->free_list[migratetype]))
1100 return COMPACT_PARTIAL;
1101
1102
1103 if (cc->order >= pageblock_order && area->nr_free)
1104 return COMPACT_PARTIAL;
1105 }
1106
1107 return COMPACT_CONTINUE;
1108}
1109
1110
1111
1112
1113
1114
1115
1116
1117unsigned long compaction_suitable(struct zone *zone, int order)
1118{
1119 int fragindex;
1120 unsigned long watermark;
1121
1122
1123
1124
1125
1126 if (order == -1)
1127 return COMPACT_CONTINUE;
1128
1129
1130
1131
1132
1133
1134 watermark = low_wmark_pages(zone) + (2UL << order);
1135 if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1136 return COMPACT_SKIPPED;
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149 fragindex = fragmentation_index(zone, order);
1150 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
1151 return COMPACT_SKIPPED;
1152
1153 if (fragindex == -1000 && zone_watermark_ok(zone, order, watermark,
1154 0, 0))
1155 return COMPACT_PARTIAL;
1156
1157 return COMPACT_CONTINUE;
1158}
1159
1160static int compact_zone(struct zone *zone, struct compact_control *cc)
1161{
1162 int ret;
1163 unsigned long start_pfn = zone->zone_start_pfn;
1164 unsigned long end_pfn = zone_end_pfn(zone);
1165 const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
1166 const bool sync = cc->mode != MIGRATE_ASYNC;
1167
1168 ret = compaction_suitable(zone, cc->order);
1169 switch (ret) {
1170 case COMPACT_PARTIAL:
1171 case COMPACT_SKIPPED:
1172
1173 return ret;
1174 case COMPACT_CONTINUE:
1175
1176 ;
1177 }
1178
1179
1180
1181
1182
1183
1184 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
1185 __reset_isolation_suitable(zone);
1186
1187
1188
1189
1190
1191
1192 cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
1193 cc->free_pfn = zone->compact_cached_free_pfn;
1194 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
1195 cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
1196 zone->compact_cached_free_pfn = cc->free_pfn;
1197 }
1198 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
1199 cc->migrate_pfn = start_pfn;
1200 zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1201 zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
1202 }
1203
1204 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn, cc->free_pfn, end_pfn);
1205
1206 migrate_prep_local();
1207
1208 while ((ret = compact_finished(zone, cc, migratetype)) ==
1209 COMPACT_CONTINUE) {
1210 int err;
1211
1212 switch (isolate_migratepages(zone, cc)) {
1213 case ISOLATE_ABORT:
1214 ret = COMPACT_PARTIAL;
1215 putback_movable_pages(&cc->migratepages);
1216 cc->nr_migratepages = 0;
1217 goto out;
1218 case ISOLATE_NONE:
1219 continue;
1220 case ISOLATE_SUCCESS:
1221 ;
1222 }
1223
1224 err = migrate_pages(&cc->migratepages, compaction_alloc,
1225 compaction_free, (unsigned long)cc, cc->mode,
1226 MR_COMPACTION);
1227
1228 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
1229 &cc->migratepages);
1230
1231
1232 cc->nr_migratepages = 0;
1233 if (err) {
1234 putback_movable_pages(&cc->migratepages);
1235
1236
1237
1238
1239 if (err == -ENOMEM && cc->free_pfn > cc->migrate_pfn) {
1240 ret = COMPACT_PARTIAL;
1241 goto out;
1242 }
1243 }
1244 }
1245
1246out:
1247
1248 cc->nr_freepages -= release_freepages(&cc->freepages);
1249 VM_BUG_ON(cc->nr_freepages != 0);
1250
1251 trace_mm_compaction_end(ret);
1252
1253 return ret;
1254}
1255
1256static unsigned long compact_zone_order(struct zone *zone, int order,
1257 gfp_t gfp_mask, enum migrate_mode mode, int *contended)
1258{
1259 unsigned long ret;
1260 struct compact_control cc = {
1261 .nr_freepages = 0,
1262 .nr_migratepages = 0,
1263 .order = order,
1264 .gfp_mask = gfp_mask,
1265 .zone = zone,
1266 .mode = mode,
1267 };
1268 INIT_LIST_HEAD(&cc.freepages);
1269 INIT_LIST_HEAD(&cc.migratepages);
1270
1271 ret = compact_zone(zone, &cc);
1272
1273 VM_BUG_ON(!list_empty(&cc.freepages));
1274 VM_BUG_ON(!list_empty(&cc.migratepages));
1275
1276 *contended = cc.contended;
1277 return ret;
1278}
1279
1280int sysctl_extfrag_threshold = 500;
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295unsigned long try_to_compact_pages(struct zonelist *zonelist,
1296 int order, gfp_t gfp_mask, nodemask_t *nodemask,
1297 enum migrate_mode mode, int *contended,
1298 struct zone **candidate_zone)
1299{
1300 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1301 int may_enter_fs = gfp_mask & __GFP_FS;
1302 int may_perform_io = gfp_mask & __GFP_IO;
1303 struct zoneref *z;
1304 struct zone *zone;
1305 int rc = COMPACT_DEFERRED;
1306 int alloc_flags = 0;
1307 int all_zones_contended = COMPACT_CONTENDED_LOCK;
1308
1309 *contended = COMPACT_CONTENDED_NONE;
1310
1311
1312 if (!order || !may_enter_fs || !may_perform_io)
1313 return COMPACT_SKIPPED;
1314
1315#ifdef CONFIG_CMA
1316 if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
1317 alloc_flags |= ALLOC_CMA;
1318#endif
1319
1320 for_each_zone_zonelist_nodemask(zone, z, zonelist, high_zoneidx,
1321 nodemask) {
1322 int status;
1323 int zone_contended;
1324
1325 if (compaction_deferred(zone, order))
1326 continue;
1327
1328 status = compact_zone_order(zone, order, gfp_mask, mode,
1329 &zone_contended);
1330 rc = max(status, rc);
1331
1332
1333
1334
1335 all_zones_contended &= zone_contended;
1336
1337
1338 if (zone_watermark_ok(zone, order, low_wmark_pages(zone), 0,
1339 alloc_flags)) {
1340 *candidate_zone = zone;
1341
1342
1343
1344
1345
1346
1347 compaction_defer_reset(zone, order, false);
1348
1349
1350
1351
1352
1353
1354
1355
1356 if (zone_contended == COMPACT_CONTENDED_SCHED)
1357 *contended = COMPACT_CONTENDED_SCHED;
1358
1359 goto break_loop;
1360 }
1361
1362 if (mode != MIGRATE_ASYNC) {
1363
1364
1365
1366
1367
1368 defer_compaction(zone, order);
1369 }
1370
1371
1372
1373
1374
1375
1376
1377 if ((zone_contended == COMPACT_CONTENDED_SCHED)
1378 || fatal_signal_pending(current)) {
1379 *contended = COMPACT_CONTENDED_SCHED;
1380 goto break_loop;
1381 }
1382
1383 continue;
1384break_loop:
1385
1386
1387
1388
1389 all_zones_contended = 0;
1390 break;
1391 }
1392
1393
1394
1395
1396
1397 if (rc > COMPACT_SKIPPED && all_zones_contended)
1398 *contended = COMPACT_CONTENDED_LOCK;
1399
1400 return rc;
1401}
1402
1403
1404
1405static void __compact_pgdat(pg_data_t *pgdat, struct compact_control *cc)
1406{
1407 int zoneid;
1408 struct zone *zone;
1409
1410 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1411
1412 zone = &pgdat->node_zones[zoneid];
1413 if (!populated_zone(zone))
1414 continue;
1415
1416 cc->nr_freepages = 0;
1417 cc->nr_migratepages = 0;
1418 cc->zone = zone;
1419 INIT_LIST_HEAD(&cc->freepages);
1420 INIT_LIST_HEAD(&cc->migratepages);
1421
1422 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
1423 compact_zone(zone, cc);
1424
1425 if (cc->order > 0) {
1426 if (zone_watermark_ok(zone, cc->order,
1427 low_wmark_pages(zone), 0, 0))
1428 compaction_defer_reset(zone, cc->order, false);
1429 }
1430
1431 VM_BUG_ON(!list_empty(&cc->freepages));
1432 VM_BUG_ON(!list_empty(&cc->migratepages));
1433 }
1434}
1435
1436void compact_pgdat(pg_data_t *pgdat, int order)
1437{
1438 struct compact_control cc = {
1439 .order = order,
1440 .mode = MIGRATE_ASYNC,
1441 };
1442
1443 if (!order)
1444 return;
1445
1446 __compact_pgdat(pgdat, &cc);
1447}
1448
1449static void compact_node(int nid)
1450{
1451 struct compact_control cc = {
1452 .order = -1,
1453 .mode = MIGRATE_SYNC,
1454 .ignore_skip_hint = true,
1455 };
1456
1457 __compact_pgdat(NODE_DATA(nid), &cc);
1458}
1459
1460
1461static void compact_nodes(void)
1462{
1463 int nid;
1464
1465
1466 lru_add_drain_all();
1467
1468 for_each_online_node(nid)
1469 compact_node(nid);
1470}
1471
1472
1473int sysctl_compact_memory;
1474
1475
1476int sysctl_compaction_handler(struct ctl_table *table, int write,
1477 void __user *buffer, size_t *length, loff_t *ppos)
1478{
1479 if (write)
1480 compact_nodes();
1481
1482 return 0;
1483}
1484
1485int sysctl_extfrag_handler(struct ctl_table *table, int write,
1486 void __user *buffer, size_t *length, loff_t *ppos)
1487{
1488 proc_dointvec_minmax(table, write, buffer, length, ppos);
1489
1490 return 0;
1491}
1492
1493#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
1494static ssize_t sysfs_compact_node(struct device *dev,
1495 struct device_attribute *attr,
1496 const char *buf, size_t count)
1497{
1498 int nid = dev->id;
1499
1500 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
1501
1502 lru_add_drain_all();
1503
1504 compact_node(nid);
1505 }
1506
1507 return count;
1508}
1509static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
1510
1511int compaction_register_node(struct node *node)
1512{
1513 return device_create_file(&node->dev, &dev_attr_compact);
1514}
1515
1516void compaction_unregister_node(struct node *node)
1517{
1518 return device_remove_file(&node->dev, &dev_attr_compact);
1519}
1520#endif
1521
1522#endif
1523