blob: bcf3ac288b56d560dbfd82ec8c183898e9a3c586 [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);
Matthew Wilcox3cd018b42021-11-19 16:43:15 -0800159 INIT_LIST_HEAD(pages);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700160}
161EXPORT_SYMBOL(put_pages_list);
162
Mel Gorman18022c52012-07-31 16:44:51 -0700163/*
164 * get_kernel_pages() - pin kernel pages in memory
165 * @kiov: An array of struct kvec structures
166 * @nr_segs: number of segments to pin
167 * @write: pinning for read/write, currently ignored
168 * @pages: array that receives pointers to the pages pinned.
169 * Should be at least nr_segs long.
170 *
171 * Returns number of pages pinned. This may be fewer than the number
172 * requested. If nr_pages is 0 or negative, returns 0. If no pages
173 * were pinned, returns -errno. Each page returned must be released
174 * with a put_page() call when it is finished with.
175 */
176int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
177 struct page **pages)
178{
179 int seg;
180
181 for (seg = 0; seg < nr_segs; seg++) {
182 if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
183 return seg;
184
Mel Gorman5a178112012-07-31 16:45:02 -0700185 pages[seg] = kmap_to_page(kiov[seg].iov_base);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300186 get_page(pages[seg]);
Mel Gorman18022c52012-07-31 16:44:51 -0700187 }
188
189 return seg;
190}
191EXPORT_SYMBOL_GPL(get_kernel_pages);
192
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700193static void pagevec_lru_move_fn(struct pagevec *pvec,
Alex Shic7c7b802020-12-15 12:33:56 -0800194 void (*move_fn)(struct page *page, struct lruvec *lruvec))
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700195{
196 int i;
Alex Shi6168d0d2020-12-15 12:34:29 -0800197 struct lruvec *lruvec = NULL;
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700198 unsigned long flags = 0;
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700199
200 for (i = 0; i < pagevec_count(pvec); i++) {
201 struct page *page = pvec->pages[i];
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -0400202 struct folio *folio = page_folio(page);
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700203
Alex Shifc574c22020-12-15 12:34:25 -0800204 /* block memcg migration during page moving between lru */
205 if (!TestClearPageLRU(page))
206 continue;
207
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -0400208 lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags);
Alex Shic7c7b802020-12-15 12:33:56 -0800209 (*move_fn)(page, lruvec);
Alex Shifc574c22020-12-15 12:34:25 -0800210
211 SetPageLRU(page);
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700212 }
Alex Shi6168d0d2020-12-15 12:34:29 -0800213 if (lruvec)
214 unlock_page_lruvec_irqrestore(lruvec, flags);
Mel Gormanc6f92f92017-11-15 17:37:55 -0800215 release_pages(pvec->pages, pvec->nr);
Linus Torvalds83896fb2011-01-17 14:42:34 -0800216 pagevec_reinit(pvec);
Shaohua Lid8505de2011-01-13 15:47:33 -0800217}
218
Alex Shic7c7b802020-12-15 12:33:56 -0800219static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec)
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700220{
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500221 struct folio *folio = page_folio(page);
222
223 if (!folio_test_unevictable(folio)) {
224 lruvec_del_folio(lruvec, folio);
225 folio_clear_active(folio);
226 lruvec_add_folio_tail(lruvec, folio);
227 __count_vm_events(PGROTATED, folio_nr_pages(folio));
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700228 }
229}
230
Minchan Kimd479960e2021-05-04 18:36:54 -0700231/* return true if pagevec needs to drain */
232static bool pagevec_add_and_need_flush(struct pagevec *pvec, struct page *page)
233{
234 bool ret = false;
235
236 if (!pagevec_add(pvec, page) || PageCompound(page) ||
237 lru_cache_disabled())
238 ret = true;
239
240 return ret;
241}
242
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700243/*
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500244 * Writeback is about to end against a folio which has been marked for
245 * immediate reclaim. If it still appears to be reclaimable, move it
246 * to the tail of the inactive list.
Alex Shic7c7b802020-12-15 12:33:56 -0800247 *
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500248 * folio_rotate_reclaimable() must disable IRQs, to prevent nasty races.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500250void folio_rotate_reclaimable(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500252 if (!folio_test_locked(folio) && !folio_test_dirty(folio) &&
253 !folio_test_unevictable(folio) && folio_test_lru(folio)) {
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700254 struct pagevec *pvec;
255 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500257 folio_get(folio);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200258 local_lock_irqsave(&lru_rotate.lock, flags);
259 pvec = this_cpu_ptr(&lru_rotate.pvec);
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -0500260 if (pagevec_add_and_need_flush(pvec, &folio->page))
Alex Shic7c7b802020-12-15 12:33:56 -0800261 pagevec_lru_move_fn(pvec, pagevec_move_tail_fn);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200262 local_unlock_irqrestore(&lru_rotate.lock, flags);
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Johannes Weiner96f8bf42020-06-03 16:03:09 -0700266void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages)
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800267{
Johannes Weiner7cf111b2020-06-03 16:03:06 -0700268 do {
269 unsigned long lrusize;
270
Alex Shi6168d0d2020-12-15 12:34:29 -0800271 /*
272 * Hold lruvec->lru_lock is safe here, since
273 * 1) The pinned lruvec in reclaim, or
274 * 2) From a pre-LRU page during refault (which also holds the
275 * rcu lock, so would be safe even if the page was on the LRU
276 * and could move simultaneously to a new lruvec).
277 */
278 spin_lock_irq(&lruvec->lru_lock);
Johannes Weiner7cf111b2020-06-03 16:03:06 -0700279 /* Record cost event */
Johannes Weiner96f8bf42020-06-03 16:03:09 -0700280 if (file)
281 lruvec->file_cost += nr_pages;
Johannes Weiner7cf111b2020-06-03 16:03:06 -0700282 else
Johannes Weiner96f8bf42020-06-03 16:03:09 -0700283 lruvec->anon_cost += nr_pages;
Johannes Weiner7cf111b2020-06-03 16:03:06 -0700284
285 /*
286 * Decay previous events
287 *
288 * Because workloads change over time (and to avoid
289 * overflow) we keep these statistics as a floating
290 * average, which ends up weighing recent refaults
291 * more than old ones.
292 */
293 lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) +
294 lruvec_page_state(lruvec, NR_ACTIVE_ANON) +
295 lruvec_page_state(lruvec, NR_INACTIVE_FILE) +
296 lruvec_page_state(lruvec, NR_ACTIVE_FILE);
297
298 if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) {
299 lruvec->file_cost /= 2;
300 lruvec->anon_cost /= 2;
301 }
Alex Shi6168d0d2020-12-15 12:34:29 -0800302 spin_unlock_irq(&lruvec->lru_lock);
Johannes Weiner7cf111b2020-06-03 16:03:06 -0700303 } while ((lruvec = parent_lruvec(lruvec)));
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800304}
305
Matthew Wilcox (Oracle)0995d7e2021-04-29 10:27:16 -0400306void lru_note_cost_folio(struct folio *folio)
Johannes Weiner96f8bf42020-06-03 16:03:09 -0700307{
Matthew Wilcox (Oracle)0995d7e2021-04-29 10:27:16 -0400308 lru_note_cost(folio_lruvec(folio), folio_is_file_lru(folio),
309 folio_nr_pages(folio));
Johannes Weiner96f8bf42020-06-03 16:03:09 -0700310}
311
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400312static void __folio_activate(struct folio *folio, struct lruvec *lruvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400314 if (!folio_test_active(folio) && !folio_test_unevictable(folio)) {
315 long nr_pages = folio_nr_pages(folio);
Linus Torvalds7a608572011-01-17 14:42:19 -0800316
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400317 lruvec_del_folio(lruvec, folio);
318 folio_set_active(folio);
319 lruvec_add_folio(lruvec, folio);
320 trace_mm_lru_activate(folio);
Linus Torvalds7a608572011-01-17 14:42:19 -0800321
Shakeel Butt21e330f2020-06-03 16:03:19 -0700322 __count_vm_events(PGACTIVATE, nr_pages);
323 __count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE,
324 nr_pages);
Linus Torvalds7a608572011-01-17 14:42:19 -0800325 }
Shaohua Lieb709b02011-05-24 17:12:55 -0700326}
327
328#ifdef CONFIG_SMP
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400329static void __activate_page(struct page *page, struct lruvec *lruvec)
330{
331 return __folio_activate(page_folio(page), lruvec);
332}
333
Shaohua Lieb709b02011-05-24 17:12:55 -0700334static void activate_page_drain(int cpu)
335{
Ingo Molnarb01b2142020-05-27 22:11:15 +0200336 struct pagevec *pvec = &per_cpu(lru_pvecs.activate_page, cpu);
Shaohua Lieb709b02011-05-24 17:12:55 -0700337
338 if (pagevec_count(pvec))
Alex Shic7c7b802020-12-15 12:33:56 -0800339 pagevec_lru_move_fn(pvec, __activate_page);
Shaohua Lieb709b02011-05-24 17:12:55 -0700340}
341
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700342static bool need_activate_page_drain(int cpu)
343{
Ingo Molnarb01b2142020-05-27 22:11:15 +0200344 return pagevec_count(&per_cpu(lru_pvecs.activate_page, cpu)) != 0;
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700345}
346
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400347static void folio_activate(struct folio *folio)
Shaohua Lieb709b02011-05-24 17:12:55 -0700348{
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400349 if (folio_test_lru(folio) && !folio_test_active(folio) &&
350 !folio_test_unevictable(folio)) {
Ingo Molnarb01b2142020-05-27 22:11:15 +0200351 struct pagevec *pvec;
Shaohua Lieb709b02011-05-24 17:12:55 -0700352
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400353 folio_get(folio);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200354 local_lock(&lru_pvecs.lock);
355 pvec = this_cpu_ptr(&lru_pvecs.activate_page);
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400356 if (pagevec_add_and_need_flush(pvec, &folio->page))
Alex Shic7c7b802020-12-15 12:33:56 -0800357 pagevec_lru_move_fn(pvec, __activate_page);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200358 local_unlock(&lru_pvecs.lock);
Shaohua Lieb709b02011-05-24 17:12:55 -0700359 }
360}
361
362#else
363static inline void activate_page_drain(int cpu)
364{
365}
366
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400367static void folio_activate(struct folio *folio)
Shaohua Lieb709b02011-05-24 17:12:55 -0700368{
Alex Shi6168d0d2020-12-15 12:34:29 -0800369 struct lruvec *lruvec;
Shaohua Lieb709b02011-05-24 17:12:55 -0700370
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400371 if (folio_test_clear_lru(folio)) {
Matthew Wilcox (Oracle)e809c3f2021-06-28 21:59:47 -0400372 lruvec = folio_lruvec_lock_irq(folio);
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400373 __folio_activate(folio, lruvec);
Alex Shi6168d0d2020-12-15 12:34:29 -0800374 unlock_page_lruvec_irq(lruvec);
Matthew Wilcox (Oracle)f2d27392021-04-27 10:37:50 -0400375 folio_set_lru(folio);
Alex Shi6168d0d2020-12-15 12:34:29 -0800376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
Shaohua Lieb709b02011-05-24 17:12:55 -0700378#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400380static void __lru_cache_activate_folio(struct folio *folio)
Mel Gorman059285a2013-07-03 15:02:30 -0700381{
Ingo Molnarb01b2142020-05-27 22:11:15 +0200382 struct pagevec *pvec;
Mel Gorman059285a2013-07-03 15:02:30 -0700383 int i;
384
Ingo Molnarb01b2142020-05-27 22:11:15 +0200385 local_lock(&lru_pvecs.lock);
386 pvec = this_cpu_ptr(&lru_pvecs.lru_add);
387
Mel Gorman059285a2013-07-03 15:02:30 -0700388 /*
389 * Search backwards on the optimistic assumption that the page being
390 * activated has just been added to this pagevec. Note that only
391 * the local pagevec is examined as a !PageLRU page could be in the
392 * process of being released, reclaimed, migrated or on a remote
393 * pagevec that is currently being drained. Furthermore, marking
394 * a remote pagevec's page PageActive potentially hits a race where
395 * a page is marked PageActive just after it is added to the inactive
396 * list causing accounting errors and BUG_ON checks to trigger.
397 */
398 for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
399 struct page *pagevec_page = pvec->pages[i];
400
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400401 if (pagevec_page == &folio->page) {
402 folio_set_active(folio);
Mel Gorman059285a2013-07-03 15:02:30 -0700403 break;
404 }
405 }
406
Ingo Molnarb01b2142020-05-27 22:11:15 +0200407 local_unlock(&lru_pvecs.lock);
Mel Gorman059285a2013-07-03 15:02:30 -0700408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/*
411 * Mark a page as having seen activity.
412 *
413 * inactive,unreferenced -> inactive,referenced
414 * inactive,referenced -> active,unreferenced
415 * active,unreferenced -> active,referenced
Hugh Dickinseb39d612014-08-06 16:06:43 -0700416 *
417 * When a newly allocated page is not yet visible, so safe for non-atomic ops,
418 * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400420void folio_mark_accessed(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400422 if (!folio_test_referenced(folio)) {
423 folio_set_referenced(folio);
424 } else if (folio_test_unevictable(folio)) {
Fengguang Wua1100a72019-11-30 17:50:00 -0800425 /*
426 * Unevictable pages are on the "LRU_UNEVICTABLE" list. But,
427 * this list is never rotated or maintained, so marking an
428 * evictable page accessed has no effect.
429 */
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400430 } else if (!folio_test_active(folio)) {
Mel Gorman059285a2013-07-03 15:02:30 -0700431 /*
432 * If the page is on the LRU, queue it for activation via
Ingo Molnarb01b2142020-05-27 22:11:15 +0200433 * lru_pvecs.activate_page. Otherwise, assume the page is on a
Mel Gorman059285a2013-07-03 15:02:30 -0700434 * pagevec, mark it active and it'll be moved to the active
435 * LRU on the next drain.
436 */
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400437 if (folio_test_lru(folio))
438 folio_activate(folio);
Mel Gorman059285a2013-07-03 15:02:30 -0700439 else
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400440 __lru_cache_activate_folio(folio);
441 folio_clear_referenced(folio);
442 workingset_activation(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400444 if (folio_test_idle(folio))
445 folio_clear_idle(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
Matthew Wilcox (Oracle)76580b62021-04-27 10:47:39 -0400447EXPORT_SYMBOL(folio_mark_accessed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
KOSAKI Motohirof04e9eb2008-10-18 20:26:19 -0700449/**
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400450 * folio_add_lru - Add a folio to an LRU list.
451 * @folio: The folio to be added to the LRU.
Jianyu Zhan2329d372014-06-04 16:07:31 -0700452 *
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400453 * Queue the folio for addition to the LRU. The decision on whether
Jianyu Zhan2329d372014-06-04 16:07:31 -0700454 * to add the page to the [in]active [file|anon] list is deferred until the
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400455 * pagevec is drained. This gives a chance for the caller of folio_add_lru()
456 * have the folio added to the active list using folio_mark_accessed().
KOSAKI Motohirof04e9eb2008-10-18 20:26:19 -0700457 */
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400458void folio_add_lru(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Johannes Weiner6058eae2020-06-03 16:02:40 -0700460 struct pagevec *pvec;
461
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400462 VM_BUG_ON_FOLIO(folio_test_active(folio) && folio_test_unevictable(folio), folio);
463 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
Johannes Weiner6058eae2020-06-03 16:02:40 -0700464
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400465 folio_get(folio);
Johannes Weiner6058eae2020-06-03 16:02:40 -0700466 local_lock(&lru_pvecs.lock);
467 pvec = this_cpu_ptr(&lru_pvecs.lru_add);
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400468 if (pagevec_add_and_need_flush(pvec, &folio->page))
Johannes Weiner6058eae2020-06-03 16:02:40 -0700469 __pagevec_lru_add(pvec);
470 local_unlock(&lru_pvecs.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
Matthew Wilcox (Oracle)0d311252021-04-29 11:09:31 -0400472EXPORT_SYMBOL(folio_add_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700474/**
Joonsoo Kimb5181542020-08-11 18:30:40 -0700475 * lru_cache_add_inactive_or_unevictable
Johannes Weiner00501b52014-08-08 14:19:20 -0700476 * @page: the page to be added to LRU
477 * @vma: vma in which page is mapped for determining reclaimability
478 *
Joonsoo Kimb5181542020-08-11 18:30:40 -0700479 * Place @page on the inactive or unevictable LRU list, depending on its
Miaohe Lin12eab422020-10-13 16:52:24 -0700480 * evictability.
Johannes Weiner00501b52014-08-08 14:19:20 -0700481 */
Joonsoo Kimb5181542020-08-11 18:30:40 -0700482void lru_cache_add_inactive_or_unevictable(struct page *page,
Johannes Weiner00501b52014-08-08 14:19:20 -0700483 struct vm_area_struct *vma)
484{
Joonsoo Kimb5181542020-08-11 18:30:40 -0700485 bool unevictable;
486
Johannes Weiner00501b52014-08-08 14:19:20 -0700487 VM_BUG_ON_PAGE(PageLRU(page), page);
488
Joonsoo Kimb5181542020-08-11 18:30:40 -0700489 unevictable = (vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED;
490 if (unlikely(unevictable) && !TestSetPageMlocked(page)) {
Hugh Dickins09647302020-09-18 21:20:15 -0700491 int nr_pages = thp_nr_pages(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700492 /*
Shijie Luocb152a12021-05-06 18:05:51 -0700493 * We use the irq-unsafe __mod_zone_page_state because this
Johannes Weiner00501b52014-08-08 14:19:20 -0700494 * counter is not modified from interrupt context, and the pte
495 * lock is held(spinlock), which implies preemption disabled.
496 */
Hugh Dickins09647302020-09-18 21:20:15 -0700497 __mod_zone_page_state(page_zone(page), NR_MLOCK, nr_pages);
498 count_vm_events(UNEVICTABLE_PGMLOCKED, nr_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -0700499 }
Shakeel Butt9c4e6b12018-02-21 14:45:28 -0800500 lru_cache_add(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700501}
502
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700503/*
Minchan Kim31560182011-03-22 16:32:52 -0700504 * If the page can not be invalidated, it is moved to the
505 * inactive list to speed up its reclaim. It is moved to the
506 * head of the list, rather than the tail, to give the flusher
507 * threads some time to write it out, as this is much more
508 * effective than the single-page writeout from reclaim.
Minchan Kim278df9f2011-03-22 16:32:54 -0700509 *
510 * If the page isn't page_mapped and dirty/writeback, the page
511 * could reclaim asap using PG_reclaim.
512 *
513 * 1. active, mapped page -> none
514 * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
515 * 3. inactive, mapped page -> none
516 * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
517 * 5. inactive, clean -> inactive, tail
518 * 6. Others -> none
519 *
520 * In 4, why it moves inactive's head, the VM expects the page would
521 * be write it out by flusher threads as this is much more effective
522 * than the single-page writeout from reclaim.
Minchan Kim31560182011-03-22 16:32:52 -0700523 */
Alex Shic7c7b802020-12-15 12:33:56 -0800524static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec)
Minchan Kim31560182011-03-22 16:32:52 -0700525{
Yu Zhao46ae6b22021-02-24 12:08:25 -0800526 bool active = PageActive(page);
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700527 int nr_pages = thp_nr_pages(page);
Minchan Kim31560182011-03-22 16:32:52 -0700528
Minchan Kimbad49d92011-05-11 15:13:30 -0700529 if (PageUnevictable(page))
530 return;
531
Minchan Kim31560182011-03-22 16:32:52 -0700532 /* Some processes are using the page */
533 if (page_mapped(page))
534 return;
535
Yu Zhao46ae6b22021-02-24 12:08:25 -0800536 del_page_from_lru_list(page, lruvec);
Minchan Kim31560182011-03-22 16:32:52 -0700537 ClearPageActive(page);
538 ClearPageReferenced(page);
Minchan Kim31560182011-03-22 16:32:52 -0700539
Minchan Kim278df9f2011-03-22 16:32:54 -0700540 if (PageWriteback(page) || PageDirty(page)) {
541 /*
542 * PG_reclaim could be raced with end_page_writeback
543 * It can make readahead confusing. But race window
544 * is _really_ small and it's non-critical problem.
545 */
Yu Zhao3a9c9782021-02-24 12:08:17 -0800546 add_page_to_lru_list(page, lruvec);
Minchan Kim278df9f2011-03-22 16:32:54 -0700547 SetPageReclaim(page);
548 } else {
549 /*
550 * The page's writeback ends up during pagevec
Hyeonggon Yooc4ffefd2021-06-30 18:53:10 -0700551 * We move that page into tail of inactive.
Minchan Kim278df9f2011-03-22 16:32:54 -0700552 */
Yu Zhao3a9c9782021-02-24 12:08:17 -0800553 add_page_to_lru_list_tail(page, lruvec);
Shakeel Butt5d91f312020-06-03 16:03:16 -0700554 __count_vm_events(PGROTATED, nr_pages);
Minchan Kim278df9f2011-03-22 16:32:54 -0700555 }
556
Shakeel Butt21e330f2020-06-03 16:03:19 -0700557 if (active) {
Shakeel Butt5d91f312020-06-03 16:03:16 -0700558 __count_vm_events(PGDEACTIVATE, nr_pages);
Shakeel Butt21e330f2020-06-03 16:03:19 -0700559 __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
560 nr_pages);
561 }
Minchan Kim31560182011-03-22 16:32:52 -0700562}
563
Alex Shic7c7b802020-12-15 12:33:56 -0800564static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec)
Minchan Kim9c276cc2019-09-25 16:49:08 -0700565{
Alex Shifc574c22020-12-15 12:34:25 -0800566 if (PageActive(page) && !PageUnevictable(page)) {
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700567 int nr_pages = thp_nr_pages(page);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700568
Yu Zhao46ae6b22021-02-24 12:08:25 -0800569 del_page_from_lru_list(page, lruvec);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700570 ClearPageActive(page);
571 ClearPageReferenced(page);
Yu Zhao3a9c9782021-02-24 12:08:17 -0800572 add_page_to_lru_list(page, lruvec);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700573
Shakeel Butt21e330f2020-06-03 16:03:19 -0700574 __count_vm_events(PGDEACTIVATE, nr_pages);
575 __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
576 nr_pages);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700577 }
578}
Minchan Kim10853a02016-01-15 16:55:11 -0800579
Alex Shic7c7b802020-12-15 12:33:56 -0800580static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec)
Minchan Kim10853a02016-01-15 16:55:11 -0800581{
Alex Shifc574c22020-12-15 12:34:25 -0800582 if (PageAnon(page) && PageSwapBacked(page) &&
Shaohua Li24c92eb2017-10-03 16:15:29 -0700583 !PageSwapCache(page) && !PageUnevictable(page)) {
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700584 int nr_pages = thp_nr_pages(page);
Minchan Kim10853a02016-01-15 16:55:11 -0800585
Yu Zhao46ae6b22021-02-24 12:08:25 -0800586 del_page_from_lru_list(page, lruvec);
Minchan Kim10853a02016-01-15 16:55:11 -0800587 ClearPageActive(page);
588 ClearPageReferenced(page);
Shaohua Lif7ad2a62017-05-03 14:52:29 -0700589 /*
Huang Ying9de4f222020-04-06 20:04:41 -0700590 * Lazyfree pages are clean anonymous pages. They have
591 * PG_swapbacked flag cleared, to distinguish them from normal
592 * anonymous pages
Shaohua Lif7ad2a62017-05-03 14:52:29 -0700593 */
594 ClearPageSwapBacked(page);
Yu Zhao3a9c9782021-02-24 12:08:17 -0800595 add_page_to_lru_list(page, lruvec);
Minchan Kim10853a02016-01-15 16:55:11 -0800596
Shakeel Butt21e330f2020-06-03 16:03:19 -0700597 __count_vm_events(PGLAZYFREE, nr_pages);
598 __count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE,
599 nr_pages);
Minchan Kim10853a02016-01-15 16:55:11 -0800600 }
601}
602
Minchan Kim31560182011-03-22 16:32:52 -0700603/*
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700604 * Drain pages out of the cpu's pagevecs.
605 * Either "cpu" is the current CPU, and preemption has already been
606 * disabled; or "cpu" is being hot-unplugged, and is already dead.
607 */
Konstantin Khlebnikovf0cb3c72012-03-21 16:34:06 -0700608void lru_add_drain_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Ingo Molnarb01b2142020-05-27 22:11:15 +0200610 struct pagevec *pvec = &per_cpu(lru_pvecs.lru_add, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Mel Gorman13f7f782013-07-03 15:02:28 -0700612 if (pagevec_count(pvec))
Mel Gormana0b8cab32013-07-03 15:02:32 -0700613 __pagevec_lru_add(pvec);
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700614
Ingo Molnarb01b2142020-05-27 22:11:15 +0200615 pvec = &per_cpu(lru_rotate.pvec, cpu);
Qian Cai7e0cc012020-08-14 17:31:50 -0700616 /* Disabling interrupts below acts as a compiler barrier. */
617 if (data_race(pagevec_count(pvec))) {
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700618 unsigned long flags;
619
620 /* No harm done if a racing interrupt already did this */
Ingo Molnarb01b2142020-05-27 22:11:15 +0200621 local_lock_irqsave(&lru_rotate.lock, flags);
Alex Shic7c7b802020-12-15 12:33:56 -0800622 pagevec_lru_move_fn(pvec, pagevec_move_tail_fn);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200623 local_unlock_irqrestore(&lru_rotate.lock, flags);
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700624 }
Minchan Kim31560182011-03-22 16:32:52 -0700625
Ingo Molnarb01b2142020-05-27 22:11:15 +0200626 pvec = &per_cpu(lru_pvecs.lru_deactivate_file, cpu);
Minchan Kim31560182011-03-22 16:32:52 -0700627 if (pagevec_count(pvec))
Alex Shic7c7b802020-12-15 12:33:56 -0800628 pagevec_lru_move_fn(pvec, lru_deactivate_file_fn);
Shaohua Lieb709b02011-05-24 17:12:55 -0700629
Ingo Molnarb01b2142020-05-27 22:11:15 +0200630 pvec = &per_cpu(lru_pvecs.lru_deactivate, cpu);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700631 if (pagevec_count(pvec))
Alex Shic7c7b802020-12-15 12:33:56 -0800632 pagevec_lru_move_fn(pvec, lru_deactivate_fn);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700633
Ingo Molnarb01b2142020-05-27 22:11:15 +0200634 pvec = &per_cpu(lru_pvecs.lru_lazyfree, cpu);
Minchan Kim10853a02016-01-15 16:55:11 -0800635 if (pagevec_count(pvec))
Alex Shic7c7b802020-12-15 12:33:56 -0800636 pagevec_lru_move_fn(pvec, lru_lazyfree_fn);
Minchan Kim10853a02016-01-15 16:55:11 -0800637
Shaohua Lieb709b02011-05-24 17:12:55 -0700638 activate_page_drain(cpu);
Minchan Kim31560182011-03-22 16:32:52 -0700639}
640
641/**
Minchan Kimcc5993b2015-04-15 16:13:26 -0700642 * deactivate_file_page - forcefully deactivate a file page
Minchan Kim31560182011-03-22 16:32:52 -0700643 * @page: page to deactivate
644 *
645 * This function hints the VM that @page is a good reclaim candidate,
646 * for example if its invalidation fails due to the page being dirty
647 * or under writeback.
648 */
Minchan Kimcc5993b2015-04-15 16:13:26 -0700649void deactivate_file_page(struct page *page)
Minchan Kim31560182011-03-22 16:32:52 -0700650{
Minchan Kim821ed6b2011-05-24 17:12:31 -0700651 /*
Minchan Kimcc5993b2015-04-15 16:13:26 -0700652 * In a workload with many unevictable page such as mprotect,
653 * unevictable page deactivation for accelerating reclaim is pointless.
Minchan Kim821ed6b2011-05-24 17:12:31 -0700654 */
655 if (PageUnevictable(page))
656 return;
657
Minchan Kim31560182011-03-22 16:32:52 -0700658 if (likely(get_page_unless_zero(page))) {
Ingo Molnarb01b2142020-05-27 22:11:15 +0200659 struct pagevec *pvec;
660
661 local_lock(&lru_pvecs.lock);
662 pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate_file);
Minchan Kim31560182011-03-22 16:32:52 -0700663
Minchan Kimd479960e2021-05-04 18:36:54 -0700664 if (pagevec_add_and_need_flush(pvec, page))
Alex Shic7c7b802020-12-15 12:33:56 -0800665 pagevec_lru_move_fn(pvec, lru_deactivate_file_fn);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200666 local_unlock(&lru_pvecs.lock);
Minchan Kim31560182011-03-22 16:32:52 -0700667 }
Andrew Morton80bfed92006-01-06 00:11:14 -0800668}
669
Minchan Kim9c276cc2019-09-25 16:49:08 -0700670/*
671 * deactivate_page - deactivate a page
672 * @page: page to deactivate
673 *
674 * deactivate_page() moves @page to the inactive list if @page was on the active
675 * list and was not an unevictable page. This is done to accelerate the reclaim
676 * of @page.
677 */
678void deactivate_page(struct page *page)
679{
680 if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
Ingo Molnarb01b2142020-05-27 22:11:15 +0200681 struct pagevec *pvec;
Minchan Kim9c276cc2019-09-25 16:49:08 -0700682
Ingo Molnarb01b2142020-05-27 22:11:15 +0200683 local_lock(&lru_pvecs.lock);
684 pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700685 get_page(page);
Minchan Kimd479960e2021-05-04 18:36:54 -0700686 if (pagevec_add_and_need_flush(pvec, page))
Alex Shic7c7b802020-12-15 12:33:56 -0800687 pagevec_lru_move_fn(pvec, lru_deactivate_fn);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200688 local_unlock(&lru_pvecs.lock);
Minchan Kim9c276cc2019-09-25 16:49:08 -0700689 }
690}
691
Minchan Kim10853a02016-01-15 16:55:11 -0800692/**
Shaohua Lif7ad2a62017-05-03 14:52:29 -0700693 * mark_page_lazyfree - make an anon page lazyfree
Minchan Kim10853a02016-01-15 16:55:11 -0800694 * @page: page to deactivate
695 *
Shaohua Lif7ad2a62017-05-03 14:52:29 -0700696 * mark_page_lazyfree() moves @page to the inactive file list.
697 * This is done to accelerate the reclaim of @page.
Minchan Kim10853a02016-01-15 16:55:11 -0800698 */
Shaohua Lif7ad2a62017-05-03 14:52:29 -0700699void mark_page_lazyfree(struct page *page)
Minchan Kim10853a02016-01-15 16:55:11 -0800700{
Shaohua Lif7ad2a62017-05-03 14:52:29 -0700701 if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
Shaohua Li24c92eb2017-10-03 16:15:29 -0700702 !PageSwapCache(page) && !PageUnevictable(page)) {
Ingo Molnarb01b2142020-05-27 22:11:15 +0200703 struct pagevec *pvec;
Minchan Kim10853a02016-01-15 16:55:11 -0800704
Ingo Molnarb01b2142020-05-27 22:11:15 +0200705 local_lock(&lru_pvecs.lock);
706 pvec = this_cpu_ptr(&lru_pvecs.lru_lazyfree);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300707 get_page(page);
Minchan Kimd479960e2021-05-04 18:36:54 -0700708 if (pagevec_add_and_need_flush(pvec, page))
Alex Shic7c7b802020-12-15 12:33:56 -0800709 pagevec_lru_move_fn(pvec, lru_lazyfree_fn);
Ingo Molnarb01b2142020-05-27 22:11:15 +0200710 local_unlock(&lru_pvecs.lock);
Minchan Kim10853a02016-01-15 16:55:11 -0800711 }
712}
713
Andrew Morton80bfed92006-01-06 00:11:14 -0800714void lru_add_drain(void)
715{
Ingo Molnarb01b2142020-05-27 22:11:15 +0200716 local_lock(&lru_pvecs.lock);
717 lru_add_drain_cpu(smp_processor_id());
718 local_unlock(&lru_pvecs.lock);
719}
720
Minchan Kim243418e2021-09-24 15:43:47 -0700721/*
722 * It's called from per-cpu workqueue context in SMP case so
723 * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on
724 * the same cpu. It shouldn't be a problem in !SMP case since
725 * the core is only one and the locks will disable preemption.
726 */
727static void lru_add_and_bh_lrus_drain(void)
728{
729 local_lock(&lru_pvecs.lock);
730 lru_add_drain_cpu(smp_processor_id());
731 local_unlock(&lru_pvecs.lock);
732 invalidate_bh_lrus_cpu();
733}
734
Ingo Molnarb01b2142020-05-27 22:11:15 +0200735void lru_add_drain_cpu_zone(struct zone *zone)
736{
737 local_lock(&lru_pvecs.lock);
738 lru_add_drain_cpu(smp_processor_id());
739 drain_local_pages(zone);
740 local_unlock(&lru_pvecs.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Michal Hocko6ea183d2019-02-20 22:19:54 -0800743#ifdef CONFIG_SMP
744
745static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
746
David Howellsc4028952006-11-22 14:57:56 +0000747static void lru_add_drain_per_cpu(struct work_struct *dummy)
Nick Piggin053837f2006-01-18 17:42:27 -0800748{
Minchan Kim243418e2021-09-24 15:43:47 -0700749 lru_add_and_bh_lrus_drain();
Nick Piggin053837f2006-01-18 17:42:27 -0800750}
751
Michal Hocko9852a722018-01-31 16:16:19 -0800752/*
753 * Doesn't need any cpu hotplug locking because we do rely on per-cpu
754 * kworkers being shut down before our page_alloc_cpu_dead callback is
755 * executed on the offlined cpu.
756 * Calling this function with cpu hotplug locks held can actually lead
757 * to obscure indirect dependencies via WQ context.
758 */
Minchan Kimd479960e2021-05-04 18:36:54 -0700759inline void __lru_add_drain_all(bool force_all_cpus)
Nick Piggin053837f2006-01-18 17:42:27 -0800760{
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200761 /*
762 * lru_drain_gen - Global pages generation number
763 *
764 * (A) Definition: global lru_drain_gen = x implies that all generations
765 * 0 < n <= x are already *scheduled* for draining.
766 *
767 * This is an optimization for the highly-contended use case where a
768 * user space workload keeps constantly generating a flow of pages for
769 * each CPU.
770 */
771 static unsigned int lru_drain_gen;
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700772 static struct cpumask has_work;
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200773 static DEFINE_MUTEX(lock);
774 unsigned cpu, this_gen;
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700775
Michal Hockoce612872017-04-07 16:05:05 -0700776 /*
777 * Make sure nobody triggers this path before mm_percpu_wq is fully
778 * initialized.
779 */
780 if (WARN_ON(!mm_percpu_wq))
781 return;
782
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200783 /*
784 * Guarantee pagevec counter stores visible by this CPU are visible to
785 * other CPUs before loading the current drain generation.
786 */
787 smp_mb();
788
789 /*
790 * (B) Locally cache global LRU draining generation number
791 *
792 * The read barrier ensures that the counter is loaded before the mutex
793 * is taken. It pairs with smp_mb() inside the mutex critical section
794 * at (D).
795 */
796 this_gen = smp_load_acquire(&lru_drain_gen);
Konstantin Khlebnikoveef1a422019-11-30 17:50:40 -0800797
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700798 mutex_lock(&lock);
Konstantin Khlebnikoveef1a422019-11-30 17:50:40 -0800799
800 /*
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200801 * (C) Exit the draining operation if a newer generation, from another
802 * lru_add_drain_all(), was already scheduled for draining. Check (A).
Konstantin Khlebnikoveef1a422019-11-30 17:50:40 -0800803 */
Minchan Kimd479960e2021-05-04 18:36:54 -0700804 if (unlikely(this_gen != lru_drain_gen && !force_all_cpus))
Konstantin Khlebnikoveef1a422019-11-30 17:50:40 -0800805 goto done;
806
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200807 /*
808 * (D) Increment global generation number
809 *
810 * Pairs with smp_load_acquire() at (B), outside of the critical
811 * section. Use a full memory barrier to guarantee that the new global
812 * drain generation number is stored before loading pagevec counters.
813 *
814 * This pairing must be done here, before the for_each_online_cpu loop
815 * below which drains the page vectors.
816 *
817 * Let x, y, and z represent some system CPU numbers, where x < y < z.
Shijie Luocb152a12021-05-06 18:05:51 -0700818 * Assume CPU #z is in the middle of the for_each_online_cpu loop
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200819 * below and has already reached CPU #y's per-cpu data. CPU #x comes
820 * along, adds some pages to its per-cpu vectors, then calls
821 * lru_add_drain_all().
822 *
823 * If the paired barrier is done at any later step, e.g. after the
824 * loop, CPU #x will just exit at (C) and miss flushing out all of its
825 * added pages.
826 */
827 WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1);
828 smp_mb();
Konstantin Khlebnikoveef1a422019-11-30 17:50:40 -0800829
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700830 cpumask_clear(&has_work);
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700831 for_each_online_cpu(cpu) {
832 struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
833
Minchan Kimd479960e2021-05-04 18:36:54 -0700834 if (force_all_cpus ||
835 pagevec_count(&per_cpu(lru_pvecs.lru_add, cpu)) ||
Qian Cai7e0cc012020-08-14 17:31:50 -0700836 data_race(pagevec_count(&per_cpu(lru_rotate.pvec, cpu))) ||
Ingo Molnarb01b2142020-05-27 22:11:15 +0200837 pagevec_count(&per_cpu(lru_pvecs.lru_deactivate_file, cpu)) ||
838 pagevec_count(&per_cpu(lru_pvecs.lru_deactivate, cpu)) ||
839 pagevec_count(&per_cpu(lru_pvecs.lru_lazyfree, cpu)) ||
Minchan Kim8cc621d2021-05-04 18:37:00 -0700840 need_activate_page_drain(cpu) ||
841 has_bh_in_lru(cpu, NULL)) {
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700842 INIT_WORK(work, lru_add_drain_per_cpu);
Michal Hockoce612872017-04-07 16:05:05 -0700843 queue_work_on(cpu, mm_percpu_wq, work);
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200844 __cpumask_set_cpu(cpu, &has_work);
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700845 }
846 }
847
848 for_each_cpu(cpu, &has_work)
849 flush_work(&per_cpu(lru_add_drain_work, cpu));
850
Konstantin Khlebnikoveef1a422019-11-30 17:50:40 -0800851done:
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700852 mutex_unlock(&lock);
Nick Piggin053837f2006-01-18 17:42:27 -0800853}
Minchan Kimd479960e2021-05-04 18:36:54 -0700854
855void lru_add_drain_all(void)
856{
857 __lru_add_drain_all(false);
858}
Michal Hocko6ea183d2019-02-20 22:19:54 -0800859#else
860void lru_add_drain_all(void)
861{
862 lru_add_drain();
863}
Ahmed S. Darwish6446a512020-08-27 13:40:38 +0200864#endif /* CONFIG_SMP */
Nick Piggin053837f2006-01-18 17:42:27 -0800865
Minchan Kimd479960e2021-05-04 18:36:54 -0700866atomic_t lru_disable_count = ATOMIC_INIT(0);
867
868/*
869 * lru_cache_disable() needs to be called before we start compiling
870 * a list of pages to be migrated using isolate_lru_page().
871 * It drains pages on LRU cache and then disable on all cpus until
872 * lru_cache_enable is called.
873 *
874 * Must be paired with a call to lru_cache_enable().
875 */
876void lru_cache_disable(void)
877{
878 atomic_inc(&lru_disable_count);
879#ifdef CONFIG_SMP
880 /*
881 * lru_add_drain_all in the force mode will schedule draining on
882 * all online CPUs so any calls of lru_cache_disabled wrapped by
883 * local_lock or preemption disabled would be ordered by that.
884 * The atomic operation doesn't need to have stronger ordering
Quanfa Fu0b8f0d82022-01-14 14:09:25 -0800885 * requirements because that is enforced by the scheduling
Minchan Kimd479960e2021-05-04 18:36:54 -0700886 * guarantees.
887 */
888 __lru_add_drain_all(true);
889#else
Minchan Kim243418e2021-09-24 15:43:47 -0700890 lru_add_and_bh_lrus_drain();
Minchan Kimd479960e2021-05-04 18:36:54 -0700891#endif
892}
893
Michal Hockoaabfb572014-10-09 15:28:52 -0700894/**
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300895 * release_pages - batched put_page()
Michal Hockoaabfb572014-10-09 15:28:52 -0700896 * @pages: array of pages to release
897 * @nr: number of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 *
Michal Hockoaabfb572014-10-09 15:28:52 -0700899 * Decrement the reference count on all the pages in @pages. If it
900 * fell to zero, remove the page from the LRU and free it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
Mel Gormanc6f92f92017-11-15 17:37:55 -0800902void release_pages(struct page **pages, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 int i;
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -0800905 LIST_HEAD(pages_to_free);
Alex Shi6168d0d2020-12-15 12:34:29 -0800906 struct lruvec *lruvec = NULL;
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -0400907 unsigned long flags = 0;
Kees Cook3f649ab2020-06-03 13:09:38 -0700908 unsigned int lock_batch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 for (i = 0; i < nr; i++) {
911 struct page *page = pages[i];
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -0400912 struct folio *folio = page_folio(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Michal Hockoaabfb572014-10-09 15:28:52 -0700914 /*
915 * Make sure the IRQ-safe lock-holding time does not get
916 * excessive with a continuous string of pages from the
Alex Shi6168d0d2020-12-15 12:34:29 -0800917 * same lruvec. The lock is held only if lruvec != NULL.
Michal Hockoaabfb572014-10-09 15:28:52 -0700918 */
Alex Shi6168d0d2020-12-15 12:34:29 -0800919 if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) {
920 unlock_page_lruvec_irqrestore(lruvec, flags);
921 lruvec = NULL;
Michal Hockoaabfb572014-10-09 15:28:52 -0700922 }
923
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -0400924 page = &folio->page;
Aaron Lu6fcb52a2016-10-07 17:00:08 -0700925 if (is_huge_zero_page(page))
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -0700926 continue;
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -0700927
Ira Weinyc5d6c452019-06-05 14:49:22 -0700928 if (is_zone_device_page(page)) {
Alex Shi6168d0d2020-12-15 12:34:29 -0800929 if (lruvec) {
930 unlock_page_lruvec_irqrestore(lruvec, flags);
931 lruvec = NULL;
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700932 }
Ira Weinyc5d6c452019-06-05 14:49:22 -0700933 /*
934 * ZONE_DEVICE pages that return 'false' from
Miaohe Lina3e7bea2020-10-13 16:52:15 -0700935 * page_is_devmap_managed() do not require special
Ira Weinyc5d6c452019-06-05 14:49:22 -0700936 * processing, and instead, expect a call to
937 * put_page_testzero().
938 */
John Hubbard07d80262020-01-30 22:12:28 -0800939 if (page_is_devmap_managed(page)) {
940 put_devmap_managed_page(page);
Ira Weinyc5d6c452019-06-05 14:49:22 -0700941 continue;
John Hubbard07d80262020-01-30 22:12:28 -0800942 }
Ralph Campbell43fbdeb2020-12-14 19:05:55 -0800943 if (put_page_testzero(page))
944 put_dev_pagemap(page->pgmap);
945 continue;
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700946 }
947
Nick Pigginb5810032005-10-29 18:16:12 -0700948 if (!put_page_testzero(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 continue;
950
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800951 if (PageCompound(page)) {
Alex Shi6168d0d2020-12-15 12:34:29 -0800952 if (lruvec) {
953 unlock_page_lruvec_irqrestore(lruvec, flags);
954 lruvec = NULL;
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800955 }
956 __put_compound_page(page);
957 continue;
958 }
959
Nick Piggin46453a62006-03-22 00:07:58 -0800960 if (PageLRU(page)) {
Alexander Duyck2a5e4e32020-12-15 12:34:33 -0800961 struct lruvec *prev_lruvec = lruvec;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700962
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -0400963 lruvec = folio_lruvec_relock_irqsave(folio, lruvec,
Alexander Duyck2a5e4e32020-12-15 12:34:33 -0800964 &flags);
965 if (prev_lruvec != lruvec)
Michal Hockoaabfb572014-10-09 15:28:52 -0700966 lock_batch = 0;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700967
Yu Zhao46ae6b22021-02-24 12:08:25 -0800968 del_page_from_lru_list(page, lruvec);
Yu Zhao87560172021-02-24 12:08:28 -0800969 __clear_page_lru_flags(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Nick Piggin46453a62006-03-22 00:07:58 -0800971
Nicholas Piggin62906022016-12-25 13:00:30 +1000972 __ClearPageWaiters(page);
Mel Gormanc53954a2013-07-03 15:02:34 -0700973
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -0800974 list_add(&page->lru, &pages_to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Alex Shi6168d0d2020-12-15 12:34:29 -0800976 if (lruvec)
977 unlock_page_lruvec_irqrestore(lruvec, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Johannes Weiner747db952014-08-08 14:19:24 -0700979 mem_cgroup_uncharge_list(&pages_to_free);
Mel Gorman2d4894b2017-11-15 17:37:59 -0800980 free_unref_page_list(&pages_to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
Miklos Szeredi0be85572010-10-27 15:34:46 -0700982EXPORT_SYMBOL(release_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984/*
985 * The pages which we're about to release may be in the deferred lru-addition
986 * queues. That would prevent them from really being freed right now. That's
987 * OK from a correctness point of view but is inefficient - those pages may be
988 * cache-warm and we want to give them back to the page allocator ASAP.
989 *
990 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
991 * and __pagevec_lru_add_active() call release_pages() directly to avoid
992 * mutual recursion.
993 */
994void __pagevec_release(struct pagevec *pvec)
995{
Mel Gorman7f0b5fb2017-11-15 17:38:10 -0800996 if (!pvec->percpu_pvec_drained) {
Mel Gormand9ed0d02017-11-15 17:37:48 -0800997 lru_add_drain();
Mel Gorman7f0b5fb2017-11-15 17:38:10 -0800998 pvec->percpu_pvec_drained = true;
Mel Gormand9ed0d02017-11-15 17:37:48 -0800999 }
Mel Gormanc6f92f92017-11-15 17:37:55 -08001000 release_pages(pvec->pages, pagevec_count(pvec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 pagevec_reinit(pvec);
1002}
Steve French7f285702005-11-01 10:22:55 -08001003EXPORT_SYMBOL(__pagevec_release);
1004
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001005static void __pagevec_lru_add_fn(struct folio *folio, struct lruvec *lruvec)
Shaohua Li3dd7ae82011-03-22 16:33:45 -07001006{
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001007 int was_unevictable = folio_test_clear_unevictable(folio);
1008 long nr_pages = folio_nr_pages(folio);
Shaohua Li3dd7ae82011-03-22 16:33:45 -07001009
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001010 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
Shaohua Li3dd7ae82011-03-22 16:33:45 -07001011
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001012 /*
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001013 * A folio becomes evictable in two ways:
Peng Fandae966d2019-05-13 17:19:26 -07001014 * 1) Within LRU lock [munlock_vma_page() and __munlock_pagevec()].
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001015 * 2) Before acquiring LRU lock to put the folio on the correct LRU
1016 * and then
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001017 * a) do PageLRU check with lock [check_move_unevictable_pages]
1018 * b) do PageLRU check before lock [clear_page_mlock]
1019 *
1020 * (1) & (2a) are ok as LRU lock will serialize them. For (2b), we need
1021 * following strict ordering:
1022 *
1023 * #0: __pagevec_lru_add_fn #1: clear_page_mlock
1024 *
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001025 * folio_set_lru() folio_test_clear_mlocked()
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001026 * smp_mb() // explicit ordering // above provides strict
1027 * // ordering
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001028 * folio_test_mlocked() folio_test_lru()
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001029 *
1030 *
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001031 * if '#1' does not observe setting of PG_lru by '#0' and
1032 * fails isolation, the explicit barrier will make sure that
1033 * folio_evictable check will put the folio on the correct
1034 * LRU. Without smp_mb(), folio_set_lru() can be reordered
1035 * after folio_test_mlocked() check and can make '#1' fail the
1036 * isolation of the folio whose mlocked bit is cleared (#0 is
1037 * also looking at the same folio) and the evictable folio will
1038 * be stranded on an unevictable LRU.
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001039 */
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001040 folio_set_lru(folio);
Yang Shi9a9b6cc2020-04-01 21:06:23 -07001041 smp_mb__after_atomic();
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001042
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001043 if (folio_evictable(folio)) {
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001044 if (was_unevictable)
Shakeel Butt5d91f312020-06-03 16:03:16 -07001045 __count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages);
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001046 } else {
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001047 folio_clear_active(folio);
1048 folio_set_unevictable(folio);
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001049 if (!was_unevictable)
Shakeel Butt5d91f312020-06-03 16:03:16 -07001050 __count_vm_events(UNEVICTABLE_PGCULLED, nr_pages);
Shakeel Butt9c4e6b12018-02-21 14:45:28 -08001051 }
1052
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001053 lruvec_add_folio(lruvec, folio);
1054 trace_mm_lru_insertion(folio);
Shaohua Li3dd7ae82011-03-22 16:33:45 -07001055}
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 * Add the passed pages to the LRU, then drop the caller's refcount
1059 * on them. Reinitialises the caller's pagevec.
1060 */
Mel Gormana0b8cab32013-07-03 15:02:32 -07001061void __pagevec_lru_add(struct pagevec *pvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Alex Shifc574c22020-12-15 12:34:25 -08001063 int i;
Alex Shi6168d0d2020-12-15 12:34:29 -08001064 struct lruvec *lruvec = NULL;
Alex Shifc574c22020-12-15 12:34:25 -08001065 unsigned long flags = 0;
1066
1067 for (i = 0; i < pagevec_count(pvec); i++) {
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001068 struct folio *folio = page_folio(pvec->pages[i]);
Alex Shifc574c22020-12-15 12:34:25 -08001069
Matthew Wilcox (Oracle)0de340c2021-06-29 22:27:31 -04001070 lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags);
Matthew Wilcox (Oracle)934387c2021-05-14 15:08:29 -04001071 __pagevec_lru_add_fn(folio, lruvec);
Alex Shifc574c22020-12-15 12:34:25 -08001072 }
Alex Shi6168d0d2020-12-15 12:34:29 -08001073 if (lruvec)
1074 unlock_page_lruvec_irqrestore(lruvec, flags);
Alex Shifc574c22020-12-15 12:34:25 -08001075 release_pages(pvec->pages, pvec->nr);
1076 pagevec_reinit(pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079/**
Matthew Wilcox (Oracle)1613fac2021-12-07 14:28:49 -05001080 * folio_batch_remove_exceptionals() - Prune non-folios from a batch.
1081 * @fbatch: The batch to prune
Johannes Weiner0cd61442014-04-03 14:47:46 -07001082 *
Matthew Wilcox (Oracle)1613fac2021-12-07 14:28:49 -05001083 * find_get_entries() fills a batch with both folios and shadow/swap/DAX
1084 * entries. This function prunes all the non-folio entries from @fbatch
1085 * without leaving holes, so that it can be passed on to folio-only batch
1086 * operations.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001087 */
Matthew Wilcox (Oracle)1613fac2021-12-07 14:28:49 -05001088void folio_batch_remove_exceptionals(struct folio_batch *fbatch)
Johannes Weiner0cd61442014-04-03 14:47:46 -07001089{
Matthew Wilcox (Oracle)1613fac2021-12-07 14:28:49 -05001090 unsigned int i, j;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001091
Matthew Wilcox (Oracle)1613fac2021-12-07 14:28:49 -05001092 for (i = 0, j = 0; i < folio_batch_count(fbatch); i++) {
1093 struct folio *folio = fbatch->folios[i];
1094 if (!xa_is_value(folio))
1095 fbatch->folios[j++] = folio;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001096 }
Matthew Wilcox (Oracle)1613fac2021-12-07 14:28:49 -05001097 fbatch->nr = j;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001098}
1099
1100/**
Jan Karab947cee2017-09-06 16:21:21 -07001101 * pagevec_lookup_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 * @pvec: Where the resulting pages are placed
1103 * @mapping: The address_space to search
1104 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07001105 * @end: The final page index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 *
Randy Dunlape02a9f02018-01-31 16:21:19 -08001107 * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE
Jan Karab947cee2017-09-06 16:21:21 -07001108 * pages in the mapping starting from index @start and upto index @end
1109 * (inclusive). The pages are placed in @pvec. pagevec_lookup() takes a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * reference against the pages in @pvec.
1111 *
1112 * The search returns a group of mapping-contiguous pages with ascending
Jan Karad72dc8a2017-09-06 16:21:18 -07001113 * indexes. There may be holes in the indices due to not-present pages. We
1114 * also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 *
Jan Karab947cee2017-09-06 16:21:21 -07001116 * pagevec_lookup_range() returns the number of pages which were found. If this
Randy Dunlape02a9f02018-01-31 16:21:19 -08001117 * number is smaller than PAGEVEC_SIZE, the end of specified range has been
Jan Karab947cee2017-09-06 16:21:21 -07001118 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 */
Jan Karab947cee2017-09-06 16:21:21 -07001120unsigned pagevec_lookup_range(struct pagevec *pvec,
Jan Kara397162f2017-09-06 16:21:43 -07001121 struct address_space *mapping, pgoff_t *start, pgoff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Jan Kara397162f2017-09-06 16:21:43 -07001123 pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE,
Jan Karab947cee2017-09-06 16:21:21 -07001124 pvec->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return pagevec_count(pvec);
1126}
Jan Karab947cee2017-09-06 16:21:21 -07001127EXPORT_SYMBOL(pagevec_lookup_range);
Christoph Hellwig78539fd2006-01-11 20:47:41 +11001128
Jan Kara72b045a2017-11-15 17:34:33 -08001129unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
1130 struct address_space *mapping, pgoff_t *index, pgoff_t end,
Matthew Wilcox10bbd232017-12-05 17:30:38 -05001131 xa_mark_t tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Jan Kara72b045a2017-11-15 17:34:33 -08001133 pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
Jan Kara67fd7072017-11-15 17:35:19 -08001134 PAGEVEC_SIZE, pvec->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return pagevec_count(pvec);
1136}
Jan Kara72b045a2017-11-15 17:34:33 -08001137EXPORT_SYMBOL(pagevec_lookup_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139/*
1140 * Perform any setup for the swap system
1141 */
1142void __init swap_setup(void)
1143{
Arun KSca79b0c2018-12-28 00:34:29 -08001144 unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 /* Use a smaller cluster for small-memory machines */
1147 if (megs < 16)
1148 page_cluster = 2;
1149 else
1150 page_cluster = 3;
1151 /*
1152 * Right now other parts of the system means that we
1153 * _really_ don't want to cluster much more
1154 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
John Hubbard07d80262020-01-30 22:12:28 -08001156
1157#ifdef CONFIG_DEV_PAGEMAP_OPS
1158void put_devmap_managed_page(struct page *page)
1159{
1160 int count;
1161
1162 if (WARN_ON_ONCE(!page_is_devmap_managed(page)))
1163 return;
1164
1165 count = page_ref_dec_return(page);
1166
1167 /*
1168 * devmap page refcounts are 1-based, rather than 0-based: if
1169 * refcount is 1, then the page is free and the refcount is
1170 * stable because nobody holds a reference on the page.
1171 */
1172 if (count == 1)
1173 free_devmap_managed_page(page);
1174 else if (!count)
1175 __put_page(page);
1176}
1177EXPORT_SYMBOL(put_devmap_managed_page);
1178#endif