]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * linux/mm/swap.c | |
4 | * | |
5 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds | |
6 | */ | |
7 | ||
8 | /* | |
183ff22b | 9 | * This file contains the default values for the operation of the |
1da177e4 | 10 | * Linux VM subsystem. Fine-tuning documentation can be found in |
57043247 | 11 | * Documentation/admin-guide/sysctl/vm.rst. |
1da177e4 LT |
12 | * Started 18.12.91 |
13 | * Swap aging added 23.2.95, Stephen Tweedie. | |
14 | * Buffermem limits added 12.3.98, Rik van Riel. | |
15 | */ | |
16 | ||
17 | #include <linux/mm.h> | |
18 | #include <linux/sched.h> | |
19 | #include <linux/kernel_stat.h> | |
20 | #include <linux/swap.h> | |
21 | #include <linux/mman.h> | |
22 | #include <linux/pagemap.h> | |
23 | #include <linux/pagevec.h> | |
24 | #include <linux/init.h> | |
b95f1b31 | 25 | #include <linux/export.h> |
1da177e4 | 26 | #include <linux/mm_inline.h> |
1da177e4 | 27 | #include <linux/percpu_counter.h> |
3565fce3 | 28 | #include <linux/memremap.h> |
1da177e4 LT |
29 | #include <linux/percpu.h> |
30 | #include <linux/cpu.h> | |
31 | #include <linux/notifier.h> | |
e0bf68dd | 32 | #include <linux/backing-dev.h> |
66e1707b | 33 | #include <linux/memcontrol.h> |
5a0e3ad6 | 34 | #include <linux/gfp.h> |
a27bb332 | 35 | #include <linux/uio.h> |
822fc613 | 36 | #include <linux/hugetlb.h> |
33c3fc71 | 37 | #include <linux/page_idle.h> |
b01b2141 | 38 | #include <linux/local_lock.h> |
8cc621d2 | 39 | #include <linux/buffer_head.h> |
1da177e4 | 40 | |
64d6519d LS |
41 | #include "internal.h" |
42 | ||
c6286c98 MG |
43 | #define CREATE_TRACE_POINTS |
44 | #include <trace/events/pagemap.h> | |
45 | ||
1da177e4 LT |
46 | /* How many pages do we try to swap or page in/out together? */ |
47 | int page_cluster; | |
48 | ||
b01b2141 IM |
49 | /* Protecting only lru_rotate.pvec which requires disabling interrupts */ |
50 | struct lru_rotate { | |
51 | local_lock_t lock; | |
52 | struct pagevec pvec; | |
53 | }; | |
54 | static DEFINE_PER_CPU(struct lru_rotate, lru_rotate) = { | |
55 | .lock = INIT_LOCAL_LOCK(lock), | |
56 | }; | |
57 | ||
58 | /* | |
59 | * The following struct pagevec are grouped together because they are protected | |
60 | * by disabling preemption (and interrupts remain enabled). | |
61 | */ | |
62 | struct lru_pvecs { | |
63 | local_lock_t lock; | |
64 | struct pagevec lru_add; | |
65 | struct pagevec lru_deactivate_file; | |
66 | struct pagevec lru_deactivate; | |
67 | struct pagevec lru_lazyfree; | |
a4a921aa | 68 | #ifdef CONFIG_SMP |
b01b2141 | 69 | struct pagevec activate_page; |
a4a921aa | 70 | #endif |
b01b2141 IM |
71 | }; |
72 | static DEFINE_PER_CPU(struct lru_pvecs, lru_pvecs) = { | |
73 | .lock = INIT_LOCAL_LOCK(lock), | |
74 | }; | |
902aaed0 | 75 | |
b221385b | 76 | /* |
b109b870 HD |
77 | * This path almost never happens for VM activity - pages are normally freed |
78 | * via pagevecs. But it gets used by networking - and for compound pages. | |
b221385b | 79 | */ |
920c7a5d | 80 | static void __page_cache_release(struct page *page) |
b221385b AB |
81 | { |
82 | if (PageLRU(page)) { | |
e809c3fe | 83 | struct folio *folio = page_folio(page); |
fa9add64 HD |
84 | struct lruvec *lruvec; |
85 | unsigned long flags; | |
b221385b | 86 | |
e809c3fe | 87 | lruvec = folio_lruvec_lock_irqsave(folio, &flags); |
46ae6b2c | 88 | del_page_from_lru_list(page, lruvec); |
87560179 | 89 | __clear_page_lru_flags(page); |
6168d0da | 90 | unlock_page_lruvec_irqrestore(lruvec, flags); |
b221385b | 91 | } |
b109b870 HD |
92 | /* See comment on PageMlocked in release_pages() */ |
93 | if (unlikely(PageMlocked(page))) { | |
94 | int nr_pages = thp_nr_pages(page); | |
95 | ||
96 | __ClearPageMlocked(page); | |
97 | mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages); | |
98 | count_vm_events(UNEVICTABLE_PGCLEARED, nr_pages); | |
99 | } | |
62906027 | 100 | __ClearPageWaiters(page); |
91807063 AA |
101 | } |
102 | ||
103 | static void __put_single_page(struct page *page) | |
104 | { | |
105 | __page_cache_release(page); | |
bbc6b703 | 106 | mem_cgroup_uncharge(page_folio(page)); |
44042b44 | 107 | free_unref_page(page, 0); |
b221385b AB |
108 | } |
109 | ||
91807063 | 110 | static void __put_compound_page(struct page *page) |
1da177e4 | 111 | { |
822fc613 NH |
112 | /* |
113 | * __page_cache_release() is supposed to be called for thp, not for | |
114 | * hugetlb. This is because hugetlb page does never have PageLRU set | |
115 | * (it's never listed to any LRU lists) and no memcg routines should | |
116 | * be called for hugetlb (it has a separate hugetlb_cgroup.) | |
117 | */ | |
118 | if (!PageHuge(page)) | |
119 | __page_cache_release(page); | |
ff45fc3c | 120 | destroy_compound_page(page); |
91807063 AA |
121 | } |
122 | ||
ddc58f27 | 123 | void __put_page(struct page *page) |
8519fb30 | 124 | { |
71389703 DW |
125 | if (is_zone_device_page(page)) { |
126 | put_dev_pagemap(page->pgmap); | |
127 | ||
128 | /* | |
129 | * The page belongs to the device that created pgmap. Do | |
130 | * not return it to page allocator. | |
131 | */ | |
132 | return; | |
133 | } | |
134 | ||
8519fb30 | 135 | if (unlikely(PageCompound(page))) |
ddc58f27 KS |
136 | __put_compound_page(page); |
137 | else | |
91807063 | 138 | __put_single_page(page); |
1da177e4 | 139 | } |
ddc58f27 | 140 | EXPORT_SYMBOL(__put_page); |
70b50f94 | 141 | |
1d7ea732 | 142 | /** |
7682486b RD |
143 | * put_pages_list() - release a list of pages |
144 | * @pages: list of pages threaded on page->lru | |
1d7ea732 | 145 | * |
988c69f1 | 146 | * Release a list of pages which are strung together on page.lru. |
1d7ea732 AZ |
147 | */ |
148 | void put_pages_list(struct list_head *pages) | |
149 | { | |
988c69f1 MWO |
150 | struct page *page, *next; |
151 | ||
152 | list_for_each_entry_safe(page, next, pages, lru) { | |
153 | if (!put_page_testzero(page)) { | |
154 | list_del(&page->lru); | |
155 | continue; | |
156 | } | |
157 | if (PageHead(page)) { | |
158 | list_del(&page->lru); | |
159 | __put_compound_page(page); | |
160 | continue; | |
161 | } | |
162 | /* Cannot be PageLRU because it's passed to us using the lru */ | |
163 | __ClearPageWaiters(page); | |
1d7ea732 | 164 | } |
988c69f1 MWO |
165 | |
166 | free_unref_page_list(pages); | |
3cd018b4 | 167 | INIT_LIST_HEAD(pages); |
1d7ea732 AZ |
168 | } |
169 | EXPORT_SYMBOL(put_pages_list); | |
170 | ||
18022c5d MG |
171 | /* |
172 | * get_kernel_pages() - pin kernel pages in memory | |
173 | * @kiov: An array of struct kvec structures | |
174 | * @nr_segs: number of segments to pin | |
175 | * @write: pinning for read/write, currently ignored | |
176 | * @pages: array that receives pointers to the pages pinned. | |
177 | * Should be at least nr_segs long. | |
178 | * | |
179 | * Returns number of pages pinned. This may be fewer than the number | |
180 | * requested. If nr_pages is 0 or negative, returns 0. If no pages | |
181 | * were pinned, returns -errno. Each page returned must be released | |
182 | * with a put_page() call when it is finished with. | |
183 | */ | |
184 | int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write, | |
185 | struct page **pages) | |
186 | { | |
187 | int seg; | |
188 | ||
189 | for (seg = 0; seg < nr_segs; seg++) { | |
190 | if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE)) | |
191 | return seg; | |
192 | ||
5a178119 | 193 | pages[seg] = kmap_to_page(kiov[seg].iov_base); |
09cbfeaf | 194 | get_page(pages[seg]); |
18022c5d MG |
195 | } |
196 | ||
197 | return seg; | |
198 | } | |
199 | EXPORT_SYMBOL_GPL(get_kernel_pages); | |
200 | ||
3dd7ae8e | 201 | static void pagevec_lru_move_fn(struct pagevec *pvec, |
c7c7b80c | 202 | void (*move_fn)(struct page *page, struct lruvec *lruvec)) |
902aaed0 HH |
203 | { |
204 | int i; | |
6168d0da | 205 | struct lruvec *lruvec = NULL; |
3dd7ae8e | 206 | unsigned long flags = 0; |
902aaed0 HH |
207 | |
208 | for (i = 0; i < pagevec_count(pvec); i++) { | |
209 | struct page *page = pvec->pages[i]; | |
0de340cb | 210 | struct folio *folio = page_folio(page); |
3dd7ae8e | 211 | |
fc574c23 AS |
212 | /* block memcg migration during page moving between lru */ |
213 | if (!TestClearPageLRU(page)) | |
214 | continue; | |
215 | ||
0de340cb | 216 | lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags); |
c7c7b80c | 217 | (*move_fn)(page, lruvec); |
fc574c23 AS |
218 | |
219 | SetPageLRU(page); | |
902aaed0 | 220 | } |
6168d0da AS |
221 | if (lruvec) |
222 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
c6f92f9f | 223 | release_pages(pvec->pages, pvec->nr); |
83896fb5 | 224 | pagevec_reinit(pvec); |
d8505dee SL |
225 | } |
226 | ||
c7c7b80c | 227 | static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec) |
3dd7ae8e | 228 | { |
575ced1c MWO |
229 | struct folio *folio = page_folio(page); |
230 | ||
231 | if (!folio_test_unevictable(folio)) { | |
232 | lruvec_del_folio(lruvec, folio); | |
233 | folio_clear_active(folio); | |
234 | lruvec_add_folio_tail(lruvec, folio); | |
235 | __count_vm_events(PGROTATED, folio_nr_pages(folio)); | |
3dd7ae8e SL |
236 | } |
237 | } | |
238 | ||
d479960e MK |
239 | /* return true if pagevec needs to drain */ |
240 | static bool pagevec_add_and_need_flush(struct pagevec *pvec, struct page *page) | |
241 | { | |
242 | bool ret = false; | |
243 | ||
244 | if (!pagevec_add(pvec, page) || PageCompound(page) || | |
245 | lru_cache_disabled()) | |
246 | ret = true; | |
247 | ||
248 | return ret; | |
249 | } | |
250 | ||
1da177e4 | 251 | /* |
575ced1c MWO |
252 | * Writeback is about to end against a folio which has been marked for |
253 | * immediate reclaim. If it still appears to be reclaimable, move it | |
254 | * to the tail of the inactive list. | |
c7c7b80c | 255 | * |
575ced1c | 256 | * folio_rotate_reclaimable() must disable IRQs, to prevent nasty races. |
1da177e4 | 257 | */ |
575ced1c | 258 | void folio_rotate_reclaimable(struct folio *folio) |
1da177e4 | 259 | { |
575ced1c MWO |
260 | if (!folio_test_locked(folio) && !folio_test_dirty(folio) && |
261 | !folio_test_unevictable(folio) && folio_test_lru(folio)) { | |
ac6aadb2 MS |
262 | struct pagevec *pvec; |
263 | unsigned long flags; | |
264 | ||
575ced1c | 265 | folio_get(folio); |
b01b2141 IM |
266 | local_lock_irqsave(&lru_rotate.lock, flags); |
267 | pvec = this_cpu_ptr(&lru_rotate.pvec); | |
575ced1c | 268 | if (pagevec_add_and_need_flush(pvec, &folio->page)) |
c7c7b80c | 269 | pagevec_lru_move_fn(pvec, pagevec_move_tail_fn); |
b01b2141 | 270 | local_unlock_irqrestore(&lru_rotate.lock, flags); |
ac6aadb2 | 271 | } |
1da177e4 LT |
272 | } |
273 | ||
96f8bf4f | 274 | void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages) |
3e2f41f1 | 275 | { |
7cf111bc JW |
276 | do { |
277 | unsigned long lrusize; | |
278 | ||
6168d0da AS |
279 | /* |
280 | * Hold lruvec->lru_lock is safe here, since | |
281 | * 1) The pinned lruvec in reclaim, or | |
282 | * 2) From a pre-LRU page during refault (which also holds the | |
283 | * rcu lock, so would be safe even if the page was on the LRU | |
284 | * and could move simultaneously to a new lruvec). | |
285 | */ | |
286 | spin_lock_irq(&lruvec->lru_lock); | |
7cf111bc | 287 | /* Record cost event */ |
96f8bf4f JW |
288 | if (file) |
289 | lruvec->file_cost += nr_pages; | |
7cf111bc | 290 | else |
96f8bf4f | 291 | lruvec->anon_cost += nr_pages; |
7cf111bc JW |
292 | |
293 | /* | |
294 | * Decay previous events | |
295 | * | |
296 | * Because workloads change over time (and to avoid | |
297 | * overflow) we keep these statistics as a floating | |
298 | * average, which ends up weighing recent refaults | |
299 | * more than old ones. | |
300 | */ | |
301 | lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) + | |
302 | lruvec_page_state(lruvec, NR_ACTIVE_ANON) + | |
303 | lruvec_page_state(lruvec, NR_INACTIVE_FILE) + | |
304 | lruvec_page_state(lruvec, NR_ACTIVE_FILE); | |
305 | ||
306 | if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) { | |
307 | lruvec->file_cost /= 2; | |
308 | lruvec->anon_cost /= 2; | |
309 | } | |
6168d0da | 310 | spin_unlock_irq(&lruvec->lru_lock); |
7cf111bc | 311 | } while ((lruvec = parent_lruvec(lruvec))); |
3e2f41f1 KM |
312 | } |
313 | ||
0995d7e5 | 314 | void lru_note_cost_folio(struct folio *folio) |
96f8bf4f | 315 | { |
0995d7e5 MWO |
316 | lru_note_cost(folio_lruvec(folio), folio_is_file_lru(folio), |
317 | folio_nr_pages(folio)); | |
96f8bf4f JW |
318 | } |
319 | ||
f2d27392 | 320 | static void __folio_activate(struct folio *folio, struct lruvec *lruvec) |
1da177e4 | 321 | { |
f2d27392 MWO |
322 | if (!folio_test_active(folio) && !folio_test_unevictable(folio)) { |
323 | long nr_pages = folio_nr_pages(folio); | |
744ed144 | 324 | |
f2d27392 MWO |
325 | lruvec_del_folio(lruvec, folio); |
326 | folio_set_active(folio); | |
327 | lruvec_add_folio(lruvec, folio); | |
328 | trace_mm_lru_activate(folio); | |
4f98a2fe | 329 | |
21e330fc SB |
330 | __count_vm_events(PGACTIVATE, nr_pages); |
331 | __count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE, | |
332 | nr_pages); | |
1da177e4 | 333 | } |
eb709b0d SL |
334 | } |
335 | ||
336 | #ifdef CONFIG_SMP | |
f2d27392 MWO |
337 | static void __activate_page(struct page *page, struct lruvec *lruvec) |
338 | { | |
339 | return __folio_activate(page_folio(page), lruvec); | |
340 | } | |
341 | ||
eb709b0d SL |
342 | static void activate_page_drain(int cpu) |
343 | { | |
b01b2141 | 344 | struct pagevec *pvec = &per_cpu(lru_pvecs.activate_page, cpu); |
eb709b0d SL |
345 | |
346 | if (pagevec_count(pvec)) | |
c7c7b80c | 347 | pagevec_lru_move_fn(pvec, __activate_page); |
eb709b0d SL |
348 | } |
349 | ||
5fbc4616 CM |
350 | static bool need_activate_page_drain(int cpu) |
351 | { | |
b01b2141 | 352 | return pagevec_count(&per_cpu(lru_pvecs.activate_page, cpu)) != 0; |
5fbc4616 CM |
353 | } |
354 | ||
f2d27392 | 355 | static void folio_activate(struct folio *folio) |
eb709b0d | 356 | { |
f2d27392 MWO |
357 | if (folio_test_lru(folio) && !folio_test_active(folio) && |
358 | !folio_test_unevictable(folio)) { | |
b01b2141 | 359 | struct pagevec *pvec; |
eb709b0d | 360 | |
f2d27392 | 361 | folio_get(folio); |
b01b2141 IM |
362 | local_lock(&lru_pvecs.lock); |
363 | pvec = this_cpu_ptr(&lru_pvecs.activate_page); | |
f2d27392 | 364 | if (pagevec_add_and_need_flush(pvec, &folio->page)) |
c7c7b80c | 365 | pagevec_lru_move_fn(pvec, __activate_page); |
b01b2141 | 366 | local_unlock(&lru_pvecs.lock); |
eb709b0d SL |
367 | } |
368 | } | |
369 | ||
370 | #else | |
371 | static inline void activate_page_drain(int cpu) | |
372 | { | |
373 | } | |
374 | ||
f2d27392 | 375 | static void folio_activate(struct folio *folio) |
eb709b0d | 376 | { |
6168d0da | 377 | struct lruvec *lruvec; |
eb709b0d | 378 | |
f2d27392 | 379 | if (folio_test_clear_lru(folio)) { |
e809c3fe | 380 | lruvec = folio_lruvec_lock_irq(folio); |
f2d27392 | 381 | __folio_activate(folio, lruvec); |
6168d0da | 382 | unlock_page_lruvec_irq(lruvec); |
f2d27392 | 383 | folio_set_lru(folio); |
6168d0da | 384 | } |
1da177e4 | 385 | } |
eb709b0d | 386 | #endif |
1da177e4 | 387 | |
76580b65 | 388 | static void __lru_cache_activate_folio(struct folio *folio) |
059285a2 | 389 | { |
b01b2141 | 390 | struct pagevec *pvec; |
059285a2 MG |
391 | int i; |
392 | ||
b01b2141 IM |
393 | local_lock(&lru_pvecs.lock); |
394 | pvec = this_cpu_ptr(&lru_pvecs.lru_add); | |
395 | ||
059285a2 MG |
396 | /* |
397 | * Search backwards on the optimistic assumption that the page being | |
398 | * activated has just been added to this pagevec. Note that only | |
399 | * the local pagevec is examined as a !PageLRU page could be in the | |
400 | * process of being released, reclaimed, migrated or on a remote | |
401 | * pagevec that is currently being drained. Furthermore, marking | |
402 | * a remote pagevec's page PageActive potentially hits a race where | |
403 | * a page is marked PageActive just after it is added to the inactive | |
404 | * list causing accounting errors and BUG_ON checks to trigger. | |
405 | */ | |
406 | for (i = pagevec_count(pvec) - 1; i >= 0; i--) { | |
407 | struct page *pagevec_page = pvec->pages[i]; | |
408 | ||
76580b65 MWO |
409 | if (pagevec_page == &folio->page) { |
410 | folio_set_active(folio); | |
059285a2 MG |
411 | break; |
412 | } | |
413 | } | |
414 | ||
b01b2141 | 415 | local_unlock(&lru_pvecs.lock); |
059285a2 MG |
416 | } |
417 | ||
1da177e4 LT |
418 | /* |
419 | * Mark a page as having seen activity. | |
420 | * | |
421 | * inactive,unreferenced -> inactive,referenced | |
422 | * inactive,referenced -> active,unreferenced | |
423 | * active,unreferenced -> active,referenced | |
eb39d618 HD |
424 | * |
425 | * When a newly allocated page is not yet visible, so safe for non-atomic ops, | |
426 | * __SetPageReferenced(page) may be substituted for mark_page_accessed(page). | |
1da177e4 | 427 | */ |
76580b65 | 428 | void folio_mark_accessed(struct folio *folio) |
1da177e4 | 429 | { |
76580b65 MWO |
430 | if (!folio_test_referenced(folio)) { |
431 | folio_set_referenced(folio); | |
432 | } else if (folio_test_unevictable(folio)) { | |
a1100a74 FW |
433 | /* |
434 | * Unevictable pages are on the "LRU_UNEVICTABLE" list. But, | |
435 | * this list is never rotated or maintained, so marking an | |
436 | * evictable page accessed has no effect. | |
437 | */ | |
76580b65 | 438 | } else if (!folio_test_active(folio)) { |
059285a2 MG |
439 | /* |
440 | * If the page is on the LRU, queue it for activation via | |
b01b2141 | 441 | * lru_pvecs.activate_page. Otherwise, assume the page is on a |
059285a2 MG |
442 | * pagevec, mark it active and it'll be moved to the active |
443 | * LRU on the next drain. | |
444 | */ | |
76580b65 MWO |
445 | if (folio_test_lru(folio)) |
446 | folio_activate(folio); | |
059285a2 | 447 | else |
76580b65 MWO |
448 | __lru_cache_activate_folio(folio); |
449 | folio_clear_referenced(folio); | |
450 | workingset_activation(folio); | |
1da177e4 | 451 | } |
76580b65 MWO |
452 | if (folio_test_idle(folio)) |
453 | folio_clear_idle(folio); | |
1da177e4 | 454 | } |
76580b65 | 455 | EXPORT_SYMBOL(folio_mark_accessed); |
1da177e4 | 456 | |
f04e9ebb | 457 | /** |
0d31125d MWO |
458 | * folio_add_lru - Add a folio to an LRU list. |
459 | * @folio: The folio to be added to the LRU. | |
2329d375 | 460 | * |
0d31125d | 461 | * Queue the folio for addition to the LRU. The decision on whether |
2329d375 | 462 | * to add the page to the [in]active [file|anon] list is deferred until the |
0d31125d MWO |
463 | * pagevec is drained. This gives a chance for the caller of folio_add_lru() |
464 | * have the folio added to the active list using folio_mark_accessed(). | |
f04e9ebb | 465 | */ |
0d31125d | 466 | void folio_add_lru(struct folio *folio) |
1da177e4 | 467 | { |
6058eaec JW |
468 | struct pagevec *pvec; |
469 | ||
0d31125d MWO |
470 | VM_BUG_ON_FOLIO(folio_test_active(folio) && folio_test_unevictable(folio), folio); |
471 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); | |
6058eaec | 472 | |
0d31125d | 473 | folio_get(folio); |
6058eaec JW |
474 | local_lock(&lru_pvecs.lock); |
475 | pvec = this_cpu_ptr(&lru_pvecs.lru_add); | |
0d31125d | 476 | if (pagevec_add_and_need_flush(pvec, &folio->page)) |
6058eaec JW |
477 | __pagevec_lru_add(pvec); |
478 | local_unlock(&lru_pvecs.lock); | |
1da177e4 | 479 | } |
0d31125d | 480 | EXPORT_SYMBOL(folio_add_lru); |
1da177e4 | 481 | |
00501b53 | 482 | /** |
b518154e | 483 | * lru_cache_add_inactive_or_unevictable |
00501b53 JW |
484 | * @page: the page to be added to LRU |
485 | * @vma: vma in which page is mapped for determining reclaimability | |
486 | * | |
b518154e | 487 | * Place @page on the inactive or unevictable LRU list, depending on its |
12eab428 | 488 | * evictability. |
00501b53 | 489 | */ |
b518154e | 490 | void lru_cache_add_inactive_or_unevictable(struct page *page, |
00501b53 JW |
491 | struct vm_area_struct *vma) |
492 | { | |
b518154e JK |
493 | bool unevictable; |
494 | ||
00501b53 JW |
495 | VM_BUG_ON_PAGE(PageLRU(page), page); |
496 | ||
b518154e JK |
497 | unevictable = (vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED; |
498 | if (unlikely(unevictable) && !TestSetPageMlocked(page)) { | |
0964730b | 499 | int nr_pages = thp_nr_pages(page); |
b109b870 HD |
500 | |
501 | mod_zone_page_state(page_zone(page), NR_MLOCK, nr_pages); | |
0964730b | 502 | count_vm_events(UNEVICTABLE_PGMLOCKED, nr_pages); |
00501b53 | 503 | } |
9c4e6b1a | 504 | lru_cache_add(page); |
00501b53 JW |
505 | } |
506 | ||
31560180 MK |
507 | /* |
508 | * If the page can not be invalidated, it is moved to the | |
509 | * inactive list to speed up its reclaim. It is moved to the | |
510 | * head of the list, rather than the tail, to give the flusher | |
511 | * threads some time to write it out, as this is much more | |
512 | * effective than the single-page writeout from reclaim. | |
278df9f4 MK |
513 | * |
514 | * If the page isn't page_mapped and dirty/writeback, the page | |
515 | * could reclaim asap using PG_reclaim. | |
516 | * | |
517 | * 1. active, mapped page -> none | |
518 | * 2. active, dirty/writeback page -> inactive, head, PG_reclaim | |
519 | * 3. inactive, mapped page -> none | |
520 | * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim | |
521 | * 5. inactive, clean -> inactive, tail | |
522 | * 6. Others -> none | |
523 | * | |
524 | * In 4, why it moves inactive's head, the VM expects the page would | |
525 | * be write it out by flusher threads as this is much more effective | |
526 | * than the single-page writeout from reclaim. | |
31560180 | 527 | */ |
c7c7b80c | 528 | static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec) |
31560180 | 529 | { |
46ae6b2c | 530 | bool active = PageActive(page); |
6c357848 | 531 | int nr_pages = thp_nr_pages(page); |
31560180 | 532 | |
bad49d9c MK |
533 | if (PageUnevictable(page)) |
534 | return; | |
535 | ||
31560180 MK |
536 | /* Some processes are using the page */ |
537 | if (page_mapped(page)) | |
538 | return; | |
539 | ||
46ae6b2c | 540 | del_page_from_lru_list(page, lruvec); |
31560180 MK |
541 | ClearPageActive(page); |
542 | ClearPageReferenced(page); | |
31560180 | 543 | |
278df9f4 MK |
544 | if (PageWriteback(page) || PageDirty(page)) { |
545 | /* | |
546 | * PG_reclaim could be raced with end_page_writeback | |
547 | * It can make readahead confusing. But race window | |
548 | * is _really_ small and it's non-critical problem. | |
549 | */ | |
3a9c9788 | 550 | add_page_to_lru_list(page, lruvec); |
278df9f4 MK |
551 | SetPageReclaim(page); |
552 | } else { | |
553 | /* | |
554 | * The page's writeback ends up during pagevec | |
c4ffefd1 | 555 | * We move that page into tail of inactive. |
278df9f4 | 556 | */ |
3a9c9788 | 557 | add_page_to_lru_list_tail(page, lruvec); |
5d91f31f | 558 | __count_vm_events(PGROTATED, nr_pages); |
278df9f4 MK |
559 | } |
560 | ||
21e330fc | 561 | if (active) { |
5d91f31f | 562 | __count_vm_events(PGDEACTIVATE, nr_pages); |
21e330fc SB |
563 | __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, |
564 | nr_pages); | |
565 | } | |
31560180 MK |
566 | } |
567 | ||
c7c7b80c | 568 | static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec) |
9c276cc6 | 569 | { |
fc574c23 | 570 | if (PageActive(page) && !PageUnevictable(page)) { |
6c357848 | 571 | int nr_pages = thp_nr_pages(page); |
9c276cc6 | 572 | |
46ae6b2c | 573 | del_page_from_lru_list(page, lruvec); |
9c276cc6 MK |
574 | ClearPageActive(page); |
575 | ClearPageReferenced(page); | |
3a9c9788 | 576 | add_page_to_lru_list(page, lruvec); |
9c276cc6 | 577 | |
21e330fc SB |
578 | __count_vm_events(PGDEACTIVATE, nr_pages); |
579 | __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, | |
580 | nr_pages); | |
9c276cc6 MK |
581 | } |
582 | } | |
10853a03 | 583 | |
c7c7b80c | 584 | static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec) |
10853a03 | 585 | { |
fc574c23 | 586 | if (PageAnon(page) && PageSwapBacked(page) && |
24c92eb7 | 587 | !PageSwapCache(page) && !PageUnevictable(page)) { |
6c357848 | 588 | int nr_pages = thp_nr_pages(page); |
10853a03 | 589 | |
46ae6b2c | 590 | del_page_from_lru_list(page, lruvec); |
10853a03 MK |
591 | ClearPageActive(page); |
592 | ClearPageReferenced(page); | |
f7ad2a6c | 593 | /* |
9de4f22a YH |
594 | * Lazyfree pages are clean anonymous pages. They have |
595 | * PG_swapbacked flag cleared, to distinguish them from normal | |
596 | * anonymous pages | |
f7ad2a6c SL |
597 | */ |
598 | ClearPageSwapBacked(page); | |
3a9c9788 | 599 | add_page_to_lru_list(page, lruvec); |
10853a03 | 600 | |
21e330fc SB |
601 | __count_vm_events(PGLAZYFREE, nr_pages); |
602 | __count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE, | |
603 | nr_pages); | |
10853a03 MK |
604 | } |
605 | } | |
606 | ||
902aaed0 HH |
607 | /* |
608 | * Drain pages out of the cpu's pagevecs. | |
609 | * Either "cpu" is the current CPU, and preemption has already been | |
610 | * disabled; or "cpu" is being hot-unplugged, and is already dead. | |
611 | */ | |
f0cb3c76 | 612 | void lru_add_drain_cpu(int cpu) |
1da177e4 | 613 | { |
b01b2141 | 614 | struct pagevec *pvec = &per_cpu(lru_pvecs.lru_add, cpu); |
1da177e4 | 615 | |
13f7f789 | 616 | if (pagevec_count(pvec)) |
a0b8cab3 | 617 | __pagevec_lru_add(pvec); |
902aaed0 | 618 | |
b01b2141 | 619 | pvec = &per_cpu(lru_rotate.pvec, cpu); |
7e0cc01e QC |
620 | /* Disabling interrupts below acts as a compiler barrier. */ |
621 | if (data_race(pagevec_count(pvec))) { | |
902aaed0 HH |
622 | unsigned long flags; |
623 | ||
624 | /* No harm done if a racing interrupt already did this */ | |
b01b2141 | 625 | local_lock_irqsave(&lru_rotate.lock, flags); |
c7c7b80c | 626 | pagevec_lru_move_fn(pvec, pagevec_move_tail_fn); |
b01b2141 | 627 | local_unlock_irqrestore(&lru_rotate.lock, flags); |
902aaed0 | 628 | } |
31560180 | 629 | |
b01b2141 | 630 | pvec = &per_cpu(lru_pvecs.lru_deactivate_file, cpu); |
31560180 | 631 | if (pagevec_count(pvec)) |
c7c7b80c | 632 | pagevec_lru_move_fn(pvec, lru_deactivate_file_fn); |
eb709b0d | 633 | |
b01b2141 | 634 | pvec = &per_cpu(lru_pvecs.lru_deactivate, cpu); |
9c276cc6 | 635 | if (pagevec_count(pvec)) |
c7c7b80c | 636 | pagevec_lru_move_fn(pvec, lru_deactivate_fn); |
9c276cc6 | 637 | |
b01b2141 | 638 | pvec = &per_cpu(lru_pvecs.lru_lazyfree, cpu); |
10853a03 | 639 | if (pagevec_count(pvec)) |
c7c7b80c | 640 | pagevec_lru_move_fn(pvec, lru_lazyfree_fn); |
10853a03 | 641 | |
eb709b0d | 642 | activate_page_drain(cpu); |
31560180 MK |
643 | } |
644 | ||
645 | /** | |
cc5993bd | 646 | * deactivate_file_page - forcefully deactivate a file page |
31560180 MK |
647 | * @page: page to deactivate |
648 | * | |
649 | * This function hints the VM that @page is a good reclaim candidate, | |
650 | * for example if its invalidation fails due to the page being dirty | |
651 | * or under writeback. | |
652 | */ | |
cc5993bd | 653 | void deactivate_file_page(struct page *page) |
31560180 | 654 | { |
821ed6bb | 655 | /* |
cc5993bd MK |
656 | * In a workload with many unevictable page such as mprotect, |
657 | * unevictable page deactivation for accelerating reclaim is pointless. | |
821ed6bb MK |
658 | */ |
659 | if (PageUnevictable(page)) | |
660 | return; | |
661 | ||
31560180 | 662 | if (likely(get_page_unless_zero(page))) { |
b01b2141 IM |
663 | struct pagevec *pvec; |
664 | ||
665 | local_lock(&lru_pvecs.lock); | |
666 | pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate_file); | |
31560180 | 667 | |
d479960e | 668 | if (pagevec_add_and_need_flush(pvec, page)) |
c7c7b80c | 669 | pagevec_lru_move_fn(pvec, lru_deactivate_file_fn); |
b01b2141 | 670 | local_unlock(&lru_pvecs.lock); |
31560180 | 671 | } |
80bfed90 AM |
672 | } |
673 | ||
9c276cc6 MK |
674 | /* |
675 | * deactivate_page - deactivate a page | |
676 | * @page: page to deactivate | |
677 | * | |
678 | * deactivate_page() moves @page to the inactive list if @page was on the active | |
679 | * list and was not an unevictable page. This is done to accelerate the reclaim | |
680 | * of @page. | |
681 | */ | |
682 | void deactivate_page(struct page *page) | |
683 | { | |
684 | if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) { | |
b01b2141 | 685 | struct pagevec *pvec; |
9c276cc6 | 686 | |
b01b2141 IM |
687 | local_lock(&lru_pvecs.lock); |
688 | pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate); | |
9c276cc6 | 689 | get_page(page); |
d479960e | 690 | if (pagevec_add_and_need_flush(pvec, page)) |
c7c7b80c | 691 | pagevec_lru_move_fn(pvec, lru_deactivate_fn); |
b01b2141 | 692 | local_unlock(&lru_pvecs.lock); |
9c276cc6 MK |
693 | } |
694 | } | |
695 | ||
10853a03 | 696 | /** |
f7ad2a6c | 697 | * mark_page_lazyfree - make an anon page lazyfree |
10853a03 MK |
698 | * @page: page to deactivate |
699 | * | |
f7ad2a6c SL |
700 | * mark_page_lazyfree() moves @page to the inactive file list. |
701 | * This is done to accelerate the reclaim of @page. | |
10853a03 | 702 | */ |
f7ad2a6c | 703 | void mark_page_lazyfree(struct page *page) |
10853a03 | 704 | { |
f7ad2a6c | 705 | if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) && |
24c92eb7 | 706 | !PageSwapCache(page) && !PageUnevictable(page)) { |
b01b2141 | 707 | struct pagevec *pvec; |
10853a03 | 708 | |
b01b2141 IM |
709 | local_lock(&lru_pvecs.lock); |
710 | pvec = this_cpu_ptr(&lru_pvecs.lru_lazyfree); | |
09cbfeaf | 711 | get_page(page); |
d479960e | 712 | if (pagevec_add_and_need_flush(pvec, page)) |
c7c7b80c | 713 | pagevec_lru_move_fn(pvec, lru_lazyfree_fn); |
b01b2141 | 714 | local_unlock(&lru_pvecs.lock); |
10853a03 MK |
715 | } |
716 | } | |
717 | ||
80bfed90 AM |
718 | void lru_add_drain(void) |
719 | { | |
b01b2141 IM |
720 | local_lock(&lru_pvecs.lock); |
721 | lru_add_drain_cpu(smp_processor_id()); | |
722 | local_unlock(&lru_pvecs.lock); | |
723 | } | |
724 | ||
243418e3 MK |
725 | /* |
726 | * It's called from per-cpu workqueue context in SMP case so | |
727 | * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on | |
728 | * the same cpu. It shouldn't be a problem in !SMP case since | |
729 | * the core is only one and the locks will disable preemption. | |
730 | */ | |
731 | static void lru_add_and_bh_lrus_drain(void) | |
732 | { | |
733 | local_lock(&lru_pvecs.lock); | |
734 | lru_add_drain_cpu(smp_processor_id()); | |
735 | local_unlock(&lru_pvecs.lock); | |
736 | invalidate_bh_lrus_cpu(); | |
737 | } | |
738 | ||
b01b2141 IM |
739 | void lru_add_drain_cpu_zone(struct zone *zone) |
740 | { | |
741 | local_lock(&lru_pvecs.lock); | |
742 | lru_add_drain_cpu(smp_processor_id()); | |
743 | drain_local_pages(zone); | |
744 | local_unlock(&lru_pvecs.lock); | |
1da177e4 LT |
745 | } |
746 | ||
6ea183d6 MH |
747 | #ifdef CONFIG_SMP |
748 | ||
749 | static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work); | |
750 | ||
c4028958 | 751 | static void lru_add_drain_per_cpu(struct work_struct *dummy) |
053837fc | 752 | { |
243418e3 | 753 | lru_add_and_bh_lrus_drain(); |
053837fc NP |
754 | } |
755 | ||
9852a721 MH |
756 | /* |
757 | * Doesn't need any cpu hotplug locking because we do rely on per-cpu | |
758 | * kworkers being shut down before our page_alloc_cpu_dead callback is | |
759 | * executed on the offlined cpu. | |
760 | * Calling this function with cpu hotplug locks held can actually lead | |
761 | * to obscure indirect dependencies via WQ context. | |
762 | */ | |
d479960e | 763 | inline void __lru_add_drain_all(bool force_all_cpus) |
053837fc | 764 | { |
6446a513 AD |
765 | /* |
766 | * lru_drain_gen - Global pages generation number | |
767 | * | |
768 | * (A) Definition: global lru_drain_gen = x implies that all generations | |
769 | * 0 < n <= x are already *scheduled* for draining. | |
770 | * | |
771 | * This is an optimization for the highly-contended use case where a | |
772 | * user space workload keeps constantly generating a flow of pages for | |
773 | * each CPU. | |
774 | */ | |
775 | static unsigned int lru_drain_gen; | |
5fbc4616 | 776 | static struct cpumask has_work; |
6446a513 AD |
777 | static DEFINE_MUTEX(lock); |
778 | unsigned cpu, this_gen; | |
5fbc4616 | 779 | |
ce612879 MH |
780 | /* |
781 | * Make sure nobody triggers this path before mm_percpu_wq is fully | |
782 | * initialized. | |
783 | */ | |
784 | if (WARN_ON(!mm_percpu_wq)) | |
785 | return; | |
786 | ||
6446a513 AD |
787 | /* |
788 | * Guarantee pagevec counter stores visible by this CPU are visible to | |
789 | * other CPUs before loading the current drain generation. | |
790 | */ | |
791 | smp_mb(); | |
792 | ||
793 | /* | |
794 | * (B) Locally cache global LRU draining generation number | |
795 | * | |
796 | * The read barrier ensures that the counter is loaded before the mutex | |
797 | * is taken. It pairs with smp_mb() inside the mutex critical section | |
798 | * at (D). | |
799 | */ | |
800 | this_gen = smp_load_acquire(&lru_drain_gen); | |
eef1a429 | 801 | |
5fbc4616 | 802 | mutex_lock(&lock); |
eef1a429 KK |
803 | |
804 | /* | |
6446a513 AD |
805 | * (C) Exit the draining operation if a newer generation, from another |
806 | * lru_add_drain_all(), was already scheduled for draining. Check (A). | |
eef1a429 | 807 | */ |
d479960e | 808 | if (unlikely(this_gen != lru_drain_gen && !force_all_cpus)) |
eef1a429 KK |
809 | goto done; |
810 | ||
6446a513 AD |
811 | /* |
812 | * (D) Increment global generation number | |
813 | * | |
814 | * Pairs with smp_load_acquire() at (B), outside of the critical | |
815 | * section. Use a full memory barrier to guarantee that the new global | |
816 | * drain generation number is stored before loading pagevec counters. | |
817 | * | |
818 | * This pairing must be done here, before the for_each_online_cpu loop | |
819 | * below which drains the page vectors. | |
820 | * | |
821 | * Let x, y, and z represent some system CPU numbers, where x < y < z. | |
cb152a1a | 822 | * Assume CPU #z is in the middle of the for_each_online_cpu loop |
6446a513 AD |
823 | * below and has already reached CPU #y's per-cpu data. CPU #x comes |
824 | * along, adds some pages to its per-cpu vectors, then calls | |
825 | * lru_add_drain_all(). | |
826 | * | |
827 | * If the paired barrier is done at any later step, e.g. after the | |
828 | * loop, CPU #x will just exit at (C) and miss flushing out all of its | |
829 | * added pages. | |
830 | */ | |
831 | WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1); | |
832 | smp_mb(); | |
eef1a429 | 833 | |
5fbc4616 | 834 | cpumask_clear(&has_work); |
5fbc4616 CM |
835 | for_each_online_cpu(cpu) { |
836 | struct work_struct *work = &per_cpu(lru_add_drain_work, cpu); | |
837 | ||
d479960e MK |
838 | if (force_all_cpus || |
839 | pagevec_count(&per_cpu(lru_pvecs.lru_add, cpu)) || | |
7e0cc01e | 840 | data_race(pagevec_count(&per_cpu(lru_rotate.pvec, cpu))) || |
b01b2141 IM |
841 | pagevec_count(&per_cpu(lru_pvecs.lru_deactivate_file, cpu)) || |
842 | pagevec_count(&per_cpu(lru_pvecs.lru_deactivate, cpu)) || | |
843 | pagevec_count(&per_cpu(lru_pvecs.lru_lazyfree, cpu)) || | |
8cc621d2 MK |
844 | need_activate_page_drain(cpu) || |
845 | has_bh_in_lru(cpu, NULL)) { | |
5fbc4616 | 846 | INIT_WORK(work, lru_add_drain_per_cpu); |
ce612879 | 847 | queue_work_on(cpu, mm_percpu_wq, work); |
6446a513 | 848 | __cpumask_set_cpu(cpu, &has_work); |
5fbc4616 CM |
849 | } |
850 | } | |
851 | ||
852 | for_each_cpu(cpu, &has_work) | |
853 | flush_work(&per_cpu(lru_add_drain_work, cpu)); | |
854 | ||
eef1a429 | 855 | done: |
5fbc4616 | 856 | mutex_unlock(&lock); |
053837fc | 857 | } |
d479960e MK |
858 | |
859 | void lru_add_drain_all(void) | |
860 | { | |
861 | __lru_add_drain_all(false); | |
862 | } | |
6ea183d6 MH |
863 | #else |
864 | void lru_add_drain_all(void) | |
865 | { | |
866 | lru_add_drain(); | |
867 | } | |
6446a513 | 868 | #endif /* CONFIG_SMP */ |
053837fc | 869 | |
d479960e MK |
870 | atomic_t lru_disable_count = ATOMIC_INIT(0); |
871 | ||
872 | /* | |
873 | * lru_cache_disable() needs to be called before we start compiling | |
874 | * a list of pages to be migrated using isolate_lru_page(). | |
875 | * It drains pages on LRU cache and then disable on all cpus until | |
876 | * lru_cache_enable is called. | |
877 | * | |
878 | * Must be paired with a call to lru_cache_enable(). | |
879 | */ | |
880 | void lru_cache_disable(void) | |
881 | { | |
882 | atomic_inc(&lru_disable_count); | |
883 | #ifdef CONFIG_SMP | |
884 | /* | |
885 | * lru_add_drain_all in the force mode will schedule draining on | |
886 | * all online CPUs so any calls of lru_cache_disabled wrapped by | |
887 | * local_lock or preemption disabled would be ordered by that. | |
888 | * The atomic operation doesn't need to have stronger ordering | |
0b8f0d87 | 889 | * requirements because that is enforced by the scheduling |
d479960e MK |
890 | * guarantees. |
891 | */ | |
892 | __lru_add_drain_all(true); | |
893 | #else | |
243418e3 | 894 | lru_add_and_bh_lrus_drain(); |
d479960e MK |
895 | #endif |
896 | } | |
897 | ||
aabfb572 | 898 | /** |
ea1754a0 | 899 | * release_pages - batched put_page() |
aabfb572 MH |
900 | * @pages: array of pages to release |
901 | * @nr: number of pages | |
1da177e4 | 902 | * |
aabfb572 MH |
903 | * Decrement the reference count on all the pages in @pages. If it |
904 | * fell to zero, remove the page from the LRU and free it. | |
1da177e4 | 905 | */ |
c6f92f9f | 906 | void release_pages(struct page **pages, int nr) |
1da177e4 LT |
907 | { |
908 | int i; | |
cc59850e | 909 | LIST_HEAD(pages_to_free); |
6168d0da | 910 | struct lruvec *lruvec = NULL; |
0de340cb | 911 | unsigned long flags = 0; |
3f649ab7 | 912 | unsigned int lock_batch; |
1da177e4 | 913 | |
1da177e4 LT |
914 | for (i = 0; i < nr; i++) { |
915 | struct page *page = pages[i]; | |
0de340cb | 916 | struct folio *folio = page_folio(page); |
1da177e4 | 917 | |
aabfb572 MH |
918 | /* |
919 | * Make sure the IRQ-safe lock-holding time does not get | |
920 | * excessive with a continuous string of pages from the | |
6168d0da | 921 | * same lruvec. The lock is held only if lruvec != NULL. |
aabfb572 | 922 | */ |
6168d0da AS |
923 | if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) { |
924 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
925 | lruvec = NULL; | |
aabfb572 MH |
926 | } |
927 | ||
0de340cb | 928 | page = &folio->page; |
6fcb52a5 | 929 | if (is_huge_zero_page(page)) |
aa88b68c | 930 | continue; |
aa88b68c | 931 | |
c5d6c45e | 932 | if (is_zone_device_page(page)) { |
6168d0da AS |
933 | if (lruvec) { |
934 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
935 | lruvec = NULL; | |
df6ad698 | 936 | } |
c5d6c45e IW |
937 | /* |
938 | * ZONE_DEVICE pages that return 'false' from | |
a3e7bea0 | 939 | * page_is_devmap_managed() do not require special |
c5d6c45e IW |
940 | * processing, and instead, expect a call to |
941 | * put_page_testzero(). | |
942 | */ | |
07d80269 JH |
943 | if (page_is_devmap_managed(page)) { |
944 | put_devmap_managed_page(page); | |
c5d6c45e | 945 | continue; |
07d80269 | 946 | } |
43fbdeb3 RC |
947 | if (put_page_testzero(page)) |
948 | put_dev_pagemap(page->pgmap); | |
949 | continue; | |
df6ad698 JG |
950 | } |
951 | ||
b5810039 | 952 | if (!put_page_testzero(page)) |
1da177e4 LT |
953 | continue; |
954 | ||
ddc58f27 | 955 | if (PageCompound(page)) { |
6168d0da AS |
956 | if (lruvec) { |
957 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
958 | lruvec = NULL; | |
ddc58f27 KS |
959 | } |
960 | __put_compound_page(page); | |
961 | continue; | |
962 | } | |
963 | ||
46453a6e | 964 | if (PageLRU(page)) { |
2a5e4e34 AD |
965 | struct lruvec *prev_lruvec = lruvec; |
966 | ||
0de340cb | 967 | lruvec = folio_lruvec_relock_irqsave(folio, lruvec, |
2a5e4e34 AD |
968 | &flags); |
969 | if (prev_lruvec != lruvec) | |
aabfb572 | 970 | lock_batch = 0; |
fa9add64 | 971 | |
46ae6b2c | 972 | del_page_from_lru_list(page, lruvec); |
87560179 | 973 | __clear_page_lru_flags(page); |
46453a6e NP |
974 | } |
975 | ||
b109b870 HD |
976 | /* |
977 | * In rare cases, when truncation or holepunching raced with | |
978 | * munlock after VM_LOCKED was cleared, Mlocked may still be | |
979 | * found set here. This does not indicate a problem, unless | |
980 | * "unevictable_pgs_cleared" appears worryingly large. | |
981 | */ | |
982 | if (unlikely(PageMlocked(page))) { | |
983 | __ClearPageMlocked(page); | |
984 | dec_zone_page_state(page, NR_MLOCK); | |
985 | count_vm_event(UNEVICTABLE_PGCLEARED); | |
986 | } | |
987 | ||
62906027 | 988 | __ClearPageWaiters(page); |
c53954a0 | 989 | |
cc59850e | 990 | list_add(&page->lru, &pages_to_free); |
1da177e4 | 991 | } |
6168d0da AS |
992 | if (lruvec) |
993 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
1da177e4 | 994 | |
747db954 | 995 | mem_cgroup_uncharge_list(&pages_to_free); |
2d4894b5 | 996 | free_unref_page_list(&pages_to_free); |
1da177e4 | 997 | } |
0be8557b | 998 | EXPORT_SYMBOL(release_pages); |
1da177e4 LT |
999 | |
1000 | /* | |
1001 | * The pages which we're about to release may be in the deferred lru-addition | |
1002 | * queues. That would prevent them from really being freed right now. That's | |
1003 | * OK from a correctness point of view but is inefficient - those pages may be | |
1004 | * cache-warm and we want to give them back to the page allocator ASAP. | |
1005 | * | |
1006 | * So __pagevec_release() will drain those queues here. __pagevec_lru_add() | |
1007 | * and __pagevec_lru_add_active() call release_pages() directly to avoid | |
1008 | * mutual recursion. | |
1009 | */ | |
1010 | void __pagevec_release(struct pagevec *pvec) | |
1011 | { | |
7f0b5fb9 | 1012 | if (!pvec->percpu_pvec_drained) { |
d9ed0d08 | 1013 | lru_add_drain(); |
7f0b5fb9 | 1014 | pvec->percpu_pvec_drained = true; |
d9ed0d08 | 1015 | } |
c6f92f9f | 1016 | release_pages(pvec->pages, pagevec_count(pvec)); |
1da177e4 LT |
1017 | pagevec_reinit(pvec); |
1018 | } | |
7f285701 SF |
1019 | EXPORT_SYMBOL(__pagevec_release); |
1020 | ||
934387c9 | 1021 | static void __pagevec_lru_add_fn(struct folio *folio, struct lruvec *lruvec) |
3dd7ae8e | 1022 | { |
934387c9 MWO |
1023 | int was_unevictable = folio_test_clear_unevictable(folio); |
1024 | long nr_pages = folio_nr_pages(folio); | |
3dd7ae8e | 1025 | |
934387c9 | 1026 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); |
3dd7ae8e | 1027 | |
9c4e6b1a | 1028 | /* |
934387c9 | 1029 | * A folio becomes evictable in two ways: |
dae966dc | 1030 | * 1) Within LRU lock [munlock_vma_page() and __munlock_pagevec()]. |
934387c9 MWO |
1031 | * 2) Before acquiring LRU lock to put the folio on the correct LRU |
1032 | * and then | |
9c4e6b1a SB |
1033 | * a) do PageLRU check with lock [check_move_unevictable_pages] |
1034 | * b) do PageLRU check before lock [clear_page_mlock] | |
1035 | * | |
1036 | * (1) & (2a) are ok as LRU lock will serialize them. For (2b), we need | |
1037 | * following strict ordering: | |
1038 | * | |
1039 | * #0: __pagevec_lru_add_fn #1: clear_page_mlock | |
1040 | * | |
934387c9 | 1041 | * folio_set_lru() folio_test_clear_mlocked() |
9c4e6b1a SB |
1042 | * smp_mb() // explicit ordering // above provides strict |
1043 | * // ordering | |
934387c9 | 1044 | * folio_test_mlocked() folio_test_lru() |
9c4e6b1a SB |
1045 | * |
1046 | * | |
934387c9 MWO |
1047 | * if '#1' does not observe setting of PG_lru by '#0' and |
1048 | * fails isolation, the explicit barrier will make sure that | |
1049 | * folio_evictable check will put the folio on the correct | |
1050 | * LRU. Without smp_mb(), folio_set_lru() can be reordered | |
1051 | * after folio_test_mlocked() check and can make '#1' fail the | |
1052 | * isolation of the folio whose mlocked bit is cleared (#0 is | |
1053 | * also looking at the same folio) and the evictable folio will | |
1054 | * be stranded on an unevictable LRU. | |
9c4e6b1a | 1055 | */ |
934387c9 | 1056 | folio_set_lru(folio); |
9a9b6cce | 1057 | smp_mb__after_atomic(); |
9c4e6b1a | 1058 | |
934387c9 | 1059 | if (folio_evictable(folio)) { |
9c4e6b1a | 1060 | if (was_unevictable) |
5d91f31f | 1061 | __count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages); |
9c4e6b1a | 1062 | } else { |
934387c9 MWO |
1063 | folio_clear_active(folio); |
1064 | folio_set_unevictable(folio); | |
07ca7606 | 1065 | folio->mlock_count = !!folio_test_mlocked(folio); |
9c4e6b1a | 1066 | if (!was_unevictable) |
5d91f31f | 1067 | __count_vm_events(UNEVICTABLE_PGCULLED, nr_pages); |
9c4e6b1a SB |
1068 | } |
1069 | ||
934387c9 MWO |
1070 | lruvec_add_folio(lruvec, folio); |
1071 | trace_mm_lru_insertion(folio); | |
3dd7ae8e SL |
1072 | } |
1073 | ||
1da177e4 LT |
1074 | /* |
1075 | * Add the passed pages to the LRU, then drop the caller's refcount | |
1076 | * on them. Reinitialises the caller's pagevec. | |
1077 | */ | |
a0b8cab3 | 1078 | void __pagevec_lru_add(struct pagevec *pvec) |
1da177e4 | 1079 | { |
fc574c23 | 1080 | int i; |
6168d0da | 1081 | struct lruvec *lruvec = NULL; |
fc574c23 AS |
1082 | unsigned long flags = 0; |
1083 | ||
1084 | for (i = 0; i < pagevec_count(pvec); i++) { | |
934387c9 | 1085 | struct folio *folio = page_folio(pvec->pages[i]); |
fc574c23 | 1086 | |
0de340cb | 1087 | lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags); |
934387c9 | 1088 | __pagevec_lru_add_fn(folio, lruvec); |
fc574c23 | 1089 | } |
6168d0da AS |
1090 | if (lruvec) |
1091 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
fc574c23 AS |
1092 | release_pages(pvec->pages, pvec->nr); |
1093 | pagevec_reinit(pvec); | |
1da177e4 | 1094 | } |
1da177e4 | 1095 | |
0cd6144a | 1096 | /** |
1613fac9 MWO |
1097 | * folio_batch_remove_exceptionals() - Prune non-folios from a batch. |
1098 | * @fbatch: The batch to prune | |
0cd6144a | 1099 | * |
1613fac9 MWO |
1100 | * find_get_entries() fills a batch with both folios and shadow/swap/DAX |
1101 | * entries. This function prunes all the non-folio entries from @fbatch | |
1102 | * without leaving holes, so that it can be passed on to folio-only batch | |
1103 | * operations. | |
0cd6144a | 1104 | */ |
1613fac9 | 1105 | void folio_batch_remove_exceptionals(struct folio_batch *fbatch) |
0cd6144a | 1106 | { |
1613fac9 | 1107 | unsigned int i, j; |
0cd6144a | 1108 | |
1613fac9 MWO |
1109 | for (i = 0, j = 0; i < folio_batch_count(fbatch); i++) { |
1110 | struct folio *folio = fbatch->folios[i]; | |
1111 | if (!xa_is_value(folio)) | |
1112 | fbatch->folios[j++] = folio; | |
0cd6144a | 1113 | } |
1613fac9 | 1114 | fbatch->nr = j; |
0cd6144a JW |
1115 | } |
1116 | ||
1da177e4 | 1117 | /** |
b947cee4 | 1118 | * pagevec_lookup_range - gang pagecache lookup |
1da177e4 LT |
1119 | * @pvec: Where the resulting pages are placed |
1120 | * @mapping: The address_space to search | |
1121 | * @start: The starting page index | |
b947cee4 | 1122 | * @end: The final page index |
1da177e4 | 1123 | * |
e02a9f04 | 1124 | * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE |
b947cee4 JK |
1125 | * pages in the mapping starting from index @start and upto index @end |
1126 | * (inclusive). The pages are placed in @pvec. pagevec_lookup() takes a | |
1da177e4 LT |
1127 | * reference against the pages in @pvec. |
1128 | * | |
1129 | * The search returns a group of mapping-contiguous pages with ascending | |
d72dc8a2 JK |
1130 | * indexes. There may be holes in the indices due to not-present pages. We |
1131 | * also update @start to index the next page for the traversal. | |
1da177e4 | 1132 | * |
b947cee4 | 1133 | * pagevec_lookup_range() returns the number of pages which were found. If this |
e02a9f04 | 1134 | * number is smaller than PAGEVEC_SIZE, the end of specified range has been |
b947cee4 | 1135 | * reached. |
1da177e4 | 1136 | */ |
b947cee4 | 1137 | unsigned pagevec_lookup_range(struct pagevec *pvec, |
397162ff | 1138 | struct address_space *mapping, pgoff_t *start, pgoff_t end) |
1da177e4 | 1139 | { |
397162ff | 1140 | pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE, |
b947cee4 | 1141 | pvec->pages); |
1da177e4 LT |
1142 | return pagevec_count(pvec); |
1143 | } | |
b947cee4 | 1144 | EXPORT_SYMBOL(pagevec_lookup_range); |
78539fdf | 1145 | |
72b045ae JK |
1146 | unsigned pagevec_lookup_range_tag(struct pagevec *pvec, |
1147 | struct address_space *mapping, pgoff_t *index, pgoff_t end, | |
10bbd235 | 1148 | xa_mark_t tag) |
1da177e4 | 1149 | { |
72b045ae | 1150 | pvec->nr = find_get_pages_range_tag(mapping, index, end, tag, |
67fd707f | 1151 | PAGEVEC_SIZE, pvec->pages); |
1da177e4 LT |
1152 | return pagevec_count(pvec); |
1153 | } | |
72b045ae | 1154 | EXPORT_SYMBOL(pagevec_lookup_range_tag); |
1da177e4 | 1155 | |
1da177e4 LT |
1156 | /* |
1157 | * Perform any setup for the swap system | |
1158 | */ | |
1159 | void __init swap_setup(void) | |
1160 | { | |
ca79b0c2 | 1161 | unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT); |
e0bf68dd | 1162 | |
1da177e4 LT |
1163 | /* Use a smaller cluster for small-memory machines */ |
1164 | if (megs < 16) | |
1165 | page_cluster = 2; | |
1166 | else | |
1167 | page_cluster = 3; | |
1168 | /* | |
1169 | * Right now other parts of the system means that we | |
1170 | * _really_ don't want to cluster much more | |
1171 | */ | |
1da177e4 | 1172 | } |
07d80269 JH |
1173 | |
1174 | #ifdef CONFIG_DEV_PAGEMAP_OPS | |
1175 | void put_devmap_managed_page(struct page *page) | |
1176 | { | |
1177 | int count; | |
1178 | ||
1179 | if (WARN_ON_ONCE(!page_is_devmap_managed(page))) | |
1180 | return; | |
1181 | ||
1182 | count = page_ref_dec_return(page); | |
1183 | ||
1184 | /* | |
1185 | * devmap page refcounts are 1-based, rather than 0-based: if | |
1186 | * refcount is 1, then the page is free and the refcount is | |
1187 | * stable because nobody holds a reference on the page. | |
1188 | */ | |
1189 | if (count == 1) | |
1190 | free_devmap_managed_page(page); | |
1191 | else if (!count) | |
1192 | __put_page(page); | |
1193 | } | |
1194 | EXPORT_SYMBOL(put_devmap_managed_page); | |
1195 | #endif |