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
2181static void throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
2182 nodemask_t *nodemask)
2183{
2184 struct zone *zone;
2185 int high_zoneidx = gfp_zone(gfp_mask);
2186 pg_data_t *pgdat;
2187
2188
2189
2190
2191
2192
2193
2194
2195 if (current->flags & PF_KTHREAD)
2196 return;
2197
2198
2199 first_zones_zonelist(zonelist, high_zoneidx, NULL, &zone);
2200 pgdat = zone->zone_pgdat;
2201 if (pfmemalloc_watermark_ok(pgdat))
2202 return;
2203
2204
2205 count_vm_event(PGSCAN_DIRECT_THROTTLE);
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215 if (!(gfp_mask & __GFP_FS)) {
2216 wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
2217 pfmemalloc_watermark_ok(pgdat), HZ);
2218 return;
2219 }
2220
2221
2222 wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
2223 pfmemalloc_watermark_ok(pgdat));
2224}
2225
2226unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
2227 gfp_t gfp_mask, nodemask_t *nodemask)
2228{
2229 unsigned long nr_reclaimed;
2230 struct scan_control sc = {
2231 .gfp_mask = gfp_mask,
2232 .may_writepage = !laptop_mode,
2233 .nr_to_reclaim = SWAP_CLUSTER_MAX,
2234 .may_unmap = 1,
2235 .may_swap = 1,
2236 .order = order,
2237 .priority = DEF_PRIORITY,
2238 .target_mem_cgroup = NULL,
2239 .nodemask = nodemask,
2240 };
2241 struct shrink_control shrink = {
2242 .gfp_mask = sc.gfp_mask,
2243 };
2244
2245 throttle_direct_reclaim(gfp_mask, zonelist, nodemask);
2246
2247
2248
2249
2250
2251 if (fatal_signal_pending(current))
2252 return 1;
2253
2254 trace_mm_vmscan_direct_reclaim_begin(order,
2255 sc.may_writepage,
2256 gfp_mask);
2257
2258 nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
2259
2260 trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
2261
2262 return nr_reclaimed;
2263}
2264
2265#ifdef CONFIG_MEMCG
2266
2267unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg,
2268 gfp_t gfp_mask, bool noswap,
2269 struct zone *zone,
2270 unsigned long *nr_scanned)
2271{
2272 struct scan_control sc = {
2273 .nr_scanned = 0,
2274 .nr_to_reclaim = SWAP_CLUSTER_MAX,
2275 .may_writepage = !laptop_mode,
2276 .may_unmap = 1,
2277 .may_swap = !noswap,
2278 .order = 0,
2279 .priority = 0,
2280 .target_mem_cgroup = memcg,
2281 };
2282 struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
2283
2284 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
2285 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
2286
2287 trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
2288 sc.may_writepage,
2289 sc.gfp_mask);
2290
2291
2292
2293
2294
2295
2296
2297
2298 shrink_lruvec(lruvec, &sc);
2299
2300 trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
2301
2302 *nr_scanned = sc.nr_scanned;
2303 return sc.nr_reclaimed;
2304}
2305
2306unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
2307 gfp_t gfp_mask,
2308 bool noswap)
2309{
2310 struct zonelist *zonelist;
2311 unsigned long nr_reclaimed;
2312 int nid;
2313 struct scan_control sc = {
2314 .may_writepage = !laptop_mode,
2315 .may_unmap = 1,
2316 .may_swap = !noswap,
2317 .nr_to_reclaim = SWAP_CLUSTER_MAX,
2318 .order = 0,
2319 .priority = DEF_PRIORITY,
2320 .target_mem_cgroup = memcg,
2321 .nodemask = NULL,
2322 .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
2323 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
2324 };
2325 struct shrink_control shrink = {
2326 .gfp_mask = sc.gfp_mask,
2327 };
2328
2329
2330
2331
2332
2333
2334 nid = mem_cgroup_select_victim_node(memcg);
2335
2336 zonelist = NODE_DATA(nid)->node_zonelists;
2337
2338 trace_mm_vmscan_memcg_reclaim_begin(0,
2339 sc.may_writepage,
2340 sc.gfp_mask);
2341
2342 nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
2343
2344 trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
2345
2346 return nr_reclaimed;
2347}
2348#endif
2349
2350static void age_active_anon(struct zone *zone, struct scan_control *sc)
2351{
2352 struct mem_cgroup *memcg;
2353
2354 if (!total_swap_pages)
2355 return;
2356
2357 memcg = mem_cgroup_iter(NULL, NULL, NULL);
2358 do {
2359 struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg);
2360
2361 if (inactive_anon_is_low(lruvec))
2362 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
2363 sc, LRU_ACTIVE_ANON);
2364
2365 memcg = mem_cgroup_iter(NULL, memcg, NULL);
2366 } while (memcg);
2367}
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385static bool pgdat_balanced(pg_data_t *pgdat, unsigned long balanced_pages,
2386 int classzone_idx)
2387{
2388 unsigned long present_pages = 0;
2389 int i;
2390
2391 for (i = 0; i <= classzone_idx; i++)
2392 present_pages += pgdat->node_zones[i].present_pages;
2393
2394
2395 return balanced_pages >= (present_pages >> 2);
2396}
2397
2398
2399
2400
2401
2402
2403
2404static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, long remaining,
2405 int classzone_idx)
2406{
2407 int i;
2408 unsigned long balanced = 0;
2409 bool all_zones_ok = true;
2410
2411
2412 if (remaining)
2413 return false;
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424 if (waitqueue_active(&pgdat->pfmemalloc_wait)) {
2425 wake_up(&pgdat->pfmemalloc_wait);
2426 return false;
2427 }
2428
2429
2430 for (i = 0; i <= classzone_idx; i++) {
2431 struct zone *zone = pgdat->node_zones + i;
2432
2433 if (!populated_zone(zone))
2434 continue;
2435
2436
2437
2438
2439
2440
2441
2442 if (zone->all_unreclaimable) {
2443 balanced += zone->present_pages;
2444 continue;
2445 }
2446
2447 if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone),
2448 i, 0))
2449 all_zones_ok = false;
2450 else
2451 balanced += zone->present_pages;
2452 }
2453
2454
2455
2456
2457
2458
2459 if (order)
2460 return pgdat_balanced(pgdat, balanced, classzone_idx);
2461 else
2462 return all_zones_ok;
2463}
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486static unsigned long balance_pgdat(pg_data_t *pgdat, int order,
2487 int *classzone_idx)
2488{
2489 int all_zones_ok;
2490 unsigned long balanced;
2491 int i;
2492 int end_zone = 0;
2493 unsigned long total_scanned;
2494 struct reclaim_state *reclaim_state = current->reclaim_state;
2495 unsigned long nr_soft_reclaimed;
2496 unsigned long nr_soft_scanned;
2497 struct scan_control sc = {
2498 .gfp_mask = GFP_KERNEL,
2499 .may_unmap = 1,
2500 .may_swap = 1,
2501
2502
2503
2504
2505 .nr_to_reclaim = ULONG_MAX,
2506 .order = order,
2507 .target_mem_cgroup = NULL,
2508 };
2509 struct shrink_control shrink = {
2510 .gfp_mask = sc.gfp_mask,
2511 };
2512loop_again:
2513 total_scanned = 0;
2514 sc.priority = DEF_PRIORITY;
2515 sc.nr_reclaimed = 0;
2516 sc.may_writepage = !laptop_mode;
2517 count_vm_event(PAGEOUTRUN);
2518
2519 do {
2520 unsigned long lru_pages = 0;
2521 int has_under_min_watermark_zone = 0;
2522
2523 all_zones_ok = 1;
2524 balanced = 0;
2525
2526
2527
2528
2529
2530 for (i = pgdat->nr_zones - 1; i >= 0; i--) {
2531 struct zone *zone = pgdat->node_zones + i;
2532
2533 if (!populated_zone(zone))
2534 continue;
2535
2536 if (zone->all_unreclaimable &&
2537 sc.priority != DEF_PRIORITY)
2538 continue;
2539
2540
2541
2542
2543
2544 age_active_anon(zone, &sc);
2545
2546
2547
2548
2549
2550
2551
2552 if (buffer_heads_over_limit && is_highmem_idx(i)) {
2553 end_zone = i;
2554 break;
2555 }
2556
2557 if (!zone_watermark_ok_safe(zone, order,
2558 high_wmark_pages(zone), 0, 0)) {
2559 end_zone = i;
2560 break;
2561 } else {
2562
2563 zone_clear_flag(zone, ZONE_CONGESTED);
2564 }
2565 }
2566 if (i < 0)
2567 goto out;
2568
2569 for (i = 0; i <= end_zone; i++) {
2570 struct zone *zone = pgdat->node_zones + i;
2571
2572 lru_pages += zone_reclaimable_pages(zone);
2573 }
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584 for (i = 0; i <= end_zone; i++) {
2585 struct zone *zone = pgdat->node_zones + i;
2586 int nr_slab, testorder;
2587 unsigned long balance_gap;
2588
2589 if (!populated_zone(zone))
2590 continue;
2591
2592 if (zone->all_unreclaimable &&
2593 sc.priority != DEF_PRIORITY)
2594 continue;
2595
2596 sc.nr_scanned = 0;
2597
2598 nr_soft_scanned = 0;
2599
2600
2601
2602 nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone,
2603 order, sc.gfp_mask,
2604 &nr_soft_scanned);
2605 sc.nr_reclaimed += nr_soft_reclaimed;
2606 total_scanned += nr_soft_scanned;
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616 balance_gap = min(low_wmark_pages(zone),
2617 (zone->present_pages +
2618 KSWAPD_ZONE_BALANCE_GAP_RATIO-1) /
2619 KSWAPD_ZONE_BALANCE_GAP_RATIO);
2620
2621
2622
2623
2624
2625
2626
2627 testorder = order;
2628 if (COMPACTION_BUILD && order &&
2629 compaction_suitable(zone, order) !=
2630 COMPACT_SKIPPED)
2631 testorder = 0;
2632
2633 if ((buffer_heads_over_limit && is_highmem_idx(i)) ||
2634 !zone_watermark_ok_safe(zone, testorder,
2635 high_wmark_pages(zone) + balance_gap,
2636 end_zone, 0)) {
2637 shrink_zone(zone, &sc);
2638
2639 reclaim_state->reclaimed_slab = 0;
2640 nr_slab = shrink_slab(&shrink, sc.nr_scanned, lru_pages);
2641 sc.nr_reclaimed += reclaim_state->reclaimed_slab;
2642 total_scanned += sc.nr_scanned;
2643
2644 if (nr_slab == 0 && !zone_reclaimable(zone))
2645 zone->all_unreclaimable = 1;
2646 }
2647
2648
2649
2650
2651
2652
2653 if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
2654 total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2)
2655 sc.may_writepage = 1;
2656
2657 if (zone->all_unreclaimable) {
2658 if (end_zone && end_zone == i)
2659 end_zone--;
2660 continue;
2661 }
2662
2663 if (!zone_watermark_ok_safe(zone, testorder,
2664 high_wmark_pages(zone), end_zone, 0)) {
2665 all_zones_ok = 0;
2666
2667
2668
2669
2670
2671 if (!zone_watermark_ok_safe(zone, order,
2672 min_wmark_pages(zone), end_zone, 0))
2673 has_under_min_watermark_zone = 1;
2674 } else {
2675
2676
2677
2678
2679
2680
2681
2682 zone_clear_flag(zone, ZONE_CONGESTED);
2683 if (i <= *classzone_idx)
2684 balanced += zone->present_pages;
2685 }
2686
2687 }
2688
2689
2690
2691
2692
2693
2694 if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
2695 pfmemalloc_watermark_ok(pgdat))
2696 wake_up(&pgdat->pfmemalloc_wait);
2697
2698 if (all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))
2699 break;
2700
2701
2702
2703
2704 if (total_scanned && (sc.priority < DEF_PRIORITY - 2)) {
2705 if (has_under_min_watermark_zone)
2706 count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT);
2707 else
2708 congestion_wait(BLK_RW_ASYNC, HZ/10);
2709 }
2710
2711
2712
2713
2714
2715
2716
2717 if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX)
2718 break;
2719 } while (--sc.priority >= 0);
2720out:
2721
2722
2723
2724
2725
2726
2727 if (!(all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))) {
2728 cond_resched();
2729
2730 try_to_freeze();
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746 if (sc.nr_reclaimed < SWAP_CLUSTER_MAX)
2747 order = sc.order = 0;
2748
2749 goto loop_again;
2750 }
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760 if (order) {
2761 int zones_need_compaction = 1;
2762
2763 for (i = 0; i <= end_zone; i++) {
2764 struct zone *zone = pgdat->node_zones + i;
2765
2766 if (!populated_zone(zone))
2767 continue;
2768
2769 if (zone->all_unreclaimable &&
2770 sc.priority != DEF_PRIORITY)
2771 continue;
2772
2773
2774 if (COMPACTION_BUILD &&
2775 compaction_suitable(zone, order) == COMPACT_SKIPPED)
2776 goto loop_again;
2777
2778
2779 if (!zone_watermark_ok(zone, 0,
2780 high_wmark_pages(zone), 0, 0)) {
2781 order = sc.order = 0;
2782 goto loop_again;
2783 }
2784
2785
2786 if (zone_watermark_ok(zone, order,
2787 low_wmark_pages(zone), *classzone_idx, 0))
2788 zones_need_compaction = 0;
2789
2790
2791 zone_clear_flag(zone, ZONE_CONGESTED);
2792 }
2793
2794 if (zones_need_compaction)
2795 compact_pgdat(pgdat, order);
2796 }
2797
2798
2799
2800
2801
2802
2803
2804 *classzone_idx = end_zone;
2805 return order;
2806}
2807
2808static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx)
2809{
2810 long remaining = 0;
2811 DEFINE_WAIT(wait);
2812
2813 if (freezing(current) || kthread_should_stop())
2814 return;
2815
2816 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2817
2818
2819 if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
2820 remaining = schedule_timeout(HZ/10);
2821 finish_wait(&pgdat->kswapd_wait, &wait);
2822 prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
2823 }
2824
2825
2826
2827
2828
2829 if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) {
2830 trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840 set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
2841
2842 if (!kthread_should_stop())
2843 schedule();
2844
2845 set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
2846 } else {
2847 if (remaining)
2848 count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
2849 else
2850 count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
2851 }
2852 finish_wait(&pgdat->kswapd_wait, &wait);
2853}
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868static int kswapd(void *p)
2869{
2870 unsigned long order, new_order;
2871 unsigned balanced_order;
2872 int classzone_idx, new_classzone_idx;
2873 int balanced_classzone_idx;
2874 pg_data_t *pgdat = (pg_data_t*)p;
2875 struct task_struct *tsk = current;
2876
2877 struct reclaim_state reclaim_state = {
2878 .reclaimed_slab = 0,
2879 };
2880 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
2881
2882 lockdep_set_current_reclaim_state(GFP_KERNEL);
2883
2884 if (!cpumask_empty(cpumask))
2885 set_cpus_allowed_ptr(tsk, cpumask);
2886 current->reclaim_state = &reclaim_state;
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900 tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2901 set_freezable();
2902
2903 order = new_order = 0;
2904 balanced_order = 0;
2905 classzone_idx = new_classzone_idx = pgdat->nr_zones - 1;
2906 balanced_classzone_idx = classzone_idx;
2907 for ( ; ; ) {
2908 int ret;
2909
2910
2911
2912
2913
2914
2915 if (balanced_classzone_idx >= new_classzone_idx &&
2916 balanced_order == new_order) {
2917 new_order = pgdat->kswapd_max_order;
2918 new_classzone_idx = pgdat->classzone_idx;
2919 pgdat->kswapd_max_order = 0;
2920 pgdat->classzone_idx = pgdat->nr_zones - 1;
2921 }
2922
2923 if (order < new_order || classzone_idx > new_classzone_idx) {
2924
2925
2926
2927
2928 order = new_order;
2929 classzone_idx = new_classzone_idx;
2930 } else {
2931 kswapd_try_to_sleep(pgdat, balanced_order,
2932 balanced_classzone_idx);
2933 order = pgdat->kswapd_max_order;
2934 classzone_idx = pgdat->classzone_idx;
2935 new_order = order;
2936 new_classzone_idx = classzone_idx;
2937 pgdat->kswapd_max_order = 0;
2938 pgdat->classzone_idx = pgdat->nr_zones - 1;
2939 }
2940
2941 ret = try_to_freeze();
2942 if (kthread_should_stop())
2943 break;
2944
2945
2946
2947
2948
2949 if (!ret) {
2950 trace_mm_vmscan_kswapd_wake(pgdat->node_id, order);
2951 balanced_classzone_idx = classzone_idx;
2952 balanced_order = balance_pgdat(pgdat, order,
2953 &balanced_classzone_idx);
2954 }
2955 }
2956 return 0;
2957}
2958
2959
2960
2961
2962void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
2963{
2964 pg_data_t *pgdat;
2965
2966 if (!populated_zone(zone))
2967 return;
2968
2969 if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL))
2970 return;
2971 pgdat = zone->zone_pgdat;
2972 if (pgdat->kswapd_max_order < order) {
2973 pgdat->kswapd_max_order = order;
2974 pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx);
2975 }
2976 if (!waitqueue_active(&pgdat->kswapd_wait))
2977 return;
2978 if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0))
2979 return;
2980
2981 trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order);
2982 wake_up_interruptible(&pgdat->kswapd_wait);
2983}
2984
2985
2986
2987
2988
2989
2990
2991
2992unsigned long global_reclaimable_pages(void)
2993{
2994 int nr;
2995
2996 nr = global_page_state(NR_ACTIVE_FILE) +
2997 global_page_state(NR_INACTIVE_FILE);
2998
2999 if (nr_swap_pages > 0)
3000 nr += global_page_state(NR_ACTIVE_ANON) +
3001 global_page_state(NR_INACTIVE_ANON);
3002
3003 return nr;
3004}
3005
3006unsigned long zone_reclaimable_pages(struct zone *zone)
3007{
3008 int nr;
3009
3010 nr = zone_page_state(zone, NR_ACTIVE_FILE) +
3011 zone_page_state(zone, NR_INACTIVE_FILE);
3012
3013 if (nr_swap_pages > 0)
3014 nr += zone_page_state(zone, NR_ACTIVE_ANON) +
3015 zone_page_state(zone, NR_INACTIVE_ANON);
3016
3017 return nr;
3018}
3019
3020#ifdef CONFIG_HIBERNATION
3021
3022
3023
3024
3025
3026
3027
3028
3029unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
3030{
3031 struct reclaim_state reclaim_state;
3032 struct scan_control sc = {
3033 .gfp_mask = GFP_HIGHUSER_MOVABLE,
3034 .may_swap = 1,
3035 .may_unmap = 1,
3036 .may_writepage = 1,
3037 .nr_to_reclaim = nr_to_reclaim,
3038 .hibernation_mode = 1,
3039 .order = 0,
3040 .priority = DEF_PRIORITY,
3041 };
3042 struct shrink_control shrink = {
3043 .gfp_mask = sc.gfp_mask,
3044 };
3045 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
3046 struct task_struct *p = current;
3047 unsigned long nr_reclaimed;
3048
3049 p->flags |= PF_MEMALLOC;
3050 lockdep_set_current_reclaim_state(sc.gfp_mask);
3051 reclaim_state.reclaimed_slab = 0;
3052 p->reclaim_state = &reclaim_state;
3053
3054 nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink);
3055
3056 p->reclaim_state = NULL;
3057 lockdep_clear_current_reclaim_state();
3058 p->flags &= ~PF_MEMALLOC;
3059
3060 return nr_reclaimed;
3061}
3062#endif
3063
3064
3065
3066
3067
3068static int __devinit cpu_callback(struct notifier_block *nfb,
3069 unsigned long action, void *hcpu)
3070{
3071 int nid;
3072
3073 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
3074 for_each_node_state(nid, N_HIGH_MEMORY) {
3075 pg_data_t *pgdat = NODE_DATA(nid);
3076 const struct cpumask *mask;
3077
3078 mask = cpumask_of_node(pgdat->node_id);
3079
3080 if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
3081
3082 set_cpus_allowed_ptr(pgdat->kswapd, mask);
3083 }
3084 }
3085 return NOTIFY_OK;
3086}
3087
3088
3089
3090
3091
3092int kswapd_run(int nid)
3093{
3094 pg_data_t *pgdat = NODE_DATA(nid);
3095 int ret = 0;
3096
3097 if (pgdat->kswapd)
3098 return 0;
3099
3100 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
3101 if (IS_ERR(pgdat->kswapd)) {
3102
3103 BUG_ON(system_state == SYSTEM_BOOTING);
3104 printk("Failed to start kswapd on node %d\n",nid);
3105 pgdat->kswapd = NULL;
3106 ret = -1;
3107 }
3108 return ret;
3109}
3110
3111
3112
3113
3114
3115void kswapd_stop(int nid)
3116{
3117 struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
3118
3119 if (kswapd) {
3120 kthread_stop(kswapd);
3121 NODE_DATA(nid)->kswapd = NULL;
3122 }
3123}
3124
3125static int __init kswapd_init(void)
3126{
3127 int nid;
3128
3129 swap_setup();
3130 for_each_node_state(nid, N_HIGH_MEMORY)
3131 kswapd_run(nid);
3132 hotcpu_notifier(cpu_callback, 0);
3133 return 0;
3134}
3135
3136module_init(kswapd_init)
3137
3138#ifdef CONFIG_NUMA
3139
3140
3141
3142
3143
3144
3145int zone_reclaim_mode __read_mostly;
3146
3147#define RECLAIM_OFF 0
3148#define RECLAIM_ZONE (1<<0)
3149#define RECLAIM_WRITE (1<<1)
3150#define RECLAIM_SWAP (1<<2)
3151
3152
3153
3154
3155
3156
3157#define ZONE_RECLAIM_PRIORITY 4
3158
3159
3160
3161
3162
3163int sysctl_min_unmapped_ratio = 1;
3164
3165
3166
3167
3168
3169int sysctl_min_slab_ratio = 5;
3170
3171static inline unsigned long zone_unmapped_file_pages(struct zone *zone)
3172{
3173 unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED);
3174 unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) +
3175 zone_page_state(zone, NR_ACTIVE_FILE);
3176
3177
3178
3179
3180
3181
3182 return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
3183}
3184
3185
3186static long zone_pagecache_reclaimable(struct zone *zone)
3187{
3188 long nr_pagecache_reclaimable;
3189 long delta = 0;
3190
3191
3192
3193
3194
3195
3196
3197 if (zone_reclaim_mode & RECLAIM_SWAP)
3198 nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES);
3199 else
3200 nr_pagecache_reclaimable = zone_unmapped_file_pages(zone);
3201
3202
3203 if (!(zone_reclaim_mode & RECLAIM_WRITE))
3204 delta += zone_page_state(zone, NR_FILE_DIRTY);
3205
3206
3207 if (unlikely(delta > nr_pagecache_reclaimable))
3208 delta = nr_pagecache_reclaimable;
3209
3210 return nr_pagecache_reclaimable - delta;
3211}
3212
3213
3214
3215
3216static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
3217{
3218
3219 const unsigned long nr_pages = 1 << order;
3220 struct task_struct *p = current;
3221 struct reclaim_state reclaim_state;
3222 struct scan_control sc = {
3223 .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE),
3224 .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP),
3225 .may_swap = 1,
3226 .nr_to_reclaim = max_t(unsigned long, nr_pages,
3227 SWAP_CLUSTER_MAX),
3228 .gfp_mask = gfp_mask,
3229 .order = order,
3230 .priority = ZONE_RECLAIM_PRIORITY,
3231 };
3232 struct shrink_control shrink = {
3233 .gfp_mask = sc.gfp_mask,
3234 };
3235 unsigned long nr_slab_pages0, nr_slab_pages1;
3236
3237 cond_resched();
3238
3239
3240
3241
3242
3243 p->flags |= PF_MEMALLOC | PF_SWAPWRITE;
3244 lockdep_set_current_reclaim_state(gfp_mask);
3245 reclaim_state.reclaimed_slab = 0;
3246 p->reclaim_state = &reclaim_state;
3247
3248 if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) {
3249
3250
3251
3252
3253 do {
3254 shrink_zone(zone, &sc);
3255 } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
3256 }
3257
3258 nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
3259 if (nr_slab_pages0 > zone->min_slab_pages) {
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270 for (;;) {
3271 unsigned long lru_pages = zone_reclaimable_pages(zone);
3272
3273
3274 if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages))
3275 break;
3276
3277
3278 nr_slab_pages1 = zone_page_state(zone,
3279 NR_SLAB_RECLAIMABLE);
3280 if (nr_slab_pages1 + nr_pages <= nr_slab_pages0)
3281 break;
3282 }
3283
3284
3285
3286
3287
3288 nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE);
3289 if (nr_slab_pages1 < nr_slab_pages0)
3290 sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1;
3291 }
3292
3293 p->reclaim_state = NULL;
3294 current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE);
3295 lockdep_clear_current_reclaim_state();
3296 return sc.nr_reclaimed >= nr_pages;
3297}
3298
3299int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order)
3300{
3301 int node_id;
3302 int ret;
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314 if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages &&
3315 zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages)
3316 return ZONE_RECLAIM_FULL;
3317
3318 if (zone->all_unreclaimable)
3319 return ZONE_RECLAIM_FULL;
3320
3321
3322
3323
3324 if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC))
3325 return ZONE_RECLAIM_NOSCAN;
3326
3327
3328
3329
3330
3331
3332
3333 node_id = zone_to_nid(zone);
3334 if (node_state(node_id, N_CPU) && node_id != numa_node_id())
3335 return ZONE_RECLAIM_NOSCAN;
3336
3337 if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED))
3338 return ZONE_RECLAIM_NOSCAN;
3339
3340 ret = __zone_reclaim(zone, gfp_mask, order);
3341 zone_clear_flag(zone, ZONE_RECLAIM_LOCKED);
3342
3343 if (!ret)
3344 count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
3345
3346 return ret;
3347}
3348#endif
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364int page_evictable(struct page *page, struct vm_area_struct *vma)
3365{
3366
3367 if (mapping_unevictable(page_mapping(page)))
3368 return 0;
3369
3370 if (PageMlocked(page) || (vma && mlocked_vma_newpage(vma, page)))
3371 return 0;
3372
3373 return 1;
3374}
3375
3376#ifdef CONFIG_SHMEM
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386void check_move_unevictable_pages(struct page **pages, int nr_pages)
3387{
3388 struct lruvec *lruvec;
3389 struct zone *zone = NULL;
3390 int pgscanned = 0;
3391 int pgrescued = 0;
3392 int i;
3393
3394 for (i = 0; i < nr_pages; i++) {
3395 struct page *page = pages[i];
3396 struct zone *pagezone;
3397
3398 pgscanned++;
3399 pagezone = page_zone(page);
3400 if (pagezone != zone) {
3401 if (zone)
3402 spin_unlock_irq(&zone->lru_lock);
3403 zone = pagezone;
3404 spin_lock_irq(&zone->lru_lock);
3405 }
3406 lruvec = mem_cgroup_page_lruvec(page, zone);
3407
3408 if (!PageLRU(page) || !PageUnevictable(page))
3409 continue;
3410
3411 if (page_evictable(page, NULL)) {
3412 enum lru_list lru = page_lru_base_type(page);
3413
3414 VM_BUG_ON(PageActive(page));
3415 ClearPageUnevictable(page);
3416 del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
3417 add_page_to_lru_list(page, lruvec, lru);
3418 pgrescued++;
3419 }
3420 }
3421
3422 if (zone) {
3423 __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
3424 __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
3425 spin_unlock_irq(&zone->lru_lock);
3426 }
3427}
3428#endif
3429
3430static void warn_scan_unevictable_pages(void)
3431{
3432 printk_once(KERN_WARNING
3433 "%s: The scan_unevictable_pages sysctl/node-interface has been "
3434 "disabled for lack of a legitimate use case. If you have "
3435 "one, please send an email to linux-mm@kvack.org.\n",
3436 current->comm);
3437}
3438
3439
3440
3441
3442
3443unsigned long scan_unevictable_pages;
3444
3445int scan_unevictable_handler(struct ctl_table *table, int write,
3446 void __user *buffer,
3447 size_t *length, loff_t *ppos)
3448{
3449 warn_scan_unevictable_pages();
3450 proc_doulongvec_minmax(table, write, buffer, length, ppos);
3451 scan_unevictable_pages = 0;
3452 return 0;
3453}
3454
3455#ifdef CONFIG_NUMA
3456
3457
3458
3459
3460
3461static ssize_t read_scan_unevictable_node(struct device *dev,
3462 struct device_attribute *attr,
3463 char *buf)
3464{
3465 warn_scan_unevictable_pages();
3466 return sprintf(buf, "0\n");
3467}
3468
3469static ssize_t write_scan_unevictable_node(struct device *dev,
3470 struct device_attribute *attr,
3471 const char *buf, size_t count)
3472{
3473 warn_scan_unevictable_pages();
3474 return 1;
3475}
3476
3477
3478static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR,
3479 read_scan_unevictable_node,
3480 write_scan_unevictable_node);
3481
3482int scan_unevictable_register_node(struct node *node)
3483{
3484 return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages);
3485}
3486
3487void scan_unevictable_unregister_node(struct node *node)
3488{
3489 device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages);
3490}
3491#endif
3492