hc
2023-12-09 958e46acc8e900e8569dd467c1af9b8d2d019394
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,26 @@
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;
11131131
1132
+ memset(stat, 0, sizeof(*stat));
11141133 cond_resched();
11151134
11161135 while (!list_empty(page_list)) {
11171136 struct address_space *mapping;
11181137 struct page *page;
1119
- int may_enter_fs;
1120
- enum page_references references = PAGEREF_RECLAIM_CLEAN;
1121
- bool dirty, writeback;
1138
+ enum page_references references = PAGEREF_RECLAIM;
1139
+ bool dirty, writeback, may_enter_fs;
1140
+ unsigned int nr_pages;
11221141
11231142 cond_resched();
11241143
....@@ -1130,18 +1149,16 @@
11301149
11311150 VM_BUG_ON_PAGE(PageActive(page), page);
11321151
1133
- sc->nr_scanned++;
1152
+ nr_pages = compound_nr(page);
1153
+
1154
+ /* Account the number of base pages even though THP */
1155
+ sc->nr_scanned += nr_pages;
11341156
11351157 if (unlikely(!page_evictable(page)))
11361158 goto activate_locked;
11371159
11381160 if (!sc->may_unmap && page_mapped(page))
11391161 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++;
11451162
11461163 may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
11471164 (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
....@@ -1154,10 +1171,10 @@
11541171 */
11551172 page_check_dirty_writeback(page, &dirty, &writeback);
11561173 if (dirty || writeback)
1157
- nr_dirty++;
1174
+ stat->nr_dirty++;
11581175
11591176 if (dirty && !writeback)
1160
- nr_unqueued_dirty++;
1177
+ stat->nr_unqueued_dirty++;
11611178
11621179 /*
11631180 * Treat this page as congested if the underlying BDI is or if
....@@ -1169,7 +1186,7 @@
11691186 if (((dirty || writeback) && mapping &&
11701187 inode_write_congested(mapping->host)) ||
11711188 (writeback && PageReclaim(page)))
1172
- nr_congested++;
1189
+ stat->nr_congested++;
11731190
11741191 /*
11751192 * If a page at the tail of the LRU is under writeback, there
....@@ -1218,11 +1235,11 @@
12181235 if (current_is_kswapd() &&
12191236 PageReclaim(page) &&
12201237 test_bit(PGDAT_WRITEBACK, &pgdat->flags)) {
1221
- nr_immediate++;
1238
+ stat->nr_immediate++;
12221239 goto activate_locked;
12231240
12241241 /* Case 2 above */
1225
- } else if (sane_reclaim(sc) ||
1242
+ } else if (writeback_throttling_sane(sc) ||
12261243 !PageReclaim(page) || !may_enter_fs) {
12271244 /*
12281245 * This is slightly racy - end_page_writeback()
....@@ -1236,7 +1253,7 @@
12361253 * and it's also appropriate in global reclaim.
12371254 */
12381255 SetPageReclaim(page);
1239
- nr_writeback++;
1256
+ stat->nr_writeback++;
12401257 goto activate_locked;
12411258
12421259 /* Case 3 above */
....@@ -1249,14 +1266,14 @@
12491266 }
12501267 }
12511268
1252
- if (!force_reclaim)
1269
+ if (!ignore_references)
12531270 references = page_check_references(page, sc);
12541271
12551272 switch (references) {
12561273 case PAGEREF_ACTIVATE:
12571274 goto activate_locked;
12581275 case PAGEREF_KEEP:
1259
- nr_ref_keep++;
1276
+ stat->nr_ref_keep += nr_pages;
12601277 goto keep_locked;
12611278 case PAGEREF_RECLAIM:
12621279 case PAGEREF_RECLAIM_CLEAN:
....@@ -1271,6 +1288,8 @@
12711288 if (PageAnon(page) && PageSwapBacked(page)) {
12721289 if (!PageSwapCache(page)) {
12731290 if (!(sc->gfp_mask & __GFP_IO))
1291
+ goto keep_locked;
1292
+ if (page_maybe_dma_pinned(page))
12741293 goto keep_locked;
12751294 if (PageTransHuge(page)) {
12761295 /* cannot split THP, skip it */
....@@ -1288,7 +1307,7 @@
12881307 }
12891308 if (!add_to_swap(page)) {
12901309 if (!PageTransHuge(page))
1291
- goto activate_locked;
1310
+ goto activate_locked_split;
12921311 /* Fallback to swap normal pages */
12931312 if (split_huge_page_to_list(page,
12941313 page_list))
....@@ -1297,10 +1316,10 @@
12971316 count_vm_event(THP_SWPOUT_FALLBACK);
12981317 #endif
12991318 if (!add_to_swap(page))
1300
- goto activate_locked;
1319
+ goto activate_locked_split;
13011320 }
13021321
1303
- may_enter_fs = 1;
1322
+ may_enter_fs = true;
13041323
13051324 /* Adding to swap updated mapping */
13061325 mapping = page_mapping(page);
....@@ -1312,16 +1331,33 @@
13121331 }
13131332
13141333 /*
1334
+ * THP may get split above, need minus tail pages and update
1335
+ * nr_pages to avoid accounting tail pages twice.
1336
+ *
1337
+ * The tail pages that are added into swap cache successfully
1338
+ * reach here.
1339
+ */
1340
+ if ((nr_pages > 1) && !PageTransHuge(page)) {
1341
+ sc->nr_scanned -= (nr_pages - 1);
1342
+ nr_pages = 1;
1343
+ }
1344
+
1345
+ /*
13151346 * The page is mapped into the page tables of one or more
13161347 * processes. Try to unmap it here.
13171348 */
13181349 if (page_mapped(page)) {
1319
- enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH;
1350
+ enum ttu_flags flags = TTU_BATCH_FLUSH;
1351
+ bool was_swapbacked = PageSwapBacked(page);
13201352
13211353 if (unlikely(PageTransHuge(page)))
13221354 flags |= TTU_SPLIT_HUGE_PMD;
1355
+ if (!ignore_references)
1356
+ trace_android_vh_page_trylock_set(page);
13231357 if (!try_to_unmap(page, flags)) {
1324
- nr_unmap_fail++;
1358
+ stat->nr_unmap_fail += nr_pages;
1359
+ if (!was_swapbacked && PageSwapBacked(page))
1360
+ stat->nr_lazyfree_fail += nr_pages;
13251361 goto activate_locked;
13261362 }
13271363 }
....@@ -1337,7 +1373,7 @@
13371373 * the rest of the LRU for clean pages and see
13381374 * the same dirty pages again (PageReclaim).
13391375 */
1340
- if (page_is_file_cache(page) &&
1376
+ if (page_is_file_lru(page) &&
13411377 (!current_is_kswapd() || !PageReclaim(page) ||
13421378 !test_bit(PGDAT_DIRTY, &pgdat->flags))) {
13431379 /*
....@@ -1365,12 +1401,14 @@
13651401 * starts and then write it out here.
13661402 */
13671403 try_to_unmap_flush_dirty();
1368
- switch (pageout(page, mapping, sc)) {
1404
+ switch (pageout(page, mapping)) {
13691405 case PAGE_KEEP:
13701406 goto keep_locked;
13711407 case PAGE_ACTIVATE:
13721408 goto activate_locked;
13731409 case PAGE_SUCCESS:
1410
+ stat->nr_pageout += thp_nr_pages(page);
1411
+
13741412 if (PageWriteback(page))
13751413 goto keep;
13761414 if (PageDirty(page))
....@@ -1426,6 +1464,7 @@
14261464 * increment nr_reclaimed here (and
14271465 * leave it off the LRU).
14281466 */
1467
+ trace_android_vh_page_trylock_clear(page);
14291468 nr_reclaimed++;
14301469 continue;
14311470 }
....@@ -1443,30 +1482,38 @@
14431482
14441483 count_vm_event(PGLAZYFREED);
14451484 count_memcg_page_event(page, PGLAZYFREED);
1446
- } else if (!mapping || !__remove_mapping(mapping, page, true))
1485
+ } else if (!mapping || !__remove_mapping(mapping, page, true,
1486
+ sc->target_mem_cgroup))
14471487 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);
1488
+
1489
+ unlock_page(page);
14561490 free_it:
1457
- nr_reclaimed++;
1491
+ /*
1492
+ * THP may get swapped out in a whole, need account
1493
+ * all base pages.
1494
+ */
1495
+ nr_reclaimed += nr_pages;
14581496
14591497 /*
14601498 * Is there need to periodically free_page_list? It would
14611499 * appear not as the counts should be low
14621500 */
1463
- if (unlikely(PageTransHuge(page))) {
1464
- mem_cgroup_uncharge(page);
1465
- (*get_compound_page_dtor(page))(page);
1466
- } else
1501
+ trace_android_vh_page_trylock_clear(page);
1502
+ if (unlikely(PageTransHuge(page)))
1503
+ destroy_compound_page(page);
1504
+ else
14671505 list_add(&page->lru, &free_pages);
14681506 continue;
14691507
1508
+activate_locked_split:
1509
+ /*
1510
+ * The tail pages that are failed to add into swap cache
1511
+ * reach here. Fixup nr_scanned and nr_pages.
1512
+ */
1513
+ if (nr_pages > 1) {
1514
+ sc->nr_scanned -= (nr_pages - 1);
1515
+ nr_pages = 1;
1516
+ }
14701517 activate_locked:
14711518 /* Not a candidate for swapping, so reclaim swap space. */
14721519 if (PageSwapCache(page) && (mem_cgroup_swap_full(page) ||
....@@ -1474,8 +1521,9 @@
14741521 try_to_free_swap(page);
14751522 VM_BUG_ON_PAGE(PageActive(page), page);
14761523 if (!PageMlocked(page)) {
1524
+ int type = page_is_file_lru(page);
14771525 SetPageActive(page);
1478
- pgactivate++;
1526
+ stat->nr_activate[type] += nr_pages;
14791527 count_memcg_page_event(page, PGACTIVATE);
14801528 }
14811529 keep_locked:
....@@ -1485,6 +1533,8 @@
14851533 VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page);
14861534 }
14871535
1536
+ pgactivate = stat->nr_activate[0] + stat->nr_activate[1];
1537
+
14881538 mem_cgroup_uncharge_list(&free_pages);
14891539 try_to_unmap_flush();
14901540 free_unref_page_list(&free_pages);
....@@ -1492,20 +1542,10 @@
14921542 list_splice(&ret_pages, page_list);
14931543 count_vm_events(PGACTIVATE, pgactivate);
14941544
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
- }
15051545 return nr_reclaimed;
15061546 }
15071547
1508
-unsigned long reclaim_clean_pages_from_list(struct zone *zone,
1548
+unsigned int reclaim_clean_pages_from_list(struct zone *zone,
15091549 struct list_head *page_list)
15101550 {
15111551 struct scan_control sc = {
....@@ -1513,23 +1553,35 @@
15131553 .priority = DEF_PRIORITY,
15141554 .may_unmap = 1,
15151555 };
1516
- unsigned long ret;
1556
+ struct reclaim_stat stat;
1557
+ unsigned int nr_reclaimed;
15171558 struct page *page, *next;
15181559 LIST_HEAD(clean_pages);
15191560
15201561 list_for_each_entry_safe(page, next, page_list, lru) {
1521
- if (page_is_file_cache(page) && !PageDirty(page) &&
1562
+ if (page_is_file_lru(page) && !PageDirty(page) &&
15221563 !__PageMovable(page) && !PageUnevictable(page)) {
15231564 ClearPageActive(page);
15241565 list_move(&page->lru, &clean_pages);
15251566 }
15261567 }
15271568
1528
- ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc,
1529
- TTU_IGNORE_ACCESS, NULL, true);
1569
+ nr_reclaimed = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc,
1570
+ &stat, true);
15301571 list_splice(&clean_pages, page_list);
1531
- mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret);
1532
- return ret;
1572
+ mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
1573
+ -(long)nr_reclaimed);
1574
+ /*
1575
+ * Since lazyfree pages are isolated from file LRU from the beginning,
1576
+ * they will rotate back to anonymous LRU in the end if it failed to
1577
+ * discard so isolated count will be mismatched.
1578
+ * Compensate the isolated count for both LRU lists.
1579
+ */
1580
+ mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON,
1581
+ stat.nr_lazyfree_fail);
1582
+ mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE,
1583
+ -(long)stat.nr_lazyfree_fail);
1584
+ return nr_reclaimed;
15331585 }
15341586
15351587 /*
....@@ -1612,7 +1664,7 @@
16121664
16131665 /*
16141666 * 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.
1667
+ * be complete before mem_cgroup_update_lru_size due to a sanity check.
16161668 */
16171669 static __always_inline void update_lru_sizes(struct lruvec *lruvec,
16181670 enum lru_list lru, unsigned long *nr_zone_taken)
....@@ -1623,16 +1675,13 @@
16231675 if (!nr_zone_taken[zid])
16241676 continue;
16251677
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
1678
+ update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
16301679 }
16311680
16321681 }
16331682
1634
-/*
1635
- * zone_lru_lock is heavily contended. Some of the functions that
1683
+/**
1684
+ * pgdat->lru_lock is heavily contended. Some of the functions that
16361685 * shrink the lists perform better by taking out a batch of pages
16371686 * and working on them outside the LRU lock.
16381687 *
....@@ -1646,7 +1695,6 @@
16461695 * @dst: The temp list to put pages on to.
16471696 * @nr_scanned: The number of pages that were scanned.
16481697 * @sc: The scan_control struct for this reclaim session
1649
- * @mode: One of the LRU isolation modes
16501698 * @lru: LRU list id for isolating
16511699 *
16521700 * returns how many pages were moved onto *@dst.
....@@ -1654,7 +1702,7 @@
16541702 static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
16551703 struct lruvec *lruvec, struct list_head *dst,
16561704 unsigned long *nr_scanned, struct scan_control *sc,
1657
- isolate_mode_t mode, enum lru_list lru)
1705
+ enum lru_list lru)
16581706 {
16591707 struct list_head *src = &lruvec->lists[lru];
16601708 unsigned long nr_taken = 0;
....@@ -1663,11 +1711,11 @@
16631711 unsigned long skipped = 0;
16641712 unsigned long scan, total_scan, nr_pages;
16651713 LIST_HEAD(pages_skipped);
1714
+ isolate_mode_t mode = (sc->may_unmap ? 0 : ISOLATE_UNMAPPED);
16661715
1716
+ total_scan = 0;
16671717 scan = 0;
1668
- for (total_scan = 0;
1669
- scan < nr_to_scan && nr_taken < nr_to_scan && !list_empty(src);
1670
- total_scan++) {
1718
+ while (scan < nr_to_scan && !list_empty(src)) {
16711719 struct page *page;
16721720
16731721 page = lru_to_page(src);
....@@ -1675,9 +1723,12 @@
16751723
16761724 VM_BUG_ON_PAGE(!PageLRU(page), page);
16771725
1726
+ nr_pages = compound_nr(page);
1727
+ total_scan += nr_pages;
1728
+
16781729 if (page_zonenum(page) > sc->reclaim_idx) {
16791730 list_move(&page->lru, &pages_skipped);
1680
- nr_skipped[page_zonenum(page)]++;
1731
+ nr_skipped[page_zonenum(page)] += nr_pages;
16811732 continue;
16821733 }
16831734
....@@ -1686,13 +1737,17 @@
16861737 * return with no isolated pages if the LRU mostly contains
16871738 * ineligible pages. This causes the VM to not reclaim any
16881739 * pages, triggering a premature OOM.
1740
+ *
1741
+ * Account all tail pages of THP. This would not cause
1742
+ * premature OOM since __isolate_lru_page() returns -EBUSY
1743
+ * only when the page is being freed somewhere else.
16891744 */
1690
- scan++;
1745
+ scan += nr_pages;
16911746 switch (__isolate_lru_page(page, mode)) {
16921747 case 0:
1693
- nr_pages = hpage_nr_pages(page);
16941748 nr_taken += nr_pages;
16951749 nr_zone_taken[page_zonenum(page)] += nr_pages;
1750
+ trace_android_vh_del_page_from_lrulist(page, false, lru);
16961751 list_move(&page->lru, dst);
16971752 break;
16981753
....@@ -1753,7 +1808,7 @@
17531808 * Restrictions:
17541809 *
17551810 * (1) Must be called with an elevated refcount on the page. This is a
1756
- * fundamentnal difference from isolate_lru_pages (which is called
1811
+ * fundamental difference from isolate_lru_pages (which is called
17571812 * without a stable reference).
17581813 * (2) the lru_lock must not be held.
17591814 * (3) interrupts must be enabled.
....@@ -1766,11 +1821,11 @@
17661821 WARN_RATELIMIT(PageTail(page), "trying to isolate tail page");
17671822
17681823 if (PageLRU(page)) {
1769
- struct zone *zone = page_zone(page);
1824
+ pg_data_t *pgdat = page_pgdat(page);
17701825 struct lruvec *lruvec;
17711826
1772
- spin_lock_irq(zone_lru_lock(zone));
1773
- lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
1827
+ spin_lock_irq(&pgdat->lru_lock);
1828
+ lruvec = mem_cgroup_page_lruvec(page, pgdat);
17741829 if (PageLRU(page)) {
17751830 int lru = page_lru(page);
17761831 get_page(page);
....@@ -1778,14 +1833,14 @@
17781833 del_page_from_lru_list(page, lruvec, lru);
17791834 ret = 0;
17801835 }
1781
- spin_unlock_irq(zone_lru_lock(zone));
1836
+ spin_unlock_irq(&pgdat->lru_lock);
17821837 }
17831838 return ret;
17841839 }
17851840
17861841 /*
17871842 * 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
1843
+ * then get rescheduled. When there are massive number of tasks doing page
17891844 * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
17901845 * the LRU list will go small and be scanned faster than necessary, leading to
17911846 * unnecessary swapping, thrashing and OOM.
....@@ -1798,7 +1853,7 @@
17981853 if (current_is_kswapd())
17991854 return 0;
18001855
1801
- if (!sane_reclaim(sc))
1856
+ if (!writeback_throttling_sane(sc))
18021857 return 0;
18031858
18041859 if (file) {
....@@ -1820,40 +1875,55 @@
18201875 return isolated > inactive;
18211876 }
18221877
1823
-static noinline_for_stack void
1824
-putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list)
1878
+/*
1879
+ * This moves pages from @list to corresponding LRU list.
1880
+ *
1881
+ * We move them the other way if the page is referenced by one or more
1882
+ * processes, from rmap.
1883
+ *
1884
+ * If the pages are mostly unmapped, the processing is fast and it is
1885
+ * appropriate to hold zone_lru_lock across the whole operation. But if
1886
+ * the pages are mapped, the processing is slow (page_referenced()) so we
1887
+ * should drop zone_lru_lock around each page. It's impossible to balance
1888
+ * this, so instead we remove the pages from the LRU while processing them.
1889
+ * It is safe to rely on PG_active against the non-LRU pages in here because
1890
+ * nobody will play with that bit on a non-LRU page.
1891
+ *
1892
+ * The downside is that we have to touch page->_refcount against each page.
1893
+ * But we had to alter page->flags anyway.
1894
+ *
1895
+ * Returns the number of pages moved to the given lruvec.
1896
+ */
1897
+
1898
+static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
1899
+ struct list_head *list)
18251900 {
1826
- struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
18271901 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
1902
+ int nr_pages, nr_moved = 0;
18281903 LIST_HEAD(pages_to_free);
1904
+ struct page *page;
1905
+ enum lru_list lru;
18291906
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
-
1907
+ while (!list_empty(list)) {
1908
+ page = lru_to_page(list);
18371909 VM_BUG_ON_PAGE(PageLRU(page), page);
1838
- list_del(&page->lru);
18391910 if (unlikely(!page_evictable(page))) {
1911
+ list_del(&page->lru);
18401912 spin_unlock_irq(&pgdat->lru_lock);
18411913 putback_lru_page(page);
18421914 spin_lock_irq(&pgdat->lru_lock);
18431915 continue;
18441916 }
1845
-
18461917 lruvec = mem_cgroup_page_lruvec(page, pgdat);
18471918
18481919 SetPageLRU(page);
18491920 lru = page_lru(page);
1850
- add_page_to_lru_list(page, lruvec, lru);
18511921
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
- }
1922
+ nr_pages = thp_nr_pages(page);
1923
+ update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
1924
+ list_move(&page->lru, &lruvec->lists[lru]);
1925
+ trace_android_vh_add_page_to_lrulist(page, false, lru);
1926
+
18571927 if (put_page_testzero(page)) {
18581928 __ClearPageLRU(page);
18591929 __ClearPageActive(page);
....@@ -1861,29 +1931,34 @@
18611931
18621932 if (unlikely(PageCompound(page))) {
18631933 spin_unlock_irq(&pgdat->lru_lock);
1864
- mem_cgroup_uncharge(page);
1865
- (*get_compound_page_dtor(page))(page);
1934
+ destroy_compound_page(page);
18661935 spin_lock_irq(&pgdat->lru_lock);
18671936 } else
18681937 list_add(&page->lru, &pages_to_free);
1938
+ } else {
1939
+ nr_moved += nr_pages;
1940
+ if (PageActive(page))
1941
+ workingset_age_nonresident(lruvec, nr_pages);
18691942 }
18701943 }
18711944
18721945 /*
18731946 * To save our caller's stack, now use input list for pages to free.
18741947 */
1875
- list_splice(&pages_to_free, page_list);
1948
+ list_splice(&pages_to_free, list);
1949
+
1950
+ return nr_moved;
18761951 }
18771952
18781953 /*
18791954 * 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.
1955
+ * a backing device by writing to the page cache it sets PF_LOCAL_THROTTLE.
18811956 * In that case we should only throttle if the backing device it is
18821957 * writing to is congested. In other cases it is safe to throttle.
18831958 */
18841959 static int current_may_throttle(void)
18851960 {
1886
- return !(current->flags & PF_LESS_THROTTLE) ||
1961
+ return !(current->flags & PF_LOCAL_THROTTLE) ||
18871962 current->backing_dev_info == NULL ||
18881963 bdi_write_congested(current->backing_dev_info);
18891964 }
....@@ -1898,13 +1973,12 @@
18981973 {
18991974 LIST_HEAD(page_list);
19001975 unsigned long nr_scanned;
1901
- unsigned long nr_reclaimed = 0;
1976
+ unsigned int nr_reclaimed = 0;
19021977 unsigned long nr_taken;
1903
- struct reclaim_stat stat = {};
1904
- isolate_mode_t isolate_mode = 0;
1905
- int file = is_file_lru(lru);
1978
+ struct reclaim_stat stat;
1979
+ bool file = is_file_lru(lru);
1980
+ enum vm_event_item item;
19061981 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
1907
- struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
19081982 bool stalled = false;
19091983
19101984 while (unlikely(too_many_isolated(pgdat, file, sc))) {
....@@ -1922,54 +1996,37 @@
19221996
19231997 lru_add_drain();
19241998
1925
- if (!sc->may_unmap)
1926
- isolate_mode |= ISOLATE_UNMAPPED;
1927
-
19281999 spin_lock_irq(&pgdat->lru_lock);
19292000
19302001 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
1931
- &nr_scanned, sc, isolate_mode, lru);
2002
+ &nr_scanned, sc, lru);
19322003
19332004 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
1934
- reclaim_stat->recent_scanned[file] += nr_taken;
2005
+ item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT;
2006
+ if (!cgroup_reclaim(sc))
2007
+ __count_vm_events(item, nr_scanned);
2008
+ __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned);
2009
+ __count_vm_events(PGSCAN_ANON + file, nr_scanned);
19352010
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
- }
19472011 spin_unlock_irq(&pgdat->lru_lock);
19482012
19492013 if (nr_taken == 0)
19502014 return 0;
19512015
1952
- nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0,
1953
- &stat, false);
2016
+ nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, &stat, false);
2017
+ trace_android_vh_handle_failed_page_trylock(&page_list);
19542018
19552019 spin_lock_irq(&pgdat->lru_lock);
19562020
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);
2021
+ move_pages_to_lru(lruvec, &page_list);
19702022
19712023 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
1972
-
2024
+ lru_note_cost(lruvec, file, stat.nr_pageout);
2025
+ item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT;
2026
+ if (!cgroup_reclaim(sc))
2027
+ __count_vm_events(item, nr_reclaimed);
2028
+ __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed);
2029
+ __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed);
19732030 spin_unlock_irq(&pgdat->lru_lock);
19742031
19752032 mem_cgroup_uncharge_list(&page_list);
....@@ -2003,73 +2060,6 @@
20032060 return nr_reclaimed;
20042061 }
20052062
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
-
20732063 static void shrink_active_list(unsigned long nr_to_scan,
20742064 struct lruvec *lruvec,
20752065 struct scan_control *sc,
....@@ -2082,28 +2072,25 @@
20822072 LIST_HEAD(l_active);
20832073 LIST_HEAD(l_inactive);
20842074 struct page *page;
2085
- struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
20862075 unsigned nr_deactivate, nr_activate;
20872076 unsigned nr_rotated = 0;
2088
- isolate_mode_t isolate_mode = 0;
20892077 int file = is_file_lru(lru);
20902078 struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2079
+ bool bypass = false;
2080
+ bool should_protect = false;
20912081
20922082 lru_add_drain();
2093
-
2094
- if (!sc->may_unmap)
2095
- isolate_mode |= ISOLATE_UNMAPPED;
20962083
20972084 spin_lock_irq(&pgdat->lru_lock);
20982085
20992086 nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
2100
- &nr_scanned, sc, isolate_mode, lru);
2087
+ &nr_scanned, sc, lru);
21012088
21022089 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
2103
- reclaim_stat->recent_scanned[file] += nr_taken;
21042090
2105
- __count_vm_events(PGREFILL, nr_scanned);
2106
- count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned);
2091
+ if (!cgroup_reclaim(sc))
2092
+ __count_vm_events(PGREFILL, nr_scanned);
2093
+ __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned);
21072094
21082095 spin_unlock_irq(&pgdat->lru_lock);
21092096
....@@ -2125,9 +2112,20 @@
21252112 }
21262113 }
21272114
2115
+ trace_android_vh_page_should_be_protected(page, &should_protect);
2116
+ if (unlikely(should_protect)) {
2117
+ nr_rotated += thp_nr_pages(page);
2118
+ list_add(&page->lru, &l_active);
2119
+ continue;
2120
+ }
2121
+
2122
+ trace_android_vh_page_referenced_check_bypass(page, nr_to_scan, lru, &bypass);
2123
+ if (bypass)
2124
+ goto skip_page_referenced;
2125
+ trace_android_vh_page_trylock_set(page);
2126
+ /* Referenced or rmap lock contention: rotate */
21282127 if (page_referenced(page, 0, sc->target_mem_cgroup,
2129
- &vm_flags)) {
2130
- nr_rotated += hpage_nr_pages(page);
2128
+ &vm_flags) != 0) {
21312129 /*
21322130 * Identify referenced, file-backed active pages and
21332131 * give them one more trip around the active list. So
....@@ -2137,12 +2135,15 @@
21372135 * IO, plus JVM can create lots of anon VM_EXEC pages,
21382136 * so we ignore them here.
21392137 */
2140
- if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
2138
+ if ((vm_flags & VM_EXEC) && page_is_file_lru(page)) {
2139
+ trace_android_vh_page_trylock_clear(page);
2140
+ nr_rotated += thp_nr_pages(page);
21412141 list_add(&page->lru, &l_active);
21422142 continue;
21432143 }
21442144 }
2145
-
2145
+ trace_android_vh_page_trylock_clear(page);
2146
+skip_page_referenced:
21462147 ClearPageActive(page); /* we are de-activating */
21472148 SetPageWorkingset(page);
21482149 list_add(&page->lru, &l_inactive);
....@@ -2152,23 +2153,91 @@
21522153 * Move pages back to the lru list.
21532154 */
21542155 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;
21622156
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);
2157
+ nr_activate = move_pages_to_lru(lruvec, &l_active);
2158
+ nr_deactivate = move_pages_to_lru(lruvec, &l_inactive);
2159
+ /* Keep all free pages in l_active list */
2160
+ list_splice(&l_inactive, &l_active);
2161
+
2162
+ __count_vm_events(PGDEACTIVATE, nr_deactivate);
2163
+ __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate);
2164
+
21652165 __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
21662166 spin_unlock_irq(&pgdat->lru_lock);
21672167
2168
- mem_cgroup_uncharge_list(&l_hold);
2169
- free_unref_page_list(&l_hold);
2168
+ mem_cgroup_uncharge_list(&l_active);
2169
+ free_unref_page_list(&l_active);
21702170 trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate,
21712171 nr_deactivate, nr_rotated, sc->priority, file);
2172
+}
2173
+
2174
+unsigned long reclaim_pages(struct list_head *page_list)
2175
+{
2176
+ int nid = NUMA_NO_NODE;
2177
+ unsigned int nr_reclaimed = 0;
2178
+ LIST_HEAD(node_page_list);
2179
+ struct reclaim_stat dummy_stat;
2180
+ struct page *page;
2181
+ struct scan_control sc = {
2182
+ .gfp_mask = GFP_KERNEL,
2183
+ .priority = DEF_PRIORITY,
2184
+ .may_writepage = 1,
2185
+ .may_unmap = 1,
2186
+ .may_swap = 1,
2187
+ };
2188
+
2189
+ while (!list_empty(page_list)) {
2190
+ page = lru_to_page(page_list);
2191
+ if (nid == NUMA_NO_NODE) {
2192
+ nid = page_to_nid(page);
2193
+ INIT_LIST_HEAD(&node_page_list);
2194
+ }
2195
+
2196
+ if (nid == page_to_nid(page)) {
2197
+ ClearPageActive(page);
2198
+ list_move(&page->lru, &node_page_list);
2199
+ continue;
2200
+ }
2201
+
2202
+ nr_reclaimed += shrink_page_list(&node_page_list,
2203
+ NODE_DATA(nid),
2204
+ &sc, &dummy_stat, false);
2205
+ while (!list_empty(&node_page_list)) {
2206
+ page = lru_to_page(&node_page_list);
2207
+ list_del(&page->lru);
2208
+ putback_lru_page(page);
2209
+ }
2210
+
2211
+ nid = NUMA_NO_NODE;
2212
+ }
2213
+
2214
+ if (!list_empty(&node_page_list)) {
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
+
2225
+ return nr_reclaimed;
2226
+}
2227
+EXPORT_SYMBOL_GPL(reclaim_pages);
2228
+
2229
+static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
2230
+ struct lruvec *lruvec, struct scan_control *sc)
2231
+{
2232
+ if (is_active_lru(lru)) {
2233
+ if (sc->may_deactivate & (1 << is_file_lru(lru)))
2234
+ shrink_active_list(nr_to_scan, lruvec, sc, lru);
2235
+ else
2236
+ sc->skipped_deactivate = 1;
2237
+ return 0;
2238
+ }
2239
+
2240
+ return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
21722241 }
21732242
21742243 /*
....@@ -2199,62 +2268,31 @@
21992268 * 1TB 101 10GB
22002269 * 10TB 320 32GB
22012270 */
2202
-static bool inactive_list_is_low(struct lruvec *lruvec, bool file,
2203
- struct scan_control *sc, bool trace)
2271
+static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru)
22042272 {
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;
2273
+ enum lru_list active_lru = inactive_lru + LRU_ACTIVE;
22082274 unsigned long inactive, active;
22092275 unsigned long inactive_ratio;
2210
- unsigned long refaults;
22112276 unsigned long gb;
2277
+ bool skip = false;
22122278
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;
2279
+ inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru);
2280
+ active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru);
22192281
2220
- inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx);
2221
- active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx);
2282
+ gb = (inactive + active) >> (30 - PAGE_SHIFT);
2283
+ trace_android_vh_inactive_is_low(gb, &inactive_ratio, inactive_lru, &skip);
2284
+ if (skip)
2285
+ goto out;
22222286
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
- }
2287
+ if (gb)
2288
+ inactive_ratio = int_sqrt(10 * gb);
2289
+ else
2290
+ inactive_ratio = 1;
22382291
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);
2292
+ trace_android_vh_tune_inactive_ratio(&inactive_ratio, is_file_lru(inactive_lru));
22442293
2294
+out:
22452295 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);
22582296 }
22592297
22602298 enum scan_balance {
....@@ -2273,20 +2311,18 @@
22732311 * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
22742312 * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
22752313 */
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)
2314
+static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
2315
+ unsigned long *nr)
22792316 {
2317
+ struct mem_cgroup *memcg = lruvec_memcg(lruvec);
2318
+ unsigned long anon_cost, file_cost, total_cost;
22802319 int swappiness = mem_cgroup_swappiness(memcg);
2281
- struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
2282
- u64 fraction[2];
2320
+ u64 fraction[ANON_AND_FILE];
22832321 u64 denominator = 0; /* gcc */
2284
- struct pglist_data *pgdat = lruvec_pgdat(lruvec);
2285
- unsigned long anon_prio, file_prio;
22862322 enum scan_balance scan_balance;
2287
- unsigned long anon, file;
22882323 unsigned long ap, fp;
22892324 enum lru_list lru;
2325
+ bool balance_anon_file_reclaim = false;
22902326
22912327 /* If we have no swap space, do not bother scanning anon pages. */
22922328 if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) {
....@@ -2294,6 +2330,7 @@
22942330 goto out;
22952331 }
22962332
2333
+ trace_android_vh_tune_swappiness(&swappiness);
22972334 /*
22982335 * Global reclaim will swap to prevent OOM even with no
22992336 * swappiness, but memcg users want to use this knob to
....@@ -2301,7 +2338,7 @@
23012338 * using the memory controller's swap limit feature would be
23022339 * too expensive.
23032340 */
2304
- if (!global_reclaim(sc) && !swappiness) {
2341
+ if (cgroup_reclaim(sc) && !swappiness) {
23052342 scan_balance = SCAN_FILE;
23062343 goto out;
23072344 }
....@@ -2317,129 +2354,133 @@
23172354 }
23182355
23192356 /*
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.
2357
+ * If the system is almost out of file pages, force-scan anon.
23272358 */
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
- }
2359
+ if (sc->file_is_tiny) {
2360
+ scan_balance = SCAN_ANON;
2361
+ goto out;
23592362 }
23602363
2364
+ trace_android_rvh_set_balance_anon_file_reclaim(&balance_anon_file_reclaim);
2365
+
23612366 /*
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.
2367
+ * If there is enough inactive page cache, we do not reclaim
2368
+ * anything from the anonymous working right now. But when balancing
2369
+ * anon and page cache files for reclaim, allow swapping of anon pages
2370
+ * even if there are a number of inactive file cache pages.
23692371 */
2370
- if (!inactive_list_is_low(lruvec, true, sc, false) &&
2371
- lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) {
2372
+ if (!balance_anon_file_reclaim && sc->cache_trim_mode) {
23722373 scan_balance = SCAN_FILE;
23732374 goto out;
23742375 }
23752376
23762377 scan_balance = SCAN_FRACT;
2377
-
23782378 /*
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.
2379
+ * Calculate the pressure balance between anon and file pages.
23892380 *
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.
2381
+ * The amount of pressure we put on each LRU is inversely
2382
+ * proportional to the cost of reclaiming each list, as
2383
+ * determined by the share of pages that are refaulting, times
2384
+ * the relative IO cost of bringing back a swapped out
2385
+ * anonymous page vs reloading a filesystem page (swappiness).
23932386 *
2394
- * anon in [0], file in [1]
2387
+ * Although we limit that influence to ensure no list gets
2388
+ * left behind completely: at least a third of the pressure is
2389
+ * applied, before swappiness.
2390
+ *
2391
+ * With swappiness at 100, anon and file have equal IO cost.
23952392 */
2393
+ total_cost = sc->anon_cost + sc->file_cost;
2394
+ anon_cost = total_cost + sc->anon_cost;
2395
+ file_cost = total_cost + sc->file_cost;
2396
+ total_cost = anon_cost + file_cost;
23962397
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);
2398
+ ap = swappiness * (total_cost + 1);
2399
+ ap /= anon_cost + 1;
24012400
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);
2401
+ fp = (200 - swappiness) * (total_cost + 1);
2402
+ fp /= file_cost + 1;
24242403
24252404 fraction[0] = ap;
24262405 fraction[1] = fp;
2427
- denominator = ap + fp + 1;
2406
+ denominator = ap + fp;
24282407 out:
2429
- *lru_pages = 0;
2408
+ trace_android_vh_tune_scan_type((char *)(&scan_balance));
24302409 for_each_evictable_lru(lru) {
24312410 int file = is_file_lru(lru);
2432
- unsigned long size;
2411
+ unsigned long lruvec_size;
2412
+ unsigned long low, min;
24332413 unsigned long scan;
24342414
2435
- size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
2436
- scan = size >> sc->priority;
2415
+ lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
2416
+ mem_cgroup_protection(sc->target_mem_cgroup, memcg,
2417
+ &min, &low);
2418
+
2419
+ if (min || low) {
2420
+ /*
2421
+ * Scale a cgroup's reclaim pressure by proportioning
2422
+ * its current usage to its memory.low or memory.min
2423
+ * setting.
2424
+ *
2425
+ * This is important, as otherwise scanning aggression
2426
+ * becomes extremely binary -- from nothing as we
2427
+ * approach the memory protection threshold, to totally
2428
+ * nominal as we exceed it. This results in requiring
2429
+ * setting extremely liberal protection thresholds. It
2430
+ * also means we simply get no protection at all if we
2431
+ * set it too low, which is not ideal.
2432
+ *
2433
+ * If there is any protection in place, we reduce scan
2434
+ * pressure by how much of the total memory used is
2435
+ * within protection thresholds.
2436
+ *
2437
+ * There is one special case: in the first reclaim pass,
2438
+ * we skip over all groups that are within their low
2439
+ * protection. If that fails to reclaim enough pages to
2440
+ * satisfy the reclaim goal, we come back and override
2441
+ * the best-effort low protection. However, we still
2442
+ * ideally want to honor how well-behaved groups are in
2443
+ * that case instead of simply punishing them all
2444
+ * equally. As such, we reclaim them based on how much
2445
+ * memory they are using, reducing the scan pressure
2446
+ * again by how much of the total memory used is under
2447
+ * hard protection.
2448
+ */
2449
+ unsigned long cgroup_size = mem_cgroup_size(memcg);
2450
+ unsigned long protection;
2451
+
2452
+ /* memory.low scaling, make sure we retry before OOM */
2453
+ if (!sc->memcg_low_reclaim && low > min) {
2454
+ protection = low;
2455
+ sc->memcg_low_skipped = 1;
2456
+ } else {
2457
+ protection = min;
2458
+ }
2459
+
2460
+ /* Avoid TOCTOU with earlier protection check */
2461
+ cgroup_size = max(cgroup_size, protection);
2462
+
2463
+ scan = lruvec_size - lruvec_size * protection /
2464
+ (cgroup_size + 1);
2465
+
2466
+ /*
2467
+ * Minimally target SWAP_CLUSTER_MAX pages to keep
2468
+ * reclaim moving forwards, avoiding decrementing
2469
+ * sc->priority further than desirable.
2470
+ */
2471
+ scan = max(scan, SWAP_CLUSTER_MAX);
2472
+ } else {
2473
+ scan = lruvec_size;
2474
+ }
2475
+
2476
+ scan >>= sc->priority;
2477
+
24372478 /*
24382479 * If the cgroup's already been deleted, make sure to
24392480 * scrape out the remaining cache.
24402481 */
24412482 if (!scan && !mem_cgroup_online(memcg))
2442
- scan = min(size, SWAP_CLUSTER_MAX);
2483
+ scan = min(lruvec_size, SWAP_CLUSTER_MAX);
24432484
24442485 switch (scan_balance) {
24452486 case SCAN_EQUAL:
....@@ -2461,38 +2502,30 @@
24612502 case SCAN_FILE:
24622503 case SCAN_ANON:
24632504 /* Scan one type exclusively */
2464
- if ((scan_balance == SCAN_FILE) != file) {
2465
- size = 0;
2505
+ if ((scan_balance == SCAN_FILE) != file)
24662506 scan = 0;
2467
- }
24682507 break;
24692508 default:
24702509 /* Look ma, no brain */
24712510 BUG();
24722511 }
24732512
2474
- *lru_pages += size;
24752513 nr[lru] = scan;
24762514 }
24772515 }
24782516
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)
2517
+static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
24842518 {
2485
- struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg);
24862519 unsigned long nr[NR_LRU_LISTS];
24872520 unsigned long targets[NR_LRU_LISTS];
24882521 unsigned long nr_to_scan;
24892522 enum lru_list lru;
24902523 unsigned long nr_reclaimed = 0;
24912524 unsigned long nr_to_reclaim = sc->nr_to_reclaim;
2525
+ bool proportional_reclaim;
24922526 struct blk_plug plug;
2493
- bool scan_adjusted;
24942527
2495
- get_scan_count(lruvec, memcg, sc, nr, lru_pages);
2528
+ get_scan_count(lruvec, sc, nr);
24962529
24972530 /* Record the original scan target for proportional adjustments later */
24982531 memcpy(targets, nr, sizeof(nr));
....@@ -2508,8 +2541,8 @@
25082541 * abort proportional reclaim if either the file or anon lru has already
25092542 * dropped to zero at the first pass.
25102543 */
2511
- scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() &&
2512
- sc->priority == DEF_PRIORITY);
2544
+ proportional_reclaim = (!cgroup_reclaim(sc) && !current_is_kswapd() &&
2545
+ sc->priority == DEF_PRIORITY);
25132546
25142547 blk_start_plug(&plug);
25152548 while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
....@@ -2529,7 +2562,7 @@
25292562
25302563 cond_resched();
25312564
2532
- if (nr_reclaimed < nr_to_reclaim || scan_adjusted)
2565
+ if (nr_reclaimed < nr_to_reclaim || proportional_reclaim)
25332566 continue;
25342567
25352568 /*
....@@ -2580,8 +2613,6 @@
25802613 nr_scanned = targets[lru] - nr[lru];
25812614 nr[lru] = targets[lru] * (100 - percentage) / 100;
25822615 nr[lru] -= min(nr[lru], nr_scanned);
2583
-
2584
- scan_adjusted = true;
25852616 }
25862617 blk_finish_plug(&plug);
25872618 sc->nr_reclaimed += nr_reclaimed;
....@@ -2590,7 +2621,7 @@
25902621 * Even if we did not try to evict anon pages at all, we want to
25912622 * rebalance the anon lru active/inactive ratio.
25922623 */
2593
- if (inactive_list_is_low(lruvec, false, sc, true))
2624
+ if (total_swap_pages && inactive_is_low(lruvec, LRU_INACTIVE_ANON))
25942625 shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
25952626 sc, LRU_ACTIVE_ANON);
25962627 }
....@@ -2610,12 +2641,11 @@
26102641 * Reclaim/compaction is used for high-order allocation requests. It reclaims
26112642 * order-0 pages before compacting the zone. should_continue_reclaim() returns
26122643 * 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.
2644
+ * calls try_to_compact_pages() that it will have enough free pages to succeed.
26142645 * It will give up earlier than that if there is difficulty reclaiming pages.
26152646 */
26162647 static inline bool should_continue_reclaim(struct pglist_data *pgdat,
26172648 unsigned long nr_reclaimed,
2618
- unsigned long nr_scanned,
26192649 struct scan_control *sc)
26202650 {
26212651 unsigned long pages_for_compaction;
....@@ -2626,40 +2656,18 @@
26262656 if (!in_reclaim_compaction(sc))
26272657 return false;
26282658
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
-
26522659 /*
2653
- * If we have not reclaimed enough pages for compaction and the
2654
- * inactive lists are large enough, continue reclaiming
2660
+ * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX
2661
+ * number of pages that were scanned. This will return to the caller
2662
+ * with the risk reclaim/compaction and the resulting allocation attempt
2663
+ * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL
2664
+ * allocations through requiring that the full LRU list has been scanned
2665
+ * first, by assuming that zero delta of sc->nr_scanned means full LRU
2666
+ * scan, but that approximation was wrong, and there were corner cases
2667
+ * where always a non-zero amount of pages were scanned.
26552668 */
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;
2669
+ if (!nr_reclaimed)
2670
+ return false;
26632671
26642672 /* If compaction would go ahead or the allocation would succeed, stop */
26652673 for (z = 0; z <= sc->reclaim_idx; z++) {
....@@ -2676,179 +2684,262 @@
26762684 ;
26772685 }
26782686 }
2679
- return true;
2687
+
2688
+ /*
2689
+ * If we have not reclaimed enough pages for compaction and the
2690
+ * inactive lists are large enough, continue reclaiming
2691
+ */
2692
+ pages_for_compaction = compact_gap(sc->order);
2693
+ inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE);
2694
+ if (get_nr_swap_pages() > 0)
2695
+ inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON);
2696
+
2697
+ return inactive_lru_pages > pages_for_compaction;
26802698 }
26812699
2682
-static bool pgdat_memcg_congested(pg_data_t *pgdat, struct mem_cgroup *memcg)
2700
+static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc)
26832701 {
2684
- return test_bit(PGDAT_CONGESTED, &pgdat->flags) ||
2685
- (memcg && memcg_congested(pgdat, memcg));
2702
+ struct mem_cgroup *target_memcg = sc->target_mem_cgroup;
2703
+ struct mem_cgroup *memcg;
2704
+
2705
+ memcg = mem_cgroup_iter(target_memcg, NULL, NULL);
2706
+ do {
2707
+ struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
2708
+ unsigned long reclaimed;
2709
+ unsigned long scanned;
2710
+ bool skip = false;
2711
+
2712
+ /*
2713
+ * This loop can become CPU-bound when target memcgs
2714
+ * aren't eligible for reclaim - either because they
2715
+ * don't have any reclaimable pages, or because their
2716
+ * memory is explicitly protected. Avoid soft lockups.
2717
+ */
2718
+ cond_resched();
2719
+
2720
+ trace_android_vh_shrink_node_memcgs(memcg, &skip);
2721
+ if (skip)
2722
+ continue;
2723
+
2724
+ mem_cgroup_calculate_protection(target_memcg, memcg);
2725
+
2726
+ if (mem_cgroup_below_min(memcg)) {
2727
+ /*
2728
+ * Hard protection.
2729
+ * If there is no reclaimable memory, OOM.
2730
+ */
2731
+ continue;
2732
+ } else if (mem_cgroup_below_low(memcg)) {
2733
+ /*
2734
+ * Soft protection.
2735
+ * Respect the protection only as long as
2736
+ * there is an unprotected supply
2737
+ * of reclaimable memory from other cgroups.
2738
+ */
2739
+ if (!sc->memcg_low_reclaim) {
2740
+ sc->memcg_low_skipped = 1;
2741
+ continue;
2742
+ }
2743
+ memcg_memory_event(memcg, MEMCG_LOW);
2744
+ }
2745
+
2746
+ reclaimed = sc->nr_reclaimed;
2747
+ scanned = sc->nr_scanned;
2748
+
2749
+ shrink_lruvec(lruvec, sc);
2750
+
2751
+ shrink_slab(sc->gfp_mask, pgdat->node_id, memcg,
2752
+ 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
+ } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL)));
26862760 }
26872761
2688
-static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc)
2762
+static void shrink_node(pg_data_t *pgdat, struct scan_control *sc)
26892763 {
26902764 struct reclaim_state *reclaim_state = current->reclaim_state;
26912765 unsigned long nr_reclaimed, nr_scanned;
2766
+ struct lruvec *target_lruvec;
26922767 bool reclaimable = false;
2768
+ unsigned long file;
26932769
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;
2770
+ target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat);
27022771
2703
- memset(&sc->nr, 0, sizeof(sc->nr));
2772
+again:
2773
+ memset(&sc->nr, 0, sizeof(sc->nr));
27042774
2705
- nr_reclaimed = sc->nr_reclaimed;
2706
- nr_scanned = sc->nr_scanned;
2775
+ nr_reclaimed = sc->nr_reclaimed;
2776
+ nr_scanned = sc->nr_scanned;
27072777
2708
- memcg = mem_cgroup_iter(root, NULL, &reclaim);
2709
- do {
2710
- unsigned long lru_pages;
2711
- unsigned long reclaimed;
2712
- unsigned long scanned;
2778
+ /*
2779
+ * Determine the scan balance between anon and file LRUs.
2780
+ */
2781
+ spin_lock_irq(&pgdat->lru_lock);
2782
+ sc->anon_cost = target_lruvec->anon_cost;
2783
+ sc->file_cost = target_lruvec->file_cost;
2784
+ spin_unlock_irq(&pgdat->lru_lock);
27132785
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();
2786
+ /*
2787
+ * Target desirable inactive:active list ratios for the anon
2788
+ * and file LRU lists.
2789
+ */
2790
+ if (!sc->force_deactivate) {
2791
+ unsigned long refaults;
27212792
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
- */
2793
+ refaults = lruvec_page_state(target_lruvec,
2794
+ WORKINGSET_ACTIVATE_ANON);
2795
+ if (refaults != target_lruvec->refaults[0] ||
2796
+ inactive_is_low(target_lruvec, LRU_INACTIVE_ANON))
2797
+ sc->may_deactivate |= DEACTIVATE_ANON;
2798
+ else
2799
+ sc->may_deactivate &= ~DEACTIVATE_ANON;
2800
+
2801
+ /*
2802
+ * When refaults are being observed, it means a new
2803
+ * workingset is being established. Deactivate to get
2804
+ * rid of any stale active pages quickly.
2805
+ */
2806
+ refaults = lruvec_page_state(target_lruvec,
2807
+ WORKINGSET_ACTIVATE_FILE);
2808
+ if (refaults != target_lruvec->refaults[1] ||
2809
+ inactive_is_low(target_lruvec, LRU_INACTIVE_FILE))
2810
+ sc->may_deactivate |= DEACTIVATE_FILE;
2811
+ else
2812
+ sc->may_deactivate &= ~DEACTIVATE_FILE;
2813
+ } else
2814
+ sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE;
2815
+
2816
+ /*
2817
+ * If we have plenty of inactive file pages that aren't
2818
+ * thrashing, try to reclaim those first before touching
2819
+ * anonymous pages.
2820
+ */
2821
+ file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE);
2822
+ if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE))
2823
+ sc->cache_trim_mode = 1;
2824
+ else
2825
+ sc->cache_trim_mode = 0;
2826
+
2827
+ /*
2828
+ * Prevent the reclaimer from falling into the cache trap: as
2829
+ * cache pages start out inactive, every cache fault will tip
2830
+ * the scan balance towards the file LRU. And as the file LRU
2831
+ * shrinks, so does the window for rotation from references.
2832
+ * This means we have a runaway feedback loop where a tiny
2833
+ * thrashing file LRU becomes infinitely more attractive than
2834
+ * anon pages. Try to detect this based on file LRU size.
2835
+ */
2836
+ if (!cgroup_reclaim(sc)) {
2837
+ unsigned long total_high_wmark = 0;
2838
+ unsigned long free, anon;
2839
+ int z;
2840
+
2841
+ free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES);
2842
+ file = node_page_state(pgdat, NR_ACTIVE_FILE) +
2843
+ node_page_state(pgdat, NR_INACTIVE_FILE);
2844
+
2845
+ for (z = 0; z < MAX_NR_ZONES; z++) {
2846
+ struct zone *zone = &pgdat->node_zones[z];
2847
+ if (!managed_zone(zone))
27282848 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
- }
27452849
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);
2850
+ total_high_wmark += high_wmark_pages(zone);
28302851 }
28312852
28322853 /*
2833
- * Legacy memcg will stall in page writeback so avoid forcibly
2834
- * stalling in wait_iff_congested().
2854
+ * Consider anon: if that's low too, this isn't a
2855
+ * runaway file reclaim problem, but rather just
2856
+ * extreme pressure. Reclaim as per usual then.
28352857 */
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);
2858
+ anon = node_page_state(pgdat, NR_INACTIVE_ANON);
2859
+
2860
+ sc->file_is_tiny =
2861
+ file + free <= total_high_wmark &&
2862
+ !(sc->may_deactivate & DEACTIVATE_ANON) &&
2863
+ anon >> sc->priority;
2864
+ }
2865
+
2866
+ shrink_node_memcgs(pgdat, sc);
2867
+
2868
+ if (reclaim_state) {
2869
+ sc->nr_reclaimed += reclaim_state->reclaimed_slab;
2870
+ reclaim_state->reclaimed_slab = 0;
2871
+ }
2872
+
2873
+ /* Record the subtree's reclaim efficiency */
2874
+ vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true,
2875
+ sc->nr_scanned - nr_scanned,
2876
+ sc->nr_reclaimed - nr_reclaimed);
2877
+
2878
+ if (sc->nr_reclaimed - nr_reclaimed)
2879
+ reclaimable = true;
2880
+
2881
+ if (current_is_kswapd()) {
2882
+ /*
2883
+ * If reclaim is isolating dirty pages under writeback,
2884
+ * it implies that the long-lived page allocation rate
2885
+ * is exceeding the page laundering rate. Either the
2886
+ * global limits are not being effective at throttling
2887
+ * processes due to the page distribution throughout
2888
+ * zones or there is heavy usage of a slow backing
2889
+ * device. The only option is to throttle from reclaim
2890
+ * context which is not ideal as there is no guarantee
2891
+ * the dirtying process is throttled in the same way
2892
+ * balance_dirty_pages() manages.
2893
+ *
2894
+ * Once a node is flagged PGDAT_WRITEBACK, kswapd will
2895
+ * count the number of pages under pages flagged for
2896
+ * immediate reclaim and stall if any are encountered
2897
+ * in the nr_immediate check below.
2898
+ */
2899
+ if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken)
2900
+ set_bit(PGDAT_WRITEBACK, &pgdat->flags);
2901
+
2902
+ /* Allow kswapd to start writing pages during reclaim.*/
2903
+ if (sc->nr.unqueued_dirty == sc->nr.file_taken)
2904
+ set_bit(PGDAT_DIRTY, &pgdat->flags);
28392905
28402906 /*
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.
2907
+ * If kswapd scans pages marked for immediate
2908
+ * reclaim and under writeback (nr_immediate), it
2909
+ * implies that pages are cycling through the LRU
2910
+ * faster than they are written so also forcibly stall.
28452911 */
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);
2912
+ if (sc->nr.immediate)
2913
+ congestion_wait(BLK_RW_ASYNC, HZ/10);
2914
+ }
28492915
2850
- } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed,
2851
- sc->nr_scanned - nr_scanned, sc));
2916
+ /*
2917
+ * Tag a node/memcg as congested if all the dirty pages
2918
+ * scanned were backed by a congested BDI and
2919
+ * wait_iff_congested will stall.
2920
+ *
2921
+ * Legacy memcg will stall in page writeback so avoid forcibly
2922
+ * stalling in wait_iff_congested().
2923
+ */
2924
+ if ((current_is_kswapd() ||
2925
+ (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) &&
2926
+ sc->nr.dirty && sc->nr.dirty == sc->nr.congested)
2927
+ set_bit(LRUVEC_CONGESTED, &target_lruvec->flags);
2928
+
2929
+ /*
2930
+ * Stall direct reclaim for IO completions if underlying BDIs
2931
+ * and node is congested. Allow kswapd to continue until it
2932
+ * starts encountering unqueued dirty pages or cycling through
2933
+ * the LRU too quickly.
2934
+ */
2935
+ if (!current_is_kswapd() && current_may_throttle() &&
2936
+ !sc->hibernation_mode &&
2937
+ test_bit(LRUVEC_CONGESTED, &target_lruvec->flags))
2938
+ wait_iff_congested(BLK_RW_ASYNC, HZ/10);
2939
+
2940
+ if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed,
2941
+ sc))
2942
+ goto again;
28522943
28532944 /*
28542945 * Kswapd gives up on balancing particular nodes after too
....@@ -2858,8 +2949,6 @@
28582949 */
28592950 if (reclaimable)
28602951 pgdat->kswapd_failures = 0;
2861
-
2862
- return reclaimable;
28632952 }
28642953
28652954 /*
....@@ -2928,7 +3017,7 @@
29283017 * Take care memory controller reclaiming has small influence
29293018 * to global LRU.
29303019 */
2931
- if (global_reclaim(sc)) {
3020
+ if (!cgroup_reclaim(sc)) {
29323021 if (!cpuset_zone_allowed(zone,
29333022 GFP_KERNEL | __GFP_HARDWALL))
29343023 continue;
....@@ -2987,19 +3076,17 @@
29873076 sc->gfp_mask = orig_mask;
29883077 }
29893078
2990
-static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat)
3079
+static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat)
29913080 {
2992
- struct mem_cgroup *memcg;
3081
+ struct lruvec *target_lruvec;
3082
+ unsigned long refaults;
29933083
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)));
3084
+ target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat);
3085
+ refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON);
3086
+ target_lruvec->refaults[0] = refaults;
3087
+ refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE);
3088
+ target_lruvec->refaults[1] = refaults;
3089
+ trace_android_vh_snapshot_refaults(target_lruvec);
30033090 }
30043091
30053092 /*
....@@ -3028,7 +3115,7 @@
30283115 retry:
30293116 delayacct_freepages_start();
30303117
3031
- if (global_reclaim(sc))
3118
+ if (!cgroup_reclaim(sc))
30323119 __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1);
30333120
30343121 do {
....@@ -3057,8 +3144,16 @@
30573144 if (zone->zone_pgdat == last_pgdat)
30583145 continue;
30593146 last_pgdat = zone->zone_pgdat;
3147
+
30603148 snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat);
3061
- set_memcg_congestion(last_pgdat, sc->target_mem_cgroup, false);
3149
+
3150
+ if (cgroup_reclaim(sc)) {
3151
+ struct lruvec *lruvec;
3152
+
3153
+ lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup,
3154
+ zone->zone_pgdat);
3155
+ clear_bit(LRUVEC_CONGESTED, &lruvec->flags);
3156
+ }
30623157 }
30633158
30643159 delayacct_freepages_end();
....@@ -3070,9 +3165,26 @@
30703165 if (sc->compaction_ready)
30713166 return 1;
30723167
3168
+ /*
3169
+ * We make inactive:active ratio decisions based on the node's
3170
+ * composition of memory, but a restrictive reclaim_idx or a
3171
+ * memory.low cgroup setting can exempt large amounts of
3172
+ * memory from reclaim. Neither of which are very common, so
3173
+ * instead of doing costly eligibility calculations of the
3174
+ * entire cgroup subtree up front, we assume the estimates are
3175
+ * good, and retry with forcible deactivation if that fails.
3176
+ */
3177
+ if (sc->skipped_deactivate) {
3178
+ sc->priority = initial_priority;
3179
+ sc->force_deactivate = 1;
3180
+ sc->skipped_deactivate = 0;
3181
+ goto retry;
3182
+ }
3183
+
30733184 /* Untapped cgroup reserves? Don't OOM, retry. */
30743185 if (sc->memcg_low_skipped) {
30753186 sc->priority = initial_priority;
3187
+ sc->force_deactivate = 0;
30763188 sc->memcg_low_reclaim = 1;
30773189 sc->memcg_low_skipped = 0;
30783190 goto retry;
....@@ -3112,8 +3224,8 @@
31123224
31133225 /* kswapd must be awake if processes are being throttled */
31143226 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);
3227
+ if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL)
3228
+ WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL);
31173229
31183230 wake_up_interruptible(&pgdat->kswapd_wait);
31193231 }
....@@ -3246,25 +3358,26 @@
32463358 if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask))
32473359 return 1;
32483360
3249
- trace_mm_vmscan_direct_reclaim_begin(order,
3250
- sc.may_writepage,
3251
- sc.gfp_mask,
3252
- sc.reclaim_idx);
3361
+ set_task_reclaim_state(current, &sc.reclaim_state);
3362
+ trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask);
32533363
32543364 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
32553365
32563366 trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
3367
+ set_task_reclaim_state(current, NULL);
32573368
32583369 return nr_reclaimed;
32593370 }
32603371
32613372 #ifdef CONFIG_MEMCG
32623373
3374
+/* Only used by soft limit reclaim. Do not reuse for anything else. */
32633375 unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg,
32643376 gfp_t gfp_mask, bool noswap,
32653377 pg_data_t *pgdat,
32663378 unsigned long *nr_scanned)
32673379 {
3380
+ struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
32683381 struct scan_control sc = {
32693382 .nr_to_reclaim = SWAP_CLUSTER_MAX,
32703383 .target_mem_cgroup = memcg,
....@@ -3273,15 +3386,14 @@
32733386 .reclaim_idx = MAX_NR_ZONES - 1,
32743387 .may_swap = !noswap,
32753388 };
3276
- unsigned long lru_pages;
3389
+
3390
+ WARN_ON_ONCE(!current->reclaim_state);
32773391
32783392 sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
32793393 (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
32803394
32813395 trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
3282
- sc.may_writepage,
3283
- sc.gfp_mask,
3284
- sc.reclaim_idx);
3396
+ sc.gfp_mask);
32853397
32863398 /*
32873399 * NOTE: Although we can get the priority field, using it
....@@ -3290,11 +3402,12 @@
32903402 * will pick up pages from other mem cgroup's as well. We hack
32913403 * the priority and make it zero.
32923404 */
3293
- shrink_node_memcg(pgdat, memcg, &sc, &lru_pages);
3405
+ shrink_lruvec(lruvec, &sc);
32943406
32953407 trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
32963408
32973409 *nr_scanned = sc.nr_scanned;
3410
+
32983411 return sc.nr_reclaimed;
32993412 }
33003413
....@@ -3303,10 +3416,7 @@
33033416 gfp_t gfp_mask,
33043417 bool may_swap)
33053418 {
3306
- struct zonelist *zonelist;
33073419 unsigned long nr_reclaimed;
3308
- unsigned long pflags;
3309
- int nid;
33103420 unsigned int noreclaim_flag;
33113421 struct scan_control sc = {
33123422 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
....@@ -3319,78 +3429,101 @@
33193429 .may_unmap = 1,
33203430 .may_swap = may_swap,
33213431 };
3322
-
33233432 /*
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.
3433
+ * Traverse the ZONELIST_FALLBACK zonelist of the current node to put
3434
+ * equal pressure on all the nodes. This is based on the assumption that
3435
+ * the reclaim does not bail out early.
33273436 */
3328
- nid = mem_cgroup_select_victim_node(memcg);
3437
+ struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
33293438
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);
3439
+ set_task_reclaim_state(current, &sc.reclaim_state);
3440
+ trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask);
33383441 noreclaim_flag = memalloc_noreclaim_save();
33393442
33403443 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
33413444
33423445 memalloc_noreclaim_restore(noreclaim_flag);
3343
- psi_memstall_leave(&pflags);
3344
-
33453446 trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
3447
+ set_task_reclaim_state(current, NULL);
33463448
33473449 return nr_reclaimed;
33483450 }
3451
+EXPORT_SYMBOL_GPL(try_to_free_mem_cgroup_pages);
33493452 #endif
33503453
33513454 static void age_active_anon(struct pglist_data *pgdat,
33523455 struct scan_control *sc)
33533456 {
33543457 struct mem_cgroup *memcg;
3458
+ struct lruvec *lruvec;
33553459
33563460 if (!total_swap_pages)
33573461 return;
33583462
3463
+ lruvec = mem_cgroup_lruvec(NULL, pgdat);
3464
+ if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON))
3465
+ return;
3466
+
33593467 memcg = mem_cgroup_iter(NULL, NULL, NULL);
33603468 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
-
3469
+ lruvec = mem_cgroup_lruvec(memcg, pgdat);
3470
+ shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
3471
+ sc, LRU_ACTIVE_ANON);
33673472 memcg = mem_cgroup_iter(NULL, memcg, NULL);
33683473 } while (memcg);
33693474 }
33703475
3476
+static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx)
3477
+{
3478
+ int i;
3479
+ struct zone *zone;
3480
+
3481
+ /*
3482
+ * Check for watermark boosts top-down as the higher zones
3483
+ * are more likely to be boosted. Both watermarks and boosts
3484
+ * should not be checked at the same time as reclaim would
3485
+ * start prematurely when there is no boosting and a lower
3486
+ * zone is balanced.
3487
+ */
3488
+ for (i = highest_zoneidx; i >= 0; i--) {
3489
+ zone = pgdat->node_zones + i;
3490
+ if (!managed_zone(zone))
3491
+ continue;
3492
+
3493
+ if (zone->watermark_boost)
3494
+ return true;
3495
+ }
3496
+
3497
+ return false;
3498
+}
3499
+
33713500 /*
33723501 * Returns true if there is an eligible zone balanced for the request order
3373
- * and classzone_idx
3502
+ * and highest_zoneidx
33743503 */
3375
-static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx)
3504
+static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx)
33763505 {
33773506 int i;
33783507 unsigned long mark = -1;
33793508 struct zone *zone;
33803509
3381
- for (i = 0; i <= classzone_idx; i++) {
3510
+ /*
3511
+ * Check watermarks bottom-up as lower zones are more likely to
3512
+ * meet watermarks.
3513
+ */
3514
+ for (i = 0; i <= highest_zoneidx; i++) {
33823515 zone = pgdat->node_zones + i;
33833516
33843517 if (!managed_zone(zone))
33853518 continue;
33863519
33873520 mark = high_wmark_pages(zone);
3388
- if (zone_watermark_ok_safe(zone, order, mark, classzone_idx))
3521
+ if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx))
33893522 return true;
33903523 }
33913524
33923525 /*
3393
- * If a node has no populated zone within classzone_idx, it does not
3526
+ * If a node has no populated zone within highest_zoneidx, it does not
33943527 * need balancing by definition. This can happen if a zone-restricted
33953528 * allocation tries to wake a remote kswapd.
33963529 */
....@@ -3403,7 +3536,9 @@
34033536 /* Clear pgdat state for congested, dirty or under writeback. */
34043537 static void clear_pgdat_congested(pg_data_t *pgdat)
34053538 {
3406
- clear_bit(PGDAT_CONGESTED, &pgdat->flags);
3539
+ struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat);
3540
+
3541
+ clear_bit(LRUVEC_CONGESTED, &lruvec->flags);
34073542 clear_bit(PGDAT_DIRTY, &pgdat->flags);
34083543 clear_bit(PGDAT_WRITEBACK, &pgdat->flags);
34093544 }
....@@ -3414,7 +3549,8 @@
34143549 *
34153550 * Returns true if kswapd is ready to sleep
34163551 */
3417
-static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx)
3552
+static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order,
3553
+ int highest_zoneidx)
34183554 {
34193555 /*
34203556 * The throttled processes are normally woken up in balance_pgdat() as
....@@ -3436,7 +3572,7 @@
34363572 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
34373573 return true;
34383574
3439
- if (pgdat_balanced(pgdat, order, classzone_idx)) {
3575
+ if (pgdat_balanced(pgdat, order, highest_zoneidx)) {
34403576 clear_pgdat_congested(pgdat);
34413577 return true;
34423578 }
....@@ -3496,37 +3632,57 @@
34963632 *
34973633 * kswapd scans the zones in the highmem->normal->dma direction. It skips
34983634 * 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
3635
+ * found to have free_pages <= high_wmark_pages(zone), any page in that zone
35003636 * or lower is eligible for reclaim until at least one usable zone is
35013637 * balanced.
35023638 */
3503
-static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx)
3639
+static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx)
35043640 {
35053641 int i;
35063642 unsigned long nr_soft_reclaimed;
35073643 unsigned long nr_soft_scanned;
35083644 unsigned long pflags;
3645
+ unsigned long nr_boost_reclaim;
3646
+ unsigned long zone_boosts[MAX_NR_ZONES] = { 0, };
3647
+ bool boosted;
35093648 struct zone *zone;
35103649 struct scan_control sc = {
35113650 .gfp_mask = GFP_KERNEL,
35123651 .order = order,
3513
- .priority = DEF_PRIORITY,
3514
- .may_writepage = !laptop_mode,
35153652 .may_unmap = 1,
3516
- .may_swap = 1,
35173653 };
35183654
3655
+ set_task_reclaim_state(current, &sc.reclaim_state);
35193656 psi_memstall_enter(&pflags);
35203657 __fs_reclaim_acquire();
35213658
35223659 count_vm_event(PAGEOUTRUN);
35233660
3661
+ /*
3662
+ * Account for the reclaim boost. Note that the zone boost is left in
3663
+ * place so that parallel allocations that are near the watermark will
3664
+ * stall or direct reclaim until kswapd is finished.
3665
+ */
3666
+ nr_boost_reclaim = 0;
3667
+ for (i = 0; i <= highest_zoneidx; i++) {
3668
+ zone = pgdat->node_zones + i;
3669
+ if (!managed_zone(zone))
3670
+ continue;
3671
+
3672
+ nr_boost_reclaim += zone->watermark_boost;
3673
+ zone_boosts[i] = zone->watermark_boost;
3674
+ }
3675
+ boosted = nr_boost_reclaim;
3676
+
3677
+restart:
3678
+ sc.priority = DEF_PRIORITY;
35243679 do {
35253680 unsigned long nr_reclaimed = sc.nr_reclaimed;
35263681 bool raise_priority = true;
3682
+ bool balanced;
35273683 bool ret;
35283684
3529
- sc.reclaim_idx = classzone_idx;
3685
+ sc.reclaim_idx = highest_zoneidx;
35303686
35313687 /*
35323688 * If the number of buffer_heads exceeds the maximum allowed
....@@ -3550,12 +3706,38 @@
35503706 }
35513707
35523708 /*
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.
3709
+ * If the pgdat is imbalanced then ignore boosting and preserve
3710
+ * the watermarks for a later time and restart. Note that the
3711
+ * zone watermarks will be still reset at the end of balancing
3712
+ * on the grounds that the normal reclaim should be enough to
3713
+ * re-evaluate if boosting is required when kswapd next wakes.
35563714 */
3557
- if (pgdat_balanced(pgdat, sc.order, classzone_idx))
3715
+ balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx);
3716
+ if (!balanced && nr_boost_reclaim) {
3717
+ nr_boost_reclaim = 0;
3718
+ goto restart;
3719
+ }
3720
+
3721
+ /*
3722
+ * If boosting is not active then only reclaim if there are no
3723
+ * eligible zones. Note that sc.reclaim_idx is not used as
3724
+ * buffer_heads_over_limit may have adjusted it.
3725
+ */
3726
+ if (!nr_boost_reclaim && balanced)
35583727 goto out;
3728
+
3729
+ /* Limit the priority of boosting to avoid reclaim writeback */
3730
+ if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2)
3731
+ raise_priority = false;
3732
+
3733
+ /*
3734
+ * Do not writeback or swap pages for boosted reclaim. The
3735
+ * intent is to relieve pressure not issue sub-optimal IO
3736
+ * from reclaim context. If no pages are reclaimed, the
3737
+ * reclaim will be aborted.
3738
+ */
3739
+ sc.may_writepage = !laptop_mode && !nr_boost_reclaim;
3740
+ sc.may_swap = !nr_boost_reclaim;
35593741
35603742 /*
35613743 * Do some background aging of the anon list, to give
....@@ -3608,6 +3790,16 @@
36083790 * progress in reclaiming pages
36093791 */
36103792 nr_reclaimed = sc.nr_reclaimed - nr_reclaimed;
3793
+ nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed);
3794
+
3795
+ /*
3796
+ * If reclaim made no progress for a boost, stop reclaim as
3797
+ * IO cannot be queued and it could be an infinite loop in
3798
+ * extreme circumstances.
3799
+ */
3800
+ if (nr_boost_reclaim && !nr_reclaimed)
3801
+ break;
3802
+
36113803 if (raise_priority || !nr_reclaimed)
36123804 sc.priority--;
36133805 } while (sc.priority >= 1);
....@@ -3616,9 +3808,33 @@
36163808 pgdat->kswapd_failures++;
36173809
36183810 out:
3811
+ /* If reclaim was boosted, account for the reclaim done in this pass */
3812
+ if (boosted) {
3813
+ unsigned long flags;
3814
+
3815
+ for (i = 0; i <= highest_zoneidx; i++) {
3816
+ if (!zone_boosts[i])
3817
+ continue;
3818
+
3819
+ /* Increments are under the zone lock */
3820
+ zone = pgdat->node_zones + i;
3821
+ spin_lock_irqsave(&zone->lock, flags);
3822
+ zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]);
3823
+ spin_unlock_irqrestore(&zone->lock, flags);
3824
+ }
3825
+
3826
+ /*
3827
+ * As there is now likely space, wakeup kcompact to defragment
3828
+ * pageblocks.
3829
+ */
3830
+ wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx);
3831
+ }
3832
+
36193833 snapshot_refaults(NULL, pgdat);
36203834 __fs_reclaim_release();
36213835 psi_memstall_leave(&pflags);
3836
+ set_task_reclaim_state(current, NULL);
3837
+
36223838 /*
36233839 * Return the order kswapd stopped reclaiming at as
36243840 * prepare_kswapd_sleep() takes it into account. If another caller
....@@ -3629,22 +3845,22 @@
36293845 }
36303846
36313847 /*
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.
3848
+ * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to
3849
+ * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is
3850
+ * not a valid index then either kswapd runs for first time or kswapd couldn't
3851
+ * sleep after previous reclaim attempt (node is still unbalanced). In that
3852
+ * case return the zone index of the previous kswapd reclaim cycle.
36373853 */
3638
-static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat,
3639
- enum zone_type prev_classzone_idx)
3854
+static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat,
3855
+ enum zone_type prev_highest_zoneidx)
36403856 {
3641
- enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_classzone_idx);
3857
+ enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
36423858
3643
- return curr_idx == MAX_NR_ZONES ? prev_classzone_idx : curr_idx;
3859
+ return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx;
36443860 }
36453861
36463862 static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order,
3647
- unsigned int classzone_idx)
3863
+ unsigned int highest_zoneidx)
36483864 {
36493865 long remaining = 0;
36503866 DEFINE_WAIT(wait);
....@@ -3661,7 +3877,7 @@
36613877 * eligible zone balanced that it's also unlikely that compaction will
36623878 * succeed.
36633879 */
3664
- if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
3880
+ if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
36653881 /*
36663882 * Compaction records what page blocks it recently failed to
36673883 * isolate pages from and skips them in the future scanning.
....@@ -3674,18 +3890,19 @@
36743890 * We have freed the memory, now we should compact it to make
36753891 * allocation of the requested order possible.
36763892 */
3677
- wakeup_kcompactd(pgdat, alloc_order, classzone_idx);
3893
+ wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx);
36783894
36793895 remaining = schedule_timeout(HZ/10);
36803896
36813897 /*
3682
- * If woken prematurely then reset kswapd_classzone_idx and
3898
+ * If woken prematurely then reset kswapd_highest_zoneidx and
36833899 * order. The values will either be from a wakeup request or
36843900 * the previous request that slept prematurely.
36853901 */
36863902 if (remaining) {
3687
- WRITE_ONCE(pgdat->kswapd_classzone_idx,
3688
- kswapd_classzone_idx(pgdat, classzone_idx));
3903
+ WRITE_ONCE(pgdat->kswapd_highest_zoneidx,
3904
+ kswapd_highest_zoneidx(pgdat,
3905
+ highest_zoneidx));
36893906
36903907 if (READ_ONCE(pgdat->kswapd_order) < reclaim_order)
36913908 WRITE_ONCE(pgdat->kswapd_order, reclaim_order);
....@@ -3700,7 +3917,7 @@
37003917 * go fully to sleep until explicitly woken up.
37013918 */
37023919 if (!remaining &&
3703
- prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
3920
+ prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) {
37043921 trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
37053922
37063923 /*
....@@ -3742,18 +3959,13 @@
37423959 static int kswapd(void *p)
37433960 {
37443961 unsigned int alloc_order, reclaim_order;
3745
- unsigned int classzone_idx = MAX_NR_ZONES - 1;
3962
+ unsigned int highest_zoneidx = MAX_NR_ZONES - 1;
37463963 pg_data_t *pgdat = (pg_data_t*)p;
37473964 struct task_struct *tsk = current;
3748
-
3749
- struct reclaim_state reclaim_state = {
3750
- .reclaimed_slab = 0,
3751
- };
37523965 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
37533966
37543967 if (!cpumask_empty(cpumask))
37553968 set_cpus_allowed_ptr(tsk, cpumask);
3756
- current->reclaim_state = &reclaim_state;
37573969
37583970 /*
37593971 * Tell the memory management that we're a "memory allocator",
....@@ -3771,22 +3983,24 @@
37713983 set_freezable();
37723984
37733985 WRITE_ONCE(pgdat->kswapd_order, 0);
3774
- WRITE_ONCE(pgdat->kswapd_classzone_idx, MAX_NR_ZONES);
3986
+ WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
37753987 for ( ; ; ) {
37763988 bool ret;
37773989
37783990 alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order);
3779
- classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx);
3991
+ highest_zoneidx = kswapd_highest_zoneidx(pgdat,
3992
+ highest_zoneidx);
37803993
37813994 kswapd_try_sleep:
37823995 kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order,
3783
- classzone_idx);
3996
+ highest_zoneidx);
37843997
3785
- /* Read the new order and classzone_idx */
3998
+ /* Read the new order and highest_zoneidx */
37863999 alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order);
3787
- classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx);
4000
+ highest_zoneidx = kswapd_highest_zoneidx(pgdat,
4001
+ highest_zoneidx);
37884002 WRITE_ONCE(pgdat->kswapd_order, 0);
3789
- WRITE_ONCE(pgdat->kswapd_classzone_idx, MAX_NR_ZONES);
4003
+ WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES);
37904004
37914005 ret = try_to_freeze();
37924006 if (kthread_should_stop())
....@@ -3807,17 +4021,57 @@
38074021 * but kcompactd is woken to compact for the original
38084022 * request (alloc_order).
38094023 */
3810
- trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx,
4024
+ trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx,
38114025 alloc_order);
3812
- reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx);
4026
+ reclaim_order = balance_pgdat(pgdat, alloc_order,
4027
+ highest_zoneidx);
38134028 if (reclaim_order < alloc_order)
38144029 goto kswapd_try_sleep;
38154030 }
38164031
38174032 tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD);
3818
- current->reclaim_state = NULL;
38194033
38204034 return 0;
4035
+}
4036
+
4037
+static int kswapd_per_node_run(int nid)
4038
+{
4039
+ pg_data_t *pgdat = NODE_DATA(nid);
4040
+ int hid;
4041
+ int ret = 0;
4042
+
4043
+ for (hid = 0; hid < kswapd_threads; ++hid) {
4044
+ pgdat->mkswapd[hid] = kthread_run(kswapd, pgdat, "kswapd%d:%d",
4045
+ nid, hid);
4046
+ if (IS_ERR(pgdat->mkswapd[hid])) {
4047
+ /* failure at boot is fatal */
4048
+ WARN_ON(system_state < SYSTEM_RUNNING);
4049
+ pr_err("Failed to start kswapd%d on node %d\n",
4050
+ hid, nid);
4051
+ ret = PTR_ERR(pgdat->mkswapd[hid]);
4052
+ pgdat->mkswapd[hid] = NULL;
4053
+ continue;
4054
+ }
4055
+ if (!pgdat->kswapd)
4056
+ pgdat->kswapd = pgdat->mkswapd[hid];
4057
+ }
4058
+
4059
+ return ret;
4060
+}
4061
+
4062
+static void kswapd_per_node_stop(int nid)
4063
+{
4064
+ int hid = 0;
4065
+ struct task_struct *kswapd;
4066
+
4067
+ for (hid = 0; hid < kswapd_threads; hid++) {
4068
+ kswapd = NODE_DATA(nid)->mkswapd[hid];
4069
+ if (kswapd) {
4070
+ kthread_stop(kswapd);
4071
+ NODE_DATA(nid)->mkswapd[hid] = NULL;
4072
+ }
4073
+ }
4074
+ NODE_DATA(nid)->kswapd = NULL;
38214075 }
38224076
38234077 /*
....@@ -3828,7 +4082,7 @@
38284082 * needed.
38294083 */
38304084 void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order,
3831
- enum zone_type classzone_idx)
4085
+ enum zone_type highest_zoneidx)
38324086 {
38334087 pg_data_t *pgdat;
38344088 enum zone_type curr_idx;
....@@ -3840,10 +4094,10 @@
38404094 return;
38414095
38424096 pgdat = zone->zone_pgdat;
3843
- curr_idx = READ_ONCE(pgdat->kswapd_classzone_idx);
4097
+ curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx);
38444098
3845
- if (curr_idx == MAX_NR_ZONES || curr_idx < classzone_idx)
3846
- WRITE_ONCE(pgdat->kswapd_classzone_idx, classzone_idx);
4099
+ if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx)
4100
+ WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx);
38474101
38484102 if (READ_ONCE(pgdat->kswapd_order) < order)
38494103 WRITE_ONCE(pgdat->kswapd_order, order);
....@@ -3853,7 +4107,8 @@
38534107
38544108 /* Hopeless node, leave it to direct reclaim if possible */
38554109 if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ||
3856
- pgdat_balanced(pgdat, order, classzone_idx)) {
4110
+ (pgdat_balanced(pgdat, order, highest_zoneidx) &&
4111
+ !pgdat_watermark_boosted(pgdat, highest_zoneidx))) {
38574112 /*
38584113 * There may be plenty of free memory available, but it's too
38594114 * fragmented for high-order allocations. Wake up kcompactd
....@@ -3862,11 +4117,11 @@
38624117 * ratelimit its work.
38634118 */
38644119 if (!(gfp_flags & __GFP_DIRECT_RECLAIM))
3865
- wakeup_kcompactd(pgdat, order, classzone_idx);
4120
+ wakeup_kcompactd(pgdat, order, highest_zoneidx);
38664121 return;
38674122 }
38684123
3869
- trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, classzone_idx, order,
4124
+ trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order,
38704125 gfp_flags);
38714126 wake_up_interruptible(&pgdat->kswapd_wait);
38724127 }
....@@ -3882,7 +4137,6 @@
38824137 */
38834138 unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
38844139 {
3885
- struct reclaim_state reclaim_state;
38864140 struct scan_control sc = {
38874141 .nr_to_reclaim = nr_to_reclaim,
38884142 .gfp_mask = GFP_HIGHUSER_MOVABLE,
....@@ -3894,45 +4148,22 @@
38944148 .hibernation_mode = 1,
38954149 };
38964150 struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
3897
- struct task_struct *p = current;
38984151 unsigned long nr_reclaimed;
38994152 unsigned int noreclaim_flag;
39004153
39014154 fs_reclaim_acquire(sc.gfp_mask);
39024155 noreclaim_flag = memalloc_noreclaim_save();
3903
- reclaim_state.reclaimed_slab = 0;
3904
- p->reclaim_state = &reclaim_state;
4156
+ set_task_reclaim_state(current, &sc.reclaim_state);
39054157
39064158 nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
39074159
3908
- p->reclaim_state = NULL;
4160
+ set_task_reclaim_state(current, NULL);
39094161 memalloc_noreclaim_restore(noreclaim_flag);
39104162 fs_reclaim_release(sc.gfp_mask);
39114163
39124164 return nr_reclaimed;
39134165 }
39144166 #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
-}
39364167
39374168 /*
39384169 * This kswapd start function will be called by init and node-hot-add.
....@@ -3945,6 +4176,9 @@
39454176
39464177 if (pgdat->kswapd)
39474178 return 0;
4179
+
4180
+ if (kswapd_threads > 1)
4181
+ return kswapd_per_node_run(nid);
39484182
39494183 pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
39504184 if (IS_ERR(pgdat->kswapd)) {
....@@ -3965,6 +4199,11 @@
39654199 {
39664200 struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
39674201
4202
+ if (kswapd_threads > 1) {
4203
+ kswapd_per_node_stop(nid);
4204
+ return;
4205
+ }
4206
+
39684207 if (kswapd) {
39694208 kthread_stop(kswapd);
39704209 NODE_DATA(nid)->kswapd = NULL;
....@@ -3973,15 +4212,11 @@
39734212
39744213 static int __init kswapd_init(void)
39754214 {
3976
- int nid, ret;
4215
+ int nid;
39774216
39784217 swap_setup();
39794218 for_each_node_state(nid, N_MEMORY)
39804219 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);
39854220 return 0;
39864221 }
39874222
....@@ -3996,10 +4231,13 @@
39964231 */
39974232 int node_reclaim_mode __read_mostly;
39984233
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 */
4234
+/*
4235
+ * These bit locations are exposed in the vm.zone_reclaim_mode sysctl
4236
+ * ABI. New bits are OK, but existing bits can never change.
4237
+ */
4238
+#define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */
4239
+#define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */
4240
+#define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */
40034241
40044242 /*
40054243 * Priority for NODE_RECLAIM. This determines the fraction of pages
....@@ -4070,7 +4308,6 @@
40704308 /* Minimum pages needed in order to stay on node */
40714309 const unsigned long nr_pages = 1 << order;
40724310 struct task_struct *p = current;
4073
- struct reclaim_state reclaim_state;
40744311 unsigned int noreclaim_flag;
40754312 struct scan_control sc = {
40764313 .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
....@@ -4083,6 +4320,9 @@
40834320 .reclaim_idx = gfp_zone(gfp_mask),
40844321 };
40854322
4323
+ trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order,
4324
+ sc.gfp_mask);
4325
+
40864326 cond_resched();
40874327 fs_reclaim_acquire(sc.gfp_mask);
40884328 /*
....@@ -4092,8 +4332,7 @@
40924332 */
40934333 noreclaim_flag = memalloc_noreclaim_save();
40944334 p->flags |= PF_SWAPWRITE;
4095
- reclaim_state.reclaimed_slab = 0;
4096
- p->reclaim_state = &reclaim_state;
4335
+ set_task_reclaim_state(p, &sc.reclaim_state);
40974336
40984337 if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) {
40994338 /*
....@@ -4105,10 +4344,13 @@
41054344 } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
41064345 }
41074346
4108
- p->reclaim_state = NULL;
4347
+ set_task_reclaim_state(p, NULL);
41094348 current->flags &= ~PF_SWAPWRITE;
41104349 memalloc_noreclaim_restore(noreclaim_flag);
41114350 fs_reclaim_release(sc.gfp_mask);
4351
+
4352
+ trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed);
4353
+
41124354 return sc.nr_reclaimed >= nr_pages;
41134355 }
41144356
....@@ -4127,7 +4369,8 @@
41274369 * unmapped file backed pages.
41284370 */
41294371 if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages &&
4130
- node_page_state(pgdat, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages)
4372
+ node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <=
4373
+ pgdat->min_slab_pages)
41314374 return NODE_RECLAIM_FULL;
41324375
41334376 /*
....@@ -4158,29 +4401,6 @@
41584401 }
41594402 #endif
41604403
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
-
41844404 /**
41854405 * check_move_unevictable_pages - check pages for evictability and move to
41864406 * appropriate zone lru list
....@@ -4201,8 +4421,14 @@
42014421 for (i = 0; i < pvec->nr; i++) {
42024422 struct page *page = pvec->pages[i];
42034423 struct pglist_data *pagepgdat = page_pgdat(page);
4424
+ int nr_pages;
42044425
4205
- pgscanned++;
4426
+ if (PageTransTail(page))
4427
+ continue;
4428
+
4429
+ nr_pages = thp_nr_pages(page);
4430
+ pgscanned += nr_pages;
4431
+
42064432 if (pagepgdat != pgdat) {
42074433 if (pgdat)
42084434 spin_unlock_irq(&pgdat->lru_lock);
....@@ -4221,7 +4447,7 @@
42214447 ClearPageUnevictable(page);
42224448 del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
42234449 add_page_to_lru_list(page, lruvec, lru);
4224
- pgrescued++;
4450
+ pgrescued += nr_pages;
42254451 }
42264452 }
42274453