]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
1da177e4 LT |
3 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds |
4 | * | |
5 | * Swap reorganised 29.12.95, Stephen Tweedie. | |
6 | * kswapd added: 7.1.96 sct | |
7 | * Removed kswapd_ctl limits, and swap out as many pages as needed | |
8 | * to bring the system back to freepages.high: 2.4.97, Rik van Riel. | |
9 | * Zone aware kswapd started 02/00, Kanoj Sarcar ([email protected]). | |
10 | * Multiqueue VM started 5.8.00, Rik van Riel. | |
11 | */ | |
12 | ||
b1de0d13 MH |
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
14 | ||
1da177e4 | 15 | #include <linux/mm.h> |
5b3cc15a | 16 | #include <linux/sched/mm.h> |
1da177e4 | 17 | #include <linux/module.h> |
5a0e3ad6 | 18 | #include <linux/gfp.h> |
1da177e4 LT |
19 | #include <linux/kernel_stat.h> |
20 | #include <linux/swap.h> | |
21 | #include <linux/pagemap.h> | |
22 | #include <linux/init.h> | |
23 | #include <linux/highmem.h> | |
70ddf637 | 24 | #include <linux/vmpressure.h> |
e129b5c2 | 25 | #include <linux/vmstat.h> |
1da177e4 LT |
26 | #include <linux/file.h> |
27 | #include <linux/writeback.h> | |
28 | #include <linux/blkdev.h> | |
07f67a8d | 29 | #include <linux/buffer_head.h> /* for buffer_heads_over_limit */ |
1da177e4 | 30 | #include <linux/mm_inline.h> |
1da177e4 LT |
31 | #include <linux/backing-dev.h> |
32 | #include <linux/rmap.h> | |
33 | #include <linux/topology.h> | |
34 | #include <linux/cpu.h> | |
35 | #include <linux/cpuset.h> | |
3e7d3449 | 36 | #include <linux/compaction.h> |
1da177e4 | 37 | #include <linux/notifier.h> |
248a0301 | 38 | #include <linux/delay.h> |
3218ae14 | 39 | #include <linux/kthread.h> |
7dfb7103 | 40 | #include <linux/freezer.h> |
66e1707b | 41 | #include <linux/memcontrol.h> |
26aa2d19 | 42 | #include <linux/migrate.h> |
873b4771 | 43 | #include <linux/delayacct.h> |
af936a16 | 44 | #include <linux/sysctl.h> |
91952440 | 45 | #include <linux/memory-tiers.h> |
929bea7c | 46 | #include <linux/oom.h> |
64e3d12f | 47 | #include <linux/pagevec.h> |
268bb0ce | 48 | #include <linux/prefetch.h> |
b1de0d13 | 49 | #include <linux/printk.h> |
f9fe48be | 50 | #include <linux/dax.h> |
eb414681 | 51 | #include <linux/psi.h> |
bd74fdae YZ |
52 | #include <linux/pagewalk.h> |
53 | #include <linux/shmem_fs.h> | |
354ed597 | 54 | #include <linux/ctype.h> |
d6c3af7d | 55 | #include <linux/debugfs.h> |
57e9cc50 | 56 | #include <linux/khugepaged.h> |
e4dde56c YZ |
57 | #include <linux/rculist_nulls.h> |
58 | #include <linux/random.h> | |
1da177e4 LT |
59 | |
60 | #include <asm/tlbflush.h> | |
61 | #include <asm/div64.h> | |
62 | ||
63 | #include <linux/swapops.h> | |
117aad1e | 64 | #include <linux/balloon_compaction.h> |
c574bbe9 | 65 | #include <linux/sched/sysctl.h> |
1da177e4 | 66 | |
0f8053a5 | 67 | #include "internal.h" |
014bb1de | 68 | #include "swap.h" |
0f8053a5 | 69 | |
33906bc5 MG |
70 | #define CREATE_TRACE_POINTS |
71 | #include <trace/events/vmscan.h> | |
72 | ||
1da177e4 | 73 | struct scan_control { |
22fba335 KM |
74 | /* How many pages shrink_list() should reclaim */ |
75 | unsigned long nr_to_reclaim; | |
76 | ||
ee814fe2 JW |
77 | /* |
78 | * Nodemask of nodes allowed by the caller. If NULL, all nodes | |
79 | * are scanned. | |
80 | */ | |
81 | nodemask_t *nodemask; | |
9e3b2f8c | 82 | |
f16015fb JW |
83 | /* |
84 | * The memory cgroup that hit its limit and as a result is the | |
85 | * primary target of this reclaim invocation. | |
86 | */ | |
87 | struct mem_cgroup *target_mem_cgroup; | |
66e1707b | 88 | |
7cf111bc JW |
89 | /* |
90 | * Scan pressure balancing between anon and file LRUs | |
91 | */ | |
92 | unsigned long anon_cost; | |
93 | unsigned long file_cost; | |
94 | ||
49fd9b6d | 95 | /* Can active folios be deactivated as part of reclaim? */ |
b91ac374 JW |
96 | #define DEACTIVATE_ANON 1 |
97 | #define DEACTIVATE_FILE 2 | |
98 | unsigned int may_deactivate:2; | |
99 | unsigned int force_deactivate:1; | |
100 | unsigned int skipped_deactivate:1; | |
101 | ||
1276ad68 | 102 | /* Writepage batching in laptop mode; RECLAIM_WRITE */ |
ee814fe2 JW |
103 | unsigned int may_writepage:1; |
104 | ||
49fd9b6d | 105 | /* Can mapped folios be reclaimed? */ |
ee814fe2 JW |
106 | unsigned int may_unmap:1; |
107 | ||
49fd9b6d | 108 | /* Can folios be swapped as part of reclaim? */ |
ee814fe2 JW |
109 | unsigned int may_swap:1; |
110 | ||
73b73bac YA |
111 | /* Proactive reclaim invoked by userspace through memory.reclaim */ |
112 | unsigned int proactive:1; | |
113 | ||
d6622f63 | 114 | /* |
f56ce412 JW |
115 | * Cgroup memory below memory.low is protected as long as we |
116 | * don't threaten to OOM. If any cgroup is reclaimed at | |
117 | * reduced force or passed over entirely due to its memory.low | |
118 | * setting (memcg_low_skipped), and nothing is reclaimed as a | |
119 | * result, then go back for one more cycle that reclaims the protected | |
120 | * memory (memcg_low_reclaim) to avert OOM. | |
d6622f63 YX |
121 | */ |
122 | unsigned int memcg_low_reclaim:1; | |
123 | unsigned int memcg_low_skipped:1; | |
241994ed | 124 | |
ee814fe2 JW |
125 | unsigned int hibernation_mode:1; |
126 | ||
127 | /* One of the zones is ready for compaction */ | |
128 | unsigned int compaction_ready:1; | |
129 | ||
b91ac374 JW |
130 | /* There is easily reclaimable cold cache in the current node */ |
131 | unsigned int cache_trim_mode:1; | |
132 | ||
49fd9b6d | 133 | /* The file folios on the current node are dangerously low */ |
53138cea JW |
134 | unsigned int file_is_tiny:1; |
135 | ||
26aa2d19 DH |
136 | /* Always discard instead of demoting to lower tier memory */ |
137 | unsigned int no_demotion:1; | |
138 | ||
bb451fdf GT |
139 | /* Allocation order */ |
140 | s8 order; | |
141 | ||
142 | /* Scan (total_size >> priority) pages at once */ | |
143 | s8 priority; | |
144 | ||
49fd9b6d | 145 | /* The highest zone to isolate folios for reclaim from */ |
bb451fdf GT |
146 | s8 reclaim_idx; |
147 | ||
148 | /* This context's GFP mask */ | |
149 | gfp_t gfp_mask; | |
150 | ||
ee814fe2 JW |
151 | /* Incremented by the number of inactive pages that were scanned */ |
152 | unsigned long nr_scanned; | |
153 | ||
154 | /* Number of pages freed so far during a call to shrink_zones() */ | |
155 | unsigned long nr_reclaimed; | |
d108c772 AR |
156 | |
157 | struct { | |
158 | unsigned int dirty; | |
159 | unsigned int unqueued_dirty; | |
160 | unsigned int congested; | |
161 | unsigned int writeback; | |
162 | unsigned int immediate; | |
163 | unsigned int file_taken; | |
164 | unsigned int taken; | |
165 | } nr; | |
e5ca8071 YS |
166 | |
167 | /* for recording the reclaimed slab by now */ | |
168 | struct reclaim_state reclaim_state; | |
1da177e4 LT |
169 | }; |
170 | ||
1da177e4 | 171 | #ifdef ARCH_HAS_PREFETCHW |
166e3d32 | 172 | #define prefetchw_prev_lru_folio(_folio, _base, _field) \ |
1da177e4 | 173 | do { \ |
166e3d32 MWO |
174 | if ((_folio)->lru.prev != _base) { \ |
175 | struct folio *prev; \ | |
1da177e4 | 176 | \ |
166e3d32 | 177 | prev = lru_to_folio(&(_folio->lru)); \ |
1da177e4 LT |
178 | prefetchw(&prev->_field); \ |
179 | } \ | |
180 | } while (0) | |
181 | #else | |
166e3d32 | 182 | #define prefetchw_prev_lru_folio(_folio, _base, _field) do { } while (0) |
1da177e4 LT |
183 | #endif |
184 | ||
185 | /* | |
c843966c | 186 | * From 0 .. 200. Higher means more swappy. |
1da177e4 LT |
187 | */ |
188 | int vm_swappiness = 60; | |
1da177e4 | 189 | |
0a432dcb | 190 | #ifdef CONFIG_MEMCG |
a178015c | 191 | |
7a704474 | 192 | /* Returns true for reclaim through cgroup limits or cgroup interfaces. */ |
b5ead35e | 193 | static bool cgroup_reclaim(struct scan_control *sc) |
89b5fae5 | 194 | { |
b5ead35e | 195 | return sc->target_mem_cgroup; |
89b5fae5 | 196 | } |
97c9341f | 197 | |
7a704474 YA |
198 | /* |
199 | * Returns true for reclaim on the root cgroup. This is true for direct | |
200 | * allocator reclaim and reclaim through cgroup interfaces on the root cgroup. | |
201 | */ | |
202 | static bool root_reclaim(struct scan_control *sc) | |
a579086c YZ |
203 | { |
204 | return !sc->target_mem_cgroup || mem_cgroup_is_root(sc->target_mem_cgroup); | |
205 | } | |
206 | ||
97c9341f | 207 | /** |
b5ead35e | 208 | * writeback_throttling_sane - is the usual dirty throttling mechanism available? |
97c9341f TH |
209 | * @sc: scan_control in question |
210 | * | |
211 | * The normal page dirty throttling mechanism in balance_dirty_pages() is | |
212 | * completely broken with the legacy memcg and direct stalling in | |
49fd9b6d | 213 | * shrink_folio_list() is used for throttling instead, which lacks all the |
97c9341f TH |
214 | * niceties such as fairness, adaptive pausing, bandwidth proportional |
215 | * allocation and configurability. | |
216 | * | |
217 | * This function tests whether the vmscan currently in progress can assume | |
218 | * that the normal dirty throttling mechanism is operational. | |
219 | */ | |
b5ead35e | 220 | static bool writeback_throttling_sane(struct scan_control *sc) |
97c9341f | 221 | { |
b5ead35e | 222 | if (!cgroup_reclaim(sc)) |
97c9341f TH |
223 | return true; |
224 | #ifdef CONFIG_CGROUP_WRITEBACK | |
69234ace | 225 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
97c9341f TH |
226 | return true; |
227 | #endif | |
228 | return false; | |
229 | } | |
91a45470 | 230 | #else |
b5ead35e | 231 | static bool cgroup_reclaim(struct scan_control *sc) |
89b5fae5 | 232 | { |
b5ead35e | 233 | return false; |
89b5fae5 | 234 | } |
97c9341f | 235 | |
7a704474 | 236 | static bool root_reclaim(struct scan_control *sc) |
a579086c YZ |
237 | { |
238 | return true; | |
239 | } | |
240 | ||
b5ead35e | 241 | static bool writeback_throttling_sane(struct scan_control *sc) |
97c9341f TH |
242 | { |
243 | return true; | |
244 | } | |
91a45470 KH |
245 | #endif |
246 | ||
ef05e689 YA |
247 | static void set_task_reclaim_state(struct task_struct *task, |
248 | struct reclaim_state *rs) | |
249 | { | |
250 | /* Check for an overwrite */ | |
251 | WARN_ON_ONCE(rs && task->reclaim_state); | |
252 | ||
253 | /* Check for the nulling of an already-nulled member */ | |
254 | WARN_ON_ONCE(!rs && !task->reclaim_state); | |
255 | ||
256 | task->reclaim_state = rs; | |
257 | } | |
258 | ||
583c27a1 YA |
259 | /* |
260 | * flush_reclaim_state(): add pages reclaimed outside of LRU-based reclaim to | |
261 | * scan_control->nr_reclaimed. | |
262 | */ | |
263 | static void flush_reclaim_state(struct scan_control *sc) | |
264 | { | |
265 | /* | |
266 | * Currently, reclaim_state->reclaimed includes three types of pages | |
267 | * freed outside of vmscan: | |
268 | * (1) Slab pages. | |
269 | * (2) Clean file pages from pruned inodes (on highmem systems). | |
270 | * (3) XFS freed buffer pages. | |
271 | * | |
272 | * For all of these cases, we cannot universally link the pages to a | |
273 | * single memcg. For example, a memcg-aware shrinker can free one object | |
274 | * charged to the target memcg, causing an entire page to be freed. | |
275 | * If we count the entire page as reclaimed from the memcg, we end up | |
276 | * overestimating the reclaimed amount (potentially under-reclaiming). | |
277 | * | |
278 | * Only count such pages for global reclaim to prevent under-reclaiming | |
279 | * from the target memcg; preventing unnecessary retries during memcg | |
280 | * charging and false positives from proactive reclaim. | |
281 | * | |
282 | * For uncommon cases where the freed pages were actually mostly | |
283 | * charged to the target memcg, we end up underestimating the reclaimed | |
284 | * amount. This should be fine. The freed pages will be uncharged | |
285 | * anyway, even if they are not counted here properly, and we will be | |
286 | * able to make forward progress in charging (which is usually in a | |
287 | * retry loop). | |
288 | * | |
289 | * We can go one step further, and report the uncharged objcg pages in | |
290 | * memcg reclaim, to make reporting more accurate and reduce | |
291 | * underestimation, but it's probably not worth the complexity for now. | |
292 | */ | |
7a704474 | 293 | if (current->reclaim_state && root_reclaim(sc)) { |
583c27a1 YA |
294 | sc->nr_reclaimed += current->reclaim_state->reclaimed; |
295 | current->reclaim_state->reclaimed = 0; | |
296 | } | |
297 | } | |
298 | ||
26aa2d19 DH |
299 | static bool can_demote(int nid, struct scan_control *sc) |
300 | { | |
20b51af1 YH |
301 | if (!numa_demotion_enabled) |
302 | return false; | |
3f1509c5 JW |
303 | if (sc && sc->no_demotion) |
304 | return false; | |
26aa2d19 DH |
305 | if (next_demotion_node(nid) == NUMA_NO_NODE) |
306 | return false; | |
307 | ||
20b51af1 | 308 | return true; |
26aa2d19 DH |
309 | } |
310 | ||
a2a36488 KB |
311 | static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, |
312 | int nid, | |
313 | struct scan_control *sc) | |
314 | { | |
315 | if (memcg == NULL) { | |
316 | /* | |
317 | * For non-memcg reclaim, is there | |
318 | * space in any swap device? | |
319 | */ | |
320 | if (get_nr_swap_pages() > 0) | |
321 | return true; | |
322 | } else { | |
323 | /* Is the memcg below its swap limit? */ | |
324 | if (mem_cgroup_get_nr_swap_pages(memcg) > 0) | |
325 | return true; | |
326 | } | |
327 | ||
328 | /* | |
329 | * The page can not be swapped. | |
330 | * | |
331 | * Can it be reclaimed from this node via demotion? | |
332 | */ | |
333 | return can_demote(nid, sc); | |
334 | } | |
335 | ||
5a1c84b4 | 336 | /* |
49fd9b6d | 337 | * This misses isolated folios which are not accounted for to save counters. |
5a1c84b4 | 338 | * As the data only determines if reclaim or compaction continues, it is |
49fd9b6d | 339 | * not expected that isolated folios will be a dominating factor. |
5a1c84b4 MG |
340 | */ |
341 | unsigned long zone_reclaimable_pages(struct zone *zone) | |
342 | { | |
343 | unsigned long nr; | |
344 | ||
345 | nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + | |
346 | zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); | |
a2a36488 | 347 | if (can_reclaim_anon_pages(NULL, zone_to_nid(zone), NULL)) |
5a1c84b4 MG |
348 | nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + |
349 | zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); | |
350 | ||
351 | return nr; | |
352 | } | |
353 | ||
fd538803 MH |
354 | /** |
355 | * lruvec_lru_size - Returns the number of pages on the given LRU list. | |
356 | * @lruvec: lru vector | |
357 | * @lru: lru to use | |
8b3a899a | 358 | * @zone_idx: zones to consider (use MAX_NR_ZONES - 1 for the whole LRU list) |
fd538803 | 359 | */ |
2091339d YZ |
360 | static unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, |
361 | int zone_idx) | |
c9f299d9 | 362 | { |
de3b0150 | 363 | unsigned long size = 0; |
fd538803 MH |
364 | int zid; |
365 | ||
8b3a899a | 366 | for (zid = 0; zid <= zone_idx; zid++) { |
fd538803 | 367 | struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; |
c9f299d9 | 368 | |
fd538803 MH |
369 | if (!managed_zone(zone)) |
370 | continue; | |
371 | ||
372 | if (!mem_cgroup_disabled()) | |
de3b0150 | 373 | size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid); |
fd538803 | 374 | else |
de3b0150 | 375 | size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru); |
fd538803 | 376 | } |
de3b0150 | 377 | return size; |
b4536f0c MH |
378 | } |
379 | ||
e83b39d6 | 380 | static unsigned long drop_slab_node(int nid) |
cb731d6c | 381 | { |
e83b39d6 JK |
382 | unsigned long freed = 0; |
383 | struct mem_cgroup *memcg = NULL; | |
cb731d6c | 384 | |
e83b39d6 | 385 | memcg = mem_cgroup_iter(NULL, NULL, NULL); |
cb731d6c | 386 | do { |
e83b39d6 JK |
387 | freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); |
388 | } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); | |
069c411d | 389 | |
e83b39d6 | 390 | return freed; |
cb731d6c VD |
391 | } |
392 | ||
393 | void drop_slab(void) | |
394 | { | |
395 | int nid; | |
e83b39d6 JK |
396 | int shift = 0; |
397 | unsigned long freed; | |
398 | ||
399 | do { | |
400 | freed = 0; | |
401 | for_each_online_node(nid) { | |
402 | if (fatal_signal_pending(current)) | |
403 | return; | |
cb731d6c | 404 | |
e83b39d6 JK |
405 | freed += drop_slab_node(nid); |
406 | } | |
407 | } while ((freed >> shift++) > 1); | |
cb731d6c VD |
408 | } |
409 | ||
57e9cc50 JW |
410 | static int reclaimer_offset(void) |
411 | { | |
23e9f013 | 412 | #ifdef CONFIG_NUMA_BALANCING |
57e9cc50 JW |
413 | BUILD_BUG_ON(PGSTEAL_DIRECT - PGSTEAL_KSWAPD != |
414 | PGDEMOTE_DIRECT - PGDEMOTE_KSWAPD); | |
57e9cc50 JW |
415 | BUILD_BUG_ON(PGSTEAL_KHUGEPAGED - PGSTEAL_KSWAPD != |
416 | PGDEMOTE_KHUGEPAGED - PGDEMOTE_KSWAPD); | |
23e9f013 LZ |
417 | #endif |
418 | BUILD_BUG_ON(PGSTEAL_DIRECT - PGSTEAL_KSWAPD != | |
419 | PGSCAN_DIRECT - PGSCAN_KSWAPD); | |
57e9cc50 JW |
420 | BUILD_BUG_ON(PGSTEAL_KHUGEPAGED - PGSTEAL_KSWAPD != |
421 | PGSCAN_KHUGEPAGED - PGSCAN_KSWAPD); | |
422 | ||
423 | if (current_is_kswapd()) | |
424 | return 0; | |
425 | if (current_is_khugepaged()) | |
426 | return PGSTEAL_KHUGEPAGED - PGSTEAL_KSWAPD; | |
427 | return PGSTEAL_DIRECT - PGSTEAL_KSWAPD; | |
428 | } | |
429 | ||
e0cd5e7f | 430 | static inline int is_page_cache_freeable(struct folio *folio) |
1da177e4 | 431 | { |
ceddc3a5 | 432 | /* |
49fd9b6d MWO |
433 | * A freeable page cache folio is referenced only by the caller |
434 | * that isolated the folio, the page cache and optional filesystem | |
435 | * private data at folio->private. | |
ceddc3a5 | 436 | */ |
e0cd5e7f MWO |
437 | return folio_ref_count(folio) - folio_test_private(folio) == |
438 | 1 + folio_nr_pages(folio); | |
1da177e4 LT |
439 | } |
440 | ||
1da177e4 | 441 | /* |
e0cd5e7f | 442 | * We detected a synchronous write error writing a folio out. Probably |
1da177e4 LT |
443 | * -ENOSPC. We need to propagate that into the address_space for a subsequent |
444 | * fsync(), msync() or close(). | |
445 | * | |
446 | * The tricky part is that after writepage we cannot touch the mapping: nothing | |
e0cd5e7f MWO |
447 | * prevents it from being freed up. But we have a ref on the folio and once |
448 | * that folio is locked, the mapping is pinned. | |
1da177e4 | 449 | * |
e0cd5e7f | 450 | * We're allowed to run sleeping folio_lock() here because we know the caller has |
1da177e4 LT |
451 | * __GFP_FS. |
452 | */ | |
453 | static void handle_write_error(struct address_space *mapping, | |
e0cd5e7f | 454 | struct folio *folio, int error) |
1da177e4 | 455 | { |
e0cd5e7f MWO |
456 | folio_lock(folio); |
457 | if (folio_mapping(folio) == mapping) | |
3e9f45bd | 458 | mapping_set_error(mapping, error); |
e0cd5e7f | 459 | folio_unlock(folio); |
1da177e4 LT |
460 | } |
461 | ||
1b4e3f26 MG |
462 | static bool skip_throttle_noprogress(pg_data_t *pgdat) |
463 | { | |
464 | int reclaimable = 0, write_pending = 0; | |
465 | int i; | |
466 | ||
467 | /* | |
468 | * If kswapd is disabled, reschedule if necessary but do not | |
469 | * throttle as the system is likely near OOM. | |
470 | */ | |
471 | if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) | |
472 | return true; | |
473 | ||
474 | /* | |
49fd9b6d MWO |
475 | * If there are a lot of dirty/writeback folios then do not |
476 | * throttle as throttling will occur when the folios cycle | |
1b4e3f26 MG |
477 | * towards the end of the LRU if still under writeback. |
478 | */ | |
479 | for (i = 0; i < MAX_NR_ZONES; i++) { | |
480 | struct zone *zone = pgdat->node_zones + i; | |
481 | ||
36c26128 | 482 | if (!managed_zone(zone)) |
1b4e3f26 MG |
483 | continue; |
484 | ||
485 | reclaimable += zone_reclaimable_pages(zone); | |
486 | write_pending += zone_page_state_snapshot(zone, | |
487 | NR_ZONE_WRITE_PENDING); | |
488 | } | |
489 | if (2 * write_pending <= reclaimable) | |
490 | return true; | |
491 | ||
492 | return false; | |
493 | } | |
494 | ||
c3f4a9a2 | 495 | void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason) |
8cd7c588 MG |
496 | { |
497 | wait_queue_head_t *wqh = &pgdat->reclaim_wait[reason]; | |
c3f4a9a2 | 498 | long timeout, ret; |
8cd7c588 MG |
499 | DEFINE_WAIT(wait); |
500 | ||
501 | /* | |
54e6842d | 502 | * Do not throttle user workers, kthreads other than kswapd or |
8cd7c588 MG |
503 | * workqueues. They may be required for reclaim to make |
504 | * forward progress (e.g. journalling workqueues or kthreads). | |
505 | */ | |
506 | if (!current_is_kswapd() && | |
54e6842d | 507 | current->flags & (PF_USER_WORKER|PF_KTHREAD)) { |
b485c6f1 | 508 | cond_resched(); |
8cd7c588 | 509 | return; |
b485c6f1 | 510 | } |
8cd7c588 | 511 | |
c3f4a9a2 MG |
512 | /* |
513 | * These figures are pulled out of thin air. | |
514 | * VMSCAN_THROTTLE_ISOLATED is a transient condition based on too many | |
515 | * parallel reclaimers which is a short-lived event so the timeout is | |
516 | * short. Failing to make progress or waiting on writeback are | |
517 | * potentially long-lived events so use a longer timeout. This is shaky | |
518 | * logic as a failure to make progress could be due to anything from | |
49fd9b6d | 519 | * writeback to a slow device to excessive referenced folios at the tail |
c3f4a9a2 MG |
520 | * of the inactive LRU. |
521 | */ | |
522 | switch(reason) { | |
523 | case VMSCAN_THROTTLE_WRITEBACK: | |
524 | timeout = HZ/10; | |
525 | ||
526 | if (atomic_inc_return(&pgdat->nr_writeback_throttled) == 1) { | |
527 | WRITE_ONCE(pgdat->nr_reclaim_start, | |
528 | node_page_state(pgdat, NR_THROTTLED_WRITTEN)); | |
529 | } | |
530 | ||
531 | break; | |
1b4e3f26 MG |
532 | case VMSCAN_THROTTLE_CONGESTED: |
533 | fallthrough; | |
c3f4a9a2 | 534 | case VMSCAN_THROTTLE_NOPROGRESS: |
1b4e3f26 MG |
535 | if (skip_throttle_noprogress(pgdat)) { |
536 | cond_resched(); | |
537 | return; | |
538 | } | |
539 | ||
540 | timeout = 1; | |
541 | ||
c3f4a9a2 MG |
542 | break; |
543 | case VMSCAN_THROTTLE_ISOLATED: | |
544 | timeout = HZ/50; | |
545 | break; | |
546 | default: | |
547 | WARN_ON_ONCE(1); | |
548 | timeout = HZ; | |
549 | break; | |
8cd7c588 MG |
550 | } |
551 | ||
552 | prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); | |
553 | ret = schedule_timeout(timeout); | |
554 | finish_wait(wqh, &wait); | |
d818fca1 | 555 | |
c3f4a9a2 | 556 | if (reason == VMSCAN_THROTTLE_WRITEBACK) |
d818fca1 | 557 | atomic_dec(&pgdat->nr_writeback_throttled); |
8cd7c588 MG |
558 | |
559 | trace_mm_vmscan_throttled(pgdat->node_id, jiffies_to_usecs(timeout), | |
560 | jiffies_to_usecs(timeout - ret), | |
561 | reason); | |
562 | } | |
563 | ||
564 | /* | |
49fd9b6d MWO |
565 | * Account for folios written if tasks are throttled waiting on dirty |
566 | * folios to clean. If enough folios have been cleaned since throttling | |
8cd7c588 MG |
567 | * started then wakeup the throttled tasks. |
568 | */ | |
512b7931 | 569 | void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, |
8cd7c588 MG |
570 | int nr_throttled) |
571 | { | |
572 | unsigned long nr_written; | |
573 | ||
512b7931 | 574 | node_stat_add_folio(folio, NR_THROTTLED_WRITTEN); |
8cd7c588 MG |
575 | |
576 | /* | |
577 | * This is an inaccurate read as the per-cpu deltas may not | |
578 | * be synchronised. However, given that the system is | |
579 | * writeback throttled, it is not worth taking the penalty | |
580 | * of getting an accurate count. At worst, the throttle | |
581 | * timeout guarantees forward progress. | |
582 | */ | |
583 | nr_written = node_page_state(pgdat, NR_THROTTLED_WRITTEN) - | |
584 | READ_ONCE(pgdat->nr_reclaim_start); | |
585 | ||
586 | if (nr_written > SWAP_CLUSTER_MAX * nr_throttled) | |
587 | wake_up(&pgdat->reclaim_wait[VMSCAN_THROTTLE_WRITEBACK]); | |
588 | } | |
589 | ||
04e62a29 CL |
590 | /* possible outcome of pageout() */ |
591 | typedef enum { | |
49fd9b6d | 592 | /* failed to write folio out, folio is locked */ |
04e62a29 | 593 | PAGE_KEEP, |
49fd9b6d | 594 | /* move folio to the active list, folio is locked */ |
04e62a29 | 595 | PAGE_ACTIVATE, |
49fd9b6d | 596 | /* folio has been sent to the disk successfully, folio is unlocked */ |
04e62a29 | 597 | PAGE_SUCCESS, |
49fd9b6d | 598 | /* folio is clean and locked */ |
04e62a29 CL |
599 | PAGE_CLEAN, |
600 | } pageout_t; | |
601 | ||
1da177e4 | 602 | /* |
49fd9b6d | 603 | * pageout is called by shrink_folio_list() for each dirty folio. |
1742f19f | 604 | * Calls ->writepage(). |
1da177e4 | 605 | */ |
2282679f N |
606 | static pageout_t pageout(struct folio *folio, struct address_space *mapping, |
607 | struct swap_iocb **plug) | |
1da177e4 LT |
608 | { |
609 | /* | |
e0cd5e7f | 610 | * If the folio is dirty, only perform writeback if that write |
1da177e4 LT |
611 | * will be non-blocking. To prevent this allocation from being |
612 | * stalled by pagecache activity. But note that there may be | |
613 | * stalls if we need to run get_block(). We could test | |
614 | * PagePrivate for that. | |
615 | * | |
8174202b | 616 | * If this process is currently in __generic_file_write_iter() against |
e0cd5e7f | 617 | * this folio's queue, we can perform writeback even if that |
1da177e4 LT |
618 | * will block. |
619 | * | |
e0cd5e7f | 620 | * If the folio is swapcache, write it back even if that would |
1da177e4 LT |
621 | * block, for some throttling. This happens by accident, because |
622 | * swap_backing_dev_info is bust: it doesn't reflect the | |
623 | * congestion state of the swapdevs. Easy to fix, if needed. | |
1da177e4 | 624 | */ |
e0cd5e7f | 625 | if (!is_page_cache_freeable(folio)) |
1da177e4 LT |
626 | return PAGE_KEEP; |
627 | if (!mapping) { | |
628 | /* | |
e0cd5e7f MWO |
629 | * Some data journaling orphaned folios can have |
630 | * folio->mapping == NULL while being dirty with clean buffers. | |
1da177e4 | 631 | */ |
e0cd5e7f | 632 | if (folio_test_private(folio)) { |
68189fef | 633 | if (try_to_free_buffers(folio)) { |
e0cd5e7f MWO |
634 | folio_clear_dirty(folio); |
635 | pr_info("%s: orphaned folio\n", __func__); | |
1da177e4 LT |
636 | return PAGE_CLEAN; |
637 | } | |
638 | } | |
639 | return PAGE_KEEP; | |
640 | } | |
641 | if (mapping->a_ops->writepage == NULL) | |
642 | return PAGE_ACTIVATE; | |
1da177e4 | 643 | |
e0cd5e7f | 644 | if (folio_clear_dirty_for_io(folio)) { |
1da177e4 LT |
645 | int res; |
646 | struct writeback_control wbc = { | |
647 | .sync_mode = WB_SYNC_NONE, | |
648 | .nr_to_write = SWAP_CLUSTER_MAX, | |
111ebb6e OH |
649 | .range_start = 0, |
650 | .range_end = LLONG_MAX, | |
1da177e4 | 651 | .for_reclaim = 1, |
2282679f | 652 | .swap_plug = plug, |
1da177e4 LT |
653 | }; |
654 | ||
e0cd5e7f MWO |
655 | folio_set_reclaim(folio); |
656 | res = mapping->a_ops->writepage(&folio->page, &wbc); | |
1da177e4 | 657 | if (res < 0) |
e0cd5e7f | 658 | handle_write_error(mapping, folio, res); |
994fc28c | 659 | if (res == AOP_WRITEPAGE_ACTIVATE) { |
e0cd5e7f | 660 | folio_clear_reclaim(folio); |
1da177e4 LT |
661 | return PAGE_ACTIVATE; |
662 | } | |
c661b078 | 663 | |
e0cd5e7f | 664 | if (!folio_test_writeback(folio)) { |
1da177e4 | 665 | /* synchronous write or broken a_ops? */ |
e0cd5e7f | 666 | folio_clear_reclaim(folio); |
1da177e4 | 667 | } |
e0cd5e7f MWO |
668 | trace_mm_vmscan_write_folio(folio); |
669 | node_stat_add_folio(folio, NR_VMSCAN_WRITE); | |
1da177e4 LT |
670 | return PAGE_SUCCESS; |
671 | } | |
672 | ||
673 | return PAGE_CLEAN; | |
674 | } | |
675 | ||
a649fd92 | 676 | /* |
49fd9b6d | 677 | * Same as remove_mapping, but if the folio is removed from the mapping, it |
e286781d | 678 | * gets returned with a refcount of 0. |
a649fd92 | 679 | */ |
be7c07d6 | 680 | static int __remove_mapping(struct address_space *mapping, struct folio *folio, |
b910718a | 681 | bool reclaimed, struct mem_cgroup *target_memcg) |
49d2e9cc | 682 | { |
bd4c82c2 | 683 | int refcount; |
aae466b0 | 684 | void *shadow = NULL; |
c4843a75 | 685 | |
be7c07d6 MWO |
686 | BUG_ON(!folio_test_locked(folio)); |
687 | BUG_ON(mapping != folio_mapping(folio)); | |
49d2e9cc | 688 | |
be7c07d6 | 689 | if (!folio_test_swapcache(folio)) |
51b8c1fe | 690 | spin_lock(&mapping->host->i_lock); |
30472509 | 691 | xa_lock_irq(&mapping->i_pages); |
49d2e9cc | 692 | /* |
49fd9b6d | 693 | * The non racy check for a busy folio. |
0fd0e6b0 NP |
694 | * |
695 | * Must be careful with the order of the tests. When someone has | |
49fd9b6d MWO |
696 | * a ref to the folio, it may be possible that they dirty it then |
697 | * drop the reference. So if the dirty flag is tested before the | |
698 | * refcount here, then the following race may occur: | |
0fd0e6b0 NP |
699 | * |
700 | * get_user_pages(&page); | |
701 | * [user mapping goes away] | |
702 | * write_to(page); | |
49fd9b6d MWO |
703 | * !folio_test_dirty(folio) [good] |
704 | * folio_set_dirty(folio); | |
705 | * folio_put(folio); | |
706 | * !refcount(folio) [good, discard it] | |
0fd0e6b0 NP |
707 | * |
708 | * [oops, our write_to data is lost] | |
709 | * | |
710 | * Reversing the order of the tests ensures such a situation cannot | |
49fd9b6d MWO |
711 | * escape unnoticed. The smp_rmb is needed to ensure the folio->flags |
712 | * load is not satisfied before that of folio->_refcount. | |
0fd0e6b0 | 713 | * |
49fd9b6d | 714 | * Note that if the dirty flag is always set via folio_mark_dirty, |
b93b0163 | 715 | * and thus under the i_pages lock, then this ordering is not required. |
49d2e9cc | 716 | */ |
be7c07d6 MWO |
717 | refcount = 1 + folio_nr_pages(folio); |
718 | if (!folio_ref_freeze(folio, refcount)) | |
49d2e9cc | 719 | goto cannot_free; |
49fd9b6d | 720 | /* note: atomic_cmpxchg in folio_ref_freeze provides the smp_rmb */ |
be7c07d6 MWO |
721 | if (unlikely(folio_test_dirty(folio))) { |
722 | folio_ref_unfreeze(folio, refcount); | |
49d2e9cc | 723 | goto cannot_free; |
e286781d | 724 | } |
49d2e9cc | 725 | |
be7c07d6 | 726 | if (folio_test_swapcache(folio)) { |
3d2c9087 | 727 | swp_entry_t swap = folio->swap; |
ac35a490 | 728 | |
aae466b0 | 729 | if (reclaimed && !mapping_exiting(mapping)) |
8927f647 | 730 | shadow = workingset_eviction(folio, target_memcg); |
ceff9d33 | 731 | __delete_from_swap_cache(folio, swap, shadow); |
c449deb2 | 732 | mem_cgroup_swapout(folio, swap); |
30472509 | 733 | xa_unlock_irq(&mapping->i_pages); |
4081f744 | 734 | put_swap_folio(folio, swap); |
e286781d | 735 | } else { |
d2329aa0 | 736 | void (*free_folio)(struct folio *); |
6072d13c | 737 | |
d2329aa0 | 738 | free_folio = mapping->a_ops->free_folio; |
a528910e JW |
739 | /* |
740 | * Remember a shadow entry for reclaimed file cache in | |
741 | * order to detect refaults, thus thrashing, later on. | |
742 | * | |
743 | * But don't store shadows in an address space that is | |
238c3046 | 744 | * already exiting. This is not just an optimization, |
a528910e JW |
745 | * inode reclaim needs to empty out the radix tree or |
746 | * the nodes are lost. Don't plant shadows behind its | |
747 | * back. | |
f9fe48be RZ |
748 | * |
749 | * We also don't store shadows for DAX mappings because the | |
49fd9b6d | 750 | * only page cache folios found in these are zero pages |
f9fe48be RZ |
751 | * covering holes, and because we don't want to mix DAX |
752 | * exceptional entries and shadow exceptional entries in the | |
b93b0163 | 753 | * same address_space. |
a528910e | 754 | */ |
be7c07d6 | 755 | if (reclaimed && folio_is_file_lru(folio) && |
f9fe48be | 756 | !mapping_exiting(mapping) && !dax_mapping(mapping)) |
8927f647 MWO |
757 | shadow = workingset_eviction(folio, target_memcg); |
758 | __filemap_remove_folio(folio, shadow); | |
30472509 | 759 | xa_unlock_irq(&mapping->i_pages); |
51b8c1fe JW |
760 | if (mapping_shrinkable(mapping)) |
761 | inode_add_lru(mapping->host); | |
762 | spin_unlock(&mapping->host->i_lock); | |
6072d13c | 763 | |
d2329aa0 MWO |
764 | if (free_folio) |
765 | free_folio(folio); | |
49d2e9cc CL |
766 | } |
767 | ||
49d2e9cc CL |
768 | return 1; |
769 | ||
770 | cannot_free: | |
30472509 | 771 | xa_unlock_irq(&mapping->i_pages); |
be7c07d6 | 772 | if (!folio_test_swapcache(folio)) |
51b8c1fe | 773 | spin_unlock(&mapping->host->i_lock); |
49d2e9cc CL |
774 | return 0; |
775 | } | |
776 | ||
5100da38 MWO |
777 | /** |
778 | * remove_mapping() - Attempt to remove a folio from its mapping. | |
779 | * @mapping: The address space. | |
780 | * @folio: The folio to remove. | |
781 | * | |
782 | * If the folio is dirty, under writeback or if someone else has a ref | |
783 | * on it, removal will fail. | |
784 | * Return: The number of pages removed from the mapping. 0 if the folio | |
785 | * could not be removed. | |
786 | * Context: The caller should have a single refcount on the folio and | |
787 | * hold its lock. | |
e286781d | 788 | */ |
5100da38 | 789 | long remove_mapping(struct address_space *mapping, struct folio *folio) |
e286781d | 790 | { |
be7c07d6 | 791 | if (__remove_mapping(mapping, folio, false, NULL)) { |
e286781d | 792 | /* |
5100da38 | 793 | * Unfreezing the refcount with 1 effectively |
e286781d NP |
794 | * drops the pagecache ref for us without requiring another |
795 | * atomic operation. | |
796 | */ | |
be7c07d6 | 797 | folio_ref_unfreeze(folio, 1); |
5100da38 | 798 | return folio_nr_pages(folio); |
e286781d NP |
799 | } |
800 | return 0; | |
801 | } | |
802 | ||
894bc310 | 803 | /** |
ca6d60f3 MWO |
804 | * folio_putback_lru - Put previously isolated folio onto appropriate LRU list. |
805 | * @folio: Folio to be returned to an LRU list. | |
894bc310 | 806 | * |
ca6d60f3 MWO |
807 | * Add previously isolated @folio to appropriate LRU list. |
808 | * The folio may still be unevictable for other reasons. | |
894bc310 | 809 | * |
ca6d60f3 | 810 | * Context: lru_lock must not be held, interrupts must be enabled. |
894bc310 | 811 | */ |
ca6d60f3 | 812 | void folio_putback_lru(struct folio *folio) |
894bc310 | 813 | { |
ca6d60f3 MWO |
814 | folio_add_lru(folio); |
815 | folio_put(folio); /* drop ref from isolate */ | |
894bc310 LS |
816 | } |
817 | ||
49fd9b6d MWO |
818 | enum folio_references { |
819 | FOLIOREF_RECLAIM, | |
820 | FOLIOREF_RECLAIM_CLEAN, | |
821 | FOLIOREF_KEEP, | |
822 | FOLIOREF_ACTIVATE, | |
dfc8d636 JW |
823 | }; |
824 | ||
49fd9b6d | 825 | static enum folio_references folio_check_references(struct folio *folio, |
dfc8d636 JW |
826 | struct scan_control *sc) |
827 | { | |
d92013d1 | 828 | int referenced_ptes, referenced_folio; |
dfc8d636 | 829 | unsigned long vm_flags; |
dfc8d636 | 830 | |
b3ac0413 MWO |
831 | referenced_ptes = folio_referenced(folio, 1, sc->target_mem_cgroup, |
832 | &vm_flags); | |
d92013d1 | 833 | referenced_folio = folio_test_clear_referenced(folio); |
dfc8d636 | 834 | |
dfc8d636 | 835 | /* |
d92013d1 MWO |
836 | * The supposedly reclaimable folio was found to be in a VM_LOCKED vma. |
837 | * Let the folio, now marked Mlocked, be moved to the unevictable list. | |
dfc8d636 JW |
838 | */ |
839 | if (vm_flags & VM_LOCKED) | |
49fd9b6d | 840 | return FOLIOREF_ACTIVATE; |
dfc8d636 | 841 | |
6d4675e6 MK |
842 | /* rmap lock contention: rotate */ |
843 | if (referenced_ptes == -1) | |
49fd9b6d | 844 | return FOLIOREF_KEEP; |
6d4675e6 | 845 | |
64574746 | 846 | if (referenced_ptes) { |
64574746 | 847 | /* |
d92013d1 | 848 | * All mapped folios start out with page table |
64574746 | 849 | * references from the instantiating fault, so we need |
9030fb0b | 850 | * to look twice if a mapped file/anon folio is used more |
64574746 JW |
851 | * than once. |
852 | * | |
853 | * Mark it and spare it for another trip around the | |
854 | * inactive list. Another page table reference will | |
855 | * lead to its activation. | |
856 | * | |
d92013d1 MWO |
857 | * Note: the mark is set for activated folios as well |
858 | * so that recently deactivated but used folios are | |
64574746 JW |
859 | * quickly recovered. |
860 | */ | |
d92013d1 | 861 | folio_set_referenced(folio); |
64574746 | 862 | |
d92013d1 | 863 | if (referenced_folio || referenced_ptes > 1) |
49fd9b6d | 864 | return FOLIOREF_ACTIVATE; |
64574746 | 865 | |
c909e993 | 866 | /* |
d92013d1 | 867 | * Activate file-backed executable folios after first usage. |
c909e993 | 868 | */ |
f19a27e3 | 869 | if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio)) |
49fd9b6d | 870 | return FOLIOREF_ACTIVATE; |
c909e993 | 871 | |
49fd9b6d | 872 | return FOLIOREF_KEEP; |
64574746 | 873 | } |
dfc8d636 | 874 | |
d92013d1 | 875 | /* Reclaim if clean, defer dirty folios to writeback */ |
f19a27e3 | 876 | if (referenced_folio && folio_is_file_lru(folio)) |
49fd9b6d | 877 | return FOLIOREF_RECLAIM_CLEAN; |
64574746 | 878 | |
49fd9b6d | 879 | return FOLIOREF_RECLAIM; |
dfc8d636 JW |
880 | } |
881 | ||
49fd9b6d | 882 | /* Check if a folio is dirty or under writeback */ |
e20c41b1 | 883 | static void folio_check_dirty_writeback(struct folio *folio, |
e2be15f6 MG |
884 | bool *dirty, bool *writeback) |
885 | { | |
b4597226 MG |
886 | struct address_space *mapping; |
887 | ||
e2be15f6 | 888 | /* |
49fd9b6d | 889 | * Anonymous folios are not handled by flushers and must be written |
32a331a7 | 890 | * from reclaim context. Do not stall reclaim based on them. |
49fd9b6d | 891 | * MADV_FREE anonymous folios are put into inactive file list too. |
32a331a7 ML |
892 | * They could be mistakenly treated as file lru. So further anon |
893 | * test is needed. | |
e2be15f6 | 894 | */ |
e20c41b1 MWO |
895 | if (!folio_is_file_lru(folio) || |
896 | (folio_test_anon(folio) && !folio_test_swapbacked(folio))) { | |
e2be15f6 MG |
897 | *dirty = false; |
898 | *writeback = false; | |
899 | return; | |
900 | } | |
901 | ||
e20c41b1 MWO |
902 | /* By default assume that the folio flags are accurate */ |
903 | *dirty = folio_test_dirty(folio); | |
904 | *writeback = folio_test_writeback(folio); | |
b4597226 MG |
905 | |
906 | /* Verify dirty/writeback state if the filesystem supports it */ | |
e20c41b1 | 907 | if (!folio_test_private(folio)) |
b4597226 MG |
908 | return; |
909 | ||
e20c41b1 | 910 | mapping = folio_mapping(folio); |
b4597226 | 911 | if (mapping && mapping->a_ops->is_dirty_writeback) |
520f301c | 912 | mapping->a_ops->is_dirty_writeback(folio, dirty, writeback); |
e2be15f6 MG |
913 | } |
914 | ||
4e096ae1 MWO |
915 | static struct folio *alloc_demote_folio(struct folio *src, |
916 | unsigned long private) | |
26aa2d19 | 917 | { |
4e096ae1 | 918 | struct folio *dst; |
32008027 JG |
919 | nodemask_t *allowed_mask; |
920 | struct migration_target_control *mtc; | |
921 | ||
922 | mtc = (struct migration_target_control *)private; | |
923 | ||
924 | allowed_mask = mtc->nmask; | |
925 | /* | |
926 | * make sure we allocate from the target node first also trying to | |
927 | * demote or reclaim pages from the target node via kswapd if we are | |
928 | * low on free memory on target node. If we don't do this and if | |
929 | * we have free memory on the slower(lower) memtier, we would start | |
930 | * allocating pages from slower(lower) memory tiers without even forcing | |
931 | * a demotion of cold pages from the target memtier. This can result | |
932 | * in the kernel placing hot pages in slower(lower) memory tiers. | |
933 | */ | |
934 | mtc->nmask = NULL; | |
935 | mtc->gfp_mask |= __GFP_THISNODE; | |
4e096ae1 MWO |
936 | dst = alloc_migration_target(src, (unsigned long)mtc); |
937 | if (dst) | |
938 | return dst; | |
26aa2d19 | 939 | |
32008027 JG |
940 | mtc->gfp_mask &= ~__GFP_THISNODE; |
941 | mtc->nmask = allowed_mask; | |
942 | ||
4e096ae1 | 943 | return alloc_migration_target(src, (unsigned long)mtc); |
26aa2d19 DH |
944 | } |
945 | ||
946 | /* | |
49fd9b6d MWO |
947 | * Take folios on @demote_folios and attempt to demote them to another node. |
948 | * Folios which are not demoted are left on @demote_folios. | |
26aa2d19 | 949 | */ |
49fd9b6d | 950 | static unsigned int demote_folio_list(struct list_head *demote_folios, |
26aa2d19 DH |
951 | struct pglist_data *pgdat) |
952 | { | |
953 | int target_nid = next_demotion_node(pgdat->node_id); | |
954 | unsigned int nr_succeeded; | |
32008027 JG |
955 | nodemask_t allowed_mask; |
956 | ||
957 | struct migration_target_control mtc = { | |
958 | /* | |
959 | * Allocate from 'node', or fail quickly and quietly. | |
960 | * When this happens, 'page' will likely just be discarded | |
961 | * instead of migrated. | |
962 | */ | |
963 | .gfp_mask = (GFP_HIGHUSER_MOVABLE & ~__GFP_RECLAIM) | __GFP_NOWARN | | |
964 | __GFP_NOMEMALLOC | GFP_NOWAIT, | |
965 | .nid = target_nid, | |
966 | .nmask = &allowed_mask | |
967 | }; | |
26aa2d19 | 968 | |
49fd9b6d | 969 | if (list_empty(demote_folios)) |
26aa2d19 DH |
970 | return 0; |
971 | ||
972 | if (target_nid == NUMA_NO_NODE) | |
973 | return 0; | |
974 | ||
32008027 JG |
975 | node_get_allowed_targets(pgdat, &allowed_mask); |
976 | ||
26aa2d19 | 977 | /* Demotion ignores all cpuset and mempolicy settings */ |
4e096ae1 | 978 | migrate_pages(demote_folios, alloc_demote_folio, NULL, |
32008027 JG |
979 | (unsigned long)&mtc, MIGRATE_ASYNC, MR_DEMOTION, |
980 | &nr_succeeded); | |
23e9f013 LZ |
981 | #ifdef CONFIG_NUMA_BALANCING |
982 | mod_node_page_state(pgdat, PGDEMOTE_KSWAPD + reclaimer_offset(), | |
983 | nr_succeeded); | |
984 | #endif | |
668e4147 | 985 | |
26aa2d19 DH |
986 | return nr_succeeded; |
987 | } | |
988 | ||
c28a0e96 | 989 | static bool may_enter_fs(struct folio *folio, gfp_t gfp_mask) |
d791ea67 N |
990 | { |
991 | if (gfp_mask & __GFP_FS) | |
992 | return true; | |
c28a0e96 | 993 | if (!folio_test_swapcache(folio) || !(gfp_mask & __GFP_IO)) |
d791ea67 N |
994 | return false; |
995 | /* | |
996 | * We can "enter_fs" for swap-cache with only __GFP_IO | |
997 | * providing this isn't SWP_FS_OPS. | |
998 | * ->flags can be updated non-atomicially (scan_swap_map_slots), | |
999 | * but that will never affect SWP_FS_OPS, so the data_race | |
1000 | * is safe. | |
1001 | */ | |
b98c359f | 1002 | return !data_race(folio_swap_flags(folio) & SWP_FS_OPS); |
d791ea67 N |
1003 | } |
1004 | ||
1da177e4 | 1005 | /* |
49fd9b6d | 1006 | * shrink_folio_list() returns the number of reclaimed pages |
1da177e4 | 1007 | */ |
49fd9b6d MWO |
1008 | static unsigned int shrink_folio_list(struct list_head *folio_list, |
1009 | struct pglist_data *pgdat, struct scan_control *sc, | |
1010 | struct reclaim_stat *stat, bool ignore_references) | |
1011 | { | |
1012 | LIST_HEAD(ret_folios); | |
1013 | LIST_HEAD(free_folios); | |
1014 | LIST_HEAD(demote_folios); | |
730ec8c0 MS |
1015 | unsigned int nr_reclaimed = 0; |
1016 | unsigned int pgactivate = 0; | |
26aa2d19 | 1017 | bool do_demote_pass; |
2282679f | 1018 | struct swap_iocb *plug = NULL; |
1da177e4 | 1019 | |
060f005f | 1020 | memset(stat, 0, sizeof(*stat)); |
1da177e4 | 1021 | cond_resched(); |
26aa2d19 | 1022 | do_demote_pass = can_demote(pgdat->node_id, sc); |
1da177e4 | 1023 | |
26aa2d19 | 1024 | retry: |
49fd9b6d | 1025 | while (!list_empty(folio_list)) { |
1da177e4 | 1026 | struct address_space *mapping; |
be7c07d6 | 1027 | struct folio *folio; |
49fd9b6d | 1028 | enum folio_references references = FOLIOREF_RECLAIM; |
d791ea67 | 1029 | bool dirty, writeback; |
98879b3b | 1030 | unsigned int nr_pages; |
1da177e4 LT |
1031 | |
1032 | cond_resched(); | |
1033 | ||
49fd9b6d | 1034 | folio = lru_to_folio(folio_list); |
be7c07d6 | 1035 | list_del(&folio->lru); |
1da177e4 | 1036 | |
c28a0e96 | 1037 | if (!folio_trylock(folio)) |
1da177e4 LT |
1038 | goto keep; |
1039 | ||
c28a0e96 | 1040 | VM_BUG_ON_FOLIO(folio_test_active(folio), folio); |
1da177e4 | 1041 | |
c28a0e96 | 1042 | nr_pages = folio_nr_pages(folio); |
98879b3b | 1043 | |
c28a0e96 | 1044 | /* Account the number of base pages */ |
98879b3b | 1045 | sc->nr_scanned += nr_pages; |
80e43426 | 1046 | |
c28a0e96 | 1047 | if (unlikely(!folio_evictable(folio))) |
ad6b6704 | 1048 | goto activate_locked; |
894bc310 | 1049 | |
1bee2c16 | 1050 | if (!sc->may_unmap && folio_mapped(folio)) |
80e43426 CL |
1051 | goto keep_locked; |
1052 | ||
018ee47f YZ |
1053 | /* folio_update_gen() tried to promote this page? */ |
1054 | if (lru_gen_enabled() && !ignore_references && | |
1055 | folio_mapped(folio) && folio_test_referenced(folio)) | |
1056 | goto keep_locked; | |
1057 | ||
e2be15f6 | 1058 | /* |
894befec | 1059 | * The number of dirty pages determines if a node is marked |
8cd7c588 | 1060 | * reclaim_congested. kswapd will stall and start writing |
c28a0e96 | 1061 | * folios if the tail of the LRU is all dirty unqueued folios. |
e2be15f6 | 1062 | */ |
e20c41b1 | 1063 | folio_check_dirty_writeback(folio, &dirty, &writeback); |
e2be15f6 | 1064 | if (dirty || writeback) |
c79b7b96 | 1065 | stat->nr_dirty += nr_pages; |
e2be15f6 MG |
1066 | |
1067 | if (dirty && !writeback) | |
c79b7b96 | 1068 | stat->nr_unqueued_dirty += nr_pages; |
e2be15f6 | 1069 | |
d04e8acd | 1070 | /* |
c28a0e96 MWO |
1071 | * Treat this folio as congested if folios are cycling |
1072 | * through the LRU so quickly that the folios marked | |
1073 | * for immediate reclaim are making it to the end of | |
1074 | * the LRU a second time. | |
d04e8acd | 1075 | */ |
c28a0e96 | 1076 | if (writeback && folio_test_reclaim(folio)) |
c79b7b96 | 1077 | stat->nr_congested += nr_pages; |
e2be15f6 | 1078 | |
283aba9f | 1079 | /* |
d33e4e14 | 1080 | * If a folio at the tail of the LRU is under writeback, there |
283aba9f MG |
1081 | * are three cases to consider. |
1082 | * | |
c28a0e96 MWO |
1083 | * 1) If reclaim is encountering an excessive number |
1084 | * of folios under writeback and this folio has both | |
1085 | * the writeback and reclaim flags set, then it | |
d33e4e14 MWO |
1086 | * indicates that folios are being queued for I/O but |
1087 | * are being recycled through the LRU before the I/O | |
1088 | * can complete. Waiting on the folio itself risks an | |
1089 | * indefinite stall if it is impossible to writeback | |
1090 | * the folio due to I/O error or disconnected storage | |
1091 | * so instead note that the LRU is being scanned too | |
1092 | * quickly and the caller can stall after the folio | |
1093 | * list has been processed. | |
283aba9f | 1094 | * |
d33e4e14 | 1095 | * 2) Global or new memcg reclaim encounters a folio that is |
ecf5fc6e MH |
1096 | * not marked for immediate reclaim, or the caller does not |
1097 | * have __GFP_FS (or __GFP_IO if it's simply going to swap, | |
d33e4e14 | 1098 | * not to fs). In this case mark the folio for immediate |
97c9341f | 1099 | * reclaim and continue scanning. |
283aba9f | 1100 | * |
d791ea67 | 1101 | * Require may_enter_fs() because we would wait on fs, which |
d33e4e14 MWO |
1102 | * may not have submitted I/O yet. And the loop driver might |
1103 | * enter reclaim, and deadlock if it waits on a folio for | |
283aba9f MG |
1104 | * which it is needed to do the write (loop masks off |
1105 | * __GFP_IO|__GFP_FS for this reason); but more thought | |
1106 | * would probably show more reasons. | |
1107 | * | |
d33e4e14 MWO |
1108 | * 3) Legacy memcg encounters a folio that already has the |
1109 | * reclaim flag set. memcg does not have any dirty folio | |
283aba9f | 1110 | * throttling so we could easily OOM just because too many |
d33e4e14 | 1111 | * folios are in writeback and there is nothing else to |
283aba9f | 1112 | * reclaim. Wait for the writeback to complete. |
c55e8d03 | 1113 | * |
d33e4e14 MWO |
1114 | * In cases 1) and 2) we activate the folios to get them out of |
1115 | * the way while we continue scanning for clean folios on the | |
c55e8d03 JW |
1116 | * inactive list and refilling from the active list. The |
1117 | * observation here is that waiting for disk writes is more | |
1118 | * expensive than potentially causing reloads down the line. | |
1119 | * Since they're marked for immediate reclaim, they won't put | |
1120 | * memory pressure on the cache working set any longer than it | |
1121 | * takes to write them to disk. | |
283aba9f | 1122 | */ |
d33e4e14 | 1123 | if (folio_test_writeback(folio)) { |
283aba9f MG |
1124 | /* Case 1 above */ |
1125 | if (current_is_kswapd() && | |
d33e4e14 | 1126 | folio_test_reclaim(folio) && |
599d0c95 | 1127 | test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { |
c79b7b96 | 1128 | stat->nr_immediate += nr_pages; |
c55e8d03 | 1129 | goto activate_locked; |
283aba9f MG |
1130 | |
1131 | /* Case 2 above */ | |
b5ead35e | 1132 | } else if (writeback_throttling_sane(sc) || |
d33e4e14 | 1133 | !folio_test_reclaim(folio) || |
c28a0e96 | 1134 | !may_enter_fs(folio, sc->gfp_mask)) { |
c3b94f44 | 1135 | /* |
d33e4e14 | 1136 | * This is slightly racy - |
c28a0e96 MWO |
1137 | * folio_end_writeback() might have |
1138 | * just cleared the reclaim flag, then | |
1139 | * setting the reclaim flag here ends up | |
1140 | * interpreted as the readahead flag - but | |
1141 | * that does not matter enough to care. | |
1142 | * What we do want is for this folio to | |
1143 | * have the reclaim flag set next time | |
1144 | * memcg reclaim reaches the tests above, | |
1145 | * so it will then wait for writeback to | |
1146 | * avoid OOM; and it's also appropriate | |
d33e4e14 | 1147 | * in global reclaim. |
c3b94f44 | 1148 | */ |
d33e4e14 | 1149 | folio_set_reclaim(folio); |
c79b7b96 | 1150 | stat->nr_writeback += nr_pages; |
c55e8d03 | 1151 | goto activate_locked; |
283aba9f MG |
1152 | |
1153 | /* Case 3 above */ | |
1154 | } else { | |
d33e4e14 MWO |
1155 | folio_unlock(folio); |
1156 | folio_wait_writeback(folio); | |
1157 | /* then go back and try same folio again */ | |
49fd9b6d | 1158 | list_add_tail(&folio->lru, folio_list); |
7fadc820 | 1159 | continue; |
e62e384e | 1160 | } |
c661b078 | 1161 | } |
1da177e4 | 1162 | |
8940b34a | 1163 | if (!ignore_references) |
d92013d1 | 1164 | references = folio_check_references(folio, sc); |
02c6de8d | 1165 | |
dfc8d636 | 1166 | switch (references) { |
49fd9b6d | 1167 | case FOLIOREF_ACTIVATE: |
1da177e4 | 1168 | goto activate_locked; |
49fd9b6d | 1169 | case FOLIOREF_KEEP: |
98879b3b | 1170 | stat->nr_ref_keep += nr_pages; |
64574746 | 1171 | goto keep_locked; |
49fd9b6d MWO |
1172 | case FOLIOREF_RECLAIM: |
1173 | case FOLIOREF_RECLAIM_CLEAN: | |
c28a0e96 | 1174 | ; /* try to reclaim the folio below */ |
dfc8d636 | 1175 | } |
1da177e4 | 1176 | |
26aa2d19 | 1177 | /* |
c28a0e96 | 1178 | * Before reclaiming the folio, try to relocate |
26aa2d19 DH |
1179 | * its contents to another node. |
1180 | */ | |
1181 | if (do_demote_pass && | |
c28a0e96 | 1182 | (thp_migration_supported() || !folio_test_large(folio))) { |
49fd9b6d | 1183 | list_add(&folio->lru, &demote_folios); |
c28a0e96 | 1184 | folio_unlock(folio); |
26aa2d19 DH |
1185 | continue; |
1186 | } | |
1187 | ||
1da177e4 LT |
1188 | /* |
1189 | * Anonymous process memory has backing store? | |
1190 | * Try to allocate it some swap space here. | |
c28a0e96 | 1191 | * Lazyfree folio could be freed directly |
1da177e4 | 1192 | */ |
c28a0e96 MWO |
1193 | if (folio_test_anon(folio) && folio_test_swapbacked(folio)) { |
1194 | if (!folio_test_swapcache(folio)) { | |
bd4c82c2 YH |
1195 | if (!(sc->gfp_mask & __GFP_IO)) |
1196 | goto keep_locked; | |
d4b4084a | 1197 | if (folio_maybe_dma_pinned(folio)) |
feb889fb | 1198 | goto keep_locked; |
c28a0e96 MWO |
1199 | if (folio_test_large(folio)) { |
1200 | /* cannot split folio, skip it */ | |
d4b4084a | 1201 | if (!can_split_folio(folio, NULL)) |
bd4c82c2 YH |
1202 | goto activate_locked; |
1203 | /* | |
c28a0e96 | 1204 | * Split folios without a PMD map right |
bd4c82c2 YH |
1205 | * away. Chances are some or all of the |
1206 | * tail pages can be freed without IO. | |
1207 | */ | |
d4b4084a | 1208 | if (!folio_entire_mapcount(folio) && |
346cf613 | 1209 | split_folio_to_list(folio, |
49fd9b6d | 1210 | folio_list)) |
bd4c82c2 YH |
1211 | goto activate_locked; |
1212 | } | |
09c02e56 MWO |
1213 | if (!add_to_swap(folio)) { |
1214 | if (!folio_test_large(folio)) | |
98879b3b | 1215 | goto activate_locked_split; |
bd4c82c2 | 1216 | /* Fallback to swap normal pages */ |
346cf613 | 1217 | if (split_folio_to_list(folio, |
49fd9b6d | 1218 | folio_list)) |
bd4c82c2 | 1219 | goto activate_locked; |
fe490cc0 | 1220 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
811244a5 | 1221 | count_memcg_folio_events(folio, THP_SWPOUT_FALLBACK, 1); |
fe490cc0 YH |
1222 | count_vm_event(THP_SWPOUT_FALLBACK); |
1223 | #endif | |
09c02e56 | 1224 | if (!add_to_swap(folio)) |
98879b3b | 1225 | goto activate_locked_split; |
bd4c82c2 | 1226 | } |
bd4c82c2 | 1227 | } |
c28a0e96 MWO |
1228 | } else if (folio_test_swapbacked(folio) && |
1229 | folio_test_large(folio)) { | |
1230 | /* Split shmem folio */ | |
49fd9b6d | 1231 | if (split_folio_to_list(folio, folio_list)) |
7751b2da | 1232 | goto keep_locked; |
e2be15f6 | 1233 | } |
1da177e4 | 1234 | |
98879b3b | 1235 | /* |
c28a0e96 MWO |
1236 | * If the folio was split above, the tail pages will make |
1237 | * their own pass through this function and be accounted | |
1238 | * then. | |
98879b3b | 1239 | */ |
c28a0e96 | 1240 | if ((nr_pages > 1) && !folio_test_large(folio)) { |
98879b3b YS |
1241 | sc->nr_scanned -= (nr_pages - 1); |
1242 | nr_pages = 1; | |
1243 | } | |
1244 | ||
1da177e4 | 1245 | /* |
1bee2c16 | 1246 | * The folio is mapped into the page tables of one or more |
1da177e4 LT |
1247 | * processes. Try to unmap it here. |
1248 | */ | |
1bee2c16 | 1249 | if (folio_mapped(folio)) { |
013339df | 1250 | enum ttu_flags flags = TTU_BATCH_FLUSH; |
1bee2c16 | 1251 | bool was_swapbacked = folio_test_swapbacked(folio); |
bd4c82c2 | 1252 | |
1bee2c16 | 1253 | if (folio_test_pmd_mappable(folio)) |
bd4c82c2 | 1254 | flags |= TTU_SPLIT_HUGE_PMD; |
1f318a9b | 1255 | |
869f7ee6 | 1256 | try_to_unmap(folio, flags); |
1bee2c16 | 1257 | if (folio_mapped(folio)) { |
98879b3b | 1258 | stat->nr_unmap_fail += nr_pages; |
1bee2c16 MWO |
1259 | if (!was_swapbacked && |
1260 | folio_test_swapbacked(folio)) | |
1f318a9b | 1261 | stat->nr_lazyfree_fail += nr_pages; |
1da177e4 | 1262 | goto activate_locked; |
1da177e4 LT |
1263 | } |
1264 | } | |
1265 | ||
d824ec2a JK |
1266 | /* |
1267 | * Folio is unmapped now so it cannot be newly pinned anymore. | |
1268 | * No point in trying to reclaim folio if it is pinned. | |
1269 | * Furthermore we don't want to reclaim underlying fs metadata | |
1270 | * if the folio is pinned and thus potentially modified by the | |
1271 | * pinning process as that may upset the filesystem. | |
1272 | */ | |
1273 | if (folio_maybe_dma_pinned(folio)) | |
1274 | goto activate_locked; | |
1275 | ||
5441d490 | 1276 | mapping = folio_mapping(folio); |
49bd2bf9 | 1277 | if (folio_test_dirty(folio)) { |
ee72886d | 1278 | /* |
49bd2bf9 | 1279 | * Only kswapd can writeback filesystem folios |
4eda4823 | 1280 | * to avoid risk of stack overflow. But avoid |
49bd2bf9 | 1281 | * injecting inefficient single-folio I/O into |
4eda4823 | 1282 | * flusher writeback as much as possible: only |
49bd2bf9 MWO |
1283 | * write folios when we've encountered many |
1284 | * dirty folios, and when we've already scanned | |
1285 | * the rest of the LRU for clean folios and see | |
1286 | * the same dirty folios again (with the reclaim | |
1287 | * flag set). | |
ee72886d | 1288 | */ |
49bd2bf9 MWO |
1289 | if (folio_is_file_lru(folio) && |
1290 | (!current_is_kswapd() || | |
1291 | !folio_test_reclaim(folio) || | |
4eda4823 | 1292 | !test_bit(PGDAT_DIRTY, &pgdat->flags))) { |
49ea7eb6 MG |
1293 | /* |
1294 | * Immediately reclaim when written back. | |
5a9e3474 | 1295 | * Similar in principle to folio_deactivate() |
49bd2bf9 | 1296 | * except we already have the folio isolated |
49ea7eb6 MG |
1297 | * and know it's dirty |
1298 | */ | |
49bd2bf9 MWO |
1299 | node_stat_mod_folio(folio, NR_VMSCAN_IMMEDIATE, |
1300 | nr_pages); | |
1301 | folio_set_reclaim(folio); | |
49ea7eb6 | 1302 | |
c55e8d03 | 1303 | goto activate_locked; |
ee72886d MG |
1304 | } |
1305 | ||
49fd9b6d | 1306 | if (references == FOLIOREF_RECLAIM_CLEAN) |
1da177e4 | 1307 | goto keep_locked; |
c28a0e96 | 1308 | if (!may_enter_fs(folio, sc->gfp_mask)) |
1da177e4 | 1309 | goto keep_locked; |
52a8363e | 1310 | if (!sc->may_writepage) |
1da177e4 LT |
1311 | goto keep_locked; |
1312 | ||
d950c947 | 1313 | /* |
49bd2bf9 MWO |
1314 | * Folio is dirty. Flush the TLB if a writable entry |
1315 | * potentially exists to avoid CPU writes after I/O | |
d950c947 MG |
1316 | * starts and then write it out here. |
1317 | */ | |
1318 | try_to_unmap_flush_dirty(); | |
2282679f | 1319 | switch (pageout(folio, mapping, &plug)) { |
1da177e4 LT |
1320 | case PAGE_KEEP: |
1321 | goto keep_locked; | |
1322 | case PAGE_ACTIVATE: | |
1323 | goto activate_locked; | |
1324 | case PAGE_SUCCESS: | |
c79b7b96 | 1325 | stat->nr_pageout += nr_pages; |
96f8bf4f | 1326 | |
49bd2bf9 | 1327 | if (folio_test_writeback(folio)) |
41ac1999 | 1328 | goto keep; |
49bd2bf9 | 1329 | if (folio_test_dirty(folio)) |
1da177e4 | 1330 | goto keep; |
7d3579e8 | 1331 | |
1da177e4 LT |
1332 | /* |
1333 | * A synchronous write - probably a ramdisk. Go | |
49bd2bf9 | 1334 | * ahead and try to reclaim the folio. |
1da177e4 | 1335 | */ |
49bd2bf9 | 1336 | if (!folio_trylock(folio)) |
1da177e4 | 1337 | goto keep; |
49bd2bf9 MWO |
1338 | if (folio_test_dirty(folio) || |
1339 | folio_test_writeback(folio)) | |
1da177e4 | 1340 | goto keep_locked; |
49bd2bf9 | 1341 | mapping = folio_mapping(folio); |
01359eb2 | 1342 | fallthrough; |
1da177e4 | 1343 | case PAGE_CLEAN: |
49bd2bf9 | 1344 | ; /* try to free the folio below */ |
1da177e4 LT |
1345 | } |
1346 | } | |
1347 | ||
1348 | /* | |
0a36111c MWO |
1349 | * If the folio has buffers, try to free the buffer |
1350 | * mappings associated with this folio. If we succeed | |
1351 | * we try to free the folio as well. | |
1da177e4 | 1352 | * |
0a36111c MWO |
1353 | * We do this even if the folio is dirty. |
1354 | * filemap_release_folio() does not perform I/O, but it | |
1355 | * is possible for a folio to have the dirty flag set, | |
1356 | * but it is actually clean (all its buffers are clean). | |
1357 | * This happens if the buffers were written out directly, | |
1358 | * with submit_bh(). ext3 will do this, as well as | |
1359 | * the blockdev mapping. filemap_release_folio() will | |
1360 | * discover that cleanness and will drop the buffers | |
1361 | * and mark the folio clean - it can be freed. | |
1da177e4 | 1362 | * |
0a36111c MWO |
1363 | * Rarely, folios can have buffers and no ->mapping. |
1364 | * These are the folios which were not successfully | |
1365 | * invalidated in truncate_cleanup_folio(). We try to | |
1366 | * drop those buffers here and if that worked, and the | |
1367 | * folio is no longer mapped into process address space | |
1368 | * (refcount == 1) it can be freed. Otherwise, leave | |
1369 | * the folio on the LRU so it is swappable. | |
1da177e4 | 1370 | */ |
0201ebf2 | 1371 | if (folio_needs_release(folio)) { |
0a36111c | 1372 | if (!filemap_release_folio(folio, sc->gfp_mask)) |
1da177e4 | 1373 | goto activate_locked; |
0a36111c MWO |
1374 | if (!mapping && folio_ref_count(folio) == 1) { |
1375 | folio_unlock(folio); | |
1376 | if (folio_put_testzero(folio)) | |
e286781d NP |
1377 | goto free_it; |
1378 | else { | |
1379 | /* | |
1380 | * rare race with speculative reference. | |
1381 | * the speculative reference will free | |
0a36111c | 1382 | * this folio shortly, so we may |
e286781d NP |
1383 | * increment nr_reclaimed here (and |
1384 | * leave it off the LRU). | |
1385 | */ | |
9aafcffc | 1386 | nr_reclaimed += nr_pages; |
e286781d NP |
1387 | continue; |
1388 | } | |
1389 | } | |
1da177e4 LT |
1390 | } |
1391 | ||
64daa5d8 | 1392 | if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) { |
802a3a92 | 1393 | /* follow __remove_mapping for reference */ |
64daa5d8 | 1394 | if (!folio_ref_freeze(folio, 1)) |
802a3a92 | 1395 | goto keep_locked; |
d17be2d9 | 1396 | /* |
64daa5d8 | 1397 | * The folio has only one reference left, which is |
d17be2d9 | 1398 | * from the isolation. After the caller puts the |
64daa5d8 MWO |
1399 | * folio back on the lru and drops the reference, the |
1400 | * folio will be freed anyway. It doesn't matter | |
1401 | * which lru it goes on. So we don't bother checking | |
1402 | * the dirty flag here. | |
d17be2d9 | 1403 | */ |
64daa5d8 MWO |
1404 | count_vm_events(PGLAZYFREED, nr_pages); |
1405 | count_memcg_folio_events(folio, PGLAZYFREED, nr_pages); | |
be7c07d6 | 1406 | } else if (!mapping || !__remove_mapping(mapping, folio, true, |
b910718a | 1407 | sc->target_mem_cgroup)) |
802a3a92 | 1408 | goto keep_locked; |
9a1ea439 | 1409 | |
c28a0e96 | 1410 | folio_unlock(folio); |
e286781d | 1411 | free_it: |
98879b3b | 1412 | /* |
c28a0e96 MWO |
1413 | * Folio may get swapped out as a whole, need to account |
1414 | * all pages in it. | |
98879b3b YS |
1415 | */ |
1416 | nr_reclaimed += nr_pages; | |
abe4c3b5 MG |
1417 | |
1418 | /* | |
49fd9b6d | 1419 | * Is there need to periodically free_folio_list? It would |
abe4c3b5 MG |
1420 | * appear not as the counts should be low |
1421 | */ | |
c28a0e96 | 1422 | if (unlikely(folio_test_large(folio))) |
5375336c | 1423 | destroy_large_folio(folio); |
7ae88534 | 1424 | else |
49fd9b6d | 1425 | list_add(&folio->lru, &free_folios); |
1da177e4 LT |
1426 | continue; |
1427 | ||
98879b3b YS |
1428 | activate_locked_split: |
1429 | /* | |
1430 | * The tail pages that are failed to add into swap cache | |
1431 | * reach here. Fixup nr_scanned and nr_pages. | |
1432 | */ | |
1433 | if (nr_pages > 1) { | |
1434 | sc->nr_scanned -= (nr_pages - 1); | |
1435 | nr_pages = 1; | |
1436 | } | |
1da177e4 | 1437 | activate_locked: |
68a22394 | 1438 | /* Not a candidate for swapping, so reclaim swap space. */ |
246b6480 | 1439 | if (folio_test_swapcache(folio) && |
9202d527 | 1440 | (mem_cgroup_swap_full(folio) || folio_test_mlocked(folio))) |
bdb0ed54 | 1441 | folio_free_swap(folio); |
246b6480 MWO |
1442 | VM_BUG_ON_FOLIO(folio_test_active(folio), folio); |
1443 | if (!folio_test_mlocked(folio)) { | |
1444 | int type = folio_is_file_lru(folio); | |
1445 | folio_set_active(folio); | |
98879b3b | 1446 | stat->nr_activate[type] += nr_pages; |
246b6480 | 1447 | count_memcg_folio_events(folio, PGACTIVATE, nr_pages); |
ad6b6704 | 1448 | } |
1da177e4 | 1449 | keep_locked: |
c28a0e96 | 1450 | folio_unlock(folio); |
1da177e4 | 1451 | keep: |
49fd9b6d | 1452 | list_add(&folio->lru, &ret_folios); |
c28a0e96 MWO |
1453 | VM_BUG_ON_FOLIO(folio_test_lru(folio) || |
1454 | folio_test_unevictable(folio), folio); | |
1da177e4 | 1455 | } |
49fd9b6d | 1456 | /* 'folio_list' is always empty here */ |
26aa2d19 | 1457 | |
c28a0e96 | 1458 | /* Migrate folios selected for demotion */ |
49fd9b6d MWO |
1459 | nr_reclaimed += demote_folio_list(&demote_folios, pgdat); |
1460 | /* Folios that could not be demoted are still in @demote_folios */ | |
1461 | if (!list_empty(&demote_folios)) { | |
6b426d07 | 1462 | /* Folios which weren't demoted go back on @folio_list */ |
49fd9b6d | 1463 | list_splice_init(&demote_folios, folio_list); |
6b426d07 MA |
1464 | |
1465 | /* | |
1466 | * goto retry to reclaim the undemoted folios in folio_list if | |
1467 | * desired. | |
1468 | * | |
1469 | * Reclaiming directly from top tier nodes is not often desired | |
1470 | * due to it breaking the LRU ordering: in general memory | |
1471 | * should be reclaimed from lower tier nodes and demoted from | |
1472 | * top tier nodes. | |
1473 | * | |
1474 | * However, disabling reclaim from top tier nodes entirely | |
1475 | * would cause ooms in edge scenarios where lower tier memory | |
1476 | * is unreclaimable for whatever reason, eg memory being | |
1477 | * mlocked or too hot to reclaim. We can disable reclaim | |
1478 | * from top tier nodes in proactive reclaim though as that is | |
1479 | * not real memory pressure. | |
1480 | */ | |
1481 | if (!sc->proactive) { | |
1482 | do_demote_pass = false; | |
1483 | goto retry; | |
1484 | } | |
26aa2d19 | 1485 | } |
abe4c3b5 | 1486 | |
98879b3b YS |
1487 | pgactivate = stat->nr_activate[0] + stat->nr_activate[1]; |
1488 | ||
49fd9b6d | 1489 | mem_cgroup_uncharge_list(&free_folios); |
72b252ae | 1490 | try_to_unmap_flush(); |
49fd9b6d | 1491 | free_unref_page_list(&free_folios); |
abe4c3b5 | 1492 | |
49fd9b6d | 1493 | list_splice(&ret_folios, folio_list); |
886cf190 | 1494 | count_vm_events(PGACTIVATE, pgactivate); |
060f005f | 1495 | |
2282679f N |
1496 | if (plug) |
1497 | swap_write_unplug(plug); | |
05ff5137 | 1498 | return nr_reclaimed; |
1da177e4 LT |
1499 | } |
1500 | ||
730ec8c0 | 1501 | unsigned int reclaim_clean_pages_from_list(struct zone *zone, |
49fd9b6d | 1502 | struct list_head *folio_list) |
02c6de8d MK |
1503 | { |
1504 | struct scan_control sc = { | |
1505 | .gfp_mask = GFP_KERNEL, | |
02c6de8d MK |
1506 | .may_unmap = 1, |
1507 | }; | |
1f318a9b | 1508 | struct reclaim_stat stat; |
730ec8c0 | 1509 | unsigned int nr_reclaimed; |
b8cecb93 MWO |
1510 | struct folio *folio, *next; |
1511 | LIST_HEAD(clean_folios); | |
2d2b8d2b | 1512 | unsigned int noreclaim_flag; |
02c6de8d | 1513 | |
b8cecb93 MWO |
1514 | list_for_each_entry_safe(folio, next, folio_list, lru) { |
1515 | if (!folio_test_hugetlb(folio) && folio_is_file_lru(folio) && | |
1516 | !folio_test_dirty(folio) && !__folio_test_movable(folio) && | |
1517 | !folio_test_unevictable(folio)) { | |
1518 | folio_clear_active(folio); | |
1519 | list_move(&folio->lru, &clean_folios); | |
02c6de8d MK |
1520 | } |
1521 | } | |
1522 | ||
2d2b8d2b YZ |
1523 | /* |
1524 | * We should be safe here since we are only dealing with file pages and | |
1525 | * we are not kswapd and therefore cannot write dirty file pages. But | |
1526 | * call memalloc_noreclaim_save() anyway, just in case these conditions | |
1527 | * change in the future. | |
1528 | */ | |
1529 | noreclaim_flag = memalloc_noreclaim_save(); | |
49fd9b6d | 1530 | nr_reclaimed = shrink_folio_list(&clean_folios, zone->zone_pgdat, &sc, |
013339df | 1531 | &stat, true); |
2d2b8d2b YZ |
1532 | memalloc_noreclaim_restore(noreclaim_flag); |
1533 | ||
b8cecb93 | 1534 | list_splice(&clean_folios, folio_list); |
2da9f630 NP |
1535 | mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, |
1536 | -(long)nr_reclaimed); | |
1f318a9b JK |
1537 | /* |
1538 | * Since lazyfree pages are isolated from file LRU from the beginning, | |
1539 | * they will rotate back to anonymous LRU in the end if it failed to | |
1540 | * discard so isolated count will be mismatched. | |
1541 | * Compensate the isolated count for both LRU lists. | |
1542 | */ | |
1543 | mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON, | |
1544 | stat.nr_lazyfree_fail); | |
1545 | mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, | |
2da9f630 | 1546 | -(long)stat.nr_lazyfree_fail); |
1f318a9b | 1547 | return nr_reclaimed; |
02c6de8d MK |
1548 | } |
1549 | ||
7ee36a14 MG |
1550 | /* |
1551 | * Update LRU sizes after isolating pages. The LRU size updates must | |
55b65a57 | 1552 | * be complete before mem_cgroup_update_lru_size due to a sanity check. |
7ee36a14 MG |
1553 | */ |
1554 | static __always_inline void update_lru_sizes(struct lruvec *lruvec, | |
b4536f0c | 1555 | enum lru_list lru, unsigned long *nr_zone_taken) |
7ee36a14 | 1556 | { |
7ee36a14 MG |
1557 | int zid; |
1558 | ||
7ee36a14 MG |
1559 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
1560 | if (!nr_zone_taken[zid]) | |
1561 | continue; | |
1562 | ||
a892cb6b | 1563 | update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); |
b4536f0c MH |
1564 | } |
1565 | ||
7ee36a14 MG |
1566 | } |
1567 | ||
5da226db ZH |
1568 | #ifdef CONFIG_CMA |
1569 | /* | |
1570 | * It is waste of effort to scan and reclaim CMA pages if it is not available | |
1571 | * for current allocation context. Kswapd can not be enrolled as it can not | |
1572 | * distinguish this scenario by using sc->gfp_mask = GFP_KERNEL | |
1573 | */ | |
1574 | static bool skip_cma(struct folio *folio, struct scan_control *sc) | |
1575 | { | |
1576 | return !current_is_kswapd() && | |
1577 | gfp_migratetype(sc->gfp_mask) != MIGRATE_MOVABLE && | |
97144ce0 | 1578 | folio_migratetype(folio) == MIGRATE_CMA; |
5da226db ZH |
1579 | } |
1580 | #else | |
1581 | static bool skip_cma(struct folio *folio, struct scan_control *sc) | |
1582 | { | |
1583 | return false; | |
1584 | } | |
1585 | #endif | |
1586 | ||
f611fab7 | 1587 | /* |
15b44736 HD |
1588 | * Isolating page from the lruvec to fill in @dst list by nr_to_scan times. |
1589 | * | |
1590 | * lruvec->lru_lock is heavily contended. Some of the functions that | |
1da177e4 LT |
1591 | * shrink the lists perform better by taking out a batch of pages |
1592 | * and working on them outside the LRU lock. | |
1593 | * | |
1594 | * For pagecache intensive workloads, this function is the hottest | |
1595 | * spot in the kernel (apart from copy_*_user functions). | |
1596 | * | |
15b44736 | 1597 | * Lru_lock must be held before calling this function. |
1da177e4 | 1598 | * |
791b48b6 | 1599 | * @nr_to_scan: The number of eligible pages to look through on the list. |
5dc35979 | 1600 | * @lruvec: The LRU vector to pull pages from. |
1da177e4 | 1601 | * @dst: The temp list to put pages on to. |
f626012d | 1602 | * @nr_scanned: The number of pages that were scanned. |
fe2c2a10 | 1603 | * @sc: The scan_control struct for this reclaim session |
3cb99451 | 1604 | * @lru: LRU list id for isolating |
1da177e4 LT |
1605 | * |
1606 | * returns how many pages were moved onto *@dst. | |
1607 | */ | |
49fd9b6d | 1608 | static unsigned long isolate_lru_folios(unsigned long nr_to_scan, |
5dc35979 | 1609 | struct lruvec *lruvec, struct list_head *dst, |
fe2c2a10 | 1610 | unsigned long *nr_scanned, struct scan_control *sc, |
a9e7c39f | 1611 | enum lru_list lru) |
1da177e4 | 1612 | { |
75b00af7 | 1613 | struct list_head *src = &lruvec->lists[lru]; |
69e05944 | 1614 | unsigned long nr_taken = 0; |
599d0c95 | 1615 | unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; |
7cc30fcf | 1616 | unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; |
3db65812 | 1617 | unsigned long skipped = 0; |
791b48b6 | 1618 | unsigned long scan, total_scan, nr_pages; |
166e3d32 | 1619 | LIST_HEAD(folios_skipped); |
1da177e4 | 1620 | |
98879b3b | 1621 | total_scan = 0; |
791b48b6 | 1622 | scan = 0; |
98879b3b | 1623 | while (scan < nr_to_scan && !list_empty(src)) { |
89f6c88a | 1624 | struct list_head *move_to = src; |
166e3d32 | 1625 | struct folio *folio; |
5ad333eb | 1626 | |
166e3d32 MWO |
1627 | folio = lru_to_folio(src); |
1628 | prefetchw_prev_lru_folio(folio, src, flags); | |
1da177e4 | 1629 | |
166e3d32 | 1630 | nr_pages = folio_nr_pages(folio); |
98879b3b YS |
1631 | total_scan += nr_pages; |
1632 | ||
5da226db ZH |
1633 | if (folio_zonenum(folio) > sc->reclaim_idx || |
1634 | skip_cma(folio, sc)) { | |
166e3d32 MWO |
1635 | nr_skipped[folio_zonenum(folio)] += nr_pages; |
1636 | move_to = &folios_skipped; | |
89f6c88a | 1637 | goto move; |
b2e18757 MG |
1638 | } |
1639 | ||
791b48b6 | 1640 | /* |
166e3d32 MWO |
1641 | * Do not count skipped folios because that makes the function |
1642 | * return with no isolated folios if the LRU mostly contains | |
1643 | * ineligible folios. This causes the VM to not reclaim any | |
1644 | * folios, triggering a premature OOM. | |
1645 | * Account all pages in a folio. | |
791b48b6 | 1646 | */ |
98879b3b | 1647 | scan += nr_pages; |
89f6c88a | 1648 | |
166e3d32 | 1649 | if (!folio_test_lru(folio)) |
89f6c88a | 1650 | goto move; |
166e3d32 | 1651 | if (!sc->may_unmap && folio_mapped(folio)) |
89f6c88a HD |
1652 | goto move; |
1653 | ||
c2135f7c | 1654 | /* |
166e3d32 MWO |
1655 | * Be careful not to clear the lru flag until after we're |
1656 | * sure the folio is not being freed elsewhere -- the | |
1657 | * folio release code relies on it. | |
c2135f7c | 1658 | */ |
166e3d32 | 1659 | if (unlikely(!folio_try_get(folio))) |
89f6c88a | 1660 | goto move; |
5ad333eb | 1661 | |
166e3d32 MWO |
1662 | if (!folio_test_clear_lru(folio)) { |
1663 | /* Another thread is already isolating this folio */ | |
1664 | folio_put(folio); | |
89f6c88a | 1665 | goto move; |
5ad333eb | 1666 | } |
c2135f7c AS |
1667 | |
1668 | nr_taken += nr_pages; | |
166e3d32 | 1669 | nr_zone_taken[folio_zonenum(folio)] += nr_pages; |
89f6c88a HD |
1670 | move_to = dst; |
1671 | move: | |
166e3d32 | 1672 | list_move(&folio->lru, move_to); |
1da177e4 LT |
1673 | } |
1674 | ||
b2e18757 | 1675 | /* |
166e3d32 | 1676 | * Splice any skipped folios to the start of the LRU list. Note that |
b2e18757 MG |
1677 | * this disrupts the LRU order when reclaiming for lower zones but |
1678 | * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX | |
166e3d32 | 1679 | * scanning would soon rescan the same folios to skip and waste lots |
b2cb6826 | 1680 | * of cpu cycles. |
b2e18757 | 1681 | */ |
166e3d32 | 1682 | if (!list_empty(&folios_skipped)) { |
7cc30fcf MG |
1683 | int zid; |
1684 | ||
166e3d32 | 1685 | list_splice(&folios_skipped, src); |
7cc30fcf MG |
1686 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
1687 | if (!nr_skipped[zid]) | |
1688 | continue; | |
1689 | ||
1690 | __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); | |
1265e3a6 | 1691 | skipped += nr_skipped[zid]; |
7cc30fcf MG |
1692 | } |
1693 | } | |
791b48b6 | 1694 | *nr_scanned = total_scan; |
1265e3a6 | 1695 | trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, |
3dfbb555 | 1696 | total_scan, skipped, nr_taken, lru); |
b4536f0c | 1697 | update_lru_sizes(lruvec, lru, nr_zone_taken); |
1da177e4 LT |
1698 | return nr_taken; |
1699 | } | |
1700 | ||
62695a84 | 1701 | /** |
d1d8a3b4 MWO |
1702 | * folio_isolate_lru() - Try to isolate a folio from its LRU list. |
1703 | * @folio: Folio to isolate from its LRU list. | |
62695a84 | 1704 | * |
d1d8a3b4 MWO |
1705 | * Isolate a @folio from an LRU list and adjust the vmstat statistic |
1706 | * corresponding to whatever LRU list the folio was on. | |
62695a84 | 1707 | * |
d1d8a3b4 MWO |
1708 | * The folio will have its LRU flag cleared. If it was found on the |
1709 | * active list, it will have the Active flag set. If it was found on the | |
1710 | * unevictable list, it will have the Unevictable flag set. These flags | |
894bc310 | 1711 | * may need to be cleared by the caller before letting the page go. |
62695a84 | 1712 | * |
d1d8a3b4 | 1713 | * Context: |
a5d09bed | 1714 | * |
49fd9b6d MWO |
1715 | * (1) Must be called with an elevated refcount on the folio. This is a |
1716 | * fundamental difference from isolate_lru_folios() (which is called | |
62695a84 | 1717 | * without a stable reference). |
d1d8a3b4 MWO |
1718 | * (2) The lru_lock must not be held. |
1719 | * (3) Interrupts must be enabled. | |
1720 | * | |
be2d5756 BW |
1721 | * Return: true if the folio was removed from an LRU list. |
1722 | * false if the folio was not on an LRU list. | |
62695a84 | 1723 | */ |
be2d5756 | 1724 | bool folio_isolate_lru(struct folio *folio) |
62695a84 | 1725 | { |
be2d5756 | 1726 | bool ret = false; |
62695a84 | 1727 | |
d1d8a3b4 | 1728 | VM_BUG_ON_FOLIO(!folio_ref_count(folio), folio); |
0c917313 | 1729 | |
d1d8a3b4 | 1730 | if (folio_test_clear_lru(folio)) { |
fa9add64 | 1731 | struct lruvec *lruvec; |
62695a84 | 1732 | |
d1d8a3b4 | 1733 | folio_get(folio); |
e809c3fe | 1734 | lruvec = folio_lruvec_lock_irq(folio); |
d1d8a3b4 | 1735 | lruvec_del_folio(lruvec, folio); |
6168d0da | 1736 | unlock_page_lruvec_irq(lruvec); |
be2d5756 | 1737 | ret = true; |
62695a84 | 1738 | } |
d25b5bd8 | 1739 | |
62695a84 NP |
1740 | return ret; |
1741 | } | |
1742 | ||
35cd7815 | 1743 | /* |
d37dd5dc | 1744 | * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and |
178821b8 | 1745 | * then get rescheduled. When there are massive number of tasks doing page |
d37dd5dc FW |
1746 | * allocation, such sleeping direct reclaimers may keep piling up on each CPU, |
1747 | * the LRU list will go small and be scanned faster than necessary, leading to | |
1748 | * unnecessary swapping, thrashing and OOM. | |
35cd7815 | 1749 | */ |
599d0c95 | 1750 | static int too_many_isolated(struct pglist_data *pgdat, int file, |
35cd7815 RR |
1751 | struct scan_control *sc) |
1752 | { | |
1753 | unsigned long inactive, isolated; | |
d818fca1 | 1754 | bool too_many; |
35cd7815 RR |
1755 | |
1756 | if (current_is_kswapd()) | |
1757 | return 0; | |
1758 | ||
b5ead35e | 1759 | if (!writeback_throttling_sane(sc)) |
35cd7815 RR |
1760 | return 0; |
1761 | ||
1762 | if (file) { | |
599d0c95 MG |
1763 | inactive = node_page_state(pgdat, NR_INACTIVE_FILE); |
1764 | isolated = node_page_state(pgdat, NR_ISOLATED_FILE); | |
35cd7815 | 1765 | } else { |
599d0c95 MG |
1766 | inactive = node_page_state(pgdat, NR_INACTIVE_ANON); |
1767 | isolated = node_page_state(pgdat, NR_ISOLATED_ANON); | |
35cd7815 RR |
1768 | } |
1769 | ||
3cf23841 FW |
1770 | /* |
1771 | * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they | |
1772 | * won't get blocked by normal direct-reclaimers, forming a circular | |
1773 | * deadlock. | |
1774 | */ | |
5221b5a8 | 1775 | if (gfp_has_io_fs(sc->gfp_mask)) |
3cf23841 FW |
1776 | inactive >>= 3; |
1777 | ||
d818fca1 MG |
1778 | too_many = isolated > inactive; |
1779 | ||
1780 | /* Wake up tasks throttled due to too_many_isolated. */ | |
1781 | if (!too_many) | |
1782 | wake_throttle_isolated(pgdat); | |
1783 | ||
1784 | return too_many; | |
35cd7815 RR |
1785 | } |
1786 | ||
a222f341 | 1787 | /* |
49fd9b6d | 1788 | * move_folios_to_lru() moves folios from private @list to appropriate LRU list. |
ff00a170 | 1789 | * On return, @list is reused as a list of folios to be freed by the caller. |
a222f341 KT |
1790 | * |
1791 | * Returns the number of pages moved to the given lruvec. | |
1792 | */ | |
49fd9b6d MWO |
1793 | static unsigned int move_folios_to_lru(struct lruvec *lruvec, |
1794 | struct list_head *list) | |
66635629 | 1795 | { |
a222f341 | 1796 | int nr_pages, nr_moved = 0; |
ff00a170 | 1797 | LIST_HEAD(folios_to_free); |
66635629 | 1798 | |
a222f341 | 1799 | while (!list_empty(list)) { |
ff00a170 MWO |
1800 | struct folio *folio = lru_to_folio(list); |
1801 | ||
1802 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); | |
1803 | list_del(&folio->lru); | |
1804 | if (unlikely(!folio_evictable(folio))) { | |
6168d0da | 1805 | spin_unlock_irq(&lruvec->lru_lock); |
ff00a170 | 1806 | folio_putback_lru(folio); |
6168d0da | 1807 | spin_lock_irq(&lruvec->lru_lock); |
66635629 MG |
1808 | continue; |
1809 | } | |
fa9add64 | 1810 | |
3d06afab | 1811 | /* |
ff00a170 | 1812 | * The folio_set_lru needs to be kept here for list integrity. |
3d06afab | 1813 | * Otherwise: |
49fd9b6d | 1814 | * #0 move_folios_to_lru #1 release_pages |
ff00a170 MWO |
1815 | * if (!folio_put_testzero()) |
1816 | * if (folio_put_testzero()) | |
1817 | * !lru //skip lru_lock | |
1818 | * folio_set_lru() | |
1819 | * list_add(&folio->lru,) | |
1820 | * list_add(&folio->lru,) | |
3d06afab | 1821 | */ |
ff00a170 | 1822 | folio_set_lru(folio); |
a222f341 | 1823 | |
ff00a170 MWO |
1824 | if (unlikely(folio_put_testzero(folio))) { |
1825 | __folio_clear_lru_flags(folio); | |
2bcf8879 | 1826 | |
ff00a170 | 1827 | if (unlikely(folio_test_large(folio))) { |
6168d0da | 1828 | spin_unlock_irq(&lruvec->lru_lock); |
5375336c | 1829 | destroy_large_folio(folio); |
6168d0da | 1830 | spin_lock_irq(&lruvec->lru_lock); |
2bcf8879 | 1831 | } else |
ff00a170 | 1832 | list_add(&folio->lru, &folios_to_free); |
3d06afab AS |
1833 | |
1834 | continue; | |
66635629 | 1835 | } |
3d06afab | 1836 | |
afca9157 AS |
1837 | /* |
1838 | * All pages were isolated from the same lruvec (and isolation | |
1839 | * inhibits memcg migration). | |
1840 | */ | |
ff00a170 MWO |
1841 | VM_BUG_ON_FOLIO(!folio_matches_lruvec(folio, lruvec), folio); |
1842 | lruvec_add_folio(lruvec, folio); | |
1843 | nr_pages = folio_nr_pages(folio); | |
3d06afab | 1844 | nr_moved += nr_pages; |
ff00a170 | 1845 | if (folio_test_active(folio)) |
3d06afab | 1846 | workingset_age_nonresident(lruvec, nr_pages); |
66635629 | 1847 | } |
66635629 | 1848 | |
3f79768f HD |
1849 | /* |
1850 | * To save our caller's stack, now use input list for pages to free. | |
1851 | */ | |
ff00a170 | 1852 | list_splice(&folios_to_free, list); |
a222f341 KT |
1853 | |
1854 | return nr_moved; | |
66635629 MG |
1855 | } |
1856 | ||
399ba0b9 | 1857 | /* |
5829f7db ML |
1858 | * If a kernel thread (such as nfsd for loop-back mounts) services a backing |
1859 | * device by writing to the page cache it sets PF_LOCAL_THROTTLE. In this case | |
1860 | * we should not throttle. Otherwise it is safe to do so. | |
399ba0b9 N |
1861 | */ |
1862 | static int current_may_throttle(void) | |
1863 | { | |
b9b1335e | 1864 | return !(current->flags & PF_LOCAL_THROTTLE); |
399ba0b9 N |
1865 | } |
1866 | ||
1da177e4 | 1867 | /* |
b2e18757 | 1868 | * shrink_inactive_list() is a helper for shrink_node(). It returns the number |
1742f19f | 1869 | * of reclaimed pages |
1da177e4 | 1870 | */ |
49fd9b6d MWO |
1871 | static unsigned long shrink_inactive_list(unsigned long nr_to_scan, |
1872 | struct lruvec *lruvec, struct scan_control *sc, | |
1873 | enum lru_list lru) | |
1da177e4 | 1874 | { |
49fd9b6d | 1875 | LIST_HEAD(folio_list); |
e247dbce | 1876 | unsigned long nr_scanned; |
730ec8c0 | 1877 | unsigned int nr_reclaimed = 0; |
e247dbce | 1878 | unsigned long nr_taken; |
060f005f | 1879 | struct reclaim_stat stat; |
497a6c1b | 1880 | bool file = is_file_lru(lru); |
f46b7912 | 1881 | enum vm_event_item item; |
599d0c95 | 1882 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
db73ee0d | 1883 | bool stalled = false; |
78dc583d | 1884 | |
599d0c95 | 1885 | while (unlikely(too_many_isolated(pgdat, file, sc))) { |
db73ee0d MH |
1886 | if (stalled) |
1887 | return 0; | |
1888 | ||
1889 | /* wait a bit for the reclaimer. */ | |
db73ee0d | 1890 | stalled = true; |
c3f4a9a2 | 1891 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_ISOLATED); |
35cd7815 RR |
1892 | |
1893 | /* We are about to die and free our memory. Return now. */ | |
1894 | if (fatal_signal_pending(current)) | |
1895 | return SWAP_CLUSTER_MAX; | |
1896 | } | |
1897 | ||
1da177e4 | 1898 | lru_add_drain(); |
f80c0673 | 1899 | |
6168d0da | 1900 | spin_lock_irq(&lruvec->lru_lock); |
b35ea17b | 1901 | |
49fd9b6d | 1902 | nr_taken = isolate_lru_folios(nr_to_scan, lruvec, &folio_list, |
a9e7c39f | 1903 | &nr_scanned, sc, lru); |
95d918fc | 1904 | |
599d0c95 | 1905 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); |
57e9cc50 | 1906 | item = PGSCAN_KSWAPD + reclaimer_offset(); |
b5ead35e | 1907 | if (!cgroup_reclaim(sc)) |
f46b7912 KT |
1908 | __count_vm_events(item, nr_scanned); |
1909 | __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned); | |
497a6c1b JW |
1910 | __count_vm_events(PGSCAN_ANON + file, nr_scanned); |
1911 | ||
6168d0da | 1912 | spin_unlock_irq(&lruvec->lru_lock); |
b35ea17b | 1913 | |
d563c050 | 1914 | if (nr_taken == 0) |
66635629 | 1915 | return 0; |
5ad333eb | 1916 | |
49fd9b6d | 1917 | nr_reclaimed = shrink_folio_list(&folio_list, pgdat, sc, &stat, false); |
c661b078 | 1918 | |
6168d0da | 1919 | spin_lock_irq(&lruvec->lru_lock); |
49fd9b6d | 1920 | move_folios_to_lru(lruvec, &folio_list); |
497a6c1b JW |
1921 | |
1922 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); | |
57e9cc50 | 1923 | item = PGSTEAL_KSWAPD + reclaimer_offset(); |
b5ead35e | 1924 | if (!cgroup_reclaim(sc)) |
f46b7912 KT |
1925 | __count_vm_events(item, nr_reclaimed); |
1926 | __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed); | |
497a6c1b | 1927 | __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed); |
6168d0da | 1928 | spin_unlock_irq(&lruvec->lru_lock); |
3f79768f | 1929 | |
0538a82c | 1930 | lru_note_cost(lruvec, file, stat.nr_pageout, nr_scanned - nr_reclaimed); |
49fd9b6d MWO |
1931 | mem_cgroup_uncharge_list(&folio_list); |
1932 | free_unref_page_list(&folio_list); | |
e11da5b4 | 1933 | |
1c610d5f | 1934 | /* |
49fd9b6d | 1935 | * If dirty folios are scanned that are not queued for IO, it |
1c610d5f | 1936 | * implies that flushers are not doing their job. This can |
49fd9b6d | 1937 | * happen when memory pressure pushes dirty folios to the end of |
1c610d5f AR |
1938 | * the LRU before the dirty limits are breached and the dirty |
1939 | * data has expired. It can also happen when the proportion of | |
49fd9b6d | 1940 | * dirty folios grows not through writes but through memory |
1c610d5f AR |
1941 | * pressure reclaiming all the clean cache. And in some cases, |
1942 | * the flushers simply cannot keep up with the allocation | |
1943 | * rate. Nudge the flusher threads in case they are asleep. | |
1944 | */ | |
81a70c21 | 1945 | if (stat.nr_unqueued_dirty == nr_taken) { |
1c610d5f | 1946 | wakeup_flusher_threads(WB_REASON_VMSCAN); |
81a70c21 AK |
1947 | /* |
1948 | * For cgroupv1 dirty throttling is achieved by waking up | |
1949 | * the kernel flusher here and later waiting on folios | |
1950 | * which are in writeback to finish (see shrink_folio_list()). | |
1951 | * | |
1952 | * Flusher may not be able to issue writeback quickly | |
1953 | * enough for cgroupv1 writeback throttling to work | |
1954 | * on a large system. | |
1955 | */ | |
1956 | if (!writeback_throttling_sane(sc)) | |
1957 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); | |
1958 | } | |
1c610d5f | 1959 | |
d108c772 AR |
1960 | sc->nr.dirty += stat.nr_dirty; |
1961 | sc->nr.congested += stat.nr_congested; | |
1962 | sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; | |
1963 | sc->nr.writeback += stat.nr_writeback; | |
1964 | sc->nr.immediate += stat.nr_immediate; | |
1965 | sc->nr.taken += nr_taken; | |
1966 | if (file) | |
1967 | sc->nr.file_taken += nr_taken; | |
8e950282 | 1968 | |
599d0c95 | 1969 | trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, |
d51d1e64 | 1970 | nr_scanned, nr_reclaimed, &stat, sc->priority, file); |
05ff5137 | 1971 | return nr_reclaimed; |
1da177e4 LT |
1972 | } |
1973 | ||
15b44736 | 1974 | /* |
07f67a8d | 1975 | * shrink_active_list() moves folios from the active LRU to the inactive LRU. |
15b44736 | 1976 | * |
07f67a8d | 1977 | * We move them the other way if the folio is referenced by one or more |
15b44736 HD |
1978 | * processes. |
1979 | * | |
07f67a8d | 1980 | * If the folios are mostly unmapped, the processing is fast and it is |
15b44736 | 1981 | * appropriate to hold lru_lock across the whole operation. But if |
07f67a8d MWO |
1982 | * the folios are mapped, the processing is slow (folio_referenced()), so |
1983 | * we should drop lru_lock around each folio. It's impossible to balance | |
1984 | * this, so instead we remove the folios from the LRU while processing them. | |
1985 | * It is safe to rely on the active flag against the non-LRU folios in here | |
1986 | * because nobody will play with that bit on a non-LRU folio. | |
15b44736 | 1987 | * |
07f67a8d MWO |
1988 | * The downside is that we have to touch folio->_refcount against each folio. |
1989 | * But we had to alter folio->flags anyway. | |
15b44736 | 1990 | */ |
f626012d | 1991 | static void shrink_active_list(unsigned long nr_to_scan, |
1a93be0e | 1992 | struct lruvec *lruvec, |
f16015fb | 1993 | struct scan_control *sc, |
9e3b2f8c | 1994 | enum lru_list lru) |
1da177e4 | 1995 | { |
44c241f1 | 1996 | unsigned long nr_taken; |
f626012d | 1997 | unsigned long nr_scanned; |
6fe6b7e3 | 1998 | unsigned long vm_flags; |
07f67a8d | 1999 | LIST_HEAD(l_hold); /* The folios which were snipped off */ |
8cab4754 | 2000 | LIST_HEAD(l_active); |
b69408e8 | 2001 | LIST_HEAD(l_inactive); |
9d998b4f MH |
2002 | unsigned nr_deactivate, nr_activate; |
2003 | unsigned nr_rotated = 0; | |
3cb99451 | 2004 | int file = is_file_lru(lru); |
599d0c95 | 2005 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
1da177e4 LT |
2006 | |
2007 | lru_add_drain(); | |
f80c0673 | 2008 | |
6168d0da | 2009 | spin_lock_irq(&lruvec->lru_lock); |
925b7673 | 2010 | |
49fd9b6d | 2011 | nr_taken = isolate_lru_folios(nr_to_scan, lruvec, &l_hold, |
a9e7c39f | 2012 | &nr_scanned, sc, lru); |
89b5fae5 | 2013 | |
599d0c95 | 2014 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); |
1cfb419b | 2015 | |
912c0572 SB |
2016 | if (!cgroup_reclaim(sc)) |
2017 | __count_vm_events(PGREFILL, nr_scanned); | |
2fa2690c | 2018 | __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); |
9d5e6a9f | 2019 | |
6168d0da | 2020 | spin_unlock_irq(&lruvec->lru_lock); |
1da177e4 | 2021 | |
1da177e4 | 2022 | while (!list_empty(&l_hold)) { |
b3ac0413 | 2023 | struct folio *folio; |
b3ac0413 | 2024 | |
1da177e4 | 2025 | cond_resched(); |
b3ac0413 MWO |
2026 | folio = lru_to_folio(&l_hold); |
2027 | list_del(&folio->lru); | |
7e9cd484 | 2028 | |
07f67a8d MWO |
2029 | if (unlikely(!folio_evictable(folio))) { |
2030 | folio_putback_lru(folio); | |
894bc310 LS |
2031 | continue; |
2032 | } | |
2033 | ||
cc715d99 | 2034 | if (unlikely(buffer_heads_over_limit)) { |
0201ebf2 DH |
2035 | if (folio_needs_release(folio) && |
2036 | folio_trylock(folio)) { | |
2037 | filemap_release_folio(folio, 0); | |
07f67a8d | 2038 | folio_unlock(folio); |
cc715d99 MG |
2039 | } |
2040 | } | |
2041 | ||
6d4675e6 | 2042 | /* Referenced or rmap lock contention: rotate */ |
b3ac0413 | 2043 | if (folio_referenced(folio, 0, sc->target_mem_cgroup, |
6d4675e6 | 2044 | &vm_flags) != 0) { |
8cab4754 | 2045 | /* |
07f67a8d | 2046 | * Identify referenced, file-backed active folios and |
8cab4754 WF |
2047 | * give them one more trip around the active list. So |
2048 | * that executable code get better chances to stay in | |
07f67a8d | 2049 | * memory under moderate memory pressure. Anon folios |
8cab4754 | 2050 | * are not likely to be evicted by use-once streaming |
07f67a8d | 2051 | * IO, plus JVM can create lots of anon VM_EXEC folios, |
8cab4754 WF |
2052 | * so we ignore them here. |
2053 | */ | |
07f67a8d MWO |
2054 | if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio)) { |
2055 | nr_rotated += folio_nr_pages(folio); | |
2056 | list_add(&folio->lru, &l_active); | |
8cab4754 WF |
2057 | continue; |
2058 | } | |
2059 | } | |
7e9cd484 | 2060 | |
07f67a8d MWO |
2061 | folio_clear_active(folio); /* we are de-activating */ |
2062 | folio_set_workingset(folio); | |
2063 | list_add(&folio->lru, &l_inactive); | |
1da177e4 LT |
2064 | } |
2065 | ||
b555749a | 2066 | /* |
07f67a8d | 2067 | * Move folios back to the lru list. |
b555749a | 2068 | */ |
6168d0da | 2069 | spin_lock_irq(&lruvec->lru_lock); |
556adecb | 2070 | |
49fd9b6d MWO |
2071 | nr_activate = move_folios_to_lru(lruvec, &l_active); |
2072 | nr_deactivate = move_folios_to_lru(lruvec, &l_inactive); | |
07f67a8d | 2073 | /* Keep all free folios in l_active list */ |
f372d89e | 2074 | list_splice(&l_inactive, &l_active); |
9851ac13 KT |
2075 | |
2076 | __count_vm_events(PGDEACTIVATE, nr_deactivate); | |
2077 | __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate); | |
2078 | ||
599d0c95 | 2079 | __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); |
6168d0da | 2080 | spin_unlock_irq(&lruvec->lru_lock); |
2bcf8879 | 2081 | |
0538a82c JW |
2082 | if (nr_rotated) |
2083 | lru_note_cost(lruvec, file, 0, nr_rotated); | |
f372d89e KT |
2084 | mem_cgroup_uncharge_list(&l_active); |
2085 | free_unref_page_list(&l_active); | |
9d998b4f MH |
2086 | trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, |
2087 | nr_deactivate, nr_rotated, sc->priority, file); | |
1da177e4 LT |
2088 | } |
2089 | ||
49fd9b6d | 2090 | static unsigned int reclaim_folio_list(struct list_head *folio_list, |
1fe47c0b | 2091 | struct pglist_data *pgdat) |
1a4e58cc | 2092 | { |
1a4e58cc | 2093 | struct reclaim_stat dummy_stat; |
1fe47c0b ML |
2094 | unsigned int nr_reclaimed; |
2095 | struct folio *folio; | |
1a4e58cc MK |
2096 | struct scan_control sc = { |
2097 | .gfp_mask = GFP_KERNEL, | |
1a4e58cc MK |
2098 | .may_writepage = 1, |
2099 | .may_unmap = 1, | |
2100 | .may_swap = 1, | |
26aa2d19 | 2101 | .no_demotion = 1, |
1a4e58cc MK |
2102 | }; |
2103 | ||
49fd9b6d MWO |
2104 | nr_reclaimed = shrink_folio_list(folio_list, pgdat, &sc, &dummy_stat, false); |
2105 | while (!list_empty(folio_list)) { | |
2106 | folio = lru_to_folio(folio_list); | |
1fe47c0b ML |
2107 | list_del(&folio->lru); |
2108 | folio_putback_lru(folio); | |
2109 | } | |
2110 | ||
2111 | return nr_reclaimed; | |
2112 | } | |
2113 | ||
a83f0551 | 2114 | unsigned long reclaim_pages(struct list_head *folio_list) |
1fe47c0b | 2115 | { |
ed657e55 | 2116 | int nid; |
1fe47c0b | 2117 | unsigned int nr_reclaimed = 0; |
a83f0551 | 2118 | LIST_HEAD(node_folio_list); |
1fe47c0b ML |
2119 | unsigned int noreclaim_flag; |
2120 | ||
a83f0551 | 2121 | if (list_empty(folio_list)) |
1ae65e27 WY |
2122 | return nr_reclaimed; |
2123 | ||
2d2b8d2b YZ |
2124 | noreclaim_flag = memalloc_noreclaim_save(); |
2125 | ||
a83f0551 | 2126 | nid = folio_nid(lru_to_folio(folio_list)); |
1ae65e27 | 2127 | do { |
a83f0551 | 2128 | struct folio *folio = lru_to_folio(folio_list); |
1a4e58cc | 2129 | |
a83f0551 MWO |
2130 | if (nid == folio_nid(folio)) { |
2131 | folio_clear_active(folio); | |
2132 | list_move(&folio->lru, &node_folio_list); | |
1a4e58cc MK |
2133 | continue; |
2134 | } | |
2135 | ||
49fd9b6d | 2136 | nr_reclaimed += reclaim_folio_list(&node_folio_list, NODE_DATA(nid)); |
a83f0551 MWO |
2137 | nid = folio_nid(lru_to_folio(folio_list)); |
2138 | } while (!list_empty(folio_list)); | |
1a4e58cc | 2139 | |
49fd9b6d | 2140 | nr_reclaimed += reclaim_folio_list(&node_folio_list, NODE_DATA(nid)); |
1a4e58cc | 2141 | |
2d2b8d2b YZ |
2142 | memalloc_noreclaim_restore(noreclaim_flag); |
2143 | ||
1a4e58cc MK |
2144 | return nr_reclaimed; |
2145 | } | |
2146 | ||
b91ac374 JW |
2147 | static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, |
2148 | struct lruvec *lruvec, struct scan_control *sc) | |
2149 | { | |
2150 | if (is_active_lru(lru)) { | |
2151 | if (sc->may_deactivate & (1 << is_file_lru(lru))) | |
2152 | shrink_active_list(nr_to_scan, lruvec, sc, lru); | |
2153 | else | |
2154 | sc->skipped_deactivate = 1; | |
2155 | return 0; | |
2156 | } | |
2157 | ||
2158 | return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); | |
2159 | } | |
2160 | ||
59dc76b0 RR |
2161 | /* |
2162 | * The inactive anon list should be small enough that the VM never has | |
2163 | * to do too much work. | |
14797e23 | 2164 | * |
59dc76b0 RR |
2165 | * The inactive file list should be small enough to leave most memory |
2166 | * to the established workingset on the scan-resistant active list, | |
2167 | * but large enough to avoid thrashing the aggregate readahead window. | |
56e49d21 | 2168 | * |
59dc76b0 | 2169 | * Both inactive lists should also be large enough that each inactive |
49fd9b6d | 2170 | * folio has a chance to be referenced again before it is reclaimed. |
56e49d21 | 2171 | * |
2a2e4885 JW |
2172 | * If that fails and refaulting is observed, the inactive list grows. |
2173 | * | |
49fd9b6d | 2174 | * The inactive_ratio is the target ratio of ACTIVE to INACTIVE folios |
3a50d14d | 2175 | * on this LRU, maintained by the pageout code. An inactive_ratio |
49fd9b6d | 2176 | * of 3 means 3:1 or 25% of the folios are kept on the inactive list. |
56e49d21 | 2177 | * |
59dc76b0 RR |
2178 | * total target max |
2179 | * memory ratio inactive | |
2180 | * ------------------------------------- | |
2181 | * 10MB 1 5MB | |
2182 | * 100MB 1 50MB | |
2183 | * 1GB 3 250MB | |
2184 | * 10GB 10 0.9GB | |
2185 | * 100GB 31 3GB | |
2186 | * 1TB 101 10GB | |
2187 | * 10TB 320 32GB | |
56e49d21 | 2188 | */ |
b91ac374 | 2189 | static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru) |
56e49d21 | 2190 | { |
b91ac374 | 2191 | enum lru_list active_lru = inactive_lru + LRU_ACTIVE; |
2a2e4885 JW |
2192 | unsigned long inactive, active; |
2193 | unsigned long inactive_ratio; | |
59dc76b0 | 2194 | unsigned long gb; |
e3790144 | 2195 | |
b91ac374 JW |
2196 | inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru); |
2197 | active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru); | |
f8d1a311 | 2198 | |
b91ac374 | 2199 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
4002570c | 2200 | if (gb) |
b91ac374 JW |
2201 | inactive_ratio = int_sqrt(10 * gb); |
2202 | else | |
2203 | inactive_ratio = 1; | |
fd538803 | 2204 | |
59dc76b0 | 2205 | return inactive * inactive_ratio < active; |
b39415b2 RR |
2206 | } |
2207 | ||
9a265114 JW |
2208 | enum scan_balance { |
2209 | SCAN_EQUAL, | |
2210 | SCAN_FRACT, | |
2211 | SCAN_ANON, | |
2212 | SCAN_FILE, | |
2213 | }; | |
2214 | ||
ed547ab6 | 2215 | static void prepare_scan_control(pg_data_t *pgdat, struct scan_control *sc) |
f1e1a7be YZ |
2216 | { |
2217 | unsigned long file; | |
2218 | struct lruvec *target_lruvec; | |
2219 | ||
ac35a490 YZ |
2220 | if (lru_gen_enabled()) |
2221 | return; | |
2222 | ||
f1e1a7be YZ |
2223 | target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); |
2224 | ||
2225 | /* | |
2226 | * Flush the memory cgroup stats, so that we read accurate per-memcg | |
2227 | * lruvec stats for heuristics. | |
2228 | */ | |
7d7ef0a4 | 2229 | mem_cgroup_flush_stats(sc->target_mem_cgroup); |
f1e1a7be YZ |
2230 | |
2231 | /* | |
2232 | * Determine the scan balance between anon and file LRUs. | |
2233 | */ | |
2234 | spin_lock_irq(&target_lruvec->lru_lock); | |
2235 | sc->anon_cost = target_lruvec->anon_cost; | |
2236 | sc->file_cost = target_lruvec->file_cost; | |
2237 | spin_unlock_irq(&target_lruvec->lru_lock); | |
2238 | ||
2239 | /* | |
2240 | * Target desirable inactive:active list ratios for the anon | |
2241 | * and file LRU lists. | |
2242 | */ | |
2243 | if (!sc->force_deactivate) { | |
2244 | unsigned long refaults; | |
2245 | ||
2246 | /* | |
2247 | * When refaults are being observed, it means a new | |
2248 | * workingset is being established. Deactivate to get | |
2249 | * rid of any stale active pages quickly. | |
2250 | */ | |
2251 | refaults = lruvec_page_state(target_lruvec, | |
2252 | WORKINGSET_ACTIVATE_ANON); | |
2253 | if (refaults != target_lruvec->refaults[WORKINGSET_ANON] || | |
2254 | inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)) | |
2255 | sc->may_deactivate |= DEACTIVATE_ANON; | |
2256 | else | |
2257 | sc->may_deactivate &= ~DEACTIVATE_ANON; | |
2258 | ||
2259 | refaults = lruvec_page_state(target_lruvec, | |
2260 | WORKINGSET_ACTIVATE_FILE); | |
2261 | if (refaults != target_lruvec->refaults[WORKINGSET_FILE] || | |
2262 | inactive_is_low(target_lruvec, LRU_INACTIVE_FILE)) | |
2263 | sc->may_deactivate |= DEACTIVATE_FILE; | |
2264 | else | |
2265 | sc->may_deactivate &= ~DEACTIVATE_FILE; | |
2266 | } else | |
2267 | sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE; | |
2268 | ||
2269 | /* | |
2270 | * If we have plenty of inactive file pages that aren't | |
2271 | * thrashing, try to reclaim those first before touching | |
2272 | * anonymous pages. | |
2273 | */ | |
2274 | file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE); | |
2275 | if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE)) | |
2276 | sc->cache_trim_mode = 1; | |
2277 | else | |
2278 | sc->cache_trim_mode = 0; | |
2279 | ||
2280 | /* | |
2281 | * Prevent the reclaimer from falling into the cache trap: as | |
2282 | * cache pages start out inactive, every cache fault will tip | |
2283 | * the scan balance towards the file LRU. And as the file LRU | |
2284 | * shrinks, so does the window for rotation from references. | |
2285 | * This means we have a runaway feedback loop where a tiny | |
2286 | * thrashing file LRU becomes infinitely more attractive than | |
2287 | * anon pages. Try to detect this based on file LRU size. | |
2288 | */ | |
2289 | if (!cgroup_reclaim(sc)) { | |
2290 | unsigned long total_high_wmark = 0; | |
2291 | unsigned long free, anon; | |
2292 | int z; | |
2293 | ||
2294 | free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); | |
2295 | file = node_page_state(pgdat, NR_ACTIVE_FILE) + | |
2296 | node_page_state(pgdat, NR_INACTIVE_FILE); | |
2297 | ||
2298 | for (z = 0; z < MAX_NR_ZONES; z++) { | |
2299 | struct zone *zone = &pgdat->node_zones[z]; | |
2300 | ||
2301 | if (!managed_zone(zone)) | |
2302 | continue; | |
2303 | ||
2304 | total_high_wmark += high_wmark_pages(zone); | |
2305 | } | |
2306 | ||
2307 | /* | |
2308 | * Consider anon: if that's low too, this isn't a | |
2309 | * runaway file reclaim problem, but rather just | |
2310 | * extreme pressure. Reclaim as per usual then. | |
2311 | */ | |
2312 | anon = node_page_state(pgdat, NR_INACTIVE_ANON); | |
2313 | ||
2314 | sc->file_is_tiny = | |
2315 | file + free <= total_high_wmark && | |
2316 | !(sc->may_deactivate & DEACTIVATE_ANON) && | |
2317 | anon >> sc->priority; | |
2318 | } | |
2319 | } | |
2320 | ||
4f98a2fe RR |
2321 | /* |
2322 | * Determine how aggressively the anon and file LRU lists should be | |
02e458d8 | 2323 | * scanned. |
4f98a2fe | 2324 | * |
49fd9b6d MWO |
2325 | * nr[0] = anon inactive folios to scan; nr[1] = anon active folios to scan |
2326 | * nr[2] = file inactive folios to scan; nr[3] = file active folios to scan | |
4f98a2fe | 2327 | */ |
afaf07a6 JW |
2328 | static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, |
2329 | unsigned long *nr) | |
4f98a2fe | 2330 | { |
a2a36488 | 2331 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); |
afaf07a6 | 2332 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); |
d483a5dd | 2333 | unsigned long anon_cost, file_cost, total_cost; |
33377678 | 2334 | int swappiness = mem_cgroup_swappiness(memcg); |
ed017373 | 2335 | u64 fraction[ANON_AND_FILE]; |
9a265114 | 2336 | u64 denominator = 0; /* gcc */ |
9a265114 | 2337 | enum scan_balance scan_balance; |
4f98a2fe | 2338 | unsigned long ap, fp; |
4111304d | 2339 | enum lru_list lru; |
76a33fc3 | 2340 | |
49fd9b6d | 2341 | /* If we have no swap space, do not bother scanning anon folios. */ |
a2a36488 | 2342 | if (!sc->may_swap || !can_reclaim_anon_pages(memcg, pgdat->node_id, sc)) { |
9a265114 | 2343 | scan_balance = SCAN_FILE; |
76a33fc3 SL |
2344 | goto out; |
2345 | } | |
4f98a2fe | 2346 | |
10316b31 JW |
2347 | /* |
2348 | * Global reclaim will swap to prevent OOM even with no | |
2349 | * swappiness, but memcg users want to use this knob to | |
2350 | * disable swapping for individual groups completely when | |
2351 | * using the memory controller's swap limit feature would be | |
2352 | * too expensive. | |
2353 | */ | |
b5ead35e | 2354 | if (cgroup_reclaim(sc) && !swappiness) { |
9a265114 | 2355 | scan_balance = SCAN_FILE; |
10316b31 JW |
2356 | goto out; |
2357 | } | |
2358 | ||
2359 | /* | |
2360 | * Do not apply any pressure balancing cleverness when the | |
2361 | * system is close to OOM, scan both anon and file equally | |
2362 | * (unless the swappiness setting disagrees with swapping). | |
2363 | */ | |
02695175 | 2364 | if (!sc->priority && swappiness) { |
9a265114 | 2365 | scan_balance = SCAN_EQUAL; |
10316b31 JW |
2366 | goto out; |
2367 | } | |
2368 | ||
62376251 | 2369 | /* |
53138cea | 2370 | * If the system is almost out of file pages, force-scan anon. |
62376251 | 2371 | */ |
b91ac374 | 2372 | if (sc->file_is_tiny) { |
53138cea JW |
2373 | scan_balance = SCAN_ANON; |
2374 | goto out; | |
62376251 JW |
2375 | } |
2376 | ||
7c5bd705 | 2377 | /* |
b91ac374 JW |
2378 | * If there is enough inactive page cache, we do not reclaim |
2379 | * anything from the anonymous working right now. | |
7c5bd705 | 2380 | */ |
b91ac374 | 2381 | if (sc->cache_trim_mode) { |
9a265114 | 2382 | scan_balance = SCAN_FILE; |
7c5bd705 JW |
2383 | goto out; |
2384 | } | |
2385 | ||
9a265114 | 2386 | scan_balance = SCAN_FRACT; |
58c37f6e | 2387 | /* |
314b57fb JW |
2388 | * Calculate the pressure balance between anon and file pages. |
2389 | * | |
2390 | * The amount of pressure we put on each LRU is inversely | |
2391 | * proportional to the cost of reclaiming each list, as | |
2392 | * determined by the share of pages that are refaulting, times | |
2393 | * the relative IO cost of bringing back a swapped out | |
2394 | * anonymous page vs reloading a filesystem page (swappiness). | |
2395 | * | |
d483a5dd JW |
2396 | * Although we limit that influence to ensure no list gets |
2397 | * left behind completely: at least a third of the pressure is | |
2398 | * applied, before swappiness. | |
2399 | * | |
314b57fb | 2400 | * With swappiness at 100, anon and file have equal IO cost. |
58c37f6e | 2401 | */ |
d483a5dd JW |
2402 | total_cost = sc->anon_cost + sc->file_cost; |
2403 | anon_cost = total_cost + sc->anon_cost; | |
2404 | file_cost = total_cost + sc->file_cost; | |
2405 | total_cost = anon_cost + file_cost; | |
58c37f6e | 2406 | |
d483a5dd JW |
2407 | ap = swappiness * (total_cost + 1); |
2408 | ap /= anon_cost + 1; | |
4f98a2fe | 2409 | |
d483a5dd JW |
2410 | fp = (200 - swappiness) * (total_cost + 1); |
2411 | fp /= file_cost + 1; | |
4f98a2fe | 2412 | |
76a33fc3 SL |
2413 | fraction[0] = ap; |
2414 | fraction[1] = fp; | |
a4fe1631 | 2415 | denominator = ap + fp; |
76a33fc3 | 2416 | out: |
688035f7 JW |
2417 | for_each_evictable_lru(lru) { |
2418 | int file = is_file_lru(lru); | |
9783aa99 | 2419 | unsigned long lruvec_size; |
f56ce412 | 2420 | unsigned long low, min; |
688035f7 | 2421 | unsigned long scan; |
9783aa99 CD |
2422 | |
2423 | lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); | |
f56ce412 JW |
2424 | mem_cgroup_protection(sc->target_mem_cgroup, memcg, |
2425 | &min, &low); | |
9783aa99 | 2426 | |
f56ce412 | 2427 | if (min || low) { |
9783aa99 CD |
2428 | /* |
2429 | * Scale a cgroup's reclaim pressure by proportioning | |
2430 | * its current usage to its memory.low or memory.min | |
2431 | * setting. | |
2432 | * | |
2433 | * This is important, as otherwise scanning aggression | |
2434 | * becomes extremely binary -- from nothing as we | |
2435 | * approach the memory protection threshold, to totally | |
2436 | * nominal as we exceed it. This results in requiring | |
2437 | * setting extremely liberal protection thresholds. It | |
2438 | * also means we simply get no protection at all if we | |
2439 | * set it too low, which is not ideal. | |
1bc63fb1 CD |
2440 | * |
2441 | * If there is any protection in place, we reduce scan | |
2442 | * pressure by how much of the total memory used is | |
2443 | * within protection thresholds. | |
9783aa99 | 2444 | * |
9de7ca46 CD |
2445 | * There is one special case: in the first reclaim pass, |
2446 | * we skip over all groups that are within their low | |
2447 | * protection. If that fails to reclaim enough pages to | |
2448 | * satisfy the reclaim goal, we come back and override | |
2449 | * the best-effort low protection. However, we still | |
2450 | * ideally want to honor how well-behaved groups are in | |
2451 | * that case instead of simply punishing them all | |
2452 | * equally. As such, we reclaim them based on how much | |
1bc63fb1 CD |
2453 | * memory they are using, reducing the scan pressure |
2454 | * again by how much of the total memory used is under | |
2455 | * hard protection. | |
9783aa99 | 2456 | */ |
1bc63fb1 | 2457 | unsigned long cgroup_size = mem_cgroup_size(memcg); |
f56ce412 JW |
2458 | unsigned long protection; |
2459 | ||
2460 | /* memory.low scaling, make sure we retry before OOM */ | |
2461 | if (!sc->memcg_low_reclaim && low > min) { | |
2462 | protection = low; | |
2463 | sc->memcg_low_skipped = 1; | |
2464 | } else { | |
2465 | protection = min; | |
2466 | } | |
1bc63fb1 CD |
2467 | |
2468 | /* Avoid TOCTOU with earlier protection check */ | |
2469 | cgroup_size = max(cgroup_size, protection); | |
2470 | ||
2471 | scan = lruvec_size - lruvec_size * protection / | |
32d4f4b7 | 2472 | (cgroup_size + 1); |
9783aa99 CD |
2473 | |
2474 | /* | |
1bc63fb1 | 2475 | * Minimally target SWAP_CLUSTER_MAX pages to keep |
55b65a57 | 2476 | * reclaim moving forwards, avoiding decrementing |
9de7ca46 | 2477 | * sc->priority further than desirable. |
9783aa99 | 2478 | */ |
1bc63fb1 | 2479 | scan = max(scan, SWAP_CLUSTER_MAX); |
9783aa99 CD |
2480 | } else { |
2481 | scan = lruvec_size; | |
2482 | } | |
2483 | ||
2484 | scan >>= sc->priority; | |
6b4f7799 | 2485 | |
688035f7 JW |
2486 | /* |
2487 | * If the cgroup's already been deleted, make sure to | |
2488 | * scrape out the remaining cache. | |
2489 | */ | |
2490 | if (!scan && !mem_cgroup_online(memcg)) | |
9783aa99 | 2491 | scan = min(lruvec_size, SWAP_CLUSTER_MAX); |
6b4f7799 | 2492 | |
688035f7 JW |
2493 | switch (scan_balance) { |
2494 | case SCAN_EQUAL: | |
2495 | /* Scan lists relative to size */ | |
2496 | break; | |
2497 | case SCAN_FRACT: | |
9a265114 | 2498 | /* |
688035f7 JW |
2499 | * Scan types proportional to swappiness and |
2500 | * their relative recent reclaim efficiency. | |
76073c64 GS |
2501 | * Make sure we don't miss the last page on |
2502 | * the offlined memory cgroups because of a | |
2503 | * round-off error. | |
9a265114 | 2504 | */ |
76073c64 GS |
2505 | scan = mem_cgroup_online(memcg) ? |
2506 | div64_u64(scan * fraction[file], denominator) : | |
2507 | DIV64_U64_ROUND_UP(scan * fraction[file], | |
68600f62 | 2508 | denominator); |
688035f7 JW |
2509 | break; |
2510 | case SCAN_FILE: | |
2511 | case SCAN_ANON: | |
2512 | /* Scan one type exclusively */ | |
e072bff6 | 2513 | if ((scan_balance == SCAN_FILE) != file) |
688035f7 | 2514 | scan = 0; |
688035f7 JW |
2515 | break; |
2516 | default: | |
2517 | /* Look ma, no brain */ | |
2518 | BUG(); | |
9a265114 | 2519 | } |
688035f7 | 2520 | |
688035f7 | 2521 | nr[lru] = scan; |
76a33fc3 | 2522 | } |
6e08a369 | 2523 | } |
4f98a2fe | 2524 | |
2f368a9f DH |
2525 | /* |
2526 | * Anonymous LRU management is a waste if there is | |
2527 | * ultimately no way to reclaim the memory. | |
2528 | */ | |
2529 | static bool can_age_anon_pages(struct pglist_data *pgdat, | |
2530 | struct scan_control *sc) | |
2531 | { | |
2532 | /* Aging the anon LRU is valuable if swap is present: */ | |
2533 | if (total_swap_pages > 0) | |
2534 | return true; | |
2535 | ||
2536 | /* Also valuable if anon pages can be demoted: */ | |
2537 | return can_demote(pgdat->node_id, sc); | |
2538 | } | |
2539 | ||
ec1c86b2 YZ |
2540 | #ifdef CONFIG_LRU_GEN |
2541 | ||
354ed597 YZ |
2542 | #ifdef CONFIG_LRU_GEN_ENABLED |
2543 | DEFINE_STATIC_KEY_ARRAY_TRUE(lru_gen_caps, NR_LRU_GEN_CAPS); | |
2544 | #define get_cap(cap) static_branch_likely(&lru_gen_caps[cap]) | |
2545 | #else | |
2546 | DEFINE_STATIC_KEY_ARRAY_FALSE(lru_gen_caps, NR_LRU_GEN_CAPS); | |
2547 | #define get_cap(cap) static_branch_unlikely(&lru_gen_caps[cap]) | |
2548 | #endif | |
2549 | ||
bd02df41 A |
2550 | static bool should_walk_mmu(void) |
2551 | { | |
2552 | return arch_has_hw_pte_young() && get_cap(LRU_GEN_MM_WALK); | |
2553 | } | |
2554 | ||
2555 | static bool should_clear_pmd_young(void) | |
2556 | { | |
2557 | return arch_has_hw_nonleaf_pmd_young() && get_cap(LRU_GEN_NONLEAF_YOUNG); | |
2558 | } | |
2559 | ||
ec1c86b2 YZ |
2560 | /****************************************************************************** |
2561 | * shorthand helpers | |
2562 | ******************************************************************************/ | |
2563 | ||
ac35a490 YZ |
2564 | #define LRU_REFS_FLAGS (BIT(PG_referenced) | BIT(PG_workingset)) |
2565 | ||
2566 | #define DEFINE_MAX_SEQ(lruvec) \ | |
2567 | unsigned long max_seq = READ_ONCE((lruvec)->lrugen.max_seq) | |
2568 | ||
2569 | #define DEFINE_MIN_SEQ(lruvec) \ | |
2570 | unsigned long min_seq[ANON_AND_FILE] = { \ | |
2571 | READ_ONCE((lruvec)->lrugen.min_seq[LRU_GEN_ANON]), \ | |
2572 | READ_ONCE((lruvec)->lrugen.min_seq[LRU_GEN_FILE]), \ | |
2573 | } | |
2574 | ||
ec1c86b2 YZ |
2575 | #define for_each_gen_type_zone(gen, type, zone) \ |
2576 | for ((gen) = 0; (gen) < MAX_NR_GENS; (gen)++) \ | |
2577 | for ((type) = 0; (type) < ANON_AND_FILE; (type)++) \ | |
2578 | for ((zone) = 0; (zone) < MAX_NR_ZONES; (zone)++) | |
2579 | ||
e4dde56c YZ |
2580 | #define get_memcg_gen(seq) ((seq) % MEMCG_NR_GENS) |
2581 | #define get_memcg_bin(bin) ((bin) % MEMCG_NR_BINS) | |
2582 | ||
bd74fdae | 2583 | static struct lruvec *get_lruvec(struct mem_cgroup *memcg, int nid) |
ec1c86b2 YZ |
2584 | { |
2585 | struct pglist_data *pgdat = NODE_DATA(nid); | |
2586 | ||
2587 | #ifdef CONFIG_MEMCG | |
2588 | if (memcg) { | |
2589 | struct lruvec *lruvec = &memcg->nodeinfo[nid]->lruvec; | |
2590 | ||
931b6a8b | 2591 | /* see the comment in mem_cgroup_lruvec() */ |
ec1c86b2 YZ |
2592 | if (!lruvec->pgdat) |
2593 | lruvec->pgdat = pgdat; | |
2594 | ||
2595 | return lruvec; | |
2596 | } | |
2597 | #endif | |
2598 | VM_WARN_ON_ONCE(!mem_cgroup_disabled()); | |
2599 | ||
931b6a8b | 2600 | return &pgdat->__lruvec; |
ec1c86b2 YZ |
2601 | } |
2602 | ||
ac35a490 YZ |
2603 | static int get_swappiness(struct lruvec *lruvec, struct scan_control *sc) |
2604 | { | |
2605 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
2606 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); | |
2607 | ||
e9d4e1ee YZ |
2608 | if (!sc->may_swap) |
2609 | return 0; | |
2610 | ||
ac35a490 YZ |
2611 | if (!can_demote(pgdat->node_id, sc) && |
2612 | mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH) | |
2613 | return 0; | |
2614 | ||
2615 | return mem_cgroup_swappiness(memcg); | |
2616 | } | |
2617 | ||
2618 | static int get_nr_gens(struct lruvec *lruvec, int type) | |
2619 | { | |
2620 | return lruvec->lrugen.max_seq - lruvec->lrugen.min_seq[type] + 1; | |
2621 | } | |
2622 | ||
2623 | static bool __maybe_unused seq_is_valid(struct lruvec *lruvec) | |
2624 | { | |
391655fe | 2625 | /* see the comment on lru_gen_folio */ |
ac35a490 YZ |
2626 | return get_nr_gens(lruvec, LRU_GEN_FILE) >= MIN_NR_GENS && |
2627 | get_nr_gens(lruvec, LRU_GEN_FILE) <= get_nr_gens(lruvec, LRU_GEN_ANON) && | |
2628 | get_nr_gens(lruvec, LRU_GEN_ANON) <= MAX_NR_GENS; | |
2629 | } | |
2630 | ||
ccbbbb85 A |
2631 | /****************************************************************************** |
2632 | * Bloom filters | |
2633 | ******************************************************************************/ | |
2634 | ||
2635 | /* | |
2636 | * Bloom filters with m=1<<15, k=2 and the false positive rates of ~1/5 when | |
2637 | * n=10,000 and ~1/2 when n=20,000, where, conventionally, m is the number of | |
2638 | * bits in a bitmap, k is the number of hash functions and n is the number of | |
2639 | * inserted items. | |
2640 | * | |
2641 | * Page table walkers use one of the two filters to reduce their search space. | |
2642 | * To get rid of non-leaf entries that no longer have enough leaf entries, the | |
2643 | * aging uses the double-buffering technique to flip to the other filter each | |
2644 | * time it produces a new generation. For non-leaf entries that have enough | |
2645 | * leaf entries, the aging carries them over to the next generation in | |
2646 | * walk_pmd_range(); the eviction also report them when walking the rmap | |
2647 | * in lru_gen_look_around(). | |
2648 | * | |
2649 | * For future optimizations: | |
2650 | * 1. It's not necessary to keep both filters all the time. The spare one can be | |
2651 | * freed after the RCU grace period and reallocated if needed again. | |
2652 | * 2. And when reallocating, it's worth scaling its size according to the number | |
2653 | * of inserted entries in the other filter, to reduce the memory overhead on | |
2654 | * small systems and false positives on large systems. | |
2655 | * 3. Jenkins' hash function is an alternative to Knuth's. | |
2656 | */ | |
2657 | #define BLOOM_FILTER_SHIFT 15 | |
2658 | ||
2659 | static inline int filter_gen_from_seq(unsigned long seq) | |
2660 | { | |
2661 | return seq % NR_BLOOM_FILTERS; | |
2662 | } | |
2663 | ||
2664 | static void get_item_key(void *item, int *key) | |
2665 | { | |
2666 | u32 hash = hash_ptr(item, BLOOM_FILTER_SHIFT * 2); | |
2667 | ||
2668 | BUILD_BUG_ON(BLOOM_FILTER_SHIFT * 2 > BITS_PER_TYPE(u32)); | |
2669 | ||
2670 | key[0] = hash & (BIT(BLOOM_FILTER_SHIFT) - 1); | |
2671 | key[1] = hash >> BLOOM_FILTER_SHIFT; | |
2672 | } | |
2673 | ||
2674 | static bool test_bloom_filter(struct lruvec *lruvec, unsigned long seq, void *item) | |
2675 | { | |
2676 | int key[2]; | |
2677 | unsigned long *filter; | |
2678 | int gen = filter_gen_from_seq(seq); | |
2679 | ||
2680 | filter = READ_ONCE(lruvec->mm_state.filters[gen]); | |
2681 | if (!filter) | |
2682 | return true; | |
2683 | ||
2684 | get_item_key(item, key); | |
2685 | ||
2686 | return test_bit(key[0], filter) && test_bit(key[1], filter); | |
2687 | } | |
2688 | ||
2689 | static void update_bloom_filter(struct lruvec *lruvec, unsigned long seq, void *item) | |
2690 | { | |
2691 | int key[2]; | |
2692 | unsigned long *filter; | |
2693 | int gen = filter_gen_from_seq(seq); | |
2694 | ||
2695 | filter = READ_ONCE(lruvec->mm_state.filters[gen]); | |
2696 | if (!filter) | |
2697 | return; | |
2698 | ||
2699 | get_item_key(item, key); | |
2700 | ||
2701 | if (!test_bit(key[0], filter)) | |
2702 | set_bit(key[0], filter); | |
2703 | if (!test_bit(key[1], filter)) | |
2704 | set_bit(key[1], filter); | |
2705 | } | |
2706 | ||
2707 | static void reset_bloom_filter(struct lruvec *lruvec, unsigned long seq) | |
2708 | { | |
2709 | unsigned long *filter; | |
2710 | int gen = filter_gen_from_seq(seq); | |
2711 | ||
2712 | filter = lruvec->mm_state.filters[gen]; | |
2713 | if (filter) { | |
2714 | bitmap_clear(filter, 0, BIT(BLOOM_FILTER_SHIFT)); | |
2715 | return; | |
2716 | } | |
2717 | ||
2718 | filter = bitmap_zalloc(BIT(BLOOM_FILTER_SHIFT), | |
2719 | __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN); | |
2720 | WRITE_ONCE(lruvec->mm_state.filters[gen], filter); | |
2721 | } | |
2722 | ||
bd74fdae YZ |
2723 | /****************************************************************************** |
2724 | * mm_struct list | |
2725 | ******************************************************************************/ | |
2726 | ||
2727 | static struct lru_gen_mm_list *get_mm_list(struct mem_cgroup *memcg) | |
2728 | { | |
2729 | static struct lru_gen_mm_list mm_list = { | |
2730 | .fifo = LIST_HEAD_INIT(mm_list.fifo), | |
2731 | .lock = __SPIN_LOCK_UNLOCKED(mm_list.lock), | |
2732 | }; | |
2733 | ||
2734 | #ifdef CONFIG_MEMCG | |
2735 | if (memcg) | |
2736 | return &memcg->mm_list; | |
2737 | #endif | |
2738 | VM_WARN_ON_ONCE(!mem_cgroup_disabled()); | |
2739 | ||
2740 | return &mm_list; | |
2741 | } | |
2742 | ||
2743 | void lru_gen_add_mm(struct mm_struct *mm) | |
2744 | { | |
2745 | int nid; | |
2746 | struct mem_cgroup *memcg = get_mem_cgroup_from_mm(mm); | |
2747 | struct lru_gen_mm_list *mm_list = get_mm_list(memcg); | |
2748 | ||
2749 | VM_WARN_ON_ONCE(!list_empty(&mm->lru_gen.list)); | |
2750 | #ifdef CONFIG_MEMCG | |
2751 | VM_WARN_ON_ONCE(mm->lru_gen.memcg); | |
2752 | mm->lru_gen.memcg = memcg; | |
2753 | #endif | |
2754 | spin_lock(&mm_list->lock); | |
2755 | ||
2756 | for_each_node_state(nid, N_MEMORY) { | |
2757 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
2758 | ||
bd74fdae YZ |
2759 | /* the first addition since the last iteration */ |
2760 | if (lruvec->mm_state.tail == &mm_list->fifo) | |
2761 | lruvec->mm_state.tail = &mm->lru_gen.list; | |
2762 | } | |
2763 | ||
2764 | list_add_tail(&mm->lru_gen.list, &mm_list->fifo); | |
2765 | ||
2766 | spin_unlock(&mm_list->lock); | |
2767 | } | |
2768 | ||
2769 | void lru_gen_del_mm(struct mm_struct *mm) | |
2770 | { | |
2771 | int nid; | |
2772 | struct lru_gen_mm_list *mm_list; | |
2773 | struct mem_cgroup *memcg = NULL; | |
2774 | ||
2775 | if (list_empty(&mm->lru_gen.list)) | |
2776 | return; | |
2777 | ||
2778 | #ifdef CONFIG_MEMCG | |
2779 | memcg = mm->lru_gen.memcg; | |
2780 | #endif | |
2781 | mm_list = get_mm_list(memcg); | |
2782 | ||
2783 | spin_lock(&mm_list->lock); | |
2784 | ||
2785 | for_each_node(nid) { | |
2786 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
2787 | ||
7f63cf2d KS |
2788 | /* where the current iteration continues after */ |
2789 | if (lruvec->mm_state.head == &mm->lru_gen.list) | |
2790 | lruvec->mm_state.head = lruvec->mm_state.head->prev; | |
2791 | ||
2792 | /* where the last iteration ended before */ | |
bd74fdae YZ |
2793 | if (lruvec->mm_state.tail == &mm->lru_gen.list) |
2794 | lruvec->mm_state.tail = lruvec->mm_state.tail->next; | |
bd74fdae YZ |
2795 | } |
2796 | ||
2797 | list_del_init(&mm->lru_gen.list); | |
2798 | ||
2799 | spin_unlock(&mm_list->lock); | |
2800 | ||
2801 | #ifdef CONFIG_MEMCG | |
2802 | mem_cgroup_put(mm->lru_gen.memcg); | |
2803 | mm->lru_gen.memcg = NULL; | |
2804 | #endif | |
2805 | } | |
2806 | ||
2807 | #ifdef CONFIG_MEMCG | |
2808 | void lru_gen_migrate_mm(struct mm_struct *mm) | |
2809 | { | |
2810 | struct mem_cgroup *memcg; | |
2811 | struct task_struct *task = rcu_dereference_protected(mm->owner, true); | |
2812 | ||
2813 | VM_WARN_ON_ONCE(task->mm != mm); | |
2814 | lockdep_assert_held(&task->alloc_lock); | |
2815 | ||
2816 | /* for mm_update_next_owner() */ | |
2817 | if (mem_cgroup_disabled()) | |
2818 | return; | |
2819 | ||
de08eaa6 YZ |
2820 | /* migration can happen before addition */ |
2821 | if (!mm->lru_gen.memcg) | |
2822 | return; | |
2823 | ||
bd74fdae YZ |
2824 | rcu_read_lock(); |
2825 | memcg = mem_cgroup_from_task(task); | |
2826 | rcu_read_unlock(); | |
2827 | if (memcg == mm->lru_gen.memcg) | |
2828 | return; | |
2829 | ||
bd74fdae YZ |
2830 | VM_WARN_ON_ONCE(list_empty(&mm->lru_gen.list)); |
2831 | ||
2832 | lru_gen_del_mm(mm); | |
2833 | lru_gen_add_mm(mm); | |
2834 | } | |
2835 | #endif | |
2836 | ||
bd74fdae YZ |
2837 | static void reset_mm_stats(struct lruvec *lruvec, struct lru_gen_mm_walk *walk, bool last) |
2838 | { | |
2839 | int i; | |
2840 | int hist; | |
2841 | ||
2842 | lockdep_assert_held(&get_mm_list(lruvec_memcg(lruvec))->lock); | |
2843 | ||
2844 | if (walk) { | |
2845 | hist = lru_hist_from_seq(walk->max_seq); | |
2846 | ||
2847 | for (i = 0; i < NR_MM_STATS; i++) { | |
2848 | WRITE_ONCE(lruvec->mm_state.stats[hist][i], | |
2849 | lruvec->mm_state.stats[hist][i] + walk->mm_stats[i]); | |
2850 | walk->mm_stats[i] = 0; | |
2851 | } | |
2852 | } | |
2853 | ||
2854 | if (NR_HIST_GENS > 1 && last) { | |
2855 | hist = lru_hist_from_seq(lruvec->mm_state.seq + 1); | |
2856 | ||
2857 | for (i = 0; i < NR_MM_STATS; i++) | |
2858 | WRITE_ONCE(lruvec->mm_state.stats[hist][i], 0); | |
2859 | } | |
2860 | } | |
2861 | ||
2862 | static bool should_skip_mm(struct mm_struct *mm, struct lru_gen_mm_walk *walk) | |
2863 | { | |
2864 | int type; | |
2865 | unsigned long size = 0; | |
2866 | struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec); | |
2867 | int key = pgdat->node_id % BITS_PER_TYPE(mm->lru_gen.bitmap); | |
2868 | ||
2869 | if (!walk->force_scan && !test_bit(key, &mm->lru_gen.bitmap)) | |
2870 | return true; | |
2871 | ||
2872 | clear_bit(key, &mm->lru_gen.bitmap); | |
2873 | ||
2874 | for (type = !walk->can_swap; type < ANON_AND_FILE; type++) { | |
2875 | size += type ? get_mm_counter(mm, MM_FILEPAGES) : | |
2876 | get_mm_counter(mm, MM_ANONPAGES) + | |
2877 | get_mm_counter(mm, MM_SHMEMPAGES); | |
2878 | } | |
2879 | ||
2880 | if (size < MIN_LRU_BATCH) | |
2881 | return true; | |
2882 | ||
2883 | return !mmget_not_zero(mm); | |
2884 | } | |
2885 | ||
2886 | static bool iterate_mm_list(struct lruvec *lruvec, struct lru_gen_mm_walk *walk, | |
2887 | struct mm_struct **iter) | |
2888 | { | |
2889 | bool first = false; | |
7f63cf2d | 2890 | bool last = false; |
bd74fdae YZ |
2891 | struct mm_struct *mm = NULL; |
2892 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
2893 | struct lru_gen_mm_list *mm_list = get_mm_list(memcg); | |
2894 | struct lru_gen_mm_state *mm_state = &lruvec->mm_state; | |
2895 | ||
2896 | /* | |
7f63cf2d KS |
2897 | * mm_state->seq is incremented after each iteration of mm_list. There |
2898 | * are three interesting cases for this page table walker: | |
2899 | * 1. It tries to start a new iteration with a stale max_seq: there is | |
2900 | * nothing left to do. | |
2901 | * 2. It started the next iteration: it needs to reset the Bloom filter | |
2902 | * so that a fresh set of PTE tables can be recorded. | |
2903 | * 3. It ended the current iteration: it needs to reset the mm stats | |
2904 | * counters and tell its caller to increment max_seq. | |
bd74fdae YZ |
2905 | */ |
2906 | spin_lock(&mm_list->lock); | |
2907 | ||
2908 | VM_WARN_ON_ONCE(mm_state->seq + 1 < walk->max_seq); | |
bd74fdae | 2909 | |
7f63cf2d | 2910 | if (walk->max_seq <= mm_state->seq) |
bd74fdae | 2911 | goto done; |
bd74fdae | 2912 | |
7f63cf2d KS |
2913 | if (!mm_state->head) |
2914 | mm_state->head = &mm_list->fifo; | |
bd74fdae | 2915 | |
7f63cf2d | 2916 | if (mm_state->head == &mm_list->fifo) |
bd74fdae | 2917 | first = true; |
bd74fdae | 2918 | |
7f63cf2d | 2919 | do { |
bd74fdae | 2920 | mm_state->head = mm_state->head->next; |
7f63cf2d KS |
2921 | if (mm_state->head == &mm_list->fifo) { |
2922 | WRITE_ONCE(mm_state->seq, mm_state->seq + 1); | |
2923 | last = true; | |
2924 | break; | |
2925 | } | |
bd74fdae YZ |
2926 | |
2927 | /* force scan for those added after the last iteration */ | |
7f63cf2d KS |
2928 | if (!mm_state->tail || mm_state->tail == mm_state->head) { |
2929 | mm_state->tail = mm_state->head->next; | |
bd74fdae YZ |
2930 | walk->force_scan = true; |
2931 | } | |
2932 | ||
7f63cf2d | 2933 | mm = list_entry(mm_state->head, struct mm_struct, lru_gen.list); |
bd74fdae YZ |
2934 | if (should_skip_mm(mm, walk)) |
2935 | mm = NULL; | |
7f63cf2d | 2936 | } while (!mm); |
bd74fdae | 2937 | done: |
bd74fdae YZ |
2938 | if (*iter || last) |
2939 | reset_mm_stats(lruvec, walk, last); | |
2940 | ||
2941 | spin_unlock(&mm_list->lock); | |
2942 | ||
2943 | if (mm && first) | |
2944 | reset_bloom_filter(lruvec, walk->max_seq + 1); | |
2945 | ||
2946 | if (*iter) | |
2947 | mmput_async(*iter); | |
2948 | ||
2949 | *iter = mm; | |
2950 | ||
2951 | return last; | |
2952 | } | |
2953 | ||
2954 | static bool iterate_mm_list_nowalk(struct lruvec *lruvec, unsigned long max_seq) | |
2955 | { | |
2956 | bool success = false; | |
2957 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
2958 | struct lru_gen_mm_list *mm_list = get_mm_list(memcg); | |
2959 | struct lru_gen_mm_state *mm_state = &lruvec->mm_state; | |
2960 | ||
2961 | spin_lock(&mm_list->lock); | |
2962 | ||
2963 | VM_WARN_ON_ONCE(mm_state->seq + 1 < max_seq); | |
2964 | ||
7f63cf2d KS |
2965 | if (max_seq > mm_state->seq) { |
2966 | mm_state->head = NULL; | |
2967 | mm_state->tail = NULL; | |
bd74fdae YZ |
2968 | WRITE_ONCE(mm_state->seq, mm_state->seq + 1); |
2969 | reset_mm_stats(lruvec, NULL, true); | |
2970 | success = true; | |
2971 | } | |
2972 | ||
2973 | spin_unlock(&mm_list->lock); | |
2974 | ||
2975 | return success; | |
2976 | } | |
2977 | ||
ac35a490 | 2978 | /****************************************************************************** |
32d32ef1 | 2979 | * PID controller |
ac35a490 YZ |
2980 | ******************************************************************************/ |
2981 | ||
2982 | /* | |
2983 | * A feedback loop based on Proportional-Integral-Derivative (PID) controller. | |
2984 | * | |
2985 | * The P term is refaulted/(evicted+protected) from a tier in the generation | |
2986 | * currently being evicted; the I term is the exponential moving average of the | |
2987 | * P term over the generations previously evicted, using the smoothing factor | |
2988 | * 1/2; the D term isn't supported. | |
2989 | * | |
2990 | * The setpoint (SP) is always the first tier of one type; the process variable | |
2991 | * (PV) is either any tier of the other type or any other tier of the same | |
2992 | * type. | |
2993 | * | |
2994 | * The error is the difference between the SP and the PV; the correction is to | |
2995 | * turn off protection when SP>PV or turn on protection when SP<PV. | |
2996 | * | |
2997 | * For future optimizations: | |
2998 | * 1. The D term may discount the other two terms over time so that long-lived | |
2999 | * generations can resist stale information. | |
3000 | */ | |
3001 | struct ctrl_pos { | |
3002 | unsigned long refaulted; | |
3003 | unsigned long total; | |
3004 | int gain; | |
3005 | }; | |
3006 | ||
3007 | static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain, | |
3008 | struct ctrl_pos *pos) | |
3009 | { | |
391655fe | 3010 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ac35a490 YZ |
3011 | int hist = lru_hist_from_seq(lrugen->min_seq[type]); |
3012 | ||
3013 | pos->refaulted = lrugen->avg_refaulted[type][tier] + | |
3014 | atomic_long_read(&lrugen->refaulted[hist][type][tier]); | |
3015 | pos->total = lrugen->avg_total[type][tier] + | |
3016 | atomic_long_read(&lrugen->evicted[hist][type][tier]); | |
3017 | if (tier) | |
3018 | pos->total += lrugen->protected[hist][type][tier - 1]; | |
3019 | pos->gain = gain; | |
3020 | } | |
3021 | ||
3022 | static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover) | |
3023 | { | |
3024 | int hist, tier; | |
391655fe | 3025 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ac35a490 YZ |
3026 | bool clear = carryover ? NR_HIST_GENS == 1 : NR_HIST_GENS > 1; |
3027 | unsigned long seq = carryover ? lrugen->min_seq[type] : lrugen->max_seq + 1; | |
3028 | ||
3029 | lockdep_assert_held(&lruvec->lru_lock); | |
3030 | ||
3031 | if (!carryover && !clear) | |
3032 | return; | |
3033 | ||
3034 | hist = lru_hist_from_seq(seq); | |
3035 | ||
3036 | for (tier = 0; tier < MAX_NR_TIERS; tier++) { | |
3037 | if (carryover) { | |
3038 | unsigned long sum; | |
3039 | ||
3040 | sum = lrugen->avg_refaulted[type][tier] + | |
3041 | atomic_long_read(&lrugen->refaulted[hist][type][tier]); | |
3042 | WRITE_ONCE(lrugen->avg_refaulted[type][tier], sum / 2); | |
3043 | ||
3044 | sum = lrugen->avg_total[type][tier] + | |
3045 | atomic_long_read(&lrugen->evicted[hist][type][tier]); | |
3046 | if (tier) | |
3047 | sum += lrugen->protected[hist][type][tier - 1]; | |
3048 | WRITE_ONCE(lrugen->avg_total[type][tier], sum / 2); | |
3049 | } | |
3050 | ||
3051 | if (clear) { | |
3052 | atomic_long_set(&lrugen->refaulted[hist][type][tier], 0); | |
3053 | atomic_long_set(&lrugen->evicted[hist][type][tier], 0); | |
3054 | if (tier) | |
3055 | WRITE_ONCE(lrugen->protected[hist][type][tier - 1], 0); | |
3056 | } | |
3057 | } | |
3058 | } | |
3059 | ||
3060 | static bool positive_ctrl_err(struct ctrl_pos *sp, struct ctrl_pos *pv) | |
3061 | { | |
3062 | /* | |
3063 | * Return true if the PV has a limited number of refaults or a lower | |
3064 | * refaulted/total than the SP. | |
3065 | */ | |
3066 | return pv->refaulted < MIN_LRU_BATCH || | |
3067 | pv->refaulted * (sp->total + MIN_LRU_BATCH) * sp->gain <= | |
3068 | (sp->refaulted + 1) * pv->total * pv->gain; | |
3069 | } | |
3070 | ||
3071 | /****************************************************************************** | |
3072 | * the aging | |
3073 | ******************************************************************************/ | |
3074 | ||
018ee47f YZ |
3075 | /* promote pages accessed through page tables */ |
3076 | static int folio_update_gen(struct folio *folio, int gen) | |
3077 | { | |
3078 | unsigned long new_flags, old_flags = READ_ONCE(folio->flags); | |
3079 | ||
3080 | VM_WARN_ON_ONCE(gen >= MAX_NR_GENS); | |
3081 | VM_WARN_ON_ONCE(!rcu_read_lock_held()); | |
3082 | ||
3083 | do { | |
3084 | /* lru_gen_del_folio() has isolated this page? */ | |
3085 | if (!(old_flags & LRU_GEN_MASK)) { | |
49fd9b6d | 3086 | /* for shrink_folio_list() */ |
018ee47f YZ |
3087 | new_flags = old_flags | BIT(PG_referenced); |
3088 | continue; | |
3089 | } | |
3090 | ||
3091 | new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS); | |
3092 | new_flags |= (gen + 1UL) << LRU_GEN_PGOFF; | |
3093 | } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags)); | |
3094 | ||
3095 | return ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1; | |
3096 | } | |
3097 | ||
ac35a490 YZ |
3098 | /* protect pages accessed multiple times through file descriptors */ |
3099 | static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio, bool reclaiming) | |
3100 | { | |
3101 | int type = folio_is_file_lru(folio); | |
391655fe | 3102 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ac35a490 YZ |
3103 | int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]); |
3104 | unsigned long new_flags, old_flags = READ_ONCE(folio->flags); | |
3105 | ||
3106 | VM_WARN_ON_ONCE_FOLIO(!(old_flags & LRU_GEN_MASK), folio); | |
3107 | ||
3108 | do { | |
018ee47f YZ |
3109 | new_gen = ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1; |
3110 | /* folio_update_gen() has promoted this page? */ | |
3111 | if (new_gen >= 0 && new_gen != old_gen) | |
3112 | return new_gen; | |
3113 | ||
ac35a490 YZ |
3114 | new_gen = (old_gen + 1) % MAX_NR_GENS; |
3115 | ||
3116 | new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS); | |
3117 | new_flags |= (new_gen + 1UL) << LRU_GEN_PGOFF; | |
3118 | /* for folio_end_writeback() */ | |
3119 | if (reclaiming) | |
3120 | new_flags |= BIT(PG_reclaim); | |
3121 | } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags)); | |
3122 | ||
3123 | lru_gen_update_size(lruvec, folio, old_gen, new_gen); | |
3124 | ||
3125 | return new_gen; | |
3126 | } | |
3127 | ||
bd74fdae YZ |
3128 | static void update_batch_size(struct lru_gen_mm_walk *walk, struct folio *folio, |
3129 | int old_gen, int new_gen) | |
3130 | { | |
3131 | int type = folio_is_file_lru(folio); | |
3132 | int zone = folio_zonenum(folio); | |
3133 | int delta = folio_nr_pages(folio); | |
3134 | ||
3135 | VM_WARN_ON_ONCE(old_gen >= MAX_NR_GENS); | |
3136 | VM_WARN_ON_ONCE(new_gen >= MAX_NR_GENS); | |
3137 | ||
3138 | walk->batched++; | |
3139 | ||
3140 | walk->nr_pages[old_gen][type][zone] -= delta; | |
3141 | walk->nr_pages[new_gen][type][zone] += delta; | |
3142 | } | |
3143 | ||
3144 | static void reset_batch_size(struct lruvec *lruvec, struct lru_gen_mm_walk *walk) | |
3145 | { | |
3146 | int gen, type, zone; | |
391655fe | 3147 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
bd74fdae YZ |
3148 | |
3149 | walk->batched = 0; | |
3150 | ||
3151 | for_each_gen_type_zone(gen, type, zone) { | |
3152 | enum lru_list lru = type * LRU_INACTIVE_FILE; | |
3153 | int delta = walk->nr_pages[gen][type][zone]; | |
3154 | ||
3155 | if (!delta) | |
3156 | continue; | |
3157 | ||
3158 | walk->nr_pages[gen][type][zone] = 0; | |
3159 | WRITE_ONCE(lrugen->nr_pages[gen][type][zone], | |
3160 | lrugen->nr_pages[gen][type][zone] + delta); | |
3161 | ||
3162 | if (lru_gen_is_active(lruvec, gen)) | |
3163 | lru += LRU_ACTIVE; | |
3164 | __update_lru_size(lruvec, lru, zone, delta); | |
3165 | } | |
3166 | } | |
3167 | ||
3168 | static int should_skip_vma(unsigned long start, unsigned long end, struct mm_walk *args) | |
3169 | { | |
3170 | struct address_space *mapping; | |
3171 | struct vm_area_struct *vma = args->vma; | |
3172 | struct lru_gen_mm_walk *walk = args->private; | |
3173 | ||
3174 | if (!vma_is_accessible(vma)) | |
3175 | return true; | |
3176 | ||
3177 | if (is_vm_hugetlb_page(vma)) | |
3178 | return true; | |
3179 | ||
8788f678 YZ |
3180 | if (!vma_has_recency(vma)) |
3181 | return true; | |
3182 | ||
3183 | if (vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) | |
bd74fdae YZ |
3184 | return true; |
3185 | ||
3186 | if (vma == get_gate_vma(vma->vm_mm)) | |
3187 | return true; | |
3188 | ||
3189 | if (vma_is_anonymous(vma)) | |
3190 | return !walk->can_swap; | |
3191 | ||
3192 | if (WARN_ON_ONCE(!vma->vm_file || !vma->vm_file->f_mapping)) | |
3193 | return true; | |
3194 | ||
3195 | mapping = vma->vm_file->f_mapping; | |
3196 | if (mapping_unevictable(mapping)) | |
3197 | return true; | |
3198 | ||
3199 | if (shmem_mapping(mapping)) | |
3200 | return !walk->can_swap; | |
3201 | ||
3202 | /* to exclude special mappings like dax, etc. */ | |
3203 | return !mapping->a_ops->read_folio; | |
3204 | } | |
3205 | ||
3206 | /* | |
3207 | * Some userspace memory allocators map many single-page VMAs. Instead of | |
3208 | * returning back to the PGD table for each of such VMAs, finish an entire PMD | |
3209 | * table to reduce zigzags and improve cache performance. | |
3210 | */ | |
3211 | static bool get_next_vma(unsigned long mask, unsigned long size, struct mm_walk *args, | |
3212 | unsigned long *vm_start, unsigned long *vm_end) | |
3213 | { | |
3214 | unsigned long start = round_up(*vm_end, size); | |
3215 | unsigned long end = (start | ~mask) + 1; | |
78ba531f | 3216 | VMA_ITERATOR(vmi, args->mm, start); |
bd74fdae YZ |
3217 | |
3218 | VM_WARN_ON_ONCE(mask & size); | |
3219 | VM_WARN_ON_ONCE((start & mask) != (*vm_start & mask)); | |
3220 | ||
78ba531f | 3221 | for_each_vma(vmi, args->vma) { |
bd74fdae YZ |
3222 | if (end && end <= args->vma->vm_start) |
3223 | return false; | |
3224 | ||
78ba531f | 3225 | if (should_skip_vma(args->vma->vm_start, args->vma->vm_end, args)) |
bd74fdae | 3226 | continue; |
bd74fdae YZ |
3227 | |
3228 | *vm_start = max(start, args->vma->vm_start); | |
3229 | *vm_end = min(end - 1, args->vma->vm_end - 1) + 1; | |
3230 | ||
3231 | return true; | |
3232 | } | |
3233 | ||
3234 | return false; | |
3235 | } | |
3236 | ||
018ee47f YZ |
3237 | static unsigned long get_pte_pfn(pte_t pte, struct vm_area_struct *vma, unsigned long addr) |
3238 | { | |
3239 | unsigned long pfn = pte_pfn(pte); | |
3240 | ||
3241 | VM_WARN_ON_ONCE(addr < vma->vm_start || addr >= vma->vm_end); | |
3242 | ||
3243 | if (!pte_present(pte) || is_zero_pfn(pfn)) | |
3244 | return -1; | |
3245 | ||
3246 | if (WARN_ON_ONCE(pte_devmap(pte) || pte_special(pte))) | |
3247 | return -1; | |
3248 | ||
3249 | if (WARN_ON_ONCE(!pfn_valid(pfn))) | |
3250 | return -1; | |
3251 | ||
3252 | return pfn; | |
3253 | } | |
3254 | ||
bd74fdae YZ |
3255 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG) |
3256 | static unsigned long get_pmd_pfn(pmd_t pmd, struct vm_area_struct *vma, unsigned long addr) | |
3257 | { | |
3258 | unsigned long pfn = pmd_pfn(pmd); | |
3259 | ||
3260 | VM_WARN_ON_ONCE(addr < vma->vm_start || addr >= vma->vm_end); | |
3261 | ||
3262 | if (!pmd_present(pmd) || is_huge_zero_pmd(pmd)) | |
3263 | return -1; | |
3264 | ||
3265 | if (WARN_ON_ONCE(pmd_devmap(pmd))) | |
3266 | return -1; | |
3267 | ||
3268 | if (WARN_ON_ONCE(!pfn_valid(pfn))) | |
3269 | return -1; | |
3270 | ||
3271 | return pfn; | |
3272 | } | |
3273 | #endif | |
3274 | ||
018ee47f | 3275 | static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *memcg, |
bd74fdae | 3276 | struct pglist_data *pgdat, bool can_swap) |
018ee47f YZ |
3277 | { |
3278 | struct folio *folio; | |
3279 | ||
3280 | /* try to avoid unnecessary memory loads */ | |
3281 | if (pfn < pgdat->node_start_pfn || pfn >= pgdat_end_pfn(pgdat)) | |
3282 | return NULL; | |
3283 | ||
3284 | folio = pfn_folio(pfn); | |
3285 | if (folio_nid(folio) != pgdat->node_id) | |
3286 | return NULL; | |
3287 | ||
3288 | if (folio_memcg_rcu(folio) != memcg) | |
3289 | return NULL; | |
3290 | ||
bd74fdae YZ |
3291 | /* file VMAs can contain anon pages from COW */ |
3292 | if (!folio_is_file_lru(folio) && !can_swap) | |
3293 | return NULL; | |
3294 | ||
018ee47f YZ |
3295 | return folio; |
3296 | } | |
3297 | ||
bd74fdae YZ |
3298 | static bool suitable_to_scan(int total, int young) |
3299 | { | |
3300 | int n = clamp_t(int, cache_line_size() / sizeof(pte_t), 2, 8); | |
3301 | ||
3302 | /* suitable if the average number of young PTEs per cacheline is >=1 */ | |
3303 | return young * n >= total; | |
3304 | } | |
3305 | ||
3306 | static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end, | |
3307 | struct mm_walk *args) | |
3308 | { | |
3309 | int i; | |
3310 | pte_t *pte; | |
3311 | spinlock_t *ptl; | |
3312 | unsigned long addr; | |
3313 | int total = 0; | |
3314 | int young = 0; | |
3315 | struct lru_gen_mm_walk *walk = args->private; | |
3316 | struct mem_cgroup *memcg = lruvec_memcg(walk->lruvec); | |
3317 | struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec); | |
3318 | int old_gen, new_gen = lru_gen_from_seq(walk->max_seq); | |
3319 | ||
52fc0483 HD |
3320 | pte = pte_offset_map_nolock(args->mm, pmd, start & PMD_MASK, &ptl); |
3321 | if (!pte) | |
3322 | return false; | |
3323 | if (!spin_trylock(ptl)) { | |
3324 | pte_unmap(pte); | |
bd74fdae | 3325 | return false; |
52fc0483 | 3326 | } |
bd74fdae YZ |
3327 | |
3328 | arch_enter_lazy_mmu_mode(); | |
bd74fdae YZ |
3329 | restart: |
3330 | for (i = pte_index(start), addr = start; addr != end; i++, addr += PAGE_SIZE) { | |
3331 | unsigned long pfn; | |
3332 | struct folio *folio; | |
c33c7948 | 3333 | pte_t ptent = ptep_get(pte + i); |
bd74fdae YZ |
3334 | |
3335 | total++; | |
3336 | walk->mm_stats[MM_LEAF_TOTAL]++; | |
3337 | ||
c33c7948 | 3338 | pfn = get_pte_pfn(ptent, args->vma, addr); |
bd74fdae YZ |
3339 | if (pfn == -1) |
3340 | continue; | |
3341 | ||
c33c7948 | 3342 | if (!pte_young(ptent)) { |
bd74fdae YZ |
3343 | walk->mm_stats[MM_LEAF_OLD]++; |
3344 | continue; | |
3345 | } | |
3346 | ||
3347 | folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap); | |
3348 | if (!folio) | |
3349 | continue; | |
3350 | ||
3351 | if (!ptep_test_and_clear_young(args->vma, addr, pte + i)) | |
3352 | VM_WARN_ON_ONCE(true); | |
3353 | ||
3354 | young++; | |
3355 | walk->mm_stats[MM_LEAF_YOUNG]++; | |
3356 | ||
c33c7948 | 3357 | if (pte_dirty(ptent) && !folio_test_dirty(folio) && |
bd74fdae YZ |
3358 | !(folio_test_anon(folio) && folio_test_swapbacked(folio) && |
3359 | !folio_test_swapcache(folio))) | |
3360 | folio_mark_dirty(folio); | |
3361 | ||
3362 | old_gen = folio_update_gen(folio, new_gen); | |
3363 | if (old_gen >= 0 && old_gen != new_gen) | |
3364 | update_batch_size(walk, folio, old_gen, new_gen); | |
3365 | } | |
3366 | ||
3367 | if (i < PTRS_PER_PTE && get_next_vma(PMD_MASK, PAGE_SIZE, args, &start, &end)) | |
3368 | goto restart; | |
3369 | ||
bd74fdae | 3370 | arch_leave_lazy_mmu_mode(); |
52fc0483 | 3371 | pte_unmap_unlock(pte, ptl); |
bd74fdae YZ |
3372 | |
3373 | return suitable_to_scan(total, young); | |
3374 | } | |
3375 | ||
3376 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG) | |
b5ff4133 A |
3377 | static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area_struct *vma, |
3378 | struct mm_walk *args, unsigned long *bitmap, unsigned long *first) | |
bd74fdae YZ |
3379 | { |
3380 | int i; | |
3381 | pmd_t *pmd; | |
3382 | spinlock_t *ptl; | |
3383 | struct lru_gen_mm_walk *walk = args->private; | |
3384 | struct mem_cgroup *memcg = lruvec_memcg(walk->lruvec); | |
3385 | struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec); | |
3386 | int old_gen, new_gen = lru_gen_from_seq(walk->max_seq); | |
3387 | ||
3388 | VM_WARN_ON_ONCE(pud_leaf(*pud)); | |
3389 | ||
3390 | /* try to batch at most 1+MIN_LRU_BATCH+1 entries */ | |
b5ff4133 A |
3391 | if (*first == -1) { |
3392 | *first = addr; | |
3393 | bitmap_zero(bitmap, MIN_LRU_BATCH); | |
bd74fdae YZ |
3394 | return; |
3395 | } | |
3396 | ||
b5ff4133 | 3397 | i = addr == -1 ? 0 : pmd_index(addr) - pmd_index(*first); |
bd74fdae YZ |
3398 | if (i && i <= MIN_LRU_BATCH) { |
3399 | __set_bit(i - 1, bitmap); | |
3400 | return; | |
3401 | } | |
3402 | ||
b5ff4133 | 3403 | pmd = pmd_offset(pud, *first); |
bd74fdae YZ |
3404 | |
3405 | ptl = pmd_lockptr(args->mm, pmd); | |
3406 | if (!spin_trylock(ptl)) | |
3407 | goto done; | |
3408 | ||
3409 | arch_enter_lazy_mmu_mode(); | |
3410 | ||
3411 | do { | |
3412 | unsigned long pfn; | |
3413 | struct folio *folio; | |
b5ff4133 A |
3414 | |
3415 | /* don't round down the first address */ | |
3416 | addr = i ? (*first & PMD_MASK) + i * PMD_SIZE : *first; | |
bd74fdae YZ |
3417 | |
3418 | pfn = get_pmd_pfn(pmd[i], vma, addr); | |
3419 | if (pfn == -1) | |
3420 | goto next; | |
3421 | ||
3422 | if (!pmd_trans_huge(pmd[i])) { | |
bd02df41 | 3423 | if (should_clear_pmd_young()) |
bd74fdae YZ |
3424 | pmdp_test_and_clear_young(vma, addr, pmd + i); |
3425 | goto next; | |
3426 | } | |
3427 | ||
3428 | folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap); | |
3429 | if (!folio) | |
3430 | goto next; | |
3431 | ||
3432 | if (!pmdp_test_and_clear_young(vma, addr, pmd + i)) | |
3433 | goto next; | |
3434 | ||
3435 | walk->mm_stats[MM_LEAF_YOUNG]++; | |
3436 | ||
3437 | if (pmd_dirty(pmd[i]) && !folio_test_dirty(folio) && | |
3438 | !(folio_test_anon(folio) && folio_test_swapbacked(folio) && | |
3439 | !folio_test_swapcache(folio))) | |
3440 | folio_mark_dirty(folio); | |
3441 | ||
3442 | old_gen = folio_update_gen(folio, new_gen); | |
3443 | if (old_gen >= 0 && old_gen != new_gen) | |
3444 | update_batch_size(walk, folio, old_gen, new_gen); | |
3445 | next: | |
3446 | i = i > MIN_LRU_BATCH ? 0 : find_next_bit(bitmap, MIN_LRU_BATCH, i) + 1; | |
3447 | } while (i <= MIN_LRU_BATCH); | |
3448 | ||
3449 | arch_leave_lazy_mmu_mode(); | |
3450 | spin_unlock(ptl); | |
3451 | done: | |
b5ff4133 | 3452 | *first = -1; |
bd74fdae YZ |
3453 | } |
3454 | #else | |
b5ff4133 A |
3455 | static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area_struct *vma, |
3456 | struct mm_walk *args, unsigned long *bitmap, unsigned long *first) | |
bd74fdae YZ |
3457 | { |
3458 | } | |
3459 | #endif | |
3460 | ||
3461 | static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end, | |
3462 | struct mm_walk *args) | |
3463 | { | |
3464 | int i; | |
3465 | pmd_t *pmd; | |
3466 | unsigned long next; | |
3467 | unsigned long addr; | |
3468 | struct vm_area_struct *vma; | |
0285762c | 3469 | DECLARE_BITMAP(bitmap, MIN_LRU_BATCH); |
b5ff4133 | 3470 | unsigned long first = -1; |
bd74fdae | 3471 | struct lru_gen_mm_walk *walk = args->private; |
bd74fdae YZ |
3472 | |
3473 | VM_WARN_ON_ONCE(pud_leaf(*pud)); | |
3474 | ||
3475 | /* | |
3476 | * Finish an entire PMD in two passes: the first only reaches to PTE | |
3477 | * tables to avoid taking the PMD lock; the second, if necessary, takes | |
3478 | * the PMD lock to clear the accessed bit in PMD entries. | |
3479 | */ | |
3480 | pmd = pmd_offset(pud, start & PUD_MASK); | |
3481 | restart: | |
3482 | /* walk_pte_range() may call get_next_vma() */ | |
3483 | vma = args->vma; | |
3484 | for (i = pmd_index(start), addr = start; addr != end; i++, addr = next) { | |
dab6e717 | 3485 | pmd_t val = pmdp_get_lockless(pmd + i); |
bd74fdae YZ |
3486 | |
3487 | next = pmd_addr_end(addr, end); | |
3488 | ||
3489 | if (!pmd_present(val) || is_huge_zero_pmd(val)) { | |
3490 | walk->mm_stats[MM_LEAF_TOTAL]++; | |
3491 | continue; | |
3492 | } | |
3493 | ||
3494 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
3495 | if (pmd_trans_huge(val)) { | |
3496 | unsigned long pfn = pmd_pfn(val); | |
3497 | struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec); | |
3498 | ||
3499 | walk->mm_stats[MM_LEAF_TOTAL]++; | |
3500 | ||
3501 | if (!pmd_young(val)) { | |
3502 | walk->mm_stats[MM_LEAF_OLD]++; | |
3503 | continue; | |
3504 | } | |
3505 | ||
3506 | /* try to avoid unnecessary memory loads */ | |
3507 | if (pfn < pgdat->node_start_pfn || pfn >= pgdat_end_pfn(pgdat)) | |
3508 | continue; | |
3509 | ||
b5ff4133 | 3510 | walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first); |
bd74fdae YZ |
3511 | continue; |
3512 | } | |
3513 | #endif | |
3514 | walk->mm_stats[MM_NONLEAF_TOTAL]++; | |
3515 | ||
bd02df41 | 3516 | if (should_clear_pmd_young()) { |
354ed597 YZ |
3517 | if (!pmd_young(val)) |
3518 | continue; | |
bd74fdae | 3519 | |
b5ff4133 | 3520 | walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first); |
354ed597 | 3521 | } |
4aaf269c | 3522 | |
bd74fdae YZ |
3523 | if (!walk->force_scan && !test_bloom_filter(walk->lruvec, walk->max_seq, pmd + i)) |
3524 | continue; | |
3525 | ||
3526 | walk->mm_stats[MM_NONLEAF_FOUND]++; | |
3527 | ||
3528 | if (!walk_pte_range(&val, addr, next, args)) | |
3529 | continue; | |
3530 | ||
3531 | walk->mm_stats[MM_NONLEAF_ADDED]++; | |
3532 | ||
3533 | /* carry over to the next generation */ | |
3534 | update_bloom_filter(walk->lruvec, walk->max_seq + 1, pmd + i); | |
3535 | } | |
3536 | ||
b5ff4133 | 3537 | walk_pmd_range_locked(pud, -1, vma, args, bitmap, &first); |
bd74fdae YZ |
3538 | |
3539 | if (i < PTRS_PER_PMD && get_next_vma(PUD_MASK, PMD_SIZE, args, &start, &end)) | |
3540 | goto restart; | |
3541 | } | |
3542 | ||
3543 | static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end, | |
3544 | struct mm_walk *args) | |
3545 | { | |
3546 | int i; | |
3547 | pud_t *pud; | |
3548 | unsigned long addr; | |
3549 | unsigned long next; | |
3550 | struct lru_gen_mm_walk *walk = args->private; | |
3551 | ||
3552 | VM_WARN_ON_ONCE(p4d_leaf(*p4d)); | |
3553 | ||
3554 | pud = pud_offset(p4d, start & P4D_MASK); | |
3555 | restart: | |
3556 | for (i = pud_index(start), addr = start; addr != end; i++, addr = next) { | |
3557 | pud_t val = READ_ONCE(pud[i]); | |
3558 | ||
3559 | next = pud_addr_end(addr, end); | |
3560 | ||
3561 | if (!pud_present(val) || WARN_ON_ONCE(pud_leaf(val))) | |
3562 | continue; | |
3563 | ||
3564 | walk_pmd_range(&val, addr, next, args); | |
3565 | ||
bd74fdae YZ |
3566 | if (need_resched() || walk->batched >= MAX_LRU_BATCH) { |
3567 | end = (addr | ~PUD_MASK) + 1; | |
3568 | goto done; | |
3569 | } | |
3570 | } | |
3571 | ||
3572 | if (i < PTRS_PER_PUD && get_next_vma(P4D_MASK, PUD_SIZE, args, &start, &end)) | |
3573 | goto restart; | |
3574 | ||
3575 | end = round_up(end, P4D_SIZE); | |
3576 | done: | |
3577 | if (!end || !args->vma) | |
3578 | return 1; | |
3579 | ||
3580 | walk->next_addr = max(end, args->vma->vm_start); | |
3581 | ||
3582 | return -EAGAIN; | |
3583 | } | |
3584 | ||
3585 | static void walk_mm(struct lruvec *lruvec, struct mm_struct *mm, struct lru_gen_mm_walk *walk) | |
3586 | { | |
3587 | static const struct mm_walk_ops mm_walk_ops = { | |
3588 | .test_walk = should_skip_vma, | |
3589 | .p4d_entry = walk_pud_range, | |
49b06385 | 3590 | .walk_lock = PGWALK_RDLOCK, |
bd74fdae YZ |
3591 | }; |
3592 | ||
3593 | int err; | |
3594 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
3595 | ||
3596 | walk->next_addr = FIRST_USER_ADDRESS; | |
3597 | ||
3598 | do { | |
7f63cf2d KS |
3599 | DEFINE_MAX_SEQ(lruvec); |
3600 | ||
bd74fdae YZ |
3601 | err = -EBUSY; |
3602 | ||
7f63cf2d KS |
3603 | /* another thread might have called inc_max_seq() */ |
3604 | if (walk->max_seq != max_seq) | |
3605 | break; | |
3606 | ||
bd74fdae YZ |
3607 | /* folio_update_gen() requires stable folio_memcg() */ |
3608 | if (!mem_cgroup_trylock_pages(memcg)) | |
3609 | break; | |
3610 | ||
3611 | /* the caller might be holding the lock for write */ | |
3612 | if (mmap_read_trylock(mm)) { | |
3613 | err = walk_page_range(mm, walk->next_addr, ULONG_MAX, &mm_walk_ops, walk); | |
3614 | ||
3615 | mmap_read_unlock(mm); | |
3616 | } | |
3617 | ||
3618 | mem_cgroup_unlock_pages(); | |
3619 | ||
3620 | if (walk->batched) { | |
3621 | spin_lock_irq(&lruvec->lru_lock); | |
3622 | reset_batch_size(lruvec, walk); | |
3623 | spin_unlock_irq(&lruvec->lru_lock); | |
3624 | } | |
3625 | ||
3626 | cond_resched(); | |
3627 | } while (err == -EAGAIN); | |
3628 | } | |
3629 | ||
e9d4e1ee | 3630 | static struct lru_gen_mm_walk *set_mm_walk(struct pglist_data *pgdat, bool force_alloc) |
bd74fdae YZ |
3631 | { |
3632 | struct lru_gen_mm_walk *walk = current->reclaim_state->mm_walk; | |
3633 | ||
3634 | if (pgdat && current_is_kswapd()) { | |
3635 | VM_WARN_ON_ONCE(walk); | |
3636 | ||
3637 | walk = &pgdat->mm_walk; | |
e9d4e1ee | 3638 | } else if (!walk && force_alloc) { |
bd74fdae YZ |
3639 | VM_WARN_ON_ONCE(current_is_kswapd()); |
3640 | ||
3641 | walk = kzalloc(sizeof(*walk), __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN); | |
3642 | } | |
3643 | ||
3644 | current->reclaim_state->mm_walk = walk; | |
3645 | ||
3646 | return walk; | |
3647 | } | |
3648 | ||
3649 | static void clear_mm_walk(void) | |
3650 | { | |
3651 | struct lru_gen_mm_walk *walk = current->reclaim_state->mm_walk; | |
3652 | ||
3653 | VM_WARN_ON_ONCE(walk && memchr_inv(walk->nr_pages, 0, sizeof(walk->nr_pages))); | |
3654 | VM_WARN_ON_ONCE(walk && memchr_inv(walk->mm_stats, 0, sizeof(walk->mm_stats))); | |
3655 | ||
3656 | current->reclaim_state->mm_walk = NULL; | |
3657 | ||
3658 | if (!current_is_kswapd()) | |
3659 | kfree(walk); | |
3660 | } | |
3661 | ||
d6c3af7d | 3662 | static bool inc_min_seq(struct lruvec *lruvec, int type, bool can_swap) |
ac35a490 | 3663 | { |
d6c3af7d YZ |
3664 | int zone; |
3665 | int remaining = MAX_LRU_BATCH; | |
391655fe | 3666 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
d6c3af7d YZ |
3667 | int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]); |
3668 | ||
3669 | if (type == LRU_GEN_ANON && !can_swap) | |
3670 | goto done; | |
3671 | ||
3672 | /* prevent cold/hot inversion if force_scan is true */ | |
3673 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
6df1b221 | 3674 | struct list_head *head = &lrugen->folios[old_gen][type][zone]; |
d6c3af7d YZ |
3675 | |
3676 | while (!list_empty(head)) { | |
3677 | struct folio *folio = lru_to_folio(head); | |
3678 | ||
3679 | VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); | |
3680 | VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio); | |
3681 | VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio); | |
3682 | VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio); | |
ac35a490 | 3683 | |
d6c3af7d | 3684 | new_gen = folio_inc_gen(lruvec, folio, false); |
6df1b221 | 3685 | list_move_tail(&folio->lru, &lrugen->folios[new_gen][type][zone]); |
d6c3af7d YZ |
3686 | |
3687 | if (!--remaining) | |
3688 | return false; | |
3689 | } | |
3690 | } | |
3691 | done: | |
ac35a490 YZ |
3692 | reset_ctrl_pos(lruvec, type, true); |
3693 | WRITE_ONCE(lrugen->min_seq[type], lrugen->min_seq[type] + 1); | |
d6c3af7d YZ |
3694 | |
3695 | return true; | |
ac35a490 YZ |
3696 | } |
3697 | ||
3698 | static bool try_to_inc_min_seq(struct lruvec *lruvec, bool can_swap) | |
3699 | { | |
3700 | int gen, type, zone; | |
3701 | bool success = false; | |
391655fe | 3702 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ac35a490 YZ |
3703 | DEFINE_MIN_SEQ(lruvec); |
3704 | ||
3705 | VM_WARN_ON_ONCE(!seq_is_valid(lruvec)); | |
3706 | ||
3707 | /* find the oldest populated generation */ | |
3708 | for (type = !can_swap; type < ANON_AND_FILE; type++) { | |
3709 | while (min_seq[type] + MIN_NR_GENS <= lrugen->max_seq) { | |
3710 | gen = lru_gen_from_seq(min_seq[type]); | |
3711 | ||
3712 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
6df1b221 | 3713 | if (!list_empty(&lrugen->folios[gen][type][zone])) |
ac35a490 YZ |
3714 | goto next; |
3715 | } | |
3716 | ||
3717 | min_seq[type]++; | |
3718 | } | |
3719 | next: | |
3720 | ; | |
3721 | } | |
3722 | ||
391655fe | 3723 | /* see the comment on lru_gen_folio */ |
ac35a490 YZ |
3724 | if (can_swap) { |
3725 | min_seq[LRU_GEN_ANON] = min(min_seq[LRU_GEN_ANON], min_seq[LRU_GEN_FILE]); | |
3726 | min_seq[LRU_GEN_FILE] = max(min_seq[LRU_GEN_ANON], lrugen->min_seq[LRU_GEN_FILE]); | |
3727 | } | |
3728 | ||
3729 | for (type = !can_swap; type < ANON_AND_FILE; type++) { | |
3730 | if (min_seq[type] == lrugen->min_seq[type]) | |
3731 | continue; | |
3732 | ||
3733 | reset_ctrl_pos(lruvec, type, true); | |
3734 | WRITE_ONCE(lrugen->min_seq[type], min_seq[type]); | |
3735 | success = true; | |
3736 | } | |
3737 | ||
3738 | return success; | |
3739 | } | |
3740 | ||
d6c3af7d | 3741 | static void inc_max_seq(struct lruvec *lruvec, bool can_swap, bool force_scan) |
ac35a490 YZ |
3742 | { |
3743 | int prev, next; | |
3744 | int type, zone; | |
391655fe | 3745 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
bb5e7f23 | 3746 | restart: |
ac35a490 YZ |
3747 | spin_lock_irq(&lruvec->lru_lock); |
3748 | ||
3749 | VM_WARN_ON_ONCE(!seq_is_valid(lruvec)); | |
3750 | ||
ac35a490 YZ |
3751 | for (type = ANON_AND_FILE - 1; type >= 0; type--) { |
3752 | if (get_nr_gens(lruvec, type) != MAX_NR_GENS) | |
3753 | continue; | |
3754 | ||
d6c3af7d | 3755 | VM_WARN_ON_ONCE(!force_scan && (type == LRU_GEN_FILE || can_swap)); |
ac35a490 | 3756 | |
bb5e7f23 KS |
3757 | if (inc_min_seq(lruvec, type, can_swap)) |
3758 | continue; | |
3759 | ||
3760 | spin_unlock_irq(&lruvec->lru_lock); | |
3761 | cond_resched(); | |
3762 | goto restart; | |
ac35a490 YZ |
3763 | } |
3764 | ||
3765 | /* | |
3766 | * Update the active/inactive LRU sizes for compatibility. Both sides of | |
3767 | * the current max_seq need to be covered, since max_seq+1 can overlap | |
3768 | * with min_seq[LRU_GEN_ANON] if swapping is constrained. And if they do | |
3769 | * overlap, cold/hot inversion happens. | |
3770 | */ | |
3771 | prev = lru_gen_from_seq(lrugen->max_seq - 1); | |
3772 | next = lru_gen_from_seq(lrugen->max_seq + 1); | |
3773 | ||
3774 | for (type = 0; type < ANON_AND_FILE; type++) { | |
3775 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
3776 | enum lru_list lru = type * LRU_INACTIVE_FILE; | |
3777 | long delta = lrugen->nr_pages[prev][type][zone] - | |
3778 | lrugen->nr_pages[next][type][zone]; | |
3779 | ||
3780 | if (!delta) | |
3781 | continue; | |
3782 | ||
3783 | __update_lru_size(lruvec, lru, zone, delta); | |
3784 | __update_lru_size(lruvec, lru + LRU_ACTIVE, zone, -delta); | |
3785 | } | |
3786 | } | |
3787 | ||
3788 | for (type = 0; type < ANON_AND_FILE; type++) | |
3789 | reset_ctrl_pos(lruvec, type, false); | |
3790 | ||
1332a809 | 3791 | WRITE_ONCE(lrugen->timestamps[next], jiffies); |
ac35a490 YZ |
3792 | /* make sure preceding modifications appear */ |
3793 | smp_store_release(&lrugen->max_seq, lrugen->max_seq + 1); | |
bd74fdae | 3794 | |
ac35a490 YZ |
3795 | spin_unlock_irq(&lruvec->lru_lock); |
3796 | } | |
3797 | ||
bd74fdae | 3798 | static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long max_seq, |
d6c3af7d | 3799 | struct scan_control *sc, bool can_swap, bool force_scan) |
bd74fdae YZ |
3800 | { |
3801 | bool success; | |
3802 | struct lru_gen_mm_walk *walk; | |
3803 | struct mm_struct *mm = NULL; | |
391655fe | 3804 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
bd74fdae YZ |
3805 | |
3806 | VM_WARN_ON_ONCE(max_seq > READ_ONCE(lrugen->max_seq)); | |
3807 | ||
3808 | /* see the comment in iterate_mm_list() */ | |
3809 | if (max_seq <= READ_ONCE(lruvec->mm_state.seq)) { | |
3810 | success = false; | |
3811 | goto done; | |
3812 | } | |
3813 | ||
3814 | /* | |
3815 | * If the hardware doesn't automatically set the accessed bit, fallback | |
3816 | * to lru_gen_look_around(), which only clears the accessed bit in a | |
3817 | * handful of PTEs. Spreading the work out over a period of time usually | |
3818 | * is less efficient, but it avoids bursty page faults. | |
3819 | */ | |
bd02df41 | 3820 | if (!should_walk_mmu()) { |
bd74fdae YZ |
3821 | success = iterate_mm_list_nowalk(lruvec, max_seq); |
3822 | goto done; | |
3823 | } | |
3824 | ||
e9d4e1ee | 3825 | walk = set_mm_walk(NULL, true); |
bd74fdae YZ |
3826 | if (!walk) { |
3827 | success = iterate_mm_list_nowalk(lruvec, max_seq); | |
3828 | goto done; | |
3829 | } | |
3830 | ||
3831 | walk->lruvec = lruvec; | |
3832 | walk->max_seq = max_seq; | |
3833 | walk->can_swap = can_swap; | |
d6c3af7d | 3834 | walk->force_scan = force_scan; |
bd74fdae YZ |
3835 | |
3836 | do { | |
3837 | success = iterate_mm_list(lruvec, walk, &mm); | |
3838 | if (mm) | |
3839 | walk_mm(lruvec, mm, walk); | |
bd74fdae YZ |
3840 | } while (mm); |
3841 | done: | |
7f63cf2d KS |
3842 | if (success) |
3843 | inc_max_seq(lruvec, can_swap, force_scan); | |
bd74fdae | 3844 | |
7f63cf2d | 3845 | return success; |
bd74fdae YZ |
3846 | } |
3847 | ||
7b8144e6 A |
3848 | /****************************************************************************** |
3849 | * working set protection | |
3850 | ******************************************************************************/ | |
3851 | ||
7348cc91 | 3852 | static bool lruvec_is_sizable(struct lruvec *lruvec, struct scan_control *sc) |
ac35a490 | 3853 | { |
7348cc91 YZ |
3854 | int gen, type, zone; |
3855 | unsigned long total = 0; | |
3856 | bool can_swap = get_swappiness(lruvec, sc); | |
3857 | struct lru_gen_folio *lrugen = &lruvec->lrugen; | |
ac35a490 YZ |
3858 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); |
3859 | DEFINE_MAX_SEQ(lruvec); | |
3860 | DEFINE_MIN_SEQ(lruvec); | |
3861 | ||
7348cc91 YZ |
3862 | for (type = !can_swap; type < ANON_AND_FILE; type++) { |
3863 | unsigned long seq; | |
ac35a490 | 3864 | |
7348cc91 YZ |
3865 | for (seq = min_seq[type]; seq <= max_seq; seq++) { |
3866 | gen = lru_gen_from_seq(seq); | |
ac35a490 | 3867 | |
7348cc91 YZ |
3868 | for (zone = 0; zone < MAX_NR_ZONES; zone++) |
3869 | total += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); | |
3870 | } | |
3871 | } | |
ac35a490 | 3872 | |
7348cc91 YZ |
3873 | /* whether the size is big enough to be helpful */ |
3874 | return mem_cgroup_online(memcg) ? (total >> sc->priority) : total; | |
3875 | } | |
1332a809 | 3876 | |
7348cc91 YZ |
3877 | static bool lruvec_is_reclaimable(struct lruvec *lruvec, struct scan_control *sc, |
3878 | unsigned long min_ttl) | |
3879 | { | |
3880 | int gen; | |
3881 | unsigned long birth; | |
3882 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
3883 | DEFINE_MIN_SEQ(lruvec); | |
1332a809 | 3884 | |
7348cc91 YZ |
3885 | /* see the comment on lru_gen_folio */ |
3886 | gen = lru_gen_from_seq(min_seq[LRU_GEN_FILE]); | |
3887 | birth = READ_ONCE(lruvec->lrugen.timestamps[gen]); | |
1332a809 | 3888 | |
7348cc91 YZ |
3889 | if (time_is_after_jiffies(birth + min_ttl)) |
3890 | return false; | |
1332a809 | 3891 | |
7348cc91 YZ |
3892 | if (!lruvec_is_sizable(lruvec, sc)) |
3893 | return false; | |
3894 | ||
3895 | mem_cgroup_calculate_protection(NULL, memcg); | |
3896 | ||
3897 | return !mem_cgroup_below_min(NULL, memcg); | |
ac35a490 YZ |
3898 | } |
3899 | ||
1332a809 YZ |
3900 | /* to protect the working set of the last N jiffies */ |
3901 | static unsigned long lru_gen_min_ttl __read_mostly; | |
3902 | ||
ac35a490 YZ |
3903 | static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc) |
3904 | { | |
3905 | struct mem_cgroup *memcg; | |
1332a809 | 3906 | unsigned long min_ttl = READ_ONCE(lru_gen_min_ttl); |
ac35a490 YZ |
3907 | |
3908 | VM_WARN_ON_ONCE(!current_is_kswapd()); | |
3909 | ||
7348cc91 YZ |
3910 | /* check the order to exclude compaction-induced reclaim */ |
3911 | if (!min_ttl || sc->order || sc->priority == DEF_PRIORITY) | |
f76c8337 | 3912 | return; |
bd74fdae | 3913 | |
ac35a490 YZ |
3914 | memcg = mem_cgroup_iter(NULL, NULL, NULL); |
3915 | do { | |
3916 | struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); | |
3917 | ||
7348cc91 YZ |
3918 | if (lruvec_is_reclaimable(lruvec, sc, min_ttl)) { |
3919 | mem_cgroup_iter_break(NULL, memcg); | |
3920 | return; | |
3921 | } | |
ac35a490 YZ |
3922 | |
3923 | cond_resched(); | |
3924 | } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); | |
bd74fdae | 3925 | |
1332a809 YZ |
3926 | /* |
3927 | * The main goal is to OOM kill if every generation from all memcgs is | |
3928 | * younger than min_ttl. However, another possibility is all memcgs are | |
7348cc91 | 3929 | * either too small or below min. |
1332a809 YZ |
3930 | */ |
3931 | if (mutex_trylock(&oom_lock)) { | |
3932 | struct oom_control oc = { | |
3933 | .gfp_mask = sc->gfp_mask, | |
3934 | }; | |
3935 | ||
3936 | out_of_memory(&oc); | |
3937 | ||
3938 | mutex_unlock(&oom_lock); | |
3939 | } | |
ac35a490 YZ |
3940 | } |
3941 | ||
db19a43d A |
3942 | /****************************************************************************** |
3943 | * rmap/PT walk feedback | |
3944 | ******************************************************************************/ | |
3945 | ||
018ee47f | 3946 | /* |
49fd9b6d | 3947 | * This function exploits spatial locality when shrink_folio_list() walks the |
bd74fdae YZ |
3948 | * rmap. It scans the adjacent PTEs of a young PTE and promotes hot pages. If |
3949 | * the scan was done cacheline efficiently, it adds the PMD entry pointing to | |
3950 | * the PTE table to the Bloom filter. This forms a feedback loop between the | |
3951 | * eviction and the aging. | |
018ee47f YZ |
3952 | */ |
3953 | void lru_gen_look_around(struct page_vma_mapped_walk *pvmw) | |
3954 | { | |
3955 | int i; | |
018ee47f YZ |
3956 | unsigned long start; |
3957 | unsigned long end; | |
bd74fdae YZ |
3958 | struct lru_gen_mm_walk *walk; |
3959 | int young = 0; | |
abf08672 A |
3960 | pte_t *pte = pvmw->pte; |
3961 | unsigned long addr = pvmw->address; | |
018ee47f | 3962 | struct folio *folio = pfn_folio(pvmw->pfn); |
a3235ea2 | 3963 | bool can_swap = !folio_is_file_lru(folio); |
018ee47f YZ |
3964 | struct mem_cgroup *memcg = folio_memcg(folio); |
3965 | struct pglist_data *pgdat = folio_pgdat(folio); | |
3966 | struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); | |
3967 | DEFINE_MAX_SEQ(lruvec); | |
3968 | int old_gen, new_gen = lru_gen_from_seq(max_seq); | |
3969 | ||
3970 | lockdep_assert_held(pvmw->ptl); | |
3971 | VM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio); | |
3972 | ||
3973 | if (spin_is_contended(pvmw->ptl)) | |
3974 | return; | |
3975 | ||
bd74fdae YZ |
3976 | /* avoid taking the LRU lock under the PTL when possible */ |
3977 | walk = current->reclaim_state ? current->reclaim_state->mm_walk : NULL; | |
3978 | ||
abf08672 A |
3979 | start = max(addr & PMD_MASK, pvmw->vma->vm_start); |
3980 | end = min(addr | ~PMD_MASK, pvmw->vma->vm_end - 1) + 1; | |
018ee47f YZ |
3981 | |
3982 | if (end - start > MIN_LRU_BATCH * PAGE_SIZE) { | |
abf08672 | 3983 | if (addr - start < MIN_LRU_BATCH * PAGE_SIZE / 2) |
018ee47f | 3984 | end = start + MIN_LRU_BATCH * PAGE_SIZE; |
abf08672 | 3985 | else if (end - addr < MIN_LRU_BATCH * PAGE_SIZE / 2) |
018ee47f YZ |
3986 | start = end - MIN_LRU_BATCH * PAGE_SIZE; |
3987 | else { | |
abf08672 A |
3988 | start = addr - MIN_LRU_BATCH * PAGE_SIZE / 2; |
3989 | end = addr + MIN_LRU_BATCH * PAGE_SIZE / 2; | |
018ee47f YZ |
3990 | } |
3991 | } | |
3992 | ||
abf08672 A |
3993 | /* folio_update_gen() requires stable folio_memcg() */ |
3994 | if (!mem_cgroup_trylock_pages(memcg)) | |
3995 | return; | |
018ee47f | 3996 | |
018ee47f YZ |
3997 | arch_enter_lazy_mmu_mode(); |
3998 | ||
abf08672 A |
3999 | pte -= (addr - start) / PAGE_SIZE; |
4000 | ||
018ee47f YZ |
4001 | for (i = 0, addr = start; addr != end; i++, addr += PAGE_SIZE) { |
4002 | unsigned long pfn; | |
c33c7948 | 4003 | pte_t ptent = ptep_get(pte + i); |
018ee47f | 4004 | |
c33c7948 | 4005 | pfn = get_pte_pfn(ptent, pvmw->vma, addr); |
018ee47f YZ |
4006 | if (pfn == -1) |
4007 | continue; | |
4008 | ||
c33c7948 | 4009 | if (!pte_young(ptent)) |
018ee47f YZ |
4010 | continue; |
4011 | ||
a3235ea2 | 4012 | folio = get_pfn_folio(pfn, memcg, pgdat, can_swap); |
018ee47f YZ |
4013 | if (!folio) |
4014 | continue; | |
4015 | ||
4016 | if (!ptep_test_and_clear_young(pvmw->vma, addr, pte + i)) | |
4017 | VM_WARN_ON_ONCE(true); | |
4018 | ||
bd74fdae YZ |
4019 | young++; |
4020 | ||
c33c7948 | 4021 | if (pte_dirty(ptent) && !folio_test_dirty(folio) && |
018ee47f YZ |
4022 | !(folio_test_anon(folio) && folio_test_swapbacked(folio) && |
4023 | !folio_test_swapcache(folio))) | |
4024 | folio_mark_dirty(folio); | |
4025 | ||
abf08672 A |
4026 | if (walk) { |
4027 | old_gen = folio_update_gen(folio, new_gen); | |
4028 | if (old_gen >= 0 && old_gen != new_gen) | |
4029 | update_batch_size(walk, folio, old_gen, new_gen); | |
4030 | ||
4031 | continue; | |
4032 | } | |
4033 | ||
018ee47f YZ |
4034 | old_gen = folio_lru_gen(folio); |
4035 | if (old_gen < 0) | |
4036 | folio_set_referenced(folio); | |
4037 | else if (old_gen != new_gen) | |
abf08672 | 4038 | folio_activate(folio); |
018ee47f YZ |
4039 | } |
4040 | ||
4041 | arch_leave_lazy_mmu_mode(); | |
abf08672 | 4042 | mem_cgroup_unlock_pages(); |
018ee47f | 4043 | |
bd74fdae YZ |
4044 | /* feedback from rmap walkers to page table walkers */ |
4045 | if (suitable_to_scan(i, young)) | |
4046 | update_bloom_filter(lruvec, max_seq, pvmw->pmd); | |
018ee47f YZ |
4047 | } |
4048 | ||
36c7b4db A |
4049 | /****************************************************************************** |
4050 | * memcg LRU | |
4051 | ******************************************************************************/ | |
4052 | ||
4053 | /* see the comment on MEMCG_NR_GENS */ | |
4054 | enum { | |
4055 | MEMCG_LRU_NOP, | |
4056 | MEMCG_LRU_HEAD, | |
4057 | MEMCG_LRU_TAIL, | |
4058 | MEMCG_LRU_OLD, | |
4059 | MEMCG_LRU_YOUNG, | |
4060 | }; | |
4061 | ||
4062 | #ifdef CONFIG_MEMCG | |
4063 | ||
4064 | static int lru_gen_memcg_seg(struct lruvec *lruvec) | |
4065 | { | |
4066 | return READ_ONCE(lruvec->lrugen.seg); | |
4067 | } | |
4068 | ||
4069 | static void lru_gen_rotate_memcg(struct lruvec *lruvec, int op) | |
4070 | { | |
4071 | int seg; | |
4072 | int old, new; | |
814bc1de | 4073 | unsigned long flags; |
36c7b4db A |
4074 | int bin = get_random_u32_below(MEMCG_NR_BINS); |
4075 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); | |
4076 | ||
814bc1de | 4077 | spin_lock_irqsave(&pgdat->memcg_lru.lock, flags); |
36c7b4db A |
4078 | |
4079 | VM_WARN_ON_ONCE(hlist_nulls_unhashed(&lruvec->lrugen.list)); | |
4080 | ||
4081 | seg = 0; | |
4082 | new = old = lruvec->lrugen.gen; | |
4083 | ||
4084 | /* see the comment on MEMCG_NR_GENS */ | |
4085 | if (op == MEMCG_LRU_HEAD) | |
4086 | seg = MEMCG_LRU_HEAD; | |
4087 | else if (op == MEMCG_LRU_TAIL) | |
4088 | seg = MEMCG_LRU_TAIL; | |
4089 | else if (op == MEMCG_LRU_OLD) | |
4090 | new = get_memcg_gen(pgdat->memcg_lru.seq); | |
4091 | else if (op == MEMCG_LRU_YOUNG) | |
4092 | new = get_memcg_gen(pgdat->memcg_lru.seq + 1); | |
4093 | else | |
4094 | VM_WARN_ON_ONCE(true); | |
4095 | ||
8aa42061 YZ |
4096 | WRITE_ONCE(lruvec->lrugen.seg, seg); |
4097 | WRITE_ONCE(lruvec->lrugen.gen, new); | |
4098 | ||
36c7b4db A |
4099 | hlist_nulls_del_rcu(&lruvec->lrugen.list); |
4100 | ||
4101 | if (op == MEMCG_LRU_HEAD || op == MEMCG_LRU_OLD) | |
4102 | hlist_nulls_add_head_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[new][bin]); | |
4103 | else | |
4104 | hlist_nulls_add_tail_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[new][bin]); | |
4105 | ||
4106 | pgdat->memcg_lru.nr_memcgs[old]--; | |
4107 | pgdat->memcg_lru.nr_memcgs[new]++; | |
4108 | ||
36c7b4db A |
4109 | if (!pgdat->memcg_lru.nr_memcgs[old] && old == get_memcg_gen(pgdat->memcg_lru.seq)) |
4110 | WRITE_ONCE(pgdat->memcg_lru.seq, pgdat->memcg_lru.seq + 1); | |
4111 | ||
814bc1de | 4112 | spin_unlock_irqrestore(&pgdat->memcg_lru.lock, flags); |
36c7b4db A |
4113 | } |
4114 | ||
4115 | void lru_gen_online_memcg(struct mem_cgroup *memcg) | |
4116 | { | |
4117 | int gen; | |
4118 | int nid; | |
4119 | int bin = get_random_u32_below(MEMCG_NR_BINS); | |
4120 | ||
4121 | for_each_node(nid) { | |
4122 | struct pglist_data *pgdat = NODE_DATA(nid); | |
4123 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
4124 | ||
814bc1de | 4125 | spin_lock_irq(&pgdat->memcg_lru.lock); |
36c7b4db A |
4126 | |
4127 | VM_WARN_ON_ONCE(!hlist_nulls_unhashed(&lruvec->lrugen.list)); | |
4128 | ||
4129 | gen = get_memcg_gen(pgdat->memcg_lru.seq); | |
4130 | ||
8aa42061 YZ |
4131 | lruvec->lrugen.gen = gen; |
4132 | ||
36c7b4db A |
4133 | hlist_nulls_add_tail_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[gen][bin]); |
4134 | pgdat->memcg_lru.nr_memcgs[gen]++; | |
4135 | ||
814bc1de | 4136 | spin_unlock_irq(&pgdat->memcg_lru.lock); |
36c7b4db A |
4137 | } |
4138 | } | |
4139 | ||
4140 | void lru_gen_offline_memcg(struct mem_cgroup *memcg) | |
4141 | { | |
4142 | int nid; | |
4143 | ||
4144 | for_each_node(nid) { | |
4145 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
4146 | ||
4147 | lru_gen_rotate_memcg(lruvec, MEMCG_LRU_OLD); | |
4148 | } | |
4149 | } | |
4150 | ||
4151 | void lru_gen_release_memcg(struct mem_cgroup *memcg) | |
4152 | { | |
4153 | int gen; | |
4154 | int nid; | |
4155 | ||
4156 | for_each_node(nid) { | |
4157 | struct pglist_data *pgdat = NODE_DATA(nid); | |
4158 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
4159 | ||
814bc1de | 4160 | spin_lock_irq(&pgdat->memcg_lru.lock); |
36c7b4db | 4161 | |
6867c7a3 M |
4162 | if (hlist_nulls_unhashed(&lruvec->lrugen.list)) |
4163 | goto unlock; | |
36c7b4db A |
4164 | |
4165 | gen = lruvec->lrugen.gen; | |
4166 | ||
6867c7a3 | 4167 | hlist_nulls_del_init_rcu(&lruvec->lrugen.list); |
36c7b4db A |
4168 | pgdat->memcg_lru.nr_memcgs[gen]--; |
4169 | ||
4170 | if (!pgdat->memcg_lru.nr_memcgs[gen] && gen == get_memcg_gen(pgdat->memcg_lru.seq)) | |
4171 | WRITE_ONCE(pgdat->memcg_lru.seq, pgdat->memcg_lru.seq + 1); | |
6867c7a3 | 4172 | unlock: |
814bc1de | 4173 | spin_unlock_irq(&pgdat->memcg_lru.lock); |
36c7b4db A |
4174 | } |
4175 | } | |
4176 | ||
5c7e7a0d | 4177 | void lru_gen_soft_reclaim(struct mem_cgroup *memcg, int nid) |
36c7b4db | 4178 | { |
5c7e7a0d A |
4179 | struct lruvec *lruvec = get_lruvec(memcg, nid); |
4180 | ||
36c7b4db A |
4181 | /* see the comment on MEMCG_NR_GENS */ |
4182 | if (lru_gen_memcg_seg(lruvec) != MEMCG_LRU_HEAD) | |
4183 | lru_gen_rotate_memcg(lruvec, MEMCG_LRU_HEAD); | |
4184 | } | |
4185 | ||
4186 | #else /* !CONFIG_MEMCG */ | |
4187 | ||
4188 | static int lru_gen_memcg_seg(struct lruvec *lruvec) | |
4189 | { | |
4190 | return 0; | |
4191 | } | |
4192 | ||
4193 | #endif | |
4194 | ||
ac35a490 YZ |
4195 | /****************************************************************************** |
4196 | * the eviction | |
4197 | ******************************************************************************/ | |
4198 | ||
669281ee KS |
4199 | static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_control *sc, |
4200 | int tier_idx) | |
ac35a490 YZ |
4201 | { |
4202 | bool success; | |
4203 | int gen = folio_lru_gen(folio); | |
4204 | int type = folio_is_file_lru(folio); | |
4205 | int zone = folio_zonenum(folio); | |
4206 | int delta = folio_nr_pages(folio); | |
4207 | int refs = folio_lru_refs(folio); | |
4208 | int tier = lru_tier_from_refs(refs); | |
391655fe | 4209 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ac35a490 YZ |
4210 | |
4211 | VM_WARN_ON_ONCE_FOLIO(gen >= MAX_NR_GENS, folio); | |
4212 | ||
4213 | /* unevictable */ | |
4214 | if (!folio_evictable(folio)) { | |
4215 | success = lru_gen_del_folio(lruvec, folio, true); | |
4216 | VM_WARN_ON_ONCE_FOLIO(!success, folio); | |
4217 | folio_set_unevictable(folio); | |
4218 | lruvec_add_folio(lruvec, folio); | |
4219 | __count_vm_events(UNEVICTABLE_PGCULLED, delta); | |
4220 | return true; | |
4221 | } | |
4222 | ||
4223 | /* dirty lazyfree */ | |
4224 | if (type == LRU_GEN_FILE && folio_test_anon(folio) && folio_test_dirty(folio)) { | |
4225 | success = lru_gen_del_folio(lruvec, folio, true); | |
4226 | VM_WARN_ON_ONCE_FOLIO(!success, folio); | |
4227 | folio_set_swapbacked(folio); | |
4228 | lruvec_add_folio_tail(lruvec, folio); | |
4229 | return true; | |
4230 | } | |
4231 | ||
018ee47f YZ |
4232 | /* promoted */ |
4233 | if (gen != lru_gen_from_seq(lrugen->min_seq[type])) { | |
6df1b221 | 4234 | list_move(&folio->lru, &lrugen->folios[gen][type][zone]); |
018ee47f YZ |
4235 | return true; |
4236 | } | |
4237 | ||
ac35a490 | 4238 | /* protected */ |
08148805 | 4239 | if (tier > tier_idx || refs == BIT(LRU_REFS_WIDTH)) { |
ac35a490 YZ |
4240 | int hist = lru_hist_from_seq(lrugen->min_seq[type]); |
4241 | ||
4242 | gen = folio_inc_gen(lruvec, folio, false); | |
6df1b221 | 4243 | list_move_tail(&folio->lru, &lrugen->folios[gen][type][zone]); |
ac35a490 YZ |
4244 | |
4245 | WRITE_ONCE(lrugen->protected[hist][type][tier - 1], | |
4246 | lrugen->protected[hist][type][tier - 1] + delta); | |
ac35a490 YZ |
4247 | return true; |
4248 | } | |
4249 | ||
669281ee | 4250 | /* ineligible */ |
b7108d66 | 4251 | if (zone > sc->reclaim_idx || skip_cma(folio, sc)) { |
669281ee KS |
4252 | gen = folio_inc_gen(lruvec, folio, false); |
4253 | list_move_tail(&folio->lru, &lrugen->folios[gen][type][zone]); | |
4254 | return true; | |
4255 | } | |
4256 | ||
ac35a490 YZ |
4257 | /* waiting for writeback */ |
4258 | if (folio_test_locked(folio) || folio_test_writeback(folio) || | |
4259 | (type == LRU_GEN_FILE && folio_test_dirty(folio))) { | |
4260 | gen = folio_inc_gen(lruvec, folio, true); | |
6df1b221 | 4261 | list_move(&folio->lru, &lrugen->folios[gen][type][zone]); |
ac35a490 YZ |
4262 | return true; |
4263 | } | |
4264 | ||
4265 | return false; | |
4266 | } | |
4267 | ||
4268 | static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct scan_control *sc) | |
4269 | { | |
4270 | bool success; | |
4271 | ||
ac35a490 | 4272 | /* swapping inhibited */ |
e9d4e1ee | 4273 | if (!(sc->gfp_mask & __GFP_IO) && |
ac35a490 YZ |
4274 | (folio_test_dirty(folio) || |
4275 | (folio_test_anon(folio) && !folio_test_swapcache(folio)))) | |
4276 | return false; | |
4277 | ||
4278 | /* raced with release_pages() */ | |
4279 | if (!folio_try_get(folio)) | |
4280 | return false; | |
4281 | ||
4282 | /* raced with another isolation */ | |
4283 | if (!folio_test_clear_lru(folio)) { | |
4284 | folio_put(folio); | |
4285 | return false; | |
4286 | } | |
4287 | ||
4288 | /* see the comment on MAX_NR_TIERS */ | |
4289 | if (!folio_test_referenced(folio)) | |
4290 | set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, 0); | |
4291 | ||
49fd9b6d | 4292 | /* for shrink_folio_list() */ |
ac35a490 YZ |
4293 | folio_clear_reclaim(folio); |
4294 | folio_clear_referenced(folio); | |
4295 | ||
4296 | success = lru_gen_del_folio(lruvec, folio, true); | |
4297 | VM_WARN_ON_ONCE_FOLIO(!success, folio); | |
4298 | ||
4299 | return true; | |
4300 | } | |
4301 | ||
4302 | static int scan_folios(struct lruvec *lruvec, struct scan_control *sc, | |
4303 | int type, int tier, struct list_head *list) | |
4304 | { | |
669281ee KS |
4305 | int i; |
4306 | int gen; | |
ac35a490 YZ |
4307 | enum vm_event_item item; |
4308 | int sorted = 0; | |
4309 | int scanned = 0; | |
4310 | int isolated = 0; | |
8c2214fc | 4311 | int skipped = 0; |
ac35a490 | 4312 | int remaining = MAX_LRU_BATCH; |
391655fe | 4313 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ac35a490 YZ |
4314 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); |
4315 | ||
4316 | VM_WARN_ON_ONCE(!list_empty(list)); | |
4317 | ||
4318 | if (get_nr_gens(lruvec, type) == MIN_NR_GENS) | |
4319 | return 0; | |
4320 | ||
4321 | gen = lru_gen_from_seq(lrugen->min_seq[type]); | |
4322 | ||
669281ee | 4323 | for (i = MAX_NR_ZONES; i > 0; i--) { |
ac35a490 | 4324 | LIST_HEAD(moved); |
8c2214fc | 4325 | int skipped_zone = 0; |
669281ee | 4326 | int zone = (sc->reclaim_idx + i) % MAX_NR_ZONES; |
6df1b221 | 4327 | struct list_head *head = &lrugen->folios[gen][type][zone]; |
ac35a490 YZ |
4328 | |
4329 | while (!list_empty(head)) { | |
4330 | struct folio *folio = lru_to_folio(head); | |
4331 | int delta = folio_nr_pages(folio); | |
4332 | ||
4333 | VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); | |
4334 | VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio); | |
4335 | VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio); | |
4336 | VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio); | |
4337 | ||
4338 | scanned += delta; | |
4339 | ||
669281ee | 4340 | if (sort_folio(lruvec, folio, sc, tier)) |
ac35a490 YZ |
4341 | sorted += delta; |
4342 | else if (isolate_folio(lruvec, folio, sc)) { | |
4343 | list_add(&folio->lru, list); | |
4344 | isolated += delta; | |
4345 | } else { | |
4346 | list_move(&folio->lru, &moved); | |
8c2214fc | 4347 | skipped_zone += delta; |
ac35a490 YZ |
4348 | } |
4349 | ||
8c2214fc | 4350 | if (!--remaining || max(isolated, skipped_zone) >= MIN_LRU_BATCH) |
ac35a490 YZ |
4351 | break; |
4352 | } | |
4353 | ||
8c2214fc | 4354 | if (skipped_zone) { |
ac35a490 | 4355 | list_splice(&moved, head); |
8c2214fc JK |
4356 | __count_zid_vm_events(PGSCAN_SKIP, zone, skipped_zone); |
4357 | skipped += skipped_zone; | |
ac35a490 YZ |
4358 | } |
4359 | ||
4360 | if (!remaining || isolated >= MIN_LRU_BATCH) | |
4361 | break; | |
4362 | } | |
4363 | ||
57e9cc50 | 4364 | item = PGSCAN_KSWAPD + reclaimer_offset(); |
ac35a490 YZ |
4365 | if (!cgroup_reclaim(sc)) { |
4366 | __count_vm_events(item, isolated); | |
4367 | __count_vm_events(PGREFILL, sorted); | |
4368 | } | |
4369 | __count_memcg_events(memcg, item, isolated); | |
4370 | __count_memcg_events(memcg, PGREFILL, sorted); | |
4371 | __count_vm_events(PGSCAN_ANON + type, isolated); | |
8c2214fc JK |
4372 | trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, MAX_LRU_BATCH, |
4373 | scanned, skipped, isolated, | |
4374 | type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON); | |
ac35a490 YZ |
4375 | |
4376 | /* | |
e9d4e1ee YZ |
4377 | * There might not be eligible folios due to reclaim_idx. Check the |
4378 | * remaining to prevent livelock if it's not making progress. | |
ac35a490 YZ |
4379 | */ |
4380 | return isolated || !remaining ? scanned : 0; | |
4381 | } | |
4382 | ||
4383 | static int get_tier_idx(struct lruvec *lruvec, int type) | |
4384 | { | |
4385 | int tier; | |
4386 | struct ctrl_pos sp, pv; | |
4387 | ||
4388 | /* | |
4389 | * To leave a margin for fluctuations, use a larger gain factor (1:2). | |
4390 | * This value is chosen because any other tier would have at least twice | |
4391 | * as many refaults as the first tier. | |
4392 | */ | |
4393 | read_ctrl_pos(lruvec, type, 0, 1, &sp); | |
4394 | for (tier = 1; tier < MAX_NR_TIERS; tier++) { | |
4395 | read_ctrl_pos(lruvec, type, tier, 2, &pv); | |
4396 | if (!positive_ctrl_err(&sp, &pv)) | |
4397 | break; | |
4398 | } | |
4399 | ||
4400 | return tier - 1; | |
4401 | } | |
4402 | ||
4403 | static int get_type_to_scan(struct lruvec *lruvec, int swappiness, int *tier_idx) | |
4404 | { | |
4405 | int type, tier; | |
4406 | struct ctrl_pos sp, pv; | |
4407 | int gain[ANON_AND_FILE] = { swappiness, 200 - swappiness }; | |
4408 | ||
4409 | /* | |
4410 | * Compare the first tier of anon with that of file to determine which | |
4411 | * type to scan. Also need to compare other tiers of the selected type | |
4412 | * with the first tier of the other type to determine the last tier (of | |
4413 | * the selected type) to evict. | |
4414 | */ | |
4415 | read_ctrl_pos(lruvec, LRU_GEN_ANON, 0, gain[LRU_GEN_ANON], &sp); | |
4416 | read_ctrl_pos(lruvec, LRU_GEN_FILE, 0, gain[LRU_GEN_FILE], &pv); | |
4417 | type = positive_ctrl_err(&sp, &pv); | |
4418 | ||
4419 | read_ctrl_pos(lruvec, !type, 0, gain[!type], &sp); | |
4420 | for (tier = 1; tier < MAX_NR_TIERS; tier++) { | |
4421 | read_ctrl_pos(lruvec, type, tier, gain[type], &pv); | |
4422 | if (!positive_ctrl_err(&sp, &pv)) | |
4423 | break; | |
4424 | } | |
4425 | ||
4426 | *tier_idx = tier - 1; | |
4427 | ||
4428 | return type; | |
4429 | } | |
4430 | ||
4431 | static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness, | |
4432 | int *type_scanned, struct list_head *list) | |
4433 | { | |
4434 | int i; | |
4435 | int type; | |
4436 | int scanned; | |
4437 | int tier = -1; | |
4438 | DEFINE_MIN_SEQ(lruvec); | |
4439 | ||
4440 | /* | |
4441 | * Try to make the obvious choice first. When anon and file are both | |
4442 | * available from the same generation, interpret swappiness 1 as file | |
4443 | * first and 200 as anon first. | |
4444 | */ | |
4445 | if (!swappiness) | |
4446 | type = LRU_GEN_FILE; | |
4447 | else if (min_seq[LRU_GEN_ANON] < min_seq[LRU_GEN_FILE]) | |
4448 | type = LRU_GEN_ANON; | |
4449 | else if (swappiness == 1) | |
4450 | type = LRU_GEN_FILE; | |
4451 | else if (swappiness == 200) | |
4452 | type = LRU_GEN_ANON; | |
4453 | else | |
4454 | type = get_type_to_scan(lruvec, swappiness, &tier); | |
4455 | ||
4456 | for (i = !swappiness; i < ANON_AND_FILE; i++) { | |
4457 | if (tier < 0) | |
4458 | tier = get_tier_idx(lruvec, type); | |
4459 | ||
4460 | scanned = scan_folios(lruvec, sc, type, tier, list); | |
4461 | if (scanned) | |
4462 | break; | |
4463 | ||
4464 | type = !type; | |
4465 | tier = -1; | |
4466 | } | |
4467 | ||
4468 | *type_scanned = type; | |
4469 | ||
4470 | return scanned; | |
4471 | } | |
4472 | ||
a579086c | 4473 | static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness) |
ac35a490 YZ |
4474 | { |
4475 | int type; | |
4476 | int scanned; | |
4477 | int reclaimed; | |
4478 | LIST_HEAD(list); | |
359a5e14 | 4479 | LIST_HEAD(clean); |
ac35a490 | 4480 | struct folio *folio; |
359a5e14 | 4481 | struct folio *next; |
ac35a490 YZ |
4482 | enum vm_event_item item; |
4483 | struct reclaim_stat stat; | |
bd74fdae | 4484 | struct lru_gen_mm_walk *walk; |
359a5e14 | 4485 | bool skip_retry = false; |
ac35a490 YZ |
4486 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); |
4487 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); | |
4488 | ||
4489 | spin_lock_irq(&lruvec->lru_lock); | |
4490 | ||
4491 | scanned = isolate_folios(lruvec, sc, swappiness, &type, &list); | |
4492 | ||
4493 | scanned += try_to_inc_min_seq(lruvec, swappiness); | |
4494 | ||
4495 | if (get_nr_gens(lruvec, !swappiness) == MIN_NR_GENS) | |
4496 | scanned = 0; | |
4497 | ||
4498 | spin_unlock_irq(&lruvec->lru_lock); | |
4499 | ||
4500 | if (list_empty(&list)) | |
4501 | return scanned; | |
359a5e14 | 4502 | retry: |
49fd9b6d | 4503 | reclaimed = shrink_folio_list(&list, pgdat, sc, &stat, false); |
359a5e14 | 4504 | sc->nr_reclaimed += reclaimed; |
8c2214fc JK |
4505 | trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, |
4506 | scanned, reclaimed, &stat, sc->priority, | |
4507 | type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON); | |
ac35a490 | 4508 | |
359a5e14 YZ |
4509 | list_for_each_entry_safe_reverse(folio, next, &list, lru) { |
4510 | if (!folio_evictable(folio)) { | |
4511 | list_del(&folio->lru); | |
4512 | folio_putback_lru(folio); | |
4513 | continue; | |
4514 | } | |
ac35a490 | 4515 | |
ac35a490 | 4516 | if (folio_test_reclaim(folio) && |
359a5e14 YZ |
4517 | (folio_test_dirty(folio) || folio_test_writeback(folio))) { |
4518 | /* restore LRU_REFS_FLAGS cleared by isolate_folio() */ | |
4519 | if (folio_test_workingset(folio)) | |
4520 | folio_set_referenced(folio); | |
4521 | continue; | |
4522 | } | |
4523 | ||
4524 | if (skip_retry || folio_test_active(folio) || folio_test_referenced(folio) || | |
4525 | folio_mapped(folio) || folio_test_locked(folio) || | |
4526 | folio_test_dirty(folio) || folio_test_writeback(folio)) { | |
4527 | /* don't add rejected folios to the oldest generation */ | |
4528 | set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, | |
4529 | BIT(PG_active)); | |
4530 | continue; | |
4531 | } | |
4532 | ||
4533 | /* retry folios that may have missed folio_rotate_reclaimable() */ | |
4534 | list_move(&folio->lru, &clean); | |
4535 | sc->nr_scanned -= folio_nr_pages(folio); | |
ac35a490 YZ |
4536 | } |
4537 | ||
4538 | spin_lock_irq(&lruvec->lru_lock); | |
4539 | ||
49fd9b6d | 4540 | move_folios_to_lru(lruvec, &list); |
ac35a490 | 4541 | |
bd74fdae YZ |
4542 | walk = current->reclaim_state->mm_walk; |
4543 | if (walk && walk->batched) | |
4544 | reset_batch_size(lruvec, walk); | |
4545 | ||
57e9cc50 | 4546 | item = PGSTEAL_KSWAPD + reclaimer_offset(); |
ac35a490 YZ |
4547 | if (!cgroup_reclaim(sc)) |
4548 | __count_vm_events(item, reclaimed); | |
4549 | __count_memcg_events(memcg, item, reclaimed); | |
4550 | __count_vm_events(PGSTEAL_ANON + type, reclaimed); | |
4551 | ||
4552 | spin_unlock_irq(&lruvec->lru_lock); | |
4553 | ||
4554 | mem_cgroup_uncharge_list(&list); | |
4555 | free_unref_page_list(&list); | |
4556 | ||
359a5e14 YZ |
4557 | INIT_LIST_HEAD(&list); |
4558 | list_splice_init(&clean, &list); | |
4559 | ||
4560 | if (!list_empty(&list)) { | |
4561 | skip_retry = true; | |
4562 | goto retry; | |
4563 | } | |
ac35a490 YZ |
4564 | |
4565 | return scanned; | |
4566 | } | |
4567 | ||
77d4459a YZ |
4568 | static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, |
4569 | struct scan_control *sc, bool can_swap, unsigned long *nr_to_scan) | |
4570 | { | |
4571 | int gen, type, zone; | |
4572 | unsigned long old = 0; | |
4573 | unsigned long young = 0; | |
4574 | unsigned long total = 0; | |
4575 | struct lru_gen_folio *lrugen = &lruvec->lrugen; | |
4576 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
4577 | DEFINE_MIN_SEQ(lruvec); | |
4578 | ||
4579 | /* whether this lruvec is completely out of cold folios */ | |
4580 | if (min_seq[!can_swap] + MIN_NR_GENS > max_seq) { | |
4581 | *nr_to_scan = 0; | |
4582 | return true; | |
4583 | } | |
4584 | ||
4585 | for (type = !can_swap; type < ANON_AND_FILE; type++) { | |
4586 | unsigned long seq; | |
4587 | ||
4588 | for (seq = min_seq[type]; seq <= max_seq; seq++) { | |
4589 | unsigned long size = 0; | |
4590 | ||
4591 | gen = lru_gen_from_seq(seq); | |
4592 | ||
4593 | for (zone = 0; zone < MAX_NR_ZONES; zone++) | |
4594 | size += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); | |
4595 | ||
4596 | total += size; | |
4597 | if (seq == max_seq) | |
4598 | young += size; | |
4599 | else if (seq + MIN_NR_GENS == max_seq) | |
4600 | old += size; | |
4601 | } | |
4602 | } | |
4603 | ||
4604 | /* try to scrape all its memory if this memcg was deleted */ | |
4376807b YZ |
4605 | if (!mem_cgroup_online(memcg)) { |
4606 | *nr_to_scan = total; | |
4607 | return false; | |
4608 | } | |
4609 | ||
4610 | *nr_to_scan = total >> sc->priority; | |
77d4459a YZ |
4611 | |
4612 | /* | |
4613 | * The aging tries to be lazy to reduce the overhead, while the eviction | |
4614 | * stalls when the number of generations reaches MIN_NR_GENS. Hence, the | |
4615 | * ideal number of generations is MIN_NR_GENS+1. | |
4616 | */ | |
4617 | if (min_seq[!can_swap] + MIN_NR_GENS < max_seq) | |
4618 | return false; | |
4619 | ||
4620 | /* | |
4621 | * It's also ideal to spread pages out evenly, i.e., 1/(MIN_NR_GENS+1) | |
4622 | * of the total number of pages for each generation. A reasonable range | |
4623 | * for this average portion is [1/MIN_NR_GENS, 1/(MIN_NR_GENS+2)]. The | |
4624 | * aging cares about the upper bound of hot pages, while the eviction | |
4625 | * cares about the lower bound of cold pages. | |
4626 | */ | |
4627 | if (young * MIN_NR_GENS > total) | |
4628 | return true; | |
4629 | if (old * (MIN_NR_GENS + 2) < total) | |
4630 | return true; | |
4631 | ||
4632 | return false; | |
4633 | } | |
4634 | ||
bd74fdae YZ |
4635 | /* |
4636 | * For future optimizations: | |
4637 | * 1. Defer try_to_inc_max_seq() to workqueues to reduce latency for memcg | |
4638 | * reclaim. | |
4639 | */ | |
e4dde56c | 4640 | static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc, bool can_swap) |
ac35a490 | 4641 | { |
ac35a490 YZ |
4642 | unsigned long nr_to_scan; |
4643 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
4644 | DEFINE_MAX_SEQ(lruvec); | |
ac35a490 | 4645 | |
e9d4e1ee | 4646 | if (mem_cgroup_below_min(sc->target_mem_cgroup, memcg)) |
8aa42061 | 4647 | return -1; |
ac35a490 | 4648 | |
7348cc91 | 4649 | if (!should_run_aging(lruvec, max_seq, sc, can_swap, &nr_to_scan)) |
ac35a490 YZ |
4650 | return nr_to_scan; |
4651 | ||
4652 | /* skip the aging path at the default priority */ | |
4653 | if (sc->priority == DEF_PRIORITY) | |
7348cc91 | 4654 | return nr_to_scan; |
ac35a490 | 4655 | |
7348cc91 | 4656 | /* skip this lruvec as it's low on cold folios */ |
e4dde56c | 4657 | return try_to_inc_max_seq(lruvec, max_seq, sc, can_swap, false) ? -1 : 0; |
ac35a490 YZ |
4658 | } |
4659 | ||
5095a2b2 | 4660 | static bool should_abort_scan(struct lruvec *lruvec, struct scan_control *sc) |
f76c8337 | 4661 | { |
5095a2b2 YZ |
4662 | int i; |
4663 | enum zone_watermarks mark; | |
4664 | ||
a579086c | 4665 | /* don't abort memcg reclaim to ensure fairness */ |
7a704474 | 4666 | if (!root_reclaim(sc)) |
5095a2b2 | 4667 | return false; |
f76c8337 | 4668 | |
5095a2b2 YZ |
4669 | if (sc->nr_reclaimed >= max(sc->nr_to_reclaim, compact_gap(sc->order))) |
4670 | return true; | |
4671 | ||
4672 | /* check the order to exclude compaction-induced reclaim */ | |
4673 | if (!current_is_kswapd() || sc->order) | |
4674 | return false; | |
4675 | ||
4676 | mark = sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING ? | |
4677 | WMARK_PROMO : WMARK_HIGH; | |
4678 | ||
4679 | for (i = 0; i <= sc->reclaim_idx; i++) { | |
4680 | struct zone *zone = lruvec_pgdat(lruvec)->node_zones + i; | |
4681 | unsigned long size = wmark_pages(zone, mark) + MIN_LRU_BATCH; | |
4682 | ||
4683 | if (managed_zone(zone) && !zone_watermark_ok(zone, 0, size, sc->reclaim_idx, 0)) | |
4684 | return false; | |
4685 | } | |
f76c8337 | 4686 | |
5095a2b2 YZ |
4687 | /* kswapd should abort if all eligible zones are safe */ |
4688 | return true; | |
f76c8337 YZ |
4689 | } |
4690 | ||
e4dde56c | 4691 | static bool try_to_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) |
ac35a490 | 4692 | { |
e4dde56c | 4693 | long nr_to_scan; |
ac35a490 | 4694 | unsigned long scanned = 0; |
e9d4e1ee YZ |
4695 | int swappiness = get_swappiness(lruvec, sc); |
4696 | ||
4697 | /* clean file folios are more likely to exist */ | |
4698 | if (swappiness && !(sc->gfp_mask & __GFP_IO)) | |
4699 | swappiness = 1; | |
ac35a490 | 4700 | |
ac35a490 YZ |
4701 | while (true) { |
4702 | int delta; | |
ac35a490 | 4703 | |
7348cc91 | 4704 | nr_to_scan = get_nr_to_scan(lruvec, sc, swappiness); |
e4dde56c | 4705 | if (nr_to_scan <= 0) |
7348cc91 | 4706 | break; |
ac35a490 | 4707 | |
a579086c | 4708 | delta = evict_folios(lruvec, sc, swappiness); |
ac35a490 | 4709 | if (!delta) |
7348cc91 | 4710 | break; |
ac35a490 YZ |
4711 | |
4712 | scanned += delta; | |
4713 | if (scanned >= nr_to_scan) | |
4714 | break; | |
4715 | ||
5095a2b2 | 4716 | if (should_abort_scan(lruvec, sc)) |
f76c8337 YZ |
4717 | break; |
4718 | ||
ac35a490 YZ |
4719 | cond_resched(); |
4720 | } | |
4721 | ||
8aa42061 | 4722 | /* whether this lruvec should be rotated */ |
e4dde56c YZ |
4723 | return nr_to_scan < 0; |
4724 | } | |
4725 | ||
4726 | static int shrink_one(struct lruvec *lruvec, struct scan_control *sc) | |
4727 | { | |
4728 | bool success; | |
4729 | unsigned long scanned = sc->nr_scanned; | |
4730 | unsigned long reclaimed = sc->nr_reclaimed; | |
e4dde56c YZ |
4731 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); |
4732 | struct pglist_data *pgdat = lruvec_pgdat(lruvec); | |
4733 | ||
e4dde56c YZ |
4734 | mem_cgroup_calculate_protection(NULL, memcg); |
4735 | ||
4736 | if (mem_cgroup_below_min(NULL, memcg)) | |
4737 | return MEMCG_LRU_YOUNG; | |
4738 | ||
4739 | if (mem_cgroup_below_low(NULL, memcg)) { | |
4740 | /* see the comment on MEMCG_NR_GENS */ | |
4376807b | 4741 | if (lru_gen_memcg_seg(lruvec) != MEMCG_LRU_TAIL) |
e4dde56c YZ |
4742 | return MEMCG_LRU_TAIL; |
4743 | ||
4744 | memcg_memory_event(memcg, MEMCG_LOW); | |
4745 | } | |
4746 | ||
4747 | success = try_to_shrink_lruvec(lruvec, sc); | |
4748 | ||
4749 | shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, sc->priority); | |
4750 | ||
4751 | if (!sc->proactive) | |
4752 | vmpressure(sc->gfp_mask, memcg, false, sc->nr_scanned - scanned, | |
4753 | sc->nr_reclaimed - reclaimed); | |
4754 | ||
583c27a1 | 4755 | flush_reclaim_state(sc); |
e4dde56c | 4756 | |
4376807b YZ |
4757 | if (success && mem_cgroup_online(memcg)) |
4758 | return MEMCG_LRU_YOUNG; | |
4759 | ||
4760 | if (!success && lruvec_is_sizable(lruvec, sc)) | |
4761 | return 0; | |
4762 | ||
4763 | /* one retry if offlined or too small */ | |
4764 | return lru_gen_memcg_seg(lruvec) != MEMCG_LRU_TAIL ? | |
4765 | MEMCG_LRU_TAIL : MEMCG_LRU_YOUNG; | |
e4dde56c YZ |
4766 | } |
4767 | ||
4768 | #ifdef CONFIG_MEMCG | |
4769 | ||
4770 | static void shrink_many(struct pglist_data *pgdat, struct scan_control *sc) | |
4771 | { | |
9f550d78 | 4772 | int op; |
e4dde56c YZ |
4773 | int gen; |
4774 | int bin; | |
4775 | int first_bin; | |
4776 | struct lruvec *lruvec; | |
4777 | struct lru_gen_folio *lrugen; | |
9f550d78 | 4778 | struct mem_cgroup *memcg; |
8aa42061 | 4779 | struct hlist_nulls_node *pos; |
e4dde56c | 4780 | |
8aa42061 | 4781 | gen = get_memcg_gen(READ_ONCE(pgdat->memcg_lru.seq)); |
e4dde56c YZ |
4782 | bin = first_bin = get_random_u32_below(MEMCG_NR_BINS); |
4783 | restart: | |
9f550d78 YZ |
4784 | op = 0; |
4785 | memcg = NULL; | |
e4dde56c YZ |
4786 | |
4787 | rcu_read_lock(); | |
4788 | ||
4789 | hlist_nulls_for_each_entry_rcu(lrugen, pos, &pgdat->memcg_lru.fifo[gen][bin], list) { | |
6867c7a3 | 4790 | if (op) { |
e4dde56c | 4791 | lru_gen_rotate_memcg(lruvec, op); |
6867c7a3 M |
4792 | op = 0; |
4793 | } | |
e4dde56c YZ |
4794 | |
4795 | mem_cgroup_put(memcg); | |
8aa42061 YZ |
4796 | memcg = NULL; |
4797 | ||
4798 | if (gen != READ_ONCE(lrugen->gen)) | |
4799 | continue; | |
e4dde56c YZ |
4800 | |
4801 | lruvec = container_of(lrugen, struct lruvec, lrugen); | |
4802 | memcg = lruvec_memcg(lruvec); | |
4803 | ||
4804 | if (!mem_cgroup_tryget(memcg)) { | |
6867c7a3 | 4805 | lru_gen_release_memcg(memcg); |
e4dde56c YZ |
4806 | memcg = NULL; |
4807 | continue; | |
4808 | } | |
4809 | ||
4810 | rcu_read_unlock(); | |
4811 | ||
4812 | op = shrink_one(lruvec, sc); | |
4813 | ||
e4dde56c | 4814 | rcu_read_lock(); |
9f550d78 | 4815 | |
5095a2b2 | 4816 | if (should_abort_scan(lruvec, sc)) |
9f550d78 | 4817 | break; |
e4dde56c YZ |
4818 | } |
4819 | ||
4820 | rcu_read_unlock(); | |
4821 | ||
9f550d78 YZ |
4822 | if (op) |
4823 | lru_gen_rotate_memcg(lruvec, op); | |
4824 | ||
4825 | mem_cgroup_put(memcg); | |
4826 | ||
5095a2b2 | 4827 | if (!is_a_nulls(pos)) |
9f550d78 YZ |
4828 | return; |
4829 | ||
e4dde56c YZ |
4830 | /* restart if raced with lru_gen_rotate_memcg() */ |
4831 | if (gen != get_nulls_value(pos)) | |
4832 | goto restart; | |
4833 | ||
4834 | /* try the rest of the bins of the current generation */ | |
4835 | bin = get_memcg_bin(bin + 1); | |
4836 | if (bin != first_bin) | |
4837 | goto restart; | |
e4dde56c YZ |
4838 | } |
4839 | ||
4840 | static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) | |
4841 | { | |
4842 | struct blk_plug plug; | |
4843 | ||
7a704474 | 4844 | VM_WARN_ON_ONCE(root_reclaim(sc)); |
e9d4e1ee | 4845 | VM_WARN_ON_ONCE(!sc->may_writepage || !sc->may_unmap); |
e4dde56c YZ |
4846 | |
4847 | lru_add_drain(); | |
4848 | ||
4849 | blk_start_plug(&plug); | |
4850 | ||
e9d4e1ee | 4851 | set_mm_walk(NULL, sc->proactive); |
e4dde56c YZ |
4852 | |
4853 | if (try_to_shrink_lruvec(lruvec, sc)) | |
4854 | lru_gen_rotate_memcg(lruvec, MEMCG_LRU_YOUNG); | |
4855 | ||
4856 | clear_mm_walk(); | |
4857 | ||
4858 | blk_finish_plug(&plug); | |
4859 | } | |
4860 | ||
4861 | #else /* !CONFIG_MEMCG */ | |
4862 | ||
4863 | static void shrink_many(struct pglist_data *pgdat, struct scan_control *sc) | |
4864 | { | |
4865 | BUILD_BUG(); | |
4866 | } | |
4867 | ||
4868 | static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) | |
4869 | { | |
4870 | BUILD_BUG(); | |
4871 | } | |
4872 | ||
4873 | #endif | |
4874 | ||
4875 | static void set_initial_priority(struct pglist_data *pgdat, struct scan_control *sc) | |
4876 | { | |
4877 | int priority; | |
4878 | unsigned long reclaimable; | |
4879 | struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat); | |
4880 | ||
4881 | if (sc->priority != DEF_PRIORITY || sc->nr_to_reclaim < MIN_LRU_BATCH) | |
4882 | return; | |
4883 | /* | |
8aa42061 YZ |
4884 | * Determine the initial priority based on |
4885 | * (total >> priority) * reclaimed_to_scanned_ratio = nr_to_reclaim, | |
4886 | * where reclaimed_to_scanned_ratio = inactive / total. | |
e4dde56c YZ |
4887 | */ |
4888 | reclaimable = node_page_state(pgdat, NR_INACTIVE_FILE); | |
4889 | if (get_swappiness(lruvec, sc)) | |
4890 | reclaimable += node_page_state(pgdat, NR_INACTIVE_ANON); | |
4891 | ||
e4dde56c YZ |
4892 | /* round down reclaimable and round up sc->nr_to_reclaim */ |
4893 | priority = fls_long(reclaimable) - 1 - fls_long(sc->nr_to_reclaim - 1); | |
4894 | ||
4895 | sc->priority = clamp(priority, 0, DEF_PRIORITY); | |
4896 | } | |
4897 | ||
4898 | static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *sc) | |
4899 | { | |
4900 | struct blk_plug plug; | |
4901 | unsigned long reclaimed = sc->nr_reclaimed; | |
4902 | ||
7a704474 | 4903 | VM_WARN_ON_ONCE(!root_reclaim(sc)); |
e4dde56c | 4904 | |
e9d4e1ee YZ |
4905 | /* |
4906 | * Unmapped clean folios are already prioritized. Scanning for more of | |
4907 | * them is likely futile and can cause high reclaim latency when there | |
4908 | * is a large number of memcgs. | |
4909 | */ | |
4910 | if (!sc->may_writepage || !sc->may_unmap) | |
4911 | goto done; | |
4912 | ||
e4dde56c YZ |
4913 | lru_add_drain(); |
4914 | ||
4915 | blk_start_plug(&plug); | |
4916 | ||
e9d4e1ee | 4917 | set_mm_walk(pgdat, sc->proactive); |
e4dde56c YZ |
4918 | |
4919 | set_initial_priority(pgdat, sc); | |
4920 | ||
4921 | if (current_is_kswapd()) | |
4922 | sc->nr_reclaimed = 0; | |
4923 | ||
4924 | if (mem_cgroup_disabled()) | |
4925 | shrink_one(&pgdat->__lruvec, sc); | |
4926 | else | |
4927 | shrink_many(pgdat, sc); | |
4928 | ||
4929 | if (current_is_kswapd()) | |
4930 | sc->nr_reclaimed += reclaimed; | |
4931 | ||
bd74fdae YZ |
4932 | clear_mm_walk(); |
4933 | ||
ac35a490 | 4934 | blk_finish_plug(&plug); |
e9d4e1ee | 4935 | done: |
e4dde56c YZ |
4936 | /* kswapd should never fail */ |
4937 | pgdat->kswapd_failures = 0; | |
4938 | } | |
4939 | ||
354ed597 YZ |
4940 | /****************************************************************************** |
4941 | * state change | |
4942 | ******************************************************************************/ | |
4943 | ||
4944 | static bool __maybe_unused state_is_valid(struct lruvec *lruvec) | |
4945 | { | |
391655fe | 4946 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
354ed597 YZ |
4947 | |
4948 | if (lrugen->enabled) { | |
4949 | enum lru_list lru; | |
4950 | ||
4951 | for_each_evictable_lru(lru) { | |
4952 | if (!list_empty(&lruvec->lists[lru])) | |
4953 | return false; | |
4954 | } | |
4955 | } else { | |
4956 | int gen, type, zone; | |
4957 | ||
4958 | for_each_gen_type_zone(gen, type, zone) { | |
6df1b221 | 4959 | if (!list_empty(&lrugen->folios[gen][type][zone])) |
354ed597 YZ |
4960 | return false; |
4961 | } | |
4962 | } | |
4963 | ||
4964 | return true; | |
4965 | } | |
4966 | ||
4967 | static bool fill_evictable(struct lruvec *lruvec) | |
4968 | { | |
4969 | enum lru_list lru; | |
4970 | int remaining = MAX_LRU_BATCH; | |
4971 | ||
4972 | for_each_evictable_lru(lru) { | |
4973 | int type = is_file_lru(lru); | |
4974 | bool active = is_active_lru(lru); | |
4975 | struct list_head *head = &lruvec->lists[lru]; | |
4976 | ||
4977 | while (!list_empty(head)) { | |
4978 | bool success; | |
4979 | struct folio *folio = lru_to_folio(head); | |
4980 | ||
4981 | VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); | |
4982 | VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio) != active, folio); | |
4983 | VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio); | |
4984 | VM_WARN_ON_ONCE_FOLIO(folio_lru_gen(folio) != -1, folio); | |
4985 | ||
4986 | lruvec_del_folio(lruvec, folio); | |
4987 | success = lru_gen_add_folio(lruvec, folio, false); | |
4988 | VM_WARN_ON_ONCE(!success); | |
4989 | ||
4990 | if (!--remaining) | |
4991 | return false; | |
4992 | } | |
4993 | } | |
4994 | ||
4995 | return true; | |
4996 | } | |
4997 | ||
4998 | static bool drain_evictable(struct lruvec *lruvec) | |
4999 | { | |
5000 | int gen, type, zone; | |
5001 | int remaining = MAX_LRU_BATCH; | |
5002 | ||
5003 | for_each_gen_type_zone(gen, type, zone) { | |
6df1b221 | 5004 | struct list_head *head = &lruvec->lrugen.folios[gen][type][zone]; |
354ed597 YZ |
5005 | |
5006 | while (!list_empty(head)) { | |
5007 | bool success; | |
5008 | struct folio *folio = lru_to_folio(head); | |
5009 | ||
5010 | VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); | |
5011 | VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio); | |
5012 | VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio); | |
5013 | VM_WARN_ON_ONCE_FOLIO(folio_zonenum(folio) != zone, folio); | |
5014 | ||
5015 | success = lru_gen_del_folio(lruvec, folio, false); | |
5016 | VM_WARN_ON_ONCE(!success); | |
5017 | lruvec_add_folio(lruvec, folio); | |
5018 | ||
5019 | if (!--remaining) | |
5020 | return false; | |
5021 | } | |
5022 | } | |
5023 | ||
5024 | return true; | |
5025 | } | |
5026 | ||
5027 | static void lru_gen_change_state(bool enabled) | |
5028 | { | |
5029 | static DEFINE_MUTEX(state_mutex); | |
5030 | ||
5031 | struct mem_cgroup *memcg; | |
5032 | ||
5033 | cgroup_lock(); | |
5034 | cpus_read_lock(); | |
5035 | get_online_mems(); | |
5036 | mutex_lock(&state_mutex); | |
5037 | ||
5038 | if (enabled == lru_gen_enabled()) | |
5039 | goto unlock; | |
5040 | ||
5041 | if (enabled) | |
5042 | static_branch_enable_cpuslocked(&lru_gen_caps[LRU_GEN_CORE]); | |
5043 | else | |
5044 | static_branch_disable_cpuslocked(&lru_gen_caps[LRU_GEN_CORE]); | |
5045 | ||
5046 | memcg = mem_cgroup_iter(NULL, NULL, NULL); | |
5047 | do { | |
5048 | int nid; | |
5049 | ||
5050 | for_each_node(nid) { | |
5051 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
5052 | ||
354ed597 YZ |
5053 | spin_lock_irq(&lruvec->lru_lock); |
5054 | ||
5055 | VM_WARN_ON_ONCE(!seq_is_valid(lruvec)); | |
5056 | VM_WARN_ON_ONCE(!state_is_valid(lruvec)); | |
5057 | ||
5058 | lruvec->lrugen.enabled = enabled; | |
5059 | ||
5060 | while (!(enabled ? fill_evictable(lruvec) : drain_evictable(lruvec))) { | |
5061 | spin_unlock_irq(&lruvec->lru_lock); | |
5062 | cond_resched(); | |
5063 | spin_lock_irq(&lruvec->lru_lock); | |
5064 | } | |
5065 | ||
5066 | spin_unlock_irq(&lruvec->lru_lock); | |
5067 | } | |
5068 | ||
5069 | cond_resched(); | |
5070 | } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); | |
5071 | unlock: | |
5072 | mutex_unlock(&state_mutex); | |
5073 | put_online_mems(); | |
5074 | cpus_read_unlock(); | |
5075 | cgroup_unlock(); | |
5076 | } | |
5077 | ||
5078 | /****************************************************************************** | |
5079 | * sysfs interface | |
5080 | ******************************************************************************/ | |
5081 | ||
9a52b2f3 | 5082 | static ssize_t min_ttl_ms_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
1332a809 | 5083 | { |
9a52b2f3 | 5084 | return sysfs_emit(buf, "%u\n", jiffies_to_msecs(READ_ONCE(lru_gen_min_ttl))); |
1332a809 YZ |
5085 | } |
5086 | ||
07017acb | 5087 | /* see Documentation/admin-guide/mm/multigen_lru.rst for details */ |
9a52b2f3 A |
5088 | static ssize_t min_ttl_ms_store(struct kobject *kobj, struct kobj_attribute *attr, |
5089 | const char *buf, size_t len) | |
1332a809 YZ |
5090 | { |
5091 | unsigned int msecs; | |
5092 | ||
5093 | if (kstrtouint(buf, 0, &msecs)) | |
5094 | return -EINVAL; | |
5095 | ||
5096 | WRITE_ONCE(lru_gen_min_ttl, msecs_to_jiffies(msecs)); | |
5097 | ||
5098 | return len; | |
5099 | } | |
5100 | ||
9a52b2f3 | 5101 | static struct kobj_attribute lru_gen_min_ttl_attr = __ATTR_RW(min_ttl_ms); |
1332a809 | 5102 | |
9a52b2f3 | 5103 | static ssize_t enabled_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
354ed597 YZ |
5104 | { |
5105 | unsigned int caps = 0; | |
5106 | ||
5107 | if (get_cap(LRU_GEN_CORE)) | |
5108 | caps |= BIT(LRU_GEN_CORE); | |
5109 | ||
bd02df41 | 5110 | if (should_walk_mmu()) |
354ed597 YZ |
5111 | caps |= BIT(LRU_GEN_MM_WALK); |
5112 | ||
bd02df41 | 5113 | if (should_clear_pmd_young()) |
354ed597 YZ |
5114 | caps |= BIT(LRU_GEN_NONLEAF_YOUNG); |
5115 | ||
8ef9c32a | 5116 | return sysfs_emit(buf, "0x%04x\n", caps); |
354ed597 YZ |
5117 | } |
5118 | ||
07017acb | 5119 | /* see Documentation/admin-guide/mm/multigen_lru.rst for details */ |
9a52b2f3 | 5120 | static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, |
354ed597 YZ |
5121 | const char *buf, size_t len) |
5122 | { | |
5123 | int i; | |
5124 | unsigned int caps; | |
5125 | ||
5126 | if (tolower(*buf) == 'n') | |
5127 | caps = 0; | |
5128 | else if (tolower(*buf) == 'y') | |
5129 | caps = -1; | |
5130 | else if (kstrtouint(buf, 0, &caps)) | |
5131 | return -EINVAL; | |
5132 | ||
5133 | for (i = 0; i < NR_LRU_GEN_CAPS; i++) { | |
5134 | bool enabled = caps & BIT(i); | |
5135 | ||
5136 | if (i == LRU_GEN_CORE) | |
5137 | lru_gen_change_state(enabled); | |
5138 | else if (enabled) | |
5139 | static_branch_enable(&lru_gen_caps[i]); | |
5140 | else | |
5141 | static_branch_disable(&lru_gen_caps[i]); | |
5142 | } | |
5143 | ||
5144 | return len; | |
5145 | } | |
5146 | ||
9a52b2f3 | 5147 | static struct kobj_attribute lru_gen_enabled_attr = __ATTR_RW(enabled); |
354ed597 YZ |
5148 | |
5149 | static struct attribute *lru_gen_attrs[] = { | |
1332a809 | 5150 | &lru_gen_min_ttl_attr.attr, |
354ed597 YZ |
5151 | &lru_gen_enabled_attr.attr, |
5152 | NULL | |
5153 | }; | |
5154 | ||
9a52b2f3 | 5155 | static const struct attribute_group lru_gen_attr_group = { |
354ed597 YZ |
5156 | .name = "lru_gen", |
5157 | .attrs = lru_gen_attrs, | |
5158 | }; | |
5159 | ||
d6c3af7d YZ |
5160 | /****************************************************************************** |
5161 | * debugfs interface | |
5162 | ******************************************************************************/ | |
5163 | ||
5164 | static void *lru_gen_seq_start(struct seq_file *m, loff_t *pos) | |
5165 | { | |
5166 | struct mem_cgroup *memcg; | |
5167 | loff_t nr_to_skip = *pos; | |
5168 | ||
5169 | m->private = kvmalloc(PATH_MAX, GFP_KERNEL); | |
5170 | if (!m->private) | |
5171 | return ERR_PTR(-ENOMEM); | |
5172 | ||
5173 | memcg = mem_cgroup_iter(NULL, NULL, NULL); | |
5174 | do { | |
5175 | int nid; | |
5176 | ||
5177 | for_each_node_state(nid, N_MEMORY) { | |
5178 | if (!nr_to_skip--) | |
5179 | return get_lruvec(memcg, nid); | |
5180 | } | |
5181 | } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); | |
5182 | ||
5183 | return NULL; | |
5184 | } | |
5185 | ||
5186 | static void lru_gen_seq_stop(struct seq_file *m, void *v) | |
5187 | { | |
5188 | if (!IS_ERR_OR_NULL(v)) | |
5189 | mem_cgroup_iter_break(NULL, lruvec_memcg(v)); | |
5190 | ||
5191 | kvfree(m->private); | |
5192 | m->private = NULL; | |
5193 | } | |
5194 | ||
5195 | static void *lru_gen_seq_next(struct seq_file *m, void *v, loff_t *pos) | |
5196 | { | |
5197 | int nid = lruvec_pgdat(v)->node_id; | |
5198 | struct mem_cgroup *memcg = lruvec_memcg(v); | |
5199 | ||
5200 | ++*pos; | |
5201 | ||
5202 | nid = next_memory_node(nid); | |
5203 | if (nid == MAX_NUMNODES) { | |
5204 | memcg = mem_cgroup_iter(NULL, memcg, NULL); | |
5205 | if (!memcg) | |
5206 | return NULL; | |
5207 | ||
5208 | nid = first_memory_node; | |
5209 | } | |
5210 | ||
5211 | return get_lruvec(memcg, nid); | |
5212 | } | |
5213 | ||
5214 | static void lru_gen_seq_show_full(struct seq_file *m, struct lruvec *lruvec, | |
5215 | unsigned long max_seq, unsigned long *min_seq, | |
5216 | unsigned long seq) | |
5217 | { | |
5218 | int i; | |
5219 | int type, tier; | |
5220 | int hist = lru_hist_from_seq(seq); | |
391655fe | 5221 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
d6c3af7d YZ |
5222 | |
5223 | for (tier = 0; tier < MAX_NR_TIERS; tier++) { | |
5224 | seq_printf(m, " %10d", tier); | |
5225 | for (type = 0; type < ANON_AND_FILE; type++) { | |
5226 | const char *s = " "; | |
5227 | unsigned long n[3] = {}; | |
5228 | ||
5229 | if (seq == max_seq) { | |
5230 | s = "RT "; | |
5231 | n[0] = READ_ONCE(lrugen->avg_refaulted[type][tier]); | |
5232 | n[1] = READ_ONCE(lrugen->avg_total[type][tier]); | |
5233 | } else if (seq == min_seq[type] || NR_HIST_GENS > 1) { | |
5234 | s = "rep"; | |
5235 | n[0] = atomic_long_read(&lrugen->refaulted[hist][type][tier]); | |
5236 | n[1] = atomic_long_read(&lrugen->evicted[hist][type][tier]); | |
5237 | if (tier) | |
5238 | n[2] = READ_ONCE(lrugen->protected[hist][type][tier - 1]); | |
5239 | } | |
5240 | ||
5241 | for (i = 0; i < 3; i++) | |
5242 | seq_printf(m, " %10lu%c", n[i], s[i]); | |
5243 | } | |
5244 | seq_putc(m, '\n'); | |
5245 | } | |
5246 | ||
5247 | seq_puts(m, " "); | |
5248 | for (i = 0; i < NR_MM_STATS; i++) { | |
5249 | const char *s = " "; | |
5250 | unsigned long n = 0; | |
5251 | ||
5252 | if (seq == max_seq && NR_HIST_GENS == 1) { | |
5253 | s = "LOYNFA"; | |
5254 | n = READ_ONCE(lruvec->mm_state.stats[hist][i]); | |
5255 | } else if (seq != max_seq && NR_HIST_GENS > 1) { | |
5256 | s = "loynfa"; | |
5257 | n = READ_ONCE(lruvec->mm_state.stats[hist][i]); | |
5258 | } | |
5259 | ||
5260 | seq_printf(m, " %10lu%c", n, s[i]); | |
5261 | } | |
5262 | seq_putc(m, '\n'); | |
5263 | } | |
5264 | ||
07017acb | 5265 | /* see Documentation/admin-guide/mm/multigen_lru.rst for details */ |
d6c3af7d YZ |
5266 | static int lru_gen_seq_show(struct seq_file *m, void *v) |
5267 | { | |
5268 | unsigned long seq; | |
5269 | bool full = !debugfs_real_fops(m->file)->write; | |
5270 | struct lruvec *lruvec = v; | |
391655fe | 5271 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
d6c3af7d YZ |
5272 | int nid = lruvec_pgdat(lruvec)->node_id; |
5273 | struct mem_cgroup *memcg = lruvec_memcg(lruvec); | |
5274 | DEFINE_MAX_SEQ(lruvec); | |
5275 | DEFINE_MIN_SEQ(lruvec); | |
5276 | ||
5277 | if (nid == first_memory_node) { | |
5278 | const char *path = memcg ? m->private : ""; | |
5279 | ||
5280 | #ifdef CONFIG_MEMCG | |
5281 | if (memcg) | |
5282 | cgroup_path(memcg->css.cgroup, m->private, PATH_MAX); | |
5283 | #endif | |
5284 | seq_printf(m, "memcg %5hu %s\n", mem_cgroup_id(memcg), path); | |
5285 | } | |
5286 | ||
5287 | seq_printf(m, " node %5d\n", nid); | |
5288 | ||
5289 | if (!full) | |
5290 | seq = min_seq[LRU_GEN_ANON]; | |
5291 | else if (max_seq >= MAX_NR_GENS) | |
5292 | seq = max_seq - MAX_NR_GENS + 1; | |
5293 | else | |
5294 | seq = 0; | |
5295 | ||
5296 | for (; seq <= max_seq; seq++) { | |
5297 | int type, zone; | |
5298 | int gen = lru_gen_from_seq(seq); | |
5299 | unsigned long birth = READ_ONCE(lruvec->lrugen.timestamps[gen]); | |
5300 | ||
5301 | seq_printf(m, " %10lu %10u", seq, jiffies_to_msecs(jiffies - birth)); | |
5302 | ||
5303 | for (type = 0; type < ANON_AND_FILE; type++) { | |
5304 | unsigned long size = 0; | |
5305 | char mark = full && seq < min_seq[type] ? 'x' : ' '; | |
5306 | ||
5307 | for (zone = 0; zone < MAX_NR_ZONES; zone++) | |
5308 | size += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); | |
5309 | ||
5310 | seq_printf(m, " %10lu%c", size, mark); | |
5311 | } | |
5312 | ||
5313 | seq_putc(m, '\n'); | |
5314 | ||
5315 | if (full) | |
5316 | lru_gen_seq_show_full(m, lruvec, max_seq, min_seq, seq); | |
5317 | } | |
5318 | ||
5319 | return 0; | |
5320 | } | |
5321 | ||
5322 | static const struct seq_operations lru_gen_seq_ops = { | |
5323 | .start = lru_gen_seq_start, | |
5324 | .stop = lru_gen_seq_stop, | |
5325 | .next = lru_gen_seq_next, | |
5326 | .show = lru_gen_seq_show, | |
5327 | }; | |
5328 | ||
5329 | static int run_aging(struct lruvec *lruvec, unsigned long seq, struct scan_control *sc, | |
5330 | bool can_swap, bool force_scan) | |
5331 | { | |
5332 | DEFINE_MAX_SEQ(lruvec); | |
5333 | DEFINE_MIN_SEQ(lruvec); | |
5334 | ||
5335 | if (seq < max_seq) | |
5336 | return 0; | |
5337 | ||
5338 | if (seq > max_seq) | |
5339 | return -EINVAL; | |
5340 | ||
5341 | if (!force_scan && min_seq[!can_swap] + MAX_NR_GENS - 1 <= max_seq) | |
5342 | return -ERANGE; | |
5343 | ||
5344 | try_to_inc_max_seq(lruvec, max_seq, sc, can_swap, force_scan); | |
5345 | ||
5346 | return 0; | |
5347 | } | |
5348 | ||
5349 | static int run_eviction(struct lruvec *lruvec, unsigned long seq, struct scan_control *sc, | |
5350 | int swappiness, unsigned long nr_to_reclaim) | |
5351 | { | |
5352 | DEFINE_MAX_SEQ(lruvec); | |
5353 | ||
5354 | if (seq + MIN_NR_GENS > max_seq) | |
5355 | return -EINVAL; | |
5356 | ||
5357 | sc->nr_reclaimed = 0; | |
5358 | ||
5359 | while (!signal_pending(current)) { | |
5360 | DEFINE_MIN_SEQ(lruvec); | |
5361 | ||
5362 | if (seq < min_seq[!swappiness]) | |
5363 | return 0; | |
5364 | ||
5365 | if (sc->nr_reclaimed >= nr_to_reclaim) | |
5366 | return 0; | |
5367 | ||
a579086c | 5368 | if (!evict_folios(lruvec, sc, swappiness)) |
d6c3af7d YZ |
5369 | return 0; |
5370 | ||
5371 | cond_resched(); | |
5372 | } | |
5373 | ||
5374 | return -EINTR; | |
5375 | } | |
5376 | ||
5377 | static int run_cmd(char cmd, int memcg_id, int nid, unsigned long seq, | |
5378 | struct scan_control *sc, int swappiness, unsigned long opt) | |
5379 | { | |
5380 | struct lruvec *lruvec; | |
5381 | int err = -EINVAL; | |
5382 | struct mem_cgroup *memcg = NULL; | |
5383 | ||
5384 | if (nid < 0 || nid >= MAX_NUMNODES || !node_state(nid, N_MEMORY)) | |
5385 | return -EINVAL; | |
5386 | ||
5387 | if (!mem_cgroup_disabled()) { | |
5388 | rcu_read_lock(); | |
e4dde56c | 5389 | |
d6c3af7d | 5390 | memcg = mem_cgroup_from_id(memcg_id); |
e4dde56c | 5391 | if (!mem_cgroup_tryget(memcg)) |
d6c3af7d | 5392 | memcg = NULL; |
e4dde56c | 5393 | |
d6c3af7d YZ |
5394 | rcu_read_unlock(); |
5395 | ||
5396 | if (!memcg) | |
5397 | return -EINVAL; | |
5398 | } | |
5399 | ||
5400 | if (memcg_id != mem_cgroup_id(memcg)) | |
5401 | goto done; | |
5402 | ||
5403 | lruvec = get_lruvec(memcg, nid); | |
5404 | ||
5405 | if (swappiness < 0) | |
5406 | swappiness = get_swappiness(lruvec, sc); | |
5407 | else if (swappiness > 200) | |
5408 | goto done; | |
5409 | ||
5410 | switch (cmd) { | |
5411 | case '+': | |
5412 | err = run_aging(lruvec, seq, sc, swappiness, opt); | |
5413 | break; | |
5414 | case '-': | |
5415 | err = run_eviction(lruvec, seq, sc, swappiness, opt); | |
5416 | break; | |
5417 | } | |
5418 | done: | |
5419 | mem_cgroup_put(memcg); | |
5420 | ||
5421 | return err; | |
5422 | } | |
5423 | ||
07017acb | 5424 | /* see Documentation/admin-guide/mm/multigen_lru.rst for details */ |
d6c3af7d YZ |
5425 | static ssize_t lru_gen_seq_write(struct file *file, const char __user *src, |
5426 | size_t len, loff_t *pos) | |
5427 | { | |
5428 | void *buf; | |
5429 | char *cur, *next; | |
5430 | unsigned int flags; | |
5431 | struct blk_plug plug; | |
5432 | int err = -EINVAL; | |
5433 | struct scan_control sc = { | |
5434 | .may_writepage = true, | |
5435 | .may_unmap = true, | |
5436 | .may_swap = true, | |
5437 | .reclaim_idx = MAX_NR_ZONES - 1, | |
5438 | .gfp_mask = GFP_KERNEL, | |
5439 | }; | |
5440 | ||
5441 | buf = kvmalloc(len + 1, GFP_KERNEL); | |
5442 | if (!buf) | |
5443 | return -ENOMEM; | |
5444 | ||
5445 | if (copy_from_user(buf, src, len)) { | |
5446 | kvfree(buf); | |
5447 | return -EFAULT; | |
5448 | } | |
5449 | ||
5450 | set_task_reclaim_state(current, &sc.reclaim_state); | |
5451 | flags = memalloc_noreclaim_save(); | |
5452 | blk_start_plug(&plug); | |
e9d4e1ee | 5453 | if (!set_mm_walk(NULL, true)) { |
d6c3af7d YZ |
5454 | err = -ENOMEM; |
5455 | goto done; | |
5456 | } | |
5457 | ||
5458 | next = buf; | |
5459 | next[len] = '\0'; | |
5460 | ||
5461 | while ((cur = strsep(&next, ",;\n"))) { | |
5462 | int n; | |
5463 | int end; | |
5464 | char cmd; | |
5465 | unsigned int memcg_id; | |
5466 | unsigned int nid; | |
5467 | unsigned long seq; | |
5468 | unsigned int swappiness = -1; | |
5469 | unsigned long opt = -1; | |
5470 | ||
5471 | cur = skip_spaces(cur); | |
5472 | if (!*cur) | |
5473 | continue; | |
5474 | ||
5475 | n = sscanf(cur, "%c %u %u %lu %n %u %n %lu %n", &cmd, &memcg_id, &nid, | |
5476 | &seq, &end, &swappiness, &end, &opt, &end); | |
5477 | if (n < 4 || cur[end]) { | |
5478 | err = -EINVAL; | |
5479 | break; | |
5480 | } | |
5481 | ||
5482 | err = run_cmd(cmd, memcg_id, nid, seq, &sc, swappiness, opt); | |
5483 | if (err) | |
5484 | break; | |
5485 | } | |
5486 | done: | |
5487 | clear_mm_walk(); | |
5488 | blk_finish_plug(&plug); | |
5489 | memalloc_noreclaim_restore(flags); | |
5490 | set_task_reclaim_state(current, NULL); | |
5491 | ||
5492 | kvfree(buf); | |
5493 | ||
5494 | return err ? : len; | |
5495 | } | |
5496 | ||
5497 | static int lru_gen_seq_open(struct inode *inode, struct file *file) | |
5498 | { | |
5499 | return seq_open(file, &lru_gen_seq_ops); | |
5500 | } | |
5501 | ||
5502 | static const struct file_operations lru_gen_rw_fops = { | |
5503 | .open = lru_gen_seq_open, | |
5504 | .read = seq_read, | |
5505 | .write = lru_gen_seq_write, | |
5506 | .llseek = seq_lseek, | |
5507 | .release = seq_release, | |
5508 | }; | |
5509 | ||
5510 | static const struct file_operations lru_gen_ro_fops = { | |
5511 | .open = lru_gen_seq_open, | |
5512 | .read = seq_read, | |
5513 | .llseek = seq_lseek, | |
5514 | .release = seq_release, | |
5515 | }; | |
5516 | ||
ec1c86b2 YZ |
5517 | /****************************************************************************** |
5518 | * initialization | |
5519 | ******************************************************************************/ | |
5520 | ||
5521 | void lru_gen_init_lruvec(struct lruvec *lruvec) | |
5522 | { | |
1332a809 | 5523 | int i; |
ec1c86b2 | 5524 | int gen, type, zone; |
391655fe | 5525 | struct lru_gen_folio *lrugen = &lruvec->lrugen; |
ec1c86b2 YZ |
5526 | |
5527 | lrugen->max_seq = MIN_NR_GENS + 1; | |
354ed597 | 5528 | lrugen->enabled = lru_gen_enabled(); |
ec1c86b2 | 5529 | |
1332a809 YZ |
5530 | for (i = 0; i <= MIN_NR_GENS + 1; i++) |
5531 | lrugen->timestamps[i] = jiffies; | |
5532 | ||
ec1c86b2 | 5533 | for_each_gen_type_zone(gen, type, zone) |
6df1b221 | 5534 | INIT_LIST_HEAD(&lrugen->folios[gen][type][zone]); |
bd74fdae YZ |
5535 | |
5536 | lruvec->mm_state.seq = MIN_NR_GENS; | |
ec1c86b2 YZ |
5537 | } |
5538 | ||
5539 | #ifdef CONFIG_MEMCG | |
e4dde56c YZ |
5540 | |
5541 | void lru_gen_init_pgdat(struct pglist_data *pgdat) | |
5542 | { | |
5543 | int i, j; | |
5544 | ||
5545 | spin_lock_init(&pgdat->memcg_lru.lock); | |
5546 | ||
5547 | for (i = 0; i < MEMCG_NR_GENS; i++) { | |
5548 | for (j = 0; j < MEMCG_NR_BINS; j++) | |
5549 | INIT_HLIST_NULLS_HEAD(&pgdat->memcg_lru.fifo[i][j], i); | |
5550 | } | |
5551 | } | |
5552 | ||
ec1c86b2 YZ |
5553 | void lru_gen_init_memcg(struct mem_cgroup *memcg) |
5554 | { | |
bd74fdae YZ |
5555 | INIT_LIST_HEAD(&memcg->mm_list.fifo); |
5556 | spin_lock_init(&memcg->mm_list.lock); | |
ec1c86b2 YZ |
5557 | } |
5558 | ||
5559 | void lru_gen_exit_memcg(struct mem_cgroup *memcg) | |
5560 | { | |
bd74fdae | 5561 | int i; |
ec1c86b2 YZ |
5562 | int nid; |
5563 | ||
37cc9997 A |
5564 | VM_WARN_ON_ONCE(!list_empty(&memcg->mm_list.fifo)); |
5565 | ||
ec1c86b2 YZ |
5566 | for_each_node(nid) { |
5567 | struct lruvec *lruvec = get_lruvec(memcg, nid); | |
5568 | ||
5569 | VM_WARN_ON_ONCE(memchr_inv(lruvec->lrugen.nr_pages, 0, | |
5570 | sizeof(lruvec->lrugen.nr_pages))); | |
bd74fdae | 5571 | |
37cc9997 A |
5572 | lruvec->lrugen.list.next = LIST_POISON1; |
5573 | ||
bd74fdae YZ |
5574 | for (i = 0; i < NR_BLOOM_FILTERS; i++) { |
5575 | bitmap_free(lruvec->mm_state.filters[i]); | |
5576 | lruvec->mm_state.filters[i] = NULL; | |
5577 | } | |
ec1c86b2 YZ |
5578 | } |
5579 | } | |
e4dde56c | 5580 | |
e4dde56c | 5581 | #endif /* CONFIG_MEMCG */ |
ec1c86b2 YZ |
5582 | |
5583 | static int __init init_lru_gen(void) | |
5584 | { | |
5585 | BUILD_BUG_ON(MIN_NR_GENS + 1 >= MAX_NR_GENS); | |
5586 | BUILD_BUG_ON(BIT(LRU_GEN_WIDTH) <= MAX_NR_GENS); | |
5587 | ||
354ed597 YZ |
5588 | if (sysfs_create_group(mm_kobj, &lru_gen_attr_group)) |
5589 | pr_err("lru_gen: failed to create sysfs group\n"); | |
5590 | ||
d6c3af7d YZ |
5591 | debugfs_create_file("lru_gen", 0644, NULL, NULL, &lru_gen_rw_fops); |
5592 | debugfs_create_file("lru_gen_full", 0444, NULL, NULL, &lru_gen_ro_fops); | |
5593 | ||
ec1c86b2 YZ |
5594 | return 0; |
5595 | }; | |
5596 | late_initcall(init_lru_gen); | |
5597 | ||
ac35a490 YZ |
5598 | #else /* !CONFIG_LRU_GEN */ |
5599 | ||
5600 | static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc) | |
5601 | { | |
5602 | } | |
5603 | ||
5604 | static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) | |
5605 | { | |
5606 | } | |
5607 | ||
e4dde56c YZ |
5608 | static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *sc) |
5609 | { | |
5610 | } | |
5611 | ||
ec1c86b2 YZ |
5612 | #endif /* CONFIG_LRU_GEN */ |
5613 | ||
afaf07a6 | 5614 | static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) |
9b4f98cd JW |
5615 | { |
5616 | unsigned long nr[NR_LRU_LISTS]; | |
e82e0561 | 5617 | unsigned long targets[NR_LRU_LISTS]; |
9b4f98cd JW |
5618 | unsigned long nr_to_scan; |
5619 | enum lru_list lru; | |
5620 | unsigned long nr_reclaimed = 0; | |
5621 | unsigned long nr_to_reclaim = sc->nr_to_reclaim; | |
f53af428 | 5622 | bool proportional_reclaim; |
9b4f98cd JW |
5623 | struct blk_plug plug; |
5624 | ||
7a704474 | 5625 | if (lru_gen_enabled() && !root_reclaim(sc)) { |
ac35a490 YZ |
5626 | lru_gen_shrink_lruvec(lruvec, sc); |
5627 | return; | |
5628 | } | |
5629 | ||
afaf07a6 | 5630 | get_scan_count(lruvec, sc, nr); |
9b4f98cd | 5631 | |
e82e0561 MG |
5632 | /* Record the original scan target for proportional adjustments later */ |
5633 | memcpy(targets, nr, sizeof(nr)); | |
5634 | ||
1a501907 MG |
5635 | /* |
5636 | * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal | |
5637 | * event that can occur when there is little memory pressure e.g. | |
5638 | * multiple streaming readers/writers. Hence, we do not abort scanning | |
5639 | * when the requested number of pages are reclaimed when scanning at | |
5640 | * DEF_PRIORITY on the assumption that the fact we are direct | |
5641 | * reclaiming implies that kswapd is not keeping up and it is best to | |
5642 | * do a batch of work at once. For memcg reclaim one check is made to | |
5643 | * abort proportional reclaim if either the file or anon lru has already | |
5644 | * dropped to zero at the first pass. | |
5645 | */ | |
f53af428 JW |
5646 | proportional_reclaim = (!cgroup_reclaim(sc) && !current_is_kswapd() && |
5647 | sc->priority == DEF_PRIORITY); | |
1a501907 | 5648 | |
9b4f98cd JW |
5649 | blk_start_plug(&plug); |
5650 | while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || | |
5651 | nr[LRU_INACTIVE_FILE]) { | |
e82e0561 MG |
5652 | unsigned long nr_anon, nr_file, percentage; |
5653 | unsigned long nr_scanned; | |
5654 | ||
9b4f98cd JW |
5655 | for_each_evictable_lru(lru) { |
5656 | if (nr[lru]) { | |
5657 | nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); | |
5658 | nr[lru] -= nr_to_scan; | |
5659 | ||
5660 | nr_reclaimed += shrink_list(lru, nr_to_scan, | |
3b991208 | 5661 | lruvec, sc); |
9b4f98cd JW |
5662 | } |
5663 | } | |
e82e0561 | 5664 | |
bd041733 MH |
5665 | cond_resched(); |
5666 | ||
f53af428 | 5667 | if (nr_reclaimed < nr_to_reclaim || proportional_reclaim) |
e82e0561 MG |
5668 | continue; |
5669 | ||
e82e0561 MG |
5670 | /* |
5671 | * For kswapd and memcg, reclaim at least the number of pages | |
1a501907 | 5672 | * requested. Ensure that the anon and file LRUs are scanned |
e82e0561 MG |
5673 | * proportionally what was requested by get_scan_count(). We |
5674 | * stop reclaiming one LRU and reduce the amount scanning | |
5675 | * proportional to the original scan target. | |
5676 | */ | |
5677 | nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; | |
5678 | nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; | |
5679 | ||
1a501907 MG |
5680 | /* |
5681 | * It's just vindictive to attack the larger once the smaller | |
5682 | * has gone to zero. And given the way we stop scanning the | |
5683 | * smaller below, this makes sure that we only make one nudge | |
5684 | * towards proportionality once we've got nr_to_reclaim. | |
5685 | */ | |
5686 | if (!nr_file || !nr_anon) | |
5687 | break; | |
5688 | ||
e82e0561 MG |
5689 | if (nr_file > nr_anon) { |
5690 | unsigned long scan_target = targets[LRU_INACTIVE_ANON] + | |
5691 | targets[LRU_ACTIVE_ANON] + 1; | |
5692 | lru = LRU_BASE; | |
5693 | percentage = nr_anon * 100 / scan_target; | |
5694 | } else { | |
5695 | unsigned long scan_target = targets[LRU_INACTIVE_FILE] + | |
5696 | targets[LRU_ACTIVE_FILE] + 1; | |
5697 | lru = LRU_FILE; | |
5698 | percentage = nr_file * 100 / scan_target; | |
5699 | } | |
5700 | ||
5701 | /* Stop scanning the smaller of the LRU */ | |
5702 | nr[lru] = 0; | |
5703 | nr[lru + LRU_ACTIVE] = 0; | |
5704 | ||
5705 | /* | |
5706 | * Recalculate the other LRU scan count based on its original | |
5707 | * scan target and the percentage scanning already complete | |
5708 | */ | |
5709 | lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; | |
5710 | nr_scanned = targets[lru] - nr[lru]; | |
5711 | nr[lru] = targets[lru] * (100 - percentage) / 100; | |
5712 | nr[lru] -= min(nr[lru], nr_scanned); | |
5713 | ||
5714 | lru += LRU_ACTIVE; | |
5715 | nr_scanned = targets[lru] - nr[lru]; | |
5716 | nr[lru] = targets[lru] * (100 - percentage) / 100; | |
5717 | nr[lru] -= min(nr[lru], nr_scanned); | |
9b4f98cd JW |
5718 | } |
5719 | blk_finish_plug(&plug); | |
5720 | sc->nr_reclaimed += nr_reclaimed; | |
5721 | ||
5722 | /* | |
5723 | * Even if we did not try to evict anon pages at all, we want to | |
5724 | * rebalance the anon lru active/inactive ratio. | |
5725 | */ | |
2f368a9f DH |
5726 | if (can_age_anon_pages(lruvec_pgdat(lruvec), sc) && |
5727 | inactive_is_low(lruvec, LRU_INACTIVE_ANON)) | |
9b4f98cd JW |
5728 | shrink_active_list(SWAP_CLUSTER_MAX, lruvec, |
5729 | sc, LRU_ACTIVE_ANON); | |
9b4f98cd JW |
5730 | } |
5731 | ||
23b9da55 | 5732 | /* Use reclaim/compaction for costly allocs or under memory pressure */ |
9e3b2f8c | 5733 | static bool in_reclaim_compaction(struct scan_control *sc) |
23b9da55 | 5734 | { |
d84da3f9 | 5735 | if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && |
23b9da55 | 5736 | (sc->order > PAGE_ALLOC_COSTLY_ORDER || |
9e3b2f8c | 5737 | sc->priority < DEF_PRIORITY - 2)) |
23b9da55 MG |
5738 | return true; |
5739 | ||
5740 | return false; | |
5741 | } | |
5742 | ||
3e7d3449 | 5743 | /* |
23b9da55 MG |
5744 | * Reclaim/compaction is used for high-order allocation requests. It reclaims |
5745 | * order-0 pages before compacting the zone. should_continue_reclaim() returns | |
5746 | * true if more pages should be reclaimed such that when the page allocator | |
df3a45f9 | 5747 | * calls try_to_compact_pages() that it will have enough free pages to succeed. |
23b9da55 | 5748 | * It will give up earlier than that if there is difficulty reclaiming pages. |
3e7d3449 | 5749 | */ |
a9dd0a83 | 5750 | static inline bool should_continue_reclaim(struct pglist_data *pgdat, |
3e7d3449 | 5751 | unsigned long nr_reclaimed, |
3e7d3449 MG |
5752 | struct scan_control *sc) |
5753 | { | |
5754 | unsigned long pages_for_compaction; | |
5755 | unsigned long inactive_lru_pages; | |
a9dd0a83 | 5756 | int z; |
3e7d3449 MG |
5757 | |
5758 | /* If not in reclaim/compaction mode, stop */ | |
9e3b2f8c | 5759 | if (!in_reclaim_compaction(sc)) |
3e7d3449 MG |
5760 | return false; |
5761 | ||
5ee04716 VB |
5762 | /* |
5763 | * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX | |
5764 | * number of pages that were scanned. This will return to the caller | |
5765 | * with the risk reclaim/compaction and the resulting allocation attempt | |
5766 | * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL | |
5767 | * allocations through requiring that the full LRU list has been scanned | |
5768 | * first, by assuming that zero delta of sc->nr_scanned means full LRU | |
5769 | * scan, but that approximation was wrong, and there were corner cases | |
5770 | * where always a non-zero amount of pages were scanned. | |
5771 | */ | |
5772 | if (!nr_reclaimed) | |
5773 | return false; | |
3e7d3449 | 5774 | |
3e7d3449 | 5775 | /* If compaction would go ahead or the allocation would succeed, stop */ |
a9dd0a83 MG |
5776 | for (z = 0; z <= sc->reclaim_idx; z++) { |
5777 | struct zone *zone = &pgdat->node_zones[z]; | |
6aa303de | 5778 | if (!managed_zone(zone)) |
a9dd0a83 MG |
5779 | continue; |
5780 | ||
e8606320 JW |
5781 | /* Allocation can already succeed, nothing to do */ |
5782 | if (zone_watermark_ok(zone, sc->order, min_wmark_pages(zone), | |
5783 | sc->reclaim_idx, 0)) | |
5784 | return false; | |
5785 | ||
3cf04937 | 5786 | if (compaction_suitable(zone, sc->order, sc->reclaim_idx)) |
a9dd0a83 | 5787 | return false; |
3e7d3449 | 5788 | } |
1c6c1597 HD |
5789 | |
5790 | /* | |
5791 | * If we have not reclaimed enough pages for compaction and the | |
5792 | * inactive lists are large enough, continue reclaiming | |
5793 | */ | |
5794 | pages_for_compaction = compact_gap(sc->order); | |
5795 | inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); | |
a2a36488 | 5796 | if (can_reclaim_anon_pages(NULL, pgdat->node_id, sc)) |
1c6c1597 HD |
5797 | inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); |
5798 | ||
5ee04716 | 5799 | return inactive_lru_pages > pages_for_compaction; |
3e7d3449 MG |
5800 | } |
5801 | ||
0f6a5cff | 5802 | static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) |
1da177e4 | 5803 | { |
0f6a5cff | 5804 | struct mem_cgroup *target_memcg = sc->target_mem_cgroup; |
d2af3397 | 5805 | struct mem_cgroup *memcg; |
1da177e4 | 5806 | |
0f6a5cff | 5807 | memcg = mem_cgroup_iter(target_memcg, NULL, NULL); |
d2af3397 | 5808 | do { |
afaf07a6 | 5809 | struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); |
d2af3397 JW |
5810 | unsigned long reclaimed; |
5811 | unsigned long scanned; | |
5660048c | 5812 | |
e3336cab XP |
5813 | /* |
5814 | * This loop can become CPU-bound when target memcgs | |
5815 | * aren't eligible for reclaim - either because they | |
5816 | * don't have any reclaimable pages, or because their | |
5817 | * memory is explicitly protected. Avoid soft lockups. | |
5818 | */ | |
5819 | cond_resched(); | |
5820 | ||
45c7f7e1 CD |
5821 | mem_cgroup_calculate_protection(target_memcg, memcg); |
5822 | ||
adb82130 | 5823 | if (mem_cgroup_below_min(target_memcg, memcg)) { |
d2af3397 JW |
5824 | /* |
5825 | * Hard protection. | |
5826 | * If there is no reclaimable memory, OOM. | |
5827 | */ | |
5828 | continue; | |
adb82130 | 5829 | } else if (mem_cgroup_below_low(target_memcg, memcg)) { |
d2af3397 JW |
5830 | /* |
5831 | * Soft protection. | |
5832 | * Respect the protection only as long as | |
5833 | * there is an unprotected supply | |
5834 | * of reclaimable memory from other cgroups. | |
5835 | */ | |
5836 | if (!sc->memcg_low_reclaim) { | |
5837 | sc->memcg_low_skipped = 1; | |
bf8d5d52 | 5838 | continue; |
241994ed | 5839 | } |
d2af3397 | 5840 | memcg_memory_event(memcg, MEMCG_LOW); |
d2af3397 | 5841 | } |
241994ed | 5842 | |
d2af3397 JW |
5843 | reclaimed = sc->nr_reclaimed; |
5844 | scanned = sc->nr_scanned; | |
afaf07a6 JW |
5845 | |
5846 | shrink_lruvec(lruvec, sc); | |
70ddf637 | 5847 | |
d2af3397 JW |
5848 | shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, |
5849 | sc->priority); | |
6b4f7799 | 5850 | |
d2af3397 | 5851 | /* Record the group's reclaim efficiency */ |
73b73bac YA |
5852 | if (!sc->proactive) |
5853 | vmpressure(sc->gfp_mask, memcg, false, | |
5854 | sc->nr_scanned - scanned, | |
5855 | sc->nr_reclaimed - reclaimed); | |
70ddf637 | 5856 | |
0f6a5cff JW |
5857 | } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL))); |
5858 | } | |
5859 | ||
6c9e0907 | 5860 | static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) |
0f6a5cff | 5861 | { |
54c4fe08 | 5862 | unsigned long nr_reclaimed, nr_scanned, nr_node_reclaimed; |
1b05117d | 5863 | struct lruvec *target_lruvec; |
0f6a5cff JW |
5864 | bool reclaimable = false; |
5865 | ||
7a704474 | 5866 | if (lru_gen_enabled() && root_reclaim(sc)) { |
e4dde56c YZ |
5867 | lru_gen_shrink_node(pgdat, sc); |
5868 | return; | |
5869 | } | |
5870 | ||
1b05117d JW |
5871 | target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); |
5872 | ||
0f6a5cff JW |
5873 | again: |
5874 | memset(&sc->nr, 0, sizeof(sc->nr)); | |
5875 | ||
5876 | nr_reclaimed = sc->nr_reclaimed; | |
5877 | nr_scanned = sc->nr_scanned; | |
5878 | ||
ed547ab6 | 5879 | prepare_scan_control(pgdat, sc); |
53138cea | 5880 | |
0f6a5cff | 5881 | shrink_node_memcgs(pgdat, sc); |
2344d7e4 | 5882 | |
583c27a1 | 5883 | flush_reclaim_state(sc); |
d108c772 | 5884 | |
54c4fe08 | 5885 | nr_node_reclaimed = sc->nr_reclaimed - nr_reclaimed; |
d108c772 | 5886 | |
d2af3397 | 5887 | /* Record the subtree's reclaim efficiency */ |
73b73bac YA |
5888 | if (!sc->proactive) |
5889 | vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, | |
54c4fe08 | 5890 | sc->nr_scanned - nr_scanned, nr_node_reclaimed); |
d108c772 | 5891 | |
54c4fe08 | 5892 | if (nr_node_reclaimed) |
d2af3397 | 5893 | reclaimable = true; |
d108c772 | 5894 | |
d2af3397 JW |
5895 | if (current_is_kswapd()) { |
5896 | /* | |
5897 | * If reclaim is isolating dirty pages under writeback, | |
5898 | * it implies that the long-lived page allocation rate | |
5899 | * is exceeding the page laundering rate. Either the | |
5900 | * global limits are not being effective at throttling | |
5901 | * processes due to the page distribution throughout | |
5902 | * zones or there is heavy usage of a slow backing | |
5903 | * device. The only option is to throttle from reclaim | |
5904 | * context which is not ideal as there is no guarantee | |
5905 | * the dirtying process is throttled in the same way | |
5906 | * balance_dirty_pages() manages. | |
5907 | * | |
5908 | * Once a node is flagged PGDAT_WRITEBACK, kswapd will | |
5909 | * count the number of pages under pages flagged for | |
5910 | * immediate reclaim and stall if any are encountered | |
5911 | * in the nr_immediate check below. | |
5912 | */ | |
5913 | if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) | |
5914 | set_bit(PGDAT_WRITEBACK, &pgdat->flags); | |
d108c772 | 5915 | |
d2af3397 JW |
5916 | /* Allow kswapd to start writing pages during reclaim.*/ |
5917 | if (sc->nr.unqueued_dirty == sc->nr.file_taken) | |
5918 | set_bit(PGDAT_DIRTY, &pgdat->flags); | |
e3c1ac58 | 5919 | |
d108c772 | 5920 | /* |
1eba09c1 | 5921 | * If kswapd scans pages marked for immediate |
d2af3397 JW |
5922 | * reclaim and under writeback (nr_immediate), it |
5923 | * implies that pages are cycling through the LRU | |
8cd7c588 MG |
5924 | * faster than they are written so forcibly stall |
5925 | * until some pages complete writeback. | |
d108c772 | 5926 | */ |
d2af3397 | 5927 | if (sc->nr.immediate) |
c3f4a9a2 | 5928 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK); |
d2af3397 JW |
5929 | } |
5930 | ||
5931 | /* | |
8cd7c588 MG |
5932 | * Tag a node/memcg as congested if all the dirty pages were marked |
5933 | * for writeback and immediate reclaim (counted in nr.congested). | |
1b05117d | 5934 | * |
d2af3397 | 5935 | * Legacy memcg will stall in page writeback so avoid forcibly |
8cd7c588 | 5936 | * stalling in reclaim_throttle(). |
d2af3397 | 5937 | */ |
1bc545bf YA |
5938 | if (sc->nr.dirty && sc->nr.dirty == sc->nr.congested) { |
5939 | if (cgroup_reclaim(sc) && writeback_throttling_sane(sc)) | |
5940 | set_bit(LRUVEC_CGROUP_CONGESTED, &target_lruvec->flags); | |
5941 | ||
5942 | if (current_is_kswapd()) | |
5943 | set_bit(LRUVEC_NODE_CONGESTED, &target_lruvec->flags); | |
5944 | } | |
d2af3397 JW |
5945 | |
5946 | /* | |
8cd7c588 MG |
5947 | * Stall direct reclaim for IO completions if the lruvec is |
5948 | * node is congested. Allow kswapd to continue until it | |
d2af3397 JW |
5949 | * starts encountering unqueued dirty pages or cycling through |
5950 | * the LRU too quickly. | |
5951 | */ | |
1b05117d JW |
5952 | if (!current_is_kswapd() && current_may_throttle() && |
5953 | !sc->hibernation_mode && | |
1bc545bf YA |
5954 | (test_bit(LRUVEC_CGROUP_CONGESTED, &target_lruvec->flags) || |
5955 | test_bit(LRUVEC_NODE_CONGESTED, &target_lruvec->flags))) | |
1b4e3f26 | 5956 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_CONGESTED); |
d108c772 | 5957 | |
54c4fe08 | 5958 | if (should_continue_reclaim(pgdat, nr_node_reclaimed, sc)) |
d2af3397 | 5959 | goto again; |
2344d7e4 | 5960 | |
c73322d0 JW |
5961 | /* |
5962 | * Kswapd gives up on balancing particular nodes after too | |
5963 | * many failures to reclaim anything from them and goes to | |
5964 | * sleep. On reclaim progress, reset the failure counter. A | |
5965 | * successful direct reclaim run will revive a dormant kswapd. | |
5966 | */ | |
5967 | if (reclaimable) | |
5968 | pgdat->kswapd_failures = 0; | |
f16015fb JW |
5969 | } |
5970 | ||
53853e2d | 5971 | /* |
fdd4c614 VB |
5972 | * Returns true if compaction should go ahead for a costly-order request, or |
5973 | * the allocation would already succeed without compaction. Return false if we | |
5974 | * should reclaim first. | |
53853e2d | 5975 | */ |
4f588331 | 5976 | static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) |
fe4b1b24 | 5977 | { |
31483b6a | 5978 | unsigned long watermark; |
fe4b1b24 | 5979 | |
e8606320 JW |
5980 | /* Allocation can already succeed, nothing to do */ |
5981 | if (zone_watermark_ok(zone, sc->order, min_wmark_pages(zone), | |
5982 | sc->reclaim_idx, 0)) | |
fdd4c614 | 5983 | return true; |
fe4b1b24 | 5984 | |
e8606320 | 5985 | /* Compaction cannot yet proceed. Do reclaim. */ |
3cf04937 | 5986 | if (!compaction_suitable(zone, sc->order, sc->reclaim_idx)) |
e8606320 | 5987 | return false; |
f98a497e | 5988 | |
53853e2d | 5989 | /* |
fdd4c614 VB |
5990 | * Compaction is already possible, but it takes time to run and there |
5991 | * are potentially other callers using the pages just freed. So proceed | |
5992 | * with reclaim to make a buffer of free pages available to give | |
5993 | * compaction a reasonable chance of completing and allocating the page. | |
5994 | * Note that we won't actually reclaim the whole buffer in one attempt | |
5995 | * as the target watermark in should_continue_reclaim() is lower. But if | |
5996 | * we are already above the high+gap watermark, don't reclaim at all. | |
53853e2d | 5997 | */ |
fdd4c614 | 5998 | watermark = high_wmark_pages(zone) + compact_gap(sc->order); |
fe4b1b24 | 5999 | |
fdd4c614 | 6000 | return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); |
fe4b1b24 MG |
6001 | } |
6002 | ||
69392a40 MG |
6003 | static void consider_reclaim_throttle(pg_data_t *pgdat, struct scan_control *sc) |
6004 | { | |
66ce520b MG |
6005 | /* |
6006 | * If reclaim is making progress greater than 12% efficiency then | |
6007 | * wake all the NOPROGRESS throttled tasks. | |
6008 | */ | |
6009 | if (sc->nr_reclaimed > (sc->nr_scanned >> 3)) { | |
69392a40 MG |
6010 | wait_queue_head_t *wqh; |
6011 | ||
6012 | wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_NOPROGRESS]; | |
6013 | if (waitqueue_active(wqh)) | |
6014 | wake_up(wqh); | |
6015 | ||
6016 | return; | |
6017 | } | |
6018 | ||
6019 | /* | |
1b4e3f26 MG |
6020 | * Do not throttle kswapd or cgroup reclaim on NOPROGRESS as it will |
6021 | * throttle on VMSCAN_THROTTLE_WRITEBACK if there are too many pages | |
6022 | * under writeback and marked for immediate reclaim at the tail of the | |
6023 | * LRU. | |
69392a40 | 6024 | */ |
1b4e3f26 | 6025 | if (current_is_kswapd() || cgroup_reclaim(sc)) |
69392a40 MG |
6026 | return; |
6027 | ||
6028 | /* Throttle if making no progress at high prioities. */ | |
1b4e3f26 | 6029 | if (sc->priority == 1 && !sc->nr_reclaimed) |
c3f4a9a2 | 6030 | reclaim_throttle(pgdat, VMSCAN_THROTTLE_NOPROGRESS); |
69392a40 MG |
6031 | } |
6032 | ||
1da177e4 LT |
6033 | /* |
6034 | * This is the direct reclaim path, for page-allocating processes. We only | |
6035 | * try to reclaim pages from zones which will satisfy the caller's allocation | |
6036 | * request. | |
6037 | * | |
1da177e4 LT |
6038 | * If a zone is deemed to be full of pinned pages then just give it a light |
6039 | * scan then give up on it. | |
6040 | */ | |
0a0337e0 | 6041 | static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) |
1da177e4 | 6042 | { |
dd1a239f | 6043 | struct zoneref *z; |
54a6eb5c | 6044 | struct zone *zone; |
0608f43d AM |
6045 | unsigned long nr_soft_reclaimed; |
6046 | unsigned long nr_soft_scanned; | |
619d0d76 | 6047 | gfp_t orig_mask; |
79dafcdc | 6048 | pg_data_t *last_pgdat = NULL; |
1b4e3f26 | 6049 | pg_data_t *first_pgdat = NULL; |
1cfb419b | 6050 | |
cc715d99 MG |
6051 | /* |
6052 | * If the number of buffer_heads in the machine exceeds the maximum | |
6053 | * allowed level, force direct reclaim to scan the highmem zone as | |
6054 | * highmem pages could be pinning lowmem pages storing buffer_heads | |
6055 | */ | |
619d0d76 | 6056 | orig_mask = sc->gfp_mask; |
b2e18757 | 6057 | if (buffer_heads_over_limit) { |
cc715d99 | 6058 | sc->gfp_mask |= __GFP_HIGHMEM; |
4f588331 | 6059 | sc->reclaim_idx = gfp_zone(sc->gfp_mask); |
b2e18757 | 6060 | } |
cc715d99 | 6061 | |
d4debc66 | 6062 | for_each_zone_zonelist_nodemask(zone, z, zonelist, |
b2e18757 | 6063 | sc->reclaim_idx, sc->nodemask) { |
1cfb419b KH |
6064 | /* |
6065 | * Take care memory controller reclaiming has small influence | |
6066 | * to global LRU. | |
6067 | */ | |
b5ead35e | 6068 | if (!cgroup_reclaim(sc)) { |
344736f2 VD |
6069 | if (!cpuset_zone_allowed(zone, |
6070 | GFP_KERNEL | __GFP_HARDWALL)) | |
1cfb419b | 6071 | continue; |
65ec02cb | 6072 | |
0b06496a JW |
6073 | /* |
6074 | * If we already have plenty of memory free for | |
6075 | * compaction in this zone, don't free any more. | |
6076 | * Even though compaction is invoked for any | |
6077 | * non-zero order, only frequent costly order | |
6078 | * reclamation is disruptive enough to become a | |
6079 | * noticeable problem, like transparent huge | |
6080 | * page allocations. | |
6081 | */ | |
6082 | if (IS_ENABLED(CONFIG_COMPACTION) && | |
6083 | sc->order > PAGE_ALLOC_COSTLY_ORDER && | |
4f588331 | 6084 | compaction_ready(zone, sc)) { |
0b06496a JW |
6085 | sc->compaction_ready = true; |
6086 | continue; | |
e0887c19 | 6087 | } |
0b06496a | 6088 | |
79dafcdc MG |
6089 | /* |
6090 | * Shrink each node in the zonelist once. If the | |
6091 | * zonelist is ordered by zone (not the default) then a | |
6092 | * node may be shrunk multiple times but in that case | |
6093 | * the user prefers lower zones being preserved. | |
6094 | */ | |
6095 | if (zone->zone_pgdat == last_pgdat) | |
6096 | continue; | |
6097 | ||
0608f43d AM |
6098 | /* |
6099 | * This steals pages from memory cgroups over softlimit | |
6100 | * and returns the number of reclaimed pages and | |
6101 | * scanned pages. This works for global memory pressure | |
6102 | * and balancing, not for a memcg's limit. | |
6103 | */ | |
6104 | nr_soft_scanned = 0; | |
ef8f2327 | 6105 | nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, |
0608f43d AM |
6106 | sc->order, sc->gfp_mask, |
6107 | &nr_soft_scanned); | |
6108 | sc->nr_reclaimed += nr_soft_reclaimed; | |
6109 | sc->nr_scanned += nr_soft_scanned; | |
ac34a1a3 | 6110 | /* need some check for avoid more shrink_zone() */ |
1cfb419b | 6111 | } |
408d8544 | 6112 | |
1b4e3f26 MG |
6113 | if (!first_pgdat) |
6114 | first_pgdat = zone->zone_pgdat; | |
6115 | ||
79dafcdc MG |
6116 | /* See comment about same check for global reclaim above */ |
6117 | if (zone->zone_pgdat == last_pgdat) | |
6118 | continue; | |
6119 | last_pgdat = zone->zone_pgdat; | |
970a39a3 | 6120 | shrink_node(zone->zone_pgdat, sc); |
1da177e4 | 6121 | } |
e0c23279 | 6122 | |
80082938 MG |
6123 | if (first_pgdat) |
6124 | consider_reclaim_throttle(first_pgdat, sc); | |
1b4e3f26 | 6125 | |
619d0d76 WY |
6126 | /* |
6127 | * Restore to original mask to avoid the impact on the caller if we | |
6128 | * promoted it to __GFP_HIGHMEM. | |
6129 | */ | |
6130 | sc->gfp_mask = orig_mask; | |
1da177e4 | 6131 | } |
4f98a2fe | 6132 | |
b910718a | 6133 | static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat) |
2a2e4885 | 6134 | { |
b910718a JW |
6135 | struct lruvec *target_lruvec; |
6136 | unsigned long refaults; | |
2a2e4885 | 6137 | |
ac35a490 YZ |
6138 | if (lru_gen_enabled()) |
6139 | return; | |
6140 | ||
b910718a | 6141 | target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat); |
170b04b7 | 6142 | refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON); |
e9c2dbc8 | 6143 | target_lruvec->refaults[WORKINGSET_ANON] = refaults; |
170b04b7 | 6144 | refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE); |
e9c2dbc8 | 6145 | target_lruvec->refaults[WORKINGSET_FILE] = refaults; |
2a2e4885 JW |
6146 | } |
6147 | ||
1da177e4 LT |
6148 | /* |
6149 | * This is the main entry point to direct page reclaim. | |
6150 | * | |
6151 | * If a full scan of the inactive list fails to free enough memory then we | |
6152 | * are "out of memory" and something needs to be killed. | |
6153 | * | |
6154 | * If the caller is !__GFP_FS then the probability of a failure is reasonably | |
6155 | * high - the zone may be full of dirty or under-writeback pages, which this | |
5b0830cb JA |
6156 | * caller can't do much about. We kick the writeback threads and take explicit |
6157 | * naps in the hope that some of these pages can be written. But if the | |
6158 | * allocating task holds filesystem locks which prevent writeout this might not | |
6159 | * work, and the allocation attempt will fail. | |
a41f24ea NA |
6160 | * |
6161 | * returns: 0, if no pages reclaimed | |
6162 | * else, the number of pages reclaimed | |
1da177e4 | 6163 | */ |
dac1d27b | 6164 | static unsigned long do_try_to_free_pages(struct zonelist *zonelist, |
3115cd91 | 6165 | struct scan_control *sc) |
1da177e4 | 6166 | { |
241994ed | 6167 | int initial_priority = sc->priority; |
2a2e4885 JW |
6168 | pg_data_t *last_pgdat; |
6169 | struct zoneref *z; | |
6170 | struct zone *zone; | |
241994ed | 6171 | retry: |
873b4771 KK |
6172 | delayacct_freepages_start(); |
6173 | ||
b5ead35e | 6174 | if (!cgroup_reclaim(sc)) |
7cc30fcf | 6175 | __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); |
1da177e4 | 6176 | |
9e3b2f8c | 6177 | do { |
73b73bac YA |
6178 | if (!sc->proactive) |
6179 | vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, | |
6180 | sc->priority); | |
66e1707b | 6181 | sc->nr_scanned = 0; |
0a0337e0 | 6182 | shrink_zones(zonelist, sc); |
c6a8a8c5 | 6183 | |
bb21c7ce | 6184 | if (sc->nr_reclaimed >= sc->nr_to_reclaim) |
0b06496a JW |
6185 | break; |
6186 | ||
6187 | if (sc->compaction_ready) | |
6188 | break; | |
1da177e4 | 6189 | |
0e50ce3b MK |
6190 | /* |
6191 | * If we're getting trouble reclaiming, start doing | |
6192 | * writepage even in laptop mode. | |
6193 | */ | |
6194 | if (sc->priority < DEF_PRIORITY - 2) | |
6195 | sc->may_writepage = 1; | |
0b06496a | 6196 | } while (--sc->priority >= 0); |
bb21c7ce | 6197 | |
2a2e4885 JW |
6198 | last_pgdat = NULL; |
6199 | for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, | |
6200 | sc->nodemask) { | |
6201 | if (zone->zone_pgdat == last_pgdat) | |
6202 | continue; | |
6203 | last_pgdat = zone->zone_pgdat; | |
1b05117d | 6204 | |
2a2e4885 | 6205 | snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); |
1b05117d JW |
6206 | |
6207 | if (cgroup_reclaim(sc)) { | |
6208 | struct lruvec *lruvec; | |
6209 | ||
6210 | lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, | |
6211 | zone->zone_pgdat); | |
1bc545bf | 6212 | clear_bit(LRUVEC_CGROUP_CONGESTED, &lruvec->flags); |
1b05117d | 6213 | } |
2a2e4885 JW |
6214 | } |
6215 | ||
873b4771 KK |
6216 | delayacct_freepages_end(); |
6217 | ||
bb21c7ce KM |
6218 | if (sc->nr_reclaimed) |
6219 | return sc->nr_reclaimed; | |
6220 | ||
0cee34fd | 6221 | /* Aborted reclaim to try compaction? don't OOM, then */ |
0b06496a | 6222 | if (sc->compaction_ready) |
7335084d MG |
6223 | return 1; |
6224 | ||
b91ac374 JW |
6225 | /* |
6226 | * We make inactive:active ratio decisions based on the node's | |
6227 | * composition of memory, but a restrictive reclaim_idx or a | |
6228 | * memory.low cgroup setting can exempt large amounts of | |
6229 | * memory from reclaim. Neither of which are very common, so | |
6230 | * instead of doing costly eligibility calculations of the | |
6231 | * entire cgroup subtree up front, we assume the estimates are | |
6232 | * good, and retry with forcible deactivation if that fails. | |
6233 | */ | |
6234 | if (sc->skipped_deactivate) { | |
6235 | sc->priority = initial_priority; | |
6236 | sc->force_deactivate = 1; | |
6237 | sc->skipped_deactivate = 0; | |
6238 | goto retry; | |
6239 | } | |
6240 | ||
241994ed | 6241 | /* Untapped cgroup reserves? Don't OOM, retry. */ |
d6622f63 | 6242 | if (sc->memcg_low_skipped) { |
241994ed | 6243 | sc->priority = initial_priority; |
b91ac374 | 6244 | sc->force_deactivate = 0; |
d6622f63 YX |
6245 | sc->memcg_low_reclaim = 1; |
6246 | sc->memcg_low_skipped = 0; | |
241994ed JW |
6247 | goto retry; |
6248 | } | |
6249 | ||
bb21c7ce | 6250 | return 0; |
1da177e4 LT |
6251 | } |
6252 | ||
c73322d0 | 6253 | static bool allow_direct_reclaim(pg_data_t *pgdat) |
5515061d MG |
6254 | { |
6255 | struct zone *zone; | |
6256 | unsigned long pfmemalloc_reserve = 0; | |
6257 | unsigned long free_pages = 0; | |
6258 | int i; | |
6259 | bool wmark_ok; | |
6260 | ||
c73322d0 JW |
6261 | if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) |
6262 | return true; | |
6263 | ||
5515061d MG |
6264 | for (i = 0; i <= ZONE_NORMAL; i++) { |
6265 | zone = &pgdat->node_zones[i]; | |
d450abd8 JW |
6266 | if (!managed_zone(zone)) |
6267 | continue; | |
6268 | ||
6269 | if (!zone_reclaimable_pages(zone)) | |
675becce MG |
6270 | continue; |
6271 | ||
5515061d | 6272 | pfmemalloc_reserve += min_wmark_pages(zone); |
501b2651 | 6273 | free_pages += zone_page_state_snapshot(zone, NR_FREE_PAGES); |
5515061d MG |
6274 | } |
6275 | ||
675becce MG |
6276 | /* If there are no reserves (unexpected config) then do not throttle */ |
6277 | if (!pfmemalloc_reserve) | |
6278 | return true; | |
6279 | ||
5515061d MG |
6280 | wmark_ok = free_pages > pfmemalloc_reserve / 2; |
6281 | ||
6282 | /* kswapd must be awake if processes are being throttled */ | |
6283 | if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { | |
97a225e6 JK |
6284 | if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL) |
6285 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL); | |
5644e1fb | 6286 | |
5515061d MG |
6287 | wake_up_interruptible(&pgdat->kswapd_wait); |
6288 | } | |
6289 | ||
6290 | return wmark_ok; | |
6291 | } | |
6292 | ||
6293 | /* | |
6294 | * Throttle direct reclaimers if backing storage is backed by the network | |
6295 | * and the PFMEMALLOC reserve for the preferred node is getting dangerously | |
6296 | * depleted. kswapd will continue to make progress and wake the processes | |
50694c28 MG |
6297 | * when the low watermark is reached. |
6298 | * | |
6299 | * Returns true if a fatal signal was delivered during throttling. If this | |
6300 | * happens, the page allocator should not consider triggering the OOM killer. | |
5515061d | 6301 | */ |
50694c28 | 6302 | static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, |
5515061d MG |
6303 | nodemask_t *nodemask) |
6304 | { | |
675becce | 6305 | struct zoneref *z; |
5515061d | 6306 | struct zone *zone; |
675becce | 6307 | pg_data_t *pgdat = NULL; |
5515061d MG |
6308 | |
6309 | /* | |
6310 | * Kernel threads should not be throttled as they may be indirectly | |
6311 | * responsible for cleaning pages necessary for reclaim to make forward | |
6312 | * progress. kjournald for example may enter direct reclaim while | |
6313 | * committing a transaction where throttling it could forcing other | |
6314 | * processes to block on log_wait_commit(). | |
6315 | */ | |
6316 | if (current->flags & PF_KTHREAD) | |
50694c28 MG |
6317 | goto out; |
6318 | ||
6319 | /* | |
6320 | * If a fatal signal is pending, this process should not throttle. | |
6321 | * It should return quickly so it can exit and free its memory | |
6322 | */ | |
6323 | if (fatal_signal_pending(current)) | |
6324 | goto out; | |
5515061d | 6325 | |
675becce MG |
6326 | /* |
6327 | * Check if the pfmemalloc reserves are ok by finding the first node | |
6328 | * with a usable ZONE_NORMAL or lower zone. The expectation is that | |
6329 | * GFP_KERNEL will be required for allocating network buffers when | |
6330 | * swapping over the network so ZONE_HIGHMEM is unusable. | |
6331 | * | |
6332 | * Throttling is based on the first usable node and throttled processes | |
6333 | * wait on a queue until kswapd makes progress and wakes them. There | |
6334 | * is an affinity then between processes waking up and where reclaim | |
6335 | * progress has been made assuming the process wakes on the same node. | |
6336 | * More importantly, processes running on remote nodes will not compete | |
6337 | * for remote pfmemalloc reserves and processes on different nodes | |
6338 | * should make reasonable progress. | |
6339 | */ | |
6340 | for_each_zone_zonelist_nodemask(zone, z, zonelist, | |
17636faa | 6341 | gfp_zone(gfp_mask), nodemask) { |
675becce MG |
6342 | if (zone_idx(zone) > ZONE_NORMAL) |
6343 | continue; | |
6344 | ||
6345 | /* Throttle based on the first usable node */ | |
6346 | pgdat = zone->zone_pgdat; | |
c73322d0 | 6347 | if (allow_direct_reclaim(pgdat)) |
675becce MG |
6348 | goto out; |
6349 | break; | |
6350 | } | |
6351 | ||
6352 | /* If no zone was usable by the allocation flags then do not throttle */ | |
6353 | if (!pgdat) | |
50694c28 | 6354 | goto out; |
5515061d | 6355 | |
68243e76 MG |
6356 | /* Account for the throttling */ |
6357 | count_vm_event(PGSCAN_DIRECT_THROTTLE); | |
6358 | ||
5515061d MG |
6359 | /* |
6360 | * If the caller cannot enter the filesystem, it's possible that it | |
6361 | * is due to the caller holding an FS lock or performing a journal | |
6362 | * transaction in the case of a filesystem like ext[3|4]. In this case, | |
6363 | * it is not safe to block on pfmemalloc_wait as kswapd could be | |
6364 | * blocked waiting on the same lock. Instead, throttle for up to a | |
6365 | * second before continuing. | |
6366 | */ | |
2e786d9e | 6367 | if (!(gfp_mask & __GFP_FS)) |
5515061d | 6368 | wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, |
c73322d0 | 6369 | allow_direct_reclaim(pgdat), HZ); |
2e786d9e ML |
6370 | else |
6371 | /* Throttle until kswapd wakes the process */ | |
6372 | wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, | |
6373 | allow_direct_reclaim(pgdat)); | |
50694c28 | 6374 | |
50694c28 MG |
6375 | if (fatal_signal_pending(current)) |
6376 | return true; | |
6377 | ||
6378 | out: | |
6379 | return false; | |
5515061d MG |
6380 | } |
6381 | ||
dac1d27b | 6382 | unsigned long try_to_free_pages(struct zonelist *zonelist, int order, |
327c0e96 | 6383 | gfp_t gfp_mask, nodemask_t *nodemask) |
66e1707b | 6384 | { |
33906bc5 | 6385 | unsigned long nr_reclaimed; |
66e1707b | 6386 | struct scan_control sc = { |
ee814fe2 | 6387 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
f2f43e56 | 6388 | .gfp_mask = current_gfp_context(gfp_mask), |
b2e18757 | 6389 | .reclaim_idx = gfp_zone(gfp_mask), |
ee814fe2 JW |
6390 | .order = order, |
6391 | .nodemask = nodemask, | |
6392 | .priority = DEF_PRIORITY, | |
66e1707b | 6393 | .may_writepage = !laptop_mode, |
a6dc60f8 | 6394 | .may_unmap = 1, |
2e2e4259 | 6395 | .may_swap = 1, |
66e1707b BS |
6396 | }; |
6397 | ||
bb451fdf GT |
6398 | /* |
6399 | * scan_control uses s8 fields for order, priority, and reclaim_idx. | |
6400 | * Confirm they are large enough for max values. | |
6401 | */ | |
23baf831 | 6402 | BUILD_BUG_ON(MAX_ORDER >= S8_MAX); |
bb451fdf GT |
6403 | BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); |
6404 | BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); | |
6405 | ||
5515061d | 6406 | /* |
50694c28 MG |
6407 | * Do not enter reclaim if fatal signal was delivered while throttled. |
6408 | * 1 is returned so that the page allocator does not OOM kill at this | |
6409 | * point. | |
5515061d | 6410 | */ |
f2f43e56 | 6411 | if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) |
5515061d MG |
6412 | return 1; |
6413 | ||
1732d2b0 | 6414 | set_task_reclaim_state(current, &sc.reclaim_state); |
3481c37f | 6415 | trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask); |
33906bc5 | 6416 | |
3115cd91 | 6417 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
33906bc5 MG |
6418 | |
6419 | trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); | |
1732d2b0 | 6420 | set_task_reclaim_state(current, NULL); |
33906bc5 MG |
6421 | |
6422 | return nr_reclaimed; | |
66e1707b BS |
6423 | } |
6424 | ||
c255a458 | 6425 | #ifdef CONFIG_MEMCG |
66e1707b | 6426 | |
d2e5fb92 | 6427 | /* Only used by soft limit reclaim. Do not reuse for anything else. */ |
a9dd0a83 | 6428 | unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, |
4e416953 | 6429 | gfp_t gfp_mask, bool noswap, |
ef8f2327 | 6430 | pg_data_t *pgdat, |
0ae5e89c | 6431 | unsigned long *nr_scanned) |
4e416953 | 6432 | { |
afaf07a6 | 6433 | struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); |
4e416953 | 6434 | struct scan_control sc = { |
b8f5c566 | 6435 | .nr_to_reclaim = SWAP_CLUSTER_MAX, |
ee814fe2 | 6436 | .target_mem_cgroup = memcg, |
4e416953 BS |
6437 | .may_writepage = !laptop_mode, |
6438 | .may_unmap = 1, | |
b2e18757 | 6439 | .reclaim_idx = MAX_NR_ZONES - 1, |
4e416953 | 6440 | .may_swap = !noswap, |
4e416953 | 6441 | }; |
0ae5e89c | 6442 | |
d2e5fb92 MH |
6443 | WARN_ON_ONCE(!current->reclaim_state); |
6444 | ||
4e416953 BS |
6445 | sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | |
6446 | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); | |
bdce6d9e | 6447 | |
9e3b2f8c | 6448 | trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, |
3481c37f | 6449 | sc.gfp_mask); |
bdce6d9e | 6450 | |
4e416953 BS |
6451 | /* |
6452 | * NOTE: Although we can get the priority field, using it | |
6453 | * here is not a good idea, since it limits the pages we can scan. | |
a9dd0a83 | 6454 | * if we don't reclaim here, the shrink_node from balance_pgdat |
4e416953 BS |
6455 | * will pick up pages from other mem cgroup's as well. We hack |
6456 | * the priority and make it zero. | |
6457 | */ | |
afaf07a6 | 6458 | shrink_lruvec(lruvec, &sc); |
bdce6d9e KM |
6459 | |
6460 | trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); | |
6461 | ||
0ae5e89c | 6462 | *nr_scanned = sc.nr_scanned; |
0308f7cf | 6463 | |
4e416953 BS |
6464 | return sc.nr_reclaimed; |
6465 | } | |
6466 | ||
72835c86 | 6467 | unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, |
b70a2a21 | 6468 | unsigned long nr_pages, |
a7885eb8 | 6469 | gfp_t gfp_mask, |
55ab834a | 6470 | unsigned int reclaim_options) |
66e1707b | 6471 | { |
bdce6d9e | 6472 | unsigned long nr_reclaimed; |
499118e9 | 6473 | unsigned int noreclaim_flag; |
66e1707b | 6474 | struct scan_control sc = { |
b70a2a21 | 6475 | .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), |
7dea19f9 | 6476 | .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | |
a09ed5e0 | 6477 | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), |
b2e18757 | 6478 | .reclaim_idx = MAX_NR_ZONES - 1, |
ee814fe2 JW |
6479 | .target_mem_cgroup = memcg, |
6480 | .priority = DEF_PRIORITY, | |
6481 | .may_writepage = !laptop_mode, | |
6482 | .may_unmap = 1, | |
73b73bac YA |
6483 | .may_swap = !!(reclaim_options & MEMCG_RECLAIM_MAY_SWAP), |
6484 | .proactive = !!(reclaim_options & MEMCG_RECLAIM_PROACTIVE), | |
a09ed5e0 | 6485 | }; |
889976db | 6486 | /* |
fa40d1ee SB |
6487 | * Traverse the ZONELIST_FALLBACK zonelist of the current node to put |
6488 | * equal pressure on all the nodes. This is based on the assumption that | |
6489 | * the reclaim does not bail out early. | |
889976db | 6490 | */ |
fa40d1ee | 6491 | struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); |
889976db | 6492 | |
fa40d1ee | 6493 | set_task_reclaim_state(current, &sc.reclaim_state); |
3481c37f | 6494 | trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask); |
499118e9 | 6495 | noreclaim_flag = memalloc_noreclaim_save(); |
eb414681 | 6496 | |
3115cd91 | 6497 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
eb414681 | 6498 | |
499118e9 | 6499 | memalloc_noreclaim_restore(noreclaim_flag); |
bdce6d9e | 6500 | trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); |
1732d2b0 | 6501 | set_task_reclaim_state(current, NULL); |
bdce6d9e KM |
6502 | |
6503 | return nr_reclaimed; | |
66e1707b BS |
6504 | } |
6505 | #endif | |
6506 | ||
ac35a490 | 6507 | static void kswapd_age_node(struct pglist_data *pgdat, struct scan_control *sc) |
f16015fb | 6508 | { |
b95a2f2d | 6509 | struct mem_cgroup *memcg; |
b91ac374 | 6510 | struct lruvec *lruvec; |
f16015fb | 6511 | |
ac35a490 YZ |
6512 | if (lru_gen_enabled()) { |
6513 | lru_gen_age_node(pgdat, sc); | |
6514 | return; | |
6515 | } | |
6516 | ||
2f368a9f | 6517 | if (!can_age_anon_pages(pgdat, sc)) |
b95a2f2d JW |
6518 | return; |
6519 | ||
b91ac374 JW |
6520 | lruvec = mem_cgroup_lruvec(NULL, pgdat); |
6521 | if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON)) | |
6522 | return; | |
6523 | ||
b95a2f2d JW |
6524 | memcg = mem_cgroup_iter(NULL, NULL, NULL); |
6525 | do { | |
b91ac374 JW |
6526 | lruvec = mem_cgroup_lruvec(memcg, pgdat); |
6527 | shrink_active_list(SWAP_CLUSTER_MAX, lruvec, | |
6528 | sc, LRU_ACTIVE_ANON); | |
b95a2f2d JW |
6529 | memcg = mem_cgroup_iter(NULL, memcg, NULL); |
6530 | } while (memcg); | |
f16015fb JW |
6531 | } |
6532 | ||
97a225e6 | 6533 | static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx) |
1c30844d MG |
6534 | { |
6535 | int i; | |
6536 | struct zone *zone; | |
6537 | ||
6538 | /* | |
6539 | * Check for watermark boosts top-down as the higher zones | |
6540 | * are more likely to be boosted. Both watermarks and boosts | |
1eba09c1 | 6541 | * should not be checked at the same time as reclaim would |
1c30844d MG |
6542 | * start prematurely when there is no boosting and a lower |
6543 | * zone is balanced. | |
6544 | */ | |
97a225e6 | 6545 | for (i = highest_zoneidx; i >= 0; i--) { |
1c30844d MG |
6546 | zone = pgdat->node_zones + i; |
6547 | if (!managed_zone(zone)) | |
6548 | continue; | |
6549 | ||
6550 | if (zone->watermark_boost) | |
6551 | return true; | |
6552 | } | |
6553 | ||
6554 | return false; | |
6555 | } | |
6556 | ||
e716f2eb MG |
6557 | /* |
6558 | * Returns true if there is an eligible zone balanced for the request order | |
97a225e6 | 6559 | * and highest_zoneidx |
e716f2eb | 6560 | */ |
97a225e6 | 6561 | static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx) |
60cefed4 | 6562 | { |
e716f2eb MG |
6563 | int i; |
6564 | unsigned long mark = -1; | |
6565 | struct zone *zone; | |
60cefed4 | 6566 | |
1c30844d MG |
6567 | /* |
6568 | * Check watermarks bottom-up as lower zones are more likely to | |
6569 | * meet watermarks. | |
6570 | */ | |
97a225e6 | 6571 | for (i = 0; i <= highest_zoneidx; i++) { |
e716f2eb | 6572 | zone = pgdat->node_zones + i; |
6256c6b4 | 6573 | |
e716f2eb MG |
6574 | if (!managed_zone(zone)) |
6575 | continue; | |
6576 | ||
c574bbe9 YH |
6577 | if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) |
6578 | mark = wmark_pages(zone, WMARK_PROMO); | |
6579 | else | |
6580 | mark = high_wmark_pages(zone); | |
97a225e6 | 6581 | if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx)) |
e716f2eb MG |
6582 | return true; |
6583 | } | |
6584 | ||
6585 | /* | |
36c26128 | 6586 | * If a node has no managed zone within highest_zoneidx, it does not |
e716f2eb MG |
6587 | * need balancing by definition. This can happen if a zone-restricted |
6588 | * allocation tries to wake a remote kswapd. | |
6589 | */ | |
6590 | if (mark == -1) | |
6591 | return true; | |
6592 | ||
6593 | return false; | |
60cefed4 JW |
6594 | } |
6595 | ||
631b6e08 MG |
6596 | /* Clear pgdat state for congested, dirty or under writeback. */ |
6597 | static void clear_pgdat_congested(pg_data_t *pgdat) | |
6598 | { | |
1b05117d JW |
6599 | struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat); |
6600 | ||
1bc545bf YA |
6601 | clear_bit(LRUVEC_NODE_CONGESTED, &lruvec->flags); |
6602 | clear_bit(LRUVEC_CGROUP_CONGESTED, &lruvec->flags); | |
631b6e08 MG |
6603 | clear_bit(PGDAT_DIRTY, &pgdat->flags); |
6604 | clear_bit(PGDAT_WRITEBACK, &pgdat->flags); | |
6605 | } | |
6606 | ||
5515061d MG |
6607 | /* |
6608 | * Prepare kswapd for sleeping. This verifies that there are no processes | |
6609 | * waiting in throttle_direct_reclaim() and that watermarks have been met. | |
6610 | * | |
6611 | * Returns true if kswapd is ready to sleep | |
6612 | */ | |
97a225e6 JK |
6613 | static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, |
6614 | int highest_zoneidx) | |
f50de2d3 | 6615 | { |
5515061d | 6616 | /* |
9e5e3661 | 6617 | * The throttled processes are normally woken up in balance_pgdat() as |
c73322d0 | 6618 | * soon as allow_direct_reclaim() is true. But there is a potential |
9e5e3661 VB |
6619 | * race between when kswapd checks the watermarks and a process gets |
6620 | * throttled. There is also a potential race if processes get | |
6621 | * throttled, kswapd wakes, a large process exits thereby balancing the | |
6622 | * zones, which causes kswapd to exit balance_pgdat() before reaching | |
6623 | * the wake up checks. If kswapd is going to sleep, no process should | |
6624 | * be sleeping on pfmemalloc_wait, so wake them now if necessary. If | |
6625 | * the wake up is premature, processes will wake kswapd and get | |
6626 | * throttled again. The difference from wake ups in balance_pgdat() is | |
6627 | * that here we are under prepare_to_wait(). | |
5515061d | 6628 | */ |
9e5e3661 VB |
6629 | if (waitqueue_active(&pgdat->pfmemalloc_wait)) |
6630 | wake_up_all(&pgdat->pfmemalloc_wait); | |
f50de2d3 | 6631 | |
c73322d0 JW |
6632 | /* Hopeless node, leave it to direct reclaim */ |
6633 | if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) | |
6634 | return true; | |
6635 | ||
97a225e6 | 6636 | if (pgdat_balanced(pgdat, order, highest_zoneidx)) { |
e716f2eb MG |
6637 | clear_pgdat_congested(pgdat); |
6638 | return true; | |
1d82de61 MG |
6639 | } |
6640 | ||
333b0a45 | 6641 | return false; |
f50de2d3 MG |
6642 | } |
6643 | ||
75485363 | 6644 | /* |
1d82de61 MG |
6645 | * kswapd shrinks a node of pages that are at or below the highest usable |
6646 | * zone that is currently unbalanced. | |
b8e83b94 MG |
6647 | * |
6648 | * Returns true if kswapd scanned at least the requested number of pages to | |
283aba9f MG |
6649 | * reclaim or if the lack of progress was due to pages under writeback. |
6650 | * This is used to determine if the scanning priority needs to be raised. | |
75485363 | 6651 | */ |
1d82de61 | 6652 | static bool kswapd_shrink_node(pg_data_t *pgdat, |
accf6242 | 6653 | struct scan_control *sc) |
75485363 | 6654 | { |
1d82de61 MG |
6655 | struct zone *zone; |
6656 | int z; | |
75485363 | 6657 | |
1d82de61 MG |
6658 | /* Reclaim a number of pages proportional to the number of zones */ |
6659 | sc->nr_to_reclaim = 0; | |
970a39a3 | 6660 | for (z = 0; z <= sc->reclaim_idx; z++) { |
1d82de61 | 6661 | zone = pgdat->node_zones + z; |
6aa303de | 6662 | if (!managed_zone(zone)) |
1d82de61 | 6663 | continue; |
7c954f6d | 6664 | |
1d82de61 MG |
6665 | sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); |
6666 | } | |
7c954f6d MG |
6667 | |
6668 | /* | |
1d82de61 MG |
6669 | * Historically care was taken to put equal pressure on all zones but |
6670 | * now pressure is applied based on node LRU order. | |
7c954f6d | 6671 | */ |
970a39a3 | 6672 | shrink_node(pgdat, sc); |
283aba9f | 6673 | |
7c954f6d | 6674 | /* |
1d82de61 MG |
6675 | * Fragmentation may mean that the system cannot be rebalanced for |
6676 | * high-order allocations. If twice the allocation size has been | |
6677 | * reclaimed then recheck watermarks only at order-0 to prevent | |
6678 | * excessive reclaim. Assume that a process requested a high-order | |
6679 | * can direct reclaim/compact. | |
7c954f6d | 6680 | */ |
9861a62c | 6681 | if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) |
1d82de61 | 6682 | sc->order = 0; |
7c954f6d | 6683 | |
b8e83b94 | 6684 | return sc->nr_scanned >= sc->nr_to_reclaim; |
75485363 MG |
6685 | } |
6686 | ||
c49c2c47 MG |
6687 | /* Page allocator PCP high watermark is lowered if reclaim is active. */ |
6688 | static inline void | |
6689 | update_reclaim_active(pg_data_t *pgdat, int highest_zoneidx, bool active) | |
6690 | { | |
6691 | int i; | |
6692 | struct zone *zone; | |
6693 | ||
6694 | for (i = 0; i <= highest_zoneidx; i++) { | |
6695 | zone = pgdat->node_zones + i; | |
6696 | ||
6697 | if (!managed_zone(zone)) | |
6698 | continue; | |
6699 | ||
6700 | if (active) | |
6701 | set_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); | |
6702 | else | |
6703 | clear_bit(ZONE_RECLAIM_ACTIVE, &zone->flags); | |
6704 | } | |
6705 | } | |
6706 | ||
6707 | static inline void | |
6708 | set_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) | |
6709 | { | |
6710 | update_reclaim_active(pgdat, highest_zoneidx, true); | |
6711 | } | |
6712 | ||
6713 | static inline void | |
6714 | clear_reclaim_active(pg_data_t *pgdat, int highest_zoneidx) | |
6715 | { | |
6716 | update_reclaim_active(pgdat, highest_zoneidx, false); | |
6717 | } | |
6718 | ||
1da177e4 | 6719 | /* |
1d82de61 MG |
6720 | * For kswapd, balance_pgdat() will reclaim pages across a node from zones |
6721 | * that are eligible for use by the caller until at least one zone is | |
6722 | * balanced. | |
1da177e4 | 6723 | * |
1d82de61 | 6724 | * Returns the order kswapd finished reclaiming at. |
1da177e4 LT |
6725 | * |
6726 | * kswapd scans the zones in the highmem->normal->dma direction. It skips | |
41858966 | 6727 | * zones which have free_pages > high_wmark_pages(zone), but once a zone is |
8bb4e7a2 | 6728 | * found to have free_pages <= high_wmark_pages(zone), any page in that zone |
1d82de61 MG |
6729 | * or lower is eligible for reclaim until at least one usable zone is |
6730 | * balanced. | |
1da177e4 | 6731 | */ |
97a225e6 | 6732 | static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) |
1da177e4 | 6733 | { |
1da177e4 | 6734 | int i; |
0608f43d AM |
6735 | unsigned long nr_soft_reclaimed; |
6736 | unsigned long nr_soft_scanned; | |
eb414681 | 6737 | unsigned long pflags; |
1c30844d MG |
6738 | unsigned long nr_boost_reclaim; |
6739 | unsigned long zone_boosts[MAX_NR_ZONES] = { 0, }; | |
6740 | bool boosted; | |
1d82de61 | 6741 | struct zone *zone; |
179e9639 AM |
6742 | struct scan_control sc = { |
6743 | .gfp_mask = GFP_KERNEL, | |
ee814fe2 | 6744 | .order = order, |
a6dc60f8 | 6745 | .may_unmap = 1, |
179e9639 | 6746 | }; |
93781325 | 6747 | |
1732d2b0 | 6748 | set_task_reclaim_state(current, &sc.reclaim_state); |
eb414681 | 6749 | psi_memstall_enter(&pflags); |
4f3eaf45 | 6750 | __fs_reclaim_acquire(_THIS_IP_); |
93781325 | 6751 | |
f8891e5e | 6752 | count_vm_event(PAGEOUTRUN); |
1da177e4 | 6753 | |
1c30844d MG |
6754 | /* |
6755 | * Account for the reclaim boost. Note that the zone boost is left in | |
6756 | * place so that parallel allocations that are near the watermark will | |
6757 | * stall or direct reclaim until kswapd is finished. | |
6758 | */ | |
6759 | nr_boost_reclaim = 0; | |
97a225e6 | 6760 | for (i = 0; i <= highest_zoneidx; i++) { |
1c30844d MG |
6761 | zone = pgdat->node_zones + i; |
6762 | if (!managed_zone(zone)) | |
6763 | continue; | |
6764 | ||
6765 | nr_boost_reclaim += zone->watermark_boost; | |
6766 | zone_boosts[i] = zone->watermark_boost; | |
6767 | } | |
6768 | boosted = nr_boost_reclaim; | |
6769 | ||
6770 | restart: | |
c49c2c47 | 6771 | set_reclaim_active(pgdat, highest_zoneidx); |
1c30844d | 6772 | sc.priority = DEF_PRIORITY; |
9e3b2f8c | 6773 | do { |
c73322d0 | 6774 | unsigned long nr_reclaimed = sc.nr_reclaimed; |
b8e83b94 | 6775 | bool raise_priority = true; |
1c30844d | 6776 | bool balanced; |
93781325 | 6777 | bool ret; |
b8e83b94 | 6778 | |
97a225e6 | 6779 | sc.reclaim_idx = highest_zoneidx; |
1da177e4 | 6780 | |
86c79f6b | 6781 | /* |
84c7a777 MG |
6782 | * If the number of buffer_heads exceeds the maximum allowed |
6783 | * then consider reclaiming from all zones. This has a dual | |
6784 | * purpose -- on 64-bit systems it is expected that | |
6785 | * buffer_heads are stripped during active rotation. On 32-bit | |
6786 | * systems, highmem pages can pin lowmem memory and shrinking | |
6787 | * buffers can relieve lowmem pressure. Reclaim may still not | |
6788 | * go ahead if all eligible zones for the original allocation | |
6789 | * request are balanced to avoid excessive reclaim from kswapd. | |
86c79f6b MG |
6790 | */ |
6791 | if (buffer_heads_over_limit) { | |
6792 | for (i = MAX_NR_ZONES - 1; i >= 0; i--) { | |
6793 | zone = pgdat->node_zones + i; | |
6aa303de | 6794 | if (!managed_zone(zone)) |
86c79f6b | 6795 | continue; |
cc715d99 | 6796 | |
970a39a3 | 6797 | sc.reclaim_idx = i; |
e1dbeda6 | 6798 | break; |
1da177e4 | 6799 | } |
1da177e4 | 6800 | } |
dafcb73e | 6801 | |
86c79f6b | 6802 | /* |
1c30844d MG |
6803 | * If the pgdat is imbalanced then ignore boosting and preserve |
6804 | * the watermarks for a later time and restart. Note that the | |
6805 | * zone watermarks will be still reset at the end of balancing | |
6806 | * on the grounds that the normal reclaim should be enough to | |
6807 | * re-evaluate if boosting is required when kswapd next wakes. | |
6808 | */ | |
97a225e6 | 6809 | balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx); |
1c30844d MG |
6810 | if (!balanced && nr_boost_reclaim) { |
6811 | nr_boost_reclaim = 0; | |
6812 | goto restart; | |
6813 | } | |
6814 | ||
6815 | /* | |
6816 | * If boosting is not active then only reclaim if there are no | |
6817 | * eligible zones. Note that sc.reclaim_idx is not used as | |
6818 | * buffer_heads_over_limit may have adjusted it. | |
86c79f6b | 6819 | */ |
1c30844d | 6820 | if (!nr_boost_reclaim && balanced) |
e716f2eb | 6821 | goto out; |
e1dbeda6 | 6822 | |
1c30844d MG |
6823 | /* Limit the priority of boosting to avoid reclaim writeback */ |
6824 | if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2) | |
6825 | raise_priority = false; | |
6826 | ||
6827 | /* | |
6828 | * Do not writeback or swap pages for boosted reclaim. The | |
6829 | * intent is to relieve pressure not issue sub-optimal IO | |
6830 | * from reclaim context. If no pages are reclaimed, the | |
6831 | * reclaim will be aborted. | |
6832 | */ | |
6833 | sc.may_writepage = !laptop_mode && !nr_boost_reclaim; | |
6834 | sc.may_swap = !nr_boost_reclaim; | |
1c30844d | 6835 | |
1d82de61 | 6836 | /* |
ac35a490 YZ |
6837 | * Do some background aging, to give pages a chance to be |
6838 | * referenced before reclaiming. All pages are rotated | |
6839 | * regardless of classzone as this is about consistent aging. | |
1d82de61 | 6840 | */ |
ac35a490 | 6841 | kswapd_age_node(pgdat, &sc); |
1d82de61 | 6842 | |
b7ea3c41 MG |
6843 | /* |
6844 | * If we're getting trouble reclaiming, start doing writepage | |
6845 | * even in laptop mode. | |
6846 | */ | |
047d72c3 | 6847 | if (sc.priority < DEF_PRIORITY - 2) |
b7ea3c41 MG |
6848 | sc.may_writepage = 1; |
6849 | ||
1d82de61 MG |
6850 | /* Call soft limit reclaim before calling shrink_node. */ |
6851 | sc.nr_scanned = 0; | |
6852 | nr_soft_scanned = 0; | |
ef8f2327 | 6853 | nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, |
1d82de61 MG |
6854 | sc.gfp_mask, &nr_soft_scanned); |
6855 | sc.nr_reclaimed += nr_soft_reclaimed; | |
6856 | ||
1da177e4 | 6857 | /* |
1d82de61 MG |
6858 | * There should be no need to raise the scanning priority if |
6859 | * enough pages are already being scanned that that high | |
6860 | * watermark would be met at 100% efficiency. | |
1da177e4 | 6861 | */ |
970a39a3 | 6862 | if (kswapd_shrink_node(pgdat, &sc)) |
1d82de61 | 6863 | raise_priority = false; |
5515061d MG |
6864 | |
6865 | /* | |
6866 | * If the low watermark is met there is no need for processes | |
6867 | * to be throttled on pfmemalloc_wait as they should not be | |
6868 | * able to safely make forward progress. Wake them | |
6869 | */ | |
6870 | if (waitqueue_active(&pgdat->pfmemalloc_wait) && | |
c73322d0 | 6871 | allow_direct_reclaim(pgdat)) |
cfc51155 | 6872 | wake_up_all(&pgdat->pfmemalloc_wait); |
5515061d | 6873 | |
b8e83b94 | 6874 | /* Check if kswapd should be suspending */ |
4f3eaf45 | 6875 | __fs_reclaim_release(_THIS_IP_); |
93781325 | 6876 | ret = try_to_freeze(); |
4f3eaf45 | 6877 | __fs_reclaim_acquire(_THIS_IP_); |
93781325 | 6878 | if (ret || kthread_should_stop()) |
b8e83b94 | 6879 | break; |
8357376d | 6880 | |
73ce02e9 | 6881 | /* |
b8e83b94 MG |
6882 | * Raise priority if scanning rate is too low or there was no |
6883 | * progress in reclaiming pages | |
73ce02e9 | 6884 | */ |
c73322d0 | 6885 | nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; |
1c30844d MG |
6886 | nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed); |
6887 | ||
6888 | /* | |
6889 | * If reclaim made no progress for a boost, stop reclaim as | |
6890 | * IO cannot be queued and it could be an infinite loop in | |
6891 | * extreme circumstances. | |
6892 | */ | |
6893 | if (nr_boost_reclaim && !nr_reclaimed) | |
6894 | break; | |
6895 | ||
c73322d0 | 6896 | if (raise_priority || !nr_reclaimed) |
b8e83b94 | 6897 | sc.priority--; |
1d82de61 | 6898 | } while (sc.priority >= 1); |
1da177e4 | 6899 | |
c73322d0 JW |
6900 | if (!sc.nr_reclaimed) |
6901 | pgdat->kswapd_failures++; | |
6902 | ||
b8e83b94 | 6903 | out: |
c49c2c47 MG |
6904 | clear_reclaim_active(pgdat, highest_zoneidx); |
6905 | ||
1c30844d MG |
6906 | /* If reclaim was boosted, account for the reclaim done in this pass */ |
6907 | if (boosted) { | |
6908 | unsigned long flags; | |
6909 | ||
97a225e6 | 6910 | for (i = 0; i <= highest_zoneidx; i++) { |
1c30844d MG |
6911 | if (!zone_boosts[i]) |
6912 | continue; | |
6913 | ||
6914 | /* Increments are under the zone lock */ | |
6915 | zone = pgdat->node_zones + i; | |
6916 | spin_lock_irqsave(&zone->lock, flags); | |
6917 | zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]); | |
6918 | spin_unlock_irqrestore(&zone->lock, flags); | |
6919 | } | |
6920 | ||
6921 | /* | |
6922 | * As there is now likely space, wakeup kcompact to defragment | |
6923 | * pageblocks. | |
6924 | */ | |
97a225e6 | 6925 | wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx); |
1c30844d MG |
6926 | } |
6927 | ||
2a2e4885 | 6928 | snapshot_refaults(NULL, pgdat); |
4f3eaf45 | 6929 | __fs_reclaim_release(_THIS_IP_); |
eb414681 | 6930 | psi_memstall_leave(&pflags); |
1732d2b0 | 6931 | set_task_reclaim_state(current, NULL); |
e5ca8071 | 6932 | |
0abdee2b | 6933 | /* |
1d82de61 MG |
6934 | * Return the order kswapd stopped reclaiming at as |
6935 | * prepare_kswapd_sleep() takes it into account. If another caller | |
6936 | * entered the allocator slow path while kswapd was awake, order will | |
6937 | * remain at the higher level. | |
0abdee2b | 6938 | */ |
1d82de61 | 6939 | return sc.order; |
1da177e4 LT |
6940 | } |
6941 | ||
e716f2eb | 6942 | /* |
97a225e6 JK |
6943 | * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to |
6944 | * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is | |
6945 | * not a valid index then either kswapd runs for first time or kswapd couldn't | |
6946 | * sleep after previous reclaim attempt (node is still unbalanced). In that | |
6947 | * case return the zone index of the previous kswapd reclaim cycle. | |
e716f2eb | 6948 | */ |
97a225e6 JK |
6949 | static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, |
6950 | enum zone_type prev_highest_zoneidx) | |
e716f2eb | 6951 | { |
97a225e6 | 6952 | enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); |
5644e1fb | 6953 | |
97a225e6 | 6954 | return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx; |
e716f2eb MG |
6955 | } |
6956 | ||
38087d9b | 6957 | static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, |
97a225e6 | 6958 | unsigned int highest_zoneidx) |
f0bc0a60 KM |
6959 | { |
6960 | long remaining = 0; | |
6961 | DEFINE_WAIT(wait); | |
6962 | ||
6963 | if (freezing(current) || kthread_should_stop()) | |
6964 | return; | |
6965 | ||
6966 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); | |
6967 | ||
333b0a45 SG |
6968 | /* |
6969 | * Try to sleep for a short interval. Note that kcompactd will only be | |
6970 | * woken if it is possible to sleep for a short interval. This is | |
6971 | * deliberate on the assumption that if reclaim cannot keep an | |
6972 | * eligible zone balanced that it's also unlikely that compaction will | |
6973 | * succeed. | |
6974 | */ | |
97a225e6 | 6975 | if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { |
fd901c95 VB |
6976 | /* |
6977 | * Compaction records what page blocks it recently failed to | |
6978 | * isolate pages from and skips them in the future scanning. | |
6979 | * When kswapd is going to sleep, it is reasonable to assume | |
6980 | * that pages and compaction may succeed so reset the cache. | |
6981 | */ | |
6982 | reset_isolation_suitable(pgdat); | |
6983 | ||
6984 | /* | |
6985 | * We have freed the memory, now we should compact it to make | |
6986 | * allocation of the requested order possible. | |
6987 | */ | |
97a225e6 | 6988 | wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx); |
fd901c95 | 6989 | |
f0bc0a60 | 6990 | remaining = schedule_timeout(HZ/10); |
38087d9b MG |
6991 | |
6992 | /* | |
97a225e6 | 6993 | * If woken prematurely then reset kswapd_highest_zoneidx and |
38087d9b MG |
6994 | * order. The values will either be from a wakeup request or |
6995 | * the previous request that slept prematurely. | |
6996 | */ | |
6997 | if (remaining) { | |
97a225e6 JK |
6998 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, |
6999 | kswapd_highest_zoneidx(pgdat, | |
7000 | highest_zoneidx)); | |
5644e1fb QC |
7001 | |
7002 | if (READ_ONCE(pgdat->kswapd_order) < reclaim_order) | |
7003 | WRITE_ONCE(pgdat->kswapd_order, reclaim_order); | |
38087d9b MG |
7004 | } |
7005 | ||
f0bc0a60 KM |
7006 | finish_wait(&pgdat->kswapd_wait, &wait); |
7007 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); | |
7008 | } | |
7009 | ||
7010 | /* | |
7011 | * After a short sleep, check if it was a premature sleep. If not, then | |
7012 | * go fully to sleep until explicitly woken up. | |
7013 | */ | |
d9f21d42 | 7014 | if (!remaining && |
97a225e6 | 7015 | prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { |
f0bc0a60 KM |
7016 | trace_mm_vmscan_kswapd_sleep(pgdat->node_id); |
7017 | ||
7018 | /* | |
7019 | * vmstat counters are not perfectly accurate and the estimated | |
7020 | * value for counters such as NR_FREE_PAGES can deviate from the | |
7021 | * true value by nr_online_cpus * threshold. To avoid the zone | |
7022 | * watermarks being breached while under pressure, we reduce the | |
7023 | * per-cpu vmstat threshold while kswapd is awake and restore | |
7024 | * them before going back to sleep. | |
7025 | */ | |
7026 | set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); | |
1c7e7f6c AK |
7027 | |
7028 | if (!kthread_should_stop()) | |
7029 | schedule(); | |
7030 | ||
f0bc0a60 KM |
7031 | set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); |
7032 | } else { | |
7033 | if (remaining) | |
7034 | count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); | |
7035 | else | |
7036 | count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); | |
7037 | } | |
7038 | finish_wait(&pgdat->kswapd_wait, &wait); | |
7039 | } | |
7040 | ||
1da177e4 LT |
7041 | /* |
7042 | * The background pageout daemon, started as a kernel thread | |
4f98a2fe | 7043 | * from the init process. |
1da177e4 LT |
7044 | * |
7045 | * This basically trickles out pages so that we have _some_ | |
7046 | * free memory available even if there is no other activity | |
7047 | * that frees anything up. This is needed for things like routing | |
7048 | * etc, where we otherwise might have all activity going on in | |
7049 | * asynchronous contexts that cannot page things out. | |
7050 | * | |
7051 | * If there are applications that are active memory-allocators | |
7052 | * (most normal use), this basically shouldn't matter. | |
7053 | */ | |
7054 | static int kswapd(void *p) | |
7055 | { | |
e716f2eb | 7056 | unsigned int alloc_order, reclaim_order; |
97a225e6 | 7057 | unsigned int highest_zoneidx = MAX_NR_ZONES - 1; |
68d68ff6 | 7058 | pg_data_t *pgdat = (pg_data_t *)p; |
1da177e4 | 7059 | struct task_struct *tsk = current; |
a70f7302 | 7060 | const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); |
1da177e4 | 7061 | |
174596a0 | 7062 | if (!cpumask_empty(cpumask)) |
c5f59f08 | 7063 | set_cpus_allowed_ptr(tsk, cpumask); |
1da177e4 LT |
7064 | |
7065 | /* | |
7066 | * Tell the memory management that we're a "memory allocator", | |
7067 | * and that if we need more memory we should get access to it | |
7068 | * regardless (see "__alloc_pages()"). "kswapd" should | |
7069 | * never get caught in the normal page freeing logic. | |
7070 | * | |
7071 | * (Kswapd normally doesn't need memory anyway, but sometimes | |
7072 | * you need a small amount of memory in order to be able to | |
7073 | * page out something else, and this flag essentially protects | |
7074 | * us from recursively trying to free more memory as we're | |
7075 | * trying to free the first piece of memory in the first place). | |
7076 | */ | |
b698f0a1 | 7077 | tsk->flags |= PF_MEMALLOC | PF_KSWAPD; |
83144186 | 7078 | set_freezable(); |
1da177e4 | 7079 | |
5644e1fb | 7080 | WRITE_ONCE(pgdat->kswapd_order, 0); |
97a225e6 | 7081 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); |
8cd7c588 | 7082 | atomic_set(&pgdat->nr_writeback_throttled, 0); |
1da177e4 | 7083 | for ( ; ; ) { |
6f6313d4 | 7084 | bool ret; |
3e1d1d28 | 7085 | |
5644e1fb | 7086 | alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order); |
97a225e6 JK |
7087 | highest_zoneidx = kswapd_highest_zoneidx(pgdat, |
7088 | highest_zoneidx); | |
e716f2eb | 7089 | |
38087d9b MG |
7090 | kswapd_try_sleep: |
7091 | kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, | |
97a225e6 | 7092 | highest_zoneidx); |
215ddd66 | 7093 | |
97a225e6 | 7094 | /* Read the new order and highest_zoneidx */ |
2b47a24c | 7095 | alloc_order = READ_ONCE(pgdat->kswapd_order); |
97a225e6 JK |
7096 | highest_zoneidx = kswapd_highest_zoneidx(pgdat, |
7097 | highest_zoneidx); | |
5644e1fb | 7098 | WRITE_ONCE(pgdat->kswapd_order, 0); |
97a225e6 | 7099 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); |
1da177e4 | 7100 | |
8fe23e05 DR |
7101 | ret = try_to_freeze(); |
7102 | if (kthread_should_stop()) | |
7103 | break; | |
7104 | ||
7105 | /* | |
7106 | * We can speed up thawing tasks if we don't call balance_pgdat | |
7107 | * after returning from the refrigerator | |
7108 | */ | |
38087d9b MG |
7109 | if (ret) |
7110 | continue; | |
7111 | ||
7112 | /* | |
7113 | * Reclaim begins at the requested order but if a high-order | |
7114 | * reclaim fails then kswapd falls back to reclaiming for | |
7115 | * order-0. If that happens, kswapd will consider sleeping | |
7116 | * for the order it finished reclaiming at (reclaim_order) | |
7117 | * but kcompactd is woken to compact for the original | |
7118 | * request (alloc_order). | |
7119 | */ | |
97a225e6 | 7120 | trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx, |
e5146b12 | 7121 | alloc_order); |
97a225e6 JK |
7122 | reclaim_order = balance_pgdat(pgdat, alloc_order, |
7123 | highest_zoneidx); | |
38087d9b MG |
7124 | if (reclaim_order < alloc_order) |
7125 | goto kswapd_try_sleep; | |
1da177e4 | 7126 | } |
b0a8cc58 | 7127 | |
b698f0a1 | 7128 | tsk->flags &= ~(PF_MEMALLOC | PF_KSWAPD); |
71abdc15 | 7129 | |
1da177e4 LT |
7130 | return 0; |
7131 | } | |
7132 | ||
7133 | /* | |
5ecd9d40 DR |
7134 | * A zone is low on free memory or too fragmented for high-order memory. If |
7135 | * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's | |
7136 | * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim | |
7137 | * has failed or is not needed, still wake up kcompactd if only compaction is | |
7138 | * needed. | |
1da177e4 | 7139 | */ |
5ecd9d40 | 7140 | void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, |
97a225e6 | 7141 | enum zone_type highest_zoneidx) |
1da177e4 LT |
7142 | { |
7143 | pg_data_t *pgdat; | |
5644e1fb | 7144 | enum zone_type curr_idx; |
1da177e4 | 7145 | |
6aa303de | 7146 | if (!managed_zone(zone)) |
1da177e4 LT |
7147 | return; |
7148 | ||
5ecd9d40 | 7149 | if (!cpuset_zone_allowed(zone, gfp_flags)) |
1da177e4 | 7150 | return; |
5644e1fb | 7151 | |
88f5acf8 | 7152 | pgdat = zone->zone_pgdat; |
97a225e6 | 7153 | curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); |
5644e1fb | 7154 | |
97a225e6 JK |
7155 | if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx) |
7156 | WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx); | |
5644e1fb QC |
7157 | |
7158 | if (READ_ONCE(pgdat->kswapd_order) < order) | |
7159 | WRITE_ONCE(pgdat->kswapd_order, order); | |
dffcac2c | 7160 | |
8d0986e2 | 7161 | if (!waitqueue_active(&pgdat->kswapd_wait)) |
1da177e4 | 7162 | return; |
e1a55637 | 7163 | |
5ecd9d40 DR |
7164 | /* Hopeless node, leave it to direct reclaim if possible */ |
7165 | if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || | |
97a225e6 JK |
7166 | (pgdat_balanced(pgdat, order, highest_zoneidx) && |
7167 | !pgdat_watermark_boosted(pgdat, highest_zoneidx))) { | |
5ecd9d40 DR |
7168 | /* |
7169 | * There may be plenty of free memory available, but it's too | |
7170 | * fragmented for high-order allocations. Wake up kcompactd | |
7171 | * and rely on compaction_suitable() to determine if it's | |
7172 | * needed. If it fails, it will defer subsequent attempts to | |
7173 | * ratelimit its work. | |
7174 | */ | |
7175 | if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) | |
97a225e6 | 7176 | wakeup_kcompactd(pgdat, order, highest_zoneidx); |
e716f2eb | 7177 | return; |
5ecd9d40 | 7178 | } |
88f5acf8 | 7179 | |
97a225e6 | 7180 | trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order, |
5ecd9d40 | 7181 | gfp_flags); |
8d0986e2 | 7182 | wake_up_interruptible(&pgdat->kswapd_wait); |
1da177e4 LT |
7183 | } |
7184 | ||
c6f37f12 | 7185 | #ifdef CONFIG_HIBERNATION |
1da177e4 | 7186 | /* |
7b51755c | 7187 | * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of |
d6277db4 RW |
7188 | * freed pages. |
7189 | * | |
7190 | * Rather than trying to age LRUs the aim is to preserve the overall | |
7191 | * LRU order by reclaiming preferentially | |
7192 | * inactive > active > active referenced > active mapped | |
1da177e4 | 7193 | */ |
7b51755c | 7194 | unsigned long shrink_all_memory(unsigned long nr_to_reclaim) |
1da177e4 | 7195 | { |
d6277db4 | 7196 | struct scan_control sc = { |
ee814fe2 | 7197 | .nr_to_reclaim = nr_to_reclaim, |
7b51755c | 7198 | .gfp_mask = GFP_HIGHUSER_MOVABLE, |
b2e18757 | 7199 | .reclaim_idx = MAX_NR_ZONES - 1, |
ee814fe2 | 7200 | .priority = DEF_PRIORITY, |
d6277db4 | 7201 | .may_writepage = 1, |
ee814fe2 JW |
7202 | .may_unmap = 1, |
7203 | .may_swap = 1, | |
7b51755c | 7204 | .hibernation_mode = 1, |
1da177e4 | 7205 | }; |
a09ed5e0 | 7206 | struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); |
7b51755c | 7207 | unsigned long nr_reclaimed; |
499118e9 | 7208 | unsigned int noreclaim_flag; |
1da177e4 | 7209 | |
d92a8cfc | 7210 | fs_reclaim_acquire(sc.gfp_mask); |
93781325 | 7211 | noreclaim_flag = memalloc_noreclaim_save(); |
1732d2b0 | 7212 | set_task_reclaim_state(current, &sc.reclaim_state); |
d6277db4 | 7213 | |
3115cd91 | 7214 | nr_reclaimed = do_try_to_free_pages(zonelist, &sc); |
d979677c | 7215 | |
1732d2b0 | 7216 | set_task_reclaim_state(current, NULL); |
499118e9 | 7217 | memalloc_noreclaim_restore(noreclaim_flag); |
93781325 | 7218 | fs_reclaim_release(sc.gfp_mask); |
d6277db4 | 7219 | |
7b51755c | 7220 | return nr_reclaimed; |
1da177e4 | 7221 | } |
c6f37f12 | 7222 | #endif /* CONFIG_HIBERNATION */ |
1da177e4 | 7223 | |
3218ae14 YG |
7224 | /* |
7225 | * This kswapd start function will be called by init and node-hot-add. | |
3218ae14 | 7226 | */ |
e5797dc0 | 7227 | void __meminit kswapd_run(int nid) |
3218ae14 YG |
7228 | { |
7229 | pg_data_t *pgdat = NODE_DATA(nid); | |
3218ae14 | 7230 | |
b4a0215e KW |
7231 | pgdat_kswapd_lock(pgdat); |
7232 | if (!pgdat->kswapd) { | |
7233 | pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); | |
7234 | if (IS_ERR(pgdat->kswapd)) { | |
7235 | /* failure at boot is fatal */ | |
037dd8f9 AC |
7236 | pr_err("Failed to start kswapd on node %d,ret=%ld\n", |
7237 | nid, PTR_ERR(pgdat->kswapd)); | |
b4a0215e | 7238 | BUG_ON(system_state < SYSTEM_RUNNING); |
b4a0215e KW |
7239 | pgdat->kswapd = NULL; |
7240 | } | |
3218ae14 | 7241 | } |
b4a0215e | 7242 | pgdat_kswapd_unlock(pgdat); |
3218ae14 YG |
7243 | } |
7244 | ||
8fe23e05 | 7245 | /* |
d8adde17 | 7246 | * Called by memory hotplug when all memory in a node is offlined. Caller must |
e8da368a | 7247 | * be holding mem_hotplug_begin/done(). |
8fe23e05 | 7248 | */ |
e5797dc0 | 7249 | void __meminit kswapd_stop(int nid) |
8fe23e05 | 7250 | { |
b4a0215e KW |
7251 | pg_data_t *pgdat = NODE_DATA(nid); |
7252 | struct task_struct *kswapd; | |
8fe23e05 | 7253 | |
b4a0215e KW |
7254 | pgdat_kswapd_lock(pgdat); |
7255 | kswapd = pgdat->kswapd; | |
d8adde17 | 7256 | if (kswapd) { |
8fe23e05 | 7257 | kthread_stop(kswapd); |
b4a0215e | 7258 | pgdat->kswapd = NULL; |
d8adde17 | 7259 | } |
b4a0215e | 7260 | pgdat_kswapd_unlock(pgdat); |
8fe23e05 DR |
7261 | } |
7262 | ||
1da177e4 LT |
7263 | static int __init kswapd_init(void) |
7264 | { | |
6b700b5b | 7265 | int nid; |
69e05944 | 7266 | |
1da177e4 | 7267 | swap_setup(); |
48fb2e24 | 7268 | for_each_node_state(nid, N_MEMORY) |
3218ae14 | 7269 | kswapd_run(nid); |
1da177e4 LT |
7270 | return 0; |
7271 | } | |
7272 | ||
7273 | module_init(kswapd_init) | |
9eeff239 CL |
7274 | |
7275 | #ifdef CONFIG_NUMA | |
7276 | /* | |
a5f5f91d | 7277 | * Node reclaim mode |
9eeff239 | 7278 | * |
a5f5f91d | 7279 | * If non-zero call node_reclaim when the number of free pages falls below |
9eeff239 | 7280 | * the watermarks. |
9eeff239 | 7281 | */ |
a5f5f91d | 7282 | int node_reclaim_mode __read_mostly; |
9eeff239 | 7283 | |
a92f7126 | 7284 | /* |
a5f5f91d | 7285 | * Priority for NODE_RECLAIM. This determines the fraction of pages |
a92f7126 CL |
7286 | * of a node considered for each zone_reclaim. 4 scans 1/16th of |
7287 | * a zone. | |
7288 | */ | |
a5f5f91d | 7289 | #define NODE_RECLAIM_PRIORITY 4 |
a92f7126 | 7290 | |
9614634f | 7291 | /* |
a5f5f91d | 7292 | * Percentage of pages in a zone that must be unmapped for node_reclaim to |
9614634f CL |
7293 | * occur. |
7294 | */ | |
7295 | int sysctl_min_unmapped_ratio = 1; | |
7296 | ||
0ff38490 CL |
7297 | /* |
7298 | * If the number of slab pages in a zone grows beyond this percentage then | |
7299 | * slab reclaim needs to occur. | |
7300 | */ | |
7301 | int sysctl_min_slab_ratio = 5; | |
7302 | ||
11fb9989 | 7303 | static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) |
90afa5de | 7304 | { |
11fb9989 MG |
7305 | unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); |
7306 | unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + | |
7307 | node_page_state(pgdat, NR_ACTIVE_FILE); | |
90afa5de MG |
7308 | |
7309 | /* | |
7310 | * It's possible for there to be more file mapped pages than | |
7311 | * accounted for by the pages on the file LRU lists because | |
7312 | * tmpfs pages accounted for as ANON can also be FILE_MAPPED | |
7313 | */ | |
7314 | return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; | |
7315 | } | |
7316 | ||
7317 | /* Work out how many page cache pages we can reclaim in this reclaim_mode */ | |
a5f5f91d | 7318 | static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) |
90afa5de | 7319 | { |
d031a157 AM |
7320 | unsigned long nr_pagecache_reclaimable; |
7321 | unsigned long delta = 0; | |
90afa5de MG |
7322 | |
7323 | /* | |
95bbc0c7 | 7324 | * If RECLAIM_UNMAP is set, then all file pages are considered |
90afa5de | 7325 | * potentially reclaimable. Otherwise, we have to worry about |
11fb9989 | 7326 | * pages like swapcache and node_unmapped_file_pages() provides |
90afa5de MG |
7327 | * a better estimate |
7328 | */ | |
a5f5f91d MG |
7329 | if (node_reclaim_mode & RECLAIM_UNMAP) |
7330 | nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); | |
90afa5de | 7331 | else |
a5f5f91d | 7332 | nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); |
90afa5de MG |
7333 | |
7334 | /* If we can't clean pages, remove dirty pages from consideration */ | |
a5f5f91d MG |
7335 | if (!(node_reclaim_mode & RECLAIM_WRITE)) |
7336 | delta += node_page_state(pgdat, NR_FILE_DIRTY); | |
90afa5de MG |
7337 | |
7338 | /* Watch for any possible underflows due to delta */ | |
7339 | if (unlikely(delta > nr_pagecache_reclaimable)) | |
7340 | delta = nr_pagecache_reclaimable; | |
7341 | ||
7342 | return nr_pagecache_reclaimable - delta; | |
7343 | } | |
7344 | ||
9eeff239 | 7345 | /* |
a5f5f91d | 7346 | * Try to free up some pages from this node through reclaim. |
9eeff239 | 7347 | */ |
a5f5f91d | 7348 | static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) |
9eeff239 | 7349 | { |
7fb2d46d | 7350 | /* Minimum pages needed in order to stay on node */ |
69e05944 | 7351 | const unsigned long nr_pages = 1 << order; |
9eeff239 | 7352 | struct task_struct *p = current; |
499118e9 | 7353 | unsigned int noreclaim_flag; |
179e9639 | 7354 | struct scan_control sc = { |
62b726c1 | 7355 | .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), |
f2f43e56 | 7356 | .gfp_mask = current_gfp_context(gfp_mask), |
bd2f6199 | 7357 | .order = order, |
a5f5f91d MG |
7358 | .priority = NODE_RECLAIM_PRIORITY, |
7359 | .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), | |
7360 | .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), | |
ee814fe2 | 7361 | .may_swap = 1, |
f2f43e56 | 7362 | .reclaim_idx = gfp_zone(gfp_mask), |
179e9639 | 7363 | }; |
57f29762 | 7364 | unsigned long pflags; |
9eeff239 | 7365 | |
132bb8cf YS |
7366 | trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order, |
7367 | sc.gfp_mask); | |
7368 | ||
9eeff239 | 7369 | cond_resched(); |
57f29762 | 7370 | psi_memstall_enter(&pflags); |
76a0fb4f | 7371 | delayacct_freepages_start(); |
93781325 | 7372 | fs_reclaim_acquire(sc.gfp_mask); |
d4f7796e | 7373 | /* |
95bbc0c7 | 7374 | * We need to be able to allocate from the reserves for RECLAIM_UNMAP |
d4f7796e | 7375 | */ |
499118e9 | 7376 | noreclaim_flag = memalloc_noreclaim_save(); |
1732d2b0 | 7377 | set_task_reclaim_state(p, &sc.reclaim_state); |
c84db23c | 7378 | |
d8ff6fde ML |
7379 | if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages || |
7380 | node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) > pgdat->min_slab_pages) { | |
0ff38490 | 7381 | /* |
894befec | 7382 | * Free memory by calling shrink node with increasing |
0ff38490 CL |
7383 | * priorities until we have enough memory freed. |
7384 | */ | |
0ff38490 | 7385 | do { |
970a39a3 | 7386 | shrink_node(pgdat, &sc); |
9e3b2f8c | 7387 | } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); |
0ff38490 | 7388 | } |
c84db23c | 7389 | |
1732d2b0 | 7390 | set_task_reclaim_state(p, NULL); |
499118e9 | 7391 | memalloc_noreclaim_restore(noreclaim_flag); |
93781325 | 7392 | fs_reclaim_release(sc.gfp_mask); |
57f29762 | 7393 | psi_memstall_leave(&pflags); |
76a0fb4f | 7394 | delayacct_freepages_end(); |
132bb8cf YS |
7395 | |
7396 | trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed); | |
7397 | ||
a79311c1 | 7398 | return sc.nr_reclaimed >= nr_pages; |
9eeff239 | 7399 | } |
179e9639 | 7400 | |
a5f5f91d | 7401 | int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) |
179e9639 | 7402 | { |
d773ed6b | 7403 | int ret; |
179e9639 AM |
7404 | |
7405 | /* | |
a5f5f91d | 7406 | * Node reclaim reclaims unmapped file backed pages and |
0ff38490 | 7407 | * slab pages if we are over the defined limits. |
34aa1330 | 7408 | * |
9614634f CL |
7409 | * A small portion of unmapped file backed pages is needed for |
7410 | * file I/O otherwise pages read by file I/O will be immediately | |
a5f5f91d MG |
7411 | * thrown out if the node is overallocated. So we do not reclaim |
7412 | * if less than a specified percentage of the node is used by | |
9614634f | 7413 | * unmapped file backed pages. |
179e9639 | 7414 | */ |
a5f5f91d | 7415 | if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && |
d42f3245 RG |
7416 | node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <= |
7417 | pgdat->min_slab_pages) | |
a5f5f91d | 7418 | return NODE_RECLAIM_FULL; |
179e9639 AM |
7419 | |
7420 | /* | |
d773ed6b | 7421 | * Do not scan if the allocation should not be delayed. |
179e9639 | 7422 | */ |
d0164adc | 7423 | if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) |
a5f5f91d | 7424 | return NODE_RECLAIM_NOSCAN; |
179e9639 AM |
7425 | |
7426 | /* | |
a5f5f91d | 7427 | * Only run node reclaim on the local node or on nodes that do not |
179e9639 AM |
7428 | * have associated processors. This will favor the local processor |
7429 | * over remote processors and spread off node memory allocations | |
7430 | * as wide as possible. | |
7431 | */ | |
a5f5f91d MG |
7432 | if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) |
7433 | return NODE_RECLAIM_NOSCAN; | |
d773ed6b | 7434 | |
a5f5f91d MG |
7435 | if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) |
7436 | return NODE_RECLAIM_NOSCAN; | |
fa5e084e | 7437 | |
a5f5f91d MG |
7438 | ret = __node_reclaim(pgdat, gfp_mask, order); |
7439 | clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); | |
d773ed6b | 7440 | |
24cf7251 MG |
7441 | if (!ret) |
7442 | count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); | |
7443 | ||
d773ed6b | 7444 | return ret; |
179e9639 | 7445 | } |
9eeff239 | 7446 | #endif |
894bc310 | 7447 | |
89e004ea | 7448 | /** |
77414d19 MWO |
7449 | * check_move_unevictable_folios - Move evictable folios to appropriate zone |
7450 | * lru list | |
7451 | * @fbatch: Batch of lru folios to check. | |
89e004ea | 7452 | * |
77414d19 | 7453 | * Checks folios for evictability, if an evictable folio is in the unevictable |
64e3d12f | 7454 | * lru list, moves it to the appropriate evictable lru list. This function |
77414d19 | 7455 | * should be only used for lru folios. |
89e004ea | 7456 | */ |
77414d19 | 7457 | void check_move_unevictable_folios(struct folio_batch *fbatch) |
89e004ea | 7458 | { |
6168d0da | 7459 | struct lruvec *lruvec = NULL; |
24513264 HD |
7460 | int pgscanned = 0; |
7461 | int pgrescued = 0; | |
7462 | int i; | |
89e004ea | 7463 | |
77414d19 MWO |
7464 | for (i = 0; i < fbatch->nr; i++) { |
7465 | struct folio *folio = fbatch->folios[i]; | |
7466 | int nr_pages = folio_nr_pages(folio); | |
8d8869ca | 7467 | |
8d8869ca | 7468 | pgscanned += nr_pages; |
89e004ea | 7469 | |
77414d19 MWO |
7470 | /* block memcg migration while the folio moves between lrus */ |
7471 | if (!folio_test_clear_lru(folio)) | |
d25b5bd8 AS |
7472 | continue; |
7473 | ||
0de340cb | 7474 | lruvec = folio_lruvec_relock_irq(folio, lruvec); |
77414d19 MWO |
7475 | if (folio_evictable(folio) && folio_test_unevictable(folio)) { |
7476 | lruvec_del_folio(lruvec, folio); | |
7477 | folio_clear_unevictable(folio); | |
7478 | lruvec_add_folio(lruvec, folio); | |
8d8869ca | 7479 | pgrescued += nr_pages; |
89e004ea | 7480 | } |
77414d19 | 7481 | folio_set_lru(folio); |
24513264 | 7482 | } |
89e004ea | 7483 | |
6168d0da | 7484 | if (lruvec) { |
24513264 HD |
7485 | __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); |
7486 | __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); | |
6168d0da | 7487 | unlock_page_lruvec_irq(lruvec); |
d25b5bd8 AS |
7488 | } else if (pgscanned) { |
7489 | count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); | |
89e004ea | 7490 | } |
89e004ea | 7491 | } |
77414d19 | 7492 | EXPORT_SYMBOL_GPL(check_move_unevictable_folios); |