]>
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 | { | |
493 | VM_BUG_ON_PAGE(PageLRU(page), page); | |
494 | ||
2fbb0c10 HD |
495 | if (unlikely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED)) |
496 | mlock_new_page(page); | |
497 | else | |
498 | lru_cache_add(page); | |
00501b53 JW |
499 | } |
500 | ||
31560180 MK |
501 | /* |
502 | * If the page can not be invalidated, it is moved to the | |
503 | * inactive list to speed up its reclaim. It is moved to the | |
504 | * head of the list, rather than the tail, to give the flusher | |
505 | * threads some time to write it out, as this is much more | |
506 | * effective than the single-page writeout from reclaim. | |
278df9f4 MK |
507 | * |
508 | * If the page isn't page_mapped and dirty/writeback, the page | |
509 | * could reclaim asap using PG_reclaim. | |
510 | * | |
511 | * 1. active, mapped page -> none | |
512 | * 2. active, dirty/writeback page -> inactive, head, PG_reclaim | |
513 | * 3. inactive, mapped page -> none | |
514 | * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim | |
515 | * 5. inactive, clean -> inactive, tail | |
516 | * 6. Others -> none | |
517 | * | |
518 | * In 4, why it moves inactive's head, the VM expects the page would | |
519 | * be write it out by flusher threads as this is much more effective | |
520 | * than the single-page writeout from reclaim. | |
31560180 | 521 | */ |
c7c7b80c | 522 | static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec) |
31560180 | 523 | { |
46ae6b2c | 524 | bool active = PageActive(page); |
6c357848 | 525 | int nr_pages = thp_nr_pages(page); |
31560180 | 526 | |
bad49d9c MK |
527 | if (PageUnevictable(page)) |
528 | return; | |
529 | ||
31560180 MK |
530 | /* Some processes are using the page */ |
531 | if (page_mapped(page)) | |
532 | return; | |
533 | ||
46ae6b2c | 534 | del_page_from_lru_list(page, lruvec); |
31560180 MK |
535 | ClearPageActive(page); |
536 | ClearPageReferenced(page); | |
31560180 | 537 | |
278df9f4 MK |
538 | if (PageWriteback(page) || PageDirty(page)) { |
539 | /* | |
540 | * PG_reclaim could be raced with end_page_writeback | |
541 | * It can make readahead confusing. But race window | |
542 | * is _really_ small and it's non-critical problem. | |
543 | */ | |
3a9c9788 | 544 | add_page_to_lru_list(page, lruvec); |
278df9f4 MK |
545 | SetPageReclaim(page); |
546 | } else { | |
547 | /* | |
548 | * The page's writeback ends up during pagevec | |
c4ffefd1 | 549 | * We move that page into tail of inactive. |
278df9f4 | 550 | */ |
3a9c9788 | 551 | add_page_to_lru_list_tail(page, lruvec); |
5d91f31f | 552 | __count_vm_events(PGROTATED, nr_pages); |
278df9f4 MK |
553 | } |
554 | ||
21e330fc | 555 | if (active) { |
5d91f31f | 556 | __count_vm_events(PGDEACTIVATE, nr_pages); |
21e330fc SB |
557 | __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, |
558 | nr_pages); | |
559 | } | |
31560180 MK |
560 | } |
561 | ||
c7c7b80c | 562 | static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec) |
9c276cc6 | 563 | { |
fc574c23 | 564 | if (PageActive(page) && !PageUnevictable(page)) { |
6c357848 | 565 | int nr_pages = thp_nr_pages(page); |
9c276cc6 | 566 | |
46ae6b2c | 567 | del_page_from_lru_list(page, lruvec); |
9c276cc6 MK |
568 | ClearPageActive(page); |
569 | ClearPageReferenced(page); | |
3a9c9788 | 570 | add_page_to_lru_list(page, lruvec); |
9c276cc6 | 571 | |
21e330fc SB |
572 | __count_vm_events(PGDEACTIVATE, nr_pages); |
573 | __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, | |
574 | nr_pages); | |
9c276cc6 MK |
575 | } |
576 | } | |
10853a03 | 577 | |
c7c7b80c | 578 | static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec) |
10853a03 | 579 | { |
fc574c23 | 580 | if (PageAnon(page) && PageSwapBacked(page) && |
24c92eb7 | 581 | !PageSwapCache(page) && !PageUnevictable(page)) { |
6c357848 | 582 | int nr_pages = thp_nr_pages(page); |
10853a03 | 583 | |
46ae6b2c | 584 | del_page_from_lru_list(page, lruvec); |
10853a03 MK |
585 | ClearPageActive(page); |
586 | ClearPageReferenced(page); | |
f7ad2a6c | 587 | /* |
9de4f22a YH |
588 | * Lazyfree pages are clean anonymous pages. They have |
589 | * PG_swapbacked flag cleared, to distinguish them from normal | |
590 | * anonymous pages | |
f7ad2a6c SL |
591 | */ |
592 | ClearPageSwapBacked(page); | |
3a9c9788 | 593 | add_page_to_lru_list(page, lruvec); |
10853a03 | 594 | |
21e330fc SB |
595 | __count_vm_events(PGLAZYFREE, nr_pages); |
596 | __count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE, | |
597 | nr_pages); | |
10853a03 MK |
598 | } |
599 | } | |
600 | ||
902aaed0 HH |
601 | /* |
602 | * Drain pages out of the cpu's pagevecs. | |
603 | * Either "cpu" is the current CPU, and preemption has already been | |
604 | * disabled; or "cpu" is being hot-unplugged, and is already dead. | |
605 | */ | |
f0cb3c76 | 606 | void lru_add_drain_cpu(int cpu) |
1da177e4 | 607 | { |
b01b2141 | 608 | struct pagevec *pvec = &per_cpu(lru_pvecs.lru_add, cpu); |
1da177e4 | 609 | |
13f7f789 | 610 | if (pagevec_count(pvec)) |
a0b8cab3 | 611 | __pagevec_lru_add(pvec); |
902aaed0 | 612 | |
b01b2141 | 613 | pvec = &per_cpu(lru_rotate.pvec, cpu); |
7e0cc01e QC |
614 | /* Disabling interrupts below acts as a compiler barrier. */ |
615 | if (data_race(pagevec_count(pvec))) { | |
902aaed0 HH |
616 | unsigned long flags; |
617 | ||
618 | /* No harm done if a racing interrupt already did this */ | |
b01b2141 | 619 | local_lock_irqsave(&lru_rotate.lock, flags); |
c7c7b80c | 620 | pagevec_lru_move_fn(pvec, pagevec_move_tail_fn); |
b01b2141 | 621 | local_unlock_irqrestore(&lru_rotate.lock, flags); |
902aaed0 | 622 | } |
31560180 | 623 | |
b01b2141 | 624 | pvec = &per_cpu(lru_pvecs.lru_deactivate_file, cpu); |
31560180 | 625 | if (pagevec_count(pvec)) |
c7c7b80c | 626 | pagevec_lru_move_fn(pvec, lru_deactivate_file_fn); |
eb709b0d | 627 | |
b01b2141 | 628 | pvec = &per_cpu(lru_pvecs.lru_deactivate, cpu); |
9c276cc6 | 629 | if (pagevec_count(pvec)) |
c7c7b80c | 630 | pagevec_lru_move_fn(pvec, lru_deactivate_fn); |
9c276cc6 | 631 | |
b01b2141 | 632 | pvec = &per_cpu(lru_pvecs.lru_lazyfree, cpu); |
10853a03 | 633 | if (pagevec_count(pvec)) |
c7c7b80c | 634 | pagevec_lru_move_fn(pvec, lru_lazyfree_fn); |
10853a03 | 635 | |
eb709b0d | 636 | activate_page_drain(cpu); |
2fbb0c10 | 637 | mlock_page_drain(cpu); |
31560180 MK |
638 | } |
639 | ||
640 | /** | |
cc5993bd | 641 | * deactivate_file_page - forcefully deactivate a file page |
31560180 MK |
642 | * @page: page to deactivate |
643 | * | |
644 | * This function hints the VM that @page is a good reclaim candidate, | |
645 | * for example if its invalidation fails due to the page being dirty | |
646 | * or under writeback. | |
647 | */ | |
cc5993bd | 648 | void deactivate_file_page(struct page *page) |
31560180 | 649 | { |
821ed6bb | 650 | /* |
cc5993bd MK |
651 | * In a workload with many unevictable page such as mprotect, |
652 | * unevictable page deactivation for accelerating reclaim is pointless. | |
821ed6bb MK |
653 | */ |
654 | if (PageUnevictable(page)) | |
655 | return; | |
656 | ||
31560180 | 657 | if (likely(get_page_unless_zero(page))) { |
b01b2141 IM |
658 | struct pagevec *pvec; |
659 | ||
660 | local_lock(&lru_pvecs.lock); | |
661 | pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate_file); | |
31560180 | 662 | |
d479960e | 663 | if (pagevec_add_and_need_flush(pvec, page)) |
c7c7b80c | 664 | pagevec_lru_move_fn(pvec, lru_deactivate_file_fn); |
b01b2141 | 665 | local_unlock(&lru_pvecs.lock); |
31560180 | 666 | } |
80bfed90 AM |
667 | } |
668 | ||
9c276cc6 MK |
669 | /* |
670 | * deactivate_page - deactivate a page | |
671 | * @page: page to deactivate | |
672 | * | |
673 | * deactivate_page() moves @page to the inactive list if @page was on the active | |
674 | * list and was not an unevictable page. This is done to accelerate the reclaim | |
675 | * of @page. | |
676 | */ | |
677 | void deactivate_page(struct page *page) | |
678 | { | |
679 | if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) { | |
b01b2141 | 680 | struct pagevec *pvec; |
9c276cc6 | 681 | |
b01b2141 IM |
682 | local_lock(&lru_pvecs.lock); |
683 | pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate); | |
9c276cc6 | 684 | get_page(page); |
d479960e | 685 | if (pagevec_add_and_need_flush(pvec, page)) |
c7c7b80c | 686 | pagevec_lru_move_fn(pvec, lru_deactivate_fn); |
b01b2141 | 687 | local_unlock(&lru_pvecs.lock); |
9c276cc6 MK |
688 | } |
689 | } | |
690 | ||
10853a03 | 691 | /** |
f7ad2a6c | 692 | * mark_page_lazyfree - make an anon page lazyfree |
10853a03 MK |
693 | * @page: page to deactivate |
694 | * | |
f7ad2a6c SL |
695 | * mark_page_lazyfree() moves @page to the inactive file list. |
696 | * This is done to accelerate the reclaim of @page. | |
10853a03 | 697 | */ |
f7ad2a6c | 698 | void mark_page_lazyfree(struct page *page) |
10853a03 | 699 | { |
f7ad2a6c | 700 | if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) && |
24c92eb7 | 701 | !PageSwapCache(page) && !PageUnevictable(page)) { |
b01b2141 | 702 | struct pagevec *pvec; |
10853a03 | 703 | |
b01b2141 IM |
704 | local_lock(&lru_pvecs.lock); |
705 | pvec = this_cpu_ptr(&lru_pvecs.lru_lazyfree); | |
09cbfeaf | 706 | get_page(page); |
d479960e | 707 | if (pagevec_add_and_need_flush(pvec, page)) |
c7c7b80c | 708 | pagevec_lru_move_fn(pvec, lru_lazyfree_fn); |
b01b2141 | 709 | local_unlock(&lru_pvecs.lock); |
10853a03 MK |
710 | } |
711 | } | |
712 | ||
80bfed90 AM |
713 | void lru_add_drain(void) |
714 | { | |
b01b2141 IM |
715 | local_lock(&lru_pvecs.lock); |
716 | lru_add_drain_cpu(smp_processor_id()); | |
717 | local_unlock(&lru_pvecs.lock); | |
718 | } | |
719 | ||
243418e3 MK |
720 | /* |
721 | * It's called from per-cpu workqueue context in SMP case so | |
722 | * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on | |
723 | * the same cpu. It shouldn't be a problem in !SMP case since | |
724 | * the core is only one and the locks will disable preemption. | |
725 | */ | |
726 | static void lru_add_and_bh_lrus_drain(void) | |
727 | { | |
728 | local_lock(&lru_pvecs.lock); | |
729 | lru_add_drain_cpu(smp_processor_id()); | |
730 | local_unlock(&lru_pvecs.lock); | |
731 | invalidate_bh_lrus_cpu(); | |
732 | } | |
733 | ||
b01b2141 IM |
734 | void lru_add_drain_cpu_zone(struct zone *zone) |
735 | { | |
736 | local_lock(&lru_pvecs.lock); | |
737 | lru_add_drain_cpu(smp_processor_id()); | |
738 | drain_local_pages(zone); | |
739 | local_unlock(&lru_pvecs.lock); | |
1da177e4 LT |
740 | } |
741 | ||
6ea183d6 MH |
742 | #ifdef CONFIG_SMP |
743 | ||
744 | static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work); | |
745 | ||
c4028958 | 746 | static void lru_add_drain_per_cpu(struct work_struct *dummy) |
053837fc | 747 | { |
243418e3 | 748 | lru_add_and_bh_lrus_drain(); |
053837fc NP |
749 | } |
750 | ||
9852a721 MH |
751 | /* |
752 | * Doesn't need any cpu hotplug locking because we do rely on per-cpu | |
753 | * kworkers being shut down before our page_alloc_cpu_dead callback is | |
754 | * executed on the offlined cpu. | |
755 | * Calling this function with cpu hotplug locks held can actually lead | |
756 | * to obscure indirect dependencies via WQ context. | |
757 | */ | |
d479960e | 758 | inline void __lru_add_drain_all(bool force_all_cpus) |
053837fc | 759 | { |
6446a513 AD |
760 | /* |
761 | * lru_drain_gen - Global pages generation number | |
762 | * | |
763 | * (A) Definition: global lru_drain_gen = x implies that all generations | |
764 | * 0 < n <= x are already *scheduled* for draining. | |
765 | * | |
766 | * This is an optimization for the highly-contended use case where a | |
767 | * user space workload keeps constantly generating a flow of pages for | |
768 | * each CPU. | |
769 | */ | |
770 | static unsigned int lru_drain_gen; | |
5fbc4616 | 771 | static struct cpumask has_work; |
6446a513 AD |
772 | static DEFINE_MUTEX(lock); |
773 | unsigned cpu, this_gen; | |
5fbc4616 | 774 | |
ce612879 MH |
775 | /* |
776 | * Make sure nobody triggers this path before mm_percpu_wq is fully | |
777 | * initialized. | |
778 | */ | |
779 | if (WARN_ON(!mm_percpu_wq)) | |
780 | return; | |
781 | ||
6446a513 AD |
782 | /* |
783 | * Guarantee pagevec counter stores visible by this CPU are visible to | |
784 | * other CPUs before loading the current drain generation. | |
785 | */ | |
786 | smp_mb(); | |
787 | ||
788 | /* | |
789 | * (B) Locally cache global LRU draining generation number | |
790 | * | |
791 | * The read barrier ensures that the counter is loaded before the mutex | |
792 | * is taken. It pairs with smp_mb() inside the mutex critical section | |
793 | * at (D). | |
794 | */ | |
795 | this_gen = smp_load_acquire(&lru_drain_gen); | |
eef1a429 | 796 | |
5fbc4616 | 797 | mutex_lock(&lock); |
eef1a429 KK |
798 | |
799 | /* | |
6446a513 AD |
800 | * (C) Exit the draining operation if a newer generation, from another |
801 | * lru_add_drain_all(), was already scheduled for draining. Check (A). | |
eef1a429 | 802 | */ |
d479960e | 803 | if (unlikely(this_gen != lru_drain_gen && !force_all_cpus)) |
eef1a429 KK |
804 | goto done; |
805 | ||
6446a513 AD |
806 | /* |
807 | * (D) Increment global generation number | |
808 | * | |
809 | * Pairs with smp_load_acquire() at (B), outside of the critical | |
810 | * section. Use a full memory barrier to guarantee that the new global | |
811 | * drain generation number is stored before loading pagevec counters. | |
812 | * | |
813 | * This pairing must be done here, before the for_each_online_cpu loop | |
814 | * below which drains the page vectors. | |
815 | * | |
816 | * Let x, y, and z represent some system CPU numbers, where x < y < z. | |
cb152a1a | 817 | * Assume CPU #z is in the middle of the for_each_online_cpu loop |
6446a513 AD |
818 | * below and has already reached CPU #y's per-cpu data. CPU #x comes |
819 | * along, adds some pages to its per-cpu vectors, then calls | |
820 | * lru_add_drain_all(). | |
821 | * | |
822 | * If the paired barrier is done at any later step, e.g. after the | |
823 | * loop, CPU #x will just exit at (C) and miss flushing out all of its | |
824 | * added pages. | |
825 | */ | |
826 | WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1); | |
827 | smp_mb(); | |
eef1a429 | 828 | |
5fbc4616 | 829 | cpumask_clear(&has_work); |
5fbc4616 CM |
830 | for_each_online_cpu(cpu) { |
831 | struct work_struct *work = &per_cpu(lru_add_drain_work, cpu); | |
832 | ||
d479960e MK |
833 | if (force_all_cpus || |
834 | pagevec_count(&per_cpu(lru_pvecs.lru_add, cpu)) || | |
7e0cc01e | 835 | data_race(pagevec_count(&per_cpu(lru_rotate.pvec, cpu))) || |
b01b2141 IM |
836 | pagevec_count(&per_cpu(lru_pvecs.lru_deactivate_file, cpu)) || |
837 | pagevec_count(&per_cpu(lru_pvecs.lru_deactivate, cpu)) || | |
838 | pagevec_count(&per_cpu(lru_pvecs.lru_lazyfree, cpu)) || | |
8cc621d2 | 839 | need_activate_page_drain(cpu) || |
2fbb0c10 | 840 | need_mlock_page_drain(cpu) || |
8cc621d2 | 841 | has_bh_in_lru(cpu, NULL)) { |
5fbc4616 | 842 | INIT_WORK(work, lru_add_drain_per_cpu); |
ce612879 | 843 | queue_work_on(cpu, mm_percpu_wq, work); |
6446a513 | 844 | __cpumask_set_cpu(cpu, &has_work); |
5fbc4616 CM |
845 | } |
846 | } | |
847 | ||
848 | for_each_cpu(cpu, &has_work) | |
849 | flush_work(&per_cpu(lru_add_drain_work, cpu)); | |
850 | ||
eef1a429 | 851 | done: |
5fbc4616 | 852 | mutex_unlock(&lock); |
053837fc | 853 | } |
d479960e MK |
854 | |
855 | void lru_add_drain_all(void) | |
856 | { | |
857 | __lru_add_drain_all(false); | |
858 | } | |
6ea183d6 MH |
859 | #else |
860 | void lru_add_drain_all(void) | |
861 | { | |
862 | lru_add_drain(); | |
863 | } | |
6446a513 | 864 | #endif /* CONFIG_SMP */ |
053837fc | 865 | |
d479960e MK |
866 | atomic_t lru_disable_count = ATOMIC_INIT(0); |
867 | ||
868 | /* | |
869 | * lru_cache_disable() needs to be called before we start compiling | |
870 | * a list of pages to be migrated using isolate_lru_page(). | |
871 | * It drains pages on LRU cache and then disable on all cpus until | |
872 | * lru_cache_enable is called. | |
873 | * | |
874 | * Must be paired with a call to lru_cache_enable(). | |
875 | */ | |
876 | void lru_cache_disable(void) | |
877 | { | |
878 | atomic_inc(&lru_disable_count); | |
879 | #ifdef CONFIG_SMP | |
880 | /* | |
881 | * lru_add_drain_all in the force mode will schedule draining on | |
882 | * all online CPUs so any calls of lru_cache_disabled wrapped by | |
883 | * local_lock or preemption disabled would be ordered by that. | |
884 | * The atomic operation doesn't need to have stronger ordering | |
0b8f0d87 | 885 | * requirements because that is enforced by the scheduling |
d479960e MK |
886 | * guarantees. |
887 | */ | |
888 | __lru_add_drain_all(true); | |
889 | #else | |
243418e3 | 890 | lru_add_and_bh_lrus_drain(); |
d479960e MK |
891 | #endif |
892 | } | |
893 | ||
aabfb572 | 894 | /** |
ea1754a0 | 895 | * release_pages - batched put_page() |
aabfb572 MH |
896 | * @pages: array of pages to release |
897 | * @nr: number of pages | |
1da177e4 | 898 | * |
aabfb572 MH |
899 | * Decrement the reference count on all the pages in @pages. If it |
900 | * fell to zero, remove the page from the LRU and free it. | |
1da177e4 | 901 | */ |
c6f92f9f | 902 | void release_pages(struct page **pages, int nr) |
1da177e4 LT |
903 | { |
904 | int i; | |
cc59850e | 905 | LIST_HEAD(pages_to_free); |
6168d0da | 906 | struct lruvec *lruvec = NULL; |
0de340cb | 907 | unsigned long flags = 0; |
3f649ab7 | 908 | unsigned int lock_batch; |
1da177e4 | 909 | |
1da177e4 LT |
910 | for (i = 0; i < nr; i++) { |
911 | struct page *page = pages[i]; | |
0de340cb | 912 | struct folio *folio = page_folio(page); |
1da177e4 | 913 | |
aabfb572 MH |
914 | /* |
915 | * Make sure the IRQ-safe lock-holding time does not get | |
916 | * excessive with a continuous string of pages from the | |
6168d0da | 917 | * same lruvec. The lock is held only if lruvec != NULL. |
aabfb572 | 918 | */ |
6168d0da AS |
919 | if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) { |
920 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
921 | lruvec = NULL; | |
aabfb572 MH |
922 | } |
923 | ||
0de340cb | 924 | page = &folio->page; |
6fcb52a5 | 925 | if (is_huge_zero_page(page)) |
aa88b68c | 926 | continue; |
aa88b68c | 927 | |
c5d6c45e | 928 | if (is_zone_device_page(page)) { |
6168d0da AS |
929 | if (lruvec) { |
930 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
931 | lruvec = NULL; | |
df6ad698 | 932 | } |
89574945 | 933 | if (put_devmap_managed_page(page)) |
c5d6c45e | 934 | continue; |
43fbdeb3 RC |
935 | if (put_page_testzero(page)) |
936 | put_dev_pagemap(page->pgmap); | |
937 | continue; | |
df6ad698 JG |
938 | } |
939 | ||
b5810039 | 940 | if (!put_page_testzero(page)) |
1da177e4 LT |
941 | continue; |
942 | ||
ddc58f27 | 943 | if (PageCompound(page)) { |
6168d0da AS |
944 | if (lruvec) { |
945 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
946 | lruvec = NULL; | |
ddc58f27 KS |
947 | } |
948 | __put_compound_page(page); | |
949 | continue; | |
950 | } | |
951 | ||
46453a6e | 952 | if (PageLRU(page)) { |
2a5e4e34 AD |
953 | struct lruvec *prev_lruvec = lruvec; |
954 | ||
0de340cb | 955 | lruvec = folio_lruvec_relock_irqsave(folio, lruvec, |
2a5e4e34 AD |
956 | &flags); |
957 | if (prev_lruvec != lruvec) | |
aabfb572 | 958 | lock_batch = 0; |
fa9add64 | 959 | |
46ae6b2c | 960 | del_page_from_lru_list(page, lruvec); |
87560179 | 961 | __clear_page_lru_flags(page); |
46453a6e NP |
962 | } |
963 | ||
b109b870 HD |
964 | /* |
965 | * In rare cases, when truncation or holepunching raced with | |
966 | * munlock after VM_LOCKED was cleared, Mlocked may still be | |
967 | * found set here. This does not indicate a problem, unless | |
968 | * "unevictable_pgs_cleared" appears worryingly large. | |
969 | */ | |
970 | if (unlikely(PageMlocked(page))) { | |
971 | __ClearPageMlocked(page); | |
972 | dec_zone_page_state(page, NR_MLOCK); | |
973 | count_vm_event(UNEVICTABLE_PGCLEARED); | |
974 | } | |
975 | ||
62906027 | 976 | __ClearPageWaiters(page); |
c53954a0 | 977 | |
cc59850e | 978 | list_add(&page->lru, &pages_to_free); |
1da177e4 | 979 | } |
6168d0da AS |
980 | if (lruvec) |
981 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
1da177e4 | 982 | |
747db954 | 983 | mem_cgroup_uncharge_list(&pages_to_free); |
2d4894b5 | 984 | free_unref_page_list(&pages_to_free); |
1da177e4 | 985 | } |
0be8557b | 986 | EXPORT_SYMBOL(release_pages); |
1da177e4 LT |
987 | |
988 | /* | |
989 | * The pages which we're about to release may be in the deferred lru-addition | |
990 | * queues. That would prevent them from really being freed right now. That's | |
991 | * OK from a correctness point of view but is inefficient - those pages may be | |
992 | * cache-warm and we want to give them back to the page allocator ASAP. | |
993 | * | |
994 | * So __pagevec_release() will drain those queues here. __pagevec_lru_add() | |
995 | * and __pagevec_lru_add_active() call release_pages() directly to avoid | |
996 | * mutual recursion. | |
997 | */ | |
998 | void __pagevec_release(struct pagevec *pvec) | |
999 | { | |
7f0b5fb9 | 1000 | if (!pvec->percpu_pvec_drained) { |
d9ed0d08 | 1001 | lru_add_drain(); |
7f0b5fb9 | 1002 | pvec->percpu_pvec_drained = true; |
d9ed0d08 | 1003 | } |
c6f92f9f | 1004 | release_pages(pvec->pages, pagevec_count(pvec)); |
1da177e4 LT |
1005 | pagevec_reinit(pvec); |
1006 | } | |
7f285701 SF |
1007 | EXPORT_SYMBOL(__pagevec_release); |
1008 | ||
934387c9 | 1009 | static void __pagevec_lru_add_fn(struct folio *folio, struct lruvec *lruvec) |
3dd7ae8e | 1010 | { |
934387c9 MWO |
1011 | int was_unevictable = folio_test_clear_unevictable(folio); |
1012 | long nr_pages = folio_nr_pages(folio); | |
3dd7ae8e | 1013 | |
934387c9 | 1014 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); |
3dd7ae8e | 1015 | |
2262ace6 | 1016 | folio_set_lru(folio); |
9c4e6b1a | 1017 | /* |
2262ace6 HD |
1018 | * Is an smp_mb__after_atomic() still required here, before |
1019 | * folio_evictable() tests PageMlocked, to rule out the possibility | |
1020 | * of stranding an evictable folio on an unevictable LRU? I think | |
2fbb0c10 | 1021 | * not, because __munlock_page() only clears PageMlocked while the LRU |
2262ace6 | 1022 | * lock is held. |
9c4e6b1a | 1023 | * |
2262ace6 HD |
1024 | * (That is not true of __page_cache_release(), and not necessarily |
1025 | * true of release_pages(): but those only clear PageMlocked after | |
1026 | * put_page_testzero() has excluded any other users of the page.) | |
9c4e6b1a | 1027 | */ |
934387c9 | 1028 | if (folio_evictable(folio)) { |
9c4e6b1a | 1029 | if (was_unevictable) |
5d91f31f | 1030 | __count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages); |
9c4e6b1a | 1031 | } else { |
934387c9 MWO |
1032 | folio_clear_active(folio); |
1033 | folio_set_unevictable(folio); | |
2fbb0c10 HD |
1034 | /* |
1035 | * folio->mlock_count = !!folio_test_mlocked(folio)? | |
1036 | * But that leaves __mlock_page() in doubt whether another | |
1037 | * actor has already counted the mlock or not. Err on the | |
1038 | * safe side, underestimate, let page reclaim fix it, rather | |
1039 | * than leaving a page on the unevictable LRU indefinitely. | |
1040 | */ | |
1041 | folio->mlock_count = 0; | |
9c4e6b1a | 1042 | if (!was_unevictable) |
5d91f31f | 1043 | __count_vm_events(UNEVICTABLE_PGCULLED, nr_pages); |
9c4e6b1a SB |
1044 | } |
1045 | ||
934387c9 MWO |
1046 | lruvec_add_folio(lruvec, folio); |
1047 | trace_mm_lru_insertion(folio); | |
3dd7ae8e SL |
1048 | } |
1049 | ||
1da177e4 LT |
1050 | /* |
1051 | * Add the passed pages to the LRU, then drop the caller's refcount | |
1052 | * on them. Reinitialises the caller's pagevec. | |
1053 | */ | |
a0b8cab3 | 1054 | void __pagevec_lru_add(struct pagevec *pvec) |
1da177e4 | 1055 | { |
fc574c23 | 1056 | int i; |
6168d0da | 1057 | struct lruvec *lruvec = NULL; |
fc574c23 AS |
1058 | unsigned long flags = 0; |
1059 | ||
1060 | for (i = 0; i < pagevec_count(pvec); i++) { | |
934387c9 | 1061 | struct folio *folio = page_folio(pvec->pages[i]); |
fc574c23 | 1062 | |
0de340cb | 1063 | lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags); |
934387c9 | 1064 | __pagevec_lru_add_fn(folio, lruvec); |
fc574c23 | 1065 | } |
6168d0da AS |
1066 | if (lruvec) |
1067 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
fc574c23 AS |
1068 | release_pages(pvec->pages, pvec->nr); |
1069 | pagevec_reinit(pvec); | |
1da177e4 | 1070 | } |
1da177e4 | 1071 | |
0cd6144a | 1072 | /** |
1613fac9 MWO |
1073 | * folio_batch_remove_exceptionals() - Prune non-folios from a batch. |
1074 | * @fbatch: The batch to prune | |
0cd6144a | 1075 | * |
1613fac9 MWO |
1076 | * find_get_entries() fills a batch with both folios and shadow/swap/DAX |
1077 | * entries. This function prunes all the non-folio entries from @fbatch | |
1078 | * without leaving holes, so that it can be passed on to folio-only batch | |
1079 | * operations. | |
0cd6144a | 1080 | */ |
1613fac9 | 1081 | void folio_batch_remove_exceptionals(struct folio_batch *fbatch) |
0cd6144a | 1082 | { |
1613fac9 | 1083 | unsigned int i, j; |
0cd6144a | 1084 | |
1613fac9 MWO |
1085 | for (i = 0, j = 0; i < folio_batch_count(fbatch); i++) { |
1086 | struct folio *folio = fbatch->folios[i]; | |
1087 | if (!xa_is_value(folio)) | |
1088 | fbatch->folios[j++] = folio; | |
0cd6144a | 1089 | } |
1613fac9 | 1090 | fbatch->nr = j; |
0cd6144a JW |
1091 | } |
1092 | ||
1da177e4 | 1093 | /** |
b947cee4 | 1094 | * pagevec_lookup_range - gang pagecache lookup |
1da177e4 LT |
1095 | * @pvec: Where the resulting pages are placed |
1096 | * @mapping: The address_space to search | |
1097 | * @start: The starting page index | |
b947cee4 | 1098 | * @end: The final page index |
1da177e4 | 1099 | * |
e02a9f04 | 1100 | * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE |
b947cee4 JK |
1101 | * pages in the mapping starting from index @start and upto index @end |
1102 | * (inclusive). The pages are placed in @pvec. pagevec_lookup() takes a | |
1da177e4 LT |
1103 | * reference against the pages in @pvec. |
1104 | * | |
1105 | * The search returns a group of mapping-contiguous pages with ascending | |
d72dc8a2 JK |
1106 | * indexes. There may be holes in the indices due to not-present pages. We |
1107 | * also update @start to index the next page for the traversal. | |
1da177e4 | 1108 | * |
b947cee4 | 1109 | * pagevec_lookup_range() returns the number of pages which were found. If this |
e02a9f04 | 1110 | * number is smaller than PAGEVEC_SIZE, the end of specified range has been |
b947cee4 | 1111 | * reached. |
1da177e4 | 1112 | */ |
b947cee4 | 1113 | unsigned pagevec_lookup_range(struct pagevec *pvec, |
397162ff | 1114 | struct address_space *mapping, pgoff_t *start, pgoff_t end) |
1da177e4 | 1115 | { |
397162ff | 1116 | pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE, |
b947cee4 | 1117 | pvec->pages); |
1da177e4 LT |
1118 | return pagevec_count(pvec); |
1119 | } | |
b947cee4 | 1120 | EXPORT_SYMBOL(pagevec_lookup_range); |
78539fdf | 1121 | |
72b045ae JK |
1122 | unsigned pagevec_lookup_range_tag(struct pagevec *pvec, |
1123 | struct address_space *mapping, pgoff_t *index, pgoff_t end, | |
10bbd235 | 1124 | xa_mark_t tag) |
1da177e4 | 1125 | { |
72b045ae | 1126 | pvec->nr = find_get_pages_range_tag(mapping, index, end, tag, |
67fd707f | 1127 | PAGEVEC_SIZE, pvec->pages); |
1da177e4 LT |
1128 | return pagevec_count(pvec); |
1129 | } | |
72b045ae | 1130 | EXPORT_SYMBOL(pagevec_lookup_range_tag); |
1da177e4 | 1131 | |
1da177e4 LT |
1132 | /* |
1133 | * Perform any setup for the swap system | |
1134 | */ | |
1135 | void __init swap_setup(void) | |
1136 | { | |
ca79b0c2 | 1137 | unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT); |
e0bf68dd | 1138 | |
1da177e4 LT |
1139 | /* Use a smaller cluster for small-memory machines */ |
1140 | if (megs < 16) | |
1141 | page_cluster = 2; | |
1142 | else | |
1143 | page_cluster = 3; | |
1144 | /* | |
1145 | * Right now other parts of the system means that we | |
1146 | * _really_ don't want to cluster much more | |
1147 | */ | |
1da177e4 | 1148 | } |