hc
2024-05-10 748e4f3d702def1a4bff191e0cf93b6a05340f01
kernel/mm/vmscan.c
....@@ -63,6 +63,12 @@
6363 #define CREATE_TRACE_POINTS
6464 #include <trace/events/vmscan.h>
6565
66
+#undef CREATE_TRACE_POINTS
67
+#include <trace/hooks/vmscan.h>
68
+
69
+EXPORT_TRACEPOINT_SYMBOL_GPL(mm_vmscan_direct_reclaim_begin);
70
+EXPORT_TRACEPOINT_SYMBOL_GPL(mm_vmscan_direct_reclaim_end);
71
+
6672 struct scan_control {
6773 /* How many pages shrink_list() should reclaim */
6874 unsigned long nr_to_reclaim;
....@@ -79,6 +85,19 @@
7985 */
8086 struct mem_cgroup *target_mem_cgroup;
8187
88
+ /*
89
+ * Scan pressure balancing between anon and file LRUs
90
+ */
91
+ unsigned long anon_cost;
92
+ unsigned long file_cost;
93
+
94
+ /* Can active pages be deactivated as part of reclaim? */
95
+#define DEACTIVATE_ANON 1
96
+#define DEACTIVATE_FILE 2
97
+ unsigned int may_deactivate:2;
98
+ unsigned int force_deactivate:1;
99
+ unsigned int skipped_deactivate:1;
100
+
82101 /* Writepage batching in laptop mode; RECLAIM_WRITE */
83102 unsigned int may_writepage:1;
84103
....@@ -89,9 +108,12 @@
89108 unsigned int may_swap:1;
90109
91110 /*
92
- * Cgroups are not reclaimed below their configured memory.low,
93
- * unless we threaten to OOM. If any cgroups are skipped due to
94
- * memory.low and nothing was reclaimed, go back for memory.low.
111
+ * Cgroup memory below memory.low is protected as long as we
112
+ * don't threaten to OOM. If any cgroup is reclaimed at
113
+ * reduced force or passed over entirely due to its memory.low
114
+ * setting (memcg_low_skipped), and nothing is reclaimed as a
115
+ * result, then go back for one more cycle that reclaims the protected
116
+ * memory (memcg_low_reclaim) to avert OOM.
95117 */
96118 unsigned int memcg_low_reclaim:1;
97119 unsigned int memcg_low_skipped:1;
....@@ -100,6 +122,12 @@
100122
101123 /* One of the zones is ready for compaction */
102124 unsigned int compaction_ready:1;
125
+
126
+ /* There is easily reclaimable cold cache in the current node */
127
+ unsigned int cache_trim_mode:1;
128
+
129
+ /* The file pages on the current node are dangerously low */
130
+ unsigned int file_is_tiny:1;
103131
104132 /* Allocation order */
105133 s8 order;
....@@ -128,21 +156,10 @@
128156 unsigned int file_taken;
129157 unsigned int taken;
130158 } nr;
131
-};
132159
133
-#ifdef ARCH_HAS_PREFETCH
134
-#define prefetch_prev_lru_page(_page, _base, _field) \
135
- do { \
136
- if ((_page)->lru.prev != _base) { \
137
- struct page *prev; \
138
- \
139
- prev = lru_to_page(&(_page->lru)); \
140
- prefetch(&prev->_field); \
141
- } \
142
- } while (0)
143
-#else
144
-#define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
145
-#endif
160
+ /* for recording the reclaimed slab by now */
161
+ struct reclaim_state reclaim_state;
162
+};
146163
147164 #ifdef ARCH_HAS_PREFETCHW
148165 #define prefetchw_prev_lru_page(_page, _base, _field) \
....@@ -159,20 +176,43 @@
159176 #endif
160177
161178 /*
162
- * From 0 .. 100. Higher means more swappy.
179
+ * From 0 .. 200. Higher means more swappy.
163180 */
164181 int vm_swappiness = 60;
165
-/*
166
- * The total number of pages which are beyond the high watermark within all
167
- * zones.
168
- */
169
-unsigned long vm_total_pages;
182
+
183
+#define DEF_KSWAPD_THREADS_PER_NODE 1
184
+static int kswapd_threads = DEF_KSWAPD_THREADS_PER_NODE;
185
+static int __init kswapd_per_node_setup(char *str)
186
+{
187
+ int tmp;
188
+
189
+ if (kstrtoint(str, 0, &tmp) < 0)
190
+ return 0;
191
+
192
+ if (tmp > MAX_KSWAPD_THREADS || tmp <= 0)
193
+ return 0;
194
+
195
+ kswapd_threads = tmp;
196
+ return 1;
197
+}
198
+__setup("kswapd_per_node=", kswapd_per_node_setup);
199
+
200
+static void set_task_reclaim_state(struct task_struct *task,
201
+ struct reclaim_state *rs)
202
+{
203
+ /* Check for an overwrite */
204
+ WARN_ON_ONCE(rs && task->reclaim_state);
205
+
206
+ /* Check for the nulling of an already-nulled member */
207
+ WARN_ON_ONCE(!rs && !task->reclaim_state);
208
+
209
+ task->reclaim_state = rs;
210
+}
170211
171212 static LIST_HEAD(shrinker_list);
172213 static DECLARE_RWSEM(shrinker_rwsem);
173214
174
-#ifdef CONFIG_MEMCG_KMEM
175
-
215
+#ifdef CONFIG_MEMCG
176216 /*
177217 * We allow subsystems to populate their shrinker-related
178218 * LRU lists before register_shrinker_prepared() is called
....@@ -224,25 +264,14 @@
224264 idr_remove(&shrinker_idr, id);
225265 up_write(&shrinker_rwsem);
226266 }
227
-#else /* CONFIG_MEMCG_KMEM */
228
-static int prealloc_memcg_shrinker(struct shrinker *shrinker)
229
-{
230
- return 0;
231
-}
232267
233
-static void unregister_memcg_shrinker(struct shrinker *shrinker)
268
+static bool cgroup_reclaim(struct scan_control *sc)
234269 {
235
-}
236
-#endif /* CONFIG_MEMCG_KMEM */
237
-
238
-#ifdef CONFIG_MEMCG
239
-static bool global_reclaim(struct scan_control *sc)
240
-{
241
- return !sc->target_mem_cgroup;
270
+ return sc->target_mem_cgroup;
242271 }
243272
244273 /**
245
- * sane_reclaim - is the usual dirty throttling mechanism operational?
274
+ * writeback_throttling_sane - is the usual dirty throttling mechanism available?
246275 * @sc: scan_control in question
247276 *
248277 * The normal page dirty throttling mechanism in balance_dirty_pages() is
....@@ -254,11 +283,9 @@
254283 * This function tests whether the vmscan currently in progress can assume
255284 * that the normal dirty throttling mechanism is operational.
256285 */
257
-static bool sane_reclaim(struct scan_control *sc)
286
+static bool writeback_throttling_sane(struct scan_control *sc)
258287 {
259
- struct mem_cgroup *memcg = sc->target_mem_cgroup;
260
-
261
- if (!memcg)
288
+ if (!cgroup_reclaim(sc))
262289 return true;
263290 #ifdef CONFIG_CGROUP_WRITEBACK
264291 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
....@@ -266,50 +293,24 @@
266293 #endif
267294 return false;
268295 }
269
-
270
-static void set_memcg_congestion(pg_data_t *pgdat,
271
- struct mem_cgroup *memcg,
272
- bool congested)
273
-{
274
- struct mem_cgroup_per_node *mn;
275
-
276
- if (!memcg)
277
- return;
278
-
279
- mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
280
- WRITE_ONCE(mn->congested, congested);
281
-}
282
-
283
-static bool memcg_congested(pg_data_t *pgdat,
284
- struct mem_cgroup *memcg)
285
-{
286
- struct mem_cgroup_per_node *mn;
287
-
288
- mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
289
- return READ_ONCE(mn->congested);
290
-
291
-}
292296 #else
293
-static bool global_reclaim(struct scan_control *sc)
297
+static int prealloc_memcg_shrinker(struct shrinker *shrinker)
294298 {
295
- return true;
299
+ return 0;
296300 }
297301
298
-static bool sane_reclaim(struct scan_control *sc)
299
-{
300
- return true;
301
-}
302
-
303
-static inline void set_memcg_congestion(struct pglist_data *pgdat,
304
- struct mem_cgroup *memcg, bool congested)
302
+static void unregister_memcg_shrinker(struct shrinker *shrinker)
305303 {
306304 }
307305
308
-static inline bool memcg_congested(struct pglist_data *pgdat,
309
- struct mem_cgroup *memcg)
306
+static bool cgroup_reclaim(struct scan_control *sc)
310307 {
311308 return false;
309
+}
312310
311
+static bool writeback_throttling_sane(struct scan_control *sc)
312
+{
313
+ return true;
313314 }
314315 #endif
315316
....@@ -339,31 +340,21 @@
339340 */
340341 unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx)
341342 {
342
- unsigned long lru_size;
343
+ unsigned long size = 0;
343344 int zid;
344345
345
- if (!mem_cgroup_disabled())
346
- lru_size = mem_cgroup_get_lru_size(lruvec, lru);
347
- else
348
- lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru);
349
-
350
- for (zid = zone_idx + 1; zid < MAX_NR_ZONES; zid++) {
346
+ for (zid = 0; zid <= zone_idx && zid < MAX_NR_ZONES; zid++) {
351347 struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid];
352
- unsigned long size;
353348
354349 if (!managed_zone(zone))
355350 continue;
356351
357352 if (!mem_cgroup_disabled())
358
- size = mem_cgroup_get_zone_lru_size(lruvec, lru, zid);
353
+ size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid);
359354 else
360
- size = zone_page_state(&lruvec_pgdat(lruvec)->node_zones[zid],
361
- NR_ZONE_LRU_BASE + lru);
362
- lru_size -= min(size, lru_size);
355
+ size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru);
363356 }
364
-
365
- return lru_size;
366
-
357
+ return size;
367358 }
368359
369360 /*
....@@ -371,7 +362,7 @@
371362 */
372363 int prealloc_shrinker(struct shrinker *shrinker)
373364 {
374
- size_t size = sizeof(*shrinker->nr_deferred);
365
+ unsigned int size = sizeof(*shrinker->nr_deferred);
375366
376367 if (shrinker->flags & SHRINKER_NUMA_AWARE)
377368 size *= nr_node_ids;
....@@ -409,7 +400,7 @@
409400 {
410401 down_write(&shrinker_rwsem);
411402 list_add_tail(&shrinker->list, &shrinker_list);
412
-#ifdef CONFIG_MEMCG_KMEM
403
+#ifdef CONFIG_MEMCG
413404 if (shrinker->flags & SHRINKER_MEMCG_AWARE)
414405 idr_replace(&shrinker_idr, shrinker, shrinker->id);
415406 #endif
....@@ -475,13 +466,22 @@
475466 nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0);
476467
477468 total_scan = nr;
478
- delta = freeable >> priority;
479
- delta *= 4;
480
- do_div(delta, shrinker->seeks);
469
+ if (shrinker->seeks) {
470
+ delta = freeable >> priority;
471
+ delta *= 4;
472
+ do_div(delta, shrinker->seeks);
473
+ } else {
474
+ /*
475
+ * These objects don't require any IO to create. Trim
476
+ * them aggressively under memory pressure to keep
477
+ * them from causing refetches in the IO caches.
478
+ */
479
+ delta = freeable / 2;
480
+ }
481481
482482 total_scan += delta;
483483 if (total_scan < 0) {
484
- pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n",
484
+ pr_err("shrink_slab: %pS negative objects to delete nr=%ld\n",
485485 shrinker->scan_objects, total_scan);
486486 total_scan = freeable;
487487 next_deferred = nr;
....@@ -567,7 +567,7 @@
567567 return freed;
568568 }
569569
570
-#ifdef CONFIG_MEMCG_KMEM
570
+#ifdef CONFIG_MEMCG
571571 static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
572572 struct mem_cgroup *memcg, int priority)
573573 {
....@@ -575,7 +575,7 @@
575575 unsigned long ret, freed = 0;
576576 int i;
577577
578
- if (!memcg_kmem_enabled() || !mem_cgroup_online(memcg))
578
+ if (!mem_cgroup_online(memcg))
579579 return 0;
580580
581581 if (!down_read_trylock(&shrinker_rwsem))
....@@ -600,6 +600,11 @@
600600 clear_bit(i, map->map);
601601 continue;
602602 }
603
+
604
+ /* Call non-slab shrinkers even though kmem is disabled */
605
+ if (!memcg_kmem_enabled() &&
606
+ !(shrinker->flags & SHRINKER_NONSLAB))
607
+ continue;
603608
604609 ret = do_shrink_slab(&sc, shrinker, priority);
605610 if (ret == SHRINK_EMPTY) {
....@@ -637,13 +642,13 @@
637642 up_read(&shrinker_rwsem);
638643 return freed;
639644 }
640
-#else /* CONFIG_MEMCG_KMEM */
645
+#else /* CONFIG_MEMCG */
641646 static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
642647 struct mem_cgroup *memcg, int priority)
643648 {
644649 return 0;
645650 }
646
-#endif /* CONFIG_MEMCG_KMEM */
651
+#endif /* CONFIG_MEMCG */
647652
648653 /**
649654 * shrink_slab - shrink slab caches
....@@ -665,12 +670,17 @@
665670 *
666671 * Returns the number of reclaimed slab objects.
667672 */
668
-static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
673
+unsigned long shrink_slab(gfp_t gfp_mask, int nid,
669674 struct mem_cgroup *memcg,
670675 int priority)
671676 {
672677 unsigned long ret, freed = 0;
673678 struct shrinker *shrinker;
679
+ bool bypass = false;
680
+
681
+ trace_android_vh_shrink_slab_bypass(gfp_mask, nid, memcg, priority, &bypass);
682
+ if (bypass)
683
+ return 0;
674684
675685 /*
676686 * The root memcg might be allocated even though memcg is disabled
....@@ -698,7 +708,7 @@
698708 freed += ret;
699709 /*
700710 * Bail out if someone want to register a new shrinker to
701
- * prevent the regsitration from being stalled for long periods
711
+ * prevent the registration from being stalled for long periods
702712 * by parallel ongoing shrinking.
703713 */
704714 if (rwsem_is_contended(&shrinker_rwsem)) {
....@@ -712,6 +722,7 @@
712722 cond_resched();
713723 return freed;
714724 }
725
+EXPORT_SYMBOL_GPL(shrink_slab);
715726
716727 void drop_slab_node(int nid)
717728 {
....@@ -719,6 +730,9 @@
719730
720731 do {
721732 struct mem_cgroup *memcg = NULL;
733
+
734
+ if (fatal_signal_pending(current))
735
+ return;
722736
723737 freed = 0;
724738 memcg = mem_cgroup_iter(NULL, NULL, NULL);
....@@ -740,15 +754,14 @@
740754 {
741755 /*
742756 * A freeable page cache page is referenced only by the caller
743
- * that isolated the page, the page cache radix tree and
744
- * optional buffer heads at page->private.
757
+ * that isolated the page, the page cache and optional buffer
758
+ * heads at page->private.
745759 */
746
- int radix_pins = PageTransHuge(page) && PageSwapCache(page) ?
747
- HPAGE_PMD_NR : 1;
748
- return page_count(page) - page_has_private(page) == 1 + radix_pins;
760
+ int page_cache_pins = thp_nr_pages(page);
761
+ return page_count(page) - page_has_private(page) == 1 + page_cache_pins;
749762 }
750763
751
-static int may_write_to_inode(struct inode *inode, struct scan_control *sc)
764
+static int may_write_to_inode(struct inode *inode)
752765 {
753766 if (current->flags & PF_SWAPWRITE)
754767 return 1;
....@@ -796,8 +809,7 @@
796809 * pageout is called by shrink_page_list() for each dirty page.
797810 * Calls ->writepage().
798811 */
799
-static pageout_t pageout(struct page *page, struct address_space *mapping,
800
- struct scan_control *sc)
812
+static pageout_t pageout(struct page *page, struct address_space *mapping)
801813 {
802814 /*
803815 * If the page is dirty, only perform writeback if that write
....@@ -833,7 +845,7 @@
833845 }
834846 if (mapping->a_ops->writepage == NULL)
835847 return PAGE_ACTIVATE;
836
- if (!may_write_to_inode(mapping->host, sc))
848
+ if (!may_write_to_inode(mapping->host))
837849 return PAGE_KEEP;
838850
839851 if (clear_page_dirty_for_io(page)) {
....@@ -872,10 +884,11 @@
872884 * gets returned with a refcount of 0.
873885 */
874886 static int __remove_mapping(struct address_space *mapping, struct page *page,
875
- bool reclaimed)
887
+ bool reclaimed, struct mem_cgroup *target_memcg)
876888 {
877889 unsigned long flags;
878890 int refcount;
891
+ void *shadow = NULL;
879892
880893 BUG_ON(!PageLocked(page));
881894 BUG_ON(mapping != page_mapping(page));
....@@ -906,10 +919,7 @@
906919 * Note that if SetPageDirty is always performed via set_page_dirty,
907920 * and thus under the i_pages lock, then this ordering is not required.
908921 */
909
- if (unlikely(PageTransHuge(page)) && PageSwapCache(page))
910
- refcount = 1 + HPAGE_PMD_NR;
911
- else
912
- refcount = 2;
922
+ refcount = 1 + compound_nr(page);
913923 if (!page_ref_freeze(page, refcount))
914924 goto cannot_free;
915925 /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */
....@@ -921,12 +931,13 @@
921931 if (PageSwapCache(page)) {
922932 swp_entry_t swap = { .val = page_private(page) };
923933 mem_cgroup_swapout(page, swap);
924
- __delete_from_swap_cache(page);
934
+ if (reclaimed && !mapping_exiting(mapping))
935
+ shadow = workingset_eviction(page, target_memcg);
936
+ __delete_from_swap_cache(page, swap, shadow);
925937 xa_unlock_irqrestore(&mapping->i_pages, flags);
926938 put_swap_page(page, swap);
927939 } else {
928940 void (*freepage)(struct page *);
929
- void *shadow = NULL;
930941
931942 freepage = mapping->a_ops->freepage;
932943 /*
....@@ -934,7 +945,7 @@
934945 * order to detect refaults, thus thrashing, later on.
935946 *
936947 * But don't store shadows in an address space that is
937
- * already exiting. This is not just an optizimation,
948
+ * already exiting. This is not just an optimization,
938949 * inode reclaim needs to empty out the radix tree or
939950 * the nodes are lost. Don't plant shadows behind its
940951 * back.
....@@ -945,9 +956,9 @@
945956 * exceptional entries and shadow exceptional entries in the
946957 * same address_space.
947958 */
948
- if (reclaimed && page_is_file_cache(page) &&
959
+ if (reclaimed && page_is_file_lru(page) &&
949960 !mapping_exiting(mapping) && !dax_mapping(mapping))
950
- shadow = workingset_eviction(mapping, page);
961
+ shadow = workingset_eviction(page, target_memcg);
951962 __delete_from_page_cache(page, shadow);
952963 xa_unlock_irqrestore(&mapping->i_pages, flags);
953964
....@@ -970,7 +981,7 @@
970981 */
971982 int remove_mapping(struct address_space *mapping, struct page *page)
972983 {
973
- if (__remove_mapping(mapping, page, false)) {
984
+ if (__remove_mapping(mapping, page, false, NULL)) {
974985 /*
975986 * Unfreezing the refcount with 1 rather than 2 effectively
976987 * drops the pagecache ref for us without requiring another
....@@ -1009,11 +1020,24 @@
10091020 {
10101021 int referenced_ptes, referenced_page;
10111022 unsigned long vm_flags;
1023
+ bool should_protect = false;
1024
+ bool trylock_fail = false;
1025
+ int ret = 0;
10121026
1027
+ trace_android_vh_page_should_be_protected(page, &should_protect);
1028
+ if (unlikely(should_protect))
1029
+ return PAGEREF_ACTIVATE;
1030
+
1031
+ trace_android_vh_page_trylock_set(page);
1032
+ trace_android_vh_check_page_look_around_ref(page, &ret);
1033
+ if (ret)
1034
+ return ret;
10131035 referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup,
10141036 &vm_flags);
10151037 referenced_page = TestClearPageReferenced(page);
1016
-
1038
+ trace_android_vh_page_trylock_get_result(page, &trylock_fail);
1039
+ if (trylock_fail)
1040
+ return PAGEREF_KEEP;
10171041 /*
10181042 * Mlock lost the isolation race with us. Let try_to_unmap()
10191043 * move the page to the unevictable list.
....@@ -1021,9 +1045,11 @@
10211045 if (vm_flags & VM_LOCKED)
10221046 return PAGEREF_RECLAIM;
10231047
1048
+ /* rmap lock contention: rotate */
1049
+ if (referenced_ptes == -1)
1050
+ return PAGEREF_KEEP;
1051
+
10241052 if (referenced_ptes) {
1025
- if (PageSwapBacked(page))
1026
- return PAGEREF_ACTIVATE;
10271053 /*
10281054 * All mapped pages start out with page table
10291055 * references from the instantiating fault, so we need
....@@ -1046,7 +1072,7 @@
10461072 /*
10471073 * Activate file-backed executable pages after first usage.
10481074 */
1049
- if (vm_flags & VM_EXEC)
1075
+ if ((vm_flags & VM_EXEC) && !PageSwapBacked(page))
10501076 return PAGEREF_ACTIVATE;
10511077
10521078 return PAGEREF_KEEP;
....@@ -1069,7 +1095,7 @@
10691095 * Anonymous pages are not handled by flushers and must be written
10701096 * from reclaim context. Do not stall reclaim based on them
10711097 */
1072
- if (!page_is_file_cache(page) ||
1098
+ if (!page_is_file_lru(page) ||
10731099 (PageAnon(page) && !PageSwapBacked(page))) {
10741100 *dirty = false;
10751101 *writeback = false;
....@@ -1092,33 +1118,27 @@
10921118 /*
10931119 * shrink_page_list() returns the number of reclaimed pages
10941120 */
1095
-static unsigned long shrink_page_list(struct list_head *page_list,
1096
- struct pglist_data *pgdat,
1097
- struct scan_control *sc,
1098
- enum ttu_flags ttu_flags,
1099
- struct reclaim_stat *stat,
1100
- bool force_reclaim)
1121
+static unsigned int shrink_page_list(struct list_head *page_list,
1122
+ struct pglist_data *pgdat,
1123
+ struct scan_control *sc,
1124
+ struct reclaim_stat *stat,
1125
+ bool ignore_references)
11011126 {
11021127 LIST_HEAD(ret_pages);
11031128 LIST_HEAD(free_pages);
1104
- int pgactivate = 0;
1105
- unsigned nr_unqueued_dirty = 0;
1106
- unsigned nr_dirty = 0;
1107
- unsigned nr_congested = 0;
1108
- unsigned nr_reclaimed = 0;
1109
- unsigned nr_writeback = 0;
1110
- unsigned nr_immediate = 0;
1111
- unsigned nr_ref_keep = 0;
1112
- unsigned nr_unmap_fail = 0;
1129
+ unsigned int nr_reclaimed = 0;
1130
+ unsigned int pgactivate = 0;
1131
+ bool page_trylock_result;
11131132
1133
+ memset(stat, 0, sizeof(*stat));
11141134 cond_resched();
11151135
11161136 while (!list_empty(page_list)) {
11171137 struct address_space *mapping;
11181138 struct page *page;
1119
- int may_enter_fs;
1120
- enum page_references references = PAGEREF_RECLAIM_CLEAN;
1121
- bool dirty, writeback;
1139
+ enum page_references references = PAGEREF_RECLAIM;
1140
+ bool dirty, writeback, may_enter_fs;
1141
+ unsigned int nr_pages;
11221142
11231143 cond_resched();
11241144
....@@ -1130,18 +1150,16 @@
11301150
11311151 VM_BUG_ON_PAGE(PageActive(page), page);
11321152
1133
- sc->nr_scanned++;
1153
+ nr_pages = compound_nr(page);
1154
+
1155
+ /* Account the number of base pages even though THP */
1156
+ sc->nr_scanned += nr_pages;
11341157
11351158 if (unlikely(!page_evictable(page)))
11361159 goto activate_locked;
11371160
11381161 if (!sc->may_unmap && page_mapped(page))
11391162 goto keep_locked;
1140
-
1141
- /* Double the slab pressure for mapped and swapcache pages */
1142
- if ((page_mapped(page) || PageSwapCache(page)) &&
1143
- !(PageAnon(page) && !PageSwapBacked(page)))
1144
- sc->nr_scanned++;
11451163
11461164 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
11471165 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
....@@ -1154,10 +1172,10 @@
11541172 */
11551173 page_check_dirty_writeback(page, &dirty, &writeback);
11561174 if (dirty || writeback)
1157
- nr_dirty++;
1175
+ stat->nr_dirty++;
11581176
11591177 if (dirty && !writeback)
1160
- nr_unqueued_dirty++;
1178
+ stat->nr_unqueued_dirty++;
11611179
11621180 /*
11631181 * Treat this page as congested if the underlying BDI is or if
....@@ -1169,7 +1187,7 @@
11691187 if (((dirty || writeback) && mapping &&
11701188 inode_write_congested(mapping->host)) ||
11711189 (writeback && PageReclaim(page)))
1172
- nr_congested++;
1190
+ stat->nr_congested++;
11731191
11741192 /*
11751193 * If a page at the tail of the LRU is under writeback, there
....@@ -1218,11 +1236,11 @@
12181236 if (current_is_kswapd() &&
12191237 PageReclaim(page) &&
12201238 test_bit(PGDAT_WRITEBACK, &pgdat->flags)) {
1221
- nr_immediate++;
1239
+ stat->nr_immediate++;
12221240 goto activate_locked;
12231241
12241242 /* Case 2 above */
1225
- } else if (sane_reclaim(sc) ||
1243
+ } else if (writeback_throttling_sane(sc) ||
12261244 !PageReclaim(page) || !may_enter_fs) {
12271245 /*
12281246 * This is slightly racy - end_page_writeback()
....@@ -1236,7 +1254,7 @@
12361254 * and it's also appropriate in global reclaim.
12371255 */
12381256 SetPageReclaim(page);
1239
- nr_writeback++;
1257
+ stat->nr_writeback++;
12401258 goto activate_locked;
12411259
12421260 /* Case 3 above */
....@@ -1249,14 +1267,14 @@
12491267 }
12501268 }
12511269
1252
- if (!force_reclaim)
1270
+ if (!ignore_references)
12531271 references = page_check_references(page, sc);
12541272
12551273 switch (references) {
12561274 case PAGEREF_ACTIVATE:
12571275 goto activate_locked;
12581276 case PAGEREF_KEEP:
1259
- nr_ref_keep++;
1277
+ stat->nr_ref_keep += nr_pages;
12601278 goto keep_locked;
12611279 case PAGEREF_RECLAIM:
12621280 case PAGEREF_RECLAIM_CLEAN:
....@@ -1271,6 +1289,8 @@
12711289 if (PageAnon(page) && PageSwapBacked(page)) {
12721290 if (!PageSwapCache(page)) {
12731291 if (!(sc->gfp_mask & __GFP_IO))
1292
+ goto keep_locked;
1293
+ if (page_maybe_dma_pinned(page))
12741294 goto keep_locked;
12751295 if (PageTransHuge(page)) {
12761296 /* cannot split THP, skip it */
....@@ -1288,7 +1308,7 @@
12881308 }
12891309 if (!add_to_swap(page)) {
12901310 if (!PageTransHuge(page))
1291
- goto activate_locked;
1311
+ goto activate_locked_split;
12921312 /* Fallback to swap normal pages */
12931313 if (split_huge_page_to_list(page,
12941314 page_list))
....@@ -1297,10 +1317,10 @@
12971317 count_vm_event(THP_SWPOUT_FALLBACK);
12981318 #endif
12991319 if (!add_to_swap(page))
1300
- goto activate_locked;
1320
+ goto activate_locked_split;
13011321 }
13021322
1303
- may_enter_fs = 1;
1323
+ may_enter_fs = true;
13041324
13051325 /* Adding to swap updated mapping */
13061326 mapping = page_mapping(page);
....@@ -1312,16 +1332,33 @@
13121332 }
13131333
13141334 /*
1335
+ * THP may get split above, need minus tail pages and update
1336
+ * nr_pages to avoid accounting tail pages twice.
1337
+ *
1338
+ * The tail pages that are added into swap cache successfully
1339
+ * reach here.
1340
+ */
1341
+ if ((nr_pages > 1) && !PageTransHuge(page)) {
1342
+ sc->nr_scanned -= (nr_pages - 1);
1343
+ nr_pages = 1;
1344
+ }
1345
+
1346
+ /*
13151347 * The page is mapped into the page tables of one or more
13161348 * processes. Try to unmap it here.
13171349 */
13181350 if (page_mapped(page)) {
1319
- enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH;
1351
+ enum ttu_flags flags = TTU_BATCH_FLUSH;
1352
+ bool was_swapbacked = PageSwapBacked(page);
13201353
13211354 if (unlikely(PageTransHuge(page)))
13221355 flags |= TTU_SPLIT_HUGE_PMD;
1356
+ if (!ignore_references)
1357
+ trace_android_vh_page_trylock_set(page);
13231358 if (!try_to_unmap(page, flags)) {
1324
- nr_unmap_fail++;
1359
+ stat->nr_unmap_fail += nr_pages;
1360
+ if (!was_swapbacked && PageSwapBacked(page))
1361
+ stat->nr_lazyfree_fail += nr_pages;
13251362 goto activate_locked;
13261363 }
13271364 }
....@@ -1337,7 +1374,7 @@
13371374 * the rest of the LRU for clean pages and see
13381375 * the same dirty pages again (PageReclaim).
13391376 */
1340
- if (page_is_file_cache(page) &&
1377
+ if (page_is_file_lru(page) &&
13411378 (!current_is_kswapd() || !PageReclaim(page) ||
13421379 !test_bit(PGDAT_DIRTY, &pgdat->flags))) {
13431380 /*
....@@ -1365,12 +1402,14 @@
13651402 * starts and then write it out here.
13661403 */
13671404 try_to_unmap_flush_dirty();
1368
- switch (pageout(page, mapping, sc)) {
1405
+ switch (pageout(page, mapping)) {
13691406 case PAGE_KEEP:
13701407 goto keep_locked;
13711408 case PAGE_ACTIVATE:
13721409 goto activate_locked;
13731410 case PAGE_SUCCESS:
1411
+ stat->nr_pageout += thp_nr_pages(page);
1412
+
13741413 if (PageWriteback(page))
13751414 goto keep;
13761415 if (PageDirty(page))
....@@ -1426,6 +1465,7 @@
14261465 * increment nr_reclaimed here (and
14271466 * leave it off the LRU).
14281467 */
1468
+ trace_android_vh_page_trylock_clear(page);
14291469 nr_reclaimed++;
14301470 continue;
14311471 }
....@@ -1443,30 +1483,38 @@
14431483
14441484 count_vm_event(PGLAZYFREED);
14451485 count_memcg_page_event(page, PGLAZYFREED);
1446
- } else if (!mapping || !__remove_mapping(mapping, page, true))
1486
+ } else if (!mapping || !__remove_mapping(mapping, page, true,
1487
+ sc->target_mem_cgroup))
14471488 goto keep_locked;
1448
- /*
1449
- * At this point, we have no other references and there is
1450
- * no way to pick any more up (removed from LRU, removed
1451
- * from pagecache). Can use non-atomic bitops now (and
1452
- * we obviously don't have to worry about waking up a process
1453
- * waiting on the page lock, because there are no references.
1454
- */
1455
- __ClearPageLocked(page);
1489
+
1490
+ unlock_page(page);
14561491 free_it:
1457
- nr_reclaimed++;
1492
+ /*
1493
+ * THP may get swapped out in a whole, need account
1494
+ * all base pages.
1495
+ */
1496
+ nr_reclaimed += nr_pages;
14581497
14591498 /*
14601499 * Is there need to periodically free_page_list? It would
14611500 * appear not as the counts should be low
14621501 */
1463
- if (unlikely(PageTransHuge(page))) {
1464
- mem_cgroup_uncharge(page);
1465
- (*get_compound_page_dtor(page))(page);
1466
- } else
1502
+ trace_android_vh_page_trylock_clear(page);
1503
+ if (unlikely(PageTransHuge(page)))
1504
+ destroy_compound_page(page);
1505
+ else
14671506 list_add(&page->lru, &free_pages);
14681507 continue;
14691508
1509
+activate_locked_split:
1510
+ /*
1511
+ * The tail pages that are failed to add into swap cache
1512
+ * reach here. Fixup nr_scanned and nr_pages.
1513
+ */
1514
+ if (nr_pages > 1) {
1515
+ sc->nr_scanned -= (nr_pages - 1);
1516
+ nr_pages = 1;
1517
+ }
14701518 activate_locked:
14711519 /* Not a candidate for swapping, so reclaim swap space. */
14721520 if (PageSwapCache(page) && (mem_cgroup_swap_full(page) ||
....@@ -1474,16 +1522,31 @@
14741522 try_to_free_swap(page);
14751523 VM_BUG_ON_PAGE(PageActive(page), page);
14761524 if (!PageMlocked(page)) {
1525
+ int type = page_is_file_lru(page);
14771526 SetPageActive(page);
1478
- pgactivate++;
1527
+ stat->nr_activate[type] += nr_pages;
14791528 count_memcg_page_event(page, PGACTIVATE);
14801529 }
14811530 keep_locked:
1531
+ /*
1532
+ * The page with trylock-bit will be added ret_pages and
1533
+ * handled in trace_android_vh_handle_failed_page_trylock.
1534
+ * In the progress[unlock_page, handled], the page carried
1535
+ * with trylock-bit will cause some error-issues in other
1536
+ * scene, so clear trylock-bit here.
1537
+ * trace_android_vh_page_trylock_get_result will clear
1538
+ * trylock-bit and return if page tyrlock failed in
1539
+ * reclaim-process. Here we just want to clear trylock-bit
1540
+ * so that ignore page_trylock_result.
1541
+ */
1542
+ trace_android_vh_page_trylock_get_result(page, &page_trylock_result);
14821543 unlock_page(page);
14831544 keep:
14841545 list_add(&page->lru, &ret_pages);
14851546 VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page);
14861547 }
1548
+
1549
+ pgactivate = stat->nr_activate[0] + stat->nr_activate[1];
14871550
14881551 mem_cgroup_uncharge_list(&free_pages);
14891552 try_to_unmap_flush();
....@@ -1492,20 +1555,10 @@
14921555 list_splice(&ret_pages, page_list);
14931556 count_vm_events(PGACTIVATE, pgactivate);
14941557
1495
- if (stat) {
1496
- stat->nr_dirty = nr_dirty;
1497
- stat->nr_congested = nr_congested;
1498
- stat->nr_unqueued_dirty = nr_unqueued_dirty;
1499
- stat->nr_writeback = nr_writeback;
1500
- stat->nr_immediate = nr_immediate;
1501
- stat->nr_activate = pgactivate;
1502
- stat->nr_ref_keep = nr_ref_keep;
1503
- stat->nr_unmap_fail = nr_unmap_fail;
1504
- }
15051558 return nr_reclaimed;
15061559 }
15071560
1508
-unsigned long reclaim_clean_pages_from_list(struct zone *zone,
1561
+unsigned int reclaim_clean_pages_from_list(struct zone *zone,
15091562 struct list_head *page_list)
15101563 {
15111564 struct scan_control sc = {
....@@ -1513,23 +1566,35 @@
15131566 .priority = DEF_PRIORITY,
15141567 .may_unmap = 1,
15151568 };
1516
- unsigned long ret;
1569
+ struct reclaim_stat stat;
1570
+ unsigned int nr_reclaimed;
15171571 struct page *page, *next;
15181572 LIST_HEAD(clean_pages);
15191573
15201574 list_for_each_entry_safe(page, next, page_list, lru) {
1521
- if (page_is_file_cache(page) && !PageDirty(page) &&
1575
+ if (page_is_file_lru(page) && !PageDirty(page) &&
15221576 !__PageMovable(page) && !PageUnevictable(page)) {
15231577 ClearPageActive(page);
15241578 list_move(&page->lru, &clean_pages);
15251579 }
15261580 }
15271581
1528
- ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc,
1529
- TTU_IGNORE_ACCESS, NULL, true);
1582
+ nr_reclaimed = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc,
1583
+ &stat, true);
15301584 list_splice(&clean_pages, page_list);
1531
- mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret);
1532
- return ret;
1585
+ mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
1586
+ -(long)nr_reclaimed);
1587
+ /*
1588
+ * Since lazyfree pages are isolated from file LRU from the beginning,
1589
+ * they will rotate back to anonymous LRU in the end if it failed to
1590
+ * discard so isolated count will be mismatched.
1591
+ * Compensate the isolated count for both LRU lists.
1592
+ */
1593
+ mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON,
1594
+ stat.nr_lazyfree_fail);
1595
+ mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
1596
+ -(long)stat.nr_lazyfree_fail);
1597
+ return nr_reclaimed;
15331598 }
15341599
15351600 /*
....@@ -1612,7 +1677,7 @@
16121677
16131678 /*
16141679 * Update LRU sizes after isolating pages. The LRU size updates must
1615
- * be complete before mem_cgroup_update_lru_size due to a santity check.
1680
+ * be complete before mem_cgroup_update_lru_size due to a sanity check.
16161681 */
16171682 static __always_inline void update_lru_sizes(struct lruvec *lruvec,
16181683 enum lru_list lru, unsigned long *nr_zone_taken)
....@@ -1623,16 +1688,13 @@
16231688 if (!nr_zone_taken[zid])
16241689 continue;
16251690
1626
- __update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
1627
-#ifdef CONFIG_MEMCG
1628
- mem_cgroup_update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
1629
-#endif
1691
+ update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
16301692 }
16311693
16321694 }
16331695
1634
-/*
1635
- * zone_lru_lock is heavily contended. Some of the functions that
1696
+/**
1697
+ * pgdat->lru_lock is heavily contended. Some of the functions that
16361698 * shrink the lists perform better by taking out a batch of pages
16371699 * and working on them outside the LRU lock.
16381700 *
....@@ -1646,7 +1708,6 @@
16461708 * @dst: The temp list to put pages on to.
16471709 * @nr_scanned: The number of pages that were scanned.
16481710 * @sc: The scan_control struct for this reclaim session
1649
- * @mode: One of the LRU isolation modes
16501711 * @lru: LRU list id for isolating
16511712 *
16521713 * returns how many pages were moved onto *@dst.
....@@ -1654,7 +1715,7 @@
16541715 static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
16551716 struct lruvec *lruvec, struct list_head *dst,
16561717 unsigned long *nr_scanned, struct scan_control *sc,
1657
- isolate_mode_t mode, enum lru_list lru)
1718
+ enum lru_list lru)
16581719 {
16591720 struct list_head *src = &lruvec->lists[lru];
16601721 unsigned long nr_taken = 0;
....@@ -1663,11 +1724,11 @@
16631724 unsigned long skipped = 0;
16641725 unsigned long scan, total_scan, nr_pages;
16651726 LIST_HEAD(pages_skipped);
1727
+ isolate_mode_t mode = (sc->may_unmap ? 0 : ISOLATE_UNMAPPED);
16661728
1729
+ total_scan = 0;
16671730 scan = 0;
1668
- for (total_scan = 0;
1669
- scan < nr_to_scan && nr_taken < nr_to_scan && !list_empty(src);
1670
- total_scan++) {
1731
+ while (scan < nr_to_scan && !list_empty(src)) {
16711732 struct page *page;
16721733
16731734 page = lru_to_page(src);
....@@ -1675,9 +1736,12 @@
16751736
16761737 VM_BUG_ON_PAGE(!PageLRU(page), page);
16771738
1739
+ nr_pages = compound_nr(page);
1740
+ total_scan += nr_pages;
1741
+
16781742 if (page_zonenum(page) > sc->reclaim_idx) {
16791743 list_move(&page->lru, &pages_skipped);
1680
- nr_skipped[page_zonenum(page)]++;
1744
+ nr_skipped[page_zonenum(page)] += nr_pages;
16811745 continue;
16821746 }
16831747
....@@ -1686,13 +1750,17 @@
16861750 * return with no isolated pages if the LRU mostly contains
16871751 * ineligible pages. This causes the VM to not reclaim any
16881752 * pages, triggering a premature OOM.
1753
+ *
1754
+ * Account all tail pages of THP. This would not cause
1755
+ * premature OOM since __isolate_lru_page() returns -EBUSY
1756
+ * only when the page is being freed somewhere else.
16891757 */
1690
- scan++;
1758
+ scan += nr_pages;
16911759 switch (__isolate_lru_page(page, mode)) {
16921760 case 0:
1693
- nr_pages = hpage_nr_pages(page);
16941761 nr_taken += nr_pages;
16951762 nr_zone_taken[page_zonenum(page)] += nr_pages;
1763
+ trace_android_vh_del_page_from_lrulist(page, false, lru);
16961764 list_move(&page->lru, dst);
16971765 break;
16981766
....@@ -1753,7 +1821,7 @@
17531821 * Restrictions:
17541822 *
17551823 * (1) Must be called with an elevated refcount on the page. This is a
1756
- * fundamentnal difference from isolate_lru_pages (which is called
1824
+ * fundamental difference from isolate_lru_pages (which is called
17571825 * without a stable reference).
17581826 * (2) the lru_lock must not be held.
17591827 * (3) interrupts must be enabled.
....@@ -1766,11 +1834,11 @@
17661834 WARN_RATELIMIT(PageTail(page), "trying to isolate tail page");
17671835
17681836 if (PageLRU(page)) {
1769
- struct zone *zone = page_zone(page);
1837
+ pg_data_t *pgdat = page_pgdat(page);
17701838 struct lruvec *lruvec;
17711839
1772
- spin_lock_irq(zone_lru_lock(zone));
1773
- lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
1840
+ spin_lock_irq(&pgdat->lru_lock);
1841
+ lruvec = mem_cgroup_page_lruvec(page, pgdat);
17741842 if (PageLRU(page)) {
17751843 int lru = page_lru(page);
17761844 get_page(page);
....@@ -1778,14 +1846,14 @@
17781846 del_page_from_lru_list(page, lruvec, lru);
17791847 ret = 0;
17801848 }
1781
- spin_unlock_irq(zone_lru_lock(zone));
1849
+ spin_unlock_irq(&pgdat->lru_lock);
17821850 }
17831851 return ret;
17841852 }
17851853
17861854 /*
17871855 * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
1788
- * then get resheduled. When there are massive number of tasks doing page
1856
+ * then get rescheduled. When there are massive number of tasks doing page
17891857 * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
17901858 * the LRU list will go small and be scanned faster than necessary, leading to
17911859 * unnecessary swapping, thrashing and OOM.
....@@ -1798,7 +1866,7 @@
17981866 if (current_is_kswapd())
17991867 return 0;
18001868
1801
- if (!sane_reclaim(sc))
1869
+ if (!writeback_throttling_sane(sc))
18021870 return 0;
18031871
18041872 if (file) {
....@@ -1820,40 +1888,55 @@
18201888 return isolated > inactive;
18211889 }
18221890
1823
-static noinline_for_stack void
1824
-putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list)
1891
+/*
1892
+ * This moves pages from @list to corresponding LRU list.
1893
+ *
1894
+ * We move them the other way if the page is referenced by one or more
1895
+ * processes, from rmap.
1896
+ *
1897
+ * If the pages are mostly unmapped, the processing is fast and it is
1898
+ * appropriate to hold zone_lru_lock across the whole operation. But if
1899
+ * the pages are mapped, the processing is slow (page_referenced()) so we
1900
+ * should drop zone_lru_lock around each page. It's impossible to balance
1901
+ * this, so instead we remove the pages from the LRU while processing them.
1902
+ * It is safe to rely on PG_active against the non-LRU pages in here because
1903
+ * nobody will play with that bit on a non-LRU page.
1904
+ *
1905
+ * The downside is that we have to touch page->_refcount against each page.
1906
+ * But we had to alter page->flags anyway.
1907
+ *
1908
+ * Returns the number of pages moved to the given lruvec.
1909
+ */
1910
+
1911
+static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
1912
+ struct list_head *list)
18251913 {
1826
- struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
18271914 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
1915
+ int nr_pages, nr_moved = 0;
18281916 LIST_HEAD(pages_to_free);
1917
+ struct page *page;
1918
+ enum lru_list lru;
18291919
1830
- /*
1831
- * Put back any unfreeable pages.
1832
- */
1833
- while (!list_empty(page_list)) {
1834
- struct page *page = lru_to_page(page_list);
1835
- int lru;
1836
-
1920
+ while (!list_empty(list)) {
1921
+ page = lru_to_page(list);
18371922 VM_BUG_ON_PAGE(PageLRU(page), page);
1838
- list_del(&page->lru);
18391923 if (unlikely(!page_evictable(page))) {
1924
+ list_del(&page->lru);
18401925 spin_unlock_irq(&pgdat->lru_lock);
18411926 putback_lru_page(page);
18421927 spin_lock_irq(&pgdat->lru_lock);
18431928 continue;
18441929 }
1845
-
18461930 lruvec = mem_cgroup_page_lruvec(page, pgdat);
18471931
18481932 SetPageLRU(page);
18491933 lru = page_lru(page);
1850
- add_page_to_lru_list(page, lruvec, lru);
18511934
1852
- if (is_active_lru(lru)) {
1853
- int file = is_file_lru(lru);
1854
- int numpages = hpage_nr_pages(page);
1855
- reclaim_stat->recent_rotated[file] += numpages;
1856
- }
1935
+ nr_pages = thp_nr_pages(page);
1936
+ update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
1937
+ list_move(&page->lru, &lruvec->lists[lru]);
1938
+ trace_android_vh_add_page_to_lrulist(page, false, lru);
1939
+
18571940 if (put_page_testzero(page)) {
18581941 __ClearPageLRU(page);
18591942 __ClearPageActive(page);
....@@ -1861,29 +1944,34 @@
18611944
18621945 if (unlikely(PageCompound(page))) {
18631946 spin_unlock_irq(&pgdat->lru_lock);
1864
- mem_cgroup_uncharge(page);
1865
- (*get_compound_page_dtor(page))(page);
1947
+ destroy_compound_page(page);
18661948 spin_lock_irq(&pgdat->lru_lock);
18671949 } else
18681950 list_add(&page->lru, &pages_to_free);
1951
+ } else {
1952
+ nr_moved += nr_pages;
1953
+ if (PageActive(page))
1954
+ workingset_age_nonresident(lruvec, nr_pages);
18691955 }
18701956 }
18711957
18721958 /*
18731959 * To save our caller's stack, now use input list for pages to free.
18741960 */
1875
- list_splice(&pages_to_free, page_list);
1961
+ list_splice(&pages_to_free, list);
1962
+
1963
+ return nr_moved;
18761964 }
18771965
18781966 /*
18791967 * If a kernel thread (such as nfsd for loop-back mounts) services
1880
- * a backing device by writing to the page cache it sets PF_LESS_THROTTLE.
1968
+ * a backing device by writing to the page cache it sets PF_LOCAL_THROTTLE.
18811969 * In that case we should only throttle if the backing device it is
18821970 * writing to is congested. In other cases it is safe to throttle.
18831971 */
18841972 static int current_may_throttle(void)
18851973 {
1886
- return !(current->flags & PF_LESS_THROTTLE) ||
1974
+ return !(current->flags & PF_LOCAL_THROTTLE) ||
18871975 current->backing_dev_info == NULL ||
18881976 bdi_write_congested(current->backing_dev_info);
18891977 }
....@@ -1898,13 +1986,12 @@
18981986 {
18991987 LIST_HEAD(page_list);
19001988 unsigned long nr_scanned;
1901
- unsigned long nr_reclaimed = 0;
1989
+ unsigned int nr_reclaimed = 0;
19021990 unsigned long nr_taken;
1903
- struct reclaim_stat stat = {};
1904
- isolate_mode_t isolate_mode = 0;
1905
- int file = is_file_lru(lru);
1991
+ struct reclaim_stat stat;
1992
+ bool file = is_file_lru(lru);
1993
+ enum vm_event_item item;
19061994 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
1907
- struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
19081995 bool stalled = false;
19091996
19101997 while (unlikely(too_many_isolated(pgdat, file, sc))) {
....@@ -1922,54 +2009,37 @@
19222009
19232010 lru_add_drain();
19242011
1925
- if (!sc->may_unmap)
1926
- isolate_mode |= ISOLATE_UNMAPPED;
1927
-
19282012 spin_lock_irq(&pgdat->lru_lock);
19292013
19302014 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
1931
- &nr_scanned, sc, isolate_mode, lru);
2015
+ &nr_scanned, sc, lru);
19322016
19332017 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
1934
- reclaim_stat->recent_scanned[file] += nr_taken;
2018
+ item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT;
2019
+ if (!cgroup_reclaim(sc))
2020
+ __count_vm_events(item, nr_scanned);
2021
+ __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned);
2022
+ __count_vm_events(PGSCAN_ANON + file, nr_scanned);
19352023
1936
- if (current_is_kswapd()) {
1937
- if (global_reclaim(sc))
1938
- __count_vm_events(PGSCAN_KSWAPD, nr_scanned);
1939
- count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD,
1940
- nr_scanned);
1941
- } else {
1942
- if (global_reclaim(sc))
1943
- __count_vm_events(PGSCAN_DIRECT, nr_scanned);
1944
- count_memcg_events(lruvec_memcg(lruvec), PGSCAN_DIRECT,
1945
- nr_scanned);
1946
- }
19472024 spin_unlock_irq(&pgdat->lru_lock);
19482025
19492026 if (nr_taken == 0)
19502027 return 0;
19512028
1952
- nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0,
1953
- &stat, false);
2029
+ nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, &stat, false);
2030
+ trace_android_vh_handle_failed_page_trylock(&page_list);
19542031
19552032 spin_lock_irq(&pgdat->lru_lock);
19562033
1957
- if (current_is_kswapd()) {
1958
- if (global_reclaim(sc))
1959
- __count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed);
1960
- count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_KSWAPD,
1961
- nr_reclaimed);
1962
- } else {
1963
- if (global_reclaim(sc))
1964
- __count_vm_events(PGSTEAL_DIRECT, nr_reclaimed);
1965
- count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_DIRECT,
1966
- nr_reclaimed);
1967
- }
1968
-
1969
- putback_inactive_pages(lruvec, &page_list);
2034
+ move_pages_to_lru(lruvec, &page_list);
19702035
19712036 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
1972
-
2037
+ lru_note_cost(lruvec, file, stat.nr_pageout);
2038
+ item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT;
2039
+ if (!cgroup_reclaim(sc))
2040
+ __count_vm_events(item, nr_reclaimed);
2041
+ __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed);
2042
+ __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed);
19732043 spin_unlock_irq(&pgdat->lru_lock);
19742044
19752045 mem_cgroup_uncharge_list(&page_list);
....@@ -2003,73 +2073,6 @@
20032073 return nr_reclaimed;
20042074 }
20052075
2006
-/*
2007
- * This moves pages from the active list to the inactive list.
2008
- *
2009
- * We move them the other way if the page is referenced by one or more
2010
- * processes, from rmap.
2011
- *
2012
- * If the pages are mostly unmapped, the processing is fast and it is
2013
- * appropriate to hold zone_lru_lock across the whole operation. But if
2014
- * the pages are mapped, the processing is slow (page_referenced()) so we
2015
- * should drop zone_lru_lock around each page. It's impossible to balance
2016
- * this, so instead we remove the pages from the LRU while processing them.
2017
- * It is safe to rely on PG_active against the non-LRU pages in here because
2018
- * nobody will play with that bit on a non-LRU page.
2019
- *
2020
- * The downside is that we have to touch page->_refcount against each page.
2021
- * But we had to alter page->flags anyway.
2022
- *
2023
- * Returns the number of pages moved to the given lru.
2024
- */
2025
-
2026
-static unsigned move_active_pages_to_lru(struct lruvec *lruvec,
2027
- struct list_head *list,
2028
- struct list_head *pages_to_free,
2029
- enum lru_list lru)
2030
-{
2031
- struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2032
- struct page *page;
2033
- int nr_pages;
2034
- int nr_moved = 0;
2035
-
2036
- while (!list_empty(list)) {
2037
- page = lru_to_page(list);
2038
- lruvec = mem_cgroup_page_lruvec(page, pgdat);
2039
-
2040
- VM_BUG_ON_PAGE(PageLRU(page), page);
2041
- SetPageLRU(page);
2042
-
2043
- nr_pages = hpage_nr_pages(page);
2044
- update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
2045
- list_move(&page->lru, &lruvec->lists[lru]);
2046
-
2047
- if (put_page_testzero(page)) {
2048
- __ClearPageLRU(page);
2049
- __ClearPageActive(page);
2050
- del_page_from_lru_list(page, lruvec, lru);
2051
-
2052
- if (unlikely(PageCompound(page))) {
2053
- spin_unlock_irq(&pgdat->lru_lock);
2054
- mem_cgroup_uncharge(page);
2055
- (*get_compound_page_dtor(page))(page);
2056
- spin_lock_irq(&pgdat->lru_lock);
2057
- } else
2058
- list_add(&page->lru, pages_to_free);
2059
- } else {
2060
- nr_moved += nr_pages;
2061
- }
2062
- }
2063
-
2064
- if (!is_active_lru(lru)) {
2065
- __count_vm_events(PGDEACTIVATE, nr_moved);
2066
- count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
2067
- nr_moved);
2068
- }
2069
-
2070
- return nr_moved;
2071
-}
2072
-
20732076 static void shrink_active_list(unsigned long nr_to_scan,
20742077 struct lruvec *lruvec,
20752078 struct scan_control *sc,
....@@ -2082,28 +2085,25 @@
20822085 LIST_HEAD(l_active);
20832086 LIST_HEAD(l_inactive);
20842087 struct page *page;
2085
- struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
20862088 unsigned nr_deactivate, nr_activate;
20872089 unsigned nr_rotated = 0;
2088
- isolate_mode_t isolate_mode = 0;
20892090 int file = is_file_lru(lru);
20902091 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2092
+ bool bypass = false;
2093
+ bool should_protect = false;
20912094
20922095 lru_add_drain();
2093
-
2094
- if (!sc->may_unmap)
2095
- isolate_mode |= ISOLATE_UNMAPPED;
20962096
20972097 spin_lock_irq(&pgdat->lru_lock);
20982098
20992099 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
2100
- &nr_scanned, sc, isolate_mode, lru);
2100
+ &nr_scanned, sc, lru);
21012101
21022102 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
2103
- reclaim_stat->recent_scanned[file] += nr_taken;
21042103
2105
- __count_vm_events(PGREFILL, nr_scanned);
2106
- count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned);
2104
+ if (!cgroup_reclaim(sc))
2105
+ __count_vm_events(PGREFILL, nr_scanned);
2106
+ __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned);
21072107
21082108 spin_unlock_irq(&pgdat->lru_lock);
21092109
....@@ -2125,9 +2125,20 @@
21252125 }
21262126 }
21272127
2128
+ trace_android_vh_page_should_be_protected(page, &should_protect);
2129
+ if (unlikely(should_protect)) {
2130
+ nr_rotated += thp_nr_pages(page);
2131
+ list_add(&page->lru, &l_active);
2132
+ continue;
2133
+ }
2134
+
2135
+ trace_android_vh_page_referenced_check_bypass(page, nr_to_scan, lru, &bypass);
2136
+ if (bypass)
2137
+ goto skip_page_referenced;
2138
+ trace_android_vh_page_trylock_set(page);
2139
+ /* Referenced or rmap lock contention: rotate */
21282140 if (page_referenced(page, 0, sc->target_mem_cgroup,
2129
- &vm_flags)) {
2130
- nr_rotated += hpage_nr_pages(page);
2141
+ &vm_flags) != 0) {
21312142 /*
21322143 * Identify referenced, file-backed active pages and
21332144 * give them one more trip around the active list. So
....@@ -2137,12 +2148,15 @@
21372148 * IO, plus JVM can create lots of anon VM_EXEC pages,
21382149 * so we ignore them here.
21392150 */
2140
- if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
2151
+ if ((vm_flags & VM_EXEC) && page_is_file_lru(page)) {
2152
+ trace_android_vh_page_trylock_clear(page);
2153
+ nr_rotated += thp_nr_pages(page);
21412154 list_add(&page->lru, &l_active);
21422155 continue;
21432156 }
21442157 }
2145
-
2158
+ trace_android_vh_page_trylock_clear(page);
2159
+skip_page_referenced:
21462160 ClearPageActive(page); /* we are de-activating */
21472161 SetPageWorkingset(page);
21482162 list_add(&page->lru, &l_inactive);
....@@ -2152,23 +2166,91 @@
21522166 * Move pages back to the lru list.
21532167 */
21542168 spin_lock_irq(&pgdat->lru_lock);
2155
- /*
2156
- * Count referenced pages from currently used mappings as rotated,
2157
- * even though only some of them are actually re-activated. This
2158
- * helps balance scan pressure between file and anonymous pages in
2159
- * get_scan_count.
2160
- */
2161
- reclaim_stat->recent_rotated[file] += nr_rotated;
21622169
2163
- nr_activate = move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru);
2164
- nr_deactivate = move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE);
2170
+ nr_activate = move_pages_to_lru(lruvec, &l_active);
2171
+ nr_deactivate = move_pages_to_lru(lruvec, &l_inactive);
2172
+ /* Keep all free pages in l_active list */
2173
+ list_splice(&l_inactive, &l_active);
2174
+
2175
+ __count_vm_events(PGDEACTIVATE, nr_deactivate);
2176
+ __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate);
2177
+
21652178 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
21662179 spin_unlock_irq(&pgdat->lru_lock);
21672180
2168
- mem_cgroup_uncharge_list(&l_hold);
2169
- free_unref_page_list(&l_hold);
2181
+ mem_cgroup_uncharge_list(&l_active);
2182
+ free_unref_page_list(&l_active);
21702183 trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate,
21712184 nr_deactivate, nr_rotated, sc->priority, file);
2185
+}
2186
+
2187
+unsigned long reclaim_pages(struct list_head *page_list)
2188
+{
2189
+ int nid = NUMA_NO_NODE;
2190
+ unsigned int nr_reclaimed = 0;
2191
+ LIST_HEAD(node_page_list);
2192
+ struct reclaim_stat dummy_stat;
2193
+ struct page *page;
2194
+ struct scan_control sc = {
2195
+ .gfp_mask = GFP_KERNEL,
2196
+ .priority = DEF_PRIORITY,
2197
+ .may_writepage = 1,
2198
+ .may_unmap = 1,
2199
+ .may_swap = 1,
2200
+ };
2201
+
2202
+ while (!list_empty(page_list)) {
2203
+ page = lru_to_page(page_list);
2204
+ if (nid == NUMA_NO_NODE) {
2205
+ nid = page_to_nid(page);
2206
+ INIT_LIST_HEAD(&node_page_list);
2207
+ }
2208
+
2209
+ if (nid == page_to_nid(page)) {
2210
+ ClearPageActive(page);
2211
+ list_move(&page->lru, &node_page_list);
2212
+ continue;
2213
+ }
2214
+
2215
+ nr_reclaimed += shrink_page_list(&node_page_list,
2216
+ NODE_DATA(nid),
2217
+ &sc, &dummy_stat, false);
2218
+ while (!list_empty(&node_page_list)) {
2219
+ page = lru_to_page(&node_page_list);
2220
+ list_del(&page->lru);
2221
+ putback_lru_page(page);
2222
+ }
2223
+
2224
+ nid = NUMA_NO_NODE;
2225
+ }
2226
+
2227
+ if (!list_empty(&node_page_list)) {
2228
+ nr_reclaimed += shrink_page_list(&node_page_list,
2229
+ NODE_DATA(nid),
2230
+ &sc, &dummy_stat, false);
2231
+ while (!list_empty(&node_page_list)) {
2232
+ page = lru_to_page(&node_page_list);
2233
+ list_del(&page->lru);
2234
+ putback_lru_page(page);
2235
+ }
2236
+ }
2237
+
2238
+ return nr_reclaimed;
2239
+}
2240
+EXPORT_SYMBOL_GPL(reclaim_pages);
2241
+
2242
+static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
2243
+ struct lruvec *lruvec, struct scan_control *sc)
2244
+{
2245
+ if (is_active_lru(lru)) {
2246
+ if (sc->may_deactivate & (1 << is_file_lru(lru)))
2247
+ shrink_active_list(nr_to_scan, lruvec, sc, lru);
2248
+ else
2249
+ sc->skipped_deactivate = 1;
2250
+ return 0;
2251
+ }
2252
+
2253
+ return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
21722254 }
21732255
21742256 /*
....@@ -2199,62 +2281,31 @@
21992281 * 1TB 101 10GB
22002282 * 10TB 320 32GB
22012283 */
2202
-static bool inactive_list_is_low(struct lruvec *lruvec, bool file,
2203
- struct scan_control *sc, bool trace)
2284
+static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru)
22042285 {
2205
- enum lru_list active_lru = file * LRU_FILE + LRU_ACTIVE;
2206
- struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2207
- enum lru_list inactive_lru = file * LRU_FILE;
2286
+ enum lru_list active_lru = inactive_lru + LRU_ACTIVE;
22082287 unsigned long inactive, active;
22092288 unsigned long inactive_ratio;
2210
- unsigned long refaults;
22112289 unsigned long gb;
2290
+ bool skip = false;
22122291
2213
- /*
2214
- * If we don't have swap space, anonymous page deactivation
2215
- * is pointless.
2216
- */
2217
- if (!file && !total_swap_pages)
2218
- return false;
2292
+ inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru);
2293
+ active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru);
22192294
2220
- inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx);
2221
- active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx);
2295
+ gb = (inactive + active) >> (30 - PAGE_SHIFT);
2296
+ trace_android_vh_inactive_is_low(gb, &inactive_ratio, inactive_lru, &skip);
2297
+ if (skip)
2298
+ goto out;
22222299
2223
- /*
2224
- * When refaults are being observed, it means a new workingset
2225
- * is being established. Disable active list protection to get
2226
- * rid of the stale workingset quickly.
2227
- */
2228
- refaults = lruvec_page_state(lruvec, WORKINGSET_ACTIVATE);
2229
- if (file && lruvec->refaults != refaults) {
2230
- inactive_ratio = 0;
2231
- } else {
2232
- gb = (inactive + active) >> (30 - PAGE_SHIFT);
2233
- if (gb)
2234
- inactive_ratio = int_sqrt(10 * gb);
2235
- else
2236
- inactive_ratio = 1;
2237
- }
2300
+ if (gb)
2301
+ inactive_ratio = int_sqrt(10 * gb);
2302
+ else
2303
+ inactive_ratio = 1;
22382304
2239
- if (trace)
2240
- trace_mm_vmscan_inactive_list_is_low(pgdat->node_id, sc->reclaim_idx,
2241
- lruvec_lru_size(lruvec, inactive_lru, MAX_NR_ZONES), inactive,
2242
- lruvec_lru_size(lruvec, active_lru, MAX_NR_ZONES), active,
2243
- inactive_ratio, file);
2305
+ trace_android_vh_tune_inactive_ratio(&inactive_ratio, is_file_lru(inactive_lru));
22442306
2307
+out:
22452308 return inactive * inactive_ratio < active;
2246
-}
2247
-
2248
-static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
2249
- struct lruvec *lruvec, struct scan_control *sc)
2250
-{
2251
- if (is_active_lru(lru)) {
2252
- if (inactive_list_is_low(lruvec, is_file_lru(lru), sc, true))
2253
- shrink_active_list(nr_to_scan, lruvec, sc, lru);
2254
- return 0;
2255
- }
2256
-
2257
- return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
22582309 }
22592310
22602311 enum scan_balance {
....@@ -2273,20 +2324,18 @@
22732324 * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
22742325 * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
22752326 */
2276
-static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg,
2277
- struct scan_control *sc, unsigned long *nr,
2278
- unsigned long *lru_pages)
2327
+static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
2328
+ unsigned long *nr)
22792329 {
2330
+ struct mem_cgroup *memcg = lruvec_memcg(lruvec);
2331
+ unsigned long anon_cost, file_cost, total_cost;
22802332 int swappiness = mem_cgroup_swappiness(memcg);
2281
- struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
2282
- u64 fraction[2];
2333
+ u64 fraction[ANON_AND_FILE];
22832334 u64 denominator = 0; /* gcc */
2284
- struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2285
- unsigned long anon_prio, file_prio;
22862335 enum scan_balance scan_balance;
2287
- unsigned long anon, file;
22882336 unsigned long ap, fp;
22892337 enum lru_list lru;
2338
+ bool balance_anon_file_reclaim = false;
22902339
22912340 /* If we have no swap space, do not bother scanning anon pages. */
22922341 if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) {
....@@ -2294,6 +2343,7 @@
22942343 goto out;
22952344 }
22962345
2346
+ trace_android_vh_tune_swappiness(&swappiness);
22972347 /*
22982348 * Global reclaim will swap to prevent OOM even with no
22992349 * swappiness, but memcg users want to use this knob to
....@@ -2301,7 +2351,7 @@
23012351 * using the memory controller's swap limit feature would be
23022352 * too expensive.
23032353 */
2304
- if (!global_reclaim(sc) && !swappiness) {
2354
+ if (cgroup_reclaim(sc) && !swappiness) {
23052355 scan_balance = SCAN_FILE;
23062356 goto out;
23072357 }
....@@ -2317,129 +2367,133 @@
23172367 }
23182368
23192369 /*
2320
- * Prevent the reclaimer from falling into the cache trap: as
2321
- * cache pages start out inactive, every cache fault will tip
2322
- * the scan balance towards the file LRU. And as the file LRU
2323
- * shrinks, so does the window for rotation from references.
2324
- * This means we have a runaway feedback loop where a tiny
2325
- * thrashing file LRU becomes infinitely more attractive than
2326
- * anon pages. Try to detect this based on file LRU size.
2370
+ * If the system is almost out of file pages, force-scan anon.
23272371 */
2328
- if (global_reclaim(sc)) {
2329
- unsigned long pgdatfile;
2330
- unsigned long pgdatfree;
2331
- int z;
2332
- unsigned long total_high_wmark = 0;
2333
-
2334
- pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES);
2335
- pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) +
2336
- node_page_state(pgdat, NR_INACTIVE_FILE);
2337
-
2338
- for (z = 0; z < MAX_NR_ZONES; z++) {
2339
- struct zone *zone = &pgdat->node_zones[z];
2340
- if (!managed_zone(zone))
2341
- continue;
2342
-
2343
- total_high_wmark += high_wmark_pages(zone);
2344
- }
2345
-
2346
- if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) {
2347
- /*
2348
- * Force SCAN_ANON if there are enough inactive
2349
- * anonymous pages on the LRU in eligible zones.
2350
- * Otherwise, the small LRU gets thrashed.
2351
- */
2352
- if (!inactive_list_is_low(lruvec, false, sc, false) &&
2353
- lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, sc->reclaim_idx)
2354
- >> sc->priority) {
2355
- scan_balance = SCAN_ANON;
2356
- goto out;
2357
- }
2358
- }
2372
+ if (sc->file_is_tiny) {
2373
+ scan_balance = SCAN_ANON;
2374
+ goto out;
23592375 }
23602376
2377
+ trace_android_rvh_set_balance_anon_file_reclaim(&balance_anon_file_reclaim);
2378
+
23612379 /*
2362
- * If there is enough inactive page cache, i.e. if the size of the
2363
- * inactive list is greater than that of the active list *and* the
2364
- * inactive list actually has some pages to scan on this priority, we
2365
- * do not reclaim anything from the anonymous working set right now.
2366
- * Without the second condition we could end up never scanning an
2367
- * lruvec even if it has plenty of old anonymous pages unless the
2368
- * system is under heavy pressure.
2380
+ * If there is enough inactive page cache, we do not reclaim
2381
+ * anything from the anonymous working right now. But when balancing
2382
+ * anon and page cache files for reclaim, allow swapping of anon pages
2383
+ * even if there are a number of inactive file cache pages.
23692384 */
2370
- if (!inactive_list_is_low(lruvec, true, sc, false) &&
2371
- lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) {
2385
+ if (!balance_anon_file_reclaim && sc->cache_trim_mode) {
23722386 scan_balance = SCAN_FILE;
23732387 goto out;
23742388 }
23752389
23762390 scan_balance = SCAN_FRACT;
2377
-
23782391 /*
2379
- * With swappiness at 100, anonymous and file have the same priority.
2380
- * This scanning priority is essentially the inverse of IO cost.
2381
- */
2382
- anon_prio = swappiness;
2383
- file_prio = 200 - anon_prio;
2384
-
2385
- /*
2386
- * OK, so we have swap space and a fair amount of page cache
2387
- * pages. We use the recently rotated / recently scanned
2388
- * ratios to determine how valuable each cache is.
2392
+ * Calculate the pressure balance between anon and file pages.
23892393 *
2390
- * Because workloads change over time (and to avoid overflow)
2391
- * we keep these statistics as a floating average, which ends
2392
- * up weighing recent references more than old ones.
2394
+ * The amount of pressure we put on each LRU is inversely
2395
+ * proportional to the cost of reclaiming each list, as
2396
+ * determined by the share of pages that are refaulting, times
2397
+ * the relative IO cost of bringing back a swapped out
2398
+ * anonymous page vs reloading a filesystem page (swappiness).
23932399 *
2394
- * anon in [0], file in [1]
2400
+ * Although we limit that influence to ensure no list gets
2401
+ * left behind completely: at least a third of the pressure is
2402
+ * applied, before swappiness.
2403
+ *
2404
+ * With swappiness at 100, anon and file have equal IO cost.
23952405 */
2406
+ total_cost = sc->anon_cost + sc->file_cost;
2407
+ anon_cost = total_cost + sc->anon_cost;
2408
+ file_cost = total_cost + sc->file_cost;
2409
+ total_cost = anon_cost + file_cost;
23962410
2397
- anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) +
2398
- lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES);
2399
- file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) +
2400
- lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES);
2411
+ ap = swappiness * (total_cost + 1);
2412
+ ap /= anon_cost + 1;
24012413
2402
- spin_lock_irq(&pgdat->lru_lock);
2403
- if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
2404
- reclaim_stat->recent_scanned[0] /= 2;
2405
- reclaim_stat->recent_rotated[0] /= 2;
2406
- }
2407
-
2408
- if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
2409
- reclaim_stat->recent_scanned[1] /= 2;
2410
- reclaim_stat->recent_rotated[1] /= 2;
2411
- }
2412
-
2413
- /*
2414
- * The amount of pressure on anon vs file pages is inversely
2415
- * proportional to the fraction of recently scanned pages on
2416
- * each list that were recently referenced and in active use.
2417
- */
2418
- ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1);
2419
- ap /= reclaim_stat->recent_rotated[0] + 1;
2420
-
2421
- fp = file_prio * (reclaim_stat->recent_scanned[1] + 1);
2422
- fp /= reclaim_stat->recent_rotated[1] + 1;
2423
- spin_unlock_irq(&pgdat->lru_lock);
2414
+ fp = (200 - swappiness) * (total_cost + 1);
2415
+ fp /= file_cost + 1;
24242416
24252417 fraction[0] = ap;
24262418 fraction[1] = fp;
2427
- denominator = ap + fp + 1;
2419
+ denominator = ap + fp;
24282420 out:
2429
- *lru_pages = 0;
2421
+ trace_android_vh_tune_scan_type((char *)(&scan_balance));
24302422 for_each_evictable_lru(lru) {
24312423 int file = is_file_lru(lru);
2432
- unsigned long size;
2424
+ unsigned long lruvec_size;
2425
+ unsigned long low, min;
24332426 unsigned long scan;
24342427
2435
- size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
2436
- scan = size >> sc->priority;
2428
+ lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
2429
+ mem_cgroup_protection(sc->target_mem_cgroup, memcg,
2430
+ &min, &low);
2431
+
2432
+ if (min || low) {
2433
+ /*
2434
+ * Scale a cgroup's reclaim pressure by proportioning
2435
+ * its current usage to its memory.low or memory.min
2436
+ * setting.
2437
+ *
2438
+ * This is important, as otherwise scanning aggression
2439
+ * becomes extremely binary -- from nothing as we
2440
+ * approach the memory protection threshold, to totally
2441
+ * nominal as we exceed it. This results in requiring
2442
+ * setting extremely liberal protection thresholds. It
2443
+ * also means we simply get no protection at all if we
2444
+ * set it too low, which is not ideal.
2445
+ *
2446
+ * If there is any protection in place, we reduce scan
2447
+ * pressure by how much of the total memory used is
2448
+ * within protection thresholds.
2449
+ *
2450
+ * There is one special case: in the first reclaim pass,
2451
+ * we skip over all groups that are within their low
2452
+ * protection. If that fails to reclaim enough pages to
2453
+ * satisfy the reclaim goal, we come back and override
2454
+ * the best-effort low protection. However, we still
2455
+ * ideally want to honor how well-behaved groups are in
2456
+ * that case instead of simply punishing them all
2457
+ * equally. As such, we reclaim them based on how much
2458
+ * memory they are using, reducing the scan pressure
2459
+ * again by how much of the total memory used is under
2460
+ * hard protection.
2461
+ */
2462
+ unsigned long cgroup_size = mem_cgroup_size(memcg);
2463
+ unsigned long protection;
2464
+
2465
+ /* memory.low scaling, make sure we retry before OOM */
2466
+ if (!sc->memcg_low_reclaim && low > min) {
2467
+ protection = low;
2468
+ sc->memcg_low_skipped = 1;
2469
+ } else {
2470
+ protection = min;
2471
+ }
2472
+
2473
+ /* Avoid TOCTOU with earlier protection check */
2474
+ cgroup_size = max(cgroup_size, protection);
2475
+
2476
+ scan = lruvec_size - lruvec_size * protection /
2477
+ (cgroup_size + 1);
2478
+
2479
+ /*
2480
+ * Minimally target SWAP_CLUSTER_MAX pages to keep
2481
+ * reclaim moving forwards, avoiding decrementing
2482
+ * sc->priority further than desirable.
2483
+ */
2484
+ scan = max(scan, SWAP_CLUSTER_MAX);
2485
+ } else {
2486
+ scan = lruvec_size;
2487
+ }
2488
+
2489
+ scan >>= sc->priority;
2490
+
24372491 /*
24382492 * If the cgroup's already been deleted, make sure to
24392493 * scrape out the remaining cache.
24402494 */
24412495 if (!scan && !mem_cgroup_online(memcg))
2442
- scan = min(size, SWAP_CLUSTER_MAX);
2496
+ scan = min(lruvec_size, SWAP_CLUSTER_MAX);
24432497
24442498 switch (scan_balance) {
24452499 case SCAN_EQUAL:
....@@ -2461,38 +2515,30 @@
24612515 case SCAN_FILE:
24622516 case SCAN_ANON:
24632517 /* Scan one type exclusively */
2464
- if ((scan_balance == SCAN_FILE) != file) {
2465
- size = 0;
2518
+ if ((scan_balance == SCAN_FILE) != file)
24662519 scan = 0;
2467
- }
24682520 break;
24692521 default:
24702522 /* Look ma, no brain */
24712523 BUG();
24722524 }
24732525
2474
- *lru_pages += size;
24752526 nr[lru] = scan;
24762527 }
24772528 }
24782529
2479
-/*
2480
- * This is a basic per-node page freer. Used by both kswapd and direct reclaim.
2481
- */
2482
-static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg,
2483
- struct scan_control *sc, unsigned long *lru_pages)
2530
+static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
24842531 {
2485
- struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg);
24862532 unsigned long nr[NR_LRU_LISTS];
24872533 unsigned long targets[NR_LRU_LISTS];
24882534 unsigned long nr_to_scan;
24892535 enum lru_list lru;
24902536 unsigned long nr_reclaimed = 0;
24912537 unsigned long nr_to_reclaim = sc->nr_to_reclaim;
2538
+ bool proportional_reclaim;
24922539 struct blk_plug plug;
2493
- bool scan_adjusted;
24942540
2495
- get_scan_count(lruvec, memcg, sc, nr, lru_pages);
2541
+ get_scan_count(lruvec, sc, nr);
24962542
24972543 /* Record the original scan target for proportional adjustments later */
24982544 memcpy(targets, nr, sizeof(nr));
....@@ -2508,8 +2554,8 @@
25082554 * abort proportional reclaim if either the file or anon lru has already
25092555 * dropped to zero at the first pass.
25102556 */
2511
- scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() &&
2512
- sc->priority == DEF_PRIORITY);
2557
+ proportional_reclaim = (!cgroup_reclaim(sc) && !current_is_kswapd() &&
2558
+ sc->priority == DEF_PRIORITY);
25132559
25142560 blk_start_plug(&plug);
25152561 while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
....@@ -2529,7 +2575,7 @@
25292575
25302576 cond_resched();
25312577
2532
- if (nr_reclaimed < nr_to_reclaim || scan_adjusted)
2578
+ if (nr_reclaimed < nr_to_reclaim || proportional_reclaim)
25332579 continue;
25342580
25352581 /*
....@@ -2580,8 +2626,6 @@
25802626 nr_scanned = targets[lru] - nr[lru];
25812627 nr[lru] = targets[lru] * (100 - percentage) / 100;
25822628 nr[lru] -= min(nr[lru], nr_scanned);
2583
-
2584
- scan_adjusted = true;
25852629 }
25862630 blk_finish_plug(&plug);
25872631 sc->nr_reclaimed += nr_reclaimed;
....@@ -2590,7 +2634,7 @@
25902634 * Even if we did not try to evict anon pages at all, we want to
25912635 * rebalance the anon lru active/inactive ratio.
25922636 */
2593
- if (inactive_list_is_low(lruvec, false, sc, true))
2637
+ if (total_swap_pages && inactive_is_low(lruvec, LRU_INACTIVE_ANON))
25942638 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
25952639 sc, LRU_ACTIVE_ANON);
25962640 }
....@@ -2610,12 +2654,11 @@
26102654 * Reclaim/compaction is used for high-order allocation requests. It reclaims
26112655 * order-0 pages before compacting the zone. should_continue_reclaim() returns
26122656 * true if more pages should be reclaimed such that when the page allocator
2613
- * calls try_to_compact_zone() that it will have enough free pages to succeed.
2657
+ * calls try_to_compact_pages() that it will have enough free pages to succeed.
26142658 * It will give up earlier than that if there is difficulty reclaiming pages.
26152659 */
26162660 static inline bool should_continue_reclaim(struct pglist_data *pgdat,
26172661 unsigned long nr_reclaimed,
2618
- unsigned long nr_scanned,
26192662 struct scan_control *sc)
26202663 {
26212664 unsigned long pages_for_compaction;
....@@ -2626,40 +2669,18 @@
26262669 if (!in_reclaim_compaction(sc))
26272670 return false;
26282671
2629
- /* Consider stopping depending on scan and reclaim activity */
2630
- if (sc->gfp_mask & __GFP_RETRY_MAYFAIL) {
2631
- /*
2632
- * For __GFP_RETRY_MAYFAIL allocations, stop reclaiming if the
2633
- * full LRU list has been scanned and we are still failing
2634
- * to reclaim pages. This full LRU scan is potentially
2635
- * expensive but a __GFP_RETRY_MAYFAIL caller really wants to succeed
2636
- */
2637
- if (!nr_reclaimed && !nr_scanned)
2638
- return false;
2639
- } else {
2640
- /*
2641
- * For non-__GFP_RETRY_MAYFAIL allocations which can presumably
2642
- * fail without consequence, stop if we failed to reclaim
2643
- * any pages from the last SWAP_CLUSTER_MAX number of
2644
- * pages that were scanned. This will return to the
2645
- * caller faster at the risk reclaim/compaction and
2646
- * the resulting allocation attempt fails
2647
- */
2648
- if (!nr_reclaimed)
2649
- return false;
2650
- }
2651
-
26522672 /*
2653
- * If we have not reclaimed enough pages for compaction and the
2654
- * inactive lists are large enough, continue reclaiming
2673
+ * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX
2674
+ * number of pages that were scanned. This will return to the caller
2675
+ * with the risk reclaim/compaction and the resulting allocation attempt
2676
+ * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL
2677
+ * allocations through requiring that the full LRU list has been scanned
2678
+ * first, by assuming that zero delta of sc->nr_scanned means full LRU
2679
+ * scan, but that approximation was wrong, and there were corner cases
2680
+ * where always a non-zero amount of pages were scanned.
26552681 */
2656
- pages_for_compaction = compact_gap(sc->order);
2657
- inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE);
2658
- if (get_nr_swap_pages() > 0)
2659
- inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON);
2660
- if (sc->nr_reclaimed < pages_for_compaction &&
2661
- inactive_lru_pages > pages_for_compaction)
2662
- return true;
2682
+ if (!nr_reclaimed)
2683
+ return false;
26632684
26642685 /* If compaction would go ahead or the allocation would succeed, stop */
26652686 for (z = 0; z <= sc->reclaim_idx; z++) {
....@@ -2676,179 +2697,262 @@
26762697 ;
26772698 }
26782699 }
2679
- return true;
2700
+
2701
+ /*
2702
+ * If we have not reclaimed enough pages for compaction and the
2703
+ * inactive lists are large enough, continue reclaiming
2704
+ */
2705
+ pages_for_compaction = compact_gap(sc->order);
2706
+ inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE);
2707
+ if (get_nr_swap_pages() > 0)
2708
+ inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON);
2709
+
2710
+ return inactive_lru_pages > pages_for_compaction;
26802711 }
26812712
2682
-static bool pgdat_memcg_congested(pg_data_t *pgdat, struct mem_cgroup *memcg)
2713
+static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc)
26832714 {
2684
- return test_bit(PGDAT_CONGESTED, &pgdat->flags) ||
2685
- (memcg && memcg_congested(pgdat, memcg));
2715
+ struct mem_cgroup *target_memcg = sc->target_mem_cgroup;
2716
+ struct mem_cgroup *memcg;
2717
+
2718
+ memcg = mem_cgroup_iter(target_memcg, NULL, NULL);
2719
+ do {
2720
+ struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
2721
+ unsigned long reclaimed;
2722
+ unsigned long scanned;
2723
+ bool skip = false;
2724
+
2725
+ /*
2726
+ * This loop can become CPU-bound when target memcgs
2727
+ * aren't eligible for reclaim - either because they
2728
+ * don't have any reclaimable pages, or because their
2729
+ * memory is explicitly protected. Avoid soft lockups.
2730
+ */
2731
+ cond_resched();
2732
+
2733
+ trace_android_vh_shrink_node_memcgs(memcg, &skip);
2734
+ if (skip)
2735
+ continue;
2736
+
2737
+ mem_cgroup_calculate_protection(target_memcg, memcg);
2738
+
2739
+ if (mem_cgroup_below_min(memcg)) {
2740
+ /*
2741
+ * Hard protection.
2742
+ * If there is no reclaimable memory, OOM.
2743
+ */
2744
+ continue;
2745
+ } else if (mem_cgroup_below_low(memcg)) {
2746
+ /*
2747
+ * Soft protection.
2748
+ * Respect the protection only as long as
2749
+ * there is an unprotected supply
2750
+ * of reclaimable memory from other cgroups.
2751
+ */
2752
+ if (!sc->memcg_low_reclaim) {
2753
+ sc->memcg_low_skipped = 1;
2754
+ continue;
2755
+ }
2756
+ memcg_memory_event(memcg, MEMCG_LOW);
2757
+ }
2758
+
2759
+ reclaimed = sc->nr_reclaimed;
2760
+ scanned = sc->nr_scanned;
2761
+
2762
+ shrink_lruvec(lruvec, sc);
2763
+
2764
+ shrink_slab(sc->gfp_mask, pgdat->node_id, memcg,
2765
+ sc->priority);
2766
+
2767
+ /* Record the group's reclaim efficiency */
2768
+ vmpressure(sc->gfp_mask, memcg, false,
2769
+ sc->nr_scanned - scanned,
2770
+ sc->nr_reclaimed - reclaimed);
2771
+
2772
+ } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL)));
26862773 }
26872774
2688
-static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc)
2775
+static void shrink_node(pg_data_t *pgdat, struct scan_control *sc)
26892776 {
26902777 struct reclaim_state *reclaim_state = current->reclaim_state;
26912778 unsigned long nr_reclaimed, nr_scanned;
2779
+ struct lruvec *target_lruvec;
26922780 bool reclaimable = false;
2781
+ unsigned long file;
26932782
2694
- do {
2695
- struct mem_cgroup *root = sc->target_mem_cgroup;
2696
- struct mem_cgroup_reclaim_cookie reclaim = {
2697
- .pgdat = pgdat,
2698
- .priority = sc->priority,
2699
- };
2700
- unsigned long node_lru_pages = 0;
2701
- struct mem_cgroup *memcg;
2783
+ target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat);
27022784
2703
- memset(&sc->nr, 0, sizeof(sc->nr));
2785
+again:
2786
+ memset(&sc->nr, 0, sizeof(sc->nr));
27042787
2705
- nr_reclaimed = sc->nr_reclaimed;
2706
- nr_scanned = sc->nr_scanned;
2788
+ nr_reclaimed = sc->nr_reclaimed;
2789
+ nr_scanned = sc->nr_scanned;
27072790
2708
- memcg = mem_cgroup_iter(root, NULL, &reclaim);
2709
- do {
2710
- unsigned long lru_pages;
2711
- unsigned long reclaimed;
2712
- unsigned long scanned;
2791
+ /*
2792
+ * Determine the scan balance between anon and file LRUs.
2793
+ */
2794
+ spin_lock_irq(&pgdat->lru_lock);
2795
+ sc->anon_cost = target_lruvec->anon_cost;
2796
+ sc->file_cost = target_lruvec->file_cost;
2797
+ spin_unlock_irq(&pgdat->lru_lock);
27132798
2714
- /*
2715
- * This loop can become CPU-bound when target memcgs
2716
- * aren't eligible for reclaim - either because they
2717
- * don't have any reclaimable pages, or because their
2718
- * memory is explicitly protected. Avoid soft lockups.
2719
- */
2720
- cond_resched();
2799
+ /*
2800
+ * Target desirable inactive:active list ratios for the anon
2801
+ * and file LRU lists.
2802
+ */
2803
+ if (!sc->force_deactivate) {
2804
+ unsigned long refaults;
27212805
2722
- switch (mem_cgroup_protected(root, memcg)) {
2723
- case MEMCG_PROT_MIN:
2724
- /*
2725
- * Hard protection.
2726
- * If there is no reclaimable memory, OOM.
2727
- */
2806
+ refaults = lruvec_page_state(target_lruvec,
2807
+ WORKINGSET_ACTIVATE_ANON);
2808
+ if (refaults != target_lruvec->refaults[0] ||
2809
+ inactive_is_low(target_lruvec, LRU_INACTIVE_ANON))
2810
+ sc->may_deactivate |= DEACTIVATE_ANON;
2811
+ else
2812
+ sc->may_deactivate &= ~DEACTIVATE_ANON;
2813
+
2814
+ /*
2815
+ * When refaults are being observed, it means a new
2816
+ * workingset is being established. Deactivate to get
2817
+ * rid of any stale active pages quickly.
2818
+ */
2819
+ refaults = lruvec_page_state(target_lruvec,
2820
+ WORKINGSET_ACTIVATE_FILE);
2821
+ if (refaults != target_lruvec->refaults[1] ||
2822
+ inactive_is_low(target_lruvec, LRU_INACTIVE_FILE))
2823
+ sc->may_deactivate |= DEACTIVATE_FILE;
2824
+ else
2825
+ sc->may_deactivate &= ~DEACTIVATE_FILE;
2826
+ } else
2827
+ sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE;
2828
+
2829
+ /*
2830
+ * If we have plenty of inactive file pages that aren't
2831
+ * thrashing, try to reclaim those first before touching
2832
+ * anonymous pages.
2833
+ */
2834
+ file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE);
2835
+ if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE))
2836
+ sc->cache_trim_mode = 1;
2837
+ else
2838
+ sc->cache_trim_mode = 0;
2839
+
2840
+ /*
2841
+ * Prevent the reclaimer from falling into the cache trap: as
2842
+ * cache pages start out inactive, every cache fault will tip
2843
+ * the scan balance towards the file LRU. And as the file LRU
2844
+ * shrinks, so does the window for rotation from references.
2845
+ * This means we have a runaway feedback loop where a tiny
2846
+ * thrashing file LRU becomes infinitely more attractive than
2847
+ * anon pages. Try to detect this based on file LRU size.
2848
+ */
2849
+ if (!cgroup_reclaim(sc)) {
2850
+ unsigned long total_high_wmark = 0;
2851
+ unsigned long free, anon;
2852
+ int z;
2853
+
2854
+ free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES);
2855
+ file = node_page_state(pgdat, NR_ACTIVE_FILE) +
2856
+ node_page_state(pgdat, NR_INACTIVE_FILE);
2857
+
2858
+ for (z = 0; z < MAX_NR_ZONES; z++) {
2859
+ struct zone *zone = &pgdat->node_zones[z];
2860
+ if (!managed_zone(zone))
27282861 continue;
2729
- case MEMCG_PROT_LOW:
2730
- /*
2731
- * Soft protection.
2732
- * Respect the protection only as long as
2733
- * there is an unprotected supply
2734
- * of reclaimable memory from other cgroups.
2735
- */
2736
- if (!sc->memcg_low_reclaim) {
2737
- sc->memcg_low_skipped = 1;
2738
- continue;
2739
- }
2740
- memcg_memory_event(memcg, MEMCG_LOW);
2741
- break;
2742
- case MEMCG_PROT_NONE:
2743
- break;
2744
- }
27452862
2746
- reclaimed = sc->nr_reclaimed;
2747
- scanned = sc->nr_scanned;
2748
- shrink_node_memcg(pgdat, memcg, sc, &lru_pages);
2749
- node_lru_pages += lru_pages;
2750
-
2751
- shrink_slab(sc->gfp_mask, pgdat->node_id,
2752
- memcg, sc->priority);
2753
-
2754
- /* Record the group's reclaim efficiency */
2755
- vmpressure(sc->gfp_mask, memcg, false,
2756
- sc->nr_scanned - scanned,
2757
- sc->nr_reclaimed - reclaimed);
2758
-
2759
- /*
2760
- * Direct reclaim and kswapd have to scan all memory
2761
- * cgroups to fulfill the overall scan target for the
2762
- * node.
2763
- *
2764
- * Limit reclaim, on the other hand, only cares about
2765
- * nr_to_reclaim pages to be reclaimed and it will
2766
- * retry with decreasing priority if one round over the
2767
- * whole hierarchy is not sufficient.
2768
- */
2769
- if (!global_reclaim(sc) &&
2770
- sc->nr_reclaimed >= sc->nr_to_reclaim) {
2771
- mem_cgroup_iter_break(root, memcg);
2772
- break;
2773
- }
2774
- } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim)));
2775
-
2776
- if (reclaim_state) {
2777
- sc->nr_reclaimed += reclaim_state->reclaimed_slab;
2778
- reclaim_state->reclaimed_slab = 0;
2779
- }
2780
-
2781
- /* Record the subtree's reclaim efficiency */
2782
- vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true,
2783
- sc->nr_scanned - nr_scanned,
2784
- sc->nr_reclaimed - nr_reclaimed);
2785
-
2786
- if (sc->nr_reclaimed - nr_reclaimed)
2787
- reclaimable = true;
2788
-
2789
- if (current_is_kswapd()) {
2790
- /*
2791
- * If reclaim is isolating dirty pages under writeback,
2792
- * it implies that the long-lived page allocation rate
2793
- * is exceeding the page laundering rate. Either the
2794
- * global limits are not being effective at throttling
2795
- * processes due to the page distribution throughout
2796
- * zones or there is heavy usage of a slow backing
2797
- * device. The only option is to throttle from reclaim
2798
- * context which is not ideal as there is no guarantee
2799
- * the dirtying process is throttled in the same way
2800
- * balance_dirty_pages() manages.
2801
- *
2802
- * Once a node is flagged PGDAT_WRITEBACK, kswapd will
2803
- * count the number of pages under pages flagged for
2804
- * immediate reclaim and stall if any are encountered
2805
- * in the nr_immediate check below.
2806
- */
2807
- if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken)
2808
- set_bit(PGDAT_WRITEBACK, &pgdat->flags);
2809
-
2810
- /*
2811
- * Tag a node as congested if all the dirty pages
2812
- * scanned were backed by a congested BDI and
2813
- * wait_iff_congested will stall.
2814
- */
2815
- if (sc->nr.dirty && sc->nr.dirty == sc->nr.congested)
2816
- set_bit(PGDAT_CONGESTED, &pgdat->flags);
2817
-
2818
- /* Allow kswapd to start writing pages during reclaim.*/
2819
- if (sc->nr.unqueued_dirty == sc->nr.file_taken)
2820
- set_bit(PGDAT_DIRTY, &pgdat->flags);
2821
-
2822
- /*
2823
- * If kswapd scans pages marked marked for immediate
2824
- * reclaim and under writeback (nr_immediate), it
2825
- * implies that pages are cycling through the LRU
2826
- * faster than they are written so also forcibly stall.
2827
- */
2828
- if (sc->nr.immediate)
2829
- congestion_wait(BLK_RW_ASYNC, HZ/10);
2863
+ total_high_wmark += high_wmark_pages(zone);
28302864 }
28312865
28322866 /*
2833
- * Legacy memcg will stall in page writeback so avoid forcibly
2834
- * stalling in wait_iff_congested().
2867
+ * Consider anon: if that's low too, this isn't a
2868
+ * runaway file reclaim problem, but rather just
2869
+ * extreme pressure. Reclaim as per usual then.
28352870 */
2836
- if (!global_reclaim(sc) && sane_reclaim(sc) &&
2837
- sc->nr.dirty && sc->nr.dirty == sc->nr.congested)
2838
- set_memcg_congestion(pgdat, root, true);
2871
+ anon = node_page_state(pgdat, NR_INACTIVE_ANON);
2872
+
2873
+ sc->file_is_tiny =
2874
+ file + free <= total_high_wmark &&
2875
+ !(sc->may_deactivate & DEACTIVATE_ANON) &&
2876
+ anon >> sc->priority;
2877
+ }
2878
+
2879
+ shrink_node_memcgs(pgdat, sc);
2880
+
2881
+ if (reclaim_state) {
2882
+ sc->nr_reclaimed += reclaim_state->reclaimed_slab;
2883
+ reclaim_state->reclaimed_slab = 0;
2884
+ }
2885
+
2886
+ /* Record the subtree's reclaim efficiency */
2887
+ vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true,
2888
+ sc->nr_scanned - nr_scanned,
2889
+ sc->nr_reclaimed - nr_reclaimed);
2890
+
2891
+ if (sc->nr_reclaimed - nr_reclaimed)
2892
+ reclaimable = true;
2893
+
2894
+ if (current_is_kswapd()) {
2895
+ /*
2896
+ * If reclaim is isolating dirty pages under writeback,
2897
+ * it implies that the long-lived page allocation rate
2898
+ * is exceeding the page laundering rate. Either the
2899
+ * global limits are not being effective at throttling
2900
+ * processes due to the page distribution throughout
2901
+ * zones or there is heavy usage of a slow backing
2902
+ * device. The only option is to throttle from reclaim
2903
+ * context which is not ideal as there is no guarantee
2904
+ * the dirtying process is throttled in the same way
2905
+ * balance_dirty_pages() manages.
2906
+ *
2907
+ * Once a node is flagged PGDAT_WRITEBACK, kswapd will
2908
+ * count the number of pages under pages flagged for
2909
+ * immediate reclaim and stall if any are encountered
2910
+ * in the nr_immediate check below.
2911
+ */
2912
+ if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken)
2913
+ set_bit(PGDAT_WRITEBACK, &pgdat->flags);
2914
+
2915
+ /* Allow kswapd to start writing pages during reclaim.*/
2916
+ if (sc->nr.unqueued_dirty == sc->nr.file_taken)
2917
+ set_bit(PGDAT_DIRTY, &pgdat->flags);
28392918
28402919 /*
2841
- * Stall direct reclaim for IO completions if underlying BDIs
2842
- * and node is congested. Allow kswapd to continue until it
2843
- * starts encountering unqueued dirty pages or cycling through
2844
- * the LRU too quickly.
2920
+ * If kswapd scans pages marked for immediate
2921
+ * reclaim and under writeback (nr_immediate), it
2922
+ * implies that pages are cycling through the LRU
2923
+ * faster than they are written so also forcibly stall.
28452924 */
2846
- if (!sc->hibernation_mode && !current_is_kswapd() &&
2847
- current_may_throttle() && pgdat_memcg_congested(pgdat, root))
2848
- wait_iff_congested(BLK_RW_ASYNC, HZ/10);
2925
+ if (sc->nr.immediate)
2926
+ congestion_wait(BLK_RW_ASYNC, HZ/10);
2927
+ }
28492928
2850
- } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed,
2851
- sc->nr_scanned - nr_scanned, sc));
2929
+ /*
2930
+ * Tag a node/memcg as congested if all the dirty pages
2931
+ * scanned were backed by a congested BDI and
2932
+ * wait_iff_congested will stall.
2933
+ *
2934
+ * Legacy memcg will stall in page writeback so avoid forcibly
2935
+ * stalling in wait_iff_congested().
2936
+ */
2937
+ if ((current_is_kswapd() ||
2938
+ (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) &&
2939
+ sc->nr.dirty && sc->nr.dirty == sc->nr.congested)
2940
+ set_bit(LRUVEC_CONGESTED, &target_lruvec->flags);
2941
+
2942
+ /*
2943
+ * Stall direct reclaim for IO completions if underlying BDIs
2944
+ * and node is congested. Allow kswapd to continue until it
2945
+ * starts encountering unqueued dirty pages or cycling through
2946
+ * the LRU too quickly.
2947
+ */
2948
+ if (!current_is_kswapd() && current_may_throttle() &&
2949
+ !sc->hibernation_mode &&
2950
+ test_bit(LRUVEC_CONGESTED, &target_lruvec->flags))
2951
+ wait_iff_congested(BLK_RW_ASYNC, HZ/10);
2952
+
2953
+ if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed,
2954
+ sc))
2955
+ goto again;
28522956
28532957 /*
28542958 * Kswapd gives up on balancing particular nodes after too
....@@ -2858,8 +2962,6 @@
28582962 */
28592963 if (reclaimable)
28602964 pgdat->kswapd_failures = 0;
2861
-
2862
- return reclaimable;
28632965 }
28642966
28652967 /*
....@@ -2928,7 +3030,7 @@
29283030 * Take care memory controller reclaiming has small influence
29293031 * to global LRU.
29303032 */
2931
- if (global_reclaim(sc)) {
3033
+ if (!cgroup_reclaim(sc)) {
29323034 if (!cpuset_zone_allowed(zone,
29333035 GFP_KERNEL | __GFP_HARDWALL))
29343036 continue;
....@@ -2987,19 +3089,17 @@
29873089 sc->gfp_mask = orig_mask;
29883090 }
29893091
2990
-static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat)
3092
+static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat)
29913093 {
2992
- struct mem_cgroup *memcg;
3094
+ struct lruvec *target_lruvec;
3095
+ unsigned long refaults;
29933096
2994
- memcg = mem_cgroup_iter(root_memcg, NULL, NULL);
2995
- do {
2996
- unsigned long refaults;
2997
- struct lruvec *lruvec;
2998
-
2999
- lruvec = mem_cgroup_lruvec(pgdat, memcg);
3000
- refaults = lruvec_page_state(lruvec, WORKINGSET_ACTIVATE);
3001
- lruvec->refaults = refaults;
3002
- } while ((memcg = mem_cgroup_iter(root_memcg, memcg, NULL)));
3097
+ target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat);
3098
+ refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON);
3099
+ target_lruvec->refaults[0] = refaults;
3100
+ refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE);
3101
+ target_lruvec->refaults[1] = refaults;
3102
+ trace_android_vh_snapshot_refaults(target_lruvec);
30033103 }
30043104
30053105 /*
....@@ -3028,7 +3128,7 @@
30283128 retry:
30293129 delayacct_freepages_start();
30303130
3031
- if (global_reclaim(sc))
3131
+ if (!cgroup_reclaim(sc))
30323132 __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1);
30333133
30343134 do {
....@@ -3057,8 +3157,16 @@
30573157 if (zone->zone_pgdat == last_pgdat)
30583158 continue;
30593159 last_pgdat = zone->zone_pgdat;
3160
+
30603161 snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat);
3061
- set_memcg_congestion(last_pgdat, sc->target_mem_cgroup, false);
3162
+
3163
+ if (cgroup_reclaim(sc)) {
3164
+ struct lruvec *lruvec;
3165
+
3166
+ lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup,
3167
+ zone->zone_pgdat);
3168
+ clear_bit(LRUVEC_CONGESTED, &lruvec->flags);
3169
+ }
30623170 }
30633171
30643172 delayacct_freepages_end();
....@@ -3070,9 +3178,26 @@
30703178 if (sc->compaction_ready)
30713179 return 1;
30723180
3181
+ /*
3182
+ * We make inactive:active ratio decisions based on the node's
3183
+ * composition of memory, but a restrictive reclaim_idx or a
3184
+ * memory.low cgroup setting can exempt large amounts of
3185
+ * memory from reclaim. Neither of which are very common, so
3186
+ * instead of doing costly eligibility calculations of the
3187
+ * entire cgroup subtree up front, we assume the estimates are
3188
+ * good, and retry with forcible deactivation if that fails.
3189
+ */
3190
+ if (sc->skipped_deactivate) {
3191
+ sc->priority = initial_priority;
3192
+ sc->force_deactivate = 1;
3193
+ sc->skipped_deactivate = 0;
3194
+ goto retry;
3195
+ }
3196
+
30733197 /* Untapped cgroup reserves? Don't OOM, retry. */
30743198 if (sc->memcg_low_skipped) {
30753199 sc->priority = initial_priority;
3200
+ sc->force_deactivate = 0;
30763201 sc->memcg_low_reclaim = 1;
30773202 sc->memcg_low_skipped = 0;
30783203 goto retry;
....@@ -3112,8 +3237,8 @@
31123237
31133238 /* kswapd must be awake if processes are being throttled */
31143239 if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
3115
- if (READ_ONCE(pgdat->kswapd_classzone_idx) > ZONE_NORMAL)
3116
- WRITE_ONCE(pgdat->kswapd_classzone_idx, ZONE_NORMAL);
3240
+ if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL)
3241
+ WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL);
31173242
31183243 wake_up_interruptible(&pgdat->kswapd_wait);
31193244 }
....@@ -3246,25 +3371,26 @@
32463371 if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask))
32473372 return 1;
32483373
3249
- trace_mm_vmscan_direct_reclaim_begin(order,
3250
- sc.may_writepage,
3251
- sc.gfp_mask,
3252
- sc.reclaim_idx);
3374
+ set_task_reclaim_state(current, &sc.reclaim_state);
3375
+ trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask);
32533376
32543377 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
32553378
32563379 trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
3380
+ set_task_reclaim_state(current, NULL);
32573381
32583382 return nr_reclaimed;
32593383 }
32603384
32613385 #ifdef CONFIG_MEMCG
32623386
3387
+/* Only used by soft limit reclaim. Do not reuse for anything else. */
32633388 unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg,
32643389 gfp_t gfp_mask, bool noswap,
32653390 pg_data_t *pgdat,
32663391 unsigned long *nr_scanned)
32673392 {
3393
+ struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
32683394 struct scan_control sc = {
32693395 .nr_to_reclaim = SWAP_CLUSTER_MAX,
32703396 .target_mem_cgroup = memcg,
....@@ -3273,15 +3399,14 @@
32733399 .reclaim_idx = MAX_NR_ZONES - 1,
32743400 .may_swap = !noswap,
32753401 };
3276
- unsigned long lru_pages;
3402
+
3403
+ WARN_ON_ONCE(!current->reclaim_state);
32773404
32783405 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
32793406 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
32803407
32813408 trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
3282
- sc.may_writepage,
3283
- sc.gfp_mask,
3284
- sc.reclaim_idx);
3409
+ sc.gfp_mask);
32853410
32863411 /*
32873412 * NOTE: Although we can get the priority field, using it
....@@ -3290,11 +3415,12 @@
32903415 * will pick up pages from other mem cgroup's as well. We hack
32913416 * the priority and make it zero.
32923417 */
3293
- shrink_node_memcg(pgdat, memcg, &sc, &lru_pages);
3418
+ shrink_lruvec(lruvec, &sc);
32943419
32953420 trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
32963421
32973422 *nr_scanned = sc.nr_scanned;
3423
+
32983424 return sc.nr_reclaimed;
32993425 }
33003426
....@@ -3303,10 +3429,7 @@
33033429 gfp_t gfp_mask,
33043430 bool may_swap)
33053431 {
3306
- struct zonelist *zonelist;
33073432 unsigned long nr_reclaimed;
3308
- unsigned long pflags;
3309
- int nid;
33103433 unsigned int noreclaim_flag;
33113434 struct scan_control sc = {
33123435 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
....@@ -3319,78 +3442,101 @@
33193442 .may_unmap = 1,
33203443 .may_swap = may_swap,
33213444 };
3322
-
33233445 /*
3324
- * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't
3325
- * take care of from where we get pages. So the node where we start the
3326
- * scan does not need to be the current node.
3446
+ * Traverse the ZONELIST_FALLBACK zonelist of the current node to put
3447
+ * equal pressure on all the nodes. This is based on the assumption that
3448
+ * the reclaim does not bail out early.
33273449 */
3328
- nid = mem_cgroup_select_victim_node(memcg);
3450
+ struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
33293451
3330
- zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK];
3331
-
3332
- trace_mm_vmscan_memcg_reclaim_begin(0,
3333
- sc.may_writepage,
3334
- sc.gfp_mask,
3335
- sc.reclaim_idx);
3336
-
3337
- psi_memstall_enter(&pflags);
3452
+ set_task_reclaim_state(current, &sc.reclaim_state);
3453
+ trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask);
33383454 noreclaim_flag = memalloc_noreclaim_save();
33393455
33403456 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
33413457
33423458 memalloc_noreclaim_restore(noreclaim_flag);
3343
- psi_memstall_leave(&pflags);
3344
-
33453459 trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
3460
+ set_task_reclaim_state(current, NULL);
33463461
33473462 return nr_reclaimed;
33483463 }
3464
+EXPORT_SYMBOL_GPL(try_to_free_mem_cgroup_pages);
33493465 #endif
33503466
33513467 static void age_active_anon(struct pglist_data *pgdat,
33523468 struct scan_control *sc)
33533469 {
33543470 struct mem_cgroup *memcg;
3471
+ struct lruvec *lruvec;
33553472
33563473 if (!total_swap_pages)
33573474 return;
33583475
3476
+ lruvec = mem_cgroup_lruvec(NULL, pgdat);
3477
+ if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON))
3478
+ return;
3479
+
33593480 memcg = mem_cgroup_iter(NULL, NULL, NULL);
33603481 do {
3361
- struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg);
3362
-
3363
- if (inactive_list_is_low(lruvec, false, sc, true))
3364
- shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
3365
- sc, LRU_ACTIVE_ANON);
3366
-
3482
+ lruvec = mem_cgroup_lruvec(memcg, pgdat);
3483
+ shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
3484
+ sc, LRU_ACTIVE_ANON);
33673485 memcg = mem_cgroup_iter(NULL, memcg, NULL);
33683486 } while (memcg);
33693487 }
33703488
3489
+static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx)
3490
+{
3491
+ int i;
3492
+ struct zone *zone;
3493
+
3494
+ /*
3495
+ * Check for watermark boosts top-down as the higher zones
3496
+ * are more likely to be boosted. Both watermarks and boosts
3497
+ * should not be checked at the same time as reclaim would
3498
+ * start prematurely when there is no boosting and a lower
3499
+ * zone is balanced.
3500
+ */
3501
+ for (i = highest_zoneidx; i >= 0; i--) {
3502
+ zone = pgdat->node_zones + i;
3503
+ if (!managed_zone(zone))
3504
+ continue;
3505
+
3506
+ if (zone->watermark_boost)
3507
+ return true;
3508
+ }
3509
+
3510
+ return false;
3511
+}
3512
+
33713513 /*
33723514 * Returns true if there is an eligible zone balanced for the request order
3373
- * and classzone_idx
3515
+ * and highest_zoneidx
33743516 */
3375
-static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx)
3517
+static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx)
33763518 {
33773519 int i;
33783520 unsigned long mark = -1;
33793521 struct zone *zone;
33803522
3381
- for (i = 0; i <= classzone_idx; i++) {
3523
+ /*
3524
+ * Check watermarks bottom-up as lower zones are more likely to
3525
+ * meet watermarks.
3526
+ */
3527
+ for (i = 0; i <= highest_zoneidx; i++) {
33823528 zone = pgdat->node_zones + i;
33833529
33843530 if (!managed_zone(zone))
33853531 continue;
33863532
33873533 mark = high_wmark_pages(zone);
3388
- if (zone_watermark_ok_safe(zone, order, mark, classzone_idx))
3534
+ if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx))
33893535 return true;
33903536 }
33913537
33923538 /*
3393
- * If a node has no populated zone within classzone_idx, it does not
3539
+ * If a node has no populated zone within highest_zoneidx, it does not
33943540 * need balancing by definition. This can happen if a zone-restricted
33953541 * allocation tries to wake a remote kswapd.
33963542 */
....@@ -3403,7 +3549,9 @@
34033549 /* Clear pgdat state for congested, dirty or under writeback. */
34043550 static void clear_pgdat_congested(pg_data_t *pgdat)
34053551 {
3406
- clear_bit(PGDAT_CONGESTED, &pgdat->flags);
3552
+ struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat);
3553
+
3554
+ clear_bit(LRUVEC_CONGESTED, &lruvec->flags);
34073555 clear_bit(PGDAT_DIRTY, &pgdat->flags);
34083556 clear_bit(PGDAT_WRITEBACK, &pgdat->flags);
34093557 }
....@@ -3414,7 +3562,8 @@
34143562 *
34153563 * Returns true if kswapd is ready to sleep
34163564 */
3417
-static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx)
3565
+static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order,
3566
+ int highest_zoneidx)
34183567 {
34193568 /*
34203569 * The throttled processes are normally woken up in balance_pgdat() as
....@@ -3436,7 +3585,7 @@
34363585 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
34373586 return true;
34383587
3439
- if (pgdat_balanced(pgdat, order, classzone_idx)) {
3588
+ if (pgdat_balanced(pgdat, order, highest_zoneidx)) {
34403589 clear_pgdat_congested(pgdat);
34413590 return true;
34423591 }
....@@ -3496,37 +3645,57 @@
34963645 *
34973646 * kswapd scans the zones in the highmem->normal->dma direction. It skips
34983647 * zones which have free_pages > high_wmark_pages(zone), but once a zone is
3499
- * found to have free_pages <= high_wmark_pages(zone), any page is that zone
3648
+ * found to have free_pages <= high_wmark_pages(zone), any page in that zone
35003649 * or lower is eligible for reclaim until at least one usable zone is
35013650 * balanced.
35023651 */
3503
-static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx)
3652
+static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx)
35043653 {
35053654 int i;
35063655 unsigned long nr_soft_reclaimed;
35073656 unsigned long nr_soft_scanned;
35083657 unsigned long pflags;
3658
+ unsigned long nr_boost_reclaim;
3659
+ unsigned long zone_boosts[MAX_NR_ZONES] = { 0, };
3660
+ bool boosted;
35093661 struct zone *zone;
35103662 struct scan_control sc = {
35113663 .gfp_mask = GFP_KERNEL,
35123664 .order = order,
3513
- .priority = DEF_PRIORITY,
3514
- .may_writepage = !laptop_mode,
35153665 .may_unmap = 1,
3516
- .may_swap = 1,
35173666 };
35183667
3668
+ set_task_reclaim_state(current, &sc.reclaim_state);
35193669 psi_memstall_enter(&pflags);
35203670 __fs_reclaim_acquire();
35213671
35223672 count_vm_event(PAGEOUTRUN);
35233673
3674
+ /*
3675
+ * Account for the reclaim boost. Note that the zone boost is left in
3676
+ * place so that parallel allocations that are near the watermark will
3677
+ * stall or direct reclaim until kswapd is finished.
3678
+ */
3679
+ nr_boost_reclaim = 0;
3680
+ for (i = 0; i <= highest_zoneidx; i++) {
3681
+ zone = pgdat->node_zones + i;
3682
+ if (!managed_zone(zone))
3683
+ continue;
3684
+
3685
+ nr_boost_reclaim += zone->watermark_boost;
3686
+ zone_boosts[i] = zone->watermark_boost;
3687
+ }
3688
+ boosted = nr_boost_reclaim;
3689
+
3690
+restart:
3691
+ sc.priority = DEF_PRIORITY;
35243692 do {
35253693 unsigned long nr_reclaimed = sc.nr_reclaimed;
35263694 bool raise_priority = true;
3695
+ bool balanced;
35273696 bool ret;
35283697
3529
- sc.reclaim_idx = classzone_idx;
3698
+ sc.reclaim_idx = highest_zoneidx;
35303699
35313700 /*
35323701 * If the number of buffer_heads exceeds the maximum allowed
....@@ -3550,12 +3719,38 @@
35503719 }
35513720
35523721 /*
3553
- * Only reclaim if there are no eligible zones. Note that
3554
- * sc.reclaim_idx is not used as buffer_heads_over_limit may
3555
- * have adjusted it.
3722
+ * If the pgdat is imbalanced then ignore boosting and preserve
3723
+ * the watermarks for a later time and restart. Note that the
3724
+ * zone watermarks will be still reset at the end of balancing
3725
+ * on the grounds that the normal reclaim should be enough to
3726
+ * re-evaluate if boosting is required when kswapd next wakes.
35563727 */
3557
- if (pgdat_balanced(pgdat, sc.order, classzone_idx))
3728
+ balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx);
3729
+ if (!balanced && nr_boost_reclaim) {
3730
+ nr_boost_reclaim = 0;
3731
+ goto restart;
3732
+ }
3733
+
3734
+ /*
3735
+ * If boosting is not active then only reclaim if there are no
3736
+ * eligible zones. Note that sc.reclaim_idx is not used as
3737
+ * buffer_heads_over_limit may have adjusted it.
3738
+ */
3739
+ if (!nr_boost_reclaim && balanced)
35583740 goto out;
3741
+
3742
+ /* Limit the priority of boosting to avoid reclaim writeback */
3743
+ if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2)
3744
+ raise_priority = false;
3745
+
3746
+ /*
3747
+ * Do not writeback or swap pages for boosted reclaim. The
3748
+ * intent is to relieve pressure not issue sub-optimal IO
3749
+ * from reclaim context. If no pages are reclaimed, the
3750
+ * reclaim will be aborted.
3751
+ */
3752
+ sc.may_writepage = !laptop_mode && !nr_boost_reclaim;
3753
+ sc.may_swap = !nr_boost_reclaim;
35593754
35603755 /*
35613756 * Do some background aging of the anon list, to give
....@@ -3608,6 +3803,16 @@
36083803 * progress in reclaiming pages
36093804 */
36103805 nr_reclaimed = sc.nr_reclaimed - nr_reclaimed;
3806
+ nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed);
3807
+
3808
+ /*
3809
+ * If reclaim made no progress for a boost, stop reclaim as
3810
+ * IO cannot be queued and it could be an infinite loop in
3811
+ * extreme circumstances.
3812
+ */
3813
+ if (nr_boost_reclaim && !nr_reclaimed)
3814
+ break;
3815
+
36113816 if (raise_priority || !nr_reclaimed)
36123817 sc.priority--;
36133818 } while (sc.priority >= 1);
....@@ -3616,9 +3821,33 @@
36163821 pgdat->kswapd_failures++;
36173822
36183823 out:
3824
+ /* If reclaim was boosted, account for the reclaim done in this pass */
3825
+ if (boosted) {
3826
+ unsigned long flags;
3827
+
3828
+ for (i = 0; i <= highest_zoneidx; i++) {
3829
+ if (!zone_boosts[i])
3830
+ continue;
3831
+
3832
+ /* Increments are under the zone lock */
3833
+ zone = pgdat->node_zones + i;
3834
+ spin_lock_irqsave(&zone->lock, flags);
3835
+ zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]);
3836
+ spin_unlock_irqrestore(&zone->lock, flags);
3837
+ }
3838
+
3839
+ /*
3840
+ * As there is now likely space, wakeup kcompact to defragment
3841
+ * pageblocks.
3842
+ */
3843
+ wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx);
3844
+ }
3845
+
36193846 snapshot_refaults(NULL, pgdat);
36203847 __fs_reclaim_release();
36213848 psi_memstall_leave(&pflags);
3849
+ set_task_reclaim_state(current, NULL);
3850
+
36223851 /*
36233852 * Return the order kswapd stopped reclaiming at as
36243853 * prepare_kswapd_sleep() takes it into account. If another caller
....@@ -3629,22 +3858,22 @@
36293858 }
36303859
36313860 /*
3632
- * The pgdat->kswapd_classzone_idx is used to pass the highest zone index to be
3633
- * reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is not
3634
- * a valid index then either kswapd runs for first time or kswapd couldn't sleep
3635
- * after previous reclaim attempt (node is still unbalanced). In that case
3636
- * return the zone index of the previous kswapd reclaim cycle.
3861
+ * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to
3862
+ * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is
3863
+ * not a valid index then either kswapd runs for first time or kswapd couldn't
3864
+ * sleep after previous reclaim attempt (node is still unbalanced). In that
3865
+ * case return the zone index of the previous kswapd reclaim cycle.
36373866 */
3638
-static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat,
3639
- enum zone_type prev_classzone_idx)
3867
+static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,
3868
+ enum zone_type prev_highest_zoneidx)
36403869 {
3641
- enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_classzone_idx);
3870
+ enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
36423871
3643
- return curr_idx == MAX_NR_ZONES ? prev_classzone_idx : curr_idx;
3872
+ return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx;
36443873 }
36453874
36463875 static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order,
3647
- unsigned int classzone_idx)
3876
+ unsigned int highest_zoneidx)
36483877 {
36493878 long remaining = 0;
36503879 DEFINE_WAIT(wait);
....@@ -3661,7 +3890,7 @@
36613890 * eligible zone balanced that it's also unlikely that compaction will
36623891 * succeed.
36633892 */
3664
- if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
3893
+ if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
36653894 /*
36663895 * Compaction records what page blocks it recently failed to
36673896 * isolate pages from and skips them in the future scanning.
....@@ -3674,18 +3903,19 @@
36743903 * We have freed the memory, now we should compact it to make
36753904 * allocation of the requested order possible.
36763905 */
3677
- wakeup_kcompactd(pgdat, alloc_order, classzone_idx);
3906
+ wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx);
36783907
36793908 remaining = schedule_timeout(HZ/10);
36803909
36813910 /*
3682
- * If woken prematurely then reset kswapd_classzone_idx and
3911
+ * If woken prematurely then reset kswapd_highest_zoneidx and
36833912 * order. The values will either be from a wakeup request or
36843913 * the previous request that slept prematurely.
36853914 */
36863915 if (remaining) {
3687
- WRITE_ONCE(pgdat->kswapd_classzone_idx,
3688
- kswapd_classzone_idx(pgdat, classzone_idx));
3916
+ WRITE_ONCE(pgdat->kswapd_highest_zoneidx,
3917
+ kswapd_highest_zoneidx(pgdat,
3918
+ highest_zoneidx));
36893919
36903920 if (READ_ONCE(pgdat->kswapd_order) < reclaim_order)
36913921 WRITE_ONCE(pgdat->kswapd_order, reclaim_order);
....@@ -3700,7 +3930,7 @@
37003930 * go fully to sleep until explicitly woken up.
37013931 */
37023932 if (!remaining &&
3703
- prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
3933
+ prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
37043934 trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
37053935
37063936 /*
....@@ -3742,18 +3972,13 @@
37423972 static int kswapd(void *p)
37433973 {
37443974 unsigned int alloc_order, reclaim_order;
3745
- unsigned int classzone_idx = MAX_NR_ZONES - 1;
3975
+ unsigned int highest_zoneidx = MAX_NR_ZONES - 1;
37463976 pg_data_t *pgdat = (pg_data_t*)p;
37473977 struct task_struct *tsk = current;
3748
-
3749
- struct reclaim_state reclaim_state = {
3750
- .reclaimed_slab = 0,
3751
- };
37523978 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
37533979
37543980 if (!cpumask_empty(cpumask))
37553981 set_cpus_allowed_ptr(tsk, cpumask);
3756
- current->reclaim_state = &reclaim_state;
37573982
37583983 /*
37593984 * Tell the memory management that we're a "memory allocator",
....@@ -3771,22 +3996,24 @@
37713996 set_freezable();
37723997
37733998 WRITE_ONCE(pgdat->kswapd_order, 0);
3774
- WRITE_ONCE(pgdat->kswapd_classzone_idx, MAX_NR_ZONES);
3999
+ WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
37754000 for ( ; ; ) {
37764001 bool ret;
37774002
37784003 alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order);
3779
- classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx);
4004
+ highest_zoneidx = kswapd_highest_zoneidx(pgdat,
4005
+ highest_zoneidx);
37804006
37814007 kswapd_try_sleep:
37824008 kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order,
3783
- classzone_idx);
4009
+ highest_zoneidx);
37844010
3785
- /* Read the new order and classzone_idx */
4011
+ /* Read the new order and highest_zoneidx */
37864012 alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order);
3787
- classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx);
4013
+ highest_zoneidx = kswapd_highest_zoneidx(pgdat,
4014
+ highest_zoneidx);
37884015 WRITE_ONCE(pgdat->kswapd_order, 0);
3789
- WRITE_ONCE(pgdat->kswapd_classzone_idx, MAX_NR_ZONES);
4016
+ WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
37904017
37914018 ret = try_to_freeze();
37924019 if (kthread_should_stop())
....@@ -3807,17 +4034,57 @@
38074034 * but kcompactd is woken to compact for the original
38084035 * request (alloc_order).
38094036 */
3810
- trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx,
4037
+ trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx,
38114038 alloc_order);
3812
- reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx);
4039
+ reclaim_order = balance_pgdat(pgdat, alloc_order,
4040
+ highest_zoneidx);
38134041 if (reclaim_order < alloc_order)
38144042 goto kswapd_try_sleep;
38154043 }
38164044
38174045 tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD);
3818
- current->reclaim_state = NULL;
38194046
38204047 return 0;
4048
+}
4049
+
4050
+static int kswapd_per_node_run(int nid)
4051
+{
4052
+ pg_data_t *pgdat = NODE_DATA(nid);
4053
+ int hid;
4054
+ int ret = 0;
4055
+
4056
+ for (hid = 0; hid < kswapd_threads; ++hid) {
4057
+ pgdat->mkswapd[hid] = kthread_run(kswapd, pgdat, "kswapd%d:%d",
4058
+ nid, hid);
4059
+ if (IS_ERR(pgdat->mkswapd[hid])) {
4060
+ /* failure at boot is fatal */
4061
+ WARN_ON(system_state < SYSTEM_RUNNING);
4062
+ pr_err("Failed to start kswapd%d on node %d\n",
4063
+ hid, nid);
4064
+ ret = PTR_ERR(pgdat->mkswapd[hid]);
4065
+ pgdat->mkswapd[hid] = NULL;
4066
+ continue;
4067
+ }
4068
+ if (!pgdat->kswapd)
4069
+ pgdat->kswapd = pgdat->mkswapd[hid];
4070
+ }
4071
+
4072
+ return ret;
4073
+}
4074
+
4075
+static void kswapd_per_node_stop(int nid)
4076
+{
4077
+ int hid = 0;
4078
+ struct task_struct *kswapd;
4079
+
4080
+ for (hid = 0; hid < kswapd_threads; hid++) {
4081
+ kswapd = NODE_DATA(nid)->mkswapd[hid];
4082
+ if (kswapd) {
4083
+ kthread_stop(kswapd);
4084
+ NODE_DATA(nid)->mkswapd[hid] = NULL;
4085
+ }
4086
+ }
4087
+ NODE_DATA(nid)->kswapd = NULL;
38214088 }
38224089
38234090 /*
....@@ -3828,7 +4095,7 @@
38284095 * needed.
38294096 */
38304097 void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order,
3831
- enum zone_type classzone_idx)
4098
+ enum zone_type highest_zoneidx)
38324099 {
38334100 pg_data_t *pgdat;
38344101 enum zone_type curr_idx;
....@@ -3840,10 +4107,10 @@
38404107 return;
38414108
38424109 pgdat = zone->zone_pgdat;
3843
- curr_idx = READ_ONCE(pgdat->kswapd_classzone_idx);
4110
+ curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
38444111
3845
- if (curr_idx == MAX_NR_ZONES || curr_idx < classzone_idx)
3846
- WRITE_ONCE(pgdat->kswapd_classzone_idx, classzone_idx);
4112
+ if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx)
4113
+ WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx);
38474114
38484115 if (READ_ONCE(pgdat->kswapd_order) < order)
38494116 WRITE_ONCE(pgdat->kswapd_order, order);
....@@ -3853,7 +4120,8 @@
38534120
38544121 /* Hopeless node, leave it to direct reclaim if possible */
38554122 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ||
3856
- pgdat_balanced(pgdat, order, classzone_idx)) {
4123
+ (pgdat_balanced(pgdat, order, highest_zoneidx) &&
4124
+ !pgdat_watermark_boosted(pgdat, highest_zoneidx))) {
38574125 /*
38584126 * There may be plenty of free memory available, but it's too
38594127 * fragmented for high-order allocations. Wake up kcompactd
....@@ -3862,11 +4130,11 @@
38624130 * ratelimit its work.
38634131 */
38644132 if (!(gfp_flags & __GFP_DIRECT_RECLAIM))
3865
- wakeup_kcompactd(pgdat, order, classzone_idx);
4133
+ wakeup_kcompactd(pgdat, order, highest_zoneidx);
38664134 return;
38674135 }
38684136
3869
- trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, classzone_idx, order,
4137
+ trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order,
38704138 gfp_flags);
38714139 wake_up_interruptible(&pgdat->kswapd_wait);
38724140 }
....@@ -3882,7 +4150,6 @@
38824150 */
38834151 unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
38844152 {
3885
- struct reclaim_state reclaim_state;
38864153 struct scan_control sc = {
38874154 .nr_to_reclaim = nr_to_reclaim,
38884155 .gfp_mask = GFP_HIGHUSER_MOVABLE,
....@@ -3894,45 +4161,22 @@
38944161 .hibernation_mode = 1,
38954162 };
38964163 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
3897
- struct task_struct *p = current;
38984164 unsigned long nr_reclaimed;
38994165 unsigned int noreclaim_flag;
39004166
39014167 fs_reclaim_acquire(sc.gfp_mask);
39024168 noreclaim_flag = memalloc_noreclaim_save();
3903
- reclaim_state.reclaimed_slab = 0;
3904
- p->reclaim_state = &reclaim_state;
4169
+ set_task_reclaim_state(current, &sc.reclaim_state);
39054170
39064171 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
39074172
3908
- p->reclaim_state = NULL;
4173
+ set_task_reclaim_state(current, NULL);
39094174 memalloc_noreclaim_restore(noreclaim_flag);
39104175 fs_reclaim_release(sc.gfp_mask);
39114176
39124177 return nr_reclaimed;
39134178 }
39144179 #endif /* CONFIG_HIBERNATION */
3915
-
3916
-/* It's optimal to keep kswapds on the same CPUs as their memory, but
3917
- not required for correctness. So if the last cpu in a node goes
3918
- away, we get changed to run anywhere: as the first one comes back,
3919
- restore their cpu bindings. */
3920
-static int kswapd_cpu_online(unsigned int cpu)
3921
-{
3922
- int nid;
3923
-
3924
- for_each_node_state(nid, N_MEMORY) {
3925
- pg_data_t *pgdat = NODE_DATA(nid);
3926
- const struct cpumask *mask;
3927
-
3928
- mask = cpumask_of_node(pgdat->node_id);
3929
-
3930
- if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
3931
- /* One of our CPUs online: restore mask */
3932
- set_cpus_allowed_ptr(pgdat->kswapd, mask);
3933
- }
3934
- return 0;
3935
-}
39364180
39374181 /*
39384182 * This kswapd start function will be called by init and node-hot-add.
....@@ -3945,6 +4189,9 @@
39454189
39464190 if (pgdat->kswapd)
39474191 return 0;
4192
+
4193
+ if (kswapd_threads > 1)
4194
+ return kswapd_per_node_run(nid);
39484195
39494196 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
39504197 if (IS_ERR(pgdat->kswapd)) {
....@@ -3965,6 +4212,11 @@
39654212 {
39664213 struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
39674214
4215
+ if (kswapd_threads > 1) {
4216
+ kswapd_per_node_stop(nid);
4217
+ return;
4218
+ }
4219
+
39684220 if (kswapd) {
39694221 kthread_stop(kswapd);
39704222 NODE_DATA(nid)->kswapd = NULL;
....@@ -3973,15 +4225,11 @@
39734225
39744226 static int __init kswapd_init(void)
39754227 {
3976
- int nid, ret;
4228
+ int nid;
39774229
39784230 swap_setup();
39794231 for_each_node_state(nid, N_MEMORY)
39804232 kswapd_run(nid);
3981
- ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
3982
- "mm/vmscan:online", kswapd_cpu_online,
3983
- NULL);
3984
- WARN_ON(ret < 0);
39854233 return 0;
39864234 }
39874235
....@@ -3996,10 +4244,13 @@
39964244 */
39974245 int node_reclaim_mode __read_mostly;
39984246
3999
-#define RECLAIM_OFF 0
4000
-#define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
4001
-#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
4002
-#define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */
4247
+/*
4248
+ * These bit locations are exposed in the vm.zone_reclaim_mode sysctl
4249
+ * ABI. New bits are OK, but existing bits can never change.
4250
+ */
4251
+#define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
4252
+#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
4253
+#define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */
40034254
40044255 /*
40054256 * Priority for NODE_RECLAIM. This determines the fraction of pages
....@@ -4070,7 +4321,6 @@
40704321 /* Minimum pages needed in order to stay on node */
40714322 const unsigned long nr_pages = 1 << order;
40724323 struct task_struct *p = current;
4073
- struct reclaim_state reclaim_state;
40744324 unsigned int noreclaim_flag;
40754325 struct scan_control sc = {
40764326 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
....@@ -4083,6 +4333,9 @@
40834333 .reclaim_idx = gfp_zone(gfp_mask),
40844334 };
40854335
4336
+ trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order,
4337
+ sc.gfp_mask);
4338
+
40864339 cond_resched();
40874340 fs_reclaim_acquire(sc.gfp_mask);
40884341 /*
....@@ -4092,8 +4345,7 @@
40924345 */
40934346 noreclaim_flag = memalloc_noreclaim_save();
40944347 p->flags |= PF_SWAPWRITE;
4095
- reclaim_state.reclaimed_slab = 0;
4096
- p->reclaim_state = &reclaim_state;
4348
+ set_task_reclaim_state(p, &sc.reclaim_state);
40974349
40984350 if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) {
40994351 /*
....@@ -4105,10 +4357,13 @@
41054357 } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
41064358 }
41074359
4108
- p->reclaim_state = NULL;
4360
+ set_task_reclaim_state(p, NULL);
41094361 current->flags &= ~PF_SWAPWRITE;
41104362 memalloc_noreclaim_restore(noreclaim_flag);
41114363 fs_reclaim_release(sc.gfp_mask);
4364
+
4365
+ trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed);
4366
+
41124367 return sc.nr_reclaimed >= nr_pages;
41134368 }
41144369
....@@ -4127,7 +4382,8 @@
41274382 * unmapped file backed pages.
41284383 */
41294384 if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages &&
4130
- node_page_state(pgdat, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages)
4385
+ node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <=
4386
+ pgdat->min_slab_pages)
41314387 return NODE_RECLAIM_FULL;
41324388
41334389 /*
....@@ -4158,29 +4414,6 @@
41584414 }
41594415 #endif
41604416
4161
-/*
4162
- * page_evictable - test whether a page is evictable
4163
- * @page: the page to test
4164
- *
4165
- * Test whether page is evictable--i.e., should be placed on active/inactive
4166
- * lists vs unevictable list.
4167
- *
4168
- * Reasons page might not be evictable:
4169
- * (1) page's mapping marked unevictable
4170
- * (2) page is part of an mlocked VMA
4171
- *
4172
- */
4173
-int page_evictable(struct page *page)
4174
-{
4175
- int ret;
4176
-
4177
- /* Prevent address_space of inode and swap cache from being freed */
4178
- rcu_read_lock();
4179
- ret = !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
4180
- rcu_read_unlock();
4181
- return ret;
4182
-}
4183
-
41844417 /**
41854418 * check_move_unevictable_pages - check pages for evictability and move to
41864419 * appropriate zone lru list
....@@ -4201,8 +4434,14 @@
42014434 for (i = 0; i < pvec->nr; i++) {
42024435 struct page *page = pvec->pages[i];
42034436 struct pglist_data *pagepgdat = page_pgdat(page);
4437
+ int nr_pages;
42044438
4205
- pgscanned++;
4439
+ if (PageTransTail(page))
4440
+ continue;
4441
+
4442
+ nr_pages = thp_nr_pages(page);
4443
+ pgscanned += nr_pages;
4444
+
42064445 if (pagepgdat != pgdat) {
42074446 if (pgdat)
42084447 spin_unlock_irq(&pgdat->lru_lock);
....@@ -4221,7 +4460,7 @@
42214460 ClearPageUnevictable(page);
42224461 del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
42234462 add_page_to_lru_list(page, lruvec, lru);
4224
- pgrescued++;
4463
+ pgrescued += nr_pages;
42254464 }
42264465 }
42274466