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