1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/mm.h>
15#include <linux/module.h>
16#include <linux/gfp.h>
17#include <linux/kernel_stat.h>
18#include <linux/swap.h>
19#include <linux/pagemap.h>
20#include <linux/init.h>
21#include <linux/highmem.h>
22#include <linux/vmstat.h>
23#include <linux/file.h>
24#include <linux/writeback.h>
25#include <linux/blkdev.h>
26#include <linux/buffer_head.h>
27
28#include <linux/mm_inline.h>
29#include <linux/backing-dev.h>
30#include <linux/rmap.h>
31#include <linux/topology.h>
32#include <linux/cpu.h>
33#include <linux/cpuset.h>
34#include <linux/compaction.h>
35#include <linux/notifier.h>
36#include <linux/rwsem.h>
37#include <linux/delay.h>
38#include <linux/kthread.h>
39#include <linux/freezer.h>
40#include <linux/memcontrol.h>
41#include <linux/delayacct.h>
42#include <linux/sysctl.h>
43#include <linux/oom.h>
44#include <linux/prefetch.h>
45
46#include <asm/tlbflush.h>
47#include <asm/div64.h>
48
49#include <linux/swapops.h>
50
51#include "internal.h"
52
53#define CREATE_TRACE_POINTS
54#include <trace/events/vmscan.h>
55
56struct scan_control {
57
58 unsigned long nr_scanned;
59
60
61 unsigned long nr_reclaimed;
62
63
64 unsigned long nr_to_reclaim;
65
66 unsigned long hibernation_mode;
67
68
69 gfp_t gfp_mask;
70
71 int may_writepage;
72
73
74 int may_unmap;
75
76
77 int may_swap;
78
79 int order;
80
81
82 int priority;
83
84
85
86
87
88 struct mem_cgroup *target_mem_cgroup;
89
90
91
92
93
94 nodemask_t *nodemask;
95};
96
97#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
98
99#ifdef ARCH_HAS_PREFETCH
100#define prefetch_prev_lru_page(_page, _base, _field) \
101 do { \
102 if ((_page)->lru.prev != _base) { \
103 struct page *prev; \
104 \
105 prev = lru_to_page(&(_page->lru)); \
106 prefetch(&prev->_field); \
107 } \
108 } while (0)
109#else
110#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
111#endif
112
113#ifdef ARCH_HAS_PREFETCHW
114#define prefetchw_prev_lru_page(_page, _base, _field) \
115 do { \
116 if ((_page)->lru.prev != _base) { \
117 struct page *prev; \
118 \
119 prev = lru_to_page(&(_page->lru)); \
120 prefetchw(&prev->_field); \
121 } \
122 } while (0)
123#else
124#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
125#endif
126
127
128
129
130int vm_swappiness = 60;
131long vm_total_pages;
132
133static LIST_HEAD(shrinker_list);
134static DECLARE_RWSEM(shrinker_rwsem);
135
136#ifdef CONFIG_MEMCG
137static bool global_reclaim(struct scan_control *sc)
138{
139 return !sc->target_mem_cgroup;
140}
141#else
142static bool global_reclaim(struct scan_control *sc)
143{
144 return true;
145}
146#endif
147
148static unsigned long get_lru_size(struct lruvec *lruvec, enum lru_list lru)
149{
150 if (!mem_cgroup_disabled())
151 return mem_cgroup_get_lru_size(lruvec, lru);
152
153 return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru);
154}
155
156
157
158
159void register_shrinker(struct shrinker *shrinker)
160{
161 atomic_long_set(&shrinker->nr_in_batch, 0);
162 down_write(&shrinker_rwsem);
163 list_add_tail(&shrinker->list, &shrinker_list);
164 up_write(&shrinker_rwsem);
165}
166EXPORT_SYMBOL(register_shrinker);
167
168
169
170
171void unregister_shrinker(struct shrinker *shrinker)
172{
173 down_write(&shrinker_rwsem);
174 list_del(&shrinker->list);
175 up_write(&shrinker_rwsem);
176}
177EXPORT_SYMBOL(unregister_shrinker);
178
179static inline int do_shrinker_shrink(struct shrinker *shrinker,
180 struct shrink_control *sc,
181 unsigned long nr_to_scan)
182{
183 sc->nr_to_scan = nr_to_scan;
184 return (*shrinker->shrink)(shrinker, sc);
185}
186
187#define SHRINK_BATCH 128
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207unsigned long shrink_slab(struct shrink_control *shrink,
208 unsigned long nr_pages_scanned,
209 unsigned long lru_pages)
210{
211 struct shrinker *shrinker;
212 unsigned long ret = 0;
213
214 if (nr_pages_scanned == 0)
215 nr_pages_scanned = SWAP_CLUSTER_MAX;
216
217 if (!down_read_trylock(&shrinker_rwsem)) {
218
219 ret = 1;
220 goto out;
221 }
222
223 list_for_each_entry(shrinker, &shrinker_list, list) {
224 unsigned long long delta;
225 long total_scan;
226 long max_pass;
227 int shrink_ret = 0;
228 long nr;
229 long new_nr;
230 long batch_size = shrinker->batch ? shrinker->batch
231 : SHRINK_BATCH;
232
233 max_pass = do_shrinker_shrink(shrinker, shrink, 0);
234 if (max_pass <= 0)
235 continue;
236
237
238
239
240
241
242 nr = atomic_long_xchg(&shrinker->nr_in_batch, 0);
243
244 total_scan = nr;
245 delta = (4 * nr_pages_scanned) / shrinker->seeks;
246 delta *= max_pass;
247 do_div(delta, lru_pages + 1);
248 total_scan += delta;
249 if (total_scan < 0) {
250 printk(KERN_ERR "shrink_slab: %pF negative objects to "
251 "delete nr=%ld\n",
252 shrinker->shrink, total_scan);
253 total_scan = max_pass;
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
268 if (delta < max_pass / 4)
269 total_scan = min(total_scan, max_pass / 2);
270
271
272
273
274
275
276 if (total_scan > max_pass * 2)
277 total_scan = max_pass * 2;
278
279 trace_mm_shrink_slab_start(shrinker, shrink, nr,
280 nr_pages_scanned, lru_pages,
281 max_pass, delta, total_scan);
282
283 while (total_scan >= batch_size) {
284 int nr_before;
285
286 nr_before = do_shrinker_shrink(shrinker, shrink, 0);
287 shrink_ret = do_shrinker_shrink(shrinker, shrink,
288 batch_size);
289 if (shrink_ret == -1)
290 break;
291 if (shrink_ret < nr_before)
292 ret += nr_before - shrink_ret;
293 count_vm_events(SLABS_SCANNED, batch_size);
294 total_scan -= batch_size;
295
296 cond_resched();
297 }
298
299
300
301
302
303
304 if (total_scan > 0)
305 new_nr = atomic_long_add_return(total_scan,
306 &shrinker->nr_in_batch);
307 else
308 new_nr = atomic_long_read(&shrinker->nr_in_batch);
309
310 trace_mm_shrink_slab_end(shrinker, shrink_ret, nr, new_nr);
311 }
312 up_read(&shrinker_rwsem);
313out:
314 cond_resched();
315 return ret;
316}
317
318static inline int is_page_cache_freeable(struct page *page)
319{
320
321
322
323
324
325 return page_count(page) - page_has_private(page) == 2;
326}
327
328static int may_write_to_queue(struct backing_dev_info *bdi,
329 struct scan_control *sc)
330{
331 if (current->flags & PF_SWAPWRITE)
332 return 1;
333 if (!bdi_write_congested(bdi))
334 return 1;
335 if (bdi == current->backing_dev_info)
336 return 1;
337 return 0;
338}
339
340
341
342
343
344
345
346
347
348
349
350
351
352static void handle_write_error(struct address_space *mapping,
353 struct page *page, int error)
354{
355 lock_page(page);
356 if (page_mapping(page) == mapping)
357 mapping_set_error(mapping, error);
358 unlock_page(page);
359}
360
361
362typedef enum {
363
364 PAGE_KEEP,
365
366 PAGE_ACTIVATE,
367
368 PAGE_SUCCESS,
369
370 PAGE_CLEAN,
371} pageout_t;
372
373
374
375
376
377static pageout_t pageout(struct page *page, struct address_space *mapping,
378 struct scan_control *sc)
379{
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396 if (!is_page_cache_freeable(page))
397 return PAGE_KEEP;
398 if (!mapping) {
399
400
401
402
403 if (page_has_private(page)) {
404 if (try_to_free_buffers(page)) {
405 ClearPageDirty(page);
406 printk("%s: orphaned page\n", __func__);
407 return PAGE_CLEAN;
408 }
409 }
410 return PAGE_KEEP;
411 }
412 if (mapping->a_ops->writepage == NULL)
413 return PAGE_ACTIVATE;
414 if (!may_write_to_queue(mapping->backing_dev_info, sc))
415 return PAGE_KEEP;
416
417 if (clear_page_dirty_for_io(page)) {
418 int res;
419 struct writeback_control wbc = {
420 .sync_mode = WB_SYNC_NONE,
421 .nr_to_write = SWAP_CLUSTER_MAX,
422 .range_start = 0,
423 .range_end = LLONG_MAX,
424 .for_reclaim = 1,
425 };
426
427 SetPageReclaim(page);
428 res = mapping->a_ops->writepage(page, &wbc);
429 if (res < 0)
430 handle_write_error(mapping, page, res);
431 if (res == AOP_WRITEPAGE_ACTIVATE) {
432 ClearPageReclaim(page);
433 return PAGE_ACTIVATE;
434 }
435
436 if (!PageWriteback(page)) {
437
438 ClearPageReclaim(page);
439 }
440 trace_mm_vmscan_writepage(page, trace_reclaim_flags(page));
441 inc_zone_page_state(page, NR_VMSCAN_WRITE);
442 return PAGE_SUCCESS;
443 }
444
445 return PAGE_CLEAN;
446}
447
448
449
450
451
452static int __remove_mapping(struct address_space *mapping, struct page *page)
453{
454 BUG_ON(!PageLocked(page));
455 BUG_ON(mapping != page_mapping(page));
456
457 spin_lock_irq(&mapping->tree_lock);
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 if (!page_freeze_refs(page, 2))
484 goto cannot_free;
485
486 if (unlikely(PageDirty(page))) {
487 page_unfreeze_refs(page, 2);
488 goto cannot_free;
489 }
490
491 if (PageSwapCache(page)) {
492 swp_entry_t swap = { .val = page_private(page) };
493 __delete_from_swap_cache(page);
494 spin_unlock_irq(&mapping->tree_lock);
495 swapcache_free(swap, page);
496 } else {
497 void (*freepage)(struct page *);
498
499 freepage = mapping->a_ops->freepage;
500
501 __delete_from_page_cache(page);
502 spin_unlock_irq(&mapping->tree_lock);
503 mem_cgroup_uncharge_cache_page(page);
504
505 if (freepage != NULL)
506 freepage(page);
507 }
508
509 return 1;
510
511cannot_free:
512 spin_unlock_irq(&mapping->tree_lock);
513 return 0;
514}
515
516
517
518
519
520
521
522int remove_mapping(struct address_space *mapping, struct page *page)
523{
524 if (__remove_mapping(mapping, page)) {
525
526
527
528
529
530 page_unfreeze_refs(page, 1);
531 return 1;
532 }
533 return 0;
534}
535
536
537
538
539
540
541
542
543
544
545void putback_lru_page(struct page *page)
546{
547 int lru;
548 int active = !!TestClearPageActive(page);
549 int was_unevictable = PageUnevictable(page);
550
551 VM_BUG_ON(PageLRU(page));
552
553redo:
554 ClearPageUnevictable(page);
555
556 if (page_evictable(page, NULL)) {
557
558
559
560
561
562
563 lru = active + page_lru_base_type(page);
564 lru_cache_add_lru(page, lru);
565 } else {
566
567
568
569
570 lru = LRU_UNEVICTABLE;
571 add_page_to_unevictable_list(page);
572
573
574
575
576
577
578
579
580
581
582 smp_mb();
583 }
584
585
586
587
588
589
590 if (lru == LRU_UNEVICTABLE && page_evictable(page, NULL)) {
591 if (!isolate_lru_page(page)) {
592 put_page(page);
593 goto redo;
594 }
595
596
597
598
599 }
600
601 if (was_unevictable && lru != LRU_UNEVICTABLE)
602 count_vm_event(UNEVICTABLE_PGRESCUED);
603 else if (!was_unevictable && lru == LRU_UNEVICTABLE)
604 count_vm_event(UNEVICTABLE_PGCULLED);
605
606 put_page(page);
607}
608
609enum page_references {
610 PAGEREF_RECLAIM,
611 PAGEREF_RECLAIM_CLEAN,
612 PAGEREF_KEEP,
613 PAGEREF_ACTIVATE,
614};
615
616static enum page_references page_check_references(struct page *page,
617 struct scan_control *sc)
618{
619 int referenced_ptes, referenced_page;
620 unsigned long vm_flags;
621
622 referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup,
623 &vm_flags);
624 referenced_page = TestClearPageReferenced(page);
625
626
627
628
629
630 if (vm_flags & VM_LOCKED)
631 return PAGEREF_RECLAIM;
632
633 if (referenced_ptes) {
634 if (PageSwapBacked(page))
635 return PAGEREF_ACTIVATE;
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650 SetPageReferenced(page);
651
652 if (referenced_page || referenced_ptes > 1)
653 return PAGEREF_ACTIVATE;
654
655
656
657
658 if (vm_flags & VM_EXEC)
659 return PAGEREF_ACTIVATE;
660
661 return PAGEREF_KEEP;
662 }
663
664
665 if (referenced_page && !PageSwapBacked(page))
666 return PAGEREF_RECLAIM_CLEAN;
667
668 return PAGEREF_RECLAIM;
669}
670
671
672
673
674static unsigned long shrink_page_list(struct list_head *page_list,
675 struct zone *zone,
676 struct scan_control *sc,
677 unsigned long *ret_nr_dirty,
678 unsigned long *ret_nr_writeback)
679{
680 LIST_HEAD(ret_pages);
681 LIST_HEAD(free_pages);
682 int pgactivate = 0;
683 unsigned long nr_dirty = 0;
684 unsigned long nr_congested = 0;
685 unsigned long nr_reclaimed = 0;
686 unsigned long nr_writeback = 0;
687
688 cond_resched();
689
690 mem_cgroup_uncharge_start();
691 while (!list_empty(page_list)) {
692 enum page_references references;
693 struct address_space *mapping;
694 struct page *page;
695 int may_enter_fs;
696
697 cond_resched();
698
699 page = lru_to_page(page_list);
700 list_del(&page->lru);
701
702 if (!trylock_page(page))
703 goto keep;
704
705 VM_BUG_ON(PageActive(page));
706 VM_BUG_ON(page_zone(page) != zone);
707
708 sc->nr_scanned++;
709
710 if (unlikely(!page_evictable(page, NULL)))
711 goto cull_mlocked;
712
713 if (!sc->may_unmap && page_mapped(page))
714 goto keep_locked;
715
716
717 if (page_mapped(page) || PageSwapCache(page))
718 sc->nr_scanned++;
719
720 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
721 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
722
723 if (PageWriteback(page)) {
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741 if (global_reclaim(sc) ||
742 !PageReclaim(page) || !(sc->gfp_mask & __GFP_IO)) {
743
744
745
746
747
748
749
750
751
752
753
754 SetPageReclaim(page);
755 nr_writeback++;
756 goto keep_locked;
757 }
758 wait_on_page_writeback(page);
759 }
760
761 references = page_check_references(page, sc);
762 switch (references) {
763 case PAGEREF_ACTIVATE:
764 goto activate_locked;
765 case PAGEREF_KEEP:
766 goto keep_locked;
767 case PAGEREF_RECLAIM:
768 case PAGEREF_RECLAIM_CLEAN:
769 ;
770 }
771
772
773
774
775
776 if (PageAnon(page) && !PageSwapCache(page)) {
777 if (!(sc->gfp_mask & __GFP_IO))
778 goto keep_locked;
779 if (!add_to_swap(page))
780 goto activate_locked;
781 may_enter_fs = 1;
782 }
783
784 mapping = page_mapping(page);
785
786
787
788
789
790 if (page_mapped(page) && mapping) {
791 switch (try_to_unmap(page, TTU_UNMAP)) {
792 case SWAP_FAIL:
793 goto activate_locked;
794 case SWAP_AGAIN:
795 goto keep_locked;
796 case SWAP_MLOCK:
797 goto cull_mlocked;
798 case SWAP_SUCCESS:
799 ;
800 }
801 }
802
803 if (PageDirty(page)) {
804 nr_dirty++;
805
806
807
808
809
810
811 if (page_is_file_cache(page) &&
812 (!current_is_kswapd() ||
813 sc->priority >= DEF_PRIORITY - 2)) {
814
815
816
817
818
819
820 inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE);
821 SetPageReclaim(page);
822
823 goto keep_locked;
824 }
825
826 if (references == PAGEREF_RECLAIM_CLEAN)
827 goto keep_locked;
828 if (!may_enter_fs)
829 goto keep_locked;
830 if (!sc->may_writepage)
831 goto keep_locked;
832
833
834 switch (pageout(page, mapping, sc)) {
835 case PAGE_KEEP:
836 nr_congested++;
837 goto keep_locked;
838 case PAGE_ACTIVATE:
839 goto activate_locked;
840 case PAGE_SUCCESS:
841 if (PageWriteback(page))
842 goto keep;
843 if (PageDirty(page))
844 goto keep;
845
846
847
848
849
850 if (!trylock_page(page))
851 goto keep;
852 if (PageDirty(page) || PageWriteback(page))
853 goto keep_locked;
854 mapping = page_mapping(page);
855 case PAGE_CLEAN:
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 if (page_has_private(page)) {
882 if (!try_to_release_page(page, sc->gfp_mask))
883 goto activate_locked;
884 if (!mapping && page_count(page) == 1) {
885 unlock_page(page);
886 if (put_page_testzero(page))
887 goto free_it;
888 else {
889
890
891
892
893
894
895
896 nr_reclaimed++;
897 continue;
898 }
899 }
900 }
901
902 if (!mapping || !__remove_mapping(mapping, page))
903 goto keep_locked;
904
905
906
907
908
909
910
911
912 __clear_page_locked(page);
913free_it:
914 nr_reclaimed++;
915
916
917
918
919
920 list_add(&page->lru, &free_pages);
921 continue;
922
923cull_mlocked:
924 if (PageSwapCache(page))
925 try_to_free_swap(page);
926 unlock_page(page);
927 putback_lru_page(page);
928 continue;
929
930activate_locked:
931
932 if (PageSwapCache(page) && vm_swap_full())
933 try_to_free_swap(page);
934 VM_BUG_ON(PageActive(page));
935 SetPageActive(page);
936 pgactivate++;
937keep_locked:
938 unlock_page(page);
939keep:
940 list_add(&page->lru, &ret_pages);
941 VM_BUG_ON(PageLRU(page) || PageUnevictable(page));
942 }
943
944
945
946
947
948
949
950 if (nr_dirty && nr_dirty == nr_congested && global_reclaim(sc))
951 zone_set_flag(zone, ZONE_CONGESTED);
952
953 free_hot_cold_page_list(&free_pages, 1);
954
955 list_splice(&ret_pages, page_list);
956 count_vm_events(PGACTIVATE, pgactivate);
957 mem_cgroup_uncharge_end();
958 *ret_nr_dirty += nr_dirty;
959 *ret_nr_writeback += nr_writeback;
960 return nr_reclaimed;
961}
962
963
964
965
966
967
968
969
970
971
972
973int __isolate_lru_page(struct page *page, isolate_mode_t mode)
974{
975 int ret = -EINVAL;
976
977
978 if (!PageLRU(page))
979 return ret;
980
981
982 if (PageUnevictable(page))
983 return ret;
984
985 ret = -EBUSY;
986
987
988
989
990
991
992
993
994
995
996
997
998 if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) {
999
1000 if (PageWriteback(page))
1001 return ret;
1002
1003 if (PageDirty(page)) {
1004 struct address_space *mapping;
1005
1006
1007 if (mode & ISOLATE_CLEAN)
1008 return ret;
1009
1010
1011
1012
1013
1014
1015 mapping = page_mapping(page);
1016 if (mapping && !mapping->a_ops->migratepage)
1017 return ret;
1018 }
1019 }
1020
1021 if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
1022 return ret;
1023
1024 if (likely(get_page_unless_zero(page))) {
1025
1026
1027
1028
1029
1030 ClearPageLRU(page);
1031 ret = 0;
1032 }
1033
1034 return ret;
1035}
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
1058 struct lruvec *lruvec, struct list_head *dst,
1059 unsigned long *nr_scanned, struct scan_control *sc,
1060 isolate_mode_t mode, enum lru_list lru)
1061{
1062 struct list_head *src = &lruvec->lists[lru];
1063 unsigned long nr_taken = 0;
1064 unsigned long scan;
1065
1066 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
1067 struct page *page;
1068 int nr_pages;
1069
1070 page = lru_to_page(src);
1071 prefetchw_prev_lru_page(page, src, flags);
1072
1073 VM_BUG_ON(!PageLRU(page));
1074
1075 switch (__isolate_lru_page(page, mode)) {
1076 case 0:
1077 nr_pages = hpage_nr_pages(page);
1078 mem_cgroup_update_lru_size(lruvec, lru, -nr_pages);
1079 list_move(&page->lru, dst);
1080 nr_taken += nr_pages;
1081 break;
1082
1083 case -EBUSY:
1084
1085 list_move(&page->lru, src);
1086 continue;
1087
1088 default:
1089 BUG();
1090 }
1091 }
1092
1093 *nr_scanned = scan;
1094 trace_mm_vmscan_lru_isolate(sc->order, nr_to_scan, scan,
1095 nr_taken, mode, is_file_lru(lru));
1096 return nr_taken;
1097}
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124int isolate_lru_page(struct page *page)
1125{
1126 int ret = -EBUSY;
1127
1128 VM_BUG_ON(!page_count(page));
1129
1130 if (PageLRU(page)) {
1131 struct zone *zone = page_zone(page);
1132 struct lruvec *lruvec;
1133
1134 spin_lock_irq(&zone->lru_lock);
1135 lruvec = mem_cgroup_page_lruvec(page, zone);
1136 if (PageLRU(page)) {
1137 int lru = page_lru(page);
1138 get_page(page);
1139 ClearPageLRU(page);
1140 del_page_from_lru_list(page, lruvec, lru);
1141 ret = 0;
1142 }
1143 spin_unlock_irq(&zone->lru_lock);
1144 }
1145 return ret;
1146}
1147
1148
1149
1150
1151static int too_many_isolated(struct zone *zone, int file,
1152 struct scan_control *sc)
1153{
1154 unsigned long inactive, isolated;
1155
1156 if (current_is_kswapd())
1157 return 0;
1158
1159 if (!global_reclaim(sc))
1160 return 0;
1161
1162 if (file) {
1163 inactive = zone_page_state(zone, NR_INACTIVE_FILE);
1164 isolated = zone_page_state(zone, NR_ISOLATED_FILE);
1165 } else {
1166 inactive = zone_page_state(zone, NR_INACTIVE_ANON);
1167 isolated = zone_page_state(zone, NR_ISOLATED_ANON);
1168 }
1169
1170 return isolated > inactive;
1171}
1172
1173static noinline_for_stack void
1174putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list)
1175{
1176 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
1177 struct zone *zone = lruvec_zone(lruvec);
1178 LIST_HEAD(pages_to_free);
1179
1180
1181
1182
1183 while (!list_empty(page_list)) {
1184 struct page *page = lru_to_page(page_list);
1185 int lru;
1186
1187 VM_BUG_ON(PageLRU(page));
1188 list_del(&page->lru);
1189 if (unlikely(!page_evictable(page, NULL))) {
1190 spin_unlock_irq(&zone->lru_lock);
1191 putback_lru_page(page);
1192 spin_lock_irq(&zone->lru_lock);
1193 continue;
1194 }
1195
1196 lruvec = mem_cgroup_page_lruvec(page, zone);
1197
1198 SetPageLRU(page);
1199 lru = page_lru(page);
1200 add_page_to_lru_list(page, lruvec, lru);
1201
1202 if (is_active_lru(lru)) {
1203 int file = is_file_lru(lru);
1204 int numpages = hpage_nr_pages(page);
1205 reclaim_stat->recent_rotated[file] += numpages;
1206 }
1207 if (put_page_testzero(page)) {
1208 __ClearPageLRU(page);
1209 __ClearPageActive(page);
1210 del_page_from_lru_list(page, lruvec, lru);
1211
1212 if (unlikely(PageCompound(page))) {
1213 spin_unlock_irq(&zone->lru_lock);
1214 (*get_compound_page_dtor(page))(page);
1215 spin_lock_irq(&zone->lru_lock);
1216 } else
1217 list_add(&page->lru, &pages_to_free);
1218 }
1219 }
1220
1221
1222
1223
1224 list_splice(&pages_to_free, page_list);
1225}
1226
1227
1228
1229
1230
1231static noinline_for_stack unsigned long
1232shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
1233 struct scan_control *sc, enum lru_list lru)
1234{
1235 LIST_HEAD(page_list);
1236 unsigned long nr_scanned;
1237 unsigned long nr_reclaimed = 0;
1238 unsigned long nr_taken;
1239 unsigned long nr_dirty = 0;
1240 unsigned long nr_writeback = 0;
1241 isolate_mode_t isolate_mode = 0;
1242 int file = is_file_lru(lru);
1243 struct zone *zone = lruvec_zone(lruvec);
1244 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
1245
1246 while (unlikely(too_many_isolated(zone, file, sc))) {
1247 congestion_wait(BLK_RW_ASYNC, HZ/10);
1248
1249
1250 if (fatal_signal_pending(current))
1251 return SWAP_CLUSTER_MAX;
1252 }
1253
1254 lru_add_drain();
1255
1256 if (!sc->may_unmap)
1257 isolate_mode |= ISOLATE_UNMAPPED;
1258 if (!sc->may_writepage)
1259 isolate_mode |= ISOLATE_CLEAN;
1260
1261 spin_lock_irq(&zone->lru_lock);
1262
1263 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
1264 &nr_scanned, sc, isolate_mode, lru);
1265
1266 __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken);
1267 __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
1268
1269 if (global_reclaim(sc)) {
1270 zone->pages_scanned += nr_scanned;
1271 if (current_is_kswapd())
1272 __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scanned);
1273 else
1274 __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scanned);
1275 }
1276 spin_unlock_irq(&zone->lru_lock);
1277
1278 if (nr_taken == 0)
1279 return 0;
1280
1281 nr_reclaimed = shrink_page_list(&page_list, zone, sc,
1282 &nr_dirty, &nr_writeback);
1283
1284 spin_lock_irq(&zone->lru_lock);
1285
1286 reclaim_stat->recent_scanned[file] += nr_taken;
1287
1288 if (global_reclaim(sc)) {
1289 if (current_is_kswapd())
1290 __count_zone_vm_events(PGSTEAL_KSWAPD, zone,
1291 nr_reclaimed);
1292 else
1293 __count_zone_vm_events(PGSTEAL_DIRECT, zone,
1294 nr_reclaimed);
1295 }
1296
1297 putback_inactive_pages(lruvec, &page_list);
1298
1299 __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
1300
1301 spin_unlock_irq(&zone->lru_lock);
1302
1303 free_hot_cold_page_list(&page_list, 1);
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328 if (nr_writeback && nr_writeback >=
1329 (nr_taken >> (DEF_PRIORITY - sc->priority)))
1330 wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
1331
1332 trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id,
1333 zone_idx(zone),
1334 nr_scanned, nr_reclaimed,
1335 sc->priority,
1336 trace_shrink_flags(file));
1337 return nr_reclaimed;
1338}
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358static void move_active_pages_to_lru(struct lruvec *lruvec,
1359 struct list_head *list,
1360 struct list_head *pages_to_free,
1361 enum lru_list lru)
1362{
1363 struct zone *zone = lruvec_zone(lruvec);
1364 unsigned long pgmoved = 0;
1365 struct page *page;
1366 int nr_pages;
1367
1368 while (!list_empty(list)) {
1369 page = lru_to_page(list);
1370 lruvec = mem_cgroup_page_lruvec(page, zone);
1371
1372 VM_BUG_ON(PageLRU(page));
1373 SetPageLRU(page);
1374
1375 nr_pages = hpage_nr_pages(page);
1376 mem_cgroup_update_lru_size(lruvec, lru, nr_pages);
1377 list_move(&page->lru, &lruvec->lists[lru]);
1378 pgmoved += nr_pages;
1379
1380 if (put_page_testzero(page)) {
1381 __ClearPageLRU(page);
1382 __ClearPageActive(page);
1383 del_page_from_lru_list(page, lruvec, lru);
1384
1385 if (unlikely(PageCompound(page))) {
1386 spin_unlock_irq(&zone->lru_lock);
1387 (*get_compound_page_dtor(page))(page);
1388 spin_lock_irq(&zone->lru_lock);
1389 } else
1390 list_add(&page->lru, pages_to_free);
1391 }
1392 }
1393 __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved);
1394 if (!is_active_lru(lru))
1395 __count_vm_events(PGDEACTIVATE, pgmoved);
1396}
1397
1398static void shrink_active_list(unsigned long nr_to_scan,
1399 struct lruvec *lruvec,
1400 struct scan_control *sc,
1401 enum lru_list lru)
1402{
1403 unsigned long nr_taken;
1404 unsigned long nr_scanned;
1405 unsigned long vm_flags;
1406 LIST_HEAD(l_hold);
1407 LIST_HEAD(l_active);
1408 LIST_HEAD(l_inactive);
1409 struct page *page;
1410 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
1411 unsigned long nr_rotated = 0;
1412 isolate_mode_t isolate_mode = 0;
1413 int file = is_file_lru(lru);
1414 struct zone *zone = lruvec_zone(lruvec);
1415
1416 lru_add_drain();
1417
1418 if (!sc->may_unmap)
1419 isolate_mode |= ISOLATE_UNMAPPED;
1420 if (!sc->may_writepage)
1421 isolate_mode |= ISOLATE_CLEAN;
1422
1423 spin_lock_irq(&zone->lru_lock);
1424
1425 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
1426 &nr_scanned, sc, isolate_mode, lru);
1427 if (global_reclaim(sc))
1428 zone->pages_scanned += nr_scanned;
1429
1430 reclaim_stat->recent_scanned[file] += nr_taken;
1431
1432 __count_zone_vm_events(PGREFILL, zone, nr_scanned);
1433 __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken);
1434 __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken);
1435 spin_unlock_irq(&zone->lru_lock);
1436
1437 while (!list_empty(&l_hold)) {
1438 cond_resched();
1439 page = lru_to_page(&l_hold);
1440 list_del(&page->lru);
1441
1442 if (unlikely(!page_evictable(page, NULL))) {
1443 putback_lru_page(page);
1444 continue;
1445 }
1446
1447 if (unlikely(buffer_heads_over_limit)) {
1448 if (page_has_private(page) && trylock_page(page)) {
1449 if (page_has_private(page))
1450 try_to_release_page(page, 0);
1451 unlock_page(page);
1452 }
1453 }
1454
1455 if (page_referenced(page, 0, sc->target_mem_cgroup,
1456 &vm_flags)) {
1457 nr_rotated += hpage_nr_pages(page);
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467 if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
1468 list_add(&page->lru, &l_active);
1469 continue;
1470 }
1471 }
1472
1473 ClearPageActive(page);
1474 list_add(&page->lru, &l_inactive);
1475 }
1476
1477
1478
1479
1480 spin_lock_irq(&zone->lru_lock);
1481
1482
1483
1484
1485
1486
1487 reclaim_stat->recent_rotated[file] += nr_rotated;
1488
1489 move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru);
1490 move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE);
1491 __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken);
1492 spin_unlock_irq(&zone->lru_lock);
1493
1494 free_hot_cold_page_list(&l_hold, 1);
1495}
1496
1497#ifdef CONFIG_SWAP
1498static int inactive_anon_is_low_global(struct zone *zone)
1499{
1500 unsigned long active, inactive;
1501
1502 active = zone_page_state(zone, NR_ACTIVE_ANON);
1503 inactive = zone_page_state(zone, NR_INACTIVE_ANON);
1504
1505 if (inactive * zone->inactive_ratio < active)
1506 return 1;
1507
1508 return 0;
1509}
1510
1511
1512
1513
1514
1515
1516
1517
1518static int inactive_anon_is_low(struct lruvec *lruvec)
1519{
1520
1521
1522
1523
1524 if (!total_swap_pages)
1525 return 0;
1526
1527 if (!mem_cgroup_disabled())
1528 return mem_cgroup_inactive_anon_is_low(lruvec);
1529
1530 return inactive_anon_is_low_global(lruvec_zone(lruvec));
1531}
1532#else
1533static inline int inactive_anon_is_low(struct lruvec *lruvec)
1534{
1535 return 0;
1536}
1537#endif
1538
1539static int inactive_file_is_low_global(struct zone *zone)
1540{
1541 unsigned long active, inactive;
1542
1543 active = zone_page_state(zone, NR_ACTIVE_FILE);
1544 inactive = zone_page_state(zone, NR_INACTIVE_FILE);
1545
1546 return (active > inactive);
1547}
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563static int inactive_file_is_low(struct lruvec *lruvec)
1564{
1565 if (!mem_cgroup_disabled())
1566 return mem_cgroup_inactive_file_is_low(lruvec);
1567
1568 return inactive_file_is_low_global(lruvec_zone(lruvec));
1569}
1570
1571static int inactive_list_is_low(struct lruvec *lruvec, enum lru_list lru)
1572{
1573 if (is_file_lru(lru))
1574 return inactive_file_is_low(lruvec);
1575 else
1576 return inactive_anon_is_low(lruvec);
1577}
1578
1579static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
1580 struct lruvec *lruvec, struct scan_control *sc)
1581{
1582 if (is_active_lru(lru)) {
1583 if (inactive_list_is_low(lruvec, lru))
1584 shrink_active_list(nr_to_scan, lruvec, sc, lru);
1585 return 0;
1586 }
1587
1588 return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
1589}
1590
1591static int vmscan_swappiness(struct scan_control *sc)
1592{
1593 if (global_reclaim(sc))
1594 return vm_swappiness;
1595 return mem_cgroup_swappiness(sc->target_mem_cgroup);
1596}
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
1608 unsigned long *nr)
1609{
1610 unsigned long anon, file, free;
1611 unsigned long anon_prio, file_prio;
1612 unsigned long ap, fp;
1613 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
1614 u64 fraction[2], denominator;
1615 enum lru_list lru;
1616 int noswap = 0;
1617 bool force_scan = false;
1618 struct zone *zone = lruvec_zone(lruvec);
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630 if (current_is_kswapd() && zone->all_unreclaimable)
1631 force_scan = true;
1632 if (!global_reclaim(sc))
1633 force_scan = true;
1634
1635
1636 if (!sc->may_swap || (nr_swap_pages <= 0)) {
1637 noswap = 1;
1638 fraction[0] = 0;
1639 fraction[1] = 1;
1640 denominator = 1;
1641 goto out;
1642 }
1643
1644 anon = get_lru_size(lruvec, LRU_ACTIVE_ANON) +
1645 get_lru_size(lruvec, LRU_INACTIVE_ANON);
1646 file = get_lru_size(lruvec, LRU_ACTIVE_FILE) +
1647 get_lru_size(lruvec, LRU_INACTIVE_FILE);
1648
1649 if (global_reclaim(sc)) {
1650 free = zone_page_state(zone, NR_FREE_PAGES);
1651
1652
1653 if (unlikely(file + free <= high_wmark_pages(zone))) {
1654 fraction[0] = 1;
1655 fraction[1] = 0;
1656 denominator = 1;
1657 goto out;
1658 }
1659 }
1660
1661
1662
1663
1664
1665 anon_prio = vmscan_swappiness(sc);
1666 file_prio = 200 - anon_prio;
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679 spin_lock_irq(&zone->lru_lock);
1680 if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
1681 reclaim_stat->recent_scanned[0] /= 2;
1682 reclaim_stat->recent_rotated[0] /= 2;
1683 }
1684
1685 if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
1686 reclaim_stat->recent_scanned[1] /= 2;
1687 reclaim_stat->recent_rotated[1] /= 2;
1688 }
1689
1690
1691
1692
1693
1694
1695 ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1);
1696 ap /= reclaim_stat->recent_rotated[0] + 1;
1697
1698 fp = file_prio * (reclaim_stat->recent_scanned[1] + 1);
1699 fp /= reclaim_stat->recent_rotated[1] + 1;
1700 spin_unlock_irq(&zone->lru_lock);
1701
1702 fraction[0] = ap;
1703 fraction[1] = fp;
1704 denominator = ap + fp + 1;
1705out:
1706 for_each_evictable_lru(lru) {
1707 int file = is_file_lru(lru);
1708 unsigned long scan;
1709
1710 scan = get_lru_size(lruvec, lru);
1711 if (sc->priority || noswap || !vmscan_swappiness(sc)) {
1712 scan >>= sc->priority;
1713 if (!scan && force_scan)
1714 scan = SWAP_CLUSTER_MAX;
1715 scan = div64_u64(scan * fraction[file], denominator);
1716 }
1717 nr[lru] = scan;
1718 }
1719}
1720
1721
1722static bool in_reclaim_compaction(struct scan_control *sc)
1723{
1724 if (COMPACTION_BUILD && sc->order &&
1725 (sc->order > PAGE_ALLOC_COSTLY_ORDER ||
1726 sc->priority < DEF_PRIORITY - 2))
1727 return true;
1728
1729 return false;
1730}
1731
1732
1733
1734
1735
1736
1737
1738
1739static inline bool should_continue_reclaim(struct lruvec *lruvec,
1740 unsigned long nr_reclaimed,
1741 unsigned long nr_scanned,
1742 struct scan_control *sc)
1743{
1744 unsigned long pages_for_compaction;
1745 unsigned long inactive_lru_pages;
1746
1747
1748 if (!in_reclaim_compaction(sc))
1749 return false;
1750
1751
1752 if (sc->gfp_mask & __GFP_REPEAT) {
1753
1754
1755
1756
1757
1758
1759 if (!nr_reclaimed && !nr_scanned)
1760 return false;
1761 } else {
1762
1763
1764
1765
1766
1767
1768
1769
1770 if (!nr_reclaimed)
1771 return false;
1772 }
1773
1774
1775
1776
1777
1778 pages_for_compaction = (2UL << sc->order);
1779 inactive_lru_pages = get_lru_size(lruvec, LRU_INACTIVE_FILE);
1780 if (nr_swap_pages > 0)
1781 inactive_lru_pages += get_lru_size(lruvec, LRU_INACTIVE_ANON);
1782 if (sc->nr_reclaimed < pages_for_compaction &&
1783 inactive_lru_pages > pages_for_compaction)
1784 return true;
1785
1786
1787 switch (compaction_suitable(lruvec_zone(lruvec), sc->order)) {
1788 case COMPACT_PARTIAL:
1789 case COMPACT_CONTINUE:
1790 return false;
1791 default:
1792 return true;
1793 }
1794}
1795
1796
1797
1798
1799static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
1800{
1801 unsigned long nr[NR_LRU_LISTS];
1802 unsigned long nr_to_scan;
1803 enum lru_list lru;
1804 unsigned long nr_reclaimed, nr_scanned;
1805 unsigned long nr_to_reclaim = sc->nr_to_reclaim;
1806 struct blk_plug plug;
1807
1808restart:
1809 nr_reclaimed = 0;
1810 nr_scanned = sc->nr_scanned;
1811 get_scan_count(lruvec, sc, nr);
1812
1813 blk_start_plug(&plug);
1814 while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
1815 nr[LRU_INACTIVE_FILE]) {
1816 for_each_evictable_lru(lru) {
1817 if (nr[lru]) {
1818 nr_to_scan = min_t(unsigned long,
1819 nr[lru], SWAP_CLUSTER_MAX);
1820 nr[lru] -= nr_to_scan;
1821
1822 nr_reclaimed += shrink_list(lru, nr_to_scan,
1823 lruvec, sc);
1824 }
1825 }
1826
1827
1828
1829
1830
1831
1832
1833
1834 if (nr_reclaimed >= nr_to_reclaim &&
1835 sc->priority < DEF_PRIORITY)
1836 break;
1837 }
1838 blk_finish_plug(&plug);
1839 sc->nr_reclaimed += nr_reclaimed;
1840
1841
1842
1843
1844
1845 if (inactive_anon_is_low(lruvec))
1846 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
1847 sc, LRU_ACTIVE_ANON);
1848
1849
1850 if (should_continue_reclaim(lruvec, nr_reclaimed,
1851 sc->nr_scanned - nr_scanned, sc))
1852 goto restart;
1853
1854 throttle_vm_writeout(sc->gfp_mask);
1855}
1856
1857static void shrink_zone(struct zone *zone, struct scan_control *sc)
1858{
1859 struct mem_cgroup *root = sc->target_mem_cgroup;
1860 struct mem_cgroup_reclaim_cookie reclaim = {
1861 .zone = zone,
1862 .priority = sc->priority,
1863 };
1864 struct mem_cgroup *memcg;
1865
1866 memcg = mem_cgroup_iter(root, NULL, &reclaim);
1867 do {
1868 struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
1869
1870 shrink_lruvec(lruvec, sc);
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882 if (!global_reclaim(sc)) {
1883 mem_cgroup_iter_break(root, memcg);
1884 break;
1885 }
1886 memcg = mem_cgroup_iter(root, memcg, &reclaim);
1887 } while (memcg);
1888}
1889
1890
1891static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
1892{
1893 unsigned long balance_gap, watermark;
1894 bool watermark_ok;
1895
1896
1897 if (sc->order <= PAGE_ALLOC_COSTLY_ORDER)
1898 return false;
1899
1900
1901
1902
1903
1904
1905
1906 balance_gap = min(low_wmark_pages(zone),
1907 (zone->present_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
1908 KSWAPD_ZONE_BALANCE_GAP_RATIO);
1909 watermark = high_wmark_pages(zone) + balance_gap + (2UL << sc->order);
1910 watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0);
1911
1912
1913
1914
1915
1916 if (compaction_deferred(zone, sc->order))
1917 return watermark_ok;
1918
1919
1920 if (!compaction_suitable(zone, sc->order))
1921 return false;
1922
1923 return watermark_ok;
1924}
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
1948{
1949 struct zoneref *z;
1950 struct zone *zone;
1951 unsigned long nr_soft_reclaimed;
1952 unsigned long nr_soft_scanned;
1953 bool aborted_reclaim = false;
1954
1955
1956
1957
1958
1959
1960 if (buffer_heads_over_limit)
1961 sc->gfp_mask |= __GFP_HIGHMEM;
1962
1963 for_each_zone_zonelist_nodemask(zone, z, zonelist,
1964 gfp_zone(sc->gfp_mask), sc->nodemask) {
1965 if (!populated_zone(zone))
1966 continue;
1967
1968
1969
1970
1971 if (global_reclaim(sc)) {
1972 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
1973 continue;
1974 if (zone->all_unreclaimable &&
1975 sc->priority != DEF_PRIORITY)
1976 continue;
1977 if (COMPACTION_BUILD) {
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987 if (compaction_ready(zone, sc)) {
1988 aborted_reclaim = true;
1989 continue;
1990 }
1991 }
1992
1993
1994
1995
1996
1997
1998 nr_soft_scanned = 0;
1999 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
2000 sc->order, sc->gfp_mask,
2001 &nr_soft_scanned);
2002 sc->nr_reclaimed += nr_soft_reclaimed;
2003 sc->nr_scanned += nr_soft_scanned;
2004
2005 }
2006
2007 shrink_zone(zone, sc);
2008 }
2009
2010 return aborted_reclaim;
2011}
2012
2013static bool zone_reclaimable(struct zone *zone)
2014{
2015 return zone->pages_scanned < zone_reclaimable_pages(zone) * 6;
2016}
2017
2018
2019static bool all_unreclaimable(struct zonelist *zonelist,
2020 struct scan_control *sc)
2021{
2022 struct zoneref *z;
2023 struct zone *zone;
2024
2025 for_each_zone_zonelist_nodemask(zone, z, zonelist,
2026 gfp_zone(sc->gfp_mask), sc->nodemask) {
2027 if (!populated_zone(zone))
2028 continue;
2029 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
2030 continue;
2031 if (!zone->all_unreclaimable)
2032 return false;
2033 }
2034
2035 return true;
2036}
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
2055 struct scan_control *sc,
2056 struct shrink_control *shrink)
2057{
2058 unsigned long total_scanned = 0;
2059 struct reclaim_state *reclaim_state = current->reclaim_state;
2060 struct zoneref *z;
2061 struct zone *zone;
2062 unsigned long writeback_threshold;
2063 bool aborted_reclaim;
2064
2065 delayacct_freepages_start();
2066
2067 if (global_reclaim(sc))
2068 count_vm_event(ALLOCSTALL);
2069
2070 do {
2071 sc->nr_scanned = 0;
2072 aborted_reclaim = shrink_zones(zonelist, sc);
2073
2074
2075
2076
2077
2078 if (global_reclaim(sc)) {
2079 unsigned long lru_pages = 0;
2080 for_each_zone_zonelist(zone, z, zonelist,
2081 gfp_zone(sc->gfp_mask)) {
2082 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
2083 continue;
2084
2085 lru_pages += zone_reclaimable_pages(zone);
2086 }
2087
2088 shrink_slab(shrink, sc->nr_scanned, lru_pages);
2089 if (reclaim_state) {
2090 sc->nr_reclaimed += reclaim_state->reclaimed_slab;
2091 reclaim_state->reclaimed_slab = 0;
2092 }
2093 }
2094 total_scanned += sc->nr_scanned;
2095 if (sc->nr_reclaimed >= sc->nr_to_reclaim)
2096 goto out;
2097
2098
2099
2100
2101
2102
2103
2104
2105 writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2;
2106 if (total_scanned > writeback_threshold) {
2107 wakeup_flusher_threads(laptop_mode ? 0 : total_scanned,
2108 WB_REASON_TRY_TO_FREE_PAGES);
2109 sc->may_writepage = 1;
2110 }
2111
2112
2113 if (!sc->hibernation_mode && sc->nr_scanned &&
2114 sc->priority < DEF_PRIORITY - 2) {
2115 struct zone *preferred_zone;
2116
2117 first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask),
2118 &cpuset_current_mems_allowed,
2119 &preferred_zone);
2120 wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10);
2121 }
2122 } while (--sc->priority >= 0);
2123
2124out:
2125 delayacct_freepages_end();
2126
2127 if (sc->nr_reclaimed)
2128 return sc->nr_reclaimed;
2129
2130
2131
2132
2133
2134
2135 if (oom_killer_disabled)
2136 return 0;
2137
2138
2139 if (aborted_reclaim)
2140 return 1;
2141
2142
2143 if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc))
2144 return 1;
2145
2146 return 0;
2147}
2148
2149static bool pfmemalloc_watermark_ok(pg_data_t *pgdat)
2150{
2151 struct zone *zone;
2152 unsigned long pfmemalloc_reserve = 0;
2153 unsigned long free_pages = 0;
2154 int i;
2155 bool wmark_ok;
2156
2157 for (i = 0; i <= ZONE_NORMAL; i++) {
2158 zone = &pgdat->node_zones[i];
2159 pfmemalloc_reserve += min_wmark_pages(zone);
2160 free_pages += zone_page_state(zone, NR_FREE_PAGES);
2161 }
2162
2163 wmark_ok = free_pages > pfmemalloc_reserve / 2;
2164
2165
2166 if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
2167 pgdat->classzone_idx = min(pgdat->classzone_idx,
2168 (enum zone_type)ZONE_NORMAL);
2169 wake_up_interruptible(&pgdat->kswapd_wait);
2170 }
2171
2172 return wmark_ok;
2173}
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
2185 nodemask_t *nodemask)
2186{
2187 struct zone *zone;
2188 int high_zoneidx = gfp_zone(gfp_mask);
2189 pg_data_t *pgdat;
2190
2191
2192
2193
2194
2195
2196
2197
2198 if (current->flags & PF_KTHREAD)
2199 goto out;
2200
2201
2202
2203
2204
2205 if (fatal_signal_pending(current))
2206 goto out;
2207
2208
2209 first_zones_zonelist(zonelist, high_zoneidx, NULL, &zone);
2210 pgdat = zone->zone_pgdat;
2211 if (pfmemalloc_watermark_ok(pgdat))
2212 goto out;
2213
2214
2215 count_vm_event(PGSCAN_DIRECT_THROTTLE);
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225 if (!(gfp_mask & __GFP_FS)) {
2226 wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
2227 pfmemalloc_watermark_ok(pgdat), HZ);
2228
2229 goto check_pending;
2230 }
2231
2232
2233 wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
2234 pfmemalloc_watermark_ok(pgdat));
2235
2236check_pending:
2237 if (fatal_signal_pending(current))
2238 return true;
2239
2240out:
2241 return false;
2242}
2243
2244unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
2245 gfp_t gfp_mask, nodemask_t *nodemask)
2246{
2247 unsigned long nr_reclaimed;
2248 struct scan_control sc = {
2249 .gfp_mask = gfp_mask,
2250 .may_writepage = !laptop_mode,
2251 .nr_to_reclaim = SWAP_CLUSTER_MAX,
2252 .may_unmap = 1,
2253 .may_swap = 1,
2254 .order = order,
2255 .priority = DEF_PRIORITY,
2256 .target_mem_cgroup = NULL,
2257 .nodemask = nodemask,
2258 };
2259 struct shrink_control shrink = {
2260 .gfp_mask = sc.gfp_mask,
2261 };
2262
2263
2264
2265
2266
2267
2268 if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask))
2269 return 1;
2270
2271 trace_mm_vmscan_direct_reclaim_begin(order,
2272 sc.may_writepage,
2273 gfp_mask);
2274
2275 nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
2276
2277 trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
2278
2279 return nr_reclaimed;
2280}
2281
2282#ifdef CONFIG_MEMCG
2283
2284unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg,
2285 gfp_t gfp_mask, bool noswap,
2286 struct zone *zone,
2287 unsigned long *nr_scanned)
2288{
2289 struct scan_control sc = {
2290 .nr_scanned = 0,
2291 .nr_to_reclaim = SWAP_CLUSTER_MAX,
2292 .may_writepage = !laptop_mode,
2293 .may_unmap = 1,
2294 .may_swap = !noswap,
2295 .order = 0,
2296 .priority = 0,
2297 .target_mem_cgroup = memcg,
2298 };
2299 struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
2300
2301 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
2302 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
2303
2304 trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
2305 sc.may_writepage,
2306 sc.gfp_mask);
2307
2308
2309
2310
2311
2312
2313
2314
2315 shrink_lruvec(lruvec, &sc);
2316
2317 trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
2318
2319 *nr_scanned = sc.nr_scanned;
2320 return sc.nr_reclaimed;
2321}
2322
2323unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
2324 gfp_t gfp_mask,
2325 bool noswap)
2326{
2327 struct zonelist *zonelist;
2328 unsigned long nr_reclaimed;
2329 int nid;
2330 struct scan_control sc = {
2331 .may_writepage = !laptop_mode,
2332 .may_unmap = 1,
2333 .may_swap = !noswap,
2334 .nr_to_reclaim = SWAP_CLUSTER_MAX,
2335 .order = 0,
2336 .priority = DEF_PRIORITY,
2337 .target_mem_cgroup = memcg,
2338 .nodemask = NULL,
2339 .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
2340 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
2341 };
2342 struct shrink_control shrink = {
2343 .gfp_mask = sc.gfp_mask,
2344 };
2345
2346
2347
2348
2349
2350
2351 nid = mem_cgroup_select_victim_node(memcg);
2352
2353 zonelist = NODE_DATA(nid)->node_zonelists;
2354
2355 trace_mm_vmscan_memcg_reclaim_begin(0,
2356 sc.may_writepage,
2357 sc.gfp_mask);
2358
2359 nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
2360
2361 trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
2362
2363 return nr_reclaimed;
2364}
2365#endif
2366
2367static void age_active_anon(struct zone *zone, struct scan_control *sc)
2368{
2369 struct mem_cgroup *memcg;
2370
2371 if (!total_swap_pages)
2372 return;
2373
2374 memcg = mem_cgroup_iter(NULL, NULL, NULL);
2375 do {
2376 struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
2377
2378 if (inactive_anon_is_low(lruvec))
2379 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
2380 sc, LRU_ACTIVE_ANON);
2381
2382 memcg = mem_cgroup_iter(NULL, memcg, NULL);
2383 } while (memcg);
2384}
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402static bool pgdat_balanced(pg_data_t *pgdat, unsigned long balanced_pages,
2403 int classzone_idx)
2404{
2405 unsigned long present_pages = 0;
2406 int i;
2407
2408 for (i = 0; i <= classzone_idx; i++)
2409 present_pages += pgdat->node_zones[i].present_pages;
2410
2411
2412 return balanced_pages >= (present_pages >> 2);
2413}
2414
2415
2416
2417
2418
2419
2420
2421static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, long remaining,
2422 int classzone_idx)
2423{
2424 int i;
2425 unsigned long balanced = 0;
2426 bool all_zones_ok = true;
2427
2428
2429 if (remaining)
2430 return false;
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441 if (waitqueue_active(&pgdat->pfmemalloc_wait)) {
2442 wake_up(&pgdat->pfmemalloc_wait);
2443 return false;
2444 }
2445
2446
2447 for (i = 0; i <= classzone_idx; i++) {
2448 struct zone *zone = pgdat->node_zones + i;
2449
2450 if (!populated_zone(zone))
2451 continue;
2452
2453
2454
2455
2456
2457
2458
2459 if (zone->all_unreclaimable) {
2460 balanced += zone->present_pages;
2461 continue;
2462 }
2463
2464 if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone),
2465 i, 0))
2466 all_zones_ok = false;
2467 else
2468 balanced += zone->present_pages;
2469 }
2470
2471
2472
2473
2474
2475
2476 if (order)
2477 return pgdat_balanced(pgdat, balanced, classzone_idx);
2478 else
2479 return all_zones_ok;
2480}
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503static unsigned long balance_pgdat(pg_data_t *pgdat, int order,
2504 int *classzone_idx)
2505{
2506 int all_zones_ok;
2507 unsigned long balanced;
2508 int i;
2509 int end_zone = 0;
2510 unsigned long total_scanned;
2511 struct reclaim_state *reclaim_state = current->reclaim_state;
2512 unsigned long nr_soft_reclaimed;
2513 unsigned long nr_soft_scanned;
2514 struct scan_control sc = {
2515 .gfp_mask = GFP_KERNEL,
2516 .may_unmap = 1,
2517 .may_swap = 1,
2518
2519
2520
2521
2522 .nr_to_reclaim = ULONG_MAX,
2523 .order = order,
2524 .target_mem_cgroup = NULL,
2525 };
2526 struct shrink_control shrink = {
2527 .gfp_mask = sc.gfp_mask,
2528 };
2529loop_again:
2530 total_scanned = 0;
2531 sc.priority = DEF_PRIORITY;
2532 sc.nr_reclaimed = 0;
2533 sc.may_writepage = !laptop_mode;
2534 count_vm_event(PAGEOUTRUN);
2535
2536 do {
2537 unsigned long lru_pages = 0;
2538 int has_under_min_watermark_zone = 0;
2539
2540 all_zones_ok = 1;
2541 balanced = 0;
2542
2543
2544
2545
2546
2547 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
2548 struct zone *zone = pgdat->node_zones + i;
2549
2550 if (!populated_zone(zone))
2551 continue;
2552
2553 if (zone->all_unreclaimable &&
2554 sc.priority != DEF_PRIORITY)
2555 continue;
2556
2557
2558
2559
2560
2561 age_active_anon(zone, &sc);
2562
2563
2564
2565
2566
2567
2568
2569 if (buffer_heads_over_limit && is_highmem_idx(i)) {
2570 end_zone = i;
2571 break;
2572 }
2573
2574 if (!zone_watermark_ok_safe(zone, order,
2575 high_wmark_pages(zone), 0, 0)) {
2576 end_zone = i;
2577 break;
2578 } else {
2579
2580 zone_clear_flag(zone, ZONE_CONGESTED);
2581 }
2582 }
2583 if (i < 0)
2584 goto out;
2585
2586 for (i = 0; i <= end_zone; i++) {
2587 struct zone *zone = pgdat->node_zones + i;
2588
2589 lru_pages += zone_reclaimable_pages(zone);
2590 }
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601 for (i = 0; i <= end_zone; i++) {
2602 struct zone *zone = pgdat->node_zones + i;
2603 int nr_slab, testorder;
2604 unsigned long balance_gap;
2605
2606 if (!populated_zone(zone))
2607 continue;
2608
2609 if (zone->all_unreclaimable &&
2610 sc.priority != DEF_PRIORITY)
2611 continue;
2612
2613 sc.nr_scanned = 0;
2614
2615 nr_soft_scanned = 0;
2616
2617
2618
2619 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
2620 order, sc.gfp_mask,
2621 &nr_soft_scanned);
2622 sc.nr_reclaimed += nr_soft_reclaimed;
2623 total_scanned += nr_soft_scanned;
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633 balance_gap = min(low_wmark_pages(zone),
2634 (zone->present_pages +
2635 KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
2636 KSWAPD_ZONE_BALANCE_GAP_RATIO);
2637
2638
2639
2640
2641
2642
2643
2644 testorder = order;
2645 if (COMPACTION_BUILD && order &&
2646 compaction_suitable(zone, order) !=
2647 COMPACT_SKIPPED)
2648 testorder = 0;
2649
2650 if ((buffer_heads_over_limit && is_highmem_idx(i)) ||
2651 !zone_watermark_ok_safe(zone, testorder,
2652 high_wmark_pages(zone) + balance_gap,
2653 end_zone, 0)) {
2654 shrink_zone(zone, &sc);
2655
2656 reclaim_state->reclaimed_slab = 0;
2657 nr_slab = shrink_slab(&shrink, sc.nr_scanned, lru_pages);
2658 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
2659 total_scanned += sc.nr_scanned;
2660
2661 if (nr_slab == 0 && !zone_reclaimable(zone))
2662 zone->all_unreclaimable = 1;
2663 }
2664
2665
2666
2667
2668
2669
2670 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
2671 total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2)
2672 sc.may_writepage = 1;
2673
2674 if (zone->all_unreclaimable) {
2675 if (end_zone && end_zone == i)
2676 end_zone--;
2677 continue;
2678 }
2679
2680 if (!zone_watermark_ok_safe(zone, testorder,
2681 high_wmark_pages(zone), end_zone, 0)) {
2682 all_zones_ok = 0;
2683
2684
2685
2686
2687
2688 if (!zone_watermark_ok_safe(zone, order,
2689 min_wmark_pages(zone), end_zone, 0))
2690 has_under_min_watermark_zone = 1;
2691 } else {
2692
2693
2694
2695
2696
2697
2698
2699 zone_clear_flag(zone, ZONE_CONGESTED);
2700 if (i <= *classzone_idx)
2701 balanced += zone->present_pages;
2702 }
2703
2704 }
2705
2706
2707
2708
2709
2710
2711 if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
2712 pfmemalloc_watermark_ok(pgdat))
2713 wake_up(&pgdat->pfmemalloc_wait);
2714
2715 if (all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))
2716 break;
2717
2718
2719
2720
2721 if (total_scanned && (sc.priority < DEF_PRIORITY - 2)) {
2722 if (has_under_min_watermark_zone)
2723 count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT);
2724 else
2725 congestion_wait(BLK_RW_ASYNC, HZ/10);
2726 }
2727
2728
2729
2730
2731
2732
2733
2734 if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
2735 break;
2736 } while (--sc.priority >= 0);
2737out:
2738
2739
2740
2741
2742
2743
2744 if (!(all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))) {
2745 cond_resched();
2746
2747 try_to_freeze();
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763 if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
2764 order = sc.order = 0;
2765
2766 goto loop_again;
2767 }
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777 if (order) {
2778 int zones_need_compaction = 1;
2779
2780 for (i = 0; i <= end_zone; i++) {
2781 struct zone *zone = pgdat->node_zones + i;
2782
2783 if (!populated_zone(zone))
2784 continue;
2785
2786 if (zone->all_unreclaimable &&
2787 sc.priority != DEF_PRIORITY)
2788 continue;
2789
2790
2791 if (COMPACTION_BUILD &&
2792 compaction_suitable(zone, order) == COMPACT_SKIPPED)
2793 goto loop_again;
2794
2795
2796 if (!zone_watermark_ok(zone, 0,
2797 high_wmark_pages(zone), 0, 0)) {
2798 order = sc.order = 0;
2799 goto loop_again;
2800 }
2801
2802
2803 if (zone_watermark_ok(zone, order,
2804 low_wmark_pages(zone), *classzone_idx, 0))
2805 zones_need_compaction = 0;
2806
2807
2808 zone_clear_flag(zone, ZONE_CONGESTED);
2809 }
2810
2811 if (zones_need_compaction)
2812 compact_pgdat(pgdat, order);
2813 }
2814
2815
2816
2817
2818
2819
2820
2821 *classzone_idx = end_zone;
2822 return order;
2823}
2824
2825static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx)
2826{
2827 long remaining = 0;
2828 DEFINE_WAIT(wait);
2829
2830 if (freezing(current) || kthread_should_stop())
2831 return;
2832
2833 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2834
2835
2836 if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
2837 remaining = schedule_timeout(HZ/10);
2838 finish_wait(&pgdat->kswapd_wait, &wait);
2839 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2840 }
2841
2842
2843
2844
2845
2846 if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
2847 trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857 set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
2858
2859 if (!kthread_should_stop())
2860 schedule();
2861
2862 set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
2863 } else {
2864 if (remaining)
2865 count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
2866 else
2867 count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
2868 }
2869 finish_wait(&pgdat->kswapd_wait, &wait);
2870}
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885static int kswapd(void *p)
2886{
2887 unsigned long order, new_order;
2888 unsigned balanced_order;
2889 int classzone_idx, new_classzone_idx;
2890 int balanced_classzone_idx;
2891 pg_data_t *pgdat = (pg_data_t*)p;
2892 struct task_struct *tsk = current;
2893
2894 struct reclaim_state reclaim_state = {
2895 .reclaimed_slab = 0,
2896 };
2897 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
2898
2899 lockdep_set_current_reclaim_state(GFP_KERNEL);
2900
2901 if (!cpumask_empty(cpumask))
2902 set_cpus_allowed_ptr(tsk, cpumask);
2903 current->reclaim_state = &reclaim_state;
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2918 set_freezable();
2919
2920 order = new_order = 0;
2921 balanced_order = 0;
2922 classzone_idx = new_classzone_idx = pgdat->nr_zones - 1;
2923 balanced_classzone_idx = classzone_idx;
2924 for ( ; ; ) {
2925 int ret;
2926
2927
2928
2929
2930
2931
2932 if (balanced_classzone_idx >= new_classzone_idx &&
2933 balanced_order == new_order) {
2934 new_order = pgdat->kswapd_max_order;
2935 new_classzone_idx = pgdat->classzone_idx;
2936 pgdat->kswapd_max_order = 0;
2937 pgdat->classzone_idx = pgdat->nr_zones - 1;
2938 }
2939
2940 if (order < new_order || classzone_idx > new_classzone_idx) {
2941
2942
2943
2944
2945 order = new_order;
2946 classzone_idx = new_classzone_idx;
2947 } else {
2948 kswapd_try_to_sleep(pgdat, balanced_order,
2949 balanced_classzone_idx);
2950 order = pgdat->kswapd_max_order;
2951 classzone_idx = pgdat->classzone_idx;
2952 new_order = order;
2953 new_classzone_idx = classzone_idx;
2954 pgdat->kswapd_max_order = 0;
2955 pgdat->classzone_idx = pgdat->nr_zones - 1;
2956 }
2957
2958 ret = try_to_freeze();
2959 if (kthread_should_stop())
2960 break;
2961
2962
2963
2964
2965
2966 if (!ret) {
2967 trace_mm_vmscan_kswapd_wake(pgdat->node_id, order);
2968 balanced_classzone_idx = classzone_idx;
2969 balanced_order = balance_pgdat(pgdat, order,
2970 &balanced_classzone_idx);
2971 }
2972 }
2973
2974 current->reclaim_state = NULL;
2975 return 0;
2976}
2977
2978
2979
2980
2981void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
2982{
2983 pg_data_t *pgdat;
2984
2985 if (!populated_zone(zone))
2986 return;
2987
2988 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
2989 return;
2990 pgdat = zone->zone_pgdat;
2991 if (pgdat->kswapd_max_order < order) {
2992 pgdat->kswapd_max_order = order;
2993 pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx);
2994 }
2995 if (!waitqueue_active(&pgdat->kswapd_wait))
2996 return;
2997 if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0))
2998 return;
2999
3000 trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
3001 wake_up_interruptible(&pgdat->kswapd_wait);
3002}
3003
3004
3005
3006
3007
3008
3009
3010
3011unsigned long global_reclaimable_pages(void)
3012{
3013 int nr;
3014
3015 nr = global_page_state(NR_ACTIVE_FILE) +
3016 global_page_state(NR_INACTIVE_FILE);
3017
3018 if (nr_swap_pages > 0)
3019 nr += global_page_state(NR_ACTIVE_ANON) +
3020 global_page_state(NR_INACTIVE_ANON);
3021
3022 return nr;
3023}
3024
3025unsigned long zone_reclaimable_pages(struct zone *zone)
3026{
3027 int nr;
3028
3029 nr = zone_page_state(zone, NR_ACTIVE_FILE) +
3030 zone_page_state(zone, NR_INACTIVE_FILE);
3031
3032 if (nr_swap_pages > 0)
3033 nr += zone_page_state(zone, NR_ACTIVE_ANON) +
3034 zone_page_state(zone, NR_INACTIVE_ANON);
3035
3036 return nr;
3037}
3038
3039#ifdef CONFIG_HIBERNATION
3040
3041
3042
3043
3044
3045
3046
3047
3048unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
3049{
3050 struct reclaim_state reclaim_state;
3051 struct scan_control sc = {
3052 .gfp_mask = GFP_HIGHUSER_MOVABLE,
3053 .may_swap = 1,
3054 .may_unmap = 1,
3055 .may_writepage = 1,
3056 .nr_to_reclaim = nr_to_reclaim,
3057 .hibernation_mode = 1,
3058 .order = 0,
3059 .priority = DEF_PRIORITY,
3060 };
3061 struct shrink_control shrink = {
3062 .gfp_mask = sc.gfp_mask,
3063 };
3064 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
3065 struct task_struct *p = current;
3066 unsigned long nr_reclaimed;
3067
3068 p->flags |= PF_MEMALLOC;
3069 lockdep_set_current_reclaim_state(sc.gfp_mask);
3070 reclaim_state.reclaimed_slab = 0;
3071 p->reclaim_state = &reclaim_state;
3072
3073 nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
3074
3075 p->reclaim_state = NULL;
3076 lockdep_clear_current_reclaim_state();
3077 p->flags &= ~PF_MEMALLOC;
3078
3079 return nr_reclaimed;
3080}
3081#endif
3082
3083
3084
3085
3086
3087static int __devinit cpu_callback(struct notifier_block *nfb,
3088 unsigned long action, void *hcpu)
3089{
3090 int nid;
3091
3092 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
3093 for_each_node_state(nid, N_HIGH_MEMORY) {
3094 pg_data_t *pgdat = NODE_DATA(nid);
3095 const struct cpumask *mask;
3096
3097 mask = cpumask_of_node(pgdat->node_id);
3098
3099 if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
3100
3101 set_cpus_allowed_ptr(pgdat->kswapd, mask);
3102 }
3103 }
3104 return NOTIFY_OK;
3105}
3106
3107
3108
3109
3110
3111int kswapd_run(int nid)
3112{
3113 pg_data_t *pgdat = NODE_DATA(nid);
3114 int ret = 0;
3115
3116 if (pgdat->kswapd)
3117 return 0;
3118
3119 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
3120 if (IS_ERR(pgdat->kswapd)) {
3121
3122 BUG_ON(system_state == SYSTEM_BOOTING);
3123 printk("Failed to start kswapd on node %d\n",nid);
3124 pgdat->kswapd = NULL;
3125 ret = -1;
3126 }
3127 return ret;
3128}
3129
3130
3131
3132
3133
3134void kswapd_stop(int nid)
3135{
3136 struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
3137
3138 if (kswapd) {
3139 kthread_stop(kswapd);
3140 NODE_DATA(nid)->kswapd = NULL;
3141 }
3142}
3143
3144static int __init kswapd_init(void)
3145{
3146 int nid;
3147
3148 swap_setup();
3149 for_each_node_state(nid, N_HIGH_MEMORY)
3150 kswapd_run(nid);
3151 hotcpu_notifier(cpu_callback, 0);
3152 return 0;
3153}
3154
3155module_init(kswapd_init)
3156
3157#ifdef CONFIG_NUMA
3158
3159
3160
3161
3162
3163
3164int zone_reclaim_mode __read_mostly;
3165
3166#define RECLAIM_OFF 0
3167#define RECLAIM_ZONE (1<<0)
3168#define RECLAIM_WRITE (1<<1)
3169#define RECLAIM_SWAP (1<<2)
3170
3171
3172
3173
3174
3175
3176#define ZONE_RECLAIM_PRIORITY 4
3177
3178
3179
3180
3181
3182int sysctl_min_unmapped_ratio = 1;
3183
3184
3185
3186
3187
3188int sysctl_min_slab_ratio = 5;
3189
3190static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
3191{
3192 unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
3193 unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
3194 zone_page_state(zone, NR_ACTIVE_FILE);
3195
3196
3197
3198
3199
3200
3201 return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
3202}
3203
3204
3205static long zone_pagecache_reclaimable(struct zone *zone)
3206{
3207 long nr_pagecache_reclaimable;
3208 long delta = 0;
3209
3210
3211
3212
3213
3214
3215
3216 if (zone_reclaim_mode & RECLAIM_SWAP)
3217 nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
3218 else
3219 nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
3220
3221
3222 if (!(zone_reclaim_mode & RECLAIM_WRITE))
3223 delta += zone_page_state(zone, NR_FILE_DIRTY);
3224
3225
3226 if (unlikely(delta > nr_pagecache_reclaimable))
3227 delta = nr_pagecache_reclaimable;
3228
3229 return nr_pagecache_reclaimable - delta;
3230}
3231
3232
3233
3234
3235static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
3236{
3237
3238 const unsigned long nr_pages = 1 << order;
3239 struct task_struct *p = current;
3240 struct reclaim_state reclaim_state;
3241 struct scan_control sc = {
3242 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
3243 .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
3244 .may_swap = 1,
3245 .nr_to_reclaim = max_t(unsigned long, nr_pages,
3246 SWAP_CLUSTER_MAX),
3247 .gfp_mask = gfp_mask,
3248 .order = order,
3249 .priority = ZONE_RECLAIM_PRIORITY,
3250 };
3251 struct shrink_control shrink = {
3252 .gfp_mask = sc.gfp_mask,
3253 };
3254 unsigned long nr_slab_pages0, nr_slab_pages1;
3255
3256 cond_resched();
3257
3258
3259
3260
3261
3262 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
3263 lockdep_set_current_reclaim_state(gfp_mask);
3264 reclaim_state.reclaimed_slab = 0;
3265 p->reclaim_state = &reclaim_state;
3266
3267 if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
3268
3269
3270
3271
3272 do {
3273 shrink_zone(zone, &sc);
3274 } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
3275 }
3276
3277 nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
3278 if (nr_slab_pages0 > zone->min_slab_pages) {
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289 for (;;) {
3290 unsigned long lru_pages = zone_reclaimable_pages(zone);
3291
3292
3293 if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages))
3294 break;
3295
3296
3297 nr_slab_pages1 = zone_page_state(zone,
3298 NR_SLAB_RECLAIMABLE);
3299 if (nr_slab_pages1 + nr_pages <= nr_slab_pages0)
3300 break;
3301 }
3302
3303
3304
3305
3306
3307 nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
3308 if (nr_slab_pages1 < nr_slab_pages0)
3309 sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1;
3310 }
3311
3312 p->reclaim_state = NULL;
3313 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
3314 lockdep_clear_current_reclaim_state();
3315 return sc.nr_reclaimed >= nr_pages;
3316}
3317
3318int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
3319{
3320 int node_id;
3321 int ret;
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333 if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
3334 zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
3335 return ZONE_RECLAIM_FULL;
3336
3337 if (zone->all_unreclaimable)
3338 return ZONE_RECLAIM_FULL;
3339
3340
3341
3342
3343 if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
3344 return ZONE_RECLAIM_NOSCAN;
3345
3346
3347
3348
3349
3350
3351
3352 node_id = zone_to_nid(zone);
3353 if (node_state(node_id, N_CPU) && node_id != numa_node_id())
3354 return ZONE_RECLAIM_NOSCAN;
3355
3356 if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
3357 return ZONE_RECLAIM_NOSCAN;
3358
3359 ret = __zone_reclaim(zone, gfp_mask, order);
3360 zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
3361
3362 if (!ret)
3363 count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
3364
3365 return ret;
3366}
3367#endif
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383int page_evictable(struct page *page, struct vm_area_struct *vma)
3384{
3385
3386 if (mapping_unevictable(page_mapping(page)))
3387 return 0;
3388
3389 if (PageMlocked(page) || (vma && mlocked_vma_newpage(vma, page)))
3390 return 0;
3391
3392 return 1;
3393}
3394
3395#ifdef CONFIG_SHMEM
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405void check_move_unevictable_pages(struct page **pages, int nr_pages)
3406{
3407 struct lruvec *lruvec;
3408 struct zone *zone = NULL;
3409 int pgscanned = 0;
3410 int pgrescued = 0;
3411 int i;
3412
3413 for (i = 0; i < nr_pages; i++) {
3414 struct page *page = pages[i];
3415 struct zone *pagezone;
3416
3417 pgscanned++;
3418 pagezone = page_zone(page);
3419 if (pagezone != zone) {
3420 if (zone)
3421 spin_unlock_irq(&zone->lru_lock);
3422 zone = pagezone;
3423 spin_lock_irq(&zone->lru_lock);
3424 }
3425 lruvec = mem_cgroup_page_lruvec(page, zone);
3426
3427 if (!PageLRU(page) || !PageUnevictable(page))
3428 continue;
3429
3430 if (page_evictable(page, NULL)) {
3431 enum lru_list lru = page_lru_base_type(page);
3432
3433 VM_BUG_ON(PageActive(page));
3434 ClearPageUnevictable(page);
3435 del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
3436 add_page_to_lru_list(page, lruvec, lru);
3437 pgrescued++;
3438 }
3439 }
3440
3441 if (zone) {
3442 __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
3443 __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
3444 spin_unlock_irq(&zone->lru_lock);
3445 }
3446}
3447#endif
3448
3449static void warn_scan_unevictable_pages(void)
3450{
3451 printk_once(KERN_WARNING
3452 "%s: The scan_unevictable_pages sysctl/node-interface has been "
3453 "disabled for lack of a legitimate use case. If you have "
3454 "one, please send an email to linux-mm@kvack.org.\n",
3455 current->comm);
3456}
3457
3458
3459
3460
3461
3462unsigned long scan_unevictable_pages;
3463
3464int scan_unevictable_handler(struct ctl_table *table, int write,
3465 void __user *buffer,
3466 size_t *length, loff_t *ppos)
3467{
3468 warn_scan_unevictable_pages();
3469 proc_doulongvec_minmax(table, write, buffer, length, ppos);
3470 scan_unevictable_pages = 0;
3471 return 0;
3472}
3473
3474#ifdef CONFIG_NUMA
3475
3476
3477
3478
3479
3480static ssize_t read_scan_unevictable_node(struct device *dev,
3481 struct device_attribute *attr,
3482 char *buf)
3483{
3484 warn_scan_unevictable_pages();
3485 return sprintf(buf, "0\n");
3486}
3487
3488static ssize_t write_scan_unevictable_node(struct device *dev,
3489 struct device_attribute *attr,
3490 const char *buf, size_t count)
3491{
3492 warn_scan_unevictable_pages();
3493 return 1;
3494}
3495
3496
3497static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
3498 read_scan_unevictable_node,
3499 write_scan_unevictable_node);
3500
3501int scan_unevictable_register_node(struct node *node)
3502{
3503 return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages);
3504}
3505
3506void scan_unevictable_unregister_node(struct node *node)
3507{
3508 device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages);
3509}
3510#endif
3511