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> 171da177e4SLinus Torvalds #include <linux/module.h> 185a0e3ad6STejun Heo #include <linux/gfp.h> 191da177e4SLinus Torvalds #include <linux/kernel_stat.h> 201da177e4SLinus Torvalds #include <linux/swap.h> 211da177e4SLinus Torvalds #include <linux/pagemap.h> 221da177e4SLinus Torvalds #include <linux/init.h> 231da177e4SLinus Torvalds #include <linux/highmem.h> 2470ddf637SAnton Vorontsov #include <linux/vmpressure.h> 25e129b5c2SAndrew Morton #include <linux/vmstat.h> 261da177e4SLinus Torvalds #include <linux/file.h> 271da177e4SLinus Torvalds #include <linux/writeback.h> 281da177e4SLinus Torvalds #include <linux/blkdev.h> 291da177e4SLinus Torvalds #include <linux/buffer_head.h> /* for try_to_release_page(), 301da177e4SLinus Torvalds buffer_heads_over_limit */ 311da177e4SLinus Torvalds #include <linux/mm_inline.h> 321da177e4SLinus Torvalds #include <linux/backing-dev.h> 331da177e4SLinus Torvalds #include <linux/rmap.h> 341da177e4SLinus Torvalds #include <linux/topology.h> 351da177e4SLinus Torvalds #include <linux/cpu.h> 361da177e4SLinus Torvalds #include <linux/cpuset.h> 373e7d3449SMel Gorman #include <linux/compaction.h> 381da177e4SLinus Torvalds #include <linux/notifier.h> 391da177e4SLinus Torvalds #include <linux/rwsem.h> 40248a0301SRafael J. Wysocki #include <linux/delay.h> 413218ae14SYasunori Goto #include <linux/kthread.h> 427dfb7103SNigel Cunningham #include <linux/freezer.h> 4366e1707bSBalbir Singh #include <linux/memcontrol.h> 44873b4771SKeika Kobayashi #include <linux/delayacct.h> 45af936a16SLee Schermerhorn #include <linux/sysctl.h> 46929bea7cSKOSAKI Motohiro #include <linux/oom.h> 47268bb0ceSLinus Torvalds #include <linux/prefetch.h> 48b1de0d13SMitchel Humpherys #include <linux/printk.h> 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <asm/tlbflush.h> 511da177e4SLinus Torvalds #include <asm/div64.h> 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds #include <linux/swapops.h> 54117aad1eSRafael Aquini #include <linux/balloon_compaction.h> 551da177e4SLinus Torvalds 560f8053a5SNick Piggin #include "internal.h" 570f8053a5SNick Piggin 5833906bc5SMel Gorman #define CREATE_TRACE_POINTS 5933906bc5SMel Gorman #include <trace/events/vmscan.h> 6033906bc5SMel Gorman 611da177e4SLinus Torvalds struct scan_control { 6222fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6322fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 6422fba335SKOSAKI Motohiro 651da177e4SLinus Torvalds /* This context's GFP mask */ 666daa0e28SAl Viro gfp_t gfp_mask; 671da177e4SLinus Torvalds 68ee814fe2SJohannes Weiner /* Allocation order */ 695ad333ebSAndy Whitcroft int order; 7066e1707bSBalbir Singh 71ee814fe2SJohannes Weiner /* 72ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 73ee814fe2SJohannes Weiner * are scanned. 74ee814fe2SJohannes Weiner */ 75ee814fe2SJohannes Weiner nodemask_t *nodemask; 769e3b2f8cSKonstantin Khlebnikov 775f53e762SKOSAKI Motohiro /* 78f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 79f16015fbSJohannes Weiner * primary target of this reclaim invocation. 80f16015fbSJohannes Weiner */ 81f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 8266e1707bSBalbir Singh 83ee814fe2SJohannes Weiner /* Scan (total_size >> priority) pages at once */ 84ee814fe2SJohannes Weiner int priority; 85ee814fe2SJohannes Weiner 86ee814fe2SJohannes Weiner unsigned int may_writepage:1; 87ee814fe2SJohannes Weiner 88ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 89ee814fe2SJohannes Weiner unsigned int may_unmap:1; 90ee814fe2SJohannes Weiner 91ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 92ee814fe2SJohannes Weiner unsigned int may_swap:1; 93ee814fe2SJohannes Weiner 94*241994edSJohannes Weiner /* Can cgroups be reclaimed below their normal consumption range? */ 95*241994edSJohannes Weiner unsigned int may_thrash:1; 96*241994edSJohannes Weiner 97ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 98ee814fe2SJohannes Weiner 99ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 100ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 101ee814fe2SJohannes Weiner 102ee814fe2SJohannes Weiner /* Incremented by the number of inactive pages that were scanned */ 103ee814fe2SJohannes Weiner unsigned long nr_scanned; 104ee814fe2SJohannes Weiner 105ee814fe2SJohannes Weiner /* Number of pages freed so far during a call to shrink_zones() */ 106ee814fe2SJohannes Weiner unsigned long nr_reclaimed; 1071da177e4SLinus Torvalds }; 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1121da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1131da177e4SLinus Torvalds do { \ 1141da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1151da177e4SLinus Torvalds struct page *prev; \ 1161da177e4SLinus Torvalds \ 1171da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1181da177e4SLinus Torvalds prefetch(&prev->_field); \ 1191da177e4SLinus Torvalds } \ 1201da177e4SLinus Torvalds } while (0) 1211da177e4SLinus Torvalds #else 1221da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1231da177e4SLinus Torvalds #endif 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1261da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1271da177e4SLinus Torvalds do { \ 1281da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1291da177e4SLinus Torvalds struct page *prev; \ 1301da177e4SLinus Torvalds \ 1311da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1321da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1331da177e4SLinus Torvalds } \ 1341da177e4SLinus Torvalds } while (0) 1351da177e4SLinus Torvalds #else 1361da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1371da177e4SLinus Torvalds #endif 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* 1401da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1411da177e4SLinus Torvalds */ 1421da177e4SLinus Torvalds int vm_swappiness = 60; 143d0480be4SWang Sheng-Hui /* 144d0480be4SWang Sheng-Hui * The total number of pages which are beyond the high watermark within all 145d0480be4SWang Sheng-Hui * zones. 146d0480be4SWang Sheng-Hui */ 147d0480be4SWang Sheng-Hui unsigned long vm_total_pages; 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1501da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1511da177e4SLinus Torvalds 152c255a458SAndrew Morton #ifdef CONFIG_MEMCG 15389b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 15489b5fae5SJohannes Weiner { 155f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 15689b5fae5SJohannes Weiner } 15791a45470SKAMEZAWA Hiroyuki #else 15889b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 15989b5fae5SJohannes Weiner { 16089b5fae5SJohannes Weiner return true; 16189b5fae5SJohannes Weiner } 16291a45470SKAMEZAWA Hiroyuki #endif 16391a45470SKAMEZAWA Hiroyuki 164a1c3bfb2SJohannes Weiner static unsigned long zone_reclaimable_pages(struct zone *zone) 1656e543d57SLisa Du { 1666e543d57SLisa Du int nr; 1676e543d57SLisa Du 1686e543d57SLisa Du nr = zone_page_state(zone, NR_ACTIVE_FILE) + 1696e543d57SLisa Du zone_page_state(zone, NR_INACTIVE_FILE); 1706e543d57SLisa Du 1716e543d57SLisa Du if (get_nr_swap_pages() > 0) 1726e543d57SLisa Du nr += zone_page_state(zone, NR_ACTIVE_ANON) + 1736e543d57SLisa Du zone_page_state(zone, NR_INACTIVE_ANON); 1746e543d57SLisa Du 1756e543d57SLisa Du return nr; 1766e543d57SLisa Du } 1776e543d57SLisa Du 1786e543d57SLisa Du bool zone_reclaimable(struct zone *zone) 1796e543d57SLisa Du { 1800d5d823aSMel Gorman return zone_page_state(zone, NR_PAGES_SCANNED) < 1810d5d823aSMel Gorman zone_reclaimable_pages(zone) * 6; 1826e543d57SLisa Du } 1836e543d57SLisa Du 1844d7dcca2SHugh Dickins static unsigned long get_lru_size(struct lruvec *lruvec, enum lru_list lru) 185c9f299d9SKOSAKI Motohiro { 186c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1874d7dcca2SHugh Dickins return mem_cgroup_get_lru_size(lruvec, lru); 188a3d8e054SKOSAKI Motohiro 189074291feSKonstantin Khlebnikov return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru); 190c9f299d9SKOSAKI Motohiro } 191c9f299d9SKOSAKI Motohiro 1921da177e4SLinus Torvalds /* 1931d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 1941da177e4SLinus Torvalds */ 1951d3d4437SGlauber Costa int register_shrinker(struct shrinker *shrinker) 1961da177e4SLinus Torvalds { 1971d3d4437SGlauber Costa size_t size = sizeof(*shrinker->nr_deferred); 1981d3d4437SGlauber Costa 1991d3d4437SGlauber Costa /* 2001d3d4437SGlauber Costa * If we only have one possible node in the system anyway, save 2011d3d4437SGlauber Costa * ourselves the trouble and disable NUMA aware behavior. This way we 2021d3d4437SGlauber Costa * will save memory and some small loop time later. 2031d3d4437SGlauber Costa */ 2041d3d4437SGlauber Costa if (nr_node_ids == 1) 2051d3d4437SGlauber Costa shrinker->flags &= ~SHRINKER_NUMA_AWARE; 2061d3d4437SGlauber Costa 2071d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 2081d3d4437SGlauber Costa size *= nr_node_ids; 2091d3d4437SGlauber Costa 2101d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 2111d3d4437SGlauber Costa if (!shrinker->nr_deferred) 2121d3d4437SGlauber Costa return -ENOMEM; 2131d3d4437SGlauber Costa 2141da177e4SLinus Torvalds down_write(&shrinker_rwsem); 2151da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 2161da177e4SLinus Torvalds up_write(&shrinker_rwsem); 2171d3d4437SGlauber Costa return 0; 2181da177e4SLinus Torvalds } 2198e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds /* 2221da177e4SLinus Torvalds * Remove one 2231da177e4SLinus Torvalds */ 2248e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 2251da177e4SLinus Torvalds { 2261da177e4SLinus Torvalds down_write(&shrinker_rwsem); 2271da177e4SLinus Torvalds list_del(&shrinker->list); 2281da177e4SLinus Torvalds up_write(&shrinker_rwsem); 229ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 2301da177e4SLinus Torvalds } 2318e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds #define SHRINK_BATCH 128 2341d3d4437SGlauber Costa 2356b4f7799SJohannes Weiner static unsigned long shrink_slabs(struct shrink_control *shrinkctl, 2366b4f7799SJohannes Weiner struct shrinker *shrinker, 2376b4f7799SJohannes Weiner unsigned long nr_scanned, 2386b4f7799SJohannes Weiner unsigned long nr_eligible) 2391da177e4SLinus Torvalds { 24024f7c6b9SDave Chinner unsigned long freed = 0; 2411da177e4SLinus Torvalds unsigned long long delta; 242635697c6SKonstantin Khlebnikov long total_scan; 243d5bc5fd3SVladimir Davydov long freeable; 244acf92b48SDave Chinner long nr; 245acf92b48SDave Chinner long new_nr; 2461d3d4437SGlauber Costa int nid = shrinkctl->nid; 247e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 248e9299f50SDave Chinner : SHRINK_BATCH; 2491da177e4SLinus Torvalds 250d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 251d5bc5fd3SVladimir Davydov if (freeable == 0) 2521d3d4437SGlauber Costa return 0; 253635697c6SKonstantin Khlebnikov 254acf92b48SDave Chinner /* 255acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 256acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 257acf92b48SDave Chinner * don't also do this scanning work. 258acf92b48SDave Chinner */ 2591d3d4437SGlauber Costa nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 260acf92b48SDave Chinner 261acf92b48SDave Chinner total_scan = nr; 2626b4f7799SJohannes Weiner delta = (4 * nr_scanned) / shrinker->seeks; 263d5bc5fd3SVladimir Davydov delta *= freeable; 2646b4f7799SJohannes Weiner do_div(delta, nr_eligible + 1); 265acf92b48SDave Chinner total_scan += delta; 266acf92b48SDave Chinner if (total_scan < 0) { 2678612c663SPintu Kumar pr_err("shrink_slab: %pF negative objects to delete nr=%ld\n", 268a0b02131SDave Chinner shrinker->scan_objects, total_scan); 269d5bc5fd3SVladimir Davydov total_scan = freeable; 270ea164d73SAndrea Arcangeli } 271ea164d73SAndrea Arcangeli 272ea164d73SAndrea Arcangeli /* 2733567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 2743567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 2753567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 2763567b59aSDave Chinner * nr being built up so when a shrink that can do some work 2773567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 278d5bc5fd3SVladimir Davydov * freeable. This is bad for sustaining a working set in 2793567b59aSDave Chinner * memory. 2803567b59aSDave Chinner * 2813567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 2823567b59aSDave Chinner * a large delta change is calculated directly. 2833567b59aSDave Chinner */ 284d5bc5fd3SVladimir Davydov if (delta < freeable / 4) 285d5bc5fd3SVladimir Davydov total_scan = min(total_scan, freeable / 2); 2863567b59aSDave Chinner 2873567b59aSDave Chinner /* 288ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 289ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 290ea164d73SAndrea Arcangeli * freeable entries. 291ea164d73SAndrea Arcangeli */ 292d5bc5fd3SVladimir Davydov if (total_scan > freeable * 2) 293d5bc5fd3SVladimir Davydov total_scan = freeable * 2; 2941da177e4SLinus Torvalds 29524f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 2966b4f7799SJohannes Weiner nr_scanned, nr_eligible, 297d5bc5fd3SVladimir Davydov freeable, delta, total_scan); 29809576073SDave Chinner 2990b1fb40aSVladimir Davydov /* 3000b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 3010b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 3020b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 3030b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 3040b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 3050b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 3060b1fb40aSVladimir Davydov * batch_size. 3070b1fb40aSVladimir Davydov * 3080b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 3090b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 310d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 3110b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 3120b1fb40aSVladimir Davydov * possible. 3130b1fb40aSVladimir Davydov */ 3140b1fb40aSVladimir Davydov while (total_scan >= batch_size || 315d5bc5fd3SVladimir Davydov total_scan >= freeable) { 31624f7c6b9SDave Chinner unsigned long ret; 3170b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 3181da177e4SLinus Torvalds 3190b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 32024f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 32124f7c6b9SDave Chinner if (ret == SHRINK_STOP) 3221da177e4SLinus Torvalds break; 32324f7c6b9SDave Chinner freed += ret; 32424f7c6b9SDave Chinner 3250b1fb40aSVladimir Davydov count_vm_events(SLABS_SCANNED, nr_to_scan); 3260b1fb40aSVladimir Davydov total_scan -= nr_to_scan; 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds cond_resched(); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 331acf92b48SDave Chinner /* 332acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 333acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 334acf92b48SDave Chinner * scan, there is no need to do an update. 335acf92b48SDave Chinner */ 33683aeeadaSKonstantin Khlebnikov if (total_scan > 0) 33783aeeadaSKonstantin Khlebnikov new_nr = atomic_long_add_return(total_scan, 3381d3d4437SGlauber Costa &shrinker->nr_deferred[nid]); 33983aeeadaSKonstantin Khlebnikov else 3401d3d4437SGlauber Costa new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 341acf92b48SDave Chinner 342df9024a8SDave Hansen trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan); 3431d3d4437SGlauber Costa return freed; 3441d3d4437SGlauber Costa } 3451d3d4437SGlauber Costa 3466b4f7799SJohannes Weiner /** 3476b4f7799SJohannes Weiner * shrink_node_slabs - shrink slab caches of a given node 3486b4f7799SJohannes Weiner * @gfp_mask: allocation context 3496b4f7799SJohannes Weiner * @nid: node whose slab caches to target 3506b4f7799SJohannes Weiner * @nr_scanned: pressure numerator 3516b4f7799SJohannes Weiner * @nr_eligible: pressure denominator 3521d3d4437SGlauber Costa * 3536b4f7799SJohannes Weiner * Call the shrink functions to age shrinkable caches. 3541d3d4437SGlauber Costa * 3556b4f7799SJohannes Weiner * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set, 3566b4f7799SJohannes Weiner * unaware shrinkers will receive a node id of 0 instead. 3571d3d4437SGlauber Costa * 3586b4f7799SJohannes Weiner * @nr_scanned and @nr_eligible form a ratio that indicate how much of 3596b4f7799SJohannes Weiner * the available objects should be scanned. Page reclaim for example 3606b4f7799SJohannes Weiner * passes the number of pages scanned and the number of pages on the 3616b4f7799SJohannes Weiner * LRU lists that it considered on @nid, plus a bias in @nr_scanned 3626b4f7799SJohannes Weiner * when it encountered mapped pages. The ratio is further biased by 3636b4f7799SJohannes Weiner * the ->seeks setting of the shrink function, which indicates the 3646b4f7799SJohannes Weiner * cost to recreate an object relative to that of an LRU page. 3651d3d4437SGlauber Costa * 3666b4f7799SJohannes Weiner * Returns the number of reclaimed slab objects. 3671d3d4437SGlauber Costa */ 3686b4f7799SJohannes Weiner unsigned long shrink_node_slabs(gfp_t gfp_mask, int nid, 3696b4f7799SJohannes Weiner unsigned long nr_scanned, 3706b4f7799SJohannes Weiner unsigned long nr_eligible) 3711d3d4437SGlauber Costa { 3721d3d4437SGlauber Costa struct shrinker *shrinker; 3731d3d4437SGlauber Costa unsigned long freed = 0; 3741d3d4437SGlauber Costa 3756b4f7799SJohannes Weiner if (nr_scanned == 0) 3766b4f7799SJohannes Weiner nr_scanned = SWAP_CLUSTER_MAX; 3771d3d4437SGlauber Costa 3781d3d4437SGlauber Costa if (!down_read_trylock(&shrinker_rwsem)) { 3791d3d4437SGlauber Costa /* 3801d3d4437SGlauber Costa * If we would return 0, our callers would understand that we 3811d3d4437SGlauber Costa * have nothing else to shrink and give up trying. By returning 3821d3d4437SGlauber Costa * 1 we keep it going and assume we'll be able to shrink next 3831d3d4437SGlauber Costa * time. 3841d3d4437SGlauber Costa */ 3851d3d4437SGlauber Costa freed = 1; 3861d3d4437SGlauber Costa goto out; 3871d3d4437SGlauber Costa } 3881d3d4437SGlauber Costa 3891d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 3906b4f7799SJohannes Weiner struct shrink_control sc = { 3916b4f7799SJohannes Weiner .gfp_mask = gfp_mask, 3926b4f7799SJohannes Weiner .nid = nid, 3936b4f7799SJohannes Weiner }; 3946b4f7799SJohannes Weiner 3956b4f7799SJohannes Weiner if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) 3966b4f7799SJohannes Weiner sc.nid = 0; 3976b4f7799SJohannes Weiner 3986b4f7799SJohannes Weiner freed += shrink_slabs(&sc, shrinker, nr_scanned, nr_eligible); 399ec97097bSVladimir Davydov } 4001d3d4437SGlauber Costa 4011da177e4SLinus Torvalds up_read(&shrinker_rwsem); 402f06590bdSMinchan Kim out: 403f06590bdSMinchan Kim cond_resched(); 40424f7c6b9SDave Chinner return freed; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 4081da177e4SLinus Torvalds { 409ceddc3a5SJohannes Weiner /* 410ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 411ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 412ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 413ceddc3a5SJohannes Weiner */ 414edcf4748SJohannes Weiner return page_count(page) - page_has_private(page) == 2; 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 4177d3579e8SKOSAKI Motohiro static int may_write_to_queue(struct backing_dev_info *bdi, 4187d3579e8SKOSAKI Motohiro struct scan_control *sc) 4191da177e4SLinus Torvalds { 420930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 4211da177e4SLinus Torvalds return 1; 4221da177e4SLinus Torvalds if (!bdi_write_congested(bdi)) 4231da177e4SLinus Torvalds return 1; 4241da177e4SLinus Torvalds if (bdi == current->backing_dev_info) 4251da177e4SLinus Torvalds return 1; 4261da177e4SLinus Torvalds return 0; 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds /* 4301da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 4311da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 4321da177e4SLinus Torvalds * fsync(), msync() or close(). 4331da177e4SLinus Torvalds * 4341da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 4351da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 4361da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 4371da177e4SLinus Torvalds * 4381da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 4391da177e4SLinus Torvalds * __GFP_FS. 4401da177e4SLinus Torvalds */ 4411da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 4421da177e4SLinus Torvalds struct page *page, int error) 4431da177e4SLinus Torvalds { 4447eaceaccSJens Axboe lock_page(page); 4453e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 4463e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 4471da177e4SLinus Torvalds unlock_page(page); 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds 45004e62a29SChristoph Lameter /* possible outcome of pageout() */ 45104e62a29SChristoph Lameter typedef enum { 45204e62a29SChristoph Lameter /* failed to write page out, page is locked */ 45304e62a29SChristoph Lameter PAGE_KEEP, 45404e62a29SChristoph Lameter /* move page to the active list, page is locked */ 45504e62a29SChristoph Lameter PAGE_ACTIVATE, 45604e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 45704e62a29SChristoph Lameter PAGE_SUCCESS, 45804e62a29SChristoph Lameter /* page is clean and locked */ 45904e62a29SChristoph Lameter PAGE_CLEAN, 46004e62a29SChristoph Lameter } pageout_t; 46104e62a29SChristoph Lameter 4621da177e4SLinus Torvalds /* 4631742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 4641742f19fSAndrew Morton * Calls ->writepage(). 4651da177e4SLinus Torvalds */ 466c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 4677d3579e8SKOSAKI Motohiro struct scan_control *sc) 4681da177e4SLinus Torvalds { 4691da177e4SLinus Torvalds /* 4701da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 4711da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 4721da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 4731da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 4741da177e4SLinus Torvalds * PagePrivate for that. 4751da177e4SLinus Torvalds * 4768174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 4771da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 4781da177e4SLinus Torvalds * will block. 4791da177e4SLinus Torvalds * 4801da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 4811da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 4821da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 4831da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 4841da177e4SLinus Torvalds */ 4851da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 4861da177e4SLinus Torvalds return PAGE_KEEP; 4871da177e4SLinus Torvalds if (!mapping) { 4881da177e4SLinus Torvalds /* 4891da177e4SLinus Torvalds * Some data journaling orphaned pages can have 4901da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 4911da177e4SLinus Torvalds */ 492266cf658SDavid Howells if (page_has_private(page)) { 4931da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 4941da177e4SLinus Torvalds ClearPageDirty(page); 495b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 4961da177e4SLinus Torvalds return PAGE_CLEAN; 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds return PAGE_KEEP; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 5021da177e4SLinus Torvalds return PAGE_ACTIVATE; 5030e093d99SMel Gorman if (!may_write_to_queue(mapping->backing_dev_info, sc)) 5041da177e4SLinus Torvalds return PAGE_KEEP; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 5071da177e4SLinus Torvalds int res; 5081da177e4SLinus Torvalds struct writeback_control wbc = { 5091da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 5101da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 511111ebb6eSOGAWA Hirofumi .range_start = 0, 512111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 5131da177e4SLinus Torvalds .for_reclaim = 1, 5141da177e4SLinus Torvalds }; 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds SetPageReclaim(page); 5171da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 5181da177e4SLinus Torvalds if (res < 0) 5191da177e4SLinus Torvalds handle_write_error(mapping, page, res); 520994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 5211da177e4SLinus Torvalds ClearPageReclaim(page); 5221da177e4SLinus Torvalds return PAGE_ACTIVATE; 5231da177e4SLinus Torvalds } 524c661b078SAndy Whitcroft 5251da177e4SLinus Torvalds if (!PageWriteback(page)) { 5261da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 5271da177e4SLinus Torvalds ClearPageReclaim(page); 5281da177e4SLinus Torvalds } 52923b9da55SMel Gorman trace_mm_vmscan_writepage(page, trace_reclaim_flags(page)); 530e129b5c2SAndrew Morton inc_zone_page_state(page, NR_VMSCAN_WRITE); 5311da177e4SLinus Torvalds return PAGE_SUCCESS; 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds return PAGE_CLEAN; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 537a649fd92SAndrew Morton /* 538e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 539e286781dSNick Piggin * gets returned with a refcount of 0. 540a649fd92SAndrew Morton */ 541a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 542a528910eSJohannes Weiner bool reclaimed) 54349d2e9ccSChristoph Lameter { 54428e4d965SNick Piggin BUG_ON(!PageLocked(page)); 54528e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 54649d2e9ccSChristoph Lameter 54719fd6231SNick Piggin spin_lock_irq(&mapping->tree_lock); 54849d2e9ccSChristoph Lameter /* 5490fd0e6b0SNick Piggin * The non racy check for a busy page. 5500fd0e6b0SNick Piggin * 5510fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 5520fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 5530fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 5540fd0e6b0SNick Piggin * here, then the following race may occur: 5550fd0e6b0SNick Piggin * 5560fd0e6b0SNick Piggin * get_user_pages(&page); 5570fd0e6b0SNick Piggin * [user mapping goes away] 5580fd0e6b0SNick Piggin * write_to(page); 5590fd0e6b0SNick Piggin * !PageDirty(page) [good] 5600fd0e6b0SNick Piggin * SetPageDirty(page); 5610fd0e6b0SNick Piggin * put_page(page); 5620fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 5630fd0e6b0SNick Piggin * 5640fd0e6b0SNick Piggin * [oops, our write_to data is lost] 5650fd0e6b0SNick Piggin * 5660fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 5670fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 5680fd0e6b0SNick Piggin * load is not satisfied before that of page->_count. 5690fd0e6b0SNick Piggin * 5700fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 5710fd0e6b0SNick Piggin * and thus under tree_lock, then this ordering is not required. 57249d2e9ccSChristoph Lameter */ 573e286781dSNick Piggin if (!page_freeze_refs(page, 2)) 57449d2e9ccSChristoph Lameter goto cannot_free; 575e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 576e286781dSNick Piggin if (unlikely(PageDirty(page))) { 577e286781dSNick Piggin page_unfreeze_refs(page, 2); 57849d2e9ccSChristoph Lameter goto cannot_free; 579e286781dSNick Piggin } 58049d2e9ccSChristoph Lameter 58149d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 58249d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 5830a31bc97SJohannes Weiner mem_cgroup_swapout(page, swap); 58449d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 58519fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 5860a31bc97SJohannes Weiner swapcache_free(swap); 587e286781dSNick Piggin } else { 5886072d13cSLinus Torvalds void (*freepage)(struct page *); 589a528910eSJohannes Weiner void *shadow = NULL; 5906072d13cSLinus Torvalds 5916072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 592a528910eSJohannes Weiner /* 593a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 594a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 595a528910eSJohannes Weiner * 596a528910eSJohannes Weiner * But don't store shadows in an address space that is 597a528910eSJohannes Weiner * already exiting. This is not just an optizimation, 598a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 599a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 600a528910eSJohannes Weiner * back. 601a528910eSJohannes Weiner */ 602a528910eSJohannes Weiner if (reclaimed && page_is_file_cache(page) && 603a528910eSJohannes Weiner !mapping_exiting(mapping)) 604a528910eSJohannes Weiner shadow = workingset_eviction(mapping, page); 605a528910eSJohannes Weiner __delete_from_page_cache(page, shadow); 60619fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 6076072d13cSLinus Torvalds 6086072d13cSLinus Torvalds if (freepage != NULL) 6096072d13cSLinus Torvalds freepage(page); 610e286781dSNick Piggin } 611e286781dSNick Piggin 61249d2e9ccSChristoph Lameter return 1; 61349d2e9ccSChristoph Lameter 61449d2e9ccSChristoph Lameter cannot_free: 61519fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 61649d2e9ccSChristoph Lameter return 0; 61749d2e9ccSChristoph Lameter } 61849d2e9ccSChristoph Lameter 6191da177e4SLinus Torvalds /* 620e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 621e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 622e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 623e286781dSNick Piggin * this page. 624e286781dSNick Piggin */ 625e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 626e286781dSNick Piggin { 627a528910eSJohannes Weiner if (__remove_mapping(mapping, page, false)) { 628e286781dSNick Piggin /* 629e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 630e286781dSNick Piggin * drops the pagecache ref for us without requiring another 631e286781dSNick Piggin * atomic operation. 632e286781dSNick Piggin */ 633e286781dSNick Piggin page_unfreeze_refs(page, 1); 634e286781dSNick Piggin return 1; 635e286781dSNick Piggin } 636e286781dSNick Piggin return 0; 637e286781dSNick Piggin } 638e286781dSNick Piggin 639894bc310SLee Schermerhorn /** 640894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 641894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 642894bc310SLee Schermerhorn * 643894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 644894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 645894bc310SLee Schermerhorn * 646894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 647894bc310SLee Schermerhorn */ 648894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 649894bc310SLee Schermerhorn { 6500ec3b74cSVlastimil Babka bool is_unevictable; 651bbfd28eeSLee Schermerhorn int was_unevictable = PageUnevictable(page); 652894bc310SLee Schermerhorn 653309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 654894bc310SLee Schermerhorn 655894bc310SLee Schermerhorn redo: 656894bc310SLee Schermerhorn ClearPageUnevictable(page); 657894bc310SLee Schermerhorn 65839b5f29aSHugh Dickins if (page_evictable(page)) { 659894bc310SLee Schermerhorn /* 660894bc310SLee Schermerhorn * For evictable pages, we can use the cache. 661894bc310SLee Schermerhorn * In event of a race, worst case is we end up with an 662894bc310SLee Schermerhorn * unevictable page on [in]active list. 663894bc310SLee Schermerhorn * We know how to handle that. 664894bc310SLee Schermerhorn */ 6650ec3b74cSVlastimil Babka is_unevictable = false; 666c53954a0SMel Gorman lru_cache_add(page); 667894bc310SLee Schermerhorn } else { 668894bc310SLee Schermerhorn /* 669894bc310SLee Schermerhorn * Put unevictable pages directly on zone's unevictable 670894bc310SLee Schermerhorn * list. 671894bc310SLee Schermerhorn */ 6720ec3b74cSVlastimil Babka is_unevictable = true; 673894bc310SLee Schermerhorn add_page_to_unevictable_list(page); 6746a7b9548SJohannes Weiner /* 67521ee9f39SMinchan Kim * When racing with an mlock or AS_UNEVICTABLE clearing 67621ee9f39SMinchan Kim * (page is unlocked) make sure that if the other thread 67721ee9f39SMinchan Kim * does not observe our setting of PG_lru and fails 67824513264SHugh Dickins * isolation/check_move_unevictable_pages, 67921ee9f39SMinchan Kim * we see PG_mlocked/AS_UNEVICTABLE cleared below and move 6806a7b9548SJohannes Weiner * the page back to the evictable list. 6816a7b9548SJohannes Weiner * 68221ee9f39SMinchan Kim * The other side is TestClearPageMlocked() or shmem_lock(). 6836a7b9548SJohannes Weiner */ 6846a7b9548SJohannes Weiner smp_mb(); 685894bc310SLee Schermerhorn } 686894bc310SLee Schermerhorn 687894bc310SLee Schermerhorn /* 688894bc310SLee Schermerhorn * page's status can change while we move it among lru. If an evictable 689894bc310SLee Schermerhorn * page is on unevictable list, it never be freed. To avoid that, 690894bc310SLee Schermerhorn * check after we added it to the list, again. 691894bc310SLee Schermerhorn */ 6920ec3b74cSVlastimil Babka if (is_unevictable && page_evictable(page)) { 693894bc310SLee Schermerhorn if (!isolate_lru_page(page)) { 694894bc310SLee Schermerhorn put_page(page); 695894bc310SLee Schermerhorn goto redo; 696894bc310SLee Schermerhorn } 697894bc310SLee Schermerhorn /* This means someone else dropped this page from LRU 698894bc310SLee Schermerhorn * So, it will be freed or putback to LRU again. There is 699894bc310SLee Schermerhorn * nothing to do here. 700894bc310SLee Schermerhorn */ 701894bc310SLee Schermerhorn } 702894bc310SLee Schermerhorn 7030ec3b74cSVlastimil Babka if (was_unevictable && !is_unevictable) 704bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGRESCUED); 7050ec3b74cSVlastimil Babka else if (!was_unevictable && is_unevictable) 706bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGCULLED); 707bbfd28eeSLee Schermerhorn 708894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 709894bc310SLee Schermerhorn } 710894bc310SLee Schermerhorn 711dfc8d636SJohannes Weiner enum page_references { 712dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 713dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 71464574746SJohannes Weiner PAGEREF_KEEP, 715dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 716dfc8d636SJohannes Weiner }; 717dfc8d636SJohannes Weiner 718dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 719dfc8d636SJohannes Weiner struct scan_control *sc) 720dfc8d636SJohannes Weiner { 72164574746SJohannes Weiner int referenced_ptes, referenced_page; 722dfc8d636SJohannes Weiner unsigned long vm_flags; 723dfc8d636SJohannes Weiner 724c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 725c3ac9a8aSJohannes Weiner &vm_flags); 72664574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 727dfc8d636SJohannes Weiner 728dfc8d636SJohannes Weiner /* 729dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 730dfc8d636SJohannes Weiner * move the page to the unevictable list. 731dfc8d636SJohannes Weiner */ 732dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 733dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 734dfc8d636SJohannes Weiner 73564574746SJohannes Weiner if (referenced_ptes) { 736e4898273SMichal Hocko if (PageSwapBacked(page)) 73764574746SJohannes Weiner return PAGEREF_ACTIVATE; 73864574746SJohannes Weiner /* 73964574746SJohannes Weiner * All mapped pages start out with page table 74064574746SJohannes Weiner * references from the instantiating fault, so we need 74164574746SJohannes Weiner * to look twice if a mapped file page is used more 74264574746SJohannes Weiner * than once. 74364574746SJohannes Weiner * 74464574746SJohannes Weiner * Mark it and spare it for another trip around the 74564574746SJohannes Weiner * inactive list. Another page table reference will 74664574746SJohannes Weiner * lead to its activation. 74764574746SJohannes Weiner * 74864574746SJohannes Weiner * Note: the mark is set for activated pages as well 74964574746SJohannes Weiner * so that recently deactivated but used pages are 75064574746SJohannes Weiner * quickly recovered. 75164574746SJohannes Weiner */ 75264574746SJohannes Weiner SetPageReferenced(page); 75364574746SJohannes Weiner 75434dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 755dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 756dfc8d636SJohannes Weiner 757c909e993SKonstantin Khlebnikov /* 758c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 759c909e993SKonstantin Khlebnikov */ 760c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 761c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 762c909e993SKonstantin Khlebnikov 76364574746SJohannes Weiner return PAGEREF_KEEP; 76464574746SJohannes Weiner } 76564574746SJohannes Weiner 766dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 7672e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 768dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 76964574746SJohannes Weiner 77064574746SJohannes Weiner return PAGEREF_RECLAIM; 771dfc8d636SJohannes Weiner } 772dfc8d636SJohannes Weiner 773e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 774e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 775e2be15f6SMel Gorman bool *dirty, bool *writeback) 776e2be15f6SMel Gorman { 777b4597226SMel Gorman struct address_space *mapping; 778b4597226SMel Gorman 779e2be15f6SMel Gorman /* 780e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 781e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 782e2be15f6SMel Gorman */ 783e2be15f6SMel Gorman if (!page_is_file_cache(page)) { 784e2be15f6SMel Gorman *dirty = false; 785e2be15f6SMel Gorman *writeback = false; 786e2be15f6SMel Gorman return; 787e2be15f6SMel Gorman } 788e2be15f6SMel Gorman 789e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 790e2be15f6SMel Gorman *dirty = PageDirty(page); 791e2be15f6SMel Gorman *writeback = PageWriteback(page); 792b4597226SMel Gorman 793b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 794b4597226SMel Gorman if (!page_has_private(page)) 795b4597226SMel Gorman return; 796b4597226SMel Gorman 797b4597226SMel Gorman mapping = page_mapping(page); 798b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 799b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 800e2be15f6SMel Gorman } 801e2be15f6SMel Gorman 802e286781dSNick Piggin /* 8031742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 8041da177e4SLinus Torvalds */ 8051742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 8066a18adb3SKonstantin Khlebnikov struct zone *zone, 807f84f6e2bSMel Gorman struct scan_control *sc, 80802c6de8dSMinchan Kim enum ttu_flags ttu_flags, 8098e950282SMel Gorman unsigned long *ret_nr_dirty, 810d43006d5SMel Gorman unsigned long *ret_nr_unqueued_dirty, 8118e950282SMel Gorman unsigned long *ret_nr_congested, 81202c6de8dSMinchan Kim unsigned long *ret_nr_writeback, 813b1a6f21eSMel Gorman unsigned long *ret_nr_immediate, 81402c6de8dSMinchan Kim bool force_reclaim) 8151da177e4SLinus Torvalds { 8161da177e4SLinus Torvalds LIST_HEAD(ret_pages); 817abe4c3b5SMel Gorman LIST_HEAD(free_pages); 8181da177e4SLinus Torvalds int pgactivate = 0; 819d43006d5SMel Gorman unsigned long nr_unqueued_dirty = 0; 8200e093d99SMel Gorman unsigned long nr_dirty = 0; 8210e093d99SMel Gorman unsigned long nr_congested = 0; 82205ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 82392df3a72SMel Gorman unsigned long nr_writeback = 0; 824b1a6f21eSMel Gorman unsigned long nr_immediate = 0; 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds cond_resched(); 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds while (!list_empty(page_list)) { 8291da177e4SLinus Torvalds struct address_space *mapping; 8301da177e4SLinus Torvalds struct page *page; 8311da177e4SLinus Torvalds int may_enter_fs; 83202c6de8dSMinchan Kim enum page_references references = PAGEREF_RECLAIM_CLEAN; 833e2be15f6SMel Gorman bool dirty, writeback; 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds cond_resched(); 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds page = lru_to_page(page_list); 8381da177e4SLinus Torvalds list_del(&page->lru); 8391da177e4SLinus Torvalds 840529ae9aaSNick Piggin if (!trylock_page(page)) 8411da177e4SLinus Torvalds goto keep; 8421da177e4SLinus Torvalds 843309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 844309381feSSasha Levin VM_BUG_ON_PAGE(page_zone(page) != zone, page); 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds sc->nr_scanned++; 84780e43426SChristoph Lameter 84839b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 849b291f000SNick Piggin goto cull_mlocked; 850894bc310SLee Schermerhorn 851a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 85280e43426SChristoph Lameter goto keep_locked; 85380e43426SChristoph Lameter 8541da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 8551da177e4SLinus Torvalds if (page_mapped(page) || PageSwapCache(page)) 8561da177e4SLinus Torvalds sc->nr_scanned++; 8571da177e4SLinus Torvalds 858c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 859c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 860c661b078SAndy Whitcroft 861e62e384eSMichal Hocko /* 862e2be15f6SMel Gorman * The number of dirty pages determines if a zone is marked 863e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 864e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 865e2be15f6SMel Gorman * is all dirty unqueued pages. 866e2be15f6SMel Gorman */ 867e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 868e2be15f6SMel Gorman if (dirty || writeback) 869e2be15f6SMel Gorman nr_dirty++; 870e2be15f6SMel Gorman 871e2be15f6SMel Gorman if (dirty && !writeback) 872e2be15f6SMel Gorman nr_unqueued_dirty++; 873e2be15f6SMel Gorman 874d04e8acdSMel Gorman /* 875d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 876d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 877d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 878d04e8acdSMel Gorman * end of the LRU a second time. 879d04e8acdSMel Gorman */ 880e2be15f6SMel Gorman mapping = page_mapping(page); 8811da58ee2SJamie Liu if (((dirty || writeback) && mapping && 8821da58ee2SJamie Liu bdi_write_congested(mapping->backing_dev_info)) || 883d04e8acdSMel Gorman (writeback && PageReclaim(page))) 884e2be15f6SMel Gorman nr_congested++; 885e2be15f6SMel Gorman 886e2be15f6SMel Gorman /* 887283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 888283aba9fSMel Gorman * are three cases to consider. 889e62e384eSMichal Hocko * 890283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 891283aba9fSMel Gorman * under writeback and this page is both under writeback and 892283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 893283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 894283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 895283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 896283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 897b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 898b1a6f21eSMel Gorman * caller can stall after page list has been processed. 899c3b94f44SHugh Dickins * 900283aba9fSMel Gorman * 2) Global reclaim encounters a page, memcg encounters a 901283aba9fSMel Gorman * page that is not marked for immediate reclaim or 902283aba9fSMel Gorman * the caller does not have __GFP_IO. In this case mark 903283aba9fSMel Gorman * the page for immediate reclaim and continue scanning. 904283aba9fSMel Gorman * 905283aba9fSMel Gorman * __GFP_IO is checked because a loop driver thread might 906283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 907283aba9fSMel Gorman * which it is needed to do the write (loop masks off 908283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 909283aba9fSMel Gorman * would probably show more reasons. 910283aba9fSMel Gorman * 911283aba9fSMel Gorman * Don't require __GFP_FS, since we're not going into the 912283aba9fSMel Gorman * FS, just waiting on its writeback completion. Worryingly, 913283aba9fSMel Gorman * ext4 gfs2 and xfs allocate pages with 914283aba9fSMel Gorman * grab_cache_page_write_begin(,,AOP_FLAG_NOFS), so testing 915283aba9fSMel Gorman * may_enter_fs here is liable to OOM on them. 916283aba9fSMel Gorman * 917283aba9fSMel Gorman * 3) memcg encounters a page that is not already marked 918283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 919283aba9fSMel Gorman * throttling so we could easily OOM just because too many 920283aba9fSMel Gorman * pages are in writeback and there is nothing else to 921283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 922e62e384eSMichal Hocko */ 923283aba9fSMel Gorman if (PageWriteback(page)) { 924283aba9fSMel Gorman /* Case 1 above */ 925283aba9fSMel Gorman if (current_is_kswapd() && 926283aba9fSMel Gorman PageReclaim(page) && 92757054651SJohannes Weiner test_bit(ZONE_WRITEBACK, &zone->flags)) { 928b1a6f21eSMel Gorman nr_immediate++; 929b1a6f21eSMel Gorman goto keep_locked; 930283aba9fSMel Gorman 931283aba9fSMel Gorman /* Case 2 above */ 932283aba9fSMel Gorman } else if (global_reclaim(sc) || 933c3b94f44SHugh Dickins !PageReclaim(page) || !(sc->gfp_mask & __GFP_IO)) { 934c3b94f44SHugh Dickins /* 935c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 936c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 937c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 938c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 939c3b94f44SHugh Dickins * enough to care. What we do want is for this 940c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 941c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 942c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 943c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 944c3b94f44SHugh Dickins */ 945c3b94f44SHugh Dickins SetPageReclaim(page); 94692df3a72SMel Gorman nr_writeback++; 947283aba9fSMel Gorman 948c3b94f44SHugh Dickins goto keep_locked; 949283aba9fSMel Gorman 950283aba9fSMel Gorman /* Case 3 above */ 951283aba9fSMel Gorman } else { 952c3b94f44SHugh Dickins wait_on_page_writeback(page); 953e62e384eSMichal Hocko } 954283aba9fSMel Gorman } 9551da177e4SLinus Torvalds 95602c6de8dSMinchan Kim if (!force_reclaim) 9576a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 95802c6de8dSMinchan Kim 959dfc8d636SJohannes Weiner switch (references) { 960dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 9611da177e4SLinus Torvalds goto activate_locked; 96264574746SJohannes Weiner case PAGEREF_KEEP: 96364574746SJohannes Weiner goto keep_locked; 964dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 965dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 966dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 967dfc8d636SJohannes Weiner } 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds /* 9701da177e4SLinus Torvalds * Anonymous process memory has backing store? 9711da177e4SLinus Torvalds * Try to allocate it some swap space here. 9721da177e4SLinus Torvalds */ 973b291f000SNick Piggin if (PageAnon(page) && !PageSwapCache(page)) { 97463eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 97563eb6b93SHugh Dickins goto keep_locked; 9765bc7b8acSShaohua Li if (!add_to_swap(page, page_list)) 9771da177e4SLinus Torvalds goto activate_locked; 97863eb6b93SHugh Dickins may_enter_fs = 1; 9791da177e4SLinus Torvalds 980e2be15f6SMel Gorman /* Adding to swap updated mapping */ 9811da177e4SLinus Torvalds mapping = page_mapping(page); 982e2be15f6SMel Gorman } 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds /* 9851da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 9861da177e4SLinus Torvalds * processes. Try to unmap it here. 9871da177e4SLinus Torvalds */ 9881da177e4SLinus Torvalds if (page_mapped(page) && mapping) { 98902c6de8dSMinchan Kim switch (try_to_unmap(page, ttu_flags)) { 9901da177e4SLinus Torvalds case SWAP_FAIL: 9911da177e4SLinus Torvalds goto activate_locked; 9921da177e4SLinus Torvalds case SWAP_AGAIN: 9931da177e4SLinus Torvalds goto keep_locked; 994b291f000SNick Piggin case SWAP_MLOCK: 995b291f000SNick Piggin goto cull_mlocked; 9961da177e4SLinus Torvalds case SWAP_SUCCESS: 9971da177e4SLinus Torvalds ; /* try to free the page below */ 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds if (PageDirty(page)) { 1002ee72886dSMel Gorman /* 1003ee72886dSMel Gorman * Only kswapd can writeback filesystem pages to 1004d43006d5SMel Gorman * avoid risk of stack overflow but only writeback 1005d43006d5SMel Gorman * if many dirty pages have been encountered. 1006ee72886dSMel Gorman */ 1007f84f6e2bSMel Gorman if (page_is_file_cache(page) && 10089e3b2f8cSKonstantin Khlebnikov (!current_is_kswapd() || 100957054651SJohannes Weiner !test_bit(ZONE_DIRTY, &zone->flags))) { 101049ea7eb6SMel Gorman /* 101149ea7eb6SMel Gorman * Immediately reclaim when written back. 101249ea7eb6SMel Gorman * Similar in principal to deactivate_page() 101349ea7eb6SMel Gorman * except we already have the page isolated 101449ea7eb6SMel Gorman * and know it's dirty 101549ea7eb6SMel Gorman */ 101649ea7eb6SMel Gorman inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE); 101749ea7eb6SMel Gorman SetPageReclaim(page); 101849ea7eb6SMel Gorman 1019ee72886dSMel Gorman goto keep_locked; 1020ee72886dSMel Gorman } 1021ee72886dSMel Gorman 1022dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 10231da177e4SLinus Torvalds goto keep_locked; 10244dd4b920SAndrew Morton if (!may_enter_fs) 10251da177e4SLinus Torvalds goto keep_locked; 102652a8363eSChristoph Lameter if (!sc->may_writepage) 10271da177e4SLinus Torvalds goto keep_locked; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds /* Page is dirty, try to write it out here */ 10307d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 10311da177e4SLinus Torvalds case PAGE_KEEP: 10321da177e4SLinus Torvalds goto keep_locked; 10331da177e4SLinus Torvalds case PAGE_ACTIVATE: 10341da177e4SLinus Torvalds goto activate_locked; 10351da177e4SLinus Torvalds case PAGE_SUCCESS: 10367d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 103741ac1999SMel Gorman goto keep; 10387d3579e8SKOSAKI Motohiro if (PageDirty(page)) 10391da177e4SLinus Torvalds goto keep; 10407d3579e8SKOSAKI Motohiro 10411da177e4SLinus Torvalds /* 10421da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 10431da177e4SLinus Torvalds * ahead and try to reclaim the page. 10441da177e4SLinus Torvalds */ 1045529ae9aaSNick Piggin if (!trylock_page(page)) 10461da177e4SLinus Torvalds goto keep; 10471da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 10481da177e4SLinus Torvalds goto keep_locked; 10491da177e4SLinus Torvalds mapping = page_mapping(page); 10501da177e4SLinus Torvalds case PAGE_CLEAN: 10511da177e4SLinus Torvalds ; /* try to free the page below */ 10521da177e4SLinus Torvalds } 10531da177e4SLinus Torvalds } 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds /* 10561da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 10571da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 10581da177e4SLinus Torvalds * the page as well. 10591da177e4SLinus Torvalds * 10601da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 10611da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 10621da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 10631da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 10641da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 10651da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 10661da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 10671da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 10681da177e4SLinus Torvalds * 10691da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 10701da177e4SLinus Torvalds * the pages which were not successfully invalidated in 10711da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 10721da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 10731da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 10741da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 10751da177e4SLinus Torvalds */ 1076266cf658SDavid Howells if (page_has_private(page)) { 10771da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 10781da177e4SLinus Torvalds goto activate_locked; 1079e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1080e286781dSNick Piggin unlock_page(page); 1081e286781dSNick Piggin if (put_page_testzero(page)) 10821da177e4SLinus Torvalds goto free_it; 1083e286781dSNick Piggin else { 1084e286781dSNick Piggin /* 1085e286781dSNick Piggin * rare race with speculative reference. 1086e286781dSNick Piggin * the speculative reference will free 1087e286781dSNick Piggin * this page shortly, so we may 1088e286781dSNick Piggin * increment nr_reclaimed here (and 1089e286781dSNick Piggin * leave it off the LRU). 1090e286781dSNick Piggin */ 1091e286781dSNick Piggin nr_reclaimed++; 1092e286781dSNick Piggin continue; 1093e286781dSNick Piggin } 1094e286781dSNick Piggin } 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 1097a528910eSJohannes Weiner if (!mapping || !__remove_mapping(mapping, page, true)) 109849d2e9ccSChristoph Lameter goto keep_locked; 10991da177e4SLinus Torvalds 1100a978d6f5SNick Piggin /* 1101a978d6f5SNick Piggin * At this point, we have no other references and there is 1102a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 1103a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 1104a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 1105a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 1106a978d6f5SNick Piggin */ 1107a978d6f5SNick Piggin __clear_page_locked(page); 1108e286781dSNick Piggin free_it: 110905ff5137SAndrew Morton nr_reclaimed++; 1110abe4c3b5SMel Gorman 1111abe4c3b5SMel Gorman /* 1112abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1113abe4c3b5SMel Gorman * appear not as the counts should be low 1114abe4c3b5SMel Gorman */ 1115abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 11161da177e4SLinus Torvalds continue; 11171da177e4SLinus Torvalds 1118b291f000SNick Piggin cull_mlocked: 111963d6c5adSHugh Dickins if (PageSwapCache(page)) 112063d6c5adSHugh Dickins try_to_free_swap(page); 1121b291f000SNick Piggin unlock_page(page); 1122b291f000SNick Piggin putback_lru_page(page); 1123b291f000SNick Piggin continue; 1124b291f000SNick Piggin 11251da177e4SLinus Torvalds activate_locked: 112668a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 112768a22394SRik van Riel if (PageSwapCache(page) && vm_swap_full()) 1128a2c43eedSHugh Dickins try_to_free_swap(page); 1129309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 11301da177e4SLinus Torvalds SetPageActive(page); 11311da177e4SLinus Torvalds pgactivate++; 11321da177e4SLinus Torvalds keep_locked: 11331da177e4SLinus Torvalds unlock_page(page); 11341da177e4SLinus Torvalds keep: 11351da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1136309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 11371da177e4SLinus Torvalds } 1138abe4c3b5SMel Gorman 1139747db954SJohannes Weiner mem_cgroup_uncharge_list(&free_pages); 1140b745bc85SMel Gorman free_hot_cold_page_list(&free_pages, true); 1141abe4c3b5SMel Gorman 11421da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1143f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 11440a31bc97SJohannes Weiner 11458e950282SMel Gorman *ret_nr_dirty += nr_dirty; 11468e950282SMel Gorman *ret_nr_congested += nr_congested; 1147d43006d5SMel Gorman *ret_nr_unqueued_dirty += nr_unqueued_dirty; 114892df3a72SMel Gorman *ret_nr_writeback += nr_writeback; 1149b1a6f21eSMel Gorman *ret_nr_immediate += nr_immediate; 115005ff5137SAndrew Morton return nr_reclaimed; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 115302c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone, 115402c6de8dSMinchan Kim struct list_head *page_list) 115502c6de8dSMinchan Kim { 115602c6de8dSMinchan Kim struct scan_control sc = { 115702c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 115802c6de8dSMinchan Kim .priority = DEF_PRIORITY, 115902c6de8dSMinchan Kim .may_unmap = 1, 116002c6de8dSMinchan Kim }; 11618e950282SMel Gorman unsigned long ret, dummy1, dummy2, dummy3, dummy4, dummy5; 116202c6de8dSMinchan Kim struct page *page, *next; 116302c6de8dSMinchan Kim LIST_HEAD(clean_pages); 116402c6de8dSMinchan Kim 116502c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1166117aad1eSRafael Aquini if (page_is_file_cache(page) && !PageDirty(page) && 1167117aad1eSRafael Aquini !isolated_balloon_page(page)) { 116802c6de8dSMinchan Kim ClearPageActive(page); 116902c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 117002c6de8dSMinchan Kim } 117102c6de8dSMinchan Kim } 117202c6de8dSMinchan Kim 117302c6de8dSMinchan Kim ret = shrink_page_list(&clean_pages, zone, &sc, 117402c6de8dSMinchan Kim TTU_UNMAP|TTU_IGNORE_ACCESS, 11758e950282SMel Gorman &dummy1, &dummy2, &dummy3, &dummy4, &dummy5, true); 117602c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 117783da7510SChristoph Lameter mod_zone_page_state(zone, NR_ISOLATED_FILE, -ret); 117802c6de8dSMinchan Kim return ret; 117902c6de8dSMinchan Kim } 118002c6de8dSMinchan Kim 11815ad333ebSAndy Whitcroft /* 11825ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 11835ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 11845ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 11855ad333ebSAndy Whitcroft * 11865ad333ebSAndy Whitcroft * page: page to consider 11875ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 11885ad333ebSAndy Whitcroft * 11895ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 11905ad333ebSAndy Whitcroft */ 1191f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 11925ad333ebSAndy Whitcroft { 11935ad333ebSAndy Whitcroft int ret = -EINVAL; 11945ad333ebSAndy Whitcroft 11955ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 11965ad333ebSAndy Whitcroft if (!PageLRU(page)) 11975ad333ebSAndy Whitcroft return ret; 11985ad333ebSAndy Whitcroft 1199e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1200e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1201894bc310SLee Schermerhorn return ret; 1202894bc310SLee Schermerhorn 12035ad333ebSAndy Whitcroft ret = -EBUSY; 120408e552c6SKAMEZAWA Hiroyuki 1205c8244935SMel Gorman /* 1206c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1207c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1208c8244935SMel Gorman * blocking - clean pages for the most part. 1209c8244935SMel Gorman * 1210c8244935SMel Gorman * ISOLATE_CLEAN means that only clean pages should be isolated. This 1211c8244935SMel Gorman * is used by reclaim when it is cannot write to backing storage 1212c8244935SMel Gorman * 1213c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1214c8244935SMel Gorman * that it is possible to migrate without blocking 1215c8244935SMel Gorman */ 1216c8244935SMel Gorman if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) { 1217c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1218c8244935SMel Gorman if (PageWriteback(page)) 121939deaf85SMinchan Kim return ret; 122039deaf85SMinchan Kim 1221c8244935SMel Gorman if (PageDirty(page)) { 1222c8244935SMel Gorman struct address_space *mapping; 1223c8244935SMel Gorman 1224c8244935SMel Gorman /* ISOLATE_CLEAN means only clean pages */ 1225c8244935SMel Gorman if (mode & ISOLATE_CLEAN) 1226c8244935SMel Gorman return ret; 1227c8244935SMel Gorman 1228c8244935SMel Gorman /* 1229c8244935SMel Gorman * Only pages without mappings or that have a 1230c8244935SMel Gorman * ->migratepage callback are possible to migrate 1231c8244935SMel Gorman * without blocking 1232c8244935SMel Gorman */ 1233c8244935SMel Gorman mapping = page_mapping(page); 1234c8244935SMel Gorman if (mapping && !mapping->a_ops->migratepage) 1235c8244935SMel Gorman return ret; 1236c8244935SMel Gorman } 1237c8244935SMel Gorman } 1238c8244935SMel Gorman 1239f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1240f80c0673SMinchan Kim return ret; 1241f80c0673SMinchan Kim 12425ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 12435ad333ebSAndy Whitcroft /* 12445ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 12455ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 12465ad333ebSAndy Whitcroft * page release code relies on it. 12475ad333ebSAndy Whitcroft */ 12485ad333ebSAndy Whitcroft ClearPageLRU(page); 12495ad333ebSAndy Whitcroft ret = 0; 12505ad333ebSAndy Whitcroft } 12515ad333ebSAndy Whitcroft 12525ad333ebSAndy Whitcroft return ret; 12535ad333ebSAndy Whitcroft } 12545ad333ebSAndy Whitcroft 125549d2e9ccSChristoph Lameter /* 12561da177e4SLinus Torvalds * zone->lru_lock is heavily contended. Some of the functions that 12571da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 12581da177e4SLinus Torvalds * and working on them outside the LRU lock. 12591da177e4SLinus Torvalds * 12601da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 12611da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 12621da177e4SLinus Torvalds * 12631da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 12641da177e4SLinus Torvalds * 12651da177e4SLinus Torvalds * @nr_to_scan: The number of pages to look through on the list. 12665dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 12671da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1268f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1269fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 12705ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 12713cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 12721da177e4SLinus Torvalds * 12731da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 12741da177e4SLinus Torvalds */ 127569e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 12765dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1277fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 12783cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 12791da177e4SLinus Torvalds { 128075b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 128169e05944SAndrew Morton unsigned long nr_taken = 0; 1282c9b02d97SWu Fengguang unsigned long scan; 12831da177e4SLinus Torvalds 1284c9b02d97SWu Fengguang for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { 12855ad333ebSAndy Whitcroft struct page *page; 1286fa9add64SHugh Dickins int nr_pages; 12875ad333ebSAndy Whitcroft 12881da177e4SLinus Torvalds page = lru_to_page(src); 12891da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 12901da177e4SLinus Torvalds 1291309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 12928d438f96SNick Piggin 1293f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 12945ad333ebSAndy Whitcroft case 0: 1295fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1296fa9add64SHugh Dickins mem_cgroup_update_lru_size(lruvec, lru, -nr_pages); 12975ad333ebSAndy Whitcroft list_move(&page->lru, dst); 1298fa9add64SHugh Dickins nr_taken += nr_pages; 12995ad333ebSAndy Whitcroft break; 13007c8ee9a8SNick Piggin 13015ad333ebSAndy Whitcroft case -EBUSY: 13025ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 13035ad333ebSAndy Whitcroft list_move(&page->lru, src); 13045ad333ebSAndy Whitcroft continue; 13055ad333ebSAndy Whitcroft 13065ad333ebSAndy Whitcroft default: 13075ad333ebSAndy Whitcroft BUG(); 13085ad333ebSAndy Whitcroft } 13095ad333ebSAndy Whitcroft } 13101da177e4SLinus Torvalds 1311f626012dSHugh Dickins *nr_scanned = scan; 131275b00af7SHugh Dickins trace_mm_vmscan_lru_isolate(sc->order, nr_to_scan, scan, 131375b00af7SHugh Dickins nr_taken, mode, is_file_lru(lru)); 13141da177e4SLinus Torvalds return nr_taken; 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds 131762695a84SNick Piggin /** 131862695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 131962695a84SNick Piggin * @page: page to isolate from its LRU list 132062695a84SNick Piggin * 132162695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 132262695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 132362695a84SNick Piggin * 132462695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 132562695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 132662695a84SNick Piggin * 132762695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1328894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1329894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1330894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 133162695a84SNick Piggin * 133262695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 133362695a84SNick Piggin * found will be decremented. 133462695a84SNick Piggin * 133562695a84SNick Piggin * Restrictions: 133662695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 133762695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 133862695a84SNick Piggin * without a stable reference). 133962695a84SNick Piggin * (2) the lru_lock must not be held. 134062695a84SNick Piggin * (3) interrupts must be enabled. 134162695a84SNick Piggin */ 134262695a84SNick Piggin int isolate_lru_page(struct page *page) 134362695a84SNick Piggin { 134462695a84SNick Piggin int ret = -EBUSY; 134562695a84SNick Piggin 1346309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 13470c917313SKonstantin Khlebnikov 134862695a84SNick Piggin if (PageLRU(page)) { 134962695a84SNick Piggin struct zone *zone = page_zone(page); 1350fa9add64SHugh Dickins struct lruvec *lruvec; 135162695a84SNick Piggin 135262695a84SNick Piggin spin_lock_irq(&zone->lru_lock); 1353fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 13540c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1355894bc310SLee Schermerhorn int lru = page_lru(page); 13560c917313SKonstantin Khlebnikov get_page(page); 135762695a84SNick Piggin ClearPageLRU(page); 1358fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1359fa9add64SHugh Dickins ret = 0; 136062695a84SNick Piggin } 136162695a84SNick Piggin spin_unlock_irq(&zone->lru_lock); 136262695a84SNick Piggin } 136362695a84SNick Piggin return ret; 136462695a84SNick Piggin } 136562695a84SNick Piggin 13665ad333ebSAndy Whitcroft /* 1367d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1368d37dd5dcSFengguang Wu * then get resheduled. When there are massive number of tasks doing page 1369d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1370d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1371d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 137235cd7815SRik van Riel */ 137335cd7815SRik van Riel static int too_many_isolated(struct zone *zone, int file, 137435cd7815SRik van Riel struct scan_control *sc) 137535cd7815SRik van Riel { 137635cd7815SRik van Riel unsigned long inactive, isolated; 137735cd7815SRik van Riel 137835cd7815SRik van Riel if (current_is_kswapd()) 137935cd7815SRik van Riel return 0; 138035cd7815SRik van Riel 138189b5fae5SJohannes Weiner if (!global_reclaim(sc)) 138235cd7815SRik van Riel return 0; 138335cd7815SRik van Riel 138435cd7815SRik van Riel if (file) { 138535cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_FILE); 138635cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_FILE); 138735cd7815SRik van Riel } else { 138835cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_ANON); 138935cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_ANON); 139035cd7815SRik van Riel } 139135cd7815SRik van Riel 13923cf23841SFengguang Wu /* 13933cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 13943cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 13953cf23841SFengguang Wu * deadlock. 13963cf23841SFengguang Wu */ 13973cf23841SFengguang Wu if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS) 13983cf23841SFengguang Wu inactive >>= 3; 13993cf23841SFengguang Wu 140035cd7815SRik van Riel return isolated > inactive; 140135cd7815SRik van Riel } 140235cd7815SRik van Riel 140366635629SMel Gorman static noinline_for_stack void 140475b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) 140566635629SMel Gorman { 140627ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 140727ac81d8SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 14083f79768fSHugh Dickins LIST_HEAD(pages_to_free); 140966635629SMel Gorman 141066635629SMel Gorman /* 141166635629SMel Gorman * Put back any unfreeable pages. 141266635629SMel Gorman */ 141366635629SMel Gorman while (!list_empty(page_list)) { 14143f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 141566635629SMel Gorman int lru; 14163f79768fSHugh Dickins 1417309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 141866635629SMel Gorman list_del(&page->lru); 141939b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 142066635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 142166635629SMel Gorman putback_lru_page(page); 142266635629SMel Gorman spin_lock_irq(&zone->lru_lock); 142366635629SMel Gorman continue; 142466635629SMel Gorman } 1425fa9add64SHugh Dickins 1426fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 1427fa9add64SHugh Dickins 14287a608572SLinus Torvalds SetPageLRU(page); 142966635629SMel Gorman lru = page_lru(page); 1430fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 1431fa9add64SHugh Dickins 143266635629SMel Gorman if (is_active_lru(lru)) { 143366635629SMel Gorman int file = is_file_lru(lru); 14349992af10SRik van Riel int numpages = hpage_nr_pages(page); 14359992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 143666635629SMel Gorman } 14372bcf8879SHugh Dickins if (put_page_testzero(page)) { 14382bcf8879SHugh Dickins __ClearPageLRU(page); 14392bcf8879SHugh Dickins __ClearPageActive(page); 1440fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 14412bcf8879SHugh Dickins 14422bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 144366635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 1444747db954SJohannes Weiner mem_cgroup_uncharge(page); 14452bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 144666635629SMel Gorman spin_lock_irq(&zone->lru_lock); 14472bcf8879SHugh Dickins } else 14482bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 144966635629SMel Gorman } 145066635629SMel Gorman } 145166635629SMel Gorman 14523f79768fSHugh Dickins /* 14533f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 14543f79768fSHugh Dickins */ 14553f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 145666635629SMel Gorman } 145766635629SMel Gorman 145866635629SMel Gorman /* 1459399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 1460399ba0b9SNeilBrown * a backing device by writing to the page cache it sets PF_LESS_THROTTLE. 1461399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 1462399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 1463399ba0b9SNeilBrown */ 1464399ba0b9SNeilBrown static int current_may_throttle(void) 1465399ba0b9SNeilBrown { 1466399ba0b9SNeilBrown return !(current->flags & PF_LESS_THROTTLE) || 1467399ba0b9SNeilBrown current->backing_dev_info == NULL || 1468399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 1469399ba0b9SNeilBrown } 1470399ba0b9SNeilBrown 1471399ba0b9SNeilBrown /* 14721742f19fSAndrew Morton * shrink_inactive_list() is a helper for shrink_zone(). It returns the number 14731742f19fSAndrew Morton * of reclaimed pages 14741da177e4SLinus Torvalds */ 147566635629SMel Gorman static noinline_for_stack unsigned long 14761a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 14779e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 14781da177e4SLinus Torvalds { 14791da177e4SLinus Torvalds LIST_HEAD(page_list); 1480e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 148105ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1482e247dbceSKOSAKI Motohiro unsigned long nr_taken; 14838e950282SMel Gorman unsigned long nr_dirty = 0; 14848e950282SMel Gorman unsigned long nr_congested = 0; 1485e2be15f6SMel Gorman unsigned long nr_unqueued_dirty = 0; 148692df3a72SMel Gorman unsigned long nr_writeback = 0; 1487b1a6f21eSMel Gorman unsigned long nr_immediate = 0; 1488f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 14893cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 14901a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 14911a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 149278dc583dSKOSAKI Motohiro 149335cd7815SRik van Riel while (unlikely(too_many_isolated(zone, file, sc))) { 149458355c78SKOSAKI Motohiro congestion_wait(BLK_RW_ASYNC, HZ/10); 149535cd7815SRik van Riel 149635cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 149735cd7815SRik van Riel if (fatal_signal_pending(current)) 149835cd7815SRik van Riel return SWAP_CLUSTER_MAX; 149935cd7815SRik van Riel } 150035cd7815SRik van Riel 15011da177e4SLinus Torvalds lru_add_drain(); 1502f80c0673SMinchan Kim 1503f80c0673SMinchan Kim if (!sc->may_unmap) 150461317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1505f80c0673SMinchan Kim if (!sc->may_writepage) 150661317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1507f80c0673SMinchan Kim 15081da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 15091da177e4SLinus Torvalds 15105dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 15115dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 151295d918fcSKonstantin Khlebnikov 151395d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 151495d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 151595d918fcSKonstantin Khlebnikov 151689b5fae5SJohannes Weiner if (global_reclaim(sc)) { 15170d5d823aSMel Gorman __mod_zone_page_state(zone, NR_PAGES_SCANNED, nr_scanned); 1518b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 151975b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scanned); 1520b35ea17bSKOSAKI Motohiro else 152175b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scanned); 1522b35ea17bSKOSAKI Motohiro } 152366635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 1524d563c050SHillf Danton 1525d563c050SHillf Danton if (nr_taken == 0) 152666635629SMel Gorman return 0; 1527b35ea17bSKOSAKI Motohiro 152802c6de8dSMinchan Kim nr_reclaimed = shrink_page_list(&page_list, zone, sc, TTU_UNMAP, 15298e950282SMel Gorman &nr_dirty, &nr_unqueued_dirty, &nr_congested, 15308e950282SMel Gorman &nr_writeback, &nr_immediate, 1531b1a6f21eSMel Gorman false); 1532c661b078SAndy Whitcroft 15333f79768fSHugh Dickins spin_lock_irq(&zone->lru_lock); 15343f79768fSHugh Dickins 153595d918fcSKonstantin Khlebnikov reclaim_stat->recent_scanned[file] += nr_taken; 1536d563c050SHillf Danton 1537904249aaSYing Han if (global_reclaim(sc)) { 1538b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1539904249aaSYing Han __count_zone_vm_events(PGSTEAL_KSWAPD, zone, 1540904249aaSYing Han nr_reclaimed); 1541904249aaSYing Han else 1542904249aaSYing Han __count_zone_vm_events(PGSTEAL_DIRECT, zone, 1543904249aaSYing Han nr_reclaimed); 1544904249aaSYing Han } 1545a74609faSNick Piggin 154627ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 15473f79768fSHugh Dickins 154895d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 15493f79768fSHugh Dickins 15503f79768fSHugh Dickins spin_unlock_irq(&zone->lru_lock); 15513f79768fSHugh Dickins 1552747db954SJohannes Weiner mem_cgroup_uncharge_list(&page_list); 1553b745bc85SMel Gorman free_hot_cold_page_list(&page_list, true); 1554e11da5b4SMel Gorman 155592df3a72SMel Gorman /* 155692df3a72SMel Gorman * If reclaim is isolating dirty pages under writeback, it implies 155792df3a72SMel Gorman * that the long-lived page allocation rate is exceeding the page 155892df3a72SMel Gorman * laundering rate. Either the global limits are not being effective 155992df3a72SMel Gorman * at throttling processes due to the page distribution throughout 156092df3a72SMel Gorman * zones or there is heavy usage of a slow backing device. The 156192df3a72SMel Gorman * only option is to throttle from reclaim context which is not ideal 156292df3a72SMel Gorman * as there is no guarantee the dirtying process is throttled in the 156392df3a72SMel Gorman * same way balance_dirty_pages() manages. 156492df3a72SMel Gorman * 15658e950282SMel Gorman * Once a zone is flagged ZONE_WRITEBACK, kswapd will count the number 15668e950282SMel Gorman * of pages under pages flagged for immediate reclaim and stall if any 15678e950282SMel Gorman * are encountered in the nr_immediate check below. 156892df3a72SMel Gorman */ 1569918fc718SMel Gorman if (nr_writeback && nr_writeback == nr_taken) 157057054651SJohannes Weiner set_bit(ZONE_WRITEBACK, &zone->flags); 157192df3a72SMel Gorman 1572d43006d5SMel Gorman /* 1573b1a6f21eSMel Gorman * memcg will stall in page writeback so only consider forcibly 1574b1a6f21eSMel Gorman * stalling for global reclaim 1575d43006d5SMel Gorman */ 1576b1a6f21eSMel Gorman if (global_reclaim(sc)) { 1577b1a6f21eSMel Gorman /* 15788e950282SMel Gorman * Tag a zone as congested if all the dirty pages scanned were 15798e950282SMel Gorman * backed by a congested BDI and wait_iff_congested will stall. 15808e950282SMel Gorman */ 15818e950282SMel Gorman if (nr_dirty && nr_dirty == nr_congested) 158257054651SJohannes Weiner set_bit(ZONE_CONGESTED, &zone->flags); 15838e950282SMel Gorman 15848e950282SMel Gorman /* 1585b1a6f21eSMel Gorman * If dirty pages are scanned that are not queued for IO, it 1586b1a6f21eSMel Gorman * implies that flushers are not keeping up. In this case, flag 158757054651SJohannes Weiner * the zone ZONE_DIRTY and kswapd will start writing pages from 158857054651SJohannes Weiner * reclaim context. 1589b1a6f21eSMel Gorman */ 1590b1a6f21eSMel Gorman if (nr_unqueued_dirty == nr_taken) 159157054651SJohannes Weiner set_bit(ZONE_DIRTY, &zone->flags); 1592b1a6f21eSMel Gorman 1593b1a6f21eSMel Gorman /* 1594b738d764SLinus Torvalds * If kswapd scans pages marked marked for immediate 1595b738d764SLinus Torvalds * reclaim and under writeback (nr_immediate), it implies 1596b738d764SLinus Torvalds * that pages are cycling through the LRU faster than 1597b1a6f21eSMel Gorman * they are written so also forcibly stall. 1598b1a6f21eSMel Gorman */ 1599b738d764SLinus Torvalds if (nr_immediate && current_may_throttle()) 1600b1a6f21eSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 1601e2be15f6SMel Gorman } 1602d43006d5SMel Gorman 16038e950282SMel Gorman /* 16048e950282SMel Gorman * Stall direct reclaim for IO completions if underlying BDIs or zone 16058e950282SMel Gorman * is congested. Allow kswapd to continue until it starts encountering 16068e950282SMel Gorman * unqueued dirty pages or cycling through the LRU too quickly. 16078e950282SMel Gorman */ 1608399ba0b9SNeilBrown if (!sc->hibernation_mode && !current_is_kswapd() && 1609399ba0b9SNeilBrown current_may_throttle()) 16108e950282SMel Gorman wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10); 16118e950282SMel Gorman 1612e11da5b4SMel Gorman trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id, 1613e11da5b4SMel Gorman zone_idx(zone), 1614e11da5b4SMel Gorman nr_scanned, nr_reclaimed, 16159e3b2f8cSKonstantin Khlebnikov sc->priority, 161623b9da55SMel Gorman trace_shrink_flags(file)); 161705ff5137SAndrew Morton return nr_reclaimed; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds 16203bb1a852SMartin Bligh /* 16211cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 16221cfb419bSKAMEZAWA Hiroyuki * 16231cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 16241cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 16251cfb419bSKAMEZAWA Hiroyuki * 16261cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 16271cfb419bSKAMEZAWA Hiroyuki * appropriate to hold zone->lru_lock across the whole operation. But if 16281cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 16291cfb419bSKAMEZAWA Hiroyuki * should drop zone->lru_lock around each page. It's impossible to balance 16301cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 16311cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 16321cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 16331cfb419bSKAMEZAWA Hiroyuki * 16341cfb419bSKAMEZAWA Hiroyuki * The downside is that we have to touch page->_count against each page. 16351cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 16361cfb419bSKAMEZAWA Hiroyuki */ 16371cfb419bSKAMEZAWA Hiroyuki 1638fa9add64SHugh Dickins static void move_active_pages_to_lru(struct lruvec *lruvec, 16393eb4140fSWu Fengguang struct list_head *list, 16402bcf8879SHugh Dickins struct list_head *pages_to_free, 16413eb4140fSWu Fengguang enum lru_list lru) 16423eb4140fSWu Fengguang { 1643fa9add64SHugh Dickins struct zone *zone = lruvec_zone(lruvec); 16443eb4140fSWu Fengguang unsigned long pgmoved = 0; 16453eb4140fSWu Fengguang struct page *page; 1646fa9add64SHugh Dickins int nr_pages; 16473eb4140fSWu Fengguang 16483eb4140fSWu Fengguang while (!list_empty(list)) { 16493eb4140fSWu Fengguang page = lru_to_page(list); 1650fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 16513eb4140fSWu Fengguang 1652309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 16533eb4140fSWu Fengguang SetPageLRU(page); 16543eb4140fSWu Fengguang 1655fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1656fa9add64SHugh Dickins mem_cgroup_update_lru_size(lruvec, lru, nr_pages); 1657925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 1658fa9add64SHugh Dickins pgmoved += nr_pages; 16593eb4140fSWu Fengguang 16602bcf8879SHugh Dickins if (put_page_testzero(page)) { 16612bcf8879SHugh Dickins __ClearPageLRU(page); 16622bcf8879SHugh Dickins __ClearPageActive(page); 1663fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 16642bcf8879SHugh Dickins 16652bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 16663eb4140fSWu Fengguang spin_unlock_irq(&zone->lru_lock); 1667747db954SJohannes Weiner mem_cgroup_uncharge(page); 16682bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 16693eb4140fSWu Fengguang spin_lock_irq(&zone->lru_lock); 16702bcf8879SHugh Dickins } else 16712bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 16723eb4140fSWu Fengguang } 16733eb4140fSWu Fengguang } 16743eb4140fSWu Fengguang __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved); 16753eb4140fSWu Fengguang if (!is_active_lru(lru)) 16763eb4140fSWu Fengguang __count_vm_events(PGDEACTIVATE, pgmoved); 16773eb4140fSWu Fengguang } 16781cfb419bSKAMEZAWA Hiroyuki 1679f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 16801a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 1681f16015fbSJohannes Weiner struct scan_control *sc, 16829e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 16831cfb419bSKAMEZAWA Hiroyuki { 168444c241f1SKOSAKI Motohiro unsigned long nr_taken; 1685f626012dSHugh Dickins unsigned long nr_scanned; 16866fe6b7e3SWu Fengguang unsigned long vm_flags; 16871cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 16888cab4754SWu Fengguang LIST_HEAD(l_active); 1689b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 16901cfb419bSKAMEZAWA Hiroyuki struct page *page; 16911a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 169244c241f1SKOSAKI Motohiro unsigned long nr_rotated = 0; 1693f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 16943cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 16951a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 16961cfb419bSKAMEZAWA Hiroyuki 16971da177e4SLinus Torvalds lru_add_drain(); 1698f80c0673SMinchan Kim 1699f80c0673SMinchan Kim if (!sc->may_unmap) 170061317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1701f80c0673SMinchan Kim if (!sc->may_writepage) 170261317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1703f80c0673SMinchan Kim 17041da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 1705925b7673SJohannes Weiner 17065dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 17075dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 170889b5fae5SJohannes Weiner if (global_reclaim(sc)) 17090d5d823aSMel Gorman __mod_zone_page_state(zone, NR_PAGES_SCANNED, nr_scanned); 171089b5fae5SJohannes Weiner 1711b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 17121cfb419bSKAMEZAWA Hiroyuki 1713f626012dSHugh Dickins __count_zone_vm_events(PGREFILL, zone, nr_scanned); 17143cb99451SKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 1715a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 17161da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 17191da177e4SLinus Torvalds cond_resched(); 17201da177e4SLinus Torvalds page = lru_to_page(&l_hold); 17211da177e4SLinus Torvalds list_del(&page->lru); 17227e9cd484SRik van Riel 172339b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1724894bc310SLee Schermerhorn putback_lru_page(page); 1725894bc310SLee Schermerhorn continue; 1726894bc310SLee Schermerhorn } 1727894bc310SLee Schermerhorn 1728cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 1729cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 1730cc715d99SMel Gorman if (page_has_private(page)) 1731cc715d99SMel Gorman try_to_release_page(page, 0); 1732cc715d99SMel Gorman unlock_page(page); 1733cc715d99SMel Gorman } 1734cc715d99SMel Gorman } 1735cc715d99SMel Gorman 1736c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 1737c3ac9a8aSJohannes Weiner &vm_flags)) { 17389992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 17398cab4754SWu Fengguang /* 17408cab4754SWu Fengguang * Identify referenced, file-backed active pages and 17418cab4754SWu Fengguang * give them one more trip around the active list. So 17428cab4754SWu Fengguang * that executable code get better chances to stay in 17438cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 17448cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 17458cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 17468cab4754SWu Fengguang * so we ignore them here. 17478cab4754SWu Fengguang */ 174841e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 17498cab4754SWu Fengguang list_add(&page->lru, &l_active); 17508cab4754SWu Fengguang continue; 17518cab4754SWu Fengguang } 17528cab4754SWu Fengguang } 17537e9cd484SRik van Riel 17545205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 17551da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds 1758b555749aSAndrew Morton /* 17598cab4754SWu Fengguang * Move pages back to the lru list. 1760b555749aSAndrew Morton */ 17612a1dc509SJohannes Weiner spin_lock_irq(&zone->lru_lock); 17624f98a2feSRik van Riel /* 17638cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 17648cab4754SWu Fengguang * even though only some of them are actually re-activated. This 17658cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 17667c0db9e9SJerome Marchand * get_scan_count. 1767556adecbSRik van Riel */ 1768b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 1769556adecbSRik van Riel 1770fa9add64SHugh Dickins move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru); 1771fa9add64SHugh Dickins move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE); 1772a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 1773f8891e5eSChristoph Lameter spin_unlock_irq(&zone->lru_lock); 17742bcf8879SHugh Dickins 1775747db954SJohannes Weiner mem_cgroup_uncharge_list(&l_hold); 1776b745bc85SMel Gorman free_hot_cold_page_list(&l_hold, true); 17771da177e4SLinus Torvalds } 17781da177e4SLinus Torvalds 177974e3f3c3SMinchan Kim #ifdef CONFIG_SWAP 178014797e23SKOSAKI Motohiro static int inactive_anon_is_low_global(struct zone *zone) 1781f89eb90eSKOSAKI Motohiro { 1782f89eb90eSKOSAKI Motohiro unsigned long active, inactive; 1783f89eb90eSKOSAKI Motohiro 1784f89eb90eSKOSAKI Motohiro active = zone_page_state(zone, NR_ACTIVE_ANON); 1785f89eb90eSKOSAKI Motohiro inactive = zone_page_state(zone, NR_INACTIVE_ANON); 1786f89eb90eSKOSAKI Motohiro 1787f89eb90eSKOSAKI Motohiro if (inactive * zone->inactive_ratio < active) 1788f89eb90eSKOSAKI Motohiro return 1; 1789f89eb90eSKOSAKI Motohiro 1790f89eb90eSKOSAKI Motohiro return 0; 1791f89eb90eSKOSAKI Motohiro } 1792f89eb90eSKOSAKI Motohiro 179314797e23SKOSAKI Motohiro /** 179414797e23SKOSAKI Motohiro * inactive_anon_is_low - check if anonymous pages need to be deactivated 1795c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 179614797e23SKOSAKI Motohiro * 179714797e23SKOSAKI Motohiro * Returns true if the zone does not have enough inactive anon pages, 179814797e23SKOSAKI Motohiro * meaning some active anon pages need to be deactivated. 179914797e23SKOSAKI Motohiro */ 1800c56d5c7dSKonstantin Khlebnikov static int inactive_anon_is_low(struct lruvec *lruvec) 180114797e23SKOSAKI Motohiro { 180274e3f3c3SMinchan Kim /* 180374e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 180474e3f3c3SMinchan Kim * is pointless. 180574e3f3c3SMinchan Kim */ 180674e3f3c3SMinchan Kim if (!total_swap_pages) 180774e3f3c3SMinchan Kim return 0; 180874e3f3c3SMinchan Kim 1809c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1810c56d5c7dSKonstantin Khlebnikov return mem_cgroup_inactive_anon_is_low(lruvec); 1811f16015fbSJohannes Weiner 1812c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low_global(lruvec_zone(lruvec)); 181314797e23SKOSAKI Motohiro } 181474e3f3c3SMinchan Kim #else 1815c56d5c7dSKonstantin Khlebnikov static inline int inactive_anon_is_low(struct lruvec *lruvec) 181674e3f3c3SMinchan Kim { 181774e3f3c3SMinchan Kim return 0; 181874e3f3c3SMinchan Kim } 181974e3f3c3SMinchan Kim #endif 182014797e23SKOSAKI Motohiro 182156e49d21SRik van Riel /** 182256e49d21SRik van Riel * inactive_file_is_low - check if file pages need to be deactivated 1823c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 182456e49d21SRik van Riel * 182556e49d21SRik van Riel * When the system is doing streaming IO, memory pressure here 182656e49d21SRik van Riel * ensures that active file pages get deactivated, until more 182756e49d21SRik van Riel * than half of the file pages are on the inactive list. 182856e49d21SRik van Riel * 182956e49d21SRik van Riel * Once we get to that situation, protect the system's working 183056e49d21SRik van Riel * set from being evicted by disabling active file page aging. 183156e49d21SRik van Riel * 183256e49d21SRik van Riel * This uses a different ratio than the anonymous pages, because 183356e49d21SRik van Riel * the page cache uses a use-once replacement algorithm. 183456e49d21SRik van Riel */ 1835c56d5c7dSKonstantin Khlebnikov static int inactive_file_is_low(struct lruvec *lruvec) 183656e49d21SRik van Riel { 1837e3790144SJohannes Weiner unsigned long inactive; 1838e3790144SJohannes Weiner unsigned long active; 183956e49d21SRik van Riel 1840e3790144SJohannes Weiner inactive = get_lru_size(lruvec, LRU_INACTIVE_FILE); 1841e3790144SJohannes Weiner active = get_lru_size(lruvec, LRU_ACTIVE_FILE); 1842e3790144SJohannes Weiner 1843e3790144SJohannes Weiner return active > inactive; 184456e49d21SRik van Riel } 184556e49d21SRik van Riel 184675b00af7SHugh Dickins static int inactive_list_is_low(struct lruvec *lruvec, enum lru_list lru) 1847b39415b2SRik van Riel { 184875b00af7SHugh Dickins if (is_file_lru(lru)) 1849c56d5c7dSKonstantin Khlebnikov return inactive_file_is_low(lruvec); 1850b39415b2SRik van Riel else 1851c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low(lruvec); 1852b39415b2SRik van Riel } 1853b39415b2SRik van Riel 18544f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 18551a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, struct scan_control *sc) 1856b69408e8SChristoph Lameter { 1857b39415b2SRik van Riel if (is_active_lru(lru)) { 185875b00af7SHugh Dickins if (inactive_list_is_low(lruvec, lru)) 18591a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 1860556adecbSRik van Riel return 0; 1861556adecbSRik van Riel } 1862556adecbSRik van Riel 18631a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 1864b69408e8SChristoph Lameter } 1865b69408e8SChristoph Lameter 18669a265114SJohannes Weiner enum scan_balance { 18679a265114SJohannes Weiner SCAN_EQUAL, 18689a265114SJohannes Weiner SCAN_FRACT, 18699a265114SJohannes Weiner SCAN_ANON, 18709a265114SJohannes Weiner SCAN_FILE, 18719a265114SJohannes Weiner }; 18729a265114SJohannes Weiner 18731da177e4SLinus Torvalds /* 18744f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 18754f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 18764f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 18774f98a2feSRik van Riel * onto the active list instead of evict. 18784f98a2feSRik van Riel * 1879be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 1880be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 18814f98a2feSRik van Riel */ 188202695175SJohannes Weiner static void get_scan_count(struct lruvec *lruvec, int swappiness, 18836b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *nr, 18846b4f7799SJohannes Weiner unsigned long *lru_pages) 18854f98a2feSRik van Riel { 188690126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 18879a265114SJohannes Weiner u64 fraction[2]; 18889a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 188990126375SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 18909a265114SJohannes Weiner unsigned long anon_prio, file_prio; 18919a265114SJohannes Weiner enum scan_balance scan_balance; 18920bf1457fSJohannes Weiner unsigned long anon, file; 18939a265114SJohannes Weiner bool force_scan = false; 18949a265114SJohannes Weiner unsigned long ap, fp; 18959a265114SJohannes Weiner enum lru_list lru; 18966f04f48dSSuleiman Souhlal bool some_scanned; 18976f04f48dSSuleiman Souhlal int pass; 1898246e87a9SKAMEZAWA Hiroyuki 1899f11c0ca5SJohannes Weiner /* 1900f11c0ca5SJohannes Weiner * If the zone or memcg is small, nr[l] can be 0. This 1901f11c0ca5SJohannes Weiner * results in no scanning on this priority and a potential 1902f11c0ca5SJohannes Weiner * priority drop. Global direct reclaim can go to the next 1903f11c0ca5SJohannes Weiner * zone and tends to have no problems. Global kswapd is for 1904f11c0ca5SJohannes Weiner * zone balancing and it needs to scan a minimum amount. When 1905f11c0ca5SJohannes Weiner * reclaiming for a memcg, a priority drop can cause high 1906f11c0ca5SJohannes Weiner * latencies, so it's better to scan a minimum amount there as 1907f11c0ca5SJohannes Weiner * well. 1908f11c0ca5SJohannes Weiner */ 190990cbc250SVladimir Davydov if (current_is_kswapd()) { 191090cbc250SVladimir Davydov if (!zone_reclaimable(zone)) 1911a4d3e9e7SJohannes Weiner force_scan = true; 191290cbc250SVladimir Davydov if (!mem_cgroup_lruvec_online(lruvec)) 191390cbc250SVladimir Davydov force_scan = true; 191490cbc250SVladimir Davydov } 191589b5fae5SJohannes Weiner if (!global_reclaim(sc)) 1916a4d3e9e7SJohannes Weiner force_scan = true; 191776a33fc3SShaohua Li 191876a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 1919ec8acf20SShaohua Li if (!sc->may_swap || (get_nr_swap_pages() <= 0)) { 19209a265114SJohannes Weiner scan_balance = SCAN_FILE; 192176a33fc3SShaohua Li goto out; 192276a33fc3SShaohua Li } 19234f98a2feSRik van Riel 192410316b31SJohannes Weiner /* 192510316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 192610316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 192710316b31SJohannes Weiner * disable swapping for individual groups completely when 192810316b31SJohannes Weiner * using the memory controller's swap limit feature would be 192910316b31SJohannes Weiner * too expensive. 193010316b31SJohannes Weiner */ 193102695175SJohannes Weiner if (!global_reclaim(sc) && !swappiness) { 19329a265114SJohannes Weiner scan_balance = SCAN_FILE; 193310316b31SJohannes Weiner goto out; 193410316b31SJohannes Weiner } 193510316b31SJohannes Weiner 193610316b31SJohannes Weiner /* 193710316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 193810316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 193910316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 194010316b31SJohannes Weiner */ 194102695175SJohannes Weiner if (!sc->priority && swappiness) { 19429a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 194310316b31SJohannes Weiner goto out; 194410316b31SJohannes Weiner } 194510316b31SJohannes Weiner 194611d16c25SJohannes Weiner /* 194762376251SJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 194862376251SJohannes Weiner * cache pages start out inactive, every cache fault will tip 194962376251SJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 195062376251SJohannes Weiner * shrinks, so does the window for rotation from references. 195162376251SJohannes Weiner * This means we have a runaway feedback loop where a tiny 195262376251SJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 195362376251SJohannes Weiner * anon pages. Try to detect this based on file LRU size. 195462376251SJohannes Weiner */ 195562376251SJohannes Weiner if (global_reclaim(sc)) { 19562ab051e1SJerome Marchand unsigned long zonefile; 19572ab051e1SJerome Marchand unsigned long zonefree; 195862376251SJohannes Weiner 19592ab051e1SJerome Marchand zonefree = zone_page_state(zone, NR_FREE_PAGES); 19602ab051e1SJerome Marchand zonefile = zone_page_state(zone, NR_ACTIVE_FILE) + 19612ab051e1SJerome Marchand zone_page_state(zone, NR_INACTIVE_FILE); 19622ab051e1SJerome Marchand 19632ab051e1SJerome Marchand if (unlikely(zonefile + zonefree <= high_wmark_pages(zone))) { 196462376251SJohannes Weiner scan_balance = SCAN_ANON; 196562376251SJohannes Weiner goto out; 196662376251SJohannes Weiner } 196762376251SJohannes Weiner } 196862376251SJohannes Weiner 196962376251SJohannes Weiner /* 19707c5bd705SJohannes Weiner * There is enough inactive page cache, do not reclaim 19717c5bd705SJohannes Weiner * anything from the anonymous working set right now. 1972e9868505SRik van Riel */ 19737c5bd705SJohannes Weiner if (!inactive_file_is_low(lruvec)) { 19749a265114SJohannes Weiner scan_balance = SCAN_FILE; 1975e9868505SRik van Riel goto out; 19764f98a2feSRik van Riel } 19774f98a2feSRik van Riel 19789a265114SJohannes Weiner scan_balance = SCAN_FRACT; 19799a265114SJohannes Weiner 19804f98a2feSRik van Riel /* 198158c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 198258c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 198358c37f6eSKOSAKI Motohiro */ 198402695175SJohannes Weiner anon_prio = swappiness; 198575b00af7SHugh Dickins file_prio = 200 - anon_prio; 198658c37f6eSKOSAKI Motohiro 198758c37f6eSKOSAKI Motohiro /* 19884f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 19894f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 19904f98a2feSRik van Riel * ratios to determine how valuable each cache is. 19914f98a2feSRik van Riel * 19924f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 19934f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 19944f98a2feSRik van Riel * up weighing recent references more than old ones. 19954f98a2feSRik van Riel * 19964f98a2feSRik van Riel * anon in [0], file in [1] 19974f98a2feSRik van Riel */ 19982ab051e1SJerome Marchand 19992ab051e1SJerome Marchand anon = get_lru_size(lruvec, LRU_ACTIVE_ANON) + 20002ab051e1SJerome Marchand get_lru_size(lruvec, LRU_INACTIVE_ANON); 20012ab051e1SJerome Marchand file = get_lru_size(lruvec, LRU_ACTIVE_FILE) + 20022ab051e1SJerome Marchand get_lru_size(lruvec, LRU_INACTIVE_FILE); 20032ab051e1SJerome Marchand 200490126375SKonstantin Khlebnikov spin_lock_irq(&zone->lru_lock); 200558c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 20066e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 20076e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 20084f98a2feSRik van Riel } 20094f98a2feSRik van Riel 20106e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 20116e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 20126e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 20134f98a2feSRik van Riel } 20144f98a2feSRik van Riel 20154f98a2feSRik van Riel /* 201600d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 201700d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 201800d8089cSRik van Riel * each list that were recently referenced and in active use. 20194f98a2feSRik van Riel */ 2020fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 20216e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 20224f98a2feSRik van Riel 2023fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 20246e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 202590126375SKonstantin Khlebnikov spin_unlock_irq(&zone->lru_lock); 20264f98a2feSRik van Riel 202776a33fc3SShaohua Li fraction[0] = ap; 202876a33fc3SShaohua Li fraction[1] = fp; 202976a33fc3SShaohua Li denominator = ap + fp + 1; 203076a33fc3SShaohua Li out: 20316f04f48dSSuleiman Souhlal some_scanned = false; 20326f04f48dSSuleiman Souhlal /* Only use force_scan on second pass. */ 20336f04f48dSSuleiman Souhlal for (pass = 0; !some_scanned && pass < 2; pass++) { 20346b4f7799SJohannes Weiner *lru_pages = 0; 20354111304dSHugh Dickins for_each_evictable_lru(lru) { 20364111304dSHugh Dickins int file = is_file_lru(lru); 2037d778df51SJohannes Weiner unsigned long size; 203876a33fc3SShaohua Li unsigned long scan; 203976a33fc3SShaohua Li 2040d778df51SJohannes Weiner size = get_lru_size(lruvec, lru); 2041d778df51SJohannes Weiner scan = size >> sc->priority; 20429a265114SJohannes Weiner 20436f04f48dSSuleiman Souhlal if (!scan && pass && force_scan) 2044d778df51SJohannes Weiner scan = min(size, SWAP_CLUSTER_MAX); 20459a265114SJohannes Weiner 20469a265114SJohannes Weiner switch (scan_balance) { 20479a265114SJohannes Weiner case SCAN_EQUAL: 20489a265114SJohannes Weiner /* Scan lists relative to size */ 20499a265114SJohannes Weiner break; 20509a265114SJohannes Weiner case SCAN_FRACT: 20519a265114SJohannes Weiner /* 20529a265114SJohannes Weiner * Scan types proportional to swappiness and 20539a265114SJohannes Weiner * their relative recent reclaim efficiency. 20549a265114SJohannes Weiner */ 20556f04f48dSSuleiman Souhlal scan = div64_u64(scan * fraction[file], 20566f04f48dSSuleiman Souhlal denominator); 20579a265114SJohannes Weiner break; 20589a265114SJohannes Weiner case SCAN_FILE: 20599a265114SJohannes Weiner case SCAN_ANON: 20609a265114SJohannes Weiner /* Scan one type exclusively */ 20616b4f7799SJohannes Weiner if ((scan_balance == SCAN_FILE) != file) { 20626b4f7799SJohannes Weiner size = 0; 20639a265114SJohannes Weiner scan = 0; 20646b4f7799SJohannes Weiner } 20659a265114SJohannes Weiner break; 20669a265114SJohannes Weiner default: 20679a265114SJohannes Weiner /* Look ma, no brain */ 20689a265114SJohannes Weiner BUG(); 20699a265114SJohannes Weiner } 20706b4f7799SJohannes Weiner 20716b4f7799SJohannes Weiner *lru_pages += size; 20724111304dSHugh Dickins nr[lru] = scan; 20736b4f7799SJohannes Weiner 20746f04f48dSSuleiman Souhlal /* 20756f04f48dSSuleiman Souhlal * Skip the second pass and don't force_scan, 20766f04f48dSSuleiman Souhlal * if we found something to scan. 20776f04f48dSSuleiman Souhlal */ 20786f04f48dSSuleiman Souhlal some_scanned |= !!scan; 20796f04f48dSSuleiman Souhlal } 208076a33fc3SShaohua Li } 20816e08a369SWu Fengguang } 20824f98a2feSRik van Riel 20839b4f98cdSJohannes Weiner /* 20849b4f98cdSJohannes Weiner * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. 20859b4f98cdSJohannes Weiner */ 208602695175SJohannes Weiner static void shrink_lruvec(struct lruvec *lruvec, int swappiness, 20876b4f7799SJohannes Weiner struct scan_control *sc, unsigned long *lru_pages) 20889b4f98cdSJohannes Weiner { 20899b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2090e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 20919b4f98cdSJohannes Weiner unsigned long nr_to_scan; 20929b4f98cdSJohannes Weiner enum lru_list lru; 20939b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 20949b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 20959b4f98cdSJohannes Weiner struct blk_plug plug; 20961a501907SMel Gorman bool scan_adjusted; 20979b4f98cdSJohannes Weiner 20986b4f7799SJohannes Weiner get_scan_count(lruvec, swappiness, sc, nr, lru_pages); 20999b4f98cdSJohannes Weiner 2100e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2101e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2102e82e0561SMel Gorman 21031a501907SMel Gorman /* 21041a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 21051a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 21061a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 21071a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 21081a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 21091a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 21101a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 21111a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 21121a501907SMel Gorman * dropped to zero at the first pass. 21131a501907SMel Gorman */ 21141a501907SMel Gorman scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() && 21151a501907SMel Gorman sc->priority == DEF_PRIORITY); 21161a501907SMel Gorman 21179b4f98cdSJohannes Weiner blk_start_plug(&plug); 21189b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 21199b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2120e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2121e82e0561SMel Gorman unsigned long nr_scanned; 2122e82e0561SMel Gorman 21239b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 21249b4f98cdSJohannes Weiner if (nr[lru]) { 21259b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 21269b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 21279b4f98cdSJohannes Weiner 21289b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 21299b4f98cdSJohannes Weiner lruvec, sc); 21309b4f98cdSJohannes Weiner } 21319b4f98cdSJohannes Weiner } 2132e82e0561SMel Gorman 2133e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2134e82e0561SMel Gorman continue; 2135e82e0561SMel Gorman 21369b4f98cdSJohannes Weiner /* 2137e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 21381a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2139e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2140e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2141e82e0561SMel Gorman * proportional to the original scan target. 2142e82e0561SMel Gorman */ 2143e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2144e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2145e82e0561SMel Gorman 21461a501907SMel Gorman /* 21471a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 21481a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 21491a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 21501a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 21511a501907SMel Gorman */ 21521a501907SMel Gorman if (!nr_file || !nr_anon) 21531a501907SMel Gorman break; 21541a501907SMel Gorman 2155e82e0561SMel Gorman if (nr_file > nr_anon) { 2156e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2157e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2158e82e0561SMel Gorman lru = LRU_BASE; 2159e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2160e82e0561SMel Gorman } else { 2161e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2162e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2163e82e0561SMel Gorman lru = LRU_FILE; 2164e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2165e82e0561SMel Gorman } 2166e82e0561SMel Gorman 2167e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2168e82e0561SMel Gorman nr[lru] = 0; 2169e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2170e82e0561SMel Gorman 2171e82e0561SMel Gorman /* 2172e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2173e82e0561SMel Gorman * scan target and the percentage scanning already complete 2174e82e0561SMel Gorman */ 2175e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2176e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2177e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2178e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2179e82e0561SMel Gorman 2180e82e0561SMel Gorman lru += LRU_ACTIVE; 2181e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2182e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2183e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2184e82e0561SMel Gorman 2185e82e0561SMel Gorman scan_adjusted = true; 21869b4f98cdSJohannes Weiner } 21879b4f98cdSJohannes Weiner blk_finish_plug(&plug); 21889b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 21899b4f98cdSJohannes Weiner 21909b4f98cdSJohannes Weiner /* 21919b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 21929b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 21939b4f98cdSJohannes Weiner */ 21949b4f98cdSJohannes Weiner if (inactive_anon_is_low(lruvec)) 21959b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 21969b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 21979b4f98cdSJohannes Weiner 21989b4f98cdSJohannes Weiner throttle_vm_writeout(sc->gfp_mask); 21999b4f98cdSJohannes Weiner } 22009b4f98cdSJohannes Weiner 220123b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 22029e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 220323b9da55SMel Gorman { 2204d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 220523b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 22069e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 220723b9da55SMel Gorman return true; 220823b9da55SMel Gorman 220923b9da55SMel Gorman return false; 221023b9da55SMel Gorman } 221123b9da55SMel Gorman 22124f98a2feSRik van Riel /* 221323b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 221423b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 221523b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 221623b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 221723b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 22183e7d3449SMel Gorman */ 22199b4f98cdSJohannes Weiner static inline bool should_continue_reclaim(struct zone *zone, 22203e7d3449SMel Gorman unsigned long nr_reclaimed, 22213e7d3449SMel Gorman unsigned long nr_scanned, 22223e7d3449SMel Gorman struct scan_control *sc) 22233e7d3449SMel Gorman { 22243e7d3449SMel Gorman unsigned long pages_for_compaction; 22253e7d3449SMel Gorman unsigned long inactive_lru_pages; 22263e7d3449SMel Gorman 22273e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 22289e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 22293e7d3449SMel Gorman return false; 22303e7d3449SMel Gorman 22312876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 22322876592fSMel Gorman if (sc->gfp_mask & __GFP_REPEAT) { 22333e7d3449SMel Gorman /* 22342876592fSMel Gorman * For __GFP_REPEAT allocations, stop reclaiming if the 22352876592fSMel Gorman * full LRU list has been scanned and we are still failing 22362876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 22372876592fSMel Gorman * expensive but a __GFP_REPEAT caller really wants to succeed 22383e7d3449SMel Gorman */ 22393e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 22403e7d3449SMel Gorman return false; 22412876592fSMel Gorman } else { 22422876592fSMel Gorman /* 22432876592fSMel Gorman * For non-__GFP_REPEAT allocations which can presumably 22442876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 22452876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 22462876592fSMel Gorman * pages that were scanned. This will return to the 22472876592fSMel Gorman * caller faster at the risk reclaim/compaction and 22482876592fSMel Gorman * the resulting allocation attempt fails 22492876592fSMel Gorman */ 22502876592fSMel Gorman if (!nr_reclaimed) 22512876592fSMel Gorman return false; 22522876592fSMel Gorman } 22533e7d3449SMel Gorman 22543e7d3449SMel Gorman /* 22553e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 22563e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 22573e7d3449SMel Gorman */ 22583e7d3449SMel Gorman pages_for_compaction = (2UL << sc->order); 22599b4f98cdSJohannes Weiner inactive_lru_pages = zone_page_state(zone, NR_INACTIVE_FILE); 2260ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 22619b4f98cdSJohannes Weiner inactive_lru_pages += zone_page_state(zone, NR_INACTIVE_ANON); 22623e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 22633e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 22643e7d3449SMel Gorman return true; 22653e7d3449SMel Gorman 22663e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 2267ebff3980SVlastimil Babka switch (compaction_suitable(zone, sc->order, 0, 0)) { 22683e7d3449SMel Gorman case COMPACT_PARTIAL: 22693e7d3449SMel Gorman case COMPACT_CONTINUE: 22703e7d3449SMel Gorman return false; 22713e7d3449SMel Gorman default: 22723e7d3449SMel Gorman return true; 22733e7d3449SMel Gorman } 22743e7d3449SMel Gorman } 22753e7d3449SMel Gorman 22766b4f7799SJohannes Weiner static bool shrink_zone(struct zone *zone, struct scan_control *sc, 22776b4f7799SJohannes Weiner bool is_classzone) 2278f16015fbSJohannes Weiner { 22799b4f98cdSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 22802344d7e4SJohannes Weiner bool reclaimable = false; 22819b4f98cdSJohannes Weiner 22829b4f98cdSJohannes Weiner do { 22835660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 22845660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 22855660048cSJohannes Weiner .zone = zone, 22869e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 22875660048cSJohannes Weiner }; 22886b4f7799SJohannes Weiner unsigned long zone_lru_pages = 0; 2289694fbc0fSAndrew Morton struct mem_cgroup *memcg; 22905660048cSJohannes Weiner 22919b4f98cdSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 22929b4f98cdSJohannes Weiner nr_scanned = sc->nr_scanned; 22939b4f98cdSJohannes Weiner 2294694fbc0fSAndrew Morton memcg = mem_cgroup_iter(root, NULL, &reclaim); 2295694fbc0fSAndrew Morton do { 22966b4f7799SJohannes Weiner unsigned long lru_pages; 22979b4f98cdSJohannes Weiner struct lruvec *lruvec; 229802695175SJohannes Weiner int swappiness; 22999b4f98cdSJohannes Weiner 2300*241994edSJohannes Weiner if (mem_cgroup_low(root, memcg)) { 2301*241994edSJohannes Weiner if (!sc->may_thrash) 2302*241994edSJohannes Weiner continue; 2303*241994edSJohannes Weiner mem_cgroup_events(memcg, MEMCG_LOW, 1); 2304*241994edSJohannes Weiner } 2305*241994edSJohannes Weiner 23069b4f98cdSJohannes Weiner lruvec = mem_cgroup_zone_lruvec(zone, memcg); 230702695175SJohannes Weiner swappiness = mem_cgroup_swappiness(memcg); 23085660048cSJohannes Weiner 23096b4f7799SJohannes Weiner shrink_lruvec(lruvec, swappiness, sc, &lru_pages); 23106b4f7799SJohannes Weiner zone_lru_pages += lru_pages; 2311f9be23d6SKonstantin Khlebnikov 23125660048cSJohannes Weiner /* 2313a394cb8eSMichal Hocko * Direct reclaim and kswapd have to scan all memory 2314a394cb8eSMichal Hocko * cgroups to fulfill the overall scan target for the 23159b4f98cdSJohannes Weiner * zone. 2316a394cb8eSMichal Hocko * 2317a394cb8eSMichal Hocko * Limit reclaim, on the other hand, only cares about 2318a394cb8eSMichal Hocko * nr_to_reclaim pages to be reclaimed and it will 2319a394cb8eSMichal Hocko * retry with decreasing priority if one round over the 2320a394cb8eSMichal Hocko * whole hierarchy is not sufficient. 23215660048cSJohannes Weiner */ 2322a394cb8eSMichal Hocko if (!global_reclaim(sc) && 2323a394cb8eSMichal Hocko sc->nr_reclaimed >= sc->nr_to_reclaim) { 23245660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 23255660048cSJohannes Weiner break; 23265660048cSJohannes Weiner } 2327*241994edSJohannes Weiner } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim))); 232870ddf637SAnton Vorontsov 23296b4f7799SJohannes Weiner /* 23306b4f7799SJohannes Weiner * Shrink the slab caches in the same proportion that 23316b4f7799SJohannes Weiner * the eligible LRU pages were scanned. 23326b4f7799SJohannes Weiner */ 23336b4f7799SJohannes Weiner if (global_reclaim(sc) && is_classzone) { 23346b4f7799SJohannes Weiner struct reclaim_state *reclaim_state; 23356b4f7799SJohannes Weiner 23366b4f7799SJohannes Weiner shrink_node_slabs(sc->gfp_mask, zone_to_nid(zone), 23376b4f7799SJohannes Weiner sc->nr_scanned - nr_scanned, 23386b4f7799SJohannes Weiner zone_lru_pages); 23396b4f7799SJohannes Weiner 23406b4f7799SJohannes Weiner reclaim_state = current->reclaim_state; 23416b4f7799SJohannes Weiner if (reclaim_state) { 23426b4f7799SJohannes Weiner sc->nr_reclaimed += 23436b4f7799SJohannes Weiner reclaim_state->reclaimed_slab; 23446b4f7799SJohannes Weiner reclaim_state->reclaimed_slab = 0; 23456b4f7799SJohannes Weiner } 23466b4f7799SJohannes Weiner } 23476b4f7799SJohannes Weiner 234870ddf637SAnton Vorontsov vmpressure(sc->gfp_mask, sc->target_mem_cgroup, 234970ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 235070ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 235170ddf637SAnton Vorontsov 23522344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 23532344d7e4SJohannes Weiner reclaimable = true; 23542344d7e4SJohannes Weiner 23559b4f98cdSJohannes Weiner } while (should_continue_reclaim(zone, sc->nr_reclaimed - nr_reclaimed, 23569b4f98cdSJohannes Weiner sc->nr_scanned - nr_scanned, sc)); 23572344d7e4SJohannes Weiner 23582344d7e4SJohannes Weiner return reclaimable; 2359f16015fbSJohannes Weiner } 2360f16015fbSJohannes Weiner 236153853e2dSVlastimil Babka /* 236253853e2dSVlastimil Babka * Returns true if compaction should go ahead for a high-order request, or 236353853e2dSVlastimil Babka * the high-order allocation would succeed without compaction. 236453853e2dSVlastimil Babka */ 23650b06496aSJohannes Weiner static inline bool compaction_ready(struct zone *zone, int order) 2366fe4b1b24SMel Gorman { 2367fe4b1b24SMel Gorman unsigned long balance_gap, watermark; 2368fe4b1b24SMel Gorman bool watermark_ok; 2369fe4b1b24SMel Gorman 2370fe4b1b24SMel Gorman /* 2371fe4b1b24SMel Gorman * Compaction takes time to run and there are potentially other 2372fe4b1b24SMel Gorman * callers using the pages just freed. Continue reclaiming until 2373fe4b1b24SMel Gorman * there is a buffer of free pages available to give compaction 2374fe4b1b24SMel Gorman * a reasonable chance of completing and allocating the page 2375fe4b1b24SMel Gorman */ 23764be89a34SJianyu Zhan balance_gap = min(low_wmark_pages(zone), DIV_ROUND_UP( 23774be89a34SJianyu Zhan zone->managed_pages, KSWAPD_ZONE_BALANCE_GAP_RATIO)); 23780b06496aSJohannes Weiner watermark = high_wmark_pages(zone) + balance_gap + (2UL << order); 2379fe4b1b24SMel Gorman watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0); 2380fe4b1b24SMel Gorman 2381fe4b1b24SMel Gorman /* 2382fe4b1b24SMel Gorman * If compaction is deferred, reclaim up to a point where 2383fe4b1b24SMel Gorman * compaction will have a chance of success when re-enabled 2384fe4b1b24SMel Gorman */ 23850b06496aSJohannes Weiner if (compaction_deferred(zone, order)) 2386fe4b1b24SMel Gorman return watermark_ok; 2387fe4b1b24SMel Gorman 238853853e2dSVlastimil Babka /* 238953853e2dSVlastimil Babka * If compaction is not ready to start and allocation is not likely 239053853e2dSVlastimil Babka * to succeed without it, then keep reclaiming. 239153853e2dSVlastimil Babka */ 2392ebff3980SVlastimil Babka if (compaction_suitable(zone, order, 0, 0) == COMPACT_SKIPPED) 2393fe4b1b24SMel Gorman return false; 2394fe4b1b24SMel Gorman 2395fe4b1b24SMel Gorman return watermark_ok; 2396fe4b1b24SMel Gorman } 2397fe4b1b24SMel Gorman 23981da177e4SLinus Torvalds /* 23991da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 24001da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 24011da177e4SLinus Torvalds * request. 24021da177e4SLinus Torvalds * 240341858966SMel Gorman * We reclaim from a zone even if that zone is over high_wmark_pages(zone). 240441858966SMel Gorman * Because: 24051da177e4SLinus Torvalds * a) The caller may be trying to free *extra* pages to satisfy a higher-order 24061da177e4SLinus Torvalds * allocation or 240741858966SMel Gorman * b) The target zone may be at high_wmark_pages(zone) but the lower zones 240841858966SMel Gorman * must go *over* high_wmark_pages(zone) to satisfy the `incremental min' 240941858966SMel Gorman * zone defense algorithm. 24101da177e4SLinus Torvalds * 24111da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 24121da177e4SLinus Torvalds * scan then give up on it. 24132344d7e4SJohannes Weiner * 24142344d7e4SJohannes Weiner * Returns true if a zone was reclaimable. 24151da177e4SLinus Torvalds */ 24162344d7e4SJohannes Weiner static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 24171da177e4SLinus Torvalds { 2418dd1a239fSMel Gorman struct zoneref *z; 241954a6eb5cSMel Gorman struct zone *zone; 24200608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 24210608f43dSAndrew Morton unsigned long nr_soft_scanned; 2422619d0d76SWeijie Yang gfp_t orig_mask; 24239bbc04eeSWeijie Yang enum zone_type requested_highidx = gfp_zone(sc->gfp_mask); 24242344d7e4SJohannes Weiner bool reclaimable = false; 24251cfb419bSKAMEZAWA Hiroyuki 2426cc715d99SMel Gorman /* 2427cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2428cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2429cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2430cc715d99SMel Gorman */ 2431619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 2432cc715d99SMel Gorman if (buffer_heads_over_limit) 2433cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 2434cc715d99SMel Gorman 2435d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 24366b4f7799SJohannes Weiner requested_highidx, sc->nodemask) { 24376b4f7799SJohannes Weiner enum zone_type classzone_idx; 24386b4f7799SJohannes Weiner 2439f3fe6512SCon Kolivas if (!populated_zone(zone)) 24401da177e4SLinus Torvalds continue; 24416b4f7799SJohannes Weiner 24426b4f7799SJohannes Weiner classzone_idx = requested_highidx; 24436b4f7799SJohannes Weiner while (!populated_zone(zone->zone_pgdat->node_zones + 24446b4f7799SJohannes Weiner classzone_idx)) 24456b4f7799SJohannes Weiner classzone_idx--; 24466b4f7799SJohannes Weiner 24471cfb419bSKAMEZAWA Hiroyuki /* 24481cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 24491cfb419bSKAMEZAWA Hiroyuki * to global LRU. 24501cfb419bSKAMEZAWA Hiroyuki */ 245189b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2452344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, 2453344736f2SVladimir Davydov GFP_KERNEL | __GFP_HARDWALL)) 24541da177e4SLinus Torvalds continue; 245565ec02cbSVladimir Davydov 24566e543d57SLisa Du if (sc->priority != DEF_PRIORITY && 24576e543d57SLisa Du !zone_reclaimable(zone)) 24581da177e4SLinus Torvalds continue; /* Let kswapd poll it */ 24590b06496aSJohannes Weiner 2460e0887c19SRik van Riel /* 2461e0c23279SMel Gorman * If we already have plenty of memory free for 2462e0c23279SMel Gorman * compaction in this zone, don't free any more. 2463e0c23279SMel Gorman * Even though compaction is invoked for any 2464e0c23279SMel Gorman * non-zero order, only frequent costly order 2465e0c23279SMel Gorman * reclamation is disruptive enough to become a 2466c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2467c7cfa37bSCopot Alexandru * page allocations. 2468e0887c19SRik van Riel */ 24690b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 24700b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 24710b06496aSJohannes Weiner zonelist_zone_idx(z) <= requested_highidx && 24720b06496aSJohannes Weiner compaction_ready(zone, sc->order)) { 24730b06496aSJohannes Weiner sc->compaction_ready = true; 2474e0887c19SRik van Riel continue; 2475e0887c19SRik van Riel } 24760b06496aSJohannes Weiner 24770608f43dSAndrew Morton /* 24780608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 24790608f43dSAndrew Morton * and returns the number of reclaimed pages and 24800608f43dSAndrew Morton * scanned pages. This works for global memory pressure 24810608f43dSAndrew Morton * and balancing, not for a memcg's limit. 24820608f43dSAndrew Morton */ 24830608f43dSAndrew Morton nr_soft_scanned = 0; 24840608f43dSAndrew Morton nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 24850608f43dSAndrew Morton sc->order, sc->gfp_mask, 24860608f43dSAndrew Morton &nr_soft_scanned); 24870608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 24880608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 24892344d7e4SJohannes Weiner if (nr_soft_reclaimed) 24902344d7e4SJohannes Weiner reclaimable = true; 2491ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2492ac34a1a3SKAMEZAWA Hiroyuki } 2493d149e3b2SYing Han 24946b4f7799SJohannes Weiner if (shrink_zone(zone, sc, zone_idx(zone) == classzone_idx)) 24952344d7e4SJohannes Weiner reclaimable = true; 24962344d7e4SJohannes Weiner 24972344d7e4SJohannes Weiner if (global_reclaim(sc) && 24982344d7e4SJohannes Weiner !reclaimable && zone_reclaimable(zone)) 24992344d7e4SJohannes Weiner reclaimable = true; 25001da177e4SLinus Torvalds } 2501e0c23279SMel Gorman 250265ec02cbSVladimir Davydov /* 2503619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 2504619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 2505619d0d76SWeijie Yang */ 2506619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 2507d1908362SMinchan Kim 25082344d7e4SJohannes Weiner return reclaimable; 25091da177e4SLinus Torvalds } 25101da177e4SLinus Torvalds 25111da177e4SLinus Torvalds /* 25121da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 25131da177e4SLinus Torvalds * 25141da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 25151da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 25161da177e4SLinus Torvalds * 25171da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 25181da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 25195b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 25205b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 25215b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 25225b0830cbSJens Axboe * work, and the allocation attempt will fail. 2523a41f24eaSNishanth Aravamudan * 2524a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2525a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 25261da177e4SLinus Torvalds */ 2527dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 25283115cd91SVladimir Davydov struct scan_control *sc) 25291da177e4SLinus Torvalds { 2530*241994edSJohannes Weiner int initial_priority = sc->priority; 253169e05944SAndrew Morton unsigned long total_scanned = 0; 253222fba335SKOSAKI Motohiro unsigned long writeback_threshold; 25332344d7e4SJohannes Weiner bool zones_reclaimable; 2534*241994edSJohannes Weiner retry: 2535873b4771SKeika Kobayashi delayacct_freepages_start(); 2536873b4771SKeika Kobayashi 253789b5fae5SJohannes Weiner if (global_reclaim(sc)) 2538f8891e5eSChristoph Lameter count_vm_event(ALLOCSTALL); 25391da177e4SLinus Torvalds 25409e3b2f8cSKonstantin Khlebnikov do { 254170ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 254270ddf637SAnton Vorontsov sc->priority); 254366e1707bSBalbir Singh sc->nr_scanned = 0; 25442344d7e4SJohannes Weiner zones_reclaimable = shrink_zones(zonelist, sc); 2545e0c23279SMel Gorman 254666e1707bSBalbir Singh total_scanned += sc->nr_scanned; 2547bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 25480b06496aSJohannes Weiner break; 25490b06496aSJohannes Weiner 25500b06496aSJohannes Weiner if (sc->compaction_ready) 25510b06496aSJohannes Weiner break; 25521da177e4SLinus Torvalds 25531da177e4SLinus Torvalds /* 25540e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 25550e50ce3bSMinchan Kim * writepage even in laptop mode. 25560e50ce3bSMinchan Kim */ 25570e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 25580e50ce3bSMinchan Kim sc->may_writepage = 1; 25590e50ce3bSMinchan Kim 25600e50ce3bSMinchan Kim /* 25611da177e4SLinus Torvalds * Try to write back as many pages as we just scanned. This 25621da177e4SLinus Torvalds * tends to cause slow streaming writers to write data to the 25631da177e4SLinus Torvalds * disk smoothly, at the dirtying rate, which is nice. But 25641da177e4SLinus Torvalds * that's undesirable in laptop mode, where we *want* lumpy 25651da177e4SLinus Torvalds * writeout. So in laptop mode, write out the whole world. 25661da177e4SLinus Torvalds */ 256722fba335SKOSAKI Motohiro writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2; 256822fba335SKOSAKI Motohiro if (total_scanned > writeback_threshold) { 25690e175a18SCurt Wohlgemuth wakeup_flusher_threads(laptop_mode ? 0 : total_scanned, 25700e175a18SCurt Wohlgemuth WB_REASON_TRY_TO_FREE_PAGES); 257166e1707bSBalbir Singh sc->may_writepage = 1; 25721da177e4SLinus Torvalds } 25730b06496aSJohannes Weiner } while (--sc->priority >= 0); 2574bb21c7ceSKOSAKI Motohiro 2575873b4771SKeika Kobayashi delayacct_freepages_end(); 2576873b4771SKeika Kobayashi 2577bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 2578bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 2579bb21c7ceSKOSAKI Motohiro 25800cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 25810b06496aSJohannes Weiner if (sc->compaction_ready) 25827335084dSMel Gorman return 1; 25837335084dSMel Gorman 2584*241994edSJohannes Weiner /* Untapped cgroup reserves? Don't OOM, retry. */ 2585*241994edSJohannes Weiner if (!sc->may_thrash) { 2586*241994edSJohannes Weiner sc->priority = initial_priority; 2587*241994edSJohannes Weiner sc->may_thrash = 1; 2588*241994edSJohannes Weiner goto retry; 2589*241994edSJohannes Weiner } 2590*241994edSJohannes Weiner 25912344d7e4SJohannes Weiner /* Any of the zones still reclaimable? Don't OOM. */ 25922344d7e4SJohannes Weiner if (zones_reclaimable) 2593bb21c7ceSKOSAKI Motohiro return 1; 2594bb21c7ceSKOSAKI Motohiro 2595bb21c7ceSKOSAKI Motohiro return 0; 25961da177e4SLinus Torvalds } 25971da177e4SLinus Torvalds 25985515061dSMel Gorman static bool pfmemalloc_watermark_ok(pg_data_t *pgdat) 25995515061dSMel Gorman { 26005515061dSMel Gorman struct zone *zone; 26015515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 26025515061dSMel Gorman unsigned long free_pages = 0; 26035515061dSMel Gorman int i; 26045515061dSMel Gorman bool wmark_ok; 26055515061dSMel Gorman 26065515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 26075515061dSMel Gorman zone = &pgdat->node_zones[i]; 2608675becceSMel Gorman if (!populated_zone(zone)) 2609675becceSMel Gorman continue; 2610675becceSMel Gorman 26115515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 26125515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 26135515061dSMel Gorman } 26145515061dSMel Gorman 2615675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 2616675becceSMel Gorman if (!pfmemalloc_reserve) 2617675becceSMel Gorman return true; 2618675becceSMel Gorman 26195515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 26205515061dSMel Gorman 26215515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 26225515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 26235515061dSMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, 26245515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 26255515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 26265515061dSMel Gorman } 26275515061dSMel Gorman 26285515061dSMel Gorman return wmark_ok; 26295515061dSMel Gorman } 26305515061dSMel Gorman 26315515061dSMel Gorman /* 26325515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 26335515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 26345515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 263550694c28SMel Gorman * when the low watermark is reached. 263650694c28SMel Gorman * 263750694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 263850694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 26395515061dSMel Gorman */ 264050694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 26415515061dSMel Gorman nodemask_t *nodemask) 26425515061dSMel Gorman { 2643675becceSMel Gorman struct zoneref *z; 26445515061dSMel Gorman struct zone *zone; 2645675becceSMel Gorman pg_data_t *pgdat = NULL; 26465515061dSMel Gorman 26475515061dSMel Gorman /* 26485515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 26495515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 26505515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 26515515061dSMel Gorman * committing a transaction where throttling it could forcing other 26525515061dSMel Gorman * processes to block on log_wait_commit(). 26535515061dSMel Gorman */ 26545515061dSMel Gorman if (current->flags & PF_KTHREAD) 265550694c28SMel Gorman goto out; 265650694c28SMel Gorman 265750694c28SMel Gorman /* 265850694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 265950694c28SMel Gorman * It should return quickly so it can exit and free its memory 266050694c28SMel Gorman */ 266150694c28SMel Gorman if (fatal_signal_pending(current)) 266250694c28SMel Gorman goto out; 26635515061dSMel Gorman 2664675becceSMel Gorman /* 2665675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 2666675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 2667675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 2668675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 2669675becceSMel Gorman * 2670675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 2671675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 2672675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 2673675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 2674675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 2675675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 2676675becceSMel Gorman * should make reasonable progress. 2677675becceSMel Gorman */ 2678675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 267917636faaSMichael S. Tsirkin gfp_zone(gfp_mask), nodemask) { 2680675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 2681675becceSMel Gorman continue; 2682675becceSMel Gorman 2683675becceSMel Gorman /* Throttle based on the first usable node */ 26845515061dSMel Gorman pgdat = zone->zone_pgdat; 26855515061dSMel Gorman if (pfmemalloc_watermark_ok(pgdat)) 268650694c28SMel Gorman goto out; 2687675becceSMel Gorman break; 2688675becceSMel Gorman } 2689675becceSMel Gorman 2690675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 2691675becceSMel Gorman if (!pgdat) 2692675becceSMel Gorman goto out; 26935515061dSMel Gorman 269468243e76SMel Gorman /* Account for the throttling */ 269568243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 269668243e76SMel Gorman 26975515061dSMel Gorman /* 26985515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 26995515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 27005515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 27015515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 27025515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 27035515061dSMel Gorman * second before continuing. 27045515061dSMel Gorman */ 27055515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 27065515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 27075515061dSMel Gorman pfmemalloc_watermark_ok(pgdat), HZ); 270850694c28SMel Gorman 270950694c28SMel Gorman goto check_pending; 27105515061dSMel Gorman } 27115515061dSMel Gorman 27125515061dSMel Gorman /* Throttle until kswapd wakes the process */ 27135515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 27145515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)); 271550694c28SMel Gorman 271650694c28SMel Gorman check_pending: 271750694c28SMel Gorman if (fatal_signal_pending(current)) 271850694c28SMel Gorman return true; 271950694c28SMel Gorman 272050694c28SMel Gorman out: 272150694c28SMel Gorman return false; 27225515061dSMel Gorman } 27235515061dSMel Gorman 2724dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 2725327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 272666e1707bSBalbir Singh { 272733906bc5SMel Gorman unsigned long nr_reclaimed; 272866e1707bSBalbir Singh struct scan_control sc = { 272922fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2730ee814fe2SJohannes Weiner .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), 2731ee814fe2SJohannes Weiner .order = order, 2732ee814fe2SJohannes Weiner .nodemask = nodemask, 2733ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 2734ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 2735a6dc60f8SJohannes Weiner .may_unmap = 1, 27362e2e4259SKOSAKI Motohiro .may_swap = 1, 273766e1707bSBalbir Singh }; 273866e1707bSBalbir Singh 27395515061dSMel Gorman /* 274050694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 274150694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 274250694c28SMel Gorman * point. 27435515061dSMel Gorman */ 274450694c28SMel Gorman if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask)) 27455515061dSMel Gorman return 1; 27465515061dSMel Gorman 274733906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 274833906bc5SMel Gorman sc.may_writepage, 274933906bc5SMel Gorman gfp_mask); 275033906bc5SMel Gorman 27513115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 275233906bc5SMel Gorman 275333906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 275433906bc5SMel Gorman 275533906bc5SMel Gorman return nr_reclaimed; 275666e1707bSBalbir Singh } 275766e1707bSBalbir Singh 2758c255a458SAndrew Morton #ifdef CONFIG_MEMCG 275966e1707bSBalbir Singh 276072835c86SJohannes Weiner unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg, 27614e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 27620ae5e89cSYing Han struct zone *zone, 27630ae5e89cSYing Han unsigned long *nr_scanned) 27644e416953SBalbir Singh { 27654e416953SBalbir Singh struct scan_control sc = { 2766b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2767ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 27684e416953SBalbir Singh .may_writepage = !laptop_mode, 27694e416953SBalbir Singh .may_unmap = 1, 27704e416953SBalbir Singh .may_swap = !noswap, 27714e416953SBalbir Singh }; 2772f9be23d6SKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 277302695175SJohannes Weiner int swappiness = mem_cgroup_swappiness(memcg); 27746b4f7799SJohannes Weiner unsigned long lru_pages; 27750ae5e89cSYing Han 27764e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 27774e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 2778bdce6d9eSKOSAKI Motohiro 27799e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 2780bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2781bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2782bdce6d9eSKOSAKI Motohiro 27834e416953SBalbir Singh /* 27844e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 27854e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 27864e416953SBalbir Singh * if we don't reclaim here, the shrink_zone from balance_pgdat 27874e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 27884e416953SBalbir Singh * the priority and make it zero. 27894e416953SBalbir Singh */ 27906b4f7799SJohannes Weiner shrink_lruvec(lruvec, swappiness, &sc, &lru_pages); 2791bdce6d9eSKOSAKI Motohiro 2792bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 2793bdce6d9eSKOSAKI Motohiro 27940ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 27954e416953SBalbir Singh return sc.nr_reclaimed; 27964e416953SBalbir Singh } 27974e416953SBalbir Singh 279872835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 2799b70a2a21SJohannes Weiner unsigned long nr_pages, 28008c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 2801b70a2a21SJohannes Weiner bool may_swap) 280266e1707bSBalbir Singh { 28034e416953SBalbir Singh struct zonelist *zonelist; 2804bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 2805889976dbSYing Han int nid; 280666e1707bSBalbir Singh struct scan_control sc = { 2807b70a2a21SJohannes Weiner .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 2808ee814fe2SJohannes Weiner .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 2809ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 2810ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 2811ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 281266e1707bSBalbir Singh .may_writepage = !laptop_mode, 2813a6dc60f8SJohannes Weiner .may_unmap = 1, 2814b70a2a21SJohannes Weiner .may_swap = may_swap, 2815a09ed5e0SYing Han }; 281666e1707bSBalbir Singh 2817889976dbSYing Han /* 2818889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 2819889976dbSYing Han * take care of from where we get pages. So the node where we start the 2820889976dbSYing Han * scan does not need to be the current node. 2821889976dbSYing Han */ 282272835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 2823889976dbSYing Han 2824889976dbSYing Han zonelist = NODE_DATA(nid)->node_zonelists; 2825bdce6d9eSKOSAKI Motohiro 2826bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 2827bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2828bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2829bdce6d9eSKOSAKI Motohiro 28303115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 2831bdce6d9eSKOSAKI Motohiro 2832bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 2833bdce6d9eSKOSAKI Motohiro 2834bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 283566e1707bSBalbir Singh } 283666e1707bSBalbir Singh #endif 283766e1707bSBalbir Singh 28389e3b2f8cSKonstantin Khlebnikov static void age_active_anon(struct zone *zone, struct scan_control *sc) 2839f16015fbSJohannes Weiner { 2840b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 2841b95a2f2dSJohannes Weiner 2842b95a2f2dSJohannes Weiner if (!total_swap_pages) 2843b95a2f2dSJohannes Weiner return; 2844b95a2f2dSJohannes Weiner 2845b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 2846b95a2f2dSJohannes Weiner do { 2847c56d5c7dSKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 2848f16015fbSJohannes Weiner 2849c56d5c7dSKonstantin Khlebnikov if (inactive_anon_is_low(lruvec)) 28501a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 28519e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 2852b95a2f2dSJohannes Weiner 2853b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 2854b95a2f2dSJohannes Weiner } while (memcg); 2855f16015fbSJohannes Weiner } 2856f16015fbSJohannes Weiner 285760cefed4SJohannes Weiner static bool zone_balanced(struct zone *zone, int order, 285860cefed4SJohannes Weiner unsigned long balance_gap, int classzone_idx) 285960cefed4SJohannes Weiner { 286060cefed4SJohannes Weiner if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone) + 286160cefed4SJohannes Weiner balance_gap, classzone_idx, 0)) 286260cefed4SJohannes Weiner return false; 286360cefed4SJohannes Weiner 2864ebff3980SVlastimil Babka if (IS_ENABLED(CONFIG_COMPACTION) && order && compaction_suitable(zone, 2865ebff3980SVlastimil Babka order, 0, classzone_idx) == COMPACT_SKIPPED) 286660cefed4SJohannes Weiner return false; 286760cefed4SJohannes Weiner 286860cefed4SJohannes Weiner return true; 286960cefed4SJohannes Weiner } 287060cefed4SJohannes Weiner 28711741c877SMel Gorman /* 28724ae0a48bSZlatko Calusic * pgdat_balanced() is used when checking if a node is balanced. 28734ae0a48bSZlatko Calusic * 28744ae0a48bSZlatko Calusic * For order-0, all zones must be balanced! 28754ae0a48bSZlatko Calusic * 28764ae0a48bSZlatko Calusic * For high-order allocations only zones that meet watermarks and are in a 28774ae0a48bSZlatko Calusic * zone allowed by the callers classzone_idx are added to balanced_pages. The 28784ae0a48bSZlatko Calusic * total of balanced pages must be at least 25% of the zones allowed by 28794ae0a48bSZlatko Calusic * classzone_idx for the node to be considered balanced. Forcing all zones to 28804ae0a48bSZlatko Calusic * be balanced for high orders can cause excessive reclaim when there are 28814ae0a48bSZlatko Calusic * imbalanced zones. 28821741c877SMel Gorman * The choice of 25% is due to 28831741c877SMel Gorman * o a 16M DMA zone that is balanced will not balance a zone on any 28841741c877SMel Gorman * reasonable sized machine 28851741c877SMel Gorman * o On all other machines, the top zone must be at least a reasonable 288625985edcSLucas De Marchi * percentage of the middle zones. For example, on 32-bit x86, highmem 28871741c877SMel Gorman * would need to be at least 256M for it to be balance a whole node. 28881741c877SMel Gorman * Similarly, on x86-64 the Normal zone would need to be at least 1G 28891741c877SMel Gorman * to balance a node on its own. These seemed like reasonable ratios. 28901741c877SMel Gorman */ 28914ae0a48bSZlatko Calusic static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx) 28921741c877SMel Gorman { 2893b40da049SJiang Liu unsigned long managed_pages = 0; 28944ae0a48bSZlatko Calusic unsigned long balanced_pages = 0; 28951741c877SMel Gorman int i; 28961741c877SMel Gorman 28974ae0a48bSZlatko Calusic /* Check the watermark levels */ 28984ae0a48bSZlatko Calusic for (i = 0; i <= classzone_idx; i++) { 28994ae0a48bSZlatko Calusic struct zone *zone = pgdat->node_zones + i; 29001741c877SMel Gorman 29014ae0a48bSZlatko Calusic if (!populated_zone(zone)) 29024ae0a48bSZlatko Calusic continue; 29034ae0a48bSZlatko Calusic 2904b40da049SJiang Liu managed_pages += zone->managed_pages; 29054ae0a48bSZlatko Calusic 29064ae0a48bSZlatko Calusic /* 29074ae0a48bSZlatko Calusic * A special case here: 29084ae0a48bSZlatko Calusic * 29094ae0a48bSZlatko Calusic * balance_pgdat() skips over all_unreclaimable after 29104ae0a48bSZlatko Calusic * DEF_PRIORITY. Effectively, it considers them balanced so 29114ae0a48bSZlatko Calusic * they must be considered balanced here as well! 29124ae0a48bSZlatko Calusic */ 29136e543d57SLisa Du if (!zone_reclaimable(zone)) { 2914b40da049SJiang Liu balanced_pages += zone->managed_pages; 29154ae0a48bSZlatko Calusic continue; 29164ae0a48bSZlatko Calusic } 29174ae0a48bSZlatko Calusic 29184ae0a48bSZlatko Calusic if (zone_balanced(zone, order, 0, i)) 2919b40da049SJiang Liu balanced_pages += zone->managed_pages; 29204ae0a48bSZlatko Calusic else if (!order) 29214ae0a48bSZlatko Calusic return false; 29224ae0a48bSZlatko Calusic } 29234ae0a48bSZlatko Calusic 29244ae0a48bSZlatko Calusic if (order) 2925b40da049SJiang Liu return balanced_pages >= (managed_pages >> 2); 29264ae0a48bSZlatko Calusic else 29274ae0a48bSZlatko Calusic return true; 29281741c877SMel Gorman } 29291741c877SMel Gorman 29305515061dSMel Gorman /* 29315515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 29325515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 29335515061dSMel Gorman * 29345515061dSMel Gorman * Returns true if kswapd is ready to sleep 29355515061dSMel Gorman */ 29365515061dSMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, long remaining, 2937dc83edd9SMel Gorman int classzone_idx) 2938f50de2d3SMel Gorman { 2939f50de2d3SMel Gorman /* If a direct reclaimer woke kswapd within HZ/10, it's premature */ 2940f50de2d3SMel Gorman if (remaining) 29415515061dSMel Gorman return false; 29425515061dSMel Gorman 29435515061dSMel Gorman /* 29449e5e3661SVlastimil Babka * The throttled processes are normally woken up in balance_pgdat() as 29459e5e3661SVlastimil Babka * soon as pfmemalloc_watermark_ok() is true. But there is a potential 29469e5e3661SVlastimil Babka * race between when kswapd checks the watermarks and a process gets 29479e5e3661SVlastimil Babka * throttled. There is also a potential race if processes get 29489e5e3661SVlastimil Babka * throttled, kswapd wakes, a large process exits thereby balancing the 29499e5e3661SVlastimil Babka * zones, which causes kswapd to exit balance_pgdat() before reaching 29509e5e3661SVlastimil Babka * the wake up checks. If kswapd is going to sleep, no process should 29519e5e3661SVlastimil Babka * be sleeping on pfmemalloc_wait, so wake them now if necessary. If 29529e5e3661SVlastimil Babka * the wake up is premature, processes will wake kswapd and get 29539e5e3661SVlastimil Babka * throttled again. The difference from wake ups in balance_pgdat() is 29549e5e3661SVlastimil Babka * that here we are under prepare_to_wait(). 29555515061dSMel Gorman */ 29569e5e3661SVlastimil Babka if (waitqueue_active(&pgdat->pfmemalloc_wait)) 29579e5e3661SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 2958f50de2d3SMel Gorman 29594ae0a48bSZlatko Calusic return pgdat_balanced(pgdat, order, classzone_idx); 2960f50de2d3SMel Gorman } 2961f50de2d3SMel Gorman 29621da177e4SLinus Torvalds /* 296375485363SMel Gorman * kswapd shrinks the zone by the number of pages required to reach 296475485363SMel Gorman * the high watermark. 2965b8e83b94SMel Gorman * 2966b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 2967283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 2968283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 296975485363SMel Gorman */ 2970b8e83b94SMel Gorman static bool kswapd_shrink_zone(struct zone *zone, 29717c954f6dSMel Gorman int classzone_idx, 297275485363SMel Gorman struct scan_control *sc, 29732ab44f43SMel Gorman unsigned long *nr_attempted) 297475485363SMel Gorman { 29757c954f6dSMel Gorman int testorder = sc->order; 29767c954f6dSMel Gorman unsigned long balance_gap; 29777c954f6dSMel Gorman bool lowmem_pressure; 297875485363SMel Gorman 297975485363SMel Gorman /* Reclaim above the high watermark. */ 298075485363SMel Gorman sc->nr_to_reclaim = max(SWAP_CLUSTER_MAX, high_wmark_pages(zone)); 29817c954f6dSMel Gorman 29827c954f6dSMel Gorman /* 29837c954f6dSMel Gorman * Kswapd reclaims only single pages with compaction enabled. Trying 29847c954f6dSMel Gorman * too hard to reclaim until contiguous free pages have become 29857c954f6dSMel Gorman * available can hurt performance by evicting too much useful data 29867c954f6dSMel Gorman * from memory. Do not reclaim more than needed for compaction. 29877c954f6dSMel Gorman */ 29887c954f6dSMel Gorman if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 2989ebff3980SVlastimil Babka compaction_suitable(zone, sc->order, 0, classzone_idx) 2990ebff3980SVlastimil Babka != COMPACT_SKIPPED) 29917c954f6dSMel Gorman testorder = 0; 29927c954f6dSMel Gorman 29937c954f6dSMel Gorman /* 29947c954f6dSMel Gorman * We put equal pressure on every zone, unless one zone has way too 29957c954f6dSMel Gorman * many pages free already. The "too many pages" is defined as the 29967c954f6dSMel Gorman * high wmark plus a "gap" where the gap is either the low 29977c954f6dSMel Gorman * watermark or 1% of the zone, whichever is smaller. 29987c954f6dSMel Gorman */ 29994be89a34SJianyu Zhan balance_gap = min(low_wmark_pages(zone), DIV_ROUND_UP( 30004be89a34SJianyu Zhan zone->managed_pages, KSWAPD_ZONE_BALANCE_GAP_RATIO)); 30017c954f6dSMel Gorman 30027c954f6dSMel Gorman /* 30037c954f6dSMel Gorman * If there is no low memory pressure or the zone is balanced then no 30047c954f6dSMel Gorman * reclaim is necessary 30057c954f6dSMel Gorman */ 30067c954f6dSMel Gorman lowmem_pressure = (buffer_heads_over_limit && is_highmem(zone)); 30077c954f6dSMel Gorman if (!lowmem_pressure && zone_balanced(zone, testorder, 30087c954f6dSMel Gorman balance_gap, classzone_idx)) 30097c954f6dSMel Gorman return true; 30107c954f6dSMel Gorman 30116b4f7799SJohannes Weiner shrink_zone(zone, sc, zone_idx(zone) == classzone_idx); 301275485363SMel Gorman 30132ab44f43SMel Gorman /* Account for the number of pages attempted to reclaim */ 30142ab44f43SMel Gorman *nr_attempted += sc->nr_to_reclaim; 30152ab44f43SMel Gorman 301657054651SJohannes Weiner clear_bit(ZONE_WRITEBACK, &zone->flags); 3017283aba9fSMel Gorman 30187c954f6dSMel Gorman /* 30197c954f6dSMel Gorman * If a zone reaches its high watermark, consider it to be no longer 30207c954f6dSMel Gorman * congested. It's possible there are dirty pages backed by congested 30217c954f6dSMel Gorman * BDIs but as pressure is relieved, speculatively avoid congestion 30227c954f6dSMel Gorman * waits. 30237c954f6dSMel Gorman */ 30246e543d57SLisa Du if (zone_reclaimable(zone) && 30257c954f6dSMel Gorman zone_balanced(zone, testorder, 0, classzone_idx)) { 302657054651SJohannes Weiner clear_bit(ZONE_CONGESTED, &zone->flags); 302757054651SJohannes Weiner clear_bit(ZONE_DIRTY, &zone->flags); 30287c954f6dSMel Gorman } 30297c954f6dSMel Gorman 3030b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 303175485363SMel Gorman } 303275485363SMel Gorman 303375485363SMel Gorman /* 30341da177e4SLinus Torvalds * For kswapd, balance_pgdat() will work across all this node's zones until 303541858966SMel Gorman * they are all at high_wmark_pages(zone). 30361da177e4SLinus Torvalds * 30370abdee2bSMel Gorman * Returns the final order kswapd was reclaiming at 30381da177e4SLinus Torvalds * 30391da177e4SLinus Torvalds * There is special handling here for zones which are full of pinned pages. 30401da177e4SLinus Torvalds * This can happen if the pages are all mlocked, or if they are all used by 30411da177e4SLinus Torvalds * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. 30421da177e4SLinus Torvalds * What we do is to detect the case where all pages in the zone have been 30431da177e4SLinus Torvalds * scanned twice and there has been zero successful reclaim. Mark the zone as 30441da177e4SLinus Torvalds * dead and from now on, only perform a short scan. Basically we're polling 30451da177e4SLinus Torvalds * the zone for when the problem goes away. 30461da177e4SLinus Torvalds * 30471da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 304841858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 304941858966SMel Gorman * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the 305041858966SMel Gorman * lower zones regardless of the number of free pages in the lower zones. This 305141858966SMel Gorman * interoperates with the page allocator fallback scheme to ensure that aging 305241858966SMel Gorman * of pages is balanced across the zones. 30531da177e4SLinus Torvalds */ 305499504748SMel Gorman static unsigned long balance_pgdat(pg_data_t *pgdat, int order, 3055dc83edd9SMel Gorman int *classzone_idx) 30561da177e4SLinus Torvalds { 30571da177e4SLinus Torvalds int i; 305899504748SMel Gorman int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ 30590608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 30600608f43dSAndrew Morton unsigned long nr_soft_scanned; 3061179e9639SAndrew Morton struct scan_control sc = { 3062179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 3063ee814fe2SJohannes Weiner .order = order, 3064b8e83b94SMel Gorman .priority = DEF_PRIORITY, 3065ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3066a6dc60f8SJohannes Weiner .may_unmap = 1, 30672e2e4259SKOSAKI Motohiro .may_swap = 1, 3068179e9639SAndrew Morton }; 3069f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 30701da177e4SLinus Torvalds 30719e3b2f8cSKonstantin Khlebnikov do { 30722ab44f43SMel Gorman unsigned long nr_attempted = 0; 3073b8e83b94SMel Gorman bool raise_priority = true; 30742ab44f43SMel Gorman bool pgdat_needs_compaction = (order > 0); 3075b8e83b94SMel Gorman 3076b8e83b94SMel Gorman sc.nr_reclaimed = 0; 30771da177e4SLinus Torvalds 30781da177e4SLinus Torvalds /* 30791da177e4SLinus Torvalds * Scan in the highmem->dma direction for the highest 30801da177e4SLinus Torvalds * zone which needs scanning 30811da177e4SLinus Torvalds */ 30821da177e4SLinus Torvalds for (i = pgdat->nr_zones - 1; i >= 0; i--) { 30831da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 30841da177e4SLinus Torvalds 3085f3fe6512SCon Kolivas if (!populated_zone(zone)) 30861da177e4SLinus Torvalds continue; 30871da177e4SLinus Torvalds 30886e543d57SLisa Du if (sc.priority != DEF_PRIORITY && 30896e543d57SLisa Du !zone_reclaimable(zone)) 30901da177e4SLinus Torvalds continue; 30911da177e4SLinus Torvalds 3092556adecbSRik van Riel /* 3093556adecbSRik van Riel * Do some background aging of the anon list, to give 3094556adecbSRik van Riel * pages a chance to be referenced before reclaiming. 3095556adecbSRik van Riel */ 30969e3b2f8cSKonstantin Khlebnikov age_active_anon(zone, &sc); 3097556adecbSRik van Riel 3098cc715d99SMel Gorman /* 3099cc715d99SMel Gorman * If the number of buffer_heads in the machine 3100cc715d99SMel Gorman * exceeds the maximum allowed level and this node 3101cc715d99SMel Gorman * has a highmem zone, force kswapd to reclaim from 3102cc715d99SMel Gorman * it to relieve lowmem pressure. 3103cc715d99SMel Gorman */ 3104cc715d99SMel Gorman if (buffer_heads_over_limit && is_highmem_idx(i)) { 3105cc715d99SMel Gorman end_zone = i; 3106cc715d99SMel Gorman break; 3107cc715d99SMel Gorman } 3108cc715d99SMel Gorman 310960cefed4SJohannes Weiner if (!zone_balanced(zone, order, 0, 0)) { 31101da177e4SLinus Torvalds end_zone = i; 3111e1dbeda6SAndrew Morton break; 3112439423f6SShaohua Li } else { 3113d43006d5SMel Gorman /* 3114d43006d5SMel Gorman * If balanced, clear the dirty and congested 3115d43006d5SMel Gorman * flags 3116d43006d5SMel Gorman */ 311757054651SJohannes Weiner clear_bit(ZONE_CONGESTED, &zone->flags); 311857054651SJohannes Weiner clear_bit(ZONE_DIRTY, &zone->flags); 31191da177e4SLinus Torvalds } 31201da177e4SLinus Torvalds } 3121dafcb73eSZlatko Calusic 3122b8e83b94SMel Gorman if (i < 0) 31231da177e4SLinus Torvalds goto out; 3124e1dbeda6SAndrew Morton 31251da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 31261da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 31271da177e4SLinus Torvalds 31282ab44f43SMel Gorman if (!populated_zone(zone)) 31292ab44f43SMel Gorman continue; 31302ab44f43SMel Gorman 31312ab44f43SMel Gorman /* 31322ab44f43SMel Gorman * If any zone is currently balanced then kswapd will 31332ab44f43SMel Gorman * not call compaction as it is expected that the 31342ab44f43SMel Gorman * necessary pages are already available. 31352ab44f43SMel Gorman */ 31362ab44f43SMel Gorman if (pgdat_needs_compaction && 31372ab44f43SMel Gorman zone_watermark_ok(zone, order, 31382ab44f43SMel Gorman low_wmark_pages(zone), 31392ab44f43SMel Gorman *classzone_idx, 0)) 31402ab44f43SMel Gorman pgdat_needs_compaction = false; 31411da177e4SLinus Torvalds } 31421da177e4SLinus Torvalds 31431da177e4SLinus Torvalds /* 3144b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 3145b7ea3c41SMel Gorman * even in laptop mode. 3146b7ea3c41SMel Gorman */ 3147b7ea3c41SMel Gorman if (sc.priority < DEF_PRIORITY - 2) 3148b7ea3c41SMel Gorman sc.may_writepage = 1; 3149b7ea3c41SMel Gorman 3150b7ea3c41SMel Gorman /* 31511da177e4SLinus Torvalds * Now scan the zone in the dma->highmem direction, stopping 31521da177e4SLinus Torvalds * at the last zone which needs scanning. 31531da177e4SLinus Torvalds * 31541da177e4SLinus Torvalds * We do this because the page allocator works in the opposite 31551da177e4SLinus Torvalds * direction. This prevents the page allocator from allocating 31561da177e4SLinus Torvalds * pages behind kswapd's direction of progress, which would 31571da177e4SLinus Torvalds * cause too much scanning of the lower zones. 31581da177e4SLinus Torvalds */ 31591da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 31601da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 31611da177e4SLinus Torvalds 3162f3fe6512SCon Kolivas if (!populated_zone(zone)) 31631da177e4SLinus Torvalds continue; 31641da177e4SLinus Torvalds 31656e543d57SLisa Du if (sc.priority != DEF_PRIORITY && 31666e543d57SLisa Du !zone_reclaimable(zone)) 31671da177e4SLinus Torvalds continue; 31681da177e4SLinus Torvalds 31691da177e4SLinus Torvalds sc.nr_scanned = 0; 31704e416953SBalbir Singh 31710608f43dSAndrew Morton nr_soft_scanned = 0; 31720608f43dSAndrew Morton /* 31730608f43dSAndrew Morton * Call soft limit reclaim before calling shrink_zone. 31740608f43dSAndrew Morton */ 31750608f43dSAndrew Morton nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 31760608f43dSAndrew Morton order, sc.gfp_mask, 31770608f43dSAndrew Morton &nr_soft_scanned); 31780608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 31790608f43dSAndrew Morton 318032a4330dSRik van Riel /* 31817c954f6dSMel Gorman * There should be no need to raise the scanning 31827c954f6dSMel Gorman * priority if enough pages are already being scanned 31837c954f6dSMel Gorman * that that high watermark would be met at 100% 31847c954f6dSMel Gorman * efficiency. 318532a4330dSRik van Riel */ 31866b4f7799SJohannes Weiner if (kswapd_shrink_zone(zone, end_zone, 31876b4f7799SJohannes Weiner &sc, &nr_attempted)) 3188b8e83b94SMel Gorman raise_priority = false; 3189b8e83b94SMel Gorman } 3190d7868daeSMel Gorman 31915515061dSMel Gorman /* 31925515061dSMel Gorman * If the low watermark is met there is no need for processes 31935515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 31945515061dSMel Gorman * able to safely make forward progress. Wake them 31955515061dSMel Gorman */ 31965515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 31975515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)) 3198cfc51155SVlastimil Babka wake_up_all(&pgdat->pfmemalloc_wait); 31995515061dSMel Gorman 32001da177e4SLinus Torvalds /* 3201b8e83b94SMel Gorman * Fragmentation may mean that the system cannot be rebalanced 3202b8e83b94SMel Gorman * for high-order allocations in all zones. If twice the 3203b8e83b94SMel Gorman * allocation size has been reclaimed and the zones are still 3204b8e83b94SMel Gorman * not balanced then recheck the watermarks at order-0 to 3205b8e83b94SMel Gorman * prevent kswapd reclaiming excessively. Assume that a 3206b8e83b94SMel Gorman * process requested a high-order can direct reclaim/compact. 32071da177e4SLinus Torvalds */ 3208b8e83b94SMel Gorman if (order && sc.nr_reclaimed >= 2UL << order) 320973ce02e9SKOSAKI Motohiro order = sc.order = 0; 321073ce02e9SKOSAKI Motohiro 3211b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 3212b8e83b94SMel Gorman if (try_to_freeze() || kthread_should_stop()) 3213b8e83b94SMel Gorman break; 3214b8e83b94SMel Gorman 3215b8e83b94SMel Gorman /* 32162ab44f43SMel Gorman * Compact if necessary and kswapd is reclaiming at least the 32172ab44f43SMel Gorman * high watermark number of pages as requsted 32182ab44f43SMel Gorman */ 32192ab44f43SMel Gorman if (pgdat_needs_compaction && sc.nr_reclaimed > nr_attempted) 32202ab44f43SMel Gorman compact_pgdat(pgdat, order); 32212ab44f43SMel Gorman 32222ab44f43SMel Gorman /* 3223b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3224b8e83b94SMel Gorman * progress in reclaiming pages 3225b8e83b94SMel Gorman */ 3226b8e83b94SMel Gorman if (raise_priority || !sc.nr_reclaimed) 3227b8e83b94SMel Gorman sc.priority--; 32289aa41348SMel Gorman } while (sc.priority >= 1 && 3229b8e83b94SMel Gorman !pgdat_balanced(pgdat, order, *classzone_idx)); 32301da177e4SLinus Torvalds 3231b8e83b94SMel Gorman out: 32320abdee2bSMel Gorman /* 32335515061dSMel Gorman * Return the order we were reclaiming at so prepare_kswapd_sleep() 32340abdee2bSMel Gorman * makes a decision on the order we were last reclaiming at. However, 32350abdee2bSMel Gorman * if another caller entered the allocator slow path while kswapd 32360abdee2bSMel Gorman * was awake, order will remain at the higher level 32370abdee2bSMel Gorman */ 3238dc83edd9SMel Gorman *classzone_idx = end_zone; 32390abdee2bSMel Gorman return order; 32401da177e4SLinus Torvalds } 32411da177e4SLinus Torvalds 3242dc83edd9SMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx) 3243f0bc0a60SKOSAKI Motohiro { 3244f0bc0a60SKOSAKI Motohiro long remaining = 0; 3245f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3246f0bc0a60SKOSAKI Motohiro 3247f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3248f0bc0a60SKOSAKI Motohiro return; 3249f0bc0a60SKOSAKI Motohiro 3250f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3251f0bc0a60SKOSAKI Motohiro 3252f0bc0a60SKOSAKI Motohiro /* Try to sleep for a short interval */ 32535515061dSMel Gorman if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) { 3254f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 3255f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3256f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3257f0bc0a60SKOSAKI Motohiro } 3258f0bc0a60SKOSAKI Motohiro 3259f0bc0a60SKOSAKI Motohiro /* 3260f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3261f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3262f0bc0a60SKOSAKI Motohiro */ 32635515061dSMel Gorman if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) { 3264f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3265f0bc0a60SKOSAKI Motohiro 3266f0bc0a60SKOSAKI Motohiro /* 3267f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3268f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3269f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3270f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3271f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3272f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3273f0bc0a60SKOSAKI Motohiro */ 3274f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 32751c7e7f6cSAaditya Kumar 327662997027SMel Gorman /* 327762997027SMel Gorman * Compaction records what page blocks it recently failed to 327862997027SMel Gorman * isolate pages from and skips them in the future scanning. 327962997027SMel Gorman * When kswapd is going to sleep, it is reasonable to assume 328062997027SMel Gorman * that pages and compaction may succeed so reset the cache. 328162997027SMel Gorman */ 328262997027SMel Gorman reset_isolation_suitable(pgdat); 328362997027SMel Gorman 32841c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3285f0bc0a60SKOSAKI Motohiro schedule(); 32861c7e7f6cSAaditya Kumar 3287f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3288f0bc0a60SKOSAKI Motohiro } else { 3289f0bc0a60SKOSAKI Motohiro if (remaining) 3290f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3291f0bc0a60SKOSAKI Motohiro else 3292f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3293f0bc0a60SKOSAKI Motohiro } 3294f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3295f0bc0a60SKOSAKI Motohiro } 3296f0bc0a60SKOSAKI Motohiro 32971da177e4SLinus Torvalds /* 32981da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 32991da177e4SLinus Torvalds * from the init process. 33001da177e4SLinus Torvalds * 33011da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 33021da177e4SLinus Torvalds * free memory available even if there is no other activity 33031da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 33041da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 33051da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 33061da177e4SLinus Torvalds * 33071da177e4SLinus Torvalds * If there are applications that are active memory-allocators 33081da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 33091da177e4SLinus Torvalds */ 33101da177e4SLinus Torvalds static int kswapd(void *p) 33111da177e4SLinus Torvalds { 3312215ddd66SMel Gorman unsigned long order, new_order; 3313d2ebd0f6SAlex,Shi unsigned balanced_order; 3314215ddd66SMel Gorman int classzone_idx, new_classzone_idx; 3315d2ebd0f6SAlex,Shi int balanced_classzone_idx; 33161da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 33171da177e4SLinus Torvalds struct task_struct *tsk = current; 3318f0bc0a60SKOSAKI Motohiro 33191da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 33201da177e4SLinus Torvalds .reclaimed_slab = 0, 33211da177e4SLinus Torvalds }; 3322a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 33231da177e4SLinus Torvalds 3324cf40bd16SNick Piggin lockdep_set_current_reclaim_state(GFP_KERNEL); 3325cf40bd16SNick Piggin 3326174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3327c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 33281da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 33291da177e4SLinus Torvalds 33301da177e4SLinus Torvalds /* 33311da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 33321da177e4SLinus Torvalds * and that if we need more memory we should get access to it 33331da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 33341da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 33351da177e4SLinus Torvalds * 33361da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 33371da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 33381da177e4SLinus Torvalds * page out something else, and this flag essentially protects 33391da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 33401da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 33411da177e4SLinus Torvalds */ 3342930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 334383144186SRafael J. Wysocki set_freezable(); 33441da177e4SLinus Torvalds 3345215ddd66SMel Gorman order = new_order = 0; 3346d2ebd0f6SAlex,Shi balanced_order = 0; 3347215ddd66SMel Gorman classzone_idx = new_classzone_idx = pgdat->nr_zones - 1; 3348d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 33491da177e4SLinus Torvalds for ( ; ; ) { 33506f6313d4SJeff Liu bool ret; 33513e1d1d28SChristoph Lameter 3352215ddd66SMel Gorman /* 3353215ddd66SMel Gorman * If the last balance_pgdat was unsuccessful it's unlikely a 3354215ddd66SMel Gorman * new request of a similar or harder type will succeed soon 3355215ddd66SMel Gorman * so consider going to sleep on the basis we reclaimed at 3356215ddd66SMel Gorman */ 3357d2ebd0f6SAlex,Shi if (balanced_classzone_idx >= new_classzone_idx && 3358d2ebd0f6SAlex,Shi balanced_order == new_order) { 33591da177e4SLinus Torvalds new_order = pgdat->kswapd_max_order; 336099504748SMel Gorman new_classzone_idx = pgdat->classzone_idx; 33611da177e4SLinus Torvalds pgdat->kswapd_max_order = 0; 3362215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 3363215ddd66SMel Gorman } 3364215ddd66SMel Gorman 336599504748SMel Gorman if (order < new_order || classzone_idx > new_classzone_idx) { 33661da177e4SLinus Torvalds /* 33671da177e4SLinus Torvalds * Don't sleep if someone wants a larger 'order' 336899504748SMel Gorman * allocation or has tigher zone constraints 33691da177e4SLinus Torvalds */ 33701da177e4SLinus Torvalds order = new_order; 337199504748SMel Gorman classzone_idx = new_classzone_idx; 33721da177e4SLinus Torvalds } else { 3373d2ebd0f6SAlex,Shi kswapd_try_to_sleep(pgdat, balanced_order, 3374d2ebd0f6SAlex,Shi balanced_classzone_idx); 33751da177e4SLinus Torvalds order = pgdat->kswapd_max_order; 337699504748SMel Gorman classzone_idx = pgdat->classzone_idx; 3377f0dfcde0SAlex,Shi new_order = order; 3378f0dfcde0SAlex,Shi new_classzone_idx = classzone_idx; 33794d40502eSMel Gorman pgdat->kswapd_max_order = 0; 3380215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 33811da177e4SLinus Torvalds } 33821da177e4SLinus Torvalds 33838fe23e05SDavid Rientjes ret = try_to_freeze(); 33848fe23e05SDavid Rientjes if (kthread_should_stop()) 33858fe23e05SDavid Rientjes break; 33868fe23e05SDavid Rientjes 33878fe23e05SDavid Rientjes /* 33888fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 33898fe23e05SDavid Rientjes * after returning from the refrigerator 3390b1296cc4SRafael J. Wysocki */ 339133906bc5SMel Gorman if (!ret) { 339233906bc5SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, order); 3393d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 3394d2ebd0f6SAlex,Shi balanced_order = balance_pgdat(pgdat, order, 3395d2ebd0f6SAlex,Shi &balanced_classzone_idx); 33961da177e4SLinus Torvalds } 339733906bc5SMel Gorman } 3398b0a8cc58STakamori Yamaguchi 339971abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 3400b0a8cc58STakamori Yamaguchi current->reclaim_state = NULL; 340171abdc15SJohannes Weiner lockdep_clear_current_reclaim_state(); 340271abdc15SJohannes Weiner 34031da177e4SLinus Torvalds return 0; 34041da177e4SLinus Torvalds } 34051da177e4SLinus Torvalds 34061da177e4SLinus Torvalds /* 34071da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 34081da177e4SLinus Torvalds */ 340999504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) 34101da177e4SLinus Torvalds { 34111da177e4SLinus Torvalds pg_data_t *pgdat; 34121da177e4SLinus Torvalds 3413f3fe6512SCon Kolivas if (!populated_zone(zone)) 34141da177e4SLinus Torvalds return; 34151da177e4SLinus Torvalds 3416344736f2SVladimir Davydov if (!cpuset_zone_allowed(zone, GFP_KERNEL | __GFP_HARDWALL)) 34171da177e4SLinus Torvalds return; 341888f5acf8SMel Gorman pgdat = zone->zone_pgdat; 341999504748SMel Gorman if (pgdat->kswapd_max_order < order) { 342088f5acf8SMel Gorman pgdat->kswapd_max_order = order; 342199504748SMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx); 342299504748SMel Gorman } 34238d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 34241da177e4SLinus Torvalds return; 3425892f795dSJohannes Weiner if (zone_balanced(zone, order, 0, 0)) 342688f5acf8SMel Gorman return; 342788f5acf8SMel Gorman 342888f5acf8SMel Gorman trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order); 34298d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 34301da177e4SLinus Torvalds } 34311da177e4SLinus Torvalds 3432c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 34331da177e4SLinus Torvalds /* 34347b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3435d6277db4SRafael J. Wysocki * freed pages. 3436d6277db4SRafael J. Wysocki * 3437d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3438d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3439d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 34401da177e4SLinus Torvalds */ 34417b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 34421da177e4SLinus Torvalds { 3443d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 3444d6277db4SRafael J. Wysocki struct scan_control sc = { 34457b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 3446ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 34479e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 3448ee814fe2SJohannes Weiner .may_writepage = 1, 3449ee814fe2SJohannes Weiner .may_unmap = 1, 3450ee814fe2SJohannes Weiner .may_swap = 1, 3451ee814fe2SJohannes Weiner .hibernation_mode = 1, 34521da177e4SLinus Torvalds }; 34537b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 34547b51755cSKOSAKI Motohiro struct task_struct *p = current; 34557b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 34561da177e4SLinus Torvalds 34577b51755cSKOSAKI Motohiro p->flags |= PF_MEMALLOC; 34587b51755cSKOSAKI Motohiro lockdep_set_current_reclaim_state(sc.gfp_mask); 3459d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 34607b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 3461d6277db4SRafael J. Wysocki 34623115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3463d6277db4SRafael J. Wysocki 34647b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 34657b51755cSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 34667b51755cSKOSAKI Motohiro p->flags &= ~PF_MEMALLOC; 3467d6277db4SRafael J. Wysocki 34687b51755cSKOSAKI Motohiro return nr_reclaimed; 34691da177e4SLinus Torvalds } 3470c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 34711da177e4SLinus Torvalds 34721da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 34731da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 34741da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 34751da177e4SLinus Torvalds restore their cpu bindings. */ 3476fcb35a9bSGreg Kroah-Hartman static int cpu_callback(struct notifier_block *nfb, unsigned long action, 3477fcb35a9bSGreg Kroah-Hartman void *hcpu) 34781da177e4SLinus Torvalds { 347958c0a4a7SYasunori Goto int nid; 34801da177e4SLinus Torvalds 34818bb78442SRafael J. Wysocki if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 348248fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 3483c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3484a70f7302SRusty Russell const struct cpumask *mask; 3485a70f7302SRusty Russell 3486a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3487c5f59f08SMike Travis 34883e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 34891da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3490c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 34911da177e4SLinus Torvalds } 34921da177e4SLinus Torvalds } 34931da177e4SLinus Torvalds return NOTIFY_OK; 34941da177e4SLinus Torvalds } 34951da177e4SLinus Torvalds 34963218ae14SYasunori Goto /* 34973218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 34983218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 34993218ae14SYasunori Goto */ 35003218ae14SYasunori Goto int kswapd_run(int nid) 35013218ae14SYasunori Goto { 35023218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 35033218ae14SYasunori Goto int ret = 0; 35043218ae14SYasunori Goto 35053218ae14SYasunori Goto if (pgdat->kswapd) 35063218ae14SYasunori Goto return 0; 35073218ae14SYasunori Goto 35083218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 35093218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 35103218ae14SYasunori Goto /* failure at boot is fatal */ 35113218ae14SYasunori Goto BUG_ON(system_state == SYSTEM_BOOTING); 3512d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 3513d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 3514d72515b8SXishi Qiu pgdat->kswapd = NULL; 35153218ae14SYasunori Goto } 35163218ae14SYasunori Goto return ret; 35173218ae14SYasunori Goto } 35183218ae14SYasunori Goto 35198fe23e05SDavid Rientjes /* 3520d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 3521bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 35228fe23e05SDavid Rientjes */ 35238fe23e05SDavid Rientjes void kswapd_stop(int nid) 35248fe23e05SDavid Rientjes { 35258fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 35268fe23e05SDavid Rientjes 3527d8adde17SJiang Liu if (kswapd) { 35288fe23e05SDavid Rientjes kthread_stop(kswapd); 3529d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 3530d8adde17SJiang Liu } 35318fe23e05SDavid Rientjes } 35328fe23e05SDavid Rientjes 35331da177e4SLinus Torvalds static int __init kswapd_init(void) 35341da177e4SLinus Torvalds { 35353218ae14SYasunori Goto int nid; 353669e05944SAndrew Morton 35371da177e4SLinus Torvalds swap_setup(); 353848fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 35393218ae14SYasunori Goto kswapd_run(nid); 35401da177e4SLinus Torvalds hotcpu_notifier(cpu_callback, 0); 35411da177e4SLinus Torvalds return 0; 35421da177e4SLinus Torvalds } 35431da177e4SLinus Torvalds 35441da177e4SLinus Torvalds module_init(kswapd_init) 35459eeff239SChristoph Lameter 35469eeff239SChristoph Lameter #ifdef CONFIG_NUMA 35479eeff239SChristoph Lameter /* 35489eeff239SChristoph Lameter * Zone reclaim mode 35499eeff239SChristoph Lameter * 35509eeff239SChristoph Lameter * If non-zero call zone_reclaim when the number of free pages falls below 35519eeff239SChristoph Lameter * the watermarks. 35529eeff239SChristoph Lameter */ 35539eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly; 35549eeff239SChristoph Lameter 35551b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 35567d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 35571b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 35581b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ 35591b2ffb78SChristoph Lameter 35609eeff239SChristoph Lameter /* 3561a92f7126SChristoph Lameter * Priority for ZONE_RECLAIM. This determines the fraction of pages 3562a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3563a92f7126SChristoph Lameter * a zone. 3564a92f7126SChristoph Lameter */ 3565a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4 3566a92f7126SChristoph Lameter 35679eeff239SChristoph Lameter /* 35689614634fSChristoph Lameter * Percentage of pages in a zone that must be unmapped for zone_reclaim to 35699614634fSChristoph Lameter * occur. 35709614634fSChristoph Lameter */ 35719614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 35729614634fSChristoph Lameter 35739614634fSChristoph Lameter /* 35740ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 35750ff38490SChristoph Lameter * slab reclaim needs to occur. 35760ff38490SChristoph Lameter */ 35770ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 35780ff38490SChristoph Lameter 357990afa5deSMel Gorman static inline unsigned long zone_unmapped_file_pages(struct zone *zone) 358090afa5deSMel Gorman { 358190afa5deSMel Gorman unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED); 358290afa5deSMel Gorman unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) + 358390afa5deSMel Gorman zone_page_state(zone, NR_ACTIVE_FILE); 358490afa5deSMel Gorman 358590afa5deSMel Gorman /* 358690afa5deSMel Gorman * It's possible for there to be more file mapped pages than 358790afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 358890afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 358990afa5deSMel Gorman */ 359090afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 359190afa5deSMel Gorman } 359290afa5deSMel Gorman 359390afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 359490afa5deSMel Gorman static long zone_pagecache_reclaimable(struct zone *zone) 359590afa5deSMel Gorman { 359690afa5deSMel Gorman long nr_pagecache_reclaimable; 359790afa5deSMel Gorman long delta = 0; 359890afa5deSMel Gorman 359990afa5deSMel Gorman /* 360090afa5deSMel Gorman * If RECLAIM_SWAP is set, then all file pages are considered 360190afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 360290afa5deSMel Gorman * pages like swapcache and zone_unmapped_file_pages() provides 360390afa5deSMel Gorman * a better estimate 360490afa5deSMel Gorman */ 360590afa5deSMel Gorman if (zone_reclaim_mode & RECLAIM_SWAP) 360690afa5deSMel Gorman nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES); 360790afa5deSMel Gorman else 360890afa5deSMel Gorman nr_pagecache_reclaimable = zone_unmapped_file_pages(zone); 360990afa5deSMel Gorman 361090afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 361190afa5deSMel Gorman if (!(zone_reclaim_mode & RECLAIM_WRITE)) 361290afa5deSMel Gorman delta += zone_page_state(zone, NR_FILE_DIRTY); 361390afa5deSMel Gorman 361490afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 361590afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 361690afa5deSMel Gorman delta = nr_pagecache_reclaimable; 361790afa5deSMel Gorman 361890afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 361990afa5deSMel Gorman } 362090afa5deSMel Gorman 36210ff38490SChristoph Lameter /* 36229eeff239SChristoph Lameter * Try to free up some pages from this zone through reclaim. 36239eeff239SChristoph Lameter */ 3624179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 36259eeff239SChristoph Lameter { 36267fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 362769e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 36289eeff239SChristoph Lameter struct task_struct *p = current; 36299eeff239SChristoph Lameter struct reclaim_state reclaim_state; 3630179e9639SAndrew Morton struct scan_control sc = { 363162b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 363221caf2fcSMing Lei .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), 3633bd2f6199SJohannes Weiner .order = order, 36349e3b2f8cSKonstantin Khlebnikov .priority = ZONE_RECLAIM_PRIORITY, 3635ee814fe2SJohannes Weiner .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), 3636ee814fe2SJohannes Weiner .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP), 3637ee814fe2SJohannes Weiner .may_swap = 1, 3638179e9639SAndrew Morton }; 36399eeff239SChristoph Lameter 36409eeff239SChristoph Lameter cond_resched(); 3641d4f7796eSChristoph Lameter /* 3642d4f7796eSChristoph Lameter * We need to be able to allocate from the reserves for RECLAIM_SWAP 3643d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 3644d4f7796eSChristoph Lameter * and RECLAIM_SWAP. 3645d4f7796eSChristoph Lameter */ 3646d4f7796eSChristoph Lameter p->flags |= PF_MEMALLOC | PF_SWAPWRITE; 364776ca542dSKOSAKI Motohiro lockdep_set_current_reclaim_state(gfp_mask); 36489eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 36499eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 3650c84db23cSChristoph Lameter 365190afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) { 3652a92f7126SChristoph Lameter /* 36530ff38490SChristoph Lameter * Free memory by calling shrink zone with increasing 36540ff38490SChristoph Lameter * priorities until we have enough memory freed. 3655a92f7126SChristoph Lameter */ 3656a92f7126SChristoph Lameter do { 36576b4f7799SJohannes Weiner shrink_zone(zone, &sc, true); 36589e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 36590ff38490SChristoph Lameter } 3660a92f7126SChristoph Lameter 36619eeff239SChristoph Lameter p->reclaim_state = NULL; 3662d4f7796eSChristoph Lameter current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); 366376ca542dSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 3664a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 36659eeff239SChristoph Lameter } 3666179e9639SAndrew Morton 3667179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 3668179e9639SAndrew Morton { 3669179e9639SAndrew Morton int node_id; 3670d773ed6bSDavid Rientjes int ret; 3671179e9639SAndrew Morton 3672179e9639SAndrew Morton /* 36730ff38490SChristoph Lameter * Zone reclaim reclaims unmapped file backed pages and 36740ff38490SChristoph Lameter * slab pages if we are over the defined limits. 367534aa1330SChristoph Lameter * 36769614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 36779614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 36789614634fSChristoph Lameter * thrown out if the zone is overallocated. So we do not reclaim 36799614634fSChristoph Lameter * if less than a specified percentage of the zone is used by 36809614634fSChristoph Lameter * unmapped file backed pages. 3681179e9639SAndrew Morton */ 368290afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages && 368390afa5deSMel Gorman zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages) 3684fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3685179e9639SAndrew Morton 36866e543d57SLisa Du if (!zone_reclaimable(zone)) 3687fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3688d773ed6bSDavid Rientjes 3689179e9639SAndrew Morton /* 3690d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 3691179e9639SAndrew Morton */ 3692d773ed6bSDavid Rientjes if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC)) 3693fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3694179e9639SAndrew Morton 3695179e9639SAndrew Morton /* 3696179e9639SAndrew Morton * Only run zone reclaim on the local zone or on zones that do not 3697179e9639SAndrew Morton * have associated processors. This will favor the local processor 3698179e9639SAndrew Morton * over remote processors and spread off node memory allocations 3699179e9639SAndrew Morton * as wide as possible. 3700179e9639SAndrew Morton */ 370189fa3024SChristoph Lameter node_id = zone_to_nid(zone); 370237c0708dSChristoph Lameter if (node_state(node_id, N_CPU) && node_id != numa_node_id()) 3703fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3704d773ed6bSDavid Rientjes 370557054651SJohannes Weiner if (test_and_set_bit(ZONE_RECLAIM_LOCKED, &zone->flags)) 3706fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3707fa5e084eSMel Gorman 3708d773ed6bSDavid Rientjes ret = __zone_reclaim(zone, gfp_mask, order); 370957054651SJohannes Weiner clear_bit(ZONE_RECLAIM_LOCKED, &zone->flags); 3710d773ed6bSDavid Rientjes 371124cf7251SMel Gorman if (!ret) 371224cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 371324cf7251SMel Gorman 3714d773ed6bSDavid Rientjes return ret; 3715179e9639SAndrew Morton } 37169eeff239SChristoph Lameter #endif 3717894bc310SLee Schermerhorn 3718894bc310SLee Schermerhorn /* 3719894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 3720894bc310SLee Schermerhorn * @page: the page to test 3721894bc310SLee Schermerhorn * 3722894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 372339b5f29aSHugh Dickins * lists vs unevictable list. 3724894bc310SLee Schermerhorn * 3725894bc310SLee Schermerhorn * Reasons page might not be evictable: 3726ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 3727b291f000SNick Piggin * (2) page is part of an mlocked VMA 3728ba9ddf49SLee Schermerhorn * 3729894bc310SLee Schermerhorn */ 373039b5f29aSHugh Dickins int page_evictable(struct page *page) 3731894bc310SLee Schermerhorn { 373239b5f29aSHugh Dickins return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); 3733894bc310SLee Schermerhorn } 373489e004eaSLee Schermerhorn 373585046579SHugh Dickins #ifdef CONFIG_SHMEM 373689e004eaSLee Schermerhorn /** 373724513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 373824513264SHugh Dickins * @pages: array of pages to check 373924513264SHugh Dickins * @nr_pages: number of pages to check 374089e004eaSLee Schermerhorn * 374124513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 374285046579SHugh Dickins * 374385046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 374489e004eaSLee Schermerhorn */ 374524513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 374689e004eaSLee Schermerhorn { 3747925b7673SJohannes Weiner struct lruvec *lruvec; 374824513264SHugh Dickins struct zone *zone = NULL; 374924513264SHugh Dickins int pgscanned = 0; 375024513264SHugh Dickins int pgrescued = 0; 375189e004eaSLee Schermerhorn int i; 375289e004eaSLee Schermerhorn 375324513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 375424513264SHugh Dickins struct page *page = pages[i]; 375524513264SHugh Dickins struct zone *pagezone; 375689e004eaSLee Schermerhorn 375724513264SHugh Dickins pgscanned++; 375824513264SHugh Dickins pagezone = page_zone(page); 375989e004eaSLee Schermerhorn if (pagezone != zone) { 376089e004eaSLee Schermerhorn if (zone) 376189e004eaSLee Schermerhorn spin_unlock_irq(&zone->lru_lock); 376289e004eaSLee Schermerhorn zone = pagezone; 376389e004eaSLee Schermerhorn spin_lock_irq(&zone->lru_lock); 376489e004eaSLee Schermerhorn } 3765fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 376689e004eaSLee Schermerhorn 376724513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 376824513264SHugh Dickins continue; 376989e004eaSLee Schermerhorn 377039b5f29aSHugh Dickins if (page_evictable(page)) { 377124513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 377224513264SHugh Dickins 3773309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 377424513264SHugh Dickins ClearPageUnevictable(page); 3775fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 3776fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 377724513264SHugh Dickins pgrescued++; 377889e004eaSLee Schermerhorn } 377989e004eaSLee Schermerhorn } 378024513264SHugh Dickins 378124513264SHugh Dickins if (zone) { 378224513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 378324513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 378424513264SHugh Dickins spin_unlock_irq(&zone->lru_lock); 378524513264SHugh Dickins } 378685046579SHugh Dickins } 378785046579SHugh Dickins #endif /* CONFIG_SHMEM */ 3788