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