]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - mm/swap.c
block/switching-sched.txt: Update to blk-mq schedulers
[thirdparty/kernel/stable.git] / mm / swap.c
CommitLineData
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
LT
10 * Linux VM subsystem. Fine-tuning documentation can be found in
11 * Documentation/sysctl/vm.txt.
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>
1da177e4 38
64d6519d
LS
39#include "internal.h"
40
c6286c98
MG
41#define CREATE_TRACE_POINTS
42#include <trace/events/pagemap.h>
43
1da177e4
LT
44/* How many pages do we try to swap or page in/out together? */
45int page_cluster;
46
13f7f789 47static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
f84f9504 48static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
cc5993bd 49static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
f7ad2a6c 50static DEFINE_PER_CPU(struct pagevec, lru_lazyfree_pvecs);
a4a921aa
ML
51#ifdef CONFIG_SMP
52static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
53#endif
902aaed0 54
b221385b
AB
55/*
56 * This path almost never happens for VM activity - pages are normally
57 * freed via pagevecs. But it gets used by networking.
58 */
920c7a5d 59static void __page_cache_release(struct page *page)
b221385b
AB
60{
61 if (PageLRU(page)) {
f4b7e272 62 pg_data_t *pgdat = page_pgdat(page);
fa9add64
HD
63 struct lruvec *lruvec;
64 unsigned long flags;
b221385b 65
f4b7e272
AR
66 spin_lock_irqsave(&pgdat->lru_lock, flags);
67 lruvec = mem_cgroup_page_lruvec(page, pgdat);
309381fe 68 VM_BUG_ON_PAGE(!PageLRU(page), page);
b221385b 69 __ClearPageLRU(page);
fa9add64 70 del_page_from_lru_list(page, lruvec, page_off_lru(page));
f4b7e272 71 spin_unlock_irqrestore(&pgdat->lru_lock, flags);
b221385b 72 }
62906027 73 __ClearPageWaiters(page);
0a31bc97 74 mem_cgroup_uncharge(page);
91807063
AA
75}
76
77static void __put_single_page(struct page *page)
78{
79 __page_cache_release(page);
2d4894b5 80 free_unref_page(page);
b221385b
AB
81}
82
91807063 83static void __put_compound_page(struct page *page)
1da177e4 84{
91807063 85 compound_page_dtor *dtor;
1da177e4 86
822fc613
NH
87 /*
88 * __page_cache_release() is supposed to be called for thp, not for
89 * hugetlb. This is because hugetlb page does never have PageLRU set
90 * (it's never listed to any LRU lists) and no memcg routines should
91 * be called for hugetlb (it has a separate hugetlb_cgroup.)
92 */
93 if (!PageHuge(page))
94 __page_cache_release(page);
91807063
AA
95 dtor = get_compound_page_dtor(page);
96 (*dtor)(page);
97}
98
ddc58f27 99void __put_page(struct page *page)
8519fb30 100{
71389703
DW
101 if (is_zone_device_page(page)) {
102 put_dev_pagemap(page->pgmap);
103
104 /*
105 * The page belongs to the device that created pgmap. Do
106 * not return it to page allocator.
107 */
108 return;
109 }
110
8519fb30 111 if (unlikely(PageCompound(page)))
ddc58f27
KS
112 __put_compound_page(page);
113 else
91807063 114 __put_single_page(page);
1da177e4 115}
ddc58f27 116EXPORT_SYMBOL(__put_page);
70b50f94 117
1d7ea732 118/**
7682486b
RD
119 * put_pages_list() - release a list of pages
120 * @pages: list of pages threaded on page->lru
1d7ea732
AZ
121 *
122 * Release a list of pages which are strung together on page.lru. Currently
123 * used by read_cache_pages() and related error recovery code.
1d7ea732
AZ
124 */
125void put_pages_list(struct list_head *pages)
126{
127 while (!list_empty(pages)) {
128 struct page *victim;
129
f86196ea 130 victim = lru_to_page(pages);
1d7ea732 131 list_del(&victim->lru);
09cbfeaf 132 put_page(victim);
1d7ea732
AZ
133 }
134}
135EXPORT_SYMBOL(put_pages_list);
136
18022c5d
MG
137/*
138 * get_kernel_pages() - pin kernel pages in memory
139 * @kiov: An array of struct kvec structures
140 * @nr_segs: number of segments to pin
141 * @write: pinning for read/write, currently ignored
142 * @pages: array that receives pointers to the pages pinned.
143 * Should be at least nr_segs long.
144 *
145 * Returns number of pages pinned. This may be fewer than the number
146 * requested. If nr_pages is 0 or negative, returns 0. If no pages
147 * were pinned, returns -errno. Each page returned must be released
148 * with a put_page() call when it is finished with.
149 */
150int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
151 struct page **pages)
152{
153 int seg;
154
155 for (seg = 0; seg < nr_segs; seg++) {
156 if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
157 return seg;
158
5a178119 159 pages[seg] = kmap_to_page(kiov[seg].iov_base);
09cbfeaf 160 get_page(pages[seg]);
18022c5d
MG
161 }
162
163 return seg;
164}
165EXPORT_SYMBOL_GPL(get_kernel_pages);
166
167/*
168 * get_kernel_page() - pin a kernel page in memory
169 * @start: starting kernel address
170 * @write: pinning for read/write, currently ignored
171 * @pages: array that receives pointer to the page pinned.
172 * Must be at least nr_segs long.
173 *
174 * Returns 1 if page is pinned. If the page was not pinned, returns
175 * -errno. The page returned must be released with a put_page() call
176 * when it is finished with.
177 */
178int get_kernel_page(unsigned long start, int write, struct page **pages)
179{
180 const struct kvec kiov = {
181 .iov_base = (void *)start,
182 .iov_len = PAGE_SIZE
183 };
184
185 return get_kernel_pages(&kiov, 1, write, pages);
186}
187EXPORT_SYMBOL_GPL(get_kernel_page);
188
3dd7ae8e 189static void pagevec_lru_move_fn(struct pagevec *pvec,
fa9add64
HD
190 void (*move_fn)(struct page *page, struct lruvec *lruvec, void *arg),
191 void *arg)
902aaed0
HH
192{
193 int i;
68eb0731 194 struct pglist_data *pgdat = NULL;
fa9add64 195 struct lruvec *lruvec;
3dd7ae8e 196 unsigned long flags = 0;
902aaed0
HH
197
198 for (i = 0; i < pagevec_count(pvec); i++) {
199 struct page *page = pvec->pages[i];
68eb0731 200 struct pglist_data *pagepgdat = page_pgdat(page);
902aaed0 201
68eb0731
MG
202 if (pagepgdat != pgdat) {
203 if (pgdat)
204 spin_unlock_irqrestore(&pgdat->lru_lock, flags);
205 pgdat = pagepgdat;
206 spin_lock_irqsave(&pgdat->lru_lock, flags);
902aaed0 207 }
3dd7ae8e 208
68eb0731 209 lruvec = mem_cgroup_page_lruvec(page, pgdat);
fa9add64 210 (*move_fn)(page, lruvec, arg);
902aaed0 211 }
68eb0731
MG
212 if (pgdat)
213 spin_unlock_irqrestore(&pgdat->lru_lock, flags);
c6f92f9f 214 release_pages(pvec->pages, pvec->nr);
83896fb5 215 pagevec_reinit(pvec);
d8505dee
SL
216}
217
fa9add64
HD
218static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec,
219 void *arg)
3dd7ae8e
SL
220{
221 int *pgmoved = arg;
3dd7ae8e 222
c55e8d03
JW
223 if (PageLRU(page) && !PageUnevictable(page)) {
224 del_page_from_lru_list(page, lruvec, page_lru(page));
225 ClearPageActive(page);
226 add_page_to_lru_list_tail(page, lruvec, page_lru(page));
3dd7ae8e
SL
227 (*pgmoved)++;
228 }
229}
230
231/*
232 * pagevec_move_tail() must be called with IRQ disabled.
233 * Otherwise this may cause nasty races.
234 */
235static void pagevec_move_tail(struct pagevec *pvec)
236{
237 int pgmoved = 0;
238
239 pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
240 __count_vm_events(PGROTATED, pgmoved);
241}
242
1da177e4
LT
243/*
244 * Writeback is about to end against a page which has been marked for immediate
245 * reclaim. If it still appears to be reclaimable, move it to the tail of the
902aaed0 246 * inactive list.
1da177e4 247 */
3dd7ae8e 248void rotate_reclaimable_page(struct page *page)
1da177e4 249{
c55e8d03 250 if (!PageLocked(page) && !PageDirty(page) &&
894bc310 251 !PageUnevictable(page) && PageLRU(page)) {
ac6aadb2
MS
252 struct pagevec *pvec;
253 unsigned long flags;
254
09cbfeaf 255 get_page(page);
ac6aadb2 256 local_irq_save(flags);
7c8e0181 257 pvec = this_cpu_ptr(&lru_rotate_pvecs);
8f182270 258 if (!pagevec_add(pvec, page) || PageCompound(page))
ac6aadb2
MS
259 pagevec_move_tail(pvec);
260 local_irq_restore(flags);
261 }
1da177e4
LT
262}
263
fa9add64 264static void update_page_reclaim_stat(struct lruvec *lruvec,
3e2f41f1
KM
265 int file, int rotated)
266{
fa9add64 267 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
3e2f41f1
KM
268
269 reclaim_stat->recent_scanned[file]++;
270 if (rotated)
271 reclaim_stat->recent_rotated[file]++;
3e2f41f1
KM
272}
273
fa9add64
HD
274static void __activate_page(struct page *page, struct lruvec *lruvec,
275 void *arg)
1da177e4 276{
744ed144 277 if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
7a608572
LT
278 int file = page_is_file_cache(page);
279 int lru = page_lru_base_type(page);
744ed144 280
fa9add64 281 del_page_from_lru_list(page, lruvec, lru);
7a608572
LT
282 SetPageActive(page);
283 lru += LRU_ACTIVE;
fa9add64 284 add_page_to_lru_list(page, lruvec, lru);
24b7e581 285 trace_mm_lru_activate(page);
4f98a2fe 286
fa9add64
HD
287 __count_vm_event(PGACTIVATE);
288 update_page_reclaim_stat(lruvec, file, 1);
1da177e4 289 }
eb709b0d
SL
290}
291
292#ifdef CONFIG_SMP
eb709b0d
SL
293static void activate_page_drain(int cpu)
294{
295 struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
296
297 if (pagevec_count(pvec))
298 pagevec_lru_move_fn(pvec, __activate_page, NULL);
299}
300
5fbc4616
CM
301static bool need_activate_page_drain(int cpu)
302{
303 return pagevec_count(&per_cpu(activate_page_pvecs, cpu)) != 0;
304}
305
eb709b0d
SL
306void activate_page(struct page *page)
307{
800d8c63 308 page = compound_head(page);
eb709b0d
SL
309 if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
310 struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
311
09cbfeaf 312 get_page(page);
8f182270 313 if (!pagevec_add(pvec, page) || PageCompound(page))
eb709b0d
SL
314 pagevec_lru_move_fn(pvec, __activate_page, NULL);
315 put_cpu_var(activate_page_pvecs);
316 }
317}
318
319#else
320static inline void activate_page_drain(int cpu)
321{
322}
323
324void activate_page(struct page *page)
325{
f4b7e272 326 pg_data_t *pgdat = page_pgdat(page);
eb709b0d 327
800d8c63 328 page = compound_head(page);
f4b7e272
AR
329 spin_lock_irq(&pgdat->lru_lock);
330 __activate_page(page, mem_cgroup_page_lruvec(page, pgdat), NULL);
331 spin_unlock_irq(&pgdat->lru_lock);
1da177e4 332}
eb709b0d 333#endif
1da177e4 334
059285a2
MG
335static void __lru_cache_activate_page(struct page *page)
336{
337 struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
338 int i;
339
340 /*
341 * Search backwards on the optimistic assumption that the page being
342 * activated has just been added to this pagevec. Note that only
343 * the local pagevec is examined as a !PageLRU page could be in the
344 * process of being released, reclaimed, migrated or on a remote
345 * pagevec that is currently being drained. Furthermore, marking
346 * a remote pagevec's page PageActive potentially hits a race where
347 * a page is marked PageActive just after it is added to the inactive
348 * list causing accounting errors and BUG_ON checks to trigger.
349 */
350 for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
351 struct page *pagevec_page = pvec->pages[i];
352
353 if (pagevec_page == page) {
354 SetPageActive(page);
355 break;
356 }
357 }
358
359 put_cpu_var(lru_add_pvec);
360}
361
1da177e4
LT
362/*
363 * Mark a page as having seen activity.
364 *
365 * inactive,unreferenced -> inactive,referenced
366 * inactive,referenced -> active,unreferenced
367 * active,unreferenced -> active,referenced
eb39d618
HD
368 *
369 * When a newly allocated page is not yet visible, so safe for non-atomic ops,
370 * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
1da177e4 371 */
920c7a5d 372void mark_page_accessed(struct page *page)
1da177e4 373{
e90309c9 374 page = compound_head(page);
894bc310 375 if (!PageActive(page) && !PageUnevictable(page) &&
059285a2
MG
376 PageReferenced(page)) {
377
378 /*
379 * If the page is on the LRU, queue it for activation via
380 * activate_page_pvecs. Otherwise, assume the page is on a
381 * pagevec, mark it active and it'll be moved to the active
382 * LRU on the next drain.
383 */
384 if (PageLRU(page))
385 activate_page(page);
386 else
387 __lru_cache_activate_page(page);
1da177e4 388 ClearPageReferenced(page);
a528910e
JW
389 if (page_is_file_cache(page))
390 workingset_activation(page);
1da177e4
LT
391 } else if (!PageReferenced(page)) {
392 SetPageReferenced(page);
393 }
33c3fc71
VD
394 if (page_is_idle(page))
395 clear_page_idle(page);
1da177e4 396}
1da177e4
LT
397EXPORT_SYMBOL(mark_page_accessed);
398
2329d375 399static void __lru_cache_add(struct page *page)
1da177e4 400{
13f7f789
MG
401 struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
402
09cbfeaf 403 get_page(page);
8f182270 404 if (!pagevec_add(pvec, page) || PageCompound(page))
a0b8cab3 405 __pagevec_lru_add(pvec);
13f7f789 406 put_cpu_var(lru_add_pvec);
1da177e4 407}
2329d375
JZ
408
409/**
e02a9f04 410 * lru_cache_add_anon - add a page to the page lists
2329d375
JZ
411 * @page: the page to add
412 */
413void lru_cache_add_anon(struct page *page)
414{
6fb81a17
MG
415 if (PageActive(page))
416 ClearPageActive(page);
2329d375
JZ
417 __lru_cache_add(page);
418}
419
420void lru_cache_add_file(struct page *page)
421{
6fb81a17
MG
422 if (PageActive(page))
423 ClearPageActive(page);
2329d375
JZ
424 __lru_cache_add(page);
425}
426EXPORT_SYMBOL(lru_cache_add_file);
1da177e4 427
f04e9ebb 428/**
c53954a0 429 * lru_cache_add - add a page to a page list
f04e9ebb 430 * @page: the page to be added to the LRU.
2329d375
JZ
431 *
432 * Queue the page for addition to the LRU via pagevec. The decision on whether
433 * to add the page to the [in]active [file|anon] list is deferred until the
434 * pagevec is drained. This gives a chance for the caller of lru_cache_add()
435 * have the page added to the active list using mark_page_accessed().
f04e9ebb 436 */
c53954a0 437void lru_cache_add(struct page *page)
1da177e4 438{
309381fe
SL
439 VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
440 VM_BUG_ON_PAGE(PageLRU(page), page);
c53954a0 441 __lru_cache_add(page);
1da177e4
LT
442}
443
00501b53
JW
444/**
445 * lru_cache_add_active_or_unevictable
446 * @page: the page to be added to LRU
447 * @vma: vma in which page is mapped for determining reclaimability
448 *
449 * Place @page on the active or unevictable LRU list, depending on its
450 * evictability. Note that if the page is not evictable, it goes
451 * directly back onto it's zone's unevictable list, it does NOT use a
452 * per cpu pagevec.
453 */
454void lru_cache_add_active_or_unevictable(struct page *page,
455 struct vm_area_struct *vma)
456{
457 VM_BUG_ON_PAGE(PageLRU(page), page);
458
9c4e6b1a 459 if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED))
00501b53 460 SetPageActive(page);
9c4e6b1a 461 else if (!TestSetPageMlocked(page)) {
00501b53
JW
462 /*
463 * We use the irq-unsafe __mod_zone_page_stat because this
464 * counter is not modified from interrupt context, and the pte
465 * lock is held(spinlock), which implies preemption disabled.
466 */
467 __mod_zone_page_state(page_zone(page), NR_MLOCK,
468 hpage_nr_pages(page));
469 count_vm_event(UNEVICTABLE_PGMLOCKED);
470 }
9c4e6b1a 471 lru_cache_add(page);
00501b53
JW
472}
473
31560180
MK
474/*
475 * If the page can not be invalidated, it is moved to the
476 * inactive list to speed up its reclaim. It is moved to the
477 * head of the list, rather than the tail, to give the flusher
478 * threads some time to write it out, as this is much more
479 * effective than the single-page writeout from reclaim.
278df9f4
MK
480 *
481 * If the page isn't page_mapped and dirty/writeback, the page
482 * could reclaim asap using PG_reclaim.
483 *
484 * 1. active, mapped page -> none
485 * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
486 * 3. inactive, mapped page -> none
487 * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
488 * 5. inactive, clean -> inactive, tail
489 * 6. Others -> none
490 *
491 * In 4, why it moves inactive's head, the VM expects the page would
492 * be write it out by flusher threads as this is much more effective
493 * than the single-page writeout from reclaim.
31560180 494 */
cc5993bd 495static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
fa9add64 496 void *arg)
31560180
MK
497{
498 int lru, file;
278df9f4 499 bool active;
31560180 500
278df9f4 501 if (!PageLRU(page))
31560180
MK
502 return;
503
bad49d9c
MK
504 if (PageUnevictable(page))
505 return;
506
31560180
MK
507 /* Some processes are using the page */
508 if (page_mapped(page))
509 return;
510
278df9f4 511 active = PageActive(page);
31560180
MK
512 file = page_is_file_cache(page);
513 lru = page_lru_base_type(page);
fa9add64
HD
514
515 del_page_from_lru_list(page, lruvec, lru + active);
31560180
MK
516 ClearPageActive(page);
517 ClearPageReferenced(page);
fa9add64 518 add_page_to_lru_list(page, lruvec, lru);
31560180 519
278df9f4
MK
520 if (PageWriteback(page) || PageDirty(page)) {
521 /*
522 * PG_reclaim could be raced with end_page_writeback
523 * It can make readahead confusing. But race window
524 * is _really_ small and it's non-critical problem.
525 */
526 SetPageReclaim(page);
527 } else {
528 /*
529 * The page's writeback ends up during pagevec
530 * We moves tha page into tail of inactive.
531 */
925b7673 532 list_move_tail(&page->lru, &lruvec->lists[lru]);
278df9f4
MK
533 __count_vm_event(PGROTATED);
534 }
535
536 if (active)
537 __count_vm_event(PGDEACTIVATE);
fa9add64 538 update_page_reclaim_stat(lruvec, file, 0);
31560180
MK
539}
540
10853a03 541
f7ad2a6c 542static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
10853a03
MK
543 void *arg)
544{
f7ad2a6c 545 if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
24c92eb7 546 !PageSwapCache(page) && !PageUnevictable(page)) {
f7ad2a6c 547 bool active = PageActive(page);
10853a03 548
f7ad2a6c
SL
549 del_page_from_lru_list(page, lruvec,
550 LRU_INACTIVE_ANON + active);
10853a03
MK
551 ClearPageActive(page);
552 ClearPageReferenced(page);
f7ad2a6c
SL
553 /*
554 * lazyfree pages are clean anonymous pages. They have
555 * SwapBacked flag cleared to distinguish normal anonymous
556 * pages
557 */
558 ClearPageSwapBacked(page);
559 add_page_to_lru_list(page, lruvec, LRU_INACTIVE_FILE);
10853a03 560
f7ad2a6c 561 __count_vm_events(PGLAZYFREE, hpage_nr_pages(page));
2262185c 562 count_memcg_page_event(page, PGLAZYFREE);
f7ad2a6c 563 update_page_reclaim_stat(lruvec, 1, 0);
10853a03
MK
564 }
565}
566
902aaed0
HH
567/*
568 * Drain pages out of the cpu's pagevecs.
569 * Either "cpu" is the current CPU, and preemption has already been
570 * disabled; or "cpu" is being hot-unplugged, and is already dead.
571 */
f0cb3c76 572void lru_add_drain_cpu(int cpu)
1da177e4 573{
13f7f789 574 struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
1da177e4 575
13f7f789 576 if (pagevec_count(pvec))
a0b8cab3 577 __pagevec_lru_add(pvec);
902aaed0
HH
578
579 pvec = &per_cpu(lru_rotate_pvecs, cpu);
580 if (pagevec_count(pvec)) {
581 unsigned long flags;
582
583 /* No harm done if a racing interrupt already did this */
584 local_irq_save(flags);
585 pagevec_move_tail(pvec);
586 local_irq_restore(flags);
587 }
31560180 588
cc5993bd 589 pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
31560180 590 if (pagevec_count(pvec))
cc5993bd 591 pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
eb709b0d 592
f7ad2a6c 593 pvec = &per_cpu(lru_lazyfree_pvecs, cpu);
10853a03 594 if (pagevec_count(pvec))
f7ad2a6c 595 pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
10853a03 596
eb709b0d 597 activate_page_drain(cpu);
31560180
MK
598}
599
600/**
cc5993bd 601 * deactivate_file_page - forcefully deactivate a file page
31560180
MK
602 * @page: page to deactivate
603 *
604 * This function hints the VM that @page is a good reclaim candidate,
605 * for example if its invalidation fails due to the page being dirty
606 * or under writeback.
607 */
cc5993bd 608void deactivate_file_page(struct page *page)
31560180 609{
821ed6bb 610 /*
cc5993bd
MK
611 * In a workload with many unevictable page such as mprotect,
612 * unevictable page deactivation for accelerating reclaim is pointless.
821ed6bb
MK
613 */
614 if (PageUnevictable(page))
615 return;
616
31560180 617 if (likely(get_page_unless_zero(page))) {
cc5993bd 618 struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
31560180 619
8f182270 620 if (!pagevec_add(pvec, page) || PageCompound(page))
cc5993bd
MK
621 pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
622 put_cpu_var(lru_deactivate_file_pvecs);
31560180 623 }
80bfed90
AM
624}
625
10853a03 626/**
f7ad2a6c 627 * mark_page_lazyfree - make an anon page lazyfree
10853a03
MK
628 * @page: page to deactivate
629 *
f7ad2a6c
SL
630 * mark_page_lazyfree() moves @page to the inactive file list.
631 * This is done to accelerate the reclaim of @page.
10853a03 632 */
f7ad2a6c 633void mark_page_lazyfree(struct page *page)
10853a03 634{
f7ad2a6c 635 if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
24c92eb7 636 !PageSwapCache(page) && !PageUnevictable(page)) {
f7ad2a6c 637 struct pagevec *pvec = &get_cpu_var(lru_lazyfree_pvecs);
10853a03 638
09cbfeaf 639 get_page(page);
8f182270 640 if (!pagevec_add(pvec, page) || PageCompound(page))
f7ad2a6c
SL
641 pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
642 put_cpu_var(lru_lazyfree_pvecs);
10853a03
MK
643 }
644}
645
80bfed90
AM
646void lru_add_drain(void)
647{
f0cb3c76 648 lru_add_drain_cpu(get_cpu());
80bfed90 649 put_cpu();
1da177e4
LT
650}
651
6ea183d6
MH
652#ifdef CONFIG_SMP
653
654static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
655
c4028958 656static void lru_add_drain_per_cpu(struct work_struct *dummy)
053837fc
NP
657{
658 lru_add_drain();
659}
660
9852a721
MH
661/*
662 * Doesn't need any cpu hotplug locking because we do rely on per-cpu
663 * kworkers being shut down before our page_alloc_cpu_dead callback is
664 * executed on the offlined cpu.
665 * Calling this function with cpu hotplug locks held can actually lead
666 * to obscure indirect dependencies via WQ context.
667 */
668void lru_add_drain_all(void)
053837fc 669{
5fbc4616
CM
670 static DEFINE_MUTEX(lock);
671 static struct cpumask has_work;
672 int cpu;
673
ce612879
MH
674 /*
675 * Make sure nobody triggers this path before mm_percpu_wq is fully
676 * initialized.
677 */
678 if (WARN_ON(!mm_percpu_wq))
679 return;
680
5fbc4616 681 mutex_lock(&lock);
5fbc4616
CM
682 cpumask_clear(&has_work);
683
684 for_each_online_cpu(cpu) {
685 struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
686
687 if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
688 pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
cc5993bd 689 pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
f7ad2a6c 690 pagevec_count(&per_cpu(lru_lazyfree_pvecs, cpu)) ||
5fbc4616
CM
691 need_activate_page_drain(cpu)) {
692 INIT_WORK(work, lru_add_drain_per_cpu);
ce612879 693 queue_work_on(cpu, mm_percpu_wq, work);
5fbc4616
CM
694 cpumask_set_cpu(cpu, &has_work);
695 }
696 }
697
698 for_each_cpu(cpu, &has_work)
699 flush_work(&per_cpu(lru_add_drain_work, cpu));
700
5fbc4616 701 mutex_unlock(&lock);
053837fc 702}
6ea183d6
MH
703#else
704void lru_add_drain_all(void)
705{
706 lru_add_drain();
707}
708#endif
053837fc 709
aabfb572 710/**
ea1754a0 711 * release_pages - batched put_page()
aabfb572
MH
712 * @pages: array of pages to release
713 * @nr: number of pages
1da177e4 714 *
aabfb572
MH
715 * Decrement the reference count on all the pages in @pages. If it
716 * fell to zero, remove the page from the LRU and free it.
1da177e4 717 */
c6f92f9f 718void release_pages(struct page **pages, int nr)
1da177e4
LT
719{
720 int i;
cc59850e 721 LIST_HEAD(pages_to_free);
599d0c95 722 struct pglist_data *locked_pgdat = NULL;
fa9add64 723 struct lruvec *lruvec;
902aaed0 724 unsigned long uninitialized_var(flags);
aabfb572 725 unsigned int uninitialized_var(lock_batch);
1da177e4 726
1da177e4
LT
727 for (i = 0; i < nr; i++) {
728 struct page *page = pages[i];
1da177e4 729
aabfb572
MH
730 /*
731 * Make sure the IRQ-safe lock-holding time does not get
732 * excessive with a continuous string of pages from the
599d0c95 733 * same pgdat. The lock is held only if pgdat != NULL.
aabfb572 734 */
599d0c95
MG
735 if (locked_pgdat && ++lock_batch == SWAP_CLUSTER_MAX) {
736 spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
737 locked_pgdat = NULL;
aabfb572
MH
738 }
739
6fcb52a5 740 if (is_huge_zero_page(page))
aa88b68c 741 continue;
aa88b68c 742
df6ad698
JG
743 /* Device public page can not be huge page */
744 if (is_device_public_page(page)) {
745 if (locked_pgdat) {
746 spin_unlock_irqrestore(&locked_pgdat->lru_lock,
747 flags);
748 locked_pgdat = NULL;
749 }
e7638488 750 put_devmap_managed_page(page);
df6ad698
JG
751 continue;
752 }
753
ddc58f27 754 page = compound_head(page);
b5810039 755 if (!put_page_testzero(page))
1da177e4
LT
756 continue;
757
ddc58f27 758 if (PageCompound(page)) {
599d0c95
MG
759 if (locked_pgdat) {
760 spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
761 locked_pgdat = NULL;
ddc58f27
KS
762 }
763 __put_compound_page(page);
764 continue;
765 }
766
46453a6e 767 if (PageLRU(page)) {
599d0c95 768 struct pglist_data *pgdat = page_pgdat(page);
894bc310 769
599d0c95
MG
770 if (pgdat != locked_pgdat) {
771 if (locked_pgdat)
772 spin_unlock_irqrestore(&locked_pgdat->lru_lock,
902aaed0 773 flags);
aabfb572 774 lock_batch = 0;
599d0c95
MG
775 locked_pgdat = pgdat;
776 spin_lock_irqsave(&locked_pgdat->lru_lock, flags);
46453a6e 777 }
fa9add64 778
599d0c95 779 lruvec = mem_cgroup_page_lruvec(page, locked_pgdat);
309381fe 780 VM_BUG_ON_PAGE(!PageLRU(page), page);
67453911 781 __ClearPageLRU(page);
fa9add64 782 del_page_from_lru_list(page, lruvec, page_off_lru(page));
46453a6e
NP
783 }
784
c53954a0 785 /* Clear Active bit in case of parallel mark_page_accessed */
e3741b50 786 __ClearPageActive(page);
62906027 787 __ClearPageWaiters(page);
c53954a0 788
cc59850e 789 list_add(&page->lru, &pages_to_free);
1da177e4 790 }
599d0c95
MG
791 if (locked_pgdat)
792 spin_unlock_irqrestore(&locked_pgdat->lru_lock, flags);
1da177e4 793
747db954 794 mem_cgroup_uncharge_list(&pages_to_free);
2d4894b5 795 free_unref_page_list(&pages_to_free);
1da177e4 796}
0be8557b 797EXPORT_SYMBOL(release_pages);
1da177e4
LT
798
799/*
800 * The pages which we're about to release may be in the deferred lru-addition
801 * queues. That would prevent them from really being freed right now. That's
802 * OK from a correctness point of view but is inefficient - those pages may be
803 * cache-warm and we want to give them back to the page allocator ASAP.
804 *
805 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
806 * and __pagevec_lru_add_active() call release_pages() directly to avoid
807 * mutual recursion.
808 */
809void __pagevec_release(struct pagevec *pvec)
810{
7f0b5fb9 811 if (!pvec->percpu_pvec_drained) {
d9ed0d08 812 lru_add_drain();
7f0b5fb9 813 pvec->percpu_pvec_drained = true;
d9ed0d08 814 }
c6f92f9f 815 release_pages(pvec->pages, pagevec_count(pvec));
1da177e4
LT
816 pagevec_reinit(pvec);
817}
7f285701
SF
818EXPORT_SYMBOL(__pagevec_release);
819
12d27107 820#ifdef CONFIG_TRANSPARENT_HUGEPAGE
71e3aac0 821/* used by __split_huge_page_refcount() */
fa9add64 822void lru_add_page_tail(struct page *page, struct page *page_tail,
5bc7b8ac 823 struct lruvec *lruvec, struct list_head *list)
71e3aac0 824{
71e3aac0 825 const int file = 0;
71e3aac0 826
309381fe
SL
827 VM_BUG_ON_PAGE(!PageHead(page), page);
828 VM_BUG_ON_PAGE(PageCompound(page_tail), page);
829 VM_BUG_ON_PAGE(PageLRU(page_tail), page);
35f3aa39 830 lockdep_assert_held(&lruvec_pgdat(lruvec)->lru_lock);
71e3aac0 831
5bc7b8ac
SL
832 if (!list)
833 SetPageLRU(page_tail);
71e3aac0 834
12d27107
HD
835 if (likely(PageLRU(page)))
836 list_add_tail(&page_tail->lru, &page->lru);
5bc7b8ac
SL
837 else if (list) {
838 /* page reclaim is reclaiming a huge page */
839 get_page(page_tail);
840 list_add_tail(&page_tail->lru, list);
841 } else {
12d27107
HD
842 struct list_head *list_head;
843 /*
844 * Head page has not yet been counted, as an hpage,
845 * so we must account for each subpage individually.
846 *
847 * Use the standard add function to put page_tail on the list,
848 * but then correct its position so they all end up in order.
849 */
e180cf80 850 add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
12d27107
HD
851 list_head = page_tail->lru.prev;
852 list_move_tail(&page_tail->lru, list_head);
71e3aac0 853 }
7512102c
HD
854
855 if (!PageUnevictable(page))
e180cf80 856 update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
71e3aac0 857}
12d27107 858#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
71e3aac0 859
fa9add64
HD
860static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
861 void *arg)
3dd7ae8e 862{
9c4e6b1a
SB
863 enum lru_list lru;
864 int was_unevictable = TestClearPageUnevictable(page);
3dd7ae8e 865
309381fe 866 VM_BUG_ON_PAGE(PageLRU(page), page);
3dd7ae8e
SL
867
868 SetPageLRU(page);
9c4e6b1a
SB
869 /*
870 * Page becomes evictable in two ways:
dae966dc 871 * 1) Within LRU lock [munlock_vma_page() and __munlock_pagevec()].
9c4e6b1a
SB
872 * 2) Before acquiring LRU lock to put the page to correct LRU and then
873 * a) do PageLRU check with lock [check_move_unevictable_pages]
874 * b) do PageLRU check before lock [clear_page_mlock]
875 *
876 * (1) & (2a) are ok as LRU lock will serialize them. For (2b), we need
877 * following strict ordering:
878 *
879 * #0: __pagevec_lru_add_fn #1: clear_page_mlock
880 *
881 * SetPageLRU() TestClearPageMlocked()
882 * smp_mb() // explicit ordering // above provides strict
883 * // ordering
884 * PageMlocked() PageLRU()
885 *
886 *
887 * if '#1' does not observe setting of PG_lru by '#0' and fails
888 * isolation, the explicit barrier will make sure that page_evictable
889 * check will put the page in correct LRU. Without smp_mb(), SetPageLRU
890 * can be reordered after PageMlocked check and can make '#1' to fail
891 * the isolation of the page whose Mlocked bit is cleared (#0 is also
892 * looking at the same page) and the evictable page will be stranded
893 * in an unevictable LRU.
894 */
895 smp_mb();
896
897 if (page_evictable(page)) {
898 lru = page_lru(page);
899 update_page_reclaim_stat(lruvec, page_is_file_cache(page),
900 PageActive(page));
901 if (was_unevictable)
902 count_vm_event(UNEVICTABLE_PGRESCUED);
903 } else {
904 lru = LRU_UNEVICTABLE;
905 ClearPageActive(page);
906 SetPageUnevictable(page);
907 if (!was_unevictable)
908 count_vm_event(UNEVICTABLE_PGCULLED);
909 }
910
fa9add64 911 add_page_to_lru_list(page, lruvec, lru);
24b7e581 912 trace_mm_lru_insertion(page, lru);
3dd7ae8e
SL
913}
914
1da177e4
LT
915/*
916 * Add the passed pages to the LRU, then drop the caller's refcount
917 * on them. Reinitialises the caller's pagevec.
918 */
a0b8cab3 919void __pagevec_lru_add(struct pagevec *pvec)
1da177e4 920{
a0b8cab3 921 pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
1da177e4 922}
5095ae83 923EXPORT_SYMBOL(__pagevec_lru_add);
1da177e4 924
0cd6144a
JW
925/**
926 * pagevec_lookup_entries - gang pagecache lookup
927 * @pvec: Where the resulting entries are placed
928 * @mapping: The address_space to search
929 * @start: The starting entry index
cb6f0f34 930 * @nr_entries: The maximum number of pages
0cd6144a
JW
931 * @indices: The cache indices corresponding to the entries in @pvec
932 *
933 * pagevec_lookup_entries() will search for and return a group of up
f144c390 934 * to @nr_pages pages and shadow entries in the mapping. All
0cd6144a
JW
935 * entries are placed in @pvec. pagevec_lookup_entries() takes a
936 * reference against actual pages in @pvec.
937 *
938 * The search returns a group of mapping-contiguous entries with
939 * ascending indexes. There may be holes in the indices due to
940 * not-present entries.
941 *
942 * pagevec_lookup_entries() returns the number of entries which were
943 * found.
944 */
945unsigned pagevec_lookup_entries(struct pagevec *pvec,
946 struct address_space *mapping,
e02a9f04 947 pgoff_t start, unsigned nr_entries,
0cd6144a
JW
948 pgoff_t *indices)
949{
e02a9f04 950 pvec->nr = find_get_entries(mapping, start, nr_entries,
0cd6144a
JW
951 pvec->pages, indices);
952 return pagevec_count(pvec);
953}
954
955/**
956 * pagevec_remove_exceptionals - pagevec exceptionals pruning
957 * @pvec: The pagevec to prune
958 *
959 * pagevec_lookup_entries() fills both pages and exceptional radix
960 * tree entries into the pagevec. This function prunes all
961 * exceptionals from @pvec without leaving holes, so that it can be
962 * passed on to page-only pagevec operations.
963 */
964void pagevec_remove_exceptionals(struct pagevec *pvec)
965{
966 int i, j;
967
968 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
969 struct page *page = pvec->pages[i];
3159f943 970 if (!xa_is_value(page))
0cd6144a
JW
971 pvec->pages[j++] = page;
972 }
973 pvec->nr = j;
974}
975
1da177e4 976/**
b947cee4 977 * pagevec_lookup_range - gang pagecache lookup
1da177e4
LT
978 * @pvec: Where the resulting pages are placed
979 * @mapping: The address_space to search
980 * @start: The starting page index
b947cee4 981 * @end: The final page index
1da177e4 982 *
e02a9f04 983 * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE
b947cee4
JK
984 * pages in the mapping starting from index @start and upto index @end
985 * (inclusive). The pages are placed in @pvec. pagevec_lookup() takes a
1da177e4
LT
986 * reference against the pages in @pvec.
987 *
988 * The search returns a group of mapping-contiguous pages with ascending
d72dc8a2
JK
989 * indexes. There may be holes in the indices due to not-present pages. We
990 * also update @start to index the next page for the traversal.
1da177e4 991 *
b947cee4 992 * pagevec_lookup_range() returns the number of pages which were found. If this
e02a9f04 993 * number is smaller than PAGEVEC_SIZE, the end of specified range has been
b947cee4 994 * reached.
1da177e4 995 */
b947cee4 996unsigned pagevec_lookup_range(struct pagevec *pvec,
397162ff 997 struct address_space *mapping, pgoff_t *start, pgoff_t end)
1da177e4 998{
397162ff 999 pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE,
b947cee4 1000 pvec->pages);
1da177e4
LT
1001 return pagevec_count(pvec);
1002}
b947cee4 1003EXPORT_SYMBOL(pagevec_lookup_range);
78539fdf 1004
72b045ae
JK
1005unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
1006 struct address_space *mapping, pgoff_t *index, pgoff_t end,
10bbd235 1007 xa_mark_t tag)
1da177e4 1008{
72b045ae 1009 pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
67fd707f 1010 PAGEVEC_SIZE, pvec->pages);
1da177e4
LT
1011 return pagevec_count(pvec);
1012}
72b045ae 1013EXPORT_SYMBOL(pagevec_lookup_range_tag);
1da177e4 1014
93d3b714
JK
1015unsigned pagevec_lookup_range_nr_tag(struct pagevec *pvec,
1016 struct address_space *mapping, pgoff_t *index, pgoff_t end,
10bbd235 1017 xa_mark_t tag, unsigned max_pages)
93d3b714
JK
1018{
1019 pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
1020 min_t(unsigned int, max_pages, PAGEVEC_SIZE), pvec->pages);
1021 return pagevec_count(pvec);
1022}
1023EXPORT_SYMBOL(pagevec_lookup_range_nr_tag);
1da177e4
LT
1024/*
1025 * Perform any setup for the swap system
1026 */
1027void __init swap_setup(void)
1028{
ca79b0c2 1029 unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
e0bf68dd 1030
1da177e4
LT
1031 /* Use a smaller cluster for small-memory machines */
1032 if (megs < 16)
1033 page_cluster = 2;
1034 else
1035 page_cluster = 3;
1036 /*
1037 * Right now other parts of the system means that we
1038 * _really_ don't want to cluster much more
1039 */
1da177e4 1040}