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