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 681da177e4SLinus Torvalds /* This context's GFP mask */ 696daa0e28SAl Viro gfp_t gfp_mask; 701da177e4SLinus Torvalds 71ee814fe2SJohannes Weiner /* Allocation order */ 725ad333ebSAndy Whitcroft int order; 7366e1707bSBalbir Singh 74ee814fe2SJohannes Weiner /* 75ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 76ee814fe2SJohannes Weiner * are scanned. 77ee814fe2SJohannes Weiner */ 78ee814fe2SJohannes Weiner nodemask_t *nodemask; 799e3b2f8cSKonstantin Khlebnikov 805f53e762SKOSAKI Motohiro /* 81f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 82f16015fbSJohannes Weiner * primary target of this reclaim invocation. 83f16015fbSJohannes Weiner */ 84f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 8566e1707bSBalbir Singh 86ee814fe2SJohannes Weiner /* Scan (total_size >> priority) pages at once */ 87ee814fe2SJohannes Weiner int priority; 88ee814fe2SJohannes Weiner 89b2e18757SMel Gorman /* The highest zone to isolate pages for reclaim from */ 90b2e18757SMel Gorman enum zone_type reclaim_idx; 91b2e18757SMel Gorman 921276ad68SJohannes Weiner /* Writepage batching in laptop mode; RECLAIM_WRITE */ 93ee814fe2SJohannes Weiner unsigned int may_writepage:1; 94ee814fe2SJohannes Weiner 95ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 96ee814fe2SJohannes Weiner unsigned int may_unmap:1; 97ee814fe2SJohannes Weiner 98ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 99ee814fe2SJohannes Weiner unsigned int may_swap:1; 100ee814fe2SJohannes Weiner 101d6622f63SYisheng Xie /* 102d6622f63SYisheng Xie * Cgroups are not reclaimed below their configured memory.low, 103d6622f63SYisheng Xie * unless we threaten to OOM. If any cgroups are skipped due to 104d6622f63SYisheng Xie * memory.low and nothing was reclaimed, go back for memory.low. 105d6622f63SYisheng Xie */ 106d6622f63SYisheng Xie unsigned int memcg_low_reclaim:1; 107d6622f63SYisheng Xie unsigned int memcg_low_skipped:1; 108241994edSJohannes Weiner 109ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 110ee814fe2SJohannes Weiner 111ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 112ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 113ee814fe2SJohannes Weiner 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; 1191da177e4SLinus Torvalds }; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1221da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1231da177e4SLinus Torvalds do { \ 1241da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1251da177e4SLinus Torvalds struct page *prev; \ 1261da177e4SLinus Torvalds \ 1271da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1281da177e4SLinus Torvalds prefetch(&prev->_field); \ 1291da177e4SLinus Torvalds } \ 1301da177e4SLinus Torvalds } while (0) 1311da177e4SLinus Torvalds #else 1321da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1331da177e4SLinus Torvalds #endif 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1361da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1371da177e4SLinus Torvalds do { \ 1381da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1391da177e4SLinus Torvalds struct page *prev; \ 1401da177e4SLinus Torvalds \ 1411da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1421da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1431da177e4SLinus Torvalds } \ 1441da177e4SLinus Torvalds } while (0) 1451da177e4SLinus Torvalds #else 1461da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1471da177e4SLinus Torvalds #endif 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds /* 1501da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1511da177e4SLinus Torvalds */ 1521da177e4SLinus Torvalds int vm_swappiness = 60; 153d0480be4SWang Sheng-Hui /* 154d0480be4SWang Sheng-Hui * The total number of pages which are beyond the high watermark within all 155d0480be4SWang Sheng-Hui * zones. 156d0480be4SWang Sheng-Hui */ 157d0480be4SWang Sheng-Hui unsigned long vm_total_pages; 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1601da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1611da177e4SLinus Torvalds 162c255a458SAndrew Morton #ifdef CONFIG_MEMCG 16389b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 16489b5fae5SJohannes Weiner { 165f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 16689b5fae5SJohannes Weiner } 16797c9341fSTejun Heo 16897c9341fSTejun Heo /** 16997c9341fSTejun Heo * sane_reclaim - is the usual dirty throttling mechanism operational? 17097c9341fSTejun Heo * @sc: scan_control in question 17197c9341fSTejun Heo * 17297c9341fSTejun Heo * The normal page dirty throttling mechanism in balance_dirty_pages() is 17397c9341fSTejun Heo * completely broken with the legacy memcg and direct stalling in 17497c9341fSTejun Heo * shrink_page_list() is used for throttling instead, which lacks all the 17597c9341fSTejun Heo * niceties such as fairness, adaptive pausing, bandwidth proportional 17697c9341fSTejun Heo * allocation and configurability. 17797c9341fSTejun Heo * 17897c9341fSTejun Heo * This function tests whether the vmscan currently in progress can assume 17997c9341fSTejun Heo * that the normal dirty throttling mechanism is operational. 18097c9341fSTejun Heo */ 18197c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 18297c9341fSTejun Heo { 18397c9341fSTejun Heo struct mem_cgroup *memcg = sc->target_mem_cgroup; 18497c9341fSTejun Heo 18597c9341fSTejun Heo if (!memcg) 18697c9341fSTejun Heo return true; 18797c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 18869234aceSLinus Torvalds if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 18997c9341fSTejun Heo return true; 19097c9341fSTejun Heo #endif 19197c9341fSTejun Heo return false; 19297c9341fSTejun Heo } 19391a45470SKAMEZAWA Hiroyuki #else 19489b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 19589b5fae5SJohannes Weiner { 19689b5fae5SJohannes Weiner return true; 19789b5fae5SJohannes Weiner } 19897c9341fSTejun Heo 19997c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 20097c9341fSTejun Heo { 20197c9341fSTejun Heo return true; 20297c9341fSTejun Heo } 20391a45470SKAMEZAWA Hiroyuki #endif 20491a45470SKAMEZAWA Hiroyuki 2055a1c84b4SMel Gorman /* 2065a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 2075a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 2085a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 2095a1c84b4SMel Gorman */ 2105a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 2115a1c84b4SMel Gorman { 2125a1c84b4SMel Gorman unsigned long nr; 2135a1c84b4SMel Gorman 2145a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 2155a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 2165a1c84b4SMel Gorman if (get_nr_swap_pages() > 0) 2175a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 2185a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 2195a1c84b4SMel Gorman 2205a1c84b4SMel Gorman return nr; 2215a1c84b4SMel Gorman } 2225a1c84b4SMel Gorman 223599d0c95SMel Gorman unsigned long pgdat_reclaimable_pages(struct pglist_data *pgdat) 2246e543d57SLisa Du { 225599d0c95SMel Gorman unsigned long nr; 226599d0c95SMel Gorman 227599d0c95SMel Gorman nr = node_page_state_snapshot(pgdat, NR_ACTIVE_FILE) + 228599d0c95SMel Gorman node_page_state_snapshot(pgdat, NR_INACTIVE_FILE) + 229599d0c95SMel Gorman node_page_state_snapshot(pgdat, NR_ISOLATED_FILE); 230599d0c95SMel Gorman 231599d0c95SMel Gorman if (get_nr_swap_pages() > 0) 232599d0c95SMel Gorman nr += node_page_state_snapshot(pgdat, NR_ACTIVE_ANON) + 233599d0c95SMel Gorman node_page_state_snapshot(pgdat, NR_INACTIVE_ANON) + 234599d0c95SMel Gorman node_page_state_snapshot(pgdat, NR_ISOLATED_ANON); 235599d0c95SMel Gorman 236599d0c95SMel Gorman return nr; 237599d0c95SMel Gorman } 238599d0c95SMel Gorman 239fd538803SMichal Hocko /** 240fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 241fd538803SMichal Hocko * @lruvec: lru vector 242fd538803SMichal Hocko * @lru: lru to use 243fd538803SMichal Hocko * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 244fd538803SMichal Hocko */ 245fd538803SMichal Hocko unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx) 246c9f299d9SKOSAKI Motohiro { 247fd538803SMichal Hocko unsigned long lru_size; 248fd538803SMichal Hocko int zid; 249a3d8e054SKOSAKI Motohiro 250fd538803SMichal Hocko if (!mem_cgroup_disabled()) 251fd538803SMichal Hocko lru_size = mem_cgroup_get_lru_size(lruvec, lru); 252fd538803SMichal Hocko else 253fd538803SMichal Hocko lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru); 254fd538803SMichal Hocko 255fd538803SMichal Hocko for (zid = zone_idx + 1; zid < MAX_NR_ZONES; zid++) { 256fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 257fd538803SMichal Hocko unsigned long size; 258fd538803SMichal Hocko 259fd538803SMichal Hocko if (!managed_zone(zone)) 260fd538803SMichal Hocko continue; 261fd538803SMichal Hocko 262fd538803SMichal Hocko if (!mem_cgroup_disabled()) 263fd538803SMichal Hocko size = mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 264fd538803SMichal Hocko else 265fd538803SMichal Hocko size = zone_page_state(&lruvec_pgdat(lruvec)->node_zones[zid], 266fd538803SMichal Hocko NR_ZONE_LRU_BASE + lru); 267fd538803SMichal Hocko lru_size -= min(size, lru_size); 268c9f299d9SKOSAKI Motohiro } 269c9f299d9SKOSAKI Motohiro 270fd538803SMichal Hocko return lru_size; 271b4536f0cSMichal Hocko 272b4536f0cSMichal Hocko } 273b4536f0cSMichal Hocko 2741da177e4SLinus Torvalds /* 2751d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 2761da177e4SLinus Torvalds */ 2771d3d4437SGlauber Costa int register_shrinker(struct shrinker *shrinker) 2781da177e4SLinus Torvalds { 2791d3d4437SGlauber Costa size_t size = sizeof(*shrinker->nr_deferred); 2801d3d4437SGlauber Costa 2811d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 2821d3d4437SGlauber Costa size *= nr_node_ids; 2831d3d4437SGlauber Costa 2841d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 2851d3d4437SGlauber Costa if (!shrinker->nr_deferred) 2861d3d4437SGlauber Costa return -ENOMEM; 2871d3d4437SGlauber Costa 2881da177e4SLinus Torvalds down_write(&shrinker_rwsem); 2891da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 2901da177e4SLinus Torvalds up_write(&shrinker_rwsem); 2911d3d4437SGlauber Costa return 0; 2921da177e4SLinus Torvalds } 2938e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds /* 2961da177e4SLinus Torvalds * Remove one 2971da177e4SLinus Torvalds */ 2988e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds down_write(&shrinker_rwsem); 3011da177e4SLinus Torvalds list_del(&shrinker->list); 3021da177e4SLinus Torvalds up_write(&shrinker_rwsem); 303ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 3041da177e4SLinus Torvalds } 3058e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds #define SHRINK_BATCH 128 3081d3d4437SGlauber Costa 309cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 3106b4f7799SJohannes Weiner struct shrinker *shrinker, 3116b4f7799SJohannes Weiner unsigned long nr_scanned, 3126b4f7799SJohannes Weiner unsigned long nr_eligible) 3131da177e4SLinus Torvalds { 31424f7c6b9SDave Chinner unsigned long freed = 0; 3151da177e4SLinus Torvalds unsigned long long delta; 316635697c6SKonstantin Khlebnikov long total_scan; 317d5bc5fd3SVladimir Davydov long freeable; 318acf92b48SDave Chinner long nr; 319acf92b48SDave Chinner long new_nr; 3201d3d4437SGlauber Costa int nid = shrinkctl->nid; 321e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 322e9299f50SDave Chinner : SHRINK_BATCH; 3235f33a080SShaohua Li long scanned = 0, next_deferred; 3241da177e4SLinus Torvalds 325d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 326d5bc5fd3SVladimir Davydov if (freeable == 0) 3271d3d4437SGlauber Costa return 0; 328635697c6SKonstantin Khlebnikov 329acf92b48SDave Chinner /* 330acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 331acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 332acf92b48SDave Chinner * don't also do this scanning work. 333acf92b48SDave Chinner */ 3341d3d4437SGlauber Costa nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 335acf92b48SDave Chinner 336acf92b48SDave Chinner total_scan = nr; 3376b4f7799SJohannes Weiner delta = (4 * nr_scanned) / shrinker->seeks; 338d5bc5fd3SVladimir Davydov delta *= freeable; 3396b4f7799SJohannes Weiner do_div(delta, nr_eligible + 1); 340acf92b48SDave Chinner total_scan += delta; 341acf92b48SDave Chinner if (total_scan < 0) { 3428612c663SPintu Kumar pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n", 343a0b02131SDave Chinner shrinker->scan_objects, total_scan); 344d5bc5fd3SVladimir Davydov total_scan = freeable; 3455f33a080SShaohua Li next_deferred = nr; 3465f33a080SShaohua Li } else 3475f33a080SShaohua Li next_deferred = total_scan; 348ea164d73SAndrea Arcangeli 349ea164d73SAndrea Arcangeli /* 3503567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 3513567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 3523567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 3533567b59aSDave Chinner * nr being built up so when a shrink that can do some work 3543567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 355d5bc5fd3SVladimir Davydov * freeable. This is bad for sustaining a working set in 3563567b59aSDave Chinner * memory. 3573567b59aSDave Chinner * 3583567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 3593567b59aSDave Chinner * a large delta change is calculated directly. 3603567b59aSDave Chinner */ 361d5bc5fd3SVladimir Davydov if (delta < freeable / 4) 362d5bc5fd3SVladimir Davydov total_scan = min(total_scan, freeable / 2); 3633567b59aSDave Chinner 3643567b59aSDave Chinner /* 365ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 366ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 367ea164d73SAndrea Arcangeli * freeable entries. 368ea164d73SAndrea Arcangeli */ 369d5bc5fd3SVladimir Davydov if (total_scan > freeable * 2) 370d5bc5fd3SVladimir Davydov total_scan = freeable * 2; 3711da177e4SLinus Torvalds 37224f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 3736b4f7799SJohannes Weiner nr_scanned, nr_eligible, 374d5bc5fd3SVladimir Davydov freeable, delta, total_scan); 37509576073SDave Chinner 3760b1fb40aSVladimir Davydov /* 3770b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 3780b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 3790b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 3800b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 3810b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 3820b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 3830b1fb40aSVladimir Davydov * batch_size. 3840b1fb40aSVladimir Davydov * 3850b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 3860b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 387d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 3880b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 3890b1fb40aSVladimir Davydov * possible. 3900b1fb40aSVladimir Davydov */ 3910b1fb40aSVladimir Davydov while (total_scan >= batch_size || 392d5bc5fd3SVladimir Davydov total_scan >= freeable) { 39324f7c6b9SDave Chinner unsigned long ret; 3940b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 3951da177e4SLinus Torvalds 3960b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 397d460acb5SChris Wilson shrinkctl->nr_scanned = nr_to_scan; 39824f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 39924f7c6b9SDave Chinner if (ret == SHRINK_STOP) 4001da177e4SLinus Torvalds break; 40124f7c6b9SDave Chinner freed += ret; 40224f7c6b9SDave Chinner 403d460acb5SChris Wilson count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned); 404d460acb5SChris Wilson total_scan -= shrinkctl->nr_scanned; 405d460acb5SChris Wilson scanned += shrinkctl->nr_scanned; 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds cond_resched(); 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds 4105f33a080SShaohua Li if (next_deferred >= scanned) 4115f33a080SShaohua Li next_deferred -= scanned; 4125f33a080SShaohua Li else 4135f33a080SShaohua Li next_deferred = 0; 414acf92b48SDave Chinner /* 415acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 416acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 417acf92b48SDave Chinner * scan, there is no need to do an update. 418acf92b48SDave Chinner */ 4195f33a080SShaohua Li if (next_deferred > 0) 4205f33a080SShaohua Li new_nr = atomic_long_add_return(next_deferred, 4211d3d4437SGlauber Costa &shrinker->nr_deferred[nid]); 42283aeeadaSKonstantin Khlebnikov else 4231d3d4437SGlauber Costa new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 424acf92b48SDave Chinner 425df9024a8SDave Hansen trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan); 4261d3d4437SGlauber Costa return freed; 4271d3d4437SGlauber Costa } 4281d3d4437SGlauber Costa 4296b4f7799SJohannes Weiner /** 430cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 4316b4f7799SJohannes Weiner * @gfp_mask: allocation context 4326b4f7799SJohannes Weiner * @nid: node whose slab caches to target 433cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 4346b4f7799SJohannes Weiner * @nr_scanned: pressure numerator 4356b4f7799SJohannes Weiner * @nr_eligible: pressure denominator 4361d3d4437SGlauber Costa * 4376b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 4381d3d4437SGlauber Costa * 4396b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 4406b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 4411d3d4437SGlauber Costa * 442cb731d6cSVladimir Davydov * @memcg specifies the memory cgroup to target. If it is not NULL, 443cb731d6cSVladimir Davydov * only shrinkers with SHRINKER_MEMCG_AWARE set will be called to scan 4440fc9f58aSVladimir Davydov * objects from the memory cgroup specified. Otherwise, only unaware 4450fc9f58aSVladimir Davydov * shrinkers are called. 446cb731d6cSVladimir Davydov * 4476b4f7799SJohannes Weiner * @nr_scanned and @nr_eligible form a ratio that indicate how much of 4486b4f7799SJohannes Weiner * the available objects should be scanned. Page reclaim for example 4496b4f7799SJohannes Weiner * passes the number of pages scanned and the number of pages on the 4506b4f7799SJohannes Weiner * LRU lists that it considered on @nid, plus a bias in @nr_scanned 4516b4f7799SJohannes Weiner * when it encountered mapped pages. The ratio is further biased by 4526b4f7799SJohannes Weiner * the ->seeks setting of the shrink function, which indicates the 4536b4f7799SJohannes Weiner * cost to recreate an object relative to that of an LRU page. 4541d3d4437SGlauber Costa * 4556b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 4561d3d4437SGlauber Costa */ 457cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 458cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 4596b4f7799SJohannes Weiner unsigned long nr_scanned, 4606b4f7799SJohannes Weiner unsigned long nr_eligible) 4611d3d4437SGlauber Costa { 4621d3d4437SGlauber Costa struct shrinker *shrinker; 4631d3d4437SGlauber Costa unsigned long freed = 0; 4641d3d4437SGlauber Costa 4650fc9f58aSVladimir Davydov if (memcg && (!memcg_kmem_enabled() || !mem_cgroup_online(memcg))) 466cb731d6cSVladimir Davydov return 0; 467cb731d6cSVladimir Davydov 4686b4f7799SJohannes Weiner if (nr_scanned == 0) 4696b4f7799SJohannes Weiner nr_scanned = SWAP_CLUSTER_MAX; 4701d3d4437SGlauber Costa 4711d3d4437SGlauber Costa if (!down_read_trylock(&shrinker_rwsem)) { 4721d3d4437SGlauber Costa /* 4731d3d4437SGlauber Costa * If we would return 0, our callers would understand that we 4741d3d4437SGlauber Costa * have nothing else to shrink and give up trying. By returning 4751d3d4437SGlauber Costa * 1 we keep it going and assume we'll be able to shrink next 4761d3d4437SGlauber Costa * time. 4771d3d4437SGlauber Costa */ 4781d3d4437SGlauber Costa freed = 1; 4791d3d4437SGlauber Costa goto out; 4801d3d4437SGlauber Costa } 4811d3d4437SGlauber Costa 4821d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 4836b4f7799SJohannes Weiner struct shrink_control sc = { 4846b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 4856b4f7799SJohannes Weiner .nid = nid, 486cb731d6cSVladimir Davydov .memcg = memcg, 4876b4f7799SJohannes Weiner }; 4886b4f7799SJohannes Weiner 4890fc9f58aSVladimir Davydov /* 4900fc9f58aSVladimir Davydov * If kernel memory accounting is disabled, we ignore 4910fc9f58aSVladimir Davydov * SHRINKER_MEMCG_AWARE flag and call all shrinkers 4920fc9f58aSVladimir Davydov * passing NULL for memcg. 4930fc9f58aSVladimir Davydov */ 4940fc9f58aSVladimir Davydov if (memcg_kmem_enabled() && 4950fc9f58aSVladimir Davydov !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE)) 496cb731d6cSVladimir Davydov continue; 497cb731d6cSVladimir Davydov 4986b4f7799SJohannes Weiner if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 4996b4f7799SJohannes Weiner sc.nid = 0; 5006b4f7799SJohannes Weiner 501cb731d6cSVladimir Davydov freed += do_shrink_slab(&sc, shrinker, nr_scanned, nr_eligible); 502ec97097bSVladimir Davydov } 5031d3d4437SGlauber Costa 5041da177e4SLinus Torvalds up_read(&shrinker_rwsem); 505f06590bdSMinchan Kim out: 506f06590bdSMinchan Kim cond_resched(); 50724f7c6b9SDave Chinner return freed; 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds 510cb731d6cSVladimir Davydov void drop_slab_node(int nid) 511cb731d6cSVladimir Davydov { 512cb731d6cSVladimir Davydov unsigned long freed; 513cb731d6cSVladimir Davydov 514cb731d6cSVladimir Davydov do { 515cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 516cb731d6cSVladimir Davydov 517cb731d6cSVladimir Davydov freed = 0; 518cb731d6cSVladimir Davydov do { 519cb731d6cSVladimir Davydov freed += shrink_slab(GFP_KERNEL, nid, memcg, 520cb731d6cSVladimir Davydov 1000, 1000); 521cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 522cb731d6cSVladimir Davydov } while (freed > 10); 523cb731d6cSVladimir Davydov } 524cb731d6cSVladimir Davydov 525cb731d6cSVladimir Davydov void drop_slab(void) 526cb731d6cSVladimir Davydov { 527cb731d6cSVladimir Davydov int nid; 528cb731d6cSVladimir Davydov 529cb731d6cSVladimir Davydov for_each_online_node(nid) 530cb731d6cSVladimir Davydov drop_slab_node(nid); 531cb731d6cSVladimir Davydov } 532cb731d6cSVladimir Davydov 5331da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 5341da177e4SLinus Torvalds { 535ceddc3a5SJohannes Weiner /* 536ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 537ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 538ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 539ceddc3a5SJohannes Weiner */ 540bd4c82c2SHuang Ying int radix_pins = PageTransHuge(page) && PageSwapCache(page) ? 541bd4c82c2SHuang Ying HPAGE_PMD_NR : 1; 542bd4c82c2SHuang Ying return page_count(page) - page_has_private(page) == 1 + radix_pins; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 545703c2708STejun Heo static int may_write_to_inode(struct inode *inode, struct scan_control *sc) 5461da177e4SLinus Torvalds { 547930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 5481da177e4SLinus Torvalds return 1; 549703c2708STejun Heo if (!inode_write_congested(inode)) 5501da177e4SLinus Torvalds return 1; 551703c2708STejun Heo if (inode_to_bdi(inode) == current->backing_dev_info) 5521da177e4SLinus Torvalds return 1; 5531da177e4SLinus Torvalds return 0; 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds /* 5571da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 5581da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 5591da177e4SLinus Torvalds * fsync(), msync() or close(). 5601da177e4SLinus Torvalds * 5611da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 5621da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 5631da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 5641da177e4SLinus Torvalds * 5651da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 5661da177e4SLinus Torvalds * __GFP_FS. 5671da177e4SLinus Torvalds */ 5681da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 5691da177e4SLinus Torvalds struct page *page, int error) 5701da177e4SLinus Torvalds { 5717eaceaccSJens Axboe lock_page(page); 5723e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 5733e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 5741da177e4SLinus Torvalds unlock_page(page); 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 57704e62a29SChristoph Lameter /* possible outcome of pageout() */ 57804e62a29SChristoph Lameter typedef enum { 57904e62a29SChristoph Lameter /* failed to write page out, page is locked */ 58004e62a29SChristoph Lameter PAGE_KEEP, 58104e62a29SChristoph Lameter /* move page to the active list, page is locked */ 58204e62a29SChristoph Lameter PAGE_ACTIVATE, 58304e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 58404e62a29SChristoph Lameter PAGE_SUCCESS, 58504e62a29SChristoph Lameter /* page is clean and locked */ 58604e62a29SChristoph Lameter PAGE_CLEAN, 58704e62a29SChristoph Lameter } pageout_t; 58804e62a29SChristoph Lameter 5891da177e4SLinus Torvalds /* 5901742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 5911742f19fSAndrew Morton * Calls ->writepage(). 5921da177e4SLinus Torvalds */ 593c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 5947d3579e8SKOSAKI Motohiro struct scan_control *sc) 5951da177e4SLinus Torvalds { 5961da177e4SLinus Torvalds /* 5971da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 5981da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 5991da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 6001da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 6011da177e4SLinus Torvalds * PagePrivate for that. 6021da177e4SLinus Torvalds * 6038174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 6041da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 6051da177e4SLinus Torvalds * will block. 6061da177e4SLinus Torvalds * 6071da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 6081da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 6091da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 6101da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 6111da177e4SLinus Torvalds */ 6121da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 6131da177e4SLinus Torvalds return PAGE_KEEP; 6141da177e4SLinus Torvalds if (!mapping) { 6151da177e4SLinus Torvalds /* 6161da177e4SLinus Torvalds * Some data journaling orphaned pages can have 6171da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 6181da177e4SLinus Torvalds */ 619266cf658SDavid Howells if (page_has_private(page)) { 6201da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 6211da177e4SLinus Torvalds ClearPageDirty(page); 622b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 6231da177e4SLinus Torvalds return PAGE_CLEAN; 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds return PAGE_KEEP; 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 6291da177e4SLinus Torvalds return PAGE_ACTIVATE; 630703c2708STejun Heo if (!may_write_to_inode(mapping->host, sc)) 6311da177e4SLinus Torvalds return PAGE_KEEP; 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 6341da177e4SLinus Torvalds int res; 6351da177e4SLinus Torvalds struct writeback_control wbc = { 6361da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 6371da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 638111ebb6eSOGAWA Hirofumi .range_start = 0, 639111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 6401da177e4SLinus Torvalds .for_reclaim = 1, 6411da177e4SLinus Torvalds }; 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds SetPageReclaim(page); 6441da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 6451da177e4SLinus Torvalds if (res < 0) 6461da177e4SLinus Torvalds handle_write_error(mapping, page, res); 647994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 6481da177e4SLinus Torvalds ClearPageReclaim(page); 6491da177e4SLinus Torvalds return PAGE_ACTIVATE; 6501da177e4SLinus Torvalds } 651c661b078SAndy Whitcroft 6521da177e4SLinus Torvalds if (!PageWriteback(page)) { 6531da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 6541da177e4SLinus Torvalds ClearPageReclaim(page); 6551da177e4SLinus Torvalds } 6563aa23851Syalin wang trace_mm_vmscan_writepage(page); 657c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_WRITE); 6581da177e4SLinus Torvalds return PAGE_SUCCESS; 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds return PAGE_CLEAN; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 664a649fd92SAndrew Morton /* 665e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 666e286781dSNick Piggin * gets returned with a refcount of 0. 667a649fd92SAndrew Morton */ 668a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 669a528910eSJohannes Weiner bool reclaimed) 67049d2e9ccSChristoph Lameter { 671c4843a75SGreg Thelen unsigned long flags; 672bd4c82c2SHuang Ying int refcount; 673c4843a75SGreg Thelen 67428e4d965SNick Piggin BUG_ON(!PageLocked(page)); 67528e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 67649d2e9ccSChristoph Lameter 677c4843a75SGreg Thelen spin_lock_irqsave(&mapping->tree_lock, flags); 67849d2e9ccSChristoph Lameter /* 6790fd0e6b0SNick Piggin * The non racy check for a busy page. 6800fd0e6b0SNick Piggin * 6810fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 6820fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 6830fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 6840fd0e6b0SNick Piggin * here, then the following race may occur: 6850fd0e6b0SNick Piggin * 6860fd0e6b0SNick Piggin * get_user_pages(&page); 6870fd0e6b0SNick Piggin * [user mapping goes away] 6880fd0e6b0SNick Piggin * write_to(page); 6890fd0e6b0SNick Piggin * !PageDirty(page) [good] 6900fd0e6b0SNick Piggin * SetPageDirty(page); 6910fd0e6b0SNick Piggin * put_page(page); 6920fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 6930fd0e6b0SNick Piggin * 6940fd0e6b0SNick Piggin * [oops, our write_to data is lost] 6950fd0e6b0SNick Piggin * 6960fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 6970fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 6980139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 6990fd0e6b0SNick Piggin * 7000fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 7010fd0e6b0SNick Piggin * and thus under tree_lock, then this ordering is not required. 70249d2e9ccSChristoph Lameter */ 703bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page)) && PageSwapCache(page)) 704bd4c82c2SHuang Ying refcount = 1 + HPAGE_PMD_NR; 705bd4c82c2SHuang Ying else 706bd4c82c2SHuang Ying refcount = 2; 707bd4c82c2SHuang Ying if (!page_ref_freeze(page, refcount)) 70849d2e9ccSChristoph Lameter goto cannot_free; 709e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 710e286781dSNick Piggin if (unlikely(PageDirty(page))) { 711bd4c82c2SHuang Ying page_ref_unfreeze(page, refcount); 71249d2e9ccSChristoph Lameter goto cannot_free; 713e286781dSNick Piggin } 71449d2e9ccSChristoph Lameter 71549d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 71649d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 7170a31bc97SJohannes Weiner mem_cgroup_swapout(page, swap); 71849d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 719c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 72075f6d6d2SMinchan Kim put_swap_page(page, swap); 721e286781dSNick Piggin } else { 7226072d13cSLinus Torvalds void (*freepage)(struct page *); 723a528910eSJohannes Weiner void *shadow = NULL; 7246072d13cSLinus Torvalds 7256072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 726a528910eSJohannes Weiner /* 727a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 728a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 729a528910eSJohannes Weiner * 730a528910eSJohannes Weiner * But don't store shadows in an address space that is 731a528910eSJohannes Weiner * already exiting. This is not just an optizimation, 732a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 733a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 734a528910eSJohannes Weiner * back. 735f9fe48beSRoss Zwisler * 736f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 737f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 738f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 739f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 740f9fe48beSRoss Zwisler * same page_tree. 741a528910eSJohannes Weiner */ 742a528910eSJohannes Weiner if (reclaimed && page_is_file_cache(page) && 743f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 744a528910eSJohannes Weiner shadow = workingset_eviction(mapping, page); 74562cccb8cSJohannes Weiner __delete_from_page_cache(page, shadow); 746c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 7476072d13cSLinus Torvalds 7486072d13cSLinus Torvalds if (freepage != NULL) 7496072d13cSLinus Torvalds freepage(page); 750e286781dSNick Piggin } 751e286781dSNick Piggin 75249d2e9ccSChristoph Lameter return 1; 75349d2e9ccSChristoph Lameter 75449d2e9ccSChristoph Lameter cannot_free: 755c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 75649d2e9ccSChristoph Lameter return 0; 75749d2e9ccSChristoph Lameter } 75849d2e9ccSChristoph Lameter 7591da177e4SLinus Torvalds /* 760e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 761e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 762e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 763e286781dSNick Piggin * this page. 764e286781dSNick Piggin */ 765e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 766e286781dSNick Piggin { 767a528910eSJohannes Weiner if (__remove_mapping(mapping, page, false)) { 768e286781dSNick Piggin /* 769e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 770e286781dSNick Piggin * drops the pagecache ref for us without requiring another 771e286781dSNick Piggin * atomic operation. 772e286781dSNick Piggin */ 773fe896d18SJoonsoo Kim page_ref_unfreeze(page, 1); 774e286781dSNick Piggin return 1; 775e286781dSNick Piggin } 776e286781dSNick Piggin return 0; 777e286781dSNick Piggin } 778e286781dSNick Piggin 779894bc310SLee Schermerhorn /** 780894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 781894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 782894bc310SLee Schermerhorn * 783894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 784894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 785894bc310SLee Schermerhorn * 786894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 787894bc310SLee Schermerhorn */ 788894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 789894bc310SLee Schermerhorn { 7900ec3b74cSVlastimil Babka bool is_unevictable; 791bbfd28eeSLee Schermerhorn int was_unevictable = PageUnevictable(page); 792894bc310SLee Schermerhorn 793309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 794894bc310SLee Schermerhorn 795894bc310SLee Schermerhorn redo: 796894bc310SLee Schermerhorn ClearPageUnevictable(page); 797894bc310SLee Schermerhorn 79839b5f29aSHugh Dickins if (page_evictable(page)) { 799894bc310SLee Schermerhorn /* 800894bc310SLee Schermerhorn * For evictable pages, we can use the cache. 801894bc310SLee Schermerhorn * In event of a race, worst case is we end up with an 802894bc310SLee Schermerhorn * unevictable page on [in]active list. 803894bc310SLee Schermerhorn * We know how to handle that. 804894bc310SLee Schermerhorn */ 8050ec3b74cSVlastimil Babka is_unevictable = false; 806c53954a0SMel Gorman lru_cache_add(page); 807894bc310SLee Schermerhorn } else { 808894bc310SLee Schermerhorn /* 809894bc310SLee Schermerhorn * Put unevictable pages directly on zone's unevictable 810894bc310SLee Schermerhorn * list. 811894bc310SLee Schermerhorn */ 8120ec3b74cSVlastimil Babka is_unevictable = true; 813894bc310SLee Schermerhorn add_page_to_unevictable_list(page); 8146a7b9548SJohannes Weiner /* 81521ee9f39SMinchan Kim * When racing with an mlock or AS_UNEVICTABLE clearing 81621ee9f39SMinchan Kim * (page is unlocked) make sure that if the other thread 81721ee9f39SMinchan Kim * does not observe our setting of PG_lru and fails 81824513264SHugh Dickins * isolation/check_move_unevictable_pages, 81921ee9f39SMinchan Kim * we see PG_mlocked/AS_UNEVICTABLE cleared below and move 8206a7b9548SJohannes Weiner * the page back to the evictable list. 8216a7b9548SJohannes Weiner * 82221ee9f39SMinchan Kim * The other side is TestClearPageMlocked() or shmem_lock(). 8236a7b9548SJohannes Weiner */ 8246a7b9548SJohannes Weiner smp_mb(); 825894bc310SLee Schermerhorn } 826894bc310SLee Schermerhorn 827894bc310SLee Schermerhorn /* 828894bc310SLee Schermerhorn * page's status can change while we move it among lru. If an evictable 829894bc310SLee Schermerhorn * page is on unevictable list, it never be freed. To avoid that, 830894bc310SLee Schermerhorn * check after we added it to the list, again. 831894bc310SLee Schermerhorn */ 8320ec3b74cSVlastimil Babka if (is_unevictable && page_evictable(page)) { 833894bc310SLee Schermerhorn if (!isolate_lru_page(page)) { 834894bc310SLee Schermerhorn put_page(page); 835894bc310SLee Schermerhorn goto redo; 836894bc310SLee Schermerhorn } 837894bc310SLee Schermerhorn /* This means someone else dropped this page from LRU 838894bc310SLee Schermerhorn * So, it will be freed or putback to LRU again. There is 839894bc310SLee Schermerhorn * nothing to do here. 840894bc310SLee Schermerhorn */ 841894bc310SLee Schermerhorn } 842894bc310SLee Schermerhorn 8430ec3b74cSVlastimil Babka if (was_unevictable && !is_unevictable) 844bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGRESCUED); 8450ec3b74cSVlastimil Babka else if (!was_unevictable && is_unevictable) 846bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGCULLED); 847bbfd28eeSLee Schermerhorn 848894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 849894bc310SLee Schermerhorn } 850894bc310SLee Schermerhorn 851dfc8d636SJohannes Weiner enum page_references { 852dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 853dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 85464574746SJohannes Weiner PAGEREF_KEEP, 855dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 856dfc8d636SJohannes Weiner }; 857dfc8d636SJohannes Weiner 858dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 859dfc8d636SJohannes Weiner struct scan_control *sc) 860dfc8d636SJohannes Weiner { 86164574746SJohannes Weiner int referenced_ptes, referenced_page; 862dfc8d636SJohannes Weiner unsigned long vm_flags; 863dfc8d636SJohannes Weiner 864c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 865c3ac9a8aSJohannes Weiner &vm_flags); 86664574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 867dfc8d636SJohannes Weiner 868dfc8d636SJohannes Weiner /* 869dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 870dfc8d636SJohannes Weiner * move the page to the unevictable list. 871dfc8d636SJohannes Weiner */ 872dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 873dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 874dfc8d636SJohannes Weiner 87564574746SJohannes Weiner if (referenced_ptes) { 876e4898273SMichal Hocko if (PageSwapBacked(page)) 87764574746SJohannes Weiner return PAGEREF_ACTIVATE; 87864574746SJohannes Weiner /* 87964574746SJohannes Weiner * All mapped pages start out with page table 88064574746SJohannes Weiner * references from the instantiating fault, so we need 88164574746SJohannes Weiner * to look twice if a mapped file page is used more 88264574746SJohannes Weiner * than once. 88364574746SJohannes Weiner * 88464574746SJohannes Weiner * Mark it and spare it for another trip around the 88564574746SJohannes Weiner * inactive list. Another page table reference will 88664574746SJohannes Weiner * lead to its activation. 88764574746SJohannes Weiner * 88864574746SJohannes Weiner * Note: the mark is set for activated pages as well 88964574746SJohannes Weiner * so that recently deactivated but used pages are 89064574746SJohannes Weiner * quickly recovered. 89164574746SJohannes Weiner */ 89264574746SJohannes Weiner SetPageReferenced(page); 89364574746SJohannes Weiner 89434dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 895dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 896dfc8d636SJohannes Weiner 897c909e993SKonstantin Khlebnikov /* 898c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 899c909e993SKonstantin Khlebnikov */ 900c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 901c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 902c909e993SKonstantin Khlebnikov 90364574746SJohannes Weiner return PAGEREF_KEEP; 90464574746SJohannes Weiner } 90564574746SJohannes Weiner 906dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 9072e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 908dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 90964574746SJohannes Weiner 91064574746SJohannes Weiner return PAGEREF_RECLAIM; 911dfc8d636SJohannes Weiner } 912dfc8d636SJohannes Weiner 913e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 914e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 915e2be15f6SMel Gorman bool *dirty, bool *writeback) 916e2be15f6SMel Gorman { 917b4597226SMel Gorman struct address_space *mapping; 918b4597226SMel Gorman 919e2be15f6SMel Gorman /* 920e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 921e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 922e2be15f6SMel Gorman */ 923802a3a92SShaohua Li if (!page_is_file_cache(page) || 924802a3a92SShaohua Li (PageAnon(page) && !PageSwapBacked(page))) { 925e2be15f6SMel Gorman *dirty = false; 926e2be15f6SMel Gorman *writeback = false; 927e2be15f6SMel Gorman return; 928e2be15f6SMel Gorman } 929e2be15f6SMel Gorman 930e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 931e2be15f6SMel Gorman *dirty = PageDirty(page); 932e2be15f6SMel Gorman *writeback = PageWriteback(page); 933b4597226SMel Gorman 934b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 935b4597226SMel Gorman if (!page_has_private(page)) 936b4597226SMel Gorman return; 937b4597226SMel Gorman 938b4597226SMel Gorman mapping = page_mapping(page); 939b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 940b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 941e2be15f6SMel Gorman } 942e2be15f6SMel Gorman 9433c710c1aSMichal Hocko struct reclaim_stat { 9443c710c1aSMichal Hocko unsigned nr_dirty; 9453c710c1aSMichal Hocko unsigned nr_unqueued_dirty; 9463c710c1aSMichal Hocko unsigned nr_congested; 9473c710c1aSMichal Hocko unsigned nr_writeback; 9483c710c1aSMichal Hocko unsigned nr_immediate; 9495bccd166SMichal Hocko unsigned nr_activate; 9505bccd166SMichal Hocko unsigned nr_ref_keep; 9515bccd166SMichal Hocko unsigned nr_unmap_fail; 9523c710c1aSMichal Hocko }; 9533c710c1aSMichal Hocko 954e286781dSNick Piggin /* 9551742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 9561da177e4SLinus Torvalds */ 9571742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 958599d0c95SMel Gorman struct pglist_data *pgdat, 959f84f6e2bSMel Gorman struct scan_control *sc, 96002c6de8dSMinchan Kim enum ttu_flags ttu_flags, 9613c710c1aSMichal Hocko struct reclaim_stat *stat, 96202c6de8dSMinchan Kim bool force_reclaim) 9631da177e4SLinus Torvalds { 9641da177e4SLinus Torvalds LIST_HEAD(ret_pages); 965abe4c3b5SMel Gorman LIST_HEAD(free_pages); 9661da177e4SLinus Torvalds int pgactivate = 0; 9673c710c1aSMichal Hocko unsigned nr_unqueued_dirty = 0; 9683c710c1aSMichal Hocko unsigned nr_dirty = 0; 9693c710c1aSMichal Hocko unsigned nr_congested = 0; 9703c710c1aSMichal Hocko unsigned nr_reclaimed = 0; 9713c710c1aSMichal Hocko unsigned nr_writeback = 0; 9723c710c1aSMichal Hocko unsigned nr_immediate = 0; 9735bccd166SMichal Hocko unsigned nr_ref_keep = 0; 9745bccd166SMichal Hocko unsigned nr_unmap_fail = 0; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds cond_resched(); 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds while (!list_empty(page_list)) { 9791da177e4SLinus Torvalds struct address_space *mapping; 9801da177e4SLinus Torvalds struct page *page; 9811da177e4SLinus Torvalds int may_enter_fs; 98202c6de8dSMinchan Kim enum page_references references = PAGEREF_RECLAIM_CLEAN; 983e2be15f6SMel Gorman bool dirty, writeback; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds cond_resched(); 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds page = lru_to_page(page_list); 9881da177e4SLinus Torvalds list_del(&page->lru); 9891da177e4SLinus Torvalds 990529ae9aaSNick Piggin if (!trylock_page(page)) 9911da177e4SLinus Torvalds goto keep; 9921da177e4SLinus Torvalds 993309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds sc->nr_scanned++; 99680e43426SChristoph Lameter 99739b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 998ad6b6704SMinchan Kim goto activate_locked; 999894bc310SLee Schermerhorn 1000a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 100180e43426SChristoph Lameter goto keep_locked; 100280e43426SChristoph Lameter 10031da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 1004802a3a92SShaohua Li if ((page_mapped(page) || PageSwapCache(page)) && 1005802a3a92SShaohua Li !(PageAnon(page) && !PageSwapBacked(page))) 10061da177e4SLinus Torvalds sc->nr_scanned++; 10071da177e4SLinus Torvalds 1008c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 1009c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 1010c661b078SAndy Whitcroft 1011e62e384eSMichal Hocko /* 1012e2be15f6SMel Gorman * The number of dirty pages determines if a zone is marked 1013e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 1014e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 1015e2be15f6SMel Gorman * is all dirty unqueued pages. 1016e2be15f6SMel Gorman */ 1017e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 1018e2be15f6SMel Gorman if (dirty || writeback) 1019e2be15f6SMel Gorman nr_dirty++; 1020e2be15f6SMel Gorman 1021e2be15f6SMel Gorman if (dirty && !writeback) 1022e2be15f6SMel Gorman nr_unqueued_dirty++; 1023e2be15f6SMel Gorman 1024d04e8acdSMel Gorman /* 1025d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 1026d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 1027d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 1028d04e8acdSMel Gorman * end of the LRU a second time. 1029d04e8acdSMel Gorman */ 1030e2be15f6SMel Gorman mapping = page_mapping(page); 10311da58ee2SJamie Liu if (((dirty || writeback) && mapping && 1032703c2708STejun Heo inode_write_congested(mapping->host)) || 1033d04e8acdSMel Gorman (writeback && PageReclaim(page))) 1034e2be15f6SMel Gorman nr_congested++; 1035e2be15f6SMel Gorman 1036e2be15f6SMel Gorman /* 1037283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 1038283aba9fSMel Gorman * are three cases to consider. 1039e62e384eSMichal Hocko * 1040283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 1041283aba9fSMel Gorman * under writeback and this page is both under writeback and 1042283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 1043283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 1044283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 1045283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 1046283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 1047b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 1048b1a6f21eSMel Gorman * caller can stall after page list has been processed. 1049c3b94f44SHugh Dickins * 105097c9341fSTejun Heo * 2) Global or new memcg reclaim encounters a page that is 1051ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 1052ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 1053ecf5fc6eSMichal Hocko * not to fs). In this case mark the page for immediate 105497c9341fSTejun Heo * reclaim and continue scanning. 1055283aba9fSMel Gorman * 1056ecf5fc6eSMichal Hocko * Require may_enter_fs because we would wait on fs, which 1057ecf5fc6eSMichal Hocko * may not have submitted IO yet. And the loop driver might 1058283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 1059283aba9fSMel Gorman * which it is needed to do the write (loop masks off 1060283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 1061283aba9fSMel Gorman * would probably show more reasons. 1062283aba9fSMel Gorman * 10637fadc820SHugh Dickins * 3) Legacy memcg encounters a page that is already marked 1064283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 1065283aba9fSMel Gorman * throttling so we could easily OOM just because too many 1066283aba9fSMel Gorman * pages are in writeback and there is nothing else to 1067283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 1068c55e8d03SJohannes Weiner * 1069c55e8d03SJohannes Weiner * In cases 1) and 2) we activate the pages to get them out of 1070c55e8d03SJohannes Weiner * the way while we continue scanning for clean pages on the 1071c55e8d03SJohannes Weiner * inactive list and refilling from the active list. The 1072c55e8d03SJohannes Weiner * observation here is that waiting for disk writes is more 1073c55e8d03SJohannes Weiner * expensive than potentially causing reloads down the line. 1074c55e8d03SJohannes Weiner * Since they're marked for immediate reclaim, they won't put 1075c55e8d03SJohannes Weiner * memory pressure on the cache working set any longer than it 1076c55e8d03SJohannes Weiner * takes to write them to disk. 1077e62e384eSMichal Hocko */ 1078283aba9fSMel Gorman if (PageWriteback(page)) { 1079283aba9fSMel Gorman /* Case 1 above */ 1080283aba9fSMel Gorman if (current_is_kswapd() && 1081283aba9fSMel Gorman PageReclaim(page) && 1082599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1083b1a6f21eSMel Gorman nr_immediate++; 1084c55e8d03SJohannes Weiner goto activate_locked; 1085283aba9fSMel Gorman 1086283aba9fSMel Gorman /* Case 2 above */ 108797c9341fSTejun Heo } else if (sane_reclaim(sc) || 1088ecf5fc6eSMichal Hocko !PageReclaim(page) || !may_enter_fs) { 1089c3b94f44SHugh Dickins /* 1090c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 1091c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 1092c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 1093c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 1094c3b94f44SHugh Dickins * enough to care. What we do want is for this 1095c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 1096c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 1097c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 1098c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 1099c3b94f44SHugh Dickins */ 1100c3b94f44SHugh Dickins SetPageReclaim(page); 110192df3a72SMel Gorman nr_writeback++; 1102c55e8d03SJohannes Weiner goto activate_locked; 1103283aba9fSMel Gorman 1104283aba9fSMel Gorman /* Case 3 above */ 1105283aba9fSMel Gorman } else { 11067fadc820SHugh Dickins unlock_page(page); 1107c3b94f44SHugh Dickins wait_on_page_writeback(page); 11087fadc820SHugh Dickins /* then go back and try same page again */ 11097fadc820SHugh Dickins list_add_tail(&page->lru, page_list); 11107fadc820SHugh Dickins continue; 1111e62e384eSMichal Hocko } 1112283aba9fSMel Gorman } 11131da177e4SLinus Torvalds 111402c6de8dSMinchan Kim if (!force_reclaim) 11156a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 111602c6de8dSMinchan Kim 1117dfc8d636SJohannes Weiner switch (references) { 1118dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 11191da177e4SLinus Torvalds goto activate_locked; 112064574746SJohannes Weiner case PAGEREF_KEEP: 11215bccd166SMichal Hocko nr_ref_keep++; 112264574746SJohannes Weiner goto keep_locked; 1123dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1124dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1125dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 1126dfc8d636SJohannes Weiner } 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds /* 11291da177e4SLinus Torvalds * Anonymous process memory has backing store? 11301da177e4SLinus Torvalds * Try to allocate it some swap space here. 1131802a3a92SShaohua Li * Lazyfree page could be freed directly 11321da177e4SLinus Torvalds */ 1133bd4c82c2SHuang Ying if (PageAnon(page) && PageSwapBacked(page)) { 1134bd4c82c2SHuang Ying if (!PageSwapCache(page)) { 113563eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 113663eb6b93SHugh Dickins goto keep_locked; 1137747552b1SHuang Ying if (PageTransHuge(page)) { 1138b8f593cdSHuang Ying /* cannot split THP, skip it */ 1139747552b1SHuang Ying if (!can_split_huge_page(page, NULL)) 1140b8f593cdSHuang Ying goto activate_locked; 1141747552b1SHuang Ying /* 1142747552b1SHuang Ying * Split pages without a PMD map right 1143747552b1SHuang Ying * away. Chances are some or all of the 1144747552b1SHuang Ying * tail pages can be freed without IO. 1145747552b1SHuang Ying */ 1146747552b1SHuang Ying if (!compound_mapcount(page) && 1147bd4c82c2SHuang Ying split_huge_page_to_list(page, 1148bd4c82c2SHuang Ying page_list)) 1149747552b1SHuang Ying goto activate_locked; 1150747552b1SHuang Ying } 11510f074658SMinchan Kim if (!add_to_swap(page)) { 11520f074658SMinchan Kim if (!PageTransHuge(page)) 11531da177e4SLinus Torvalds goto activate_locked; 1154bd4c82c2SHuang Ying /* Fallback to swap normal pages */ 1155bd4c82c2SHuang Ying if (split_huge_page_to_list(page, 1156bd4c82c2SHuang Ying page_list)) 11570f074658SMinchan Kim goto activate_locked; 1158fe490cc0SHuang Ying #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1159fe490cc0SHuang Ying count_vm_event(THP_SWPOUT_FALLBACK); 1160fe490cc0SHuang Ying #endif 11610f074658SMinchan Kim if (!add_to_swap(page)) 11620f074658SMinchan Kim goto activate_locked; 11630f074658SMinchan Kim } 11640f074658SMinchan Kim 116563eb6b93SHugh Dickins may_enter_fs = 1; 11661da177e4SLinus Torvalds 1167e2be15f6SMel Gorman /* Adding to swap updated mapping */ 11681da177e4SLinus Torvalds mapping = page_mapping(page); 1169bd4c82c2SHuang Ying } 11707751b2daSKirill A. Shutemov } else if (unlikely(PageTransHuge(page))) { 11717751b2daSKirill A. Shutemov /* Split file THP */ 11727751b2daSKirill A. Shutemov if (split_huge_page_to_list(page, page_list)) 11737751b2daSKirill A. Shutemov goto keep_locked; 1174e2be15f6SMel Gorman } 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds /* 11771da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 11781da177e4SLinus Torvalds * processes. Try to unmap it here. 11791da177e4SLinus Torvalds */ 1180802a3a92SShaohua Li if (page_mapped(page)) { 1181bd4c82c2SHuang Ying enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH; 1182bd4c82c2SHuang Ying 1183bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) 1184bd4c82c2SHuang Ying flags |= TTU_SPLIT_HUGE_PMD; 1185bd4c82c2SHuang Ying if (!try_to_unmap(page, flags)) { 11865bccd166SMichal Hocko nr_unmap_fail++; 11871da177e4SLinus Torvalds goto activate_locked; 11881da177e4SLinus Torvalds } 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds if (PageDirty(page)) { 1192ee72886dSMel Gorman /* 11934eda4823SJohannes Weiner * Only kswapd can writeback filesystem pages 11944eda4823SJohannes Weiner * to avoid risk of stack overflow. But avoid 11954eda4823SJohannes Weiner * injecting inefficient single-page IO into 11964eda4823SJohannes Weiner * flusher writeback as much as possible: only 11974eda4823SJohannes Weiner * write pages when we've encountered many 11984eda4823SJohannes Weiner * dirty pages, and when we've already scanned 11994eda4823SJohannes Weiner * the rest of the LRU for clean pages and see 12004eda4823SJohannes Weiner * the same dirty pages again (PageReclaim). 1201ee72886dSMel Gorman */ 1202f84f6e2bSMel Gorman if (page_is_file_cache(page) && 12034eda4823SJohannes Weiner (!current_is_kswapd() || !PageReclaim(page) || 1204599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 120549ea7eb6SMel Gorman /* 120649ea7eb6SMel Gorman * Immediately reclaim when written back. 120749ea7eb6SMel Gorman * Similar in principal to deactivate_page() 120849ea7eb6SMel Gorman * except we already have the page isolated 120949ea7eb6SMel Gorman * and know it's dirty 121049ea7eb6SMel Gorman */ 1211c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 121249ea7eb6SMel Gorman SetPageReclaim(page); 121349ea7eb6SMel Gorman 1214c55e8d03SJohannes Weiner goto activate_locked; 1215ee72886dSMel Gorman } 1216ee72886dSMel Gorman 1217dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 12181da177e4SLinus Torvalds goto keep_locked; 12194dd4b920SAndrew Morton if (!may_enter_fs) 12201da177e4SLinus Torvalds goto keep_locked; 122152a8363eSChristoph Lameter if (!sc->may_writepage) 12221da177e4SLinus Torvalds goto keep_locked; 12231da177e4SLinus Torvalds 1224d950c947SMel Gorman /* 1225d950c947SMel Gorman * Page is dirty. Flush the TLB if a writable entry 1226d950c947SMel Gorman * potentially exists to avoid CPU writes after IO 1227d950c947SMel Gorman * starts and then write it out here. 1228d950c947SMel Gorman */ 1229d950c947SMel Gorman try_to_unmap_flush_dirty(); 12307d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 12311da177e4SLinus Torvalds case PAGE_KEEP: 12321da177e4SLinus Torvalds goto keep_locked; 12331da177e4SLinus Torvalds case PAGE_ACTIVATE: 12341da177e4SLinus Torvalds goto activate_locked; 12351da177e4SLinus Torvalds case PAGE_SUCCESS: 12367d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 123741ac1999SMel Gorman goto keep; 12387d3579e8SKOSAKI Motohiro if (PageDirty(page)) 12391da177e4SLinus Torvalds goto keep; 12407d3579e8SKOSAKI Motohiro 12411da177e4SLinus Torvalds /* 12421da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 12431da177e4SLinus Torvalds * ahead and try to reclaim the page. 12441da177e4SLinus Torvalds */ 1245529ae9aaSNick Piggin if (!trylock_page(page)) 12461da177e4SLinus Torvalds goto keep; 12471da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 12481da177e4SLinus Torvalds goto keep_locked; 12491da177e4SLinus Torvalds mapping = page_mapping(page); 12501da177e4SLinus Torvalds case PAGE_CLEAN: 12511da177e4SLinus Torvalds ; /* try to free the page below */ 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds /* 12561da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 12571da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 12581da177e4SLinus Torvalds * the page as well. 12591da177e4SLinus Torvalds * 12601da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 12611da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 12621da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 12631da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 12641da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 12651da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 12661da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 12671da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 12681da177e4SLinus Torvalds * 12691da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 12701da177e4SLinus Torvalds * the pages which were not successfully invalidated in 12711da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 12721da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 12731da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 12741da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 12751da177e4SLinus Torvalds */ 1276266cf658SDavid Howells if (page_has_private(page)) { 12771da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 12781da177e4SLinus Torvalds goto activate_locked; 1279e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1280e286781dSNick Piggin unlock_page(page); 1281e286781dSNick Piggin if (put_page_testzero(page)) 12821da177e4SLinus Torvalds goto free_it; 1283e286781dSNick Piggin else { 1284e286781dSNick Piggin /* 1285e286781dSNick Piggin * rare race with speculative reference. 1286e286781dSNick Piggin * the speculative reference will free 1287e286781dSNick Piggin * this page shortly, so we may 1288e286781dSNick Piggin * increment nr_reclaimed here (and 1289e286781dSNick Piggin * leave it off the LRU). 1290e286781dSNick Piggin */ 1291e286781dSNick Piggin nr_reclaimed++; 1292e286781dSNick Piggin continue; 1293e286781dSNick Piggin } 1294e286781dSNick Piggin } 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds 1297802a3a92SShaohua Li if (PageAnon(page) && !PageSwapBacked(page)) { 1298802a3a92SShaohua Li /* follow __remove_mapping for reference */ 1299802a3a92SShaohua Li if (!page_ref_freeze(page, 1)) 130049d2e9ccSChristoph Lameter goto keep_locked; 1301802a3a92SShaohua Li if (PageDirty(page)) { 1302802a3a92SShaohua Li page_ref_unfreeze(page, 1); 1303802a3a92SShaohua Li goto keep_locked; 1304802a3a92SShaohua Li } 13051da177e4SLinus Torvalds 1306802a3a92SShaohua Li count_vm_event(PGLAZYFREED); 13072262185cSRoman Gushchin count_memcg_page_event(page, PGLAZYFREED); 1308802a3a92SShaohua Li } else if (!mapping || !__remove_mapping(mapping, page, true)) 1309802a3a92SShaohua Li goto keep_locked; 1310a978d6f5SNick Piggin /* 1311a978d6f5SNick Piggin * At this point, we have no other references and there is 1312a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 1313a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 1314a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 1315a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 1316a978d6f5SNick Piggin */ 131748c935adSKirill A. Shutemov __ClearPageLocked(page); 1318e286781dSNick Piggin free_it: 131905ff5137SAndrew Morton nr_reclaimed++; 1320abe4c3b5SMel Gorman 1321abe4c3b5SMel Gorman /* 1322abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1323abe4c3b5SMel Gorman * appear not as the counts should be low 1324abe4c3b5SMel Gorman */ 1325bd4c82c2SHuang Ying if (unlikely(PageTransHuge(page))) { 1326bd4c82c2SHuang Ying mem_cgroup_uncharge(page); 1327bd4c82c2SHuang Ying (*get_compound_page_dtor(page))(page); 1328bd4c82c2SHuang Ying } else 1329abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 13301da177e4SLinus Torvalds continue; 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds activate_locked: 133368a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 1334ad6b6704SMinchan Kim if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || 1335ad6b6704SMinchan Kim PageMlocked(page))) 1336a2c43eedSHugh Dickins try_to_free_swap(page); 1337309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 1338ad6b6704SMinchan Kim if (!PageMlocked(page)) { 13391da177e4SLinus Torvalds SetPageActive(page); 13401da177e4SLinus Torvalds pgactivate++; 13412262185cSRoman Gushchin count_memcg_page_event(page, PGACTIVATE); 1342ad6b6704SMinchan Kim } 13431da177e4SLinus Torvalds keep_locked: 13441da177e4SLinus Torvalds unlock_page(page); 13451da177e4SLinus Torvalds keep: 13461da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1347309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 13481da177e4SLinus Torvalds } 1349abe4c3b5SMel Gorman 1350747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 135172b252aeSMel Gorman try_to_unmap_flush(); 1352*2d4894b5SMel Gorman free_unref_page_list(&free_pages); 1353abe4c3b5SMel Gorman 13541da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1355f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 13560a31bc97SJohannes Weiner 13573c710c1aSMichal Hocko if (stat) { 13583c710c1aSMichal Hocko stat->nr_dirty = nr_dirty; 13593c710c1aSMichal Hocko stat->nr_congested = nr_congested; 13603c710c1aSMichal Hocko stat->nr_unqueued_dirty = nr_unqueued_dirty; 13613c710c1aSMichal Hocko stat->nr_writeback = nr_writeback; 13623c710c1aSMichal Hocko stat->nr_immediate = nr_immediate; 13635bccd166SMichal Hocko stat->nr_activate = pgactivate; 13645bccd166SMichal Hocko stat->nr_ref_keep = nr_ref_keep; 13655bccd166SMichal Hocko stat->nr_unmap_fail = nr_unmap_fail; 13663c710c1aSMichal Hocko } 136705ff5137SAndrew Morton return nr_reclaimed; 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 137002c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone, 137102c6de8dSMinchan Kim struct list_head *page_list) 137202c6de8dSMinchan Kim { 137302c6de8dSMinchan Kim struct scan_control sc = { 137402c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 137502c6de8dSMinchan Kim .priority = DEF_PRIORITY, 137602c6de8dSMinchan Kim .may_unmap = 1, 137702c6de8dSMinchan Kim }; 13783c710c1aSMichal Hocko unsigned long ret; 137902c6de8dSMinchan Kim struct page *page, *next; 138002c6de8dSMinchan Kim LIST_HEAD(clean_pages); 138102c6de8dSMinchan Kim 138202c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1383117aad1eSRafael Aquini if (page_is_file_cache(page) && !PageDirty(page) && 1384b1123ea6SMinchan Kim !__PageMovable(page)) { 138502c6de8dSMinchan Kim ClearPageActive(page); 138602c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 138702c6de8dSMinchan Kim } 138802c6de8dSMinchan Kim } 138902c6de8dSMinchan Kim 1390599d0c95SMel Gorman ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 1391a128ca71SShaohua Li TTU_IGNORE_ACCESS, NULL, true); 139202c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 1393599d0c95SMel Gorman mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret); 139402c6de8dSMinchan Kim return ret; 139502c6de8dSMinchan Kim } 139602c6de8dSMinchan Kim 13975ad333ebSAndy Whitcroft /* 13985ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 13995ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 14005ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 14015ad333ebSAndy Whitcroft * 14025ad333ebSAndy Whitcroft * page: page to consider 14035ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 14045ad333ebSAndy Whitcroft * 14055ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 14065ad333ebSAndy Whitcroft */ 1407f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 14085ad333ebSAndy Whitcroft { 14095ad333ebSAndy Whitcroft int ret = -EINVAL; 14105ad333ebSAndy Whitcroft 14115ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 14125ad333ebSAndy Whitcroft if (!PageLRU(page)) 14135ad333ebSAndy Whitcroft return ret; 14145ad333ebSAndy Whitcroft 1415e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1416e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1417894bc310SLee Schermerhorn return ret; 1418894bc310SLee Schermerhorn 14195ad333ebSAndy Whitcroft ret = -EBUSY; 142008e552c6SKAMEZAWA Hiroyuki 1421c8244935SMel Gorman /* 1422c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1423c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1424c8244935SMel Gorman * blocking - clean pages for the most part. 1425c8244935SMel Gorman * 1426c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1427c8244935SMel Gorman * that it is possible to migrate without blocking 1428c8244935SMel Gorman */ 14291276ad68SJohannes Weiner if (mode & ISOLATE_ASYNC_MIGRATE) { 1430c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1431c8244935SMel Gorman if (PageWriteback(page)) 143239deaf85SMinchan Kim return ret; 143339deaf85SMinchan Kim 1434c8244935SMel Gorman if (PageDirty(page)) { 1435c8244935SMel Gorman struct address_space *mapping; 1436c8244935SMel Gorman 1437c8244935SMel Gorman /* 1438c8244935SMel Gorman * Only pages without mappings or that have a 1439c8244935SMel Gorman * ->migratepage callback are possible to migrate 1440c8244935SMel Gorman * without blocking 1441c8244935SMel Gorman */ 1442c8244935SMel Gorman mapping = page_mapping(page); 1443c8244935SMel Gorman if (mapping && !mapping->a_ops->migratepage) 1444c8244935SMel Gorman return ret; 1445c8244935SMel Gorman } 1446c8244935SMel Gorman } 1447c8244935SMel Gorman 1448f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1449f80c0673SMinchan Kim return ret; 1450f80c0673SMinchan Kim 14515ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 14525ad333ebSAndy Whitcroft /* 14535ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 14545ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 14555ad333ebSAndy Whitcroft * page release code relies on it. 14565ad333ebSAndy Whitcroft */ 14575ad333ebSAndy Whitcroft ClearPageLRU(page); 14585ad333ebSAndy Whitcroft ret = 0; 14595ad333ebSAndy Whitcroft } 14605ad333ebSAndy Whitcroft 14615ad333ebSAndy Whitcroft return ret; 14625ad333ebSAndy Whitcroft } 14635ad333ebSAndy Whitcroft 14647ee36a14SMel Gorman 14657ee36a14SMel Gorman /* 14667ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 14677ee36a14SMel Gorman * be complete before mem_cgroup_update_lru_size due to a santity check. 14687ee36a14SMel Gorman */ 14697ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 1470b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 14717ee36a14SMel Gorman { 14727ee36a14SMel Gorman int zid; 14737ee36a14SMel Gorman 14747ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 14757ee36a14SMel Gorman if (!nr_zone_taken[zid]) 14767ee36a14SMel Gorman continue; 14777ee36a14SMel Gorman 14787ee36a14SMel Gorman __update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1479b4536f0cSMichal Hocko #ifdef CONFIG_MEMCG 1480b4536f0cSMichal Hocko mem_cgroup_update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1481b4536f0cSMichal Hocko #endif 14827ee36a14SMel Gorman } 14837ee36a14SMel Gorman 14847ee36a14SMel Gorman } 14857ee36a14SMel Gorman 148649d2e9ccSChristoph Lameter /* 1487a52633d8SMel Gorman * zone_lru_lock is heavily contended. Some of the functions that 14881da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 14891da177e4SLinus Torvalds * and working on them outside the LRU lock. 14901da177e4SLinus Torvalds * 14911da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 14921da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 14931da177e4SLinus Torvalds * 14941da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 14951da177e4SLinus Torvalds * 1496791b48b6SMinchan Kim * @nr_to_scan: The number of eligible pages to look through on the list. 14975dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 14981da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1499f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1500fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 15015ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 15023cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 15031da177e4SLinus Torvalds * 15041da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 15051da177e4SLinus Torvalds */ 150669e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 15075dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1508fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 15093cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 15101da177e4SLinus Torvalds { 151175b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 151269e05944SAndrew Morton unsigned long nr_taken = 0; 1513599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 15147cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 15153db65812SJohannes Weiner unsigned long skipped = 0; 1516791b48b6SMinchan Kim unsigned long scan, total_scan, nr_pages; 1517b2e18757SMel Gorman LIST_HEAD(pages_skipped); 15181da177e4SLinus Torvalds 1519791b48b6SMinchan Kim scan = 0; 1520791b48b6SMinchan Kim for (total_scan = 0; 1521791b48b6SMinchan Kim scan < nr_to_scan && nr_taken < nr_to_scan && !list_empty(src); 1522791b48b6SMinchan Kim total_scan++) { 15235ad333ebSAndy Whitcroft struct page *page; 15245ad333ebSAndy Whitcroft 15251da177e4SLinus Torvalds page = lru_to_page(src); 15261da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 15271da177e4SLinus Torvalds 1528309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 15298d438f96SNick Piggin 1530b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 1531b2e18757SMel Gorman list_move(&page->lru, &pages_skipped); 15327cc30fcfSMel Gorman nr_skipped[page_zonenum(page)]++; 1533b2e18757SMel Gorman continue; 1534b2e18757SMel Gorman } 1535b2e18757SMel Gorman 1536791b48b6SMinchan Kim /* 1537791b48b6SMinchan Kim * Do not count skipped pages because that makes the function 1538791b48b6SMinchan Kim * return with no isolated pages if the LRU mostly contains 1539791b48b6SMinchan Kim * ineligible pages. This causes the VM to not reclaim any 1540791b48b6SMinchan Kim * pages, triggering a premature OOM. 1541791b48b6SMinchan Kim */ 1542791b48b6SMinchan Kim scan++; 1543f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 15445ad333ebSAndy Whitcroft case 0: 1545599d0c95SMel Gorman nr_pages = hpage_nr_pages(page); 1546599d0c95SMel Gorman nr_taken += nr_pages; 1547599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 15485ad333ebSAndy Whitcroft list_move(&page->lru, dst); 15495ad333ebSAndy Whitcroft break; 15507c8ee9a8SNick Piggin 15515ad333ebSAndy Whitcroft case -EBUSY: 15525ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 15535ad333ebSAndy Whitcroft list_move(&page->lru, src); 15545ad333ebSAndy Whitcroft continue; 15555ad333ebSAndy Whitcroft 15565ad333ebSAndy Whitcroft default: 15575ad333ebSAndy Whitcroft BUG(); 15585ad333ebSAndy Whitcroft } 15595ad333ebSAndy Whitcroft } 15601da177e4SLinus Torvalds 1561b2e18757SMel Gorman /* 1562b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 1563b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 1564b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 1565b2e18757SMel Gorman * scanning would soon rescan the same pages to skip and put the 1566b2e18757SMel Gorman * system at risk of premature OOM. 1567b2e18757SMel Gorman */ 15687cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 15697cc30fcfSMel Gorman int zid; 15707cc30fcfSMel Gorman 15713db65812SJohannes Weiner list_splice(&pages_skipped, src); 15727cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 15737cc30fcfSMel Gorman if (!nr_skipped[zid]) 15747cc30fcfSMel Gorman continue; 15757cc30fcfSMel Gorman 15767cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 15771265e3a6SMichal Hocko skipped += nr_skipped[zid]; 15787cc30fcfSMel Gorman } 15797cc30fcfSMel Gorman } 1580791b48b6SMinchan Kim *nr_scanned = total_scan; 15811265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 1582791b48b6SMinchan Kim total_scan, skipped, nr_taken, mode, lru); 1583b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 15841da177e4SLinus Torvalds return nr_taken; 15851da177e4SLinus Torvalds } 15861da177e4SLinus Torvalds 158762695a84SNick Piggin /** 158862695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 158962695a84SNick Piggin * @page: page to isolate from its LRU list 159062695a84SNick Piggin * 159162695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 159262695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 159362695a84SNick Piggin * 159462695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 159562695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 159662695a84SNick Piggin * 159762695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1598894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1599894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1600894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 160162695a84SNick Piggin * 160262695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 160362695a84SNick Piggin * found will be decremented. 160462695a84SNick Piggin * 160562695a84SNick Piggin * Restrictions: 160662695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 160762695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 160862695a84SNick Piggin * without a stable reference). 160962695a84SNick Piggin * (2) the lru_lock must not be held. 161062695a84SNick Piggin * (3) interrupts must be enabled. 161162695a84SNick Piggin */ 161262695a84SNick Piggin int isolate_lru_page(struct page *page) 161362695a84SNick Piggin { 161462695a84SNick Piggin int ret = -EBUSY; 161562695a84SNick Piggin 1616309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 1617cf2a82eeSKirill A. Shutemov WARN_RATELIMIT(PageTail(page), "trying to isolate tail page"); 16180c917313SKonstantin Khlebnikov 161962695a84SNick Piggin if (PageLRU(page)) { 162062695a84SNick Piggin struct zone *zone = page_zone(page); 1621fa9add64SHugh Dickins struct lruvec *lruvec; 162262695a84SNick Piggin 1623a52633d8SMel Gorman spin_lock_irq(zone_lru_lock(zone)); 1624599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat); 16250c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1626894bc310SLee Schermerhorn int lru = page_lru(page); 16270c917313SKonstantin Khlebnikov get_page(page); 162862695a84SNick Piggin ClearPageLRU(page); 1629fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1630fa9add64SHugh Dickins ret = 0; 163162695a84SNick Piggin } 1632a52633d8SMel Gorman spin_unlock_irq(zone_lru_lock(zone)); 163362695a84SNick Piggin } 163462695a84SNick Piggin return ret; 163562695a84SNick Piggin } 163662695a84SNick Piggin 16375ad333ebSAndy Whitcroft /* 1638d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1639d37dd5dcSFengguang Wu * then get resheduled. When there are massive number of tasks doing page 1640d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1641d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1642d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 164335cd7815SRik van Riel */ 1644599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 164535cd7815SRik van Riel struct scan_control *sc) 164635cd7815SRik van Riel { 164735cd7815SRik van Riel unsigned long inactive, isolated; 164835cd7815SRik van Riel 164935cd7815SRik van Riel if (current_is_kswapd()) 165035cd7815SRik van Riel return 0; 165135cd7815SRik van Riel 165297c9341fSTejun Heo if (!sane_reclaim(sc)) 165335cd7815SRik van Riel return 0; 165435cd7815SRik van Riel 165535cd7815SRik van Riel if (file) { 1656599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 1657599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 165835cd7815SRik van Riel } else { 1659599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 1660599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 166135cd7815SRik van Riel } 166235cd7815SRik van Riel 16633cf23841SFengguang Wu /* 16643cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 16653cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 16663cf23841SFengguang Wu * deadlock. 16673cf23841SFengguang Wu */ 1668d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 16693cf23841SFengguang Wu inactive >>= 3; 16703cf23841SFengguang Wu 167135cd7815SRik van Riel return isolated > inactive; 167235cd7815SRik van Riel } 167335cd7815SRik van Riel 167466635629SMel Gorman static noinline_for_stack void 167575b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) 167666635629SMel Gorman { 167727ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 1678599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 16793f79768fSHugh Dickins LIST_HEAD(pages_to_free); 168066635629SMel Gorman 168166635629SMel Gorman /* 168266635629SMel Gorman * Put back any unfreeable pages. 168366635629SMel Gorman */ 168466635629SMel Gorman while (!list_empty(page_list)) { 16853f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 168666635629SMel Gorman int lru; 16873f79768fSHugh Dickins 1688309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 168966635629SMel Gorman list_del(&page->lru); 169039b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1691599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 169266635629SMel Gorman putback_lru_page(page); 1693599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 169466635629SMel Gorman continue; 169566635629SMel Gorman } 1696fa9add64SHugh Dickins 1697599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 1698fa9add64SHugh Dickins 16997a608572SLinus Torvalds SetPageLRU(page); 170066635629SMel Gorman lru = page_lru(page); 1701fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 1702fa9add64SHugh Dickins 170366635629SMel Gorman if (is_active_lru(lru)) { 170466635629SMel Gorman int file = is_file_lru(lru); 17059992af10SRik van Riel int numpages = hpage_nr_pages(page); 17069992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 170766635629SMel Gorman } 17082bcf8879SHugh Dickins if (put_page_testzero(page)) { 17092bcf8879SHugh Dickins __ClearPageLRU(page); 17102bcf8879SHugh Dickins __ClearPageActive(page); 1711fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 17122bcf8879SHugh Dickins 17132bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 1714599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1715747db954SJohannes Weiner mem_cgroup_uncharge(page); 17162bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 1717599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 17182bcf8879SHugh Dickins } else 17192bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 172066635629SMel Gorman } 172166635629SMel Gorman } 172266635629SMel Gorman 17233f79768fSHugh Dickins /* 17243f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 17253f79768fSHugh Dickins */ 17263f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 172766635629SMel Gorman } 172866635629SMel Gorman 172966635629SMel Gorman /* 1730399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 1731399ba0b9SNeilBrown * a backing device by writing to the page cache it sets PF_LESS_THROTTLE. 1732399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 1733399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 1734399ba0b9SNeilBrown */ 1735399ba0b9SNeilBrown static int current_may_throttle(void) 1736399ba0b9SNeilBrown { 1737399ba0b9SNeilBrown return !(current->flags & PF_LESS_THROTTLE) || 1738399ba0b9SNeilBrown current->backing_dev_info == NULL || 1739399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 1740399ba0b9SNeilBrown } 1741399ba0b9SNeilBrown 1742399ba0b9SNeilBrown /* 1743b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 17441742f19fSAndrew Morton * of reclaimed pages 17451da177e4SLinus Torvalds */ 174666635629SMel Gorman static noinline_for_stack unsigned long 17471a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 17489e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 17491da177e4SLinus Torvalds { 17501da177e4SLinus Torvalds LIST_HEAD(page_list); 1751e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 175205ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1753e247dbceSKOSAKI Motohiro unsigned long nr_taken; 17543c710c1aSMichal Hocko struct reclaim_stat stat = {}; 1755f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 17563cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1757599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 17581a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 1759db73ee0dSMichal Hocko bool stalled = false; 176078dc583dSKOSAKI Motohiro 1761599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 1762db73ee0dSMichal Hocko if (stalled) 1763db73ee0dSMichal Hocko return 0; 1764db73ee0dSMichal Hocko 1765db73ee0dSMichal Hocko /* wait a bit for the reclaimer. */ 1766db73ee0dSMichal Hocko msleep(100); 1767db73ee0dSMichal Hocko stalled = true; 176835cd7815SRik van Riel 176935cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 177035cd7815SRik van Riel if (fatal_signal_pending(current)) 177135cd7815SRik van Riel return SWAP_CLUSTER_MAX; 177235cd7815SRik van Riel } 177335cd7815SRik van Riel 17741da177e4SLinus Torvalds lru_add_drain(); 1775f80c0673SMinchan Kim 1776f80c0673SMinchan Kim if (!sc->may_unmap) 177761317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1778f80c0673SMinchan Kim 1779599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 17801da177e4SLinus Torvalds 17815dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 17825dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 178395d918fcSKonstantin Khlebnikov 1784599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 17859d5e6a9fSHugh Dickins reclaim_stat->recent_scanned[file] += nr_taken; 178695d918fcSKonstantin Khlebnikov 17872262185cSRoman Gushchin if (current_is_kswapd()) { 17882262185cSRoman Gushchin if (global_reclaim(sc)) 1789599d0c95SMel Gorman __count_vm_events(PGSCAN_KSWAPD, nr_scanned); 17902262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD, 17912262185cSRoman Gushchin nr_scanned); 17922262185cSRoman Gushchin } else { 17932262185cSRoman Gushchin if (global_reclaim(sc)) 1794599d0c95SMel Gorman __count_vm_events(PGSCAN_DIRECT, nr_scanned); 17952262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSCAN_DIRECT, 17962262185cSRoman Gushchin nr_scanned); 1797b35ea17bSKOSAKI Motohiro } 1798599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1799d563c050SHillf Danton 1800d563c050SHillf Danton if (nr_taken == 0) 180166635629SMel Gorman return 0; 1802b35ea17bSKOSAKI Motohiro 1803a128ca71SShaohua Li nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0, 18043c710c1aSMichal Hocko &stat, false); 1805c661b078SAndy Whitcroft 1806599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 18073f79768fSHugh Dickins 18082262185cSRoman Gushchin if (current_is_kswapd()) { 18092262185cSRoman Gushchin if (global_reclaim(sc)) 1810599d0c95SMel Gorman __count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed); 18112262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_KSWAPD, 18122262185cSRoman Gushchin nr_reclaimed); 18132262185cSRoman Gushchin } else { 18142262185cSRoman Gushchin if (global_reclaim(sc)) 1815599d0c95SMel Gorman __count_vm_events(PGSTEAL_DIRECT, nr_reclaimed); 18162262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_DIRECT, 18172262185cSRoman Gushchin nr_reclaimed); 1818904249aaSYing Han } 1819a74609faSNick Piggin 182027ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 18213f79768fSHugh Dickins 1822599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 18233f79768fSHugh Dickins 1824599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 18253f79768fSHugh Dickins 1826747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 1827*2d4894b5SMel Gorman free_unref_page_list(&page_list); 1828e11da5b4SMel Gorman 182992df3a72SMel Gorman /* 183092df3a72SMel Gorman * If reclaim is isolating dirty pages under writeback, it implies 183192df3a72SMel Gorman * that the long-lived page allocation rate is exceeding the page 183292df3a72SMel Gorman * laundering rate. Either the global limits are not being effective 183392df3a72SMel Gorman * at throttling processes due to the page distribution throughout 183492df3a72SMel Gorman * zones or there is heavy usage of a slow backing device. The 183592df3a72SMel Gorman * only option is to throttle from reclaim context which is not ideal 183692df3a72SMel Gorman * as there is no guarantee the dirtying process is throttled in the 183792df3a72SMel Gorman * same way balance_dirty_pages() manages. 183892df3a72SMel Gorman * 18398e950282SMel Gorman * Once a zone is flagged ZONE_WRITEBACK, kswapd will count the number 18408e950282SMel Gorman * of pages under pages flagged for immediate reclaim and stall if any 18418e950282SMel Gorman * are encountered in the nr_immediate check below. 184292df3a72SMel Gorman */ 18433c710c1aSMichal Hocko if (stat.nr_writeback && stat.nr_writeback == nr_taken) 1844599d0c95SMel Gorman set_bit(PGDAT_WRITEBACK, &pgdat->flags); 184592df3a72SMel Gorman 1846d43006d5SMel Gorman /* 184797c9341fSTejun Heo * Legacy memcg will stall in page writeback so avoid forcibly 184897c9341fSTejun Heo * stalling here. 1849d43006d5SMel Gorman */ 185097c9341fSTejun Heo if (sane_reclaim(sc)) { 1851b1a6f21eSMel Gorman /* 18528e950282SMel Gorman * Tag a zone as congested if all the dirty pages scanned were 18538e950282SMel Gorman * backed by a congested BDI and wait_iff_congested will stall. 18548e950282SMel Gorman */ 18553c710c1aSMichal Hocko if (stat.nr_dirty && stat.nr_dirty == stat.nr_congested) 1856599d0c95SMel Gorman set_bit(PGDAT_CONGESTED, &pgdat->flags); 18578e950282SMel Gorman 18588e950282SMel Gorman /* 1859b1a6f21eSMel Gorman * If dirty pages are scanned that are not queued for IO, it 1860726d061fSJohannes Weiner * implies that flushers are not doing their job. This can 1861726d061fSJohannes Weiner * happen when memory pressure pushes dirty pages to the end of 1862726d061fSJohannes Weiner * the LRU before the dirty limits are breached and the dirty 1863726d061fSJohannes Weiner * data has expired. It can also happen when the proportion of 1864726d061fSJohannes Weiner * dirty pages grows not through writes but through memory 1865726d061fSJohannes Weiner * pressure reclaiming all the clean cache. And in some cases, 1866726d061fSJohannes Weiner * the flushers simply cannot keep up with the allocation 1867726d061fSJohannes Weiner * rate. Nudge the flusher threads in case they are asleep, but 1868726d061fSJohannes Weiner * also allow kswapd to start writing pages during reclaim. 1869b1a6f21eSMel Gorman */ 1870726d061fSJohannes Weiner if (stat.nr_unqueued_dirty == nr_taken) { 18719ba4b2dfSJens Axboe wakeup_flusher_threads(WB_REASON_VMSCAN); 1872599d0c95SMel Gorman set_bit(PGDAT_DIRTY, &pgdat->flags); 1873726d061fSJohannes Weiner } 1874b1a6f21eSMel Gorman 1875b1a6f21eSMel Gorman /* 1876b738d764SLinus Torvalds * If kswapd scans pages marked marked for immediate 1877b738d764SLinus Torvalds * reclaim and under writeback (nr_immediate), it implies 1878b738d764SLinus Torvalds * that pages are cycling through the LRU faster than 1879b1a6f21eSMel Gorman * they are written so also forcibly stall. 1880b1a6f21eSMel Gorman */ 18813c710c1aSMichal Hocko if (stat.nr_immediate && current_may_throttle()) 1882b1a6f21eSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 1883e2be15f6SMel Gorman } 1884d43006d5SMel Gorman 18858e950282SMel Gorman /* 18868e950282SMel Gorman * Stall direct reclaim for IO completions if underlying BDIs or zone 18878e950282SMel Gorman * is congested. Allow kswapd to continue until it starts encountering 18888e950282SMel Gorman * unqueued dirty pages or cycling through the LRU too quickly. 18898e950282SMel Gorman */ 1890399ba0b9SNeilBrown if (!sc->hibernation_mode && !current_is_kswapd() && 1891399ba0b9SNeilBrown current_may_throttle()) 1892599d0c95SMel Gorman wait_iff_congested(pgdat, BLK_RW_ASYNC, HZ/10); 18938e950282SMel Gorman 1894599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 1895599d0c95SMel Gorman nr_scanned, nr_reclaimed, 18965bccd166SMichal Hocko stat.nr_dirty, stat.nr_writeback, 18975bccd166SMichal Hocko stat.nr_congested, stat.nr_immediate, 18985bccd166SMichal Hocko stat.nr_activate, stat.nr_ref_keep, 18995bccd166SMichal Hocko stat.nr_unmap_fail, 1900ba5e9579Syalin wang sc->priority, file); 190105ff5137SAndrew Morton return nr_reclaimed; 19021da177e4SLinus Torvalds } 19031da177e4SLinus Torvalds 19043bb1a852SMartin Bligh /* 19051cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 19061cfb419bSKAMEZAWA Hiroyuki * 19071cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 19081cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 19091cfb419bSKAMEZAWA Hiroyuki * 19101cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 1911a52633d8SMel Gorman * appropriate to hold zone_lru_lock across the whole operation. But if 19121cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 1913a52633d8SMel Gorman * should drop zone_lru_lock around each page. It's impossible to balance 19141cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 19151cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 19161cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 19171cfb419bSKAMEZAWA Hiroyuki * 19180139aa7bSJoonsoo Kim * The downside is that we have to touch page->_refcount against each page. 19191cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 19209d998b4fSMichal Hocko * 19219d998b4fSMichal Hocko * Returns the number of pages moved to the given lru. 19221cfb419bSKAMEZAWA Hiroyuki */ 19231cfb419bSKAMEZAWA Hiroyuki 19249d998b4fSMichal Hocko static unsigned move_active_pages_to_lru(struct lruvec *lruvec, 19253eb4140fSWu Fengguang struct list_head *list, 19262bcf8879SHugh Dickins struct list_head *pages_to_free, 19273eb4140fSWu Fengguang enum lru_list lru) 19283eb4140fSWu Fengguang { 1929599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 19303eb4140fSWu Fengguang struct page *page; 1931fa9add64SHugh Dickins int nr_pages; 19329d998b4fSMichal Hocko int nr_moved = 0; 19333eb4140fSWu Fengguang 19343eb4140fSWu Fengguang while (!list_empty(list)) { 19353eb4140fSWu Fengguang page = lru_to_page(list); 1936599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 19373eb4140fSWu Fengguang 1938309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 19393eb4140fSWu Fengguang SetPageLRU(page); 19403eb4140fSWu Fengguang 1941fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1942599d0c95SMel Gorman update_lru_size(lruvec, lru, page_zonenum(page), nr_pages); 1943925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 19443eb4140fSWu Fengguang 19452bcf8879SHugh Dickins if (put_page_testzero(page)) { 19462bcf8879SHugh Dickins __ClearPageLRU(page); 19472bcf8879SHugh Dickins __ClearPageActive(page); 1948fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 19492bcf8879SHugh Dickins 19502bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 1951599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1952747db954SJohannes Weiner mem_cgroup_uncharge(page); 19532bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 1954599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19552bcf8879SHugh Dickins } else 19562bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 19579d998b4fSMichal Hocko } else { 19589d998b4fSMichal Hocko nr_moved += nr_pages; 19593eb4140fSWu Fengguang } 19603eb4140fSWu Fengguang } 19619d5e6a9fSHugh Dickins 19622262185cSRoman Gushchin if (!is_active_lru(lru)) { 1963f0958906SMichal Hocko __count_vm_events(PGDEACTIVATE, nr_moved); 19642262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, 19652262185cSRoman Gushchin nr_moved); 19662262185cSRoman Gushchin } 19679d998b4fSMichal Hocko 19689d998b4fSMichal Hocko return nr_moved; 19693eb4140fSWu Fengguang } 19701cfb419bSKAMEZAWA Hiroyuki 1971f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 19721a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 1973f16015fbSJohannes Weiner struct scan_control *sc, 19749e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 19751cfb419bSKAMEZAWA Hiroyuki { 197644c241f1SKOSAKI Motohiro unsigned long nr_taken; 1977f626012dSHugh Dickins unsigned long nr_scanned; 19786fe6b7e3SWu Fengguang unsigned long vm_flags; 19791cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 19808cab4754SWu Fengguang LIST_HEAD(l_active); 1981b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 19821cfb419bSKAMEZAWA Hiroyuki struct page *page; 19831a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 19849d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 19859d998b4fSMichal Hocko unsigned nr_rotated = 0; 1986f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 19873cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1988599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 19891cfb419bSKAMEZAWA Hiroyuki 19901da177e4SLinus Torvalds lru_add_drain(); 1991f80c0673SMinchan Kim 1992f80c0673SMinchan Kim if (!sc->may_unmap) 199361317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1994f80c0673SMinchan Kim 1995599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 1996925b7673SJohannes Weiner 19975dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 19985dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 199989b5fae5SJohannes Weiner 2000599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 2001b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 20021cfb419bSKAMEZAWA Hiroyuki 2003599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 20042262185cSRoman Gushchin count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned); 20059d5e6a9fSHugh Dickins 2006599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 20091da177e4SLinus Torvalds cond_resched(); 20101da177e4SLinus Torvalds page = lru_to_page(&l_hold); 20111da177e4SLinus Torvalds list_del(&page->lru); 20127e9cd484SRik van Riel 201339b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 2014894bc310SLee Schermerhorn putback_lru_page(page); 2015894bc310SLee Schermerhorn continue; 2016894bc310SLee Schermerhorn } 2017894bc310SLee Schermerhorn 2018cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 2019cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 2020cc715d99SMel Gorman if (page_has_private(page)) 2021cc715d99SMel Gorman try_to_release_page(page, 0); 2022cc715d99SMel Gorman unlock_page(page); 2023cc715d99SMel Gorman } 2024cc715d99SMel Gorman } 2025cc715d99SMel Gorman 2026c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 2027c3ac9a8aSJohannes Weiner &vm_flags)) { 20289992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 20298cab4754SWu Fengguang /* 20308cab4754SWu Fengguang * Identify referenced, file-backed active pages and 20318cab4754SWu Fengguang * give them one more trip around the active list. So 20328cab4754SWu Fengguang * that executable code get better chances to stay in 20338cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 20348cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 20358cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 20368cab4754SWu Fengguang * so we ignore them here. 20378cab4754SWu Fengguang */ 203841e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 20398cab4754SWu Fengguang list_add(&page->lru, &l_active); 20408cab4754SWu Fengguang continue; 20418cab4754SWu Fengguang } 20428cab4754SWu Fengguang } 20437e9cd484SRik van Riel 20445205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 20451da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 20461da177e4SLinus Torvalds } 20471da177e4SLinus Torvalds 2048b555749aSAndrew Morton /* 20498cab4754SWu Fengguang * Move pages back to the lru list. 2050b555749aSAndrew Morton */ 2051599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 20524f98a2feSRik van Riel /* 20538cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 20548cab4754SWu Fengguang * even though only some of them are actually re-activated. This 20558cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 20567c0db9e9SJerome Marchand * get_scan_count. 2057556adecbSRik van Riel */ 2058b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 2059556adecbSRik van Riel 20609d998b4fSMichal Hocko nr_activate = move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru); 20619d998b4fSMichal Hocko nr_deactivate = move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE); 2062599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 2063599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 20642bcf8879SHugh Dickins 2065747db954SJohannes Weiner mem_cgroup_uncharge_list(&l_hold); 2066*2d4894b5SMel Gorman free_unref_page_list(&l_hold); 20679d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 20689d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 20691da177e4SLinus Torvalds } 20701da177e4SLinus Torvalds 207159dc76b0SRik van Riel /* 207259dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 207359dc76b0SRik van Riel * to do too much work. 207414797e23SKOSAKI Motohiro * 207559dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 207659dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 207759dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 207859dc76b0SRik van Riel * 207959dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 208059dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 208159dc76b0SRik van Riel * 20822a2e4885SJohannes Weiner * If that fails and refaulting is observed, the inactive list grows. 20832a2e4885SJohannes Weiner * 208459dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 20853a50d14dSAndrey Ryabinin * on this LRU, maintained by the pageout code. An inactive_ratio 208659dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 208759dc76b0SRik van Riel * 208859dc76b0SRik van Riel * total target max 208959dc76b0SRik van Riel * memory ratio inactive 209059dc76b0SRik van Riel * ------------------------------------- 209159dc76b0SRik van Riel * 10MB 1 5MB 209259dc76b0SRik van Riel * 100MB 1 50MB 209359dc76b0SRik van Riel * 1GB 3 250MB 209459dc76b0SRik van Riel * 10GB 10 0.9GB 209559dc76b0SRik van Riel * 100GB 31 3GB 209659dc76b0SRik van Riel * 1TB 101 10GB 209759dc76b0SRik van Riel * 10TB 320 32GB 209814797e23SKOSAKI Motohiro */ 2099f8d1a311SMel Gorman static bool inactive_list_is_low(struct lruvec *lruvec, bool file, 21002a2e4885SJohannes Weiner struct mem_cgroup *memcg, 21012a2e4885SJohannes Weiner struct scan_control *sc, bool actual_reclaim) 210214797e23SKOSAKI Motohiro { 2103fd538803SMichal Hocko enum lru_list active_lru = file * LRU_FILE + LRU_ACTIVE; 21042a2e4885SJohannes Weiner struct pglist_data *pgdat = lruvec_pgdat(lruvec); 21052a2e4885SJohannes Weiner enum lru_list inactive_lru = file * LRU_FILE; 21062a2e4885SJohannes Weiner unsigned long inactive, active; 21072a2e4885SJohannes Weiner unsigned long inactive_ratio; 21082a2e4885SJohannes Weiner unsigned long refaults; 210959dc76b0SRik van Riel unsigned long gb; 211059dc76b0SRik van Riel 211174e3f3c3SMinchan Kim /* 211274e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 211374e3f3c3SMinchan Kim * is pointless. 211474e3f3c3SMinchan Kim */ 211559dc76b0SRik van Riel if (!file && !total_swap_pages) 211642e2e457SYaowei Bai return false; 211774e3f3c3SMinchan Kim 2118fd538803SMichal Hocko inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx); 2119fd538803SMichal Hocko active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx); 2120f8d1a311SMel Gorman 21212a2e4885SJohannes Weiner if (memcg) 2122ccda7f43SJohannes Weiner refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE); 21232a2e4885SJohannes Weiner else 21242a2e4885SJohannes Weiner refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE); 21252a2e4885SJohannes Weiner 21262a2e4885SJohannes Weiner /* 21272a2e4885SJohannes Weiner * When refaults are being observed, it means a new workingset 21282a2e4885SJohannes Weiner * is being established. Disable active list protection to get 21292a2e4885SJohannes Weiner * rid of the stale workingset quickly. 21302a2e4885SJohannes Weiner */ 21312a2e4885SJohannes Weiner if (file && actual_reclaim && lruvec->refaults != refaults) { 21322a2e4885SJohannes Weiner inactive_ratio = 0; 21332a2e4885SJohannes Weiner } else { 213459dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 213559dc76b0SRik van Riel if (gb) 213659dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2137b39415b2SRik van Riel else 213859dc76b0SRik van Riel inactive_ratio = 1; 21392a2e4885SJohannes Weiner } 214059dc76b0SRik van Riel 21412a2e4885SJohannes Weiner if (actual_reclaim) 21422a2e4885SJohannes Weiner trace_mm_vmscan_inactive_list_is_low(pgdat->node_id, sc->reclaim_idx, 2143fd538803SMichal Hocko lruvec_lru_size(lruvec, inactive_lru, MAX_NR_ZONES), inactive, 2144fd538803SMichal Hocko lruvec_lru_size(lruvec, active_lru, MAX_NR_ZONES), active, 2145fd538803SMichal Hocko inactive_ratio, file); 2146fd538803SMichal Hocko 214759dc76b0SRik van Riel return inactive * inactive_ratio < active; 2148b39415b2SRik van Riel } 2149b39415b2SRik van Riel 21504f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 21512a2e4885SJohannes Weiner struct lruvec *lruvec, struct mem_cgroup *memcg, 21522a2e4885SJohannes Weiner struct scan_control *sc) 2153b69408e8SChristoph Lameter { 2154b39415b2SRik van Riel if (is_active_lru(lru)) { 21552a2e4885SJohannes Weiner if (inactive_list_is_low(lruvec, is_file_lru(lru), 21562a2e4885SJohannes Weiner memcg, sc, true)) 21571a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 2158556adecbSRik van Riel return 0; 2159556adecbSRik van Riel } 2160556adecbSRik van Riel 21611a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2162b69408e8SChristoph Lameter } 2163b69408e8SChristoph Lameter 21649a265114SJohannes Weiner enum scan_balance { 21659a265114SJohannes Weiner SCAN_EQUAL, 21669a265114SJohannes Weiner SCAN_FRACT, 21679a265114SJohannes Weiner SCAN_ANON, 21689a265114SJohannes Weiner SCAN_FILE, 21699a265114SJohannes Weiner }; 21709a265114SJohannes Weiner 21711da177e4SLinus Torvalds /* 21724f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 21734f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 21744f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 21754f98a2feSRik van Riel * onto the active list instead of evict. 21764f98a2feSRik van Riel * 2177be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2178be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 21794f98a2feSRik van Riel */ 218033377678SVladimir Davydov static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, 21816b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *nr, 21826b4f7799SJohannes Weiner unsigned long *lru_pages) 21834f98a2feSRik van Riel { 218433377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 218590126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 21869a265114SJohannes Weiner u64 fraction[2]; 21879a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 2188599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 21899a265114SJohannes Weiner unsigned long anon_prio, file_prio; 21909a265114SJohannes Weiner enum scan_balance scan_balance; 21910bf1457fSJohannes Weiner unsigned long anon, file; 21929a265114SJohannes Weiner unsigned long ap, fp; 21939a265114SJohannes Weiner enum lru_list lru; 219476a33fc3SShaohua Li 219576a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2196d8b38438SVladimir Davydov if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) { 21979a265114SJohannes Weiner scan_balance = SCAN_FILE; 219876a33fc3SShaohua Li goto out; 219976a33fc3SShaohua Li } 22004f98a2feSRik van Riel 220110316b31SJohannes Weiner /* 220210316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 220310316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 220410316b31SJohannes Weiner * disable swapping for individual groups completely when 220510316b31SJohannes Weiner * using the memory controller's swap limit feature would be 220610316b31SJohannes Weiner * too expensive. 220710316b31SJohannes Weiner */ 220802695175SJohannes Weiner if (!global_reclaim(sc) && !swappiness) { 22099a265114SJohannes Weiner scan_balance = SCAN_FILE; 221010316b31SJohannes Weiner goto out; 221110316b31SJohannes Weiner } 221210316b31SJohannes Weiner 221310316b31SJohannes Weiner /* 221410316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 221510316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 221610316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 221710316b31SJohannes Weiner */ 221802695175SJohannes Weiner if (!sc->priority && swappiness) { 22199a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 222010316b31SJohannes Weiner goto out; 222110316b31SJohannes Weiner } 222210316b31SJohannes Weiner 222311d16c25SJohannes Weiner /* 222462376251SJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 222562376251SJohannes Weiner * cache pages start out inactive, every cache fault will tip 222662376251SJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 222762376251SJohannes Weiner * shrinks, so does the window for rotation from references. 222862376251SJohannes Weiner * This means we have a runaway feedback loop where a tiny 222962376251SJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 223062376251SJohannes Weiner * anon pages. Try to detect this based on file LRU size. 223162376251SJohannes Weiner */ 223262376251SJohannes Weiner if (global_reclaim(sc)) { 2233599d0c95SMel Gorman unsigned long pgdatfile; 2234599d0c95SMel Gorman unsigned long pgdatfree; 2235599d0c95SMel Gorman int z; 2236599d0c95SMel Gorman unsigned long total_high_wmark = 0; 223762376251SJohannes Weiner 2238599d0c95SMel Gorman pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 2239599d0c95SMel Gorman pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) + 2240599d0c95SMel Gorman node_page_state(pgdat, NR_INACTIVE_FILE); 22412ab051e1SJerome Marchand 2242599d0c95SMel Gorman for (z = 0; z < MAX_NR_ZONES; z++) { 2243599d0c95SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 22446aa303deSMel Gorman if (!managed_zone(zone)) 2245599d0c95SMel Gorman continue; 2246599d0c95SMel Gorman 2247599d0c95SMel Gorman total_high_wmark += high_wmark_pages(zone); 2248599d0c95SMel Gorman } 2249599d0c95SMel Gorman 2250599d0c95SMel Gorman if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) { 225106226226SDavid Rientjes /* 225206226226SDavid Rientjes * Force SCAN_ANON if there are enough inactive 225306226226SDavid Rientjes * anonymous pages on the LRU in eligible zones. 225406226226SDavid Rientjes * Otherwise, the small LRU gets thrashed. 225506226226SDavid Rientjes */ 225606226226SDavid Rientjes if (!inactive_list_is_low(lruvec, false, memcg, sc, false) && 225706226226SDavid Rientjes lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, sc->reclaim_idx) 225806226226SDavid Rientjes >> sc->priority) { 225962376251SJohannes Weiner scan_balance = SCAN_ANON; 226062376251SJohannes Weiner goto out; 226162376251SJohannes Weiner } 226262376251SJohannes Weiner } 226306226226SDavid Rientjes } 226462376251SJohannes Weiner 226562376251SJohannes Weiner /* 2266316bda0eSVladimir Davydov * If there is enough inactive page cache, i.e. if the size of the 2267316bda0eSVladimir Davydov * inactive list is greater than that of the active list *and* the 2268316bda0eSVladimir Davydov * inactive list actually has some pages to scan on this priority, we 2269316bda0eSVladimir Davydov * do not reclaim anything from the anonymous working set right now. 2270316bda0eSVladimir Davydov * Without the second condition we could end up never scanning an 2271316bda0eSVladimir Davydov * lruvec even if it has plenty of old anonymous pages unless the 2272316bda0eSVladimir Davydov * system is under heavy pressure. 2273e9868505SRik van Riel */ 22742a2e4885SJohannes Weiner if (!inactive_list_is_low(lruvec, true, memcg, sc, false) && 227571ab6cfeSMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) { 22769a265114SJohannes Weiner scan_balance = SCAN_FILE; 2277e9868505SRik van Riel goto out; 22784f98a2feSRik van Riel } 22794f98a2feSRik van Riel 22809a265114SJohannes Weiner scan_balance = SCAN_FRACT; 22819a265114SJohannes Weiner 22824f98a2feSRik van Riel /* 228358c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 228458c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 228558c37f6eSKOSAKI Motohiro */ 228602695175SJohannes Weiner anon_prio = swappiness; 228775b00af7SHugh Dickins file_prio = 200 - anon_prio; 228858c37f6eSKOSAKI Motohiro 228958c37f6eSKOSAKI Motohiro /* 22904f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 22914f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 22924f98a2feSRik van Riel * ratios to determine how valuable each cache is. 22934f98a2feSRik van Riel * 22944f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 22954f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 22964f98a2feSRik van Riel * up weighing recent references more than old ones. 22974f98a2feSRik van Riel * 22984f98a2feSRik van Riel * anon in [0], file in [1] 22994f98a2feSRik van Riel */ 23002ab051e1SJerome Marchand 2301fd538803SMichal Hocko anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) + 2302fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES); 2303fd538803SMichal Hocko file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) + 2304fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES); 23052ab051e1SJerome Marchand 2306599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 230758c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 23086e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 23096e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 23104f98a2feSRik van Riel } 23114f98a2feSRik van Riel 23126e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 23136e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 23146e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 23154f98a2feSRik van Riel } 23164f98a2feSRik van Riel 23174f98a2feSRik van Riel /* 231800d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 231900d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 232000d8089cSRik van Riel * each list that were recently referenced and in active use. 23214f98a2feSRik van Riel */ 2322fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 23236e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 23244f98a2feSRik van Riel 2325fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 23266e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 2327599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 23284f98a2feSRik van Riel 232976a33fc3SShaohua Li fraction[0] = ap; 233076a33fc3SShaohua Li fraction[1] = fp; 233176a33fc3SShaohua Li denominator = ap + fp + 1; 233276a33fc3SShaohua Li out: 23336b4f7799SJohannes Weiner *lru_pages = 0; 23344111304dSHugh Dickins for_each_evictable_lru(lru) { 23354111304dSHugh Dickins int file = is_file_lru(lru); 2336d778df51SJohannes Weiner unsigned long size; 233776a33fc3SShaohua Li unsigned long scan; 233876a33fc3SShaohua Li 233971ab6cfeSMichal Hocko size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 2340d778df51SJohannes Weiner scan = size >> sc->priority; 2341688035f7SJohannes Weiner /* 2342688035f7SJohannes Weiner * If the cgroup's already been deleted, make sure to 2343688035f7SJohannes Weiner * scrape out the remaining cache. 2344688035f7SJohannes Weiner */ 2345688035f7SJohannes Weiner if (!scan && !mem_cgroup_online(memcg)) 2346d778df51SJohannes Weiner scan = min(size, SWAP_CLUSTER_MAX); 23479a265114SJohannes Weiner 23489a265114SJohannes Weiner switch (scan_balance) { 23499a265114SJohannes Weiner case SCAN_EQUAL: 23509a265114SJohannes Weiner /* Scan lists relative to size */ 23519a265114SJohannes Weiner break; 23529a265114SJohannes Weiner case SCAN_FRACT: 23539a265114SJohannes Weiner /* 23549a265114SJohannes Weiner * Scan types proportional to swappiness and 23559a265114SJohannes Weiner * their relative recent reclaim efficiency. 23569a265114SJohannes Weiner */ 23576f04f48dSSuleiman Souhlal scan = div64_u64(scan * fraction[file], 23586f04f48dSSuleiman Souhlal denominator); 23599a265114SJohannes Weiner break; 23609a265114SJohannes Weiner case SCAN_FILE: 23619a265114SJohannes Weiner case SCAN_ANON: 23629a265114SJohannes Weiner /* Scan one type exclusively */ 23636b4f7799SJohannes Weiner if ((scan_balance == SCAN_FILE) != file) { 23646b4f7799SJohannes Weiner size = 0; 23659a265114SJohannes Weiner scan = 0; 23666b4f7799SJohannes Weiner } 23679a265114SJohannes Weiner break; 23689a265114SJohannes Weiner default: 23699a265114SJohannes Weiner /* Look ma, no brain */ 23709a265114SJohannes Weiner BUG(); 23719a265114SJohannes Weiner } 23726b4f7799SJohannes Weiner 23736b4f7799SJohannes Weiner *lru_pages += size; 23744111304dSHugh Dickins nr[lru] = scan; 237576a33fc3SShaohua Li } 23766e08a369SWu Fengguang } 23774f98a2feSRik van Riel 23789b4f98cdSJohannes Weiner /* 2379a9dd0a83SMel Gorman * This is a basic per-node page freer. Used by both kswapd and direct reclaim. 23809b4f98cdSJohannes Weiner */ 2381a9dd0a83SMel Gorman static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg, 23826b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *lru_pages) 23839b4f98cdSJohannes Weiner { 2384ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 23859b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2386e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 23879b4f98cdSJohannes Weiner unsigned long nr_to_scan; 23889b4f98cdSJohannes Weiner enum lru_list lru; 23899b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 23909b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 23919b4f98cdSJohannes Weiner struct blk_plug plug; 23921a501907SMel Gorman bool scan_adjusted; 23939b4f98cdSJohannes Weiner 239433377678SVladimir Davydov get_scan_count(lruvec, memcg, sc, nr, lru_pages); 23959b4f98cdSJohannes Weiner 2396e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2397e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2398e82e0561SMel Gorman 23991a501907SMel Gorman /* 24001a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 24011a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 24021a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 24031a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 24041a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 24051a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 24061a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 24071a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 24081a501907SMel Gorman * dropped to zero at the first pass. 24091a501907SMel Gorman */ 24101a501907SMel Gorman scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() && 24111a501907SMel Gorman sc->priority == DEF_PRIORITY); 24121a501907SMel Gorman 24139b4f98cdSJohannes Weiner blk_start_plug(&plug); 24149b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 24159b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2416e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2417e82e0561SMel Gorman unsigned long nr_scanned; 2418e82e0561SMel Gorman 24199b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 24209b4f98cdSJohannes Weiner if (nr[lru]) { 24219b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 24229b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 24239b4f98cdSJohannes Weiner 24249b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 24252a2e4885SJohannes Weiner lruvec, memcg, sc); 24269b4f98cdSJohannes Weiner } 24279b4f98cdSJohannes Weiner } 2428e82e0561SMel Gorman 2429bd041733SMichal Hocko cond_resched(); 2430bd041733SMichal Hocko 2431e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2432e82e0561SMel Gorman continue; 2433e82e0561SMel Gorman 24349b4f98cdSJohannes Weiner /* 2435e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 24361a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2437e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2438e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2439e82e0561SMel Gorman * proportional to the original scan target. 2440e82e0561SMel Gorman */ 2441e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2442e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2443e82e0561SMel Gorman 24441a501907SMel Gorman /* 24451a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 24461a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 24471a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 24481a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 24491a501907SMel Gorman */ 24501a501907SMel Gorman if (!nr_file || !nr_anon) 24511a501907SMel Gorman break; 24521a501907SMel Gorman 2453e82e0561SMel Gorman if (nr_file > nr_anon) { 2454e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2455e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2456e82e0561SMel Gorman lru = LRU_BASE; 2457e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2458e82e0561SMel Gorman } else { 2459e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2460e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2461e82e0561SMel Gorman lru = LRU_FILE; 2462e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2463e82e0561SMel Gorman } 2464e82e0561SMel Gorman 2465e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2466e82e0561SMel Gorman nr[lru] = 0; 2467e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2468e82e0561SMel Gorman 2469e82e0561SMel Gorman /* 2470e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2471e82e0561SMel Gorman * scan target and the percentage scanning already complete 2472e82e0561SMel Gorman */ 2473e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2474e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2475e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2476e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2477e82e0561SMel Gorman 2478e82e0561SMel Gorman lru += LRU_ACTIVE; 2479e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2480e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2481e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2482e82e0561SMel Gorman 2483e82e0561SMel Gorman scan_adjusted = true; 24849b4f98cdSJohannes Weiner } 24859b4f98cdSJohannes Weiner blk_finish_plug(&plug); 24869b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 24879b4f98cdSJohannes Weiner 24889b4f98cdSJohannes Weiner /* 24899b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 24909b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 24919b4f98cdSJohannes Weiner */ 24922a2e4885SJohannes Weiner if (inactive_list_is_low(lruvec, false, memcg, sc, true)) 24939b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 24949b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 24959b4f98cdSJohannes Weiner } 24969b4f98cdSJohannes Weiner 249723b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 24989e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 249923b9da55SMel Gorman { 2500d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 250123b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 25029e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 250323b9da55SMel Gorman return true; 250423b9da55SMel Gorman 250523b9da55SMel Gorman return false; 250623b9da55SMel Gorman } 250723b9da55SMel Gorman 25084f98a2feSRik van Riel /* 250923b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 251023b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 251123b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 251223b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 251323b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 25143e7d3449SMel Gorman */ 2515a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 25163e7d3449SMel Gorman unsigned long nr_reclaimed, 25173e7d3449SMel Gorman unsigned long nr_scanned, 25183e7d3449SMel Gorman struct scan_control *sc) 25193e7d3449SMel Gorman { 25203e7d3449SMel Gorman unsigned long pages_for_compaction; 25213e7d3449SMel Gorman unsigned long inactive_lru_pages; 2522a9dd0a83SMel Gorman int z; 25233e7d3449SMel Gorman 25243e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 25259e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 25263e7d3449SMel Gorman return false; 25273e7d3449SMel Gorman 25282876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 2529dcda9b04SMichal Hocko if (sc->gfp_mask & __GFP_RETRY_MAYFAIL) { 25303e7d3449SMel Gorman /* 2531dcda9b04SMichal Hocko * For __GFP_RETRY_MAYFAIL allocations, stop reclaiming if the 25322876592fSMel Gorman * full LRU list has been scanned and we are still failing 25332876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 2534dcda9b04SMichal Hocko * expensive but a __GFP_RETRY_MAYFAIL caller really wants to succeed 25353e7d3449SMel Gorman */ 25363e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 25373e7d3449SMel Gorman return false; 25382876592fSMel Gorman } else { 25392876592fSMel Gorman /* 2540dcda9b04SMichal Hocko * For non-__GFP_RETRY_MAYFAIL allocations which can presumably 25412876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 25422876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 25432876592fSMel Gorman * pages that were scanned. This will return to the 25442876592fSMel Gorman * caller faster at the risk reclaim/compaction and 25452876592fSMel Gorman * the resulting allocation attempt fails 25462876592fSMel Gorman */ 25472876592fSMel Gorman if (!nr_reclaimed) 25482876592fSMel Gorman return false; 25492876592fSMel Gorman } 25503e7d3449SMel Gorman 25513e7d3449SMel Gorman /* 25523e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 25533e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 25543e7d3449SMel Gorman */ 25559861a62cSVlastimil Babka pages_for_compaction = compact_gap(sc->order); 2556a9dd0a83SMel Gorman inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 2557ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 2558a9dd0a83SMel Gorman inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 25593e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 25603e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 25613e7d3449SMel Gorman return true; 25623e7d3449SMel Gorman 25633e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 2564a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 2565a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 25666aa303deSMel Gorman if (!managed_zone(zone)) 2567a9dd0a83SMel Gorman continue; 2568a9dd0a83SMel Gorman 2569a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 2570cf378319SVlastimil Babka case COMPACT_SUCCESS: 25713e7d3449SMel Gorman case COMPACT_CONTINUE: 25723e7d3449SMel Gorman return false; 25733e7d3449SMel Gorman default: 2574a9dd0a83SMel Gorman /* check next zone */ 2575a9dd0a83SMel Gorman ; 25763e7d3449SMel Gorman } 25773e7d3449SMel Gorman } 2578a9dd0a83SMel Gorman return true; 2579a9dd0a83SMel Gorman } 25803e7d3449SMel Gorman 2581970a39a3SMel Gorman static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) 2582f16015fbSJohannes Weiner { 2583cb731d6cSVladimir Davydov struct reclaim_state *reclaim_state = current->reclaim_state; 25849b4f98cdSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 25852344d7e4SJohannes Weiner bool reclaimable = false; 25869b4f98cdSJohannes Weiner 25879b4f98cdSJohannes Weiner do { 25885660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 25895660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 2590ef8f2327SMel Gorman .pgdat = pgdat, 25919e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 25925660048cSJohannes Weiner }; 2593a9dd0a83SMel Gorman unsigned long node_lru_pages = 0; 2594694fbc0fSAndrew Morton struct mem_cgroup *memcg; 25955660048cSJohannes Weiner 25969b4f98cdSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 25979b4f98cdSJohannes Weiner nr_scanned = sc->nr_scanned; 25989b4f98cdSJohannes Weiner 2599694fbc0fSAndrew Morton memcg = mem_cgroup_iter(root, NULL, &reclaim); 2600694fbc0fSAndrew Morton do { 26016b4f7799SJohannes Weiner unsigned long lru_pages; 26028e8ae645SJohannes Weiner unsigned long reclaimed; 2603cb731d6cSVladimir Davydov unsigned long scanned; 26049b4f98cdSJohannes Weiner 2605241994edSJohannes Weiner if (mem_cgroup_low(root, memcg)) { 2606d6622f63SYisheng Xie if (!sc->memcg_low_reclaim) { 2607d6622f63SYisheng Xie sc->memcg_low_skipped = 1; 2608241994edSJohannes Weiner continue; 2609d6622f63SYisheng Xie } 261031176c78SJohannes Weiner mem_cgroup_event(memcg, MEMCG_LOW); 2611241994edSJohannes Weiner } 2612241994edSJohannes Weiner 26138e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 2614cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 26155660048cSJohannes Weiner 2616a9dd0a83SMel Gorman shrink_node_memcg(pgdat, memcg, sc, &lru_pages); 2617a9dd0a83SMel Gorman node_lru_pages += lru_pages; 2618f9be23d6SKonstantin Khlebnikov 2619b5afba29SVladimir Davydov if (memcg) 2620a9dd0a83SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, 2621cb731d6cSVladimir Davydov memcg, sc->nr_scanned - scanned, 2622cb731d6cSVladimir Davydov lru_pages); 2623cb731d6cSVladimir Davydov 26248e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 26258e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 26268e8ae645SJohannes Weiner sc->nr_scanned - scanned, 26278e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 26288e8ae645SJohannes Weiner 26295660048cSJohannes Weiner /* 2630a394cb8eSMichal Hocko * Direct reclaim and kswapd have to scan all memory 2631a394cb8eSMichal Hocko * cgroups to fulfill the overall scan target for the 2632a9dd0a83SMel Gorman * node. 2633a394cb8eSMichal Hocko * 2634a394cb8eSMichal Hocko * Limit reclaim, on the other hand, only cares about 2635a394cb8eSMichal Hocko * nr_to_reclaim pages to be reclaimed and it will 2636a394cb8eSMichal Hocko * retry with decreasing priority if one round over the 2637a394cb8eSMichal Hocko * whole hierarchy is not sufficient. 26385660048cSJohannes Weiner */ 2639a394cb8eSMichal Hocko if (!global_reclaim(sc) && 2640a394cb8eSMichal Hocko sc->nr_reclaimed >= sc->nr_to_reclaim) { 26415660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 26425660048cSJohannes Weiner break; 26435660048cSJohannes Weiner } 2644241994edSJohannes Weiner } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim))); 264570ddf637SAnton Vorontsov 26466b4f7799SJohannes Weiner /* 26476b4f7799SJohannes Weiner * Shrink the slab caches in the same proportion that 26486b4f7799SJohannes Weiner * the eligible LRU pages were scanned. 26496b4f7799SJohannes Weiner */ 2650b2e18757SMel Gorman if (global_reclaim(sc)) 2651a9dd0a83SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, NULL, 26526b4f7799SJohannes Weiner sc->nr_scanned - nr_scanned, 2653a9dd0a83SMel Gorman node_lru_pages); 26546b4f7799SJohannes Weiner 26556b4f7799SJohannes Weiner if (reclaim_state) { 2656cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 26576b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 26586b4f7799SJohannes Weiner } 26596b4f7799SJohannes Weiner 26608e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 26618e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 266270ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 266370ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 266470ddf637SAnton Vorontsov 26652344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 26662344d7e4SJohannes Weiner reclaimable = true; 26672344d7e4SJohannes Weiner 2668a9dd0a83SMel Gorman } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 26699b4f98cdSJohannes Weiner sc->nr_scanned - nr_scanned, sc)); 26702344d7e4SJohannes Weiner 2671c73322d0SJohannes Weiner /* 2672c73322d0SJohannes Weiner * Kswapd gives up on balancing particular nodes after too 2673c73322d0SJohannes Weiner * many failures to reclaim anything from them and goes to 2674c73322d0SJohannes Weiner * sleep. On reclaim progress, reset the failure counter. A 2675c73322d0SJohannes Weiner * successful direct reclaim run will revive a dormant kswapd. 2676c73322d0SJohannes Weiner */ 2677c73322d0SJohannes Weiner if (reclaimable) 2678c73322d0SJohannes Weiner pgdat->kswapd_failures = 0; 2679c73322d0SJohannes Weiner 26802344d7e4SJohannes Weiner return reclaimable; 2681f16015fbSJohannes Weiner } 2682f16015fbSJohannes Weiner 268353853e2dSVlastimil Babka /* 2684fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 2685fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 2686fdd4c614SVlastimil Babka * should reclaim first. 268753853e2dSVlastimil Babka */ 26884f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 2689fe4b1b24SMel Gorman { 269031483b6aSMel Gorman unsigned long watermark; 2691fdd4c614SVlastimil Babka enum compact_result suitable; 2692fe4b1b24SMel Gorman 2693fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 2694fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 2695fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 2696fdd4c614SVlastimil Babka return true; 2697fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 2698fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 2699fe4b1b24SMel Gorman return false; 2700fe4b1b24SMel Gorman 2701fdd4c614SVlastimil Babka /* 2702fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 2703fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 2704fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 2705fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 2706fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 2707fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 2708fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 2709fdd4c614SVlastimil Babka */ 2710fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 2711fdd4c614SVlastimil Babka 2712fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 2713fe4b1b24SMel Gorman } 2714fe4b1b24SMel Gorman 27151da177e4SLinus Torvalds /* 27161da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 27171da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 27181da177e4SLinus Torvalds * request. 27191da177e4SLinus Torvalds * 27201da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 27211da177e4SLinus Torvalds * scan then give up on it. 27221da177e4SLinus Torvalds */ 27230a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 27241da177e4SLinus Torvalds { 2725dd1a239fSMel Gorman struct zoneref *z; 272654a6eb5cSMel Gorman struct zone *zone; 27270608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 27280608f43dSAndrew Morton unsigned long nr_soft_scanned; 2729619d0d76SWeijie Yang gfp_t orig_mask; 273079dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 27311cfb419bSKAMEZAWA Hiroyuki 2732cc715d99SMel Gorman /* 2733cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2734cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2735cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2736cc715d99SMel Gorman */ 2737619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 2738b2e18757SMel Gorman if (buffer_heads_over_limit) { 2739cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 27404f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 2741b2e18757SMel Gorman } 2742cc715d99SMel Gorman 2743d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2744b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 2745b2e18757SMel Gorman /* 27461cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 27471cfb419bSKAMEZAWA Hiroyuki * to global LRU. 27481cfb419bSKAMEZAWA Hiroyuki */ 274989b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2750344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 2751344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 27521da177e4SLinus Torvalds continue; 275365ec02cbSVladimir Davydov 2754e0887c19SRik van Riel /* 2755e0c23279SMel Gorman * If we already have plenty of memory free for 2756e0c23279SMel Gorman * compaction in this zone, don't free any more. 2757e0c23279SMel Gorman * Even though compaction is invoked for any 2758e0c23279SMel Gorman * non-zero order, only frequent costly order 2759e0c23279SMel Gorman * reclamation is disruptive enough to become a 2760c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2761c7cfa37bSCopot Alexandru * page allocations. 2762e0887c19SRik van Riel */ 27630b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 27640b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 27654f588331SMel Gorman compaction_ready(zone, sc)) { 27660b06496aSJohannes Weiner sc->compaction_ready = true; 2767e0887c19SRik van Riel continue; 2768e0887c19SRik van Riel } 27690b06496aSJohannes Weiner 27700608f43dSAndrew Morton /* 277179dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 277279dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 277379dafcdcSMel Gorman * node may be shrunk multiple times but in that case 277479dafcdcSMel Gorman * the user prefers lower zones being preserved. 277579dafcdcSMel Gorman */ 277679dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 277779dafcdcSMel Gorman continue; 277879dafcdcSMel Gorman 277979dafcdcSMel Gorman /* 27800608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 27810608f43dSAndrew Morton * and returns the number of reclaimed pages and 27820608f43dSAndrew Morton * scanned pages. This works for global memory pressure 27830608f43dSAndrew Morton * and balancing, not for a memcg's limit. 27840608f43dSAndrew Morton */ 27850608f43dSAndrew Morton nr_soft_scanned = 0; 2786ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 27870608f43dSAndrew Morton sc->order, sc->gfp_mask, 27880608f43dSAndrew Morton &nr_soft_scanned); 27890608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 27900608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 2791ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2792ac34a1a3SKAMEZAWA Hiroyuki } 2793d149e3b2SYing Han 279479dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 279579dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 279679dafcdcSMel Gorman continue; 279779dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 2798970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 27991da177e4SLinus Torvalds } 2800e0c23279SMel Gorman 280165ec02cbSVladimir Davydov /* 2802619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 2803619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 2804619d0d76SWeijie Yang */ 2805619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 28061da177e4SLinus Torvalds } 28071da177e4SLinus Torvalds 28082a2e4885SJohannes Weiner static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat) 28092a2e4885SJohannes Weiner { 28102a2e4885SJohannes Weiner struct mem_cgroup *memcg; 28112a2e4885SJohannes Weiner 28122a2e4885SJohannes Weiner memcg = mem_cgroup_iter(root_memcg, NULL, NULL); 28132a2e4885SJohannes Weiner do { 28142a2e4885SJohannes Weiner unsigned long refaults; 28152a2e4885SJohannes Weiner struct lruvec *lruvec; 28162a2e4885SJohannes Weiner 28172a2e4885SJohannes Weiner if (memcg) 2818ccda7f43SJohannes Weiner refaults = memcg_page_state(memcg, WORKINGSET_ACTIVATE); 28192a2e4885SJohannes Weiner else 28202a2e4885SJohannes Weiner refaults = node_page_state(pgdat, WORKINGSET_ACTIVATE); 28212a2e4885SJohannes Weiner 28222a2e4885SJohannes Weiner lruvec = mem_cgroup_lruvec(pgdat, memcg); 28232a2e4885SJohannes Weiner lruvec->refaults = refaults; 28242a2e4885SJohannes Weiner } while ((memcg = mem_cgroup_iter(root_memcg, memcg, NULL))); 28252a2e4885SJohannes Weiner } 28262a2e4885SJohannes Weiner 28271da177e4SLinus Torvalds /* 28281da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 28291da177e4SLinus Torvalds * 28301da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 28311da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 28321da177e4SLinus Torvalds * 28331da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 28341da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 28355b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 28365b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 28375b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 28385b0830cbSJens Axboe * work, and the allocation attempt will fail. 2839a41f24eaSNishanth Aravamudan * 2840a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2841a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 28421da177e4SLinus Torvalds */ 2843dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 28443115cd91SVladimir Davydov struct scan_control *sc) 28451da177e4SLinus Torvalds { 2846241994edSJohannes Weiner int initial_priority = sc->priority; 28472a2e4885SJohannes Weiner pg_data_t *last_pgdat; 28482a2e4885SJohannes Weiner struct zoneref *z; 28492a2e4885SJohannes Weiner struct zone *zone; 2850241994edSJohannes Weiner retry: 2851873b4771SKeika Kobayashi delayacct_freepages_start(); 2852873b4771SKeika Kobayashi 285389b5fae5SJohannes Weiner if (global_reclaim(sc)) 28547cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 28551da177e4SLinus Torvalds 28569e3b2f8cSKonstantin Khlebnikov do { 285770ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 285870ddf637SAnton Vorontsov sc->priority); 285966e1707bSBalbir Singh sc->nr_scanned = 0; 28600a0337e0SMichal Hocko shrink_zones(zonelist, sc); 2861e0c23279SMel Gorman 2862bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 28630b06496aSJohannes Weiner break; 28640b06496aSJohannes Weiner 28650b06496aSJohannes Weiner if (sc->compaction_ready) 28660b06496aSJohannes Weiner break; 28671da177e4SLinus Torvalds 28681da177e4SLinus Torvalds /* 28690e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 28700e50ce3bSMinchan Kim * writepage even in laptop mode. 28710e50ce3bSMinchan Kim */ 28720e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 28730e50ce3bSMinchan Kim sc->may_writepage = 1; 28740b06496aSJohannes Weiner } while (--sc->priority >= 0); 2875bb21c7ceSKOSAKI Motohiro 28762a2e4885SJohannes Weiner last_pgdat = NULL; 28772a2e4885SJohannes Weiner for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx, 28782a2e4885SJohannes Weiner sc->nodemask) { 28792a2e4885SJohannes Weiner if (zone->zone_pgdat == last_pgdat) 28802a2e4885SJohannes Weiner continue; 28812a2e4885SJohannes Weiner last_pgdat = zone->zone_pgdat; 28822a2e4885SJohannes Weiner snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat); 28832a2e4885SJohannes Weiner } 28842a2e4885SJohannes Weiner 2885873b4771SKeika Kobayashi delayacct_freepages_end(); 2886873b4771SKeika Kobayashi 2887bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 2888bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 2889bb21c7ceSKOSAKI Motohiro 28900cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 28910b06496aSJohannes Weiner if (sc->compaction_ready) 28927335084dSMel Gorman return 1; 28937335084dSMel Gorman 2894241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 2895d6622f63SYisheng Xie if (sc->memcg_low_skipped) { 2896241994edSJohannes Weiner sc->priority = initial_priority; 2897d6622f63SYisheng Xie sc->memcg_low_reclaim = 1; 2898d6622f63SYisheng Xie sc->memcg_low_skipped = 0; 2899241994edSJohannes Weiner goto retry; 2900241994edSJohannes Weiner } 2901241994edSJohannes Weiner 2902bb21c7ceSKOSAKI Motohiro return 0; 29031da177e4SLinus Torvalds } 29041da177e4SLinus Torvalds 2905c73322d0SJohannes Weiner static bool allow_direct_reclaim(pg_data_t *pgdat) 29065515061dSMel Gorman { 29075515061dSMel Gorman struct zone *zone; 29085515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 29095515061dSMel Gorman unsigned long free_pages = 0; 29105515061dSMel Gorman int i; 29115515061dSMel Gorman bool wmark_ok; 29125515061dSMel Gorman 2913c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 2914c73322d0SJohannes Weiner return true; 2915c73322d0SJohannes Weiner 29165515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 29175515061dSMel Gorman zone = &pgdat->node_zones[i]; 2918d450abd8SJohannes Weiner if (!managed_zone(zone)) 2919d450abd8SJohannes Weiner continue; 2920d450abd8SJohannes Weiner 2921d450abd8SJohannes Weiner if (!zone_reclaimable_pages(zone)) 2922675becceSMel Gorman continue; 2923675becceSMel Gorman 29245515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 29255515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 29265515061dSMel Gorman } 29275515061dSMel Gorman 2928675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 2929675becceSMel Gorman if (!pfmemalloc_reserve) 2930675becceSMel Gorman return true; 2931675becceSMel Gorman 29325515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 29335515061dSMel Gorman 29345515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 29355515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 293638087d9bSMel Gorman pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx, 29375515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 29385515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 29395515061dSMel Gorman } 29405515061dSMel Gorman 29415515061dSMel Gorman return wmark_ok; 29425515061dSMel Gorman } 29435515061dSMel Gorman 29445515061dSMel Gorman /* 29455515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 29465515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 29475515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 294850694c28SMel Gorman * when the low watermark is reached. 294950694c28SMel Gorman * 295050694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 295150694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 29525515061dSMel Gorman */ 295350694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 29545515061dSMel Gorman nodemask_t *nodemask) 29555515061dSMel Gorman { 2956675becceSMel Gorman struct zoneref *z; 29575515061dSMel Gorman struct zone *zone; 2958675becceSMel Gorman pg_data_t *pgdat = NULL; 29595515061dSMel Gorman 29605515061dSMel Gorman /* 29615515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 29625515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 29635515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 29645515061dSMel Gorman * committing a transaction where throttling it could forcing other 29655515061dSMel Gorman * processes to block on log_wait_commit(). 29665515061dSMel Gorman */ 29675515061dSMel Gorman if (current->flags & PF_KTHREAD) 296850694c28SMel Gorman goto out; 296950694c28SMel Gorman 297050694c28SMel Gorman /* 297150694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 297250694c28SMel Gorman * It should return quickly so it can exit and free its memory 297350694c28SMel Gorman */ 297450694c28SMel Gorman if (fatal_signal_pending(current)) 297550694c28SMel Gorman goto out; 29765515061dSMel Gorman 2977675becceSMel Gorman /* 2978675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 2979675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 2980675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 2981675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 2982675becceSMel Gorman * 2983675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 2984675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 2985675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 2986675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 2987675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 2988675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 2989675becceSMel Gorman * should make reasonable progress. 2990675becceSMel Gorman */ 2991675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 299217636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 2993675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 2994675becceSMel Gorman continue; 2995675becceSMel Gorman 2996675becceSMel Gorman /* Throttle based on the first usable node */ 29975515061dSMel Gorman pgdat = zone->zone_pgdat; 2998c73322d0SJohannes Weiner if (allow_direct_reclaim(pgdat)) 299950694c28SMel Gorman goto out; 3000675becceSMel Gorman break; 3001675becceSMel Gorman } 3002675becceSMel Gorman 3003675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 3004675becceSMel Gorman if (!pgdat) 3005675becceSMel Gorman goto out; 30065515061dSMel Gorman 300768243e76SMel Gorman /* Account for the throttling */ 300868243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 300968243e76SMel Gorman 30105515061dSMel Gorman /* 30115515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 30125515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 30135515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 30145515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 30155515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 30165515061dSMel Gorman * second before continuing. 30175515061dSMel Gorman */ 30185515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 30195515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 3020c73322d0SJohannes Weiner allow_direct_reclaim(pgdat), HZ); 302150694c28SMel Gorman 302250694c28SMel Gorman goto check_pending; 30235515061dSMel Gorman } 30245515061dSMel Gorman 30255515061dSMel Gorman /* Throttle until kswapd wakes the process */ 30265515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 3027c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)); 302850694c28SMel Gorman 302950694c28SMel Gorman check_pending: 303050694c28SMel Gorman if (fatal_signal_pending(current)) 303150694c28SMel Gorman return true; 303250694c28SMel Gorman 303350694c28SMel Gorman out: 303450694c28SMel Gorman return false; 30355515061dSMel Gorman } 30365515061dSMel Gorman 3037dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 3038327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 303966e1707bSBalbir Singh { 304033906bc5SMel Gorman unsigned long nr_reclaimed; 304166e1707bSBalbir Singh struct scan_control sc = { 304222fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3043f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 3044b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 3045ee814fe2SJohannes Weiner .order = order, 3046ee814fe2SJohannes Weiner .nodemask = nodemask, 3047ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 3048ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3049a6dc60f8SJohannes Weiner .may_unmap = 1, 30502e2e4259SKOSAKI Motohiro .may_swap = 1, 305166e1707bSBalbir Singh }; 305266e1707bSBalbir Singh 30535515061dSMel Gorman /* 305450694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 305550694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 305650694c28SMel Gorman * point. 30575515061dSMel Gorman */ 3058f2f43e56SNick Desaulniers if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask)) 30595515061dSMel Gorman return 1; 30605515061dSMel Gorman 306133906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 306233906bc5SMel Gorman sc.may_writepage, 3063f2f43e56SNick Desaulniers sc.gfp_mask, 3064e5146b12SMel Gorman sc.reclaim_idx); 306533906bc5SMel Gorman 30663115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 306733906bc5SMel Gorman 306833906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 306933906bc5SMel Gorman 307033906bc5SMel Gorman return nr_reclaimed; 307166e1707bSBalbir Singh } 307266e1707bSBalbir Singh 3073c255a458SAndrew Morton #ifdef CONFIG_MEMCG 307466e1707bSBalbir Singh 3075a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 30764e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 3077ef8f2327SMel Gorman pg_data_t *pgdat, 30780ae5e89cSYing Han unsigned long *nr_scanned) 30794e416953SBalbir Singh { 30804e416953SBalbir Singh struct scan_control sc = { 3081b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 3082ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 30834e416953SBalbir Singh .may_writepage = !laptop_mode, 30844e416953SBalbir Singh .may_unmap = 1, 3085b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 30864e416953SBalbir Singh .may_swap = !noswap, 30874e416953SBalbir Singh }; 30886b4f7799SJohannes Weiner unsigned long lru_pages; 30890ae5e89cSYing Han 30904e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 30914e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 3092bdce6d9eSKOSAKI Motohiro 30939e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 3094bdce6d9eSKOSAKI Motohiro sc.may_writepage, 3095e5146b12SMel Gorman sc.gfp_mask, 3096e5146b12SMel Gorman sc.reclaim_idx); 3097bdce6d9eSKOSAKI Motohiro 30984e416953SBalbir Singh /* 30994e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 31004e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 3101a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 31024e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 31034e416953SBalbir Singh * the priority and make it zero. 31044e416953SBalbir Singh */ 3105ef8f2327SMel Gorman shrink_node_memcg(pgdat, memcg, &sc, &lru_pages); 3106bdce6d9eSKOSAKI Motohiro 3107bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 3108bdce6d9eSKOSAKI Motohiro 31090ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 31104e416953SBalbir Singh return sc.nr_reclaimed; 31114e416953SBalbir Singh } 31124e416953SBalbir Singh 311372835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 3114b70a2a21SJohannes Weiner unsigned long nr_pages, 31158c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 3116b70a2a21SJohannes Weiner bool may_swap) 311766e1707bSBalbir Singh { 31184e416953SBalbir Singh struct zonelist *zonelist; 3119bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 3120889976dbSYing Han int nid; 3121499118e9SVlastimil Babka unsigned int noreclaim_flag; 312266e1707bSBalbir Singh struct scan_control sc = { 3123b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 31247dea19f9SMichal Hocko .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 3125ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 3126b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3127ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3128ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 312966e1707bSBalbir Singh .may_writepage = !laptop_mode, 3130a6dc60f8SJohannes Weiner .may_unmap = 1, 3131b70a2a21SJohannes Weiner .may_swap = may_swap, 3132a09ed5e0SYing Han }; 313366e1707bSBalbir Singh 3134889976dbSYing Han /* 3135889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 3136889976dbSYing Han * take care of from where we get pages. So the node where we start the 3137889976dbSYing Han * scan does not need to be the current node. 3138889976dbSYing Han */ 313972835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 3140889976dbSYing Han 3141c9634cf0SAneesh Kumar K.V zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK]; 3142bdce6d9eSKOSAKI Motohiro 3143bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 3144bdce6d9eSKOSAKI Motohiro sc.may_writepage, 3145e5146b12SMel Gorman sc.gfp_mask, 3146e5146b12SMel Gorman sc.reclaim_idx); 3147bdce6d9eSKOSAKI Motohiro 3148499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 31493115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3150499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3151bdce6d9eSKOSAKI Motohiro 3152bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 3153bdce6d9eSKOSAKI Motohiro 3154bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 315566e1707bSBalbir Singh } 315666e1707bSBalbir Singh #endif 315766e1707bSBalbir Singh 31581d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3159ef8f2327SMel Gorman struct scan_control *sc) 3160f16015fbSJohannes Weiner { 3161b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3162b95a2f2dSJohannes Weiner 3163b95a2f2dSJohannes Weiner if (!total_swap_pages) 3164b95a2f2dSJohannes Weiner return; 3165b95a2f2dSJohannes Weiner 3166b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3167b95a2f2dSJohannes Weiner do { 3168ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 3169f16015fbSJohannes Weiner 31702a2e4885SJohannes Weiner if (inactive_list_is_low(lruvec, false, memcg, sc, true)) 31711a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 31729e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3173b95a2f2dSJohannes Weiner 3174b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3175b95a2f2dSJohannes Weiner } while (memcg); 3176f16015fbSJohannes Weiner } 3177f16015fbSJohannes Weiner 3178e716f2ebSMel Gorman /* 3179e716f2ebSMel Gorman * Returns true if there is an eligible zone balanced for the request order 3180e716f2ebSMel Gorman * and classzone_idx 3181e716f2ebSMel Gorman */ 3182e716f2ebSMel Gorman static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx) 318360cefed4SJohannes Weiner { 3184e716f2ebSMel Gorman int i; 3185e716f2ebSMel Gorman unsigned long mark = -1; 3186e716f2ebSMel Gorman struct zone *zone; 318760cefed4SJohannes Weiner 3188e716f2ebSMel Gorman for (i = 0; i <= classzone_idx; i++) { 3189e716f2ebSMel Gorman zone = pgdat->node_zones + i; 31906256c6b4SMel Gorman 3191e716f2ebSMel Gorman if (!managed_zone(zone)) 3192e716f2ebSMel Gorman continue; 3193e716f2ebSMel Gorman 3194e716f2ebSMel Gorman mark = high_wmark_pages(zone); 3195e716f2ebSMel Gorman if (zone_watermark_ok_safe(zone, order, mark, classzone_idx)) 31966256c6b4SMel Gorman return true; 319760cefed4SJohannes Weiner } 319860cefed4SJohannes Weiner 3199e716f2ebSMel Gorman /* 3200e716f2ebSMel Gorman * If a node has no populated zone within classzone_idx, it does not 3201e716f2ebSMel Gorman * need balancing by definition. This can happen if a zone-restricted 3202e716f2ebSMel Gorman * allocation tries to wake a remote kswapd. 3203e716f2ebSMel Gorman */ 3204e716f2ebSMel Gorman if (mark == -1) 3205e716f2ebSMel Gorman return true; 3206e716f2ebSMel Gorman 3207e716f2ebSMel Gorman return false; 3208e716f2ebSMel Gorman } 3209e716f2ebSMel Gorman 3210631b6e08SMel Gorman /* Clear pgdat state for congested, dirty or under writeback. */ 3211631b6e08SMel Gorman static void clear_pgdat_congested(pg_data_t *pgdat) 3212631b6e08SMel Gorman { 3213631b6e08SMel Gorman clear_bit(PGDAT_CONGESTED, &pgdat->flags); 3214631b6e08SMel Gorman clear_bit(PGDAT_DIRTY, &pgdat->flags); 3215631b6e08SMel Gorman clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 3216631b6e08SMel Gorman } 3217631b6e08SMel Gorman 32181741c877SMel Gorman /* 32195515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 32205515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 32215515061dSMel Gorman * 32225515061dSMel Gorman * Returns true if kswapd is ready to sleep 32235515061dSMel Gorman */ 3224d9f21d42SMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx) 3225f50de2d3SMel Gorman { 32265515061dSMel Gorman /* 32279e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 3228c73322d0SJohannes Weiner * soon as allow_direct_reclaim() is true. But there is a potential 32299e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 32309e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 32319e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 32329e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 32339e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 32349e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 32359e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 32369e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 32379e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 32385515061dSMel Gorman */ 32399e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 32409e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 3241f50de2d3SMel Gorman 3242c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3243c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3244c73322d0SJohannes Weiner return true; 3245c73322d0SJohannes Weiner 3246e716f2ebSMel Gorman if (pgdat_balanced(pgdat, order, classzone_idx)) { 3247631b6e08SMel Gorman clear_pgdat_congested(pgdat); 3248333b0a45SShantanu Goel return true; 32491d82de61SMel Gorman } 32501d82de61SMel Gorman 3251333b0a45SShantanu Goel return false; 3252f50de2d3SMel Gorman } 3253f50de2d3SMel Gorman 32541da177e4SLinus Torvalds /* 32551d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 32561d82de61SMel Gorman * zone that is currently unbalanced. 3257b8e83b94SMel Gorman * 3258b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 3259283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 3260283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 326175485363SMel Gorman */ 32621d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 3263accf6242SVlastimil Babka struct scan_control *sc) 326475485363SMel Gorman { 32651d82de61SMel Gorman struct zone *zone; 32661d82de61SMel Gorman int z; 326775485363SMel Gorman 32681d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 32691d82de61SMel Gorman sc->nr_to_reclaim = 0; 3270970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 32711d82de61SMel Gorman zone = pgdat->node_zones + z; 32726aa303deSMel Gorman if (!managed_zone(zone)) 32731d82de61SMel Gorman continue; 32747c954f6dSMel Gorman 32751d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 32767c954f6dSMel Gorman } 32777c954f6dSMel Gorman 32781d82de61SMel Gorman /* 32791d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 32801d82de61SMel Gorman * now pressure is applied based on node LRU order. 32811d82de61SMel Gorman */ 3282970a39a3SMel Gorman shrink_node(pgdat, sc); 32831d82de61SMel Gorman 32841d82de61SMel Gorman /* 32851d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 32861d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 32871d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 32881d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 32891d82de61SMel Gorman * can direct reclaim/compact. 32901d82de61SMel Gorman */ 32919861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 32921d82de61SMel Gorman sc->order = 0; 32931d82de61SMel Gorman 3294b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 329575485363SMel Gorman } 329675485363SMel Gorman 329775485363SMel Gorman /* 32981d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 32991d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 33001d82de61SMel Gorman * balanced. 33011da177e4SLinus Torvalds * 33021d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 33031da177e4SLinus Torvalds * 33041da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 330541858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 33061d82de61SMel Gorman * found to have free_pages <= high_wmark_pages(zone), any page is that zone 33071d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 33081d82de61SMel Gorman * balanced. 33091da177e4SLinus Torvalds */ 3310accf6242SVlastimil Babka static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx) 33111da177e4SLinus Torvalds { 33121da177e4SLinus Torvalds int i; 33130608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 33140608f43dSAndrew Morton unsigned long nr_soft_scanned; 33151d82de61SMel Gorman struct zone *zone; 3316179e9639SAndrew Morton struct scan_control sc = { 3317179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 3318ee814fe2SJohannes Weiner .order = order, 3319b8e83b94SMel Gorman .priority = DEF_PRIORITY, 3320ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3321a6dc60f8SJohannes Weiner .may_unmap = 1, 33222e2e4259SKOSAKI Motohiro .may_swap = 1, 3323179e9639SAndrew Morton }; 3324f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 33251da177e4SLinus Torvalds 33269e3b2f8cSKonstantin Khlebnikov do { 3327c73322d0SJohannes Weiner unsigned long nr_reclaimed = sc.nr_reclaimed; 3328b8e83b94SMel Gorman bool raise_priority = true; 3329b8e83b94SMel Gorman 333084c7a777SMel Gorman sc.reclaim_idx = classzone_idx; 33311da177e4SLinus Torvalds 333286c79f6bSMel Gorman /* 333384c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 333484c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 333584c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 333684c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 333784c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 333884c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 333984c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 334084c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 334186c79f6bSMel Gorman */ 334286c79f6bSMel Gorman if (buffer_heads_over_limit) { 334386c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 334486c79f6bSMel Gorman zone = pgdat->node_zones + i; 33456aa303deSMel Gorman if (!managed_zone(zone)) 334686c79f6bSMel Gorman continue; 334786c79f6bSMel Gorman 3348970a39a3SMel Gorman sc.reclaim_idx = i; 334986c79f6bSMel Gorman break; 335086c79f6bSMel Gorman } 335186c79f6bSMel Gorman } 335286c79f6bSMel Gorman 335386c79f6bSMel Gorman /* 3354e716f2ebSMel Gorman * Only reclaim if there are no eligible zones. Note that 3355e716f2ebSMel Gorman * sc.reclaim_idx is not used as buffer_heads_over_limit may 3356e716f2ebSMel Gorman * have adjusted it. 335786c79f6bSMel Gorman */ 3358e716f2ebSMel Gorman if (pgdat_balanced(pgdat, sc.order, classzone_idx)) 33591da177e4SLinus Torvalds goto out; 3360e1dbeda6SAndrew Morton 33611da177e4SLinus Torvalds /* 33621d82de61SMel Gorman * Do some background aging of the anon list, to give 33631d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 33641d82de61SMel Gorman * pages are rotated regardless of classzone as this is 33651d82de61SMel Gorman * about consistent aging. 33661d82de61SMel Gorman */ 3367ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 33681d82de61SMel Gorman 33691d82de61SMel Gorman /* 3370b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 3371b7ea3c41SMel Gorman * even in laptop mode. 3372b7ea3c41SMel Gorman */ 3373047d72c3SJohannes Weiner if (sc.priority < DEF_PRIORITY - 2) 3374b7ea3c41SMel Gorman sc.may_writepage = 1; 3375b7ea3c41SMel Gorman 33761d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 33771da177e4SLinus Torvalds sc.nr_scanned = 0; 33780608f43dSAndrew Morton nr_soft_scanned = 0; 3379ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 33801d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 33810608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 33820608f43dSAndrew Morton 338332a4330dSRik van Riel /* 33841d82de61SMel Gorman * There should be no need to raise the scanning priority if 33851d82de61SMel Gorman * enough pages are already being scanned that that high 33861d82de61SMel Gorman * watermark would be met at 100% efficiency. 338732a4330dSRik van Riel */ 3388970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 3389b8e83b94SMel Gorman raise_priority = false; 3390d7868daeSMel Gorman 33915515061dSMel Gorman /* 33925515061dSMel Gorman * If the low watermark is met there is no need for processes 33935515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 33945515061dSMel Gorman * able to safely make forward progress. Wake them 33955515061dSMel Gorman */ 33965515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 3397c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)) 3398cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 33995515061dSMel Gorman 3400b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 3401b8e83b94SMel Gorman if (try_to_freeze() || kthread_should_stop()) 3402b8e83b94SMel Gorman break; 3403b8e83b94SMel Gorman 3404b8e83b94SMel Gorman /* 3405b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3406b8e83b94SMel Gorman * progress in reclaiming pages 3407b8e83b94SMel Gorman */ 3408c73322d0SJohannes Weiner nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 3409c73322d0SJohannes Weiner if (raise_priority || !nr_reclaimed) 3410b8e83b94SMel Gorman sc.priority--; 34111d82de61SMel Gorman } while (sc.priority >= 1); 34121da177e4SLinus Torvalds 3413c73322d0SJohannes Weiner if (!sc.nr_reclaimed) 3414c73322d0SJohannes Weiner pgdat->kswapd_failures++; 3415c73322d0SJohannes Weiner 3416b8e83b94SMel Gorman out: 34172a2e4885SJohannes Weiner snapshot_refaults(NULL, pgdat); 34180abdee2bSMel Gorman /* 34191d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 34201d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 34211d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 34221d82de61SMel Gorman * remain at the higher level. 34230abdee2bSMel Gorman */ 34241d82de61SMel Gorman return sc.order; 34251da177e4SLinus Torvalds } 34261da177e4SLinus Torvalds 3427e716f2ebSMel Gorman /* 3428e716f2ebSMel Gorman * pgdat->kswapd_classzone_idx is the highest zone index that a recent 3429e716f2ebSMel Gorman * allocation request woke kswapd for. When kswapd has not woken recently, 3430e716f2ebSMel Gorman * the value is MAX_NR_ZONES which is not a valid index. This compares a 3431e716f2ebSMel Gorman * given classzone and returns it or the highest classzone index kswapd 3432e716f2ebSMel Gorman * was recently woke for. 3433e716f2ebSMel Gorman */ 3434e716f2ebSMel Gorman static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat, 3435e716f2ebSMel Gorman enum zone_type classzone_idx) 3436e716f2ebSMel Gorman { 3437e716f2ebSMel Gorman if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES) 3438e716f2ebSMel Gorman return classzone_idx; 3439e716f2ebSMel Gorman 3440e716f2ebSMel Gorman return max(pgdat->kswapd_classzone_idx, classzone_idx); 3441e716f2ebSMel Gorman } 3442e716f2ebSMel Gorman 344338087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 344438087d9bSMel Gorman unsigned int classzone_idx) 3445f0bc0a60SKOSAKI Motohiro { 3446f0bc0a60SKOSAKI Motohiro long remaining = 0; 3447f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3448f0bc0a60SKOSAKI Motohiro 3449f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3450f0bc0a60SKOSAKI Motohiro return; 3451f0bc0a60SKOSAKI Motohiro 3452f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3453f0bc0a60SKOSAKI Motohiro 3454333b0a45SShantanu Goel /* 3455333b0a45SShantanu Goel * Try to sleep for a short interval. Note that kcompactd will only be 3456333b0a45SShantanu Goel * woken if it is possible to sleep for a short interval. This is 3457333b0a45SShantanu Goel * deliberate on the assumption that if reclaim cannot keep an 3458333b0a45SShantanu Goel * eligible zone balanced that it's also unlikely that compaction will 3459333b0a45SShantanu Goel * succeed. 3460333b0a45SShantanu Goel */ 3461d9f21d42SMel Gorman if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3462fd901c95SVlastimil Babka /* 3463fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 3464fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 3465fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 3466fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 3467fd901c95SVlastimil Babka */ 3468fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 3469fd901c95SVlastimil Babka 3470fd901c95SVlastimil Babka /* 3471fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 3472fd901c95SVlastimil Babka * allocation of the requested order possible. 3473fd901c95SVlastimil Babka */ 347438087d9bSMel Gorman wakeup_kcompactd(pgdat, alloc_order, classzone_idx); 3475fd901c95SVlastimil Babka 3476f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 347738087d9bSMel Gorman 347838087d9bSMel Gorman /* 347938087d9bSMel Gorman * If woken prematurely then reset kswapd_classzone_idx and 348038087d9bSMel Gorman * order. The values will either be from a wakeup request or 348138087d9bSMel Gorman * the previous request that slept prematurely. 348238087d9bSMel Gorman */ 348338087d9bSMel Gorman if (remaining) { 3484e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 348538087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, reclaim_order); 348638087d9bSMel Gorman } 348738087d9bSMel Gorman 3488f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3489f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3490f0bc0a60SKOSAKI Motohiro } 3491f0bc0a60SKOSAKI Motohiro 3492f0bc0a60SKOSAKI Motohiro /* 3493f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3494f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3495f0bc0a60SKOSAKI Motohiro */ 3496d9f21d42SMel Gorman if (!remaining && 3497d9f21d42SMel Gorman prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3498f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3499f0bc0a60SKOSAKI Motohiro 3500f0bc0a60SKOSAKI Motohiro /* 3501f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3502f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3503f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3504f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3505f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3506f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3507f0bc0a60SKOSAKI Motohiro */ 3508f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 35091c7e7f6cSAaditya Kumar 35101c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3511f0bc0a60SKOSAKI Motohiro schedule(); 35121c7e7f6cSAaditya Kumar 3513f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3514f0bc0a60SKOSAKI Motohiro } else { 3515f0bc0a60SKOSAKI Motohiro if (remaining) 3516f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3517f0bc0a60SKOSAKI Motohiro else 3518f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3519f0bc0a60SKOSAKI Motohiro } 3520f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3521f0bc0a60SKOSAKI Motohiro } 3522f0bc0a60SKOSAKI Motohiro 35231da177e4SLinus Torvalds /* 35241da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 35251da177e4SLinus Torvalds * from the init process. 35261da177e4SLinus Torvalds * 35271da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 35281da177e4SLinus Torvalds * free memory available even if there is no other activity 35291da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 35301da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 35311da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 35321da177e4SLinus Torvalds * 35331da177e4SLinus Torvalds * If there are applications that are active memory-allocators 35341da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 35351da177e4SLinus Torvalds */ 35361da177e4SLinus Torvalds static int kswapd(void *p) 35371da177e4SLinus Torvalds { 3538e716f2ebSMel Gorman unsigned int alloc_order, reclaim_order; 3539e716f2ebSMel Gorman unsigned int classzone_idx = MAX_NR_ZONES - 1; 35401da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 35411da177e4SLinus Torvalds struct task_struct *tsk = current; 3542f0bc0a60SKOSAKI Motohiro 35431da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 35441da177e4SLinus Torvalds .reclaimed_slab = 0, 35451da177e4SLinus Torvalds }; 3546a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 35471da177e4SLinus Torvalds 3548174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3549c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 35501da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 35511da177e4SLinus Torvalds 35521da177e4SLinus Torvalds /* 35531da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 35541da177e4SLinus Torvalds * and that if we need more memory we should get access to it 35551da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 35561da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 35571da177e4SLinus Torvalds * 35581da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 35591da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 35601da177e4SLinus Torvalds * page out something else, and this flag essentially protects 35611da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 35621da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 35631da177e4SLinus Torvalds */ 3564930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 356583144186SRafael J. Wysocki set_freezable(); 35661da177e4SLinus Torvalds 3567e716f2ebSMel Gorman pgdat->kswapd_order = 0; 3568e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 35691da177e4SLinus Torvalds for ( ; ; ) { 35706f6313d4SJeff Liu bool ret; 35713e1d1d28SChristoph Lameter 3572e716f2ebSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3573e716f2ebSMel Gorman classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 3574e716f2ebSMel Gorman 357538087d9bSMel Gorman kswapd_try_sleep: 357638087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 357738087d9bSMel Gorman classzone_idx); 3578215ddd66SMel Gorman 357938087d9bSMel Gorman /* Read the new order and classzone_idx */ 358038087d9bSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3581e716f2ebSMel Gorman classzone_idx = kswapd_classzone_idx(pgdat, 0); 358238087d9bSMel Gorman pgdat->kswapd_order = 0; 3583e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 35841da177e4SLinus Torvalds 35858fe23e05SDavid Rientjes ret = try_to_freeze(); 35868fe23e05SDavid Rientjes if (kthread_should_stop()) 35878fe23e05SDavid Rientjes break; 35888fe23e05SDavid Rientjes 35898fe23e05SDavid Rientjes /* 35908fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 35918fe23e05SDavid Rientjes * after returning from the refrigerator 3592b1296cc4SRafael J. Wysocki */ 359338087d9bSMel Gorman if (ret) 359438087d9bSMel Gorman continue; 35951d82de61SMel Gorman 359638087d9bSMel Gorman /* 359738087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 359838087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 359938087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 360038087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 360138087d9bSMel Gorman * but kcompactd is woken to compact for the original 360238087d9bSMel Gorman * request (alloc_order). 360338087d9bSMel Gorman */ 3604e5146b12SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx, 3605e5146b12SMel Gorman alloc_order); 3606d92a8cfcSPeter Zijlstra fs_reclaim_acquire(GFP_KERNEL); 360738087d9bSMel Gorman reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx); 3608d92a8cfcSPeter Zijlstra fs_reclaim_release(GFP_KERNEL); 360938087d9bSMel Gorman if (reclaim_order < alloc_order) 361038087d9bSMel Gorman goto kswapd_try_sleep; 361133906bc5SMel Gorman } 3612b0a8cc58STakamori Yamaguchi 361371abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 3614b0a8cc58STakamori Yamaguchi current->reclaim_state = NULL; 361571abdc15SJohannes Weiner 36161da177e4SLinus Torvalds return 0; 36171da177e4SLinus Torvalds } 36181da177e4SLinus Torvalds 36191da177e4SLinus Torvalds /* 36201da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 36211da177e4SLinus Torvalds */ 362299504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) 36231da177e4SLinus Torvalds { 36241da177e4SLinus Torvalds pg_data_t *pgdat; 36251da177e4SLinus Torvalds 36266aa303deSMel Gorman if (!managed_zone(zone)) 36271da177e4SLinus Torvalds return; 36281da177e4SLinus Torvalds 3629344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, GFP_KERNEL | __GFP_HARDWALL)) 36301da177e4SLinus Torvalds return; 363188f5acf8SMel Gorman pgdat = zone->zone_pgdat; 3632e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, 3633e716f2ebSMel Gorman classzone_idx); 363438087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, order); 36358d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 36361da177e4SLinus Torvalds return; 3637e1a55637SMel Gorman 3638c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3639c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3640c73322d0SJohannes Weiner return; 3641c73322d0SJohannes Weiner 3642e716f2ebSMel Gorman if (pgdat_balanced(pgdat, order, classzone_idx)) 364388f5acf8SMel Gorman return; 364488f5acf8SMel Gorman 3645e716f2ebSMel Gorman trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, classzone_idx, order); 36468d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 36471da177e4SLinus Torvalds } 36481da177e4SLinus Torvalds 3649c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 36501da177e4SLinus Torvalds /* 36517b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3652d6277db4SRafael J. Wysocki * freed pages. 3653d6277db4SRafael J. Wysocki * 3654d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3655d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3656d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 36571da177e4SLinus Torvalds */ 36587b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 36591da177e4SLinus Torvalds { 3660d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 3661d6277db4SRafael J. Wysocki struct scan_control sc = { 36627b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 3663ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 3664b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 36659e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 3666ee814fe2SJohannes Weiner .may_writepage = 1, 3667ee814fe2SJohannes Weiner .may_unmap = 1, 3668ee814fe2SJohannes Weiner .may_swap = 1, 3669ee814fe2SJohannes Weiner .hibernation_mode = 1, 36701da177e4SLinus Torvalds }; 36717b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 36727b51755cSKOSAKI Motohiro struct task_struct *p = current; 36737b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 3674499118e9SVlastimil Babka unsigned int noreclaim_flag; 36751da177e4SLinus Torvalds 3676499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 3677d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 3678d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 36797b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 3680d6277db4SRafael J. Wysocki 36813115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3682d6277db4SRafael J. Wysocki 36837b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 3684d92a8cfcSPeter Zijlstra fs_reclaim_release(sc.gfp_mask); 3685499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3686d6277db4SRafael J. Wysocki 36877b51755cSKOSAKI Motohiro return nr_reclaimed; 36881da177e4SLinus Torvalds } 3689c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 36901da177e4SLinus Torvalds 36911da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 36921da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 36931da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 36941da177e4SLinus Torvalds restore their cpu bindings. */ 3695517bbed9SSebastian Andrzej Siewior static int kswapd_cpu_online(unsigned int cpu) 36961da177e4SLinus Torvalds { 369758c0a4a7SYasunori Goto int nid; 36981da177e4SLinus Torvalds 369948fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 3700c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3701a70f7302SRusty Russell const struct cpumask *mask; 3702a70f7302SRusty Russell 3703a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3704c5f59f08SMike Travis 37053e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 37061da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3707c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 37081da177e4SLinus Torvalds } 3709517bbed9SSebastian Andrzej Siewior return 0; 37101da177e4SLinus Torvalds } 37111da177e4SLinus Torvalds 37123218ae14SYasunori Goto /* 37133218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 37143218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 37153218ae14SYasunori Goto */ 37163218ae14SYasunori Goto int kswapd_run(int nid) 37173218ae14SYasunori Goto { 37183218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 37193218ae14SYasunori Goto int ret = 0; 37203218ae14SYasunori Goto 37213218ae14SYasunori Goto if (pgdat->kswapd) 37223218ae14SYasunori Goto return 0; 37233218ae14SYasunori Goto 37243218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 37253218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 37263218ae14SYasunori Goto /* failure at boot is fatal */ 3727c6202adfSThomas Gleixner BUG_ON(system_state < SYSTEM_RUNNING); 3728d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 3729d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 3730d72515b8SXishi Qiu pgdat->kswapd = NULL; 37313218ae14SYasunori Goto } 37323218ae14SYasunori Goto return ret; 37333218ae14SYasunori Goto } 37343218ae14SYasunori Goto 37358fe23e05SDavid Rientjes /* 3736d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 3737bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 37388fe23e05SDavid Rientjes */ 37398fe23e05SDavid Rientjes void kswapd_stop(int nid) 37408fe23e05SDavid Rientjes { 37418fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 37428fe23e05SDavid Rientjes 3743d8adde17SJiang Liu if (kswapd) { 37448fe23e05SDavid Rientjes kthread_stop(kswapd); 3745d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 3746d8adde17SJiang Liu } 37478fe23e05SDavid Rientjes } 37488fe23e05SDavid Rientjes 37491da177e4SLinus Torvalds static int __init kswapd_init(void) 37501da177e4SLinus Torvalds { 3751517bbed9SSebastian Andrzej Siewior int nid, ret; 375269e05944SAndrew Morton 37531da177e4SLinus Torvalds swap_setup(); 375448fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 37553218ae14SYasunori Goto kswapd_run(nid); 3756517bbed9SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 3757517bbed9SSebastian Andrzej Siewior "mm/vmscan:online", kswapd_cpu_online, 3758517bbed9SSebastian Andrzej Siewior NULL); 3759517bbed9SSebastian Andrzej Siewior WARN_ON(ret < 0); 37601da177e4SLinus Torvalds return 0; 37611da177e4SLinus Torvalds } 37621da177e4SLinus Torvalds 37631da177e4SLinus Torvalds module_init(kswapd_init) 37649eeff239SChristoph Lameter 37659eeff239SChristoph Lameter #ifdef CONFIG_NUMA 37669eeff239SChristoph Lameter /* 3767a5f5f91dSMel Gorman * Node reclaim mode 37689eeff239SChristoph Lameter * 3769a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 37709eeff239SChristoph Lameter * the watermarks. 37719eeff239SChristoph Lameter */ 3772a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 37739eeff239SChristoph Lameter 37741b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 37757d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 37761b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 377795bbc0c7SZhihui Zhang #define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */ 37781b2ffb78SChristoph Lameter 37799eeff239SChristoph Lameter /* 3780a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 3781a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3782a92f7126SChristoph Lameter * a zone. 3783a92f7126SChristoph Lameter */ 3784a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 3785a92f7126SChristoph Lameter 37869eeff239SChristoph Lameter /* 3787a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 37889614634fSChristoph Lameter * occur. 37899614634fSChristoph Lameter */ 37909614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 37919614634fSChristoph Lameter 37929614634fSChristoph Lameter /* 37930ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 37940ff38490SChristoph Lameter * slab reclaim needs to occur. 37950ff38490SChristoph Lameter */ 37960ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 37970ff38490SChristoph Lameter 379811fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 379990afa5deSMel Gorman { 380011fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 380111fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 380211fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 380390afa5deSMel Gorman 380490afa5deSMel Gorman /* 380590afa5deSMel Gorman * It's possible for there to be more file mapped pages than 380690afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 380790afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 380890afa5deSMel Gorman */ 380990afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 381090afa5deSMel Gorman } 381190afa5deSMel Gorman 381290afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 3813a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 381490afa5deSMel Gorman { 3815d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 3816d031a157SAlexandru Moise unsigned long delta = 0; 381790afa5deSMel Gorman 381890afa5deSMel Gorman /* 381995bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 382090afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 382111fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 382290afa5deSMel Gorman * a better estimate 382390afa5deSMel Gorman */ 3824a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 3825a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 382690afa5deSMel Gorman else 3827a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 382890afa5deSMel Gorman 382990afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 3830a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 3831a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 383290afa5deSMel Gorman 383390afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 383490afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 383590afa5deSMel Gorman delta = nr_pagecache_reclaimable; 383690afa5deSMel Gorman 383790afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 383890afa5deSMel Gorman } 383990afa5deSMel Gorman 38400ff38490SChristoph Lameter /* 3841a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 38429eeff239SChristoph Lameter */ 3843a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 38449eeff239SChristoph Lameter { 38457fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 384669e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 38479eeff239SChristoph Lameter struct task_struct *p = current; 38489eeff239SChristoph Lameter struct reclaim_state reclaim_state; 3849499118e9SVlastimil Babka unsigned int noreclaim_flag; 3850179e9639SAndrew Morton struct scan_control sc = { 385162b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 3852f2f43e56SNick Desaulniers .gfp_mask = current_gfp_context(gfp_mask), 3853bd2f6199SJohannes Weiner .order = order, 3854a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 3855a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 3856a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 3857ee814fe2SJohannes Weiner .may_swap = 1, 3858f2f43e56SNick Desaulniers .reclaim_idx = gfp_zone(gfp_mask), 3859179e9639SAndrew Morton }; 38609eeff239SChristoph Lameter 38619eeff239SChristoph Lameter cond_resched(); 3862d4f7796eSChristoph Lameter /* 386395bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 3864d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 386595bbc0c7SZhihui Zhang * and RECLAIM_UNMAP. 3866d4f7796eSChristoph Lameter */ 3867499118e9SVlastimil Babka noreclaim_flag = memalloc_noreclaim_save(); 3868499118e9SVlastimil Babka p->flags |= PF_SWAPWRITE; 3869d92a8cfcSPeter Zijlstra fs_reclaim_acquire(sc.gfp_mask); 38709eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 38719eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 3872c84db23cSChristoph Lameter 3873a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 3874a92f7126SChristoph Lameter /* 38750ff38490SChristoph Lameter * Free memory by calling shrink zone with increasing 38760ff38490SChristoph Lameter * priorities until we have enough memory freed. 3877a92f7126SChristoph Lameter */ 3878a92f7126SChristoph Lameter do { 3879970a39a3SMel Gorman shrink_node(pgdat, &sc); 38809e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 38810ff38490SChristoph Lameter } 3882a92f7126SChristoph Lameter 38839eeff239SChristoph Lameter p->reclaim_state = NULL; 3884d92a8cfcSPeter Zijlstra fs_reclaim_release(gfp_mask); 3885499118e9SVlastimil Babka current->flags &= ~PF_SWAPWRITE; 3886499118e9SVlastimil Babka memalloc_noreclaim_restore(noreclaim_flag); 3887a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 38889eeff239SChristoph Lameter } 3889179e9639SAndrew Morton 3890a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 3891179e9639SAndrew Morton { 3892d773ed6bSDavid Rientjes int ret; 3893179e9639SAndrew Morton 3894179e9639SAndrew Morton /* 3895a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 38960ff38490SChristoph Lameter * slab pages if we are over the defined limits. 389734aa1330SChristoph Lameter * 38989614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 38999614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 3900a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 3901a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 39029614634fSChristoph Lameter * unmapped file backed pages. 3903179e9639SAndrew Morton */ 3904a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 3905385386cfSJohannes Weiner node_page_state(pgdat, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages) 3906a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 3907179e9639SAndrew Morton 3908179e9639SAndrew Morton /* 3909d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 3910179e9639SAndrew Morton */ 3911d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 3912a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 3913179e9639SAndrew Morton 3914179e9639SAndrew Morton /* 3915a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 3916179e9639SAndrew Morton * have associated processors. This will favor the local processor 3917179e9639SAndrew Morton * over remote processors and spread off node memory allocations 3918179e9639SAndrew Morton * as wide as possible. 3919179e9639SAndrew Morton */ 3920a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 3921a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 3922d773ed6bSDavid Rientjes 3923a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 3924a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 3925fa5e084eSMel Gorman 3926a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 3927a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 3928d773ed6bSDavid Rientjes 392924cf7251SMel Gorman if (!ret) 393024cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 393124cf7251SMel Gorman 3932d773ed6bSDavid Rientjes return ret; 3933179e9639SAndrew Morton } 39349eeff239SChristoph Lameter #endif 3935894bc310SLee Schermerhorn 3936894bc310SLee Schermerhorn /* 3937894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 3938894bc310SLee Schermerhorn * @page: the page to test 3939894bc310SLee Schermerhorn * 3940894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 394139b5f29aSHugh Dickins * lists vs unevictable list. 3942894bc310SLee Schermerhorn * 3943894bc310SLee Schermerhorn * Reasons page might not be evictable: 3944ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 3945b291f000SNick Piggin * (2) page is part of an mlocked VMA 3946ba9ddf49SLee Schermerhorn * 3947894bc310SLee Schermerhorn */ 394839b5f29aSHugh Dickins int page_evictable(struct page *page) 3949894bc310SLee Schermerhorn { 395039b5f29aSHugh Dickins return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); 3951894bc310SLee Schermerhorn } 395289e004eaSLee Schermerhorn 395385046579SHugh Dickins #ifdef CONFIG_SHMEM 395489e004eaSLee Schermerhorn /** 395524513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 395624513264SHugh Dickins * @pages: array of pages to check 395724513264SHugh Dickins * @nr_pages: number of pages to check 395889e004eaSLee Schermerhorn * 395924513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 396085046579SHugh Dickins * 396185046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 396289e004eaSLee Schermerhorn */ 396324513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 396489e004eaSLee Schermerhorn { 3965925b7673SJohannes Weiner struct lruvec *lruvec; 3966785b99feSMel Gorman struct pglist_data *pgdat = NULL; 396724513264SHugh Dickins int pgscanned = 0; 396824513264SHugh Dickins int pgrescued = 0; 396989e004eaSLee Schermerhorn int i; 397089e004eaSLee Schermerhorn 397124513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 397224513264SHugh Dickins struct page *page = pages[i]; 3973785b99feSMel Gorman struct pglist_data *pagepgdat = page_pgdat(page); 397489e004eaSLee Schermerhorn 397524513264SHugh Dickins pgscanned++; 3976785b99feSMel Gorman if (pagepgdat != pgdat) { 3977785b99feSMel Gorman if (pgdat) 3978785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 3979785b99feSMel Gorman pgdat = pagepgdat; 3980785b99feSMel Gorman spin_lock_irq(&pgdat->lru_lock); 398189e004eaSLee Schermerhorn } 3982785b99feSMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 398389e004eaSLee Schermerhorn 398424513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 398524513264SHugh Dickins continue; 398689e004eaSLee Schermerhorn 398739b5f29aSHugh Dickins if (page_evictable(page)) { 398824513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 398924513264SHugh Dickins 3990309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 399124513264SHugh Dickins ClearPageUnevictable(page); 3992fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 3993fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 399424513264SHugh Dickins pgrescued++; 399589e004eaSLee Schermerhorn } 399689e004eaSLee Schermerhorn } 399724513264SHugh Dickins 3998785b99feSMel Gorman if (pgdat) { 399924513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 400024513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 4001785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 400224513264SHugh Dickins } 400385046579SHugh Dickins } 400485046579SHugh Dickins #endif /* CONFIG_SHMEM */ 4005