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> 49268bb0ceSLinus Torvalds #include <linux/prefetch.h> 50b1de0d13SMitchel Humpherys #include <linux/printk.h> 51f9fe48beSRoss Zwisler #include <linux/dax.h> 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds #include <asm/tlbflush.h> 541da177e4SLinus Torvalds #include <asm/div64.h> 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds #include <linux/swapops.h> 57117aad1eSRafael Aquini #include <linux/balloon_compaction.h> 581da177e4SLinus Torvalds 590f8053a5SNick Piggin #include "internal.h" 600f8053a5SNick Piggin 6133906bc5SMel Gorman #define CREATE_TRACE_POINTS 6233906bc5SMel Gorman #include <trace/events/vmscan.h> 6333906bc5SMel Gorman 641da177e4SLinus Torvalds struct scan_control { 6522fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6622fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 6722fba335SKOSAKI Motohiro 68ee814fe2SJohannes Weiner /* 69ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 70ee814fe2SJohannes Weiner * are scanned. 71ee814fe2SJohannes Weiner */ 72ee814fe2SJohannes Weiner nodemask_t *nodemask; 739e3b2f8cSKonstantin Khlebnikov 745f53e762SKOSAKI Motohiro /* 75f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 76f16015fbSJohannes Weiner * primary target of this reclaim invocation. 77f16015fbSJohannes Weiner */ 78f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 7966e1707bSBalbir Singh 801276ad68SJohannes Weiner /* Writepage batching in laptop mode; RECLAIM_WRITE */ 81ee814fe2SJohannes Weiner unsigned int may_writepage:1; 82ee814fe2SJohannes Weiner 83ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 84ee814fe2SJohannes Weiner unsigned int may_unmap:1; 85ee814fe2SJohannes Weiner 86ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 87ee814fe2SJohannes Weiner unsigned int may_swap:1; 88ee814fe2SJohannes Weiner 89d6622f63SYisheng Xie /* 90d6622f63SYisheng Xie * Cgroups are not reclaimed below their configured memory.low, 91d6622f63SYisheng Xie * unless we threaten to OOM. If any cgroups are skipped due to 92d6622f63SYisheng Xie * memory.low and nothing was reclaimed, go back for memory.low. 93d6622f63SYisheng Xie */ 94d6622f63SYisheng Xie unsigned int memcg_low_reclaim:1; 95d6622f63SYisheng Xie unsigned int memcg_low_skipped:1; 96241994edSJohannes Weiner 97ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 98ee814fe2SJohannes Weiner 99ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 100ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 101ee814fe2SJohannes Weiner 102bb451fdfSGreg Thelen /* Allocation order */ 103bb451fdfSGreg Thelen s8 order; 104bb451fdfSGreg Thelen 105bb451fdfSGreg Thelen /* Scan (total_size >> priority) pages at once */ 106bb451fdfSGreg Thelen s8 priority; 107bb451fdfSGreg Thelen 108bb451fdfSGreg Thelen /* The highest zone to isolate pages for reclaim from */ 109bb451fdfSGreg Thelen s8 reclaim_idx; 110bb451fdfSGreg Thelen 111bb451fdfSGreg Thelen /* This context's GFP mask */ 112bb451fdfSGreg Thelen gfp_t gfp_mask; 113bb451fdfSGreg Thelen 114ee814fe2SJohannes Weiner /* Incremented by the number of inactive pages that were scanned */ 115ee814fe2SJohannes Weiner unsigned long nr_scanned; 116ee814fe2SJohannes Weiner 117ee814fe2SJohannes Weiner /* Number of pages freed so far during a call to shrink_zones() */ 118ee814fe2SJohannes Weiner unsigned long nr_reclaimed; 119d108c772SAndrey Ryabinin 120d108c772SAndrey Ryabinin struct { 121d108c772SAndrey Ryabinin unsigned int dirty; 122d108c772SAndrey Ryabinin unsigned int unqueued_dirty; 123d108c772SAndrey Ryabinin unsigned int congested; 124d108c772SAndrey Ryabinin unsigned int writeback; 125d108c772SAndrey Ryabinin unsigned int immediate; 126d108c772SAndrey Ryabinin unsigned int file_taken; 127d108c772SAndrey Ryabinin unsigned int taken; 128d108c772SAndrey Ryabinin } nr; 1291da177e4SLinus Torvalds }; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1321da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1331da177e4SLinus Torvalds do { \ 1341da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1351da177e4SLinus Torvalds struct page *prev; \ 1361da177e4SLinus Torvalds \ 1371da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1381da177e4SLinus Torvalds prefetch(&prev->_field); \ 1391da177e4SLinus Torvalds } \ 1401da177e4SLinus Torvalds } while (0) 1411da177e4SLinus Torvalds #else 1421da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1431da177e4SLinus Torvalds #endif 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1461da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1471da177e4SLinus Torvalds do { \ 1481da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1491da177e4SLinus Torvalds struct page *prev; \ 1501da177e4SLinus Torvalds \ 1511da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1521da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1531da177e4SLinus Torvalds } \ 1541da177e4SLinus Torvalds } while (0) 1551da177e4SLinus Torvalds #else 1561da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1571da177e4SLinus Torvalds #endif 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds /* 1601da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1611da177e4SLinus Torvalds */ 1621da177e4SLinus Torvalds int vm_swappiness = 60; 163d0480be4SWang Sheng-Hui /* 164d0480be4SWang Sheng-Hui * The total number of pages which are beyond the high watermark within all 165d0480be4SWang Sheng-Hui * zones. 166d0480be4SWang Sheng-Hui */ 167d0480be4SWang Sheng-Hui unsigned long vm_total_pages; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1701da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1711da177e4SLinus Torvalds 172b4c2b231SKirill Tkhai #ifdef CONFIG_MEMCG_KMEM 173*7e010df5SKirill Tkhai 174*7e010df5SKirill Tkhai /* 175*7e010df5SKirill Tkhai * We allow subsystems to populate their shrinker-related 176*7e010df5SKirill Tkhai * LRU lists before register_shrinker_prepared() is called 177*7e010df5SKirill Tkhai * for the shrinker, since we don't want to impose 178*7e010df5SKirill Tkhai * restrictions on their internal registration order. 179*7e010df5SKirill Tkhai * In this case shrink_slab_memcg() may find corresponding 180*7e010df5SKirill Tkhai * bit is set in the shrinkers map. 181*7e010df5SKirill Tkhai * 182*7e010df5SKirill Tkhai * This value is used by the function to detect registering 183*7e010df5SKirill Tkhai * shrinkers and to skip do_shrink_slab() calls for them. 184*7e010df5SKirill Tkhai */ 185*7e010df5SKirill Tkhai #define SHRINKER_REGISTERING ((struct shrinker *)~0UL) 186*7e010df5SKirill Tkhai 187b4c2b231SKirill Tkhai static DEFINE_IDR(shrinker_idr); 188b4c2b231SKirill Tkhai static int shrinker_nr_max; 189b4c2b231SKirill Tkhai 190b4c2b231SKirill Tkhai static int prealloc_memcg_shrinker(struct shrinker *shrinker) 191b4c2b231SKirill Tkhai { 192b4c2b231SKirill Tkhai int id, ret = -ENOMEM; 193b4c2b231SKirill Tkhai 194b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 195b4c2b231SKirill Tkhai /* This may call shrinker, so it must use down_read_trylock() */ 196*7e010df5SKirill Tkhai id = idr_alloc(&shrinker_idr, SHRINKER_REGISTERING, 0, 0, GFP_KERNEL); 197b4c2b231SKirill Tkhai if (id < 0) 198b4c2b231SKirill Tkhai goto unlock; 199b4c2b231SKirill Tkhai 2000a4465d3SKirill Tkhai if (id >= shrinker_nr_max) { 2010a4465d3SKirill Tkhai if (memcg_expand_shrinker_maps(id)) { 2020a4465d3SKirill Tkhai idr_remove(&shrinker_idr, id); 2030a4465d3SKirill Tkhai goto unlock; 2040a4465d3SKirill Tkhai } 2050a4465d3SKirill Tkhai 206b4c2b231SKirill Tkhai shrinker_nr_max = id + 1; 2070a4465d3SKirill Tkhai } 208b4c2b231SKirill Tkhai shrinker->id = id; 209b4c2b231SKirill Tkhai ret = 0; 210b4c2b231SKirill Tkhai unlock: 211b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 212b4c2b231SKirill Tkhai return ret; 213b4c2b231SKirill Tkhai } 214b4c2b231SKirill Tkhai 215b4c2b231SKirill Tkhai static void unregister_memcg_shrinker(struct shrinker *shrinker) 216b4c2b231SKirill Tkhai { 217b4c2b231SKirill Tkhai int id = shrinker->id; 218b4c2b231SKirill Tkhai 219b4c2b231SKirill Tkhai BUG_ON(id < 0); 220b4c2b231SKirill Tkhai 221b4c2b231SKirill Tkhai down_write(&shrinker_rwsem); 222b4c2b231SKirill Tkhai idr_remove(&shrinker_idr, id); 223b4c2b231SKirill Tkhai up_write(&shrinker_rwsem); 224b4c2b231SKirill Tkhai } 225b4c2b231SKirill Tkhai #else /* CONFIG_MEMCG_KMEM */ 226b4c2b231SKirill Tkhai static int prealloc_memcg_shrinker(struct shrinker *shrinker) 227b4c2b231SKirill Tkhai { 228b4c2b231SKirill Tkhai return 0; 229b4c2b231SKirill Tkhai } 230b4c2b231SKirill Tkhai 231b4c2b231SKirill Tkhai static void unregister_memcg_shrinker(struct shrinker *shrinker) 232b4c2b231SKirill Tkhai { 233b4c2b231SKirill Tkhai } 234b4c2b231SKirill Tkhai #endif /* CONFIG_MEMCG_KMEM */ 235b4c2b231SKirill Tkhai 236c255a458SAndrew Morton #ifdef CONFIG_MEMCG 23789b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 23889b5fae5SJohannes Weiner { 239f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 24089b5fae5SJohannes Weiner } 24197c9341fSTejun Heo 24297c9341fSTejun Heo /** 24397c9341fSTejun Heo * sane_reclaim - is the usual dirty throttling mechanism operational? 24497c9341fSTejun Heo * @sc: scan_control in question 24597c9341fSTejun Heo * 24697c9341fSTejun Heo * The normal page dirty throttling mechanism in balance_dirty_pages() is 24797c9341fSTejun Heo * completely broken with the legacy memcg and direct stalling in 24897c9341fSTejun Heo * shrink_page_list() is used for throttling instead, which lacks all the 24997c9341fSTejun Heo * niceties such as fairness, adaptive pausing, bandwidth proportional 25097c9341fSTejun Heo * allocation and configurability. 25197c9341fSTejun Heo * 25297c9341fSTejun Heo * This function tests whether the vmscan currently in progress can assume 25397c9341fSTejun Heo * that the normal dirty throttling mechanism is operational. 25497c9341fSTejun Heo */ 25597c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 25697c9341fSTejun Heo { 25797c9341fSTejun Heo struct mem_cgroup *memcg = sc->target_mem_cgroup; 25897c9341fSTejun Heo 25997c9341fSTejun Heo if (!memcg) 26097c9341fSTejun Heo return true; 26197c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 26269234aceSLinus Torvalds if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 26397c9341fSTejun Heo return true; 26497c9341fSTejun Heo #endif 26597c9341fSTejun Heo return false; 26697c9341fSTejun Heo } 267e3c1ac58SAndrey Ryabinin 268e3c1ac58SAndrey Ryabinin static void set_memcg_congestion(pg_data_t *pgdat, 269e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg, 270e3c1ac58SAndrey Ryabinin bool congested) 271e3c1ac58SAndrey Ryabinin { 272e3c1ac58SAndrey Ryabinin struct mem_cgroup_per_node *mn; 273e3c1ac58SAndrey Ryabinin 274e3c1ac58SAndrey Ryabinin if (!memcg) 275e3c1ac58SAndrey Ryabinin return; 276e3c1ac58SAndrey Ryabinin 277e3c1ac58SAndrey Ryabinin mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id); 278e3c1ac58SAndrey Ryabinin WRITE_ONCE(mn->congested, congested); 279e3c1ac58SAndrey Ryabinin } 280e3c1ac58SAndrey Ryabinin 281e3c1ac58SAndrey Ryabinin static bool memcg_congested(pg_data_t *pgdat, 282e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg) 283e3c1ac58SAndrey Ryabinin { 284e3c1ac58SAndrey Ryabinin struct mem_cgroup_per_node *mn; 285e3c1ac58SAndrey Ryabinin 286e3c1ac58SAndrey Ryabinin mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id); 287e3c1ac58SAndrey Ryabinin return READ_ONCE(mn->congested); 288e3c1ac58SAndrey Ryabinin 289e3c1ac58SAndrey Ryabinin } 29091a45470SKAMEZAWA Hiroyuki #else 29189b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 29289b5fae5SJohannes Weiner { 29389b5fae5SJohannes Weiner return true; 29489b5fae5SJohannes Weiner } 29597c9341fSTejun Heo 29697c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 29797c9341fSTejun Heo { 29897c9341fSTejun Heo return true; 29997c9341fSTejun Heo } 300e3c1ac58SAndrey Ryabinin 301e3c1ac58SAndrey Ryabinin static inline void set_memcg_congestion(struct pglist_data *pgdat, 302e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg, bool congested) 303e3c1ac58SAndrey Ryabinin { 304e3c1ac58SAndrey Ryabinin } 305e3c1ac58SAndrey Ryabinin 306e3c1ac58SAndrey Ryabinin static inline bool memcg_congested(struct pglist_data *pgdat, 307e3c1ac58SAndrey Ryabinin struct mem_cgroup *memcg) 308e3c1ac58SAndrey Ryabinin { 309e3c1ac58SAndrey Ryabinin return false; 310e3c1ac58SAndrey Ryabinin 311e3c1ac58SAndrey Ryabinin } 31291a45470SKAMEZAWA Hiroyuki #endif 31391a45470SKAMEZAWA Hiroyuki 3145a1c84b4SMel Gorman /* 3155a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 3165a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 3175a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 3185a1c84b4SMel Gorman */ 3195a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 3205a1c84b4SMel Gorman { 3215a1c84b4SMel Gorman unsigned long nr; 3225a1c84b4SMel Gorman 3235a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 3245a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 3255a1c84b4SMel Gorman if (get_nr_swap_pages() > 0) 3265a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 3275a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 3285a1c84b4SMel Gorman 3295a1c84b4SMel Gorman return nr; 3305a1c84b4SMel Gorman } 3315a1c84b4SMel Gorman 332fd538803SMichal Hocko /** 333fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 334fd538803SMichal Hocko * @lruvec: lru vector 335fd538803SMichal Hocko * @lru: lru to use 336fd538803SMichal Hocko * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 337fd538803SMichal Hocko */ 338fd538803SMichal Hocko unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx) 339c9f299d9SKOSAKI Motohiro { 340fd538803SMichal Hocko unsigned long lru_size; 341fd538803SMichal Hocko int zid; 342a3d8e054SKOSAKI Motohiro 343fd538803SMichal Hocko if (!mem_cgroup_disabled()) 344fd538803SMichal Hocko lru_size = mem_cgroup_get_lru_size(lruvec, lru); 345fd538803SMichal Hocko else 346fd538803SMichal Hocko lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru); 347fd538803SMichal Hocko 348fd538803SMichal Hocko for (zid = zone_idx + 1; zid < MAX_NR_ZONES; zid++) { 349fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 350fd538803SMichal Hocko unsigned long size; 351fd538803SMichal Hocko 352fd538803SMichal Hocko if (!managed_zone(zone)) 353fd538803SMichal Hocko continue; 354fd538803SMichal Hocko 355fd538803SMichal Hocko if (!mem_cgroup_disabled()) 356fd538803SMichal Hocko size = mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 357fd538803SMichal Hocko else 358fd538803SMichal Hocko size = zone_page_state(&lruvec_pgdat(lruvec)->node_zones[zid], 359fd538803SMichal Hocko NR_ZONE_LRU_BASE + lru); 360fd538803SMichal Hocko lru_size -= min(size, lru_size); 361c9f299d9SKOSAKI Motohiro } 362c9f299d9SKOSAKI Motohiro 363fd538803SMichal Hocko return lru_size; 364b4536f0cSMichal Hocko 365b4536f0cSMichal Hocko } 366b4536f0cSMichal Hocko 3671da177e4SLinus Torvalds /* 3681d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 3691da177e4SLinus Torvalds */ 3708e04944fSTetsuo Handa int prealloc_shrinker(struct shrinker *shrinker) 3711da177e4SLinus Torvalds { 3721d3d4437SGlauber Costa size_t size = sizeof(*shrinker->nr_deferred); 3731d3d4437SGlauber Costa 3741d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 3751d3d4437SGlauber Costa size *= nr_node_ids; 3761d3d4437SGlauber Costa 3771d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 3781d3d4437SGlauber Costa if (!shrinker->nr_deferred) 3791d3d4437SGlauber Costa return -ENOMEM; 380b4c2b231SKirill Tkhai 381b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) { 382b4c2b231SKirill Tkhai if (prealloc_memcg_shrinker(shrinker)) 383b4c2b231SKirill Tkhai goto free_deferred; 384b4c2b231SKirill Tkhai } 385b4c2b231SKirill Tkhai 3868e04944fSTetsuo Handa return 0; 387b4c2b231SKirill Tkhai 388b4c2b231SKirill Tkhai free_deferred: 389b4c2b231SKirill Tkhai kfree(shrinker->nr_deferred); 390b4c2b231SKirill Tkhai shrinker->nr_deferred = NULL; 391b4c2b231SKirill Tkhai return -ENOMEM; 3928e04944fSTetsuo Handa } 3931d3d4437SGlauber Costa 3948e04944fSTetsuo Handa void free_prealloced_shrinker(struct shrinker *shrinker) 3958e04944fSTetsuo Handa { 396b4c2b231SKirill Tkhai if (!shrinker->nr_deferred) 397b4c2b231SKirill Tkhai return; 398b4c2b231SKirill Tkhai 399b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) 400b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 401b4c2b231SKirill Tkhai 4028e04944fSTetsuo Handa kfree(shrinker->nr_deferred); 4038e04944fSTetsuo Handa shrinker->nr_deferred = NULL; 4048e04944fSTetsuo Handa } 4058e04944fSTetsuo Handa 4068e04944fSTetsuo Handa void register_shrinker_prepared(struct shrinker *shrinker) 4078e04944fSTetsuo Handa { 4081da177e4SLinus Torvalds down_write(&shrinker_rwsem); 4091da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 410*7e010df5SKirill Tkhai #ifdef CONFIG_MEMCG_KMEM 411*7e010df5SKirill Tkhai idr_replace(&shrinker_idr, shrinker, shrinker->id); 412*7e010df5SKirill Tkhai #endif 4131da177e4SLinus Torvalds up_write(&shrinker_rwsem); 4148e04944fSTetsuo Handa } 4158e04944fSTetsuo Handa 4168e04944fSTetsuo Handa int register_shrinker(struct shrinker *shrinker) 4178e04944fSTetsuo Handa { 4188e04944fSTetsuo Handa int err = prealloc_shrinker(shrinker); 4198e04944fSTetsuo Handa 4208e04944fSTetsuo Handa if (err) 4218e04944fSTetsuo Handa return err; 4228e04944fSTetsuo Handa register_shrinker_prepared(shrinker); 4231d3d4437SGlauber Costa return 0; 4241da177e4SLinus Torvalds } 4258e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds /* 4281da177e4SLinus Torvalds * Remove one 4291da177e4SLinus Torvalds */ 4308e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 4311da177e4SLinus Torvalds { 432bb422a73STetsuo Handa if (!shrinker->nr_deferred) 433bb422a73STetsuo Handa return; 434b4c2b231SKirill Tkhai if (shrinker->flags & SHRINKER_MEMCG_AWARE) 435b4c2b231SKirill Tkhai unregister_memcg_shrinker(shrinker); 4361da177e4SLinus Torvalds down_write(&shrinker_rwsem); 4371da177e4SLinus Torvalds list_del(&shrinker->list); 4381da177e4SLinus Torvalds up_write(&shrinker_rwsem); 439ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 440bb422a73STetsuo Handa shrinker->nr_deferred = NULL; 4411da177e4SLinus Torvalds } 4428e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds #define SHRINK_BATCH 128 4451d3d4437SGlauber Costa 446cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 4479092c71bSJosef Bacik struct shrinker *shrinker, int priority) 4481da177e4SLinus Torvalds { 44924f7c6b9SDave Chinner unsigned long freed = 0; 4501da177e4SLinus Torvalds unsigned long long delta; 451635697c6SKonstantin Khlebnikov long total_scan; 452d5bc5fd3SVladimir Davydov long freeable; 453acf92b48SDave Chinner long nr; 454acf92b48SDave Chinner long new_nr; 4551d3d4437SGlauber Costa int nid = shrinkctl->nid; 456e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 457e9299f50SDave Chinner : SHRINK_BATCH; 4585f33a080SShaohua Li long scanned = 0, next_deferred; 4591da177e4SLinus Torvalds 460ac7fb3adSKirill Tkhai if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 461ac7fb3adSKirill Tkhai nid = 0; 462ac7fb3adSKirill Tkhai 463d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 4649b996468SKirill Tkhai if (freeable == 0 || freeable == SHRINK_EMPTY) 4659b996468SKirill Tkhai return freeable; 466635697c6SKonstantin Khlebnikov 467acf92b48SDave Chinner /* 468acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 469acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 470acf92b48SDave Chinner * don't also do this scanning work. 471acf92b48SDave Chinner */ 4721d3d4437SGlauber Costa nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 473acf92b48SDave Chinner 474acf92b48SDave Chinner total_scan = nr; 4759092c71bSJosef Bacik delta = freeable >> priority; 4769092c71bSJosef Bacik delta *= 4; 4779092c71bSJosef Bacik do_div(delta, shrinker->seeks); 478acf92b48SDave Chinner total_scan += delta; 479acf92b48SDave Chinner if (total_scan < 0) { 4808612c663SPintu Kumar pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n", 481a0b02131SDave Chinner shrinker->scan_objects, total_scan); 482d5bc5fd3SVladimir Davydov total_scan = freeable; 4835f33a080SShaohua Li next_deferred = nr; 4845f33a080SShaohua Li } else 4855f33a080SShaohua Li next_deferred = total_scan; 486ea164d73SAndrea Arcangeli 487ea164d73SAndrea Arcangeli /* 4883567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 4893567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 4903567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 4913567b59aSDave Chinner * nr being built up so when a shrink that can do some work 4923567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 493d5bc5fd3SVladimir Davydov * freeable. This is bad for sustaining a working set in 4943567b59aSDave Chinner * memory. 4953567b59aSDave Chinner * 4963567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 4973567b59aSDave Chinner * a large delta change is calculated directly. 4983567b59aSDave Chinner */ 499d5bc5fd3SVladimir Davydov if (delta < freeable / 4) 500d5bc5fd3SVladimir Davydov total_scan = min(total_scan, freeable / 2); 5013567b59aSDave Chinner 5023567b59aSDave Chinner /* 503ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 504ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 505ea164d73SAndrea Arcangeli * freeable entries. 506ea164d73SAndrea Arcangeli */ 507d5bc5fd3SVladimir Davydov if (total_scan > freeable * 2) 508d5bc5fd3SVladimir Davydov total_scan = freeable * 2; 5091da177e4SLinus Torvalds 51024f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 5119092c71bSJosef Bacik freeable, delta, total_scan, priority); 51209576073SDave Chinner 5130b1fb40aSVladimir Davydov /* 5140b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 5150b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 5160b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 5170b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 5180b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 5190b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 5200b1fb40aSVladimir Davydov * batch_size. 5210b1fb40aSVladimir Davydov * 5220b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 5230b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 524d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 5250b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 5260b1fb40aSVladimir Davydov * possible. 5270b1fb40aSVladimir Davydov */ 5280b1fb40aSVladimir Davydov while (total_scan >= batch_size || 529d5bc5fd3SVladimir Davydov total_scan >= freeable) { 53024f7c6b9SDave Chinner unsigned long ret; 5310b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 5321da177e4SLinus Torvalds 5330b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 534d460acb5SChris Wilson shrinkctl->nr_scanned = nr_to_scan; 53524f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 53624f7c6b9SDave Chinner if (ret == SHRINK_STOP) 5371da177e4SLinus Torvalds break; 53824f7c6b9SDave Chinner freed += ret; 53924f7c6b9SDave Chinner 540d460acb5SChris Wilson count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); 541d460acb5SChris Wilson total_scan -= shrinkctl->nr_scanned; 542d460acb5SChris Wilson scanned += shrinkctl->nr_scanned; 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds cond_resched(); 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 5475f33a080SShaohua Li if (next_deferred >= scanned) 5485f33a080SShaohua Li next_deferred -= scanned; 5495f33a080SShaohua Li else 5505f33a080SShaohua Li next_deferred = 0; 551acf92b48SDave Chinner /* 552acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 553acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 554acf92b48SDave Chinner * scan, there is no need to do an update. 555acf92b48SDave Chinner */ 5565f33a080SShaohua Li if (next_deferred > 0) 5575f33a080SShaohua Li new_nr = atomic_long_add_return(next_deferred, 5581d3d4437SGlauber Costa &shrinker->nr_deferred[nid]); 55983aeeadaSKonstantin Khlebnikov else 5601d3d4437SGlauber Costa new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 561acf92b48SDave Chinner 562df9024a8SDave Hansen trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan); 5631d3d4437SGlauber Costa return freed; 5641d3d4437SGlauber Costa } 5651d3d4437SGlauber Costa 566b0dedc49SKirill Tkhai #ifdef CONFIG_MEMCG_KMEM 567b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 568b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 569b0dedc49SKirill Tkhai { 570b0dedc49SKirill Tkhai struct memcg_shrinker_map *map; 571b0dedc49SKirill Tkhai unsigned long freed = 0; 572b0dedc49SKirill Tkhai int ret, i; 573b0dedc49SKirill Tkhai 574b0dedc49SKirill Tkhai if (!memcg_kmem_enabled() || !mem_cgroup_online(memcg)) 575b0dedc49SKirill Tkhai return 0; 576b0dedc49SKirill Tkhai 577b0dedc49SKirill Tkhai if (!down_read_trylock(&shrinker_rwsem)) 578b0dedc49SKirill Tkhai return 0; 579b0dedc49SKirill Tkhai 580b0dedc49SKirill Tkhai map = rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_map, 581b0dedc49SKirill Tkhai true); 582b0dedc49SKirill Tkhai if (unlikely(!map)) 583b0dedc49SKirill Tkhai goto unlock; 584b0dedc49SKirill Tkhai 585b0dedc49SKirill Tkhai for_each_set_bit(i, map->map, shrinker_nr_max) { 586b0dedc49SKirill Tkhai struct shrink_control sc = { 587b0dedc49SKirill Tkhai .gfp_mask = gfp_mask, 588b0dedc49SKirill Tkhai .nid = nid, 589b0dedc49SKirill Tkhai .memcg = memcg, 590b0dedc49SKirill Tkhai }; 591b0dedc49SKirill Tkhai struct shrinker *shrinker; 592b0dedc49SKirill Tkhai 593b0dedc49SKirill Tkhai shrinker = idr_find(&shrinker_idr, i); 594*7e010df5SKirill Tkhai if (unlikely(!shrinker || shrinker == SHRINKER_REGISTERING)) { 595*7e010df5SKirill Tkhai if (!shrinker) 596b0dedc49SKirill Tkhai clear_bit(i, map->map); 597b0dedc49SKirill Tkhai continue; 598b0dedc49SKirill Tkhai } 599b0dedc49SKirill Tkhai 600b0dedc49SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 601f90280d6SKirill Tkhai if (ret == SHRINK_EMPTY) { 602f90280d6SKirill Tkhai clear_bit(i, map->map); 603f90280d6SKirill Tkhai /* 604f90280d6SKirill Tkhai * After the shrinker reported that it had no objects to 605f90280d6SKirill Tkhai * free, but before we cleared the corresponding bit in 606f90280d6SKirill Tkhai * the memcg shrinker map, a new object might have been 607f90280d6SKirill Tkhai * added. To make sure, we have the bit set in this 608f90280d6SKirill Tkhai * case, we invoke the shrinker one more time and reset 609f90280d6SKirill Tkhai * the bit if it reports that it is not empty anymore. 610f90280d6SKirill Tkhai * The memory barrier here pairs with the barrier in 611f90280d6SKirill Tkhai * memcg_set_shrinker_bit(): 612f90280d6SKirill Tkhai * 613f90280d6SKirill Tkhai * list_lru_add() shrink_slab_memcg() 614f90280d6SKirill Tkhai * list_add_tail() clear_bit() 615f90280d6SKirill Tkhai * <MB> <MB> 616f90280d6SKirill Tkhai * set_bit() do_shrink_slab() 617f90280d6SKirill Tkhai */ 618f90280d6SKirill Tkhai smp_mb__after_atomic(); 619f90280d6SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 6209b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 6219b996468SKirill Tkhai ret = 0; 622f90280d6SKirill Tkhai else 623f90280d6SKirill Tkhai memcg_set_shrinker_bit(memcg, nid, i); 624f90280d6SKirill Tkhai } 625b0dedc49SKirill Tkhai freed += ret; 626b0dedc49SKirill Tkhai 627b0dedc49SKirill Tkhai if (rwsem_is_contended(&shrinker_rwsem)) { 628b0dedc49SKirill Tkhai freed = freed ? : 1; 629b0dedc49SKirill Tkhai break; 630b0dedc49SKirill Tkhai } 631b0dedc49SKirill Tkhai } 632b0dedc49SKirill Tkhai unlock: 633b0dedc49SKirill Tkhai up_read(&shrinker_rwsem); 634b0dedc49SKirill Tkhai return freed; 635b0dedc49SKirill Tkhai } 636b0dedc49SKirill Tkhai #else /* CONFIG_MEMCG_KMEM */ 637b0dedc49SKirill Tkhai static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, 638b0dedc49SKirill Tkhai struct mem_cgroup *memcg, int priority) 639b0dedc49SKirill Tkhai { 640b0dedc49SKirill Tkhai return 0; 641b0dedc49SKirill Tkhai } 642b0dedc49SKirill Tkhai #endif /* CONFIG_MEMCG_KMEM */ 643b0dedc49SKirill Tkhai 6446b4f7799SJohannes Weiner /** 645cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 6466b4f7799SJohannes Weiner * @gfp_mask: allocation context 6476b4f7799SJohannes Weiner * @nid: node whose slab caches to target 648cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 6499092c71bSJosef Bacik * @priority: the reclaim priority 6501d3d4437SGlauber Costa * 6516b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 6521d3d4437SGlauber Costa * 6536b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 6546b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 6551d3d4437SGlauber Costa * 656aeed1d32SVladimir Davydov * @memcg specifies the memory cgroup to target. Unaware shrinkers 657aeed1d32SVladimir Davydov * are called only if it is the root cgroup. 658cb731d6cSVladimir Davydov * 6599092c71bSJosef Bacik * @priority is sc->priority, we take the number of objects and >> by priority 6609092c71bSJosef Bacik * in order to get the scan target. 6611d3d4437SGlauber Costa * 6626b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 6631d3d4437SGlauber Costa */ 664cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 665cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 6669092c71bSJosef Bacik int priority) 6671d3d4437SGlauber Costa { 6681d3d4437SGlauber Costa struct shrinker *shrinker; 6691d3d4437SGlauber Costa unsigned long freed = 0; 6709b996468SKirill Tkhai int ret; 6711d3d4437SGlauber Costa 672aeed1d32SVladimir Davydov if (!mem_cgroup_is_root(memcg)) 673b0dedc49SKirill Tkhai return shrink_slab_memcg(gfp_mask, nid, memcg, priority); 674cb731d6cSVladimir Davydov 675e830c63aSTetsuo Handa if (!down_read_trylock(&shrinker_rwsem)) 6761d3d4437SGlauber Costa goto out; 6771d3d4437SGlauber Costa 6781d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 6796b4f7799SJohannes Weiner struct shrink_control sc = { 6806b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 6816b4f7799SJohannes Weiner .nid = nid, 682cb731d6cSVladimir Davydov .memcg = memcg, 6836b4f7799SJohannes Weiner }; 6846b4f7799SJohannes Weiner 6859b996468SKirill Tkhai ret = do_shrink_slab(&sc, shrinker, priority); 6869b996468SKirill Tkhai if (ret == SHRINK_EMPTY) 6879b996468SKirill Tkhai ret = 0; 6889b996468SKirill Tkhai freed += ret; 689e496612cSMinchan Kim /* 690e496612cSMinchan Kim * Bail out if someone want to register a new shrinker to 691e496612cSMinchan Kim * prevent the regsitration from being stalled for long periods 692e496612cSMinchan Kim * by parallel ongoing shrinking. 693e496612cSMinchan Kim */ 694e496612cSMinchan Kim if (rwsem_is_contended(&shrinker_rwsem)) { 695e496612cSMinchan Kim freed = freed ? : 1; 696e496612cSMinchan Kim break; 697e496612cSMinchan Kim } 698ec97097bSVladimir Davydov } 6991d3d4437SGlauber Costa 7001da177e4SLinus Torvalds up_read(&shrinker_rwsem); 701f06590bdSMinchan Kim out: 702f06590bdSMinchan Kim cond_resched(); 70324f7c6b9SDave Chinner return freed; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 706cb731d6cSVladimir Davydov void drop_slab_node(int nid) 707cb731d6cSVladimir Davydov { 708cb731d6cSVladimir Davydov unsigned long freed; 709cb731d6cSVladimir Davydov 710cb731d6cSVladimir Davydov do { 711cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 712cb731d6cSVladimir Davydov 713cb731d6cSVladimir Davydov freed = 0; 714aeed1d32SVladimir Davydov memcg = mem_cgroup_iter(NULL, NULL, NULL); 715cb731d6cSVladimir Davydov do { 7169092c71bSJosef Bacik freed += shrink_slab(GFP_KERNEL, nid, memcg, 0); 717cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 718cb731d6cSVladimir Davydov } while (freed > 10); 719cb731d6cSVladimir Davydov } 720cb731d6cSVladimir Davydov 721cb731d6cSVladimir Davydov void drop_slab(void) 722cb731d6cSVladimir Davydov { 723cb731d6cSVladimir Davydov int nid; 724cb731d6cSVladimir Davydov 725cb731d6cSVladimir Davydov for_each_online_node(nid) 726cb731d6cSVladimir Davydov drop_slab_node(nid); 727cb731d6cSVladimir Davydov } 728cb731d6cSVladimir Davydov 7291da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 7301da177e4SLinus Torvalds { 731ceddc3a5SJohannes Weiner /* 732ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 733ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 734ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 735ceddc3a5SJohannes Weiner */ 736bd4c82c2SHuang Ying int radix_pins = PageTransHuge(page) && PageSwapCache(page) ? 737bd4c82c2SHuang Ying HPAGE_PMD_NR : 1; 738bd4c82c2SHuang Ying return page_count(page) - page_has_private(page) == 1 + radix_pins; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 741703c2708STejun Heo static int may_write_to_inode(struct inode *inode, struct scan_control *sc) 7421da177e4SLinus Torvalds { 743930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 7441da177e4SLinus Torvalds return 1; 745703c2708STejun Heo if (!inode_write_congested(inode)) 7461da177e4SLinus Torvalds return 1; 747703c2708STejun Heo if (inode_to_bdi(inode) == current->backing_dev_info) 7481da177e4SLinus Torvalds return 1; 7491da177e4SLinus Torvalds return 0; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds /* 7531da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 7541da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 7551da177e4SLinus Torvalds * fsync(), msync() or close(). 7561da177e4SLinus Torvalds * 7571da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 7581da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 7591da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 7601da177e4SLinus Torvalds * 7611da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 7621da177e4SLinus Torvalds * __GFP_FS. 7631da177e4SLinus Torvalds */ 7641da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 7651da177e4SLinus Torvalds struct page *page, int error) 7661da177e4SLinus Torvalds { 7677eaceaccSJens Axboe lock_page(page); 7683e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 7693e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 7701da177e4SLinus Torvalds unlock_page(page); 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 77304e62a29SChristoph Lameter /* possible outcome of pageout() */ 77404e62a29SChristoph Lameter typedef enum { 77504e62a29SChristoph Lameter /* failed to write page out, page is locked */ 77604e62a29SChristoph Lameter PAGE_KEEP, 77704e62a29SChristoph Lameter /* move page to the active list, page is locked */ 77804e62a29SChristoph Lameter PAGE_ACTIVATE, 77904e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 78004e62a29SChristoph Lameter PAGE_SUCCESS, 78104e62a29SChristoph Lameter /* page is clean and locked */ 78204e62a29SChristoph Lameter PAGE_CLEAN, 78304e62a29SChristoph Lameter } pageout_t; 78404e62a29SChristoph Lameter 7851da177e4SLinus Torvalds /* 7861742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 7871742f19fSAndrew Morton * Calls ->writepage(). 7881da177e4SLinus Torvalds */ 789c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 7907d3579e8SKOSAKI Motohiro struct scan_control *sc) 7911da177e4SLinus Torvalds { 7921da177e4SLinus Torvalds /* 7931da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 7941da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 7951da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 7961da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 7971da177e4SLinus Torvalds * PagePrivate for that. 7981da177e4SLinus Torvalds * 7998174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 8001da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 8011da177e4SLinus Torvalds * will block. 8021da177e4SLinus Torvalds * 8031da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 8041da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 8051da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 8061da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 8071da177e4SLinus Torvalds */ 8081da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 8091da177e4SLinus Torvalds return PAGE_KEEP; 8101da177e4SLinus Torvalds if (!mapping) { 8111da177e4SLinus Torvalds /* 8121da177e4SLinus Torvalds * Some data journaling orphaned pages can have 8131da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 8141da177e4SLinus Torvalds */ 815266cf658SDavid Howells if (page_has_private(page)) { 8161da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 8171da177e4SLinus Torvalds ClearPageDirty(page); 818b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 8191da177e4SLinus Torvalds return PAGE_CLEAN; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds return PAGE_KEEP; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 8251da177e4SLinus Torvalds return PAGE_ACTIVATE; 826703c2708STejun Heo if (!may_write_to_inode(mapping->host, sc)) 8271da177e4SLinus Torvalds return PAGE_KEEP; 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 8301da177e4SLinus Torvalds int res; 8311da177e4SLinus Torvalds struct writeback_control wbc = { 8321da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 8331da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 834111ebb6eSOGAWA Hirofumi .range_start = 0, 835111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 8361da177e4SLinus Torvalds .for_reclaim = 1, 8371da177e4SLinus Torvalds }; 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds SetPageReclaim(page); 8401da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 8411da177e4SLinus Torvalds if (res < 0) 8421da177e4SLinus Torvalds handle_write_error(mapping, page, res); 843994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 8441da177e4SLinus Torvalds ClearPageReclaim(page); 8451da177e4SLinus Torvalds return PAGE_ACTIVATE; 8461da177e4SLinus Torvalds } 847c661b078SAndy Whitcroft 8481da177e4SLinus Torvalds if (!PageWriteback(page)) { 8491da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 8501da177e4SLinus Torvalds ClearPageReclaim(page); 8511da177e4SLinus Torvalds } 8523aa23851Syalin wang trace_mm_vmscan_writepage(page); 853c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_WRITE); 8541da177e4SLinus Torvalds return PAGE_SUCCESS; 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds return PAGE_CLEAN; 8581da177e4SLinus Torvalds } 8591da177e4SLinus Torvalds 860a649fd92SAndrew Morton /* 861e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 862e286781dSNick Piggin * gets returned with a refcount of 0. 863a649fd92SAndrew Morton */ 864a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 865a528910eSJohannes Weiner bool reclaimed) 86649d2e9ccSChristoph Lameter { 867c4843a75SGreg Thelen unsigned long flags; 868bd4c82c2SHuang Ying int refcount; 869c4843a75SGreg Thelen 87028e4d965SNick Piggin BUG_ON(!PageLocked(page)); 87128e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 87249d2e9ccSChristoph Lameter 873b93b0163SMatthew Wilcox xa_lock_irqsave(&mapping->i_pages, flags); 87449d2e9ccSChristoph Lameter /* 8750fd0e6b0SNick Piggin * The non racy check for a busy page. 8760fd0e6b0SNick Piggin * 8770fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 8780fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 8790fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 8800fd0e6b0SNick Piggin * here, then the following race may occur: 8810fd0e6b0SNick Piggin * 8820fd0e6b0SNick Piggin * get_user_pages(&page); 8830fd0e6b0SNick Piggin * [user mapping goes away] 8840fd0e6b0SNick Piggin * write_to(page); 8850fd0e6b0SNick Piggin * !PageDirty(page) [good] 8860fd0e6b0SNick Piggin * SetPageDirty(page); 8870fd0e6b0SNick Piggin * put_page(page); 8880fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 8890fd0e6b0SNick Piggin * 8900fd0e6b0SNick Piggin * [oops, our write_to data is lost] 8910fd0e6b0SNick Piggin * 8920fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 8930fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 8940139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 8950fd0e6b0SNick Piggin * 8960fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 897b93b0163SMatthew Wilcox * and thus under the i_pages lock, then this ordering is not required. 89849d2e9ccSChristoph Lameter */ 899bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page)) && PageSwapCache(page)) 900bd4c82c2SHuang Ying refcount = 1 + HPAGE_PMD_NR; 901bd4c82c2SHuang Ying else 902bd4c82c2SHuang Ying refcount = 2; 903bd4c82c2SHuang Ying if (!page_ref_freeze(page, refcount)) 90449d2e9ccSChristoph Lameter goto cannot_free; 905e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 906e286781dSNick Piggin if (unlikely(PageDirty(page))) { 907bd4c82c2SHuang Ying page_ref_unfreeze(page, refcount); 90849d2e9ccSChristoph Lameter goto cannot_free; 909e286781dSNick Piggin } 91049d2e9ccSChristoph Lameter 91149d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 91249d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 9130a31bc97SJohannes Weiner mem_cgroup_swapout(page, swap); 91449d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 915b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 91675f6d6d2SMinchan Kim put_swap_page(page, swap); 917e286781dSNick Piggin } else { 9186072d13cSLinus Torvalds void (*freepage)(struct page *); 919a528910eSJohannes Weiner void *shadow = NULL; 9206072d13cSLinus Torvalds 9216072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 922a528910eSJohannes Weiner /* 923a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 924a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 925a528910eSJohannes Weiner * 926a528910eSJohannes Weiner * But don't store shadows in an address space that is 927a528910eSJohannes Weiner * already exiting. This is not just an optizimation, 928a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 929a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 930a528910eSJohannes Weiner * back. 931f9fe48beSRoss Zwisler * 932f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 933f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 934f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 935f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 936b93b0163SMatthew Wilcox * same address_space. 937a528910eSJohannes Weiner */ 938a528910eSJohannes Weiner if (reclaimed && page_is_file_cache(page) && 939f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 940a528910eSJohannes Weiner shadow = workingset_eviction(mapping, page); 94162cccb8cSJohannes Weiner __delete_from_page_cache(page, shadow); 942b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 9436072d13cSLinus Torvalds 9446072d13cSLinus Torvalds if (freepage != NULL) 9456072d13cSLinus Torvalds freepage(page); 946e286781dSNick Piggin } 947e286781dSNick Piggin 94849d2e9ccSChristoph Lameter return 1; 94949d2e9ccSChristoph Lameter 95049d2e9ccSChristoph Lameter cannot_free: 951b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 95249d2e9ccSChristoph Lameter return 0; 95349d2e9ccSChristoph Lameter } 95449d2e9ccSChristoph Lameter 9551da177e4SLinus Torvalds /* 956e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 957e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 958e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 959e286781dSNick Piggin * this page. 960e286781dSNick Piggin */ 961e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 962e286781dSNick Piggin { 963a528910eSJohannes Weiner if (__remove_mapping(mapping, page, false)) { 964e286781dSNick Piggin /* 965e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 966e286781dSNick Piggin * drops the pagecache ref for us without requiring another 967e286781dSNick Piggin * atomic operation. 968e286781dSNick Piggin */ 969fe896d18SJoonsoo Kim page_ref_unfreeze(page, 1); 970e286781dSNick Piggin return 1; 971e286781dSNick Piggin } 972e286781dSNick Piggin return 0; 973e286781dSNick Piggin } 974e286781dSNick Piggin 975894bc310SLee Schermerhorn /** 976894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 977894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 978894bc310SLee Schermerhorn * 979894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 980894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 981894bc310SLee Schermerhorn * 982894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 983894bc310SLee Schermerhorn */ 984894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 985894bc310SLee Schermerhorn { 986c53954a0SMel Gorman lru_cache_add(page); 987894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 988894bc310SLee Schermerhorn } 989894bc310SLee Schermerhorn 990dfc8d636SJohannes Weiner enum page_references { 991dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 992dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 99364574746SJohannes Weiner PAGEREF_KEEP, 994dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 995dfc8d636SJohannes Weiner }; 996dfc8d636SJohannes Weiner 997dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 998dfc8d636SJohannes Weiner struct scan_control *sc) 999dfc8d636SJohannes Weiner { 100064574746SJohannes Weiner int referenced_ptes, referenced_page; 1001dfc8d636SJohannes Weiner unsigned long vm_flags; 1002dfc8d636SJohannes Weiner 1003c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 1004c3ac9a8aSJohannes Weiner &vm_flags); 100564574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 1006dfc8d636SJohannes Weiner 1007dfc8d636SJohannes Weiner /* 1008dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 1009dfc8d636SJohannes Weiner * move the page to the unevictable list. 1010dfc8d636SJohannes Weiner */ 1011dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 1012dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 1013dfc8d636SJohannes Weiner 101464574746SJohannes Weiner if (referenced_ptes) { 1015e4898273SMichal Hocko if (PageSwapBacked(page)) 101664574746SJohannes Weiner return PAGEREF_ACTIVATE; 101764574746SJohannes Weiner /* 101864574746SJohannes Weiner * All mapped pages start out with page table 101964574746SJohannes Weiner * references from the instantiating fault, so we need 102064574746SJohannes Weiner * to look twice if a mapped file page is used more 102164574746SJohannes Weiner * than once. 102264574746SJohannes Weiner * 102364574746SJohannes Weiner * Mark it and spare it for another trip around the 102464574746SJohannes Weiner * inactive list. Another page table reference will 102564574746SJohannes Weiner * lead to its activation. 102664574746SJohannes Weiner * 102764574746SJohannes Weiner * Note: the mark is set for activated pages as well 102864574746SJohannes Weiner * so that recently deactivated but used pages are 102964574746SJohannes Weiner * quickly recovered. 103064574746SJohannes Weiner */ 103164574746SJohannes Weiner SetPageReferenced(page); 103264574746SJohannes Weiner 103334dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 1034dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 1035dfc8d636SJohannes Weiner 1036c909e993SKonstantin Khlebnikov /* 1037c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 1038c909e993SKonstantin Khlebnikov */ 1039c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 1040c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 1041c909e993SKonstantin Khlebnikov 104264574746SJohannes Weiner return PAGEREF_KEEP; 104364574746SJohannes Weiner } 104464574746SJohannes Weiner 1045dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 10462e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 1047dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 104864574746SJohannes Weiner 104964574746SJohannes Weiner return PAGEREF_RECLAIM; 1050dfc8d636SJohannes Weiner } 1051dfc8d636SJohannes Weiner 1052e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 1053e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 1054e2be15f6SMel Gorman bool *dirty, bool *writeback) 1055e2be15f6SMel Gorman { 1056b4597226SMel Gorman struct address_space *mapping; 1057b4597226SMel Gorman 1058e2be15f6SMel Gorman /* 1059e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 1060e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 1061e2be15f6SMel Gorman */ 1062802a3a92SShaohua Li if (!page_is_file_cache(page) || 1063802a3a92SShaohua Li (PageAnon(page) && !PageSwapBacked(page))) { 1064e2be15f6SMel Gorman *dirty = false; 1065e2be15f6SMel Gorman *writeback = false; 1066e2be15f6SMel Gorman return; 1067e2be15f6SMel Gorman } 1068e2be15f6SMel Gorman 1069e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 1070e2be15f6SMel Gorman *dirty = PageDirty(page); 1071e2be15f6SMel Gorman *writeback = PageWriteback(page); 1072b4597226SMel Gorman 1073b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 1074b4597226SMel Gorman if (!page_has_private(page)) 1075b4597226SMel Gorman return; 1076b4597226SMel Gorman 1077b4597226SMel Gorman mapping = page_mapping(page); 1078b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 1079b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 1080e2be15f6SMel Gorman } 1081e2be15f6SMel Gorman 1082e286781dSNick Piggin /* 10831742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 10841da177e4SLinus Torvalds */ 10851742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 1086599d0c95SMel Gorman struct pglist_data *pgdat, 1087f84f6e2bSMel Gorman struct scan_control *sc, 108802c6de8dSMinchan Kim enum ttu_flags ttu_flags, 10893c710c1aSMichal Hocko struct reclaim_stat *stat, 109002c6de8dSMinchan Kim bool force_reclaim) 10911da177e4SLinus Torvalds { 10921da177e4SLinus Torvalds LIST_HEAD(ret_pages); 1093abe4c3b5SMel Gorman LIST_HEAD(free_pages); 10941da177e4SLinus Torvalds int pgactivate = 0; 10953c710c1aSMichal Hocko unsigned nr_unqueued_dirty = 0; 10963c710c1aSMichal Hocko unsigned nr_dirty = 0; 10973c710c1aSMichal Hocko unsigned nr_congested = 0; 10983c710c1aSMichal Hocko unsigned nr_reclaimed = 0; 10993c710c1aSMichal Hocko unsigned nr_writeback = 0; 11003c710c1aSMichal Hocko unsigned nr_immediate = 0; 11015bccd166SMichal Hocko unsigned nr_ref_keep = 0; 11025bccd166SMichal Hocko unsigned nr_unmap_fail = 0; 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds cond_resched(); 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds while (!list_empty(page_list)) { 11071da177e4SLinus Torvalds struct address_space *mapping; 11081da177e4SLinus Torvalds struct page *page; 11091da177e4SLinus Torvalds int may_enter_fs; 111002c6de8dSMinchan Kim enum page_references references = PAGEREF_RECLAIM_CLEAN; 1111e2be15f6SMel Gorman bool dirty, writeback; 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds cond_resched(); 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds page = lru_to_page(page_list); 11161da177e4SLinus Torvalds list_del(&page->lru); 11171da177e4SLinus Torvalds 1118529ae9aaSNick Piggin if (!trylock_page(page)) 11191da177e4SLinus Torvalds goto keep; 11201da177e4SLinus Torvalds 1121309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds sc->nr_scanned++; 112480e43426SChristoph Lameter 112539b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 1126ad6b6704SMinchan Kim goto activate_locked; 1127894bc310SLee Schermerhorn 1128a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 112980e43426SChristoph Lameter goto keep_locked; 113080e43426SChristoph Lameter 11311da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 1132802a3a92SShaohua Li if ((page_mapped(page) || PageSwapCache(page)) && 1133802a3a92SShaohua Li !(PageAnon(page) && !PageSwapBacked(page))) 11341da177e4SLinus Torvalds sc->nr_scanned++; 11351da177e4SLinus Torvalds 1136c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 1137c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 1138c661b078SAndy Whitcroft 1139e62e384eSMichal Hocko /* 1140894befecSAndrey Ryabinin * The number of dirty pages determines if a node is marked 1141e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 1142e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 1143e2be15f6SMel Gorman * is all dirty unqueued pages. 1144e2be15f6SMel Gorman */ 1145e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 1146e2be15f6SMel Gorman if (dirty || writeback) 1147e2be15f6SMel Gorman nr_dirty++; 1148e2be15f6SMel Gorman 1149e2be15f6SMel Gorman if (dirty && !writeback) 1150e2be15f6SMel Gorman nr_unqueued_dirty++; 1151e2be15f6SMel Gorman 1152d04e8acdSMel Gorman /* 1153d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 1154d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 1155d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 1156d04e8acdSMel Gorman * end of the LRU a second time. 1157d04e8acdSMel Gorman */ 1158e2be15f6SMel Gorman mapping = page_mapping(page); 11591da58ee2SJamie Liu if (((dirty || writeback) && mapping && 1160703c2708STejun Heo inode_write_congested(mapping->host)) || 1161d04e8acdSMel Gorman (writeback && PageReclaim(page))) 1162e2be15f6SMel Gorman nr_congested++; 1163e2be15f6SMel Gorman 1164e2be15f6SMel Gorman /* 1165283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 1166283aba9fSMel Gorman * are three cases to consider. 1167e62e384eSMichal Hocko * 1168283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 1169283aba9fSMel Gorman * under writeback and this page is both under writeback and 1170283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 1171283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 1172283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 1173283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 1174283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 1175b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 1176b1a6f21eSMel Gorman * caller can stall after page list has been processed. 1177c3b94f44SHugh Dickins * 117897c9341fSTejun Heo * 2) Global or new memcg reclaim encounters a page that is 1179ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 1180ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 1181ecf5fc6eSMichal Hocko * not to fs). In this case mark the page for immediate 118297c9341fSTejun Heo * reclaim and continue scanning. 1183283aba9fSMel Gorman * 1184ecf5fc6eSMichal Hocko * Require may_enter_fs because we would wait on fs, which 1185ecf5fc6eSMichal Hocko * may not have submitted IO yet. And the loop driver might 1186283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 1187283aba9fSMel Gorman * which it is needed to do the write (loop masks off 1188283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 1189283aba9fSMel Gorman * would probably show more reasons. 1190283aba9fSMel Gorman * 11917fadc820SHugh Dickins * 3) Legacy memcg encounters a page that is already marked 1192283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 1193283aba9fSMel Gorman * throttling so we could easily OOM just because too many 1194283aba9fSMel Gorman * pages are in writeback and there is nothing else to 1195283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 1196c55e8d03SJohannes Weiner * 1197c55e8d03SJohannes Weiner * In cases 1) and 2) we activate the pages to get them out of 1198c55e8d03SJohannes Weiner * the way while we continue scanning for clean pages on the 1199c55e8d03SJohannes Weiner * inactive list and refilling from the active list. The 1200c55e8d03SJohannes Weiner * observation here is that waiting for disk writes is more 1201c55e8d03SJohannes Weiner * expensive than potentially causing reloads down the line. 1202c55e8d03SJohannes Weiner * Since they're marked for immediate reclaim, they won't put 1203c55e8d03SJohannes Weiner * memory pressure on the cache working set any longer than it 1204c55e8d03SJohannes Weiner * takes to write them to disk. 1205e62e384eSMichal Hocko */ 1206283aba9fSMel Gorman if (PageWriteback(page)) { 1207283aba9fSMel Gorman /* Case 1 above */ 1208283aba9fSMel Gorman if (current_is_kswapd() && 1209283aba9fSMel Gorman PageReclaim(page) && 1210599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1211b1a6f21eSMel Gorman nr_immediate++; 1212c55e8d03SJohannes Weiner goto activate_locked; 1213283aba9fSMel Gorman 1214283aba9fSMel Gorman /* Case 2 above */ 121597c9341fSTejun Heo } else if (sane_reclaim(sc) || 1216ecf5fc6eSMichal Hocko !PageReclaim(page) || !may_enter_fs) { 1217c3b94f44SHugh Dickins /* 1218c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 1219c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 1220c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 1221c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 1222c3b94f44SHugh Dickins * enough to care. What we do want is for this 1223c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 1224c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 1225c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 1226c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 1227c3b94f44SHugh Dickins */ 1228c3b94f44SHugh Dickins SetPageReclaim(page); 122992df3a72SMel Gorman nr_writeback++; 1230c55e8d03SJohannes Weiner goto activate_locked; 1231283aba9fSMel Gorman 1232283aba9fSMel Gorman /* Case 3 above */ 1233283aba9fSMel Gorman } else { 12347fadc820SHugh Dickins unlock_page(page); 1235c3b94f44SHugh Dickins wait_on_page_writeback(page); 12367fadc820SHugh Dickins /* then go back and try same page again */ 12377fadc820SHugh Dickins list_add_tail(&page->lru, page_list); 12387fadc820SHugh Dickins continue; 1239e62e384eSMichal Hocko } 1240283aba9fSMel Gorman } 12411da177e4SLinus Torvalds 124202c6de8dSMinchan Kim if (!force_reclaim) 12436a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 124402c6de8dSMinchan Kim 1245dfc8d636SJohannes Weiner switch (references) { 1246dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 12471da177e4SLinus Torvalds goto activate_locked; 124864574746SJohannes Weiner case PAGEREF_KEEP: 12495bccd166SMichal Hocko nr_ref_keep++; 125064574746SJohannes Weiner goto keep_locked; 1251dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1252dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1253dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 1254dfc8d636SJohannes Weiner } 12551da177e4SLinus Torvalds 12561da177e4SLinus Torvalds /* 12571da177e4SLinus Torvalds * Anonymous process memory has backing store? 12581da177e4SLinus Torvalds * Try to allocate it some swap space here. 1259802a3a92SShaohua Li * Lazyfree page could be freed directly 12601da177e4SLinus Torvalds */ 1261bd4c82c2SHuang Ying if (PageAnon(page) && PageSwapBacked(page)) { 1262bd4c82c2SHuang Ying if (!PageSwapCache(page)) { 126363eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 126463eb6b93SHugh Dickins goto keep_locked; 1265747552b1SHuang Ying if (PageTransHuge(page)) { 1266b8f593cdSHuang Ying /* cannot split THP, skip it */ 1267747552b1SHuang Ying if (!can_split_huge_page(page, NULL)) 1268b8f593cdSHuang Ying goto activate_locked; 1269747552b1SHuang Ying /* 1270747552b1SHuang Ying * Split pages without a PMD map right 1271747552b1SHuang Ying * away. Chances are some or all of the 1272747552b1SHuang Ying * tail pages can be freed without IO. 1273747552b1SHuang Ying */ 1274747552b1SHuang Ying if (!compound_mapcount(page) && 1275bd4c82c2SHuang Ying split_huge_page_to_list(page, 1276bd4c82c2SHuang Ying page_list)) 1277747552b1SHuang Ying goto activate_locked; 1278747552b1SHuang Ying } 12790f074658SMinchan Kim if (!add_to_swap(page)) { 12800f074658SMinchan Kim if (!PageTransHuge(page)) 12811da177e4SLinus Torvalds goto activate_locked; 1282bd4c82c2SHuang Ying /* Fallback to swap normal pages */ 1283bd4c82c2SHuang Ying if (split_huge_page_to_list(page, 1284bd4c82c2SHuang Ying page_list)) 12850f074658SMinchan Kim goto activate_locked; 1286fe490cc0SHuang Ying #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1287fe490cc0SHuang Ying count_vm_event(THP_SWPOUT_FALLBACK); 1288fe490cc0SHuang Ying #endif 12890f074658SMinchan Kim if (!add_to_swap(page)) 12900f074658SMinchan Kim goto activate_locked; 12910f074658SMinchan Kim } 12920f074658SMinchan Kim 129363eb6b93SHugh Dickins may_enter_fs = 1; 12941da177e4SLinus Torvalds 1295e2be15f6SMel Gorman /* Adding to swap updated mapping */ 12961da177e4SLinus Torvalds mapping = page_mapping(page); 1297bd4c82c2SHuang Ying } 12987751b2daSKirill A. Shutemov } else if (unlikely(PageTransHuge(page))) { 12997751b2daSKirill A. Shutemov /* Split file THP */ 13007751b2daSKirill A. Shutemov if (split_huge_page_to_list(page, page_list)) 13017751b2daSKirill A. Shutemov goto keep_locked; 1302e2be15f6SMel Gorman } 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds /* 13051da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 13061da177e4SLinus Torvalds * processes. Try to unmap it here. 13071da177e4SLinus Torvalds */ 1308802a3a92SShaohua Li if (page_mapped(page)) { 1309bd4c82c2SHuang Ying enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH; 1310bd4c82c2SHuang Ying 1311bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) 1312bd4c82c2SHuang Ying flags |= TTU_SPLIT_HUGE_PMD; 1313bd4c82c2SHuang Ying if (!try_to_unmap(page, flags)) { 13145bccd166SMichal Hocko nr_unmap_fail++; 13151da177e4SLinus Torvalds goto activate_locked; 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds if (PageDirty(page)) { 1320ee72886dSMel Gorman /* 13214eda4823SJohannes Weiner * Only kswapd can writeback filesystem pages 13224eda4823SJohannes Weiner * to avoid risk of stack overflow. But avoid 13234eda4823SJohannes Weiner * injecting inefficient single-page IO into 13244eda4823SJohannes Weiner * flusher writeback as much as possible: only 13254eda4823SJohannes Weiner * write pages when we've encountered many 13264eda4823SJohannes Weiner * dirty pages, and when we've already scanned 13274eda4823SJohannes Weiner * the rest of the LRU for clean pages and see 13284eda4823SJohannes Weiner * the same dirty pages again (PageReclaim). 1329ee72886dSMel Gorman */ 1330f84f6e2bSMel Gorman if (page_is_file_cache(page) && 13314eda4823SJohannes Weiner (!current_is_kswapd() || !PageReclaim(page) || 1332599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 133349ea7eb6SMel Gorman /* 133449ea7eb6SMel Gorman * Immediately reclaim when written back. 133549ea7eb6SMel Gorman * Similar in principal to deactivate_page() 133649ea7eb6SMel Gorman * except we already have the page isolated 133749ea7eb6SMel Gorman * and know it's dirty 133849ea7eb6SMel Gorman */ 1339c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 134049ea7eb6SMel Gorman SetPageReclaim(page); 134149ea7eb6SMel Gorman 1342c55e8d03SJohannes Weiner goto activate_locked; 1343ee72886dSMel Gorman } 1344ee72886dSMel Gorman 1345dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 13461da177e4SLinus Torvalds goto keep_locked; 13474dd4b920SAndrew Morton if (!may_enter_fs) 13481da177e4SLinus Torvalds goto keep_locked; 134952a8363eSChristoph Lameter if (!sc->may_writepage) 13501da177e4SLinus Torvalds goto keep_locked; 13511da177e4SLinus Torvalds 1352d950c947SMel Gorman /* 1353d950c947SMel Gorman * Page is dirty. Flush the TLB if a writable entry 1354d950c947SMel Gorman * potentially exists to avoid CPU writes after IO 1355d950c947SMel Gorman * starts and then write it out here. 1356d950c947SMel Gorman */ 1357d950c947SMel Gorman try_to_unmap_flush_dirty(); 13587d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 13591da177e4SLinus Torvalds case PAGE_KEEP: 13601da177e4SLinus Torvalds goto keep_locked; 13611da177e4SLinus Torvalds case PAGE_ACTIVATE: 13621da177e4SLinus Torvalds goto activate_locked; 13631da177e4SLinus Torvalds case PAGE_SUCCESS: 13647d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 136541ac1999SMel Gorman goto keep; 13667d3579e8SKOSAKI Motohiro if (PageDirty(page)) 13671da177e4SLinus Torvalds goto keep; 13687d3579e8SKOSAKI Motohiro 13691da177e4SLinus Torvalds /* 13701da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 13711da177e4SLinus Torvalds * ahead and try to reclaim the page. 13721da177e4SLinus Torvalds */ 1373529ae9aaSNick Piggin if (!trylock_page(page)) 13741da177e4SLinus Torvalds goto keep; 13751da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 13761da177e4SLinus Torvalds goto keep_locked; 13771da177e4SLinus Torvalds mapping = page_mapping(page); 13781da177e4SLinus Torvalds case PAGE_CLEAN: 13791da177e4SLinus Torvalds ; /* try to free the page below */ 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds /* 13841da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 13851da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 13861da177e4SLinus Torvalds * the page as well. 13871da177e4SLinus Torvalds * 13881da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 13891da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 13901da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 13911da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 13921da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 13931da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 13941da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 13951da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 13961da177e4SLinus Torvalds * 13971da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 13981da177e4SLinus Torvalds * the pages which were not successfully invalidated in 13991da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 14001da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 14011da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 14021da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 14031da177e4SLinus Torvalds */ 1404266cf658SDavid Howells if (page_has_private(page)) { 14051da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 14061da177e4SLinus Torvalds goto activate_locked; 1407e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1408e286781dSNick Piggin unlock_page(page); 1409e286781dSNick Piggin if (put_page_testzero(page)) 14101da177e4SLinus Torvalds goto free_it; 1411e286781dSNick Piggin else { 1412e286781dSNick Piggin /* 1413e286781dSNick Piggin * rare race with speculative reference. 1414e286781dSNick Piggin * the speculative reference will free 1415e286781dSNick Piggin * this page shortly, so we may 1416e286781dSNick Piggin * increment nr_reclaimed here (and 1417e286781dSNick Piggin * leave it off the LRU). 1418e286781dSNick Piggin */ 1419e286781dSNick Piggin nr_reclaimed++; 1420e286781dSNick Piggin continue; 1421e286781dSNick Piggin } 1422e286781dSNick Piggin } 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds 1425802a3a92SShaohua Li if (PageAnon(page) && !PageSwapBacked(page)) { 1426802a3a92SShaohua Li /* follow __remove_mapping for reference */ 1427802a3a92SShaohua Li if (!page_ref_freeze(page, 1)) 142849d2e9ccSChristoph Lameter goto keep_locked; 1429802a3a92SShaohua Li if (PageDirty(page)) { 1430802a3a92SShaohua Li page_ref_unfreeze(page, 1); 1431802a3a92SShaohua Li goto keep_locked; 1432802a3a92SShaohua Li } 14331da177e4SLinus Torvalds 1434802a3a92SShaohua Li count_vm_event(PGLAZYFREED); 14352262185cSRoman Gushchin count_memcg_page_event(page, PGLAZYFREED); 1436802a3a92SShaohua Li } else if (!mapping || !__remove_mapping(mapping, page, true)) 1437802a3a92SShaohua Li goto keep_locked; 1438a978d6f5SNick Piggin /* 1439a978d6f5SNick Piggin * At this point, we have no other references and there is 1440a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 1441a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 1442a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 1443a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 1444a978d6f5SNick Piggin */ 144548c935adSKirill A. Shutemov __ClearPageLocked(page); 1446e286781dSNick Piggin free_it: 144705ff5137SAndrew Morton nr_reclaimed++; 1448abe4c3b5SMel Gorman 1449abe4c3b5SMel Gorman /* 1450abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1451abe4c3b5SMel Gorman * appear not as the counts should be low 1452abe4c3b5SMel Gorman */ 1453bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) { 1454bd4c82c2SHuang Ying mem_cgroup_uncharge(page); 1455bd4c82c2SHuang Ying (*get_compound_page_dtor(page))(page); 1456bd4c82c2SHuang Ying } else 1457abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 14581da177e4SLinus Torvalds continue; 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds activate_locked: 146168a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 1462ad6b6704SMinchan Kim if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || 1463ad6b6704SMinchan Kim PageMlocked(page))) 1464a2c43eedSHugh Dickins try_to_free_swap(page); 1465309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 1466ad6b6704SMinchan Kim if (!PageMlocked(page)) { 14671da177e4SLinus Torvalds SetPageActive(page); 14681da177e4SLinus Torvalds pgactivate++; 14692262185cSRoman Gushchin count_memcg_page_event(page, PGACTIVATE); 1470ad6b6704SMinchan Kim } 14711da177e4SLinus Torvalds keep_locked: 14721da177e4SLinus Torvalds unlock_page(page); 14731da177e4SLinus Torvalds keep: 14741da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1475309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 14761da177e4SLinus Torvalds } 1477abe4c3b5SMel Gorman 1478747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 147972b252aeSMel Gorman try_to_unmap_flush(); 14802d4894b5SMel Gorman free_unref_page_list(&free_pages); 1481abe4c3b5SMel Gorman 14821da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1483f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 14840a31bc97SJohannes Weiner 14853c710c1aSMichal Hocko if (stat) { 14863c710c1aSMichal Hocko stat->nr_dirty = nr_dirty; 14873c710c1aSMichal Hocko stat->nr_congested = nr_congested; 14883c710c1aSMichal Hocko stat->nr_unqueued_dirty = nr_unqueued_dirty; 14893c710c1aSMichal Hocko stat->nr_writeback = nr_writeback; 14903c710c1aSMichal Hocko stat->nr_immediate = nr_immediate; 14915bccd166SMichal Hocko stat->nr_activate = pgactivate; 14925bccd166SMichal Hocko stat->nr_ref_keep = nr_ref_keep; 14935bccd166SMichal Hocko stat->nr_unmap_fail = nr_unmap_fail; 14943c710c1aSMichal Hocko } 149505ff5137SAndrew Morton return nr_reclaimed; 14961da177e4SLinus Torvalds } 14971da177e4SLinus Torvalds 149802c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone, 149902c6de8dSMinchan Kim struct list_head *page_list) 150002c6de8dSMinchan Kim { 150102c6de8dSMinchan Kim struct scan_control sc = { 150202c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 150302c6de8dSMinchan Kim .priority = DEF_PRIORITY, 150402c6de8dSMinchan Kim .may_unmap = 1, 150502c6de8dSMinchan Kim }; 15063c710c1aSMichal Hocko unsigned long ret; 150702c6de8dSMinchan Kim struct page *page, *next; 150802c6de8dSMinchan Kim LIST_HEAD(clean_pages); 150902c6de8dSMinchan Kim 151002c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1511117aad1eSRafael Aquini if (page_is_file_cache(page) && !PageDirty(page) && 1512b1123ea6SMinchan Kim !__PageMovable(page)) { 151302c6de8dSMinchan Kim ClearPageActive(page); 151402c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 151502c6de8dSMinchan Kim } 151602c6de8dSMinchan Kim } 151702c6de8dSMinchan Kim 1518599d0c95SMel Gorman ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 1519a128ca71SShaohua Li TTU_IGNORE_ACCESS, NULL, true); 152002c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 1521599d0c95SMel Gorman mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret); 152202c6de8dSMinchan Kim return ret; 152302c6de8dSMinchan Kim } 152402c6de8dSMinchan Kim 15255ad333ebSAndy Whitcroft /* 15265ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 15275ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 15285ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 15295ad333ebSAndy Whitcroft * 15305ad333ebSAndy Whitcroft * page: page to consider 15315ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 15325ad333ebSAndy Whitcroft * 15335ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 15345ad333ebSAndy Whitcroft */ 1535f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 15365ad333ebSAndy Whitcroft { 15375ad333ebSAndy Whitcroft int ret = -EINVAL; 15385ad333ebSAndy Whitcroft 15395ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 15405ad333ebSAndy Whitcroft if (!PageLRU(page)) 15415ad333ebSAndy Whitcroft return ret; 15425ad333ebSAndy Whitcroft 1543e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1544e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1545894bc310SLee Schermerhorn return ret; 1546894bc310SLee Schermerhorn 15475ad333ebSAndy Whitcroft ret = -EBUSY; 154808e552c6SKAMEZAWA Hiroyuki 1549c8244935SMel Gorman /* 1550c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1551c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1552c8244935SMel Gorman * blocking - clean pages for the most part. 1553c8244935SMel Gorman * 1554c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1555c8244935SMel Gorman * that it is possible to migrate without blocking 1556c8244935SMel Gorman */ 15571276ad68SJohannes Weiner if (mode & ISOLATE_ASYNC_MIGRATE) { 1558c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1559c8244935SMel Gorman if (PageWriteback(page)) 156039deaf85SMinchan Kim return ret; 156139deaf85SMinchan Kim 1562c8244935SMel Gorman if (PageDirty(page)) { 1563c8244935SMel Gorman struct address_space *mapping; 156469d763fcSMel Gorman bool migrate_dirty; 1565c8244935SMel Gorman 1566c8244935SMel Gorman /* 1567c8244935SMel Gorman * Only pages without mappings or that have a 1568c8244935SMel Gorman * ->migratepage callback are possible to migrate 156969d763fcSMel Gorman * without blocking. However, we can be racing with 157069d763fcSMel Gorman * truncation so it's necessary to lock the page 157169d763fcSMel Gorman * to stabilise the mapping as truncation holds 157269d763fcSMel Gorman * the page lock until after the page is removed 157369d763fcSMel Gorman * from the page cache. 1574c8244935SMel Gorman */ 157569d763fcSMel Gorman if (!trylock_page(page)) 157669d763fcSMel Gorman return ret; 157769d763fcSMel Gorman 1578c8244935SMel Gorman mapping = page_mapping(page); 1579145e1a71SHugh Dickins migrate_dirty = !mapping || mapping->a_ops->migratepage; 158069d763fcSMel Gorman unlock_page(page); 158169d763fcSMel Gorman if (!migrate_dirty) 1582c8244935SMel Gorman return ret; 1583c8244935SMel Gorman } 1584c8244935SMel Gorman } 1585c8244935SMel Gorman 1586f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1587f80c0673SMinchan Kim return ret; 1588f80c0673SMinchan Kim 15895ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 15905ad333ebSAndy Whitcroft /* 15915ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 15925ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 15935ad333ebSAndy Whitcroft * page release code relies on it. 15945ad333ebSAndy Whitcroft */ 15955ad333ebSAndy Whitcroft ClearPageLRU(page); 15965ad333ebSAndy Whitcroft ret = 0; 15975ad333ebSAndy Whitcroft } 15985ad333ebSAndy Whitcroft 15995ad333ebSAndy Whitcroft return ret; 16005ad333ebSAndy Whitcroft } 16015ad333ebSAndy Whitcroft 16027ee36a14SMel Gorman 16037ee36a14SMel Gorman /* 16047ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 16057ee36a14SMel Gorman * be complete before mem_cgroup_update_lru_size due to a santity check. 16067ee36a14SMel Gorman */ 16077ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 1608b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 16097ee36a14SMel Gorman { 16107ee36a14SMel Gorman int zid; 16117ee36a14SMel Gorman 16127ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 16137ee36a14SMel Gorman if (!nr_zone_taken[zid]) 16147ee36a14SMel Gorman continue; 16157ee36a14SMel Gorman 16167ee36a14SMel Gorman __update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1617b4536f0cSMichal Hocko #ifdef CONFIG_MEMCG 1618b4536f0cSMichal Hocko mem_cgroup_update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1619b4536f0cSMichal Hocko #endif 16207ee36a14SMel Gorman } 16217ee36a14SMel Gorman 16227ee36a14SMel Gorman } 16237ee36a14SMel Gorman 162449d2e9ccSChristoph Lameter /* 1625a52633d8SMel Gorman * zone_lru_lock is heavily contended. Some of the functions that 16261da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 16271da177e4SLinus Torvalds * and working on them outside the LRU lock. 16281da177e4SLinus Torvalds * 16291da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 16301da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 16311da177e4SLinus Torvalds * 16321da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 16331da177e4SLinus Torvalds * 1634791b48b6SMinchan Kim * @nr_to_scan: The number of eligible pages to look through on the list. 16355dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 16361da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1637f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1638fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 16395ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 16403cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 16411da177e4SLinus Torvalds * 16421da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 16431da177e4SLinus Torvalds */ 164469e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 16455dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1646fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 16473cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 16481da177e4SLinus Torvalds { 164975b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 165069e05944SAndrew Morton unsigned long nr_taken = 0; 1651599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 16527cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 16533db65812SJohannes Weiner unsigned long skipped = 0; 1654791b48b6SMinchan Kim unsigned long scan, total_scan, nr_pages; 1655b2e18757SMel Gorman LIST_HEAD(pages_skipped); 16561da177e4SLinus Torvalds 1657791b48b6SMinchan Kim scan = 0; 1658791b48b6SMinchan Kim for (total_scan = 0; 1659791b48b6SMinchan Kim scan < nr_to_scan && nr_taken < nr_to_scan && !list_empty(src); 1660791b48b6SMinchan Kim total_scan++) { 16615ad333ebSAndy Whitcroft struct page *page; 16625ad333ebSAndy Whitcroft 16631da177e4SLinus Torvalds page = lru_to_page(src); 16641da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 16651da177e4SLinus Torvalds 1666309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 16678d438f96SNick Piggin 1668b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 1669b2e18757SMel Gorman list_move(&page->lru, &pages_skipped); 16707cc30fcfSMel Gorman nr_skipped[page_zonenum(page)]++; 1671b2e18757SMel Gorman continue; 1672b2e18757SMel Gorman } 1673b2e18757SMel Gorman 1674791b48b6SMinchan Kim /* 1675791b48b6SMinchan Kim * Do not count skipped pages because that makes the function 1676791b48b6SMinchan Kim * return with no isolated pages if the LRU mostly contains 1677791b48b6SMinchan Kim * ineligible pages. This causes the VM to not reclaim any 1678791b48b6SMinchan Kim * pages, triggering a premature OOM. 1679791b48b6SMinchan Kim */ 1680791b48b6SMinchan Kim scan++; 1681f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 16825ad333ebSAndy Whitcroft case 0: 1683599d0c95SMel Gorman nr_pages = hpage_nr_pages(page); 1684599d0c95SMel Gorman nr_taken += nr_pages; 1685599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 16865ad333ebSAndy Whitcroft list_move(&page->lru, dst); 16875ad333ebSAndy Whitcroft break; 16887c8ee9a8SNick Piggin 16895ad333ebSAndy Whitcroft case -EBUSY: 16905ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 16915ad333ebSAndy Whitcroft list_move(&page->lru, src); 16925ad333ebSAndy Whitcroft continue; 16935ad333ebSAndy Whitcroft 16945ad333ebSAndy Whitcroft default: 16955ad333ebSAndy Whitcroft BUG(); 16965ad333ebSAndy Whitcroft } 16975ad333ebSAndy Whitcroft } 16981da177e4SLinus Torvalds 1699b2e18757SMel Gorman /* 1700b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 1701b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 1702b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 1703b2e18757SMel Gorman * scanning would soon rescan the same pages to skip and put the 1704b2e18757SMel Gorman * system at risk of premature OOM. 1705b2e18757SMel Gorman */ 17067cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 17077cc30fcfSMel Gorman int zid; 17087cc30fcfSMel Gorman 17093db65812SJohannes Weiner list_splice(&pages_skipped, src); 17107cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 17117cc30fcfSMel Gorman if (!nr_skipped[zid]) 17127cc30fcfSMel Gorman continue; 17137cc30fcfSMel Gorman 17147cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 17151265e3a6SMichal Hocko skipped += nr_skipped[zid]; 17167cc30fcfSMel Gorman } 17177cc30fcfSMel Gorman } 1718791b48b6SMinchan Kim *nr_scanned = total_scan; 17191265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 1720791b48b6SMinchan Kim total_scan, skipped, nr_taken, mode, lru); 1721b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 17221da177e4SLinus Torvalds return nr_taken; 17231da177e4SLinus Torvalds } 17241da177e4SLinus Torvalds 172562695a84SNick Piggin /** 172662695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 172762695a84SNick Piggin * @page: page to isolate from its LRU list 172862695a84SNick Piggin * 172962695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 173062695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 173162695a84SNick Piggin * 173262695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 173362695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 173462695a84SNick Piggin * 173562695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1736894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1737894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1738894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 173962695a84SNick Piggin * 174062695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 174162695a84SNick Piggin * found will be decremented. 174262695a84SNick Piggin * 174362695a84SNick Piggin * Restrictions: 1744a5d09bedSMike Rapoport * 174562695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 174662695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 174762695a84SNick Piggin * without a stable reference). 174862695a84SNick Piggin * (2) the lru_lock must not be held. 174962695a84SNick Piggin * (3) interrupts must be enabled. 175062695a84SNick Piggin */ 175162695a84SNick Piggin int isolate_lru_page(struct page *page) 175262695a84SNick Piggin { 175362695a84SNick Piggin int ret = -EBUSY; 175462695a84SNick Piggin 1755309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 1756cf2a82eeSKirill A. Shutemov WARN_RATELIMIT(PageTail(page), "trying to isolate tail page"); 17570c917313SKonstantin Khlebnikov 175862695a84SNick Piggin if (PageLRU(page)) { 175962695a84SNick Piggin struct zone *zone = page_zone(page); 1760fa9add64SHugh Dickins struct lruvec *lruvec; 176162695a84SNick Piggin 1762a52633d8SMel Gorman spin_lock_irq(zone_lru_lock(zone)); 1763599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat); 17640c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1765894bc310SLee Schermerhorn int lru = page_lru(page); 17660c917313SKonstantin Khlebnikov get_page(page); 176762695a84SNick Piggin ClearPageLRU(page); 1768fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1769fa9add64SHugh Dickins ret = 0; 177062695a84SNick Piggin } 1771a52633d8SMel Gorman spin_unlock_irq(zone_lru_lock(zone)); 177262695a84SNick Piggin } 177362695a84SNick Piggin return ret; 177462695a84SNick Piggin } 177562695a84SNick Piggin 17765ad333ebSAndy Whitcroft /* 1777d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1778d37dd5dcSFengguang Wu * then get resheduled. When there are massive number of tasks doing page 1779d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1780d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1781d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 178235cd7815SRik van Riel */ 1783599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 178435cd7815SRik van Riel struct scan_control *sc) 178535cd7815SRik van Riel { 178635cd7815SRik van Riel unsigned long inactive, isolated; 178735cd7815SRik van Riel 178835cd7815SRik van Riel if (current_is_kswapd()) 178935cd7815SRik van Riel return 0; 179035cd7815SRik van Riel 179197c9341fSTejun Heo if (!sane_reclaim(sc)) 179235cd7815SRik van Riel return 0; 179335cd7815SRik van Riel 179435cd7815SRik van Riel if (file) { 1795599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 1796599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 179735cd7815SRik van Riel } else { 1798599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 1799599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 180035cd7815SRik van Riel } 180135cd7815SRik van Riel 18023cf23841SFengguang Wu /* 18033cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 18043cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 18053cf23841SFengguang Wu * deadlock. 18063cf23841SFengguang Wu */ 1807d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 18083cf23841SFengguang Wu inactive >>= 3; 18093cf23841SFengguang Wu 181035cd7815SRik van Riel return isolated > inactive; 181135cd7815SRik van Riel } 181235cd7815SRik van Riel 181366635629SMel Gorman static noinline_for_stack void 181475b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) 181566635629SMel Gorman { 181627ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 1817599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 18183f79768fSHugh Dickins LIST_HEAD(pages_to_free); 181966635629SMel Gorman 182066635629SMel Gorman /* 182166635629SMel Gorman * Put back any unfreeable pages. 182266635629SMel Gorman */ 182366635629SMel Gorman while (!list_empty(page_list)) { 18243f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 182566635629SMel Gorman int lru; 18263f79768fSHugh Dickins 1827309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 182866635629SMel Gorman list_del(&page->lru); 182939b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1830599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 183166635629SMel Gorman putback_lru_page(page); 1832599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 183366635629SMel Gorman continue; 183466635629SMel Gorman } 1835fa9add64SHugh Dickins 1836599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 1837fa9add64SHugh Dickins 18387a608572SLinus Torvalds SetPageLRU(page); 183966635629SMel Gorman lru = page_lru(page); 1840fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 1841fa9add64SHugh Dickins 184266635629SMel Gorman if (is_active_lru(lru)) { 184366635629SMel Gorman int file = is_file_lru(lru); 18449992af10SRik van Riel int numpages = hpage_nr_pages(page); 18459992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 184666635629SMel Gorman } 18472bcf8879SHugh Dickins if (put_page_testzero(page)) { 18482bcf8879SHugh Dickins __ClearPageLRU(page); 18492bcf8879SHugh Dickins __ClearPageActive(page); 1850fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 18512bcf8879SHugh Dickins 18522bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 1853599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1854747db954SJohannes Weiner mem_cgroup_uncharge(page); 18552bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 1856599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 18572bcf8879SHugh Dickins } else 18582bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 185966635629SMel Gorman } 186066635629SMel Gorman } 186166635629SMel Gorman 18623f79768fSHugh Dickins /* 18633f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 18643f79768fSHugh Dickins */ 18653f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 186666635629SMel Gorman } 186766635629SMel Gorman 186866635629SMel Gorman /* 1869399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 1870399ba0b9SNeilBrown * a backing device by writing to the page cache it sets PF_LESS_THROTTLE. 1871399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 1872399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 1873399ba0b9SNeilBrown */ 1874399ba0b9SNeilBrown static int current_may_throttle(void) 1875399ba0b9SNeilBrown { 1876399ba0b9SNeilBrown return !(current->flags & PF_LESS_THROTTLE) || 1877399ba0b9SNeilBrown current->backing_dev_info == NULL || 1878399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 1879399ba0b9SNeilBrown } 1880399ba0b9SNeilBrown 1881399ba0b9SNeilBrown /* 1882b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 18831742f19fSAndrew Morton * of reclaimed pages 18841da177e4SLinus Torvalds */ 188566635629SMel Gorman static noinline_for_stack unsigned long 18861a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 18879e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 18881da177e4SLinus Torvalds { 18891da177e4SLinus Torvalds LIST_HEAD(page_list); 1890e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 189105ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1892e247dbceSKOSAKI Motohiro unsigned long nr_taken; 18933c710c1aSMichal Hocko struct reclaim_stat stat = {}; 1894f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 18953cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1896599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 18971a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 1898db73ee0dSMichal Hocko bool stalled = false; 189978dc583dSKOSAKI Motohiro 1900599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 1901db73ee0dSMichal Hocko if (stalled) 1902db73ee0dSMichal Hocko return 0; 1903db73ee0dSMichal Hocko 1904db73ee0dSMichal Hocko /* wait a bit for the reclaimer. */ 1905db73ee0dSMichal Hocko msleep(100); 1906db73ee0dSMichal Hocko stalled = true; 190735cd7815SRik van Riel 190835cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 190935cd7815SRik van Riel if (fatal_signal_pending(current)) 191035cd7815SRik van Riel return SWAP_CLUSTER_MAX; 191135cd7815SRik van Riel } 191235cd7815SRik van Riel 19131da177e4SLinus Torvalds lru_add_drain(); 1914f80c0673SMinchan Kim 1915f80c0673SMinchan Kim if (!sc->may_unmap) 191661317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1917f80c0673SMinchan Kim 1918599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19191da177e4SLinus Torvalds 19205dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 19215dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 192295d918fcSKonstantin Khlebnikov 1923599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 19249d5e6a9fSHugh Dickins reclaim_stat->recent_scanned[file] += nr_taken; 192595d918fcSKonstantin Khlebnikov 19262262185cSRoman Gushchin if (current_is_kswapd()) { 19272262185cSRoman Gushchin if (global_reclaim(sc)) 1928599d0c95SMel Gorman __count_vm_events(PGSCAN_KSWAPD, nr_scanned); 19292262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD, 19302262185cSRoman Gushchin nr_scanned); 19312262185cSRoman Gushchin } else { 19322262185cSRoman Gushchin if (global_reclaim(sc)) 1933599d0c95SMel Gorman __count_vm_events(PGSCAN_DIRECT, nr_scanned); 19342262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSCAN_DIRECT, 19352262185cSRoman Gushchin nr_scanned); 1936b35ea17bSKOSAKI Motohiro } 1937599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1938d563c050SHillf Danton 1939d563c050SHillf Danton if (nr_taken == 0) 194066635629SMel Gorman return 0; 1941b35ea17bSKOSAKI Motohiro 1942a128ca71SShaohua Li nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0, 19433c710c1aSMichal Hocko &stat, false); 1944c661b078SAndy Whitcroft 1945599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19463f79768fSHugh Dickins 19472262185cSRoman Gushchin if (current_is_kswapd()) { 19482262185cSRoman Gushchin if (global_reclaim(sc)) 1949599d0c95SMel Gorman __count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed); 19502262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_KSWAPD, 19512262185cSRoman Gushchin nr_reclaimed); 19522262185cSRoman Gushchin } else { 19532262185cSRoman Gushchin if (global_reclaim(sc)) 1954599d0c95SMel Gorman __count_vm_events(PGSTEAL_DIRECT, nr_reclaimed); 19552262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_DIRECT, 19562262185cSRoman Gushchin nr_reclaimed); 1957904249aaSYing Han } 1958a74609faSNick Piggin 195927ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 19603f79768fSHugh Dickins 1961599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 19623f79768fSHugh Dickins 1963599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 19643f79768fSHugh Dickins 1965747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 19662d4894b5SMel Gorman free_unref_page_list(&page_list); 1967e11da5b4SMel Gorman 196892df3a72SMel Gorman /* 19691c610d5fSAndrey Ryabinin * If dirty pages are scanned that are not queued for IO, it 19701c610d5fSAndrey Ryabinin * implies that flushers are not doing their job. This can 19711c610d5fSAndrey Ryabinin * happen when memory pressure pushes dirty pages to the end of 19721c610d5fSAndrey Ryabinin * the LRU before the dirty limits are breached and the dirty 19731c610d5fSAndrey Ryabinin * data has expired. It can also happen when the proportion of 19741c610d5fSAndrey Ryabinin * dirty pages grows not through writes but through memory 19751c610d5fSAndrey Ryabinin * pressure reclaiming all the clean cache. And in some cases, 19761c610d5fSAndrey Ryabinin * the flushers simply cannot keep up with the allocation 19771c610d5fSAndrey Ryabinin * rate. Nudge the flusher threads in case they are asleep. 19781c610d5fSAndrey Ryabinin */ 19791c610d5fSAndrey Ryabinin if (stat.nr_unqueued_dirty == nr_taken) 19801c610d5fSAndrey Ryabinin wakeup_flusher_threads(WB_REASON_VMSCAN); 19811c610d5fSAndrey Ryabinin 1982d108c772SAndrey Ryabinin sc->nr.dirty += stat.nr_dirty; 1983d108c772SAndrey Ryabinin sc->nr.congested += stat.nr_congested; 1984d108c772SAndrey Ryabinin sc->nr.unqueued_dirty += stat.nr_unqueued_dirty; 1985d108c772SAndrey Ryabinin sc->nr.writeback += stat.nr_writeback; 1986d108c772SAndrey Ryabinin sc->nr.immediate += stat.nr_immediate; 1987d108c772SAndrey Ryabinin sc->nr.taken += nr_taken; 1988d108c772SAndrey Ryabinin if (file) 1989d108c772SAndrey Ryabinin sc->nr.file_taken += nr_taken; 19908e950282SMel Gorman 1991599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 1992d51d1e64SSteven Rostedt nr_scanned, nr_reclaimed, &stat, sc->priority, file); 199305ff5137SAndrew Morton return nr_reclaimed; 19941da177e4SLinus Torvalds } 19951da177e4SLinus Torvalds 19963bb1a852SMartin Bligh /* 19971cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 19981cfb419bSKAMEZAWA Hiroyuki * 19991cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 20001cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 20011cfb419bSKAMEZAWA Hiroyuki * 20021cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 2003a52633d8SMel Gorman * appropriate to hold zone_lru_lock across the whole operation. But if 20041cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 2005a52633d8SMel Gorman * should drop zone_lru_lock around each page. It's impossible to balance 20061cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 20071cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 20081cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 20091cfb419bSKAMEZAWA Hiroyuki * 20100139aa7bSJoonsoo Kim * The downside is that we have to touch page->_refcount against each page. 20111cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 20129d998b4fSMichal Hocko * 20139d998b4fSMichal Hocko * Returns the number of pages moved to the given lru. 20141cfb419bSKAMEZAWA Hiroyuki */ 20151cfb419bSKAMEZAWA Hiroyuki 20169d998b4fSMichal Hocko static unsigned move_active_pages_to_lru(struct lruvec *lruvec, 20173eb4140fSWu Fengguang struct list_head *list, 20182bcf8879SHugh Dickins struct list_head *pages_to_free, 20193eb4140fSWu Fengguang enum lru_list lru) 20203eb4140fSWu Fengguang { 2021599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 20223eb4140fSWu Fengguang struct page *page; 2023fa9add64SHugh Dickins int nr_pages; 20249d998b4fSMichal Hocko int nr_moved = 0; 20253eb4140fSWu Fengguang 20263eb4140fSWu Fengguang while (!list_empty(list)) { 20273eb4140fSWu Fengguang page = lru_to_page(list); 2028599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 20293eb4140fSWu Fengguang 2030309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 20313eb4140fSWu Fengguang SetPageLRU(page); 20323eb4140fSWu Fengguang 2033fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 2034599d0c95SMel Gorman update_lru_size(lruvec, lru, page_zonenum(page), nr_pages); 2035925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 20363eb4140fSWu Fengguang 20372bcf8879SHugh Dickins if (put_page_testzero(page)) { 20382bcf8879SHugh Dickins __ClearPageLRU(page); 20392bcf8879SHugh Dickins __ClearPageActive(page); 2040fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 20412bcf8879SHugh Dickins 20422bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 2043599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 2044747db954SJohannes Weiner mem_cgroup_uncharge(page); 20452bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 2046599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 20472bcf8879SHugh Dickins } else 20482bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 20499d998b4fSMichal Hocko } else { 20509d998b4fSMichal Hocko nr_moved += nr_pages; 20513eb4140fSWu Fengguang } 20523eb4140fSWu Fengguang } 20539d5e6a9fSHugh Dickins 20542262185cSRoman Gushchin if (!is_active_lru(lru)) { 2055f0958906SMichal Hocko __count_vm_events(PGDEACTIVATE, nr_moved); 20562262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, 20572262185cSRoman Gushchin nr_moved); 20582262185cSRoman Gushchin } 20599d998b4fSMichal Hocko 20609d998b4fSMichal Hocko return nr_moved; 20613eb4140fSWu Fengguang } 20621cfb419bSKAMEZAWA Hiroyuki 2063f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 20641a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 2065f16015fbSJohannes Weiner struct scan_control *sc, 20669e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 20671cfb419bSKAMEZAWA Hiroyuki { 206844c241f1SKOSAKI Motohiro unsigned long nr_taken; 2069f626012dSHugh Dickins unsigned long nr_scanned; 20706fe6b7e3SWu Fengguang unsigned long vm_flags; 20711cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 20728cab4754SWu Fengguang LIST_HEAD(l_active); 2073b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 20741cfb419bSKAMEZAWA Hiroyuki struct page *page; 20751a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 20769d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 20779d998b4fSMichal Hocko unsigned nr_rotated = 0; 2078f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 20793cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 2080599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 20811cfb419bSKAMEZAWA Hiroyuki 20821da177e4SLinus Torvalds lru_add_drain(); 2083f80c0673SMinchan Kim 2084f80c0673SMinchan Kim if (!sc->may_unmap) 208561317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 2086f80c0673SMinchan Kim 2087599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 2088925b7673SJohannes Weiner 20895dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 20905dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 209189b5fae5SJohannes Weiner 2092599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 2093b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 20941cfb419bSKAMEZAWA Hiroyuki 2095599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 20962262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); 20979d5e6a9fSHugh Dickins 2098599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 20991da177e4SLinus Torvalds 21001da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 21011da177e4SLinus Torvalds cond_resched(); 21021da177e4SLinus Torvalds page = lru_to_page(&l_hold); 21031da177e4SLinus Torvalds list_del(&page->lru); 21047e9cd484SRik van Riel 210539b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 2106894bc310SLee Schermerhorn putback_lru_page(page); 2107894bc310SLee Schermerhorn continue; 2108894bc310SLee Schermerhorn } 2109894bc310SLee Schermerhorn 2110cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 2111cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 2112cc715d99SMel Gorman if (page_has_private(page)) 2113cc715d99SMel Gorman try_to_release_page(page, 0); 2114cc715d99SMel Gorman unlock_page(page); 2115cc715d99SMel Gorman } 2116cc715d99SMel Gorman } 2117cc715d99SMel Gorman 2118c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 2119c3ac9a8aSJohannes Weiner &vm_flags)) { 21209992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 21218cab4754SWu Fengguang /* 21228cab4754SWu Fengguang * Identify referenced, file-backed active pages and 21238cab4754SWu Fengguang * give them one more trip around the active list. So 21248cab4754SWu Fengguang * that executable code get better chances to stay in 21258cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 21268cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 21278cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 21288cab4754SWu Fengguang * so we ignore them here. 21298cab4754SWu Fengguang */ 213041e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 21318cab4754SWu Fengguang list_add(&page->lru, &l_active); 21328cab4754SWu Fengguang continue; 21338cab4754SWu Fengguang } 21348cab4754SWu Fengguang } 21357e9cd484SRik van Riel 21365205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 21371da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 21381da177e4SLinus Torvalds } 21391da177e4SLinus Torvalds 2140b555749aSAndrew Morton /* 21418cab4754SWu Fengguang * Move pages back to the lru list. 2142b555749aSAndrew Morton */ 2143599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 21444f98a2feSRik van Riel /* 21458cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 21468cab4754SWu Fengguang * even though only some of them are actually re-activated. This 21478cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 21487c0db9e9SJerome Marchand * get_scan_count. 2149556adecbSRik van Riel */ 2150b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 2151556adecbSRik van Riel 21529d998b4fSMichal Hocko nr_activate = move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru); 21539d998b4fSMichal Hocko nr_deactivate = move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE); 2154599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 2155599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 21562bcf8879SHugh Dickins 2157747db954SJohannes Weiner mem_cgroup_uncharge_list(&l_hold); 21582d4894b5SMel Gorman free_unref_page_list(&l_hold); 21599d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 21609d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 21611da177e4SLinus Torvalds } 21621da177e4SLinus Torvalds 216359dc76b0SRik van Riel /* 216459dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 216559dc76b0SRik van Riel * to do too much work. 216614797e23SKOSAKI Motohiro * 216759dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 216859dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 216959dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 217059dc76b0SRik van Riel * 217159dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 217259dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 217359dc76b0SRik van Riel * 21742a2e4885SJohannes Weiner * If that fails and refaulting is observed, the inactive list grows. 21752a2e4885SJohannes Weiner * 217659dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 21773a50d14dSAndrey Ryabinin * on this LRU, maintained by the pageout code. An inactive_ratio 217859dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 217959dc76b0SRik van Riel * 218059dc76b0SRik van Riel * total target max 218159dc76b0SRik van Riel * memory ratio inactive 218259dc76b0SRik van Riel * ------------------------------------- 218359dc76b0SRik van Riel * 10MB 1 5MB 218459dc76b0SRik van Riel * 100MB 1 50MB 218559dc76b0SRik van Riel * 1GB 3 250MB 218659dc76b0SRik van Riel * 10GB 10 0.9GB 218759dc76b0SRik van Riel * 100GB 31 3GB 218859dc76b0SRik van Riel * 1TB 101 10GB 218959dc76b0SRik van Riel * 10TB 320 32GB 219014797e23SKOSAKI Motohiro */ 2191f8d1a311SMel Gorman static bool inactive_list_is_low(struct lruvec *lruvec, bool file, 21922a2e4885SJohannes Weiner struct mem_cgroup *memcg, 21932a2e4885SJohannes Weiner struct scan_control *sc, bool actual_reclaim) 219414797e23SKOSAKI Motohiro { 2195fd538803SMichal Hocko enum lru_list active_lru = file * LRU_FILE + LRU_ACTIVE; 21962a2e4885SJohannes Weiner struct pglist_data *pgdat = lruvec_pgdat(lruvec); 21972a2e4885SJohannes Weiner enum lru_list inactive_lru = file * LRU_FILE; 21982a2e4885SJohannes Weiner unsigned long inactive, active; 21992a2e4885SJohannes Weiner unsigned long inactive_ratio; 22002a2e4885SJohannes Weiner unsigned long refaults; 220159dc76b0SRik van Riel unsigned long gb; 220259dc76b0SRik van Riel 220374e3f3c3SMinchan Kim /* 220474e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 220574e3f3c3SMinchan Kim * is pointless. 220674e3f3c3SMinchan Kim */ 220759dc76b0SRik van Riel if (!file && !total_swap_pages) 220842e2e457SYaowei Bai return false; 220974e3f3c3SMinchan Kim 2210fd538803SMichal Hocko inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx); 2211fd538803SMichal Hocko active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx); 2212f8d1a311SMel Gorman 22132a2e4885SJohannes Weiner if (memcg) 2214ccda7f43SJohannes Weiner refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE); 22152a2e4885SJohannes Weiner else 22162a2e4885SJohannes Weiner refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE); 22172a2e4885SJohannes Weiner 22182a2e4885SJohannes Weiner /* 22192a2e4885SJohannes Weiner * When refaults are being observed, it means a new workingset 22202a2e4885SJohannes Weiner * is being established. Disable active list protection to get 22212a2e4885SJohannes Weiner * rid of the stale workingset quickly. 22222a2e4885SJohannes Weiner */ 22232a2e4885SJohannes Weiner if (file && actual_reclaim && lruvec->refaults != refaults) { 22242a2e4885SJohannes Weiner inactive_ratio = 0; 22252a2e4885SJohannes Weiner } else { 222659dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 222759dc76b0SRik van Riel if (gb) 222859dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2229b39415b2SRik van Riel else 223059dc76b0SRik van Riel inactive_ratio = 1; 22312a2e4885SJohannes Weiner } 223259dc76b0SRik van Riel 22332a2e4885SJohannes Weiner if (actual_reclaim) 22342a2e4885SJohannes Weiner trace_mm_vmscan_inactive_list_is_low(pgdat->node_id, sc->reclaim_idx, 2235fd538803SMichal Hocko lruvec_lru_size(lruvec, inactive_lru, MAX_NR_ZONES), inactive, 2236fd538803SMichal Hocko lruvec_lru_size(lruvec, active_lru, MAX_NR_ZONES), active, 2237fd538803SMichal Hocko inactive_ratio, file); 2238fd538803SMichal Hocko 223959dc76b0SRik van Riel return inactive * inactive_ratio < active; 2240b39415b2SRik van Riel } 2241b39415b2SRik van Riel 22424f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 22432a2e4885SJohannes Weiner struct lruvec *lruvec, struct mem_cgroup *memcg, 22442a2e4885SJohannes Weiner struct scan_control *sc) 2245b69408e8SChristoph Lameter { 2246b39415b2SRik van Riel if (is_active_lru(lru)) { 22472a2e4885SJohannes Weiner if (inactive_list_is_low(lruvec, is_file_lru(lru), 22482a2e4885SJohannes Weiner memcg, sc, true)) 22491a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 2250556adecbSRik van Riel return 0; 2251556adecbSRik van Riel } 2252556adecbSRik van Riel 22531a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2254b69408e8SChristoph Lameter } 2255b69408e8SChristoph Lameter 22569a265114SJohannes Weiner enum scan_balance { 22579a265114SJohannes Weiner SCAN_EQUAL, 22589a265114SJohannes Weiner SCAN_FRACT, 22599a265114SJohannes Weiner SCAN_ANON, 22609a265114SJohannes Weiner SCAN_FILE, 22619a265114SJohannes Weiner }; 22629a265114SJohannes Weiner 22631da177e4SLinus Torvalds /* 22644f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 22654f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 22664f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 22674f98a2feSRik van Riel * onto the active list instead of evict. 22684f98a2feSRik van Riel * 2269be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2270be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 22714f98a2feSRik van Riel */ 227233377678SVladimir Davydov static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, 22736b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *nr, 22746b4f7799SJohannes Weiner unsigned long *lru_pages) 22754f98a2feSRik van Riel { 227633377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 227790126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 22789a265114SJohannes Weiner u64 fraction[2]; 22799a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 2280599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 22819a265114SJohannes Weiner unsigned long anon_prio, file_prio; 22829a265114SJohannes Weiner enum scan_balance scan_balance; 22830bf1457fSJohannes Weiner unsigned long anon, file; 22849a265114SJohannes Weiner unsigned long ap, fp; 22859a265114SJohannes Weiner enum lru_list lru; 228676a33fc3SShaohua Li 228776a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2288d8b38438SVladimir Davydov if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) { 22899a265114SJohannes Weiner scan_balance = SCAN_FILE; 229076a33fc3SShaohua Li goto out; 229176a33fc3SShaohua Li } 22924f98a2feSRik van Riel 229310316b31SJohannes Weiner /* 229410316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 229510316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 229610316b31SJohannes Weiner * disable swapping for individual groups completely when 229710316b31SJohannes Weiner * using the memory controller's swap limit feature would be 229810316b31SJohannes Weiner * too expensive. 229910316b31SJohannes Weiner */ 230002695175SJohannes Weiner if (!global_reclaim(sc) && !swappiness) { 23019a265114SJohannes Weiner scan_balance = SCAN_FILE; 230210316b31SJohannes Weiner goto out; 230310316b31SJohannes Weiner } 230410316b31SJohannes Weiner 230510316b31SJohannes Weiner /* 230610316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 230710316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 230810316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 230910316b31SJohannes Weiner */ 231002695175SJohannes Weiner if (!sc->priority && swappiness) { 23119a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 231210316b31SJohannes Weiner goto out; 231310316b31SJohannes Weiner } 231410316b31SJohannes Weiner 231511d16c25SJohannes Weiner /* 231662376251SJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 231762376251SJohannes Weiner * cache pages start out inactive, every cache fault will tip 231862376251SJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 231962376251SJohannes Weiner * shrinks, so does the window for rotation from references. 232062376251SJohannes Weiner * This means we have a runaway feedback loop where a tiny 232162376251SJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 232262376251SJohannes Weiner * anon pages. Try to detect this based on file LRU size. 232362376251SJohannes Weiner */ 232462376251SJohannes Weiner if (global_reclaim(sc)) { 2325599d0c95SMel Gorman unsigned long pgdatfile; 2326599d0c95SMel Gorman unsigned long pgdatfree; 2327599d0c95SMel Gorman int z; 2328599d0c95SMel Gorman unsigned long total_high_wmark = 0; 232962376251SJohannes Weiner 2330599d0c95SMel Gorman pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 2331599d0c95SMel Gorman pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) + 2332599d0c95SMel Gorman node_page_state(pgdat, NR_INACTIVE_FILE); 23332ab051e1SJerome Marchand 2334599d0c95SMel Gorman for (z = 0; z < MAX_NR_ZONES; z++) { 2335599d0c95SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 23366aa303deSMel Gorman if (!managed_zone(zone)) 2337599d0c95SMel Gorman continue; 2338599d0c95SMel Gorman 2339599d0c95SMel Gorman total_high_wmark += high_wmark_pages(zone); 2340599d0c95SMel Gorman } 2341599d0c95SMel Gorman 2342599d0c95SMel Gorman if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) { 234306226226SDavid Rientjes /* 234406226226SDavid Rientjes * Force SCAN_ANON if there are enough inactive 234506226226SDavid Rientjes * anonymous pages on the LRU in eligible zones. 234606226226SDavid Rientjes * Otherwise, the small LRU gets thrashed. 234706226226SDavid Rientjes */ 234806226226SDavid Rientjes if (!inactive_list_is_low(lruvec, false, memcg, sc, false) && 234906226226SDavid Rientjes lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, sc->reclaim_idx) 235006226226SDavid Rientjes >> sc->priority) { 235162376251SJohannes Weiner scan_balance = SCAN_ANON; 235262376251SJohannes Weiner goto out; 235362376251SJohannes Weiner } 235462376251SJohannes Weiner } 235506226226SDavid Rientjes } 235662376251SJohannes Weiner 235762376251SJohannes Weiner /* 2358316bda0eSVladimir Davydov * If there is enough inactive page cache, i.e. if the size of the 2359316bda0eSVladimir Davydov * inactive list is greater than that of the active list *and* the 2360316bda0eSVladimir Davydov * inactive list actually has some pages to scan on this priority, we 2361316bda0eSVladimir Davydov * do not reclaim anything from the anonymous working set right now. 2362316bda0eSVladimir Davydov * Without the second condition we could end up never scanning an 2363316bda0eSVladimir Davydov * lruvec even if it has plenty of old anonymous pages unless the 2364316bda0eSVladimir Davydov * system is under heavy pressure. 2365e9868505SRik van Riel */ 23662a2e4885SJohannes Weiner if (!inactive_list_is_low(lruvec, true, memcg, sc, false) && 236771ab6cfeSMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) { 23689a265114SJohannes Weiner scan_balance = SCAN_FILE; 2369e9868505SRik van Riel goto out; 23704f98a2feSRik van Riel } 23714f98a2feSRik van Riel 23729a265114SJohannes Weiner scan_balance = SCAN_FRACT; 23739a265114SJohannes Weiner 23744f98a2feSRik van Riel /* 237558c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 237658c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 237758c37f6eSKOSAKI Motohiro */ 237802695175SJohannes Weiner anon_prio = swappiness; 237975b00af7SHugh Dickins file_prio = 200 - anon_prio; 238058c37f6eSKOSAKI Motohiro 238158c37f6eSKOSAKI Motohiro /* 23824f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 23834f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 23844f98a2feSRik van Riel * ratios to determine how valuable each cache is. 23854f98a2feSRik van Riel * 23864f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 23874f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 23884f98a2feSRik van Riel * up weighing recent references more than old ones. 23894f98a2feSRik van Riel * 23904f98a2feSRik van Riel * anon in [0], file in [1] 23914f98a2feSRik van Riel */ 23922ab051e1SJerome Marchand 2393fd538803SMichal Hocko anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) + 2394fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES); 2395fd538803SMichal Hocko file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) + 2396fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES); 23972ab051e1SJerome Marchand 2398599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 239958c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 24006e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 24016e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 24024f98a2feSRik van Riel } 24034f98a2feSRik van Riel 24046e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 24056e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 24066e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 24074f98a2feSRik van Riel } 24084f98a2feSRik van Riel 24094f98a2feSRik van Riel /* 241000d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 241100d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 241200d8089cSRik van Riel * each list that were recently referenced and in active use. 24134f98a2feSRik van Riel */ 2414fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 24156e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 24164f98a2feSRik van Riel 2417fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 24186e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 2419599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 24204f98a2feSRik van Riel 242176a33fc3SShaohua Li fraction[0] = ap; 242276a33fc3SShaohua Li fraction[1] = fp; 242376a33fc3SShaohua Li denominator = ap + fp + 1; 242476a33fc3SShaohua Li out: 24256b4f7799SJohannes Weiner *lru_pages = 0; 24264111304dSHugh Dickins for_each_evictable_lru(lru) { 24274111304dSHugh Dickins int file = is_file_lru(lru); 2428d778df51SJohannes Weiner unsigned long size; 242976a33fc3SShaohua Li unsigned long scan; 243076a33fc3SShaohua Li 243171ab6cfeSMichal Hocko size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 2432d778df51SJohannes Weiner scan = size >> sc->priority; 2433688035f7SJohannes Weiner /* 2434688035f7SJohannes Weiner * If the cgroup's already been deleted, make sure to 2435688035f7SJohannes Weiner * scrape out the remaining cache. 2436688035f7SJohannes Weiner */ 2437688035f7SJohannes Weiner if (!scan && !mem_cgroup_online(memcg)) 2438d778df51SJohannes Weiner scan = min(size, SWAP_CLUSTER_MAX); 24399a265114SJohannes Weiner 24409a265114SJohannes Weiner switch (scan_balance) { 24419a265114SJohannes Weiner case SCAN_EQUAL: 24429a265114SJohannes Weiner /* Scan lists relative to size */ 24439a265114SJohannes Weiner break; 24449a265114SJohannes Weiner case SCAN_FRACT: 24459a265114SJohannes Weiner /* 24469a265114SJohannes Weiner * Scan types proportional to swappiness and 24479a265114SJohannes Weiner * their relative recent reclaim efficiency. 24489a265114SJohannes Weiner */ 24496f04f48dSSuleiman Souhlal scan = div64_u64(scan * fraction[file], 24506f04f48dSSuleiman Souhlal denominator); 24519a265114SJohannes Weiner break; 24529a265114SJohannes Weiner case SCAN_FILE: 24539a265114SJohannes Weiner case SCAN_ANON: 24549a265114SJohannes Weiner /* Scan one type exclusively */ 24556b4f7799SJohannes Weiner if ((scan_balance == SCAN_FILE) != file) { 24566b4f7799SJohannes Weiner size = 0; 24579a265114SJohannes Weiner scan = 0; 24586b4f7799SJohannes Weiner } 24599a265114SJohannes Weiner break; 24609a265114SJohannes Weiner default: 24619a265114SJohannes Weiner /* Look ma, no brain */ 24629a265114SJohannes Weiner BUG(); 24639a265114SJohannes Weiner } 24646b4f7799SJohannes Weiner 24656b4f7799SJohannes Weiner *lru_pages += size; 24664111304dSHugh Dickins nr[lru] = scan; 246776a33fc3SShaohua Li } 24686e08a369SWu Fengguang } 24694f98a2feSRik van Riel 24709b4f98cdSJohannes Weiner /* 2471a9dd0a83SMel Gorman * This is a basic per-node page freer. Used by both kswapd and direct reclaim. 24729b4f98cdSJohannes Weiner */ 2473a9dd0a83SMel Gorman static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg, 24746b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *lru_pages) 24759b4f98cdSJohannes Weiner { 2476ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 24779b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2478e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 24799b4f98cdSJohannes Weiner unsigned long nr_to_scan; 24809b4f98cdSJohannes Weiner enum lru_list lru; 24819b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 24829b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 24839b4f98cdSJohannes Weiner struct blk_plug plug; 24841a501907SMel Gorman bool scan_adjusted; 24859b4f98cdSJohannes Weiner 248633377678SVladimir Davydov get_scan_count(lruvec, memcg, sc, nr, lru_pages); 24879b4f98cdSJohannes Weiner 2488e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2489e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2490e82e0561SMel Gorman 24911a501907SMel Gorman /* 24921a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 24931a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 24941a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 24951a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 24961a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 24971a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 24981a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 24991a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 25001a501907SMel Gorman * dropped to zero at the first pass. 25011a501907SMel Gorman */ 25021a501907SMel Gorman scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() && 25031a501907SMel Gorman sc->priority == DEF_PRIORITY); 25041a501907SMel Gorman 25059b4f98cdSJohannes Weiner blk_start_plug(&plug); 25069b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 25079b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2508e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2509e82e0561SMel Gorman unsigned long nr_scanned; 2510e82e0561SMel Gorman 25119b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 25129b4f98cdSJohannes Weiner if (nr[lru]) { 25139b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 25149b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 25159b4f98cdSJohannes Weiner 25169b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 25172a2e4885SJohannes Weiner lruvec, memcg, sc); 25189b4f98cdSJohannes Weiner } 25199b4f98cdSJohannes Weiner } 2520e82e0561SMel Gorman 2521bd041733SMichal Hocko cond_resched(); 2522bd041733SMichal Hocko 2523e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2524e82e0561SMel Gorman continue; 2525e82e0561SMel Gorman 25269b4f98cdSJohannes Weiner /* 2527e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 25281a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2529e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2530e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2531e82e0561SMel Gorman * proportional to the original scan target. 2532e82e0561SMel Gorman */ 2533e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2534e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2535e82e0561SMel Gorman 25361a501907SMel Gorman /* 25371a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 25381a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 25391a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 25401a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 25411a501907SMel Gorman */ 25421a501907SMel Gorman if (!nr_file || !nr_anon) 25431a501907SMel Gorman break; 25441a501907SMel Gorman 2545e82e0561SMel Gorman if (nr_file > nr_anon) { 2546e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2547e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2548e82e0561SMel Gorman lru = LRU_BASE; 2549e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2550e82e0561SMel Gorman } else { 2551e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2552e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2553e82e0561SMel Gorman lru = LRU_FILE; 2554e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2555e82e0561SMel Gorman } 2556e82e0561SMel Gorman 2557e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2558e82e0561SMel Gorman nr[lru] = 0; 2559e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2560e82e0561SMel Gorman 2561e82e0561SMel Gorman /* 2562e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2563e82e0561SMel Gorman * scan target and the percentage scanning already complete 2564e82e0561SMel Gorman */ 2565e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2566e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2567e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2568e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2569e82e0561SMel Gorman 2570e82e0561SMel Gorman lru += LRU_ACTIVE; 2571e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2572e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2573e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2574e82e0561SMel Gorman 2575e82e0561SMel Gorman scan_adjusted = true; 25769b4f98cdSJohannes Weiner } 25779b4f98cdSJohannes Weiner blk_finish_plug(&plug); 25789b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 25799b4f98cdSJohannes Weiner 25809b4f98cdSJohannes Weiner /* 25819b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 25829b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 25839b4f98cdSJohannes Weiner */ 25842a2e4885SJohannes Weiner if (inactive_list_is_low(lruvec, false, memcg, sc, true)) 25859b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 25869b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 25879b4f98cdSJohannes Weiner } 25889b4f98cdSJohannes Weiner 258923b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 25909e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 259123b9da55SMel Gorman { 2592d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 259323b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 25949e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 259523b9da55SMel Gorman return true; 259623b9da55SMel Gorman 259723b9da55SMel Gorman return false; 259823b9da55SMel Gorman } 259923b9da55SMel Gorman 26004f98a2feSRik van Riel /* 260123b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 260223b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 260323b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 260423b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 260523b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 26063e7d3449SMel Gorman */ 2607a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 26083e7d3449SMel Gorman unsigned long nr_reclaimed, 26093e7d3449SMel Gorman unsigned long nr_scanned, 26103e7d3449SMel Gorman struct scan_control *sc) 26113e7d3449SMel Gorman { 26123e7d3449SMel Gorman unsigned long pages_for_compaction; 26133e7d3449SMel Gorman unsigned long inactive_lru_pages; 2614a9dd0a83SMel Gorman int z; 26153e7d3449SMel Gorman 26163e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 26179e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 26183e7d3449SMel Gorman return false; 26193e7d3449SMel Gorman 26202876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 2621dcda9b04SMichal Hocko if (sc->gfp_mask & __GFP_RETRY_MAYFAIL) { 26223e7d3449SMel Gorman /* 2623dcda9b04SMichal Hocko * For __GFP_RETRY_MAYFAIL allocations, stop reclaiming if the 26242876592fSMel Gorman * full LRU list has been scanned and we are still failing 26252876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 2626dcda9b04SMichal Hocko * expensive but a __GFP_RETRY_MAYFAIL caller really wants to succeed 26273e7d3449SMel Gorman */ 26283e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 26293e7d3449SMel Gorman return false; 26302876592fSMel Gorman } else { 26312876592fSMel Gorman /* 2632dcda9b04SMichal Hocko * For non-__GFP_RETRY_MAYFAIL allocations which can presumably 26332876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 26342876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 26352876592fSMel Gorman * pages that were scanned. This will return to the 26362876592fSMel Gorman * caller faster at the risk reclaim/compaction and 26372876592fSMel Gorman * the resulting allocation attempt fails 26382876592fSMel Gorman */ 26392876592fSMel Gorman if (!nr_reclaimed) 26402876592fSMel Gorman return false; 26412876592fSMel Gorman } 26423e7d3449SMel Gorman 26433e7d3449SMel Gorman /* 26443e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 26453e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 26463e7d3449SMel Gorman */ 26479861a62cSVlastimil Babka pages_for_compaction = compact_gap(sc->order); 2648a9dd0a83SMel Gorman inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 2649ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 2650a9dd0a83SMel Gorman inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 26513e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 26523e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 26533e7d3449SMel Gorman return true; 26543e7d3449SMel Gorman 26553e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 2656a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 2657a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 26586aa303deSMel Gorman if (!managed_zone(zone)) 2659a9dd0a83SMel Gorman continue; 2660a9dd0a83SMel Gorman 2661a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 2662cf378319SVlastimil Babka case COMPACT_SUCCESS: 26633e7d3449SMel Gorman case COMPACT_CONTINUE: 26643e7d3449SMel Gorman return false; 26653e7d3449SMel Gorman default: 2666a9dd0a83SMel Gorman /* check next zone */ 2667a9dd0a83SMel Gorman ; 26683e7d3449SMel Gorman } 26693e7d3449SMel Gorman } 2670a9dd0a83SMel Gorman return true; 2671a9dd0a83SMel Gorman } 26723e7d3449SMel Gorman 2673e3c1ac58SAndrey Ryabinin static bool pgdat_memcg_congested(pg_data_t *pgdat, struct mem_cgroup *memcg) 2674e3c1ac58SAndrey Ryabinin { 2675e3c1ac58SAndrey Ryabinin return test_bit(PGDAT_CONGESTED, &pgdat->flags) || 2676e3c1ac58SAndrey Ryabinin (memcg && memcg_congested(pgdat, memcg)); 2677e3c1ac58SAndrey Ryabinin } 2678e3c1ac58SAndrey Ryabinin 2679970a39a3SMel Gorman static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) 2680f16015fbSJohannes Weiner { 2681cb731d6cSVladimir Davydov struct reclaim_state *reclaim_state = current->reclaim_state; 26829b4f98cdSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 26832344d7e4SJohannes Weiner bool reclaimable = false; 26849b4f98cdSJohannes Weiner 26859b4f98cdSJohannes Weiner do { 26865660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 26875660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 2688ef8f2327SMel Gorman .pgdat = pgdat, 26899e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 26905660048cSJohannes Weiner }; 2691a9dd0a83SMel Gorman unsigned long node_lru_pages = 0; 2692694fbc0fSAndrew Morton struct mem_cgroup *memcg; 26935660048cSJohannes Weiner 2694d108c772SAndrey Ryabinin memset(&sc->nr, 0, sizeof(sc->nr)); 2695d108c772SAndrey Ryabinin 26969b4f98cdSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 26979b4f98cdSJohannes Weiner nr_scanned = sc->nr_scanned; 26989b4f98cdSJohannes Weiner 2699694fbc0fSAndrew Morton memcg = mem_cgroup_iter(root, NULL, &reclaim); 2700694fbc0fSAndrew Morton do { 27016b4f7799SJohannes Weiner unsigned long lru_pages; 27028e8ae645SJohannes Weiner unsigned long reclaimed; 2703cb731d6cSVladimir Davydov unsigned long scanned; 27049b4f98cdSJohannes Weiner 2705bf8d5d52SRoman Gushchin switch (mem_cgroup_protected(root, memcg)) { 2706bf8d5d52SRoman Gushchin case MEMCG_PROT_MIN: 2707bf8d5d52SRoman Gushchin /* 2708bf8d5d52SRoman Gushchin * Hard protection. 2709bf8d5d52SRoman Gushchin * If there is no reclaimable memory, OOM. 2710bf8d5d52SRoman Gushchin */ 2711bf8d5d52SRoman Gushchin continue; 2712bf8d5d52SRoman Gushchin case MEMCG_PROT_LOW: 2713bf8d5d52SRoman Gushchin /* 2714bf8d5d52SRoman Gushchin * Soft protection. 2715bf8d5d52SRoman Gushchin * Respect the protection only as long as 2716bf8d5d52SRoman Gushchin * there is an unprotected supply 2717bf8d5d52SRoman Gushchin * of reclaimable memory from other cgroups. 2718bf8d5d52SRoman Gushchin */ 2719d6622f63SYisheng Xie if (!sc->memcg_low_reclaim) { 2720d6622f63SYisheng Xie sc->memcg_low_skipped = 1; 2721241994edSJohannes Weiner continue; 2722d6622f63SYisheng Xie } 2723e27be240SJohannes Weiner memcg_memory_event(memcg, MEMCG_LOW); 2724bf8d5d52SRoman Gushchin break; 2725bf8d5d52SRoman Gushchin case MEMCG_PROT_NONE: 2726bf8d5d52SRoman Gushchin break; 2727241994edSJohannes Weiner } 2728241994edSJohannes Weiner 27298e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 2730cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 2731a9dd0a83SMel Gorman shrink_node_memcg(pgdat, memcg, sc, &lru_pages); 2732a9dd0a83SMel Gorman node_lru_pages += lru_pages; 2733f9be23d6SKonstantin Khlebnikov 2734a9dd0a83SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, 27359092c71bSJosef Bacik memcg, sc->priority); 2736cb731d6cSVladimir Davydov 27378e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 27388e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 27398e8ae645SJohannes Weiner sc->nr_scanned - scanned, 27408e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 27418e8ae645SJohannes Weiner 27425660048cSJohannes Weiner /* 2743a394cb8eSMichal Hocko * Direct reclaim and kswapd have to scan all memory 2744a394cb8eSMichal Hocko * cgroups to fulfill the overall scan target for the 2745a9dd0a83SMel Gorman * node. 2746a394cb8eSMichal Hocko * 2747a394cb8eSMichal Hocko * Limit reclaim, on the other hand, only cares about 2748a394cb8eSMichal Hocko * nr_to_reclaim pages to be reclaimed and it will 2749a394cb8eSMichal Hocko * retry with decreasing priority if one round over the 2750a394cb8eSMichal Hocko * whole hierarchy is not sufficient. 27515660048cSJohannes Weiner */ 2752a394cb8eSMichal Hocko if (!global_reclaim(sc) && 2753a394cb8eSMichal Hocko sc->nr_reclaimed >= sc->nr_to_reclaim) { 27545660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 27555660048cSJohannes Weiner break; 27565660048cSJohannes Weiner } 2757241994edSJohannes Weiner } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim))); 275870ddf637SAnton Vorontsov 27596b4f7799SJohannes Weiner if (reclaim_state) { 2760cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 27616b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 27626b4f7799SJohannes Weiner } 27636b4f7799SJohannes Weiner 27648e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 27658e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 276670ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 276770ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 276870ddf637SAnton Vorontsov 27692344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 27702344d7e4SJohannes Weiner reclaimable = true; 27712344d7e4SJohannes Weiner 2772e3c1ac58SAndrey Ryabinin if (current_is_kswapd()) { 2773d108c772SAndrey Ryabinin /* 2774e3c1ac58SAndrey Ryabinin * If reclaim is isolating dirty pages under writeback, 2775e3c1ac58SAndrey Ryabinin * it implies that the long-lived page allocation rate 2776e3c1ac58SAndrey Ryabinin * is exceeding the page laundering rate. Either the 2777e3c1ac58SAndrey Ryabinin * global limits are not being effective at throttling 2778e3c1ac58SAndrey Ryabinin * processes due to the page distribution throughout 2779e3c1ac58SAndrey Ryabinin * zones or there is heavy usage of a slow backing 2780e3c1ac58SAndrey Ryabinin * device. The only option is to throttle from reclaim 2781e3c1ac58SAndrey Ryabinin * context which is not ideal as there is no guarantee 2782d108c772SAndrey Ryabinin * the dirtying process is throttled in the same way 2783d108c772SAndrey Ryabinin * balance_dirty_pages() manages. 2784d108c772SAndrey Ryabinin * 2785e3c1ac58SAndrey Ryabinin * Once a node is flagged PGDAT_WRITEBACK, kswapd will 2786e3c1ac58SAndrey Ryabinin * count the number of pages under pages flagged for 2787e3c1ac58SAndrey Ryabinin * immediate reclaim and stall if any are encountered 2788e3c1ac58SAndrey Ryabinin * in the nr_immediate check below. 2789d108c772SAndrey Ryabinin */ 2790d108c772SAndrey Ryabinin if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken) 2791d108c772SAndrey Ryabinin set_bit(PGDAT_WRITEBACK, &pgdat->flags); 2792d108c772SAndrey Ryabinin 2793d108c772SAndrey Ryabinin /* 2794d108c772SAndrey Ryabinin * Tag a node as congested if all the dirty pages 2795d108c772SAndrey Ryabinin * scanned were backed by a congested BDI and 2796d108c772SAndrey Ryabinin * wait_iff_congested will stall. 2797d108c772SAndrey Ryabinin */ 2798d108c772SAndrey Ryabinin if (sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 2799d108c772SAndrey Ryabinin set_bit(PGDAT_CONGESTED, &pgdat->flags); 2800d108c772SAndrey Ryabinin 2801d108c772SAndrey Ryabinin /* Allow kswapd to start writing pages during reclaim.*/ 2802d108c772SAndrey Ryabinin if (sc->nr.unqueued_dirty == sc->nr.file_taken) 2803d108c772SAndrey Ryabinin set_bit(PGDAT_DIRTY, &pgdat->flags); 2804d108c772SAndrey Ryabinin 2805d108c772SAndrey Ryabinin /* 2806d108c772SAndrey Ryabinin * If kswapd scans pages marked marked for immediate 2807d108c772SAndrey Ryabinin * reclaim and under writeback (nr_immediate), it 2808d108c772SAndrey Ryabinin * implies that pages are cycling through the LRU 2809d108c772SAndrey Ryabinin * faster than they are written so also forcibly stall. 2810d108c772SAndrey Ryabinin */ 2811d108c772SAndrey Ryabinin if (sc->nr.immediate) 2812d108c772SAndrey Ryabinin congestion_wait(BLK_RW_ASYNC, HZ/10); 2813d108c772SAndrey Ryabinin } 2814d108c772SAndrey Ryabinin 2815d108c772SAndrey Ryabinin /* 2816e3c1ac58SAndrey Ryabinin * Legacy memcg will stall in page writeback so avoid forcibly 2817e3c1ac58SAndrey Ryabinin * stalling in wait_iff_congested(). 2818e3c1ac58SAndrey Ryabinin */ 2819e3c1ac58SAndrey Ryabinin if (!global_reclaim(sc) && sane_reclaim(sc) && 2820e3c1ac58SAndrey Ryabinin sc->nr.dirty && sc->nr.dirty == sc->nr.congested) 2821e3c1ac58SAndrey Ryabinin set_memcg_congestion(pgdat, root, true); 2822e3c1ac58SAndrey Ryabinin 2823e3c1ac58SAndrey Ryabinin /* 2824d108c772SAndrey Ryabinin * Stall direct reclaim for IO completions if underlying BDIs 2825d108c772SAndrey Ryabinin * and node is congested. Allow kswapd to continue until it 2826d108c772SAndrey Ryabinin * starts encountering unqueued dirty pages or cycling through 2827d108c772SAndrey Ryabinin * the LRU too quickly. 2828d108c772SAndrey Ryabinin */ 2829d108c772SAndrey Ryabinin if (!sc->hibernation_mode && !current_is_kswapd() && 2830e3c1ac58SAndrey Ryabinin current_may_throttle() && pgdat_memcg_congested(pgdat, root)) 2831e3c1ac58SAndrey Ryabinin wait_iff_congested(BLK_RW_ASYNC, HZ/10); 2832d108c772SAndrey Ryabinin 2833a9dd0a83SMel Gorman } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 28349b4f98cdSJohannes Weiner sc->nr_scanned - nr_scanned, sc)); 28352344d7e4SJohannes Weiner 2836c73322d0SJohannes Weiner /* 2837c73322d0SJohannes Weiner * Kswapd gives up on balancing particular nodes after too 2838c73322d0SJohannes Weiner * many failures to reclaim anything from them and goes to 2839c73322d0SJohannes Weiner * sleep. On reclaim progress, reset the failure counter. A 2840c73322d0SJohannes Weiner * successful direct reclaim run will revive a dormant kswapd. 2841c73322d0SJohannes Weiner */ 2842c73322d0SJohannes Weiner if (reclaimable) 2843c73322d0SJohannes Weiner pgdat->kswapd_failures = 0; 2844c73322d0SJohannes Weiner 28452344d7e4SJohannes Weiner return reclaimable; 2846f16015fbSJohannes Weiner } 2847f16015fbSJohannes Weiner 284853853e2dSVlastimil Babka /* 2849fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 2850fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 2851fdd4c614SVlastimil Babka * should reclaim first. 285253853e2dSVlastimil Babka */ 28534f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 2854fe4b1b24SMel Gorman { 285531483b6aSMel Gorman unsigned long watermark; 2856fdd4c614SVlastimil Babka enum compact_result suitable; 2857fe4b1b24SMel Gorman 2858fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 2859fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 2860fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 2861fdd4c614SVlastimil Babka return true; 2862fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 2863fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 2864fe4b1b24SMel Gorman return false; 2865fe4b1b24SMel Gorman 2866fdd4c614SVlastimil Babka /* 2867fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 2868fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 2869fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 2870fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 2871fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 2872fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 2873fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 2874fdd4c614SVlastimil Babka */ 2875fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 2876fdd4c614SVlastimil Babka 2877fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 2878fe4b1b24SMel Gorman } 2879fe4b1b24SMel Gorman 28801da177e4SLinus Torvalds /* 28811da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 28821da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 28831da177e4SLinus Torvalds * request. 28841da177e4SLinus Torvalds * 28851da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 28861da177e4SLinus Torvalds * scan then give up on it. 28871da177e4SLinus Torvalds */ 28880a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 28891da177e4SLinus Torvalds { 2890dd1a239fSMel Gorman struct zoneref *z; 289154a6eb5cSMel Gorman struct zone *zone; 28920608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 28930608f43dSAndrew Morton unsigned long nr_soft_scanned; 2894619d0d76SWeijie Yang gfp_t orig_mask; 289579dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 28961cfb419bSKAMEZAWA Hiroyuki 2897cc715d99SMel Gorman /* 2898cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2899cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2900cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2901cc715d99SMel Gorman */ 2902619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 2903b2e18757SMel Gorman if (buffer_heads_over_limit) { 2904cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 29054f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 2906b2e18757SMel Gorman } 2907cc715d99SMel Gorman 2908d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2909b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 2910b2e18757SMel Gorman /* 29111cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 29121cfb419bSKAMEZAWA Hiroyuki * to global LRU. 29131cfb419bSKAMEZAWA Hiroyuki */ 291489b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2915344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 2916344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 29171da177e4SLinus Torvalds continue; 291865ec02cbSVladimir Davydov 2919e0887c19SRik van Riel /* 2920e0c23279SMel Gorman * If we already have plenty of memory free for 2921e0c23279SMel Gorman * compaction in this zone, don't free any more. 2922e0c23279SMel Gorman * Even though compaction is invoked for any 2923e0c23279SMel Gorman * non-zero order, only frequent costly order 2924e0c23279SMel Gorman * reclamation is disruptive enough to become a 2925c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2926c7cfa37bSCopot Alexandru * page allocations. 2927e0887c19SRik van Riel */ 29280b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 29290b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 29304f588331SMel Gorman compaction_ready(zone, sc)) { 29310b06496aSJohannes Weiner sc->compaction_ready = true; 2932e0887c19SRik van Riel continue; 2933e0887c19SRik van Riel } 29340b06496aSJohannes Weiner 29350608f43dSAndrew Morton /* 293679dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 293779dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 293879dafcdcSMel Gorman * node may be shrunk multiple times but in that case 293979dafcdcSMel Gorman * the user prefers lower zones being preserved. 294079dafcdcSMel Gorman */ 294179dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 294279dafcdcSMel Gorman continue; 294379dafcdcSMel Gorman 294479dafcdcSMel Gorman /* 29450608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 29460608f43dSAndrew Morton * and returns the number of reclaimed pages and 29470608f43dSAndrew Morton * scanned pages. This works for global memory pressure 29480608f43dSAndrew Morton * and balancing, not for a memcg's limit. 29490608f43dSAndrew Morton */ 29500608f43dSAndrew Morton nr_soft_scanned = 0; 2951ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 29520608f43dSAndrew Morton sc->order, sc->gfp_mask, 29530608f43dSAndrew Morton &nr_soft_scanned); 29540608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 29550608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 2956ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2957ac34a1a3SKAMEZAWA Hiroyuki } 2958d149e3b2SYing Han 295979dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 296079dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 296179dafcdcSMel Gorman continue; 296279dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 2963970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 29641da177e4SLinus Torvalds } 2965e0c23279SMel Gorman 296665ec02cbSVladimir Davydov /* 2967619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 2968619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 2969619d0d76SWeijie Yang */ 2970619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 29711da177e4SLinus Torvalds } 29721da177e4SLinus Torvalds 29732a2e4885SJohannes Weiner static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat) 29742a2e4885SJohannes Weiner { 29752a2e4885SJohannes Weiner struct mem_cgroup *memcg; 29762a2e4885SJohannes Weiner 29772a2e4885SJohannes Weiner memcg = mem_cgroup_iter(root_memcg, NULL, NULL); 29782a2e4885SJohannes Weiner do { 29792a2e4885SJohannes Weiner unsigned long refaults; 29802a2e4885SJohannes Weiner struct lruvec *lruvec; 29812a2e4885SJohannes Weiner 29822a2e4885SJohannes Weiner if (memcg) 2983ccda7f43SJohannes Weiner refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE); 29842a2e4885SJohannes Weiner else 29852a2e4885SJohannes Weiner refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE); 29862a2e4885SJohannes Weiner 29872a2e4885SJohannes Weiner lruvec = mem_cgroup_lruvec(pgdat, memcg); 29882a2e4885SJohannes Weiner lruvec->refaults = refaults; 29892a2e4885SJohannes Weiner } while ((memcg = mem_cgroup_iter(root_memcg, memcg, NULL))); 29902a2e4885SJohannes Weiner } 29912a2e4885SJohannes Weiner 29921da177e4SLinus Torvalds /* 29931da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 29941da177e4SLinus Torvalds * 29951da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 29961da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 29971da177e4SLinus Torvalds * 29981da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 29991da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 30005b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 30015b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 30025b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 30035b0830cbSJens Axboe * work, and the allocation attempt will fail. 3004a41f24eaSNishanth Aravamudan * 3005a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 3006a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 30071da177e4SLinus Torvalds */ 3008dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 30093115cd91SVladimir Davydov struct scan_control *sc) 30101da177e4SLinus Torvalds { 3011241994edSJohannes Weiner int initial_priority = sc->priority; 30122a2e4885SJohannes Weiner pg_data_t *last_pgdat; 30132a2e4885SJohannes Weiner struct zoneref *z; 30142a2e4885SJohannes Weiner struct zone *zone; 3015241994edSJohannes Weiner retry: 3016873b4771SKeika Kobayashi delayacct_freepages_start(); 3017873b4771SKeika Kobayashi 301889b5fae5SJohannes Weiner if (global_reclaim(sc)) 30197cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 30201da177e4SLinus Torvalds 30219e3b2f8cSKonstantin Khlebnikov do { 302270ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 302370ddf637SAnton Vorontsov sc->priority); 302466e1707bSBalbir Singh sc->nr_scanned = 0; 30250a0337e0SMichal Hocko shrink_zones(zonelist, sc); 3026e0c23279SMel Gorman 3027bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 30280b06496aSJohannes Weiner break; 30290b06496aSJohannes Weiner 30300b06496aSJohannes Weiner if (sc->compaction_ready) 30310b06496aSJohannes Weiner break; 30321da177e4SLinus Torvalds 30331da177e4SLinus Torvalds /* 30340e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 30350e50ce3bSMinchan Kim * writepage even in laptop mode. 30360e50ce3bSMinchan Kim */ 30370e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 30380e50ce3bSMinchan Kim sc->may_writepage = 1; 30390b06496aSJohannes Weiner } while (--sc->priority >= 0); 3040bb21c7ceSKOSAKI Motohiro 30412a2e4885SJohannes Weiner last_pgdat = NULL; 30422a2e4885SJohannes Weiner for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, 30432a2e4885SJohannes Weiner sc->nodemask) { 30442a2e4885SJohannes Weiner if (zone->zone_pgdat == last_pgdat) 30452a2e4885SJohannes Weiner continue; 30462a2e4885SJohannes Weiner last_pgdat = zone->zone_pgdat; 30472a2e4885SJohannes Weiner snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); 3048e3c1ac58SAndrey Ryabinin set_memcg_congestion(last_pgdat, sc->target_mem_cgroup, false); 30492a2e4885SJohannes Weiner } 30502a2e4885SJohannes Weiner 3051873b4771SKeika Kobayashi delayacct_freepages_end(); 3052873b4771SKeika Kobayashi 3053bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 3054bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 3055bb21c7ceSKOSAKI Motohiro 30560cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 30570b06496aSJohannes Weiner if (sc->compaction_ready) 30587335084dSMel Gorman return 1; 30597335084dSMel Gorman 3060241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 3061d6622f63SYisheng Xie if (sc->memcg_low_skipped) { 3062241994edSJohannes Weiner sc->priority = initial_priority; 3063d6622f63SYisheng Xie sc->memcg_low_reclaim = 1; 3064d6622f63SYisheng Xie sc->memcg_low_skipped = 0; 3065241994edSJohannes Weiner goto retry; 3066241994edSJohannes Weiner } 3067241994edSJohannes Weiner 3068bb21c7ceSKOSAKI Motohiro return 0; 30691da177e4SLinus Torvalds } 30701da177e4SLinus Torvalds 3071c73322d0SJohannes Weiner static bool allow_direct_reclaim(pg_data_t *pgdat) 30725515061dSMel Gorman { 30735515061dSMel Gorman struct zone *zone; 30745515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 30755515061dSMel Gorman unsigned long free_pages = 0; 30765515061dSMel Gorman int i; 30775515061dSMel Gorman bool wmark_ok; 30785515061dSMel Gorman 3079c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3080c73322d0SJohannes Weiner return true; 3081c73322d0SJohannes Weiner 30825515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 30835515061dSMel Gorman zone = &pgdat->node_zones[i]; 3084d450abd8SJohannes Weiner if (!managed_zone(zone)) 3085d450abd8SJohannes Weiner continue; 3086d450abd8SJohannes Weiner 3087d450abd8SJohannes Weiner if (!zone_reclaimable_pages(zone)) 3088675becceSMel Gorman continue; 3089675becceSMel Gorman 30905515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 30915515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 30925515061dSMel Gorman } 30935515061dSMel Gorman 3094675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 3095675becceSMel Gorman if (!pfmemalloc_reserve) 3096675becceSMel Gorman return true; 3097675becceSMel Gorman 30985515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 30995515061dSMel Gorman 31005515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 31015515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 310238087d9bSMel Gorman pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx, 31035515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 31045515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 31055515061dSMel Gorman } 31065515061dSMel Gorman 31075515061dSMel Gorman return wmark_ok; 31085515061dSMel Gorman } 31095515061dSMel Gorman 31105515061dSMel Gorman /* 31115515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 31125515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 31135515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 311450694c28SMel Gorman * when the low watermark is reached. 311550694c28SMel Gorman * 311650694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 311750694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 31185515061dSMel Gorman */ 311950694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 31205515061dSMel Gorman nodemask_t *nodemask) 31215515061dSMel Gorman { 3122675becceSMel Gorman struct zoneref *z; 31235515061dSMel Gorman struct zone *zone; 3124675becceSMel Gorman pg_data_t *pgdat = NULL; 31255515061dSMel Gorman 31265515061dSMel Gorman /* 31275515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 31285515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 31295515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 31305515061dSMel Gorman * committing a transaction where throttling it could forcing other 31315515061dSMel Gorman * processes to block on log_wait_commit(). 31325515061dSMel Gorman */ 31335515061dSMel Gorman if (current->flags & PF_KTHREAD) 313450694c28SMel Gorman goto out; 313550694c28SMel Gorman 313650694c28SMel Gorman /* 313750694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 313850694c28SMel Gorman * It should return quickly so it can exit and free its memory 313950694c28SMel Gorman */ 314050694c28SMel Gorman if (fatal_signal_pending(current)) 314150694c28SMel Gorman goto out; 31425515061dSMel Gorman 3143675becceSMel Gorman /* 3144675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 3145675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 3146675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 3147675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 3148675becceSMel Gorman * 3149675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 3150675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 3151675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 3152675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 3153675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 3154675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 3155675becceSMel Gorman * should make reasonable progress. 3156675becceSMel Gorman */ 3157675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 315817636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 3159675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 3160675becceSMel Gorman continue; 3161675becceSMel Gorman 3162675becceSMel Gorman /* Throttle based on the first usable node */ 31635515061dSMel Gorman pgdat = zone->zone_pgdat; 3164c73322d0SJohannes Weiner if (allow_direct_reclaim(pgdat)) 316550694c28SMel Gorman goto out; 3166675becceSMel Gorman break; 3167675becceSMel Gorman } 3168675becceSMel Gorman 3169675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 3170675becceSMel Gorman if (!pgdat) 3171675becceSMel Gorman goto out; 31725515061dSMel Gorman 317368243e76SMel Gorman /* Account for the throttling */ 317468243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 317568243e76SMel Gorman 31765515061dSMel Gorman /* 31775515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 31785515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 31795515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 31805515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 31815515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 31825515061dSMel Gorman * second before continuing. 31835515061dSMel Gorman */ 31845515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 31855515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 3186c73322d0SJohannes Weiner allow_direct_reclaim(pgdat), HZ); 318750694c28SMel Gorman 318850694c28SMel Gorman goto check_pending; 31895515061dSMel Gorman } 31905515061dSMel Gorman 31915515061dSMel Gorman /* Throttle until kswapd wakes the process */ 31925515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 3193c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)); 319450694c28SMel Gorman 319550694c28SMel Gorman check_pending: 319650694c28SMel Gorman if (fatal_signal_pending(current)) 319750694c28SMel Gorman return true; 319850694c28SMel Gorman 319950694c28SMel Gorman out: 320050694c28SMel Gorman return false; 32015515061dSMel Gorman } 32025515061dSMel Gorman 3203dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 3204327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 320566e1707bSBalbir Singh { 320633906bc5SMel Gorman unsigned long nr_reclaimed; 320766e1707bSBalbir Singh struct scan_control sc = { 320822fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3209f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 3210b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 3211ee814fe2SJohannes Weiner .order = order, 3212ee814fe2SJohannes Weiner .nodemask = nodemask, 3213ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 3214ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3215a6dc60f8SJohannes Weiner .may_unmap = 1, 32162e2e4259SKOSAKI Motohiro .may_swap = 1, 321766e1707bSBalbir Singh }; 321866e1707bSBalbir Singh 32195515061dSMel Gorman /* 3220bb451fdfSGreg Thelen * scan_control uses s8 fields for order, priority, and reclaim_idx. 3221bb451fdfSGreg Thelen * Confirm they are large enough for max values. 3222bb451fdfSGreg Thelen */ 3223bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_ORDER > S8_MAX); 3224bb451fdfSGreg Thelen BUILD_BUG_ON(DEF_PRIORITY > S8_MAX); 3225bb451fdfSGreg Thelen BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX); 3226bb451fdfSGreg Thelen 3227bb451fdfSGreg Thelen /* 322850694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 322950694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 323050694c28SMel Gorman * point. 32315515061dSMel Gorman */ 3232f2f43e56SNick Desaulniers if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) 32335515061dSMel Gorman return 1; 32345515061dSMel Gorman 323533906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 323633906bc5SMel Gorman sc.may_writepage, 3237f2f43e56SNick Desaulniers sc.gfp_mask, 3238e5146b12SMel Gorman sc.reclaim_idx); 323933906bc5SMel Gorman 32403115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 324133906bc5SMel Gorman 324233906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 324333906bc5SMel Gorman 324433906bc5SMel Gorman return nr_reclaimed; 324566e1707bSBalbir Singh } 324666e1707bSBalbir Singh 3247c255a458SAndrew Morton #ifdef CONFIG_MEMCG 324866e1707bSBalbir Singh 3249a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 32504e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 3251ef8f2327SMel Gorman pg_data_t *pgdat, 32520ae5e89cSYing Han unsigned long *nr_scanned) 32534e416953SBalbir Singh { 32544e416953SBalbir Singh struct scan_control sc = { 3255b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3256ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 32574e416953SBalbir Singh .may_writepage = !laptop_mode, 32584e416953SBalbir Singh .may_unmap = 1, 3259b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 32604e416953SBalbir Singh .may_swap = !noswap, 32614e416953SBalbir Singh }; 32626b4f7799SJohannes Weiner unsigned long lru_pages; 32630ae5e89cSYing Han 32644e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 32654e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 3266bdce6d9eSKOSAKI Motohiro 32679e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 3268bdce6d9eSKOSAKI Motohiro sc.may_writepage, 3269e5146b12SMel Gorman sc.gfp_mask, 3270e5146b12SMel Gorman sc.reclaim_idx); 3271bdce6d9eSKOSAKI Motohiro 32724e416953SBalbir Singh /* 32734e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 32744e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 3275a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 32764e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 32774e416953SBalbir Singh * the priority and make it zero. 32784e416953SBalbir Singh */ 3279ef8f2327SMel Gorman shrink_node_memcg(pgdat, memcg, &sc, &lru_pages); 3280bdce6d9eSKOSAKI Motohiro 3281bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 3282bdce6d9eSKOSAKI Motohiro 32830ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 32844e416953SBalbir Singh return sc.nr_reclaimed; 32854e416953SBalbir Singh } 32864e416953SBalbir Singh 328772835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 3288b70a2a21SJohannes Weiner unsigned long nr_pages, 32898c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 3290b70a2a21SJohannes Weiner bool may_swap) 329166e1707bSBalbir Singh { 32924e416953SBalbir Singh struct zonelist *zonelist; 3293bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 3294889976dbSYing Han int nid; 3295499118e9SVlastimil Babka unsigned int noreclaim_flag; 329666e1707bSBalbir Singh struct scan_control sc = { 3297b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 32987dea19f9SMichal Hocko .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 3299ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 3300b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3301ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3302ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 330366e1707bSBalbir Singh .may_writepage = !laptop_mode, 3304a6dc60f8SJohannes Weiner .may_unmap = 1, 3305b70a2a21SJohannes Weiner .may_swap = may_swap, 3306a09ed5e0SYing Han }; 330766e1707bSBalbir Singh 3308889976dbSYing Han /* 3309889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 3310889976dbSYing Han * take care of from where we get pages. So the node where we start the 3311889976dbSYing Han * scan does not need to be the current node. 3312889976dbSYing Han */ 331372835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 3314889976dbSYing Han 3315c9634cf0SAneesh Kumar K.V zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK]; 3316bdce6d9eSKOSAKI Motohiro 3317bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 3318bdce6d9eSKOSAKI Motohiro sc.may_writepage, 3319e5146b12SMel Gorman sc.gfp_mask, 3320e5146b12SMel Gorman sc.reclaim_idx); 3321bdce6d9eSKOSAKI Motohiro 3322499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 33233115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3324499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3325bdce6d9eSKOSAKI Motohiro 3326bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 3327bdce6d9eSKOSAKI Motohiro 3328bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 332966e1707bSBalbir Singh } 333066e1707bSBalbir Singh #endif 333166e1707bSBalbir Singh 33321d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3333ef8f2327SMel Gorman struct scan_control *sc) 3334f16015fbSJohannes Weiner { 3335b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3336b95a2f2dSJohannes Weiner 3337b95a2f2dSJohannes Weiner if (!total_swap_pages) 3338b95a2f2dSJohannes Weiner return; 3339b95a2f2dSJohannes Weiner 3340b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3341b95a2f2dSJohannes Weiner do { 3342ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 3343f16015fbSJohannes Weiner 33442a2e4885SJohannes Weiner if (inactive_list_is_low(lruvec, false, memcg, sc, true)) 33451a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 33469e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3347b95a2f2dSJohannes Weiner 3348b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3349b95a2f2dSJohannes Weiner } while (memcg); 3350f16015fbSJohannes Weiner } 3351f16015fbSJohannes Weiner 3352e716f2ebSMel Gorman /* 3353e716f2ebSMel Gorman * Returns true if there is an eligible zone balanced for the request order 3354e716f2ebSMel Gorman * and classzone_idx 3355e716f2ebSMel Gorman */ 3356e716f2ebSMel Gorman static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx) 335760cefed4SJohannes Weiner { 3358e716f2ebSMel Gorman int i; 3359e716f2ebSMel Gorman unsigned long mark = -1; 3360e716f2ebSMel Gorman struct zone *zone; 336160cefed4SJohannes Weiner 3362e716f2ebSMel Gorman for (i = 0; i <= classzone_idx; i++) { 3363e716f2ebSMel Gorman zone = pgdat->node_zones + i; 33646256c6b4SMel Gorman 3365e716f2ebSMel Gorman if (!managed_zone(zone)) 3366e716f2ebSMel Gorman continue; 3367e716f2ebSMel Gorman 3368e716f2ebSMel Gorman mark = high_wmark_pages(zone); 3369e716f2ebSMel Gorman if (zone_watermark_ok_safe(zone, order, mark, classzone_idx)) 33706256c6b4SMel Gorman return true; 337160cefed4SJohannes Weiner } 337260cefed4SJohannes Weiner 3373e716f2ebSMel Gorman /* 3374e716f2ebSMel Gorman * If a node has no populated zone within classzone_idx, it does not 3375e716f2ebSMel Gorman * need balancing by definition. This can happen if a zone-restricted 3376e716f2ebSMel Gorman * allocation tries to wake a remote kswapd. 3377e716f2ebSMel Gorman */ 3378e716f2ebSMel Gorman if (mark == -1) 3379e716f2ebSMel Gorman return true; 3380e716f2ebSMel Gorman 3381e716f2ebSMel Gorman return false; 3382e716f2ebSMel Gorman } 3383e716f2ebSMel Gorman 3384631b6e08SMel Gorman /* Clear pgdat state for congested, dirty or under writeback. */ 3385631b6e08SMel Gorman static void clear_pgdat_congested(pg_data_t *pgdat) 3386631b6e08SMel Gorman { 3387631b6e08SMel Gorman clear_bit(PGDAT_CONGESTED, &pgdat->flags); 3388631b6e08SMel Gorman clear_bit(PGDAT_DIRTY, &pgdat->flags); 3389631b6e08SMel Gorman clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 3390631b6e08SMel Gorman } 3391631b6e08SMel Gorman 33921741c877SMel Gorman /* 33935515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 33945515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 33955515061dSMel Gorman * 33965515061dSMel Gorman * Returns true if kswapd is ready to sleep 33975515061dSMel Gorman */ 3398d9f21d42SMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx) 3399f50de2d3SMel Gorman { 34005515061dSMel Gorman /* 34019e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 3402c73322d0SJohannes Weiner * soon as allow_direct_reclaim() is true. But there is a potential 34039e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 34049e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 34059e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 34069e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 34079e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 34089e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 34099e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 34109e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 34119e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 34125515061dSMel Gorman */ 34139e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 34149e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 3415f50de2d3SMel Gorman 3416c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3417c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3418c73322d0SJohannes Weiner return true; 3419c73322d0SJohannes Weiner 3420e716f2ebSMel Gorman if (pgdat_balanced(pgdat, order, classzone_idx)) { 3421631b6e08SMel Gorman clear_pgdat_congested(pgdat); 3422333b0a45SShantanu Goel return true; 34231d82de61SMel Gorman } 34241d82de61SMel Gorman 3425333b0a45SShantanu Goel return false; 3426f50de2d3SMel Gorman } 3427f50de2d3SMel Gorman 34281da177e4SLinus Torvalds /* 34291d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 34301d82de61SMel Gorman * zone that is currently unbalanced. 3431b8e83b94SMel Gorman * 3432b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 3433283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 3434283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 343575485363SMel Gorman */ 34361d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 3437accf6242SVlastimil Babka struct scan_control *sc) 343875485363SMel Gorman { 34391d82de61SMel Gorman struct zone *zone; 34401d82de61SMel Gorman int z; 344175485363SMel Gorman 34421d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 34431d82de61SMel Gorman sc->nr_to_reclaim = 0; 3444970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 34451d82de61SMel Gorman zone = pgdat->node_zones + z; 34466aa303deSMel Gorman if (!managed_zone(zone)) 34471d82de61SMel Gorman continue; 34487c954f6dSMel Gorman 34491d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 34507c954f6dSMel Gorman } 34517c954f6dSMel Gorman 34521d82de61SMel Gorman /* 34531d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 34541d82de61SMel Gorman * now pressure is applied based on node LRU order. 34551d82de61SMel Gorman */ 3456970a39a3SMel Gorman shrink_node(pgdat, sc); 34571d82de61SMel Gorman 34581d82de61SMel Gorman /* 34591d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 34601d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 34611d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 34621d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 34631d82de61SMel Gorman * can direct reclaim/compact. 34641d82de61SMel Gorman */ 34659861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 34661d82de61SMel Gorman sc->order = 0; 34671d82de61SMel Gorman 3468b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 346975485363SMel Gorman } 347075485363SMel Gorman 347175485363SMel Gorman /* 34721d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 34731d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 34741d82de61SMel Gorman * balanced. 34751da177e4SLinus Torvalds * 34761d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 34771da177e4SLinus Torvalds * 34781da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 347941858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 34801d82de61SMel Gorman * found to have free_pages <= high_wmark_pages(zone), any page is that zone 34811d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 34821d82de61SMel Gorman * balanced. 34831da177e4SLinus Torvalds */ 3484accf6242SVlastimil Babka static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx) 34851da177e4SLinus Torvalds { 34861da177e4SLinus Torvalds int i; 34870608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 34880608f43dSAndrew Morton unsigned long nr_soft_scanned; 34891d82de61SMel Gorman struct zone *zone; 3490179e9639SAndrew Morton struct scan_control sc = { 3491179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 3492ee814fe2SJohannes Weiner .order = order, 3493b8e83b94SMel Gorman .priority = DEF_PRIORITY, 3494ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3495a6dc60f8SJohannes Weiner .may_unmap = 1, 34962e2e4259SKOSAKI Motohiro .may_swap = 1, 3497179e9639SAndrew Morton }; 349893781325SOmar Sandoval 349993781325SOmar Sandoval __fs_reclaim_acquire(); 350093781325SOmar Sandoval 3501f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 35021da177e4SLinus Torvalds 35039e3b2f8cSKonstantin Khlebnikov do { 3504c73322d0SJohannes Weiner unsigned long nr_reclaimed = sc.nr_reclaimed; 3505b8e83b94SMel Gorman bool raise_priority = true; 350693781325SOmar Sandoval bool ret; 3507b8e83b94SMel Gorman 350884c7a777SMel Gorman sc.reclaim_idx = classzone_idx; 35091da177e4SLinus Torvalds 351086c79f6bSMel Gorman /* 351184c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 351284c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 351384c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 351484c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 351584c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 351684c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 351784c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 351884c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 351986c79f6bSMel Gorman */ 352086c79f6bSMel Gorman if (buffer_heads_over_limit) { 352186c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 352286c79f6bSMel Gorman zone = pgdat->node_zones + i; 35236aa303deSMel Gorman if (!managed_zone(zone)) 352486c79f6bSMel Gorman continue; 352586c79f6bSMel Gorman 3526970a39a3SMel Gorman sc.reclaim_idx = i; 352786c79f6bSMel Gorman break; 352886c79f6bSMel Gorman } 352986c79f6bSMel Gorman } 353086c79f6bSMel Gorman 353186c79f6bSMel Gorman /* 3532e716f2ebSMel Gorman * Only reclaim if there are no eligible zones. Note that 3533e716f2ebSMel Gorman * sc.reclaim_idx is not used as buffer_heads_over_limit may 3534e716f2ebSMel Gorman * have adjusted it. 353586c79f6bSMel Gorman */ 3536e716f2ebSMel Gorman if (pgdat_balanced(pgdat, sc.order, classzone_idx)) 35371da177e4SLinus Torvalds goto out; 3538e1dbeda6SAndrew Morton 35391da177e4SLinus Torvalds /* 35401d82de61SMel Gorman * Do some background aging of the anon list, to give 35411d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 35421d82de61SMel Gorman * pages are rotated regardless of classzone as this is 35431d82de61SMel Gorman * about consistent aging. 35441d82de61SMel Gorman */ 3545ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 35461d82de61SMel Gorman 35471d82de61SMel Gorman /* 3548b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 3549b7ea3c41SMel Gorman * even in laptop mode. 3550b7ea3c41SMel Gorman */ 3551047d72c3SJohannes Weiner if (sc.priority < DEF_PRIORITY - 2) 3552b7ea3c41SMel Gorman sc.may_writepage = 1; 3553b7ea3c41SMel Gorman 35541d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 35551da177e4SLinus Torvalds sc.nr_scanned = 0; 35560608f43dSAndrew Morton nr_soft_scanned = 0; 3557ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 35581d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 35590608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 35600608f43dSAndrew Morton 356132a4330dSRik van Riel /* 35621d82de61SMel Gorman * There should be no need to raise the scanning priority if 35631d82de61SMel Gorman * enough pages are already being scanned that that high 35641d82de61SMel Gorman * watermark would be met at 100% efficiency. 356532a4330dSRik van Riel */ 3566970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 3567b8e83b94SMel Gorman raise_priority = false; 3568d7868daeSMel Gorman 35695515061dSMel Gorman /* 35705515061dSMel Gorman * If the low watermark is met there is no need for processes 35715515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 35725515061dSMel Gorman * able to safely make forward progress. Wake them 35735515061dSMel Gorman */ 35745515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 3575c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)) 3576cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 35775515061dSMel Gorman 3578b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 357993781325SOmar Sandoval __fs_reclaim_release(); 358093781325SOmar Sandoval ret = try_to_freeze(); 358193781325SOmar Sandoval __fs_reclaim_acquire(); 358293781325SOmar Sandoval if (ret || kthread_should_stop()) 3583b8e83b94SMel Gorman break; 3584b8e83b94SMel Gorman 3585b8e83b94SMel Gorman /* 3586b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3587b8e83b94SMel Gorman * progress in reclaiming pages 3588b8e83b94SMel Gorman */ 3589c73322d0SJohannes Weiner nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 3590c73322d0SJohannes Weiner if (raise_priority || !nr_reclaimed) 3591b8e83b94SMel Gorman sc.priority--; 35921d82de61SMel Gorman } while (sc.priority >= 1); 35931da177e4SLinus Torvalds 3594c73322d0SJohannes Weiner if (!sc.nr_reclaimed) 3595c73322d0SJohannes Weiner pgdat->kswapd_failures++; 3596c73322d0SJohannes Weiner 3597b8e83b94SMel Gorman out: 35982a2e4885SJohannes Weiner snapshot_refaults(NULL, pgdat); 359993781325SOmar Sandoval __fs_reclaim_release(); 36000abdee2bSMel Gorman /* 36011d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 36021d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 36031d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 36041d82de61SMel Gorman * remain at the higher level. 36050abdee2bSMel Gorman */ 36061d82de61SMel Gorman return sc.order; 36071da177e4SLinus Torvalds } 36081da177e4SLinus Torvalds 3609e716f2ebSMel Gorman /* 3610e716f2ebSMel Gorman * pgdat->kswapd_classzone_idx is the highest zone index that a recent 3611e716f2ebSMel Gorman * allocation request woke kswapd for. When kswapd has not woken recently, 3612e716f2ebSMel Gorman * the value is MAX_NR_ZONES which is not a valid index. This compares a 3613e716f2ebSMel Gorman * given classzone and returns it or the highest classzone index kswapd 3614e716f2ebSMel Gorman * was recently woke for. 3615e716f2ebSMel Gorman */ 3616e716f2ebSMel Gorman static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat, 3617e716f2ebSMel Gorman enum zone_type classzone_idx) 3618e716f2ebSMel Gorman { 3619e716f2ebSMel Gorman if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES) 3620e716f2ebSMel Gorman return classzone_idx; 3621e716f2ebSMel Gorman 3622e716f2ebSMel Gorman return max(pgdat->kswapd_classzone_idx, classzone_idx); 3623e716f2ebSMel Gorman } 3624e716f2ebSMel Gorman 362538087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 362638087d9bSMel Gorman unsigned int classzone_idx) 3627f0bc0a60SKOSAKI Motohiro { 3628f0bc0a60SKOSAKI Motohiro long remaining = 0; 3629f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3630f0bc0a60SKOSAKI Motohiro 3631f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3632f0bc0a60SKOSAKI Motohiro return; 3633f0bc0a60SKOSAKI Motohiro 3634f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3635f0bc0a60SKOSAKI Motohiro 3636333b0a45SShantanu Goel /* 3637333b0a45SShantanu Goel * Try to sleep for a short interval. Note that kcompactd will only be 3638333b0a45SShantanu Goel * woken if it is possible to sleep for a short interval. This is 3639333b0a45SShantanu Goel * deliberate on the assumption that if reclaim cannot keep an 3640333b0a45SShantanu Goel * eligible zone balanced that it's also unlikely that compaction will 3641333b0a45SShantanu Goel * succeed. 3642333b0a45SShantanu Goel */ 3643d9f21d42SMel Gorman if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3644fd901c95SVlastimil Babka /* 3645fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 3646fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 3647fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 3648fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 3649fd901c95SVlastimil Babka */ 3650fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 3651fd901c95SVlastimil Babka 3652fd901c95SVlastimil Babka /* 3653fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 3654fd901c95SVlastimil Babka * allocation of the requested order possible. 3655fd901c95SVlastimil Babka */ 365638087d9bSMel Gorman wakeup_kcompactd(pgdat, alloc_order, classzone_idx); 3657fd901c95SVlastimil Babka 3658f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 365938087d9bSMel Gorman 366038087d9bSMel Gorman /* 366138087d9bSMel Gorman * If woken prematurely then reset kswapd_classzone_idx and 366238087d9bSMel Gorman * order. The values will either be from a wakeup request or 366338087d9bSMel Gorman * the previous request that slept prematurely. 366438087d9bSMel Gorman */ 366538087d9bSMel Gorman if (remaining) { 3666e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 366738087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, reclaim_order); 366838087d9bSMel Gorman } 366938087d9bSMel Gorman 3670f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3671f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3672f0bc0a60SKOSAKI Motohiro } 3673f0bc0a60SKOSAKI Motohiro 3674f0bc0a60SKOSAKI Motohiro /* 3675f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3676f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3677f0bc0a60SKOSAKI Motohiro */ 3678d9f21d42SMel Gorman if (!remaining && 3679d9f21d42SMel Gorman prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3680f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3681f0bc0a60SKOSAKI Motohiro 3682f0bc0a60SKOSAKI Motohiro /* 3683f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3684f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3685f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3686f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3687f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3688f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3689f0bc0a60SKOSAKI Motohiro */ 3690f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 36911c7e7f6cSAaditya Kumar 36921c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3693f0bc0a60SKOSAKI Motohiro schedule(); 36941c7e7f6cSAaditya Kumar 3695f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3696f0bc0a60SKOSAKI Motohiro } else { 3697f0bc0a60SKOSAKI Motohiro if (remaining) 3698f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3699f0bc0a60SKOSAKI Motohiro else 3700f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3701f0bc0a60SKOSAKI Motohiro } 3702f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3703f0bc0a60SKOSAKI Motohiro } 3704f0bc0a60SKOSAKI Motohiro 37051da177e4SLinus Torvalds /* 37061da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 37071da177e4SLinus Torvalds * from the init process. 37081da177e4SLinus Torvalds * 37091da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 37101da177e4SLinus Torvalds * free memory available even if there is no other activity 37111da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 37121da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 37131da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 37141da177e4SLinus Torvalds * 37151da177e4SLinus Torvalds * If there are applications that are active memory-allocators 37161da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 37171da177e4SLinus Torvalds */ 37181da177e4SLinus Torvalds static int kswapd(void *p) 37191da177e4SLinus Torvalds { 3720e716f2ebSMel Gorman unsigned int alloc_order, reclaim_order; 3721e716f2ebSMel Gorman unsigned int classzone_idx = MAX_NR_ZONES - 1; 37221da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 37231da177e4SLinus Torvalds struct task_struct *tsk = current; 3724f0bc0a60SKOSAKI Motohiro 37251da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 37261da177e4SLinus Torvalds .reclaimed_slab = 0, 37271da177e4SLinus Torvalds }; 3728a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 37291da177e4SLinus Torvalds 3730174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3731c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 37321da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 37331da177e4SLinus Torvalds 37341da177e4SLinus Torvalds /* 37351da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 37361da177e4SLinus Torvalds * and that if we need more memory we should get access to it 37371da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 37381da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 37391da177e4SLinus Torvalds * 37401da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 37411da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 37421da177e4SLinus Torvalds * page out something else, and this flag essentially protects 37431da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 37441da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 37451da177e4SLinus Torvalds */ 3746930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 374783144186SRafael J. Wysocki set_freezable(); 37481da177e4SLinus Torvalds 3749e716f2ebSMel Gorman pgdat->kswapd_order = 0; 3750e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 37511da177e4SLinus Torvalds for ( ; ; ) { 37526f6313d4SJeff Liu bool ret; 37533e1d1d28SChristoph Lameter 3754e716f2ebSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3755e716f2ebSMel Gorman classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 3756e716f2ebSMel Gorman 375738087d9bSMel Gorman kswapd_try_sleep: 375838087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 375938087d9bSMel Gorman classzone_idx); 3760215ddd66SMel Gorman 376138087d9bSMel Gorman /* Read the new order and classzone_idx */ 376238087d9bSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3763e716f2ebSMel Gorman classzone_idx = kswapd_classzone_idx(pgdat, 0); 376438087d9bSMel Gorman pgdat->kswapd_order = 0; 3765e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 37661da177e4SLinus Torvalds 37678fe23e05SDavid Rientjes ret = try_to_freeze(); 37688fe23e05SDavid Rientjes if (kthread_should_stop()) 37698fe23e05SDavid Rientjes break; 37708fe23e05SDavid Rientjes 37718fe23e05SDavid Rientjes /* 37728fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 37738fe23e05SDavid Rientjes * after returning from the refrigerator 3774b1296cc4SRafael J. Wysocki */ 377538087d9bSMel Gorman if (ret) 377638087d9bSMel Gorman continue; 37771d82de61SMel Gorman 377838087d9bSMel Gorman /* 377938087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 378038087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 378138087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 378238087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 378338087d9bSMel Gorman * but kcompactd is woken to compact for the original 378438087d9bSMel Gorman * request (alloc_order). 378538087d9bSMel Gorman */ 3786e5146b12SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx, 3787e5146b12SMel Gorman alloc_order); 378838087d9bSMel Gorman reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx); 378938087d9bSMel Gorman if (reclaim_order < alloc_order) 379038087d9bSMel Gorman goto kswapd_try_sleep; 379133906bc5SMel Gorman } 3792b0a8cc58STakamori Yamaguchi 379371abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 3794b0a8cc58STakamori Yamaguchi current->reclaim_state = NULL; 379571abdc15SJohannes Weiner 37961da177e4SLinus Torvalds return 0; 37971da177e4SLinus Torvalds } 37981da177e4SLinus Torvalds 37991da177e4SLinus Torvalds /* 38005ecd9d40SDavid Rientjes * A zone is low on free memory or too fragmented for high-order memory. If 38015ecd9d40SDavid Rientjes * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's 38025ecd9d40SDavid Rientjes * pgdat. It will wake up kcompactd after reclaiming memory. If kswapd reclaim 38035ecd9d40SDavid Rientjes * has failed or is not needed, still wake up kcompactd if only compaction is 38045ecd9d40SDavid Rientjes * needed. 38051da177e4SLinus Torvalds */ 38065ecd9d40SDavid Rientjes void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, 38075ecd9d40SDavid Rientjes enum zone_type classzone_idx) 38081da177e4SLinus Torvalds { 38091da177e4SLinus Torvalds pg_data_t *pgdat; 38101da177e4SLinus Torvalds 38116aa303deSMel Gorman if (!managed_zone(zone)) 38121da177e4SLinus Torvalds return; 38131da177e4SLinus Torvalds 38145ecd9d40SDavid Rientjes if (!cpuset_zone_allowed(zone, gfp_flags)) 38151da177e4SLinus Torvalds return; 381688f5acf8SMel Gorman pgdat = zone->zone_pgdat; 3817e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, 3818e716f2ebSMel Gorman classzone_idx); 381938087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, order); 38208d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 38211da177e4SLinus Torvalds return; 3822e1a55637SMel Gorman 38235ecd9d40SDavid Rientjes /* Hopeless node, leave it to direct reclaim if possible */ 38245ecd9d40SDavid Rientjes if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES || 38255ecd9d40SDavid Rientjes pgdat_balanced(pgdat, order, classzone_idx)) { 38265ecd9d40SDavid Rientjes /* 38275ecd9d40SDavid Rientjes * There may be plenty of free memory available, but it's too 38285ecd9d40SDavid Rientjes * fragmented for high-order allocations. Wake up kcompactd 38295ecd9d40SDavid Rientjes * and rely on compaction_suitable() to determine if it's 38305ecd9d40SDavid Rientjes * needed. If it fails, it will defer subsequent attempts to 38315ecd9d40SDavid Rientjes * ratelimit its work. 38325ecd9d40SDavid Rientjes */ 38335ecd9d40SDavid Rientjes if (!(gfp_flags & __GFP_DIRECT_RECLAIM)) 38345ecd9d40SDavid Rientjes wakeup_kcompactd(pgdat, order, classzone_idx); 3835c73322d0SJohannes Weiner return; 38365ecd9d40SDavid Rientjes } 3837c73322d0SJohannes Weiner 38385ecd9d40SDavid Rientjes trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, classzone_idx, order, 38395ecd9d40SDavid Rientjes gfp_flags); 38408d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 38411da177e4SLinus Torvalds } 38421da177e4SLinus Torvalds 3843c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 38441da177e4SLinus Torvalds /* 38457b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3846d6277db4SRafael J. Wysocki * freed pages. 3847d6277db4SRafael J. Wysocki * 3848d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3849d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3850d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 38511da177e4SLinus Torvalds */ 38527b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 38531da177e4SLinus Torvalds { 3854d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 3855d6277db4SRafael J. Wysocki struct scan_control sc = { 38567b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 3857ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 3858b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 38599e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 3860ee814fe2SJohannes Weiner .may_writepage = 1, 3861ee814fe2SJohannes Weiner .may_unmap = 1, 3862ee814fe2SJohannes Weiner .may_swap = 1, 3863ee814fe2SJohannes Weiner .hibernation_mode = 1, 38641da177e4SLinus Torvalds }; 38657b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 38667b51755cSKOSAKI Motohiro struct task_struct *p = current; 38677b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 3868499118e9SVlastimil Babka unsigned int noreclaim_flag; 38691da177e4SLinus Torvalds 3870d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 387193781325SOmar Sandoval noreclaim_flag = memalloc_noreclaim_save(); 3872d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 38737b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 3874d6277db4SRafael J. Wysocki 38753115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3876d6277db4SRafael J. Wysocki 38777b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 3878499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 387993781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 3880d6277db4SRafael J. Wysocki 38817b51755cSKOSAKI Motohiro return nr_reclaimed; 38821da177e4SLinus Torvalds } 3883c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 38841da177e4SLinus Torvalds 38851da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 38861da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 38871da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 38881da177e4SLinus Torvalds restore their cpu bindings. */ 3889517bbed9SSebastian Andrzej Siewior static int kswapd_cpu_online(unsigned int cpu) 38901da177e4SLinus Torvalds { 389158c0a4a7SYasunori Goto int nid; 38921da177e4SLinus Torvalds 389348fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 3894c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3895a70f7302SRusty Russell const struct cpumask *mask; 3896a70f7302SRusty Russell 3897a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3898c5f59f08SMike Travis 38993e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 39001da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3901c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 39021da177e4SLinus Torvalds } 3903517bbed9SSebastian Andrzej Siewior return 0; 39041da177e4SLinus Torvalds } 39051da177e4SLinus Torvalds 39063218ae14SYasunori Goto /* 39073218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 39083218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 39093218ae14SYasunori Goto */ 39103218ae14SYasunori Goto int kswapd_run(int nid) 39113218ae14SYasunori Goto { 39123218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 39133218ae14SYasunori Goto int ret = 0; 39143218ae14SYasunori Goto 39153218ae14SYasunori Goto if (pgdat->kswapd) 39163218ae14SYasunori Goto return 0; 39173218ae14SYasunori Goto 39183218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 39193218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 39203218ae14SYasunori Goto /* failure at boot is fatal */ 3921c6202adfSThomas Gleixner BUG_ON(system_state < SYSTEM_RUNNING); 3922d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 3923d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 3924d72515b8SXishi Qiu pgdat->kswapd = NULL; 39253218ae14SYasunori Goto } 39263218ae14SYasunori Goto return ret; 39273218ae14SYasunori Goto } 39283218ae14SYasunori Goto 39298fe23e05SDavid Rientjes /* 3930d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 3931bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 39328fe23e05SDavid Rientjes */ 39338fe23e05SDavid Rientjes void kswapd_stop(int nid) 39348fe23e05SDavid Rientjes { 39358fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 39368fe23e05SDavid Rientjes 3937d8adde17SJiang Liu if (kswapd) { 39388fe23e05SDavid Rientjes kthread_stop(kswapd); 3939d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 3940d8adde17SJiang Liu } 39418fe23e05SDavid Rientjes } 39428fe23e05SDavid Rientjes 39431da177e4SLinus Torvalds static int __init kswapd_init(void) 39441da177e4SLinus Torvalds { 3945517bbed9SSebastian Andrzej Siewior int nid, ret; 394669e05944SAndrew Morton 39471da177e4SLinus Torvalds swap_setup(); 394848fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 39493218ae14SYasunori Goto kswapd_run(nid); 3950517bbed9SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 3951517bbed9SSebastian Andrzej Siewior "mm/vmscan:online", kswapd_cpu_online, 3952517bbed9SSebastian Andrzej Siewior NULL); 3953517bbed9SSebastian Andrzej Siewior WARN_ON(ret < 0); 39541da177e4SLinus Torvalds return 0; 39551da177e4SLinus Torvalds } 39561da177e4SLinus Torvalds 39571da177e4SLinus Torvalds module_init(kswapd_init) 39589eeff239SChristoph Lameter 39599eeff239SChristoph Lameter #ifdef CONFIG_NUMA 39609eeff239SChristoph Lameter /* 3961a5f5f91dSMel Gorman * Node reclaim mode 39629eeff239SChristoph Lameter * 3963a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 39649eeff239SChristoph Lameter * the watermarks. 39659eeff239SChristoph Lameter */ 3966a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 39679eeff239SChristoph Lameter 39681b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 39697d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 39701b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 397195bbc0c7SZhihui Zhang #define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */ 39721b2ffb78SChristoph Lameter 39739eeff239SChristoph Lameter /* 3974a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 3975a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3976a92f7126SChristoph Lameter * a zone. 3977a92f7126SChristoph Lameter */ 3978a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 3979a92f7126SChristoph Lameter 39809eeff239SChristoph Lameter /* 3981a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 39829614634fSChristoph Lameter * occur. 39839614634fSChristoph Lameter */ 39849614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 39859614634fSChristoph Lameter 39869614634fSChristoph Lameter /* 39870ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 39880ff38490SChristoph Lameter * slab reclaim needs to occur. 39890ff38490SChristoph Lameter */ 39900ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 39910ff38490SChristoph Lameter 399211fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 399390afa5deSMel Gorman { 399411fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 399511fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 399611fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 399790afa5deSMel Gorman 399890afa5deSMel Gorman /* 399990afa5deSMel Gorman * It's possible for there to be more file mapped pages than 400090afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 400190afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 400290afa5deSMel Gorman */ 400390afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 400490afa5deSMel Gorman } 400590afa5deSMel Gorman 400690afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 4007a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 400890afa5deSMel Gorman { 4009d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 4010d031a157SAlexandru Moise unsigned long delta = 0; 401190afa5deSMel Gorman 401290afa5deSMel Gorman /* 401395bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 401490afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 401511fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 401690afa5deSMel Gorman * a better estimate 401790afa5deSMel Gorman */ 4018a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 4019a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 402090afa5deSMel Gorman else 4021a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 402290afa5deSMel Gorman 402390afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 4024a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 4025a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 402690afa5deSMel Gorman 402790afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 402890afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 402990afa5deSMel Gorman delta = nr_pagecache_reclaimable; 403090afa5deSMel Gorman 403190afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 403290afa5deSMel Gorman } 403390afa5deSMel Gorman 40340ff38490SChristoph Lameter /* 4035a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 40369eeff239SChristoph Lameter */ 4037a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 40389eeff239SChristoph Lameter { 40397fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 404069e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 40419eeff239SChristoph Lameter struct task_struct *p = current; 40429eeff239SChristoph Lameter struct reclaim_state reclaim_state; 4043499118e9SVlastimil Babka unsigned int noreclaim_flag; 4044179e9639SAndrew Morton struct scan_control sc = { 404562b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 4046f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 4047bd2f6199SJohannes Weiner .order = order, 4048a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 4049a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 4050a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 4051ee814fe2SJohannes Weiner .may_swap = 1, 4052f2f43e56SNick Desaulniers .reclaim_idx = gfp_zone(gfp_mask), 4053179e9639SAndrew Morton }; 40549eeff239SChristoph Lameter 40559eeff239SChristoph Lameter cond_resched(); 405693781325SOmar Sandoval fs_reclaim_acquire(sc.gfp_mask); 4057d4f7796eSChristoph Lameter /* 405895bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 4059d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 406095bbc0c7SZhihui Zhang * and RECLAIM_UNMAP. 4061d4f7796eSChristoph Lameter */ 4062499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 4063499118e9SVlastimil Babka p->flags |= PF_SWAPWRITE; 40649eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 40659eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 4066c84db23cSChristoph Lameter 4067a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 4068a92f7126SChristoph Lameter /* 4069894befecSAndrey Ryabinin * Free memory by calling shrink node with increasing 40700ff38490SChristoph Lameter * priorities until we have enough memory freed. 4071a92f7126SChristoph Lameter */ 4072a92f7126SChristoph Lameter do { 4073970a39a3SMel Gorman shrink_node(pgdat, &sc); 40749e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 40750ff38490SChristoph Lameter } 4076a92f7126SChristoph Lameter 40779eeff239SChristoph Lameter p->reclaim_state = NULL; 4078499118e9SVlastimil Babka current->flags &= ~PF_SWAPWRITE; 4079499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 408093781325SOmar Sandoval fs_reclaim_release(sc.gfp_mask); 4081a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 40829eeff239SChristoph Lameter } 4083179e9639SAndrew Morton 4084a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 4085179e9639SAndrew Morton { 4086d773ed6bSDavid Rientjes int ret; 4087179e9639SAndrew Morton 4088179e9639SAndrew Morton /* 4089a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 40900ff38490SChristoph Lameter * slab pages if we are over the defined limits. 409134aa1330SChristoph Lameter * 40929614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 40939614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 4094a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 4095a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 40969614634fSChristoph Lameter * unmapped file backed pages. 4097179e9639SAndrew Morton */ 4098a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 4099385386cfSJohannes Weiner node_page_state(pgdat, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages) 4100a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 4101179e9639SAndrew Morton 4102179e9639SAndrew Morton /* 4103d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 4104179e9639SAndrew Morton */ 4105d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 4106a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4107179e9639SAndrew Morton 4108179e9639SAndrew Morton /* 4109a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 4110179e9639SAndrew Morton * have associated processors. This will favor the local processor 4111179e9639SAndrew Morton * over remote processors and spread off node memory allocations 4112179e9639SAndrew Morton * as wide as possible. 4113179e9639SAndrew Morton */ 4114a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 4115a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4116d773ed6bSDavid Rientjes 4117a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 4118a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 4119fa5e084eSMel Gorman 4120a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 4121a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 4122d773ed6bSDavid Rientjes 412324cf7251SMel Gorman if (!ret) 412424cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 412524cf7251SMel Gorman 4126d773ed6bSDavid Rientjes return ret; 4127179e9639SAndrew Morton } 41289eeff239SChristoph Lameter #endif 4129894bc310SLee Schermerhorn 4130894bc310SLee Schermerhorn /* 4131894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 4132894bc310SLee Schermerhorn * @page: the page to test 4133894bc310SLee Schermerhorn * 4134894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 413539b5f29aSHugh Dickins * lists vs unevictable list. 4136894bc310SLee Schermerhorn * 4137894bc310SLee Schermerhorn * Reasons page might not be evictable: 4138ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 4139b291f000SNick Piggin * (2) page is part of an mlocked VMA 4140ba9ddf49SLee Schermerhorn * 4141894bc310SLee Schermerhorn */ 414239b5f29aSHugh Dickins int page_evictable(struct page *page) 4143894bc310SLee Schermerhorn { 4144e92bb4ddSHuang Ying int ret; 4145e92bb4ddSHuang Ying 4146e92bb4ddSHuang Ying /* Prevent address_space of inode and swap cache from being freed */ 4147e92bb4ddSHuang Ying rcu_read_lock(); 4148e92bb4ddSHuang Ying ret = !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); 4149e92bb4ddSHuang Ying rcu_read_unlock(); 4150e92bb4ddSHuang Ying return ret; 4151894bc310SLee Schermerhorn } 415289e004eaSLee Schermerhorn 415385046579SHugh Dickins #ifdef CONFIG_SHMEM 415489e004eaSLee Schermerhorn /** 415524513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 415624513264SHugh Dickins * @pages: array of pages to check 415724513264SHugh Dickins * @nr_pages: number of pages to check 415889e004eaSLee Schermerhorn * 415924513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 416085046579SHugh Dickins * 416185046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 416289e004eaSLee Schermerhorn */ 416324513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 416489e004eaSLee Schermerhorn { 4165925b7673SJohannes Weiner struct lruvec *lruvec; 4166785b99feSMel Gorman struct pglist_data *pgdat = NULL; 416724513264SHugh Dickins int pgscanned = 0; 416824513264SHugh Dickins int pgrescued = 0; 416989e004eaSLee Schermerhorn int i; 417089e004eaSLee Schermerhorn 417124513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 417224513264SHugh Dickins struct page *page = pages[i]; 4173785b99feSMel Gorman struct pglist_data *pagepgdat = page_pgdat(page); 417489e004eaSLee Schermerhorn 417524513264SHugh Dickins pgscanned++; 4176785b99feSMel Gorman if (pagepgdat != pgdat) { 4177785b99feSMel Gorman if (pgdat) 4178785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 4179785b99feSMel Gorman pgdat = pagepgdat; 4180785b99feSMel Gorman spin_lock_irq(&pgdat->lru_lock); 418189e004eaSLee Schermerhorn } 4182785b99feSMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 418389e004eaSLee Schermerhorn 418424513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 418524513264SHugh Dickins continue; 418689e004eaSLee Schermerhorn 418739b5f29aSHugh Dickins if (page_evictable(page)) { 418824513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 418924513264SHugh Dickins 4190309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 419124513264SHugh Dickins ClearPageUnevictable(page); 4192fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 4193fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 419424513264SHugh Dickins pgrescued++; 419589e004eaSLee Schermerhorn } 419689e004eaSLee Schermerhorn } 419724513264SHugh Dickins 4198785b99feSMel Gorman if (pgdat) { 419924513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 420024513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 4201785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 420224513264SHugh Dickins } 420385046579SHugh Dickins } 420485046579SHugh Dickins #endif /* CONFIG_SHMEM */ 4205