11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/vmscan.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Swap reorganised 29.12.95, Stephen Tweedie. 71da177e4SLinus Torvalds * kswapd added: 7.1.96 sct 81da177e4SLinus Torvalds * Removed kswapd_ctl limits, and swap out as many pages as needed 91da177e4SLinus Torvalds * to bring the system back to freepages.high: 2.4.97, Rik van Riel. 101da177e4SLinus Torvalds * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com). 111da177e4SLinus Torvalds * Multiqueue VM started 5.8.00, Rik van Riel. 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 14b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15b1de0d13SMitchel Humpherys 161da177e4SLinus Torvalds #include <linux/mm.h> 175b3cc15aSIngo Molnar #include <linux/sched/mm.h> 181da177e4SLinus Torvalds #include <linux/module.h> 195a0e3ad6STejun Heo #include <linux/gfp.h> 201da177e4SLinus Torvalds #include <linux/kernel_stat.h> 211da177e4SLinus Torvalds #include <linux/swap.h> 221da177e4SLinus Torvalds #include <linux/pagemap.h> 231da177e4SLinus Torvalds #include <linux/init.h> 241da177e4SLinus Torvalds #include <linux/highmem.h> 2570ddf637SAnton Vorontsov #include <linux/vmpressure.h> 26e129b5c2SAndrew Morton #include <linux/vmstat.h> 271da177e4SLinus Torvalds #include <linux/file.h> 281da177e4SLinus Torvalds #include <linux/writeback.h> 291da177e4SLinus Torvalds #include <linux/blkdev.h> 301da177e4SLinus Torvalds #include <linux/buffer_head.h> /* for try_to_release_page(), 311da177e4SLinus Torvalds buffer_heads_over_limit */ 321da177e4SLinus Torvalds #include <linux/mm_inline.h> 331da177e4SLinus Torvalds #include <linux/backing-dev.h> 341da177e4SLinus Torvalds #include <linux/rmap.h> 351da177e4SLinus Torvalds #include <linux/topology.h> 361da177e4SLinus Torvalds #include <linux/cpu.h> 371da177e4SLinus Torvalds #include <linux/cpuset.h> 383e7d3449SMel Gorman #include <linux/compaction.h> 391da177e4SLinus Torvalds #include <linux/notifier.h> 401da177e4SLinus Torvalds #include <linux/rwsem.h> 41248a0301SRafael J. Wysocki #include <linux/delay.h> 423218ae14SYasunori Goto #include <linux/kthread.h> 437dfb7103SNigel Cunningham #include <linux/freezer.h> 4466e1707bSBalbir Singh #include <linux/memcontrol.h> 45873b4771SKeika Kobayashi #include <linux/delayacct.h> 46af936a16SLee Schermerhorn #include <linux/sysctl.h> 47929bea7cSKOSAKI Motohiro #include <linux/oom.h> 48268bb0ceSLinus Torvalds #include <linux/prefetch.h> 49b1de0d13SMitchel Humpherys #include <linux/printk.h> 50f9fe48beSRoss Zwisler #include <linux/dax.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <asm/tlbflush.h> 531da177e4SLinus Torvalds #include <asm/div64.h> 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #include <linux/swapops.h> 56117aad1eSRafael Aquini #include <linux/balloon_compaction.h> 571da177e4SLinus Torvalds 580f8053a5SNick Piggin #include "internal.h" 590f8053a5SNick Piggin 6033906bc5SMel Gorman #define CREATE_TRACE_POINTS 6133906bc5SMel Gorman #include <trace/events/vmscan.h> 6233906bc5SMel Gorman 631da177e4SLinus Torvalds struct scan_control { 6422fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6522fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 6622fba335SKOSAKI Motohiro 671da177e4SLinus Torvalds /* This context's GFP mask */ 686daa0e28SAl Viro gfp_t gfp_mask; 691da177e4SLinus Torvalds 70ee814fe2SJohannes Weiner /* Allocation order */ 715ad333ebSAndy Whitcroft int order; 7266e1707bSBalbir Singh 73ee814fe2SJohannes Weiner /* 74ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 75ee814fe2SJohannes Weiner * are scanned. 76ee814fe2SJohannes Weiner */ 77ee814fe2SJohannes Weiner nodemask_t *nodemask; 789e3b2f8cSKonstantin Khlebnikov 795f53e762SKOSAKI Motohiro /* 80f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 81f16015fbSJohannes Weiner * primary target of this reclaim invocation. 82f16015fbSJohannes Weiner */ 83f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 8466e1707bSBalbir Singh 85ee814fe2SJohannes Weiner /* Scan (total_size >> priority) pages at once */ 86ee814fe2SJohannes Weiner int priority; 87ee814fe2SJohannes Weiner 88b2e18757SMel Gorman /* The highest zone to isolate pages for reclaim from */ 89b2e18757SMel Gorman enum zone_type reclaim_idx; 90b2e18757SMel Gorman 911276ad68SJohannes Weiner /* Writepage batching in laptop mode; RECLAIM_WRITE */ 92ee814fe2SJohannes Weiner unsigned int may_writepage:1; 93ee814fe2SJohannes Weiner 94ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 95ee814fe2SJohannes Weiner unsigned int may_unmap:1; 96ee814fe2SJohannes Weiner 97ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 98ee814fe2SJohannes Weiner unsigned int may_swap:1; 99ee814fe2SJohannes Weiner 100d6622f63SYisheng Xie /* 101d6622f63SYisheng Xie * Cgroups are not reclaimed below their configured memory.low, 102d6622f63SYisheng Xie * unless we threaten to OOM. If any cgroups are skipped due to 103d6622f63SYisheng Xie * memory.low and nothing was reclaimed, go back for memory.low. 104d6622f63SYisheng Xie */ 105d6622f63SYisheng Xie unsigned int memcg_low_reclaim:1; 106d6622f63SYisheng Xie unsigned int memcg_low_skipped:1; 107241994edSJohannes Weiner 108ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 109ee814fe2SJohannes Weiner 110ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 111ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 112ee814fe2SJohannes Weiner 113ee814fe2SJohannes Weiner /* Incremented by the number of inactive pages that were scanned */ 114ee814fe2SJohannes Weiner unsigned long nr_scanned; 115ee814fe2SJohannes Weiner 116ee814fe2SJohannes Weiner /* Number of pages freed so far during a call to shrink_zones() */ 117ee814fe2SJohannes Weiner unsigned long nr_reclaimed; 1181da177e4SLinus Torvalds }; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1211da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1221da177e4SLinus Torvalds do { \ 1231da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1241da177e4SLinus Torvalds struct page *prev; \ 1251da177e4SLinus Torvalds \ 1261da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1271da177e4SLinus Torvalds prefetch(&prev->_field); \ 1281da177e4SLinus Torvalds } \ 1291da177e4SLinus Torvalds } while (0) 1301da177e4SLinus Torvalds #else 1311da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1321da177e4SLinus Torvalds #endif 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1351da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1361da177e4SLinus Torvalds do { \ 1371da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1381da177e4SLinus Torvalds struct page *prev; \ 1391da177e4SLinus Torvalds \ 1401da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1411da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1421da177e4SLinus Torvalds } \ 1431da177e4SLinus Torvalds } while (0) 1441da177e4SLinus Torvalds #else 1451da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1461da177e4SLinus Torvalds #endif 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* 1491da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1501da177e4SLinus Torvalds */ 1511da177e4SLinus Torvalds int vm_swappiness = 60; 152d0480be4SWang Sheng-Hui /* 153d0480be4SWang Sheng-Hui * The total number of pages which are beyond the high watermark within all 154d0480be4SWang Sheng-Hui * zones. 155d0480be4SWang Sheng-Hui */ 156d0480be4SWang Sheng-Hui unsigned long vm_total_pages; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1591da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1601da177e4SLinus Torvalds 161c255a458SAndrew Morton #ifdef CONFIG_MEMCG 16289b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 16389b5fae5SJohannes Weiner { 164f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 16589b5fae5SJohannes Weiner } 16697c9341fSTejun Heo 16797c9341fSTejun Heo /** 16897c9341fSTejun Heo * sane_reclaim - is the usual dirty throttling mechanism operational? 16997c9341fSTejun Heo * @sc: scan_control in question 17097c9341fSTejun Heo * 17197c9341fSTejun Heo * The normal page dirty throttling mechanism in balance_dirty_pages() is 17297c9341fSTejun Heo * completely broken with the legacy memcg and direct stalling in 17397c9341fSTejun Heo * shrink_page_list() is used for throttling instead, which lacks all the 17497c9341fSTejun Heo * niceties such as fairness, adaptive pausing, bandwidth proportional 17597c9341fSTejun Heo * allocation and configurability. 17697c9341fSTejun Heo * 17797c9341fSTejun Heo * This function tests whether the vmscan currently in progress can assume 17897c9341fSTejun Heo * that the normal dirty throttling mechanism is operational. 17997c9341fSTejun Heo */ 18097c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 18197c9341fSTejun Heo { 18297c9341fSTejun Heo struct mem_cgroup *memcg = sc->target_mem_cgroup; 18397c9341fSTejun Heo 18497c9341fSTejun Heo if (!memcg) 18597c9341fSTejun Heo return true; 18697c9341fSTejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 18769234aceSLinus Torvalds if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 18897c9341fSTejun Heo return true; 18997c9341fSTejun Heo #endif 19097c9341fSTejun Heo return false; 19197c9341fSTejun Heo } 19291a45470SKAMEZAWA Hiroyuki #else 19389b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 19489b5fae5SJohannes Weiner { 19589b5fae5SJohannes Weiner return true; 19689b5fae5SJohannes Weiner } 19797c9341fSTejun Heo 19897c9341fSTejun Heo static bool sane_reclaim(struct scan_control *sc) 19997c9341fSTejun Heo { 20097c9341fSTejun Heo return true; 20197c9341fSTejun Heo } 20291a45470SKAMEZAWA Hiroyuki #endif 20391a45470SKAMEZAWA Hiroyuki 2045a1c84b4SMel Gorman /* 2055a1c84b4SMel Gorman * This misses isolated pages which are not accounted for to save counters. 2065a1c84b4SMel Gorman * As the data only determines if reclaim or compaction continues, it is 2075a1c84b4SMel Gorman * not expected that isolated pages will be a dominating factor. 2085a1c84b4SMel Gorman */ 2095a1c84b4SMel Gorman unsigned long zone_reclaimable_pages(struct zone *zone) 2105a1c84b4SMel Gorman { 2115a1c84b4SMel Gorman unsigned long nr; 2125a1c84b4SMel Gorman 2135a1c84b4SMel Gorman nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) + 2145a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE); 2155a1c84b4SMel Gorman if (get_nr_swap_pages() > 0) 2165a1c84b4SMel Gorman nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) + 2175a1c84b4SMel Gorman zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON); 2185a1c84b4SMel Gorman 2195a1c84b4SMel Gorman return nr; 2205a1c84b4SMel Gorman } 2215a1c84b4SMel Gorman 222599d0c95SMel Gorman unsigned long pgdat_reclaimable_pages(struct pglist_data *pgdat) 2236e543d57SLisa Du { 224599d0c95SMel Gorman unsigned long nr; 225599d0c95SMel Gorman 226599d0c95SMel Gorman nr = node_page_state_snapshot(pgdat, NR_ACTIVE_FILE) + 227599d0c95SMel Gorman node_page_state_snapshot(pgdat, NR_INACTIVE_FILE) + 228599d0c95SMel Gorman node_page_state_snapshot(pgdat, NR_ISOLATED_FILE); 229599d0c95SMel Gorman 230599d0c95SMel Gorman if (get_nr_swap_pages() > 0) 231599d0c95SMel Gorman nr += node_page_state_snapshot(pgdat, NR_ACTIVE_ANON) + 232599d0c95SMel Gorman node_page_state_snapshot(pgdat, NR_INACTIVE_ANON) + 233599d0c95SMel Gorman node_page_state_snapshot(pgdat, NR_ISOLATED_ANON); 234599d0c95SMel Gorman 235599d0c95SMel Gorman return nr; 236599d0c95SMel Gorman } 237599d0c95SMel Gorman 238fd538803SMichal Hocko /** 239fd538803SMichal Hocko * lruvec_lru_size - Returns the number of pages on the given LRU list. 240fd538803SMichal Hocko * @lruvec: lru vector 241fd538803SMichal Hocko * @lru: lru to use 242fd538803SMichal Hocko * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) 243fd538803SMichal Hocko */ 244fd538803SMichal Hocko unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx) 245c9f299d9SKOSAKI Motohiro { 246fd538803SMichal Hocko unsigned long lru_size; 247fd538803SMichal Hocko int zid; 248a3d8e054SKOSAKI Motohiro 249fd538803SMichal Hocko if (!mem_cgroup_disabled()) 250fd538803SMichal Hocko lru_size = mem_cgroup_get_lru_size(lruvec, lru); 251fd538803SMichal Hocko else 252fd538803SMichal Hocko lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru); 253fd538803SMichal Hocko 254fd538803SMichal Hocko for (zid = zone_idx + 1; zid < MAX_NR_ZONES; zid++) { 255fd538803SMichal Hocko struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid]; 256fd538803SMichal Hocko unsigned long size; 257fd538803SMichal Hocko 258fd538803SMichal Hocko if (!managed_zone(zone)) 259fd538803SMichal Hocko continue; 260fd538803SMichal Hocko 261fd538803SMichal Hocko if (!mem_cgroup_disabled()) 262fd538803SMichal Hocko size = mem_cgroup_get_zone_lru_size(lruvec, lru, zid); 263fd538803SMichal Hocko else 264fd538803SMichal Hocko size = zone_page_state(&lruvec_pgdat(lruvec)->node_zones[zid], 265fd538803SMichal Hocko NR_ZONE_LRU_BASE + lru); 266fd538803SMichal Hocko lru_size -= min(size, lru_size); 267c9f299d9SKOSAKI Motohiro } 268c9f299d9SKOSAKI Motohiro 269fd538803SMichal Hocko return lru_size; 270b4536f0cSMichal Hocko 271b4536f0cSMichal Hocko } 272b4536f0cSMichal Hocko 2731da177e4SLinus Torvalds /* 2741d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 2751da177e4SLinus Torvalds */ 2761d3d4437SGlauber Costa int register_shrinker(struct shrinker *shrinker) 2771da177e4SLinus Torvalds { 2781d3d4437SGlauber Costa size_t size = sizeof(*shrinker->nr_deferred); 2791d3d4437SGlauber Costa 2801d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 2811d3d4437SGlauber Costa size *= nr_node_ids; 2821d3d4437SGlauber Costa 2831d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 2841d3d4437SGlauber Costa if (!shrinker->nr_deferred) 2851d3d4437SGlauber Costa return -ENOMEM; 2861d3d4437SGlauber Costa 2871da177e4SLinus Torvalds down_write(&shrinker_rwsem); 2881da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 2891da177e4SLinus Torvalds up_write(&shrinker_rwsem); 2901d3d4437SGlauber Costa return 0; 2911da177e4SLinus Torvalds } 2928e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds /* 2951da177e4SLinus Torvalds * Remove one 2961da177e4SLinus Torvalds */ 2978e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 2981da177e4SLinus Torvalds { 2991da177e4SLinus Torvalds down_write(&shrinker_rwsem); 3001da177e4SLinus Torvalds list_del(&shrinker->list); 3011da177e4SLinus Torvalds up_write(&shrinker_rwsem); 302ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 3031da177e4SLinus Torvalds } 3048e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds #define SHRINK_BATCH 128 3071d3d4437SGlauber Costa 308cb731d6cSVladimir Davydov static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, 3096b4f7799SJohannes Weiner struct shrinker *shrinker, 3106b4f7799SJohannes Weiner unsigned long nr_scanned, 3116b4f7799SJohannes Weiner unsigned long nr_eligible) 3121da177e4SLinus Torvalds { 31324f7c6b9SDave Chinner unsigned long freed = 0; 3141da177e4SLinus Torvalds unsigned long long delta; 315635697c6SKonstantin Khlebnikov long total_scan; 316d5bc5fd3SVladimir Davydov long freeable; 317acf92b48SDave Chinner long nr; 318acf92b48SDave Chinner long new_nr; 3191d3d4437SGlauber Costa int nid = shrinkctl->nid; 320e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 321e9299f50SDave Chinner : SHRINK_BATCH; 3225f33a080SShaohua Li long scanned = 0, next_deferred; 3231da177e4SLinus Torvalds 324d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 325d5bc5fd3SVladimir Davydov if (freeable == 0) 3261d3d4437SGlauber Costa return 0; 327635697c6SKonstantin Khlebnikov 328acf92b48SDave Chinner /* 329acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 330acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 331acf92b48SDave Chinner * don't also do this scanning work. 332acf92b48SDave Chinner */ 3331d3d4437SGlauber Costa nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 334acf92b48SDave Chinner 335acf92b48SDave Chinner total_scan = nr; 3366b4f7799SJohannes Weiner delta = (4 * nr_scanned) / shrinker->seeks; 337d5bc5fd3SVladimir Davydov delta *= freeable; 3386b4f7799SJohannes Weiner do_div(delta, nr_eligible + 1); 339acf92b48SDave Chinner total_scan += delta; 340acf92b48SDave Chinner if (total_scan < 0) { 3418612c663SPintu Kumar pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n", 342a0b02131SDave Chinner shrinker->scan_objects, total_scan); 343d5bc5fd3SVladimir Davydov total_scan = freeable; 3445f33a080SShaohua Li next_deferred = nr; 3455f33a080SShaohua Li } else 3465f33a080SShaohua Li next_deferred = total_scan; 347ea164d73SAndrea Arcangeli 348ea164d73SAndrea Arcangeli /* 3493567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 3503567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 3513567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 3523567b59aSDave Chinner * nr being built up so when a shrink that can do some work 3533567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 354d5bc5fd3SVladimir Davydov * freeable. This is bad for sustaining a working set in 3553567b59aSDave Chinner * memory. 3563567b59aSDave Chinner * 3573567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 3583567b59aSDave Chinner * a large delta change is calculated directly. 3593567b59aSDave Chinner */ 360d5bc5fd3SVladimir Davydov if (delta < freeable / 4) 361d5bc5fd3SVladimir Davydov total_scan = min(total_scan, freeable / 2); 3623567b59aSDave Chinner 3633567b59aSDave Chinner /* 364ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 365ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 366ea164d73SAndrea Arcangeli * freeable entries. 367ea164d73SAndrea Arcangeli */ 368d5bc5fd3SVladimir Davydov if (total_scan > freeable * 2) 369d5bc5fd3SVladimir Davydov total_scan = freeable * 2; 3701da177e4SLinus Torvalds 37124f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 3726b4f7799SJohannes Weiner nr_scanned, nr_eligible, 373d5bc5fd3SVladimir Davydov freeable, delta, total_scan); 37409576073SDave Chinner 3750b1fb40aSVladimir Davydov /* 3760b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 3770b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 3780b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 3790b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 3800b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 3810b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 3820b1fb40aSVladimir Davydov * batch_size. 3830b1fb40aSVladimir Davydov * 3840b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 3850b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 386d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 3870b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 3880b1fb40aSVladimir Davydov * possible. 3890b1fb40aSVladimir Davydov */ 3900b1fb40aSVladimir Davydov while (total_scan >= batch_size || 391d5bc5fd3SVladimir Davydov total_scan >= freeable) { 39224f7c6b9SDave Chinner unsigned long ret; 3930b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 3941da177e4SLinus Torvalds 3950b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 39624f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 39724f7c6b9SDave Chinner if (ret == SHRINK_STOP) 3981da177e4SLinus Torvalds break; 39924f7c6b9SDave Chinner freed += ret; 40024f7c6b9SDave Chinner 4010b1fb40aSVladimir Davydov count_vm_events(SLABS_SCANNED, nr_to_scan); 4020b1fb40aSVladimir Davydov total_scan -= nr_to_scan; 4035f33a080SShaohua Li scanned += nr_to_scan; 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds cond_resched(); 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 4085f33a080SShaohua Li if (next_deferred >= scanned) 4095f33a080SShaohua Li next_deferred -= scanned; 4105f33a080SShaohua Li else 4115f33a080SShaohua Li next_deferred = 0; 412acf92b48SDave Chinner /* 413acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 414acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 415acf92b48SDave Chinner * scan, there is no need to do an update. 416acf92b48SDave Chinner */ 4175f33a080SShaohua Li if (next_deferred > 0) 4185f33a080SShaohua Li new_nr = atomic_long_add_return(next_deferred, 4191d3d4437SGlauber Costa &shrinker->nr_deferred[nid]); 42083aeeadaSKonstantin Khlebnikov else 4211d3d4437SGlauber Costa new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 422acf92b48SDave Chinner 423df9024a8SDave Hansen trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan); 4241d3d4437SGlauber Costa return freed; 4251d3d4437SGlauber Costa } 4261d3d4437SGlauber Costa 4276b4f7799SJohannes Weiner /** 428cb731d6cSVladimir Davydov * shrink_slab - shrink slab caches 4296b4f7799SJohannes Weiner * @gfp_mask: allocation context 4306b4f7799SJohannes Weiner * @nid: node whose slab caches to target 431cb731d6cSVladimir Davydov * @memcg: memory cgroup whose slab caches to target 4326b4f7799SJohannes Weiner * @nr_scanned: pressure numerator 4336b4f7799SJohannes Weiner * @nr_eligible: pressure denominator 4341d3d4437SGlauber Costa * 4356b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 4361d3d4437SGlauber Costa * 4376b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 4386b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 4391d3d4437SGlauber Costa * 440cb731d6cSVladimir Davydov * @memcg specifies the memory cgroup to target. If it is not NULL, 441cb731d6cSVladimir Davydov * only shrinkers with SHRINKER_MEMCG_AWARE set will be called to scan 4420fc9f58aSVladimir Davydov * objects from the memory cgroup specified. Otherwise, only unaware 4430fc9f58aSVladimir Davydov * shrinkers are called. 444cb731d6cSVladimir Davydov * 4456b4f7799SJohannes Weiner * @nr_scanned and @nr_eligible form a ratio that indicate how much of 4466b4f7799SJohannes Weiner * the available objects should be scanned. Page reclaim for example 4476b4f7799SJohannes Weiner * passes the number of pages scanned and the number of pages on the 4486b4f7799SJohannes Weiner * LRU lists that it considered on @nid, plus a bias in @nr_scanned 4496b4f7799SJohannes Weiner * when it encountered mapped pages. The ratio is further biased by 4506b4f7799SJohannes Weiner * the ->seeks setting of the shrink function, which indicates the 4516b4f7799SJohannes Weiner * cost to recreate an object relative to that of an LRU page. 4521d3d4437SGlauber Costa * 4536b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 4541d3d4437SGlauber Costa */ 455cb731d6cSVladimir Davydov static unsigned long shrink_slab(gfp_t gfp_mask, int nid, 456cb731d6cSVladimir Davydov struct mem_cgroup *memcg, 4576b4f7799SJohannes Weiner unsigned long nr_scanned, 4586b4f7799SJohannes Weiner unsigned long nr_eligible) 4591d3d4437SGlauber Costa { 4601d3d4437SGlauber Costa struct shrinker *shrinker; 4611d3d4437SGlauber Costa unsigned long freed = 0; 4621d3d4437SGlauber Costa 4630fc9f58aSVladimir Davydov if (memcg && (!memcg_kmem_enabled() || !mem_cgroup_online(memcg))) 464cb731d6cSVladimir Davydov return 0; 465cb731d6cSVladimir Davydov 4666b4f7799SJohannes Weiner if (nr_scanned == 0) 4676b4f7799SJohannes Weiner nr_scanned = SWAP_CLUSTER_MAX; 4681d3d4437SGlauber Costa 4691d3d4437SGlauber Costa if (!down_read_trylock(&shrinker_rwsem)) { 4701d3d4437SGlauber Costa /* 4711d3d4437SGlauber Costa * If we would return 0, our callers would understand that we 4721d3d4437SGlauber Costa * have nothing else to shrink and give up trying. By returning 4731d3d4437SGlauber Costa * 1 we keep it going and assume we'll be able to shrink next 4741d3d4437SGlauber Costa * time. 4751d3d4437SGlauber Costa */ 4761d3d4437SGlauber Costa freed = 1; 4771d3d4437SGlauber Costa goto out; 4781d3d4437SGlauber Costa } 4791d3d4437SGlauber Costa 4801d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 4816b4f7799SJohannes Weiner struct shrink_control sc = { 4826b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 4836b4f7799SJohannes Weiner .nid = nid, 484cb731d6cSVladimir Davydov .memcg = memcg, 4856b4f7799SJohannes Weiner }; 4866b4f7799SJohannes Weiner 4870fc9f58aSVladimir Davydov /* 4880fc9f58aSVladimir Davydov * If kernel memory accounting is disabled, we ignore 4890fc9f58aSVladimir Davydov * SHRINKER_MEMCG_AWARE flag and call all shrinkers 4900fc9f58aSVladimir Davydov * passing NULL for memcg. 4910fc9f58aSVladimir Davydov */ 4920fc9f58aSVladimir Davydov if (memcg_kmem_enabled() && 4930fc9f58aSVladimir Davydov !!memcg != !!(shrinker->flags & SHRINKER_MEMCG_AWARE)) 494cb731d6cSVladimir Davydov continue; 495cb731d6cSVladimir Davydov 4966b4f7799SJohannes Weiner if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 4976b4f7799SJohannes Weiner sc.nid = 0; 4986b4f7799SJohannes Weiner 499cb731d6cSVladimir Davydov freed += do_shrink_slab(&sc, shrinker, nr_scanned, nr_eligible); 500ec97097bSVladimir Davydov } 5011d3d4437SGlauber Costa 5021da177e4SLinus Torvalds up_read(&shrinker_rwsem); 503f06590bdSMinchan Kim out: 504f06590bdSMinchan Kim cond_resched(); 50524f7c6b9SDave Chinner return freed; 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds 508cb731d6cSVladimir Davydov void drop_slab_node(int nid) 509cb731d6cSVladimir Davydov { 510cb731d6cSVladimir Davydov unsigned long freed; 511cb731d6cSVladimir Davydov 512cb731d6cSVladimir Davydov do { 513cb731d6cSVladimir Davydov struct mem_cgroup *memcg = NULL; 514cb731d6cSVladimir Davydov 515cb731d6cSVladimir Davydov freed = 0; 516cb731d6cSVladimir Davydov do { 517cb731d6cSVladimir Davydov freed += shrink_slab(GFP_KERNEL, nid, memcg, 518cb731d6cSVladimir Davydov 1000, 1000); 519cb731d6cSVladimir Davydov } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL); 520cb731d6cSVladimir Davydov } while (freed > 10); 521cb731d6cSVladimir Davydov } 522cb731d6cSVladimir Davydov 523cb731d6cSVladimir Davydov void drop_slab(void) 524cb731d6cSVladimir Davydov { 525cb731d6cSVladimir Davydov int nid; 526cb731d6cSVladimir Davydov 527cb731d6cSVladimir Davydov for_each_online_node(nid) 528cb731d6cSVladimir Davydov drop_slab_node(nid); 529cb731d6cSVladimir Davydov } 530cb731d6cSVladimir Davydov 5311da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 5321da177e4SLinus Torvalds { 533ceddc3a5SJohannes Weiner /* 534ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 535ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 536ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 537ceddc3a5SJohannes Weiner */ 538edcf4748SJohannes Weiner return page_count(page) - page_has_private(page) == 2; 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 541703c2708STejun Heo static int may_write_to_inode(struct inode *inode, struct scan_control *sc) 5421da177e4SLinus Torvalds { 543930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 5441da177e4SLinus Torvalds return 1; 545703c2708STejun Heo if (!inode_write_congested(inode)) 5461da177e4SLinus Torvalds return 1; 547703c2708STejun Heo if (inode_to_bdi(inode) == current->backing_dev_info) 5481da177e4SLinus Torvalds return 1; 5491da177e4SLinus Torvalds return 0; 5501da177e4SLinus Torvalds } 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds /* 5531da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 5541da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 5551da177e4SLinus Torvalds * fsync(), msync() or close(). 5561da177e4SLinus Torvalds * 5571da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 5581da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 5591da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 5601da177e4SLinus Torvalds * 5611da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 5621da177e4SLinus Torvalds * __GFP_FS. 5631da177e4SLinus Torvalds */ 5641da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 5651da177e4SLinus Torvalds struct page *page, int error) 5661da177e4SLinus Torvalds { 5677eaceaccSJens Axboe lock_page(page); 5683e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 5693e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 5701da177e4SLinus Torvalds unlock_page(page); 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 57304e62a29SChristoph Lameter /* possible outcome of pageout() */ 57404e62a29SChristoph Lameter typedef enum { 57504e62a29SChristoph Lameter /* failed to write page out, page is locked */ 57604e62a29SChristoph Lameter PAGE_KEEP, 57704e62a29SChristoph Lameter /* move page to the active list, page is locked */ 57804e62a29SChristoph Lameter PAGE_ACTIVATE, 57904e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 58004e62a29SChristoph Lameter PAGE_SUCCESS, 58104e62a29SChristoph Lameter /* page is clean and locked */ 58204e62a29SChristoph Lameter PAGE_CLEAN, 58304e62a29SChristoph Lameter } pageout_t; 58404e62a29SChristoph Lameter 5851da177e4SLinus Torvalds /* 5861742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 5871742f19fSAndrew Morton * Calls ->writepage(). 5881da177e4SLinus Torvalds */ 589c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 5907d3579e8SKOSAKI Motohiro struct scan_control *sc) 5911da177e4SLinus Torvalds { 5921da177e4SLinus Torvalds /* 5931da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 5941da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 5951da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 5961da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 5971da177e4SLinus Torvalds * PagePrivate for that. 5981da177e4SLinus Torvalds * 5998174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 6001da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 6011da177e4SLinus Torvalds * will block. 6021da177e4SLinus Torvalds * 6031da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 6041da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 6051da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 6061da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 6071da177e4SLinus Torvalds */ 6081da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 6091da177e4SLinus Torvalds return PAGE_KEEP; 6101da177e4SLinus Torvalds if (!mapping) { 6111da177e4SLinus Torvalds /* 6121da177e4SLinus Torvalds * Some data journaling orphaned pages can have 6131da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 6141da177e4SLinus Torvalds */ 615266cf658SDavid Howells if (page_has_private(page)) { 6161da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 6171da177e4SLinus Torvalds ClearPageDirty(page); 618b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 6191da177e4SLinus Torvalds return PAGE_CLEAN; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds return PAGE_KEEP; 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 6251da177e4SLinus Torvalds return PAGE_ACTIVATE; 626703c2708STejun Heo if (!may_write_to_inode(mapping->host, sc)) 6271da177e4SLinus Torvalds return PAGE_KEEP; 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 6301da177e4SLinus Torvalds int res; 6311da177e4SLinus Torvalds struct writeback_control wbc = { 6321da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 6331da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 634111ebb6eSOGAWA Hirofumi .range_start = 0, 635111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 6361da177e4SLinus Torvalds .for_reclaim = 1, 6371da177e4SLinus Torvalds }; 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds SetPageReclaim(page); 6401da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 6411da177e4SLinus Torvalds if (res < 0) 6421da177e4SLinus Torvalds handle_write_error(mapping, page, res); 643994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 6441da177e4SLinus Torvalds ClearPageReclaim(page); 6451da177e4SLinus Torvalds return PAGE_ACTIVATE; 6461da177e4SLinus Torvalds } 647c661b078SAndy Whitcroft 6481da177e4SLinus Torvalds if (!PageWriteback(page)) { 6491da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 6501da177e4SLinus Torvalds ClearPageReclaim(page); 6511da177e4SLinus Torvalds } 6523aa23851Syalin wang trace_mm_vmscan_writepage(page); 653c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_WRITE); 6541da177e4SLinus Torvalds return PAGE_SUCCESS; 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds return PAGE_CLEAN; 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds 660a649fd92SAndrew Morton /* 661e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 662e286781dSNick Piggin * gets returned with a refcount of 0. 663a649fd92SAndrew Morton */ 664a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 665a528910eSJohannes Weiner bool reclaimed) 66649d2e9ccSChristoph Lameter { 667c4843a75SGreg Thelen unsigned long flags; 668c4843a75SGreg Thelen 66928e4d965SNick Piggin BUG_ON(!PageLocked(page)); 67028e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 67149d2e9ccSChristoph Lameter 672c4843a75SGreg Thelen spin_lock_irqsave(&mapping->tree_lock, flags); 67349d2e9ccSChristoph Lameter /* 6740fd0e6b0SNick Piggin * The non racy check for a busy page. 6750fd0e6b0SNick Piggin * 6760fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 6770fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 6780fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 6790fd0e6b0SNick Piggin * here, then the following race may occur: 6800fd0e6b0SNick Piggin * 6810fd0e6b0SNick Piggin * get_user_pages(&page); 6820fd0e6b0SNick Piggin * [user mapping goes away] 6830fd0e6b0SNick Piggin * write_to(page); 6840fd0e6b0SNick Piggin * !PageDirty(page) [good] 6850fd0e6b0SNick Piggin * SetPageDirty(page); 6860fd0e6b0SNick Piggin * put_page(page); 6870fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 6880fd0e6b0SNick Piggin * 6890fd0e6b0SNick Piggin * [oops, our write_to data is lost] 6900fd0e6b0SNick Piggin * 6910fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 6920fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 6930139aa7bSJoonsoo Kim * load is not satisfied before that of page->_refcount. 6940fd0e6b0SNick Piggin * 6950fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 6960fd0e6b0SNick Piggin * and thus under tree_lock, then this ordering is not required. 69749d2e9ccSChristoph Lameter */ 698fe896d18SJoonsoo Kim if (!page_ref_freeze(page, 2)) 69949d2e9ccSChristoph Lameter goto cannot_free; 700e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 701e286781dSNick Piggin if (unlikely(PageDirty(page))) { 702fe896d18SJoonsoo Kim page_ref_unfreeze(page, 2); 70349d2e9ccSChristoph Lameter goto cannot_free; 704e286781dSNick Piggin } 70549d2e9ccSChristoph Lameter 70649d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 70749d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 7080a31bc97SJohannes Weiner mem_cgroup_swapout(page, swap); 70949d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 710c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 7110a31bc97SJohannes Weiner swapcache_free(swap); 712e286781dSNick Piggin } else { 7136072d13cSLinus Torvalds void (*freepage)(struct page *); 714a528910eSJohannes Weiner void *shadow = NULL; 7156072d13cSLinus Torvalds 7166072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 717a528910eSJohannes Weiner /* 718a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 719a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 720a528910eSJohannes Weiner * 721a528910eSJohannes Weiner * But don't store shadows in an address space that is 722a528910eSJohannes Weiner * already exiting. This is not just an optizimation, 723a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 724a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 725a528910eSJohannes Weiner * back. 726f9fe48beSRoss Zwisler * 727f9fe48beSRoss Zwisler * We also don't store shadows for DAX mappings because the 728f9fe48beSRoss Zwisler * only page cache pages found in these are zero pages 729f9fe48beSRoss Zwisler * covering holes, and because we don't want to mix DAX 730f9fe48beSRoss Zwisler * exceptional entries and shadow exceptional entries in the 731f9fe48beSRoss Zwisler * same page_tree. 732a528910eSJohannes Weiner */ 733a528910eSJohannes Weiner if (reclaimed && page_is_file_cache(page) && 734f9fe48beSRoss Zwisler !mapping_exiting(mapping) && !dax_mapping(mapping)) 735a528910eSJohannes Weiner shadow = workingset_eviction(mapping, page); 73662cccb8cSJohannes Weiner __delete_from_page_cache(page, shadow); 737c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 7386072d13cSLinus Torvalds 7396072d13cSLinus Torvalds if (freepage != NULL) 7406072d13cSLinus Torvalds freepage(page); 741e286781dSNick Piggin } 742e286781dSNick Piggin 74349d2e9ccSChristoph Lameter return 1; 74449d2e9ccSChristoph Lameter 74549d2e9ccSChristoph Lameter cannot_free: 746c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 74749d2e9ccSChristoph Lameter return 0; 74849d2e9ccSChristoph Lameter } 74949d2e9ccSChristoph Lameter 7501da177e4SLinus Torvalds /* 751e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 752e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 753e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 754e286781dSNick Piggin * this page. 755e286781dSNick Piggin */ 756e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 757e286781dSNick Piggin { 758a528910eSJohannes Weiner if (__remove_mapping(mapping, page, false)) { 759e286781dSNick Piggin /* 760e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 761e286781dSNick Piggin * drops the pagecache ref for us without requiring another 762e286781dSNick Piggin * atomic operation. 763e286781dSNick Piggin */ 764fe896d18SJoonsoo Kim page_ref_unfreeze(page, 1); 765e286781dSNick Piggin return 1; 766e286781dSNick Piggin } 767e286781dSNick Piggin return 0; 768e286781dSNick Piggin } 769e286781dSNick Piggin 770894bc310SLee Schermerhorn /** 771894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 772894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 773894bc310SLee Schermerhorn * 774894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 775894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 776894bc310SLee Schermerhorn * 777894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 778894bc310SLee Schermerhorn */ 779894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 780894bc310SLee Schermerhorn { 7810ec3b74cSVlastimil Babka bool is_unevictable; 782bbfd28eeSLee Schermerhorn int was_unevictable = PageUnevictable(page); 783894bc310SLee Schermerhorn 784309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 785894bc310SLee Schermerhorn 786894bc310SLee Schermerhorn redo: 787894bc310SLee Schermerhorn ClearPageUnevictable(page); 788894bc310SLee Schermerhorn 78939b5f29aSHugh Dickins if (page_evictable(page)) { 790894bc310SLee Schermerhorn /* 791894bc310SLee Schermerhorn * For evictable pages, we can use the cache. 792894bc310SLee Schermerhorn * In event of a race, worst case is we end up with an 793894bc310SLee Schermerhorn * unevictable page on [in]active list. 794894bc310SLee Schermerhorn * We know how to handle that. 795894bc310SLee Schermerhorn */ 7960ec3b74cSVlastimil Babka is_unevictable = false; 797c53954a0SMel Gorman lru_cache_add(page); 798894bc310SLee Schermerhorn } else { 799894bc310SLee Schermerhorn /* 800894bc310SLee Schermerhorn * Put unevictable pages directly on zone's unevictable 801894bc310SLee Schermerhorn * list. 802894bc310SLee Schermerhorn */ 8030ec3b74cSVlastimil Babka is_unevictable = true; 804894bc310SLee Schermerhorn add_page_to_unevictable_list(page); 8056a7b9548SJohannes Weiner /* 80621ee9f39SMinchan Kim * When racing with an mlock or AS_UNEVICTABLE clearing 80721ee9f39SMinchan Kim * (page is unlocked) make sure that if the other thread 80821ee9f39SMinchan Kim * does not observe our setting of PG_lru and fails 80924513264SHugh Dickins * isolation/check_move_unevictable_pages, 81021ee9f39SMinchan Kim * we see PG_mlocked/AS_UNEVICTABLE cleared below and move 8116a7b9548SJohannes Weiner * the page back to the evictable list. 8126a7b9548SJohannes Weiner * 81321ee9f39SMinchan Kim * The other side is TestClearPageMlocked() or shmem_lock(). 8146a7b9548SJohannes Weiner */ 8156a7b9548SJohannes Weiner smp_mb(); 816894bc310SLee Schermerhorn } 817894bc310SLee Schermerhorn 818894bc310SLee Schermerhorn /* 819894bc310SLee Schermerhorn * page's status can change while we move it among lru. If an evictable 820894bc310SLee Schermerhorn * page is on unevictable list, it never be freed. To avoid that, 821894bc310SLee Schermerhorn * check after we added it to the list, again. 822894bc310SLee Schermerhorn */ 8230ec3b74cSVlastimil Babka if (is_unevictable && page_evictable(page)) { 824894bc310SLee Schermerhorn if (!isolate_lru_page(page)) { 825894bc310SLee Schermerhorn put_page(page); 826894bc310SLee Schermerhorn goto redo; 827894bc310SLee Schermerhorn } 828894bc310SLee Schermerhorn /* This means someone else dropped this page from LRU 829894bc310SLee Schermerhorn * So, it will be freed or putback to LRU again. There is 830894bc310SLee Schermerhorn * nothing to do here. 831894bc310SLee Schermerhorn */ 832894bc310SLee Schermerhorn } 833894bc310SLee Schermerhorn 8340ec3b74cSVlastimil Babka if (was_unevictable && !is_unevictable) 835bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGRESCUED); 8360ec3b74cSVlastimil Babka else if (!was_unevictable && is_unevictable) 837bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGCULLED); 838bbfd28eeSLee Schermerhorn 839894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 840894bc310SLee Schermerhorn } 841894bc310SLee Schermerhorn 842dfc8d636SJohannes Weiner enum page_references { 843dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 844dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 84564574746SJohannes Weiner PAGEREF_KEEP, 846dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 847dfc8d636SJohannes Weiner }; 848dfc8d636SJohannes Weiner 849dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 850dfc8d636SJohannes Weiner struct scan_control *sc) 851dfc8d636SJohannes Weiner { 85264574746SJohannes Weiner int referenced_ptes, referenced_page; 853dfc8d636SJohannes Weiner unsigned long vm_flags; 854dfc8d636SJohannes Weiner 855c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 856c3ac9a8aSJohannes Weiner &vm_flags); 85764574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 858dfc8d636SJohannes Weiner 859dfc8d636SJohannes Weiner /* 860dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 861dfc8d636SJohannes Weiner * move the page to the unevictable list. 862dfc8d636SJohannes Weiner */ 863dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 864dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 865dfc8d636SJohannes Weiner 86664574746SJohannes Weiner if (referenced_ptes) { 867e4898273SMichal Hocko if (PageSwapBacked(page)) 86864574746SJohannes Weiner return PAGEREF_ACTIVATE; 86964574746SJohannes Weiner /* 87064574746SJohannes Weiner * All mapped pages start out with page table 87164574746SJohannes Weiner * references from the instantiating fault, so we need 87264574746SJohannes Weiner * to look twice if a mapped file page is used more 87364574746SJohannes Weiner * than once. 87464574746SJohannes Weiner * 87564574746SJohannes Weiner * Mark it and spare it for another trip around the 87664574746SJohannes Weiner * inactive list. Another page table reference will 87764574746SJohannes Weiner * lead to its activation. 87864574746SJohannes Weiner * 87964574746SJohannes Weiner * Note: the mark is set for activated pages as well 88064574746SJohannes Weiner * so that recently deactivated but used pages are 88164574746SJohannes Weiner * quickly recovered. 88264574746SJohannes Weiner */ 88364574746SJohannes Weiner SetPageReferenced(page); 88464574746SJohannes Weiner 88534dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 886dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 887dfc8d636SJohannes Weiner 888c909e993SKonstantin Khlebnikov /* 889c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 890c909e993SKonstantin Khlebnikov */ 891c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 892c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 893c909e993SKonstantin Khlebnikov 89464574746SJohannes Weiner return PAGEREF_KEEP; 89564574746SJohannes Weiner } 89664574746SJohannes Weiner 897dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 8982e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 899dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 90064574746SJohannes Weiner 90164574746SJohannes Weiner return PAGEREF_RECLAIM; 902dfc8d636SJohannes Weiner } 903dfc8d636SJohannes Weiner 904e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 905e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 906e2be15f6SMel Gorman bool *dirty, bool *writeback) 907e2be15f6SMel Gorman { 908b4597226SMel Gorman struct address_space *mapping; 909b4597226SMel Gorman 910e2be15f6SMel Gorman /* 911e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 912e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 913e2be15f6SMel Gorman */ 914802a3a92SShaohua Li if (!page_is_file_cache(page) || 915802a3a92SShaohua Li (PageAnon(page) && !PageSwapBacked(page))) { 916e2be15f6SMel Gorman *dirty = false; 917e2be15f6SMel Gorman *writeback = false; 918e2be15f6SMel Gorman return; 919e2be15f6SMel Gorman } 920e2be15f6SMel Gorman 921e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 922e2be15f6SMel Gorman *dirty = PageDirty(page); 923e2be15f6SMel Gorman *writeback = PageWriteback(page); 924b4597226SMel Gorman 925b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 926b4597226SMel Gorman if (!page_has_private(page)) 927b4597226SMel Gorman return; 928b4597226SMel Gorman 929b4597226SMel Gorman mapping = page_mapping(page); 930b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 931b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 932e2be15f6SMel Gorman } 933e2be15f6SMel Gorman 9343c710c1aSMichal Hocko struct reclaim_stat { 9353c710c1aSMichal Hocko unsigned nr_dirty; 9363c710c1aSMichal Hocko unsigned nr_unqueued_dirty; 9373c710c1aSMichal Hocko unsigned nr_congested; 9383c710c1aSMichal Hocko unsigned nr_writeback; 9393c710c1aSMichal Hocko unsigned nr_immediate; 9405bccd166SMichal Hocko unsigned nr_activate; 9415bccd166SMichal Hocko unsigned nr_ref_keep; 9425bccd166SMichal Hocko unsigned nr_unmap_fail; 9433c710c1aSMichal Hocko }; 9443c710c1aSMichal Hocko 945e286781dSNick Piggin /* 9461742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 9471da177e4SLinus Torvalds */ 9481742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 949599d0c95SMel Gorman struct pglist_data *pgdat, 950f84f6e2bSMel Gorman struct scan_control *sc, 95102c6de8dSMinchan Kim enum ttu_flags ttu_flags, 9523c710c1aSMichal Hocko struct reclaim_stat *stat, 95302c6de8dSMinchan Kim bool force_reclaim) 9541da177e4SLinus Torvalds { 9551da177e4SLinus Torvalds LIST_HEAD(ret_pages); 956abe4c3b5SMel Gorman LIST_HEAD(free_pages); 9571da177e4SLinus Torvalds int pgactivate = 0; 9583c710c1aSMichal Hocko unsigned nr_unqueued_dirty = 0; 9593c710c1aSMichal Hocko unsigned nr_dirty = 0; 9603c710c1aSMichal Hocko unsigned nr_congested = 0; 9613c710c1aSMichal Hocko unsigned nr_reclaimed = 0; 9623c710c1aSMichal Hocko unsigned nr_writeback = 0; 9633c710c1aSMichal Hocko unsigned nr_immediate = 0; 9645bccd166SMichal Hocko unsigned nr_ref_keep = 0; 9655bccd166SMichal Hocko unsigned nr_unmap_fail = 0; 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds cond_resched(); 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds while (!list_empty(page_list)) { 9701da177e4SLinus Torvalds struct address_space *mapping; 9711da177e4SLinus Torvalds struct page *page; 9721da177e4SLinus Torvalds int may_enter_fs; 97302c6de8dSMinchan Kim enum page_references references = PAGEREF_RECLAIM_CLEAN; 974e2be15f6SMel Gorman bool dirty, writeback; 975854e9ed0SMinchan Kim int ret = SWAP_SUCCESS; 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds cond_resched(); 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds page = lru_to_page(page_list); 9801da177e4SLinus Torvalds list_del(&page->lru); 9811da177e4SLinus Torvalds 982529ae9aaSNick Piggin if (!trylock_page(page)) 9831da177e4SLinus Torvalds goto keep; 9841da177e4SLinus Torvalds 985309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds sc->nr_scanned++; 98880e43426SChristoph Lameter 98939b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 990*ad6b6704SMinchan Kim goto activate_locked; 991894bc310SLee Schermerhorn 992a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 99380e43426SChristoph Lameter goto keep_locked; 99480e43426SChristoph Lameter 9951da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 996802a3a92SShaohua Li if ((page_mapped(page) || PageSwapCache(page)) && 997802a3a92SShaohua Li !(PageAnon(page) && !PageSwapBacked(page))) 9981da177e4SLinus Torvalds sc->nr_scanned++; 9991da177e4SLinus Torvalds 1000c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 1001c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 1002c661b078SAndy Whitcroft 1003e62e384eSMichal Hocko /* 1004e2be15f6SMel Gorman * The number of dirty pages determines if a zone is marked 1005e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 1006e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 1007e2be15f6SMel Gorman * is all dirty unqueued pages. 1008e2be15f6SMel Gorman */ 1009e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 1010e2be15f6SMel Gorman if (dirty || writeback) 1011e2be15f6SMel Gorman nr_dirty++; 1012e2be15f6SMel Gorman 1013e2be15f6SMel Gorman if (dirty && !writeback) 1014e2be15f6SMel Gorman nr_unqueued_dirty++; 1015e2be15f6SMel Gorman 1016d04e8acdSMel Gorman /* 1017d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 1018d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 1019d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 1020d04e8acdSMel Gorman * end of the LRU a second time. 1021d04e8acdSMel Gorman */ 1022e2be15f6SMel Gorman mapping = page_mapping(page); 10231da58ee2SJamie Liu if (((dirty || writeback) && mapping && 1024703c2708STejun Heo inode_write_congested(mapping->host)) || 1025d04e8acdSMel Gorman (writeback && PageReclaim(page))) 1026e2be15f6SMel Gorman nr_congested++; 1027e2be15f6SMel Gorman 1028e2be15f6SMel Gorman /* 1029283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 1030283aba9fSMel Gorman * are three cases to consider. 1031e62e384eSMichal Hocko * 1032283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 1033283aba9fSMel Gorman * under writeback and this page is both under writeback and 1034283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 1035283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 1036283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 1037283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 1038283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 1039b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 1040b1a6f21eSMel Gorman * caller can stall after page list has been processed. 1041c3b94f44SHugh Dickins * 104297c9341fSTejun Heo * 2) Global or new memcg reclaim encounters a page that is 1043ecf5fc6eSMichal Hocko * not marked for immediate reclaim, or the caller does not 1044ecf5fc6eSMichal Hocko * have __GFP_FS (or __GFP_IO if it's simply going to swap, 1045ecf5fc6eSMichal Hocko * not to fs). In this case mark the page for immediate 104697c9341fSTejun Heo * reclaim and continue scanning. 1047283aba9fSMel Gorman * 1048ecf5fc6eSMichal Hocko * Require may_enter_fs because we would wait on fs, which 1049ecf5fc6eSMichal Hocko * may not have submitted IO yet. And the loop driver might 1050283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 1051283aba9fSMel Gorman * which it is needed to do the write (loop masks off 1052283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 1053283aba9fSMel Gorman * would probably show more reasons. 1054283aba9fSMel Gorman * 10557fadc820SHugh Dickins * 3) Legacy memcg encounters a page that is already marked 1056283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 1057283aba9fSMel Gorman * throttling so we could easily OOM just because too many 1058283aba9fSMel Gorman * pages are in writeback and there is nothing else to 1059283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 1060c55e8d03SJohannes Weiner * 1061c55e8d03SJohannes Weiner * In cases 1) and 2) we activate the pages to get them out of 1062c55e8d03SJohannes Weiner * the way while we continue scanning for clean pages on the 1063c55e8d03SJohannes Weiner * inactive list and refilling from the active list. The 1064c55e8d03SJohannes Weiner * observation here is that waiting for disk writes is more 1065c55e8d03SJohannes Weiner * expensive than potentially causing reloads down the line. 1066c55e8d03SJohannes Weiner * Since they're marked for immediate reclaim, they won't put 1067c55e8d03SJohannes Weiner * memory pressure on the cache working set any longer than it 1068c55e8d03SJohannes Weiner * takes to write them to disk. 1069e62e384eSMichal Hocko */ 1070283aba9fSMel Gorman if (PageWriteback(page)) { 1071283aba9fSMel Gorman /* Case 1 above */ 1072283aba9fSMel Gorman if (current_is_kswapd() && 1073283aba9fSMel Gorman PageReclaim(page) && 1074599d0c95SMel Gorman test_bit(PGDAT_WRITEBACK, &pgdat->flags)) { 1075b1a6f21eSMel Gorman nr_immediate++; 1076c55e8d03SJohannes Weiner goto activate_locked; 1077283aba9fSMel Gorman 1078283aba9fSMel Gorman /* Case 2 above */ 107997c9341fSTejun Heo } else if (sane_reclaim(sc) || 1080ecf5fc6eSMichal Hocko !PageReclaim(page) || !may_enter_fs) { 1081c3b94f44SHugh Dickins /* 1082c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 1083c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 1084c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 1085c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 1086c3b94f44SHugh Dickins * enough to care. What we do want is for this 1087c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 1088c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 1089c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 1090c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 1091c3b94f44SHugh Dickins */ 1092c3b94f44SHugh Dickins SetPageReclaim(page); 109392df3a72SMel Gorman nr_writeback++; 1094c55e8d03SJohannes Weiner goto activate_locked; 1095283aba9fSMel Gorman 1096283aba9fSMel Gorman /* Case 3 above */ 1097283aba9fSMel Gorman } else { 10987fadc820SHugh Dickins unlock_page(page); 1099c3b94f44SHugh Dickins wait_on_page_writeback(page); 11007fadc820SHugh Dickins /* then go back and try same page again */ 11017fadc820SHugh Dickins list_add_tail(&page->lru, page_list); 11027fadc820SHugh Dickins continue; 1103e62e384eSMichal Hocko } 1104283aba9fSMel Gorman } 11051da177e4SLinus Torvalds 110602c6de8dSMinchan Kim if (!force_reclaim) 11076a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 110802c6de8dSMinchan Kim 1109dfc8d636SJohannes Weiner switch (references) { 1110dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 11111da177e4SLinus Torvalds goto activate_locked; 111264574746SJohannes Weiner case PAGEREF_KEEP: 11135bccd166SMichal Hocko nr_ref_keep++; 111464574746SJohannes Weiner goto keep_locked; 1115dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 1116dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 1117dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 1118dfc8d636SJohannes Weiner } 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds /* 11211da177e4SLinus Torvalds * Anonymous process memory has backing store? 11221da177e4SLinus Torvalds * Try to allocate it some swap space here. 1123802a3a92SShaohua Li * Lazyfree page could be freed directly 11241da177e4SLinus Torvalds */ 1125802a3a92SShaohua Li if (PageAnon(page) && PageSwapBacked(page) && 1126802a3a92SShaohua Li !PageSwapCache(page)) { 112763eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 112863eb6b93SHugh Dickins goto keep_locked; 11295bc7b8acSShaohua Li if (!add_to_swap(page, page_list)) 11301da177e4SLinus Torvalds goto activate_locked; 113163eb6b93SHugh Dickins may_enter_fs = 1; 11321da177e4SLinus Torvalds 1133e2be15f6SMel Gorman /* Adding to swap updated mapping */ 11341da177e4SLinus Torvalds mapping = page_mapping(page); 11357751b2daSKirill A. Shutemov } else if (unlikely(PageTransHuge(page))) { 11367751b2daSKirill A. Shutemov /* Split file THP */ 11377751b2daSKirill A. Shutemov if (split_huge_page_to_list(page, page_list)) 11387751b2daSKirill A. Shutemov goto keep_locked; 1139e2be15f6SMel Gorman } 11401da177e4SLinus Torvalds 11417751b2daSKirill A. Shutemov VM_BUG_ON_PAGE(PageTransHuge(page), page); 11427751b2daSKirill A. Shutemov 11431da177e4SLinus Torvalds /* 11441da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 11451da177e4SLinus Torvalds * processes. Try to unmap it here. 11461da177e4SLinus Torvalds */ 1147802a3a92SShaohua Li if (page_mapped(page)) { 1148a128ca71SShaohua Li switch (ret = try_to_unmap(page, 1149a128ca71SShaohua Li ttu_flags | TTU_BATCH_FLUSH)) { 11501da177e4SLinus Torvalds case SWAP_FAIL: 11515bccd166SMichal Hocko nr_unmap_fail++; 11521da177e4SLinus Torvalds goto activate_locked; 11531da177e4SLinus Torvalds case SWAP_AGAIN: 11541da177e4SLinus Torvalds goto keep_locked; 11551da177e4SLinus Torvalds case SWAP_SUCCESS: 11561da177e4SLinus Torvalds ; /* try to free the page below */ 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds if (PageDirty(page)) { 1161ee72886dSMel Gorman /* 11624eda4823SJohannes Weiner * Only kswapd can writeback filesystem pages 11634eda4823SJohannes Weiner * to avoid risk of stack overflow. But avoid 11644eda4823SJohannes Weiner * injecting inefficient single-page IO into 11654eda4823SJohannes Weiner * flusher writeback as much as possible: only 11664eda4823SJohannes Weiner * write pages when we've encountered many 11674eda4823SJohannes Weiner * dirty pages, and when we've already scanned 11684eda4823SJohannes Weiner * the rest of the LRU for clean pages and see 11694eda4823SJohannes Weiner * the same dirty pages again (PageReclaim). 1170ee72886dSMel Gorman */ 1171f84f6e2bSMel Gorman if (page_is_file_cache(page) && 11724eda4823SJohannes Weiner (!current_is_kswapd() || !PageReclaim(page) || 1173599d0c95SMel Gorman !test_bit(PGDAT_DIRTY, &pgdat->flags))) { 117449ea7eb6SMel Gorman /* 117549ea7eb6SMel Gorman * Immediately reclaim when written back. 117649ea7eb6SMel Gorman * Similar in principal to deactivate_page() 117749ea7eb6SMel Gorman * except we already have the page isolated 117849ea7eb6SMel Gorman * and know it's dirty 117949ea7eb6SMel Gorman */ 1180c4a25635SMel Gorman inc_node_page_state(page, NR_VMSCAN_IMMEDIATE); 118149ea7eb6SMel Gorman SetPageReclaim(page); 118249ea7eb6SMel Gorman 1183c55e8d03SJohannes Weiner goto activate_locked; 1184ee72886dSMel Gorman } 1185ee72886dSMel Gorman 1186dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 11871da177e4SLinus Torvalds goto keep_locked; 11884dd4b920SAndrew Morton if (!may_enter_fs) 11891da177e4SLinus Torvalds goto keep_locked; 119052a8363eSChristoph Lameter if (!sc->may_writepage) 11911da177e4SLinus Torvalds goto keep_locked; 11921da177e4SLinus Torvalds 1193d950c947SMel Gorman /* 1194d950c947SMel Gorman * Page is dirty. Flush the TLB if a writable entry 1195d950c947SMel Gorman * potentially exists to avoid CPU writes after IO 1196d950c947SMel Gorman * starts and then write it out here. 1197d950c947SMel Gorman */ 1198d950c947SMel Gorman try_to_unmap_flush_dirty(); 11997d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 12001da177e4SLinus Torvalds case PAGE_KEEP: 12011da177e4SLinus Torvalds goto keep_locked; 12021da177e4SLinus Torvalds case PAGE_ACTIVATE: 12031da177e4SLinus Torvalds goto activate_locked; 12041da177e4SLinus Torvalds case PAGE_SUCCESS: 12057d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 120641ac1999SMel Gorman goto keep; 12077d3579e8SKOSAKI Motohiro if (PageDirty(page)) 12081da177e4SLinus Torvalds goto keep; 12097d3579e8SKOSAKI Motohiro 12101da177e4SLinus Torvalds /* 12111da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 12121da177e4SLinus Torvalds * ahead and try to reclaim the page. 12131da177e4SLinus Torvalds */ 1214529ae9aaSNick Piggin if (!trylock_page(page)) 12151da177e4SLinus Torvalds goto keep; 12161da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 12171da177e4SLinus Torvalds goto keep_locked; 12181da177e4SLinus Torvalds mapping = page_mapping(page); 12191da177e4SLinus Torvalds case PAGE_CLEAN: 12201da177e4SLinus Torvalds ; /* try to free the page below */ 12211da177e4SLinus Torvalds } 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds /* 12251da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 12261da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 12271da177e4SLinus Torvalds * the page as well. 12281da177e4SLinus Torvalds * 12291da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 12301da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 12311da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 12321da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 12331da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 12341da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 12351da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 12361da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 12371da177e4SLinus Torvalds * 12381da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 12391da177e4SLinus Torvalds * the pages which were not successfully invalidated in 12401da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 12411da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 12421da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 12431da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 12441da177e4SLinus Torvalds */ 1245266cf658SDavid Howells if (page_has_private(page)) { 12461da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 12471da177e4SLinus Torvalds goto activate_locked; 1248e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1249e286781dSNick Piggin unlock_page(page); 1250e286781dSNick Piggin if (put_page_testzero(page)) 12511da177e4SLinus Torvalds goto free_it; 1252e286781dSNick Piggin else { 1253e286781dSNick Piggin /* 1254e286781dSNick Piggin * rare race with speculative reference. 1255e286781dSNick Piggin * the speculative reference will free 1256e286781dSNick Piggin * this page shortly, so we may 1257e286781dSNick Piggin * increment nr_reclaimed here (and 1258e286781dSNick Piggin * leave it off the LRU). 1259e286781dSNick Piggin */ 1260e286781dSNick Piggin nr_reclaimed++; 1261e286781dSNick Piggin continue; 1262e286781dSNick Piggin } 1263e286781dSNick Piggin } 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 1266802a3a92SShaohua Li if (PageAnon(page) && !PageSwapBacked(page)) { 1267802a3a92SShaohua Li /* follow __remove_mapping for reference */ 1268802a3a92SShaohua Li if (!page_ref_freeze(page, 1)) 126949d2e9ccSChristoph Lameter goto keep_locked; 1270802a3a92SShaohua Li if (PageDirty(page)) { 1271802a3a92SShaohua Li page_ref_unfreeze(page, 1); 1272802a3a92SShaohua Li goto keep_locked; 1273802a3a92SShaohua Li } 12741da177e4SLinus Torvalds 1275802a3a92SShaohua Li count_vm_event(PGLAZYFREED); 1276802a3a92SShaohua Li } else if (!mapping || !__remove_mapping(mapping, page, true)) 1277802a3a92SShaohua Li goto keep_locked; 1278a978d6f5SNick Piggin /* 1279a978d6f5SNick Piggin * At this point, we have no other references and there is 1280a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 1281a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 1282a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 1283a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 1284a978d6f5SNick Piggin */ 128548c935adSKirill A. Shutemov __ClearPageLocked(page); 1286e286781dSNick Piggin free_it: 128705ff5137SAndrew Morton nr_reclaimed++; 1288abe4c3b5SMel Gorman 1289abe4c3b5SMel Gorman /* 1290abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1291abe4c3b5SMel Gorman * appear not as the counts should be low 1292abe4c3b5SMel Gorman */ 1293abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 12941da177e4SLinus Torvalds continue; 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds activate_locked: 129768a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 1298*ad6b6704SMinchan Kim if (PageSwapCache(page) && (mem_cgroup_swap_full(page) || 1299*ad6b6704SMinchan Kim PageMlocked(page))) 1300a2c43eedSHugh Dickins try_to_free_swap(page); 1301309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 1302*ad6b6704SMinchan Kim if (!PageMlocked(page)) { 13031da177e4SLinus Torvalds SetPageActive(page); 13041da177e4SLinus Torvalds pgactivate++; 1305*ad6b6704SMinchan Kim } 13061da177e4SLinus Torvalds keep_locked: 13071da177e4SLinus Torvalds unlock_page(page); 13081da177e4SLinus Torvalds keep: 13091da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1310309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 13111da177e4SLinus Torvalds } 1312abe4c3b5SMel Gorman 1313747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 131472b252aeSMel Gorman try_to_unmap_flush(); 1315b745bc85SMel Gorman free_hot_cold_page_list(&free_pages, true); 1316abe4c3b5SMel Gorman 13171da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1318f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 13190a31bc97SJohannes Weiner 13203c710c1aSMichal Hocko if (stat) { 13213c710c1aSMichal Hocko stat->nr_dirty = nr_dirty; 13223c710c1aSMichal Hocko stat->nr_congested = nr_congested; 13233c710c1aSMichal Hocko stat->nr_unqueued_dirty = nr_unqueued_dirty; 13243c710c1aSMichal Hocko stat->nr_writeback = nr_writeback; 13253c710c1aSMichal Hocko stat->nr_immediate = nr_immediate; 13265bccd166SMichal Hocko stat->nr_activate = pgactivate; 13275bccd166SMichal Hocko stat->nr_ref_keep = nr_ref_keep; 13285bccd166SMichal Hocko stat->nr_unmap_fail = nr_unmap_fail; 13293c710c1aSMichal Hocko } 133005ff5137SAndrew Morton return nr_reclaimed; 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds 133302c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone, 133402c6de8dSMinchan Kim struct list_head *page_list) 133502c6de8dSMinchan Kim { 133602c6de8dSMinchan Kim struct scan_control sc = { 133702c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 133802c6de8dSMinchan Kim .priority = DEF_PRIORITY, 133902c6de8dSMinchan Kim .may_unmap = 1, 134002c6de8dSMinchan Kim }; 13413c710c1aSMichal Hocko unsigned long ret; 134202c6de8dSMinchan Kim struct page *page, *next; 134302c6de8dSMinchan Kim LIST_HEAD(clean_pages); 134402c6de8dSMinchan Kim 134502c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1346117aad1eSRafael Aquini if (page_is_file_cache(page) && !PageDirty(page) && 1347b1123ea6SMinchan Kim !__PageMovable(page)) { 134802c6de8dSMinchan Kim ClearPageActive(page); 134902c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 135002c6de8dSMinchan Kim } 135102c6de8dSMinchan Kim } 135202c6de8dSMinchan Kim 1353599d0c95SMel Gorman ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc, 1354a128ca71SShaohua Li TTU_IGNORE_ACCESS, NULL, true); 135502c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 1356599d0c95SMel Gorman mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret); 135702c6de8dSMinchan Kim return ret; 135802c6de8dSMinchan Kim } 135902c6de8dSMinchan Kim 13605ad333ebSAndy Whitcroft /* 13615ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 13625ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 13635ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 13645ad333ebSAndy Whitcroft * 13655ad333ebSAndy Whitcroft * page: page to consider 13665ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 13675ad333ebSAndy Whitcroft * 13685ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 13695ad333ebSAndy Whitcroft */ 1370f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 13715ad333ebSAndy Whitcroft { 13725ad333ebSAndy Whitcroft int ret = -EINVAL; 13735ad333ebSAndy Whitcroft 13745ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 13755ad333ebSAndy Whitcroft if (!PageLRU(page)) 13765ad333ebSAndy Whitcroft return ret; 13775ad333ebSAndy Whitcroft 1378e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1379e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1380894bc310SLee Schermerhorn return ret; 1381894bc310SLee Schermerhorn 13825ad333ebSAndy Whitcroft ret = -EBUSY; 138308e552c6SKAMEZAWA Hiroyuki 1384c8244935SMel Gorman /* 1385c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1386c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1387c8244935SMel Gorman * blocking - clean pages for the most part. 1388c8244935SMel Gorman * 1389c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1390c8244935SMel Gorman * that it is possible to migrate without blocking 1391c8244935SMel Gorman */ 13921276ad68SJohannes Weiner if (mode & ISOLATE_ASYNC_MIGRATE) { 1393c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1394c8244935SMel Gorman if (PageWriteback(page)) 139539deaf85SMinchan Kim return ret; 139639deaf85SMinchan Kim 1397c8244935SMel Gorman if (PageDirty(page)) { 1398c8244935SMel Gorman struct address_space *mapping; 1399c8244935SMel Gorman 1400c8244935SMel Gorman /* 1401c8244935SMel Gorman * Only pages without mappings or that have a 1402c8244935SMel Gorman * ->migratepage callback are possible to migrate 1403c8244935SMel Gorman * without blocking 1404c8244935SMel Gorman */ 1405c8244935SMel Gorman mapping = page_mapping(page); 1406c8244935SMel Gorman if (mapping && !mapping->a_ops->migratepage) 1407c8244935SMel Gorman return ret; 1408c8244935SMel Gorman } 1409c8244935SMel Gorman } 1410c8244935SMel Gorman 1411f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1412f80c0673SMinchan Kim return ret; 1413f80c0673SMinchan Kim 14145ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 14155ad333ebSAndy Whitcroft /* 14165ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 14175ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 14185ad333ebSAndy Whitcroft * page release code relies on it. 14195ad333ebSAndy Whitcroft */ 14205ad333ebSAndy Whitcroft ClearPageLRU(page); 14215ad333ebSAndy Whitcroft ret = 0; 14225ad333ebSAndy Whitcroft } 14235ad333ebSAndy Whitcroft 14245ad333ebSAndy Whitcroft return ret; 14255ad333ebSAndy Whitcroft } 14265ad333ebSAndy Whitcroft 14277ee36a14SMel Gorman 14287ee36a14SMel Gorman /* 14297ee36a14SMel Gorman * Update LRU sizes after isolating pages. The LRU size updates must 14307ee36a14SMel Gorman * be complete before mem_cgroup_update_lru_size due to a santity check. 14317ee36a14SMel Gorman */ 14327ee36a14SMel Gorman static __always_inline void update_lru_sizes(struct lruvec *lruvec, 1433b4536f0cSMichal Hocko enum lru_list lru, unsigned long *nr_zone_taken) 14347ee36a14SMel Gorman { 14357ee36a14SMel Gorman int zid; 14367ee36a14SMel Gorman 14377ee36a14SMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 14387ee36a14SMel Gorman if (!nr_zone_taken[zid]) 14397ee36a14SMel Gorman continue; 14407ee36a14SMel Gorman 14417ee36a14SMel Gorman __update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1442b4536f0cSMichal Hocko #ifdef CONFIG_MEMCG 1443b4536f0cSMichal Hocko mem_cgroup_update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]); 1444b4536f0cSMichal Hocko #endif 14457ee36a14SMel Gorman } 14467ee36a14SMel Gorman 14477ee36a14SMel Gorman } 14487ee36a14SMel Gorman 144949d2e9ccSChristoph Lameter /* 1450a52633d8SMel Gorman * zone_lru_lock is heavily contended. Some of the functions that 14511da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 14521da177e4SLinus Torvalds * and working on them outside the LRU lock. 14531da177e4SLinus Torvalds * 14541da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 14551da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 14561da177e4SLinus Torvalds * 14571da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 14581da177e4SLinus Torvalds * 14591da177e4SLinus Torvalds * @nr_to_scan: The number of pages to look through on the list. 14605dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 14611da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1462f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1463fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 14645ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 14653cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 14661da177e4SLinus Torvalds * 14671da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 14681da177e4SLinus Torvalds */ 146969e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 14705dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1471fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 14723cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 14731da177e4SLinus Torvalds { 147475b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 147569e05944SAndrew Morton unsigned long nr_taken = 0; 1476599d0c95SMel Gorman unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 }; 14777cc30fcfSMel Gorman unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; 14783db65812SJohannes Weiner unsigned long skipped = 0; 1479599d0c95SMel Gorman unsigned long scan, nr_pages; 1480b2e18757SMel Gorman LIST_HEAD(pages_skipped); 14811da177e4SLinus Torvalds 14820b802f10SVladimir Davydov for (scan = 0; scan < nr_to_scan && nr_taken < nr_to_scan && 14833db65812SJohannes Weiner !list_empty(src); scan++) { 14845ad333ebSAndy Whitcroft struct page *page; 14855ad333ebSAndy Whitcroft 14861da177e4SLinus Torvalds page = lru_to_page(src); 14871da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 14881da177e4SLinus Torvalds 1489309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 14908d438f96SNick Piggin 1491b2e18757SMel Gorman if (page_zonenum(page) > sc->reclaim_idx) { 1492b2e18757SMel Gorman list_move(&page->lru, &pages_skipped); 14937cc30fcfSMel Gorman nr_skipped[page_zonenum(page)]++; 1494b2e18757SMel Gorman continue; 1495b2e18757SMel Gorman } 1496b2e18757SMel Gorman 1497f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 14985ad333ebSAndy Whitcroft case 0: 1499599d0c95SMel Gorman nr_pages = hpage_nr_pages(page); 1500599d0c95SMel Gorman nr_taken += nr_pages; 1501599d0c95SMel Gorman nr_zone_taken[page_zonenum(page)] += nr_pages; 15025ad333ebSAndy Whitcroft list_move(&page->lru, dst); 15035ad333ebSAndy Whitcroft break; 15047c8ee9a8SNick Piggin 15055ad333ebSAndy Whitcroft case -EBUSY: 15065ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 15075ad333ebSAndy Whitcroft list_move(&page->lru, src); 15085ad333ebSAndy Whitcroft continue; 15095ad333ebSAndy Whitcroft 15105ad333ebSAndy Whitcroft default: 15115ad333ebSAndy Whitcroft BUG(); 15125ad333ebSAndy Whitcroft } 15135ad333ebSAndy Whitcroft } 15141da177e4SLinus Torvalds 1515b2e18757SMel Gorman /* 1516b2e18757SMel Gorman * Splice any skipped pages to the start of the LRU list. Note that 1517b2e18757SMel Gorman * this disrupts the LRU order when reclaiming for lower zones but 1518b2e18757SMel Gorman * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX 1519b2e18757SMel Gorman * scanning would soon rescan the same pages to skip and put the 1520b2e18757SMel Gorman * system at risk of premature OOM. 1521b2e18757SMel Gorman */ 15227cc30fcfSMel Gorman if (!list_empty(&pages_skipped)) { 15237cc30fcfSMel Gorman int zid; 15247cc30fcfSMel Gorman 15253db65812SJohannes Weiner list_splice(&pages_skipped, src); 15267cc30fcfSMel Gorman for (zid = 0; zid < MAX_NR_ZONES; zid++) { 15277cc30fcfSMel Gorman if (!nr_skipped[zid]) 15287cc30fcfSMel Gorman continue; 15297cc30fcfSMel Gorman 15307cc30fcfSMel Gorman __count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]); 15311265e3a6SMichal Hocko skipped += nr_skipped[zid]; 15327cc30fcfSMel Gorman } 15337cc30fcfSMel Gorman } 15343db65812SJohannes Weiner *nr_scanned = scan; 15351265e3a6SMichal Hocko trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan, 153632b3f297SMichal Hocko scan, skipped, nr_taken, mode, lru); 1537b4536f0cSMichal Hocko update_lru_sizes(lruvec, lru, nr_zone_taken); 15381da177e4SLinus Torvalds return nr_taken; 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds 154162695a84SNick Piggin /** 154262695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 154362695a84SNick Piggin * @page: page to isolate from its LRU list 154462695a84SNick Piggin * 154562695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 154662695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 154762695a84SNick Piggin * 154862695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 154962695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 155062695a84SNick Piggin * 155162695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1552894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1553894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1554894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 155562695a84SNick Piggin * 155662695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 155762695a84SNick Piggin * found will be decremented. 155862695a84SNick Piggin * 155962695a84SNick Piggin * Restrictions: 156062695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 156162695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 156262695a84SNick Piggin * without a stable reference). 156362695a84SNick Piggin * (2) the lru_lock must not be held. 156462695a84SNick Piggin * (3) interrupts must be enabled. 156562695a84SNick Piggin */ 156662695a84SNick Piggin int isolate_lru_page(struct page *page) 156762695a84SNick Piggin { 156862695a84SNick Piggin int ret = -EBUSY; 156962695a84SNick Piggin 1570309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 1571cf2a82eeSKirill A. Shutemov WARN_RATELIMIT(PageTail(page), "trying to isolate tail page"); 15720c917313SKonstantin Khlebnikov 157362695a84SNick Piggin if (PageLRU(page)) { 157462695a84SNick Piggin struct zone *zone = page_zone(page); 1575fa9add64SHugh Dickins struct lruvec *lruvec; 157662695a84SNick Piggin 1577a52633d8SMel Gorman spin_lock_irq(zone_lru_lock(zone)); 1578599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat); 15790c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1580894bc310SLee Schermerhorn int lru = page_lru(page); 15810c917313SKonstantin Khlebnikov get_page(page); 158262695a84SNick Piggin ClearPageLRU(page); 1583fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1584fa9add64SHugh Dickins ret = 0; 158562695a84SNick Piggin } 1586a52633d8SMel Gorman spin_unlock_irq(zone_lru_lock(zone)); 158762695a84SNick Piggin } 158862695a84SNick Piggin return ret; 158962695a84SNick Piggin } 159062695a84SNick Piggin 15915ad333ebSAndy Whitcroft /* 1592d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1593d37dd5dcSFengguang Wu * then get resheduled. When there are massive number of tasks doing page 1594d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1595d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1596d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 159735cd7815SRik van Riel */ 1598599d0c95SMel Gorman static int too_many_isolated(struct pglist_data *pgdat, int file, 159935cd7815SRik van Riel struct scan_control *sc) 160035cd7815SRik van Riel { 160135cd7815SRik van Riel unsigned long inactive, isolated; 160235cd7815SRik van Riel 160335cd7815SRik van Riel if (current_is_kswapd()) 160435cd7815SRik van Riel return 0; 160535cd7815SRik van Riel 160697c9341fSTejun Heo if (!sane_reclaim(sc)) 160735cd7815SRik van Riel return 0; 160835cd7815SRik van Riel 160935cd7815SRik van Riel if (file) { 1610599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_FILE); 1611599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_FILE); 161235cd7815SRik van Riel } else { 1613599d0c95SMel Gorman inactive = node_page_state(pgdat, NR_INACTIVE_ANON); 1614599d0c95SMel Gorman isolated = node_page_state(pgdat, NR_ISOLATED_ANON); 161535cd7815SRik van Riel } 161635cd7815SRik van Riel 16173cf23841SFengguang Wu /* 16183cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 16193cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 16203cf23841SFengguang Wu * deadlock. 16213cf23841SFengguang Wu */ 1622d0164adcSMel Gorman if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) 16233cf23841SFengguang Wu inactive >>= 3; 16243cf23841SFengguang Wu 162535cd7815SRik van Riel return isolated > inactive; 162635cd7815SRik van Riel } 162735cd7815SRik van Riel 162866635629SMel Gorman static noinline_for_stack void 162975b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) 163066635629SMel Gorman { 163127ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 1632599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 16333f79768fSHugh Dickins LIST_HEAD(pages_to_free); 163466635629SMel Gorman 163566635629SMel Gorman /* 163666635629SMel Gorman * Put back any unfreeable pages. 163766635629SMel Gorman */ 163866635629SMel Gorman while (!list_empty(page_list)) { 16393f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 164066635629SMel Gorman int lru; 16413f79768fSHugh Dickins 1642309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 164366635629SMel Gorman list_del(&page->lru); 164439b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1645599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 164666635629SMel Gorman putback_lru_page(page); 1647599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 164866635629SMel Gorman continue; 164966635629SMel Gorman } 1650fa9add64SHugh Dickins 1651599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 1652fa9add64SHugh Dickins 16537a608572SLinus Torvalds SetPageLRU(page); 165466635629SMel Gorman lru = page_lru(page); 1655fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 1656fa9add64SHugh Dickins 165766635629SMel Gorman if (is_active_lru(lru)) { 165866635629SMel Gorman int file = is_file_lru(lru); 16599992af10SRik van Riel int numpages = hpage_nr_pages(page); 16609992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 166166635629SMel Gorman } 16622bcf8879SHugh Dickins if (put_page_testzero(page)) { 16632bcf8879SHugh Dickins __ClearPageLRU(page); 16642bcf8879SHugh Dickins __ClearPageActive(page); 1665fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 16662bcf8879SHugh Dickins 16672bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 1668599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1669747db954SJohannes Weiner mem_cgroup_uncharge(page); 16702bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 1671599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 16722bcf8879SHugh Dickins } else 16732bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 167466635629SMel Gorman } 167566635629SMel Gorman } 167666635629SMel Gorman 16773f79768fSHugh Dickins /* 16783f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 16793f79768fSHugh Dickins */ 16803f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 168166635629SMel Gorman } 168266635629SMel Gorman 168366635629SMel Gorman /* 1684399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 1685399ba0b9SNeilBrown * a backing device by writing to the page cache it sets PF_LESS_THROTTLE. 1686399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 1687399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 1688399ba0b9SNeilBrown */ 1689399ba0b9SNeilBrown static int current_may_throttle(void) 1690399ba0b9SNeilBrown { 1691399ba0b9SNeilBrown return !(current->flags & PF_LESS_THROTTLE) || 1692399ba0b9SNeilBrown current->backing_dev_info == NULL || 1693399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 1694399ba0b9SNeilBrown } 1695399ba0b9SNeilBrown 1696399ba0b9SNeilBrown /* 1697b2e18757SMel Gorman * shrink_inactive_list() is a helper for shrink_node(). It returns the number 16981742f19fSAndrew Morton * of reclaimed pages 16991da177e4SLinus Torvalds */ 170066635629SMel Gorman static noinline_for_stack unsigned long 17011a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 17029e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 17031da177e4SLinus Torvalds { 17041da177e4SLinus Torvalds LIST_HEAD(page_list); 1705e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 170605ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1707e247dbceSKOSAKI Motohiro unsigned long nr_taken; 17083c710c1aSMichal Hocko struct reclaim_stat stat = {}; 1709f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 17103cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1711599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 17121a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 171378dc583dSKOSAKI Motohiro 1714599d0c95SMel Gorman while (unlikely(too_many_isolated(pgdat, file, sc))) { 171558355c78SKOSAKI Motohiro congestion_wait(BLK_RW_ASYNC, HZ/10); 171635cd7815SRik van Riel 171735cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 171835cd7815SRik van Riel if (fatal_signal_pending(current)) 171935cd7815SRik van Riel return SWAP_CLUSTER_MAX; 172035cd7815SRik van Riel } 172135cd7815SRik van Riel 17221da177e4SLinus Torvalds lru_add_drain(); 1723f80c0673SMinchan Kim 1724f80c0673SMinchan Kim if (!sc->may_unmap) 172561317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1726f80c0673SMinchan Kim 1727599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 17281da177e4SLinus Torvalds 17295dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 17305dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 173195d918fcSKonstantin Khlebnikov 1732599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 17339d5e6a9fSHugh Dickins reclaim_stat->recent_scanned[file] += nr_taken; 173495d918fcSKonstantin Khlebnikov 173589b5fae5SJohannes Weiner if (global_reclaim(sc)) { 1736b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1737599d0c95SMel Gorman __count_vm_events(PGSCAN_KSWAPD, nr_scanned); 1738b35ea17bSKOSAKI Motohiro else 1739599d0c95SMel Gorman __count_vm_events(PGSCAN_DIRECT, nr_scanned); 1740b35ea17bSKOSAKI Motohiro } 1741599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1742d563c050SHillf Danton 1743d563c050SHillf Danton if (nr_taken == 0) 174466635629SMel Gorman return 0; 1745b35ea17bSKOSAKI Motohiro 1746a128ca71SShaohua Li nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0, 17473c710c1aSMichal Hocko &stat, false); 1748c661b078SAndy Whitcroft 1749599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 17503f79768fSHugh Dickins 1751904249aaSYing Han if (global_reclaim(sc)) { 1752b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1753599d0c95SMel Gorman __count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed); 1754904249aaSYing Han else 1755599d0c95SMel Gorman __count_vm_events(PGSTEAL_DIRECT, nr_reclaimed); 1756904249aaSYing Han } 1757a74609faSNick Piggin 175827ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 17593f79768fSHugh Dickins 1760599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 17613f79768fSHugh Dickins 1762599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 17633f79768fSHugh Dickins 1764747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 1765b745bc85SMel Gorman free_hot_cold_page_list(&page_list, true); 1766e11da5b4SMel Gorman 176792df3a72SMel Gorman /* 176892df3a72SMel Gorman * If reclaim is isolating dirty pages under writeback, it implies 176992df3a72SMel Gorman * that the long-lived page allocation rate is exceeding the page 177092df3a72SMel Gorman * laundering rate. Either the global limits are not being effective 177192df3a72SMel Gorman * at throttling processes due to the page distribution throughout 177292df3a72SMel Gorman * zones or there is heavy usage of a slow backing device. The 177392df3a72SMel Gorman * only option is to throttle from reclaim context which is not ideal 177492df3a72SMel Gorman * as there is no guarantee the dirtying process is throttled in the 177592df3a72SMel Gorman * same way balance_dirty_pages() manages. 177692df3a72SMel Gorman * 17778e950282SMel Gorman * Once a zone is flagged ZONE_WRITEBACK, kswapd will count the number 17788e950282SMel Gorman * of pages under pages flagged for immediate reclaim and stall if any 17798e950282SMel Gorman * are encountered in the nr_immediate check below. 178092df3a72SMel Gorman */ 17813c710c1aSMichal Hocko if (stat.nr_writeback && stat.nr_writeback == nr_taken) 1782599d0c95SMel Gorman set_bit(PGDAT_WRITEBACK, &pgdat->flags); 178392df3a72SMel Gorman 1784d43006d5SMel Gorman /* 178597c9341fSTejun Heo * Legacy memcg will stall in page writeback so avoid forcibly 178697c9341fSTejun Heo * stalling here. 1787d43006d5SMel Gorman */ 178897c9341fSTejun Heo if (sane_reclaim(sc)) { 1789b1a6f21eSMel Gorman /* 17908e950282SMel Gorman * Tag a zone as congested if all the dirty pages scanned were 17918e950282SMel Gorman * backed by a congested BDI and wait_iff_congested will stall. 17928e950282SMel Gorman */ 17933c710c1aSMichal Hocko if (stat.nr_dirty && stat.nr_dirty == stat.nr_congested) 1794599d0c95SMel Gorman set_bit(PGDAT_CONGESTED, &pgdat->flags); 17958e950282SMel Gorman 17968e950282SMel Gorman /* 1797b1a6f21eSMel Gorman * If dirty pages are scanned that are not queued for IO, it 1798726d061fSJohannes Weiner * implies that flushers are not doing their job. This can 1799726d061fSJohannes Weiner * happen when memory pressure pushes dirty pages to the end of 1800726d061fSJohannes Weiner * the LRU before the dirty limits are breached and the dirty 1801726d061fSJohannes Weiner * data has expired. It can also happen when the proportion of 1802726d061fSJohannes Weiner * dirty pages grows not through writes but through memory 1803726d061fSJohannes Weiner * pressure reclaiming all the clean cache. And in some cases, 1804726d061fSJohannes Weiner * the flushers simply cannot keep up with the allocation 1805726d061fSJohannes Weiner * rate. Nudge the flusher threads in case they are asleep, but 1806726d061fSJohannes Weiner * also allow kswapd to start writing pages during reclaim. 1807b1a6f21eSMel Gorman */ 1808726d061fSJohannes Weiner if (stat.nr_unqueued_dirty == nr_taken) { 1809726d061fSJohannes Weiner wakeup_flusher_threads(0, WB_REASON_VMSCAN); 1810599d0c95SMel Gorman set_bit(PGDAT_DIRTY, &pgdat->flags); 1811726d061fSJohannes Weiner } 1812b1a6f21eSMel Gorman 1813b1a6f21eSMel Gorman /* 1814b738d764SLinus Torvalds * If kswapd scans pages marked marked for immediate 1815b738d764SLinus Torvalds * reclaim and under writeback (nr_immediate), it implies 1816b738d764SLinus Torvalds * that pages are cycling through the LRU faster than 1817b1a6f21eSMel Gorman * they are written so also forcibly stall. 1818b1a6f21eSMel Gorman */ 18193c710c1aSMichal Hocko if (stat.nr_immediate && current_may_throttle()) 1820b1a6f21eSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 1821e2be15f6SMel Gorman } 1822d43006d5SMel Gorman 18238e950282SMel Gorman /* 18248e950282SMel Gorman * Stall direct reclaim for IO completions if underlying BDIs or zone 18258e950282SMel Gorman * is congested. Allow kswapd to continue until it starts encountering 18268e950282SMel Gorman * unqueued dirty pages or cycling through the LRU too quickly. 18278e950282SMel Gorman */ 1828399ba0b9SNeilBrown if (!sc->hibernation_mode && !current_is_kswapd() && 1829399ba0b9SNeilBrown current_may_throttle()) 1830599d0c95SMel Gorman wait_iff_congested(pgdat, BLK_RW_ASYNC, HZ/10); 18318e950282SMel Gorman 1832599d0c95SMel Gorman trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id, 1833599d0c95SMel Gorman nr_scanned, nr_reclaimed, 18345bccd166SMichal Hocko stat.nr_dirty, stat.nr_writeback, 18355bccd166SMichal Hocko stat.nr_congested, stat.nr_immediate, 18365bccd166SMichal Hocko stat.nr_activate, stat.nr_ref_keep, 18375bccd166SMichal Hocko stat.nr_unmap_fail, 1838ba5e9579Syalin wang sc->priority, file); 183905ff5137SAndrew Morton return nr_reclaimed; 18401da177e4SLinus Torvalds } 18411da177e4SLinus Torvalds 18423bb1a852SMartin Bligh /* 18431cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 18441cfb419bSKAMEZAWA Hiroyuki * 18451cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 18461cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 18471cfb419bSKAMEZAWA Hiroyuki * 18481cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 1849a52633d8SMel Gorman * appropriate to hold zone_lru_lock across the whole operation. But if 18501cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 1851a52633d8SMel Gorman * should drop zone_lru_lock around each page. It's impossible to balance 18521cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 18531cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 18541cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 18551cfb419bSKAMEZAWA Hiroyuki * 18560139aa7bSJoonsoo Kim * The downside is that we have to touch page->_refcount against each page. 18571cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 18589d998b4fSMichal Hocko * 18599d998b4fSMichal Hocko * Returns the number of pages moved to the given lru. 18601cfb419bSKAMEZAWA Hiroyuki */ 18611cfb419bSKAMEZAWA Hiroyuki 18629d998b4fSMichal Hocko static unsigned move_active_pages_to_lru(struct lruvec *lruvec, 18633eb4140fSWu Fengguang struct list_head *list, 18642bcf8879SHugh Dickins struct list_head *pages_to_free, 18653eb4140fSWu Fengguang enum lru_list lru) 18663eb4140fSWu Fengguang { 1867599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 18683eb4140fSWu Fengguang struct page *page; 1869fa9add64SHugh Dickins int nr_pages; 18709d998b4fSMichal Hocko int nr_moved = 0; 18713eb4140fSWu Fengguang 18723eb4140fSWu Fengguang while (!list_empty(list)) { 18733eb4140fSWu Fengguang page = lru_to_page(list); 1874599d0c95SMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 18753eb4140fSWu Fengguang 1876309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 18773eb4140fSWu Fengguang SetPageLRU(page); 18783eb4140fSWu Fengguang 1879fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1880599d0c95SMel Gorman update_lru_size(lruvec, lru, page_zonenum(page), nr_pages); 1881925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 18823eb4140fSWu Fengguang 18832bcf8879SHugh Dickins if (put_page_testzero(page)) { 18842bcf8879SHugh Dickins __ClearPageLRU(page); 18852bcf8879SHugh Dickins __ClearPageActive(page); 1886fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 18872bcf8879SHugh Dickins 18882bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 1889599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 1890747db954SJohannes Weiner mem_cgroup_uncharge(page); 18912bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 1892599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 18932bcf8879SHugh Dickins } else 18942bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 18959d998b4fSMichal Hocko } else { 18969d998b4fSMichal Hocko nr_moved += nr_pages; 18973eb4140fSWu Fengguang } 18983eb4140fSWu Fengguang } 18999d5e6a9fSHugh Dickins 19003eb4140fSWu Fengguang if (!is_active_lru(lru)) 1901f0958906SMichal Hocko __count_vm_events(PGDEACTIVATE, nr_moved); 19029d998b4fSMichal Hocko 19039d998b4fSMichal Hocko return nr_moved; 19043eb4140fSWu Fengguang } 19051cfb419bSKAMEZAWA Hiroyuki 1906f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 19071a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 1908f16015fbSJohannes Weiner struct scan_control *sc, 19099e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 19101cfb419bSKAMEZAWA Hiroyuki { 191144c241f1SKOSAKI Motohiro unsigned long nr_taken; 1912f626012dSHugh Dickins unsigned long nr_scanned; 19136fe6b7e3SWu Fengguang unsigned long vm_flags; 19141cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 19158cab4754SWu Fengguang LIST_HEAD(l_active); 1916b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 19171cfb419bSKAMEZAWA Hiroyuki struct page *page; 19181a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 19199d998b4fSMichal Hocko unsigned nr_deactivate, nr_activate; 19209d998b4fSMichal Hocko unsigned nr_rotated = 0; 1921f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 19223cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1923599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 19241cfb419bSKAMEZAWA Hiroyuki 19251da177e4SLinus Torvalds lru_add_drain(); 1926f80c0673SMinchan Kim 1927f80c0673SMinchan Kim if (!sc->may_unmap) 192861317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1929f80c0673SMinchan Kim 1930599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 1931925b7673SJohannes Weiner 19325dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 19335dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 193489b5fae5SJohannes Weiner 1935599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken); 1936b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 19371cfb419bSKAMEZAWA Hiroyuki 1938599d0c95SMel Gorman __count_vm_events(PGREFILL, nr_scanned); 19399d5e6a9fSHugh Dickins 1940599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 19431da177e4SLinus Torvalds cond_resched(); 19441da177e4SLinus Torvalds page = lru_to_page(&l_hold); 19451da177e4SLinus Torvalds list_del(&page->lru); 19467e9cd484SRik van Riel 194739b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1948894bc310SLee Schermerhorn putback_lru_page(page); 1949894bc310SLee Schermerhorn continue; 1950894bc310SLee Schermerhorn } 1951894bc310SLee Schermerhorn 1952cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 1953cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 1954cc715d99SMel Gorman if (page_has_private(page)) 1955cc715d99SMel Gorman try_to_release_page(page, 0); 1956cc715d99SMel Gorman unlock_page(page); 1957cc715d99SMel Gorman } 1958cc715d99SMel Gorman } 1959cc715d99SMel Gorman 1960c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 1961c3ac9a8aSJohannes Weiner &vm_flags)) { 19629992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 19638cab4754SWu Fengguang /* 19648cab4754SWu Fengguang * Identify referenced, file-backed active pages and 19658cab4754SWu Fengguang * give them one more trip around the active list. So 19668cab4754SWu Fengguang * that executable code get better chances to stay in 19678cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 19688cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 19698cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 19708cab4754SWu Fengguang * so we ignore them here. 19718cab4754SWu Fengguang */ 197241e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 19738cab4754SWu Fengguang list_add(&page->lru, &l_active); 19748cab4754SWu Fengguang continue; 19758cab4754SWu Fengguang } 19768cab4754SWu Fengguang } 19777e9cd484SRik van Riel 19785205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 19791da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 19801da177e4SLinus Torvalds } 19811da177e4SLinus Torvalds 1982b555749aSAndrew Morton /* 19838cab4754SWu Fengguang * Move pages back to the lru list. 1984b555749aSAndrew Morton */ 1985599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 19864f98a2feSRik van Riel /* 19878cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 19888cab4754SWu Fengguang * even though only some of them are actually re-activated. This 19898cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 19907c0db9e9SJerome Marchand * get_scan_count. 1991556adecbSRik van Riel */ 1992b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 1993556adecbSRik van Riel 19949d998b4fSMichal Hocko nr_activate = move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru); 19959d998b4fSMichal Hocko nr_deactivate = move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE); 1996599d0c95SMel Gorman __mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken); 1997599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 19982bcf8879SHugh Dickins 1999747db954SJohannes Weiner mem_cgroup_uncharge_list(&l_hold); 2000b745bc85SMel Gorman free_hot_cold_page_list(&l_hold, true); 20019d998b4fSMichal Hocko trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate, 20029d998b4fSMichal Hocko nr_deactivate, nr_rotated, sc->priority, file); 20031da177e4SLinus Torvalds } 20041da177e4SLinus Torvalds 200559dc76b0SRik van Riel /* 200659dc76b0SRik van Riel * The inactive anon list should be small enough that the VM never has 200759dc76b0SRik van Riel * to do too much work. 200814797e23SKOSAKI Motohiro * 200959dc76b0SRik van Riel * The inactive file list should be small enough to leave most memory 201059dc76b0SRik van Riel * to the established workingset on the scan-resistant active list, 201159dc76b0SRik van Riel * but large enough to avoid thrashing the aggregate readahead window. 201259dc76b0SRik van Riel * 201359dc76b0SRik van Riel * Both inactive lists should also be large enough that each inactive 201459dc76b0SRik van Riel * page has a chance to be referenced again before it is reclaimed. 201559dc76b0SRik van Riel * 201659dc76b0SRik van Riel * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages 201759dc76b0SRik van Riel * on this LRU, maintained by the pageout code. A zone->inactive_ratio 201859dc76b0SRik van Riel * of 3 means 3:1 or 25% of the pages are kept on the inactive list. 201959dc76b0SRik van Riel * 202059dc76b0SRik van Riel * total target max 202159dc76b0SRik van Riel * memory ratio inactive 202259dc76b0SRik van Riel * ------------------------------------- 202359dc76b0SRik van Riel * 10MB 1 5MB 202459dc76b0SRik van Riel * 100MB 1 50MB 202559dc76b0SRik van Riel * 1GB 3 250MB 202659dc76b0SRik van Riel * 10GB 10 0.9GB 202759dc76b0SRik van Riel * 100GB 31 3GB 202859dc76b0SRik van Riel * 1TB 101 10GB 202959dc76b0SRik van Riel * 10TB 320 32GB 203014797e23SKOSAKI Motohiro */ 2031f8d1a311SMel Gorman static bool inactive_list_is_low(struct lruvec *lruvec, bool file, 2032dcec0b60SMichal Hocko struct scan_control *sc, bool trace) 203314797e23SKOSAKI Motohiro { 203459dc76b0SRik van Riel unsigned long inactive_ratio; 2035fd538803SMichal Hocko unsigned long inactive, active; 2036fd538803SMichal Hocko enum lru_list inactive_lru = file * LRU_FILE; 2037fd538803SMichal Hocko enum lru_list active_lru = file * LRU_FILE + LRU_ACTIVE; 203859dc76b0SRik van Riel unsigned long gb; 203959dc76b0SRik van Riel 204074e3f3c3SMinchan Kim /* 204174e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 204274e3f3c3SMinchan Kim * is pointless. 204374e3f3c3SMinchan Kim */ 204459dc76b0SRik van Riel if (!file && !total_swap_pages) 204542e2e457SYaowei Bai return false; 204674e3f3c3SMinchan Kim 2047fd538803SMichal Hocko inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx); 2048fd538803SMichal Hocko active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx); 2049f8d1a311SMel Gorman 205059dc76b0SRik van Riel gb = (inactive + active) >> (30 - PAGE_SHIFT); 205159dc76b0SRik van Riel if (gb) 205259dc76b0SRik van Riel inactive_ratio = int_sqrt(10 * gb); 2053b39415b2SRik van Riel else 205459dc76b0SRik van Riel inactive_ratio = 1; 205559dc76b0SRik van Riel 2056dcec0b60SMichal Hocko if (trace) 2057fd538803SMichal Hocko trace_mm_vmscan_inactive_list_is_low(lruvec_pgdat(lruvec)->node_id, 2058dcec0b60SMichal Hocko sc->reclaim_idx, 2059fd538803SMichal Hocko lruvec_lru_size(lruvec, inactive_lru, MAX_NR_ZONES), inactive, 2060fd538803SMichal Hocko lruvec_lru_size(lruvec, active_lru, MAX_NR_ZONES), active, 2061fd538803SMichal Hocko inactive_ratio, file); 2062fd538803SMichal Hocko 206359dc76b0SRik van Riel return inactive * inactive_ratio < active; 2064b39415b2SRik van Riel } 2065b39415b2SRik van Riel 20664f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 20671a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, struct scan_control *sc) 2068b69408e8SChristoph Lameter { 2069b39415b2SRik van Riel if (is_active_lru(lru)) { 2070dcec0b60SMichal Hocko if (inactive_list_is_low(lruvec, is_file_lru(lru), sc, true)) 20711a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 2072556adecbSRik van Riel return 0; 2073556adecbSRik van Riel } 2074556adecbSRik van Riel 20751a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 2076b69408e8SChristoph Lameter } 2077b69408e8SChristoph Lameter 20789a265114SJohannes Weiner enum scan_balance { 20799a265114SJohannes Weiner SCAN_EQUAL, 20809a265114SJohannes Weiner SCAN_FRACT, 20819a265114SJohannes Weiner SCAN_ANON, 20829a265114SJohannes Weiner SCAN_FILE, 20839a265114SJohannes Weiner }; 20849a265114SJohannes Weiner 20851da177e4SLinus Torvalds /* 20864f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 20874f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 20884f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 20894f98a2feSRik van Riel * onto the active list instead of evict. 20904f98a2feSRik van Riel * 2091be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 2092be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 20934f98a2feSRik van Riel */ 209433377678SVladimir Davydov static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, 20956b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *nr, 20966b4f7799SJohannes Weiner unsigned long *lru_pages) 20974f98a2feSRik van Riel { 209833377678SVladimir Davydov int swappiness = mem_cgroup_swappiness(memcg); 209990126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 21009a265114SJohannes Weiner u64 fraction[2]; 21019a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 2102599d0c95SMel Gorman struct pglist_data *pgdat = lruvec_pgdat(lruvec); 21039a265114SJohannes Weiner unsigned long anon_prio, file_prio; 21049a265114SJohannes Weiner enum scan_balance scan_balance; 21050bf1457fSJohannes Weiner unsigned long anon, file; 21069a265114SJohannes Weiner unsigned long ap, fp; 21079a265114SJohannes Weiner enum lru_list lru; 210876a33fc3SShaohua Li 210976a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 2110d8b38438SVladimir Davydov if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) { 21119a265114SJohannes Weiner scan_balance = SCAN_FILE; 211276a33fc3SShaohua Li goto out; 211376a33fc3SShaohua Li } 21144f98a2feSRik van Riel 211510316b31SJohannes Weiner /* 211610316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 211710316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 211810316b31SJohannes Weiner * disable swapping for individual groups completely when 211910316b31SJohannes Weiner * using the memory controller's swap limit feature would be 212010316b31SJohannes Weiner * too expensive. 212110316b31SJohannes Weiner */ 212202695175SJohannes Weiner if (!global_reclaim(sc) && !swappiness) { 21239a265114SJohannes Weiner scan_balance = SCAN_FILE; 212410316b31SJohannes Weiner goto out; 212510316b31SJohannes Weiner } 212610316b31SJohannes Weiner 212710316b31SJohannes Weiner /* 212810316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 212910316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 213010316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 213110316b31SJohannes Weiner */ 213202695175SJohannes Weiner if (!sc->priority && swappiness) { 21339a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 213410316b31SJohannes Weiner goto out; 213510316b31SJohannes Weiner } 213610316b31SJohannes Weiner 213711d16c25SJohannes Weiner /* 213862376251SJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 213962376251SJohannes Weiner * cache pages start out inactive, every cache fault will tip 214062376251SJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 214162376251SJohannes Weiner * shrinks, so does the window for rotation from references. 214262376251SJohannes Weiner * This means we have a runaway feedback loop where a tiny 214362376251SJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 214462376251SJohannes Weiner * anon pages. Try to detect this based on file LRU size. 214562376251SJohannes Weiner */ 214662376251SJohannes Weiner if (global_reclaim(sc)) { 2147599d0c95SMel Gorman unsigned long pgdatfile; 2148599d0c95SMel Gorman unsigned long pgdatfree; 2149599d0c95SMel Gorman int z; 2150599d0c95SMel Gorman unsigned long total_high_wmark = 0; 215162376251SJohannes Weiner 2152599d0c95SMel Gorman pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES); 2153599d0c95SMel Gorman pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) + 2154599d0c95SMel Gorman node_page_state(pgdat, NR_INACTIVE_FILE); 21552ab051e1SJerome Marchand 2156599d0c95SMel Gorman for (z = 0; z < MAX_NR_ZONES; z++) { 2157599d0c95SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 21586aa303deSMel Gorman if (!managed_zone(zone)) 2159599d0c95SMel Gorman continue; 2160599d0c95SMel Gorman 2161599d0c95SMel Gorman total_high_wmark += high_wmark_pages(zone); 2162599d0c95SMel Gorman } 2163599d0c95SMel Gorman 2164599d0c95SMel Gorman if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) { 216562376251SJohannes Weiner scan_balance = SCAN_ANON; 216662376251SJohannes Weiner goto out; 216762376251SJohannes Weiner } 216862376251SJohannes Weiner } 216962376251SJohannes Weiner 217062376251SJohannes Weiner /* 2171316bda0eSVladimir Davydov * If there is enough inactive page cache, i.e. if the size of the 2172316bda0eSVladimir Davydov * inactive list is greater than that of the active list *and* the 2173316bda0eSVladimir Davydov * inactive list actually has some pages to scan on this priority, we 2174316bda0eSVladimir Davydov * do not reclaim anything from the anonymous working set right now. 2175316bda0eSVladimir Davydov * Without the second condition we could end up never scanning an 2176316bda0eSVladimir Davydov * lruvec even if it has plenty of old anonymous pages unless the 2177316bda0eSVladimir Davydov * system is under heavy pressure. 2178e9868505SRik van Riel */ 2179dcec0b60SMichal Hocko if (!inactive_list_is_low(lruvec, true, sc, false) && 218071ab6cfeSMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) { 21819a265114SJohannes Weiner scan_balance = SCAN_FILE; 2182e9868505SRik van Riel goto out; 21834f98a2feSRik van Riel } 21844f98a2feSRik van Riel 21859a265114SJohannes Weiner scan_balance = SCAN_FRACT; 21869a265114SJohannes Weiner 21874f98a2feSRik van Riel /* 218858c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 218958c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 219058c37f6eSKOSAKI Motohiro */ 219102695175SJohannes Weiner anon_prio = swappiness; 219275b00af7SHugh Dickins file_prio = 200 - anon_prio; 219358c37f6eSKOSAKI Motohiro 219458c37f6eSKOSAKI Motohiro /* 21954f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 21964f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 21974f98a2feSRik van Riel * ratios to determine how valuable each cache is. 21984f98a2feSRik van Riel * 21994f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 22004f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 22014f98a2feSRik van Riel * up weighing recent references more than old ones. 22024f98a2feSRik van Riel * 22034f98a2feSRik van Riel * anon in [0], file in [1] 22044f98a2feSRik van Riel */ 22052ab051e1SJerome Marchand 2206fd538803SMichal Hocko anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) + 2207fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES); 2208fd538803SMichal Hocko file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) + 2209fd538803SMichal Hocko lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES); 22102ab051e1SJerome Marchand 2211599d0c95SMel Gorman spin_lock_irq(&pgdat->lru_lock); 221258c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 22136e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 22146e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 22154f98a2feSRik van Riel } 22164f98a2feSRik van Riel 22176e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 22186e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 22196e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 22204f98a2feSRik van Riel } 22214f98a2feSRik van Riel 22224f98a2feSRik van Riel /* 222300d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 222400d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 222500d8089cSRik van Riel * each list that were recently referenced and in active use. 22264f98a2feSRik van Riel */ 2227fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 22286e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 22294f98a2feSRik van Riel 2230fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 22316e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 2232599d0c95SMel Gorman spin_unlock_irq(&pgdat->lru_lock); 22334f98a2feSRik van Riel 223476a33fc3SShaohua Li fraction[0] = ap; 223576a33fc3SShaohua Li fraction[1] = fp; 223676a33fc3SShaohua Li denominator = ap + fp + 1; 223776a33fc3SShaohua Li out: 22386b4f7799SJohannes Weiner *lru_pages = 0; 22394111304dSHugh Dickins for_each_evictable_lru(lru) { 22404111304dSHugh Dickins int file = is_file_lru(lru); 2241d778df51SJohannes Weiner unsigned long size; 224276a33fc3SShaohua Li unsigned long scan; 224376a33fc3SShaohua Li 224471ab6cfeSMichal Hocko size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); 2245d778df51SJohannes Weiner scan = size >> sc->priority; 2246688035f7SJohannes Weiner /* 2247688035f7SJohannes Weiner * If the cgroup's already been deleted, make sure to 2248688035f7SJohannes Weiner * scrape out the remaining cache. 2249688035f7SJohannes Weiner */ 2250688035f7SJohannes Weiner if (!scan && !mem_cgroup_online(memcg)) 2251d778df51SJohannes Weiner scan = min(size, SWAP_CLUSTER_MAX); 22529a265114SJohannes Weiner 22539a265114SJohannes Weiner switch (scan_balance) { 22549a265114SJohannes Weiner case SCAN_EQUAL: 22559a265114SJohannes Weiner /* Scan lists relative to size */ 22569a265114SJohannes Weiner break; 22579a265114SJohannes Weiner case SCAN_FRACT: 22589a265114SJohannes Weiner /* 22599a265114SJohannes Weiner * Scan types proportional to swappiness and 22609a265114SJohannes Weiner * their relative recent reclaim efficiency. 22619a265114SJohannes Weiner */ 22626f04f48dSSuleiman Souhlal scan = div64_u64(scan * fraction[file], 22636f04f48dSSuleiman Souhlal denominator); 22649a265114SJohannes Weiner break; 22659a265114SJohannes Weiner case SCAN_FILE: 22669a265114SJohannes Weiner case SCAN_ANON: 22679a265114SJohannes Weiner /* Scan one type exclusively */ 22686b4f7799SJohannes Weiner if ((scan_balance == SCAN_FILE) != file) { 22696b4f7799SJohannes Weiner size = 0; 22709a265114SJohannes Weiner scan = 0; 22716b4f7799SJohannes Weiner } 22729a265114SJohannes Weiner break; 22739a265114SJohannes Weiner default: 22749a265114SJohannes Weiner /* Look ma, no brain */ 22759a265114SJohannes Weiner BUG(); 22769a265114SJohannes Weiner } 22776b4f7799SJohannes Weiner 22786b4f7799SJohannes Weiner *lru_pages += size; 22794111304dSHugh Dickins nr[lru] = scan; 228076a33fc3SShaohua Li } 22816e08a369SWu Fengguang } 22824f98a2feSRik van Riel 22839b4f98cdSJohannes Weiner /* 2284a9dd0a83SMel Gorman * This is a basic per-node page freer. Used by both kswapd and direct reclaim. 22859b4f98cdSJohannes Weiner */ 2286a9dd0a83SMel Gorman static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg, 22876b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *lru_pages) 22889b4f98cdSJohannes Weiner { 2289ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 22909b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2291e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 22929b4f98cdSJohannes Weiner unsigned long nr_to_scan; 22939b4f98cdSJohannes Weiner enum lru_list lru; 22949b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 22959b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 22969b4f98cdSJohannes Weiner struct blk_plug plug; 22971a501907SMel Gorman bool scan_adjusted; 22989b4f98cdSJohannes Weiner 229933377678SVladimir Davydov get_scan_count(lruvec, memcg, sc, nr, lru_pages); 23009b4f98cdSJohannes Weiner 2301e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2302e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2303e82e0561SMel Gorman 23041a501907SMel Gorman /* 23051a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 23061a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 23071a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 23081a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 23091a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 23101a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 23111a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 23121a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 23131a501907SMel Gorman * dropped to zero at the first pass. 23141a501907SMel Gorman */ 23151a501907SMel Gorman scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() && 23161a501907SMel Gorman sc->priority == DEF_PRIORITY); 23171a501907SMel Gorman 23189b4f98cdSJohannes Weiner blk_start_plug(&plug); 23199b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 23209b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2321e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2322e82e0561SMel Gorman unsigned long nr_scanned; 2323e82e0561SMel Gorman 23249b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 23259b4f98cdSJohannes Weiner if (nr[lru]) { 23269b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 23279b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 23289b4f98cdSJohannes Weiner 23299b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 23309b4f98cdSJohannes Weiner lruvec, sc); 23319b4f98cdSJohannes Weiner } 23329b4f98cdSJohannes Weiner } 2333e82e0561SMel Gorman 2334bd041733SMichal Hocko cond_resched(); 2335bd041733SMichal Hocko 2336e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2337e82e0561SMel Gorman continue; 2338e82e0561SMel Gorman 23399b4f98cdSJohannes Weiner /* 2340e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 23411a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2342e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2343e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2344e82e0561SMel Gorman * proportional to the original scan target. 2345e82e0561SMel Gorman */ 2346e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2347e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2348e82e0561SMel Gorman 23491a501907SMel Gorman /* 23501a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 23511a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 23521a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 23531a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 23541a501907SMel Gorman */ 23551a501907SMel Gorman if (!nr_file || !nr_anon) 23561a501907SMel Gorman break; 23571a501907SMel Gorman 2358e82e0561SMel Gorman if (nr_file > nr_anon) { 2359e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2360e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2361e82e0561SMel Gorman lru = LRU_BASE; 2362e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2363e82e0561SMel Gorman } else { 2364e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2365e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2366e82e0561SMel Gorman lru = LRU_FILE; 2367e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2368e82e0561SMel Gorman } 2369e82e0561SMel Gorman 2370e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2371e82e0561SMel Gorman nr[lru] = 0; 2372e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2373e82e0561SMel Gorman 2374e82e0561SMel Gorman /* 2375e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2376e82e0561SMel Gorman * scan target and the percentage scanning already complete 2377e82e0561SMel Gorman */ 2378e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2379e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2380e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2381e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2382e82e0561SMel Gorman 2383e82e0561SMel Gorman lru += LRU_ACTIVE; 2384e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2385e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2386e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2387e82e0561SMel Gorman 2388e82e0561SMel Gorman scan_adjusted = true; 23899b4f98cdSJohannes Weiner } 23909b4f98cdSJohannes Weiner blk_finish_plug(&plug); 23919b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 23929b4f98cdSJohannes Weiner 23939b4f98cdSJohannes Weiner /* 23949b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 23959b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 23969b4f98cdSJohannes Weiner */ 2397dcec0b60SMichal Hocko if (inactive_list_is_low(lruvec, false, sc, true)) 23989b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 23999b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 24009b4f98cdSJohannes Weiner } 24019b4f98cdSJohannes Weiner 240223b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 24039e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 240423b9da55SMel Gorman { 2405d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 240623b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 24079e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 240823b9da55SMel Gorman return true; 240923b9da55SMel Gorman 241023b9da55SMel Gorman return false; 241123b9da55SMel Gorman } 241223b9da55SMel Gorman 24134f98a2feSRik van Riel /* 241423b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 241523b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 241623b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 241723b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 241823b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 24193e7d3449SMel Gorman */ 2420a9dd0a83SMel Gorman static inline bool should_continue_reclaim(struct pglist_data *pgdat, 24213e7d3449SMel Gorman unsigned long nr_reclaimed, 24223e7d3449SMel Gorman unsigned long nr_scanned, 24233e7d3449SMel Gorman struct scan_control *sc) 24243e7d3449SMel Gorman { 24253e7d3449SMel Gorman unsigned long pages_for_compaction; 24263e7d3449SMel Gorman unsigned long inactive_lru_pages; 2427a9dd0a83SMel Gorman int z; 24283e7d3449SMel Gorman 24293e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 24309e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 24313e7d3449SMel Gorman return false; 24323e7d3449SMel Gorman 24332876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 24342876592fSMel Gorman if (sc->gfp_mask & __GFP_REPEAT) { 24353e7d3449SMel Gorman /* 24362876592fSMel Gorman * For __GFP_REPEAT allocations, stop reclaiming if the 24372876592fSMel Gorman * full LRU list has been scanned and we are still failing 24382876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 24392876592fSMel Gorman * expensive but a __GFP_REPEAT caller really wants to succeed 24403e7d3449SMel Gorman */ 24413e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 24423e7d3449SMel Gorman return false; 24432876592fSMel Gorman } else { 24442876592fSMel Gorman /* 24452876592fSMel Gorman * For non-__GFP_REPEAT allocations which can presumably 24462876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 24472876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 24482876592fSMel Gorman * pages that were scanned. This will return to the 24492876592fSMel Gorman * caller faster at the risk reclaim/compaction and 24502876592fSMel Gorman * the resulting allocation attempt fails 24512876592fSMel Gorman */ 24522876592fSMel Gorman if (!nr_reclaimed) 24532876592fSMel Gorman return false; 24542876592fSMel Gorman } 24553e7d3449SMel Gorman 24563e7d3449SMel Gorman /* 24573e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 24583e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 24593e7d3449SMel Gorman */ 24609861a62cSVlastimil Babka pages_for_compaction = compact_gap(sc->order); 2461a9dd0a83SMel Gorman inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE); 2462ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 2463a9dd0a83SMel Gorman inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); 24643e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 24653e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 24663e7d3449SMel Gorman return true; 24673e7d3449SMel Gorman 24683e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 2469a9dd0a83SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 2470a9dd0a83SMel Gorman struct zone *zone = &pgdat->node_zones[z]; 24716aa303deSMel Gorman if (!managed_zone(zone)) 2472a9dd0a83SMel Gorman continue; 2473a9dd0a83SMel Gorman 2474a9dd0a83SMel Gorman switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) { 2475cf378319SVlastimil Babka case COMPACT_SUCCESS: 24763e7d3449SMel Gorman case COMPACT_CONTINUE: 24773e7d3449SMel Gorman return false; 24783e7d3449SMel Gorman default: 2479a9dd0a83SMel Gorman /* check next zone */ 2480a9dd0a83SMel Gorman ; 24813e7d3449SMel Gorman } 24823e7d3449SMel Gorman } 2483a9dd0a83SMel Gorman return true; 2484a9dd0a83SMel Gorman } 24853e7d3449SMel Gorman 2486970a39a3SMel Gorman static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) 2487f16015fbSJohannes Weiner { 2488cb731d6cSVladimir Davydov struct reclaim_state *reclaim_state = current->reclaim_state; 24899b4f98cdSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 24902344d7e4SJohannes Weiner bool reclaimable = false; 24919b4f98cdSJohannes Weiner 24929b4f98cdSJohannes Weiner do { 24935660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 24945660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 2495ef8f2327SMel Gorman .pgdat = pgdat, 24969e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 24975660048cSJohannes Weiner }; 2498a9dd0a83SMel Gorman unsigned long node_lru_pages = 0; 2499694fbc0fSAndrew Morton struct mem_cgroup *memcg; 25005660048cSJohannes Weiner 25019b4f98cdSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 25029b4f98cdSJohannes Weiner nr_scanned = sc->nr_scanned; 25039b4f98cdSJohannes Weiner 2504694fbc0fSAndrew Morton memcg = mem_cgroup_iter(root, NULL, &reclaim); 2505694fbc0fSAndrew Morton do { 25066b4f7799SJohannes Weiner unsigned long lru_pages; 25078e8ae645SJohannes Weiner unsigned long reclaimed; 2508cb731d6cSVladimir Davydov unsigned long scanned; 25099b4f98cdSJohannes Weiner 2510241994edSJohannes Weiner if (mem_cgroup_low(root, memcg)) { 2511d6622f63SYisheng Xie if (!sc->memcg_low_reclaim) { 2512d6622f63SYisheng Xie sc->memcg_low_skipped = 1; 2513241994edSJohannes Weiner continue; 2514d6622f63SYisheng Xie } 2515241994edSJohannes Weiner mem_cgroup_events(memcg, MEMCG_LOW, 1); 2516241994edSJohannes Weiner } 2517241994edSJohannes Weiner 25188e8ae645SJohannes Weiner reclaimed = sc->nr_reclaimed; 2519cb731d6cSVladimir Davydov scanned = sc->nr_scanned; 25205660048cSJohannes Weiner 2521a9dd0a83SMel Gorman shrink_node_memcg(pgdat, memcg, sc, &lru_pages); 2522a9dd0a83SMel Gorman node_lru_pages += lru_pages; 2523f9be23d6SKonstantin Khlebnikov 2524b5afba29SVladimir Davydov if (memcg) 2525a9dd0a83SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, 2526cb731d6cSVladimir Davydov memcg, sc->nr_scanned - scanned, 2527cb731d6cSVladimir Davydov lru_pages); 2528cb731d6cSVladimir Davydov 25298e8ae645SJohannes Weiner /* Record the group's reclaim efficiency */ 25308e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, memcg, false, 25318e8ae645SJohannes Weiner sc->nr_scanned - scanned, 25328e8ae645SJohannes Weiner sc->nr_reclaimed - reclaimed); 25338e8ae645SJohannes Weiner 25345660048cSJohannes Weiner /* 2535a394cb8eSMichal Hocko * Direct reclaim and kswapd have to scan all memory 2536a394cb8eSMichal Hocko * cgroups to fulfill the overall scan target for the 2537a9dd0a83SMel Gorman * node. 2538a394cb8eSMichal Hocko * 2539a394cb8eSMichal Hocko * Limit reclaim, on the other hand, only cares about 2540a394cb8eSMichal Hocko * nr_to_reclaim pages to be reclaimed and it will 2541a394cb8eSMichal Hocko * retry with decreasing priority if one round over the 2542a394cb8eSMichal Hocko * whole hierarchy is not sufficient. 25435660048cSJohannes Weiner */ 2544a394cb8eSMichal Hocko if (!global_reclaim(sc) && 2545a394cb8eSMichal Hocko sc->nr_reclaimed >= sc->nr_to_reclaim) { 25465660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 25475660048cSJohannes Weiner break; 25485660048cSJohannes Weiner } 2549241994edSJohannes Weiner } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim))); 255070ddf637SAnton Vorontsov 25516b4f7799SJohannes Weiner /* 25526b4f7799SJohannes Weiner * Shrink the slab caches in the same proportion that 25536b4f7799SJohannes Weiner * the eligible LRU pages were scanned. 25546b4f7799SJohannes Weiner */ 2555b2e18757SMel Gorman if (global_reclaim(sc)) 2556a9dd0a83SMel Gorman shrink_slab(sc->gfp_mask, pgdat->node_id, NULL, 25576b4f7799SJohannes Weiner sc->nr_scanned - nr_scanned, 2558a9dd0a83SMel Gorman node_lru_pages); 25596b4f7799SJohannes Weiner 25606b4f7799SJohannes Weiner if (reclaim_state) { 2561cb731d6cSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 25626b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 25636b4f7799SJohannes Weiner } 25646b4f7799SJohannes Weiner 25658e8ae645SJohannes Weiner /* Record the subtree's reclaim efficiency */ 25668e8ae645SJohannes Weiner vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true, 256770ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 256870ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 256970ddf637SAnton Vorontsov 25702344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 25712344d7e4SJohannes Weiner reclaimable = true; 25722344d7e4SJohannes Weiner 2573a9dd0a83SMel Gorman } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, 25749b4f98cdSJohannes Weiner sc->nr_scanned - nr_scanned, sc)); 25752344d7e4SJohannes Weiner 2576c73322d0SJohannes Weiner /* 2577c73322d0SJohannes Weiner * Kswapd gives up on balancing particular nodes after too 2578c73322d0SJohannes Weiner * many failures to reclaim anything from them and goes to 2579c73322d0SJohannes Weiner * sleep. On reclaim progress, reset the failure counter. A 2580c73322d0SJohannes Weiner * successful direct reclaim run will revive a dormant kswapd. 2581c73322d0SJohannes Weiner */ 2582c73322d0SJohannes Weiner if (reclaimable) 2583c73322d0SJohannes Weiner pgdat->kswapd_failures = 0; 2584c73322d0SJohannes Weiner 25852344d7e4SJohannes Weiner return reclaimable; 2586f16015fbSJohannes Weiner } 2587f16015fbSJohannes Weiner 258853853e2dSVlastimil Babka /* 2589fdd4c614SVlastimil Babka * Returns true if compaction should go ahead for a costly-order request, or 2590fdd4c614SVlastimil Babka * the allocation would already succeed without compaction. Return false if we 2591fdd4c614SVlastimil Babka * should reclaim first. 259253853e2dSVlastimil Babka */ 25934f588331SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 2594fe4b1b24SMel Gorman { 259531483b6aSMel Gorman unsigned long watermark; 2596fdd4c614SVlastimil Babka enum compact_result suitable; 2597fe4b1b24SMel Gorman 2598fdd4c614SVlastimil Babka suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx); 2599fdd4c614SVlastimil Babka if (suitable == COMPACT_SUCCESS) 2600fdd4c614SVlastimil Babka /* Allocation should succeed already. Don't reclaim. */ 2601fdd4c614SVlastimil Babka return true; 2602fdd4c614SVlastimil Babka if (suitable == COMPACT_SKIPPED) 2603fdd4c614SVlastimil Babka /* Compaction cannot yet proceed. Do reclaim. */ 2604fe4b1b24SMel Gorman return false; 2605fe4b1b24SMel Gorman 2606fdd4c614SVlastimil Babka /* 2607fdd4c614SVlastimil Babka * Compaction is already possible, but it takes time to run and there 2608fdd4c614SVlastimil Babka * are potentially other callers using the pages just freed. So proceed 2609fdd4c614SVlastimil Babka * with reclaim to make a buffer of free pages available to give 2610fdd4c614SVlastimil Babka * compaction a reasonable chance of completing and allocating the page. 2611fdd4c614SVlastimil Babka * Note that we won't actually reclaim the whole buffer in one attempt 2612fdd4c614SVlastimil Babka * as the target watermark in should_continue_reclaim() is lower. But if 2613fdd4c614SVlastimil Babka * we are already above the high+gap watermark, don't reclaim at all. 2614fdd4c614SVlastimil Babka */ 2615fdd4c614SVlastimil Babka watermark = high_wmark_pages(zone) + compact_gap(sc->order); 2616fdd4c614SVlastimil Babka 2617fdd4c614SVlastimil Babka return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx); 2618fe4b1b24SMel Gorman } 2619fe4b1b24SMel Gorman 26201da177e4SLinus Torvalds /* 26211da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 26221da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 26231da177e4SLinus Torvalds * request. 26241da177e4SLinus Torvalds * 26251da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 26261da177e4SLinus Torvalds * scan then give up on it. 26271da177e4SLinus Torvalds */ 26280a0337e0SMichal Hocko static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 26291da177e4SLinus Torvalds { 2630dd1a239fSMel Gorman struct zoneref *z; 263154a6eb5cSMel Gorman struct zone *zone; 26320608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 26330608f43dSAndrew Morton unsigned long nr_soft_scanned; 2634619d0d76SWeijie Yang gfp_t orig_mask; 263579dafcdcSMel Gorman pg_data_t *last_pgdat = NULL; 26361cfb419bSKAMEZAWA Hiroyuki 2637cc715d99SMel Gorman /* 2638cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2639cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2640cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2641cc715d99SMel Gorman */ 2642619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 2643b2e18757SMel Gorman if (buffer_heads_over_limit) { 2644cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 26454f588331SMel Gorman sc->reclaim_idx = gfp_zone(sc->gfp_mask); 2646b2e18757SMel Gorman } 2647cc715d99SMel Gorman 2648d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2649b2e18757SMel Gorman sc->reclaim_idx, sc->nodemask) { 2650b2e18757SMel Gorman /* 26511cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 26521cfb419bSKAMEZAWA Hiroyuki * to global LRU. 26531cfb419bSKAMEZAWA Hiroyuki */ 265489b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2655344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 2656344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 26571da177e4SLinus Torvalds continue; 265865ec02cbSVladimir Davydov 2659e0887c19SRik van Riel /* 2660e0c23279SMel Gorman * If we already have plenty of memory free for 2661e0c23279SMel Gorman * compaction in this zone, don't free any more. 2662e0c23279SMel Gorman * Even though compaction is invoked for any 2663e0c23279SMel Gorman * non-zero order, only frequent costly order 2664e0c23279SMel Gorman * reclamation is disruptive enough to become a 2665c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2666c7cfa37bSCopot Alexandru * page allocations. 2667e0887c19SRik van Riel */ 26680b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 26690b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 26704f588331SMel Gorman compaction_ready(zone, sc)) { 26710b06496aSJohannes Weiner sc->compaction_ready = true; 2672e0887c19SRik van Riel continue; 2673e0887c19SRik van Riel } 26740b06496aSJohannes Weiner 26750608f43dSAndrew Morton /* 267679dafcdcSMel Gorman * Shrink each node in the zonelist once. If the 267779dafcdcSMel Gorman * zonelist is ordered by zone (not the default) then a 267879dafcdcSMel Gorman * node may be shrunk multiple times but in that case 267979dafcdcSMel Gorman * the user prefers lower zones being preserved. 268079dafcdcSMel Gorman */ 268179dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 268279dafcdcSMel Gorman continue; 268379dafcdcSMel Gorman 268479dafcdcSMel Gorman /* 26850608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 26860608f43dSAndrew Morton * and returns the number of reclaimed pages and 26870608f43dSAndrew Morton * scanned pages. This works for global memory pressure 26880608f43dSAndrew Morton * and balancing, not for a memcg's limit. 26890608f43dSAndrew Morton */ 26900608f43dSAndrew Morton nr_soft_scanned = 0; 2691ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat, 26920608f43dSAndrew Morton sc->order, sc->gfp_mask, 26930608f43dSAndrew Morton &nr_soft_scanned); 26940608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 26950608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 2696ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2697ac34a1a3SKAMEZAWA Hiroyuki } 2698d149e3b2SYing Han 269979dafcdcSMel Gorman /* See comment about same check for global reclaim above */ 270079dafcdcSMel Gorman if (zone->zone_pgdat == last_pgdat) 270179dafcdcSMel Gorman continue; 270279dafcdcSMel Gorman last_pgdat = zone->zone_pgdat; 2703970a39a3SMel Gorman shrink_node(zone->zone_pgdat, sc); 27041da177e4SLinus Torvalds } 2705e0c23279SMel Gorman 270665ec02cbSVladimir Davydov /* 2707619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 2708619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 2709619d0d76SWeijie Yang */ 2710619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 27111da177e4SLinus Torvalds } 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds /* 27141da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 27151da177e4SLinus Torvalds * 27161da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 27171da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 27181da177e4SLinus Torvalds * 27191da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 27201da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 27215b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 27225b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 27235b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 27245b0830cbSJens Axboe * work, and the allocation attempt will fail. 2725a41f24eaSNishanth Aravamudan * 2726a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2727a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 27281da177e4SLinus Torvalds */ 2729dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 27303115cd91SVladimir Davydov struct scan_control *sc) 27311da177e4SLinus Torvalds { 2732241994edSJohannes Weiner int initial_priority = sc->priority; 2733241994edSJohannes Weiner retry: 2734873b4771SKeika Kobayashi delayacct_freepages_start(); 2735873b4771SKeika Kobayashi 273689b5fae5SJohannes Weiner if (global_reclaim(sc)) 27377cc30fcfSMel Gorman __count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1); 27381da177e4SLinus Torvalds 27399e3b2f8cSKonstantin Khlebnikov do { 274070ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 274170ddf637SAnton Vorontsov sc->priority); 274266e1707bSBalbir Singh sc->nr_scanned = 0; 27430a0337e0SMichal Hocko shrink_zones(zonelist, sc); 2744e0c23279SMel Gorman 2745bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 27460b06496aSJohannes Weiner break; 27470b06496aSJohannes Weiner 27480b06496aSJohannes Weiner if (sc->compaction_ready) 27490b06496aSJohannes Weiner break; 27501da177e4SLinus Torvalds 27511da177e4SLinus Torvalds /* 27520e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 27530e50ce3bSMinchan Kim * writepage even in laptop mode. 27540e50ce3bSMinchan Kim */ 27550e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 27560e50ce3bSMinchan Kim sc->may_writepage = 1; 27570b06496aSJohannes Weiner } while (--sc->priority >= 0); 2758bb21c7ceSKOSAKI Motohiro 2759873b4771SKeika Kobayashi delayacct_freepages_end(); 2760873b4771SKeika Kobayashi 2761bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 2762bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 2763bb21c7ceSKOSAKI Motohiro 27640cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 27650b06496aSJohannes Weiner if (sc->compaction_ready) 27667335084dSMel Gorman return 1; 27677335084dSMel Gorman 2768241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 2769d6622f63SYisheng Xie if (sc->memcg_low_skipped) { 2770241994edSJohannes Weiner sc->priority = initial_priority; 2771d6622f63SYisheng Xie sc->memcg_low_reclaim = 1; 2772d6622f63SYisheng Xie sc->memcg_low_skipped = 0; 2773241994edSJohannes Weiner goto retry; 2774241994edSJohannes Weiner } 2775241994edSJohannes Weiner 2776bb21c7ceSKOSAKI Motohiro return 0; 27771da177e4SLinus Torvalds } 27781da177e4SLinus Torvalds 2779c73322d0SJohannes Weiner static bool allow_direct_reclaim(pg_data_t *pgdat) 27805515061dSMel Gorman { 27815515061dSMel Gorman struct zone *zone; 27825515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 27835515061dSMel Gorman unsigned long free_pages = 0; 27845515061dSMel Gorman int i; 27855515061dSMel Gorman bool wmark_ok; 27865515061dSMel Gorman 2787c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 2788c73322d0SJohannes Weiner return true; 2789c73322d0SJohannes Weiner 27905515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 27915515061dSMel Gorman zone = &pgdat->node_zones[i]; 2792d450abd8SJohannes Weiner if (!managed_zone(zone)) 2793d450abd8SJohannes Weiner continue; 2794d450abd8SJohannes Weiner 2795d450abd8SJohannes Weiner if (!zone_reclaimable_pages(zone)) 2796675becceSMel Gorman continue; 2797675becceSMel Gorman 27985515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 27995515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 28005515061dSMel Gorman } 28015515061dSMel Gorman 2802675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 2803675becceSMel Gorman if (!pfmemalloc_reserve) 2804675becceSMel Gorman return true; 2805675becceSMel Gorman 28065515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 28075515061dSMel Gorman 28085515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 28095515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 281038087d9bSMel Gorman pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx, 28115515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 28125515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 28135515061dSMel Gorman } 28145515061dSMel Gorman 28155515061dSMel Gorman return wmark_ok; 28165515061dSMel Gorman } 28175515061dSMel Gorman 28185515061dSMel Gorman /* 28195515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 28205515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 28215515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 282250694c28SMel Gorman * when the low watermark is reached. 282350694c28SMel Gorman * 282450694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 282550694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 28265515061dSMel Gorman */ 282750694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 28285515061dSMel Gorman nodemask_t *nodemask) 28295515061dSMel Gorman { 2830675becceSMel Gorman struct zoneref *z; 28315515061dSMel Gorman struct zone *zone; 2832675becceSMel Gorman pg_data_t *pgdat = NULL; 28335515061dSMel Gorman 28345515061dSMel Gorman /* 28355515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 28365515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 28375515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 28385515061dSMel Gorman * committing a transaction where throttling it could forcing other 28395515061dSMel Gorman * processes to block on log_wait_commit(). 28405515061dSMel Gorman */ 28415515061dSMel Gorman if (current->flags & PF_KTHREAD) 284250694c28SMel Gorman goto out; 284350694c28SMel Gorman 284450694c28SMel Gorman /* 284550694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 284650694c28SMel Gorman * It should return quickly so it can exit and free its memory 284750694c28SMel Gorman */ 284850694c28SMel Gorman if (fatal_signal_pending(current)) 284950694c28SMel Gorman goto out; 28505515061dSMel Gorman 2851675becceSMel Gorman /* 2852675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 2853675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 2854675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 2855675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 2856675becceSMel Gorman * 2857675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 2858675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 2859675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 2860675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 2861675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 2862675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 2863675becceSMel Gorman * should make reasonable progress. 2864675becceSMel Gorman */ 2865675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 286617636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 2867675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 2868675becceSMel Gorman continue; 2869675becceSMel Gorman 2870675becceSMel Gorman /* Throttle based on the first usable node */ 28715515061dSMel Gorman pgdat = zone->zone_pgdat; 2872c73322d0SJohannes Weiner if (allow_direct_reclaim(pgdat)) 287350694c28SMel Gorman goto out; 2874675becceSMel Gorman break; 2875675becceSMel Gorman } 2876675becceSMel Gorman 2877675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 2878675becceSMel Gorman if (!pgdat) 2879675becceSMel Gorman goto out; 28805515061dSMel Gorman 288168243e76SMel Gorman /* Account for the throttling */ 288268243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 288368243e76SMel Gorman 28845515061dSMel Gorman /* 28855515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 28865515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 28875515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 28885515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 28895515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 28905515061dSMel Gorman * second before continuing. 28915515061dSMel Gorman */ 28925515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 28935515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 2894c73322d0SJohannes Weiner allow_direct_reclaim(pgdat), HZ); 289550694c28SMel Gorman 289650694c28SMel Gorman goto check_pending; 28975515061dSMel Gorman } 28985515061dSMel Gorman 28995515061dSMel Gorman /* Throttle until kswapd wakes the process */ 29005515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 2901c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)); 290250694c28SMel Gorman 290350694c28SMel Gorman check_pending: 290450694c28SMel Gorman if (fatal_signal_pending(current)) 290550694c28SMel Gorman return true; 290650694c28SMel Gorman 290750694c28SMel Gorman out: 290850694c28SMel Gorman return false; 29095515061dSMel Gorman } 29105515061dSMel Gorman 2911dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 2912327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 291366e1707bSBalbir Singh { 291433906bc5SMel Gorman unsigned long nr_reclaimed; 291566e1707bSBalbir Singh struct scan_control sc = { 291622fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 29177dea19f9SMichal Hocko .gfp_mask = (gfp_mask = current_gfp_context(gfp_mask)), 2918b2e18757SMel Gorman .reclaim_idx = gfp_zone(gfp_mask), 2919ee814fe2SJohannes Weiner .order = order, 2920ee814fe2SJohannes Weiner .nodemask = nodemask, 2921ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 2922ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 2923a6dc60f8SJohannes Weiner .may_unmap = 1, 29242e2e4259SKOSAKI Motohiro .may_swap = 1, 292566e1707bSBalbir Singh }; 292666e1707bSBalbir Singh 29275515061dSMel Gorman /* 292850694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 292950694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 293050694c28SMel Gorman * point. 29315515061dSMel Gorman */ 293250694c28SMel Gorman if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask)) 29335515061dSMel Gorman return 1; 29345515061dSMel Gorman 293533906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 293633906bc5SMel Gorman sc.may_writepage, 2937e5146b12SMel Gorman gfp_mask, 2938e5146b12SMel Gorman sc.reclaim_idx); 293933906bc5SMel Gorman 29403115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 294133906bc5SMel Gorman 294233906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 294333906bc5SMel Gorman 294433906bc5SMel Gorman return nr_reclaimed; 294566e1707bSBalbir Singh } 294666e1707bSBalbir Singh 2947c255a458SAndrew Morton #ifdef CONFIG_MEMCG 294866e1707bSBalbir Singh 2949a9dd0a83SMel Gorman unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, 29504e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 2951ef8f2327SMel Gorman pg_data_t *pgdat, 29520ae5e89cSYing Han unsigned long *nr_scanned) 29534e416953SBalbir Singh { 29544e416953SBalbir Singh struct scan_control sc = { 2955b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2956ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 29574e416953SBalbir Singh .may_writepage = !laptop_mode, 29584e416953SBalbir Singh .may_unmap = 1, 2959b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 29604e416953SBalbir Singh .may_swap = !noswap, 29614e416953SBalbir Singh }; 29626b4f7799SJohannes Weiner unsigned long lru_pages; 29630ae5e89cSYing Han 29644e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 29654e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 2966bdce6d9eSKOSAKI Motohiro 29679e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 2968bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2969e5146b12SMel Gorman sc.gfp_mask, 2970e5146b12SMel Gorman sc.reclaim_idx); 2971bdce6d9eSKOSAKI Motohiro 29724e416953SBalbir Singh /* 29734e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 29744e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 2975a9dd0a83SMel Gorman * if we don't reclaim here, the shrink_node from balance_pgdat 29764e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 29774e416953SBalbir Singh * the priority and make it zero. 29784e416953SBalbir Singh */ 2979ef8f2327SMel Gorman shrink_node_memcg(pgdat, memcg, &sc, &lru_pages); 2980bdce6d9eSKOSAKI Motohiro 2981bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 2982bdce6d9eSKOSAKI Motohiro 29830ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 29844e416953SBalbir Singh return sc.nr_reclaimed; 29854e416953SBalbir Singh } 29864e416953SBalbir Singh 298772835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 2988b70a2a21SJohannes Weiner unsigned long nr_pages, 29898c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 2990b70a2a21SJohannes Weiner bool may_swap) 299166e1707bSBalbir Singh { 29924e416953SBalbir Singh struct zonelist *zonelist; 2993bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 2994889976dbSYing Han int nid; 299566e1707bSBalbir Singh struct scan_control sc = { 2996b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 29977dea19f9SMichal Hocko .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | 2998ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 2999b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 3000ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 3001ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 300266e1707bSBalbir Singh .may_writepage = !laptop_mode, 3003a6dc60f8SJohannes Weiner .may_unmap = 1, 3004b70a2a21SJohannes Weiner .may_swap = may_swap, 3005a09ed5e0SYing Han }; 300666e1707bSBalbir Singh 3007889976dbSYing Han /* 3008889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 3009889976dbSYing Han * take care of from where we get pages. So the node where we start the 3010889976dbSYing Han * scan does not need to be the current node. 3011889976dbSYing Han */ 301272835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 3013889976dbSYing Han 3014c9634cf0SAneesh Kumar K.V zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK]; 3015bdce6d9eSKOSAKI Motohiro 3016bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 3017bdce6d9eSKOSAKI Motohiro sc.may_writepage, 3018e5146b12SMel Gorman sc.gfp_mask, 3019e5146b12SMel Gorman sc.reclaim_idx); 3020bdce6d9eSKOSAKI Motohiro 302189a28483SJohannes Weiner current->flags |= PF_MEMALLOC; 30223115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 302389a28483SJohannes Weiner current->flags &= ~PF_MEMALLOC; 3024bdce6d9eSKOSAKI Motohiro 3025bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 3026bdce6d9eSKOSAKI Motohiro 3027bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 302866e1707bSBalbir Singh } 302966e1707bSBalbir Singh #endif 303066e1707bSBalbir Singh 30311d82de61SMel Gorman static void age_active_anon(struct pglist_data *pgdat, 3032ef8f2327SMel Gorman struct scan_control *sc) 3033f16015fbSJohannes Weiner { 3034b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 3035b95a2f2dSJohannes Weiner 3036b95a2f2dSJohannes Weiner if (!total_swap_pages) 3037b95a2f2dSJohannes Weiner return; 3038b95a2f2dSJohannes Weiner 3039b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 3040b95a2f2dSJohannes Weiner do { 3041ef8f2327SMel Gorman struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg); 3042f16015fbSJohannes Weiner 3043dcec0b60SMichal Hocko if (inactive_list_is_low(lruvec, false, sc, true)) 30441a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 30459e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 3046b95a2f2dSJohannes Weiner 3047b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 3048b95a2f2dSJohannes Weiner } while (memcg); 3049f16015fbSJohannes Weiner } 3050f16015fbSJohannes Weiner 3051e716f2ebSMel Gorman /* 3052e716f2ebSMel Gorman * Returns true if there is an eligible zone balanced for the request order 3053e716f2ebSMel Gorman * and classzone_idx 3054e716f2ebSMel Gorman */ 3055e716f2ebSMel Gorman static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx) 305660cefed4SJohannes Weiner { 3057e716f2ebSMel Gorman int i; 3058e716f2ebSMel Gorman unsigned long mark = -1; 3059e716f2ebSMel Gorman struct zone *zone; 306060cefed4SJohannes Weiner 3061e716f2ebSMel Gorman for (i = 0; i <= classzone_idx; i++) { 3062e716f2ebSMel Gorman zone = pgdat->node_zones + i; 30636256c6b4SMel Gorman 3064e716f2ebSMel Gorman if (!managed_zone(zone)) 3065e716f2ebSMel Gorman continue; 3066e716f2ebSMel Gorman 3067e716f2ebSMel Gorman mark = high_wmark_pages(zone); 3068e716f2ebSMel Gorman if (zone_watermark_ok_safe(zone, order, mark, classzone_idx)) 30696256c6b4SMel Gorman return true; 307060cefed4SJohannes Weiner } 307160cefed4SJohannes Weiner 3072e716f2ebSMel Gorman /* 3073e716f2ebSMel Gorman * If a node has no populated zone within classzone_idx, it does not 3074e716f2ebSMel Gorman * need balancing by definition. This can happen if a zone-restricted 3075e716f2ebSMel Gorman * allocation tries to wake a remote kswapd. 3076e716f2ebSMel Gorman */ 3077e716f2ebSMel Gorman if (mark == -1) 3078e716f2ebSMel Gorman return true; 3079e716f2ebSMel Gorman 3080e716f2ebSMel Gorman return false; 3081e716f2ebSMel Gorman } 3082e716f2ebSMel Gorman 3083631b6e08SMel Gorman /* Clear pgdat state for congested, dirty or under writeback. */ 3084631b6e08SMel Gorman static void clear_pgdat_congested(pg_data_t *pgdat) 3085631b6e08SMel Gorman { 3086631b6e08SMel Gorman clear_bit(PGDAT_CONGESTED, &pgdat->flags); 3087631b6e08SMel Gorman clear_bit(PGDAT_DIRTY, &pgdat->flags); 3088631b6e08SMel Gorman clear_bit(PGDAT_WRITEBACK, &pgdat->flags); 3089631b6e08SMel Gorman } 3090631b6e08SMel Gorman 30911741c877SMel Gorman /* 30925515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 30935515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 30945515061dSMel Gorman * 30955515061dSMel Gorman * Returns true if kswapd is ready to sleep 30965515061dSMel Gorman */ 3097d9f21d42SMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx) 3098f50de2d3SMel Gorman { 30995515061dSMel Gorman /* 31009e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 3101c73322d0SJohannes Weiner * soon as allow_direct_reclaim() is true. But there is a potential 31029e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 31039e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 31049e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 31059e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 31069e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 31079e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 31089e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 31099e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 31109e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 31115515061dSMel Gorman */ 31129e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 31139e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 3114f50de2d3SMel Gorman 3115c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3116c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3117c73322d0SJohannes Weiner return true; 3118c73322d0SJohannes Weiner 3119e716f2ebSMel Gorman if (pgdat_balanced(pgdat, order, classzone_idx)) { 3120631b6e08SMel Gorman clear_pgdat_congested(pgdat); 3121333b0a45SShantanu Goel return true; 31221d82de61SMel Gorman } 31231d82de61SMel Gorman 3124333b0a45SShantanu Goel return false; 3125f50de2d3SMel Gorman } 3126f50de2d3SMel Gorman 31271da177e4SLinus Torvalds /* 31281d82de61SMel Gorman * kswapd shrinks a node of pages that are at or below the highest usable 31291d82de61SMel Gorman * zone that is currently unbalanced. 3130b8e83b94SMel Gorman * 3131b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 3132283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 3133283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 313475485363SMel Gorman */ 31351d82de61SMel Gorman static bool kswapd_shrink_node(pg_data_t *pgdat, 3136accf6242SVlastimil Babka struct scan_control *sc) 313775485363SMel Gorman { 31381d82de61SMel Gorman struct zone *zone; 31391d82de61SMel Gorman int z; 314075485363SMel Gorman 31411d82de61SMel Gorman /* Reclaim a number of pages proportional to the number of zones */ 31421d82de61SMel Gorman sc->nr_to_reclaim = 0; 3143970a39a3SMel Gorman for (z = 0; z <= sc->reclaim_idx; z++) { 31441d82de61SMel Gorman zone = pgdat->node_zones + z; 31456aa303deSMel Gorman if (!managed_zone(zone)) 31461d82de61SMel Gorman continue; 31477c954f6dSMel Gorman 31481d82de61SMel Gorman sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX); 31497c954f6dSMel Gorman } 31507c954f6dSMel Gorman 31511d82de61SMel Gorman /* 31521d82de61SMel Gorman * Historically care was taken to put equal pressure on all zones but 31531d82de61SMel Gorman * now pressure is applied based on node LRU order. 31541d82de61SMel Gorman */ 3155970a39a3SMel Gorman shrink_node(pgdat, sc); 31561d82de61SMel Gorman 31571d82de61SMel Gorman /* 31581d82de61SMel Gorman * Fragmentation may mean that the system cannot be rebalanced for 31591d82de61SMel Gorman * high-order allocations. If twice the allocation size has been 31601d82de61SMel Gorman * reclaimed then recheck watermarks only at order-0 to prevent 31611d82de61SMel Gorman * excessive reclaim. Assume that a process requested a high-order 31621d82de61SMel Gorman * can direct reclaim/compact. 31631d82de61SMel Gorman */ 31649861a62cSVlastimil Babka if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order)) 31651d82de61SMel Gorman sc->order = 0; 31661d82de61SMel Gorman 3167b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 316875485363SMel Gorman } 316975485363SMel Gorman 317075485363SMel Gorman /* 31711d82de61SMel Gorman * For kswapd, balance_pgdat() will reclaim pages across a node from zones 31721d82de61SMel Gorman * that are eligible for use by the caller until at least one zone is 31731d82de61SMel Gorman * balanced. 31741da177e4SLinus Torvalds * 31751d82de61SMel Gorman * Returns the order kswapd finished reclaiming at. 31761da177e4SLinus Torvalds * 31771da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 317841858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 31791d82de61SMel Gorman * found to have free_pages <= high_wmark_pages(zone), any page is that zone 31801d82de61SMel Gorman * or lower is eligible for reclaim until at least one usable zone is 31811d82de61SMel Gorman * balanced. 31821da177e4SLinus Torvalds */ 3183accf6242SVlastimil Babka static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx) 31841da177e4SLinus Torvalds { 31851da177e4SLinus Torvalds int i; 31860608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 31870608f43dSAndrew Morton unsigned long nr_soft_scanned; 31881d82de61SMel Gorman struct zone *zone; 3189179e9639SAndrew Morton struct scan_control sc = { 3190179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 3191ee814fe2SJohannes Weiner .order = order, 3192b8e83b94SMel Gorman .priority = DEF_PRIORITY, 3193ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3194a6dc60f8SJohannes Weiner .may_unmap = 1, 31952e2e4259SKOSAKI Motohiro .may_swap = 1, 3196179e9639SAndrew Morton }; 3197f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 31981da177e4SLinus Torvalds 31999e3b2f8cSKonstantin Khlebnikov do { 3200c73322d0SJohannes Weiner unsigned long nr_reclaimed = sc.nr_reclaimed; 3201b8e83b94SMel Gorman bool raise_priority = true; 3202b8e83b94SMel Gorman 320384c7a777SMel Gorman sc.reclaim_idx = classzone_idx; 32041da177e4SLinus Torvalds 320586c79f6bSMel Gorman /* 320684c7a777SMel Gorman * If the number of buffer_heads exceeds the maximum allowed 320784c7a777SMel Gorman * then consider reclaiming from all zones. This has a dual 320884c7a777SMel Gorman * purpose -- on 64-bit systems it is expected that 320984c7a777SMel Gorman * buffer_heads are stripped during active rotation. On 32-bit 321084c7a777SMel Gorman * systems, highmem pages can pin lowmem memory and shrinking 321184c7a777SMel Gorman * buffers can relieve lowmem pressure. Reclaim may still not 321284c7a777SMel Gorman * go ahead if all eligible zones for the original allocation 321384c7a777SMel Gorman * request are balanced to avoid excessive reclaim from kswapd. 321486c79f6bSMel Gorman */ 321586c79f6bSMel Gorman if (buffer_heads_over_limit) { 321686c79f6bSMel Gorman for (i = MAX_NR_ZONES - 1; i >= 0; i--) { 321786c79f6bSMel Gorman zone = pgdat->node_zones + i; 32186aa303deSMel Gorman if (!managed_zone(zone)) 321986c79f6bSMel Gorman continue; 322086c79f6bSMel Gorman 3221970a39a3SMel Gorman sc.reclaim_idx = i; 322286c79f6bSMel Gorman break; 322386c79f6bSMel Gorman } 322486c79f6bSMel Gorman } 322586c79f6bSMel Gorman 322686c79f6bSMel Gorman /* 3227e716f2ebSMel Gorman * Only reclaim if there are no eligible zones. Note that 3228e716f2ebSMel Gorman * sc.reclaim_idx is not used as buffer_heads_over_limit may 3229e716f2ebSMel Gorman * have adjusted it. 323086c79f6bSMel Gorman */ 3231e716f2ebSMel Gorman if (pgdat_balanced(pgdat, sc.order, classzone_idx)) 32321da177e4SLinus Torvalds goto out; 3233e1dbeda6SAndrew Morton 32341da177e4SLinus Torvalds /* 32351d82de61SMel Gorman * Do some background aging of the anon list, to give 32361d82de61SMel Gorman * pages a chance to be referenced before reclaiming. All 32371d82de61SMel Gorman * pages are rotated regardless of classzone as this is 32381d82de61SMel Gorman * about consistent aging. 32391d82de61SMel Gorman */ 3240ef8f2327SMel Gorman age_active_anon(pgdat, &sc); 32411d82de61SMel Gorman 32421d82de61SMel Gorman /* 3243b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 3244b7ea3c41SMel Gorman * even in laptop mode. 3245b7ea3c41SMel Gorman */ 3246047d72c3SJohannes Weiner if (sc.priority < DEF_PRIORITY - 2) 3247b7ea3c41SMel Gorman sc.may_writepage = 1; 3248b7ea3c41SMel Gorman 32491d82de61SMel Gorman /* Call soft limit reclaim before calling shrink_node. */ 32501da177e4SLinus Torvalds sc.nr_scanned = 0; 32510608f43dSAndrew Morton nr_soft_scanned = 0; 3252ef8f2327SMel Gorman nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order, 32531d82de61SMel Gorman sc.gfp_mask, &nr_soft_scanned); 32540608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 32550608f43dSAndrew Morton 325632a4330dSRik van Riel /* 32571d82de61SMel Gorman * There should be no need to raise the scanning priority if 32581d82de61SMel Gorman * enough pages are already being scanned that that high 32591d82de61SMel Gorman * watermark would be met at 100% efficiency. 326032a4330dSRik van Riel */ 3261970a39a3SMel Gorman if (kswapd_shrink_node(pgdat, &sc)) 3262b8e83b94SMel Gorman raise_priority = false; 3263d7868daeSMel Gorman 32645515061dSMel Gorman /* 32655515061dSMel Gorman * If the low watermark is met there is no need for processes 32665515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 32675515061dSMel Gorman * able to safely make forward progress. Wake them 32685515061dSMel Gorman */ 32695515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 3270c73322d0SJohannes Weiner allow_direct_reclaim(pgdat)) 3271cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 32725515061dSMel Gorman 3273b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 3274b8e83b94SMel Gorman if (try_to_freeze() || kthread_should_stop()) 3275b8e83b94SMel Gorman break; 3276b8e83b94SMel Gorman 3277b8e83b94SMel Gorman /* 3278b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3279b8e83b94SMel Gorman * progress in reclaiming pages 3280b8e83b94SMel Gorman */ 3281c73322d0SJohannes Weiner nr_reclaimed = sc.nr_reclaimed - nr_reclaimed; 3282c73322d0SJohannes Weiner if (raise_priority || !nr_reclaimed) 3283b8e83b94SMel Gorman sc.priority--; 32841d82de61SMel Gorman } while (sc.priority >= 1); 32851da177e4SLinus Torvalds 3286c73322d0SJohannes Weiner if (!sc.nr_reclaimed) 3287c73322d0SJohannes Weiner pgdat->kswapd_failures++; 3288c73322d0SJohannes Weiner 3289b8e83b94SMel Gorman out: 32900abdee2bSMel Gorman /* 32911d82de61SMel Gorman * Return the order kswapd stopped reclaiming at as 32921d82de61SMel Gorman * prepare_kswapd_sleep() takes it into account. If another caller 32931d82de61SMel Gorman * entered the allocator slow path while kswapd was awake, order will 32941d82de61SMel Gorman * remain at the higher level. 32950abdee2bSMel Gorman */ 32961d82de61SMel Gorman return sc.order; 32971da177e4SLinus Torvalds } 32981da177e4SLinus Torvalds 3299e716f2ebSMel Gorman /* 3300e716f2ebSMel Gorman * pgdat->kswapd_classzone_idx is the highest zone index that a recent 3301e716f2ebSMel Gorman * allocation request woke kswapd for. When kswapd has not woken recently, 3302e716f2ebSMel Gorman * the value is MAX_NR_ZONES which is not a valid index. This compares a 3303e716f2ebSMel Gorman * given classzone and returns it or the highest classzone index kswapd 3304e716f2ebSMel Gorman * was recently woke for. 3305e716f2ebSMel Gorman */ 3306e716f2ebSMel Gorman static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat, 3307e716f2ebSMel Gorman enum zone_type classzone_idx) 3308e716f2ebSMel Gorman { 3309e716f2ebSMel Gorman if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES) 3310e716f2ebSMel Gorman return classzone_idx; 3311e716f2ebSMel Gorman 3312e716f2ebSMel Gorman return max(pgdat->kswapd_classzone_idx, classzone_idx); 3313e716f2ebSMel Gorman } 3314e716f2ebSMel Gorman 331538087d9bSMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order, 331638087d9bSMel Gorman unsigned int classzone_idx) 3317f0bc0a60SKOSAKI Motohiro { 3318f0bc0a60SKOSAKI Motohiro long remaining = 0; 3319f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3320f0bc0a60SKOSAKI Motohiro 3321f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3322f0bc0a60SKOSAKI Motohiro return; 3323f0bc0a60SKOSAKI Motohiro 3324f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3325f0bc0a60SKOSAKI Motohiro 3326333b0a45SShantanu Goel /* 3327333b0a45SShantanu Goel * Try to sleep for a short interval. Note that kcompactd will only be 3328333b0a45SShantanu Goel * woken if it is possible to sleep for a short interval. This is 3329333b0a45SShantanu Goel * deliberate on the assumption that if reclaim cannot keep an 3330333b0a45SShantanu Goel * eligible zone balanced that it's also unlikely that compaction will 3331333b0a45SShantanu Goel * succeed. 3332333b0a45SShantanu Goel */ 3333d9f21d42SMel Gorman if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3334fd901c95SVlastimil Babka /* 3335fd901c95SVlastimil Babka * Compaction records what page blocks it recently failed to 3336fd901c95SVlastimil Babka * isolate pages from and skips them in the future scanning. 3337fd901c95SVlastimil Babka * When kswapd is going to sleep, it is reasonable to assume 3338fd901c95SVlastimil Babka * that pages and compaction may succeed so reset the cache. 3339fd901c95SVlastimil Babka */ 3340fd901c95SVlastimil Babka reset_isolation_suitable(pgdat); 3341fd901c95SVlastimil Babka 3342fd901c95SVlastimil Babka /* 3343fd901c95SVlastimil Babka * We have freed the memory, now we should compact it to make 3344fd901c95SVlastimil Babka * allocation of the requested order possible. 3345fd901c95SVlastimil Babka */ 334638087d9bSMel Gorman wakeup_kcompactd(pgdat, alloc_order, classzone_idx); 3347fd901c95SVlastimil Babka 3348f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 334938087d9bSMel Gorman 335038087d9bSMel Gorman /* 335138087d9bSMel Gorman * If woken prematurely then reset kswapd_classzone_idx and 335238087d9bSMel Gorman * order. The values will either be from a wakeup request or 335338087d9bSMel Gorman * the previous request that slept prematurely. 335438087d9bSMel Gorman */ 335538087d9bSMel Gorman if (remaining) { 3356e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 335738087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, reclaim_order); 335838087d9bSMel Gorman } 335938087d9bSMel Gorman 3360f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3361f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3362f0bc0a60SKOSAKI Motohiro } 3363f0bc0a60SKOSAKI Motohiro 3364f0bc0a60SKOSAKI Motohiro /* 3365f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3366f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3367f0bc0a60SKOSAKI Motohiro */ 3368d9f21d42SMel Gorman if (!remaining && 3369d9f21d42SMel Gorman prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) { 3370f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3371f0bc0a60SKOSAKI Motohiro 3372f0bc0a60SKOSAKI Motohiro /* 3373f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3374f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3375f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3376f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3377f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3378f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3379f0bc0a60SKOSAKI Motohiro */ 3380f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 33811c7e7f6cSAaditya Kumar 33821c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3383f0bc0a60SKOSAKI Motohiro schedule(); 33841c7e7f6cSAaditya Kumar 3385f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3386f0bc0a60SKOSAKI Motohiro } else { 3387f0bc0a60SKOSAKI Motohiro if (remaining) 3388f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3389f0bc0a60SKOSAKI Motohiro else 3390f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3391f0bc0a60SKOSAKI Motohiro } 3392f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3393f0bc0a60SKOSAKI Motohiro } 3394f0bc0a60SKOSAKI Motohiro 33951da177e4SLinus Torvalds /* 33961da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 33971da177e4SLinus Torvalds * from the init process. 33981da177e4SLinus Torvalds * 33991da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 34001da177e4SLinus Torvalds * free memory available even if there is no other activity 34011da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 34021da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 34031da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 34041da177e4SLinus Torvalds * 34051da177e4SLinus Torvalds * If there are applications that are active memory-allocators 34061da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 34071da177e4SLinus Torvalds */ 34081da177e4SLinus Torvalds static int kswapd(void *p) 34091da177e4SLinus Torvalds { 3410e716f2ebSMel Gorman unsigned int alloc_order, reclaim_order; 3411e716f2ebSMel Gorman unsigned int classzone_idx = MAX_NR_ZONES - 1; 34121da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 34131da177e4SLinus Torvalds struct task_struct *tsk = current; 3414f0bc0a60SKOSAKI Motohiro 34151da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 34161da177e4SLinus Torvalds .reclaimed_slab = 0, 34171da177e4SLinus Torvalds }; 3418a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 34191da177e4SLinus Torvalds 3420cf40bd16SNick Piggin lockdep_set_current_reclaim_state(GFP_KERNEL); 3421cf40bd16SNick Piggin 3422174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3423c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 34241da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 34251da177e4SLinus Torvalds 34261da177e4SLinus Torvalds /* 34271da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 34281da177e4SLinus Torvalds * and that if we need more memory we should get access to it 34291da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 34301da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 34311da177e4SLinus Torvalds * 34321da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 34331da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 34341da177e4SLinus Torvalds * page out something else, and this flag essentially protects 34351da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 34361da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 34371da177e4SLinus Torvalds */ 3438930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 343983144186SRafael J. Wysocki set_freezable(); 34401da177e4SLinus Torvalds 3441e716f2ebSMel Gorman pgdat->kswapd_order = 0; 3442e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 34431da177e4SLinus Torvalds for ( ; ; ) { 34446f6313d4SJeff Liu bool ret; 34453e1d1d28SChristoph Lameter 3446e716f2ebSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3447e716f2ebSMel Gorman classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx); 3448e716f2ebSMel Gorman 344938087d9bSMel Gorman kswapd_try_sleep: 345038087d9bSMel Gorman kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order, 345138087d9bSMel Gorman classzone_idx); 3452215ddd66SMel Gorman 345338087d9bSMel Gorman /* Read the new order and classzone_idx */ 345438087d9bSMel Gorman alloc_order = reclaim_order = pgdat->kswapd_order; 3455e716f2ebSMel Gorman classzone_idx = kswapd_classzone_idx(pgdat, 0); 345638087d9bSMel Gorman pgdat->kswapd_order = 0; 3457e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = MAX_NR_ZONES; 34581da177e4SLinus Torvalds 34598fe23e05SDavid Rientjes ret = try_to_freeze(); 34608fe23e05SDavid Rientjes if (kthread_should_stop()) 34618fe23e05SDavid Rientjes break; 34628fe23e05SDavid Rientjes 34638fe23e05SDavid Rientjes /* 34648fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 34658fe23e05SDavid Rientjes * after returning from the refrigerator 3466b1296cc4SRafael J. Wysocki */ 346738087d9bSMel Gorman if (ret) 346838087d9bSMel Gorman continue; 34691d82de61SMel Gorman 347038087d9bSMel Gorman /* 347138087d9bSMel Gorman * Reclaim begins at the requested order but if a high-order 347238087d9bSMel Gorman * reclaim fails then kswapd falls back to reclaiming for 347338087d9bSMel Gorman * order-0. If that happens, kswapd will consider sleeping 347438087d9bSMel Gorman * for the order it finished reclaiming at (reclaim_order) 347538087d9bSMel Gorman * but kcompactd is woken to compact for the original 347638087d9bSMel Gorman * request (alloc_order). 347738087d9bSMel Gorman */ 3478e5146b12SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx, 3479e5146b12SMel Gorman alloc_order); 348038087d9bSMel Gorman reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx); 348138087d9bSMel Gorman if (reclaim_order < alloc_order) 348238087d9bSMel Gorman goto kswapd_try_sleep; 348333906bc5SMel Gorman } 3484b0a8cc58STakamori Yamaguchi 348571abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 3486b0a8cc58STakamori Yamaguchi current->reclaim_state = NULL; 348771abdc15SJohannes Weiner lockdep_clear_current_reclaim_state(); 348871abdc15SJohannes Weiner 34891da177e4SLinus Torvalds return 0; 34901da177e4SLinus Torvalds } 34911da177e4SLinus Torvalds 34921da177e4SLinus Torvalds /* 34931da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 34941da177e4SLinus Torvalds */ 349599504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) 34961da177e4SLinus Torvalds { 34971da177e4SLinus Torvalds pg_data_t *pgdat; 34981da177e4SLinus Torvalds 34996aa303deSMel Gorman if (!managed_zone(zone)) 35001da177e4SLinus Torvalds return; 35011da177e4SLinus Torvalds 3502344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, GFP_KERNEL | __GFP_HARDWALL)) 35031da177e4SLinus Torvalds return; 350488f5acf8SMel Gorman pgdat = zone->zone_pgdat; 3505e716f2ebSMel Gorman pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat, 3506e716f2ebSMel Gorman classzone_idx); 350738087d9bSMel Gorman pgdat->kswapd_order = max(pgdat->kswapd_order, order); 35088d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 35091da177e4SLinus Torvalds return; 3510e1a55637SMel Gorman 3511c73322d0SJohannes Weiner /* Hopeless node, leave it to direct reclaim */ 3512c73322d0SJohannes Weiner if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES) 3513c73322d0SJohannes Weiner return; 3514c73322d0SJohannes Weiner 3515e716f2ebSMel Gorman if (pgdat_balanced(pgdat, order, classzone_idx)) 351688f5acf8SMel Gorman return; 351788f5acf8SMel Gorman 3518e716f2ebSMel Gorman trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, classzone_idx, order); 35198d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 35201da177e4SLinus Torvalds } 35211da177e4SLinus Torvalds 3522c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 35231da177e4SLinus Torvalds /* 35247b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3525d6277db4SRafael J. Wysocki * freed pages. 3526d6277db4SRafael J. Wysocki * 3527d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3528d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3529d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 35301da177e4SLinus Torvalds */ 35317b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 35321da177e4SLinus Torvalds { 3533d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 3534d6277db4SRafael J. Wysocki struct scan_control sc = { 35357b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 3536ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 3537b2e18757SMel Gorman .reclaim_idx = MAX_NR_ZONES - 1, 35389e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 3539ee814fe2SJohannes Weiner .may_writepage = 1, 3540ee814fe2SJohannes Weiner .may_unmap = 1, 3541ee814fe2SJohannes Weiner .may_swap = 1, 3542ee814fe2SJohannes Weiner .hibernation_mode = 1, 35431da177e4SLinus Torvalds }; 35447b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 35457b51755cSKOSAKI Motohiro struct task_struct *p = current; 35467b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 35471da177e4SLinus Torvalds 35487b51755cSKOSAKI Motohiro p->flags |= PF_MEMALLOC; 35497b51755cSKOSAKI Motohiro lockdep_set_current_reclaim_state(sc.gfp_mask); 3550d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 35517b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 3552d6277db4SRafael J. Wysocki 35533115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3554d6277db4SRafael J. Wysocki 35557b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 35567b51755cSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 35577b51755cSKOSAKI Motohiro p->flags &= ~PF_MEMALLOC; 3558d6277db4SRafael J. Wysocki 35597b51755cSKOSAKI Motohiro return nr_reclaimed; 35601da177e4SLinus Torvalds } 3561c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 35621da177e4SLinus Torvalds 35631da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 35641da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 35651da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 35661da177e4SLinus Torvalds restore their cpu bindings. */ 3567517bbed9SSebastian Andrzej Siewior static int kswapd_cpu_online(unsigned int cpu) 35681da177e4SLinus Torvalds { 356958c0a4a7SYasunori Goto int nid; 35701da177e4SLinus Torvalds 357148fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 3572c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3573a70f7302SRusty Russell const struct cpumask *mask; 3574a70f7302SRusty Russell 3575a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3576c5f59f08SMike Travis 35773e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 35781da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3579c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 35801da177e4SLinus Torvalds } 3581517bbed9SSebastian Andrzej Siewior return 0; 35821da177e4SLinus Torvalds } 35831da177e4SLinus Torvalds 35843218ae14SYasunori Goto /* 35853218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 35863218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 35873218ae14SYasunori Goto */ 35883218ae14SYasunori Goto int kswapd_run(int nid) 35893218ae14SYasunori Goto { 35903218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 35913218ae14SYasunori Goto int ret = 0; 35923218ae14SYasunori Goto 35933218ae14SYasunori Goto if (pgdat->kswapd) 35943218ae14SYasunori Goto return 0; 35953218ae14SYasunori Goto 35963218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 35973218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 35983218ae14SYasunori Goto /* failure at boot is fatal */ 35993218ae14SYasunori Goto BUG_ON(system_state == SYSTEM_BOOTING); 3600d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 3601d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 3602d72515b8SXishi Qiu pgdat->kswapd = NULL; 36033218ae14SYasunori Goto } 36043218ae14SYasunori Goto return ret; 36053218ae14SYasunori Goto } 36063218ae14SYasunori Goto 36078fe23e05SDavid Rientjes /* 3608d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 3609bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 36108fe23e05SDavid Rientjes */ 36118fe23e05SDavid Rientjes void kswapd_stop(int nid) 36128fe23e05SDavid Rientjes { 36138fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 36148fe23e05SDavid Rientjes 3615d8adde17SJiang Liu if (kswapd) { 36168fe23e05SDavid Rientjes kthread_stop(kswapd); 3617d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 3618d8adde17SJiang Liu } 36198fe23e05SDavid Rientjes } 36208fe23e05SDavid Rientjes 36211da177e4SLinus Torvalds static int __init kswapd_init(void) 36221da177e4SLinus Torvalds { 3623517bbed9SSebastian Andrzej Siewior int nid, ret; 362469e05944SAndrew Morton 36251da177e4SLinus Torvalds swap_setup(); 362648fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 36273218ae14SYasunori Goto kswapd_run(nid); 3628517bbed9SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 3629517bbed9SSebastian Andrzej Siewior "mm/vmscan:online", kswapd_cpu_online, 3630517bbed9SSebastian Andrzej Siewior NULL); 3631517bbed9SSebastian Andrzej Siewior WARN_ON(ret < 0); 36321da177e4SLinus Torvalds return 0; 36331da177e4SLinus Torvalds } 36341da177e4SLinus Torvalds 36351da177e4SLinus Torvalds module_init(kswapd_init) 36369eeff239SChristoph Lameter 36379eeff239SChristoph Lameter #ifdef CONFIG_NUMA 36389eeff239SChristoph Lameter /* 3639a5f5f91dSMel Gorman * Node reclaim mode 36409eeff239SChristoph Lameter * 3641a5f5f91dSMel Gorman * If non-zero call node_reclaim when the number of free pages falls below 36429eeff239SChristoph Lameter * the watermarks. 36439eeff239SChristoph Lameter */ 3644a5f5f91dSMel Gorman int node_reclaim_mode __read_mostly; 36459eeff239SChristoph Lameter 36461b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 36477d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 36481b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 364995bbc0c7SZhihui Zhang #define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */ 36501b2ffb78SChristoph Lameter 36519eeff239SChristoph Lameter /* 3652a5f5f91dSMel Gorman * Priority for NODE_RECLAIM. This determines the fraction of pages 3653a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3654a92f7126SChristoph Lameter * a zone. 3655a92f7126SChristoph Lameter */ 3656a5f5f91dSMel Gorman #define NODE_RECLAIM_PRIORITY 4 3657a92f7126SChristoph Lameter 36589eeff239SChristoph Lameter /* 3659a5f5f91dSMel Gorman * Percentage of pages in a zone that must be unmapped for node_reclaim to 36609614634fSChristoph Lameter * occur. 36619614634fSChristoph Lameter */ 36629614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 36639614634fSChristoph Lameter 36649614634fSChristoph Lameter /* 36650ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 36660ff38490SChristoph Lameter * slab reclaim needs to occur. 36670ff38490SChristoph Lameter */ 36680ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 36690ff38490SChristoph Lameter 367011fb9989SMel Gorman static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat) 367190afa5deSMel Gorman { 367211fb9989SMel Gorman unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED); 367311fb9989SMel Gorman unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) + 367411fb9989SMel Gorman node_page_state(pgdat, NR_ACTIVE_FILE); 367590afa5deSMel Gorman 367690afa5deSMel Gorman /* 367790afa5deSMel Gorman * It's possible for there to be more file mapped pages than 367890afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 367990afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 368090afa5deSMel Gorman */ 368190afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 368290afa5deSMel Gorman } 368390afa5deSMel Gorman 368490afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 3685a5f5f91dSMel Gorman static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat) 368690afa5deSMel Gorman { 3687d031a157SAlexandru Moise unsigned long nr_pagecache_reclaimable; 3688d031a157SAlexandru Moise unsigned long delta = 0; 368990afa5deSMel Gorman 369090afa5deSMel Gorman /* 369195bbc0c7SZhihui Zhang * If RECLAIM_UNMAP is set, then all file pages are considered 369290afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 369311fb9989SMel Gorman * pages like swapcache and node_unmapped_file_pages() provides 369490afa5deSMel Gorman * a better estimate 369590afa5deSMel Gorman */ 3696a5f5f91dSMel Gorman if (node_reclaim_mode & RECLAIM_UNMAP) 3697a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES); 369890afa5deSMel Gorman else 3699a5f5f91dSMel Gorman nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat); 370090afa5deSMel Gorman 370190afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 3702a5f5f91dSMel Gorman if (!(node_reclaim_mode & RECLAIM_WRITE)) 3703a5f5f91dSMel Gorman delta += node_page_state(pgdat, NR_FILE_DIRTY); 370490afa5deSMel Gorman 370590afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 370690afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 370790afa5deSMel Gorman delta = nr_pagecache_reclaimable; 370890afa5deSMel Gorman 370990afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 371090afa5deSMel Gorman } 371190afa5deSMel Gorman 37120ff38490SChristoph Lameter /* 3713a5f5f91dSMel Gorman * Try to free up some pages from this node through reclaim. 37149eeff239SChristoph Lameter */ 3715a5f5f91dSMel Gorman static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 37169eeff239SChristoph Lameter { 37177fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 371869e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 37199eeff239SChristoph Lameter struct task_struct *p = current; 37209eeff239SChristoph Lameter struct reclaim_state reclaim_state; 3721a5f5f91dSMel Gorman int classzone_idx = gfp_zone(gfp_mask); 3722179e9639SAndrew Morton struct scan_control sc = { 372362b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 37247dea19f9SMichal Hocko .gfp_mask = (gfp_mask = current_gfp_context(gfp_mask)), 3725bd2f6199SJohannes Weiner .order = order, 3726a5f5f91dSMel Gorman .priority = NODE_RECLAIM_PRIORITY, 3727a5f5f91dSMel Gorman .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), 3728a5f5f91dSMel Gorman .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), 3729ee814fe2SJohannes Weiner .may_swap = 1, 3730a5f5f91dSMel Gorman .reclaim_idx = classzone_idx, 3731179e9639SAndrew Morton }; 37329eeff239SChristoph Lameter 37339eeff239SChristoph Lameter cond_resched(); 3734d4f7796eSChristoph Lameter /* 373595bbc0c7SZhihui Zhang * We need to be able to allocate from the reserves for RECLAIM_UNMAP 3736d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 373795bbc0c7SZhihui Zhang * and RECLAIM_UNMAP. 3738d4f7796eSChristoph Lameter */ 3739d4f7796eSChristoph Lameter p->flags |= PF_MEMALLOC | PF_SWAPWRITE; 374076ca542dSKOSAKI Motohiro lockdep_set_current_reclaim_state(gfp_mask); 37419eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 37429eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 3743c84db23cSChristoph Lameter 3744a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) { 3745a92f7126SChristoph Lameter /* 37460ff38490SChristoph Lameter * Free memory by calling shrink zone with increasing 37470ff38490SChristoph Lameter * priorities until we have enough memory freed. 3748a92f7126SChristoph Lameter */ 3749a92f7126SChristoph Lameter do { 3750970a39a3SMel Gorman shrink_node(pgdat, &sc); 37519e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 37520ff38490SChristoph Lameter } 3753a92f7126SChristoph Lameter 37549eeff239SChristoph Lameter p->reclaim_state = NULL; 3755d4f7796eSChristoph Lameter current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); 375676ca542dSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 3757a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 37589eeff239SChristoph Lameter } 3759179e9639SAndrew Morton 3760a5f5f91dSMel Gorman int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order) 3761179e9639SAndrew Morton { 3762d773ed6bSDavid Rientjes int ret; 3763179e9639SAndrew Morton 3764179e9639SAndrew Morton /* 3765a5f5f91dSMel Gorman * Node reclaim reclaims unmapped file backed pages and 37660ff38490SChristoph Lameter * slab pages if we are over the defined limits. 376734aa1330SChristoph Lameter * 37689614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 37699614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 3770a5f5f91dSMel Gorman * thrown out if the node is overallocated. So we do not reclaim 3771a5f5f91dSMel Gorman * if less than a specified percentage of the node is used by 37729614634fSChristoph Lameter * unmapped file backed pages. 3773179e9639SAndrew Morton */ 3774a5f5f91dSMel Gorman if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages && 3775a5f5f91dSMel Gorman sum_zone_node_page_state(pgdat->node_id, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages) 3776a5f5f91dSMel Gorman return NODE_RECLAIM_FULL; 3777179e9639SAndrew Morton 3778179e9639SAndrew Morton /* 3779d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 3780179e9639SAndrew Morton */ 3781d0164adcSMel Gorman if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC)) 3782a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 3783179e9639SAndrew Morton 3784179e9639SAndrew Morton /* 3785a5f5f91dSMel Gorman * Only run node reclaim on the local node or on nodes that do not 3786179e9639SAndrew Morton * have associated processors. This will favor the local processor 3787179e9639SAndrew Morton * over remote processors and spread off node memory allocations 3788179e9639SAndrew Morton * as wide as possible. 3789179e9639SAndrew Morton */ 3790a5f5f91dSMel Gorman if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id()) 3791a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 3792d773ed6bSDavid Rientjes 3793a5f5f91dSMel Gorman if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags)) 3794a5f5f91dSMel Gorman return NODE_RECLAIM_NOSCAN; 3795fa5e084eSMel Gorman 3796a5f5f91dSMel Gorman ret = __node_reclaim(pgdat, gfp_mask, order); 3797a5f5f91dSMel Gorman clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags); 3798d773ed6bSDavid Rientjes 379924cf7251SMel Gorman if (!ret) 380024cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 380124cf7251SMel Gorman 3802d773ed6bSDavid Rientjes return ret; 3803179e9639SAndrew Morton } 38049eeff239SChristoph Lameter #endif 3805894bc310SLee Schermerhorn 3806894bc310SLee Schermerhorn /* 3807894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 3808894bc310SLee Schermerhorn * @page: the page to test 3809894bc310SLee Schermerhorn * 3810894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 381139b5f29aSHugh Dickins * lists vs unevictable list. 3812894bc310SLee Schermerhorn * 3813894bc310SLee Schermerhorn * Reasons page might not be evictable: 3814ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 3815b291f000SNick Piggin * (2) page is part of an mlocked VMA 3816ba9ddf49SLee Schermerhorn * 3817894bc310SLee Schermerhorn */ 381839b5f29aSHugh Dickins int page_evictable(struct page *page) 3819894bc310SLee Schermerhorn { 382039b5f29aSHugh Dickins return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); 3821894bc310SLee Schermerhorn } 382289e004eaSLee Schermerhorn 382385046579SHugh Dickins #ifdef CONFIG_SHMEM 382489e004eaSLee Schermerhorn /** 382524513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 382624513264SHugh Dickins * @pages: array of pages to check 382724513264SHugh Dickins * @nr_pages: number of pages to check 382889e004eaSLee Schermerhorn * 382924513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 383085046579SHugh Dickins * 383185046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 383289e004eaSLee Schermerhorn */ 383324513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 383489e004eaSLee Schermerhorn { 3835925b7673SJohannes Weiner struct lruvec *lruvec; 3836785b99feSMel Gorman struct pglist_data *pgdat = NULL; 383724513264SHugh Dickins int pgscanned = 0; 383824513264SHugh Dickins int pgrescued = 0; 383989e004eaSLee Schermerhorn int i; 384089e004eaSLee Schermerhorn 384124513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 384224513264SHugh Dickins struct page *page = pages[i]; 3843785b99feSMel Gorman struct pglist_data *pagepgdat = page_pgdat(page); 384489e004eaSLee Schermerhorn 384524513264SHugh Dickins pgscanned++; 3846785b99feSMel Gorman if (pagepgdat != pgdat) { 3847785b99feSMel Gorman if (pgdat) 3848785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 3849785b99feSMel Gorman pgdat = pagepgdat; 3850785b99feSMel Gorman spin_lock_irq(&pgdat->lru_lock); 385189e004eaSLee Schermerhorn } 3852785b99feSMel Gorman lruvec = mem_cgroup_page_lruvec(page, pgdat); 385389e004eaSLee Schermerhorn 385424513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 385524513264SHugh Dickins continue; 385689e004eaSLee Schermerhorn 385739b5f29aSHugh Dickins if (page_evictable(page)) { 385824513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 385924513264SHugh Dickins 3860309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 386124513264SHugh Dickins ClearPageUnevictable(page); 3862fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 3863fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 386424513264SHugh Dickins pgrescued++; 386589e004eaSLee Schermerhorn } 386689e004eaSLee Schermerhorn } 386724513264SHugh Dickins 3868785b99feSMel Gorman if (pgdat) { 386924513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 387024513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 3871785b99feSMel Gorman spin_unlock_irq(&pgdat->lru_lock); 387224513264SHugh Dickins } 387385046579SHugh Dickins } 387485046579SHugh Dickins #endif /* CONFIG_SHMEM */ 3875