1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/mm/vmscan.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Swap reorganised 29.12.95, Stephen Tweedie. 81da177e4SLinus Torvalds * kswapd added: 7.1.96 sct 91da177e4SLinus Torvalds * Removed kswapd_ctl limits, and swap out as many pages as needed 101da177e4SLinus Torvalds * to bring the system back to freepages.high: 2.4.97, Rik van Riel. 111da177e4SLinus Torvalds * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com). 121da177e4SLinus Torvalds * Multiqueue VM started 5.8.00, Rik van Riel. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 15b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 16b1de0d13SMitchel Humpherys 171da177e4SLinus Torvalds #include <linux/mm.h> 185b3cc15aSIngo Molnar #include <linux/sched/mm.h> 191da177e4SLinus Torvalds #include <linux/module.h> 205a0e3ad6STejun Heo #include <linux/gfp.h> 211da177e4SLinus Torvalds #include <linux/kernel_stat.h> 221da177e4SLinus Torvalds #include <linux/swap.h> 231da177e4SLinus Torvalds #include <linux/pagemap.h> 241da177e4SLinus Torvalds #include <linux/init.h> 251da177e4SLinus Torvalds #include <linux/highmem.h> 2670ddf637SAnton Vorontsov #include <linux/vmpressure.h> 27e129b5c2SAndrew Morton #include <linux/vmstat.h> 281da177e4SLinus Torvalds #include <linux/file.h> 291da177e4SLinus Torvalds #include <linux/writeback.h> 301da177e4SLinus Torvalds #include <linux/blkdev.h> 311da177e4SLinus Torvalds #include <linux/buffer_head.h> /* for try_to_release_page(), 321da177e4SLinus Torvalds buffer_heads_over_limit */ 331da177e4SLinus Torvalds #include <linux/mm_inline.h> 341da177e4SLinus Torvalds #include <linux/backing-dev.h> 351da177e4SLinus Torvalds #include <linux/rmap.h> 361da177e4SLinus Torvalds #include <linux/topology.h> 371da177e4SLinus Torvalds #include <linux/cpu.h> 381da177e4SLinus Torvalds #include <linux/cpuset.h> 393e7d3449SMel Gorman #include <linux/compaction.h> 401da177e4SLinus Torvalds #include <linux/notifier.h> 411da177e4SLinus Torvalds #include <linux/rwsem.h> 42248a0301SRafael J. Wysocki #include <linux/delay.h> 433218ae14SYasunori Goto #include <linux/kthread.h> 447dfb7103SNigel Cunningham #include <linux/freezer.h> 4566e1707bSBalbir Singh #include <linux/memcontrol.h> 46873b4771SKeika Kobayashi #include <linux/delayacct.h> 47af936a16SLee Schermerhorn #include <linux/sysctl.h> 48929bea7cSKOSAKI Motohiro #include <linux/oom.h> 4964e3d12fSKuo-Hsin Yang #include <linux/pagevec.h> 50268bb0ceSLinus Torvalds #include <linux/prefetch.h> 51b1de0d13SMitchel Humpherys #include <linux/printk.h> 52f9fe48beSRoss Zwisler #include <linux/dax.h> 53eb414681SJohannes Weiner #include <linux/psi.h> 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #include <asm/tlbflush.h> 561da177e4SLinus Torvalds #include <asm/div64.h> 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds #include <linux/swapops.h> 59117aad1eSRafael Aquini #include <linux/balloon_compaction.h> 601da177e4SLinus Torvalds 610f8053a5SNick Piggin #include "internal.h" 620f8053a5SNick Piggin 6333906bc5SMel Gorman #define CREATE_TRACE_POINTS 6433906bc5SMel Gorman #include <trace/events/vmscan.h> 6533906bc5SMel Gorman 661da177e4SLinus Torvalds struct scan_control { 6722fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6822fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 6922fba335SKOSAKI Motohiro 70ee814fe2SJohannes Weiner /* 71ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 72ee814fe2SJohannes Weiner * are scanned. 73ee814fe2SJohannes Weiner */ 74ee814fe2SJohannes Weiner nodemask_t *nodemask; 759e3b2f8cSKonstantin Khlebnikov 765f53e762SKOSAKI Motohiro /* 77f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 78f16015fbSJohannes Weiner * primary target of this reclaim invocation. 79f16015fbSJohannes Weiner */ 80f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 8166e1707bSBalbir Singh 827cf111bcSJohannes Weiner /* 837cf111bcSJohannes Weiner * Scan pressure balancing between anon and file LRUs 847cf111bcSJohannes Weiner */ 857cf111bcSJohannes Weiner unsigned long anon_cost; 867cf111bcSJohannes Weiner unsigned long file_cost; 877cf111bcSJohannes Weiner 88b91ac374SJohannes Weiner /* Can active pages be deactivated as part of reclaim? */ 89b91ac374SJohannes Weiner #define DEACTIVATE_ANON 1 90b91ac374SJohannes Weiner #define DEACTIVATE_FILE 2 91b91ac374SJohannes Weiner unsigned int may_deactivate:2; 92b91ac374SJohannes Weiner unsigned int force_deactivate:1; 93b91ac374SJohannes Weiner unsigned int skipped_deactivate:1; 94b91ac374SJohannes Weiner 951276ad68SJohannes Weiner /* Writepage batching in laptop mode; RECLAIM_WRITE */ 96ee814fe2SJohannes Weiner unsigned int may_writepage:1; 97ee814fe2SJohannes Weiner 98ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 99ee814fe2SJohannes Weiner unsigned int may_unmap:1; 100ee814fe2SJohannes Weiner 101ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 102ee814fe2SJohannes Weiner unsigned int may_swap:1; 103ee814fe2SJohannes Weiner 104d6622f63SYisheng Xie /* 105d6622f63SYisheng Xie * Cgroups are not reclaimed below their configured memory.low, 106d6622f63SYisheng Xie * unless we threaten to OOM. If any cgroups are skipped due to 107d6622f63SYisheng Xie * memory.low and nothing was reclaimed, go back for memory.low. 108d6622f63SYisheng Xie */ 109d6622f63SYisheng Xie unsigned int memcg_low_reclaim:1; 110d6622f63SYisheng Xie unsigned int memcg_low_skipped:1; 111241994edSJohannes Weiner 112ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 113ee814fe2SJohannes Weiner 114ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 115ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 116ee814fe2SJohannes Weiner 117b91ac374SJohannes Weiner /* There is easily reclaimable cold cache in the current node */ 118b91ac374SJohannes Weiner unsigned int cache_trim_mode:1; 119b91ac374SJohannes Weiner 12053138ceaSJohannes Weiner /* The file pages on the current node are dangerously low */ 12153138ceaSJohannes Weiner unsigned int file_is_tiny:1; 12253138ceaSJohannes Weiner 123bb451fdfSGreg Thelen /* Allocation order */ 124bb451fdfSGreg Thelen s8 order; 125bb451fdfSGreg Thelen 126bb451fdfSGreg Thelen /* Scan (total_size >> priority) pages at once */ 127bb451fdfSGreg Thelen s8 priority; 128bb451fdfSGreg Thelen 129bb451fdfSGreg Thelen /* The highest zone to isolate pages for reclaim from */ 130bb451fdfSGreg Thelen s8 reclaim_idx; 131bb451fdfSGreg Thelen 132bb451fdfSGreg Thelen /* This context's GFP mask */ 133bb451fdfSGreg Thelen gfp_t gfp_mask; 134bb451fdfSGreg Thelen 135ee814fe2SJohannes Weiner /* Incremented by the number of inactive pages that were scanned */ 136ee814fe2SJohannes Weiner unsigned long nr_scanned; 137ee814fe2SJohannes Weiner 138ee814fe2SJohannes Weiner /* Number of pages freed so far during a call to shrink_zones() */ 139ee814fe2SJohannes Weiner unsigned long nr_reclaimed; 140d108c772SAndrey Ryabinin 141d108c772SAndrey Ryabinin struct { 142d108c772SAndrey Ryabinin unsigned int dirty; 143d108c772SAndrey Ryabinin unsigned int unqueued_dirty; 144d108c772SAndrey Ryabinin unsigned int congested; 145d108c772SAndrey Ryabinin unsigned int writeback; 146d108c772SAndrey Ryabinin unsigned int immediate; 147d108c772SAndrey Ryabinin unsigned int file_taken; 148d108c772SAndrey Ryabinin unsigned int taken; 149d108c772SAndrey Ryabinin } nr; 150e5ca8071SYafang Shao 151e5ca8071SYafang Shao /* for recording the reclaimed slab by now */ 152e5ca8071SYafang Shao struct reclaim_state reclaim_state; 1531da177e4SLinus Torvalds }; 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1561da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1571da177e4SLinus Torvalds do { \ 1581da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1591da177e4SLinus Torvalds struct page *prev; \ 1601da177e4SLinus Torvalds \ 1611da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1621da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1631da177e4SLinus Torvalds } \ 1641da177e4SLinus Torvalds } while (0) 1651da177e4SLinus Torvalds #else 1661da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1671da177e4SLinus Torvalds #endif 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds /* 170c843966cSJohannes Weiner * From 0 .. 200. Higher means more swappy. 1711da177e4SLinus Torvalds */ 1721da177e4SLinus Torvalds int vm_swappiness = 60; 1731da177e4SLinus Torvalds 1740a432dcbSYang Shi static void set_task_reclaim_state(struct task_struct *task, 1750a432dcbSYang Shi struct reclaim_state *rs) 1760a432dcbSYang Shi { 1770a432dcbSYang Shi /* Check for an overwrite */ 1780a432dcbSYang Shi WARN_ON_ONCE(rs && task->reclaim_state); 1790a432dcbSYang Shi 1800a432dcbSYang Shi /* Check for the nulling of an already-nulled member */ 1810a432dcbSYang Shi WARN_ON_ONCE(!rs && !task->reclaim_state); 1820a432dcbSYang Shi 1830a432dcbSYang Shi task->reclaim_state = rs; 1840a432dcbSYang Shi } 1850a432dcbSYang Shi 1861da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1871da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1881da177e4SLinus Torvalds 1890a432dcbSYang Shi #ifdef CONFIG_MEMCG 1907e010df5SKirill Tkhai /* 1917e010df5SKirill Tkhai * We allow subsystems to populate their shrinker-related 1927e010df5SKirill Tkhai * LRU lists before register_shrinker_prepared() is called 1937e010df5SKirill Tkhai * for the shrinker, since we don't want to impose 1947e010df5SKirill Tkhai * restrictions on their internal registration order. 1957e010df5SKirill Tkhai * In this case shrink_slab_memcg() may find corresponding 1967e010df5SKirill Tkhai * bit is set in the shrinkers map. 1977e010df5SKirill Tkhai * 1987e010df5SKirill Tkhai * This value is used by the function to detect registering 1997e010df5SKirill Tkhai * shrinkers and to skip do_shrink_slab() calls for them. 2007e010df5SKirill Tkhai */ 2017e010df5SKirill Tkhai #define SHRINKER_REGISTERING ((struct shrinker *)~0UL) 2027e010df5SKirill Tkhai 203b4c2b231SKirill Tkhai static DEFINE_IDR(shrinker_idr); 204b4c2b231SKirill Tkhai static int shrinker_nr_max; 205b4c2b231SKirill Tkhai 206b4c2b231SKirill Tkhai static int prealloc_memcg_shrinker(struct shrinker *shrinker) 207b4c2b231SKirill Tkhai { 208b4c2b231SKirill Tkhai int id, ret = -ENOMEM; 209b4c2b231SKirill Tkhai 210b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 211b4c2b231SKirill Tkhai /* This may call shrinker, so it must use down_read_trylock() */ 2127e010df5SKirill Tkhai id = idr_alloc(&shrinker_idr, SHRINKER_REGISTERING, 0, 0, GFP_KERNEL); 213b4c2b231SKirill Tkhai if (id < 0) 214b4c2b231SKirill Tkhai goto unlock; 215b4c2b231SKirill Tkhai 2160a4465d3SKirill Tkhai if (id >= shrinker_nr_max) { 2170a4465d3SKirill Tkhai if (memcg_expand_shrinker_maps(id)) { 2180a4465d3SKirill Tkhai idr_remove(&shrinker_idr, id); 2190a4465d3SKirill Tkhai goto unlock; 2200a4465d3SKirill Tkhai } 2210a4465d3SKirill Tkhai 222b4c2b231SKirill Tkhai shrinker_nr_max = id + 1; 2230a4465d3SKirill Tkhai } 224b4c2b231SKirill Tkhai shrinker->id = id; 225b4c2b231SKirill Tkhai ret = 0; 226b4c2b231SKirill Tkhai unlock: 227b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 228b4c2b231SKirill Tkhai return ret; 229b4c2b231SKirill Tkhai } 230b4c2b231SKirill Tkhai 231b4c2b231SKirill Tkhai static void unregister_memcg_shrinker(struct shrinker *shrinker) 232b4c2b231SKirill Tkhai { 233b4c2b231SKirill Tkhai int id = shrinker->id; 234b4c2b231SKirill Tkhai 235b4c2b231SKirill Tkhai BUG_ON(id < 0); 236b4c2b231SKirill Tkhai 237b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 238b4c2b231SKirill Tkhai idr_remove(&shrinker_idr, id); 239b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 240b4c2b231SKirill Tkhai } 241b4c2b231SKirill Tkhai 242b5ead35eSJohannes Weiner static bool cgroup_reclaim(struct scan_control *sc) 24389b5fae5SJohannes Weiner { 244b5ead35eSJohannes Weiner return sc->target_mem_cgroup; 24589b5fae5SJohannes Weiner } 24697c9341fSTejun Heo 24797c9341fSTejun Heo /** 248b5ead35eSJohannes Weiner * writeback_throttling_sane - is the usual dirty throttling mechanism available? 24997c9341fSTejun Heo * @sc: scan_control in question 25097c9341fSTejun Heo * 25197c9341fSTejun Heo * The normal page dirty throttling mechanism in balance_dirty_pages() is 25297c9341fSTejun Heo * completely broken with the legacy memcg and direct stalling in 25397c9341fSTejun Heo * shrink_page_list() is used for throttling instead, which lacks all the 25497c9341fSTejun Heo * niceties such as fairness, adaptive pausing, bandwidth proportional 25597c9341fSTejun Heo * allocation and configurability. 25697c9341fSTejun Heo * 25797c9341fSTejun Heo * This function tests whether the vmscan currently in progress can assume 25897c9341fSTejun Heo * that the normal dirty throttling mechanism is operational. 25997c9341fSTejun Heo */ 260b5ead35eSJohannes Weiner static bool writeback_throttling_sane(struct scan_control *sc) 26197c9341fSTejun Heo { 262b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 26397c9341fSTejun Heo return true; 26497c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 26569234aceSLinus Torvalds if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 26697c9341fSTejun Heo return true; 26797c9341fSTejun Heo #endif 26897c9341fSTejun Heo return false; 26997c9341fSTejun Heo } 27091a45470SKAMEZAWA Hiroyuki #else 2710a432dcbSYang Shi static int prealloc_memcg_shrinker(struct shrinker *shrinker) 2720a432dcbSYang Shi { 2730a432dcbSYang Shi return 0; 2740a432dcbSYang Shi } 2750a432dcbSYang Shi 2760a432dcbSYang Shi static void unregister_memcg_shrinker(struct shrinker *shrinker) 2770a432dcbSYang Shi { 2780a432dcbSYang Shi } 2790a432dcbSYang Shi 280b5ead35eSJohannes Weiner static bool cgroup_reclaim(struct scan_control *sc) 28189b5fae5SJohannes Weiner { 282b5ead35eSJohannes Weiner return false; 28389b5fae5SJohannes Weiner } 28497c9341fSTejun Heo 285b5ead35eSJohannes Weiner static bool writeback_throttling_sane(struct scan_control *sc) 28697c9341fSTejun Heo { 28797c9341fSTejun Heo return true; 28897c9341fSTejun Heo } 28991a45470SKAMEZAWA Hiroyuki #endif 29091a45470SKAMEZAWA Hiroyuki 2915a1c84b4SMel Gorman /* 2925a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 2935a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 2945a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 2955a1c84b4SMel Gorman */ 2965a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 2975a1c84b4SMel Gorman { 2985a1c84b4SMel Gorman unsigned long nr; 2995a1c84b4SMel Gorman 3005a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 3015a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 3025a1c84b4SMel Gorman if (get_nr_swap_pages() > 0) 3035a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 3045a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 3055a1c84b4SMel Gorman 3065a1c84b4SMel Gorman return nr; 3075a1c84b4SMel Gorman } 3085a1c84b4SMel Gorman 309fd538803SMichal Hocko /** 310fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 311fd538803SMichal Hocko * @lruvec: lru vector 312fd538803SMichal Hocko * @lru: lru to use 313fd538803SMichal Hocko * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 314fd538803SMichal Hocko */ 315fd538803SMichal Hocko unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx) 316c9f299d9SKOSAKI Motohiro { 317de3b0150SJohannes Weiner unsigned long size = 0; 318fd538803SMichal Hocko int zid; 319a3d8e054SKOSAKI Motohiro 320de3b0150SJohannes Weiner for (zid = 0; zid <= zone_idx && zid < MAX_NR_ZONES; zid++) { 321fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 322fd538803SMichal Hocko 323fd538803SMichal Hocko if (!managed_zone(zone)) 324fd538803SMichal Hocko continue; 325fd538803SMichal Hocko 326fd538803SMichal Hocko if (!mem_cgroup_disabled()) 327de3b0150SJohannes Weiner size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 328fd538803SMichal Hocko else 329de3b0150SJohannes Weiner size += zone_page_state(zone, NR_ZONE_LRU_BASE + lru); 330c9f299d9SKOSAKI Motohiro } 331de3b0150SJohannes Weiner return size; 332b4536f0cSMichal Hocko } 333b4536f0cSMichal Hocko 3341da177e4SLinus Torvalds /* 3351d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 3361da177e4SLinus Torvalds */ 3378e04944fSTetsuo Handa int prealloc_shrinker(struct shrinker *shrinker) 3381da177e4SLinus Torvalds { 339b9726c26SAlexey Dobriyan unsigned int size = sizeof(*shrinker->nr_deferred); 3401d3d4437SGlauber Costa 3411d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 3421d3d4437SGlauber Costa size *= nr_node_ids; 3431d3d4437SGlauber Costa 3441d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 3451d3d4437SGlauber Costa if (!shrinker->nr_deferred) 3461d3d4437SGlauber Costa return -ENOMEM; 347b4c2b231SKirill Tkhai 348b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 349b4c2b231SKirill Tkhai if (prealloc_memcg_shrinker(shrinker)) 350b4c2b231SKirill Tkhai goto free_deferred; 351b4c2b231SKirill Tkhai } 352b4c2b231SKirill Tkhai 3538e04944fSTetsuo Handa return 0; 354b4c2b231SKirill Tkhai 355b4c2b231SKirill Tkhai free_deferred: 356b4c2b231SKirill Tkhai kfree(shrinker->nr_deferred); 357b4c2b231SKirill Tkhai shrinker->nr_deferred = NULL; 358b4c2b231SKirill Tkhai return -ENOMEM; 3598e04944fSTetsuo Handa } 3601d3d4437SGlauber Costa 3618e04944fSTetsuo Handa void free_prealloced_shrinker(struct shrinker *shrinker) 3628e04944fSTetsuo Handa { 363b4c2b231SKirill Tkhai if (!shrinker->nr_deferred) 364b4c2b231SKirill Tkhai return; 365b4c2b231SKirill Tkhai 366b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) 367b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 368b4c2b231SKirill Tkhai 3698e04944fSTetsuo Handa kfree(shrinker->nr_deferred); 3708e04944fSTetsuo Handa shrinker->nr_deferred = NULL; 3718e04944fSTetsuo Handa } 3728e04944fSTetsuo Handa 3738e04944fSTetsuo Handa void register_shrinker_prepared(struct shrinker *shrinker) 3748e04944fSTetsuo Handa { 3751da177e4SLinus Torvalds down_write(&shrinker_rwsem); 3761da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 37742a9a53bSYang Shi #ifdef CONFIG_MEMCG 3788df4a44cSKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) 3797e010df5SKirill Tkhai idr_replace(&shrinker_idr, shrinker, shrinker->id); 3807e010df5SKirill Tkhai #endif 3811da177e4SLinus Torvalds up_write(&shrinker_rwsem); 3828e04944fSTetsuo Handa } 3838e04944fSTetsuo Handa 3848e04944fSTetsuo Handa int register_shrinker(struct shrinker *shrinker) 3858e04944fSTetsuo Handa { 3868e04944fSTetsuo Handa int err = prealloc_shrinker(shrinker); 3878e04944fSTetsuo Handa 3888e04944fSTetsuo Handa if (err) 3898e04944fSTetsuo Handa return err; 3908e04944fSTetsuo Handa register_shrinker_prepared(shrinker); 3911d3d4437SGlauber Costa return 0; 3921da177e4SLinus Torvalds } 3938e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds /* 3961da177e4SLinus Torvalds * Remove one 3971da177e4SLinus Torvalds */ 3988e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 3991da177e4SLinus Torvalds { 400bb422a73STetsuo Handa if (!shrinker->nr_deferred) 401bb422a73STetsuo Handa return; 402b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) 403b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 4041da177e4SLinus Torvalds down_write(&shrinker_rwsem); 4051da177e4SLinus Torvalds list_del(&shrinker->list); 4061da177e4SLinus Torvalds up_write(&shrinker_rwsem); 407ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 408bb422a73STetsuo Handa shrinker->nr_deferred = NULL; 4091da177e4SLinus Torvalds } 4108e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds #define SHRINK_BATCH 128 4131d3d4437SGlauber Costa 414cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 4159092c71bSJosef Bacik struct shrinker *shrinker, int priority) 4161da177e4SLinus Torvalds { 41724f7c6b9SDave Chinner unsigned long freed = 0; 4181da177e4SLinus Torvalds unsigned long long delta; 419635697c6SKonstantin Khlebnikov long total_scan; 420d5bc5fd3SVladimir Davydov long freeable; 421acf92b48SDave Chinner long nr; 422acf92b48SDave Chinner long new_nr; 4231d3d4437SGlauber Costa int nid = shrinkctl->nid; 424e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 425e9299f50SDave Chinner : SHRINK_BATCH; 4265f33a080SShaohua Li long scanned = 0, next_deferred; 4271da177e4SLinus Torvalds 428ac7fb3adSKirill Tkhai if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 429ac7fb3adSKirill Tkhai nid = 0; 430ac7fb3adSKirill Tkhai 431d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 4329b996468SKirill Tkhai if (freeable == 0 || freeable == SHRINK_EMPTY) 4339b996468SKirill Tkhai return freeable; 434635697c6SKonstantin Khlebnikov 435acf92b48SDave Chinner /* 436acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 437acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 438acf92b48SDave Chinner * don't also do this scanning work. 439acf92b48SDave Chinner */ 4401d3d4437SGlauber Costa nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 441acf92b48SDave Chinner 442acf92b48SDave Chinner total_scan = nr; 4434b85afbdSJohannes Weiner if (shrinker->seeks) { 4449092c71bSJosef Bacik delta = freeable >> priority; 4459092c71bSJosef Bacik delta *= 4; 4469092c71bSJosef Bacik do_div(delta, shrinker->seeks); 4474b85afbdSJohannes Weiner } else { 4484b85afbdSJohannes Weiner /* 4494b85afbdSJohannes Weiner * These objects don't require any IO to create. Trim 4504b85afbdSJohannes Weiner * them aggressively under memory pressure to keep 4514b85afbdSJohannes Weiner * them from causing refetches in the IO caches. 4524b85afbdSJohannes Weiner */ 4534b85afbdSJohannes Weiner delta = freeable / 2; 4544b85afbdSJohannes Weiner } 455172b06c3SRoman Gushchin 456acf92b48SDave Chinner total_scan += delta; 457acf92b48SDave Chinner if (total_scan < 0) { 458d75f773cSSakari Ailus pr_err("shrink_slab: %pS negative objects to delete nr=%ld\n", 459a0b02131SDave Chinner shrinker->scan_objects, total_scan); 460d5bc5fd3SVladimir Davydov total_scan = freeable; 4615f33a080SShaohua Li next_deferred = nr; 4625f33a080SShaohua Li } else 4635f33a080SShaohua Li next_deferred = total_scan; 464ea164d73SAndrea Arcangeli 465ea164d73SAndrea Arcangeli /* 4663567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 4673567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 4683567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 4693567b59aSDave Chinner * nr being built up so when a shrink that can do some work 4703567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 471d5bc5fd3SVladimir Davydov * freeable. This is bad for sustaining a working set in 4723567b59aSDave Chinner * memory. 4733567b59aSDave Chinner * 4743567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 4753567b59aSDave Chinner * a large delta change is calculated directly. 4763567b59aSDave Chinner */ 477d5bc5fd3SVladimir Davydov if (delta < freeable / 4) 478d5bc5fd3SVladimir Davydov total_scan = min(total_scan, freeable / 2); 4793567b59aSDave Chinner 4803567b59aSDave Chinner /* 481ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 482ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 483ea164d73SAndrea Arcangeli * freeable entries. 484ea164d73SAndrea Arcangeli */ 485d5bc5fd3SVladimir Davydov if (total_scan > freeable * 2) 486d5bc5fd3SVladimir Davydov total_scan = freeable * 2; 4871da177e4SLinus Torvalds 48824f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 4899092c71bSJosef Bacik freeable, delta, total_scan, priority); 49009576073SDave Chinner 4910b1fb40aSVladimir Davydov /* 4920b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 4930b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 4940b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 4950b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 4960b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 4970b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 4980b1fb40aSVladimir Davydov * batch_size. 4990b1fb40aSVladimir Davydov * 5000b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 5010b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 502d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 5030b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 5040b1fb40aSVladimir Davydov * possible. 5050b1fb40aSVladimir Davydov */ 5060b1fb40aSVladimir Davydov while (total_scan >= batch_size || 507d5bc5fd3SVladimir Davydov total_scan >= freeable) { 50824f7c6b9SDave Chinner unsigned long ret; 5090b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 5101da177e4SLinus Torvalds 5110b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 512d460acb5SChris Wilson shrinkctl->nr_scanned = nr_to_scan; 51324f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 51424f7c6b9SDave Chinner if (ret == SHRINK_STOP) 5151da177e4SLinus Torvalds break; 51624f7c6b9SDave Chinner freed += ret; 51724f7c6b9SDave Chinner 518d460acb5SChris Wilson count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); 519d460acb5SChris Wilson total_scan -= shrinkctl->nr_scanned; 520d460acb5SChris Wilson scanned += shrinkctl->nr_scanned; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds cond_resched(); 5231da177e4SLinus Torvalds } 5241da177e4SLinus Torvalds 5255f33a080SShaohua Li if (next_deferred >= scanned) 5265f33a080SShaohua Li next_deferred -= scanned; 5275f33a080SShaohua Li else 5285f33a080SShaohua Li next_deferred = 0; 529acf92b48SDave Chinner /* 530acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 531acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 532acf92b48SDave Chinner * scan, there is no need to do an update. 533acf92b48SDave Chinner */ 5345f33a080SShaohua Li if (next_deferred > 0) 5355f33a080SShaohua Li new_nr = atomic_long_add_return(next_deferred, 5361d3d4437SGlauber Costa &shrinker->nr_deferred[nid]); 53783aeeadaSKonstantin Khlebnikov else 5381d3d4437SGlauber Costa new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 539acf92b48SDave Chinner 540df9024a8SDave Hansen trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan); 5411d3d4437SGlauber Costa return freed; 5421d3d4437SGlauber Costa } 5431d3d4437SGlauber Costa 5440a432dcbSYang Shi #ifdef CONFIG_MEMCG 545b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 546b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 547b0dedc49SKirill Tkhai { 548b0dedc49SKirill Tkhai struct memcg_shrinker_map *map; 549b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 550b8e57efaSKirill Tkhai int i; 551b0dedc49SKirill Tkhai 5520a432dcbSYang Shi if (!mem_cgroup_online(memcg)) 553b0dedc49SKirill Tkhai return 0; 554b0dedc49SKirill Tkhai 555b0dedc49SKirill Tkhai if (!down_read_trylock(&shrinker_rwsem)) 556b0dedc49SKirill Tkhai return 0; 557b0dedc49SKirill Tkhai 558b0dedc49SKirill Tkhai map = rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_map, 559b0dedc49SKirill Tkhai true); 560b0dedc49SKirill Tkhai if (unlikely(!map)) 561b0dedc49SKirill Tkhai goto unlock; 562b0dedc49SKirill Tkhai 563b0dedc49SKirill Tkhai for_each_set_bit(i, map->map, shrinker_nr_max) { 564b0dedc49SKirill Tkhai struct shrink_control sc = { 565b0dedc49SKirill Tkhai .gfp_mask = gfp_mask, 566b0dedc49SKirill Tkhai .nid = nid, 567b0dedc49SKirill Tkhai .memcg = memcg, 568b0dedc49SKirill Tkhai }; 569b0dedc49SKirill Tkhai struct shrinker *shrinker; 570b0dedc49SKirill Tkhai 571b0dedc49SKirill Tkhai shrinker = idr_find(&shrinker_idr, i); 5727e010df5SKirill Tkhai if (unlikely(!shrinker || shrinker == SHRINKER_REGISTERING)) { 5737e010df5SKirill Tkhai if (!shrinker) 574b0dedc49SKirill Tkhai clear_bit(i, map->map); 575b0dedc49SKirill Tkhai continue; 576b0dedc49SKirill Tkhai } 577b0dedc49SKirill Tkhai 5780a432dcbSYang Shi /* Call non-slab shrinkers even though kmem is disabled */ 5790a432dcbSYang Shi if (!memcg_kmem_enabled() && 5800a432dcbSYang Shi !(shrinker->flags & SHRINKER_NONSLAB)) 5810a432dcbSYang Shi continue; 5820a432dcbSYang Shi 583b0dedc49SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 584f90280d6SKirill Tkhai if (ret == SHRINK_EMPTY) { 585f90280d6SKirill Tkhai clear_bit(i, map->map); 586f90280d6SKirill Tkhai /* 587f90280d6SKirill Tkhai * After the shrinker reported that it had no objects to 588f90280d6SKirill Tkhai * free, but before we cleared the corresponding bit in 589f90280d6SKirill Tkhai * the memcg shrinker map, a new object might have been 590f90280d6SKirill Tkhai * added. To make sure, we have the bit set in this 591f90280d6SKirill Tkhai * case, we invoke the shrinker one more time and reset 592f90280d6SKirill Tkhai * the bit if it reports that it is not empty anymore. 593f90280d6SKirill Tkhai * The memory barrier here pairs with the barrier in 594f90280d6SKirill Tkhai * memcg_set_shrinker_bit(): 595f90280d6SKirill Tkhai * 596f90280d6SKirill Tkhai * list_lru_add() shrink_slab_memcg() 597f90280d6SKirill Tkhai * list_add_tail() clear_bit() 598f90280d6SKirill Tkhai * <MB> <MB> 599f90280d6SKirill Tkhai * set_bit() do_shrink_slab() 600f90280d6SKirill Tkhai */ 601f90280d6SKirill Tkhai smp_mb__after_atomic(); 602f90280d6SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 6039b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 6049b996468SKirill Tkhai ret = 0; 605f90280d6SKirill Tkhai else 606f90280d6SKirill Tkhai memcg_set_shrinker_bit(memcg, nid, i); 607f90280d6SKirill Tkhai } 608b0dedc49SKirill Tkhai freed += ret; 609b0dedc49SKirill Tkhai 610b0dedc49SKirill Tkhai if (rwsem_is_contended(&shrinker_rwsem)) { 611b0dedc49SKirill Tkhai freed = freed ? : 1; 612b0dedc49SKirill Tkhai break; 613b0dedc49SKirill Tkhai } 614b0dedc49SKirill Tkhai } 615b0dedc49SKirill Tkhai unlock: 616b0dedc49SKirill Tkhai up_read(&shrinker_rwsem); 617b0dedc49SKirill Tkhai return freed; 618b0dedc49SKirill Tkhai } 6190a432dcbSYang Shi #else /* CONFIG_MEMCG */ 620b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 621b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 622b0dedc49SKirill Tkhai { 623b0dedc49SKirill Tkhai return 0; 624b0dedc49SKirill Tkhai } 6250a432dcbSYang Shi #endif /* CONFIG_MEMCG */ 626b0dedc49SKirill Tkhai 6276b4f7799SJohannes Weiner /** 628cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 6296b4f7799SJohannes Weiner * @gfp_mask: allocation context 6306b4f7799SJohannes Weiner * @nid: node whose slab caches to target 631cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 6329092c71bSJosef Bacik * @priority: the reclaim priority 6331d3d4437SGlauber Costa * 6346b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 6351d3d4437SGlauber Costa * 6366b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 6376b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 6381d3d4437SGlauber Costa * 639aeed1d32SVladimir Davydov * @memcg specifies the memory cgroup to target. Unaware shrinkers 640aeed1d32SVladimir Davydov * are called only if it is the root cgroup. 641cb731d6cSVladimir Davydov * 6429092c71bSJosef Bacik * @priority is sc->priority, we take the number of objects and >> by priority 6439092c71bSJosef Bacik * in order to get the scan target. 6441d3d4437SGlauber Costa * 6456b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 6461d3d4437SGlauber Costa */ 647cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 648cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 6499092c71bSJosef Bacik int priority) 6501d3d4437SGlauber Costa { 651b8e57efaSKirill Tkhai unsigned long ret, freed = 0; 6521d3d4437SGlauber Costa struct shrinker *shrinker; 6531d3d4437SGlauber Costa 654fa1e512fSYang Shi /* 655fa1e512fSYang Shi * The root memcg might be allocated even though memcg is disabled 656fa1e512fSYang Shi * via "cgroup_disable=memory" boot parameter. This could make 657fa1e512fSYang Shi * mem_cgroup_is_root() return false, then just run memcg slab 658fa1e512fSYang Shi * shrink, but skip global shrink. This may result in premature 659fa1e512fSYang Shi * oom. 660fa1e512fSYang Shi */ 661fa1e512fSYang Shi if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg)) 662b0dedc49SKirill Tkhai return shrink_slab_memcg(gfp_mask, nid, memcg, priority); 663cb731d6cSVladimir Davydov 664e830c63aSTetsuo Handa if (!down_read_trylock(&shrinker_rwsem)) 6651d3d4437SGlauber Costa goto out; 6661d3d4437SGlauber Costa 6671d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 6686b4f7799SJohannes Weiner struct shrink_control sc = { 6696b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 6706b4f7799SJohannes Weiner .nid = nid, 671cb731d6cSVladimir Davydov .memcg = memcg, 6726b4f7799SJohannes Weiner }; 6736b4f7799SJohannes Weiner 6749b996468SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 6759b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 6769b996468SKirill Tkhai ret = 0; 6779b996468SKirill Tkhai freed += ret; 678e496612cSMinchan Kim /* 679e496612cSMinchan Kim * Bail out if someone want to register a new shrinker to 68055b65a57SEthon Paul * prevent the registration from being stalled for long periods 681e496612cSMinchan Kim * by parallel ongoing shrinking. 682e496612cSMinchan Kim */ 683e496612cSMinchan Kim if (rwsem_is_contended(&shrinker_rwsem)) { 684e496612cSMinchan Kim freed = freed ? : 1; 685e496612cSMinchan Kim break; 686e496612cSMinchan Kim } 687ec97097bSVladimir Davydov } 6881d3d4437SGlauber Costa 6891da177e4SLinus Torvalds up_read(&shrinker_rwsem); 690f06590bdSMinchan Kim out: 691f06590bdSMinchan Kim cond_resched(); 69224f7c6b9SDave Chinner return freed; 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds 695cb731d6cSVladimir Davydov void drop_slab_node(int nid) 696cb731d6cSVladimir Davydov { 697cb731d6cSVladimir Davydov unsigned long freed; 698cb731d6cSVladimir Davydov 699cb731d6cSVladimir Davydov do { 700cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 701cb731d6cSVladimir Davydov 702*069c411dSChunxin Zang if (fatal_signal_pending(current)) 703*069c411dSChunxin Zang return; 704*069c411dSChunxin Zang 705cb731d6cSVladimir Davydov freed = 0; 706aeed1d32SVladimir Davydov memcg = mem_cgroup_iter(NULL, NULL, NULL); 707cb731d6cSVladimir Davydov do { 7089092c71bSJosef Bacik freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); 709cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 710cb731d6cSVladimir Davydov } while (freed > 10); 711cb731d6cSVladimir Davydov } 712cb731d6cSVladimir Davydov 713cb731d6cSVladimir Davydov void drop_slab(void) 714cb731d6cSVladimir Davydov { 715cb731d6cSVladimir Davydov int nid; 716cb731d6cSVladimir Davydov 717cb731d6cSVladimir Davydov for_each_online_node(nid) 718cb731d6cSVladimir Davydov drop_slab_node(nid); 719cb731d6cSVladimir Davydov } 720cb731d6cSVladimir Davydov 7211da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 7221da177e4SLinus Torvalds { 723ceddc3a5SJohannes Weiner /* 724ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 72567891fffSMatthew Wilcox * that isolated the page, the page cache and optional buffer 72667891fffSMatthew Wilcox * heads at page->private. 727ceddc3a5SJohannes Weiner */ 72867891fffSMatthew Wilcox int page_cache_pins = PageTransHuge(page) && PageSwapCache(page) ? 729bd4c82c2SHuang Ying HPAGE_PMD_NR : 1; 73067891fffSMatthew Wilcox return page_count(page) - page_has_private(page) == 1 + page_cache_pins; 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds 733cb16556dSYang Shi static int may_write_to_inode(struct inode *inode) 7341da177e4SLinus Torvalds { 735930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 7361da177e4SLinus Torvalds return 1; 737703c2708STejun Heo if (!inode_write_congested(inode)) 7381da177e4SLinus Torvalds return 1; 739703c2708STejun Heo if (inode_to_bdi(inode) == current->backing_dev_info) 7401da177e4SLinus Torvalds return 1; 7411da177e4SLinus Torvalds return 0; 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds /* 7451da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 7461da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 7471da177e4SLinus Torvalds * fsync(), msync() or close(). 7481da177e4SLinus Torvalds * 7491da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 7501da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 7511da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 7521da177e4SLinus Torvalds * 7531da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 7541da177e4SLinus Torvalds * __GFP_FS. 7551da177e4SLinus Torvalds */ 7561da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 7571da177e4SLinus Torvalds struct page *page, int error) 7581da177e4SLinus Torvalds { 7597eaceaccSJens Axboe lock_page(page); 7603e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 7613e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 7621da177e4SLinus Torvalds unlock_page(page); 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 76504e62a29SChristoph Lameter /* possible outcome of pageout() */ 76604e62a29SChristoph Lameter typedef enum { 76704e62a29SChristoph Lameter /* failed to write page out, page is locked */ 76804e62a29SChristoph Lameter PAGE_KEEP, 76904e62a29SChristoph Lameter /* move page to the active list, page is locked */ 77004e62a29SChristoph Lameter PAGE_ACTIVATE, 77104e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 77204e62a29SChristoph Lameter PAGE_SUCCESS, 77304e62a29SChristoph Lameter /* page is clean and locked */ 77404e62a29SChristoph Lameter PAGE_CLEAN, 77504e62a29SChristoph Lameter } pageout_t; 77604e62a29SChristoph Lameter 7771da177e4SLinus Torvalds /* 7781742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 7791742f19fSAndrew Morton * Calls ->writepage(). 7801da177e4SLinus Torvalds */ 781cb16556dSYang Shi static pageout_t pageout(struct page *page, struct address_space *mapping) 7821da177e4SLinus Torvalds { 7831da177e4SLinus Torvalds /* 7841da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 7851da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 7861da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 7871da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 7881da177e4SLinus Torvalds * PagePrivate for that. 7891da177e4SLinus Torvalds * 7908174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 7911da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 7921da177e4SLinus Torvalds * will block. 7931da177e4SLinus Torvalds * 7941da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 7951da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 7961da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 7971da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 7981da177e4SLinus Torvalds */ 7991da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 8001da177e4SLinus Torvalds return PAGE_KEEP; 8011da177e4SLinus Torvalds if (!mapping) { 8021da177e4SLinus Torvalds /* 8031da177e4SLinus Torvalds * Some data journaling orphaned pages can have 8041da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 8051da177e4SLinus Torvalds */ 806266cf658SDavid Howells if (page_has_private(page)) { 8071da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 8081da177e4SLinus Torvalds ClearPageDirty(page); 809b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 8101da177e4SLinus Torvalds return PAGE_CLEAN; 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds return PAGE_KEEP; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 8161da177e4SLinus Torvalds return PAGE_ACTIVATE; 817cb16556dSYang Shi if (!may_write_to_inode(mapping->host)) 8181da177e4SLinus Torvalds return PAGE_KEEP; 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 8211da177e4SLinus Torvalds int res; 8221da177e4SLinus Torvalds struct writeback_control wbc = { 8231da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 8241da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 825111ebb6eSOGAWA Hirofumi .range_start = 0, 826111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 8271da177e4SLinus Torvalds .for_reclaim = 1, 8281da177e4SLinus Torvalds }; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds SetPageReclaim(page); 8311da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 8321da177e4SLinus Torvalds if (res < 0) 8331da177e4SLinus Torvalds handle_write_error(mapping, page, res); 834994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 8351da177e4SLinus Torvalds ClearPageReclaim(page); 8361da177e4SLinus Torvalds return PAGE_ACTIVATE; 8371da177e4SLinus Torvalds } 838c661b078SAndy Whitcroft 8391da177e4SLinus Torvalds if (!PageWriteback(page)) { 8401da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 8411da177e4SLinus Torvalds ClearPageReclaim(page); 8421da177e4SLinus Torvalds } 8433aa23851Syalin wang trace_mm_vmscan_writepage(page); 844c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_WRITE); 8451da177e4SLinus Torvalds return PAGE_SUCCESS; 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds return PAGE_CLEAN; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 851a649fd92SAndrew Morton /* 852e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 853e286781dSNick Piggin * gets returned with a refcount of 0. 854a649fd92SAndrew Morton */ 855a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 856b910718aSJohannes Weiner bool reclaimed, struct mem_cgroup *target_memcg) 85749d2e9ccSChristoph Lameter { 858c4843a75SGreg Thelen unsigned long flags; 859bd4c82c2SHuang Ying int refcount; 860aae466b0SJoonsoo Kim void *shadow = NULL; 861c4843a75SGreg Thelen 86228e4d965SNick Piggin BUG_ON(!PageLocked(page)); 86328e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 86449d2e9ccSChristoph Lameter 865b93b0163SMatthew Wilcox xa_lock_irqsave(&mapping->i_pages, flags); 86649d2e9ccSChristoph Lameter /* 8670fd0e6b0SNick Piggin * The non racy check for a busy page. 8680fd0e6b0SNick Piggin * 8690fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 8700fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 8710fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 8720fd0e6b0SNick Piggin * here, then the following race may occur: 8730fd0e6b0SNick Piggin * 8740fd0e6b0SNick Piggin * get_user_pages(&page); 8750fd0e6b0SNick Piggin * [user mapping goes away] 8760fd0e6b0SNick Piggin * write_to(page); 8770fd0e6b0SNick Piggin * !PageDirty(page) [good] 8780fd0e6b0SNick Piggin * SetPageDirty(page); 8790fd0e6b0SNick Piggin * put_page(page); 8800fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 8810fd0e6b0SNick Piggin * 8820fd0e6b0SNick Piggin * [oops, our write_to data is lost] 8830fd0e6b0SNick Piggin * 8840fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 8850fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 8860139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 8870fd0e6b0SNick Piggin * 8880fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 889b93b0163SMatthew Wilcox * and thus under the i_pages lock, then this ordering is not required. 89049d2e9ccSChristoph Lameter */ 891906d278dSWilliam Kucharski refcount = 1 + compound_nr(page); 892bd4c82c2SHuang Ying if (!page_ref_freeze(page, refcount)) 89349d2e9ccSChristoph Lameter goto cannot_free; 8941c4c3b99SJiang Biao /* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */ 895e286781dSNick Piggin if (unlikely(PageDirty(page))) { 896bd4c82c2SHuang Ying page_ref_unfreeze(page, refcount); 89749d2e9ccSChristoph Lameter goto cannot_free; 898e286781dSNick Piggin } 89949d2e9ccSChristoph Lameter 90049d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 90149d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 9020a31bc97SJohannes Weiner mem_cgroup_swapout(page, swap); 903aae466b0SJoonsoo Kim if (reclaimed && !mapping_exiting(mapping)) 904aae466b0SJoonsoo Kim shadow = workingset_eviction(page, target_memcg); 905aae466b0SJoonsoo Kim __delete_from_swap_cache(page, swap, shadow); 906b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 90775f6d6d2SMinchan Kim put_swap_page(page, swap); 908e286781dSNick Piggin } else { 9096072d13cSLinus Torvalds void (*freepage)(struct page *); 9106072d13cSLinus Torvalds 9116072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 912a528910eSJohannes Weiner /* 913a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 914a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 915a528910eSJohannes Weiner * 916a528910eSJohannes Weiner * But don't store shadows in an address space that is 917238c3046Sdylan-meiners * already exiting. This is not just an optimization, 918a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 919a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 920a528910eSJohannes Weiner * back. 921f9fe48beSRoss Zwisler * 922f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 923f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 924f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 925f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 926b93b0163SMatthew Wilcox * same address_space. 927a528910eSJohannes Weiner */ 9289de4f22aSHuang Ying if (reclaimed && page_is_file_lru(page) && 929f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 930b910718aSJohannes Weiner shadow = workingset_eviction(page, target_memcg); 93162cccb8cSJohannes Weiner __delete_from_page_cache(page, shadow); 932b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 9336072d13cSLinus Torvalds 9346072d13cSLinus Torvalds if (freepage != NULL) 9356072d13cSLinus Torvalds freepage(page); 936e286781dSNick Piggin } 937e286781dSNick Piggin 93849d2e9ccSChristoph Lameter return 1; 93949d2e9ccSChristoph Lameter 94049d2e9ccSChristoph Lameter cannot_free: 941b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 94249d2e9ccSChristoph Lameter return 0; 94349d2e9ccSChristoph Lameter } 94449d2e9ccSChristoph Lameter 9451da177e4SLinus Torvalds /* 946e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 947e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 948e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 949e286781dSNick Piggin * this page. 950e286781dSNick Piggin */ 951e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 952e286781dSNick Piggin { 953b910718aSJohannes Weiner if (__remove_mapping(mapping, page, false, NULL)) { 954e286781dSNick Piggin /* 955e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 956e286781dSNick Piggin * drops the pagecache ref for us without requiring another 957e286781dSNick Piggin * atomic operation. 958e286781dSNick Piggin */ 959fe896d18SJoonsoo Kim page_ref_unfreeze(page, 1); 960e286781dSNick Piggin return 1; 961e286781dSNick Piggin } 962e286781dSNick Piggin return 0; 963e286781dSNick Piggin } 964e286781dSNick Piggin 965894bc310SLee Schermerhorn /** 966894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 967894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 968894bc310SLee Schermerhorn * 969894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 970894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 971894bc310SLee Schermerhorn * 972894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 973894bc310SLee Schermerhorn */ 974894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 975894bc310SLee Schermerhorn { 976c53954a0SMel Gorman lru_cache_add(page); 977894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 978894bc310SLee Schermerhorn } 979894bc310SLee Schermerhorn 980dfc8d636SJohannes Weiner enum page_references { 981dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 982dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 98364574746SJohannes Weiner PAGEREF_KEEP, 984dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 985dfc8d636SJohannes Weiner }; 986dfc8d636SJohannes Weiner 987dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 988dfc8d636SJohannes Weiner struct scan_control *sc) 989dfc8d636SJohannes Weiner { 99064574746SJohannes Weiner int referenced_ptes, referenced_page; 991dfc8d636SJohannes Weiner unsigned long vm_flags; 992dfc8d636SJohannes Weiner 993c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 994c3ac9a8aSJohannes Weiner &vm_flags); 99564574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 996dfc8d636SJohannes Weiner 997dfc8d636SJohannes Weiner /* 998dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 999dfc8d636SJohannes Weiner * move the page to the unevictable list. 1000dfc8d636SJohannes Weiner */ 1001dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 1002dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 1003dfc8d636SJohannes Weiner 100464574746SJohannes Weiner if (referenced_ptes) { 100564574746SJohannes Weiner /* 100664574746SJohannes Weiner * All mapped pages start out with page table 100764574746SJohannes Weiner * references from the instantiating fault, so we need 100864574746SJohannes Weiner * to look twice if a mapped file page is used more 100964574746SJohannes Weiner * than once. 101064574746SJohannes Weiner * 101164574746SJohannes Weiner * Mark it and spare it for another trip around the 101264574746SJohannes Weiner * inactive list. Another page table reference will 101364574746SJohannes Weiner * lead to its activation. 101464574746SJohannes Weiner * 101564574746SJohannes Weiner * Note: the mark is set for activated pages as well 101664574746SJohannes Weiner * so that recently deactivated but used pages are 101764574746SJohannes Weiner * quickly recovered. 101864574746SJohannes Weiner */ 101964574746SJohannes Weiner SetPageReferenced(page); 102064574746SJohannes Weiner 102134dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 1022dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 1023dfc8d636SJohannes Weiner 1024c909e993SKonstantin Khlebnikov /* 1025c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 1026c909e993SKonstantin Khlebnikov */ 1027b518154eSJoonsoo Kim if ((vm_flags & VM_EXEC) && !PageSwapBacked(page)) 1028c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 1029c909e993SKonstantin Khlebnikov 103064574746SJohannes Weiner return PAGEREF_KEEP; 103164574746SJohannes Weiner } 103264574746SJohannes Weiner 1033dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 10342e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 1035dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 103664574746SJohannes Weiner 103764574746SJohannes Weiner return PAGEREF_RECLAIM; 1038dfc8d636SJohannes Weiner } 1039dfc8d636SJohannes Weiner 1040e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 1041e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 1042e2be15f6SMel Gorman bool *dirty, bool *writeback) 1043e2be15f6SMel Gorman { 1044b4597226SMel Gorman struct address_space *mapping; 1045b4597226SMel Gorman 1046e2be15f6SMel Gorman /* 1047e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 1048e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 1049e2be15f6SMel Gorman */ 10509de4f22aSHuang Ying if (!page_is_file_lru(page) || 1051802a3a92SShaohua Li (PageAnon(page) && !PageSwapBacked(page))) { 1052e2be15f6SMel Gorman *dirty = false; 1053e2be15f6SMel Gorman *writeback = false; 1054e2be15f6SMel Gorman return; 1055e2be15f6SMel Gorman } 1056e2be15f6SMel Gorman 1057e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 1058e2be15f6SMel Gorman *dirty = PageDirty(page); 1059e2be15f6SMel Gorman *writeback = PageWriteback(page); 1060b4597226SMel Gorman 1061b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 1062b4597226SMel Gorman if (!page_has_private(page)) 1063b4597226SMel Gorman return; 1064b4597226SMel Gorman 1065b4597226SMel Gorman mapping = page_mapping(page); 1066b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 1067b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 1068e2be15f6SMel Gorman } 1069e2be15f6SMel Gorman 1070e286781dSNick Piggin /* 10711742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 10721da177e4SLinus Torvalds */ 1073730ec8c0SManinder Singh static unsigned int shrink_page_list(struct list_head *page_list, 1074599d0c95SMel Gorman struct pglist_data *pgdat, 1075f84f6e2bSMel Gorman struct scan_control *sc, 107602c6de8dSMinchan Kim enum ttu_flags ttu_flags, 10773c710c1aSMichal Hocko struct reclaim_stat *stat, 10788940b34aSMinchan Kim bool ignore_references) 10791da177e4SLinus Torvalds { 10801da177e4SLinus Torvalds LIST_HEAD(ret_pages); 1081abe4c3b5SMel Gorman LIST_HEAD(free_pages); 1082730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 1083730ec8c0SManinder Singh unsigned int pgactivate = 0; 10841da177e4SLinus Torvalds 1085060f005fSKirill Tkhai memset(stat, 0, sizeof(*stat)); 10861da177e4SLinus Torvalds cond_resched(); 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds while (!list_empty(page_list)) { 10891da177e4SLinus Torvalds struct address_space *mapping; 10901da177e4SLinus Torvalds struct page *page; 10918940b34aSMinchan Kim enum page_references references = PAGEREF_RECLAIM; 10924b793062SKirill Tkhai bool dirty, writeback, may_enter_fs; 109398879b3bSYang Shi unsigned int nr_pages; 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds cond_resched(); 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds page = lru_to_page(page_list); 10981da177e4SLinus Torvalds list_del(&page->lru); 10991da177e4SLinus Torvalds 1100529ae9aaSNick Piggin if (!trylock_page(page)) 11011da177e4SLinus Torvalds goto keep; 11021da177e4SLinus Torvalds 1103309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 11041da177e4SLinus Torvalds 1105d8c6546bSMatthew Wilcox (Oracle) nr_pages = compound_nr(page); 110698879b3bSYang Shi 110798879b3bSYang Shi /* Account the number of base pages even though THP */ 110898879b3bSYang Shi sc->nr_scanned += nr_pages; 110980e43426SChristoph Lameter 111039b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 1111ad6b6704SMinchan Kim goto activate_locked; 1112894bc310SLee Schermerhorn 1113a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 111480e43426SChristoph Lameter goto keep_locked; 111580e43426SChristoph Lameter 1116c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 1117c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 1118c661b078SAndy Whitcroft 1119e62e384eSMichal Hocko /* 1120894befecSAndrey Ryabinin * The number of dirty pages determines if a node is marked 1121e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 1122e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 1123e2be15f6SMel Gorman * is all dirty unqueued pages. 1124e2be15f6SMel Gorman */ 1125e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 1126e2be15f6SMel Gorman if (dirty || writeback) 1127060f005fSKirill Tkhai stat->nr_dirty++; 1128e2be15f6SMel Gorman 1129e2be15f6SMel Gorman if (dirty && !writeback) 1130060f005fSKirill Tkhai stat->nr_unqueued_dirty++; 1131e2be15f6SMel Gorman 1132d04e8acdSMel Gorman /* 1133d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 1134d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 1135d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 1136d04e8acdSMel Gorman * end of the LRU a second time. 1137d04e8acdSMel Gorman */ 1138e2be15f6SMel Gorman mapping = page_mapping(page); 11391da58ee2SJamie Liu if (((dirty || writeback) && mapping && 1140703c2708STejun Heo inode_write_congested(mapping->host)) || 1141d04e8acdSMel Gorman (writeback && PageReclaim(page))) 1142060f005fSKirill Tkhai stat->nr_congested++; 1143e2be15f6SMel Gorman 1144e2be15f6SMel Gorman /* 1145283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 1146283aba9fSMel Gorman * are three cases to consider. 1147e62e384eSMichal Hocko * 1148283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 1149283aba9fSMel Gorman * under writeback and this page is both under writeback and 1150283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 1151283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 1152283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 1153283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 1154283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 1155b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 1156b1a6f21eSMel Gorman * caller can stall after page list has been processed. 1157c3b94f44SHugh Dickins * 115897c9341fSTejun Heo * 2) Global or new memcg reclaim encounters a page that is 1159ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 1160ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 1161ecf5fc6eSMichal Hocko * not to fs). In this case mark the page for immediate 116297c9341fSTejun Heo * reclaim and continue scanning. 1163283aba9fSMel Gorman * 1164ecf5fc6eSMichal Hocko * Require may_enter_fs because we would wait on fs, which 1165ecf5fc6eSMichal Hocko * may not have submitted IO yet. And the loop driver might 1166283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 1167283aba9fSMel Gorman * which it is needed to do the write (loop masks off 1168283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 1169283aba9fSMel Gorman * would probably show more reasons. 1170283aba9fSMel Gorman * 11717fadc820SHugh Dickins * 3) Legacy memcg encounters a page that is already marked 1172283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 1173283aba9fSMel Gorman * throttling so we could easily OOM just because too many 1174283aba9fSMel Gorman * pages are in writeback and there is nothing else to 1175283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 1176c55e8d03SJohannes Weiner * 1177c55e8d03SJohannes Weiner * In cases 1) and 2) we activate the pages to get them out of 1178c55e8d03SJohannes Weiner * the way while we continue scanning for clean pages on the 1179c55e8d03SJohannes Weiner * inactive list and refilling from the active list. The 1180c55e8d03SJohannes Weiner * observation here is that waiting for disk writes is more 1181c55e8d03SJohannes Weiner * expensive than potentially causing reloads down the line. 1182c55e8d03SJohannes Weiner * Since they're marked for immediate reclaim, they won't put 1183c55e8d03SJohannes Weiner * memory pressure on the cache working set any longer than it 1184c55e8d03SJohannes Weiner * takes to write them to disk. 1185e62e384eSMichal Hocko */ 1186283aba9fSMel Gorman if (PageWriteback(page)) { 1187283aba9fSMel Gorman /* Case 1 above */ 1188283aba9fSMel Gorman if (current_is_kswapd() && 1189283aba9fSMel Gorman PageReclaim(page) && 1190599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1191060f005fSKirill Tkhai stat->nr_immediate++; 1192c55e8d03SJohannes Weiner goto activate_locked; 1193283aba9fSMel Gorman 1194283aba9fSMel Gorman /* Case 2 above */ 1195b5ead35eSJohannes Weiner } else if (writeback_throttling_sane(sc) || 1196ecf5fc6eSMichal Hocko !PageReclaim(page) || !may_enter_fs) { 1197c3b94f44SHugh Dickins /* 1198c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 1199c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 1200c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 1201c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 1202c3b94f44SHugh Dickins * enough to care. What we do want is for this 1203c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 1204c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 1205c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 1206c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 1207c3b94f44SHugh Dickins */ 1208c3b94f44SHugh Dickins SetPageReclaim(page); 1209060f005fSKirill Tkhai stat->nr_writeback++; 1210c55e8d03SJohannes Weiner goto activate_locked; 1211283aba9fSMel Gorman 1212283aba9fSMel Gorman /* Case 3 above */ 1213283aba9fSMel Gorman } else { 12147fadc820SHugh Dickins unlock_page(page); 1215c3b94f44SHugh Dickins wait_on_page_writeback(page); 12167fadc820SHugh Dickins /* then go back and try same page again */ 12177fadc820SHugh Dickins list_add_tail(&page->lru, page_list); 12187fadc820SHugh Dickins continue; 1219e62e384eSMichal Hocko } 1220283aba9fSMel Gorman } 12211da177e4SLinus Torvalds 12228940b34aSMinchan Kim if (!ignore_references) 12236a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 122402c6de8dSMinchan Kim 1225dfc8d636SJohannes Weiner switch (references) { 1226dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 12271da177e4SLinus Torvalds goto activate_locked; 122864574746SJohannes Weiner case PAGEREF_KEEP: 122998879b3bSYang Shi stat->nr_ref_keep += nr_pages; 123064574746SJohannes Weiner goto keep_locked; 1231dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1232dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1233dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 1234dfc8d636SJohannes Weiner } 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds /* 12371da177e4SLinus Torvalds * Anonymous process memory has backing store? 12381da177e4SLinus Torvalds * Try to allocate it some swap space here. 1239802a3a92SShaohua Li * Lazyfree page could be freed directly 12401da177e4SLinus Torvalds */ 1241bd4c82c2SHuang Ying if (PageAnon(page) && PageSwapBacked(page)) { 1242bd4c82c2SHuang Ying if (!PageSwapCache(page)) { 124363eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 124463eb6b93SHugh Dickins goto keep_locked; 1245747552b1SHuang Ying if (PageTransHuge(page)) { 1246b8f593cdSHuang Ying /* cannot split THP, skip it */ 1247747552b1SHuang Ying if (!can_split_huge_page(page, NULL)) 1248b8f593cdSHuang Ying goto activate_locked; 1249747552b1SHuang Ying /* 1250747552b1SHuang Ying * Split pages without a PMD map right 1251747552b1SHuang Ying * away. Chances are some or all of the 1252747552b1SHuang Ying * tail pages can be freed without IO. 1253747552b1SHuang Ying */ 1254747552b1SHuang Ying if (!compound_mapcount(page) && 1255bd4c82c2SHuang Ying split_huge_page_to_list(page, 1256bd4c82c2SHuang Ying page_list)) 1257747552b1SHuang Ying goto activate_locked; 1258747552b1SHuang Ying } 12590f074658SMinchan Kim if (!add_to_swap(page)) { 12600f074658SMinchan Kim if (!PageTransHuge(page)) 126198879b3bSYang Shi goto activate_locked_split; 1262bd4c82c2SHuang Ying /* Fallback to swap normal pages */ 1263bd4c82c2SHuang Ying if (split_huge_page_to_list(page, 1264bd4c82c2SHuang Ying page_list)) 12650f074658SMinchan Kim goto activate_locked; 1266fe490cc0SHuang Ying #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1267fe490cc0SHuang Ying count_vm_event(THP_SWPOUT_FALLBACK); 1268fe490cc0SHuang Ying #endif 12690f074658SMinchan Kim if (!add_to_swap(page)) 127098879b3bSYang Shi goto activate_locked_split; 12710f074658SMinchan Kim } 12720f074658SMinchan Kim 12734b793062SKirill Tkhai may_enter_fs = true; 12741da177e4SLinus Torvalds 1275e2be15f6SMel Gorman /* Adding to swap updated mapping */ 12761da177e4SLinus Torvalds mapping = page_mapping(page); 1277bd4c82c2SHuang Ying } 12787751b2daSKirill A. Shutemov } else if (unlikely(PageTransHuge(page))) { 12797751b2daSKirill A. Shutemov /* Split file THP */ 12807751b2daSKirill A. Shutemov if (split_huge_page_to_list(page, page_list)) 12817751b2daSKirill A. Shutemov goto keep_locked; 1282e2be15f6SMel Gorman } 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds /* 128598879b3bSYang Shi * THP may get split above, need minus tail pages and update 128698879b3bSYang Shi * nr_pages to avoid accounting tail pages twice. 128798879b3bSYang Shi * 128898879b3bSYang Shi * The tail pages that are added into swap cache successfully 128998879b3bSYang Shi * reach here. 129098879b3bSYang Shi */ 129198879b3bSYang Shi if ((nr_pages > 1) && !PageTransHuge(page)) { 129298879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 129398879b3bSYang Shi nr_pages = 1; 129498879b3bSYang Shi } 129598879b3bSYang Shi 129698879b3bSYang Shi /* 12971da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 12981da177e4SLinus Torvalds * processes. Try to unmap it here. 12991da177e4SLinus Torvalds */ 1300802a3a92SShaohua Li if (page_mapped(page)) { 1301bd4c82c2SHuang Ying enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH; 13021f318a9bSJaewon Kim bool was_swapbacked = PageSwapBacked(page); 1303bd4c82c2SHuang Ying 1304bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) 1305bd4c82c2SHuang Ying flags |= TTU_SPLIT_HUGE_PMD; 13061f318a9bSJaewon Kim 1307bd4c82c2SHuang Ying if (!try_to_unmap(page, flags)) { 130898879b3bSYang Shi stat->nr_unmap_fail += nr_pages; 13091f318a9bSJaewon Kim if (!was_swapbacked && PageSwapBacked(page)) 13101f318a9bSJaewon Kim stat->nr_lazyfree_fail += nr_pages; 13111da177e4SLinus Torvalds goto activate_locked; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds if (PageDirty(page)) { 1316ee72886dSMel Gorman /* 13174eda4823SJohannes Weiner * Only kswapd can writeback filesystem pages 13184eda4823SJohannes Weiner * to avoid risk of stack overflow. But avoid 13194eda4823SJohannes Weiner * injecting inefficient single-page IO into 13204eda4823SJohannes Weiner * flusher writeback as much as possible: only 13214eda4823SJohannes Weiner * write pages when we've encountered many 13224eda4823SJohannes Weiner * dirty pages, and when we've already scanned 13234eda4823SJohannes Weiner * the rest of the LRU for clean pages and see 13244eda4823SJohannes Weiner * the same dirty pages again (PageReclaim). 1325ee72886dSMel Gorman */ 13269de4f22aSHuang Ying if (page_is_file_lru(page) && 13274eda4823SJohannes Weiner (!current_is_kswapd() || !PageReclaim(page) || 1328599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 132949ea7eb6SMel Gorman /* 133049ea7eb6SMel Gorman * Immediately reclaim when written back. 133149ea7eb6SMel Gorman * Similar in principal to deactivate_page() 133249ea7eb6SMel Gorman * except we already have the page isolated 133349ea7eb6SMel Gorman * and know it's dirty 133449ea7eb6SMel Gorman */ 1335c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 133649ea7eb6SMel Gorman SetPageReclaim(page); 133749ea7eb6SMel Gorman 1338c55e8d03SJohannes Weiner goto activate_locked; 1339ee72886dSMel Gorman } 1340ee72886dSMel Gorman 1341dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 13421da177e4SLinus Torvalds goto keep_locked; 13434dd4b920SAndrew Morton if (!may_enter_fs) 13441da177e4SLinus Torvalds goto keep_locked; 134552a8363eSChristoph Lameter if (!sc->may_writepage) 13461da177e4SLinus Torvalds goto keep_locked; 13471da177e4SLinus Torvalds 1348d950c947SMel Gorman /* 1349d950c947SMel Gorman * Page is dirty. Flush the TLB if a writable entry 1350d950c947SMel Gorman * potentially exists to avoid CPU writes after IO 1351d950c947SMel Gorman * starts and then write it out here. 1352d950c947SMel Gorman */ 1353d950c947SMel Gorman try_to_unmap_flush_dirty(); 1354cb16556dSYang Shi switch (pageout(page, mapping)) { 13551da177e4SLinus Torvalds case PAGE_KEEP: 13561da177e4SLinus Torvalds goto keep_locked; 13571da177e4SLinus Torvalds case PAGE_ACTIVATE: 13581da177e4SLinus Torvalds goto activate_locked; 13591da177e4SLinus Torvalds case PAGE_SUCCESS: 13606c357848SMatthew Wilcox (Oracle) stat->nr_pageout += thp_nr_pages(page); 136196f8bf4fSJohannes Weiner 13627d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 136341ac1999SMel Gorman goto keep; 13647d3579e8SKOSAKI Motohiro if (PageDirty(page)) 13651da177e4SLinus Torvalds goto keep; 13667d3579e8SKOSAKI Motohiro 13671da177e4SLinus Torvalds /* 13681da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 13691da177e4SLinus Torvalds * ahead and try to reclaim the page. 13701da177e4SLinus Torvalds */ 1371529ae9aaSNick Piggin if (!trylock_page(page)) 13721da177e4SLinus Torvalds goto keep; 13731da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 13741da177e4SLinus Torvalds goto keep_locked; 13751da177e4SLinus Torvalds mapping = page_mapping(page); 13761da177e4SLinus Torvalds case PAGE_CLEAN: 13771da177e4SLinus Torvalds ; /* try to free the page below */ 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds /* 13821da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 13831da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 13841da177e4SLinus Torvalds * the page as well. 13851da177e4SLinus Torvalds * 13861da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 13871da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 13881da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 13891da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 13901da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 13911da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 13921da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 13931da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 13941da177e4SLinus Torvalds * 13951da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 13961da177e4SLinus Torvalds * the pages which were not successfully invalidated in 13971da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 13981da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 13991da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 14001da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 14011da177e4SLinus Torvalds */ 1402266cf658SDavid Howells if (page_has_private(page)) { 14031da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 14041da177e4SLinus Torvalds goto activate_locked; 1405e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1406e286781dSNick Piggin unlock_page(page); 1407e286781dSNick Piggin if (put_page_testzero(page)) 14081da177e4SLinus Torvalds goto free_it; 1409e286781dSNick Piggin else { 1410e286781dSNick Piggin /* 1411e286781dSNick Piggin * rare race with speculative reference. 1412e286781dSNick Piggin * the speculative reference will free 1413e286781dSNick Piggin * this page shortly, so we may 1414e286781dSNick Piggin * increment nr_reclaimed here (and 1415e286781dSNick Piggin * leave it off the LRU). 1416e286781dSNick Piggin */ 1417e286781dSNick Piggin nr_reclaimed++; 1418e286781dSNick Piggin continue; 1419e286781dSNick Piggin } 1420e286781dSNick Piggin } 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds 1423802a3a92SShaohua Li if (PageAnon(page) && !PageSwapBacked(page)) { 1424802a3a92SShaohua Li /* follow __remove_mapping for reference */ 1425802a3a92SShaohua Li if (!page_ref_freeze(page, 1)) 142649d2e9ccSChristoph Lameter goto keep_locked; 1427802a3a92SShaohua Li if (PageDirty(page)) { 1428802a3a92SShaohua Li page_ref_unfreeze(page, 1); 1429802a3a92SShaohua Li goto keep_locked; 1430802a3a92SShaohua Li } 14311da177e4SLinus Torvalds 1432802a3a92SShaohua Li count_vm_event(PGLAZYFREED); 14332262185cSRoman Gushchin count_memcg_page_event(page, PGLAZYFREED); 1434b910718aSJohannes Weiner } else if (!mapping || !__remove_mapping(mapping, page, true, 1435b910718aSJohannes Weiner sc->target_mem_cgroup)) 1436802a3a92SShaohua Li goto keep_locked; 14379a1ea439SHugh Dickins 14389a1ea439SHugh Dickins unlock_page(page); 1439e286781dSNick Piggin free_it: 144098879b3bSYang Shi /* 144198879b3bSYang Shi * THP may get swapped out in a whole, need account 144298879b3bSYang Shi * all base pages. 144398879b3bSYang Shi */ 144498879b3bSYang Shi nr_reclaimed += nr_pages; 1445abe4c3b5SMel Gorman 1446abe4c3b5SMel Gorman /* 1447abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1448abe4c3b5SMel Gorman * appear not as the counts should be low 1449abe4c3b5SMel Gorman */ 14507ae88534SYang Shi if (unlikely(PageTransHuge(page))) 1451ff45fc3cSMatthew Wilcox (Oracle) destroy_compound_page(page); 14527ae88534SYang Shi else 1453abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 14541da177e4SLinus Torvalds continue; 14551da177e4SLinus Torvalds 145698879b3bSYang Shi activate_locked_split: 145798879b3bSYang Shi /* 145898879b3bSYang Shi * The tail pages that are failed to add into swap cache 145998879b3bSYang Shi * reach here. Fixup nr_scanned and nr_pages. 146098879b3bSYang Shi */ 146198879b3bSYang Shi if (nr_pages > 1) { 146298879b3bSYang Shi sc->nr_scanned -= (nr_pages - 1); 146398879b3bSYang Shi nr_pages = 1; 146498879b3bSYang Shi } 14651da177e4SLinus Torvalds activate_locked: 146668a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 1467ad6b6704SMinchan Kim if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || 1468ad6b6704SMinchan Kim PageMlocked(page))) 1469a2c43eedSHugh Dickins try_to_free_swap(page); 1470309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 1471ad6b6704SMinchan Kim if (!PageMlocked(page)) { 14729de4f22aSHuang Ying int type = page_is_file_lru(page); 14731da177e4SLinus Torvalds SetPageActive(page); 147498879b3bSYang Shi stat->nr_activate[type] += nr_pages; 14752262185cSRoman Gushchin count_memcg_page_event(page, PGACTIVATE); 1476ad6b6704SMinchan Kim } 14771da177e4SLinus Torvalds keep_locked: 14781da177e4SLinus Torvalds unlock_page(page); 14791da177e4SLinus Torvalds keep: 14801da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1481309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 14821da177e4SLinus Torvalds } 1483abe4c3b5SMel Gorman 148498879b3bSYang Shi pgactivate = stat->nr_activate[0] + stat->nr_activate[1]; 148598879b3bSYang Shi 1486747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 148772b252aeSMel Gorman try_to_unmap_flush(); 14882d4894b5SMel Gorman free_unref_page_list(&free_pages); 1489abe4c3b5SMel Gorman 14901da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1491886cf190SKirill Tkhai count_vm_events(PGACTIVATE, pgactivate); 14920a31bc97SJohannes Weiner 149305ff5137SAndrew Morton return nr_reclaimed; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds 1496730ec8c0SManinder Singh unsigned int reclaim_clean_pages_from_list(struct zone *zone, 149702c6de8dSMinchan Kim struct list_head *page_list) 149802c6de8dSMinchan Kim { 149902c6de8dSMinchan Kim struct scan_control sc = { 150002c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 150102c6de8dSMinchan Kim .priority = DEF_PRIORITY, 150202c6de8dSMinchan Kim .may_unmap = 1, 150302c6de8dSMinchan Kim }; 15041f318a9bSJaewon Kim struct reclaim_stat stat; 1505730ec8c0SManinder Singh unsigned int nr_reclaimed; 150602c6de8dSMinchan Kim struct page *page, *next; 150702c6de8dSMinchan Kim LIST_HEAD(clean_pages); 150802c6de8dSMinchan Kim 150902c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 15109de4f22aSHuang Ying if (page_is_file_lru(page) && !PageDirty(page) && 1511a58f2cefSMinchan Kim !__PageMovable(page) && !PageUnevictable(page)) { 151202c6de8dSMinchan Kim ClearPageActive(page); 151302c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 151402c6de8dSMinchan Kim } 151502c6de8dSMinchan Kim } 151602c6de8dSMinchan Kim 15171f318a9bSJaewon Kim nr_reclaimed = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 15181f318a9bSJaewon Kim TTU_IGNORE_ACCESS, &stat, true); 151902c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 15201f318a9bSJaewon Kim mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -nr_reclaimed); 15211f318a9bSJaewon Kim /* 15221f318a9bSJaewon Kim * Since lazyfree pages are isolated from file LRU from the beginning, 15231f318a9bSJaewon Kim * they will rotate back to anonymous LRU in the end if it failed to 15241f318a9bSJaewon Kim * discard so isolated count will be mismatched. 15251f318a9bSJaewon Kim * Compensate the isolated count for both LRU lists. 15261f318a9bSJaewon Kim */ 15271f318a9bSJaewon Kim mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON, 15281f318a9bSJaewon Kim stat.nr_lazyfree_fail); 15291f318a9bSJaewon Kim mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, 15301f318a9bSJaewon Kim -stat.nr_lazyfree_fail); 15311f318a9bSJaewon Kim return nr_reclaimed; 153202c6de8dSMinchan Kim } 153302c6de8dSMinchan Kim 15345ad333ebSAndy Whitcroft /* 15355ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 15365ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 15375ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 15385ad333ebSAndy Whitcroft * 15395ad333ebSAndy Whitcroft * page: page to consider 15405ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 15415ad333ebSAndy Whitcroft * 15425ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 15435ad333ebSAndy Whitcroft */ 1544f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 15455ad333ebSAndy Whitcroft { 15465ad333ebSAndy Whitcroft int ret = -EINVAL; 15475ad333ebSAndy Whitcroft 15485ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 15495ad333ebSAndy Whitcroft if (!PageLRU(page)) 15505ad333ebSAndy Whitcroft return ret; 15515ad333ebSAndy Whitcroft 1552e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1553e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1554894bc310SLee Schermerhorn return ret; 1555894bc310SLee Schermerhorn 15565ad333ebSAndy Whitcroft ret = -EBUSY; 155708e552c6SKAMEZAWA Hiroyuki 1558c8244935SMel Gorman /* 1559c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1560c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1561c8244935SMel Gorman * blocking - clean pages for the most part. 1562c8244935SMel Gorman * 1563c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1564c8244935SMel Gorman * that it is possible to migrate without blocking 1565c8244935SMel Gorman */ 15661276ad68SJohannes Weiner if (mode & ISOLATE_ASYNC_MIGRATE) { 1567c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1568c8244935SMel Gorman if (PageWriteback(page)) 156939deaf85SMinchan Kim return ret; 157039deaf85SMinchan Kim 1571c8244935SMel Gorman if (PageDirty(page)) { 1572c8244935SMel Gorman struct address_space *mapping; 157369d763fcSMel Gorman bool migrate_dirty; 1574c8244935SMel Gorman 1575c8244935SMel Gorman /* 1576c8244935SMel Gorman * Only pages without mappings or that have a 1577c8244935SMel Gorman * ->migratepage callback are possible to migrate 157869d763fcSMel Gorman * without blocking. However, we can be racing with 157969d763fcSMel Gorman * truncation so it's necessary to lock the page 158069d763fcSMel Gorman * to stabilise the mapping as truncation holds 158169d763fcSMel Gorman * the page lock until after the page is removed 158269d763fcSMel Gorman * from the page cache. 1583c8244935SMel Gorman */ 158469d763fcSMel Gorman if (!trylock_page(page)) 158569d763fcSMel Gorman return ret; 158669d763fcSMel Gorman 1587c8244935SMel Gorman mapping = page_mapping(page); 1588145e1a71SHugh Dickins migrate_dirty = !mapping || mapping->a_ops->migratepage; 158969d763fcSMel Gorman unlock_page(page); 159069d763fcSMel Gorman if (!migrate_dirty) 1591c8244935SMel Gorman return ret; 1592c8244935SMel Gorman } 1593c8244935SMel Gorman } 1594c8244935SMel Gorman 1595f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1596f80c0673SMinchan Kim return ret; 1597f80c0673SMinchan Kim 15985ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 15995ad333ebSAndy Whitcroft /* 16005ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 16015ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 16025ad333ebSAndy Whitcroft * page release code relies on it. 16035ad333ebSAndy Whitcroft */ 16045ad333ebSAndy Whitcroft ClearPageLRU(page); 16055ad333ebSAndy Whitcroft ret = 0; 16065ad333ebSAndy Whitcroft } 16075ad333ebSAndy Whitcroft 16085ad333ebSAndy Whitcroft return ret; 16095ad333ebSAndy Whitcroft } 16105ad333ebSAndy Whitcroft 16117ee36a14SMel Gorman 16127ee36a14SMel Gorman /* 16137ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 161455b65a57SEthon Paul * be complete before mem_cgroup_update_lru_size due to a sanity check. 16157ee36a14SMel Gorman */ 16167ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 1617b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 16187ee36a14SMel Gorman { 16197ee36a14SMel Gorman int zid; 16207ee36a14SMel Gorman 16217ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 16227ee36a14SMel Gorman if (!nr_zone_taken[zid]) 16237ee36a14SMel Gorman continue; 16247ee36a14SMel Gorman 1625a892cb6bSWei Yang update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 16267ee36a14SMel Gorman } 16277ee36a14SMel Gorman 16287ee36a14SMel Gorman } 16297ee36a14SMel Gorman 1630f4b7e272SAndrey Ryabinin /** 1631f4b7e272SAndrey Ryabinin * pgdat->lru_lock is heavily contended. Some of the functions that 16321da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 16331da177e4SLinus Torvalds * and working on them outside the LRU lock. 16341da177e4SLinus Torvalds * 16351da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 16361da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 16371da177e4SLinus Torvalds * 16381da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 16391da177e4SLinus Torvalds * 1640791b48b6SMinchan Kim * @nr_to_scan: The number of eligible pages to look through on the list. 16415dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 16421da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1643f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1644fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 16453cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 16461da177e4SLinus Torvalds * 16471da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 16481da177e4SLinus Torvalds */ 164969e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 16505dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1651fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 1652a9e7c39fSKirill Tkhai enum lru_list lru) 16531da177e4SLinus Torvalds { 165475b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 165569e05944SAndrew Morton unsigned long nr_taken = 0; 1656599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 16577cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 16583db65812SJohannes Weiner unsigned long skipped = 0; 1659791b48b6SMinchan Kim unsigned long scan, total_scan, nr_pages; 1660b2e18757SMel Gorman LIST_HEAD(pages_skipped); 1661a9e7c39fSKirill Tkhai isolate_mode_t mode = (sc->may_unmap ? 0 : ISOLATE_UNMAPPED); 16621da177e4SLinus Torvalds 166398879b3bSYang Shi total_scan = 0; 1664791b48b6SMinchan Kim scan = 0; 166598879b3bSYang Shi while (scan < nr_to_scan && !list_empty(src)) { 16665ad333ebSAndy Whitcroft struct page *page; 16675ad333ebSAndy Whitcroft 16681da177e4SLinus Torvalds page = lru_to_page(src); 16691da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 16701da177e4SLinus Torvalds 1671309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 16728d438f96SNick Piggin 1673d8c6546bSMatthew Wilcox (Oracle) nr_pages = compound_nr(page); 167498879b3bSYang Shi total_scan += nr_pages; 167598879b3bSYang Shi 1676b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 1677b2e18757SMel Gorman list_move(&page->lru, &pages_skipped); 167898879b3bSYang Shi nr_skipped[page_zonenum(page)] += nr_pages; 1679b2e18757SMel Gorman continue; 1680b2e18757SMel Gorman } 1681b2e18757SMel Gorman 1682791b48b6SMinchan Kim /* 1683791b48b6SMinchan Kim * Do not count skipped pages because that makes the function 1684791b48b6SMinchan Kim * return with no isolated pages if the LRU mostly contains 1685791b48b6SMinchan Kim * ineligible pages. This causes the VM to not reclaim any 1686791b48b6SMinchan Kim * pages, triggering a premature OOM. 168798879b3bSYang Shi * 168898879b3bSYang Shi * Account all tail pages of THP. This would not cause 168998879b3bSYang Shi * premature OOM since __isolate_lru_page() returns -EBUSY 169098879b3bSYang Shi * only when the page is being freed somewhere else. 1691791b48b6SMinchan Kim */ 169298879b3bSYang Shi scan += nr_pages; 1693f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 16945ad333ebSAndy Whitcroft case 0: 1695599d0c95SMel Gorman nr_taken += nr_pages; 1696599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 16975ad333ebSAndy Whitcroft list_move(&page->lru, dst); 16985ad333ebSAndy Whitcroft break; 16997c8ee9a8SNick Piggin 17005ad333ebSAndy Whitcroft case -EBUSY: 17015ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 17025ad333ebSAndy Whitcroft list_move(&page->lru, src); 17035ad333ebSAndy Whitcroft continue; 17045ad333ebSAndy Whitcroft 17055ad333ebSAndy Whitcroft default: 17065ad333ebSAndy Whitcroft BUG(); 17075ad333ebSAndy Whitcroft } 17085ad333ebSAndy Whitcroft } 17091da177e4SLinus Torvalds 1710b2e18757SMel Gorman /* 1711b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 1712b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 1713b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 1714b2e18757SMel Gorman * scanning would soon rescan the same pages to skip and put the 1715b2e18757SMel Gorman * system at risk of premature OOM. 1716b2e18757SMel Gorman */ 17177cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 17187cc30fcfSMel Gorman int zid; 17197cc30fcfSMel Gorman 17203db65812SJohannes Weiner list_splice(&pages_skipped, src); 17217cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 17227cc30fcfSMel Gorman if (!nr_skipped[zid]) 17237cc30fcfSMel Gorman continue; 17247cc30fcfSMel Gorman 17257cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 17261265e3a6SMichal Hocko skipped += nr_skipped[zid]; 17277cc30fcfSMel Gorman } 17287cc30fcfSMel Gorman } 1729791b48b6SMinchan Kim *nr_scanned = total_scan; 17301265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 1731791b48b6SMinchan Kim total_scan, skipped, nr_taken, mode, lru); 1732b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 17331da177e4SLinus Torvalds return nr_taken; 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds 173662695a84SNick Piggin /** 173762695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 173862695a84SNick Piggin * @page: page to isolate from its LRU list 173962695a84SNick Piggin * 174062695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 174162695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 174262695a84SNick Piggin * 174362695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 174462695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 174562695a84SNick Piggin * 174662695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1747894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1748894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1749894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 175062695a84SNick Piggin * 175162695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 175262695a84SNick Piggin * found will be decremented. 175362695a84SNick Piggin * 175462695a84SNick Piggin * Restrictions: 1755a5d09bedSMike Rapoport * 175662695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 175762695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 175862695a84SNick Piggin * without a stable reference). 175962695a84SNick Piggin * (2) the lru_lock must not be held. 176062695a84SNick Piggin * (3) interrupts must be enabled. 176162695a84SNick Piggin */ 176262695a84SNick Piggin int isolate_lru_page(struct page *page) 176362695a84SNick Piggin { 176462695a84SNick Piggin int ret = -EBUSY; 176562695a84SNick Piggin 1766309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 1767cf2a82eeSKirill A. Shutemov WARN_RATELIMIT(PageTail(page), "trying to isolate tail page"); 17680c917313SKonstantin Khlebnikov 176962695a84SNick Piggin if (PageLRU(page)) { 1770f4b7e272SAndrey Ryabinin pg_data_t *pgdat = page_pgdat(page); 1771fa9add64SHugh Dickins struct lruvec *lruvec; 177262695a84SNick Piggin 1773f4b7e272SAndrey Ryabinin spin_lock_irq(&pgdat->lru_lock); 1774f4b7e272SAndrey Ryabinin lruvec = mem_cgroup_page_lruvec(page, pgdat); 17750c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1776894bc310SLee Schermerhorn int lru = page_lru(page); 17770c917313SKonstantin Khlebnikov get_page(page); 177862695a84SNick Piggin ClearPageLRU(page); 1779fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1780fa9add64SHugh Dickins ret = 0; 178162695a84SNick Piggin } 1782f4b7e272SAndrey Ryabinin spin_unlock_irq(&pgdat->lru_lock); 178362695a84SNick Piggin } 178462695a84SNick Piggin return ret; 178562695a84SNick Piggin } 178662695a84SNick Piggin 17875ad333ebSAndy Whitcroft /* 1788d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1789178821b8SXianting Tian * then get rescheduled. When there are massive number of tasks doing page 1790d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1791d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1792d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 179335cd7815SRik van Riel */ 1794599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 179535cd7815SRik van Riel struct scan_control *sc) 179635cd7815SRik van Riel { 179735cd7815SRik van Riel unsigned long inactive, isolated; 179835cd7815SRik van Riel 179935cd7815SRik van Riel if (current_is_kswapd()) 180035cd7815SRik van Riel return 0; 180135cd7815SRik van Riel 1802b5ead35eSJohannes Weiner if (!writeback_throttling_sane(sc)) 180335cd7815SRik van Riel return 0; 180435cd7815SRik van Riel 180535cd7815SRik van Riel if (file) { 1806599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 1807599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 180835cd7815SRik van Riel } else { 1809599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 1810599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 181135cd7815SRik van Riel } 181235cd7815SRik van Riel 18133cf23841SFengguang Wu /* 18143cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 18153cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 18163cf23841SFengguang Wu * deadlock. 18173cf23841SFengguang Wu */ 1818d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 18193cf23841SFengguang Wu inactive >>= 3; 18203cf23841SFengguang Wu 182135cd7815SRik van Riel return isolated > inactive; 182235cd7815SRik van Riel } 182335cd7815SRik van Riel 1824a222f341SKirill Tkhai /* 1825a222f341SKirill Tkhai * This moves pages from @list to corresponding LRU list. 1826a222f341SKirill Tkhai * 1827a222f341SKirill Tkhai * We move them the other way if the page is referenced by one or more 1828a222f341SKirill Tkhai * processes, from rmap. 1829a222f341SKirill Tkhai * 1830a222f341SKirill Tkhai * If the pages are mostly unmapped, the processing is fast and it is 1831a222f341SKirill Tkhai * appropriate to hold zone_lru_lock across the whole operation. But if 1832a222f341SKirill Tkhai * the pages are mapped, the processing is slow (page_referenced()) so we 1833a222f341SKirill Tkhai * should drop zone_lru_lock around each page. It's impossible to balance 1834a222f341SKirill Tkhai * this, so instead we remove the pages from the LRU while processing them. 1835a222f341SKirill Tkhai * It is safe to rely on PG_active against the non-LRU pages in here because 1836a222f341SKirill Tkhai * nobody will play with that bit on a non-LRU page. 1837a222f341SKirill Tkhai * 1838a222f341SKirill Tkhai * The downside is that we have to touch page->_refcount against each page. 1839a222f341SKirill Tkhai * But we had to alter page->flags anyway. 1840a222f341SKirill Tkhai * 1841a222f341SKirill Tkhai * Returns the number of pages moved to the given lruvec. 1842a222f341SKirill Tkhai */ 1843a222f341SKirill Tkhai 1844a222f341SKirill Tkhai static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec, 1845a222f341SKirill Tkhai struct list_head *list) 184666635629SMel Gorman { 1847599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 1848a222f341SKirill Tkhai int nr_pages, nr_moved = 0; 18493f79768fSHugh Dickins LIST_HEAD(pages_to_free); 1850a222f341SKirill Tkhai struct page *page; 1851a222f341SKirill Tkhai enum lru_list lru; 185266635629SMel Gorman 1853a222f341SKirill Tkhai while (!list_empty(list)) { 1854a222f341SKirill Tkhai page = lru_to_page(list); 1855309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 185639b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1857a222f341SKirill Tkhai list_del(&page->lru); 1858599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 185966635629SMel Gorman putback_lru_page(page); 1860599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 186166635629SMel Gorman continue; 186266635629SMel Gorman } 1863599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 1864fa9add64SHugh Dickins 18657a608572SLinus Torvalds SetPageLRU(page); 186666635629SMel Gorman lru = page_lru(page); 1867a222f341SKirill Tkhai 18686c357848SMatthew Wilcox (Oracle) nr_pages = thp_nr_pages(page); 1869a222f341SKirill Tkhai update_lru_size(lruvec, lru, page_zonenum(page), nr_pages); 1870a222f341SKirill Tkhai list_move(&page->lru, &lruvec->lists[lru]); 1871fa9add64SHugh Dickins 18722bcf8879SHugh Dickins if (put_page_testzero(page)) { 18732bcf8879SHugh Dickins __ClearPageLRU(page); 18742bcf8879SHugh Dickins __ClearPageActive(page); 1875fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 18762bcf8879SHugh Dickins 18772bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 1878599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1879ff45fc3cSMatthew Wilcox (Oracle) destroy_compound_page(page); 1880599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 18812bcf8879SHugh Dickins } else 18822bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 1883a222f341SKirill Tkhai } else { 1884a222f341SKirill Tkhai nr_moved += nr_pages; 188531d8fcacSJohannes Weiner if (PageActive(page)) 188631d8fcacSJohannes Weiner workingset_age_nonresident(lruvec, nr_pages); 188766635629SMel Gorman } 188866635629SMel Gorman } 188966635629SMel Gorman 18903f79768fSHugh Dickins /* 18913f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 18923f79768fSHugh Dickins */ 1893a222f341SKirill Tkhai list_splice(&pages_to_free, list); 1894a222f341SKirill Tkhai 1895a222f341SKirill Tkhai return nr_moved; 189666635629SMel Gorman } 189766635629SMel Gorman 189866635629SMel Gorman /* 1899399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 1900a37b0715SNeilBrown * a backing device by writing to the page cache it sets PF_LOCAL_THROTTLE. 1901399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 1902399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 1903399ba0b9SNeilBrown */ 1904399ba0b9SNeilBrown static int current_may_throttle(void) 1905399ba0b9SNeilBrown { 1906a37b0715SNeilBrown return !(current->flags & PF_LOCAL_THROTTLE) || 1907399ba0b9SNeilBrown current->backing_dev_info == NULL || 1908399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 1909399ba0b9SNeilBrown } 1910399ba0b9SNeilBrown 1911399ba0b9SNeilBrown /* 1912b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 19131742f19fSAndrew Morton * of reclaimed pages 19141da177e4SLinus Torvalds */ 191566635629SMel Gorman static noinline_for_stack unsigned long 19161a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 19179e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 19181da177e4SLinus Torvalds { 19191da177e4SLinus Torvalds LIST_HEAD(page_list); 1920e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 1921730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 1922e247dbceSKOSAKI Motohiro unsigned long nr_taken; 1923060f005fSKirill Tkhai struct reclaim_stat stat; 1924497a6c1bSJohannes Weiner bool file = is_file_lru(lru); 1925f46b7912SKirill Tkhai enum vm_event_item item; 1926599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 1927db73ee0dSMichal Hocko bool stalled = false; 192878dc583dSKOSAKI Motohiro 1929599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 1930db73ee0dSMichal Hocko if (stalled) 1931db73ee0dSMichal Hocko return 0; 1932db73ee0dSMichal Hocko 1933db73ee0dSMichal Hocko /* wait a bit for the reclaimer. */ 1934db73ee0dSMichal Hocko msleep(100); 1935db73ee0dSMichal Hocko stalled = true; 193635cd7815SRik van Riel 193735cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 193835cd7815SRik van Riel if (fatal_signal_pending(current)) 193935cd7815SRik van Riel return SWAP_CLUSTER_MAX; 194035cd7815SRik van Riel } 194135cd7815SRik van Riel 19421da177e4SLinus Torvalds lru_add_drain(); 1943f80c0673SMinchan Kim 1944599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19451da177e4SLinus Torvalds 19465dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 1947a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 194895d918fcSKonstantin Khlebnikov 1949599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 1950f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT; 1951b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 1952f46b7912SKirill Tkhai __count_vm_events(item, nr_scanned); 1953f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned); 1954497a6c1bSJohannes Weiner __count_vm_events(PGSCAN_ANON + file, nr_scanned); 1955497a6c1bSJohannes Weiner 1956599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1957d563c050SHillf Danton 1958d563c050SHillf Danton if (nr_taken == 0) 195966635629SMel Gorman return 0; 1960b35ea17bSKOSAKI Motohiro 1961a128ca71SShaohua Li nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0, 19623c710c1aSMichal Hocko &stat, false); 1963c661b078SAndy Whitcroft 1964599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19653f79768fSHugh Dickins 1966497a6c1bSJohannes Weiner move_pages_to_lru(lruvec, &page_list); 1967497a6c1bSJohannes Weiner 1968497a6c1bSJohannes Weiner __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 196996f8bf4fSJohannes Weiner lru_note_cost(lruvec, file, stat.nr_pageout); 1970f46b7912SKirill Tkhai item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT; 1971b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 1972f46b7912SKirill Tkhai __count_vm_events(item, nr_reclaimed); 1973f46b7912SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed); 1974497a6c1bSJohannes Weiner __count_vm_events(PGSTEAL_ANON + file, nr_reclaimed); 19753f79768fSHugh Dickins 1976599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 19773f79768fSHugh Dickins 1978747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 19792d4894b5SMel Gorman free_unref_page_list(&page_list); 1980e11da5b4SMel Gorman 198192df3a72SMel Gorman /* 19821c610d5fSAndrey Ryabinin * If dirty pages are scanned that are not queued for IO, it 19831c610d5fSAndrey Ryabinin * implies that flushers are not doing their job. This can 19841c610d5fSAndrey Ryabinin * happen when memory pressure pushes dirty pages to the end of 19851c610d5fSAndrey Ryabinin * the LRU before the dirty limits are breached and the dirty 19861c610d5fSAndrey Ryabinin * data has expired. It can also happen when the proportion of 19871c610d5fSAndrey Ryabinin * dirty pages grows not through writes but through memory 19881c610d5fSAndrey Ryabinin * pressure reclaiming all the clean cache. And in some cases, 19891c610d5fSAndrey Ryabinin * the flushers simply cannot keep up with the allocation 19901c610d5fSAndrey Ryabinin * rate. Nudge the flusher threads in case they are asleep. 19911c610d5fSAndrey Ryabinin */ 19921c610d5fSAndrey Ryabinin if (stat.nr_unqueued_dirty == nr_taken) 19931c610d5fSAndrey Ryabinin wakeup_flusher_threads(WB_REASON_VMSCAN); 19941c610d5fSAndrey Ryabinin 1995d108c772SAndrey Ryabinin sc->nr.dirty += stat.nr_dirty; 1996d108c772SAndrey Ryabinin sc->nr.congested += stat.nr_congested; 1997d108c772SAndrey Ryabinin sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; 1998d108c772SAndrey Ryabinin sc->nr.writeback += stat.nr_writeback; 1999d108c772SAndrey Ryabinin sc->nr.immediate += stat.nr_immediate; 2000d108c772SAndrey Ryabinin sc->nr.taken += nr_taken; 2001d108c772SAndrey Ryabinin if (file) 2002d108c772SAndrey Ryabinin sc->nr.file_taken += nr_taken; 20038e950282SMel Gorman 2004599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 2005d51d1e64SSteven Rostedt nr_scanned, nr_reclaimed, &stat, sc->priority, file); 200605ff5137SAndrew Morton return nr_reclaimed; 20071da177e4SLinus Torvalds } 20081da177e4SLinus Torvalds 2009f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 20101a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 2011f16015fbSJohannes Weiner struct scan_control *sc, 20129e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 20131cfb419bSKAMEZAWA Hiroyuki { 201444c241f1SKOSAKI Motohiro unsigned long nr_taken; 2015f626012dSHugh Dickins unsigned long nr_scanned; 20166fe6b7e3SWu Fengguang unsigned long vm_flags; 20171cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 20188cab4754SWu Fengguang LIST_HEAD(l_active); 2019b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 20201cfb419bSKAMEZAWA Hiroyuki struct page *page; 20219d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 20229d998b4fSMichal Hocko unsigned nr_rotated = 0; 20233cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 2024599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 20251cfb419bSKAMEZAWA Hiroyuki 20261da177e4SLinus Torvalds lru_add_drain(); 2027f80c0673SMinchan Kim 2028599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 2029925b7673SJohannes Weiner 20305dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 2031a9e7c39fSKirill Tkhai &nr_scanned, sc, lru); 203289b5fae5SJohannes Weiner 2033599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 20341cfb419bSKAMEZAWA Hiroyuki 2035912c0572SShakeel Butt if (!cgroup_reclaim(sc)) 2036599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 20372fa2690cSYafang Shao __count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); 20389d5e6a9fSHugh Dickins 2039599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 20421da177e4SLinus Torvalds cond_resched(); 20431da177e4SLinus Torvalds page = lru_to_page(&l_hold); 20441da177e4SLinus Torvalds list_del(&page->lru); 20457e9cd484SRik van Riel 204639b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 2047894bc310SLee Schermerhorn putback_lru_page(page); 2048894bc310SLee Schermerhorn continue; 2049894bc310SLee Schermerhorn } 2050894bc310SLee Schermerhorn 2051cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 2052cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 2053cc715d99SMel Gorman if (page_has_private(page)) 2054cc715d99SMel Gorman try_to_release_page(page, 0); 2055cc715d99SMel Gorman unlock_page(page); 2056cc715d99SMel Gorman } 2057cc715d99SMel Gorman } 2058cc715d99SMel Gorman 2059c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 2060c3ac9a8aSJohannes Weiner &vm_flags)) { 20618cab4754SWu Fengguang /* 20628cab4754SWu Fengguang * Identify referenced, file-backed active pages and 20638cab4754SWu Fengguang * give them one more trip around the active list. So 20648cab4754SWu Fengguang * that executable code get better chances to stay in 20658cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 20668cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 20678cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 20688cab4754SWu Fengguang * so we ignore them here. 20698cab4754SWu Fengguang */ 20709de4f22aSHuang Ying if ((vm_flags & VM_EXEC) && page_is_file_lru(page)) { 20716c357848SMatthew Wilcox (Oracle) nr_rotated += thp_nr_pages(page); 20728cab4754SWu Fengguang list_add(&page->lru, &l_active); 20738cab4754SWu Fengguang continue; 20748cab4754SWu Fengguang } 20758cab4754SWu Fengguang } 20767e9cd484SRik van Riel 20775205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 20781899ad18SJohannes Weiner SetPageWorkingset(page); 20791da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 20801da177e4SLinus Torvalds } 20811da177e4SLinus Torvalds 2082b555749aSAndrew Morton /* 20838cab4754SWu Fengguang * Move pages back to the lru list. 2084b555749aSAndrew Morton */ 2085599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 2086556adecbSRik van Riel 2087a222f341SKirill Tkhai nr_activate = move_pages_to_lru(lruvec, &l_active); 2088a222f341SKirill Tkhai nr_deactivate = move_pages_to_lru(lruvec, &l_inactive); 2089f372d89eSKirill Tkhai /* Keep all free pages in l_active list */ 2090f372d89eSKirill Tkhai list_splice(&l_inactive, &l_active); 20919851ac13SKirill Tkhai 20929851ac13SKirill Tkhai __count_vm_events(PGDEACTIVATE, nr_deactivate); 20939851ac13SKirill Tkhai __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate); 20949851ac13SKirill Tkhai 2095599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 2096599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 20972bcf8879SHugh Dickins 2098f372d89eSKirill Tkhai mem_cgroup_uncharge_list(&l_active); 2099f372d89eSKirill Tkhai free_unref_page_list(&l_active); 21009d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 21019d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 21021da177e4SLinus Torvalds } 21031da177e4SLinus Torvalds 21041a4e58ccSMinchan Kim unsigned long reclaim_pages(struct list_head *page_list) 21051a4e58ccSMinchan Kim { 2106f661d007SYang Shi int nid = NUMA_NO_NODE; 2107730ec8c0SManinder Singh unsigned int nr_reclaimed = 0; 21081a4e58ccSMinchan Kim LIST_HEAD(node_page_list); 21091a4e58ccSMinchan Kim struct reclaim_stat dummy_stat; 21101a4e58ccSMinchan Kim struct page *page; 21111a4e58ccSMinchan Kim struct scan_control sc = { 21121a4e58ccSMinchan Kim .gfp_mask = GFP_KERNEL, 21131a4e58ccSMinchan Kim .priority = DEF_PRIORITY, 21141a4e58ccSMinchan Kim .may_writepage = 1, 21151a4e58ccSMinchan Kim .may_unmap = 1, 21161a4e58ccSMinchan Kim .may_swap = 1, 21171a4e58ccSMinchan Kim }; 21181a4e58ccSMinchan Kim 21191a4e58ccSMinchan Kim while (!list_empty(page_list)) { 21201a4e58ccSMinchan Kim page = lru_to_page(page_list); 2121f661d007SYang Shi if (nid == NUMA_NO_NODE) { 21221a4e58ccSMinchan Kim nid = page_to_nid(page); 21231a4e58ccSMinchan Kim INIT_LIST_HEAD(&node_page_list); 21241a4e58ccSMinchan Kim } 21251a4e58ccSMinchan Kim 21261a4e58ccSMinchan Kim if (nid == page_to_nid(page)) { 21271a4e58ccSMinchan Kim ClearPageActive(page); 21281a4e58ccSMinchan Kim list_move(&page->lru, &node_page_list); 21291a4e58ccSMinchan Kim continue; 21301a4e58ccSMinchan Kim } 21311a4e58ccSMinchan Kim 21321a4e58ccSMinchan Kim nr_reclaimed += shrink_page_list(&node_page_list, 21331a4e58ccSMinchan Kim NODE_DATA(nid), 21341a4e58ccSMinchan Kim &sc, 0, 21351a4e58ccSMinchan Kim &dummy_stat, false); 21361a4e58ccSMinchan Kim while (!list_empty(&node_page_list)) { 21371a4e58ccSMinchan Kim page = lru_to_page(&node_page_list); 21381a4e58ccSMinchan Kim list_del(&page->lru); 21391a4e58ccSMinchan Kim putback_lru_page(page); 21401a4e58ccSMinchan Kim } 21411a4e58ccSMinchan Kim 2142f661d007SYang Shi nid = NUMA_NO_NODE; 21431a4e58ccSMinchan Kim } 21441a4e58ccSMinchan Kim 21451a4e58ccSMinchan Kim if (!list_empty(&node_page_list)) { 21461a4e58ccSMinchan Kim nr_reclaimed += shrink_page_list(&node_page_list, 21471a4e58ccSMinchan Kim NODE_DATA(nid), 21481a4e58ccSMinchan Kim &sc, 0, 21491a4e58ccSMinchan Kim &dummy_stat, false); 21501a4e58ccSMinchan Kim while (!list_empty(&node_page_list)) { 21511a4e58ccSMinchan Kim page = lru_to_page(&node_page_list); 21521a4e58ccSMinchan Kim list_del(&page->lru); 21531a4e58ccSMinchan Kim putback_lru_page(page); 21541a4e58ccSMinchan Kim } 21551a4e58ccSMinchan Kim } 21561a4e58ccSMinchan Kim 21571a4e58ccSMinchan Kim return nr_reclaimed; 21581a4e58ccSMinchan Kim } 21591a4e58ccSMinchan Kim 2160b91ac374SJohannes Weiner static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 2161b91ac374SJohannes Weiner struct lruvec *lruvec, struct scan_control *sc) 2162b91ac374SJohannes Weiner { 2163b91ac374SJohannes Weiner if (is_active_lru(lru)) { 2164b91ac374SJohannes Weiner if (sc->may_deactivate & (1 << is_file_lru(lru))) 2165b91ac374SJohannes Weiner shrink_active_list(nr_to_scan, lruvec, sc, lru); 2166b91ac374SJohannes Weiner else 2167b91ac374SJohannes Weiner sc->skipped_deactivate = 1; 2168b91ac374SJohannes Weiner return 0; 2169b91ac374SJohannes Weiner } 2170b91ac374SJohannes Weiner 2171b91ac374SJohannes Weiner return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2172b91ac374SJohannes Weiner } 2173b91ac374SJohannes Weiner 217459dc76b0SRik van Riel /* 217559dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 217659dc76b0SRik van Riel * to do too much work. 217714797e23SKOSAKI Motohiro * 217859dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 217959dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 218059dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 218159dc76b0SRik van Riel * 218259dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 218359dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 218459dc76b0SRik van Riel * 21852a2e4885SJohannes Weiner * If that fails and refaulting is observed, the inactive list grows. 21862a2e4885SJohannes Weiner * 218759dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 21883a50d14dSAndrey Ryabinin * on this LRU, maintained by the pageout code. An inactive_ratio 218959dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 219059dc76b0SRik van Riel * 219159dc76b0SRik van Riel * total target max 219259dc76b0SRik van Riel * memory ratio inactive 219359dc76b0SRik van Riel * ------------------------------------- 219459dc76b0SRik van Riel * 10MB 1 5MB 219559dc76b0SRik van Riel * 100MB 1 50MB 219659dc76b0SRik van Riel * 1GB 3 250MB 219759dc76b0SRik van Riel * 10GB 10 0.9GB 219859dc76b0SRik van Riel * 100GB 31 3GB 219959dc76b0SRik van Riel * 1TB 101 10GB 220059dc76b0SRik van Riel * 10TB 320 32GB 220114797e23SKOSAKI Motohiro */ 2202b91ac374SJohannes Weiner static bool inactive_is_low(struct lruvec *lruvec, enum lru_list inactive_lru) 220314797e23SKOSAKI Motohiro { 2204b91ac374SJohannes Weiner enum lru_list active_lru = inactive_lru + LRU_ACTIVE; 22052a2e4885SJohannes Weiner unsigned long inactive, active; 22062a2e4885SJohannes Weiner unsigned long inactive_ratio; 220759dc76b0SRik van Riel unsigned long gb; 220859dc76b0SRik van Riel 2209b91ac374SJohannes Weiner inactive = lruvec_page_state(lruvec, NR_LRU_BASE + inactive_lru); 2210b91ac374SJohannes Weiner active = lruvec_page_state(lruvec, NR_LRU_BASE + active_lru); 2211f8d1a311SMel Gorman 221259dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 22134002570cSJoonsoo Kim if (gb) 221459dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2215b39415b2SRik van Riel else 221659dc76b0SRik van Riel inactive_ratio = 1; 2217fd538803SMichal Hocko 221859dc76b0SRik van Riel return inactive * inactive_ratio < active; 2219b39415b2SRik van Riel } 2220b39415b2SRik van Riel 22219a265114SJohannes Weiner enum scan_balance { 22229a265114SJohannes Weiner SCAN_EQUAL, 22239a265114SJohannes Weiner SCAN_FRACT, 22249a265114SJohannes Weiner SCAN_ANON, 22259a265114SJohannes Weiner SCAN_FILE, 22269a265114SJohannes Weiner }; 22279a265114SJohannes Weiner 22281da177e4SLinus Torvalds /* 22294f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 22304f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 22314f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 22324f98a2feSRik van Riel * onto the active list instead of evict. 22334f98a2feSRik van Riel * 2234be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2235be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 22364f98a2feSRik van Riel */ 2237afaf07a6SJohannes Weiner static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, 2238afaf07a6SJohannes Weiner unsigned long *nr) 22394f98a2feSRik van Riel { 2240afaf07a6SJohannes Weiner struct mem_cgroup *memcg = lruvec_memcg(lruvec); 2241d483a5ddSJohannes Weiner unsigned long anon_cost, file_cost, total_cost; 224233377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 22439a265114SJohannes Weiner u64 fraction[2]; 22449a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 22459a265114SJohannes Weiner enum scan_balance scan_balance; 22469a265114SJohannes Weiner unsigned long ap, fp; 22479a265114SJohannes Weiner enum lru_list lru; 224876a33fc3SShaohua Li 224976a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2250d8b38438SVladimir Davydov if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) { 22519a265114SJohannes Weiner scan_balance = SCAN_FILE; 225276a33fc3SShaohua Li goto out; 225376a33fc3SShaohua Li } 22544f98a2feSRik van Riel 225510316b31SJohannes Weiner /* 225610316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 225710316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 225810316b31SJohannes Weiner * disable swapping for individual groups completely when 225910316b31SJohannes Weiner * using the memory controller's swap limit feature would be 226010316b31SJohannes Weiner * too expensive. 226110316b31SJohannes Weiner */ 2262b5ead35eSJohannes Weiner if (cgroup_reclaim(sc) && !swappiness) { 22639a265114SJohannes Weiner scan_balance = SCAN_FILE; 226410316b31SJohannes Weiner goto out; 226510316b31SJohannes Weiner } 226610316b31SJohannes Weiner 226710316b31SJohannes Weiner /* 226810316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 226910316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 227010316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 227110316b31SJohannes Weiner */ 227202695175SJohannes Weiner if (!sc->priority && swappiness) { 22739a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 227410316b31SJohannes Weiner goto out; 227510316b31SJohannes Weiner } 227610316b31SJohannes Weiner 227711d16c25SJohannes Weiner /* 227853138ceaSJohannes Weiner * If the system is almost out of file pages, force-scan anon. 227962376251SJohannes Weiner */ 2280b91ac374SJohannes Weiner if (sc->file_is_tiny) { 228162376251SJohannes Weiner scan_balance = SCAN_ANON; 228262376251SJohannes Weiner goto out; 228362376251SJohannes Weiner } 228462376251SJohannes Weiner 228562376251SJohannes Weiner /* 2286b91ac374SJohannes Weiner * If there is enough inactive page cache, we do not reclaim 2287b91ac374SJohannes Weiner * anything from the anonymous working right now. 2288e9868505SRik van Riel */ 2289b91ac374SJohannes Weiner if (sc->cache_trim_mode) { 22909a265114SJohannes Weiner scan_balance = SCAN_FILE; 2291e9868505SRik van Riel goto out; 22924f98a2feSRik van Riel } 22934f98a2feSRik van Riel 22949a265114SJohannes Weiner scan_balance = SCAN_FRACT; 22954f98a2feSRik van Riel /* 2296314b57fbSJohannes Weiner * Calculate the pressure balance between anon and file pages. 2297314b57fbSJohannes Weiner * 2298314b57fbSJohannes Weiner * The amount of pressure we put on each LRU is inversely 2299314b57fbSJohannes Weiner * proportional to the cost of reclaiming each list, as 2300314b57fbSJohannes Weiner * determined by the share of pages that are refaulting, times 2301314b57fbSJohannes Weiner * the relative IO cost of bringing back a swapped out 2302314b57fbSJohannes Weiner * anonymous page vs reloading a filesystem page (swappiness). 2303314b57fbSJohannes Weiner * 2304d483a5ddSJohannes Weiner * Although we limit that influence to ensure no list gets 2305d483a5ddSJohannes Weiner * left behind completely: at least a third of the pressure is 2306d483a5ddSJohannes Weiner * applied, before swappiness. 2307d483a5ddSJohannes Weiner * 2308314b57fbSJohannes Weiner * With swappiness at 100, anon and file have equal IO cost. 230958c37f6eSKOSAKI Motohiro */ 2310d483a5ddSJohannes Weiner total_cost = sc->anon_cost + sc->file_cost; 2311d483a5ddSJohannes Weiner anon_cost = total_cost + sc->anon_cost; 2312d483a5ddSJohannes Weiner file_cost = total_cost + sc->file_cost; 2313d483a5ddSJohannes Weiner total_cost = anon_cost + file_cost; 231458c37f6eSKOSAKI Motohiro 2315d483a5ddSJohannes Weiner ap = swappiness * (total_cost + 1); 2316d483a5ddSJohannes Weiner ap /= anon_cost + 1; 23174f98a2feSRik van Riel 2318d483a5ddSJohannes Weiner fp = (200 - swappiness) * (total_cost + 1); 2319d483a5ddSJohannes Weiner fp /= file_cost + 1; 23204f98a2feSRik van Riel 232176a33fc3SShaohua Li fraction[0] = ap; 232276a33fc3SShaohua Li fraction[1] = fp; 2323a4fe1631SJohannes Weiner denominator = ap + fp; 232476a33fc3SShaohua Li out: 23254111304dSHugh Dickins for_each_evictable_lru(lru) { 23264111304dSHugh Dickins int file = is_file_lru(lru); 23279783aa99SChris Down unsigned long lruvec_size; 232876a33fc3SShaohua Li unsigned long scan; 23291bc63fb1SChris Down unsigned long protection; 233076a33fc3SShaohua Li 23319783aa99SChris Down lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 233222f7496fSYafang Shao protection = mem_cgroup_protection(sc->target_mem_cgroup, 233322f7496fSYafang Shao memcg, 23341bc63fb1SChris Down sc->memcg_low_reclaim); 23359783aa99SChris Down 23361bc63fb1SChris Down if (protection) { 23379783aa99SChris Down /* 23389783aa99SChris Down * Scale a cgroup's reclaim pressure by proportioning 23399783aa99SChris Down * its current usage to its memory.low or memory.min 23409783aa99SChris Down * setting. 23419783aa99SChris Down * 23429783aa99SChris Down * This is important, as otherwise scanning aggression 23439783aa99SChris Down * becomes extremely binary -- from nothing as we 23449783aa99SChris Down * approach the memory protection threshold, to totally 23459783aa99SChris Down * nominal as we exceed it. This results in requiring 23469783aa99SChris Down * setting extremely liberal protection thresholds. It 23479783aa99SChris Down * also means we simply get no protection at all if we 23489783aa99SChris Down * set it too low, which is not ideal. 23491bc63fb1SChris Down * 23501bc63fb1SChris Down * If there is any protection in place, we reduce scan 23511bc63fb1SChris Down * pressure by how much of the total memory used is 23521bc63fb1SChris Down * within protection thresholds. 23539783aa99SChris Down * 23549de7ca46SChris Down * There is one special case: in the first reclaim pass, 23559de7ca46SChris Down * we skip over all groups that are within their low 23569de7ca46SChris Down * protection. If that fails to reclaim enough pages to 23579de7ca46SChris Down * satisfy the reclaim goal, we come back and override 23589de7ca46SChris Down * the best-effort low protection. However, we still 23599de7ca46SChris Down * ideally want to honor how well-behaved groups are in 23609de7ca46SChris Down * that case instead of simply punishing them all 23619de7ca46SChris Down * equally. As such, we reclaim them based on how much 23621bc63fb1SChris Down * memory they are using, reducing the scan pressure 23631bc63fb1SChris Down * again by how much of the total memory used is under 23641bc63fb1SChris Down * hard protection. 23659783aa99SChris Down */ 23661bc63fb1SChris Down unsigned long cgroup_size = mem_cgroup_size(memcg); 23671bc63fb1SChris Down 23681bc63fb1SChris Down /* Avoid TOCTOU with earlier protection check */ 23691bc63fb1SChris Down cgroup_size = max(cgroup_size, protection); 23701bc63fb1SChris Down 23711bc63fb1SChris Down scan = lruvec_size - lruvec_size * protection / 23721bc63fb1SChris Down cgroup_size; 23739783aa99SChris Down 23749783aa99SChris Down /* 23751bc63fb1SChris Down * Minimally target SWAP_CLUSTER_MAX pages to keep 237655b65a57SEthon Paul * reclaim moving forwards, avoiding decrementing 23779de7ca46SChris Down * sc->priority further than desirable. 23789783aa99SChris Down */ 23791bc63fb1SChris Down scan = max(scan, SWAP_CLUSTER_MAX); 23809783aa99SChris Down } else { 23819783aa99SChris Down scan = lruvec_size; 23829783aa99SChris Down } 23839783aa99SChris Down 23849783aa99SChris Down scan >>= sc->priority; 23859783aa99SChris Down 2386688035f7SJohannes Weiner /* 2387688035f7SJohannes Weiner * If the cgroup's already been deleted, make sure to 2388688035f7SJohannes Weiner * scrape out the remaining cache. 2389688035f7SJohannes Weiner */ 2390688035f7SJohannes Weiner if (!scan && !mem_cgroup_online(memcg)) 23919783aa99SChris Down scan = min(lruvec_size, SWAP_CLUSTER_MAX); 23929a265114SJohannes Weiner 23939a265114SJohannes Weiner switch (scan_balance) { 23949a265114SJohannes Weiner case SCAN_EQUAL: 23959a265114SJohannes Weiner /* Scan lists relative to size */ 23969a265114SJohannes Weiner break; 23979a265114SJohannes Weiner case SCAN_FRACT: 23989a265114SJohannes Weiner /* 23999a265114SJohannes Weiner * Scan types proportional to swappiness and 24009a265114SJohannes Weiner * their relative recent reclaim efficiency. 240176073c64SGavin Shan * Make sure we don't miss the last page on 240276073c64SGavin Shan * the offlined memory cgroups because of a 240376073c64SGavin Shan * round-off error. 24049a265114SJohannes Weiner */ 240576073c64SGavin Shan scan = mem_cgroup_online(memcg) ? 240676073c64SGavin Shan div64_u64(scan * fraction[file], denominator) : 240776073c64SGavin Shan DIV64_U64_ROUND_UP(scan * fraction[file], 24086f04f48dSSuleiman Souhlal denominator); 24099a265114SJohannes Weiner break; 24109a265114SJohannes Weiner case SCAN_FILE: 24119a265114SJohannes Weiner case SCAN_ANON: 24129a265114SJohannes Weiner /* Scan one type exclusively */ 2413e072bff6SMateusz Nosek if ((scan_balance == SCAN_FILE) != file) 24149a265114SJohannes Weiner scan = 0; 24159a265114SJohannes Weiner break; 24169a265114SJohannes Weiner default: 24179a265114SJohannes Weiner /* Look ma, no brain */ 24189a265114SJohannes Weiner BUG(); 24199a265114SJohannes Weiner } 24206b4f7799SJohannes Weiner 24214111304dSHugh Dickins nr[lru] = scan; 242276a33fc3SShaohua Li } 24236e08a369SWu Fengguang } 24244f98a2feSRik van Riel 2425afaf07a6SJohannes Weiner static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) 24269b4f98cdSJohannes Weiner { 24279b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2428e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 24299b4f98cdSJohannes Weiner unsigned long nr_to_scan; 24309b4f98cdSJohannes Weiner enum lru_list lru; 24319b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 24329b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 24339b4f98cdSJohannes Weiner struct blk_plug plug; 24341a501907SMel Gorman bool scan_adjusted; 24359b4f98cdSJohannes Weiner 2436afaf07a6SJohannes Weiner get_scan_count(lruvec, sc, nr); 24379b4f98cdSJohannes Weiner 2438e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2439e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2440e82e0561SMel Gorman 24411a501907SMel Gorman /* 24421a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 24431a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 24441a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 24451a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 24461a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 24471a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 24481a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 24491a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 24501a501907SMel Gorman * dropped to zero at the first pass. 24511a501907SMel Gorman */ 2452b5ead35eSJohannes Weiner scan_adjusted = (!cgroup_reclaim(sc) && !current_is_kswapd() && 24531a501907SMel Gorman sc->priority == DEF_PRIORITY); 24541a501907SMel Gorman 24559b4f98cdSJohannes Weiner blk_start_plug(&plug); 24569b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 24579b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2458e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2459e82e0561SMel Gorman unsigned long nr_scanned; 2460e82e0561SMel Gorman 24619b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 24629b4f98cdSJohannes Weiner if (nr[lru]) { 24639b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 24649b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 24659b4f98cdSJohannes Weiner 24669b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 24673b991208SJohannes Weiner lruvec, sc); 24689b4f98cdSJohannes Weiner } 24699b4f98cdSJohannes Weiner } 2470e82e0561SMel Gorman 2471bd041733SMichal Hocko cond_resched(); 2472bd041733SMichal Hocko 2473e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2474e82e0561SMel Gorman continue; 2475e82e0561SMel Gorman 24769b4f98cdSJohannes Weiner /* 2477e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 24781a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2479e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2480e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2481e82e0561SMel Gorman * proportional to the original scan target. 2482e82e0561SMel Gorman */ 2483e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2484e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2485e82e0561SMel Gorman 24861a501907SMel Gorman /* 24871a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 24881a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 24891a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 24901a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 24911a501907SMel Gorman */ 24921a501907SMel Gorman if (!nr_file || !nr_anon) 24931a501907SMel Gorman break; 24941a501907SMel Gorman 2495e82e0561SMel Gorman if (nr_file > nr_anon) { 2496e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2497e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2498e82e0561SMel Gorman lru = LRU_BASE; 2499e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2500e82e0561SMel Gorman } else { 2501e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2502e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2503e82e0561SMel Gorman lru = LRU_FILE; 2504e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2505e82e0561SMel Gorman } 2506e82e0561SMel Gorman 2507e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2508e82e0561SMel Gorman nr[lru] = 0; 2509e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2510e82e0561SMel Gorman 2511e82e0561SMel Gorman /* 2512e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2513e82e0561SMel Gorman * scan target and the percentage scanning already complete 2514e82e0561SMel Gorman */ 2515e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2516e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2517e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2518e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2519e82e0561SMel Gorman 2520e82e0561SMel Gorman lru += LRU_ACTIVE; 2521e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2522e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2523e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2524e82e0561SMel Gorman 2525e82e0561SMel Gorman scan_adjusted = true; 25269b4f98cdSJohannes Weiner } 25279b4f98cdSJohannes Weiner blk_finish_plug(&plug); 25289b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 25299b4f98cdSJohannes Weiner 25309b4f98cdSJohannes Weiner /* 25319b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 25329b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 25339b4f98cdSJohannes Weiner */ 2534b91ac374SJohannes Weiner if (total_swap_pages && inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 25359b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 25369b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 25379b4f98cdSJohannes Weiner } 25389b4f98cdSJohannes Weiner 253923b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 25409e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 254123b9da55SMel Gorman { 2542d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 254323b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 25449e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 254523b9da55SMel Gorman return true; 254623b9da55SMel Gorman 254723b9da55SMel Gorman return false; 254823b9da55SMel Gorman } 254923b9da55SMel Gorman 25504f98a2feSRik van Riel /* 255123b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 255223b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 255323b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 2554df3a45f9SQiwu Chen * calls try_to_compact_pages() that it will have enough free pages to succeed. 255523b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 25563e7d3449SMel Gorman */ 2557a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 25583e7d3449SMel Gorman unsigned long nr_reclaimed, 25593e7d3449SMel Gorman struct scan_control *sc) 25603e7d3449SMel Gorman { 25613e7d3449SMel Gorman unsigned long pages_for_compaction; 25623e7d3449SMel Gorman unsigned long inactive_lru_pages; 2563a9dd0a83SMel Gorman int z; 25643e7d3449SMel Gorman 25653e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 25669e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 25673e7d3449SMel Gorman return false; 25683e7d3449SMel Gorman 25693e7d3449SMel Gorman /* 25705ee04716SVlastimil Babka * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX 25715ee04716SVlastimil Babka * number of pages that were scanned. This will return to the caller 25725ee04716SVlastimil Babka * with the risk reclaim/compaction and the resulting allocation attempt 25735ee04716SVlastimil Babka * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL 25745ee04716SVlastimil Babka * allocations through requiring that the full LRU list has been scanned 25755ee04716SVlastimil Babka * first, by assuming that zero delta of sc->nr_scanned means full LRU 25765ee04716SVlastimil Babka * scan, but that approximation was wrong, and there were corner cases 25775ee04716SVlastimil Babka * where always a non-zero amount of pages were scanned. 25782876592fSMel Gorman */ 25792876592fSMel Gorman if (!nr_reclaimed) 25802876592fSMel Gorman return false; 25813e7d3449SMel Gorman 25823e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 2583a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 2584a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 25856aa303deSMel Gorman if (!managed_zone(zone)) 2586a9dd0a83SMel Gorman continue; 2587a9dd0a83SMel Gorman 2588a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 2589cf378319SVlastimil Babka case COMPACT_SUCCESS: 25903e7d3449SMel Gorman case COMPACT_CONTINUE: 25913e7d3449SMel Gorman return false; 25923e7d3449SMel Gorman default: 2593a9dd0a83SMel Gorman /* check next zone */ 2594a9dd0a83SMel Gorman ; 25953e7d3449SMel Gorman } 25963e7d3449SMel Gorman } 25971c6c1597SHillf Danton 25981c6c1597SHillf Danton /* 25991c6c1597SHillf Danton * If we have not reclaimed enough pages for compaction and the 26001c6c1597SHillf Danton * inactive lists are large enough, continue reclaiming 26011c6c1597SHillf Danton */ 26021c6c1597SHillf Danton pages_for_compaction = compact_gap(sc->order); 26031c6c1597SHillf Danton inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 26041c6c1597SHillf Danton if (get_nr_swap_pages() > 0) 26051c6c1597SHillf Danton inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 26061c6c1597SHillf Danton 26075ee04716SVlastimil Babka return inactive_lru_pages > pages_for_compaction; 2608a9dd0a83SMel Gorman } 26093e7d3449SMel Gorman 26100f6a5cffSJohannes Weiner static void shrink_node_memcgs(pg_data_t *pgdat, struct scan_control *sc) 2611f16015fbSJohannes Weiner { 26120f6a5cffSJohannes Weiner struct mem_cgroup *target_memcg = sc->target_mem_cgroup; 2613694fbc0fSAndrew Morton struct mem_cgroup *memcg; 2614d108c772SAndrey Ryabinin 26150f6a5cffSJohannes Weiner memcg = mem_cgroup_iter(target_memcg, NULL, NULL); 2616694fbc0fSAndrew Morton do { 2617afaf07a6SJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 26188e8ae645SJohannes Weiner unsigned long reclaimed; 2619cb731d6cSVladimir Davydov unsigned long scanned; 26209b4f98cdSJohannes Weiner 2621e3336cabSXunlei Pang /* 2622e3336cabSXunlei Pang * This loop can become CPU-bound when target memcgs 2623e3336cabSXunlei Pang * aren't eligible for reclaim - either because they 2624e3336cabSXunlei Pang * don't have any reclaimable pages, or because their 2625e3336cabSXunlei Pang * memory is explicitly protected. Avoid soft lockups. 2626e3336cabSXunlei Pang */ 2627e3336cabSXunlei Pang cond_resched(); 2628e3336cabSXunlei Pang 262945c7f7e1SChris Down mem_cgroup_calculate_protection(target_memcg, memcg); 263045c7f7e1SChris Down 263145c7f7e1SChris Down if (mem_cgroup_below_min(memcg)) { 2632bf8d5d52SRoman Gushchin /* 2633bf8d5d52SRoman Gushchin * Hard protection. 2634bf8d5d52SRoman Gushchin * If there is no reclaimable memory, OOM. 2635bf8d5d52SRoman Gushchin */ 2636bf8d5d52SRoman Gushchin continue; 263745c7f7e1SChris Down } else if (mem_cgroup_below_low(memcg)) { 2638bf8d5d52SRoman Gushchin /* 2639bf8d5d52SRoman Gushchin * Soft protection. 2640bf8d5d52SRoman Gushchin * Respect the protection only as long as 2641bf8d5d52SRoman Gushchin * there is an unprotected supply 2642bf8d5d52SRoman Gushchin * of reclaimable memory from other cgroups. 2643bf8d5d52SRoman Gushchin */ 2644d6622f63SYisheng Xie if (!sc->memcg_low_reclaim) { 2645d6622f63SYisheng Xie sc->memcg_low_skipped = 1; 2646241994edSJohannes Weiner continue; 2647d6622f63SYisheng Xie } 2648e27be240SJohannes Weiner memcg_memory_event(memcg, MEMCG_LOW); 2649241994edSJohannes Weiner } 2650241994edSJohannes Weiner 26518e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 2652cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 2653afaf07a6SJohannes Weiner 2654afaf07a6SJohannes Weiner shrink_lruvec(lruvec, sc); 2655f9be23d6SKonstantin Khlebnikov 265628360f39SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, memcg, 265728360f39SMel Gorman sc->priority); 2658cb731d6cSVladimir Davydov 26598e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 26608e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 26618e8ae645SJohannes Weiner sc->nr_scanned - scanned, 26628e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 26638e8ae645SJohannes Weiner 26640f6a5cffSJohannes Weiner } while ((memcg = mem_cgroup_iter(target_memcg, memcg, NULL))); 26650f6a5cffSJohannes Weiner } 26660f6a5cffSJohannes Weiner 26676c9e0907SLiu Song static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) 26680f6a5cffSJohannes Weiner { 26690f6a5cffSJohannes Weiner struct reclaim_state *reclaim_state = current->reclaim_state; 26700f6a5cffSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 26711b05117dSJohannes Weiner struct lruvec *target_lruvec; 26720f6a5cffSJohannes Weiner bool reclaimable = false; 2673b91ac374SJohannes Weiner unsigned long file; 26740f6a5cffSJohannes Weiner 26751b05117dSJohannes Weiner target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); 26761b05117dSJohannes Weiner 26770f6a5cffSJohannes Weiner again: 26780f6a5cffSJohannes Weiner memset(&sc->nr, 0, sizeof(sc->nr)); 26790f6a5cffSJohannes Weiner 26800f6a5cffSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 26810f6a5cffSJohannes Weiner nr_scanned = sc->nr_scanned; 26820f6a5cffSJohannes Weiner 268353138ceaSJohannes Weiner /* 26847cf111bcSJohannes Weiner * Determine the scan balance between anon and file LRUs. 26857cf111bcSJohannes Weiner */ 26867cf111bcSJohannes Weiner spin_lock_irq(&pgdat->lru_lock); 26877cf111bcSJohannes Weiner sc->anon_cost = target_lruvec->anon_cost; 26887cf111bcSJohannes Weiner sc->file_cost = target_lruvec->file_cost; 26897cf111bcSJohannes Weiner spin_unlock_irq(&pgdat->lru_lock); 26907cf111bcSJohannes Weiner 26917cf111bcSJohannes Weiner /* 2692b91ac374SJohannes Weiner * Target desirable inactive:active list ratios for the anon 2693b91ac374SJohannes Weiner * and file LRU lists. 2694b91ac374SJohannes Weiner */ 2695b91ac374SJohannes Weiner if (!sc->force_deactivate) { 2696b91ac374SJohannes Weiner unsigned long refaults; 2697b91ac374SJohannes Weiner 2698170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, 2699170b04b7SJoonsoo Kim WORKINGSET_ACTIVATE_ANON); 2700170b04b7SJoonsoo Kim if (refaults != target_lruvec->refaults[0] || 2701170b04b7SJoonsoo Kim inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)) 2702b91ac374SJohannes Weiner sc->may_deactivate |= DEACTIVATE_ANON; 2703b91ac374SJohannes Weiner else 2704b91ac374SJohannes Weiner sc->may_deactivate &= ~DEACTIVATE_ANON; 2705b91ac374SJohannes Weiner 2706b91ac374SJohannes Weiner /* 2707b91ac374SJohannes Weiner * When refaults are being observed, it means a new 2708b91ac374SJohannes Weiner * workingset is being established. Deactivate to get 2709b91ac374SJohannes Weiner * rid of any stale active pages quickly. 2710b91ac374SJohannes Weiner */ 2711b91ac374SJohannes Weiner refaults = lruvec_page_state(target_lruvec, 2712170b04b7SJoonsoo Kim WORKINGSET_ACTIVATE_FILE); 2713170b04b7SJoonsoo Kim if (refaults != target_lruvec->refaults[1] || 2714b91ac374SJohannes Weiner inactive_is_low(target_lruvec, LRU_INACTIVE_FILE)) 2715b91ac374SJohannes Weiner sc->may_deactivate |= DEACTIVATE_FILE; 2716b91ac374SJohannes Weiner else 2717b91ac374SJohannes Weiner sc->may_deactivate &= ~DEACTIVATE_FILE; 2718b91ac374SJohannes Weiner } else 2719b91ac374SJohannes Weiner sc->may_deactivate = DEACTIVATE_ANON | DEACTIVATE_FILE; 2720b91ac374SJohannes Weiner 2721b91ac374SJohannes Weiner /* 2722b91ac374SJohannes Weiner * If we have plenty of inactive file pages that aren't 2723b91ac374SJohannes Weiner * thrashing, try to reclaim those first before touching 2724b91ac374SJohannes Weiner * anonymous pages. 2725b91ac374SJohannes Weiner */ 2726b91ac374SJohannes Weiner file = lruvec_page_state(target_lruvec, NR_INACTIVE_FILE); 2727b91ac374SJohannes Weiner if (file >> sc->priority && !(sc->may_deactivate & DEACTIVATE_FILE)) 2728b91ac374SJohannes Weiner sc->cache_trim_mode = 1; 2729b91ac374SJohannes Weiner else 2730b91ac374SJohannes Weiner sc->cache_trim_mode = 0; 2731b91ac374SJohannes Weiner 2732b91ac374SJohannes Weiner /* 273353138ceaSJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 273453138ceaSJohannes Weiner * cache pages start out inactive, every cache fault will tip 273553138ceaSJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 273653138ceaSJohannes Weiner * shrinks, so does the window for rotation from references. 273753138ceaSJohannes Weiner * This means we have a runaway feedback loop where a tiny 273853138ceaSJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 273953138ceaSJohannes Weiner * anon pages. Try to detect this based on file LRU size. 274053138ceaSJohannes Weiner */ 274153138ceaSJohannes Weiner if (!cgroup_reclaim(sc)) { 274253138ceaSJohannes Weiner unsigned long total_high_wmark = 0; 2743b91ac374SJohannes Weiner unsigned long free, anon; 2744b91ac374SJohannes Weiner int z; 274553138ceaSJohannes Weiner 274653138ceaSJohannes Weiner free = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 274753138ceaSJohannes Weiner file = node_page_state(pgdat, NR_ACTIVE_FILE) + 274853138ceaSJohannes Weiner node_page_state(pgdat, NR_INACTIVE_FILE); 274953138ceaSJohannes Weiner 275053138ceaSJohannes Weiner for (z = 0; z < MAX_NR_ZONES; z++) { 275153138ceaSJohannes Weiner struct zone *zone = &pgdat->node_zones[z]; 275253138ceaSJohannes Weiner if (!managed_zone(zone)) 275353138ceaSJohannes Weiner continue; 275453138ceaSJohannes Weiner 275553138ceaSJohannes Weiner total_high_wmark += high_wmark_pages(zone); 275653138ceaSJohannes Weiner } 275753138ceaSJohannes Weiner 2758b91ac374SJohannes Weiner /* 2759b91ac374SJohannes Weiner * Consider anon: if that's low too, this isn't a 2760b91ac374SJohannes Weiner * runaway file reclaim problem, but rather just 2761b91ac374SJohannes Weiner * extreme pressure. Reclaim as per usual then. 2762b91ac374SJohannes Weiner */ 2763b91ac374SJohannes Weiner anon = node_page_state(pgdat, NR_INACTIVE_ANON); 2764b91ac374SJohannes Weiner 2765b91ac374SJohannes Weiner sc->file_is_tiny = 2766b91ac374SJohannes Weiner file + free <= total_high_wmark && 2767b91ac374SJohannes Weiner !(sc->may_deactivate & DEACTIVATE_ANON) && 2768b91ac374SJohannes Weiner anon >> sc->priority; 276953138ceaSJohannes Weiner } 277053138ceaSJohannes Weiner 27710f6a5cffSJohannes Weiner shrink_node_memcgs(pgdat, sc); 277270ddf637SAnton Vorontsov 27736b4f7799SJohannes Weiner if (reclaim_state) { 2774cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 27756b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 27766b4f7799SJohannes Weiner } 27776b4f7799SJohannes Weiner 27788e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 27791b05117dSJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 278070ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 278170ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 278270ddf637SAnton Vorontsov 27832344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 27842344d7e4SJohannes Weiner reclaimable = true; 27852344d7e4SJohannes Weiner 2786e3c1ac58SAndrey Ryabinin if (current_is_kswapd()) { 2787d108c772SAndrey Ryabinin /* 2788e3c1ac58SAndrey Ryabinin * If reclaim is isolating dirty pages under writeback, 2789e3c1ac58SAndrey Ryabinin * it implies that the long-lived page allocation rate 2790e3c1ac58SAndrey Ryabinin * is exceeding the page laundering rate. Either the 2791e3c1ac58SAndrey Ryabinin * global limits are not being effective at throttling 2792e3c1ac58SAndrey Ryabinin * processes due to the page distribution throughout 2793e3c1ac58SAndrey Ryabinin * zones or there is heavy usage of a slow backing 2794e3c1ac58SAndrey Ryabinin * device. The only option is to throttle from reclaim 2795e3c1ac58SAndrey Ryabinin * context which is not ideal as there is no guarantee 2796d108c772SAndrey Ryabinin * the dirtying process is throttled in the same way 2797d108c772SAndrey Ryabinin * balance_dirty_pages() manages. 2798d108c772SAndrey Ryabinin * 2799e3c1ac58SAndrey Ryabinin * Once a node is flagged PGDAT_WRITEBACK, kswapd will 2800e3c1ac58SAndrey Ryabinin * count the number of pages under pages flagged for 2801e3c1ac58SAndrey Ryabinin * immediate reclaim and stall if any are encountered 2802e3c1ac58SAndrey Ryabinin * in the nr_immediate check below. 2803d108c772SAndrey Ryabinin */ 2804d108c772SAndrey Ryabinin if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) 2805d108c772SAndrey Ryabinin set_bit(PGDAT_WRITEBACK, &pgdat->flags); 2806d108c772SAndrey Ryabinin 2807d108c772SAndrey Ryabinin /* Allow kswapd to start writing pages during reclaim.*/ 2808d108c772SAndrey Ryabinin if (sc->nr.unqueued_dirty == sc->nr.file_taken) 2809d108c772SAndrey Ryabinin set_bit(PGDAT_DIRTY, &pgdat->flags); 2810d108c772SAndrey Ryabinin 2811d108c772SAndrey Ryabinin /* 28121eba09c1SRandy Dunlap * If kswapd scans pages marked for immediate 2813d108c772SAndrey Ryabinin * reclaim and under writeback (nr_immediate), it 2814d108c772SAndrey Ryabinin * implies that pages are cycling through the LRU 2815d108c772SAndrey Ryabinin * faster than they are written so also forcibly stall. 2816d108c772SAndrey Ryabinin */ 2817d108c772SAndrey Ryabinin if (sc->nr.immediate) 2818d108c772SAndrey Ryabinin congestion_wait(BLK_RW_ASYNC, HZ/10); 2819d108c772SAndrey Ryabinin } 2820d108c772SAndrey Ryabinin 2821d108c772SAndrey Ryabinin /* 28221b05117dSJohannes Weiner * Tag a node/memcg as congested if all the dirty pages 28231b05117dSJohannes Weiner * scanned were backed by a congested BDI and 28241b05117dSJohannes Weiner * wait_iff_congested will stall. 28251b05117dSJohannes Weiner * 2826e3c1ac58SAndrey Ryabinin * Legacy memcg will stall in page writeback so avoid forcibly 2827e3c1ac58SAndrey Ryabinin * stalling in wait_iff_congested(). 2828e3c1ac58SAndrey Ryabinin */ 28291b05117dSJohannes Weiner if ((current_is_kswapd() || 28301b05117dSJohannes Weiner (cgroup_reclaim(sc) && writeback_throttling_sane(sc))) && 2831e3c1ac58SAndrey Ryabinin sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 28321b05117dSJohannes Weiner set_bit(LRUVEC_CONGESTED, &target_lruvec->flags); 2833e3c1ac58SAndrey Ryabinin 2834e3c1ac58SAndrey Ryabinin /* 2835d108c772SAndrey Ryabinin * Stall direct reclaim for IO completions if underlying BDIs 2836d108c772SAndrey Ryabinin * and node is congested. Allow kswapd to continue until it 2837d108c772SAndrey Ryabinin * starts encountering unqueued dirty pages or cycling through 2838d108c772SAndrey Ryabinin * the LRU too quickly. 2839d108c772SAndrey Ryabinin */ 28401b05117dSJohannes Weiner if (!current_is_kswapd() && current_may_throttle() && 28411b05117dSJohannes Weiner !sc->hibernation_mode && 28421b05117dSJohannes Weiner test_bit(LRUVEC_CONGESTED, &target_lruvec->flags)) 2843e3c1ac58SAndrey Ryabinin wait_iff_congested(BLK_RW_ASYNC, HZ/10); 2844d108c772SAndrey Ryabinin 2845d2af3397SJohannes Weiner if (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 2846d2af3397SJohannes Weiner sc)) 2847d2af3397SJohannes Weiner goto again; 28482344d7e4SJohannes Weiner 2849c73322d0SJohannes Weiner /* 2850c73322d0SJohannes Weiner * Kswapd gives up on balancing particular nodes after too 2851c73322d0SJohannes Weiner * many failures to reclaim anything from them and goes to 2852c73322d0SJohannes Weiner * sleep. On reclaim progress, reset the failure counter. A 2853c73322d0SJohannes Weiner * successful direct reclaim run will revive a dormant kswapd. 2854c73322d0SJohannes Weiner */ 2855c73322d0SJohannes Weiner if (reclaimable) 2856c73322d0SJohannes Weiner pgdat->kswapd_failures = 0; 2857f16015fbSJohannes Weiner } 2858f16015fbSJohannes Weiner 285953853e2dSVlastimil Babka /* 2860fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 2861fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 2862fdd4c614SVlastimil Babka * should reclaim first. 286353853e2dSVlastimil Babka */ 28644f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 2865fe4b1b24SMel Gorman { 286631483b6aSMel Gorman unsigned long watermark; 2867fdd4c614SVlastimil Babka enum compact_result suitable; 2868fe4b1b24SMel Gorman 2869fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 2870fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 2871fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 2872fdd4c614SVlastimil Babka return true; 2873fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 2874fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 2875fe4b1b24SMel Gorman return false; 2876fe4b1b24SMel Gorman 2877fdd4c614SVlastimil Babka /* 2878fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 2879fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 2880fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 2881fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 2882fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 2883fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 2884fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 2885fdd4c614SVlastimil Babka */ 2886fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 2887fdd4c614SVlastimil Babka 2888fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 2889fe4b1b24SMel Gorman } 2890fe4b1b24SMel Gorman 28911da177e4SLinus Torvalds /* 28921da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 28931da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 28941da177e4SLinus Torvalds * request. 28951da177e4SLinus Torvalds * 28961da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 28971da177e4SLinus Torvalds * scan then give up on it. 28981da177e4SLinus Torvalds */ 28990a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 29001da177e4SLinus Torvalds { 2901dd1a239fSMel Gorman struct zoneref *z; 290254a6eb5cSMel Gorman struct zone *zone; 29030608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 29040608f43dSAndrew Morton unsigned long nr_soft_scanned; 2905619d0d76SWeijie Yang gfp_t orig_mask; 290679dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 29071cfb419bSKAMEZAWA Hiroyuki 2908cc715d99SMel Gorman /* 2909cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2910cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2911cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2912cc715d99SMel Gorman */ 2913619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 2914b2e18757SMel Gorman if (buffer_heads_over_limit) { 2915cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 29164f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 2917b2e18757SMel Gorman } 2918cc715d99SMel Gorman 2919d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2920b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 2921b2e18757SMel Gorman /* 29221cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 29231cfb419bSKAMEZAWA Hiroyuki * to global LRU. 29241cfb419bSKAMEZAWA Hiroyuki */ 2925b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) { 2926344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 2927344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 29281da177e4SLinus Torvalds continue; 292965ec02cbSVladimir Davydov 2930e0887c19SRik van Riel /* 2931e0c23279SMel Gorman * If we already have plenty of memory free for 2932e0c23279SMel Gorman * compaction in this zone, don't free any more. 2933e0c23279SMel Gorman * Even though compaction is invoked for any 2934e0c23279SMel Gorman * non-zero order, only frequent costly order 2935e0c23279SMel Gorman * reclamation is disruptive enough to become a 2936c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2937c7cfa37bSCopot Alexandru * page allocations. 2938e0887c19SRik van Riel */ 29390b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 29400b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 29414f588331SMel Gorman compaction_ready(zone, sc)) { 29420b06496aSJohannes Weiner sc->compaction_ready = true; 2943e0887c19SRik van Riel continue; 2944e0887c19SRik van Riel } 29450b06496aSJohannes Weiner 29460608f43dSAndrew Morton /* 294779dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 294879dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 294979dafcdcSMel Gorman * node may be shrunk multiple times but in that case 295079dafcdcSMel Gorman * the user prefers lower zones being preserved. 295179dafcdcSMel Gorman */ 295279dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 295379dafcdcSMel Gorman continue; 295479dafcdcSMel Gorman 295579dafcdcSMel Gorman /* 29560608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 29570608f43dSAndrew Morton * and returns the number of reclaimed pages and 29580608f43dSAndrew Morton * scanned pages. This works for global memory pressure 29590608f43dSAndrew Morton * and balancing, not for a memcg's limit. 29600608f43dSAndrew Morton */ 29610608f43dSAndrew Morton nr_soft_scanned = 0; 2962ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 29630608f43dSAndrew Morton sc->order, sc->gfp_mask, 29640608f43dSAndrew Morton &nr_soft_scanned); 29650608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 29660608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 2967ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2968ac34a1a3SKAMEZAWA Hiroyuki } 2969d149e3b2SYing Han 297079dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 297179dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 297279dafcdcSMel Gorman continue; 297379dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 2974970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 29751da177e4SLinus Torvalds } 2976e0c23279SMel Gorman 297765ec02cbSVladimir Davydov /* 2978619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 2979619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 2980619d0d76SWeijie Yang */ 2981619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 29821da177e4SLinus Torvalds } 29831da177e4SLinus Torvalds 2984b910718aSJohannes Weiner static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat) 29852a2e4885SJohannes Weiner { 2986b910718aSJohannes Weiner struct lruvec *target_lruvec; 29872a2e4885SJohannes Weiner unsigned long refaults; 29882a2e4885SJohannes Weiner 2989b910718aSJohannes Weiner target_lruvec = mem_cgroup_lruvec(target_memcg, pgdat); 2990170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_ANON); 2991170b04b7SJoonsoo Kim target_lruvec->refaults[0] = refaults; 2992170b04b7SJoonsoo Kim refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE); 2993170b04b7SJoonsoo Kim target_lruvec->refaults[1] = refaults; 29942a2e4885SJohannes Weiner } 29952a2e4885SJohannes Weiner 29961da177e4SLinus Torvalds /* 29971da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 29981da177e4SLinus Torvalds * 29991da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 30001da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 30011da177e4SLinus Torvalds * 30021da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 30031da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 30045b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 30055b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 30065b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 30075b0830cbSJens Axboe * work, and the allocation attempt will fail. 3008a41f24eaSNishanth Aravamudan * 3009a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 3010a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 30111da177e4SLinus Torvalds */ 3012dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 30133115cd91SVladimir Davydov struct scan_control *sc) 30141da177e4SLinus Torvalds { 3015241994edSJohannes Weiner int initial_priority = sc->priority; 30162a2e4885SJohannes Weiner pg_data_t *last_pgdat; 30172a2e4885SJohannes Weiner struct zoneref *z; 30182a2e4885SJohannes Weiner struct zone *zone; 3019241994edSJohannes Weiner retry: 3020873b4771SKeika Kobayashi delayacct_freepages_start(); 3021873b4771SKeika Kobayashi 3022b5ead35eSJohannes Weiner if (!cgroup_reclaim(sc)) 30237cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 30241da177e4SLinus Torvalds 30259e3b2f8cSKonstantin Khlebnikov do { 302670ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 302770ddf637SAnton Vorontsov sc->priority); 302866e1707bSBalbir Singh sc->nr_scanned = 0; 30290a0337e0SMichal Hocko shrink_zones(zonelist, sc); 3030e0c23279SMel Gorman 3031bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 30320b06496aSJohannes Weiner break; 30330b06496aSJohannes Weiner 30340b06496aSJohannes Weiner if (sc->compaction_ready) 30350b06496aSJohannes Weiner break; 30361da177e4SLinus Torvalds 30371da177e4SLinus Torvalds /* 30380e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 30390e50ce3bSMinchan Kim * writepage even in laptop mode. 30400e50ce3bSMinchan Kim */ 30410e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 30420e50ce3bSMinchan Kim sc->may_writepage = 1; 30430b06496aSJohannes Weiner } while (--sc->priority >= 0); 3044bb21c7ceSKOSAKI Motohiro 30452a2e4885SJohannes Weiner last_pgdat = NULL; 30462a2e4885SJohannes Weiner for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, 30472a2e4885SJohannes Weiner sc->nodemask) { 30482a2e4885SJohannes Weiner if (zone->zone_pgdat == last_pgdat) 30492a2e4885SJohannes Weiner continue; 30502a2e4885SJohannes Weiner last_pgdat = zone->zone_pgdat; 30511b05117dSJohannes Weiner 30522a2e4885SJohannes Weiner snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); 30531b05117dSJohannes Weiner 30541b05117dSJohannes Weiner if (cgroup_reclaim(sc)) { 30551b05117dSJohannes Weiner struct lruvec *lruvec; 30561b05117dSJohannes Weiner 30571b05117dSJohannes Weiner lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, 30581b05117dSJohannes Weiner zone->zone_pgdat); 30591b05117dSJohannes Weiner clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 30601b05117dSJohannes Weiner } 30612a2e4885SJohannes Weiner } 30622a2e4885SJohannes Weiner 3063873b4771SKeika Kobayashi delayacct_freepages_end(); 3064873b4771SKeika Kobayashi 3065bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 3066bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 3067bb21c7ceSKOSAKI Motohiro 30680cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 30690b06496aSJohannes Weiner if (sc->compaction_ready) 30707335084dSMel Gorman return 1; 30717335084dSMel Gorman 3072b91ac374SJohannes Weiner /* 3073b91ac374SJohannes Weiner * We make inactive:active ratio decisions based on the node's 3074b91ac374SJohannes Weiner * composition of memory, but a restrictive reclaim_idx or a 3075b91ac374SJohannes Weiner * memory.low cgroup setting can exempt large amounts of 3076b91ac374SJohannes Weiner * memory from reclaim. Neither of which are very common, so 3077b91ac374SJohannes Weiner * instead of doing costly eligibility calculations of the 3078b91ac374SJohannes Weiner * entire cgroup subtree up front, we assume the estimates are 3079b91ac374SJohannes Weiner * good, and retry with forcible deactivation if that fails. 3080b91ac374SJohannes Weiner */ 3081b91ac374SJohannes Weiner if (sc->skipped_deactivate) { 3082b91ac374SJohannes Weiner sc->priority = initial_priority; 3083b91ac374SJohannes Weiner sc->force_deactivate = 1; 3084b91ac374SJohannes Weiner sc->skipped_deactivate = 0; 3085b91ac374SJohannes Weiner goto retry; 3086b91ac374SJohannes Weiner } 3087b91ac374SJohannes Weiner 3088241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 3089d6622f63SYisheng Xie if (sc->memcg_low_skipped) { 3090241994edSJohannes Weiner sc->priority = initial_priority; 3091b91ac374SJohannes Weiner sc->force_deactivate = 0; 3092d6622f63SYisheng Xie sc->memcg_low_reclaim = 1; 3093d6622f63SYisheng Xie sc->memcg_low_skipped = 0; 3094241994edSJohannes Weiner goto retry; 3095241994edSJohannes Weiner } 3096241994edSJohannes Weiner 3097bb21c7ceSKOSAKI Motohiro return 0; 30981da177e4SLinus Torvalds } 30991da177e4SLinus Torvalds 3100c73322d0SJohannes Weiner static bool allow_direct_reclaim(pg_data_t *pgdat) 31015515061dSMel Gorman { 31025515061dSMel Gorman struct zone *zone; 31035515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 31045515061dSMel Gorman unsigned long free_pages = 0; 31055515061dSMel Gorman int i; 31065515061dSMel Gorman bool wmark_ok; 31075515061dSMel Gorman 3108c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3109c73322d0SJohannes Weiner return true; 3110c73322d0SJohannes Weiner 31115515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 31125515061dSMel Gorman zone = &pgdat->node_zones[i]; 3113d450abd8SJohannes Weiner if (!managed_zone(zone)) 3114d450abd8SJohannes Weiner continue; 3115d450abd8SJohannes Weiner 3116d450abd8SJohannes Weiner if (!zone_reclaimable_pages(zone)) 3117675becceSMel Gorman continue; 3118675becceSMel Gorman 31195515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 31205515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 31215515061dSMel Gorman } 31225515061dSMel Gorman 3123675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 3124675becceSMel Gorman if (!pfmemalloc_reserve) 3125675becceSMel Gorman return true; 3126675becceSMel Gorman 31275515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 31285515061dSMel Gorman 31295515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 31305515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 313197a225e6SJoonsoo Kim if (READ_ONCE(pgdat->kswapd_highest_zoneidx) > ZONE_NORMAL) 313297a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, ZONE_NORMAL); 31335644e1fbSQian Cai 31345515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 31355515061dSMel Gorman } 31365515061dSMel Gorman 31375515061dSMel Gorman return wmark_ok; 31385515061dSMel Gorman } 31395515061dSMel Gorman 31405515061dSMel Gorman /* 31415515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 31425515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 31435515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 314450694c28SMel Gorman * when the low watermark is reached. 314550694c28SMel Gorman * 314650694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 314750694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 31485515061dSMel Gorman */ 314950694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 31505515061dSMel Gorman nodemask_t *nodemask) 31515515061dSMel Gorman { 3152675becceSMel Gorman struct zoneref *z; 31535515061dSMel Gorman struct zone *zone; 3154675becceSMel Gorman pg_data_t *pgdat = NULL; 31555515061dSMel Gorman 31565515061dSMel Gorman /* 31575515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 31585515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 31595515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 31605515061dSMel Gorman * committing a transaction where throttling it could forcing other 31615515061dSMel Gorman * processes to block on log_wait_commit(). 31625515061dSMel Gorman */ 31635515061dSMel Gorman if (current->flags & PF_KTHREAD) 316450694c28SMel Gorman goto out; 316550694c28SMel Gorman 316650694c28SMel Gorman /* 316750694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 316850694c28SMel Gorman * It should return quickly so it can exit and free its memory 316950694c28SMel Gorman */ 317050694c28SMel Gorman if (fatal_signal_pending(current)) 317150694c28SMel Gorman goto out; 31725515061dSMel Gorman 3173675becceSMel Gorman /* 3174675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 3175675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 3176675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 3177675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 3178675becceSMel Gorman * 3179675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 3180675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 3181675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 3182675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 3183675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 3184675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 3185675becceSMel Gorman * should make reasonable progress. 3186675becceSMel Gorman */ 3187675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 318817636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 3189675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 3190675becceSMel Gorman continue; 3191675becceSMel Gorman 3192675becceSMel Gorman /* Throttle based on the first usable node */ 31935515061dSMel Gorman pgdat = zone->zone_pgdat; 3194c73322d0SJohannes Weiner if (allow_direct_reclaim(pgdat)) 319550694c28SMel Gorman goto out; 3196675becceSMel Gorman break; 3197675becceSMel Gorman } 3198675becceSMel Gorman 3199675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 3200675becceSMel Gorman if (!pgdat) 3201675becceSMel Gorman goto out; 32025515061dSMel Gorman 320368243e76SMel Gorman /* Account for the throttling */ 320468243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 320568243e76SMel Gorman 32065515061dSMel Gorman /* 32075515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 32085515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 32095515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 32105515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 32115515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 32125515061dSMel Gorman * second before continuing. 32135515061dSMel Gorman */ 32145515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 32155515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 3216c73322d0SJohannes Weiner allow_direct_reclaim(pgdat), HZ); 321750694c28SMel Gorman 321850694c28SMel Gorman goto check_pending; 32195515061dSMel Gorman } 32205515061dSMel Gorman 32215515061dSMel Gorman /* Throttle until kswapd wakes the process */ 32225515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 3223c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)); 322450694c28SMel Gorman 322550694c28SMel Gorman check_pending: 322650694c28SMel Gorman if (fatal_signal_pending(current)) 322750694c28SMel Gorman return true; 322850694c28SMel Gorman 322950694c28SMel Gorman out: 323050694c28SMel Gorman return false; 32315515061dSMel Gorman } 32325515061dSMel Gorman 3233dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 3234327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 323566e1707bSBalbir Singh { 323633906bc5SMel Gorman unsigned long nr_reclaimed; 323766e1707bSBalbir Singh struct scan_control sc = { 323822fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3239f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 3240b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 3241ee814fe2SJohannes Weiner .order = order, 3242ee814fe2SJohannes Weiner .nodemask = nodemask, 3243ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 3244ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3245a6dc60f8SJohannes Weiner .may_unmap = 1, 32462e2e4259SKOSAKI Motohiro .may_swap = 1, 324766e1707bSBalbir Singh }; 324866e1707bSBalbir Singh 32495515061dSMel Gorman /* 3250bb451fdfSGreg Thelen * scan_control uses s8 fields for order, priority, and reclaim_idx. 3251bb451fdfSGreg Thelen * Confirm they are large enough for max values. 3252bb451fdfSGreg Thelen */ 3253bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_ORDER > S8_MAX); 3254bb451fdfSGreg Thelen BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); 3255bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); 3256bb451fdfSGreg Thelen 3257bb451fdfSGreg Thelen /* 325850694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 325950694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 326050694c28SMel Gorman * point. 32615515061dSMel Gorman */ 3262f2f43e56SNick Desaulniers if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) 32635515061dSMel Gorman return 1; 32645515061dSMel Gorman 32651732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 32663481c37fSYafang Shao trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask); 326733906bc5SMel Gorman 32683115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 326933906bc5SMel Gorman 327033906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 32711732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 327233906bc5SMel Gorman 327333906bc5SMel Gorman return nr_reclaimed; 327466e1707bSBalbir Singh } 327566e1707bSBalbir Singh 3276c255a458SAndrew Morton #ifdef CONFIG_MEMCG 327766e1707bSBalbir Singh 3278d2e5fb92SMichal Hocko /* Only used by soft limit reclaim. Do not reuse for anything else. */ 3279a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 32804e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 3281ef8f2327SMel Gorman pg_data_t *pgdat, 32820ae5e89cSYing Han unsigned long *nr_scanned) 32834e416953SBalbir Singh { 3284afaf07a6SJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); 32854e416953SBalbir Singh struct scan_control sc = { 3286b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3287ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 32884e416953SBalbir Singh .may_writepage = !laptop_mode, 32894e416953SBalbir Singh .may_unmap = 1, 3290b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 32914e416953SBalbir Singh .may_swap = !noswap, 32924e416953SBalbir Singh }; 32930ae5e89cSYing Han 3294d2e5fb92SMichal Hocko WARN_ON_ONCE(!current->reclaim_state); 3295d2e5fb92SMichal Hocko 32964e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 32974e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 3298bdce6d9eSKOSAKI Motohiro 32999e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 33003481c37fSYafang Shao sc.gfp_mask); 3301bdce6d9eSKOSAKI Motohiro 33024e416953SBalbir Singh /* 33034e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 33044e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 3305a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 33064e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 33074e416953SBalbir Singh * the priority and make it zero. 33084e416953SBalbir Singh */ 3309afaf07a6SJohannes Weiner shrink_lruvec(lruvec, &sc); 3310bdce6d9eSKOSAKI Motohiro 3311bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 3312bdce6d9eSKOSAKI Motohiro 33130ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 33140308f7cfSYafang Shao 33154e416953SBalbir Singh return sc.nr_reclaimed; 33164e416953SBalbir Singh } 33174e416953SBalbir Singh 331872835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 3319b70a2a21SJohannes Weiner unsigned long nr_pages, 33208c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 3321b70a2a21SJohannes Weiner bool may_swap) 332266e1707bSBalbir Singh { 3323bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 3324499118e9SVlastimil Babka unsigned int noreclaim_flag; 332566e1707bSBalbir Singh struct scan_control sc = { 3326b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 33277dea19f9SMichal Hocko .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 3328ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 3329b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3330ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3331ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 333266e1707bSBalbir Singh .may_writepage = !laptop_mode, 3333a6dc60f8SJohannes Weiner .may_unmap = 1, 3334b70a2a21SJohannes Weiner .may_swap = may_swap, 3335a09ed5e0SYing Han }; 3336fa40d1eeSShakeel Butt /* 3337fa40d1eeSShakeel Butt * Traverse the ZONELIST_FALLBACK zonelist of the current node to put 3338fa40d1eeSShakeel Butt * equal pressure on all the nodes. This is based on the assumption that 3339fa40d1eeSShakeel Butt * the reclaim does not bail out early. 3340fa40d1eeSShakeel Butt */ 3341fa40d1eeSShakeel Butt struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 334266e1707bSBalbir Singh 33431732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 33443481c37fSYafang Shao trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask); 3345499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 3346eb414681SJohannes Weiner 33473115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3348eb414681SJohannes Weiner 3349499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3350bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 33511732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 3352bdce6d9eSKOSAKI Motohiro 3353bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 335466e1707bSBalbir Singh } 335566e1707bSBalbir Singh #endif 335666e1707bSBalbir Singh 33571d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3358ef8f2327SMel Gorman struct scan_control *sc) 3359f16015fbSJohannes Weiner { 3360b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3361b91ac374SJohannes Weiner struct lruvec *lruvec; 3362b95a2f2dSJohannes Weiner 3363b95a2f2dSJohannes Weiner if (!total_swap_pages) 3364b95a2f2dSJohannes Weiner return; 3365b95a2f2dSJohannes Weiner 3366b91ac374SJohannes Weiner lruvec = mem_cgroup_lruvec(NULL, pgdat); 3367b91ac374SJohannes Weiner if (!inactive_is_low(lruvec, LRU_INACTIVE_ANON)) 3368b91ac374SJohannes Weiner return; 3369b91ac374SJohannes Weiner 3370b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3371b95a2f2dSJohannes Weiner do { 3372b91ac374SJohannes Weiner lruvec = mem_cgroup_lruvec(memcg, pgdat); 33731a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 33749e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3375b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3376b95a2f2dSJohannes Weiner } while (memcg); 3377f16015fbSJohannes Weiner } 3378f16015fbSJohannes Weiner 337997a225e6SJoonsoo Kim static bool pgdat_watermark_boosted(pg_data_t *pgdat, int highest_zoneidx) 33801c30844dSMel Gorman { 33811c30844dSMel Gorman int i; 33821c30844dSMel Gorman struct zone *zone; 33831c30844dSMel Gorman 33841c30844dSMel Gorman /* 33851c30844dSMel Gorman * Check for watermark boosts top-down as the higher zones 33861c30844dSMel Gorman * are more likely to be boosted. Both watermarks and boosts 33871eba09c1SRandy Dunlap * should not be checked at the same time as reclaim would 33881c30844dSMel Gorman * start prematurely when there is no boosting and a lower 33891c30844dSMel Gorman * zone is balanced. 33901c30844dSMel Gorman */ 339197a225e6SJoonsoo Kim for (i = highest_zoneidx; i >= 0; i--) { 33921c30844dSMel Gorman zone = pgdat->node_zones + i; 33931c30844dSMel Gorman if (!managed_zone(zone)) 33941c30844dSMel Gorman continue; 33951c30844dSMel Gorman 33961c30844dSMel Gorman if (zone->watermark_boost) 33971c30844dSMel Gorman return true; 33981c30844dSMel Gorman } 33991c30844dSMel Gorman 34001c30844dSMel Gorman return false; 34011c30844dSMel Gorman } 34021c30844dSMel Gorman 3403e716f2ebSMel Gorman /* 3404e716f2ebSMel Gorman * Returns true if there is an eligible zone balanced for the request order 340597a225e6SJoonsoo Kim * and highest_zoneidx 3406e716f2ebSMel Gorman */ 340797a225e6SJoonsoo Kim static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx) 340860cefed4SJohannes Weiner { 3409e716f2ebSMel Gorman int i; 3410e716f2ebSMel Gorman unsigned long mark = -1; 3411e716f2ebSMel Gorman struct zone *zone; 341260cefed4SJohannes Weiner 34131c30844dSMel Gorman /* 34141c30844dSMel Gorman * Check watermarks bottom-up as lower zones are more likely to 34151c30844dSMel Gorman * meet watermarks. 34161c30844dSMel Gorman */ 341797a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 3418e716f2ebSMel Gorman zone = pgdat->node_zones + i; 34196256c6b4SMel Gorman 3420e716f2ebSMel Gorman if (!managed_zone(zone)) 3421e716f2ebSMel Gorman continue; 3422e716f2ebSMel Gorman 3423e716f2ebSMel Gorman mark = high_wmark_pages(zone); 342497a225e6SJoonsoo Kim if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx)) 34256256c6b4SMel Gorman return true; 342660cefed4SJohannes Weiner } 342760cefed4SJohannes Weiner 3428e716f2ebSMel Gorman /* 342997a225e6SJoonsoo Kim * If a node has no populated zone within highest_zoneidx, it does not 3430e716f2ebSMel Gorman * need balancing by definition. This can happen if a zone-restricted 3431e716f2ebSMel Gorman * allocation tries to wake a remote kswapd. 3432e716f2ebSMel Gorman */ 3433e716f2ebSMel Gorman if (mark == -1) 3434e716f2ebSMel Gorman return true; 3435e716f2ebSMel Gorman 3436e716f2ebSMel Gorman return false; 3437e716f2ebSMel Gorman } 3438e716f2ebSMel Gorman 3439631b6e08SMel Gorman /* Clear pgdat state for congested, dirty or under writeback. */ 3440631b6e08SMel Gorman static void clear_pgdat_congested(pg_data_t *pgdat) 3441631b6e08SMel Gorman { 34421b05117dSJohannes Weiner struct lruvec *lruvec = mem_cgroup_lruvec(NULL, pgdat); 34431b05117dSJohannes Weiner 34441b05117dSJohannes Weiner clear_bit(LRUVEC_CONGESTED, &lruvec->flags); 3445631b6e08SMel Gorman clear_bit(PGDAT_DIRTY, &pgdat->flags); 3446631b6e08SMel Gorman clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 3447631b6e08SMel Gorman } 3448631b6e08SMel Gorman 34491741c877SMel Gorman /* 34505515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 34515515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 34525515061dSMel Gorman * 34535515061dSMel Gorman * Returns true if kswapd is ready to sleep 34545515061dSMel Gorman */ 345597a225e6SJoonsoo Kim static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, 345697a225e6SJoonsoo Kim int highest_zoneidx) 3457f50de2d3SMel Gorman { 34585515061dSMel Gorman /* 34599e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 3460c73322d0SJohannes Weiner * soon as allow_direct_reclaim() is true. But there is a potential 34619e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 34629e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 34639e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 34649e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 34659e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 34669e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 34679e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 34689e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 34699e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 34705515061dSMel Gorman */ 34719e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 34729e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 3473f50de2d3SMel Gorman 3474c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3475c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3476c73322d0SJohannes Weiner return true; 3477c73322d0SJohannes Weiner 347897a225e6SJoonsoo Kim if (pgdat_balanced(pgdat, order, highest_zoneidx)) { 3479631b6e08SMel Gorman clear_pgdat_congested(pgdat); 3480333b0a45SShantanu Goel return true; 34811d82de61SMel Gorman } 34821d82de61SMel Gorman 3483333b0a45SShantanu Goel return false; 3484f50de2d3SMel Gorman } 3485f50de2d3SMel Gorman 34861da177e4SLinus Torvalds /* 34871d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 34881d82de61SMel Gorman * zone that is currently unbalanced. 3489b8e83b94SMel Gorman * 3490b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 3491283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 3492283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 349375485363SMel Gorman */ 34941d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 3495accf6242SVlastimil Babka struct scan_control *sc) 349675485363SMel Gorman { 34971d82de61SMel Gorman struct zone *zone; 34981d82de61SMel Gorman int z; 349975485363SMel Gorman 35001d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 35011d82de61SMel Gorman sc->nr_to_reclaim = 0; 3502970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 35031d82de61SMel Gorman zone = pgdat->node_zones + z; 35046aa303deSMel Gorman if (!managed_zone(zone)) 35051d82de61SMel Gorman continue; 35067c954f6dSMel Gorman 35071d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 35087c954f6dSMel Gorman } 35097c954f6dSMel Gorman 35101d82de61SMel Gorman /* 35111d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 35121d82de61SMel Gorman * now pressure is applied based on node LRU order. 35131d82de61SMel Gorman */ 3514970a39a3SMel Gorman shrink_node(pgdat, sc); 35151d82de61SMel Gorman 35161d82de61SMel Gorman /* 35171d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 35181d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 35191d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 35201d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 35211d82de61SMel Gorman * can direct reclaim/compact. 35221d82de61SMel Gorman */ 35239861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 35241d82de61SMel Gorman sc->order = 0; 35251d82de61SMel Gorman 3526b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 352775485363SMel Gorman } 352875485363SMel Gorman 352975485363SMel Gorman /* 35301d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 35311d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 35321d82de61SMel Gorman * balanced. 35331da177e4SLinus Torvalds * 35341d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 35351da177e4SLinus Torvalds * 35361da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 353741858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 35388bb4e7a2SWei Yang * found to have free_pages <= high_wmark_pages(zone), any page in that zone 35391d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 35401d82de61SMel Gorman * balanced. 35411da177e4SLinus Torvalds */ 354297a225e6SJoonsoo Kim static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) 35431da177e4SLinus Torvalds { 35441da177e4SLinus Torvalds int i; 35450608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 35460608f43dSAndrew Morton unsigned long nr_soft_scanned; 3547eb414681SJohannes Weiner unsigned long pflags; 35481c30844dSMel Gorman unsigned long nr_boost_reclaim; 35491c30844dSMel Gorman unsigned long zone_boosts[MAX_NR_ZONES] = { 0, }; 35501c30844dSMel Gorman bool boosted; 35511d82de61SMel Gorman struct zone *zone; 3552179e9639SAndrew Morton struct scan_control sc = { 3553179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 3554ee814fe2SJohannes Weiner .order = order, 3555a6dc60f8SJohannes Weiner .may_unmap = 1, 3556179e9639SAndrew Morton }; 355793781325SOmar Sandoval 35581732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 3559eb414681SJohannes Weiner psi_memstall_enter(&pflags); 356093781325SOmar Sandoval __fs_reclaim_acquire(); 356193781325SOmar Sandoval 3562f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 35631da177e4SLinus Torvalds 35641c30844dSMel Gorman /* 35651c30844dSMel Gorman * Account for the reclaim boost. Note that the zone boost is left in 35661c30844dSMel Gorman * place so that parallel allocations that are near the watermark will 35671c30844dSMel Gorman * stall or direct reclaim until kswapd is finished. 35681c30844dSMel Gorman */ 35691c30844dSMel Gorman nr_boost_reclaim = 0; 357097a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 35711c30844dSMel Gorman zone = pgdat->node_zones + i; 35721c30844dSMel Gorman if (!managed_zone(zone)) 35731c30844dSMel Gorman continue; 35741c30844dSMel Gorman 35751c30844dSMel Gorman nr_boost_reclaim += zone->watermark_boost; 35761c30844dSMel Gorman zone_boosts[i] = zone->watermark_boost; 35771c30844dSMel Gorman } 35781c30844dSMel Gorman boosted = nr_boost_reclaim; 35791c30844dSMel Gorman 35801c30844dSMel Gorman restart: 35811c30844dSMel Gorman sc.priority = DEF_PRIORITY; 35829e3b2f8cSKonstantin Khlebnikov do { 3583c73322d0SJohannes Weiner unsigned long nr_reclaimed = sc.nr_reclaimed; 3584b8e83b94SMel Gorman bool raise_priority = true; 35851c30844dSMel Gorman bool balanced; 358693781325SOmar Sandoval bool ret; 3587b8e83b94SMel Gorman 358897a225e6SJoonsoo Kim sc.reclaim_idx = highest_zoneidx; 35891da177e4SLinus Torvalds 359086c79f6bSMel Gorman /* 359184c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 359284c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 359384c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 359484c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 359584c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 359684c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 359784c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 359884c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 359986c79f6bSMel Gorman */ 360086c79f6bSMel Gorman if (buffer_heads_over_limit) { 360186c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 360286c79f6bSMel Gorman zone = pgdat->node_zones + i; 36036aa303deSMel Gorman if (!managed_zone(zone)) 360486c79f6bSMel Gorman continue; 360586c79f6bSMel Gorman 3606970a39a3SMel Gorman sc.reclaim_idx = i; 360786c79f6bSMel Gorman break; 360886c79f6bSMel Gorman } 360986c79f6bSMel Gorman } 361086c79f6bSMel Gorman 361186c79f6bSMel Gorman /* 36121c30844dSMel Gorman * If the pgdat is imbalanced then ignore boosting and preserve 36131c30844dSMel Gorman * the watermarks for a later time and restart. Note that the 36141c30844dSMel Gorman * zone watermarks will be still reset at the end of balancing 36151c30844dSMel Gorman * on the grounds that the normal reclaim should be enough to 36161c30844dSMel Gorman * re-evaluate if boosting is required when kswapd next wakes. 361786c79f6bSMel Gorman */ 361897a225e6SJoonsoo Kim balanced = pgdat_balanced(pgdat, sc.order, highest_zoneidx); 36191c30844dSMel Gorman if (!balanced && nr_boost_reclaim) { 36201c30844dSMel Gorman nr_boost_reclaim = 0; 36211c30844dSMel Gorman goto restart; 36221c30844dSMel Gorman } 36231c30844dSMel Gorman 36241c30844dSMel Gorman /* 36251c30844dSMel Gorman * If boosting is not active then only reclaim if there are no 36261c30844dSMel Gorman * eligible zones. Note that sc.reclaim_idx is not used as 36271c30844dSMel Gorman * buffer_heads_over_limit may have adjusted it. 36281c30844dSMel Gorman */ 36291c30844dSMel Gorman if (!nr_boost_reclaim && balanced) 36301da177e4SLinus Torvalds goto out; 3631e1dbeda6SAndrew Morton 36321c30844dSMel Gorman /* Limit the priority of boosting to avoid reclaim writeback */ 36331c30844dSMel Gorman if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2) 36341c30844dSMel Gorman raise_priority = false; 36351c30844dSMel Gorman 36361c30844dSMel Gorman /* 36371c30844dSMel Gorman * Do not writeback or swap pages for boosted reclaim. The 36381c30844dSMel Gorman * intent is to relieve pressure not issue sub-optimal IO 36391c30844dSMel Gorman * from reclaim context. If no pages are reclaimed, the 36401c30844dSMel Gorman * reclaim will be aborted. 36411c30844dSMel Gorman */ 36421c30844dSMel Gorman sc.may_writepage = !laptop_mode && !nr_boost_reclaim; 36431c30844dSMel Gorman sc.may_swap = !nr_boost_reclaim; 36441c30844dSMel Gorman 36451da177e4SLinus Torvalds /* 36461d82de61SMel Gorman * Do some background aging of the anon list, to give 36471d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 36481d82de61SMel Gorman * pages are rotated regardless of classzone as this is 36491d82de61SMel Gorman * about consistent aging. 36501d82de61SMel Gorman */ 3651ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 36521d82de61SMel Gorman 36531d82de61SMel Gorman /* 3654b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 3655b7ea3c41SMel Gorman * even in laptop mode. 3656b7ea3c41SMel Gorman */ 3657047d72c3SJohannes Weiner if (sc.priority < DEF_PRIORITY - 2) 3658b7ea3c41SMel Gorman sc.may_writepage = 1; 3659b7ea3c41SMel Gorman 36601d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 36611da177e4SLinus Torvalds sc.nr_scanned = 0; 36620608f43dSAndrew Morton nr_soft_scanned = 0; 3663ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 36641d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 36650608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 36660608f43dSAndrew Morton 366732a4330dSRik van Riel /* 36681d82de61SMel Gorman * There should be no need to raise the scanning priority if 36691d82de61SMel Gorman * enough pages are already being scanned that that high 36701d82de61SMel Gorman * watermark would be met at 100% efficiency. 367132a4330dSRik van Riel */ 3672970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 3673b8e83b94SMel Gorman raise_priority = false; 3674d7868daeSMel Gorman 36755515061dSMel Gorman /* 36765515061dSMel Gorman * If the low watermark is met there is no need for processes 36775515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 36785515061dSMel Gorman * able to safely make forward progress. Wake them 36795515061dSMel Gorman */ 36805515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 3681c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)) 3682cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 36835515061dSMel Gorman 3684b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 368593781325SOmar Sandoval __fs_reclaim_release(); 368693781325SOmar Sandoval ret = try_to_freeze(); 368793781325SOmar Sandoval __fs_reclaim_acquire(); 368893781325SOmar Sandoval if (ret || kthread_should_stop()) 3689b8e83b94SMel Gorman break; 3690b8e83b94SMel Gorman 3691b8e83b94SMel Gorman /* 3692b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3693b8e83b94SMel Gorman * progress in reclaiming pages 3694b8e83b94SMel Gorman */ 3695c73322d0SJohannes Weiner nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 36961c30844dSMel Gorman nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed); 36971c30844dSMel Gorman 36981c30844dSMel Gorman /* 36991c30844dSMel Gorman * If reclaim made no progress for a boost, stop reclaim as 37001c30844dSMel Gorman * IO cannot be queued and it could be an infinite loop in 37011c30844dSMel Gorman * extreme circumstances. 37021c30844dSMel Gorman */ 37031c30844dSMel Gorman if (nr_boost_reclaim && !nr_reclaimed) 37041c30844dSMel Gorman break; 37051c30844dSMel Gorman 3706c73322d0SJohannes Weiner if (raise_priority || !nr_reclaimed) 3707b8e83b94SMel Gorman sc.priority--; 37081d82de61SMel Gorman } while (sc.priority >= 1); 37091da177e4SLinus Torvalds 3710c73322d0SJohannes Weiner if (!sc.nr_reclaimed) 3711c73322d0SJohannes Weiner pgdat->kswapd_failures++; 3712c73322d0SJohannes Weiner 3713b8e83b94SMel Gorman out: 37141c30844dSMel Gorman /* If reclaim was boosted, account for the reclaim done in this pass */ 37151c30844dSMel Gorman if (boosted) { 37161c30844dSMel Gorman unsigned long flags; 37171c30844dSMel Gorman 371897a225e6SJoonsoo Kim for (i = 0; i <= highest_zoneidx; i++) { 37191c30844dSMel Gorman if (!zone_boosts[i]) 37201c30844dSMel Gorman continue; 37211c30844dSMel Gorman 37221c30844dSMel Gorman /* Increments are under the zone lock */ 37231c30844dSMel Gorman zone = pgdat->node_zones + i; 37241c30844dSMel Gorman spin_lock_irqsave(&zone->lock, flags); 37251c30844dSMel Gorman zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]); 37261c30844dSMel Gorman spin_unlock_irqrestore(&zone->lock, flags); 37271c30844dSMel Gorman } 37281c30844dSMel Gorman 37291c30844dSMel Gorman /* 37301c30844dSMel Gorman * As there is now likely space, wakeup kcompact to defragment 37311c30844dSMel Gorman * pageblocks. 37321c30844dSMel Gorman */ 373397a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, pageblock_order, highest_zoneidx); 37341c30844dSMel Gorman } 37351c30844dSMel Gorman 37362a2e4885SJohannes Weiner snapshot_refaults(NULL, pgdat); 373793781325SOmar Sandoval __fs_reclaim_release(); 3738eb414681SJohannes Weiner psi_memstall_leave(&pflags); 37391732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 3740e5ca8071SYafang Shao 37410abdee2bSMel Gorman /* 37421d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 37431d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 37441d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 37451d82de61SMel Gorman * remain at the higher level. 37460abdee2bSMel Gorman */ 37471d82de61SMel Gorman return sc.order; 37481da177e4SLinus Torvalds } 37491da177e4SLinus Torvalds 3750e716f2ebSMel Gorman /* 375197a225e6SJoonsoo Kim * The pgdat->kswapd_highest_zoneidx is used to pass the highest zone index to 375297a225e6SJoonsoo Kim * be reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is 375397a225e6SJoonsoo Kim * not a valid index then either kswapd runs for first time or kswapd couldn't 375497a225e6SJoonsoo Kim * sleep after previous reclaim attempt (node is still unbalanced). In that 375597a225e6SJoonsoo Kim * case return the zone index of the previous kswapd reclaim cycle. 3756e716f2ebSMel Gorman */ 375797a225e6SJoonsoo Kim static enum zone_type kswapd_highest_zoneidx(pg_data_t *pgdat, 375897a225e6SJoonsoo Kim enum zone_type prev_highest_zoneidx) 3759e716f2ebSMel Gorman { 376097a225e6SJoonsoo Kim enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 37615644e1fbSQian Cai 376297a225e6SJoonsoo Kim return curr_idx == MAX_NR_ZONES ? prev_highest_zoneidx : curr_idx; 3763e716f2ebSMel Gorman } 3764e716f2ebSMel Gorman 376538087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 376697a225e6SJoonsoo Kim unsigned int highest_zoneidx) 3767f0bc0a60SKOSAKI Motohiro { 3768f0bc0a60SKOSAKI Motohiro long remaining = 0; 3769f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3770f0bc0a60SKOSAKI Motohiro 3771f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3772f0bc0a60SKOSAKI Motohiro return; 3773f0bc0a60SKOSAKI Motohiro 3774f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3775f0bc0a60SKOSAKI Motohiro 3776333b0a45SShantanu Goel /* 3777333b0a45SShantanu Goel * Try to sleep for a short interval. Note that kcompactd will only be 3778333b0a45SShantanu Goel * woken if it is possible to sleep for a short interval. This is 3779333b0a45SShantanu Goel * deliberate on the assumption that if reclaim cannot keep an 3780333b0a45SShantanu Goel * eligible zone balanced that it's also unlikely that compaction will 3781333b0a45SShantanu Goel * succeed. 3782333b0a45SShantanu Goel */ 378397a225e6SJoonsoo Kim if (prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 3784fd901c95SVlastimil Babka /* 3785fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 3786fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 3787fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 3788fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 3789fd901c95SVlastimil Babka */ 3790fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 3791fd901c95SVlastimil Babka 3792fd901c95SVlastimil Babka /* 3793fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 3794fd901c95SVlastimil Babka * allocation of the requested order possible. 3795fd901c95SVlastimil Babka */ 379697a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, alloc_order, highest_zoneidx); 3797fd901c95SVlastimil Babka 3798f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 379938087d9bSMel Gorman 380038087d9bSMel Gorman /* 380197a225e6SJoonsoo Kim * If woken prematurely then reset kswapd_highest_zoneidx and 380238087d9bSMel Gorman * order. The values will either be from a wakeup request or 380338087d9bSMel Gorman * the previous request that slept prematurely. 380438087d9bSMel Gorman */ 380538087d9bSMel Gorman if (remaining) { 380697a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, 380797a225e6SJoonsoo Kim kswapd_highest_zoneidx(pgdat, 380897a225e6SJoonsoo Kim highest_zoneidx)); 38095644e1fbSQian Cai 38105644e1fbSQian Cai if (READ_ONCE(pgdat->kswapd_order) < reclaim_order) 38115644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, reclaim_order); 381238087d9bSMel Gorman } 381338087d9bSMel Gorman 3814f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3815f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3816f0bc0a60SKOSAKI Motohiro } 3817f0bc0a60SKOSAKI Motohiro 3818f0bc0a60SKOSAKI Motohiro /* 3819f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3820f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3821f0bc0a60SKOSAKI Motohiro */ 3822d9f21d42SMel Gorman if (!remaining && 382397a225e6SJoonsoo Kim prepare_kswapd_sleep(pgdat, reclaim_order, highest_zoneidx)) { 3824f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3825f0bc0a60SKOSAKI Motohiro 3826f0bc0a60SKOSAKI Motohiro /* 3827f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3828f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3829f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3830f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3831f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3832f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3833f0bc0a60SKOSAKI Motohiro */ 3834f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 38351c7e7f6cSAaditya Kumar 38361c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3837f0bc0a60SKOSAKI Motohiro schedule(); 38381c7e7f6cSAaditya Kumar 3839f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3840f0bc0a60SKOSAKI Motohiro } else { 3841f0bc0a60SKOSAKI Motohiro if (remaining) 3842f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3843f0bc0a60SKOSAKI Motohiro else 3844f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3845f0bc0a60SKOSAKI Motohiro } 3846f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3847f0bc0a60SKOSAKI Motohiro } 3848f0bc0a60SKOSAKI Motohiro 38491da177e4SLinus Torvalds /* 38501da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 38511da177e4SLinus Torvalds * from the init process. 38521da177e4SLinus Torvalds * 38531da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 38541da177e4SLinus Torvalds * free memory available even if there is no other activity 38551da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 38561da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 38571da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 38581da177e4SLinus Torvalds * 38591da177e4SLinus Torvalds * If there are applications that are active memory-allocators 38601da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 38611da177e4SLinus Torvalds */ 38621da177e4SLinus Torvalds static int kswapd(void *p) 38631da177e4SLinus Torvalds { 3864e716f2ebSMel Gorman unsigned int alloc_order, reclaim_order; 386597a225e6SJoonsoo Kim unsigned int highest_zoneidx = MAX_NR_ZONES - 1; 38661da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 38671da177e4SLinus Torvalds struct task_struct *tsk = current; 3868a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 38691da177e4SLinus Torvalds 3870174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3871c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 38721da177e4SLinus Torvalds 38731da177e4SLinus Torvalds /* 38741da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 38751da177e4SLinus Torvalds * and that if we need more memory we should get access to it 38761da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 38771da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 38781da177e4SLinus Torvalds * 38791da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 38801da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 38811da177e4SLinus Torvalds * page out something else, and this flag essentially protects 38821da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 38831da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 38841da177e4SLinus Torvalds */ 3885930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 388683144186SRafael J. Wysocki set_freezable(); 38871da177e4SLinus Torvalds 38885644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, 0); 388997a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 38901da177e4SLinus Torvalds for ( ; ; ) { 38916f6313d4SJeff Liu bool ret; 38923e1d1d28SChristoph Lameter 38935644e1fbSQian Cai alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order); 389497a225e6SJoonsoo Kim highest_zoneidx = kswapd_highest_zoneidx(pgdat, 389597a225e6SJoonsoo Kim highest_zoneidx); 3896e716f2ebSMel Gorman 389738087d9bSMel Gorman kswapd_try_sleep: 389838087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 389997a225e6SJoonsoo Kim highest_zoneidx); 3900215ddd66SMel Gorman 390197a225e6SJoonsoo Kim /* Read the new order and highest_zoneidx */ 39025644e1fbSQian Cai alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order); 390397a225e6SJoonsoo Kim highest_zoneidx = kswapd_highest_zoneidx(pgdat, 390497a225e6SJoonsoo Kim highest_zoneidx); 39055644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, 0); 390697a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, MAX_NR_ZONES); 39071da177e4SLinus Torvalds 39088fe23e05SDavid Rientjes ret = try_to_freeze(); 39098fe23e05SDavid Rientjes if (kthread_should_stop()) 39108fe23e05SDavid Rientjes break; 39118fe23e05SDavid Rientjes 39128fe23e05SDavid Rientjes /* 39138fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 39148fe23e05SDavid Rientjes * after returning from the refrigerator 3915b1296cc4SRafael J. Wysocki */ 391638087d9bSMel Gorman if (ret) 391738087d9bSMel Gorman continue; 39181d82de61SMel Gorman 391938087d9bSMel Gorman /* 392038087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 392138087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 392238087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 392338087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 392438087d9bSMel Gorman * but kcompactd is woken to compact for the original 392538087d9bSMel Gorman * request (alloc_order). 392638087d9bSMel Gorman */ 392797a225e6SJoonsoo Kim trace_mm_vmscan_kswapd_wake(pgdat->node_id, highest_zoneidx, 3928e5146b12SMel Gorman alloc_order); 392997a225e6SJoonsoo Kim reclaim_order = balance_pgdat(pgdat, alloc_order, 393097a225e6SJoonsoo Kim highest_zoneidx); 393138087d9bSMel Gorman if (reclaim_order < alloc_order) 393238087d9bSMel Gorman goto kswapd_try_sleep; 393333906bc5SMel Gorman } 3934b0a8cc58STakamori Yamaguchi 393571abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 393671abdc15SJohannes Weiner 39371da177e4SLinus Torvalds return 0; 39381da177e4SLinus Torvalds } 39391da177e4SLinus Torvalds 39401da177e4SLinus Torvalds /* 39415ecd9d40SDavid Rientjes * A zone is low on free memory or too fragmented for high-order memory. If 39425ecd9d40SDavid Rientjes * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's 39435ecd9d40SDavid Rientjes * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim 39445ecd9d40SDavid Rientjes * has failed or is not needed, still wake up kcompactd if only compaction is 39455ecd9d40SDavid Rientjes * needed. 39461da177e4SLinus Torvalds */ 39475ecd9d40SDavid Rientjes void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, 394897a225e6SJoonsoo Kim enum zone_type highest_zoneidx) 39491da177e4SLinus Torvalds { 39501da177e4SLinus Torvalds pg_data_t *pgdat; 39515644e1fbSQian Cai enum zone_type curr_idx; 39521da177e4SLinus Torvalds 39536aa303deSMel Gorman if (!managed_zone(zone)) 39541da177e4SLinus Torvalds return; 39551da177e4SLinus Torvalds 39565ecd9d40SDavid Rientjes if (!cpuset_zone_allowed(zone, gfp_flags)) 39571da177e4SLinus Torvalds return; 3958dffcac2cSShakeel Butt 39595644e1fbSQian Cai pgdat = zone->zone_pgdat; 396097a225e6SJoonsoo Kim curr_idx = READ_ONCE(pgdat->kswapd_highest_zoneidx); 39615644e1fbSQian Cai 396297a225e6SJoonsoo Kim if (curr_idx == MAX_NR_ZONES || curr_idx < highest_zoneidx) 396397a225e6SJoonsoo Kim WRITE_ONCE(pgdat->kswapd_highest_zoneidx, highest_zoneidx); 39645644e1fbSQian Cai 39655644e1fbSQian Cai if (READ_ONCE(pgdat->kswapd_order) < order) 39665644e1fbSQian Cai WRITE_ONCE(pgdat->kswapd_order, order); 39675644e1fbSQian Cai 39688d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 39691da177e4SLinus Torvalds return; 3970e1a55637SMel Gorman 39715ecd9d40SDavid Rientjes /* Hopeless node, leave it to direct reclaim if possible */ 39725ecd9d40SDavid Rientjes if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || 397397a225e6SJoonsoo Kim (pgdat_balanced(pgdat, order, highest_zoneidx) && 397497a225e6SJoonsoo Kim !pgdat_watermark_boosted(pgdat, highest_zoneidx))) { 39755ecd9d40SDavid Rientjes /* 39765ecd9d40SDavid Rientjes * There may be plenty of free memory available, but it's too 39775ecd9d40SDavid Rientjes * fragmented for high-order allocations. Wake up kcompactd 39785ecd9d40SDavid Rientjes * and rely on compaction_suitable() to determine if it's 39795ecd9d40SDavid Rientjes * needed. If it fails, it will defer subsequent attempts to 39805ecd9d40SDavid Rientjes * ratelimit its work. 39815ecd9d40SDavid Rientjes */ 39825ecd9d40SDavid Rientjes if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) 398397a225e6SJoonsoo Kim wakeup_kcompactd(pgdat, order, highest_zoneidx); 3984c73322d0SJohannes Weiner return; 39855ecd9d40SDavid Rientjes } 3986c73322d0SJohannes Weiner 398797a225e6SJoonsoo Kim trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, highest_zoneidx, order, 39885ecd9d40SDavid Rientjes gfp_flags); 39898d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 39901da177e4SLinus Torvalds } 39911da177e4SLinus Torvalds 3992c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 39931da177e4SLinus Torvalds /* 39947b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3995d6277db4SRafael J. Wysocki * freed pages. 3996d6277db4SRafael J. Wysocki * 3997d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3998d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3999d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 40001da177e4SLinus Torvalds */ 40017b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 40021da177e4SLinus Torvalds { 4003d6277db4SRafael J. Wysocki struct scan_control sc = { 40047b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 4005ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 4006b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 40079e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 4008ee814fe2SJohannes Weiner .may_writepage = 1, 4009ee814fe2SJohannes Weiner .may_unmap = 1, 4010ee814fe2SJohannes Weiner .may_swap = 1, 4011ee814fe2SJohannes Weiner .hibernation_mode = 1, 40121da177e4SLinus Torvalds }; 40137b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 40147b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 4015499118e9SVlastimil Babka unsigned int noreclaim_flag; 40161da177e4SLinus Torvalds 4017d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 401893781325SOmar Sandoval noreclaim_flag = memalloc_noreclaim_save(); 40191732d2b0SAndrew Morton set_task_reclaim_state(current, &sc.reclaim_state); 4020d6277db4SRafael J. Wysocki 40213115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 4022d6277db4SRafael J. Wysocki 40231732d2b0SAndrew Morton set_task_reclaim_state(current, NULL); 4024499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 402593781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 4026d6277db4SRafael J. Wysocki 40277b51755cSKOSAKI Motohiro return nr_reclaimed; 40281da177e4SLinus Torvalds } 4029c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 40301da177e4SLinus Torvalds 40313218ae14SYasunori Goto /* 40323218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 40333218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 40343218ae14SYasunori Goto */ 40353218ae14SYasunori Goto int kswapd_run(int nid) 40363218ae14SYasunori Goto { 40373218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 40383218ae14SYasunori Goto int ret = 0; 40393218ae14SYasunori Goto 40403218ae14SYasunori Goto if (pgdat->kswapd) 40413218ae14SYasunori Goto return 0; 40423218ae14SYasunori Goto 40433218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 40443218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 40453218ae14SYasunori Goto /* failure at boot is fatal */ 4046c6202adfSThomas Gleixner BUG_ON(system_state < SYSTEM_RUNNING); 4047d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 4048d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 4049d72515b8SXishi Qiu pgdat->kswapd = NULL; 40503218ae14SYasunori Goto } 40513218ae14SYasunori Goto return ret; 40523218ae14SYasunori Goto } 40533218ae14SYasunori Goto 40548fe23e05SDavid Rientjes /* 4055d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 4056bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 40578fe23e05SDavid Rientjes */ 40588fe23e05SDavid Rientjes void kswapd_stop(int nid) 40598fe23e05SDavid Rientjes { 40608fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 40618fe23e05SDavid Rientjes 4062d8adde17SJiang Liu if (kswapd) { 40638fe23e05SDavid Rientjes kthread_stop(kswapd); 4064d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 4065d8adde17SJiang Liu } 40668fe23e05SDavid Rientjes } 40678fe23e05SDavid Rientjes 40681da177e4SLinus Torvalds static int __init kswapd_init(void) 40691da177e4SLinus Torvalds { 40706b700b5bSWei Yang int nid; 407169e05944SAndrew Morton 40721da177e4SLinus Torvalds swap_setup(); 407348fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 40743218ae14SYasunori Goto kswapd_run(nid); 40751da177e4SLinus Torvalds return 0; 40761da177e4SLinus Torvalds } 40771da177e4SLinus Torvalds 40781da177e4SLinus Torvalds module_init(kswapd_init) 40799eeff239SChristoph Lameter 40809eeff239SChristoph Lameter #ifdef CONFIG_NUMA 40819eeff239SChristoph Lameter /* 4082a5f5f91dSMel Gorman * Node reclaim mode 40839eeff239SChristoph Lameter * 4084a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 40859eeff239SChristoph Lameter * the watermarks. 40869eeff239SChristoph Lameter */ 4087a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 40889eeff239SChristoph Lameter 4089648b5cf3SAlex Shi #define RECLAIM_WRITE (1<<0) /* Writeout pages during reclaim */ 4090648b5cf3SAlex Shi #define RECLAIM_UNMAP (1<<1) /* Unmap pages during reclaim */ 40911b2ffb78SChristoph Lameter 40929eeff239SChristoph Lameter /* 4093a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 4094a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 4095a92f7126SChristoph Lameter * a zone. 4096a92f7126SChristoph Lameter */ 4097a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 4098a92f7126SChristoph Lameter 40999eeff239SChristoph Lameter /* 4100a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 41019614634fSChristoph Lameter * occur. 41029614634fSChristoph Lameter */ 41039614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 41049614634fSChristoph Lameter 41059614634fSChristoph Lameter /* 41060ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 41070ff38490SChristoph Lameter * slab reclaim needs to occur. 41080ff38490SChristoph Lameter */ 41090ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 41100ff38490SChristoph Lameter 411111fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 411290afa5deSMel Gorman { 411311fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 411411fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 411511fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 411690afa5deSMel Gorman 411790afa5deSMel Gorman /* 411890afa5deSMel Gorman * It's possible for there to be more file mapped pages than 411990afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 412090afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 412190afa5deSMel Gorman */ 412290afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 412390afa5deSMel Gorman } 412490afa5deSMel Gorman 412590afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 4126a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 412790afa5deSMel Gorman { 4128d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 4129d031a157SAlexandru Moise unsigned long delta = 0; 413090afa5deSMel Gorman 413190afa5deSMel Gorman /* 413295bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 413390afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 413411fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 413590afa5deSMel Gorman * a better estimate 413690afa5deSMel Gorman */ 4137a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 4138a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 413990afa5deSMel Gorman else 4140a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 414190afa5deSMel Gorman 414290afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 4143a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 4144a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 414590afa5deSMel Gorman 414690afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 414790afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 414890afa5deSMel Gorman delta = nr_pagecache_reclaimable; 414990afa5deSMel Gorman 415090afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 415190afa5deSMel Gorman } 415290afa5deSMel Gorman 41530ff38490SChristoph Lameter /* 4154a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 41559eeff239SChristoph Lameter */ 4156a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 41579eeff239SChristoph Lameter { 41587fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 415969e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 41609eeff239SChristoph Lameter struct task_struct *p = current; 4161499118e9SVlastimil Babka unsigned int noreclaim_flag; 4162179e9639SAndrew Morton struct scan_control sc = { 416362b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 4164f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 4165bd2f6199SJohannes Weiner .order = order, 4166a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 4167a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 4168a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 4169ee814fe2SJohannes Weiner .may_swap = 1, 4170f2f43e56SNick Desaulniers .reclaim_idx = gfp_zone(gfp_mask), 4171179e9639SAndrew Morton }; 41729eeff239SChristoph Lameter 4173132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order, 4174132bb8cfSYafang Shao sc.gfp_mask); 4175132bb8cfSYafang Shao 41769eeff239SChristoph Lameter cond_resched(); 417793781325SOmar Sandoval fs_reclaim_acquire(sc.gfp_mask); 4178d4f7796eSChristoph Lameter /* 417995bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 4180d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 418195bbc0c7SZhihui Zhang * and RECLAIM_UNMAP. 4182d4f7796eSChristoph Lameter */ 4183499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 4184499118e9SVlastimil Babka p->flags |= PF_SWAPWRITE; 41851732d2b0SAndrew Morton set_task_reclaim_state(p, &sc.reclaim_state); 4186c84db23cSChristoph Lameter 4187a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 4188a92f7126SChristoph Lameter /* 4189894befecSAndrey Ryabinin * Free memory by calling shrink node with increasing 41900ff38490SChristoph Lameter * priorities until we have enough memory freed. 4191a92f7126SChristoph Lameter */ 4192a92f7126SChristoph Lameter do { 4193970a39a3SMel Gorman shrink_node(pgdat, &sc); 41949e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 41950ff38490SChristoph Lameter } 4196a92f7126SChristoph Lameter 41971732d2b0SAndrew Morton set_task_reclaim_state(p, NULL); 4198499118e9SVlastimil Babka current->flags &= ~PF_SWAPWRITE; 4199499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 420093781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 4201132bb8cfSYafang Shao 4202132bb8cfSYafang Shao trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed); 4203132bb8cfSYafang Shao 4204a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 42059eeff239SChristoph Lameter } 4206179e9639SAndrew Morton 4207a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 4208179e9639SAndrew Morton { 4209d773ed6bSDavid Rientjes int ret; 4210179e9639SAndrew Morton 4211179e9639SAndrew Morton /* 4212a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 42130ff38490SChristoph Lameter * slab pages if we are over the defined limits. 421434aa1330SChristoph Lameter * 42159614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 42169614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 4217a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 4218a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 42199614634fSChristoph Lameter * unmapped file backed pages. 4220179e9639SAndrew Morton */ 4221a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 4222d42f3245SRoman Gushchin node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B) <= 4223d42f3245SRoman Gushchin pgdat->min_slab_pages) 4224a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 4225179e9639SAndrew Morton 4226179e9639SAndrew Morton /* 4227d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 4228179e9639SAndrew Morton */ 4229d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 4230a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4231179e9639SAndrew Morton 4232179e9639SAndrew Morton /* 4233a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 4234179e9639SAndrew Morton * have associated processors. This will favor the local processor 4235179e9639SAndrew Morton * over remote processors and spread off node memory allocations 4236179e9639SAndrew Morton * as wide as possible. 4237179e9639SAndrew Morton */ 4238a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 4239a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4240d773ed6bSDavid Rientjes 4241a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 4242a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4243fa5e084eSMel Gorman 4244a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 4245a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 4246d773ed6bSDavid Rientjes 424724cf7251SMel Gorman if (!ret) 424824cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 424924cf7251SMel Gorman 4250d773ed6bSDavid Rientjes return ret; 4251179e9639SAndrew Morton } 42529eeff239SChristoph Lameter #endif 4253894bc310SLee Schermerhorn 425489e004eaSLee Schermerhorn /** 425564e3d12fSKuo-Hsin Yang * check_move_unevictable_pages - check pages for evictability and move to 425664e3d12fSKuo-Hsin Yang * appropriate zone lru list 425764e3d12fSKuo-Hsin Yang * @pvec: pagevec with lru pages to check 425889e004eaSLee Schermerhorn * 425964e3d12fSKuo-Hsin Yang * Checks pages for evictability, if an evictable page is in the unevictable 426064e3d12fSKuo-Hsin Yang * lru list, moves it to the appropriate evictable lru list. This function 426164e3d12fSKuo-Hsin Yang * should be only used for lru pages. 426289e004eaSLee Schermerhorn */ 426364e3d12fSKuo-Hsin Yang void check_move_unevictable_pages(struct pagevec *pvec) 426489e004eaSLee Schermerhorn { 4265925b7673SJohannes Weiner struct lruvec *lruvec; 4266785b99feSMel Gorman struct pglist_data *pgdat = NULL; 426724513264SHugh Dickins int pgscanned = 0; 426824513264SHugh Dickins int pgrescued = 0; 426989e004eaSLee Schermerhorn int i; 427089e004eaSLee Schermerhorn 427164e3d12fSKuo-Hsin Yang for (i = 0; i < pvec->nr; i++) { 427264e3d12fSKuo-Hsin Yang struct page *page = pvec->pages[i]; 4273785b99feSMel Gorman struct pglist_data *pagepgdat = page_pgdat(page); 42748d8869caSHugh Dickins int nr_pages; 427589e004eaSLee Schermerhorn 42768d8869caSHugh Dickins if (PageTransTail(page)) 42778d8869caSHugh Dickins continue; 42788d8869caSHugh Dickins 42798d8869caSHugh Dickins nr_pages = thp_nr_pages(page); 42808d8869caSHugh Dickins pgscanned += nr_pages; 42818d8869caSHugh Dickins 4282785b99feSMel Gorman if (pagepgdat != pgdat) { 4283785b99feSMel Gorman if (pgdat) 4284785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 4285785b99feSMel Gorman pgdat = pagepgdat; 4286785b99feSMel Gorman spin_lock_irq(&pgdat->lru_lock); 428789e004eaSLee Schermerhorn } 4288785b99feSMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 428989e004eaSLee Schermerhorn 429024513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 429124513264SHugh Dickins continue; 429289e004eaSLee Schermerhorn 429339b5f29aSHugh Dickins if (page_evictable(page)) { 429424513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 429524513264SHugh Dickins 4296309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 429724513264SHugh Dickins ClearPageUnevictable(page); 4298fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 4299fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 43008d8869caSHugh Dickins pgrescued += nr_pages; 430189e004eaSLee Schermerhorn } 430289e004eaSLee Schermerhorn } 430324513264SHugh Dickins 4304785b99feSMel Gorman if (pgdat) { 430524513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 430624513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 4307785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 430824513264SHugh Dickins } 430985046579SHugh Dickins } 431064e3d12fSKuo-Hsin Yang EXPORT_SYMBOL_GPL(check_move_unevictable_pages); 4311