blob: 1841c24682f8f12fbad984830f111fa8cdfb20cb [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/swap.c
4 *
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
6 */
7
8/*
Simon Arlott183ff222007-10-20 01:27:18 +02009 * This file contains the default values for the operation of the
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Linux VM subsystem. Fine-tuning documentation can be found in
Mauro Carvalho Chehab57043242019-04-22 16:48:00 -030011 * Documentation/admin-guide/sysctl/vm.rst.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040025#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/mm_inline.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/percpu_counter.h>
Dan Williams3565fce2016-01-15 16:56:55 -080028#include <linux/memremap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/percpu.h>
30#include <linux/cpu.h>
31#include <linux/notifier.h>
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070032#include <linux/backing-dev.h>
Balbir Singh66e17072008-02-07 00:13:56 -080033#include <linux/memcontrol.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/gfp.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/uio.h>
Naoya Horiguchi822fc612015-04-15 16:14:35 -070036#include <linux/hugetlb.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070037#include <linux/page_idle.h>
Ingo Molnarb01b2142020-05-27 22:11:15 +020038#include <linux/local_lock.h>
Minchan Kim8cc621d2021-05-04 18:37:00 -070039#include <linux/buffer_head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Lee Schermerhorn64d65192008-10-18 20:26:52 -070041#include "internal.h"
42
Mel Gormanc6286c92013-07-03 15:02:26 -070043#define CREATE_TRACE_POINTS
44#include <trace/events/pagemap.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* How many pages do we try to swap or page in/out together? */
47int page_cluster;
48
Ingo Molnarb01b2142020-05-27 22:11:15 +020049/* Protecting only lru_rotate.pvec which requires disabling interrupts */
50struct lru_rotate {
51 local_lock_t lock;
52 struct pagevec pvec;
53};
54static 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 */
62struct 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;
Ming Lia4a921a2016-05-20 16:57:56 -070068#ifdef CONFIG_SMP
Ingo Molnarb01b2142020-05-27 22:11:15 +020069 struct pagevec activate_page;
Ming Lia4a921a2016-05-20 16:57:56 -070070#endif
Ingo Molnarb01b2142020-05-27 22:11:15 +020071};
72static DEFINE_PER_CPU(struct lru_pvecs, lru_pvecs) = {
73 .lock = INIT_LOCAL_LOCK(lock),
74};
Hisashi Hifumi902aaed2007-10-16 01:24:52 -070075
Adrian Bunkb2213852006-09-25 23:31:02 -070076/*
77 * This path almost never happens for VM activity - pages are normally
78 * freed via pagevecs. But it gets used by networking.
79 */
Harvey Harrison920c7a52008-02-04 22:29:26 -080080static void __page_cache_release(struct page *page)
Adrian Bunkb2213852006-09-25 23:31:02 -070081{
82 if (PageLRU(page)) {
Matthew Wilcox (Oracle)e809c3f2021-06-28 21:59:47 -040083 struct folio *folio = page_folio(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -070084 struct lruvec *lruvec;
85 unsigned long flags;
Adrian Bunkb2213852006-09-25 23:31:02 -070086
Matthew Wilcox (Oracle)e809c3f2021-06-28 21:59:47 -040087 lruvec = folio_lruvec_lock_irqsave(folio, &flags);
Yu Zhao46ae6b22021-02-24 12:08:25 -080088 del_page_from_lru_list(page, lruvec);
Yu Zhao87560172021-02-24 12:08:28 -080089 __clear_page_lru_flags(page);
Alex Shi6168d0d2020-12-15 12:34:29 -080090 unlock_page_lruvec_irqrestore(lruvec, flags);
Adrian Bunkb2213852006-09-25 23:31:02 -070091 }
Nicholas Piggin62906022016-12-25 13:00:30 +100092 __ClearPageWaiters(page);
Andrea Arcangeli91807062011-01-13 15:46:32 -080093}
94
95static void __put_single_page(struct page *page)
96{
97 __page_cache_release(page);
Matthew Wilcox (Oracle)bbc6b702021-05-01 20:42:23 -040098 mem_cgroup_uncharge(page_folio(page));
Mel Gorman44042b42021-06-28 19:43:08 -070099 free_unref_page(page, 0);
Adrian Bunkb2213852006-09-25 23:31:02 -0700100}
101
Andrea Arcangeli91807062011-01-13 15:46:32 -0800102static void __put_compound_page(struct page *page)
103{
Naoya Horiguchi822fc612015-04-15 16:14:35 -0700104 /*
105 * __page_cache_release() is supposed to be called for thp, not for
106 * hugetlb. This is because hugetlb page does never have PageLRU set
107 * (it's never listed to any LRU lists) and no memcg routines should
108 * be called for hugetlb (it has a separate hugetlb_cgroup.)
109 */
110 if (!PageHuge(page))
111 __page_cache_release(page);
Matthew Wilcox (Oracle)ff45fc32020-06-03 16:01:09 -0700112 destroy_compound_page(page);
Andrea Arcangeli91807062011-01-13 15:46:32 -0800113}
114
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800115void __put_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Dan Williams71389702017-04-28 10:23:37 -0700117 if (is_zone_device_page(page)) {
118 put_dev_pagemap(page->pgmap);
119
120 /*
121 * The page belongs to the device that created pgmap. Do
122 * not return it to page allocator.
123 */
124 return;
125 }
126
Nick Piggin8519fb32006-02-07 12:58:52 -0800127 if (unlikely(PageCompound(page)))
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800128 __put_compound_page(page);
129 else
Andrea Arcangeli91807062011-01-13 15:46:32 -0800130 __put_single_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800132EXPORT_SYMBOL(__put_page);
Andrea Arcangeli70b50f92011-11-02 13:36:59 -0700133
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700134/**
Randy Dunlap76824862008-03-19 17:00:40 -0700135 * put_pages_list() - release a list of pages
136 * @pages: list of pages threaded on page->lru
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700137 *
Matthew Wilcox (Oracle)988c69f2021-11-05 13:37:25 -0700138 * Release a list of pages which are strung together on page.lru.
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700139 */
140void put_pages_list(struct list_head *pages)
141{
Matthew Wilcox (Oracle)988c69f2021-11-05 13:37:25 -0700142 struct page *page, *next;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700143
Matthew Wilcox (Oracle)988c69f2021-11-05 13:37:25 -0700144 list_for_each_entry_safe(page, next, pages, lru) {
145 if (!put_page_testzero(page)) {
146 list_del(&page->lru);
147 continue;
148 }
149 if (PageHead(page)) {
150 list_del(&page->lru);
151 __put_compound_page(page);
152 continue;
153 }
154 /* Cannot be PageLRU because it's passed to us using the lru */
155 __ClearPageWaiters(page);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700156 }
Matthew Wilcox (Oracle)988c69f2021-11-05 13:37:25 -0700157
158 free_unref_page_list(pages);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700159}
160EXPORT_SYMBOL(put_pages_list);
161
Mel Gorman18022c52012-07-31 16:44:51 -0700162/*
163 * get_kernel_pages() - pin kernel pages in memory
164 * @kiov: An array of struct kvec structures
165 * @nr_segs: number of segments to pin
166 * @write: pinning for read/write, currently ignored
167 * @pages: array that receives pointers to the pages pinned.
168 * Should be at least nr_segs long.
169 *
170 * Returns number of pages pinned. This may be fewer than the number
171 * requested. If nr_pages is 0 or negative, returns 0. If no pages
172 * were pinned, returns -errno. Each page returned must be released
173 * with a put_page() call when it is finished with.
174 */
175int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
176 struct page **pages)
177{
178 int seg;
179
180 for (seg = 0; seg < nr_segs; seg++) {
181 if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
182 return seg;
183
Mel Gorman5a178112012-07-31 16:45:02 -0700184 pages[seg] = kmap_to_page(kiov[seg].iov_base);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300185 get_page(pages[seg]);
Mel Gorman18022c52012-07-31 16:44:51 -0700186 }
187
188 return seg;
189}
190EXPORT_SYMBOL_GPL(get_kernel_pages);
191
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700192static void pagevec_lru_move_fn(struct pagevec *pvec,
Alex Shic7c7b802020-12-15 12:33:56 -0800193 void (*move_fn)(struct page *page, struct lruvec *lruvec))
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700194{
195 int i;
Alex Shi6168d0d2020-12-15 12:34:29 -0800196 struct lruvec *lruvec = NULL;
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700197 unsigned long flags = 0;
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700198
199 for (i = 0; i < pagevec_count(pvec); i++) {
200 struct page *page = pvec->pages[i];
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -0400201 struct folio *folio = page_folio(page);
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700202
Alex Shifc574c22020-12-15 12:34:25 -0800203 /* block memcg migration during page moving between lru */
204 if (!TestClearPageLRU(page))
205 continue;
206
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -0400207 lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags);
Alex Shic7c7b802020-12-15 12:33:56 -0800208 (*move_fn)(page, lruvec);
Alex Shifc574c22020-12-15 12:34:25 -0800209
210 SetPageLRU(page);
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700211 }
Alex Shi6168d0d2020-12-15 12:34:29 -0800212 if (lruvec)
213 unlock_page_lruvec_irqrestore(lruvec, flags);
Mel Gormanc6f92f92017-11-15 17:37:55 -0800214 release_pages(pvec->pages, pvec->nr);
Linus Torvalds83896fb2011-01-17 14:42:34 -0800215 pagevec_reinit(pvec);
Shaohua Lid8505de2011-01-13 15:47:33 -0800216}
217
Alex Shic7c7b802020-12-15 12:33:56 -0800218static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec)
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700219{
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500220 struct folio *folio = page_folio(page);
221
222 if (!folio_test_unevictable(folio)) {
223 lruvec_del_folio(lruvec, folio);
224 folio_clear_active(folio);
225 lruvec_add_folio_tail(lruvec, folio);
226 __count_vm_events(PGROTATED, folio_nr_pages(folio));
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700227 }
228}
229
Minchan Kimd479960e2021-05-04 18:36:54 -0700230/* return true if pagevec needs to drain */
231static bool pagevec_add_and_need_flush(struct pagevec *pvec, struct page *page)
232{
233 bool ret = false;
234
235 if (!pagevec_add(pvec, page) || PageCompound(page) ||
236 lru_cache_disabled())
237 ret = true;
238
239 return ret;
240}
241
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700242/*
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500243 * Writeback is about to end against a folio which has been marked for
244 * immediate reclaim. If it still appears to be reclaimable, move it
245 * to the tail of the inactive list.
Alex Shic7c7b802020-12-15 12:33:56 -0800246 *
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500247 * folio_rotate_reclaimable() must disable IRQs, to prevent nasty races.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500249void folio_rotate_reclaimable(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500251 if (!folio_test_locked(folio) && !folio_test_dirty(folio) &&
252 !folio_test_unevictable(folio) && folio_test_lru(folio)) {
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700253 struct pagevec *pvec;
254 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500256 folio_get(folio);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200257 local_lock_irqsave(&lru_rotate.lock, flags);
258 pvec = this_cpu_ptr(&lru_rotate.pvec);
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500259 if (pagevec_add_and_need_flush(pvec, &folio->page))
Alex Shic7c7b802020-12-15 12:33:56 -0800260 pagevec_lru_move_fn(pvec, pagevec_move_tail_fn);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200261 local_unlock_irqrestore(&lru_rotate.lock, flags);
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Johannes Weiner96f8bf42020-06-03 16:03:09 -0700265void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages)
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800266{
Johannes Weiner7cf111b2020-06-03 16:03:06 -0700267 do {
268 unsigned long lrusize;
269
Alex Shi6168d0d2020-12-15 12:34:29 -0800270 /*
271 * Hold lruvec->lru_lock is safe here, since
272 * 1) The pinned lruvec in reclaim, or
273 * 2) From a pre-LRU page during refault (which also holds the
274 * rcu lock, so would be safe even if the page was on the LRU
275 * and could move simultaneously to a new lruvec).
276 */
277 spin_lock_irq(&lruvec->lru_lock);
Johannes Weiner7cf111b2020-06-03 16:03:06 -0700278 /* Record cost event */
Johannes Weiner96f8bf42020-06-03 16:03:09 -0700279 if (file)
280 lruvec->file_cost += nr_pages;
Johannes Weiner7cf111b2020-06-03 16:03:06 -0700281 else
Johannes Weiner96f8bf42020-06-03 16:03:09 -0700282 lruvec->anon_cost += nr_pages;
Johannes Weiner7cf111b2020-06-03 16:03:06 -0700283
284 /*
285 * Decay previous events
286 *
287 * Because workloads change over time (and to avoid
288 * overflow) we keep these statistics as a floating
289 * average, which ends up weighing recent refaults
290 * more than old ones.
291 */
292 lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) +
293 lruvec_page_state(lruvec, NR_ACTIVE_ANON) +
294 lruvec_page_state(lruvec, NR_INACTIVE_FILE) +
295 lruvec_page_state(lruvec, NR_ACTIVE_FILE);
296
297 if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) {
298 lruvec->file_cost /= 2;
299 lruvec->anon_cost /= 2;
300 }
Alex Shi6168d0d2020-12-15 12:34:29 -0800301 spin_unlock_irq(&lruvec->lru_lock);
Johannes Weiner7cf111b2020-06-03 16:03:06 -0700302 } while ((lruvec = parent_lruvec(lruvec)));
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800303}
304
Matthew Wilcox (Oracle)0995d7e2021-04-29 10:27:16 -0400305void lru_note_cost_folio(struct folio *folio)
Johannes Weiner96f8bf42020-06-03 16:03:09 -0700306{
Matthew Wilcox (Oracle)0995d7e2021-04-29 10:27:16 -0400307 lru_note_cost(folio_lruvec(folio), folio_is_file_lru(folio),
308 folio_nr_pages(folio));
Johannes Weiner96f8bf42020-06-03 16:03:09 -0700309}
310
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400311static void __folio_activate(struct folio *folio, struct lruvec *lruvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400313 if (!folio_test_active(folio) && !folio_test_unevictable(folio)) {
314 long nr_pages = folio_nr_pages(folio);
Linus Torvalds7a608572011-01-17 14:42:19 -0800315
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400316 lruvec_del_folio(lruvec, folio);
317 folio_set_active(folio);
318 lruvec_add_folio(lruvec, folio);
319 trace_mm_lru_activate(folio);
Linus Torvalds7a608572011-01-17 14:42:19 -0800320
Shakeel Butt21e330f2020-06-03 16:03:19 -0700321 __count_vm_events(PGACTIVATE, nr_pages);
322 __count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE,
323 nr_pages);
Linus Torvalds7a608572011-01-17 14:42:19 -0800324 }
Shaohua Lieb709b02011-05-24 17:12:55 -0700325}
326
327#ifdef CONFIG_SMP
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400328static void __activate_page(struct page *page, struct lruvec *lruvec)
329{
330 return __folio_activate(page_folio(page), lruvec);
331}
332
Shaohua Lieb709b02011-05-24 17:12:55 -0700333static void activate_page_drain(int cpu)
334{
Ingo Molnarb01b2142020-05-27 22:11:15 +0200335 struct pagevec *pvec = &per_cpu(lru_pvecs.activate_page, cpu);
Shaohua Lieb709b02011-05-24 17:12:55 -0700336
337 if (pagevec_count(pvec))
Alex Shic7c7b802020-12-15 12:33:56 -0800338 pagevec_lru_move_fn(pvec, __activate_page);
Shaohua Lieb709b02011-05-24 17:12:55 -0700339}
340
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700341static bool need_activate_page_drain(int cpu)
342{
Ingo Molnarb01b2142020-05-27 22:11:15 +0200343 return pagevec_count(&per_cpu(lru_pvecs.activate_page, cpu)) != 0;
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700344}
345
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400346static void folio_activate(struct folio *folio)
Shaohua Lieb709b02011-05-24 17:12:55 -0700347{
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400348 if (folio_test_lru(folio) && !folio_test_active(folio) &&
349 !folio_test_unevictable(folio)) {
Ingo Molnarb01b2142020-05-27 22:11:15 +0200350 struct pagevec *pvec;
Shaohua Lieb709b02011-05-24 17:12:55 -0700351
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400352 folio_get(folio);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200353 local_lock(&lru_pvecs.lock);
354 pvec = this_cpu_ptr(&lru_pvecs.activate_page);
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400355 if (pagevec_add_and_need_flush(pvec, &folio->page))
Alex Shic7c7b802020-12-15 12:33:56 -0800356 pagevec_lru_move_fn(pvec, __activate_page);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200357 local_unlock(&lru_pvecs.lock);
Shaohua Lieb709b02011-05-24 17:12:55 -0700358 }
359}
360
361#else
362static inline void activate_page_drain(int cpu)
363{
364}
365
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400366static void folio_activate(struct folio *folio)
Shaohua Lieb709b02011-05-24 17:12:55 -0700367{
Alex Shi6168d0d2020-12-15 12:34:29 -0800368 struct lruvec *lruvec;
Shaohua Lieb709b02011-05-24 17:12:55 -0700369
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400370 if (folio_test_clear_lru(folio)) {
Matthew Wilcox (Oracle)e809c3f2021-06-28 21:59:47 -0400371 lruvec = folio_lruvec_lock_irq(folio);
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400372 __folio_activate(folio, lruvec);
Alex Shi6168d0d2020-12-15 12:34:29 -0800373 unlock_page_lruvec_irq(lruvec);
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400374 folio_set_lru(folio);
Alex Shi6168d0d2020-12-15 12:34:29 -0800375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
Shaohua Lieb709b02011-05-24 17:12:55 -0700377#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400379static void __lru_cache_activate_folio(struct folio *folio)
Mel Gorman059285a2013-07-03 15:02:30 -0700380{
Ingo Molnarb01b2142020-05-27 22:11:15 +0200381 struct pagevec *pvec;
Mel Gorman059285a2013-07-03 15:02:30 -0700382 int i;
383
Ingo Molnarb01b2142020-05-27 22:11:15 +0200384 local_lock(&lru_pvecs.lock);
385 pvec = this_cpu_ptr(&lru_pvecs.lru_add);
386
Mel Gorman059285a2013-07-03 15:02:30 -0700387 /*
388 * Search backwards on the optimistic assumption that the page being
389 * activated has just been added to this pagevec. Note that only
390 * the local pagevec is examined as a !PageLRU page could be in the
391 * process of being released, reclaimed, migrated or on a remote
392 * pagevec that is currently being drained. Furthermore, marking
393 * a remote pagevec's page PageActive potentially hits a race where
394 * a page is marked PageActive just after it is added to the inactive
395 * list causing accounting errors and BUG_ON checks to trigger.
396 */
397 for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
398 struct page *pagevec_page = pvec->pages[i];
399
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400400 if (pagevec_page == &folio->page) {
401 folio_set_active(folio);
Mel Gorman059285a2013-07-03 15:02:30 -0700402 break;
403 }
404 }
405
Ingo Molnarb01b2142020-05-27 22:11:15 +0200406 local_unlock(&lru_pvecs.lock);
Mel Gorman059285a2013-07-03 15:02:30 -0700407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/*
410 * Mark a page as having seen activity.
411 *
412 * inactive,unreferenced -> inactive,referenced
413 * inactive,referenced -> active,unreferenced
414 * active,unreferenced -> active,referenced
Hugh Dickinseb39d612014-08-06 16:06:43 -0700415 *
416 * When a newly allocated page is not yet visible, so safe for non-atomic ops,
417 * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400419void folio_mark_accessed(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400421 if (!folio_test_referenced(folio)) {
422 folio_set_referenced(folio);
423 } else if (folio_test_unevictable(folio)) {
Fengguang Wua1100a72019-11-30 17:50:00 -0800424 /*
425 * Unevictable pages are on the "LRU_UNEVICTABLE" list. But,
426 * this list is never rotated or maintained, so marking an
427 * evictable page accessed has no effect.
428 */
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400429 } else if (!folio_test_active(folio)) {
Mel Gorman059285a2013-07-03 15:02:30 -0700430 /*
431 * If the page is on the LRU, queue it for activation via
Ingo Molnarb01b2142020-05-27 22:11:15 +0200432 * lru_pvecs.activate_page. Otherwise, assume the page is on a
Mel Gorman059285a2013-07-03 15:02:30 -0700433 * pagevec, mark it active and it'll be moved to the active
434 * LRU on the next drain.
435 */
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400436 if (folio_test_lru(folio))
437 folio_activate(folio);
Mel Gorman059285a2013-07-03 15:02:30 -0700438 else
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400439 __lru_cache_activate_folio(folio);
440 folio_clear_referenced(folio);
441 workingset_activation(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400443 if (folio_test_idle(folio))
444 folio_clear_idle(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400446EXPORT_SYMBOL(folio_mark_accessed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
KOSAKI Motohirof04e9eb2008-10-18 20:26:19 -0700448/**
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400449 * folio_add_lru - Add a folio to an LRU list.
450 * @folio: The folio to be added to the LRU.
Jianyu Zhan2329d372014-06-04 16:07:31 -0700451 *
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400452 * Queue the folio for addition to the LRU. The decision on whether
Jianyu Zhan2329d372014-06-04 16:07:31 -0700453 * to add the page to the [in]active [file|anon] list is deferred until the
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400454 * pagevec is drained. This gives a chance for the caller of folio_add_lru()
455 * have the folio added to the active list using folio_mark_accessed().
KOSAKI Motohirof04e9eb2008-10-18 20:26:19 -0700456 */
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400457void folio_add_lru(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Johannes Weiner6058eae2020-06-03 16:02:40 -0700459 struct pagevec *pvec;
460
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400461 VM_BUG_ON_FOLIO(folio_test_active(folio) && folio_test_unevictable(folio), folio);
462 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
Johannes Weiner6058eae2020-06-03 16:02:40 -0700463
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400464 folio_get(folio);
Johannes Weiner6058eae2020-06-03 16:02:40 -0700465 local_lock(&lru_pvecs.lock);
466 pvec = this_cpu_ptr(&lru_pvecs.lru_add);
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400467 if (pagevec_add_and_need_flush(pvec, &folio->page))
Johannes Weiner6058eae2020-06-03 16:02:40 -0700468 __pagevec_lru_add(pvec);
469 local_unlock(&lru_pvecs.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400471EXPORT_SYMBOL(folio_add_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700473/**
Joonsoo Kimb5181542020-08-11 18:30:40 -0700474 * lru_cache_add_inactive_or_unevictable
Johannes Weiner00501b52014-08-08 14:19:20 -0700475 * @page: the page to be added to LRU
476 * @vma: vma in which page is mapped for determining reclaimability
477 *
Joonsoo Kimb5181542020-08-11 18:30:40 -0700478 * Place @page on the inactive or unevictable LRU list, depending on its
Miaohe Lin12eab422020-10-13 16:52:24 -0700479 * evictability.
Johannes Weiner00501b52014-08-08 14:19:20 -0700480 */
Joonsoo Kimb5181542020-08-11 18:30:40 -0700481void lru_cache_add_inactive_or_unevictable(struct page *page,
Johannes Weiner00501b52014-08-08 14:19:20 -0700482 struct vm_area_struct *vma)
483{
Joonsoo Kimb5181542020-08-11 18:30:40 -0700484 bool unevictable;
485
Johannes Weiner00501b52014-08-08 14:19:20 -0700486 VM_BUG_ON_PAGE(PageLRU(page), page);
487
Joonsoo Kimb5181542020-08-11 18:30:40 -0700488 unevictable = (vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED;
489 if (unlikely(unevictable) && !TestSetPageMlocked(page)) {
Hugh Dickins09647302020-09-18 21:20:15 -0700490 int nr_pages = thp_nr_pages(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700491 /*
Shijie Luocb152a12021-05-06 18:05:51 -0700492 * We use the irq-unsafe __mod_zone_page_state because this
Johannes Weiner00501b52014-08-08 14:19:20 -0700493 * counter is not modified from interrupt context, and the pte
494 * lock is held(spinlock), which implies preemption disabled.
495 */
Hugh Dickins09647302020-09-18 21:20:15 -0700496 __mod_zone_page_state(page_zone(page), NR_MLOCK, nr_pages);
497 count_vm_events(UNEVICTABLE_PGMLOCKED, nr_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -0700498 }
Shakeel Butt9c4e6b12018-02-21 14:45:28 -0800499 lru_cache_add(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700500}
501
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700502/*
Minchan Kim31560182011-03-22 16:32:52 -0700503 * If the page can not be invalidated, it is moved to the
504 * inactive list to speed up its reclaim. It is moved to the
505 * head of the list, rather than the tail, to give the flusher
506 * threads some time to write it out, as this is much more
507 * effective than the single-page writeout from reclaim.
Minchan Kim278df9f2011-03-22 16:32:54 -0700508 *
509 * If the page isn't page_mapped and dirty/writeback, the page
510 * could reclaim asap using PG_reclaim.
511 *
512 * 1. active, mapped page -> none
513 * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
514 * 3. inactive, mapped page -> none
515 * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
516 * 5. inactive, clean -> inactive, tail
517 * 6. Others -> none
518 *
519 * In 4, why it moves inactive's head, the VM expects the page would
520 * be write it out by flusher threads as this is much more effective
521 * than the single-page writeout from reclaim.
Minchan Kim31560182011-03-22 16:32:52 -0700522 */
Alex Shic7c7b802020-12-15 12:33:56 -0800523static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec)
Minchan Kim31560182011-03-22 16:32:52 -0700524{
Yu Zhao46ae6b22021-02-24 12:08:25 -0800525 bool active = PageActive(page);
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700526 int nr_pages = thp_nr_pages(page);
Minchan Kim31560182011-03-22 16:32:52 -0700527
Minchan Kimbad49d92011-05-11 15:13:30 -0700528 if (PageUnevictable(page))
529 return;
530
Minchan Kim31560182011-03-22 16:32:52 -0700531 /* Some processes are using the page */
532 if (page_mapped(page))
533 return;
534
Yu Zhao46ae6b22021-02-24 12:08:25 -0800535 del_page_from_lru_list(page, lruvec);
Minchan Kim31560182011-03-22 16:32:52 -0700536 ClearPageActive(page);
537 ClearPageReferenced(page);
Minchan Kim31560182011-03-22 16:32:52 -0700538
Minchan Kim278df9f2011-03-22 16:32:54 -0700539 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 */
Yu Zhao3a9c9782021-02-24 12:08:17 -0800545 add_page_to_lru_list(page, lruvec);
Minchan Kim278df9f2011-03-22 16:32:54 -0700546 SetPageReclaim(page);
547 } else {
548 /*
549 * The page's writeback ends up during pagevec
Hyeonggon Yooc4ffefd2021-06-30 18:53:10 -0700550 * We move that page into tail of inactive.
Minchan Kim278df9f2011-03-22 16:32:54 -0700551 */
Yu Zhao3a9c9782021-02-24 12:08:17 -0800552 add_page_to_lru_list_tail(page, lruvec);
Shakeel Butt5d91f312020-06-03 16:03:16 -0700553 __count_vm_events(PGROTATED, nr_pages);
Minchan Kim278df9f2011-03-22 16:32:54 -0700554 }
555
Shakeel Butt21e330f2020-06-03 16:03:19 -0700556 if (active) {
Shakeel Butt5d91f312020-06-03 16:03:16 -0700557 __count_vm_events(PGDEACTIVATE, nr_pages);
Shakeel Butt21e330f2020-06-03 16:03:19 -0700558 __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
559 nr_pages);
560 }
Minchan Kim31560182011-03-22 16:32:52 -0700561}
562
Alex Shic7c7b802020-12-15 12:33:56 -0800563static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec)
Minchan Kim9c276cc2019-09-25 16:49:08 -0700564{
Alex Shifc574c22020-12-15 12:34:25 -0800565 if (PageActive(page) && !PageUnevictable(page)) {
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700566 int nr_pages = thp_nr_pages(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700567
Yu Zhao46ae6b22021-02-24 12:08:25 -0800568 del_page_from_lru_list(page, lruvec);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700569 ClearPageActive(page);
570 ClearPageReferenced(page);
Yu Zhao3a9c9782021-02-24 12:08:17 -0800571 add_page_to_lru_list(page, lruvec);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700572
Shakeel Butt21e330f2020-06-03 16:03:19 -0700573 __count_vm_events(PGDEACTIVATE, nr_pages);
574 __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
575 nr_pages);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700576 }
577}
Minchan Kim10853a02016-01-15 16:55:11 -0800578
Alex Shic7c7b802020-12-15 12:33:56 -0800579static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec)
Minchan Kim10853a02016-01-15 16:55:11 -0800580{
Alex Shifc574c22020-12-15 12:34:25 -0800581 if (PageAnon(page) && PageSwapBacked(page) &&
Shaohua Li24c92eb2017-10-03 16:15:29 -0700582 !PageSwapCache(page) && !PageUnevictable(page)) {
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700583 int nr_pages = thp_nr_pages(page);
Minchan Kim10853a02016-01-15 16:55:11 -0800584
Yu Zhao46ae6b22021-02-24 12:08:25 -0800585 del_page_from_lru_list(page, lruvec);
Minchan Kim10853a02016-01-15 16:55:11 -0800586 ClearPageActive(page);
587 ClearPageReferenced(page);
Shaohua Lif7ad2a62017-05-03 14:52:29 -0700588 /*
Huang Ying9de4f222020-04-06 20:04:41 -0700589 * Lazyfree pages are clean anonymous pages. They have
590 * PG_swapbacked flag cleared, to distinguish them from normal
591 * anonymous pages
Shaohua Lif7ad2a62017-05-03 14:52:29 -0700592 */
593 ClearPageSwapBacked(page);
Yu Zhao3a9c9782021-02-24 12:08:17 -0800594 add_page_to_lru_list(page, lruvec);
Minchan Kim10853a02016-01-15 16:55:11 -0800595
Shakeel Butt21e330f2020-06-03 16:03:19 -0700596 __count_vm_events(PGLAZYFREE, nr_pages);
597 __count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE,
598 nr_pages);
Minchan Kim10853a02016-01-15 16:55:11 -0800599 }
600}
601
Minchan Kim31560182011-03-22 16:32:52 -0700602/*
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700603 * Drain pages out of the cpu's pagevecs.
604 * Either "cpu" is the current CPU, and preemption has already been
605 * disabled; or "cpu" is being hot-unplugged, and is already dead.
606 */
Konstantin Khlebnikovf0cb3c72012-03-21 16:34:06 -0700607void lru_add_drain_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Ingo Molnarb01b2142020-05-27 22:11:15 +0200609 struct pagevec *pvec = &per_cpu(lru_pvecs.lru_add, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Mel Gorman13f7f782013-07-03 15:02:28 -0700611 if (pagevec_count(pvec))
Mel Gormana0b8cab32013-07-03 15:02:32 -0700612 __pagevec_lru_add(pvec);
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700613
Ingo Molnarb01b2142020-05-27 22:11:15 +0200614 pvec = &per_cpu(lru_rotate.pvec, cpu);
Qian Cai7e0cc012020-08-14 17:31:50 -0700615 /* Disabling interrupts below acts as a compiler barrier. */
616 if (data_race(pagevec_count(pvec))) {
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700617 unsigned long flags;
618
619 /* No harm done if a racing interrupt already did this */
Ingo Molnarb01b2142020-05-27 22:11:15 +0200620 local_lock_irqsave(&lru_rotate.lock, flags);
Alex Shic7c7b802020-12-15 12:33:56 -0800621 pagevec_lru_move_fn(pvec, pagevec_move_tail_fn);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200622 local_unlock_irqrestore(&lru_rotate.lock, flags);
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700623 }
Minchan Kim31560182011-03-22 16:32:52 -0700624
Ingo Molnarb01b2142020-05-27 22:11:15 +0200625 pvec = &per_cpu(lru_pvecs.lru_deactivate_file, cpu);
Minchan Kim31560182011-03-22 16:32:52 -0700626 if (pagevec_count(pvec))
Alex Shic7c7b802020-12-15 12:33:56 -0800627 pagevec_lru_move_fn(pvec, lru_deactivate_file_fn);
Shaohua Lieb709b02011-05-24 17:12:55 -0700628
Ingo Molnarb01b2142020-05-27 22:11:15 +0200629 pvec = &per_cpu(lru_pvecs.lru_deactivate, cpu);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700630 if (pagevec_count(pvec))
Alex Shic7c7b802020-12-15 12:33:56 -0800631 pagevec_lru_move_fn(pvec, lru_deactivate_fn);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700632
Ingo Molnarb01b2142020-05-27 22:11:15 +0200633 pvec = &per_cpu(lru_pvecs.lru_lazyfree, cpu);
Minchan Kim10853a02016-01-15 16:55:11 -0800634 if (pagevec_count(pvec))
Alex Shic7c7b802020-12-15 12:33:56 -0800635 pagevec_lru_move_fn(pvec, lru_lazyfree_fn);
Minchan Kim10853a02016-01-15 16:55:11 -0800636
Shaohua Lieb709b02011-05-24 17:12:55 -0700637 activate_page_drain(cpu);
Minchan Kim31560182011-03-22 16:32:52 -0700638}
639
640/**
Minchan Kimcc5993b2015-04-15 16:13:26 -0700641 * deactivate_file_page - forcefully deactivate a file page
Minchan Kim31560182011-03-22 16:32:52 -0700642 * @page: page to deactivate
643 *
644 * This function hints the VM that @page is a good reclaim candidate,
645 * for example if its invalidation fails due to the page being dirty
646 * or under writeback.
647 */
Minchan Kimcc5993b2015-04-15 16:13:26 -0700648void deactivate_file_page(struct page *page)
Minchan Kim31560182011-03-22 16:32:52 -0700649{
Minchan Kim821ed6b2011-05-24 17:12:31 -0700650 /*
Minchan Kimcc5993b2015-04-15 16:13:26 -0700651 * In a workload with many unevictable page such as mprotect,
652 * unevictable page deactivation for accelerating reclaim is pointless.
Minchan Kim821ed6b2011-05-24 17:12:31 -0700653 */
654 if (PageUnevictable(page))
655 return;
656
Minchan Kim31560182011-03-22 16:32:52 -0700657 if (likely(get_page_unless_zero(page))) {
Ingo Molnarb01b2142020-05-27 22:11:15 +0200658 struct pagevec *pvec;
659
660 local_lock(&lru_pvecs.lock);
661 pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate_file);
Minchan Kim31560182011-03-22 16:32:52 -0700662
Minchan Kimd479960e2021-05-04 18:36:54 -0700663 if (pagevec_add_and_need_flush(pvec, page))
Alex Shic7c7b802020-12-15 12:33:56 -0800664 pagevec_lru_move_fn(pvec, lru_deactivate_file_fn);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200665 local_unlock(&lru_pvecs.lock);
Minchan Kim31560182011-03-22 16:32:52 -0700666 }
Andrew Morton80bfed92006-01-06 00:11:14 -0800667}
668
Minchan Kim9c276cc2019-09-25 16:49:08 -0700669/*
670 * deactivate_page - deactivate a page
671 * @page: page to deactivate
672 *
673 * deactivate_page() moves @page to the inactive list if @page was on the active
674 * list and was not an unevictable page. This is done to accelerate the reclaim
675 * of @page.
676 */
677void deactivate_page(struct page *page)
678{
679 if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
Ingo Molnarb01b2142020-05-27 22:11:15 +0200680 struct pagevec *pvec;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700681
Ingo Molnarb01b2142020-05-27 22:11:15 +0200682 local_lock(&lru_pvecs.lock);
683 pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700684 get_page(page);
Minchan Kimd479960e2021-05-04 18:36:54 -0700685 if (pagevec_add_and_need_flush(pvec, page))
Alex Shic7c7b802020-12-15 12:33:56 -0800686 pagevec_lru_move_fn(pvec, lru_deactivate_fn);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200687 local_unlock(&lru_pvecs.lock);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700688 }
689}
690
Minchan Kim10853a02016-01-15 16:55:11 -0800691/**
Shaohua Lif7ad2a62017-05-03 14:52:29 -0700692 * mark_page_lazyfree - make an anon page lazyfree
Minchan Kim10853a02016-01-15 16:55:11 -0800693 * @page: page to deactivate
694 *
Shaohua Lif7ad2a62017-05-03 14:52:29 -0700695 * mark_page_lazyfree() moves @page to the inactive file list.
696 * This is done to accelerate the reclaim of @page.
Minchan Kim10853a02016-01-15 16:55:11 -0800697 */
Shaohua Lif7ad2a62017-05-03 14:52:29 -0700698void mark_page_lazyfree(struct page *page)
Minchan Kim10853a02016-01-15 16:55:11 -0800699{
Shaohua Lif7ad2a62017-05-03 14:52:29 -0700700 if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
Shaohua Li24c92eb2017-10-03 16:15:29 -0700701 !PageSwapCache(page) && !PageUnevictable(page)) {
Ingo Molnarb01b2142020-05-27 22:11:15 +0200702 struct pagevec *pvec;
Minchan Kim10853a02016-01-15 16:55:11 -0800703
Ingo Molnarb01b2142020-05-27 22:11:15 +0200704 local_lock(&lru_pvecs.lock);
705 pvec = this_cpu_ptr(&lru_pvecs.lru_lazyfree);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300706 get_page(page);
Minchan Kimd479960e2021-05-04 18:36:54 -0700707 if (pagevec_add_and_need_flush(pvec, page))
Alex Shic7c7b802020-12-15 12:33:56 -0800708 pagevec_lru_move_fn(pvec, lru_lazyfree_fn);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200709 local_unlock(&lru_pvecs.lock);
Minchan Kim10853a02016-01-15 16:55:11 -0800710 }
711}
712
Andrew Morton80bfed92006-01-06 00:11:14 -0800713void lru_add_drain(void)
714{
Ingo Molnarb01b2142020-05-27 22:11:15 +0200715 local_lock(&lru_pvecs.lock);
716 lru_add_drain_cpu(smp_processor_id());
717 local_unlock(&lru_pvecs.lock);
718}
719
Minchan Kim243418e2021-09-24 15:43:47 -0700720/*
721 * It's called from per-cpu workqueue context in SMP case so
722 * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on
723 * the same cpu. It shouldn't be a problem in !SMP case since
724 * the core is only one and the locks will disable preemption.
725 */
726static void lru_add_and_bh_lrus_drain(void)
727{
728 local_lock(&lru_pvecs.lock);
729 lru_add_drain_cpu(smp_processor_id());
730 local_unlock(&lru_pvecs.lock);
731 invalidate_bh_lrus_cpu();
732}
733
Ingo Molnarb01b2142020-05-27 22:11:15 +0200734void lru_add_drain_cpu_zone(struct zone *zone)
735{
736 local_lock(&lru_pvecs.lock);
737 lru_add_drain_cpu(smp_processor_id());
738 drain_local_pages(zone);
739 local_unlock(&lru_pvecs.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
Michal Hocko6ea183d2019-02-20 22:19:54 -0800742#ifdef CONFIG_SMP
743
744static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
745
David Howellsc4028952006-11-22 14:57:56 +0000746static void lru_add_drain_per_cpu(struct work_struct *dummy)
Nick Piggin053837f2006-01-18 17:42:27 -0800747{
Minchan Kim243418e2021-09-24 15:43:47 -0700748 lru_add_and_bh_lrus_drain();
Nick Piggin053837f2006-01-18 17:42:27 -0800749}
750
Michal Hocko9852a722018-01-31 16:16:19 -0800751/*
752 * Doesn't need any cpu hotplug locking because we do rely on per-cpu
753 * kworkers being shut down before our page_alloc_cpu_dead callback is
754 * executed on the offlined cpu.
755 * Calling this function with cpu hotplug locks held can actually lead
756 * to obscure indirect dependencies via WQ context.
757 */
Minchan Kimd479960e2021-05-04 18:36:54 -0700758inline void __lru_add_drain_all(bool force_all_cpus)
Nick Piggin053837f2006-01-18 17:42:27 -0800759{
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200760 /*
761 * lru_drain_gen - Global pages generation number
762 *
763 * (A) Definition: global lru_drain_gen = x implies that all generations
764 * 0 < n <= x are already *scheduled* for draining.
765 *
766 * This is an optimization for the highly-contended use case where a
767 * user space workload keeps constantly generating a flow of pages for
768 * each CPU.
769 */
770 static unsigned int lru_drain_gen;
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700771 static struct cpumask has_work;
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200772 static DEFINE_MUTEX(lock);
773 unsigned cpu, this_gen;
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700774
Michal Hockoce612872017-04-07 16:05:05 -0700775 /*
776 * Make sure nobody triggers this path before mm_percpu_wq is fully
777 * initialized.
778 */
779 if (WARN_ON(!mm_percpu_wq))
780 return;
781
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200782 /*
783 * Guarantee pagevec counter stores visible by this CPU are visible to
784 * other CPUs before loading the current drain generation.
785 */
786 smp_mb();
787
788 /*
789 * (B) Locally cache global LRU draining generation number
790 *
791 * The read barrier ensures that the counter is loaded before the mutex
792 * is taken. It pairs with smp_mb() inside the mutex critical section
793 * at (D).
794 */
795 this_gen = smp_load_acquire(&lru_drain_gen);
Konstantin Khlebnikoveef1a422019-11-30 17:50:40 -0800796
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700797 mutex_lock(&lock);
Konstantin Khlebnikoveef1a422019-11-30 17:50:40 -0800798
799 /*
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200800 * (C) Exit the draining operation if a newer generation, from another
801 * lru_add_drain_all(), was already scheduled for draining. Check (A).
Konstantin Khlebnikoveef1a422019-11-30 17:50:40 -0800802 */
Minchan Kimd479960e2021-05-04 18:36:54 -0700803 if (unlikely(this_gen != lru_drain_gen && !force_all_cpus))
Konstantin Khlebnikoveef1a422019-11-30 17:50:40 -0800804 goto done;
805
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200806 /*
807 * (D) Increment global generation number
808 *
809 * Pairs with smp_load_acquire() at (B), outside of the critical
810 * section. Use a full memory barrier to guarantee that the new global
811 * drain generation number is stored before loading pagevec counters.
812 *
813 * This pairing must be done here, before the for_each_online_cpu loop
814 * below which drains the page vectors.
815 *
816 * Let x, y, and z represent some system CPU numbers, where x < y < z.
Shijie Luocb152a12021-05-06 18:05:51 -0700817 * Assume CPU #z is in the middle of the for_each_online_cpu loop
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200818 * below and has already reached CPU #y's per-cpu data. CPU #x comes
819 * along, adds some pages to its per-cpu vectors, then calls
820 * lru_add_drain_all().
821 *
822 * If the paired barrier is done at any later step, e.g. after the
823 * loop, CPU #x will just exit at (C) and miss flushing out all of its
824 * added pages.
825 */
826 WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1);
827 smp_mb();
Konstantin Khlebnikoveef1a422019-11-30 17:50:40 -0800828
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700829 cpumask_clear(&has_work);
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700830 for_each_online_cpu(cpu) {
831 struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
832
Minchan Kimd479960e2021-05-04 18:36:54 -0700833 if (force_all_cpus ||
834 pagevec_count(&per_cpu(lru_pvecs.lru_add, cpu)) ||
Qian Cai7e0cc012020-08-14 17:31:50 -0700835 data_race(pagevec_count(&per_cpu(lru_rotate.pvec, cpu))) ||
Ingo Molnarb01b2142020-05-27 22:11:15 +0200836 pagevec_count(&per_cpu(lru_pvecs.lru_deactivate_file, cpu)) ||
837 pagevec_count(&per_cpu(lru_pvecs.lru_deactivate, cpu)) ||
838 pagevec_count(&per_cpu(lru_pvecs.lru_lazyfree, cpu)) ||
Minchan Kim8cc621d2021-05-04 18:37:00 -0700839 need_activate_page_drain(cpu) ||
840 has_bh_in_lru(cpu, NULL)) {
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700841 INIT_WORK(work, lru_add_drain_per_cpu);
Michal Hockoce612872017-04-07 16:05:05 -0700842 queue_work_on(cpu, mm_percpu_wq, work);
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200843 __cpumask_set_cpu(cpu, &has_work);
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700844 }
845 }
846
847 for_each_cpu(cpu, &has_work)
848 flush_work(&per_cpu(lru_add_drain_work, cpu));
849
Konstantin Khlebnikoveef1a422019-11-30 17:50:40 -0800850done:
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700851 mutex_unlock(&lock);
Nick Piggin053837f2006-01-18 17:42:27 -0800852}
Minchan Kimd479960e2021-05-04 18:36:54 -0700853
854void lru_add_drain_all(void)
855{
856 __lru_add_drain_all(false);
857}
Michal Hocko6ea183d2019-02-20 22:19:54 -0800858#else
859void lru_add_drain_all(void)
860{
861 lru_add_drain();
862}
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200863#endif /* CONFIG_SMP */
Nick Piggin053837f2006-01-18 17:42:27 -0800864
Minchan Kimd479960e2021-05-04 18:36:54 -0700865atomic_t lru_disable_count = ATOMIC_INIT(0);
866
867/*
868 * lru_cache_disable() needs to be called before we start compiling
869 * a list of pages to be migrated using isolate_lru_page().
870 * It drains pages on LRU cache and then disable on all cpus until
871 * lru_cache_enable is called.
872 *
873 * Must be paired with a call to lru_cache_enable().
874 */
875void lru_cache_disable(void)
876{
877 atomic_inc(&lru_disable_count);
878#ifdef CONFIG_SMP
879 /*
880 * lru_add_drain_all in the force mode will schedule draining on
881 * all online CPUs so any calls of lru_cache_disabled wrapped by
882 * local_lock or preemption disabled would be ordered by that.
883 * The atomic operation doesn't need to have stronger ordering
884 * requirements because that is enforeced by the scheduling
885 * guarantees.
886 */
887 __lru_add_drain_all(true);
888#else
Minchan Kim243418e2021-09-24 15:43:47 -0700889 lru_add_and_bh_lrus_drain();
Minchan Kimd479960e2021-05-04 18:36:54 -0700890#endif
891}
892
Michal Hockoaabfb572014-10-09 15:28:52 -0700893/**
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300894 * release_pages - batched put_page()
Michal Hockoaabfb572014-10-09 15:28:52 -0700895 * @pages: array of pages to release
896 * @nr: number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 *
Michal Hockoaabfb572014-10-09 15:28:52 -0700898 * Decrement the reference count on all the pages in @pages. If it
899 * fell to zero, remove the page from the LRU and free it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 */
Mel Gormanc6f92f92017-11-15 17:37:55 -0800901void release_pages(struct page **pages, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 int i;
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -0800904 LIST_HEAD(pages_to_free);
Alex Shi6168d0d2020-12-15 12:34:29 -0800905 struct lruvec *lruvec = NULL;
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -0400906 unsigned long flags = 0;
Kees Cook3f649ab2020-06-03 13:09:38 -0700907 unsigned int lock_batch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 for (i = 0; i < nr; i++) {
910 struct page *page = pages[i];
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -0400911 struct folio *folio = page_folio(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Michal Hockoaabfb572014-10-09 15:28:52 -0700913 /*
914 * Make sure the IRQ-safe lock-holding time does not get
915 * excessive with a continuous string of pages from the
Alex Shi6168d0d2020-12-15 12:34:29 -0800916 * same lruvec. The lock is held only if lruvec != NULL.
Michal Hockoaabfb572014-10-09 15:28:52 -0700917 */
Alex Shi6168d0d2020-12-15 12:34:29 -0800918 if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) {
919 unlock_page_lruvec_irqrestore(lruvec, flags);
920 lruvec = NULL;
Michal Hockoaabfb572014-10-09 15:28:52 -0700921 }
922
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -0400923 page = &folio->page;
Aaron Lu6fcb52a2016-10-07 17:00:08 -0700924 if (is_huge_zero_page(page))
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -0700925 continue;
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -0700926
Ira Weinyc5d6c452019-06-05 14:49:22 -0700927 if (is_zone_device_page(page)) {
Alex Shi6168d0d2020-12-15 12:34:29 -0800928 if (lruvec) {
929 unlock_page_lruvec_irqrestore(lruvec, flags);
930 lruvec = NULL;
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700931 }
Ira Weinyc5d6c452019-06-05 14:49:22 -0700932 /*
933 * ZONE_DEVICE pages that return 'false' from
Miaohe Lina3e7bea2020-10-13 16:52:15 -0700934 * page_is_devmap_managed() do not require special
Ira Weinyc5d6c452019-06-05 14:49:22 -0700935 * processing, and instead, expect a call to
936 * put_page_testzero().
937 */
John Hubbard07d80262020-01-30 22:12:28 -0800938 if (page_is_devmap_managed(page)) {
939 put_devmap_managed_page(page);
Ira Weinyc5d6c452019-06-05 14:49:22 -0700940 continue;
John Hubbard07d80262020-01-30 22:12:28 -0800941 }
Ralph Campbell43fbdeb2020-12-14 19:05:55 -0800942 if (put_page_testzero(page))
943 put_dev_pagemap(page->pgmap);
944 continue;
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700945 }
946
Nick Pigginb5810032005-10-29 18:16:12 -0700947 if (!put_page_testzero(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 continue;
949
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800950 if (PageCompound(page)) {
Alex Shi6168d0d2020-12-15 12:34:29 -0800951 if (lruvec) {
952 unlock_page_lruvec_irqrestore(lruvec, flags);
953 lruvec = NULL;
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800954 }
955 __put_compound_page(page);
956 continue;
957 }
958
Nick Piggin46453a62006-03-22 00:07:58 -0800959 if (PageLRU(page)) {
Alexander Duyck2a5e4e32020-12-15 12:34:33 -0800960 struct lruvec *prev_lruvec = lruvec;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700961
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -0400962 lruvec = folio_lruvec_relock_irqsave(folio, lruvec,
Alexander Duyck2a5e4e32020-12-15 12:34:33 -0800963 &flags);
964 if (prev_lruvec != lruvec)
Michal Hockoaabfb572014-10-09 15:28:52 -0700965 lock_batch = 0;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700966
Yu Zhao46ae6b22021-02-24 12:08:25 -0800967 del_page_from_lru_list(page, lruvec);
Yu Zhao87560172021-02-24 12:08:28 -0800968 __clear_page_lru_flags(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Nick Piggin46453a62006-03-22 00:07:58 -0800970
Nicholas Piggin62906022016-12-25 13:00:30 +1000971 __ClearPageWaiters(page);
Mel Gormanc53954a2013-07-03 15:02:34 -0700972
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -0800973 list_add(&page->lru, &pages_to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Alex Shi6168d0d2020-12-15 12:34:29 -0800975 if (lruvec)
976 unlock_page_lruvec_irqrestore(lruvec, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Johannes Weiner747db952014-08-08 14:19:24 -0700978 mem_cgroup_uncharge_list(&pages_to_free);
Mel Gorman2d4894b2017-11-15 17:37:59 -0800979 free_unref_page_list(&pages_to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
Miklos Szeredi0be85572010-10-27 15:34:46 -0700981EXPORT_SYMBOL(release_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983/*
984 * The pages which we're about to release may be in the deferred lru-addition
985 * queues. That would prevent them from really being freed right now. That's
986 * OK from a correctness point of view but is inefficient - those pages may be
987 * cache-warm and we want to give them back to the page allocator ASAP.
988 *
989 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
990 * and __pagevec_lru_add_active() call release_pages() directly to avoid
991 * mutual recursion.
992 */
993void __pagevec_release(struct pagevec *pvec)
994{
Mel Gorman7f0b5fb2017-11-15 17:38:10 -0800995 if (!pvec->percpu_pvec_drained) {
Mel Gormand9ed0d02017-11-15 17:37:48 -0800996 lru_add_drain();
Mel Gorman7f0b5fb2017-11-15 17:38:10 -0800997 pvec->percpu_pvec_drained = true;
Mel Gormand9ed0d02017-11-15 17:37:48 -0800998 }
Mel Gormanc6f92f92017-11-15 17:37:55 -0800999 release_pages(pvec->pages, pagevec_count(pvec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 pagevec_reinit(pvec);
1001}
Steve French7f285702005-11-01 10:22:55 -08001002EXPORT_SYMBOL(__pagevec_release);
1003
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001004static void __pagevec_lru_add_fn(struct folio *folio, struct lruvec *lruvec)
Shaohua Li3dd7ae82011-03-22 16:33:45 -07001005{
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001006 int was_unevictable = folio_test_clear_unevictable(folio);
1007 long nr_pages = folio_nr_pages(folio);
Shaohua Li3dd7ae82011-03-22 16:33:45 -07001008
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001009 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
Shaohua Li3dd7ae82011-03-22 16:33:45 -07001010
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001011 /*
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001012 * A folio becomes evictable in two ways:
Peng Fandae966d2019-05-13 17:19:26 -07001013 * 1) Within LRU lock [munlock_vma_page() and __munlock_pagevec()].
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001014 * 2) Before acquiring LRU lock to put the folio on the correct LRU
1015 * and then
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001016 * a) do PageLRU check with lock [check_move_unevictable_pages]
1017 * b) do PageLRU check before lock [clear_page_mlock]
1018 *
1019 * (1) & (2a) are ok as LRU lock will serialize them. For (2b), we need
1020 * following strict ordering:
1021 *
1022 * #0: __pagevec_lru_add_fn #1: clear_page_mlock
1023 *
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001024 * folio_set_lru() folio_test_clear_mlocked()
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001025 * smp_mb() // explicit ordering // above provides strict
1026 * // ordering
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001027 * folio_test_mlocked() folio_test_lru()
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001028 *
1029 *
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001030 * if '#1' does not observe setting of PG_lru by '#0' and
1031 * fails isolation, the explicit barrier will make sure that
1032 * folio_evictable check will put the folio on the correct
1033 * LRU. Without smp_mb(), folio_set_lru() can be reordered
1034 * after folio_test_mlocked() check and can make '#1' fail the
1035 * isolation of the folio whose mlocked bit is cleared (#0 is
1036 * also looking at the same folio) and the evictable folio will
1037 * be stranded on an unevictable LRU.
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001038 */
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001039 folio_set_lru(folio);
Yang Shi9a9b6cc2020-04-01 21:06:23 -07001040 smp_mb__after_atomic();
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001041
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001042 if (folio_evictable(folio)) {
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001043 if (was_unevictable)
Shakeel Butt5d91f312020-06-03 16:03:16 -07001044 __count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages);
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001045 } else {
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001046 folio_clear_active(folio);
1047 folio_set_unevictable(folio);
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001048 if (!was_unevictable)
Shakeel Butt5d91f312020-06-03 16:03:16 -07001049 __count_vm_events(UNEVICTABLE_PGCULLED, nr_pages);
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001050 }
1051
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001052 lruvec_add_folio(lruvec, folio);
1053 trace_mm_lru_insertion(folio);
Shaohua Li3dd7ae82011-03-22 16:33:45 -07001054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 * Add the passed pages to the LRU, then drop the caller's refcount
1058 * on them. Reinitialises the caller's pagevec.
1059 */
Mel Gormana0b8cab32013-07-03 15:02:32 -07001060void __pagevec_lru_add(struct pagevec *pvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Alex Shifc574c22020-12-15 12:34:25 -08001062 int i;
Alex Shi6168d0d2020-12-15 12:34:29 -08001063 struct lruvec *lruvec = NULL;
Alex Shifc574c22020-12-15 12:34:25 -08001064 unsigned long flags = 0;
1065
1066 for (i = 0; i < pagevec_count(pvec); i++) {
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001067 struct folio *folio = page_folio(pvec->pages[i]);
Alex Shifc574c22020-12-15 12:34:25 -08001068
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -04001069 lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags);
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001070 __pagevec_lru_add_fn(folio, lruvec);
Alex Shifc574c22020-12-15 12:34:25 -08001071 }
Alex Shi6168d0d2020-12-15 12:34:29 -08001072 if (lruvec)
1073 unlock_page_lruvec_irqrestore(lruvec, flags);
Alex Shifc574c22020-12-15 12:34:25 -08001074 release_pages(pvec->pages, pvec->nr);
1075 pagevec_reinit(pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001079 * pagevec_remove_exceptionals - pagevec exceptionals pruning
1080 * @pvec: The pagevec to prune
1081 *
Matthew Wilcox (Oracle)a656a202021-02-25 17:16:14 -08001082 * find_get_entries() fills both pages and XArray value entries (aka
1083 * exceptional entries) into the pagevec. This function prunes all
Johannes Weiner0cd61442014-04-03 14:47:46 -07001084 * exceptionals from @pvec without leaving holes, so that it can be
1085 * passed on to page-only pagevec operations.
1086 */
1087void pagevec_remove_exceptionals(struct pagevec *pvec)
1088{
1089 int i, j;
1090
1091 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
1092 struct page *page = pvec->pages[i];
Matthew Wilcox3159f942017-11-03 13:30:42 -04001093 if (!xa_is_value(page))
Johannes Weiner0cd61442014-04-03 14:47:46 -07001094 pvec->pages[j++] = page;
1095 }
1096 pvec->nr = j;
1097}
1098
1099/**
Jan Karab947cee2017-09-06 16:21:21 -07001100 * pagevec_lookup_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 * @pvec: Where the resulting pages are placed
1102 * @mapping: The address_space to search
1103 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001104 * @end: The final page index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 *
Randy Dunlape02a9f02018-01-31 16:21:19 -08001106 * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE
Jan Karab947cee2017-09-06 16:21:21 -07001107 * pages in the mapping starting from index @start and upto index @end
1108 * (inclusive). The pages are placed in @pvec. pagevec_lookup() takes a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 * reference against the pages in @pvec.
1110 *
1111 * The search returns a group of mapping-contiguous pages with ascending
Jan Karad72dc8a2017-09-06 16:21:18 -07001112 * indexes. There may be holes in the indices due to not-present pages. We
1113 * also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 *
Jan Karab947cee2017-09-06 16:21:21 -07001115 * pagevec_lookup_range() returns the number of pages which were found. If this
Randy Dunlape02a9f02018-01-31 16:21:19 -08001116 * number is smaller than PAGEVEC_SIZE, the end of specified range has been
Jan Karab947cee2017-09-06 16:21:21 -07001117 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
Jan Karab947cee2017-09-06 16:21:21 -07001119unsigned pagevec_lookup_range(struct pagevec *pvec,
Jan Kara397162f2017-09-06 16:21:43 -07001120 struct address_space *mapping, pgoff_t *start, pgoff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Jan Kara397162f2017-09-06 16:21:43 -07001122 pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE,
Jan Karab947cee2017-09-06 16:21:21 -07001123 pvec->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return pagevec_count(pvec);
1125}
Jan Karab947cee2017-09-06 16:21:21 -07001126EXPORT_SYMBOL(pagevec_lookup_range);
Christoph Hellwig78539fd2006-01-11 20:47:41 +11001127
Jan Kara72b045a2017-11-15 17:34:33 -08001128unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
1129 struct address_space *mapping, pgoff_t *index, pgoff_t end,
Matthew Wilcox10bbd232017-12-05 17:30:38 -05001130 xa_mark_t tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Jan Kara72b045a2017-11-15 17:34:33 -08001132 pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
Jan Kara67fd7072017-11-15 17:35:19 -08001133 PAGEVEC_SIZE, pvec->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return pagevec_count(pvec);
1135}
Jan Kara72b045a2017-11-15 17:34:33 -08001136EXPORT_SYMBOL(pagevec_lookup_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138/*
1139 * Perform any setup for the swap system
1140 */
1141void __init swap_setup(void)
1142{
Arun KSca79b0c2018-12-28 00:34:29 -08001143 unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /* Use a smaller cluster for small-memory machines */
1146 if (megs < 16)
1147 page_cluster = 2;
1148 else
1149 page_cluster = 3;
1150 /*
1151 * Right now other parts of the system means that we
1152 * _really_ don't want to cluster much more
1153 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
John Hubbard07d80262020-01-30 22:12:28 -08001155
1156#ifdef CONFIG_DEV_PAGEMAP_OPS
1157void put_devmap_managed_page(struct page *page)
1158{
1159 int count;
1160
1161 if (WARN_ON_ONCE(!page_is_devmap_managed(page)))
1162 return;
1163
1164 count = page_ref_dec_return(page);
1165
1166 /*
1167 * devmap page refcounts are 1-based, rather than 0-based: if
1168 * refcount is 1, then the page is free and the refcount is
1169 * stable because nobody holds a reference on the page.
1170 */
1171 if (count == 1)
1172 free_devmap_managed_page(page);
1173 else if (!count)
1174 __put_page(page);
1175}
1176EXPORT_SYMBOL(put_devmap_managed_page);
1177#endif