1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/mm/swap.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* 9183ff22bSSimon Arlott * This file contains the default values for the operation of the 101da177e4SLinus Torvalds * Linux VM subsystem. Fine-tuning documentation can be found in 1157043247SMauro Carvalho Chehab * Documentation/admin-guide/sysctl/vm.rst. 121da177e4SLinus Torvalds * Started 18.12.91 131da177e4SLinus Torvalds * Swap aging added 23.2.95, Stephen Tweedie. 141da177e4SLinus Torvalds * Buffermem limits added 12.3.98, Rik van Riel. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/mm.h> 181da177e4SLinus Torvalds #include <linux/sched.h> 191da177e4SLinus Torvalds #include <linux/kernel_stat.h> 201da177e4SLinus Torvalds #include <linux/swap.h> 211da177e4SLinus Torvalds #include <linux/mman.h> 221da177e4SLinus Torvalds #include <linux/pagemap.h> 231da177e4SLinus Torvalds #include <linux/pagevec.h> 241da177e4SLinus Torvalds #include <linux/init.h> 25b95f1b31SPaul Gortmaker #include <linux/export.h> 261da177e4SLinus Torvalds #include <linux/mm_inline.h> 271da177e4SLinus Torvalds #include <linux/percpu_counter.h> 283565fce3SDan Williams #include <linux/memremap.h> 291da177e4SLinus Torvalds #include <linux/percpu.h> 301da177e4SLinus Torvalds #include <linux/cpu.h> 311da177e4SLinus Torvalds #include <linux/notifier.h> 32e0bf68ddSPeter Zijlstra #include <linux/backing-dev.h> 3366e1707bSBalbir Singh #include <linux/memcontrol.h> 345a0e3ad6STejun Heo #include <linux/gfp.h> 35a27bb332SKent Overstreet #include <linux/uio.h> 36822fc613SNaoya Horiguchi #include <linux/hugetlb.h> 3733c3fc71SVladimir Davydov #include <linux/page_idle.h> 38b01b2141SIngo Molnar #include <linux/local_lock.h> 391da177e4SLinus Torvalds 4064d6519dSLee Schermerhorn #include "internal.h" 4164d6519dSLee Schermerhorn 42c6286c98SMel Gorman #define CREATE_TRACE_POINTS 43c6286c98SMel Gorman #include <trace/events/pagemap.h> 44c6286c98SMel Gorman 451da177e4SLinus Torvalds /* How many pages do we try to swap or page in/out together? */ 461da177e4SLinus Torvalds int page_cluster; 471da177e4SLinus Torvalds 48b01b2141SIngo Molnar /* Protecting only lru_rotate.pvec which requires disabling interrupts */ 49b01b2141SIngo Molnar struct lru_rotate { 50b01b2141SIngo Molnar local_lock_t lock; 51b01b2141SIngo Molnar struct pagevec pvec; 52b01b2141SIngo Molnar }; 53b01b2141SIngo Molnar static DEFINE_PER_CPU(struct lru_rotate, lru_rotate) = { 54b01b2141SIngo Molnar .lock = INIT_LOCAL_LOCK(lock), 55b01b2141SIngo Molnar }; 56b01b2141SIngo Molnar 57b01b2141SIngo Molnar /* 58b01b2141SIngo Molnar * The following struct pagevec are grouped together because they are protected 59b01b2141SIngo Molnar * by disabling preemption (and interrupts remain enabled). 60b01b2141SIngo Molnar */ 61b01b2141SIngo Molnar struct lru_pvecs { 62b01b2141SIngo Molnar local_lock_t lock; 63b01b2141SIngo Molnar struct pagevec lru_add; 64b01b2141SIngo Molnar struct pagevec lru_deactivate_file; 65b01b2141SIngo Molnar struct pagevec lru_deactivate; 66b01b2141SIngo Molnar struct pagevec lru_lazyfree; 67a4a921aaSMing Li #ifdef CONFIG_SMP 68b01b2141SIngo Molnar struct pagevec activate_page; 69a4a921aaSMing Li #endif 70b01b2141SIngo Molnar }; 71b01b2141SIngo Molnar static DEFINE_PER_CPU(struct lru_pvecs, lru_pvecs) = { 72b01b2141SIngo Molnar .lock = INIT_LOCAL_LOCK(lock), 73b01b2141SIngo Molnar }; 74902aaed0SHisashi Hifumi 75b221385bSAdrian Bunk /* 76b221385bSAdrian Bunk * This path almost never happens for VM activity - pages are normally 77b221385bSAdrian Bunk * freed via pagevecs. But it gets used by networking. 78b221385bSAdrian Bunk */ 79920c7a5dSHarvey Harrison static void __page_cache_release(struct page *page) 80b221385bSAdrian Bunk { 81b221385bSAdrian Bunk if (PageLRU(page)) { 82fa9add64SHugh Dickins struct lruvec *lruvec; 83fa9add64SHugh Dickins unsigned long flags; 84b221385bSAdrian Bunk 85*6168d0daSAlex Shi lruvec = lock_page_lruvec_irqsave(page, &flags); 86309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 87b221385bSAdrian Bunk __ClearPageLRU(page); 88fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_off_lru(page)); 89*6168d0daSAlex Shi unlock_page_lruvec_irqrestore(lruvec, flags); 90b221385bSAdrian Bunk } 9162906027SNicholas Piggin __ClearPageWaiters(page); 9291807063SAndrea Arcangeli } 9391807063SAndrea Arcangeli 9491807063SAndrea Arcangeli static void __put_single_page(struct page *page) 9591807063SAndrea Arcangeli { 9691807063SAndrea Arcangeli __page_cache_release(page); 977ae88534SYang Shi mem_cgroup_uncharge(page); 982d4894b5SMel Gorman free_unref_page(page); 99b221385bSAdrian Bunk } 100b221385bSAdrian Bunk 10191807063SAndrea Arcangeli static void __put_compound_page(struct page *page) 10291807063SAndrea Arcangeli { 103822fc613SNaoya Horiguchi /* 104822fc613SNaoya Horiguchi * __page_cache_release() is supposed to be called for thp, not for 105822fc613SNaoya Horiguchi * hugetlb. This is because hugetlb page does never have PageLRU set 106822fc613SNaoya Horiguchi * (it's never listed to any LRU lists) and no memcg routines should 107822fc613SNaoya Horiguchi * be called for hugetlb (it has a separate hugetlb_cgroup.) 108822fc613SNaoya Horiguchi */ 109822fc613SNaoya Horiguchi if (!PageHuge(page)) 11091807063SAndrea Arcangeli __page_cache_release(page); 111ff45fc3cSMatthew Wilcox (Oracle) destroy_compound_page(page); 11291807063SAndrea Arcangeli } 11391807063SAndrea Arcangeli 114ddc58f27SKirill A. Shutemov void __put_page(struct page *page) 115c747ce79SJianyu Zhan { 11671389703SDan Williams if (is_zone_device_page(page)) { 11771389703SDan Williams put_dev_pagemap(page->pgmap); 11871389703SDan Williams 11971389703SDan Williams /* 12071389703SDan Williams * The page belongs to the device that created pgmap. Do 12171389703SDan Williams * not return it to page allocator. 12271389703SDan Williams */ 12371389703SDan Williams return; 12471389703SDan Williams } 12571389703SDan Williams 126ddc58f27SKirill A. Shutemov if (unlikely(PageCompound(page))) 12726296ad2SAndrew Morton __put_compound_page(page); 12826296ad2SAndrew Morton else 12926296ad2SAndrew Morton __put_single_page(page); 13026296ad2SAndrew Morton } 131ddc58f27SKirill A. Shutemov EXPORT_SYMBOL(__put_page); 13270b50f94SAndrea Arcangeli 1331d7ea732SAlexander Zarochentsev /** 1347682486bSRandy Dunlap * put_pages_list() - release a list of pages 1357682486bSRandy Dunlap * @pages: list of pages threaded on page->lru 1361d7ea732SAlexander Zarochentsev * 1371d7ea732SAlexander Zarochentsev * Release a list of pages which are strung together on page.lru. Currently 1381d7ea732SAlexander Zarochentsev * used by read_cache_pages() and related error recovery code. 1391d7ea732SAlexander Zarochentsev */ 1401d7ea732SAlexander Zarochentsev void put_pages_list(struct list_head *pages) 1411d7ea732SAlexander Zarochentsev { 1421d7ea732SAlexander Zarochentsev while (!list_empty(pages)) { 1431d7ea732SAlexander Zarochentsev struct page *victim; 1441d7ea732SAlexander Zarochentsev 145f86196eaSNikolay Borisov victim = lru_to_page(pages); 1461d7ea732SAlexander Zarochentsev list_del(&victim->lru); 14709cbfeafSKirill A. Shutemov put_page(victim); 1481d7ea732SAlexander Zarochentsev } 1491d7ea732SAlexander Zarochentsev } 1501d7ea732SAlexander Zarochentsev EXPORT_SYMBOL(put_pages_list); 1511d7ea732SAlexander Zarochentsev 15218022c5dSMel Gorman /* 15318022c5dSMel Gorman * get_kernel_pages() - pin kernel pages in memory 15418022c5dSMel Gorman * @kiov: An array of struct kvec structures 15518022c5dSMel Gorman * @nr_segs: number of segments to pin 15618022c5dSMel Gorman * @write: pinning for read/write, currently ignored 15718022c5dSMel Gorman * @pages: array that receives pointers to the pages pinned. 15818022c5dSMel Gorman * Should be at least nr_segs long. 15918022c5dSMel Gorman * 16018022c5dSMel Gorman * Returns number of pages pinned. This may be fewer than the number 16118022c5dSMel Gorman * requested. If nr_pages is 0 or negative, returns 0. If no pages 16218022c5dSMel Gorman * were pinned, returns -errno. Each page returned must be released 16318022c5dSMel Gorman * with a put_page() call when it is finished with. 16418022c5dSMel Gorman */ 16518022c5dSMel Gorman int get_kernel_pages(const struct kvec *kiov, int nr_segs, int write, 16618022c5dSMel Gorman struct page **pages) 16718022c5dSMel Gorman { 16818022c5dSMel Gorman int seg; 16918022c5dSMel Gorman 17018022c5dSMel Gorman for (seg = 0; seg < nr_segs; seg++) { 17118022c5dSMel Gorman if (WARN_ON(kiov[seg].iov_len != PAGE_SIZE)) 17218022c5dSMel Gorman return seg; 17318022c5dSMel Gorman 1745a178119SMel Gorman pages[seg] = kmap_to_page(kiov[seg].iov_base); 17509cbfeafSKirill A. Shutemov get_page(pages[seg]); 17618022c5dSMel Gorman } 17718022c5dSMel Gorman 17818022c5dSMel Gorman return seg; 17918022c5dSMel Gorman } 18018022c5dSMel Gorman EXPORT_SYMBOL_GPL(get_kernel_pages); 18118022c5dSMel Gorman 18218022c5dSMel Gorman /* 18318022c5dSMel Gorman * get_kernel_page() - pin a kernel page in memory 18418022c5dSMel Gorman * @start: starting kernel address 18518022c5dSMel Gorman * @write: pinning for read/write, currently ignored 18618022c5dSMel Gorman * @pages: array that receives pointer to the page pinned. 18718022c5dSMel Gorman * Must be at least nr_segs long. 18818022c5dSMel Gorman * 18918022c5dSMel Gorman * Returns 1 if page is pinned. If the page was not pinned, returns 19018022c5dSMel Gorman * -errno. The page returned must be released with a put_page() call 19118022c5dSMel Gorman * when it is finished with. 19218022c5dSMel Gorman */ 19318022c5dSMel Gorman int get_kernel_page(unsigned long start, int write, struct page **pages) 19418022c5dSMel Gorman { 19518022c5dSMel Gorman const struct kvec kiov = { 19618022c5dSMel Gorman .iov_base = (void *)start, 19718022c5dSMel Gorman .iov_len = PAGE_SIZE 19818022c5dSMel Gorman }; 19918022c5dSMel Gorman 20018022c5dSMel Gorman return get_kernel_pages(&kiov, 1, write, pages); 20118022c5dSMel Gorman } 20218022c5dSMel Gorman EXPORT_SYMBOL_GPL(get_kernel_page); 20318022c5dSMel Gorman 2043dd7ae8eSShaohua Li static void pagevec_lru_move_fn(struct pagevec *pvec, 205c7c7b80cSAlex Shi void (*move_fn)(struct page *page, struct lruvec *lruvec)) 206902aaed0SHisashi Hifumi { 207902aaed0SHisashi Hifumi int i; 208*6168d0daSAlex Shi struct lruvec *lruvec = NULL; 2093dd7ae8eSShaohua Li unsigned long flags = 0; 210902aaed0SHisashi Hifumi 211902aaed0SHisashi Hifumi for (i = 0; i < pagevec_count(pvec); i++) { 212902aaed0SHisashi Hifumi struct page *page = pvec->pages[i]; 213*6168d0daSAlex Shi struct lruvec *new_lruvec; 2143dd7ae8eSShaohua Li 215fc574c23SAlex Shi /* block memcg migration during page moving between lru */ 216fc574c23SAlex Shi if (!TestClearPageLRU(page)) 217fc574c23SAlex Shi continue; 218fc574c23SAlex Shi 219*6168d0daSAlex Shi new_lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page)); 220*6168d0daSAlex Shi if (lruvec != new_lruvec) { 221*6168d0daSAlex Shi if (lruvec) 222*6168d0daSAlex Shi unlock_page_lruvec_irqrestore(lruvec, flags); 223*6168d0daSAlex Shi lruvec = lock_page_lruvec_irqsave(page, &flags); 224*6168d0daSAlex Shi } 225*6168d0daSAlex Shi 226c7c7b80cSAlex Shi (*move_fn)(page, lruvec); 227fc574c23SAlex Shi 228fc574c23SAlex Shi SetPageLRU(page); 2293dd7ae8eSShaohua Li } 230*6168d0daSAlex Shi if (lruvec) 231*6168d0daSAlex Shi unlock_page_lruvec_irqrestore(lruvec, flags); 232c6f92f9fSMel Gorman release_pages(pvec->pages, pvec->nr); 2333dd7ae8eSShaohua Li pagevec_reinit(pvec); 2343dd7ae8eSShaohua Li } 2353dd7ae8eSShaohua Li 236c7c7b80cSAlex Shi static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec) 2373dd7ae8eSShaohua Li { 238fc574c23SAlex Shi if (!PageUnevictable(page)) { 239c55e8d03SJohannes Weiner del_page_from_lru_list(page, lruvec, page_lru(page)); 240c55e8d03SJohannes Weiner ClearPageActive(page); 241c55e8d03SJohannes Weiner add_page_to_lru_list_tail(page, lruvec, page_lru(page)); 242c7c7b80cSAlex Shi __count_vm_events(PGROTATED, thp_nr_pages(page)); 243902aaed0SHisashi Hifumi } 244902aaed0SHisashi Hifumi } 2453dd7ae8eSShaohua Li 2463dd7ae8eSShaohua Li /* 2471da177e4SLinus Torvalds * Writeback is about to end against a page which has been marked for immediate 2481da177e4SLinus Torvalds * reclaim. If it still appears to be reclaimable, move it to the tail of the 249902aaed0SHisashi Hifumi * inactive list. 250c7c7b80cSAlex Shi * 251c7c7b80cSAlex Shi * rotate_reclaimable_page() must disable IRQs, to prevent nasty races. 2521da177e4SLinus Torvalds */ 253ac6aadb2SMiklos Szeredi void rotate_reclaimable_page(struct page *page) 2541da177e4SLinus Torvalds { 255c55e8d03SJohannes Weiner if (!PageLocked(page) && !PageDirty(page) && 256894bc310SLee Schermerhorn !PageUnevictable(page) && PageLRU(page)) { 257902aaed0SHisashi Hifumi struct pagevec *pvec; 2581da177e4SLinus Torvalds unsigned long flags; 2591da177e4SLinus Torvalds 26009cbfeafSKirill A. Shutemov get_page(page); 261b01b2141SIngo Molnar local_lock_irqsave(&lru_rotate.lock, flags); 262b01b2141SIngo Molnar pvec = this_cpu_ptr(&lru_rotate.pvec); 2638f182270SLukasz Odzioba if (!pagevec_add(pvec, page) || PageCompound(page)) 264c7c7b80cSAlex Shi pagevec_lru_move_fn(pvec, pagevec_move_tail_fn); 265b01b2141SIngo Molnar local_unlock_irqrestore(&lru_rotate.lock, flags); 266ac6aadb2SMiklos Szeredi } 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds 26996f8bf4fSJohannes Weiner void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages) 2703e2f41f1SKOSAKI Motohiro { 2717cf111bcSJohannes Weiner do { 2727cf111bcSJohannes Weiner unsigned long lrusize; 2737cf111bcSJohannes Weiner 274*6168d0daSAlex Shi /* 275*6168d0daSAlex Shi * Hold lruvec->lru_lock is safe here, since 276*6168d0daSAlex Shi * 1) The pinned lruvec in reclaim, or 277*6168d0daSAlex Shi * 2) From a pre-LRU page during refault (which also holds the 278*6168d0daSAlex Shi * rcu lock, so would be safe even if the page was on the LRU 279*6168d0daSAlex Shi * and could move simultaneously to a new lruvec). 280*6168d0daSAlex Shi */ 281*6168d0daSAlex Shi spin_lock_irq(&lruvec->lru_lock); 2827cf111bcSJohannes Weiner /* Record cost event */ 28396f8bf4fSJohannes Weiner if (file) 28496f8bf4fSJohannes Weiner lruvec->file_cost += nr_pages; 2851431d4d1SJohannes Weiner else 28696f8bf4fSJohannes Weiner lruvec->anon_cost += nr_pages; 2877cf111bcSJohannes Weiner 2887cf111bcSJohannes Weiner /* 2897cf111bcSJohannes Weiner * Decay previous events 2907cf111bcSJohannes Weiner * 2917cf111bcSJohannes Weiner * Because workloads change over time (and to avoid 2927cf111bcSJohannes Weiner * overflow) we keep these statistics as a floating 2937cf111bcSJohannes Weiner * average, which ends up weighing recent refaults 2947cf111bcSJohannes Weiner * more than old ones. 2957cf111bcSJohannes Weiner */ 2967cf111bcSJohannes Weiner lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) + 2977cf111bcSJohannes Weiner lruvec_page_state(lruvec, NR_ACTIVE_ANON) + 2987cf111bcSJohannes Weiner lruvec_page_state(lruvec, NR_INACTIVE_FILE) + 2997cf111bcSJohannes Weiner lruvec_page_state(lruvec, NR_ACTIVE_FILE); 3007cf111bcSJohannes Weiner 3017cf111bcSJohannes Weiner if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) { 3027cf111bcSJohannes Weiner lruvec->file_cost /= 2; 3037cf111bcSJohannes Weiner lruvec->anon_cost /= 2; 3047cf111bcSJohannes Weiner } 305*6168d0daSAlex Shi spin_unlock_irq(&lruvec->lru_lock); 3067cf111bcSJohannes Weiner } while ((lruvec = parent_lruvec(lruvec))); 3073e2f41f1SKOSAKI Motohiro } 3083e2f41f1SKOSAKI Motohiro 30996f8bf4fSJohannes Weiner void lru_note_cost_page(struct page *page) 31096f8bf4fSJohannes Weiner { 31196f8bf4fSJohannes Weiner lru_note_cost(mem_cgroup_page_lruvec(page, page_pgdat(page)), 3126c357848SMatthew Wilcox (Oracle) page_is_file_lru(page), thp_nr_pages(page)); 31396f8bf4fSJohannes Weiner } 31496f8bf4fSJohannes Weiner 315c7c7b80cSAlex Shi static void __activate_page(struct page *page, struct lruvec *lruvec) 316744ed144SShaohua Li { 317fc574c23SAlex Shi if (!PageActive(page) && !PageUnevictable(page)) { 318744ed144SShaohua Li int lru = page_lru_base_type(page); 3196c357848SMatthew Wilcox (Oracle) int nr_pages = thp_nr_pages(page); 320744ed144SShaohua Li 321fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 322744ed144SShaohua Li SetPageActive(page); 323744ed144SShaohua Li lru += LRU_ACTIVE; 324fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 32524b7e581SMel Gorman trace_mm_lru_activate(page); 3267a608572SLinus Torvalds 32721e330fcSShakeel Butt __count_vm_events(PGACTIVATE, nr_pages); 32821e330fcSShakeel Butt __count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE, 32921e330fcSShakeel Butt nr_pages); 330744ed144SShaohua Li } 331eb709b0dSShaohua Li } 332eb709b0dSShaohua Li 333eb709b0dSShaohua Li #ifdef CONFIG_SMP 334eb709b0dSShaohua Li static void activate_page_drain(int cpu) 335eb709b0dSShaohua Li { 336b01b2141SIngo Molnar struct pagevec *pvec = &per_cpu(lru_pvecs.activate_page, cpu); 337eb709b0dSShaohua Li 338eb709b0dSShaohua Li if (pagevec_count(pvec)) 339c7c7b80cSAlex Shi pagevec_lru_move_fn(pvec, __activate_page); 340eb709b0dSShaohua Li } 341eb709b0dSShaohua Li 3425fbc4616SChris Metcalf static bool need_activate_page_drain(int cpu) 3435fbc4616SChris Metcalf { 344b01b2141SIngo Molnar return pagevec_count(&per_cpu(lru_pvecs.activate_page, cpu)) != 0; 3455fbc4616SChris Metcalf } 3465fbc4616SChris Metcalf 347cc2828b2SYu Zhao static void activate_page(struct page *page) 348eb709b0dSShaohua Li { 349800d8c63SKirill A. Shutemov page = compound_head(page); 350eb709b0dSShaohua Li if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) { 351b01b2141SIngo Molnar struct pagevec *pvec; 352eb709b0dSShaohua Li 353b01b2141SIngo Molnar local_lock(&lru_pvecs.lock); 354b01b2141SIngo Molnar pvec = this_cpu_ptr(&lru_pvecs.activate_page); 35509cbfeafSKirill A. Shutemov get_page(page); 3568f182270SLukasz Odzioba if (!pagevec_add(pvec, page) || PageCompound(page)) 357c7c7b80cSAlex Shi pagevec_lru_move_fn(pvec, __activate_page); 358b01b2141SIngo Molnar local_unlock(&lru_pvecs.lock); 359eb709b0dSShaohua Li } 360eb709b0dSShaohua Li } 361eb709b0dSShaohua Li 362eb709b0dSShaohua Li #else 363eb709b0dSShaohua Li static inline void activate_page_drain(int cpu) 364eb709b0dSShaohua Li { 365eb709b0dSShaohua Li } 366eb709b0dSShaohua Li 367cc2828b2SYu Zhao static void activate_page(struct page *page) 368eb709b0dSShaohua Li { 369*6168d0daSAlex Shi struct lruvec *lruvec; 370eb709b0dSShaohua Li 371800d8c63SKirill A. Shutemov page = compound_head(page); 372*6168d0daSAlex Shi if (TestClearPageLRU(page)) { 373*6168d0daSAlex Shi lruvec = lock_page_lruvec_irq(page); 374*6168d0daSAlex Shi __activate_page(page, lruvec); 375*6168d0daSAlex Shi unlock_page_lruvec_irq(lruvec); 376*6168d0daSAlex Shi SetPageLRU(page); 377*6168d0daSAlex Shi } 3781da177e4SLinus Torvalds } 379eb709b0dSShaohua Li #endif 3801da177e4SLinus Torvalds 381059285a2SMel Gorman static void __lru_cache_activate_page(struct page *page) 382059285a2SMel Gorman { 383b01b2141SIngo Molnar struct pagevec *pvec; 384059285a2SMel Gorman int i; 385059285a2SMel Gorman 386b01b2141SIngo Molnar local_lock(&lru_pvecs.lock); 387b01b2141SIngo Molnar pvec = this_cpu_ptr(&lru_pvecs.lru_add); 388b01b2141SIngo Molnar 389059285a2SMel Gorman /* 390059285a2SMel Gorman * Search backwards on the optimistic assumption that the page being 391059285a2SMel Gorman * activated has just been added to this pagevec. Note that only 392059285a2SMel Gorman * the local pagevec is examined as a !PageLRU page could be in the 393059285a2SMel Gorman * process of being released, reclaimed, migrated or on a remote 394059285a2SMel Gorman * pagevec that is currently being drained. Furthermore, marking 395059285a2SMel Gorman * a remote pagevec's page PageActive potentially hits a race where 396059285a2SMel Gorman * a page is marked PageActive just after it is added to the inactive 397059285a2SMel Gorman * list causing accounting errors and BUG_ON checks to trigger. 398059285a2SMel Gorman */ 399059285a2SMel Gorman for (i = pagevec_count(pvec) - 1; i >= 0; i--) { 400059285a2SMel Gorman struct page *pagevec_page = pvec->pages[i]; 401059285a2SMel Gorman 402059285a2SMel Gorman if (pagevec_page == page) { 403059285a2SMel Gorman SetPageActive(page); 404059285a2SMel Gorman break; 405059285a2SMel Gorman } 406059285a2SMel Gorman } 407059285a2SMel Gorman 408b01b2141SIngo Molnar local_unlock(&lru_pvecs.lock); 409059285a2SMel Gorman } 410059285a2SMel Gorman 4111da177e4SLinus Torvalds /* 4121da177e4SLinus Torvalds * Mark a page as having seen activity. 4131da177e4SLinus Torvalds * 4141da177e4SLinus Torvalds * inactive,unreferenced -> inactive,referenced 4151da177e4SLinus Torvalds * inactive,referenced -> active,unreferenced 4161da177e4SLinus Torvalds * active,unreferenced -> active,referenced 417eb39d618SHugh Dickins * 418eb39d618SHugh Dickins * When a newly allocated page is not yet visible, so safe for non-atomic ops, 419eb39d618SHugh Dickins * __SetPageReferenced(page) may be substituted for mark_page_accessed(page). 4201da177e4SLinus Torvalds */ 421920c7a5dSHarvey Harrison void mark_page_accessed(struct page *page) 4221da177e4SLinus Torvalds { 423e90309c9SKirill A. Shutemov page = compound_head(page); 424059285a2SMel Gorman 425a1100a74SFengguang Wu if (!PageReferenced(page)) { 426a1100a74SFengguang Wu SetPageReferenced(page); 427a1100a74SFengguang Wu } else if (PageUnevictable(page)) { 428a1100a74SFengguang Wu /* 429a1100a74SFengguang Wu * Unevictable pages are on the "LRU_UNEVICTABLE" list. But, 430a1100a74SFengguang Wu * this list is never rotated or maintained, so marking an 431a1100a74SFengguang Wu * evictable page accessed has no effect. 432a1100a74SFengguang Wu */ 433a1100a74SFengguang Wu } else if (!PageActive(page)) { 434059285a2SMel Gorman /* 435059285a2SMel Gorman * If the page is on the LRU, queue it for activation via 436b01b2141SIngo Molnar * lru_pvecs.activate_page. Otherwise, assume the page is on a 437059285a2SMel Gorman * pagevec, mark it active and it'll be moved to the active 438059285a2SMel Gorman * LRU on the next drain. 439059285a2SMel Gorman */ 440059285a2SMel Gorman if (PageLRU(page)) 4411da177e4SLinus Torvalds activate_page(page); 442059285a2SMel Gorman else 443059285a2SMel Gorman __lru_cache_activate_page(page); 4441da177e4SLinus Torvalds ClearPageReferenced(page); 445a528910eSJohannes Weiner workingset_activation(page); 4461da177e4SLinus Torvalds } 44733c3fc71SVladimir Davydov if (page_is_idle(page)) 44833c3fc71SVladimir Davydov clear_page_idle(page); 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds EXPORT_SYMBOL(mark_page_accessed); 4511da177e4SLinus Torvalds 452f04e9ebbSKOSAKI Motohiro /** 453c53954a0SMel Gorman * lru_cache_add - add a page to a page list 454f04e9ebbSKOSAKI Motohiro * @page: the page to be added to the LRU. 4552329d375SJianyu Zhan * 4562329d375SJianyu Zhan * Queue the page for addition to the LRU via pagevec. The decision on whether 4572329d375SJianyu Zhan * to add the page to the [in]active [file|anon] list is deferred until the 4582329d375SJianyu Zhan * pagevec is drained. This gives a chance for the caller of lru_cache_add() 4592329d375SJianyu Zhan * have the page added to the active list using mark_page_accessed(). 460f04e9ebbSKOSAKI Motohiro */ 461c53954a0SMel Gorman void lru_cache_add(struct page *page) 4621da177e4SLinus Torvalds { 4636058eaecSJohannes Weiner struct pagevec *pvec; 4646058eaecSJohannes Weiner 465309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page); 466309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 4676058eaecSJohannes Weiner 4686058eaecSJohannes Weiner get_page(page); 4696058eaecSJohannes Weiner local_lock(&lru_pvecs.lock); 4706058eaecSJohannes Weiner pvec = this_cpu_ptr(&lru_pvecs.lru_add); 4716058eaecSJohannes Weiner if (!pagevec_add(pvec, page) || PageCompound(page)) 4726058eaecSJohannes Weiner __pagevec_lru_add(pvec); 4736058eaecSJohannes Weiner local_unlock(&lru_pvecs.lock); 4741da177e4SLinus Torvalds } 4756058eaecSJohannes Weiner EXPORT_SYMBOL(lru_cache_add); 4761da177e4SLinus Torvalds 477894bc310SLee Schermerhorn /** 478b518154eSJoonsoo Kim * lru_cache_add_inactive_or_unevictable 47900501b53SJohannes Weiner * @page: the page to be added to LRU 48000501b53SJohannes Weiner * @vma: vma in which page is mapped for determining reclaimability 48100501b53SJohannes Weiner * 482b518154eSJoonsoo Kim * Place @page on the inactive or unevictable LRU list, depending on its 48312eab428SMiaohe Lin * evictability. 48400501b53SJohannes Weiner */ 485b518154eSJoonsoo Kim void lru_cache_add_inactive_or_unevictable(struct page *page, 48600501b53SJohannes Weiner struct vm_area_struct *vma) 48700501b53SJohannes Weiner { 488b518154eSJoonsoo Kim bool unevictable; 489b518154eSJoonsoo Kim 49000501b53SJohannes Weiner VM_BUG_ON_PAGE(PageLRU(page), page); 49100501b53SJohannes Weiner 492b518154eSJoonsoo Kim unevictable = (vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED; 493b518154eSJoonsoo Kim if (unlikely(unevictable) && !TestSetPageMlocked(page)) { 4940964730bSHugh Dickins int nr_pages = thp_nr_pages(page); 49500501b53SJohannes Weiner /* 49600501b53SJohannes Weiner * We use the irq-unsafe __mod_zone_page_stat because this 49700501b53SJohannes Weiner * counter is not modified from interrupt context, and the pte 49800501b53SJohannes Weiner * lock is held(spinlock), which implies preemption disabled. 49900501b53SJohannes Weiner */ 5000964730bSHugh Dickins __mod_zone_page_state(page_zone(page), NR_MLOCK, nr_pages); 5010964730bSHugh Dickins count_vm_events(UNEVICTABLE_PGMLOCKED, nr_pages); 50200501b53SJohannes Weiner } 5039c4e6b1aSShakeel Butt lru_cache_add(page); 50400501b53SJohannes Weiner } 50500501b53SJohannes Weiner 506902aaed0SHisashi Hifumi /* 50731560180SMinchan Kim * If the page can not be invalidated, it is moved to the 50831560180SMinchan Kim * inactive list to speed up its reclaim. It is moved to the 50931560180SMinchan Kim * head of the list, rather than the tail, to give the flusher 51031560180SMinchan Kim * threads some time to write it out, as this is much more 51131560180SMinchan Kim * effective than the single-page writeout from reclaim. 512278df9f4SMinchan Kim * 513278df9f4SMinchan Kim * If the page isn't page_mapped and dirty/writeback, the page 514278df9f4SMinchan Kim * could reclaim asap using PG_reclaim. 515278df9f4SMinchan Kim * 516278df9f4SMinchan Kim * 1. active, mapped page -> none 517278df9f4SMinchan Kim * 2. active, dirty/writeback page -> inactive, head, PG_reclaim 518278df9f4SMinchan Kim * 3. inactive, mapped page -> none 519278df9f4SMinchan Kim * 4. inactive, dirty/writeback page -> inactive, head, PG_reclaim 520278df9f4SMinchan Kim * 5. inactive, clean -> inactive, tail 521278df9f4SMinchan Kim * 6. Others -> none 522278df9f4SMinchan Kim * 523278df9f4SMinchan Kim * In 4, why it moves inactive's head, the VM expects the page would 524278df9f4SMinchan Kim * be write it out by flusher threads as this is much more effective 525278df9f4SMinchan Kim * than the single-page writeout from reclaim. 52631560180SMinchan Kim */ 527c7c7b80cSAlex Shi static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec) 52831560180SMinchan Kim { 529fbbb602eSJohannes Weiner int lru; 530278df9f4SMinchan Kim bool active; 5316c357848SMatthew Wilcox (Oracle) int nr_pages = thp_nr_pages(page); 53231560180SMinchan Kim 533bad49d9cSMinchan Kim if (PageUnevictable(page)) 534bad49d9cSMinchan Kim return; 535bad49d9cSMinchan Kim 53631560180SMinchan Kim /* Some processes are using the page */ 53731560180SMinchan Kim if (page_mapped(page)) 53831560180SMinchan Kim return; 53931560180SMinchan Kim 540278df9f4SMinchan Kim active = PageActive(page); 54131560180SMinchan Kim lru = page_lru_base_type(page); 542fa9add64SHugh Dickins 543fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru + active); 54431560180SMinchan Kim ClearPageActive(page); 54531560180SMinchan Kim ClearPageReferenced(page); 54631560180SMinchan Kim 547278df9f4SMinchan Kim if (PageWriteback(page) || PageDirty(page)) { 548278df9f4SMinchan Kim /* 549278df9f4SMinchan Kim * PG_reclaim could be raced with end_page_writeback 550278df9f4SMinchan Kim * It can make readahead confusing. But race window 551278df9f4SMinchan Kim * is _really_ small and it's non-critical problem. 552278df9f4SMinchan Kim */ 553e7a1aaf2SYu Zhao add_page_to_lru_list(page, lruvec, lru); 554278df9f4SMinchan Kim SetPageReclaim(page); 555278df9f4SMinchan Kim } else { 556278df9f4SMinchan Kim /* 557278df9f4SMinchan Kim * The page's writeback ends up during pagevec 558278df9f4SMinchan Kim * We moves tha page into tail of inactive. 559278df9f4SMinchan Kim */ 560e7a1aaf2SYu Zhao add_page_to_lru_list_tail(page, lruvec, lru); 5615d91f31fSShakeel Butt __count_vm_events(PGROTATED, nr_pages); 562278df9f4SMinchan Kim } 563278df9f4SMinchan Kim 56421e330fcSShakeel Butt if (active) { 5655d91f31fSShakeel Butt __count_vm_events(PGDEACTIVATE, nr_pages); 56621e330fcSShakeel Butt __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, 56721e330fcSShakeel Butt nr_pages); 56821e330fcSShakeel Butt } 56931560180SMinchan Kim } 57031560180SMinchan Kim 571c7c7b80cSAlex Shi static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec) 5729c276cc6SMinchan Kim { 573fc574c23SAlex Shi if (PageActive(page) && !PageUnevictable(page)) { 5749c276cc6SMinchan Kim int lru = page_lru_base_type(page); 5756c357848SMatthew Wilcox (Oracle) int nr_pages = thp_nr_pages(page); 5769c276cc6SMinchan Kim 5779c276cc6SMinchan Kim del_page_from_lru_list(page, lruvec, lru + LRU_ACTIVE); 5789c276cc6SMinchan Kim ClearPageActive(page); 5799c276cc6SMinchan Kim ClearPageReferenced(page); 5809c276cc6SMinchan Kim add_page_to_lru_list(page, lruvec, lru); 5819c276cc6SMinchan Kim 58221e330fcSShakeel Butt __count_vm_events(PGDEACTIVATE, nr_pages); 58321e330fcSShakeel Butt __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, 58421e330fcSShakeel Butt nr_pages); 5859c276cc6SMinchan Kim } 5869c276cc6SMinchan Kim } 58710853a03SMinchan Kim 588c7c7b80cSAlex Shi static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec) 58910853a03SMinchan Kim { 590fc574c23SAlex Shi if (PageAnon(page) && PageSwapBacked(page) && 59124c92eb7SShaohua Li !PageSwapCache(page) && !PageUnevictable(page)) { 592f7ad2a6cSShaohua Li bool active = PageActive(page); 5936c357848SMatthew Wilcox (Oracle) int nr_pages = thp_nr_pages(page); 59410853a03SMinchan Kim 595f7ad2a6cSShaohua Li del_page_from_lru_list(page, lruvec, 596f7ad2a6cSShaohua Li LRU_INACTIVE_ANON + active); 59710853a03SMinchan Kim ClearPageActive(page); 59810853a03SMinchan Kim ClearPageReferenced(page); 599f7ad2a6cSShaohua Li /* 6009de4f22aSHuang Ying * Lazyfree pages are clean anonymous pages. They have 6019de4f22aSHuang Ying * PG_swapbacked flag cleared, to distinguish them from normal 6029de4f22aSHuang Ying * anonymous pages 603f7ad2a6cSShaohua Li */ 604f7ad2a6cSShaohua Li ClearPageSwapBacked(page); 605f7ad2a6cSShaohua Li add_page_to_lru_list(page, lruvec, LRU_INACTIVE_FILE); 60610853a03SMinchan Kim 60721e330fcSShakeel Butt __count_vm_events(PGLAZYFREE, nr_pages); 60821e330fcSShakeel Butt __count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE, 60921e330fcSShakeel Butt nr_pages); 61010853a03SMinchan Kim } 61110853a03SMinchan Kim } 61210853a03SMinchan Kim 61331560180SMinchan Kim /* 614902aaed0SHisashi Hifumi * Drain pages out of the cpu's pagevecs. 615902aaed0SHisashi Hifumi * Either "cpu" is the current CPU, and preemption has already been 616902aaed0SHisashi Hifumi * disabled; or "cpu" is being hot-unplugged, and is already dead. 617902aaed0SHisashi Hifumi */ 618f0cb3c76SKonstantin Khlebnikov void lru_add_drain_cpu(int cpu) 6191da177e4SLinus Torvalds { 620b01b2141SIngo Molnar struct pagevec *pvec = &per_cpu(lru_pvecs.lru_add, cpu); 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds if (pagevec_count(pvec)) 623a0b8cab3SMel Gorman __pagevec_lru_add(pvec); 624902aaed0SHisashi Hifumi 625b01b2141SIngo Molnar pvec = &per_cpu(lru_rotate.pvec, cpu); 6267e0cc01eSQian Cai /* Disabling interrupts below acts as a compiler barrier. */ 6277e0cc01eSQian Cai if (data_race(pagevec_count(pvec))) { 628902aaed0SHisashi Hifumi unsigned long flags; 629902aaed0SHisashi Hifumi 630902aaed0SHisashi Hifumi /* No harm done if a racing interrupt already did this */ 631b01b2141SIngo Molnar local_lock_irqsave(&lru_rotate.lock, flags); 632c7c7b80cSAlex Shi pagevec_lru_move_fn(pvec, pagevec_move_tail_fn); 633b01b2141SIngo Molnar local_unlock_irqrestore(&lru_rotate.lock, flags); 634902aaed0SHisashi Hifumi } 63531560180SMinchan Kim 636b01b2141SIngo Molnar pvec = &per_cpu(lru_pvecs.lru_deactivate_file, cpu); 63731560180SMinchan Kim if (pagevec_count(pvec)) 638c7c7b80cSAlex Shi pagevec_lru_move_fn(pvec, lru_deactivate_file_fn); 639eb709b0dSShaohua Li 640b01b2141SIngo Molnar pvec = &per_cpu(lru_pvecs.lru_deactivate, cpu); 6419c276cc6SMinchan Kim if (pagevec_count(pvec)) 642c7c7b80cSAlex Shi pagevec_lru_move_fn(pvec, lru_deactivate_fn); 6439c276cc6SMinchan Kim 644b01b2141SIngo Molnar pvec = &per_cpu(lru_pvecs.lru_lazyfree, cpu); 64510853a03SMinchan Kim if (pagevec_count(pvec)) 646c7c7b80cSAlex Shi pagevec_lru_move_fn(pvec, lru_lazyfree_fn); 64710853a03SMinchan Kim 648eb709b0dSShaohua Li activate_page_drain(cpu); 64931560180SMinchan Kim } 65031560180SMinchan Kim 65131560180SMinchan Kim /** 652cc5993bdSMinchan Kim * deactivate_file_page - forcefully deactivate a file page 65331560180SMinchan Kim * @page: page to deactivate 65431560180SMinchan Kim * 65531560180SMinchan Kim * This function hints the VM that @page is a good reclaim candidate, 65631560180SMinchan Kim * for example if its invalidation fails due to the page being dirty 65731560180SMinchan Kim * or under writeback. 65831560180SMinchan Kim */ 659cc5993bdSMinchan Kim void deactivate_file_page(struct page *page) 66031560180SMinchan Kim { 661821ed6bbSMinchan Kim /* 662cc5993bdSMinchan Kim * In a workload with many unevictable page such as mprotect, 663cc5993bdSMinchan Kim * unevictable page deactivation for accelerating reclaim is pointless. 664821ed6bbSMinchan Kim */ 665821ed6bbSMinchan Kim if (PageUnevictable(page)) 666821ed6bbSMinchan Kim return; 667821ed6bbSMinchan Kim 66831560180SMinchan Kim if (likely(get_page_unless_zero(page))) { 669b01b2141SIngo Molnar struct pagevec *pvec; 670b01b2141SIngo Molnar 671b01b2141SIngo Molnar local_lock(&lru_pvecs.lock); 672b01b2141SIngo Molnar pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate_file); 67331560180SMinchan Kim 6748f182270SLukasz Odzioba if (!pagevec_add(pvec, page) || PageCompound(page)) 675c7c7b80cSAlex Shi pagevec_lru_move_fn(pvec, lru_deactivate_file_fn); 676b01b2141SIngo Molnar local_unlock(&lru_pvecs.lock); 67731560180SMinchan Kim } 67880bfed90SAndrew Morton } 67980bfed90SAndrew Morton 6809c276cc6SMinchan Kim /* 6819c276cc6SMinchan Kim * deactivate_page - deactivate a page 6829c276cc6SMinchan Kim * @page: page to deactivate 6839c276cc6SMinchan Kim * 6849c276cc6SMinchan Kim * deactivate_page() moves @page to the inactive list if @page was on the active 6859c276cc6SMinchan Kim * list and was not an unevictable page. This is done to accelerate the reclaim 6869c276cc6SMinchan Kim * of @page. 6879c276cc6SMinchan Kim */ 6889c276cc6SMinchan Kim void deactivate_page(struct page *page) 6899c276cc6SMinchan Kim { 6909c276cc6SMinchan Kim if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) { 691b01b2141SIngo Molnar struct pagevec *pvec; 6929c276cc6SMinchan Kim 693b01b2141SIngo Molnar local_lock(&lru_pvecs.lock); 694b01b2141SIngo Molnar pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate); 6959c276cc6SMinchan Kim get_page(page); 6969c276cc6SMinchan Kim if (!pagevec_add(pvec, page) || PageCompound(page)) 697c7c7b80cSAlex Shi pagevec_lru_move_fn(pvec, lru_deactivate_fn); 698b01b2141SIngo Molnar local_unlock(&lru_pvecs.lock); 6999c276cc6SMinchan Kim } 7009c276cc6SMinchan Kim } 7019c276cc6SMinchan Kim 70210853a03SMinchan Kim /** 703f7ad2a6cSShaohua Li * mark_page_lazyfree - make an anon page lazyfree 70410853a03SMinchan Kim * @page: page to deactivate 70510853a03SMinchan Kim * 706f7ad2a6cSShaohua Li * mark_page_lazyfree() moves @page to the inactive file list. 707f7ad2a6cSShaohua Li * This is done to accelerate the reclaim of @page. 70810853a03SMinchan Kim */ 709f7ad2a6cSShaohua Li void mark_page_lazyfree(struct page *page) 71010853a03SMinchan Kim { 711f7ad2a6cSShaohua Li if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) && 71224c92eb7SShaohua Li !PageSwapCache(page) && !PageUnevictable(page)) { 713b01b2141SIngo Molnar struct pagevec *pvec; 71410853a03SMinchan Kim 715b01b2141SIngo Molnar local_lock(&lru_pvecs.lock); 716b01b2141SIngo Molnar pvec = this_cpu_ptr(&lru_pvecs.lru_lazyfree); 71709cbfeafSKirill A. Shutemov get_page(page); 7188f182270SLukasz Odzioba if (!pagevec_add(pvec, page) || PageCompound(page)) 719c7c7b80cSAlex Shi pagevec_lru_move_fn(pvec, lru_lazyfree_fn); 720b01b2141SIngo Molnar local_unlock(&lru_pvecs.lock); 72110853a03SMinchan Kim } 72210853a03SMinchan Kim } 72310853a03SMinchan Kim 72480bfed90SAndrew Morton void lru_add_drain(void) 72580bfed90SAndrew Morton { 726b01b2141SIngo Molnar local_lock(&lru_pvecs.lock); 727b01b2141SIngo Molnar lru_add_drain_cpu(smp_processor_id()); 728b01b2141SIngo Molnar local_unlock(&lru_pvecs.lock); 729b01b2141SIngo Molnar } 730b01b2141SIngo Molnar 731b01b2141SIngo Molnar void lru_add_drain_cpu_zone(struct zone *zone) 732b01b2141SIngo Molnar { 733b01b2141SIngo Molnar local_lock(&lru_pvecs.lock); 734b01b2141SIngo Molnar lru_add_drain_cpu(smp_processor_id()); 735b01b2141SIngo Molnar drain_local_pages(zone); 736b01b2141SIngo Molnar local_unlock(&lru_pvecs.lock); 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 7396ea183d6SMichal Hocko #ifdef CONFIG_SMP 7406ea183d6SMichal Hocko 7416ea183d6SMichal Hocko static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work); 7426ea183d6SMichal Hocko 743c4028958SDavid Howells static void lru_add_drain_per_cpu(struct work_struct *dummy) 744053837fcSNick Piggin { 745053837fcSNick Piggin lru_add_drain(); 746053837fcSNick Piggin } 747053837fcSNick Piggin 7489852a721SMichal Hocko /* 7499852a721SMichal Hocko * Doesn't need any cpu hotplug locking because we do rely on per-cpu 7509852a721SMichal Hocko * kworkers being shut down before our page_alloc_cpu_dead callback is 7519852a721SMichal Hocko * executed on the offlined cpu. 7529852a721SMichal Hocko * Calling this function with cpu hotplug locks held can actually lead 7539852a721SMichal Hocko * to obscure indirect dependencies via WQ context. 7549852a721SMichal Hocko */ 7559852a721SMichal Hocko void lru_add_drain_all(void) 756053837fcSNick Piggin { 7576446a513SAhmed S. Darwish /* 7586446a513SAhmed S. Darwish * lru_drain_gen - Global pages generation number 7596446a513SAhmed S. Darwish * 7606446a513SAhmed S. Darwish * (A) Definition: global lru_drain_gen = x implies that all generations 7616446a513SAhmed S. Darwish * 0 < n <= x are already *scheduled* for draining. 7626446a513SAhmed S. Darwish * 7636446a513SAhmed S. Darwish * This is an optimization for the highly-contended use case where a 7646446a513SAhmed S. Darwish * user space workload keeps constantly generating a flow of pages for 7656446a513SAhmed S. Darwish * each CPU. 7666446a513SAhmed S. Darwish */ 7676446a513SAhmed S. Darwish static unsigned int lru_drain_gen; 7685fbc4616SChris Metcalf static struct cpumask has_work; 7696446a513SAhmed S. Darwish static DEFINE_MUTEX(lock); 7706446a513SAhmed S. Darwish unsigned cpu, this_gen; 7715fbc4616SChris Metcalf 772ce612879SMichal Hocko /* 773ce612879SMichal Hocko * Make sure nobody triggers this path before mm_percpu_wq is fully 774ce612879SMichal Hocko * initialized. 775ce612879SMichal Hocko */ 776ce612879SMichal Hocko if (WARN_ON(!mm_percpu_wq)) 777ce612879SMichal Hocko return; 778ce612879SMichal Hocko 7796446a513SAhmed S. Darwish /* 7806446a513SAhmed S. Darwish * Guarantee pagevec counter stores visible by this CPU are visible to 7816446a513SAhmed S. Darwish * other CPUs before loading the current drain generation. 7826446a513SAhmed S. Darwish */ 7836446a513SAhmed S. Darwish smp_mb(); 7846446a513SAhmed S. Darwish 7856446a513SAhmed S. Darwish /* 7866446a513SAhmed S. Darwish * (B) Locally cache global LRU draining generation number 7876446a513SAhmed S. Darwish * 7886446a513SAhmed S. Darwish * The read barrier ensures that the counter is loaded before the mutex 7896446a513SAhmed S. Darwish * is taken. It pairs with smp_mb() inside the mutex critical section 7906446a513SAhmed S. Darwish * at (D). 7916446a513SAhmed S. Darwish */ 7926446a513SAhmed S. Darwish this_gen = smp_load_acquire(&lru_drain_gen); 793eef1a429SKonstantin Khlebnikov 7945fbc4616SChris Metcalf mutex_lock(&lock); 795eef1a429SKonstantin Khlebnikov 796eef1a429SKonstantin Khlebnikov /* 7976446a513SAhmed S. Darwish * (C) Exit the draining operation if a newer generation, from another 7986446a513SAhmed S. Darwish * lru_add_drain_all(), was already scheduled for draining. Check (A). 799eef1a429SKonstantin Khlebnikov */ 8006446a513SAhmed S. Darwish if (unlikely(this_gen != lru_drain_gen)) 801eef1a429SKonstantin Khlebnikov goto done; 802eef1a429SKonstantin Khlebnikov 8036446a513SAhmed S. Darwish /* 8046446a513SAhmed S. Darwish * (D) Increment global generation number 8056446a513SAhmed S. Darwish * 8066446a513SAhmed S. Darwish * Pairs with smp_load_acquire() at (B), outside of the critical 8076446a513SAhmed S. Darwish * section. Use a full memory barrier to guarantee that the new global 8086446a513SAhmed S. Darwish * drain generation number is stored before loading pagevec counters. 8096446a513SAhmed S. Darwish * 8106446a513SAhmed S. Darwish * This pairing must be done here, before the for_each_online_cpu loop 8116446a513SAhmed S. Darwish * below which drains the page vectors. 8126446a513SAhmed S. Darwish * 8136446a513SAhmed S. Darwish * Let x, y, and z represent some system CPU numbers, where x < y < z. 8146446a513SAhmed S. Darwish * Assume CPU #z is is in the middle of the for_each_online_cpu loop 8156446a513SAhmed S. Darwish * below and has already reached CPU #y's per-cpu data. CPU #x comes 8166446a513SAhmed S. Darwish * along, adds some pages to its per-cpu vectors, then calls 8176446a513SAhmed S. Darwish * lru_add_drain_all(). 8186446a513SAhmed S. Darwish * 8196446a513SAhmed S. Darwish * If the paired barrier is done at any later step, e.g. after the 8206446a513SAhmed S. Darwish * loop, CPU #x will just exit at (C) and miss flushing out all of its 8216446a513SAhmed S. Darwish * added pages. 8226446a513SAhmed S. Darwish */ 8236446a513SAhmed S. Darwish WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1); 8246446a513SAhmed S. Darwish smp_mb(); 825eef1a429SKonstantin Khlebnikov 8265fbc4616SChris Metcalf cpumask_clear(&has_work); 8275fbc4616SChris Metcalf for_each_online_cpu(cpu) { 8285fbc4616SChris Metcalf struct work_struct *work = &per_cpu(lru_add_drain_work, cpu); 8295fbc4616SChris Metcalf 830b01b2141SIngo Molnar if (pagevec_count(&per_cpu(lru_pvecs.lru_add, cpu)) || 8317e0cc01eSQian Cai data_race(pagevec_count(&per_cpu(lru_rotate.pvec, cpu))) || 832b01b2141SIngo Molnar pagevec_count(&per_cpu(lru_pvecs.lru_deactivate_file, cpu)) || 833b01b2141SIngo Molnar pagevec_count(&per_cpu(lru_pvecs.lru_deactivate, cpu)) || 834b01b2141SIngo Molnar pagevec_count(&per_cpu(lru_pvecs.lru_lazyfree, cpu)) || 8355fbc4616SChris Metcalf need_activate_page_drain(cpu)) { 8365fbc4616SChris Metcalf INIT_WORK(work, lru_add_drain_per_cpu); 837ce612879SMichal Hocko queue_work_on(cpu, mm_percpu_wq, work); 8386446a513SAhmed S. Darwish __cpumask_set_cpu(cpu, &has_work); 8395fbc4616SChris Metcalf } 8405fbc4616SChris Metcalf } 8415fbc4616SChris Metcalf 8425fbc4616SChris Metcalf for_each_cpu(cpu, &has_work) 8435fbc4616SChris Metcalf flush_work(&per_cpu(lru_add_drain_work, cpu)); 8445fbc4616SChris Metcalf 845eef1a429SKonstantin Khlebnikov done: 8465fbc4616SChris Metcalf mutex_unlock(&lock); 847053837fcSNick Piggin } 8486ea183d6SMichal Hocko #else 8496ea183d6SMichal Hocko void lru_add_drain_all(void) 8506ea183d6SMichal Hocko { 8516ea183d6SMichal Hocko lru_add_drain(); 8526ea183d6SMichal Hocko } 8536446a513SAhmed S. Darwish #endif /* CONFIG_SMP */ 854053837fcSNick Piggin 855aabfb572SMichal Hocko /** 856ea1754a0SKirill A. Shutemov * release_pages - batched put_page() 857aabfb572SMichal Hocko * @pages: array of pages to release 858aabfb572SMichal Hocko * @nr: number of pages 8591da177e4SLinus Torvalds * 860aabfb572SMichal Hocko * Decrement the reference count on all the pages in @pages. If it 861aabfb572SMichal Hocko * fell to zero, remove the page from the LRU and free it. 8621da177e4SLinus Torvalds */ 863c6f92f9fSMel Gorman void release_pages(struct page **pages, int nr) 8641da177e4SLinus Torvalds { 8651da177e4SLinus Torvalds int i; 866cc59850eSKonstantin Khlebnikov LIST_HEAD(pages_to_free); 867*6168d0daSAlex Shi struct lruvec *lruvec = NULL; 8683f649ab7SKees Cook unsigned long flags; 8693f649ab7SKees Cook unsigned int lock_batch; 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds for (i = 0; i < nr; i++) { 8721da177e4SLinus Torvalds struct page *page = pages[i]; 8731da177e4SLinus Torvalds 874aabfb572SMichal Hocko /* 875aabfb572SMichal Hocko * Make sure the IRQ-safe lock-holding time does not get 876aabfb572SMichal Hocko * excessive with a continuous string of pages from the 877*6168d0daSAlex Shi * same lruvec. The lock is held only if lruvec != NULL. 878aabfb572SMichal Hocko */ 879*6168d0daSAlex Shi if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) { 880*6168d0daSAlex Shi unlock_page_lruvec_irqrestore(lruvec, flags); 881*6168d0daSAlex Shi lruvec = NULL; 882aabfb572SMichal Hocko } 883aabfb572SMichal Hocko 884a9b576f7SRalph Campbell page = compound_head(page); 8856fcb52a5SAaron Lu if (is_huge_zero_page(page)) 886aa88b68cSKirill A. Shutemov continue; 887aa88b68cSKirill A. Shutemov 888c5d6c45eSIra Weiny if (is_zone_device_page(page)) { 889*6168d0daSAlex Shi if (lruvec) { 890*6168d0daSAlex Shi unlock_page_lruvec_irqrestore(lruvec, flags); 891*6168d0daSAlex Shi lruvec = NULL; 892df6ad698SJérôme Glisse } 893c5d6c45eSIra Weiny /* 894c5d6c45eSIra Weiny * ZONE_DEVICE pages that return 'false' from 895a3e7bea0SMiaohe Lin * page_is_devmap_managed() do not require special 896c5d6c45eSIra Weiny * processing, and instead, expect a call to 897c5d6c45eSIra Weiny * put_page_testzero(). 898c5d6c45eSIra Weiny */ 89907d80269SJohn Hubbard if (page_is_devmap_managed(page)) { 90007d80269SJohn Hubbard put_devmap_managed_page(page); 901df6ad698SJérôme Glisse continue; 902df6ad698SJérôme Glisse } 90343fbdeb3SRalph Campbell if (put_page_testzero(page)) 90443fbdeb3SRalph Campbell put_dev_pagemap(page->pgmap); 90543fbdeb3SRalph Campbell continue; 90607d80269SJohn Hubbard } 907df6ad698SJérôme Glisse 908b5810039SNick Piggin if (!put_page_testzero(page)) 9091da177e4SLinus Torvalds continue; 9101da177e4SLinus Torvalds 911ddc58f27SKirill A. Shutemov if (PageCompound(page)) { 912*6168d0daSAlex Shi if (lruvec) { 913*6168d0daSAlex Shi unlock_page_lruvec_irqrestore(lruvec, flags); 914*6168d0daSAlex Shi lruvec = NULL; 915ddc58f27SKirill A. Shutemov } 916ddc58f27SKirill A. Shutemov __put_compound_page(page); 917ddc58f27SKirill A. Shutemov continue; 918ddc58f27SKirill A. Shutemov } 919ddc58f27SKirill A. Shutemov 92046453a6eSNick Piggin if (PageLRU(page)) { 921*6168d0daSAlex Shi struct lruvec *new_lruvec; 922894bc310SLee Schermerhorn 923*6168d0daSAlex Shi new_lruvec = mem_cgroup_page_lruvec(page, 924*6168d0daSAlex Shi page_pgdat(page)); 925*6168d0daSAlex Shi if (new_lruvec != lruvec) { 926*6168d0daSAlex Shi if (lruvec) 927*6168d0daSAlex Shi unlock_page_lruvec_irqrestore(lruvec, 928902aaed0SHisashi Hifumi flags); 929aabfb572SMichal Hocko lock_batch = 0; 930*6168d0daSAlex Shi lruvec = lock_page_lruvec_irqsave(page, &flags); 9311da177e4SLinus Torvalds } 932fa9add64SHugh Dickins 933309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 93467453911SNick Piggin __ClearPageLRU(page); 935fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, page_off_lru(page)); 93646453a6eSNick Piggin } 93746453a6eSNick Piggin 93862906027SNicholas Piggin __ClearPageWaiters(page); 939c53954a0SMel Gorman 940cc59850eSKonstantin Khlebnikov list_add(&page->lru, &pages_to_free); 9411da177e4SLinus Torvalds } 942*6168d0daSAlex Shi if (lruvec) 943*6168d0daSAlex Shi unlock_page_lruvec_irqrestore(lruvec, flags); 9441da177e4SLinus Torvalds 945747db954SJohannes Weiner mem_cgroup_uncharge_list(&pages_to_free); 9462d4894b5SMel Gorman free_unref_page_list(&pages_to_free); 9471da177e4SLinus Torvalds } 9480be8557bSMiklos Szeredi EXPORT_SYMBOL(release_pages); 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds /* 9511da177e4SLinus Torvalds * The pages which we're about to release may be in the deferred lru-addition 9521da177e4SLinus Torvalds * queues. That would prevent them from really being freed right now. That's 9531da177e4SLinus Torvalds * OK from a correctness point of view but is inefficient - those pages may be 9541da177e4SLinus Torvalds * cache-warm and we want to give them back to the page allocator ASAP. 9551da177e4SLinus Torvalds * 9561da177e4SLinus Torvalds * So __pagevec_release() will drain those queues here. __pagevec_lru_add() 9571da177e4SLinus Torvalds * and __pagevec_lru_add_active() call release_pages() directly to avoid 9581da177e4SLinus Torvalds * mutual recursion. 9591da177e4SLinus Torvalds */ 9601da177e4SLinus Torvalds void __pagevec_release(struct pagevec *pvec) 9611da177e4SLinus Torvalds { 9627f0b5fb9SMel Gorman if (!pvec->percpu_pvec_drained) { 9631da177e4SLinus Torvalds lru_add_drain(); 9647f0b5fb9SMel Gorman pvec->percpu_pvec_drained = true; 965d9ed0d08SMel Gorman } 966c6f92f9fSMel Gorman release_pages(pvec->pages, pagevec_count(pvec)); 9671da177e4SLinus Torvalds pagevec_reinit(pvec); 9681da177e4SLinus Torvalds } 9697f285701SSteve French EXPORT_SYMBOL(__pagevec_release); 9707f285701SSteve French 971c7c7b80cSAlex Shi static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec) 9723dd7ae8eSShaohua Li { 9739c4e6b1aSShakeel Butt enum lru_list lru; 9749c4e6b1aSShakeel Butt int was_unevictable = TestClearPageUnevictable(page); 9756c357848SMatthew Wilcox (Oracle) int nr_pages = thp_nr_pages(page); 9763dd7ae8eSShaohua Li 977309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 9783dd7ae8eSShaohua Li 9799c4e6b1aSShakeel Butt /* 9809c4e6b1aSShakeel Butt * Page becomes evictable in two ways: 981dae966dcSPeng Fan * 1) Within LRU lock [munlock_vma_page() and __munlock_pagevec()]. 9829c4e6b1aSShakeel Butt * 2) Before acquiring LRU lock to put the page to correct LRU and then 9839c4e6b1aSShakeel Butt * a) do PageLRU check with lock [check_move_unevictable_pages] 9849c4e6b1aSShakeel Butt * b) do PageLRU check before lock [clear_page_mlock] 9859c4e6b1aSShakeel Butt * 9869c4e6b1aSShakeel Butt * (1) & (2a) are ok as LRU lock will serialize them. For (2b), we need 9879c4e6b1aSShakeel Butt * following strict ordering: 9889c4e6b1aSShakeel Butt * 9899c4e6b1aSShakeel Butt * #0: __pagevec_lru_add_fn #1: clear_page_mlock 9909c4e6b1aSShakeel Butt * 9919c4e6b1aSShakeel Butt * SetPageLRU() TestClearPageMlocked() 9929c4e6b1aSShakeel Butt * smp_mb() // explicit ordering // above provides strict 9939c4e6b1aSShakeel Butt * // ordering 9949c4e6b1aSShakeel Butt * PageMlocked() PageLRU() 9959c4e6b1aSShakeel Butt * 9969c4e6b1aSShakeel Butt * 9979c4e6b1aSShakeel Butt * if '#1' does not observe setting of PG_lru by '#0' and fails 9989c4e6b1aSShakeel Butt * isolation, the explicit barrier will make sure that page_evictable 9999c4e6b1aSShakeel Butt * check will put the page in correct LRU. Without smp_mb(), SetPageLRU 10009c4e6b1aSShakeel Butt * can be reordered after PageMlocked check and can make '#1' to fail 10019c4e6b1aSShakeel Butt * the isolation of the page whose Mlocked bit is cleared (#0 is also 10029c4e6b1aSShakeel Butt * looking at the same page) and the evictable page will be stranded 10039c4e6b1aSShakeel Butt * in an unevictable LRU. 10049c4e6b1aSShakeel Butt */ 10059a9b6cceSYang Shi SetPageLRU(page); 10069a9b6cceSYang Shi smp_mb__after_atomic(); 10079c4e6b1aSShakeel Butt 10089c4e6b1aSShakeel Butt if (page_evictable(page)) { 10099c4e6b1aSShakeel Butt lru = page_lru(page); 10109c4e6b1aSShakeel Butt if (was_unevictable) 10115d91f31fSShakeel Butt __count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages); 10129c4e6b1aSShakeel Butt } else { 10139c4e6b1aSShakeel Butt lru = LRU_UNEVICTABLE; 10149c4e6b1aSShakeel Butt ClearPageActive(page); 10159c4e6b1aSShakeel Butt SetPageUnevictable(page); 10169c4e6b1aSShakeel Butt if (!was_unevictable) 10175d91f31fSShakeel Butt __count_vm_events(UNEVICTABLE_PGCULLED, nr_pages); 10189c4e6b1aSShakeel Butt } 10199c4e6b1aSShakeel Butt 1020fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 102124b7e581SMel Gorman trace_mm_lru_insertion(page, lru); 10223dd7ae8eSShaohua Li } 10233dd7ae8eSShaohua Li 10241da177e4SLinus Torvalds /* 10251da177e4SLinus Torvalds * Add the passed pages to the LRU, then drop the caller's refcount 10261da177e4SLinus Torvalds * on them. Reinitialises the caller's pagevec. 10271da177e4SLinus Torvalds */ 1028a0b8cab3SMel Gorman void __pagevec_lru_add(struct pagevec *pvec) 10291da177e4SLinus Torvalds { 1030fc574c23SAlex Shi int i; 1031*6168d0daSAlex Shi struct lruvec *lruvec = NULL; 1032fc574c23SAlex Shi unsigned long flags = 0; 1033fc574c23SAlex Shi 1034fc574c23SAlex Shi for (i = 0; i < pagevec_count(pvec); i++) { 1035fc574c23SAlex Shi struct page *page = pvec->pages[i]; 1036*6168d0daSAlex Shi struct lruvec *new_lruvec; 1037fc574c23SAlex Shi 1038*6168d0daSAlex Shi new_lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page)); 1039*6168d0daSAlex Shi if (lruvec != new_lruvec) { 1040*6168d0daSAlex Shi if (lruvec) 1041*6168d0daSAlex Shi unlock_page_lruvec_irqrestore(lruvec, flags); 1042*6168d0daSAlex Shi lruvec = lock_page_lruvec_irqsave(page, &flags); 1043fc574c23SAlex Shi } 1044fc574c23SAlex Shi 1045fc574c23SAlex Shi __pagevec_lru_add_fn(page, lruvec); 1046fc574c23SAlex Shi } 1047*6168d0daSAlex Shi if (lruvec) 1048*6168d0daSAlex Shi unlock_page_lruvec_irqrestore(lruvec, flags); 1049fc574c23SAlex Shi release_pages(pvec->pages, pvec->nr); 1050fc574c23SAlex Shi pagevec_reinit(pvec); 10511da177e4SLinus Torvalds } 1052f04e9ebbSKOSAKI Motohiro 10531da177e4SLinus Torvalds /** 10540cd6144aSJohannes Weiner * pagevec_lookup_entries - gang pagecache lookup 10550cd6144aSJohannes Weiner * @pvec: Where the resulting entries are placed 10560cd6144aSJohannes Weiner * @mapping: The address_space to search 10570cd6144aSJohannes Weiner * @start: The starting entry index 1058cb6f0f34SMike Rapoport * @nr_entries: The maximum number of pages 10590cd6144aSJohannes Weiner * @indices: The cache indices corresponding to the entries in @pvec 10600cd6144aSJohannes Weiner * 10610cd6144aSJohannes Weiner * pagevec_lookup_entries() will search for and return a group of up 1062f144c390SMike Rapoport * to @nr_pages pages and shadow entries in the mapping. All 10630cd6144aSJohannes Weiner * entries are placed in @pvec. pagevec_lookup_entries() takes a 10640cd6144aSJohannes Weiner * reference against actual pages in @pvec. 10650cd6144aSJohannes Weiner * 10660cd6144aSJohannes Weiner * The search returns a group of mapping-contiguous entries with 10670cd6144aSJohannes Weiner * ascending indexes. There may be holes in the indices due to 10680cd6144aSJohannes Weiner * not-present entries. 10690cd6144aSJohannes Weiner * 107071725ed1SHugh Dickins * Only one subpage of a Transparent Huge Page is returned in one call: 107171725ed1SHugh Dickins * allowing truncate_inode_pages_range() to evict the whole THP without 107271725ed1SHugh Dickins * cycling through a pagevec of extra references. 107371725ed1SHugh Dickins * 10740cd6144aSJohannes Weiner * pagevec_lookup_entries() returns the number of entries which were 10750cd6144aSJohannes Weiner * found. 10760cd6144aSJohannes Weiner */ 10770cd6144aSJohannes Weiner unsigned pagevec_lookup_entries(struct pagevec *pvec, 10780cd6144aSJohannes Weiner struct address_space *mapping, 1079e02a9f04SRandy Dunlap pgoff_t start, unsigned nr_entries, 10800cd6144aSJohannes Weiner pgoff_t *indices) 10810cd6144aSJohannes Weiner { 1082e02a9f04SRandy Dunlap pvec->nr = find_get_entries(mapping, start, nr_entries, 10830cd6144aSJohannes Weiner pvec->pages, indices); 10840cd6144aSJohannes Weiner return pagevec_count(pvec); 10850cd6144aSJohannes Weiner } 10860cd6144aSJohannes Weiner 10870cd6144aSJohannes Weiner /** 10880cd6144aSJohannes Weiner * pagevec_remove_exceptionals - pagevec exceptionals pruning 10890cd6144aSJohannes Weiner * @pvec: The pagevec to prune 10900cd6144aSJohannes Weiner * 10910cd6144aSJohannes Weiner * pagevec_lookup_entries() fills both pages and exceptional radix 10920cd6144aSJohannes Weiner * tree entries into the pagevec. This function prunes all 10930cd6144aSJohannes Weiner * exceptionals from @pvec without leaving holes, so that it can be 10940cd6144aSJohannes Weiner * passed on to page-only pagevec operations. 10950cd6144aSJohannes Weiner */ 10960cd6144aSJohannes Weiner void pagevec_remove_exceptionals(struct pagevec *pvec) 10970cd6144aSJohannes Weiner { 10980cd6144aSJohannes Weiner int i, j; 10990cd6144aSJohannes Weiner 11000cd6144aSJohannes Weiner for (i = 0, j = 0; i < pagevec_count(pvec); i++) { 11010cd6144aSJohannes Weiner struct page *page = pvec->pages[i]; 11023159f943SMatthew Wilcox if (!xa_is_value(page)) 11030cd6144aSJohannes Weiner pvec->pages[j++] = page; 11040cd6144aSJohannes Weiner } 11050cd6144aSJohannes Weiner pvec->nr = j; 11060cd6144aSJohannes Weiner } 11070cd6144aSJohannes Weiner 11080cd6144aSJohannes Weiner /** 1109b947cee4SJan Kara * pagevec_lookup_range - gang pagecache lookup 11101da177e4SLinus Torvalds * @pvec: Where the resulting pages are placed 11111da177e4SLinus Torvalds * @mapping: The address_space to search 11121da177e4SLinus Torvalds * @start: The starting page index 1113b947cee4SJan Kara * @end: The final page index 11141da177e4SLinus Torvalds * 1115e02a9f04SRandy Dunlap * pagevec_lookup_range() will search for & return a group of up to PAGEVEC_SIZE 1116b947cee4SJan Kara * pages in the mapping starting from index @start and upto index @end 1117b947cee4SJan Kara * (inclusive). The pages are placed in @pvec. pagevec_lookup() takes a 11181da177e4SLinus Torvalds * reference against the pages in @pvec. 11191da177e4SLinus Torvalds * 11201da177e4SLinus Torvalds * The search returns a group of mapping-contiguous pages with ascending 1121d72dc8a2SJan Kara * indexes. There may be holes in the indices due to not-present pages. We 1122d72dc8a2SJan Kara * also update @start to index the next page for the traversal. 11231da177e4SLinus Torvalds * 1124b947cee4SJan Kara * pagevec_lookup_range() returns the number of pages which were found. If this 1125e02a9f04SRandy Dunlap * number is smaller than PAGEVEC_SIZE, the end of specified range has been 1126b947cee4SJan Kara * reached. 11271da177e4SLinus Torvalds */ 1128b947cee4SJan Kara unsigned pagevec_lookup_range(struct pagevec *pvec, 1129397162ffSJan Kara struct address_space *mapping, pgoff_t *start, pgoff_t end) 11301da177e4SLinus Torvalds { 1131397162ffSJan Kara pvec->nr = find_get_pages_range(mapping, start, end, PAGEVEC_SIZE, 1132b947cee4SJan Kara pvec->pages); 11331da177e4SLinus Torvalds return pagevec_count(pvec); 11341da177e4SLinus Torvalds } 1135b947cee4SJan Kara EXPORT_SYMBOL(pagevec_lookup_range); 113678539fdfSChristoph Hellwig 113772b045aeSJan Kara unsigned pagevec_lookup_range_tag(struct pagevec *pvec, 113872b045aeSJan Kara struct address_space *mapping, pgoff_t *index, pgoff_t end, 113910bbd235SMatthew Wilcox xa_mark_t tag) 11401da177e4SLinus Torvalds { 114172b045aeSJan Kara pvec->nr = find_get_pages_range_tag(mapping, index, end, tag, 114267fd707fSJan Kara PAGEVEC_SIZE, pvec->pages); 11431da177e4SLinus Torvalds return pagevec_count(pvec); 11441da177e4SLinus Torvalds } 114572b045aeSJan Kara EXPORT_SYMBOL(pagevec_lookup_range_tag); 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds /* 11481da177e4SLinus Torvalds * Perform any setup for the swap system 11491da177e4SLinus Torvalds */ 11501da177e4SLinus Torvalds void __init swap_setup(void) 11511da177e4SLinus Torvalds { 1152ca79b0c2SArun KS unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT); 1153e0bf68ddSPeter Zijlstra 11541da177e4SLinus Torvalds /* Use a smaller cluster for small-memory machines */ 11551da177e4SLinus Torvalds if (megs < 16) 11561da177e4SLinus Torvalds page_cluster = 2; 11571da177e4SLinus Torvalds else 11581da177e4SLinus Torvalds page_cluster = 3; 11591da177e4SLinus Torvalds /* 11601da177e4SLinus Torvalds * Right now other parts of the system means that we 11611da177e4SLinus Torvalds * _really_ don't want to cluster much more 11621da177e4SLinus Torvalds */ 11631da177e4SLinus Torvalds } 116407d80269SJohn Hubbard 116507d80269SJohn Hubbard #ifdef CONFIG_DEV_PAGEMAP_OPS 116607d80269SJohn Hubbard void put_devmap_managed_page(struct page *page) 116707d80269SJohn Hubbard { 116807d80269SJohn Hubbard int count; 116907d80269SJohn Hubbard 117007d80269SJohn Hubbard if (WARN_ON_ONCE(!page_is_devmap_managed(page))) 117107d80269SJohn Hubbard return; 117207d80269SJohn Hubbard 117307d80269SJohn Hubbard count = page_ref_dec_return(page); 117407d80269SJohn Hubbard 117507d80269SJohn Hubbard /* 117607d80269SJohn Hubbard * devmap page refcounts are 1-based, rather than 0-based: if 117707d80269SJohn Hubbard * refcount is 1, then the page is free and the refcount is 117807d80269SJohn Hubbard * stable because nobody holds a reference on the page. 117907d80269SJohn Hubbard */ 118007d80269SJohn Hubbard if (count == 1) 118107d80269SJohn Hubbard free_devmap_managed_page(page); 118207d80269SJohn Hubbard else if (!count) 118307d80269SJohn Hubbard __put_page(page); 118407d80269SJohn Hubbard } 118507d80269SJohn Hubbard EXPORT_SYMBOL(put_devmap_managed_page); 118607d80269SJohn Hubbard #endif 1187