blob: 674e2c93da4e958780abf8b4c6de11c8407ef25e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/swap.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 */
6
7/*
Simon Arlott183ff222007-10-20 01:27:18 +02008 * This file contains the default values for the operation of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Linux VM subsystem. Fine-tuning documentation can be found in
10 * Documentation/sysctl/vm.txt.
11 * Started 18.12.91
12 * Swap aging added 23.2.95, Stephen Tweedie.
13 * Buffermem limits added 12.3.98, Rik van Riel.
14 */
15
16#include <linux/mm.h>
17#include <linux/sched.h>
18#include <linux/kernel_stat.h>
19#include <linux/swap.h>
20#include <linux/mman.h>
21#include <linux/pagemap.h>
22#include <linux/pagevec.h>
23#include <linux/init.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040024#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/mm_inline.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/percpu_counter.h>
27#include <linux/percpu.h>
28#include <linux/cpu.h>
29#include <linux/notifier.h>
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -070030#include <linux/backing-dev.h>
Balbir Singh66e17072008-02-07 00:13:56 -080031#include <linux/memcontrol.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/gfp.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070033#include <linux/uio.h>
Naoya Horiguchi822fc612015-04-15 16:14:35 -070034#include <linux/hugetlb.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070035#include <linux/page_idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Lee Schermerhorn64d65192008-10-18 20:26:52 -070037#include "internal.h"
38
Mel Gormanc6286c92013-07-03 15:02:26 -070039#define CREATE_TRACE_POINTS
40#include <trace/events/pagemap.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* How many pages do we try to swap or page in/out together? */
43int page_cluster;
44
Mel Gorman13f7f782013-07-03 15:02:28 -070045static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
Vegard Nossumf84f95042008-07-23 21:28:14 -070046static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
Minchan Kimcc5993b2015-04-15 16:13:26 -070047static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
Minchan Kim10853a02016-01-15 16:55:11 -080048static DEFINE_PER_CPU(struct pagevec, lru_deactivate_pvecs);
Hisashi Hifumi902aaed2007-10-16 01:24:52 -070049
Adrian Bunkb2213852006-09-25 23:31:02 -070050/*
51 * This path almost never happens for VM activity - pages are normally
52 * freed via pagevecs. But it gets used by networking.
53 */
Harvey Harrison920c7a52008-02-04 22:29:26 -080054static void __page_cache_release(struct page *page)
Adrian Bunkb2213852006-09-25 23:31:02 -070055{
56 if (PageLRU(page)) {
Adrian Bunkb2213852006-09-25 23:31:02 -070057 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -070058 struct lruvec *lruvec;
59 unsigned long flags;
Adrian Bunkb2213852006-09-25 23:31:02 -070060
61 spin_lock_irqsave(&zone->lru_lock, flags);
Hugh Dickinsfa9add62012-05-29 15:07:09 -070062 lruvec = mem_cgroup_page_lruvec(page, zone);
Sasha Levin309381fea2014-01-23 15:52:54 -080063 VM_BUG_ON_PAGE(!PageLRU(page), page);
Adrian Bunkb2213852006-09-25 23:31:02 -070064 __ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -070065 del_page_from_lru_list(page, lruvec, page_off_lru(page));
Adrian Bunkb2213852006-09-25 23:31:02 -070066 spin_unlock_irqrestore(&zone->lru_lock, flags);
67 }
Johannes Weiner0a31bc92014-08-08 14:19:22 -070068 mem_cgroup_uncharge(page);
Andrea Arcangeli91807062011-01-13 15:46:32 -080069}
70
71static void __put_single_page(struct page *page)
72{
73 __page_cache_release(page);
Mel Gormanb745bc82014-06-04 16:10:22 -070074 free_hot_cold_page(page, false);
Adrian Bunkb2213852006-09-25 23:31:02 -070075}
76
Andrea Arcangeli91807062011-01-13 15:46:32 -080077static void __put_compound_page(struct page *page)
78{
79 compound_page_dtor *dtor;
80
Naoya Horiguchi822fc612015-04-15 16:14:35 -070081 /*
82 * __page_cache_release() is supposed to be called for thp, not for
83 * hugetlb. This is because hugetlb page does never have PageLRU set
84 * (it's never listed to any LRU lists) and no memcg routines should
85 * be called for hugetlb (it has a separate hugetlb_cgroup.)
86 */
87 if (!PageHuge(page))
88 __page_cache_release(page);
Andrea Arcangeli91807062011-01-13 15:46:32 -080089 dtor = get_compound_page_dtor(page);
90 (*dtor)(page);
91}
92
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -080093void __put_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Nick Piggin8519fb32006-02-07 12:58:52 -080095 if (unlikely(PageCompound(page)))
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -080096 __put_compound_page(page);
97 else
Andrea Arcangeli91807062011-01-13 15:46:32 -080098 __put_single_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800100EXPORT_SYMBOL(__put_page);
Andrea Arcangeli70b50f92011-11-02 13:36:59 -0700101
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700102/**
Randy Dunlap76824862008-03-19 17:00:40 -0700103 * put_pages_list() - release a list of pages
104 * @pages: list of pages threaded on page->lru
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700105 *
106 * Release a list of pages which are strung together on page.lru. Currently
107 * used by read_cache_pages() and related error recovery code.
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700108 */
109void put_pages_list(struct list_head *pages)
110{
111 while (!list_empty(pages)) {
112 struct page *victim;
113
114 victim = list_entry(pages->prev, struct page, lru);
115 list_del(&victim->lru);
116 page_cache_release(victim);
117 }
118}
119EXPORT_SYMBOL(put_pages_list);
120
Mel Gorman18022c52012-07-31 16:44:51 -0700121/*
122 * get_kernel_pages() - pin kernel pages in memory
123 * @kiov: An array of struct kvec structures
124 * @nr_segs: number of segments to pin
125 * @write: pinning for read/write, currently ignored
126 * @pages: array that receives pointers to the pages pinned.
127 * Should be at least nr_segs long.
128 *
129 * Returns number of pages pinned. This may be fewer than the number
130 * requested. If nr_pages is 0 or negative, returns 0. If no pages
131 * were pinned, returns -errno. Each page returned must be released
132 * with a put_page() call when it is finished with.
133 */
134int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write,
135 struct page **pages)
136{
137 int seg;
138
139 for (seg = 0; seg < nr_segs; seg++) {
140 if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE))
141 return seg;
142
Mel Gorman5a178112012-07-31 16:45:02 -0700143 pages[seg] = kmap_to_page(kiov[seg].iov_base);
Mel Gorman18022c52012-07-31 16:44:51 -0700144 page_cache_get(pages[seg]);
145 }
146
147 return seg;
148}
149EXPORT_SYMBOL_GPL(get_kernel_pages);
150
151/*
152 * get_kernel_page() - pin a kernel page in memory
153 * @start: starting kernel address
154 * @write: pinning for read/write, currently ignored
155 * @pages: array that receives pointer to the page pinned.
156 * Must be at least nr_segs long.
157 *
158 * Returns 1 if page is pinned. If the page was not pinned, returns
159 * -errno. The page returned must be released with a put_page() call
160 * when it is finished with.
161 */
162int get_kernel_page(unsigned long start, int write, struct page **pages)
163{
164 const struct kvec kiov = {
165 .iov_base = (void *)start,
166 .iov_len = PAGE_SIZE
167 };
168
169 return get_kernel_pages(&kiov, 1, write, pages);
170}
171EXPORT_SYMBOL_GPL(get_kernel_page);
172
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700173static void pagevec_lru_move_fn(struct pagevec *pvec,
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700174 void (*move_fn)(struct page *page, struct lruvec *lruvec, void *arg),
175 void *arg)
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700176{
177 int i;
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700178 struct zone *zone = NULL;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700179 struct lruvec *lruvec;
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700180 unsigned long flags = 0;
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700181
182 for (i = 0; i < pagevec_count(pvec); i++) {
183 struct page *page = pvec->pages[i];
184 struct zone *pagezone = page_zone(page);
185
186 if (pagezone != zone) {
187 if (zone)
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700188 spin_unlock_irqrestore(&zone->lru_lock, flags);
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700189 zone = pagezone;
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700190 spin_lock_irqsave(&zone->lru_lock, flags);
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700191 }
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700192
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700193 lruvec = mem_cgroup_page_lruvec(page, zone);
194 (*move_fn)(page, lruvec, arg);
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700195 }
196 if (zone)
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700197 spin_unlock_irqrestore(&zone->lru_lock, flags);
Linus Torvalds83896fb2011-01-17 14:42:34 -0800198 release_pages(pvec->pages, pvec->nr, pvec->cold);
199 pagevec_reinit(pvec);
Shaohua Lid8505de2011-01-13 15:47:33 -0800200}
201
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700202static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec,
203 void *arg)
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700204{
205 int *pgmoved = arg;
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700206
207 if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
208 enum lru_list lru = page_lru_base_type(page);
Johannes Weiner925b7672012-01-12 17:18:15 -0800209 list_move_tail(&page->lru, &lruvec->lists[lru]);
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700210 (*pgmoved)++;
211 }
212}
213
214/*
215 * pagevec_move_tail() must be called with IRQ disabled.
216 * Otherwise this may cause nasty races.
217 */
218static void pagevec_move_tail(struct pagevec *pvec)
219{
220 int pgmoved = 0;
221
222 pagevec_lru_move_fn(pvec, pagevec_move_tail_fn, &pgmoved);
223 __count_vm_events(PGROTATED, pgmoved);
224}
225
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700226/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * Writeback is about to end against a page which has been marked for immediate
228 * reclaim. If it still appears to be reclaimable, move it to the tail of the
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700229 * inactive list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700231void rotate_reclaimable_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700233 if (!PageLocked(page) && !PageDirty(page) && !PageActive(page) &&
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700234 !PageUnevictable(page) && PageLRU(page)) {
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700235 struct pagevec *pvec;
236 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700238 page_cache_get(page);
239 local_irq_save(flags);
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700240 pvec = this_cpu_ptr(&lru_rotate_pvecs);
Miklos Szerediac6aadb2008-04-28 02:12:38 -0700241 if (!pagevec_add(pvec, page))
242 pagevec_move_tail(pvec);
243 local_irq_restore(flags);
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700247static void update_page_reclaim_stat(struct lruvec *lruvec,
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800248 int file, int rotated)
249{
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700250 struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800251
252 reclaim_stat->recent_scanned[file]++;
253 if (rotated)
254 reclaim_stat->recent_rotated[file]++;
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800255}
256
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700257static void __activate_page(struct page *page, struct lruvec *lruvec,
258 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Linus Torvalds7a608572011-01-17 14:42:19 -0800260 if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
261 int file = page_is_file_cache(page);
262 int lru = page_lru_base_type(page);
Linus Torvalds7a608572011-01-17 14:42:19 -0800263
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700264 del_page_from_lru_list(page, lruvec, lru);
Linus Torvalds7a608572011-01-17 14:42:19 -0800265 SetPageActive(page);
266 lru += LRU_ACTIVE;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700267 add_page_to_lru_list(page, lruvec, lru);
Mel Gorman24b7e582014-08-06 16:07:11 -0700268 trace_mm_lru_activate(page);
Linus Torvalds7a608572011-01-17 14:42:19 -0800269
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700270 __count_vm_event(PGACTIVATE);
271 update_page_reclaim_stat(lruvec, file, 1);
Linus Torvalds7a608572011-01-17 14:42:19 -0800272 }
Shaohua Lieb709b02011-05-24 17:12:55 -0700273}
274
275#ifdef CONFIG_SMP
276static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
277
278static void activate_page_drain(int cpu)
279{
280 struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
281
282 if (pagevec_count(pvec))
283 pagevec_lru_move_fn(pvec, __activate_page, NULL);
284}
285
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700286static bool need_activate_page_drain(int cpu)
287{
288 return pagevec_count(&per_cpu(activate_page_pvecs, cpu)) != 0;
289}
290
Shaohua Lieb709b02011-05-24 17:12:55 -0700291void activate_page(struct page *page)
292{
293 if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
294 struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
295
296 page_cache_get(page);
297 if (!pagevec_add(pvec, page))
298 pagevec_lru_move_fn(pvec, __activate_page, NULL);
299 put_cpu_var(activate_page_pvecs);
300 }
301}
302
303#else
304static inline void activate_page_drain(int cpu)
305{
306}
307
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700308static bool need_activate_page_drain(int cpu)
309{
310 return false;
311}
312
Shaohua Lieb709b02011-05-24 17:12:55 -0700313void activate_page(struct page *page)
314{
315 struct zone *zone = page_zone(page);
316
317 spin_lock_irq(&zone->lru_lock);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700318 __activate_page(page, mem_cgroup_page_lruvec(page, zone), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 spin_unlock_irq(&zone->lru_lock);
320}
Shaohua Lieb709b02011-05-24 17:12:55 -0700321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Mel Gorman059285a2013-07-03 15:02:30 -0700323static void __lru_cache_activate_page(struct page *page)
324{
325 struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
326 int i;
327
328 /*
329 * Search backwards on the optimistic assumption that the page being
330 * activated has just been added to this pagevec. Note that only
331 * the local pagevec is examined as a !PageLRU page could be in the
332 * process of being released, reclaimed, migrated or on a remote
333 * pagevec that is currently being drained. Furthermore, marking
334 * a remote pagevec's page PageActive potentially hits a race where
335 * a page is marked PageActive just after it is added to the inactive
336 * list causing accounting errors and BUG_ON checks to trigger.
337 */
338 for (i = pagevec_count(pvec) - 1; i >= 0; i--) {
339 struct page *pagevec_page = pvec->pages[i];
340
341 if (pagevec_page == page) {
342 SetPageActive(page);
343 break;
344 }
345 }
346
347 put_cpu_var(lru_add_pvec);
348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/*
351 * Mark a page as having seen activity.
352 *
353 * inactive,unreferenced -> inactive,referenced
354 * inactive,referenced -> active,unreferenced
355 * active,unreferenced -> active,referenced
Hugh Dickinseb39d612014-08-06 16:06:43 -0700356 *
357 * When a newly allocated page is not yet visible, so safe for non-atomic ops,
358 * __SetPageReferenced(page) may be substituted for mark_page_accessed(page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
Harvey Harrison920c7a52008-02-04 22:29:26 -0800360void mark_page_accessed(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800362 page = compound_head(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700363 if (!PageActive(page) && !PageUnevictable(page) &&
Mel Gorman059285a2013-07-03 15:02:30 -0700364 PageReferenced(page)) {
365
366 /*
367 * If the page is on the LRU, queue it for activation via
368 * activate_page_pvecs. Otherwise, assume the page is on a
369 * pagevec, mark it active and it'll be moved to the active
370 * LRU on the next drain.
371 */
372 if (PageLRU(page))
373 activate_page(page);
374 else
375 __lru_cache_activate_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 ClearPageReferenced(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700377 if (page_is_file_cache(page))
378 workingset_activation(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 } else if (!PageReferenced(page)) {
380 SetPageReferenced(page);
381 }
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700382 if (page_is_idle(page))
383 clear_page_idle(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385EXPORT_SYMBOL(mark_page_accessed);
386
Jianyu Zhan2329d372014-06-04 16:07:31 -0700387static void __lru_cache_add(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Mel Gorman13f7f782013-07-03 15:02:28 -0700389 struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 page_cache_get(page);
Robin Dongd741c9c2012-10-08 16:29:05 -0700392 if (!pagevec_space(pvec))
Mel Gormana0b8cab32013-07-03 15:02:32 -0700393 __pagevec_lru_add(pvec);
Robin Dongd741c9c2012-10-08 16:29:05 -0700394 pagevec_add(pvec, page);
Mel Gorman13f7f782013-07-03 15:02:28 -0700395 put_cpu_var(lru_add_pvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
Jianyu Zhan2329d372014-06-04 16:07:31 -0700397
398/**
399 * lru_cache_add: add a page to the page lists
400 * @page: the page to add
401 */
402void lru_cache_add_anon(struct page *page)
403{
Mel Gorman6fb81a12014-06-04 16:10:28 -0700404 if (PageActive(page))
405 ClearPageActive(page);
Jianyu Zhan2329d372014-06-04 16:07:31 -0700406 __lru_cache_add(page);
407}
408
409void lru_cache_add_file(struct page *page)
410{
Mel Gorman6fb81a12014-06-04 16:10:28 -0700411 if (PageActive(page))
412 ClearPageActive(page);
Jianyu Zhan2329d372014-06-04 16:07:31 -0700413 __lru_cache_add(page);
414}
415EXPORT_SYMBOL(lru_cache_add_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
KOSAKI Motohirof04e9eb2008-10-18 20:26:19 -0700417/**
Mel Gormanc53954a2013-07-03 15:02:34 -0700418 * lru_cache_add - add a page to a page list
KOSAKI Motohirof04e9eb2008-10-18 20:26:19 -0700419 * @page: the page to be added to the LRU.
Jianyu Zhan2329d372014-06-04 16:07:31 -0700420 *
421 * Queue the page for addition to the LRU via pagevec. The decision on whether
422 * to add the page to the [in]active [file|anon] list is deferred until the
423 * pagevec is drained. This gives a chance for the caller of lru_cache_add()
424 * have the page added to the active list using mark_page_accessed().
KOSAKI Motohirof04e9eb2008-10-18 20:26:19 -0700425 */
Mel Gormanc53954a2013-07-03 15:02:34 -0700426void lru_cache_add(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Sasha Levin309381fea2014-01-23 15:52:54 -0800428 VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
429 VM_BUG_ON_PAGE(PageLRU(page), page);
Mel Gormanc53954a2013-07-03 15:02:34 -0700430 __lru_cache_add(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700433/**
434 * add_page_to_unevictable_list - add a page to the unevictable list
435 * @page: the page to be added to the unevictable list
436 *
437 * Add page directly to its zone's unevictable list. To avoid races with
438 * tasks that might be making the page evictable, through eg. munlock,
439 * munmap or exit, while it's not on the lru, we want to add the page
440 * while it's locked or otherwise "invisible" to other tasks. This is
441 * difficult to do when using the pagevec cache, so bypass that.
442 */
443void add_page_to_unevictable_list(struct page *page)
444{
445 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700446 struct lruvec *lruvec;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700447
448 spin_lock_irq(&zone->lru_lock);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700449 lruvec = mem_cgroup_page_lruvec(page, zone);
Naoya Horiguchief2a2cb2013-07-31 13:53:37 -0700450 ClearPageActive(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700451 SetPageUnevictable(page);
452 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700453 add_page_to_lru_list(page, lruvec, LRU_UNEVICTABLE);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700454 spin_unlock_irq(&zone->lru_lock);
455}
456
Johannes Weiner00501b52014-08-08 14:19:20 -0700457/**
458 * lru_cache_add_active_or_unevictable
459 * @page: the page to be added to LRU
460 * @vma: vma in which page is mapped for determining reclaimability
461 *
462 * Place @page on the active or unevictable LRU list, depending on its
463 * evictability. Note that if the page is not evictable, it goes
464 * directly back onto it's zone's unevictable list, it does NOT use a
465 * per cpu pagevec.
466 */
467void lru_cache_add_active_or_unevictable(struct page *page,
468 struct vm_area_struct *vma)
469{
470 VM_BUG_ON_PAGE(PageLRU(page), page);
471
472 if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED)) {
473 SetPageActive(page);
474 lru_cache_add(page);
475 return;
476 }
477
478 if (!TestSetPageMlocked(page)) {
479 /*
480 * We use the irq-unsafe __mod_zone_page_stat because this
481 * counter is not modified from interrupt context, and the pte
482 * lock is held(spinlock), which implies preemption disabled.
483 */
484 __mod_zone_page_state(page_zone(page), NR_MLOCK,
485 hpage_nr_pages(page));
486 count_vm_event(UNEVICTABLE_PGMLOCKED);
487 }
488 add_page_to_unevictable_list(page);
489}
490
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700491/*
Minchan Kim31560182011-03-22 16:32:52 -0700492 * If the page can not be invalidated, it is moved to the
493 * inactive list to speed up its reclaim. It is moved to the
494 * head of the list, rather than the tail, to give the flusher
495 * threads some time to write it out, as this is much more
496 * effective than the single-page writeout from reclaim.
Minchan Kim278df9f2011-03-22 16:32:54 -0700497 *
498 * If the page isn't page_mapped and dirty/writeback, the page
499 * could reclaim asap using PG_reclaim.
500 *
501 * 1. active, mapped page -> none
502 * 2. active, dirty/writeback page -> inactive, head, PG_reclaim
503 * 3. inactive, mapped page -> none
504 * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim
505 * 5. inactive, clean -> inactive, tail
506 * 6. Others -> none
507 *
508 * In 4, why it moves inactive's head, the VM expects the page would
509 * be write it out by flusher threads as this is much more effective
510 * than the single-page writeout from reclaim.
Minchan Kim31560182011-03-22 16:32:52 -0700511 */
Minchan Kimcc5993b2015-04-15 16:13:26 -0700512static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700513 void *arg)
Minchan Kim31560182011-03-22 16:32:52 -0700514{
515 int lru, file;
Minchan Kim278df9f2011-03-22 16:32:54 -0700516 bool active;
Minchan Kim31560182011-03-22 16:32:52 -0700517
Minchan Kim278df9f2011-03-22 16:32:54 -0700518 if (!PageLRU(page))
Minchan Kim31560182011-03-22 16:32:52 -0700519 return;
520
Minchan Kimbad49d92011-05-11 15:13:30 -0700521 if (PageUnevictable(page))
522 return;
523
Minchan Kim31560182011-03-22 16:32:52 -0700524 /* Some processes are using the page */
525 if (page_mapped(page))
526 return;
527
Minchan Kim278df9f2011-03-22 16:32:54 -0700528 active = PageActive(page);
Minchan Kim31560182011-03-22 16:32:52 -0700529 file = page_is_file_cache(page);
530 lru = page_lru_base_type(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700531
532 del_page_from_lru_list(page, lruvec, lru + active);
Minchan Kim31560182011-03-22 16:32:52 -0700533 ClearPageActive(page);
534 ClearPageReferenced(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700535 add_page_to_lru_list(page, lruvec, lru);
Minchan Kim31560182011-03-22 16:32:52 -0700536
Minchan Kim278df9f2011-03-22 16:32:54 -0700537 if (PageWriteback(page) || PageDirty(page)) {
538 /*
539 * PG_reclaim could be raced with end_page_writeback
540 * It can make readahead confusing. But race window
541 * is _really_ small and it's non-critical problem.
542 */
543 SetPageReclaim(page);
544 } else {
545 /*
546 * The page's writeback ends up during pagevec
547 * We moves tha page into tail of inactive.
548 */
Johannes Weiner925b7672012-01-12 17:18:15 -0800549 list_move_tail(&page->lru, &lruvec->lists[lru]);
Minchan Kim278df9f2011-03-22 16:32:54 -0700550 __count_vm_event(PGROTATED);
551 }
552
553 if (active)
554 __count_vm_event(PGDEACTIVATE);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700555 update_page_reclaim_stat(lruvec, file, 0);
Minchan Kim31560182011-03-22 16:32:52 -0700556}
557
Minchan Kim10853a02016-01-15 16:55:11 -0800558
559static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec,
560 void *arg)
561{
562 if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
563 int file = page_is_file_cache(page);
564 int lru = page_lru_base_type(page);
565
566 del_page_from_lru_list(page, lruvec, lru + LRU_ACTIVE);
567 ClearPageActive(page);
568 ClearPageReferenced(page);
569 add_page_to_lru_list(page, lruvec, lru);
570
571 __count_vm_event(PGDEACTIVATE);
572 update_page_reclaim_stat(lruvec, file, 0);
573 }
574}
575
Minchan Kim31560182011-03-22 16:32:52 -0700576/*
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700577 * Drain pages out of the cpu's pagevecs.
578 * Either "cpu" is the current CPU, and preemption has already been
579 * disabled; or "cpu" is being hot-unplugged, and is already dead.
580 */
Konstantin Khlebnikovf0cb3c72012-03-21 16:34:06 -0700581void lru_add_drain_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Mel Gorman13f7f782013-07-03 15:02:28 -0700583 struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Mel Gorman13f7f782013-07-03 15:02:28 -0700585 if (pagevec_count(pvec))
Mel Gormana0b8cab32013-07-03 15:02:32 -0700586 __pagevec_lru_add(pvec);
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700587
588 pvec = &per_cpu(lru_rotate_pvecs, cpu);
589 if (pagevec_count(pvec)) {
590 unsigned long flags;
591
592 /* No harm done if a racing interrupt already did this */
593 local_irq_save(flags);
594 pagevec_move_tail(pvec);
595 local_irq_restore(flags);
596 }
Minchan Kim31560182011-03-22 16:32:52 -0700597
Minchan Kimcc5993b2015-04-15 16:13:26 -0700598 pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
Minchan Kim31560182011-03-22 16:32:52 -0700599 if (pagevec_count(pvec))
Minchan Kimcc5993b2015-04-15 16:13:26 -0700600 pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
Shaohua Lieb709b02011-05-24 17:12:55 -0700601
Minchan Kim10853a02016-01-15 16:55:11 -0800602 pvec = &per_cpu(lru_deactivate_pvecs, cpu);
603 if (pagevec_count(pvec))
604 pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
605
Shaohua Lieb709b02011-05-24 17:12:55 -0700606 activate_page_drain(cpu);
Minchan Kim31560182011-03-22 16:32:52 -0700607}
608
609/**
Minchan Kimcc5993b2015-04-15 16:13:26 -0700610 * deactivate_file_page - forcefully deactivate a file page
Minchan Kim31560182011-03-22 16:32:52 -0700611 * @page: page to deactivate
612 *
613 * This function hints the VM that @page is a good reclaim candidate,
614 * for example if its invalidation fails due to the page being dirty
615 * or under writeback.
616 */
Minchan Kimcc5993b2015-04-15 16:13:26 -0700617void deactivate_file_page(struct page *page)
Minchan Kim31560182011-03-22 16:32:52 -0700618{
Minchan Kim821ed6b2011-05-24 17:12:31 -0700619 /*
Minchan Kimcc5993b2015-04-15 16:13:26 -0700620 * In a workload with many unevictable page such as mprotect,
621 * unevictable page deactivation for accelerating reclaim is pointless.
Minchan Kim821ed6b2011-05-24 17:12:31 -0700622 */
623 if (PageUnevictable(page))
624 return;
625
Minchan Kim31560182011-03-22 16:32:52 -0700626 if (likely(get_page_unless_zero(page))) {
Minchan Kimcc5993b2015-04-15 16:13:26 -0700627 struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
Minchan Kim31560182011-03-22 16:32:52 -0700628
629 if (!pagevec_add(pvec, page))
Minchan Kimcc5993b2015-04-15 16:13:26 -0700630 pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
631 put_cpu_var(lru_deactivate_file_pvecs);
Minchan Kim31560182011-03-22 16:32:52 -0700632 }
Andrew Morton80bfed92006-01-06 00:11:14 -0800633}
634
Minchan Kim10853a02016-01-15 16:55:11 -0800635/**
636 * deactivate_page - deactivate a page
637 * @page: page to deactivate
638 *
639 * deactivate_page() moves @page to the inactive list if @page was on the active
640 * list and was not an unevictable page. This is done to accelerate the reclaim
641 * of @page.
642 */
643void deactivate_page(struct page *page)
644{
645 if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
646 struct pagevec *pvec = &get_cpu_var(lru_deactivate_pvecs);
647
648 page_cache_get(page);
649 if (!pagevec_add(pvec, page))
650 pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
651 put_cpu_var(lru_deactivate_pvecs);
652 }
653}
654
Andrew Morton80bfed92006-01-06 00:11:14 -0800655void lru_add_drain(void)
656{
Konstantin Khlebnikovf0cb3c72012-03-21 16:34:06 -0700657 lru_add_drain_cpu(get_cpu());
Andrew Morton80bfed92006-01-06 00:11:14 -0800658 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
David Howellsc4028952006-11-22 14:57:56 +0000661static void lru_add_drain_per_cpu(struct work_struct *dummy)
Nick Piggin053837f2006-01-18 17:42:27 -0800662{
663 lru_add_drain();
664}
665
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700666static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
667
668void lru_add_drain_all(void)
Nick Piggin053837f2006-01-18 17:42:27 -0800669{
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700670 static DEFINE_MUTEX(lock);
671 static struct cpumask has_work;
672 int cpu;
673
674 mutex_lock(&lock);
675 get_online_cpus();
676 cpumask_clear(&has_work);
677
678 for_each_online_cpu(cpu) {
679 struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
680
681 if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
682 pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
Minchan Kimcc5993b2015-04-15 16:13:26 -0700683 pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
Minchan Kim10853a02016-01-15 16:55:11 -0800684 pagevec_count(&per_cpu(lru_deactivate_pvecs, cpu)) ||
Chris Metcalf5fbc4612013-09-12 15:13:55 -0700685 need_activate_page_drain(cpu)) {
686 INIT_WORK(work, lru_add_drain_per_cpu);
687 schedule_work_on(cpu, work);
688 cpumask_set_cpu(cpu, &has_work);
689 }
690 }
691
692 for_each_cpu(cpu, &has_work)
693 flush_work(&per_cpu(lru_add_drain_work, cpu));
694
695 put_online_cpus();
696 mutex_unlock(&lock);
Nick Piggin053837f2006-01-18 17:42:27 -0800697}
698
Michal Hockoaabfb572014-10-09 15:28:52 -0700699/**
700 * release_pages - batched page_cache_release()
701 * @pages: array of pages to release
702 * @nr: number of pages
703 * @cold: whether the pages are cache cold
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 *
Michal Hockoaabfb572014-10-09 15:28:52 -0700705 * Decrement the reference count on all the pages in @pages. If it
706 * fell to zero, remove the page from the LRU and free it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 */
Mel Gormanb745bc82014-06-04 16:10:22 -0700708void release_pages(struct page **pages, int nr, bool cold)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 int i;
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -0800711 LIST_HEAD(pages_to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct zone *zone = NULL;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700713 struct lruvec *lruvec;
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700714 unsigned long uninitialized_var(flags);
Michal Hockoaabfb572014-10-09 15:28:52 -0700715 unsigned int uninitialized_var(lock_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 for (i = 0; i < nr; i++) {
718 struct page *page = pages[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Michal Hockoaabfb572014-10-09 15:28:52 -0700720 /*
721 * Make sure the IRQ-safe lock-holding time does not get
722 * excessive with a continuous string of pages from the
723 * same zone. The lock is held only if zone != NULL.
724 */
725 if (zone && ++lock_batch == SWAP_CLUSTER_MAX) {
726 spin_unlock_irqrestore(&zone->lru_lock, flags);
727 zone = NULL;
728 }
729
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800730 page = compound_head(page);
Nick Pigginb5810032005-10-29 18:16:12 -0700731 if (!put_page_testzero(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 continue;
733
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -0800734 if (PageCompound(page)) {
735 if (zone) {
736 spin_unlock_irqrestore(&zone->lru_lock, flags);
737 zone = NULL;
738 }
739 __put_compound_page(page);
740 continue;
741 }
742
Nick Piggin46453a62006-03-22 00:07:58 -0800743 if (PageLRU(page)) {
744 struct zone *pagezone = page_zone(page);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700745
Nick Piggin46453a62006-03-22 00:07:58 -0800746 if (pagezone != zone) {
747 if (zone)
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700748 spin_unlock_irqrestore(&zone->lru_lock,
749 flags);
Michal Hockoaabfb572014-10-09 15:28:52 -0700750 lock_batch = 0;
Nick Piggin46453a62006-03-22 00:07:58 -0800751 zone = pagezone;
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700752 spin_lock_irqsave(&zone->lru_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700754
755 lruvec = mem_cgroup_page_lruvec(page, zone);
Sasha Levin309381fea2014-01-23 15:52:54 -0800756 VM_BUG_ON_PAGE(!PageLRU(page), page);
Nick Piggin67453912006-03-22 00:08:00 -0800757 __ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700758 del_page_from_lru_list(page, lruvec, page_off_lru(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Nick Piggin46453a62006-03-22 00:07:58 -0800760
Mel Gormanc53954a2013-07-03 15:02:34 -0700761 /* Clear Active bit in case of parallel mark_page_accessed */
Mel Gormane3741b52014-06-04 16:10:26 -0700762 __ClearPageActive(page);
Mel Gormanc53954a2013-07-03 15:02:34 -0700763
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -0800764 list_add(&page->lru, &pages_to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766 if (zone)
Hisashi Hifumi902aaed2007-10-16 01:24:52 -0700767 spin_unlock_irqrestore(&zone->lru_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Johannes Weiner747db952014-08-08 14:19:24 -0700769 mem_cgroup_uncharge_list(&pages_to_free);
Konstantin Khlebnikovcc598502012-01-10 15:07:04 -0800770 free_hot_cold_page_list(&pages_to_free, cold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
Miklos Szeredi0be85572010-10-27 15:34:46 -0700772EXPORT_SYMBOL(release_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774/*
775 * The pages which we're about to release may be in the deferred lru-addition
776 * queues. That would prevent them from really being freed right now. That's
777 * OK from a correctness point of view but is inefficient - those pages may be
778 * cache-warm and we want to give them back to the page allocator ASAP.
779 *
780 * So __pagevec_release() will drain those queues here. __pagevec_lru_add()
781 * and __pagevec_lru_add_active() call release_pages() directly to avoid
782 * mutual recursion.
783 */
784void __pagevec_release(struct pagevec *pvec)
785{
786 lru_add_drain();
787 release_pages(pvec->pages, pagevec_count(pvec), pvec->cold);
788 pagevec_reinit(pvec);
789}
Steve French7f285702005-11-01 10:22:55 -0800790EXPORT_SYMBOL(__pagevec_release);
791
Hugh Dickins12d27102012-01-12 17:19:52 -0800792#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800793/* used by __split_huge_page_refcount() */
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700794void lru_add_page_tail(struct page *page, struct page *page_tail,
Shaohua Li5bc7b8a2013-04-29 15:08:36 -0700795 struct lruvec *lruvec, struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800796{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800797 const int file = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800798
Sasha Levin309381fea2014-01-23 15:52:54 -0800799 VM_BUG_ON_PAGE(!PageHead(page), page);
800 VM_BUG_ON_PAGE(PageCompound(page_tail), page);
801 VM_BUG_ON_PAGE(PageLRU(page_tail), page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700802 VM_BUG_ON(NR_CPUS != 1 &&
803 !spin_is_locked(&lruvec_zone(lruvec)->lru_lock));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800804
Shaohua Li5bc7b8a2013-04-29 15:08:36 -0700805 if (!list)
806 SetPageLRU(page_tail);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800807
Hugh Dickins12d27102012-01-12 17:19:52 -0800808 if (likely(PageLRU(page)))
809 list_add_tail(&page_tail->lru, &page->lru);
Shaohua Li5bc7b8a2013-04-29 15:08:36 -0700810 else if (list) {
811 /* page reclaim is reclaiming a huge page */
812 get_page(page_tail);
813 list_add_tail(&page_tail->lru, list);
814 } else {
Hugh Dickins12d27102012-01-12 17:19:52 -0800815 struct list_head *list_head;
816 /*
817 * Head page has not yet been counted, as an hpage,
818 * so we must account for each subpage individually.
819 *
820 * Use the standard add function to put page_tail on the list,
821 * but then correct its position so they all end up in order.
822 */
Kirill A. Shutemove180cf82013-07-31 13:53:39 -0700823 add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
Hugh Dickins12d27102012-01-12 17:19:52 -0800824 list_head = page_tail->lru.prev;
825 list_move_tail(&page_tail->lru, list_head);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800826 }
Hugh Dickins75121022012-03-05 14:59:18 -0800827
828 if (!PageUnevictable(page))
Kirill A. Shutemove180cf82013-07-31 13:53:39 -0700829 update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800830}
Hugh Dickins12d27102012-01-12 17:19:52 -0800831#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800832
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700833static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec,
834 void *arg)
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700835{
Mel Gorman13f7f782013-07-03 15:02:28 -0700836 int file = page_is_file_cache(page);
837 int active = PageActive(page);
838 enum lru_list lru = page_lru(page);
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700839
Sasha Levin309381fea2014-01-23 15:52:54 -0800840 VM_BUG_ON_PAGE(PageLRU(page), page);
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700841
842 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700843 add_page_to_lru_list(page, lruvec, lru);
844 update_page_reclaim_stat(lruvec, file, active);
Mel Gorman24b7e582014-08-06 16:07:11 -0700845 trace_mm_lru_insertion(page, lru);
Shaohua Li3dd7ae82011-03-22 16:33:45 -0700846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 * Add the passed pages to the LRU, then drop the caller's refcount
850 * on them. Reinitialises the caller's pagevec.
851 */
Mel Gormana0b8cab32013-07-03 15:02:32 -0700852void __pagevec_lru_add(struct pagevec *pvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Mel Gormana0b8cab32013-07-03 15:02:32 -0700854 pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
Hugh Dickins5095ae832012-01-12 17:19:58 -0800856EXPORT_SYMBOL(__pagevec_lru_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858/**
Johannes Weiner0cd61442014-04-03 14:47:46 -0700859 * pagevec_lookup_entries - gang pagecache lookup
860 * @pvec: Where the resulting entries are placed
861 * @mapping: The address_space to search
862 * @start: The starting entry index
863 * @nr_entries: The maximum number of entries
864 * @indices: The cache indices corresponding to the entries in @pvec
865 *
866 * pagevec_lookup_entries() will search for and return a group of up
867 * to @nr_entries pages and shadow entries in the mapping. All
868 * entries are placed in @pvec. pagevec_lookup_entries() takes a
869 * reference against actual pages in @pvec.
870 *
871 * The search returns a group of mapping-contiguous entries with
872 * ascending indexes. There may be holes in the indices due to
873 * not-present entries.
874 *
875 * pagevec_lookup_entries() returns the number of entries which were
876 * found.
877 */
878unsigned pagevec_lookup_entries(struct pagevec *pvec,
879 struct address_space *mapping,
880 pgoff_t start, unsigned nr_pages,
881 pgoff_t *indices)
882{
883 pvec->nr = find_get_entries(mapping, start, nr_pages,
884 pvec->pages, indices);
885 return pagevec_count(pvec);
886}
887
888/**
889 * pagevec_remove_exceptionals - pagevec exceptionals pruning
890 * @pvec: The pagevec to prune
891 *
892 * pagevec_lookup_entries() fills both pages and exceptional radix
893 * tree entries into the pagevec. This function prunes all
894 * exceptionals from @pvec without leaving holes, so that it can be
895 * passed on to page-only pagevec operations.
896 */
897void pagevec_remove_exceptionals(struct pagevec *pvec)
898{
899 int i, j;
900
901 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
902 struct page *page = pvec->pages[i];
903 if (!radix_tree_exceptional_entry(page))
904 pvec->pages[j++] = page;
905 }
906 pvec->nr = j;
907}
908
909/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * pagevec_lookup - gang pagecache lookup
911 * @pvec: Where the resulting pages are placed
912 * @mapping: The address_space to search
913 * @start: The starting page index
914 * @nr_pages: The maximum number of pages
915 *
916 * pagevec_lookup() will search for and return a group of up to @nr_pages pages
917 * in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a
918 * reference against the pages in @pvec.
919 *
920 * The search returns a group of mapping-contiguous pages with ascending
921 * indexes. There may be holes in the indices due to not-present pages.
922 *
923 * pagevec_lookup() returns the number of pages which were found.
924 */
925unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
926 pgoff_t start, unsigned nr_pages)
927{
928 pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages);
929 return pagevec_count(pvec);
930}
Christoph Hellwig78539fd2006-01-11 20:47:41 +1100931EXPORT_SYMBOL(pagevec_lookup);
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
934 pgoff_t *index, int tag, unsigned nr_pages)
935{
936 pvec->nr = find_get_pages_tag(mapping, index, tag,
937 nr_pages, pvec->pages);
938 return pagevec_count(pvec);
939}
Steve French7f285702005-11-01 10:22:55 -0800940EXPORT_SYMBOL(pagevec_lookup_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942/*
943 * Perform any setup for the swap system
944 */
945void __init swap_setup(void)
946{
Jan Beulich44813742009-09-21 17:03:05 -0700947 unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700948#ifdef CONFIG_SWAP
Shaohua Li33806f02013-02-22 16:34:37 -0800949 int i;
950
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800951 for (i = 0; i < MAX_SWAPFILES; i++)
Shaohua Li33806f02013-02-22 16:34:37 -0800952 spin_lock_init(&swapper_spaces[i].tree_lock);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700953#endif
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /* Use a smaller cluster for small-memory machines */
956 if (megs < 16)
957 page_cluster = 2;
958 else
959 page_cluster = 3;
960 /*
961 * Right now other parts of the system means that we
962 * _really_ don't want to cluster much more
963 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}