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 68*ee814fe2SJohannes Weiner /* Allocation order */ 695ad333ebSAndy Whitcroft int order; 7066e1707bSBalbir Singh 71*ee814fe2SJohannes Weiner /* 72*ee814fe2SJohannes Weiner * Nodemask of nodes allowed by the caller. If NULL, all nodes 73*ee814fe2SJohannes Weiner * are scanned. 74*ee814fe2SJohannes Weiner */ 75*ee814fe2SJohannes 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 83*ee814fe2SJohannes Weiner /* Scan (total_size >> priority) pages at once */ 84*ee814fe2SJohannes Weiner int priority; 85*ee814fe2SJohannes Weiner 86*ee814fe2SJohannes Weiner unsigned int may_writepage:1; 87*ee814fe2SJohannes Weiner 88*ee814fe2SJohannes Weiner /* Can mapped pages be reclaimed? */ 89*ee814fe2SJohannes Weiner unsigned int may_unmap:1; 90*ee814fe2SJohannes Weiner 91*ee814fe2SJohannes Weiner /* Can pages be swapped as part of reclaim? */ 92*ee814fe2SJohannes Weiner unsigned int may_swap:1; 93*ee814fe2SJohannes Weiner 94*ee814fe2SJohannes Weiner unsigned int hibernation_mode:1; 95*ee814fe2SJohannes Weiner 96*ee814fe2SJohannes Weiner /* One of the zones is ready for compaction */ 97*ee814fe2SJohannes Weiner unsigned int compaction_ready:1; 98*ee814fe2SJohannes Weiner 99*ee814fe2SJohannes Weiner /* Incremented by the number of inactive pages that were scanned */ 100*ee814fe2SJohannes Weiner unsigned long nr_scanned; 101*ee814fe2SJohannes Weiner 102*ee814fe2SJohannes Weiner /* Number of pages freed so far during a call to shrink_zones() */ 103*ee814fe2SJohannes Weiner unsigned long nr_reclaimed; 1041da177e4SLinus Torvalds }; 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1091da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1101da177e4SLinus Torvalds do { \ 1111da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1121da177e4SLinus Torvalds struct page *prev; \ 1131da177e4SLinus Torvalds \ 1141da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1151da177e4SLinus Torvalds prefetch(&prev->_field); \ 1161da177e4SLinus Torvalds } \ 1171da177e4SLinus Torvalds } while (0) 1181da177e4SLinus Torvalds #else 1191da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1201da177e4SLinus Torvalds #endif 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1231da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1241da177e4SLinus Torvalds do { \ 1251da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1261da177e4SLinus Torvalds struct page *prev; \ 1271da177e4SLinus Torvalds \ 1281da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1291da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1301da177e4SLinus Torvalds } \ 1311da177e4SLinus Torvalds } while (0) 1321da177e4SLinus Torvalds #else 1331da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1341da177e4SLinus Torvalds #endif 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* 1371da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1381da177e4SLinus Torvalds */ 1391da177e4SLinus Torvalds int vm_swappiness = 60; 140b21e0b90SZhang Yanfei unsigned long vm_total_pages; /* The total number of pages which the VM controls */ 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1431da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1441da177e4SLinus Torvalds 145c255a458SAndrew Morton #ifdef CONFIG_MEMCG 14689b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 14789b5fae5SJohannes Weiner { 148f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 14989b5fae5SJohannes Weiner } 15091a45470SKAMEZAWA Hiroyuki #else 15189b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 15289b5fae5SJohannes Weiner { 15389b5fae5SJohannes Weiner return true; 15489b5fae5SJohannes Weiner } 15591a45470SKAMEZAWA Hiroyuki #endif 15691a45470SKAMEZAWA Hiroyuki 157a1c3bfb2SJohannes Weiner static unsigned long zone_reclaimable_pages(struct zone *zone) 1586e543d57SLisa Du { 1596e543d57SLisa Du int nr; 1606e543d57SLisa Du 1616e543d57SLisa Du nr = zone_page_state(zone, NR_ACTIVE_FILE) + 1626e543d57SLisa Du zone_page_state(zone, NR_INACTIVE_FILE); 1636e543d57SLisa Du 1646e543d57SLisa Du if (get_nr_swap_pages() > 0) 1656e543d57SLisa Du nr += zone_page_state(zone, NR_ACTIVE_ANON) + 1666e543d57SLisa Du zone_page_state(zone, NR_INACTIVE_ANON); 1676e543d57SLisa Du 1686e543d57SLisa Du return nr; 1696e543d57SLisa Du } 1706e543d57SLisa Du 1716e543d57SLisa Du bool zone_reclaimable(struct zone *zone) 1726e543d57SLisa Du { 1736e543d57SLisa Du return zone->pages_scanned < zone_reclaimable_pages(zone) * 6; 1746e543d57SLisa Du } 1756e543d57SLisa Du 1764d7dcca2SHugh Dickins static unsigned long get_lru_size(struct lruvec *lruvec, enum lru_list lru) 177c9f299d9SKOSAKI Motohiro { 178c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1794d7dcca2SHugh Dickins return mem_cgroup_get_lru_size(lruvec, lru); 180a3d8e054SKOSAKI Motohiro 181074291feSKonstantin Khlebnikov return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru); 182c9f299d9SKOSAKI Motohiro } 183c9f299d9SKOSAKI Motohiro 1841da177e4SLinus Torvalds /* 1851d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 1861da177e4SLinus Torvalds */ 1871d3d4437SGlauber Costa int register_shrinker(struct shrinker *shrinker) 1881da177e4SLinus Torvalds { 1891d3d4437SGlauber Costa size_t size = sizeof(*shrinker->nr_deferred); 1901d3d4437SGlauber Costa 1911d3d4437SGlauber Costa /* 1921d3d4437SGlauber Costa * If we only have one possible node in the system anyway, save 1931d3d4437SGlauber Costa * ourselves the trouble and disable NUMA aware behavior. This way we 1941d3d4437SGlauber Costa * will save memory and some small loop time later. 1951d3d4437SGlauber Costa */ 1961d3d4437SGlauber Costa if (nr_node_ids == 1) 1971d3d4437SGlauber Costa shrinker->flags &= ~SHRINKER_NUMA_AWARE; 1981d3d4437SGlauber Costa 1991d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 2001d3d4437SGlauber Costa size *= nr_node_ids; 2011d3d4437SGlauber Costa 2021d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 2031d3d4437SGlauber Costa if (!shrinker->nr_deferred) 2041d3d4437SGlauber Costa return -ENOMEM; 2051d3d4437SGlauber Costa 2061da177e4SLinus Torvalds down_write(&shrinker_rwsem); 2071da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 2081da177e4SLinus Torvalds up_write(&shrinker_rwsem); 2091d3d4437SGlauber Costa return 0; 2101da177e4SLinus Torvalds } 2118e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds /* 2141da177e4SLinus Torvalds * Remove one 2151da177e4SLinus Torvalds */ 2168e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 2171da177e4SLinus Torvalds { 2181da177e4SLinus Torvalds down_write(&shrinker_rwsem); 2191da177e4SLinus Torvalds list_del(&shrinker->list); 2201da177e4SLinus Torvalds up_write(&shrinker_rwsem); 221ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 2221da177e4SLinus Torvalds } 2238e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds #define SHRINK_BATCH 128 2261d3d4437SGlauber Costa 2271d3d4437SGlauber Costa static unsigned long 2281d3d4437SGlauber Costa shrink_slab_node(struct shrink_control *shrinkctl, struct shrinker *shrinker, 2291d3d4437SGlauber Costa unsigned long nr_pages_scanned, unsigned long lru_pages) 2301da177e4SLinus Torvalds { 23124f7c6b9SDave Chinner unsigned long freed = 0; 2321da177e4SLinus Torvalds unsigned long long delta; 233635697c6SKonstantin Khlebnikov long total_scan; 234d5bc5fd3SVladimir Davydov long freeable; 235acf92b48SDave Chinner long nr; 236acf92b48SDave Chinner long new_nr; 2371d3d4437SGlauber Costa int nid = shrinkctl->nid; 238e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 239e9299f50SDave Chinner : SHRINK_BATCH; 2401da177e4SLinus Torvalds 241d5bc5fd3SVladimir Davydov freeable = shrinker->count_objects(shrinker, shrinkctl); 242d5bc5fd3SVladimir Davydov if (freeable == 0) 2431d3d4437SGlauber Costa return 0; 244635697c6SKonstantin Khlebnikov 245acf92b48SDave Chinner /* 246acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 247acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 248acf92b48SDave Chinner * don't also do this scanning work. 249acf92b48SDave Chinner */ 2501d3d4437SGlauber Costa nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 251acf92b48SDave Chinner 252acf92b48SDave Chinner total_scan = nr; 2531495f230SYing Han delta = (4 * nr_pages_scanned) / shrinker->seeks; 254d5bc5fd3SVladimir Davydov delta *= freeable; 2551da177e4SLinus Torvalds do_div(delta, lru_pages + 1); 256acf92b48SDave Chinner total_scan += delta; 257acf92b48SDave Chinner if (total_scan < 0) { 25824f7c6b9SDave Chinner printk(KERN_ERR 25924f7c6b9SDave Chinner "shrink_slab: %pF negative objects to delete nr=%ld\n", 260a0b02131SDave Chinner shrinker->scan_objects, total_scan); 261d5bc5fd3SVladimir Davydov total_scan = freeable; 262ea164d73SAndrea Arcangeli } 263ea164d73SAndrea Arcangeli 264ea164d73SAndrea Arcangeli /* 2653567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 2663567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 2673567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 2683567b59aSDave Chinner * nr being built up so when a shrink that can do some work 2693567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 270d5bc5fd3SVladimir Davydov * freeable. This is bad for sustaining a working set in 2713567b59aSDave Chinner * memory. 2723567b59aSDave Chinner * 2733567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 2743567b59aSDave Chinner * a large delta change is calculated directly. 2753567b59aSDave Chinner */ 276d5bc5fd3SVladimir Davydov if (delta < freeable / 4) 277d5bc5fd3SVladimir Davydov total_scan = min(total_scan, freeable / 2); 2783567b59aSDave Chinner 2793567b59aSDave Chinner /* 280ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 281ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 282ea164d73SAndrea Arcangeli * freeable entries. 283ea164d73SAndrea Arcangeli */ 284d5bc5fd3SVladimir Davydov if (total_scan > freeable * 2) 285d5bc5fd3SVladimir Davydov total_scan = freeable * 2; 2861da177e4SLinus Torvalds 28724f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 28809576073SDave Chinner nr_pages_scanned, lru_pages, 289d5bc5fd3SVladimir Davydov freeable, delta, total_scan); 29009576073SDave Chinner 2910b1fb40aSVladimir Davydov /* 2920b1fb40aSVladimir Davydov * Normally, we should not scan less than batch_size objects in one 2930b1fb40aSVladimir Davydov * pass to avoid too frequent shrinker calls, but if the slab has less 2940b1fb40aSVladimir Davydov * than batch_size objects in total and we are really tight on memory, 2950b1fb40aSVladimir Davydov * we will try to reclaim all available objects, otherwise we can end 2960b1fb40aSVladimir Davydov * up failing allocations although there are plenty of reclaimable 2970b1fb40aSVladimir Davydov * objects spread over several slabs with usage less than the 2980b1fb40aSVladimir Davydov * batch_size. 2990b1fb40aSVladimir Davydov * 3000b1fb40aSVladimir Davydov * We detect the "tight on memory" situations by looking at the total 3010b1fb40aSVladimir Davydov * number of objects we want to scan (total_scan). If it is greater 302d5bc5fd3SVladimir Davydov * than the total number of objects on slab (freeable), we must be 3030b1fb40aSVladimir Davydov * scanning at high prio and therefore should try to reclaim as much as 3040b1fb40aSVladimir Davydov * possible. 3050b1fb40aSVladimir Davydov */ 3060b1fb40aSVladimir Davydov while (total_scan >= batch_size || 307d5bc5fd3SVladimir Davydov total_scan >= freeable) { 30824f7c6b9SDave Chinner unsigned long ret; 3090b1fb40aSVladimir Davydov unsigned long nr_to_scan = min(batch_size, total_scan); 3101da177e4SLinus Torvalds 3110b1fb40aSVladimir Davydov shrinkctl->nr_to_scan = nr_to_scan; 31224f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 31324f7c6b9SDave Chinner if (ret == SHRINK_STOP) 3141da177e4SLinus Torvalds break; 31524f7c6b9SDave Chinner freed += ret; 31624f7c6b9SDave Chinner 3170b1fb40aSVladimir Davydov count_vm_events(SLABS_SCANNED, nr_to_scan); 3180b1fb40aSVladimir Davydov total_scan -= nr_to_scan; 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds cond_resched(); 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 323acf92b48SDave Chinner /* 324acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 325acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 326acf92b48SDave Chinner * scan, there is no need to do an update. 327acf92b48SDave Chinner */ 32883aeeadaSKonstantin Khlebnikov if (total_scan > 0) 32983aeeadaSKonstantin Khlebnikov new_nr = atomic_long_add_return(total_scan, 3301d3d4437SGlauber Costa &shrinker->nr_deferred[nid]); 33183aeeadaSKonstantin Khlebnikov else 3321d3d4437SGlauber Costa new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 333acf92b48SDave Chinner 334df9024a8SDave Hansen trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan); 3351d3d4437SGlauber Costa return freed; 3361d3d4437SGlauber Costa } 3371d3d4437SGlauber Costa 3381d3d4437SGlauber Costa /* 3391d3d4437SGlauber Costa * Call the shrink functions to age shrinkable caches 3401d3d4437SGlauber Costa * 3411d3d4437SGlauber Costa * Here we assume it costs one seek to replace a lru page and that it also 3421d3d4437SGlauber Costa * takes a seek to recreate a cache object. With this in mind we age equal 3431d3d4437SGlauber Costa * percentages of the lru and ageable caches. This should balance the seeks 3441d3d4437SGlauber Costa * generated by these structures. 3451d3d4437SGlauber Costa * 3461d3d4437SGlauber Costa * If the vm encountered mapped pages on the LRU it increase the pressure on 3471d3d4437SGlauber Costa * slab to avoid swapping. 3481d3d4437SGlauber Costa * 3491d3d4437SGlauber Costa * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. 3501d3d4437SGlauber Costa * 3511d3d4437SGlauber Costa * `lru_pages' represents the number of on-LRU pages in all the zones which 3521d3d4437SGlauber Costa * are eligible for the caller's allocation attempt. It is used for balancing 3531d3d4437SGlauber Costa * slab reclaim versus page reclaim. 3541d3d4437SGlauber Costa * 3551d3d4437SGlauber Costa * Returns the number of slab objects which we shrunk. 3561d3d4437SGlauber Costa */ 3571d3d4437SGlauber Costa unsigned long shrink_slab(struct shrink_control *shrinkctl, 3581d3d4437SGlauber Costa unsigned long nr_pages_scanned, 3591d3d4437SGlauber Costa unsigned long lru_pages) 3601d3d4437SGlauber Costa { 3611d3d4437SGlauber Costa struct shrinker *shrinker; 3621d3d4437SGlauber Costa unsigned long freed = 0; 3631d3d4437SGlauber Costa 3641d3d4437SGlauber Costa if (nr_pages_scanned == 0) 3651d3d4437SGlauber Costa nr_pages_scanned = SWAP_CLUSTER_MAX; 3661d3d4437SGlauber Costa 3671d3d4437SGlauber Costa if (!down_read_trylock(&shrinker_rwsem)) { 3681d3d4437SGlauber Costa /* 3691d3d4437SGlauber Costa * If we would return 0, our callers would understand that we 3701d3d4437SGlauber Costa * have nothing else to shrink and give up trying. By returning 3711d3d4437SGlauber Costa * 1 we keep it going and assume we'll be able to shrink next 3721d3d4437SGlauber Costa * time. 3731d3d4437SGlauber Costa */ 3741d3d4437SGlauber Costa freed = 1; 3751d3d4437SGlauber Costa goto out; 3761d3d4437SGlauber Costa } 3771d3d4437SGlauber Costa 3781d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 379ec97097bSVladimir Davydov if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) { 380ec97097bSVladimir Davydov shrinkctl->nid = 0; 381ec97097bSVladimir Davydov freed += shrink_slab_node(shrinkctl, shrinker, 382ec97097bSVladimir Davydov nr_pages_scanned, lru_pages); 3831d3d4437SGlauber Costa continue; 384ec97097bSVladimir Davydov } 3851d3d4437SGlauber Costa 386ec97097bSVladimir Davydov for_each_node_mask(shrinkctl->nid, shrinkctl->nodes_to_scan) { 387ec97097bSVladimir Davydov if (node_online(shrinkctl->nid)) 3881d3d4437SGlauber Costa freed += shrink_slab_node(shrinkctl, shrinker, 3891d3d4437SGlauber Costa nr_pages_scanned, lru_pages); 3901d3d4437SGlauber Costa 3911d3d4437SGlauber Costa } 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds up_read(&shrinker_rwsem); 394f06590bdSMinchan Kim out: 395f06590bdSMinchan Kim cond_resched(); 39624f7c6b9SDave Chinner return freed; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 4001da177e4SLinus Torvalds { 401ceddc3a5SJohannes Weiner /* 402ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 403ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 404ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 405ceddc3a5SJohannes Weiner */ 406edcf4748SJohannes Weiner return page_count(page) - page_has_private(page) == 2; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4097d3579e8SKOSAKI Motohiro static int may_write_to_queue(struct backing_dev_info *bdi, 4107d3579e8SKOSAKI Motohiro struct scan_control *sc) 4111da177e4SLinus Torvalds { 412930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 4131da177e4SLinus Torvalds return 1; 4141da177e4SLinus Torvalds if (!bdi_write_congested(bdi)) 4151da177e4SLinus Torvalds return 1; 4161da177e4SLinus Torvalds if (bdi == current->backing_dev_info) 4171da177e4SLinus Torvalds return 1; 4181da177e4SLinus Torvalds return 0; 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds /* 4221da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 4231da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 4241da177e4SLinus Torvalds * fsync(), msync() or close(). 4251da177e4SLinus Torvalds * 4261da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 4271da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 4281da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 4291da177e4SLinus Torvalds * 4301da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 4311da177e4SLinus Torvalds * __GFP_FS. 4321da177e4SLinus Torvalds */ 4331da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 4341da177e4SLinus Torvalds struct page *page, int error) 4351da177e4SLinus Torvalds { 4367eaceaccSJens Axboe lock_page(page); 4373e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 4383e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 4391da177e4SLinus Torvalds unlock_page(page); 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds 44204e62a29SChristoph Lameter /* possible outcome of pageout() */ 44304e62a29SChristoph Lameter typedef enum { 44404e62a29SChristoph Lameter /* failed to write page out, page is locked */ 44504e62a29SChristoph Lameter PAGE_KEEP, 44604e62a29SChristoph Lameter /* move page to the active list, page is locked */ 44704e62a29SChristoph Lameter PAGE_ACTIVATE, 44804e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 44904e62a29SChristoph Lameter PAGE_SUCCESS, 45004e62a29SChristoph Lameter /* page is clean and locked */ 45104e62a29SChristoph Lameter PAGE_CLEAN, 45204e62a29SChristoph Lameter } pageout_t; 45304e62a29SChristoph Lameter 4541da177e4SLinus Torvalds /* 4551742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 4561742f19fSAndrew Morton * Calls ->writepage(). 4571da177e4SLinus Torvalds */ 458c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 4597d3579e8SKOSAKI Motohiro struct scan_control *sc) 4601da177e4SLinus Torvalds { 4611da177e4SLinus Torvalds /* 4621da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 4631da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 4641da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 4651da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 4661da177e4SLinus Torvalds * PagePrivate for that. 4671da177e4SLinus Torvalds * 4688174202bSAl Viro * If this process is currently in __generic_file_write_iter() against 4691da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 4701da177e4SLinus Torvalds * will block. 4711da177e4SLinus Torvalds * 4721da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 4731da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 4741da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 4751da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 4781da177e4SLinus Torvalds return PAGE_KEEP; 4791da177e4SLinus Torvalds if (!mapping) { 4801da177e4SLinus Torvalds /* 4811da177e4SLinus Torvalds * Some data journaling orphaned pages can have 4821da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 4831da177e4SLinus Torvalds */ 484266cf658SDavid Howells if (page_has_private(page)) { 4851da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 4861da177e4SLinus Torvalds ClearPageDirty(page); 487b1de0d13SMitchel Humpherys pr_info("%s: orphaned page\n", __func__); 4881da177e4SLinus Torvalds return PAGE_CLEAN; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds return PAGE_KEEP; 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 4941da177e4SLinus Torvalds return PAGE_ACTIVATE; 4950e093d99SMel Gorman if (!may_write_to_queue(mapping->backing_dev_info, sc)) 4961da177e4SLinus Torvalds return PAGE_KEEP; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 4991da177e4SLinus Torvalds int res; 5001da177e4SLinus Torvalds struct writeback_control wbc = { 5011da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 5021da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 503111ebb6eSOGAWA Hirofumi .range_start = 0, 504111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 5051da177e4SLinus Torvalds .for_reclaim = 1, 5061da177e4SLinus Torvalds }; 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds SetPageReclaim(page); 5091da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 5101da177e4SLinus Torvalds if (res < 0) 5111da177e4SLinus Torvalds handle_write_error(mapping, page, res); 512994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 5131da177e4SLinus Torvalds ClearPageReclaim(page); 5141da177e4SLinus Torvalds return PAGE_ACTIVATE; 5151da177e4SLinus Torvalds } 516c661b078SAndy Whitcroft 5171da177e4SLinus Torvalds if (!PageWriteback(page)) { 5181da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 5191da177e4SLinus Torvalds ClearPageReclaim(page); 5201da177e4SLinus Torvalds } 52123b9da55SMel Gorman trace_mm_vmscan_writepage(page, trace_reclaim_flags(page)); 522e129b5c2SAndrew Morton inc_zone_page_state(page, NR_VMSCAN_WRITE); 5231da177e4SLinus Torvalds return PAGE_SUCCESS; 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds return PAGE_CLEAN; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 529a649fd92SAndrew Morton /* 530e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 531e286781dSNick Piggin * gets returned with a refcount of 0. 532a649fd92SAndrew Morton */ 533a528910eSJohannes Weiner static int __remove_mapping(struct address_space *mapping, struct page *page, 534a528910eSJohannes Weiner bool reclaimed) 53549d2e9ccSChristoph Lameter { 53628e4d965SNick Piggin BUG_ON(!PageLocked(page)); 53728e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 53849d2e9ccSChristoph Lameter 53919fd6231SNick Piggin spin_lock_irq(&mapping->tree_lock); 54049d2e9ccSChristoph Lameter /* 5410fd0e6b0SNick Piggin * The non racy check for a busy page. 5420fd0e6b0SNick Piggin * 5430fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 5440fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 5450fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 5460fd0e6b0SNick Piggin * here, then the following race may occur: 5470fd0e6b0SNick Piggin * 5480fd0e6b0SNick Piggin * get_user_pages(&page); 5490fd0e6b0SNick Piggin * [user mapping goes away] 5500fd0e6b0SNick Piggin * write_to(page); 5510fd0e6b0SNick Piggin * !PageDirty(page) [good] 5520fd0e6b0SNick Piggin * SetPageDirty(page); 5530fd0e6b0SNick Piggin * put_page(page); 5540fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 5550fd0e6b0SNick Piggin * 5560fd0e6b0SNick Piggin * [oops, our write_to data is lost] 5570fd0e6b0SNick Piggin * 5580fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 5590fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 5600fd0e6b0SNick Piggin * load is not satisfied before that of page->_count. 5610fd0e6b0SNick Piggin * 5620fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 5630fd0e6b0SNick Piggin * and thus under tree_lock, then this ordering is not required. 56449d2e9ccSChristoph Lameter */ 565e286781dSNick Piggin if (!page_freeze_refs(page, 2)) 56649d2e9ccSChristoph Lameter goto cannot_free; 567e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 568e286781dSNick Piggin if (unlikely(PageDirty(page))) { 569e286781dSNick Piggin page_unfreeze_refs(page, 2); 57049d2e9ccSChristoph Lameter goto cannot_free; 571e286781dSNick Piggin } 57249d2e9ccSChristoph Lameter 57349d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 57449d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 57549d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 57619fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 577cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, page); 578e286781dSNick Piggin } else { 5796072d13cSLinus Torvalds void (*freepage)(struct page *); 580a528910eSJohannes Weiner void *shadow = NULL; 5816072d13cSLinus Torvalds 5826072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 583a528910eSJohannes Weiner /* 584a528910eSJohannes Weiner * Remember a shadow entry for reclaimed file cache in 585a528910eSJohannes Weiner * order to detect refaults, thus thrashing, later on. 586a528910eSJohannes Weiner * 587a528910eSJohannes Weiner * But don't store shadows in an address space that is 588a528910eSJohannes Weiner * already exiting. This is not just an optizimation, 589a528910eSJohannes Weiner * inode reclaim needs to empty out the radix tree or 590a528910eSJohannes Weiner * the nodes are lost. Don't plant shadows behind its 591a528910eSJohannes Weiner * back. 592a528910eSJohannes Weiner */ 593a528910eSJohannes Weiner if (reclaimed && page_is_file_cache(page) && 594a528910eSJohannes Weiner !mapping_exiting(mapping)) 595a528910eSJohannes Weiner shadow = workingset_eviction(mapping, page); 596a528910eSJohannes Weiner __delete_from_page_cache(page, shadow); 59719fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 598e767e056SDaisuke Nishimura mem_cgroup_uncharge_cache_page(page); 5996072d13cSLinus Torvalds 6006072d13cSLinus Torvalds if (freepage != NULL) 6016072d13cSLinus Torvalds freepage(page); 602e286781dSNick Piggin } 603e286781dSNick Piggin 60449d2e9ccSChristoph Lameter return 1; 60549d2e9ccSChristoph Lameter 60649d2e9ccSChristoph Lameter cannot_free: 60719fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 60849d2e9ccSChristoph Lameter return 0; 60949d2e9ccSChristoph Lameter } 61049d2e9ccSChristoph Lameter 6111da177e4SLinus Torvalds /* 612e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 613e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 614e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 615e286781dSNick Piggin * this page. 616e286781dSNick Piggin */ 617e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 618e286781dSNick Piggin { 619a528910eSJohannes Weiner if (__remove_mapping(mapping, page, false)) { 620e286781dSNick Piggin /* 621e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 622e286781dSNick Piggin * drops the pagecache ref for us without requiring another 623e286781dSNick Piggin * atomic operation. 624e286781dSNick Piggin */ 625e286781dSNick Piggin page_unfreeze_refs(page, 1); 626e286781dSNick Piggin return 1; 627e286781dSNick Piggin } 628e286781dSNick Piggin return 0; 629e286781dSNick Piggin } 630e286781dSNick Piggin 631894bc310SLee Schermerhorn /** 632894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 633894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 634894bc310SLee Schermerhorn * 635894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 636894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 637894bc310SLee Schermerhorn * 638894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 639894bc310SLee Schermerhorn */ 640894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 641894bc310SLee Schermerhorn { 6420ec3b74cSVlastimil Babka bool is_unevictable; 643bbfd28eeSLee Schermerhorn int was_unevictable = PageUnevictable(page); 644894bc310SLee Schermerhorn 645309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 646894bc310SLee Schermerhorn 647894bc310SLee Schermerhorn redo: 648894bc310SLee Schermerhorn ClearPageUnevictable(page); 649894bc310SLee Schermerhorn 65039b5f29aSHugh Dickins if (page_evictable(page)) { 651894bc310SLee Schermerhorn /* 652894bc310SLee Schermerhorn * For evictable pages, we can use the cache. 653894bc310SLee Schermerhorn * In event of a race, worst case is we end up with an 654894bc310SLee Schermerhorn * unevictable page on [in]active list. 655894bc310SLee Schermerhorn * We know how to handle that. 656894bc310SLee Schermerhorn */ 6570ec3b74cSVlastimil Babka is_unevictable = false; 658c53954a0SMel Gorman lru_cache_add(page); 659894bc310SLee Schermerhorn } else { 660894bc310SLee Schermerhorn /* 661894bc310SLee Schermerhorn * Put unevictable pages directly on zone's unevictable 662894bc310SLee Schermerhorn * list. 663894bc310SLee Schermerhorn */ 6640ec3b74cSVlastimil Babka is_unevictable = true; 665894bc310SLee Schermerhorn add_page_to_unevictable_list(page); 6666a7b9548SJohannes Weiner /* 66721ee9f39SMinchan Kim * When racing with an mlock or AS_UNEVICTABLE clearing 66821ee9f39SMinchan Kim * (page is unlocked) make sure that if the other thread 66921ee9f39SMinchan Kim * does not observe our setting of PG_lru and fails 67024513264SHugh Dickins * isolation/check_move_unevictable_pages, 67121ee9f39SMinchan Kim * we see PG_mlocked/AS_UNEVICTABLE cleared below and move 6726a7b9548SJohannes Weiner * the page back to the evictable list. 6736a7b9548SJohannes Weiner * 67421ee9f39SMinchan Kim * The other side is TestClearPageMlocked() or shmem_lock(). 6756a7b9548SJohannes Weiner */ 6766a7b9548SJohannes Weiner smp_mb(); 677894bc310SLee Schermerhorn } 678894bc310SLee Schermerhorn 679894bc310SLee Schermerhorn /* 680894bc310SLee Schermerhorn * page's status can change while we move it among lru. If an evictable 681894bc310SLee Schermerhorn * page is on unevictable list, it never be freed. To avoid that, 682894bc310SLee Schermerhorn * check after we added it to the list, again. 683894bc310SLee Schermerhorn */ 6840ec3b74cSVlastimil Babka if (is_unevictable && page_evictable(page)) { 685894bc310SLee Schermerhorn if (!isolate_lru_page(page)) { 686894bc310SLee Schermerhorn put_page(page); 687894bc310SLee Schermerhorn goto redo; 688894bc310SLee Schermerhorn } 689894bc310SLee Schermerhorn /* This means someone else dropped this page from LRU 690894bc310SLee Schermerhorn * So, it will be freed or putback to LRU again. There is 691894bc310SLee Schermerhorn * nothing to do here. 692894bc310SLee Schermerhorn */ 693894bc310SLee Schermerhorn } 694894bc310SLee Schermerhorn 6950ec3b74cSVlastimil Babka if (was_unevictable && !is_unevictable) 696bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGRESCUED); 6970ec3b74cSVlastimil Babka else if (!was_unevictable && is_unevictable) 698bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGCULLED); 699bbfd28eeSLee Schermerhorn 700894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 701894bc310SLee Schermerhorn } 702894bc310SLee Schermerhorn 703dfc8d636SJohannes Weiner enum page_references { 704dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 705dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 70664574746SJohannes Weiner PAGEREF_KEEP, 707dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 708dfc8d636SJohannes Weiner }; 709dfc8d636SJohannes Weiner 710dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 711dfc8d636SJohannes Weiner struct scan_control *sc) 712dfc8d636SJohannes Weiner { 71364574746SJohannes Weiner int referenced_ptes, referenced_page; 714dfc8d636SJohannes Weiner unsigned long vm_flags; 715dfc8d636SJohannes Weiner 716c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 717c3ac9a8aSJohannes Weiner &vm_flags); 71864574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 719dfc8d636SJohannes Weiner 720dfc8d636SJohannes Weiner /* 721dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 722dfc8d636SJohannes Weiner * move the page to the unevictable list. 723dfc8d636SJohannes Weiner */ 724dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 725dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 726dfc8d636SJohannes Weiner 72764574746SJohannes Weiner if (referenced_ptes) { 728e4898273SMichal Hocko if (PageSwapBacked(page)) 72964574746SJohannes Weiner return PAGEREF_ACTIVATE; 73064574746SJohannes Weiner /* 73164574746SJohannes Weiner * All mapped pages start out with page table 73264574746SJohannes Weiner * references from the instantiating fault, so we need 73364574746SJohannes Weiner * to look twice if a mapped file page is used more 73464574746SJohannes Weiner * than once. 73564574746SJohannes Weiner * 73664574746SJohannes Weiner * Mark it and spare it for another trip around the 73764574746SJohannes Weiner * inactive list. Another page table reference will 73864574746SJohannes Weiner * lead to its activation. 73964574746SJohannes Weiner * 74064574746SJohannes Weiner * Note: the mark is set for activated pages as well 74164574746SJohannes Weiner * so that recently deactivated but used pages are 74264574746SJohannes Weiner * quickly recovered. 74364574746SJohannes Weiner */ 74464574746SJohannes Weiner SetPageReferenced(page); 74564574746SJohannes Weiner 74634dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 747dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 748dfc8d636SJohannes Weiner 749c909e993SKonstantin Khlebnikov /* 750c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 751c909e993SKonstantin Khlebnikov */ 752c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 753c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 754c909e993SKonstantin Khlebnikov 75564574746SJohannes Weiner return PAGEREF_KEEP; 75664574746SJohannes Weiner } 75764574746SJohannes Weiner 758dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 7592e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 760dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 76164574746SJohannes Weiner 76264574746SJohannes Weiner return PAGEREF_RECLAIM; 763dfc8d636SJohannes Weiner } 764dfc8d636SJohannes Weiner 765e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 766e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 767e2be15f6SMel Gorman bool *dirty, bool *writeback) 768e2be15f6SMel Gorman { 769b4597226SMel Gorman struct address_space *mapping; 770b4597226SMel Gorman 771e2be15f6SMel Gorman /* 772e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 773e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 774e2be15f6SMel Gorman */ 775e2be15f6SMel Gorman if (!page_is_file_cache(page)) { 776e2be15f6SMel Gorman *dirty = false; 777e2be15f6SMel Gorman *writeback = false; 778e2be15f6SMel Gorman return; 779e2be15f6SMel Gorman } 780e2be15f6SMel Gorman 781e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 782e2be15f6SMel Gorman *dirty = PageDirty(page); 783e2be15f6SMel Gorman *writeback = PageWriteback(page); 784b4597226SMel Gorman 785b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 786b4597226SMel Gorman if (!page_has_private(page)) 787b4597226SMel Gorman return; 788b4597226SMel Gorman 789b4597226SMel Gorman mapping = page_mapping(page); 790b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 791b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 792e2be15f6SMel Gorman } 793e2be15f6SMel Gorman 794e286781dSNick Piggin /* 7951742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 7961da177e4SLinus Torvalds */ 7971742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 7986a18adb3SKonstantin Khlebnikov struct zone *zone, 799f84f6e2bSMel Gorman struct scan_control *sc, 80002c6de8dSMinchan Kim enum ttu_flags ttu_flags, 8018e950282SMel Gorman unsigned long *ret_nr_dirty, 802d43006d5SMel Gorman unsigned long *ret_nr_unqueued_dirty, 8038e950282SMel Gorman unsigned long *ret_nr_congested, 80402c6de8dSMinchan Kim unsigned long *ret_nr_writeback, 805b1a6f21eSMel Gorman unsigned long *ret_nr_immediate, 80602c6de8dSMinchan Kim bool force_reclaim) 8071da177e4SLinus Torvalds { 8081da177e4SLinus Torvalds LIST_HEAD(ret_pages); 809abe4c3b5SMel Gorman LIST_HEAD(free_pages); 8101da177e4SLinus Torvalds int pgactivate = 0; 811d43006d5SMel Gorman unsigned long nr_unqueued_dirty = 0; 8120e093d99SMel Gorman unsigned long nr_dirty = 0; 8130e093d99SMel Gorman unsigned long nr_congested = 0; 81405ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 81592df3a72SMel Gorman unsigned long nr_writeback = 0; 816b1a6f21eSMel Gorman unsigned long nr_immediate = 0; 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds cond_resched(); 8191da177e4SLinus Torvalds 82069980e31STim Chen mem_cgroup_uncharge_start(); 8211da177e4SLinus Torvalds while (!list_empty(page_list)) { 8221da177e4SLinus Torvalds struct address_space *mapping; 8231da177e4SLinus Torvalds struct page *page; 8241da177e4SLinus Torvalds int may_enter_fs; 82502c6de8dSMinchan Kim enum page_references references = PAGEREF_RECLAIM_CLEAN; 826e2be15f6SMel Gorman bool dirty, writeback; 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds cond_resched(); 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds page = lru_to_page(page_list); 8311da177e4SLinus Torvalds list_del(&page->lru); 8321da177e4SLinus Torvalds 833529ae9aaSNick Piggin if (!trylock_page(page)) 8341da177e4SLinus Torvalds goto keep; 8351da177e4SLinus Torvalds 836309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 837309381feSSasha Levin VM_BUG_ON_PAGE(page_zone(page) != zone, page); 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds sc->nr_scanned++; 84080e43426SChristoph Lameter 84139b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 842b291f000SNick Piggin goto cull_mlocked; 843894bc310SLee Schermerhorn 844a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 84580e43426SChristoph Lameter goto keep_locked; 84680e43426SChristoph Lameter 8471da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 8481da177e4SLinus Torvalds if (page_mapped(page) || PageSwapCache(page)) 8491da177e4SLinus Torvalds sc->nr_scanned++; 8501da177e4SLinus Torvalds 851c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 852c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 853c661b078SAndy Whitcroft 854e62e384eSMichal Hocko /* 855e2be15f6SMel Gorman * The number of dirty pages determines if a zone is marked 856e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 857e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 858e2be15f6SMel Gorman * is all dirty unqueued pages. 859e2be15f6SMel Gorman */ 860e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 861e2be15f6SMel Gorman if (dirty || writeback) 862e2be15f6SMel Gorman nr_dirty++; 863e2be15f6SMel Gorman 864e2be15f6SMel Gorman if (dirty && !writeback) 865e2be15f6SMel Gorman nr_unqueued_dirty++; 866e2be15f6SMel Gorman 867d04e8acdSMel Gorman /* 868d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 869d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 870d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 871d04e8acdSMel Gorman * end of the LRU a second time. 872d04e8acdSMel Gorman */ 873e2be15f6SMel Gorman mapping = page_mapping(page); 874d04e8acdSMel Gorman if ((mapping && bdi_write_congested(mapping->backing_dev_info)) || 875d04e8acdSMel Gorman (writeback && PageReclaim(page))) 876e2be15f6SMel Gorman nr_congested++; 877e2be15f6SMel Gorman 878e2be15f6SMel Gorman /* 879283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 880283aba9fSMel Gorman * are three cases to consider. 881e62e384eSMichal Hocko * 882283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 883283aba9fSMel Gorman * under writeback and this page is both under writeback and 884283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 885283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 886283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 887283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 888283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 889b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 890b1a6f21eSMel Gorman * caller can stall after page list has been processed. 891c3b94f44SHugh Dickins * 892283aba9fSMel Gorman * 2) Global reclaim encounters a page, memcg encounters a 893283aba9fSMel Gorman * page that is not marked for immediate reclaim or 894283aba9fSMel Gorman * the caller does not have __GFP_IO. In this case mark 895283aba9fSMel Gorman * the page for immediate reclaim and continue scanning. 896283aba9fSMel Gorman * 897283aba9fSMel Gorman * __GFP_IO is checked because a loop driver thread might 898283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 899283aba9fSMel Gorman * which it is needed to do the write (loop masks off 900283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 901283aba9fSMel Gorman * would probably show more reasons. 902283aba9fSMel Gorman * 903283aba9fSMel Gorman * Don't require __GFP_FS, since we're not going into the 904283aba9fSMel Gorman * FS, just waiting on its writeback completion. Worryingly, 905283aba9fSMel Gorman * ext4 gfs2 and xfs allocate pages with 906283aba9fSMel Gorman * grab_cache_page_write_begin(,,AOP_FLAG_NOFS), so testing 907283aba9fSMel Gorman * may_enter_fs here is liable to OOM on them. 908283aba9fSMel Gorman * 909283aba9fSMel Gorman * 3) memcg encounters a page that is not already marked 910283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 911283aba9fSMel Gorman * throttling so we could easily OOM just because too many 912283aba9fSMel Gorman * pages are in writeback and there is nothing else to 913283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 914e62e384eSMichal Hocko */ 915283aba9fSMel Gorman if (PageWriteback(page)) { 916283aba9fSMel Gorman /* Case 1 above */ 917283aba9fSMel Gorman if (current_is_kswapd() && 918283aba9fSMel Gorman PageReclaim(page) && 919283aba9fSMel Gorman zone_is_reclaim_writeback(zone)) { 920b1a6f21eSMel Gorman nr_immediate++; 921b1a6f21eSMel Gorman goto keep_locked; 922283aba9fSMel Gorman 923283aba9fSMel Gorman /* Case 2 above */ 924283aba9fSMel Gorman } else if (global_reclaim(sc) || 925c3b94f44SHugh Dickins !PageReclaim(page) || !(sc->gfp_mask & __GFP_IO)) { 926c3b94f44SHugh Dickins /* 927c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 928c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 929c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 930c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 931c3b94f44SHugh Dickins * enough to care. What we do want is for this 932c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 933c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 934c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 935c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 936c3b94f44SHugh Dickins */ 937c3b94f44SHugh Dickins SetPageReclaim(page); 93892df3a72SMel Gorman nr_writeback++; 939283aba9fSMel Gorman 940c3b94f44SHugh Dickins goto keep_locked; 941283aba9fSMel Gorman 942283aba9fSMel Gorman /* Case 3 above */ 943283aba9fSMel Gorman } else { 944c3b94f44SHugh Dickins wait_on_page_writeback(page); 945e62e384eSMichal Hocko } 946283aba9fSMel Gorman } 9471da177e4SLinus Torvalds 94802c6de8dSMinchan Kim if (!force_reclaim) 9496a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 95002c6de8dSMinchan Kim 951dfc8d636SJohannes Weiner switch (references) { 952dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 9531da177e4SLinus Torvalds goto activate_locked; 95464574746SJohannes Weiner case PAGEREF_KEEP: 95564574746SJohannes Weiner goto keep_locked; 956dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 957dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 958dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 959dfc8d636SJohannes Weiner } 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds /* 9621da177e4SLinus Torvalds * Anonymous process memory has backing store? 9631da177e4SLinus Torvalds * Try to allocate it some swap space here. 9641da177e4SLinus Torvalds */ 965b291f000SNick Piggin if (PageAnon(page) && !PageSwapCache(page)) { 96663eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 96763eb6b93SHugh Dickins goto keep_locked; 9685bc7b8acSShaohua Li if (!add_to_swap(page, page_list)) 9691da177e4SLinus Torvalds goto activate_locked; 97063eb6b93SHugh Dickins may_enter_fs = 1; 9711da177e4SLinus Torvalds 972e2be15f6SMel Gorman /* Adding to swap updated mapping */ 9731da177e4SLinus Torvalds mapping = page_mapping(page); 974e2be15f6SMel Gorman } 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds /* 9771da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 9781da177e4SLinus Torvalds * processes. Try to unmap it here. 9791da177e4SLinus Torvalds */ 9801da177e4SLinus Torvalds if (page_mapped(page) && mapping) { 98102c6de8dSMinchan Kim switch (try_to_unmap(page, ttu_flags)) { 9821da177e4SLinus Torvalds case SWAP_FAIL: 9831da177e4SLinus Torvalds goto activate_locked; 9841da177e4SLinus Torvalds case SWAP_AGAIN: 9851da177e4SLinus Torvalds goto keep_locked; 986b291f000SNick Piggin case SWAP_MLOCK: 987b291f000SNick Piggin goto cull_mlocked; 9881da177e4SLinus Torvalds case SWAP_SUCCESS: 9891da177e4SLinus Torvalds ; /* try to free the page below */ 9901da177e4SLinus Torvalds } 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds if (PageDirty(page)) { 994ee72886dSMel Gorman /* 995ee72886dSMel Gorman * Only kswapd can writeback filesystem pages to 996d43006d5SMel Gorman * avoid risk of stack overflow but only writeback 997d43006d5SMel Gorman * if many dirty pages have been encountered. 998ee72886dSMel Gorman */ 999f84f6e2bSMel Gorman if (page_is_file_cache(page) && 10009e3b2f8cSKonstantin Khlebnikov (!current_is_kswapd() || 1001d43006d5SMel Gorman !zone_is_reclaim_dirty(zone))) { 100249ea7eb6SMel Gorman /* 100349ea7eb6SMel Gorman * Immediately reclaim when written back. 100449ea7eb6SMel Gorman * Similar in principal to deactivate_page() 100549ea7eb6SMel Gorman * except we already have the page isolated 100649ea7eb6SMel Gorman * and know it's dirty 100749ea7eb6SMel Gorman */ 100849ea7eb6SMel Gorman inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE); 100949ea7eb6SMel Gorman SetPageReclaim(page); 101049ea7eb6SMel Gorman 1011ee72886dSMel Gorman goto keep_locked; 1012ee72886dSMel Gorman } 1013ee72886dSMel Gorman 1014dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 10151da177e4SLinus Torvalds goto keep_locked; 10164dd4b920SAndrew Morton if (!may_enter_fs) 10171da177e4SLinus Torvalds goto keep_locked; 101852a8363eSChristoph Lameter if (!sc->may_writepage) 10191da177e4SLinus Torvalds goto keep_locked; 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds /* Page is dirty, try to write it out here */ 10227d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 10231da177e4SLinus Torvalds case PAGE_KEEP: 10241da177e4SLinus Torvalds goto keep_locked; 10251da177e4SLinus Torvalds case PAGE_ACTIVATE: 10261da177e4SLinus Torvalds goto activate_locked; 10271da177e4SLinus Torvalds case PAGE_SUCCESS: 10287d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 102941ac1999SMel Gorman goto keep; 10307d3579e8SKOSAKI Motohiro if (PageDirty(page)) 10311da177e4SLinus Torvalds goto keep; 10327d3579e8SKOSAKI Motohiro 10331da177e4SLinus Torvalds /* 10341da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 10351da177e4SLinus Torvalds * ahead and try to reclaim the page. 10361da177e4SLinus Torvalds */ 1037529ae9aaSNick Piggin if (!trylock_page(page)) 10381da177e4SLinus Torvalds goto keep; 10391da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 10401da177e4SLinus Torvalds goto keep_locked; 10411da177e4SLinus Torvalds mapping = page_mapping(page); 10421da177e4SLinus Torvalds case PAGE_CLEAN: 10431da177e4SLinus Torvalds ; /* try to free the page below */ 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds /* 10481da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 10491da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 10501da177e4SLinus Torvalds * the page as well. 10511da177e4SLinus Torvalds * 10521da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 10531da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 10541da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 10551da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 10561da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 10571da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 10581da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 10591da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 10601da177e4SLinus Torvalds * 10611da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 10621da177e4SLinus Torvalds * the pages which were not successfully invalidated in 10631da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 10641da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 10651da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 10661da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 10671da177e4SLinus Torvalds */ 1068266cf658SDavid Howells if (page_has_private(page)) { 10691da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 10701da177e4SLinus Torvalds goto activate_locked; 1071e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1072e286781dSNick Piggin unlock_page(page); 1073e286781dSNick Piggin if (put_page_testzero(page)) 10741da177e4SLinus Torvalds goto free_it; 1075e286781dSNick Piggin else { 1076e286781dSNick Piggin /* 1077e286781dSNick Piggin * rare race with speculative reference. 1078e286781dSNick Piggin * the speculative reference will free 1079e286781dSNick Piggin * this page shortly, so we may 1080e286781dSNick Piggin * increment nr_reclaimed here (and 1081e286781dSNick Piggin * leave it off the LRU). 1082e286781dSNick Piggin */ 1083e286781dSNick Piggin nr_reclaimed++; 1084e286781dSNick Piggin continue; 1085e286781dSNick Piggin } 1086e286781dSNick Piggin } 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 1089a528910eSJohannes Weiner if (!mapping || !__remove_mapping(mapping, page, true)) 109049d2e9ccSChristoph Lameter goto keep_locked; 10911da177e4SLinus Torvalds 1092a978d6f5SNick Piggin /* 1093a978d6f5SNick Piggin * At this point, we have no other references and there is 1094a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 1095a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 1096a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 1097a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 1098a978d6f5SNick Piggin */ 1099a978d6f5SNick Piggin __clear_page_locked(page); 1100e286781dSNick Piggin free_it: 110105ff5137SAndrew Morton nr_reclaimed++; 1102abe4c3b5SMel Gorman 1103abe4c3b5SMel Gorman /* 1104abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1105abe4c3b5SMel Gorman * appear not as the counts should be low 1106abe4c3b5SMel Gorman */ 1107abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 11081da177e4SLinus Torvalds continue; 11091da177e4SLinus Torvalds 1110b291f000SNick Piggin cull_mlocked: 111163d6c5adSHugh Dickins if (PageSwapCache(page)) 111263d6c5adSHugh Dickins try_to_free_swap(page); 1113b291f000SNick Piggin unlock_page(page); 1114b291f000SNick Piggin putback_lru_page(page); 1115b291f000SNick Piggin continue; 1116b291f000SNick Piggin 11171da177e4SLinus Torvalds activate_locked: 111868a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 111968a22394SRik van Riel if (PageSwapCache(page) && vm_swap_full()) 1120a2c43eedSHugh Dickins try_to_free_swap(page); 1121309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 11221da177e4SLinus Torvalds SetPageActive(page); 11231da177e4SLinus Torvalds pgactivate++; 11241da177e4SLinus Torvalds keep_locked: 11251da177e4SLinus Torvalds unlock_page(page); 11261da177e4SLinus Torvalds keep: 11271da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1128309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 11291da177e4SLinus Torvalds } 1130abe4c3b5SMel Gorman 1131b745bc85SMel Gorman free_hot_cold_page_list(&free_pages, true); 1132abe4c3b5SMel Gorman 11331da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1134f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 113569980e31STim Chen mem_cgroup_uncharge_end(); 11368e950282SMel Gorman *ret_nr_dirty += nr_dirty; 11378e950282SMel Gorman *ret_nr_congested += nr_congested; 1138d43006d5SMel Gorman *ret_nr_unqueued_dirty += nr_unqueued_dirty; 113992df3a72SMel Gorman *ret_nr_writeback += nr_writeback; 1140b1a6f21eSMel Gorman *ret_nr_immediate += nr_immediate; 114105ff5137SAndrew Morton return nr_reclaimed; 11421da177e4SLinus Torvalds } 11431da177e4SLinus Torvalds 114402c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone, 114502c6de8dSMinchan Kim struct list_head *page_list) 114602c6de8dSMinchan Kim { 114702c6de8dSMinchan Kim struct scan_control sc = { 114802c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 114902c6de8dSMinchan Kim .priority = DEF_PRIORITY, 115002c6de8dSMinchan Kim .may_unmap = 1, 115102c6de8dSMinchan Kim }; 11528e950282SMel Gorman unsigned long ret, dummy1, dummy2, dummy3, dummy4, dummy5; 115302c6de8dSMinchan Kim struct page *page, *next; 115402c6de8dSMinchan Kim LIST_HEAD(clean_pages); 115502c6de8dSMinchan Kim 115602c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1157117aad1eSRafael Aquini if (page_is_file_cache(page) && !PageDirty(page) && 1158117aad1eSRafael Aquini !isolated_balloon_page(page)) { 115902c6de8dSMinchan Kim ClearPageActive(page); 116002c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 116102c6de8dSMinchan Kim } 116202c6de8dSMinchan Kim } 116302c6de8dSMinchan Kim 116402c6de8dSMinchan Kim ret = shrink_page_list(&clean_pages, zone, &sc, 116502c6de8dSMinchan Kim TTU_UNMAP|TTU_IGNORE_ACCESS, 11668e950282SMel Gorman &dummy1, &dummy2, &dummy3, &dummy4, &dummy5, true); 116702c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 116883da7510SChristoph Lameter mod_zone_page_state(zone, NR_ISOLATED_FILE, -ret); 116902c6de8dSMinchan Kim return ret; 117002c6de8dSMinchan Kim } 117102c6de8dSMinchan Kim 11725ad333ebSAndy Whitcroft /* 11735ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 11745ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 11755ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 11765ad333ebSAndy Whitcroft * 11775ad333ebSAndy Whitcroft * page: page to consider 11785ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 11795ad333ebSAndy Whitcroft * 11805ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 11815ad333ebSAndy Whitcroft */ 1182f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 11835ad333ebSAndy Whitcroft { 11845ad333ebSAndy Whitcroft int ret = -EINVAL; 11855ad333ebSAndy Whitcroft 11865ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 11875ad333ebSAndy Whitcroft if (!PageLRU(page)) 11885ad333ebSAndy Whitcroft return ret; 11895ad333ebSAndy Whitcroft 1190e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1191e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1192894bc310SLee Schermerhorn return ret; 1193894bc310SLee Schermerhorn 11945ad333ebSAndy Whitcroft ret = -EBUSY; 119508e552c6SKAMEZAWA Hiroyuki 1196c8244935SMel Gorman /* 1197c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1198c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1199c8244935SMel Gorman * blocking - clean pages for the most part. 1200c8244935SMel Gorman * 1201c8244935SMel Gorman * ISOLATE_CLEAN means that only clean pages should be isolated. This 1202c8244935SMel Gorman * is used by reclaim when it is cannot write to backing storage 1203c8244935SMel Gorman * 1204c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1205c8244935SMel Gorman * that it is possible to migrate without blocking 1206c8244935SMel Gorman */ 1207c8244935SMel Gorman if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) { 1208c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1209c8244935SMel Gorman if (PageWriteback(page)) 121039deaf85SMinchan Kim return ret; 121139deaf85SMinchan Kim 1212c8244935SMel Gorman if (PageDirty(page)) { 1213c8244935SMel Gorman struct address_space *mapping; 1214c8244935SMel Gorman 1215c8244935SMel Gorman /* ISOLATE_CLEAN means only clean pages */ 1216c8244935SMel Gorman if (mode & ISOLATE_CLEAN) 1217c8244935SMel Gorman return ret; 1218c8244935SMel Gorman 1219c8244935SMel Gorman /* 1220c8244935SMel Gorman * Only pages without mappings or that have a 1221c8244935SMel Gorman * ->migratepage callback are possible to migrate 1222c8244935SMel Gorman * without blocking 1223c8244935SMel Gorman */ 1224c8244935SMel Gorman mapping = page_mapping(page); 1225c8244935SMel Gorman if (mapping && !mapping->a_ops->migratepage) 1226c8244935SMel Gorman return ret; 1227c8244935SMel Gorman } 1228c8244935SMel Gorman } 1229c8244935SMel Gorman 1230f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1231f80c0673SMinchan Kim return ret; 1232f80c0673SMinchan Kim 12335ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 12345ad333ebSAndy Whitcroft /* 12355ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 12365ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 12375ad333ebSAndy Whitcroft * page release code relies on it. 12385ad333ebSAndy Whitcroft */ 12395ad333ebSAndy Whitcroft ClearPageLRU(page); 12405ad333ebSAndy Whitcroft ret = 0; 12415ad333ebSAndy Whitcroft } 12425ad333ebSAndy Whitcroft 12435ad333ebSAndy Whitcroft return ret; 12445ad333ebSAndy Whitcroft } 12455ad333ebSAndy Whitcroft 124649d2e9ccSChristoph Lameter /* 12471da177e4SLinus Torvalds * zone->lru_lock is heavily contended. Some of the functions that 12481da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 12491da177e4SLinus Torvalds * and working on them outside the LRU lock. 12501da177e4SLinus Torvalds * 12511da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 12521da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 12531da177e4SLinus Torvalds * 12541da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 12551da177e4SLinus Torvalds * 12561da177e4SLinus Torvalds * @nr_to_scan: The number of pages to look through on the list. 12575dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 12581da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1259f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1260fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 12615ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 12623cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 12631da177e4SLinus Torvalds * 12641da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 12651da177e4SLinus Torvalds */ 126669e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 12675dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1268fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 12693cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 12701da177e4SLinus Torvalds { 127175b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 127269e05944SAndrew Morton unsigned long nr_taken = 0; 1273c9b02d97SWu Fengguang unsigned long scan; 12741da177e4SLinus Torvalds 1275c9b02d97SWu Fengguang for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { 12765ad333ebSAndy Whitcroft struct page *page; 1277fa9add64SHugh Dickins int nr_pages; 12785ad333ebSAndy Whitcroft 12791da177e4SLinus Torvalds page = lru_to_page(src); 12801da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 12811da177e4SLinus Torvalds 1282309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 12838d438f96SNick Piggin 1284f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 12855ad333ebSAndy Whitcroft case 0: 1286fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1287fa9add64SHugh Dickins mem_cgroup_update_lru_size(lruvec, lru, -nr_pages); 12885ad333ebSAndy Whitcroft list_move(&page->lru, dst); 1289fa9add64SHugh Dickins nr_taken += nr_pages; 12905ad333ebSAndy Whitcroft break; 12917c8ee9a8SNick Piggin 12925ad333ebSAndy Whitcroft case -EBUSY: 12935ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 12945ad333ebSAndy Whitcroft list_move(&page->lru, src); 12955ad333ebSAndy Whitcroft continue; 12965ad333ebSAndy Whitcroft 12975ad333ebSAndy Whitcroft default: 12985ad333ebSAndy Whitcroft BUG(); 12995ad333ebSAndy Whitcroft } 13005ad333ebSAndy Whitcroft } 13011da177e4SLinus Torvalds 1302f626012dSHugh Dickins *nr_scanned = scan; 130375b00af7SHugh Dickins trace_mm_vmscan_lru_isolate(sc->order, nr_to_scan, scan, 130475b00af7SHugh Dickins nr_taken, mode, is_file_lru(lru)); 13051da177e4SLinus Torvalds return nr_taken; 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds 130862695a84SNick Piggin /** 130962695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 131062695a84SNick Piggin * @page: page to isolate from its LRU list 131162695a84SNick Piggin * 131262695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 131362695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 131462695a84SNick Piggin * 131562695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 131662695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 131762695a84SNick Piggin * 131862695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1319894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1320894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1321894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 132262695a84SNick Piggin * 132362695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 132462695a84SNick Piggin * found will be decremented. 132562695a84SNick Piggin * 132662695a84SNick Piggin * Restrictions: 132762695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 132862695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 132962695a84SNick Piggin * without a stable reference). 133062695a84SNick Piggin * (2) the lru_lock must not be held. 133162695a84SNick Piggin * (3) interrupts must be enabled. 133262695a84SNick Piggin */ 133362695a84SNick Piggin int isolate_lru_page(struct page *page) 133462695a84SNick Piggin { 133562695a84SNick Piggin int ret = -EBUSY; 133662695a84SNick Piggin 1337309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 13380c917313SKonstantin Khlebnikov 133962695a84SNick Piggin if (PageLRU(page)) { 134062695a84SNick Piggin struct zone *zone = page_zone(page); 1341fa9add64SHugh Dickins struct lruvec *lruvec; 134262695a84SNick Piggin 134362695a84SNick Piggin spin_lock_irq(&zone->lru_lock); 1344fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 13450c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1346894bc310SLee Schermerhorn int lru = page_lru(page); 13470c917313SKonstantin Khlebnikov get_page(page); 134862695a84SNick Piggin ClearPageLRU(page); 1349fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1350fa9add64SHugh Dickins ret = 0; 135162695a84SNick Piggin } 135262695a84SNick Piggin spin_unlock_irq(&zone->lru_lock); 135362695a84SNick Piggin } 135462695a84SNick Piggin return ret; 135562695a84SNick Piggin } 135662695a84SNick Piggin 13575ad333ebSAndy Whitcroft /* 1358d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1359d37dd5dcSFengguang Wu * then get resheduled. When there are massive number of tasks doing page 1360d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1361d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1362d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 136335cd7815SRik van Riel */ 136435cd7815SRik van Riel static int too_many_isolated(struct zone *zone, int file, 136535cd7815SRik van Riel struct scan_control *sc) 136635cd7815SRik van Riel { 136735cd7815SRik van Riel unsigned long inactive, isolated; 136835cd7815SRik van Riel 136935cd7815SRik van Riel if (current_is_kswapd()) 137035cd7815SRik van Riel return 0; 137135cd7815SRik van Riel 137289b5fae5SJohannes Weiner if (!global_reclaim(sc)) 137335cd7815SRik van Riel return 0; 137435cd7815SRik van Riel 137535cd7815SRik van Riel if (file) { 137635cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_FILE); 137735cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_FILE); 137835cd7815SRik van Riel } else { 137935cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_ANON); 138035cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_ANON); 138135cd7815SRik van Riel } 138235cd7815SRik van Riel 13833cf23841SFengguang Wu /* 13843cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 13853cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 13863cf23841SFengguang Wu * deadlock. 13873cf23841SFengguang Wu */ 13883cf23841SFengguang Wu if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS) 13893cf23841SFengguang Wu inactive >>= 3; 13903cf23841SFengguang Wu 139135cd7815SRik van Riel return isolated > inactive; 139235cd7815SRik van Riel } 139335cd7815SRik van Riel 139466635629SMel Gorman static noinline_for_stack void 139575b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) 139666635629SMel Gorman { 139727ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 139827ac81d8SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 13993f79768fSHugh Dickins LIST_HEAD(pages_to_free); 140066635629SMel Gorman 140166635629SMel Gorman /* 140266635629SMel Gorman * Put back any unfreeable pages. 140366635629SMel Gorman */ 140466635629SMel Gorman while (!list_empty(page_list)) { 14053f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 140666635629SMel Gorman int lru; 14073f79768fSHugh Dickins 1408309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 140966635629SMel Gorman list_del(&page->lru); 141039b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 141166635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 141266635629SMel Gorman putback_lru_page(page); 141366635629SMel Gorman spin_lock_irq(&zone->lru_lock); 141466635629SMel Gorman continue; 141566635629SMel Gorman } 1416fa9add64SHugh Dickins 1417fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 1418fa9add64SHugh Dickins 14197a608572SLinus Torvalds SetPageLRU(page); 142066635629SMel Gorman lru = page_lru(page); 1421fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 1422fa9add64SHugh Dickins 142366635629SMel Gorman if (is_active_lru(lru)) { 142466635629SMel Gorman int file = is_file_lru(lru); 14259992af10SRik van Riel int numpages = hpage_nr_pages(page); 14269992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 142766635629SMel Gorman } 14282bcf8879SHugh Dickins if (put_page_testzero(page)) { 14292bcf8879SHugh Dickins __ClearPageLRU(page); 14302bcf8879SHugh Dickins __ClearPageActive(page); 1431fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 14322bcf8879SHugh Dickins 14332bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 143466635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 14352bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 143666635629SMel Gorman spin_lock_irq(&zone->lru_lock); 14372bcf8879SHugh Dickins } else 14382bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 143966635629SMel Gorman } 144066635629SMel Gorman } 144166635629SMel Gorman 14423f79768fSHugh Dickins /* 14433f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 14443f79768fSHugh Dickins */ 14453f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 144666635629SMel Gorman } 144766635629SMel Gorman 144866635629SMel Gorman /* 1449399ba0b9SNeilBrown * If a kernel thread (such as nfsd for loop-back mounts) services 1450399ba0b9SNeilBrown * a backing device by writing to the page cache it sets PF_LESS_THROTTLE. 1451399ba0b9SNeilBrown * In that case we should only throttle if the backing device it is 1452399ba0b9SNeilBrown * writing to is congested. In other cases it is safe to throttle. 1453399ba0b9SNeilBrown */ 1454399ba0b9SNeilBrown static int current_may_throttle(void) 1455399ba0b9SNeilBrown { 1456399ba0b9SNeilBrown return !(current->flags & PF_LESS_THROTTLE) || 1457399ba0b9SNeilBrown current->backing_dev_info == NULL || 1458399ba0b9SNeilBrown bdi_write_congested(current->backing_dev_info); 1459399ba0b9SNeilBrown } 1460399ba0b9SNeilBrown 1461399ba0b9SNeilBrown /* 14621742f19fSAndrew Morton * shrink_inactive_list() is a helper for shrink_zone(). It returns the number 14631742f19fSAndrew Morton * of reclaimed pages 14641da177e4SLinus Torvalds */ 146566635629SMel Gorman static noinline_for_stack unsigned long 14661a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 14679e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 14681da177e4SLinus Torvalds { 14691da177e4SLinus Torvalds LIST_HEAD(page_list); 1470e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 147105ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1472e247dbceSKOSAKI Motohiro unsigned long nr_taken; 14738e950282SMel Gorman unsigned long nr_dirty = 0; 14748e950282SMel Gorman unsigned long nr_congested = 0; 1475e2be15f6SMel Gorman unsigned long nr_unqueued_dirty = 0; 147692df3a72SMel Gorman unsigned long nr_writeback = 0; 1477b1a6f21eSMel Gorman unsigned long nr_immediate = 0; 1478f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 14793cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 14801a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 14811a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 148278dc583dSKOSAKI Motohiro 148335cd7815SRik van Riel while (unlikely(too_many_isolated(zone, file, sc))) { 148458355c78SKOSAKI Motohiro congestion_wait(BLK_RW_ASYNC, HZ/10); 148535cd7815SRik van Riel 148635cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 148735cd7815SRik van Riel if (fatal_signal_pending(current)) 148835cd7815SRik van Riel return SWAP_CLUSTER_MAX; 148935cd7815SRik van Riel } 149035cd7815SRik van Riel 14911da177e4SLinus Torvalds lru_add_drain(); 1492f80c0673SMinchan Kim 1493f80c0673SMinchan Kim if (!sc->may_unmap) 149461317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1495f80c0673SMinchan Kim if (!sc->may_writepage) 149661317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1497f80c0673SMinchan Kim 14981da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 14991da177e4SLinus Torvalds 15005dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 15015dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 150295d918fcSKonstantin Khlebnikov 150395d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 150495d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 150595d918fcSKonstantin Khlebnikov 150689b5fae5SJohannes Weiner if (global_reclaim(sc)) { 1507e247dbceSKOSAKI Motohiro zone->pages_scanned += nr_scanned; 1508b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 150975b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scanned); 1510b35ea17bSKOSAKI Motohiro else 151175b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scanned); 1512b35ea17bSKOSAKI Motohiro } 151366635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 1514d563c050SHillf Danton 1515d563c050SHillf Danton if (nr_taken == 0) 151666635629SMel Gorman return 0; 1517b35ea17bSKOSAKI Motohiro 151802c6de8dSMinchan Kim nr_reclaimed = shrink_page_list(&page_list, zone, sc, TTU_UNMAP, 15198e950282SMel Gorman &nr_dirty, &nr_unqueued_dirty, &nr_congested, 15208e950282SMel Gorman &nr_writeback, &nr_immediate, 1521b1a6f21eSMel Gorman false); 1522c661b078SAndy Whitcroft 15233f79768fSHugh Dickins spin_lock_irq(&zone->lru_lock); 15243f79768fSHugh Dickins 152595d918fcSKonstantin Khlebnikov reclaim_stat->recent_scanned[file] += nr_taken; 1526d563c050SHillf Danton 1527904249aaSYing Han if (global_reclaim(sc)) { 1528b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1529904249aaSYing Han __count_zone_vm_events(PGSTEAL_KSWAPD, zone, 1530904249aaSYing Han nr_reclaimed); 1531904249aaSYing Han else 1532904249aaSYing Han __count_zone_vm_events(PGSTEAL_DIRECT, zone, 1533904249aaSYing Han nr_reclaimed); 1534904249aaSYing Han } 1535a74609faSNick Piggin 153627ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 15373f79768fSHugh Dickins 153895d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 15393f79768fSHugh Dickins 15403f79768fSHugh Dickins spin_unlock_irq(&zone->lru_lock); 15413f79768fSHugh Dickins 1542b745bc85SMel Gorman free_hot_cold_page_list(&page_list, true); 1543e11da5b4SMel Gorman 154492df3a72SMel Gorman /* 154592df3a72SMel Gorman * If reclaim is isolating dirty pages under writeback, it implies 154692df3a72SMel Gorman * that the long-lived page allocation rate is exceeding the page 154792df3a72SMel Gorman * laundering rate. Either the global limits are not being effective 154892df3a72SMel Gorman * at throttling processes due to the page distribution throughout 154992df3a72SMel Gorman * zones or there is heavy usage of a slow backing device. The 155092df3a72SMel Gorman * only option is to throttle from reclaim context which is not ideal 155192df3a72SMel Gorman * as there is no guarantee the dirtying process is throttled in the 155292df3a72SMel Gorman * same way balance_dirty_pages() manages. 155392df3a72SMel Gorman * 15548e950282SMel Gorman * Once a zone is flagged ZONE_WRITEBACK, kswapd will count the number 15558e950282SMel Gorman * of pages under pages flagged for immediate reclaim and stall if any 15568e950282SMel Gorman * are encountered in the nr_immediate check below. 155792df3a72SMel Gorman */ 1558918fc718SMel Gorman if (nr_writeback && nr_writeback == nr_taken) 1559283aba9fSMel Gorman zone_set_flag(zone, ZONE_WRITEBACK); 156092df3a72SMel Gorman 1561d43006d5SMel Gorman /* 1562b1a6f21eSMel Gorman * memcg will stall in page writeback so only consider forcibly 1563b1a6f21eSMel Gorman * stalling for global reclaim 1564d43006d5SMel Gorman */ 1565b1a6f21eSMel Gorman if (global_reclaim(sc)) { 1566b1a6f21eSMel Gorman /* 15678e950282SMel Gorman * Tag a zone as congested if all the dirty pages scanned were 15688e950282SMel Gorman * backed by a congested BDI and wait_iff_congested will stall. 15698e950282SMel Gorman */ 15708e950282SMel Gorman if (nr_dirty && nr_dirty == nr_congested) 15718e950282SMel Gorman zone_set_flag(zone, ZONE_CONGESTED); 15728e950282SMel Gorman 15738e950282SMel Gorman /* 1574b1a6f21eSMel Gorman * If dirty pages are scanned that are not queued for IO, it 1575b1a6f21eSMel Gorman * implies that flushers are not keeping up. In this case, flag 1576b1a6f21eSMel Gorman * the zone ZONE_TAIL_LRU_DIRTY and kswapd will start writing 1577b738d764SLinus Torvalds * pages from reclaim context. 1578b1a6f21eSMel Gorman */ 1579b1a6f21eSMel Gorman if (nr_unqueued_dirty == nr_taken) 1580d43006d5SMel Gorman zone_set_flag(zone, ZONE_TAIL_LRU_DIRTY); 1581b1a6f21eSMel Gorman 1582b1a6f21eSMel Gorman /* 1583b738d764SLinus Torvalds * If kswapd scans pages marked marked for immediate 1584b738d764SLinus Torvalds * reclaim and under writeback (nr_immediate), it implies 1585b738d764SLinus Torvalds * that pages are cycling through the LRU faster than 1586b1a6f21eSMel Gorman * they are written so also forcibly stall. 1587b1a6f21eSMel Gorman */ 1588b738d764SLinus Torvalds if (nr_immediate && current_may_throttle()) 1589b1a6f21eSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 1590e2be15f6SMel Gorman } 1591d43006d5SMel Gorman 15928e950282SMel Gorman /* 15938e950282SMel Gorman * Stall direct reclaim for IO completions if underlying BDIs or zone 15948e950282SMel Gorman * is congested. Allow kswapd to continue until it starts encountering 15958e950282SMel Gorman * unqueued dirty pages or cycling through the LRU too quickly. 15968e950282SMel Gorman */ 1597399ba0b9SNeilBrown if (!sc->hibernation_mode && !current_is_kswapd() && 1598399ba0b9SNeilBrown current_may_throttle()) 15998e950282SMel Gorman wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10); 16008e950282SMel Gorman 1601e11da5b4SMel Gorman trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id, 1602e11da5b4SMel Gorman zone_idx(zone), 1603e11da5b4SMel Gorman nr_scanned, nr_reclaimed, 16049e3b2f8cSKonstantin Khlebnikov sc->priority, 160523b9da55SMel Gorman trace_shrink_flags(file)); 160605ff5137SAndrew Morton return nr_reclaimed; 16071da177e4SLinus Torvalds } 16081da177e4SLinus Torvalds 16093bb1a852SMartin Bligh /* 16101cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 16111cfb419bSKAMEZAWA Hiroyuki * 16121cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 16131cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 16141cfb419bSKAMEZAWA Hiroyuki * 16151cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 16161cfb419bSKAMEZAWA Hiroyuki * appropriate to hold zone->lru_lock across the whole operation. But if 16171cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 16181cfb419bSKAMEZAWA Hiroyuki * should drop zone->lru_lock around each page. It's impossible to balance 16191cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 16201cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 16211cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 16221cfb419bSKAMEZAWA Hiroyuki * 16231cfb419bSKAMEZAWA Hiroyuki * The downside is that we have to touch page->_count against each page. 16241cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 16251cfb419bSKAMEZAWA Hiroyuki */ 16261cfb419bSKAMEZAWA Hiroyuki 1627fa9add64SHugh Dickins static void move_active_pages_to_lru(struct lruvec *lruvec, 16283eb4140fSWu Fengguang struct list_head *list, 16292bcf8879SHugh Dickins struct list_head *pages_to_free, 16303eb4140fSWu Fengguang enum lru_list lru) 16313eb4140fSWu Fengguang { 1632fa9add64SHugh Dickins struct zone *zone = lruvec_zone(lruvec); 16333eb4140fSWu Fengguang unsigned long pgmoved = 0; 16343eb4140fSWu Fengguang struct page *page; 1635fa9add64SHugh Dickins int nr_pages; 16363eb4140fSWu Fengguang 16373eb4140fSWu Fengguang while (!list_empty(list)) { 16383eb4140fSWu Fengguang page = lru_to_page(list); 1639fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 16403eb4140fSWu Fengguang 1641309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 16423eb4140fSWu Fengguang SetPageLRU(page); 16433eb4140fSWu Fengguang 1644fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1645fa9add64SHugh Dickins mem_cgroup_update_lru_size(lruvec, lru, nr_pages); 1646925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 1647fa9add64SHugh Dickins pgmoved += nr_pages; 16483eb4140fSWu Fengguang 16492bcf8879SHugh Dickins if (put_page_testzero(page)) { 16502bcf8879SHugh Dickins __ClearPageLRU(page); 16512bcf8879SHugh Dickins __ClearPageActive(page); 1652fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 16532bcf8879SHugh Dickins 16542bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 16553eb4140fSWu Fengguang spin_unlock_irq(&zone->lru_lock); 16562bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 16573eb4140fSWu Fengguang spin_lock_irq(&zone->lru_lock); 16582bcf8879SHugh Dickins } else 16592bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 16603eb4140fSWu Fengguang } 16613eb4140fSWu Fengguang } 16623eb4140fSWu Fengguang __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved); 16633eb4140fSWu Fengguang if (!is_active_lru(lru)) 16643eb4140fSWu Fengguang __count_vm_events(PGDEACTIVATE, pgmoved); 16653eb4140fSWu Fengguang } 16661cfb419bSKAMEZAWA Hiroyuki 1667f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 16681a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 1669f16015fbSJohannes Weiner struct scan_control *sc, 16709e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 16711cfb419bSKAMEZAWA Hiroyuki { 167244c241f1SKOSAKI Motohiro unsigned long nr_taken; 1673f626012dSHugh Dickins unsigned long nr_scanned; 16746fe6b7e3SWu Fengguang unsigned long vm_flags; 16751cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 16768cab4754SWu Fengguang LIST_HEAD(l_active); 1677b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 16781cfb419bSKAMEZAWA Hiroyuki struct page *page; 16791a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 168044c241f1SKOSAKI Motohiro unsigned long nr_rotated = 0; 1681f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 16823cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 16831a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 16841cfb419bSKAMEZAWA Hiroyuki 16851da177e4SLinus Torvalds lru_add_drain(); 1686f80c0673SMinchan Kim 1687f80c0673SMinchan Kim if (!sc->may_unmap) 168861317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1689f80c0673SMinchan Kim if (!sc->may_writepage) 169061317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1691f80c0673SMinchan Kim 16921da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 1693925b7673SJohannes Weiner 16945dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 16955dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 169689b5fae5SJohannes Weiner if (global_reclaim(sc)) 1697f626012dSHugh Dickins zone->pages_scanned += nr_scanned; 169889b5fae5SJohannes Weiner 1699b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 17001cfb419bSKAMEZAWA Hiroyuki 1701f626012dSHugh Dickins __count_zone_vm_events(PGREFILL, zone, nr_scanned); 17023cb99451SKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 1703a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 17041da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 17051da177e4SLinus Torvalds 17061da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 17071da177e4SLinus Torvalds cond_resched(); 17081da177e4SLinus Torvalds page = lru_to_page(&l_hold); 17091da177e4SLinus Torvalds list_del(&page->lru); 17107e9cd484SRik van Riel 171139b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1712894bc310SLee Schermerhorn putback_lru_page(page); 1713894bc310SLee Schermerhorn continue; 1714894bc310SLee Schermerhorn } 1715894bc310SLee Schermerhorn 1716cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 1717cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 1718cc715d99SMel Gorman if (page_has_private(page)) 1719cc715d99SMel Gorman try_to_release_page(page, 0); 1720cc715d99SMel Gorman unlock_page(page); 1721cc715d99SMel Gorman } 1722cc715d99SMel Gorman } 1723cc715d99SMel Gorman 1724c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 1725c3ac9a8aSJohannes Weiner &vm_flags)) { 17269992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 17278cab4754SWu Fengguang /* 17288cab4754SWu Fengguang * Identify referenced, file-backed active pages and 17298cab4754SWu Fengguang * give them one more trip around the active list. So 17308cab4754SWu Fengguang * that executable code get better chances to stay in 17318cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 17328cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 17338cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 17348cab4754SWu Fengguang * so we ignore them here. 17358cab4754SWu Fengguang */ 173641e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 17378cab4754SWu Fengguang list_add(&page->lru, &l_active); 17388cab4754SWu Fengguang continue; 17398cab4754SWu Fengguang } 17408cab4754SWu Fengguang } 17417e9cd484SRik van Riel 17425205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 17431da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 17441da177e4SLinus Torvalds } 17451da177e4SLinus Torvalds 1746b555749aSAndrew Morton /* 17478cab4754SWu Fengguang * Move pages back to the lru list. 1748b555749aSAndrew Morton */ 17492a1dc509SJohannes Weiner spin_lock_irq(&zone->lru_lock); 17504f98a2feSRik van Riel /* 17518cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 17528cab4754SWu Fengguang * even though only some of them are actually re-activated. This 17538cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 17548cab4754SWu Fengguang * get_scan_ratio. 1755556adecbSRik van Riel */ 1756b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 1757556adecbSRik van Riel 1758fa9add64SHugh Dickins move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru); 1759fa9add64SHugh Dickins move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE); 1760a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 1761f8891e5eSChristoph Lameter spin_unlock_irq(&zone->lru_lock); 17622bcf8879SHugh Dickins 1763b745bc85SMel Gorman free_hot_cold_page_list(&l_hold, true); 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds 176674e3f3c3SMinchan Kim #ifdef CONFIG_SWAP 176714797e23SKOSAKI Motohiro static int inactive_anon_is_low_global(struct zone *zone) 1768f89eb90eSKOSAKI Motohiro { 1769f89eb90eSKOSAKI Motohiro unsigned long active, inactive; 1770f89eb90eSKOSAKI Motohiro 1771f89eb90eSKOSAKI Motohiro active = zone_page_state(zone, NR_ACTIVE_ANON); 1772f89eb90eSKOSAKI Motohiro inactive = zone_page_state(zone, NR_INACTIVE_ANON); 1773f89eb90eSKOSAKI Motohiro 1774f89eb90eSKOSAKI Motohiro if (inactive * zone->inactive_ratio < active) 1775f89eb90eSKOSAKI Motohiro return 1; 1776f89eb90eSKOSAKI Motohiro 1777f89eb90eSKOSAKI Motohiro return 0; 1778f89eb90eSKOSAKI Motohiro } 1779f89eb90eSKOSAKI Motohiro 178014797e23SKOSAKI Motohiro /** 178114797e23SKOSAKI Motohiro * inactive_anon_is_low - check if anonymous pages need to be deactivated 1782c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 178314797e23SKOSAKI Motohiro * 178414797e23SKOSAKI Motohiro * Returns true if the zone does not have enough inactive anon pages, 178514797e23SKOSAKI Motohiro * meaning some active anon pages need to be deactivated. 178614797e23SKOSAKI Motohiro */ 1787c56d5c7dSKonstantin Khlebnikov static int inactive_anon_is_low(struct lruvec *lruvec) 178814797e23SKOSAKI Motohiro { 178974e3f3c3SMinchan Kim /* 179074e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 179174e3f3c3SMinchan Kim * is pointless. 179274e3f3c3SMinchan Kim */ 179374e3f3c3SMinchan Kim if (!total_swap_pages) 179474e3f3c3SMinchan Kim return 0; 179574e3f3c3SMinchan Kim 1796c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1797c56d5c7dSKonstantin Khlebnikov return mem_cgroup_inactive_anon_is_low(lruvec); 1798f16015fbSJohannes Weiner 1799c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low_global(lruvec_zone(lruvec)); 180014797e23SKOSAKI Motohiro } 180174e3f3c3SMinchan Kim #else 1802c56d5c7dSKonstantin Khlebnikov static inline int inactive_anon_is_low(struct lruvec *lruvec) 180374e3f3c3SMinchan Kim { 180474e3f3c3SMinchan Kim return 0; 180574e3f3c3SMinchan Kim } 180674e3f3c3SMinchan Kim #endif 180714797e23SKOSAKI Motohiro 180856e49d21SRik van Riel /** 180956e49d21SRik van Riel * inactive_file_is_low - check if file pages need to be deactivated 1810c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 181156e49d21SRik van Riel * 181256e49d21SRik van Riel * When the system is doing streaming IO, memory pressure here 181356e49d21SRik van Riel * ensures that active file pages get deactivated, until more 181456e49d21SRik van Riel * than half of the file pages are on the inactive list. 181556e49d21SRik van Riel * 181656e49d21SRik van Riel * Once we get to that situation, protect the system's working 181756e49d21SRik van Riel * set from being evicted by disabling active file page aging. 181856e49d21SRik van Riel * 181956e49d21SRik van Riel * This uses a different ratio than the anonymous pages, because 182056e49d21SRik van Riel * the page cache uses a use-once replacement algorithm. 182156e49d21SRik van Riel */ 1822c56d5c7dSKonstantin Khlebnikov static int inactive_file_is_low(struct lruvec *lruvec) 182356e49d21SRik van Riel { 1824e3790144SJohannes Weiner unsigned long inactive; 1825e3790144SJohannes Weiner unsigned long active; 182656e49d21SRik van Riel 1827e3790144SJohannes Weiner inactive = get_lru_size(lruvec, LRU_INACTIVE_FILE); 1828e3790144SJohannes Weiner active = get_lru_size(lruvec, LRU_ACTIVE_FILE); 1829e3790144SJohannes Weiner 1830e3790144SJohannes Weiner return active > inactive; 183156e49d21SRik van Riel } 183256e49d21SRik van Riel 183375b00af7SHugh Dickins static int inactive_list_is_low(struct lruvec *lruvec, enum lru_list lru) 1834b39415b2SRik van Riel { 183575b00af7SHugh Dickins if (is_file_lru(lru)) 1836c56d5c7dSKonstantin Khlebnikov return inactive_file_is_low(lruvec); 1837b39415b2SRik van Riel else 1838c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low(lruvec); 1839b39415b2SRik van Riel } 1840b39415b2SRik van Riel 18414f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 18421a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, struct scan_control *sc) 1843b69408e8SChristoph Lameter { 1844b39415b2SRik van Riel if (is_active_lru(lru)) { 184575b00af7SHugh Dickins if (inactive_list_is_low(lruvec, lru)) 18461a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 1847556adecbSRik van Riel return 0; 1848556adecbSRik van Riel } 1849556adecbSRik van Riel 18501a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 1851b69408e8SChristoph Lameter } 1852b69408e8SChristoph Lameter 18539a265114SJohannes Weiner enum scan_balance { 18549a265114SJohannes Weiner SCAN_EQUAL, 18559a265114SJohannes Weiner SCAN_FRACT, 18569a265114SJohannes Weiner SCAN_ANON, 18579a265114SJohannes Weiner SCAN_FILE, 18589a265114SJohannes Weiner }; 18599a265114SJohannes Weiner 18601da177e4SLinus Torvalds /* 18614f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 18624f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 18634f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 18644f98a2feSRik van Riel * onto the active list instead of evict. 18654f98a2feSRik van Riel * 1866be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 1867be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 18684f98a2feSRik van Riel */ 186902695175SJohannes Weiner static void get_scan_count(struct lruvec *lruvec, int swappiness, 187002695175SJohannes Weiner struct scan_control *sc, unsigned long *nr) 18714f98a2feSRik van Riel { 187290126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 18739a265114SJohannes Weiner u64 fraction[2]; 18749a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 187590126375SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 18769a265114SJohannes Weiner unsigned long anon_prio, file_prio; 18779a265114SJohannes Weiner enum scan_balance scan_balance; 18780bf1457fSJohannes Weiner unsigned long anon, file; 18799a265114SJohannes Weiner bool force_scan = false; 18809a265114SJohannes Weiner unsigned long ap, fp; 18819a265114SJohannes Weiner enum lru_list lru; 18826f04f48dSSuleiman Souhlal bool some_scanned; 18836f04f48dSSuleiman Souhlal int pass; 1884246e87a9SKAMEZAWA Hiroyuki 1885f11c0ca5SJohannes Weiner /* 1886f11c0ca5SJohannes Weiner * If the zone or memcg is small, nr[l] can be 0. This 1887f11c0ca5SJohannes Weiner * results in no scanning on this priority and a potential 1888f11c0ca5SJohannes Weiner * priority drop. Global direct reclaim can go to the next 1889f11c0ca5SJohannes Weiner * zone and tends to have no problems. Global kswapd is for 1890f11c0ca5SJohannes Weiner * zone balancing and it needs to scan a minimum amount. When 1891f11c0ca5SJohannes Weiner * reclaiming for a memcg, a priority drop can cause high 1892f11c0ca5SJohannes Weiner * latencies, so it's better to scan a minimum amount there as 1893f11c0ca5SJohannes Weiner * well. 1894f11c0ca5SJohannes Weiner */ 18956e543d57SLisa Du if (current_is_kswapd() && !zone_reclaimable(zone)) 1896a4d3e9e7SJohannes Weiner force_scan = true; 189789b5fae5SJohannes Weiner if (!global_reclaim(sc)) 1898a4d3e9e7SJohannes Weiner force_scan = true; 189976a33fc3SShaohua Li 190076a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 1901ec8acf20SShaohua Li if (!sc->may_swap || (get_nr_swap_pages() <= 0)) { 19029a265114SJohannes Weiner scan_balance = SCAN_FILE; 190376a33fc3SShaohua Li goto out; 190476a33fc3SShaohua Li } 19054f98a2feSRik van Riel 190610316b31SJohannes Weiner /* 190710316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 190810316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 190910316b31SJohannes Weiner * disable swapping for individual groups completely when 191010316b31SJohannes Weiner * using the memory controller's swap limit feature would be 191110316b31SJohannes Weiner * too expensive. 191210316b31SJohannes Weiner */ 191302695175SJohannes Weiner if (!global_reclaim(sc) && !swappiness) { 19149a265114SJohannes Weiner scan_balance = SCAN_FILE; 191510316b31SJohannes Weiner goto out; 191610316b31SJohannes Weiner } 191710316b31SJohannes Weiner 191810316b31SJohannes Weiner /* 191910316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 192010316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 192110316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 192210316b31SJohannes Weiner */ 192302695175SJohannes Weiner if (!sc->priority && swappiness) { 19249a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 192510316b31SJohannes Weiner goto out; 192610316b31SJohannes Weiner } 192710316b31SJohannes Weiner 19284d7dcca2SHugh Dickins anon = get_lru_size(lruvec, LRU_ACTIVE_ANON) + 19294d7dcca2SHugh Dickins get_lru_size(lruvec, LRU_INACTIVE_ANON); 19304d7dcca2SHugh Dickins file = get_lru_size(lruvec, LRU_ACTIVE_FILE) + 19314d7dcca2SHugh Dickins get_lru_size(lruvec, LRU_INACTIVE_FILE); 1932a4d3e9e7SJohannes Weiner 193311d16c25SJohannes Weiner /* 193462376251SJohannes Weiner * Prevent the reclaimer from falling into the cache trap: as 193562376251SJohannes Weiner * cache pages start out inactive, every cache fault will tip 193662376251SJohannes Weiner * the scan balance towards the file LRU. And as the file LRU 193762376251SJohannes Weiner * shrinks, so does the window for rotation from references. 193862376251SJohannes Weiner * This means we have a runaway feedback loop where a tiny 193962376251SJohannes Weiner * thrashing file LRU becomes infinitely more attractive than 194062376251SJohannes Weiner * anon pages. Try to detect this based on file LRU size. 194162376251SJohannes Weiner */ 194262376251SJohannes Weiner if (global_reclaim(sc)) { 194362376251SJohannes Weiner unsigned long free = zone_page_state(zone, NR_FREE_PAGES); 194462376251SJohannes Weiner 194562376251SJohannes Weiner if (unlikely(file + free <= high_wmark_pages(zone))) { 194662376251SJohannes Weiner scan_balance = SCAN_ANON; 194762376251SJohannes Weiner goto out; 194862376251SJohannes Weiner } 194962376251SJohannes Weiner } 195062376251SJohannes Weiner 195162376251SJohannes Weiner /* 19527c5bd705SJohannes Weiner * There is enough inactive page cache, do not reclaim 19537c5bd705SJohannes Weiner * anything from the anonymous working set right now. 1954e9868505SRik van Riel */ 19557c5bd705SJohannes Weiner if (!inactive_file_is_low(lruvec)) { 19569a265114SJohannes Weiner scan_balance = SCAN_FILE; 1957e9868505SRik van Riel goto out; 19584f98a2feSRik van Riel } 19594f98a2feSRik van Riel 19609a265114SJohannes Weiner scan_balance = SCAN_FRACT; 19619a265114SJohannes Weiner 19624f98a2feSRik van Riel /* 196358c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 196458c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 196558c37f6eSKOSAKI Motohiro */ 196602695175SJohannes Weiner anon_prio = swappiness; 196775b00af7SHugh Dickins file_prio = 200 - anon_prio; 196858c37f6eSKOSAKI Motohiro 196958c37f6eSKOSAKI Motohiro /* 19704f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 19714f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 19724f98a2feSRik van Riel * ratios to determine how valuable each cache is. 19734f98a2feSRik van Riel * 19744f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 19754f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 19764f98a2feSRik van Riel * up weighing recent references more than old ones. 19774f98a2feSRik van Riel * 19784f98a2feSRik van Riel * anon in [0], file in [1] 19794f98a2feSRik van Riel */ 198090126375SKonstantin Khlebnikov spin_lock_irq(&zone->lru_lock); 198158c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 19826e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 19836e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 19844f98a2feSRik van Riel } 19854f98a2feSRik van Riel 19866e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 19876e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 19886e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 19894f98a2feSRik van Riel } 19904f98a2feSRik van Riel 19914f98a2feSRik van Riel /* 199200d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 199300d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 199400d8089cSRik van Riel * each list that were recently referenced and in active use. 19954f98a2feSRik van Riel */ 1996fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 19976e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 19984f98a2feSRik van Riel 1999fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 20006e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 200190126375SKonstantin Khlebnikov spin_unlock_irq(&zone->lru_lock); 20024f98a2feSRik van Riel 200376a33fc3SShaohua Li fraction[0] = ap; 200476a33fc3SShaohua Li fraction[1] = fp; 200576a33fc3SShaohua Li denominator = ap + fp + 1; 200676a33fc3SShaohua Li out: 20076f04f48dSSuleiman Souhlal some_scanned = false; 20086f04f48dSSuleiman Souhlal /* Only use force_scan on second pass. */ 20096f04f48dSSuleiman Souhlal for (pass = 0; !some_scanned && pass < 2; pass++) { 20104111304dSHugh Dickins for_each_evictable_lru(lru) { 20114111304dSHugh Dickins int file = is_file_lru(lru); 2012d778df51SJohannes Weiner unsigned long size; 201376a33fc3SShaohua Li unsigned long scan; 201476a33fc3SShaohua Li 2015d778df51SJohannes Weiner size = get_lru_size(lruvec, lru); 2016d778df51SJohannes Weiner scan = size >> sc->priority; 20179a265114SJohannes Weiner 20186f04f48dSSuleiman Souhlal if (!scan && pass && force_scan) 2019d778df51SJohannes Weiner scan = min(size, SWAP_CLUSTER_MAX); 20209a265114SJohannes Weiner 20219a265114SJohannes Weiner switch (scan_balance) { 20229a265114SJohannes Weiner case SCAN_EQUAL: 20239a265114SJohannes Weiner /* Scan lists relative to size */ 20249a265114SJohannes Weiner break; 20259a265114SJohannes Weiner case SCAN_FRACT: 20269a265114SJohannes Weiner /* 20279a265114SJohannes Weiner * Scan types proportional to swappiness and 20289a265114SJohannes Weiner * their relative recent reclaim efficiency. 20299a265114SJohannes Weiner */ 20306f04f48dSSuleiman Souhlal scan = div64_u64(scan * fraction[file], 20316f04f48dSSuleiman Souhlal denominator); 20329a265114SJohannes Weiner break; 20339a265114SJohannes Weiner case SCAN_FILE: 20349a265114SJohannes Weiner case SCAN_ANON: 20359a265114SJohannes Weiner /* Scan one type exclusively */ 20369a265114SJohannes Weiner if ((scan_balance == SCAN_FILE) != file) 20379a265114SJohannes Weiner scan = 0; 20389a265114SJohannes Weiner break; 20399a265114SJohannes Weiner default: 20409a265114SJohannes Weiner /* Look ma, no brain */ 20419a265114SJohannes Weiner BUG(); 20429a265114SJohannes Weiner } 20434111304dSHugh Dickins nr[lru] = scan; 20446f04f48dSSuleiman Souhlal /* 20456f04f48dSSuleiman Souhlal * Skip the second pass and don't force_scan, 20466f04f48dSSuleiman Souhlal * if we found something to scan. 20476f04f48dSSuleiman Souhlal */ 20486f04f48dSSuleiman Souhlal some_scanned |= !!scan; 20496f04f48dSSuleiman Souhlal } 205076a33fc3SShaohua Li } 20516e08a369SWu Fengguang } 20524f98a2feSRik van Riel 20539b4f98cdSJohannes Weiner /* 20549b4f98cdSJohannes Weiner * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. 20559b4f98cdSJohannes Weiner */ 205602695175SJohannes Weiner static void shrink_lruvec(struct lruvec *lruvec, int swappiness, 205702695175SJohannes Weiner struct scan_control *sc) 20589b4f98cdSJohannes Weiner { 20599b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 2060e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 20619b4f98cdSJohannes Weiner unsigned long nr_to_scan; 20629b4f98cdSJohannes Weiner enum lru_list lru; 20639b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 20649b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 20659b4f98cdSJohannes Weiner struct blk_plug plug; 20661a501907SMel Gorman bool scan_adjusted; 20679b4f98cdSJohannes Weiner 206802695175SJohannes Weiner get_scan_count(lruvec, swappiness, sc, nr); 20699b4f98cdSJohannes Weiner 2070e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2071e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2072e82e0561SMel Gorman 20731a501907SMel Gorman /* 20741a501907SMel Gorman * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal 20751a501907SMel Gorman * event that can occur when there is little memory pressure e.g. 20761a501907SMel Gorman * multiple streaming readers/writers. Hence, we do not abort scanning 20771a501907SMel Gorman * when the requested number of pages are reclaimed when scanning at 20781a501907SMel Gorman * DEF_PRIORITY on the assumption that the fact we are direct 20791a501907SMel Gorman * reclaiming implies that kswapd is not keeping up and it is best to 20801a501907SMel Gorman * do a batch of work at once. For memcg reclaim one check is made to 20811a501907SMel Gorman * abort proportional reclaim if either the file or anon lru has already 20821a501907SMel Gorman * dropped to zero at the first pass. 20831a501907SMel Gorman */ 20841a501907SMel Gorman scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() && 20851a501907SMel Gorman sc->priority == DEF_PRIORITY); 20861a501907SMel Gorman 20879b4f98cdSJohannes Weiner blk_start_plug(&plug); 20889b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 20899b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2090e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2091e82e0561SMel Gorman unsigned long nr_scanned; 2092e82e0561SMel Gorman 20939b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 20949b4f98cdSJohannes Weiner if (nr[lru]) { 20959b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 20969b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 20979b4f98cdSJohannes Weiner 20989b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 20999b4f98cdSJohannes Weiner lruvec, sc); 21009b4f98cdSJohannes Weiner } 21019b4f98cdSJohannes Weiner } 2102e82e0561SMel Gorman 2103e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2104e82e0561SMel Gorman continue; 2105e82e0561SMel Gorman 21069b4f98cdSJohannes Weiner /* 2107e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 21081a501907SMel Gorman * requested. Ensure that the anon and file LRUs are scanned 2109e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2110e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2111e82e0561SMel Gorman * proportional to the original scan target. 2112e82e0561SMel Gorman */ 2113e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2114e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2115e82e0561SMel Gorman 21161a501907SMel Gorman /* 21171a501907SMel Gorman * It's just vindictive to attack the larger once the smaller 21181a501907SMel Gorman * has gone to zero. And given the way we stop scanning the 21191a501907SMel Gorman * smaller below, this makes sure that we only make one nudge 21201a501907SMel Gorman * towards proportionality once we've got nr_to_reclaim. 21211a501907SMel Gorman */ 21221a501907SMel Gorman if (!nr_file || !nr_anon) 21231a501907SMel Gorman break; 21241a501907SMel Gorman 2125e82e0561SMel Gorman if (nr_file > nr_anon) { 2126e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2127e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2128e82e0561SMel Gorman lru = LRU_BASE; 2129e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2130e82e0561SMel Gorman } else { 2131e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2132e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2133e82e0561SMel Gorman lru = LRU_FILE; 2134e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2135e82e0561SMel Gorman } 2136e82e0561SMel Gorman 2137e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2138e82e0561SMel Gorman nr[lru] = 0; 2139e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2140e82e0561SMel Gorman 2141e82e0561SMel Gorman /* 2142e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2143e82e0561SMel Gorman * scan target and the percentage scanning already complete 2144e82e0561SMel Gorman */ 2145e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2146e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2147e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2148e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2149e82e0561SMel Gorman 2150e82e0561SMel Gorman lru += LRU_ACTIVE; 2151e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2152e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2153e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2154e82e0561SMel Gorman 2155e82e0561SMel Gorman scan_adjusted = true; 21569b4f98cdSJohannes Weiner } 21579b4f98cdSJohannes Weiner blk_finish_plug(&plug); 21589b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 21599b4f98cdSJohannes Weiner 21609b4f98cdSJohannes Weiner /* 21619b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 21629b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 21639b4f98cdSJohannes Weiner */ 21649b4f98cdSJohannes Weiner if (inactive_anon_is_low(lruvec)) 21659b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 21669b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 21679b4f98cdSJohannes Weiner 21689b4f98cdSJohannes Weiner throttle_vm_writeout(sc->gfp_mask); 21699b4f98cdSJohannes Weiner } 21709b4f98cdSJohannes Weiner 217123b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 21729e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 217323b9da55SMel Gorman { 2174d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 217523b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 21769e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 217723b9da55SMel Gorman return true; 217823b9da55SMel Gorman 217923b9da55SMel Gorman return false; 218023b9da55SMel Gorman } 218123b9da55SMel Gorman 21824f98a2feSRik van Riel /* 218323b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 218423b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 218523b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 218623b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 218723b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 21883e7d3449SMel Gorman */ 21899b4f98cdSJohannes Weiner static inline bool should_continue_reclaim(struct zone *zone, 21903e7d3449SMel Gorman unsigned long nr_reclaimed, 21913e7d3449SMel Gorman unsigned long nr_scanned, 21923e7d3449SMel Gorman struct scan_control *sc) 21933e7d3449SMel Gorman { 21943e7d3449SMel Gorman unsigned long pages_for_compaction; 21953e7d3449SMel Gorman unsigned long inactive_lru_pages; 21963e7d3449SMel Gorman 21973e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 21989e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 21993e7d3449SMel Gorman return false; 22003e7d3449SMel Gorman 22012876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 22022876592fSMel Gorman if (sc->gfp_mask & __GFP_REPEAT) { 22033e7d3449SMel Gorman /* 22042876592fSMel Gorman * For __GFP_REPEAT allocations, stop reclaiming if the 22052876592fSMel Gorman * full LRU list has been scanned and we are still failing 22062876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 22072876592fSMel Gorman * expensive but a __GFP_REPEAT caller really wants to succeed 22083e7d3449SMel Gorman */ 22093e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 22103e7d3449SMel Gorman return false; 22112876592fSMel Gorman } else { 22122876592fSMel Gorman /* 22132876592fSMel Gorman * For non-__GFP_REPEAT allocations which can presumably 22142876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 22152876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 22162876592fSMel Gorman * pages that were scanned. This will return to the 22172876592fSMel Gorman * caller faster at the risk reclaim/compaction and 22182876592fSMel Gorman * the resulting allocation attempt fails 22192876592fSMel Gorman */ 22202876592fSMel Gorman if (!nr_reclaimed) 22212876592fSMel Gorman return false; 22222876592fSMel Gorman } 22233e7d3449SMel Gorman 22243e7d3449SMel Gorman /* 22253e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 22263e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 22273e7d3449SMel Gorman */ 22283e7d3449SMel Gorman pages_for_compaction = (2UL << sc->order); 22299b4f98cdSJohannes Weiner inactive_lru_pages = zone_page_state(zone, NR_INACTIVE_FILE); 2230ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 22319b4f98cdSJohannes Weiner inactive_lru_pages += zone_page_state(zone, NR_INACTIVE_ANON); 22323e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 22333e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 22343e7d3449SMel Gorman return true; 22353e7d3449SMel Gorman 22363e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 22379b4f98cdSJohannes Weiner switch (compaction_suitable(zone, sc->order)) { 22383e7d3449SMel Gorman case COMPACT_PARTIAL: 22393e7d3449SMel Gorman case COMPACT_CONTINUE: 22403e7d3449SMel Gorman return false; 22413e7d3449SMel Gorman default: 22423e7d3449SMel Gorman return true; 22433e7d3449SMel Gorman } 22443e7d3449SMel Gorman } 22453e7d3449SMel Gorman 22462344d7e4SJohannes Weiner static bool shrink_zone(struct zone *zone, struct scan_control *sc) 2247f16015fbSJohannes Weiner { 22489b4f98cdSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 22492344d7e4SJohannes Weiner bool reclaimable = false; 22509b4f98cdSJohannes Weiner 22519b4f98cdSJohannes Weiner do { 22525660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 22535660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 22545660048cSJohannes Weiner .zone = zone, 22559e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 22565660048cSJohannes Weiner }; 2257694fbc0fSAndrew Morton struct mem_cgroup *memcg; 22585660048cSJohannes Weiner 22599b4f98cdSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 22609b4f98cdSJohannes Weiner nr_scanned = sc->nr_scanned; 22619b4f98cdSJohannes Weiner 2262694fbc0fSAndrew Morton memcg = mem_cgroup_iter(root, NULL, &reclaim); 2263694fbc0fSAndrew Morton do { 22649b4f98cdSJohannes Weiner struct lruvec *lruvec; 226502695175SJohannes Weiner int swappiness; 22669b4f98cdSJohannes Weiner 22679b4f98cdSJohannes Weiner lruvec = mem_cgroup_zone_lruvec(zone, memcg); 226802695175SJohannes Weiner swappiness = mem_cgroup_swappiness(memcg); 22695660048cSJohannes Weiner 227002695175SJohannes Weiner shrink_lruvec(lruvec, swappiness, sc); 2271f9be23d6SKonstantin Khlebnikov 22725660048cSJohannes Weiner /* 2273a394cb8eSMichal Hocko * Direct reclaim and kswapd have to scan all memory 2274a394cb8eSMichal Hocko * cgroups to fulfill the overall scan target for the 22759b4f98cdSJohannes Weiner * zone. 2276a394cb8eSMichal Hocko * 2277a394cb8eSMichal Hocko * Limit reclaim, on the other hand, only cares about 2278a394cb8eSMichal Hocko * nr_to_reclaim pages to be reclaimed and it will 2279a394cb8eSMichal Hocko * retry with decreasing priority if one round over the 2280a394cb8eSMichal Hocko * whole hierarchy is not sufficient. 22815660048cSJohannes Weiner */ 2282a394cb8eSMichal Hocko if (!global_reclaim(sc) && 2283a394cb8eSMichal Hocko sc->nr_reclaimed >= sc->nr_to_reclaim) { 22845660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 22855660048cSJohannes Weiner break; 22865660048cSJohannes Weiner } 2287694fbc0fSAndrew Morton memcg = mem_cgroup_iter(root, memcg, &reclaim); 2288694fbc0fSAndrew Morton } while (memcg); 228970ddf637SAnton Vorontsov 229070ddf637SAnton Vorontsov vmpressure(sc->gfp_mask, sc->target_mem_cgroup, 229170ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 229270ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 229370ddf637SAnton Vorontsov 22942344d7e4SJohannes Weiner if (sc->nr_reclaimed - nr_reclaimed) 22952344d7e4SJohannes Weiner reclaimable = true; 22962344d7e4SJohannes Weiner 22979b4f98cdSJohannes Weiner } while (should_continue_reclaim(zone, sc->nr_reclaimed - nr_reclaimed, 22989b4f98cdSJohannes Weiner sc->nr_scanned - nr_scanned, sc)); 22992344d7e4SJohannes Weiner 23002344d7e4SJohannes Weiner return reclaimable; 2301f16015fbSJohannes Weiner } 2302f16015fbSJohannes Weiner 2303fe4b1b24SMel Gorman /* Returns true if compaction should go ahead for a high-order request */ 23040b06496aSJohannes Weiner static inline bool compaction_ready(struct zone *zone, int order) 2305fe4b1b24SMel Gorman { 2306fe4b1b24SMel Gorman unsigned long balance_gap, watermark; 2307fe4b1b24SMel Gorman bool watermark_ok; 2308fe4b1b24SMel Gorman 2309fe4b1b24SMel Gorman /* 2310fe4b1b24SMel Gorman * Compaction takes time to run and there are potentially other 2311fe4b1b24SMel Gorman * callers using the pages just freed. Continue reclaiming until 2312fe4b1b24SMel Gorman * there is a buffer of free pages available to give compaction 2313fe4b1b24SMel Gorman * a reasonable chance of completing and allocating the page 2314fe4b1b24SMel Gorman */ 23154be89a34SJianyu Zhan balance_gap = min(low_wmark_pages(zone), DIV_ROUND_UP( 23164be89a34SJianyu Zhan zone->managed_pages, KSWAPD_ZONE_BALANCE_GAP_RATIO)); 23170b06496aSJohannes Weiner watermark = high_wmark_pages(zone) + balance_gap + (2UL << order); 2318fe4b1b24SMel Gorman watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0); 2319fe4b1b24SMel Gorman 2320fe4b1b24SMel Gorman /* 2321fe4b1b24SMel Gorman * If compaction is deferred, reclaim up to a point where 2322fe4b1b24SMel Gorman * compaction will have a chance of success when re-enabled 2323fe4b1b24SMel Gorman */ 23240b06496aSJohannes Weiner if (compaction_deferred(zone, order)) 2325fe4b1b24SMel Gorman return watermark_ok; 2326fe4b1b24SMel Gorman 2327fe4b1b24SMel Gorman /* If compaction is not ready to start, keep reclaiming */ 23280b06496aSJohannes Weiner if (!compaction_suitable(zone, order)) 2329fe4b1b24SMel Gorman return false; 2330fe4b1b24SMel Gorman 2331fe4b1b24SMel Gorman return watermark_ok; 2332fe4b1b24SMel Gorman } 2333fe4b1b24SMel Gorman 23341da177e4SLinus Torvalds /* 23351da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 23361da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 23371da177e4SLinus Torvalds * request. 23381da177e4SLinus Torvalds * 233941858966SMel Gorman * We reclaim from a zone even if that zone is over high_wmark_pages(zone). 234041858966SMel Gorman * Because: 23411da177e4SLinus Torvalds * a) The caller may be trying to free *extra* pages to satisfy a higher-order 23421da177e4SLinus Torvalds * allocation or 234341858966SMel Gorman * b) The target zone may be at high_wmark_pages(zone) but the lower zones 234441858966SMel Gorman * must go *over* high_wmark_pages(zone) to satisfy the `incremental min' 234541858966SMel Gorman * zone defense algorithm. 23461da177e4SLinus Torvalds * 23471da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 23481da177e4SLinus Torvalds * scan then give up on it. 23492344d7e4SJohannes Weiner * 23502344d7e4SJohannes Weiner * Returns true if a zone was reclaimable. 23511da177e4SLinus Torvalds */ 23522344d7e4SJohannes Weiner static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 23531da177e4SLinus Torvalds { 2354dd1a239fSMel Gorman struct zoneref *z; 235554a6eb5cSMel Gorman struct zone *zone; 23560608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 23570608f43dSAndrew Morton unsigned long nr_soft_scanned; 235865ec02cbSVladimir Davydov unsigned long lru_pages = 0; 235965ec02cbSVladimir Davydov struct reclaim_state *reclaim_state = current->reclaim_state; 2360619d0d76SWeijie Yang gfp_t orig_mask; 23613115cd91SVladimir Davydov struct shrink_control shrink = { 23623115cd91SVladimir Davydov .gfp_mask = sc->gfp_mask, 23633115cd91SVladimir Davydov }; 23649bbc04eeSWeijie Yang enum zone_type requested_highidx = gfp_zone(sc->gfp_mask); 23652344d7e4SJohannes Weiner bool reclaimable = false; 23661cfb419bSKAMEZAWA Hiroyuki 2367cc715d99SMel Gorman /* 2368cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2369cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2370cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2371cc715d99SMel Gorman */ 2372619d0d76SWeijie Yang orig_mask = sc->gfp_mask; 2373cc715d99SMel Gorman if (buffer_heads_over_limit) 2374cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 2375cc715d99SMel Gorman 23763115cd91SVladimir Davydov nodes_clear(shrink.nodes_to_scan); 237765ec02cbSVladimir Davydov 2378d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2379d4debc66SMel Gorman gfp_zone(sc->gfp_mask), sc->nodemask) { 2380f3fe6512SCon Kolivas if (!populated_zone(zone)) 23811da177e4SLinus Torvalds continue; 23821cfb419bSKAMEZAWA Hiroyuki /* 23831cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 23841cfb419bSKAMEZAWA Hiroyuki * to global LRU. 23851cfb419bSKAMEZAWA Hiroyuki */ 238689b5fae5SJohannes Weiner if (global_reclaim(sc)) { 238702a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 23881da177e4SLinus Torvalds continue; 238965ec02cbSVladimir Davydov 239065ec02cbSVladimir Davydov lru_pages += zone_reclaimable_pages(zone); 23913115cd91SVladimir Davydov node_set(zone_to_nid(zone), shrink.nodes_to_scan); 239265ec02cbSVladimir Davydov 23936e543d57SLisa Du if (sc->priority != DEF_PRIORITY && 23946e543d57SLisa Du !zone_reclaimable(zone)) 23951da177e4SLinus Torvalds continue; /* Let kswapd poll it */ 23960b06496aSJohannes Weiner 2397e0887c19SRik van Riel /* 2398e0c23279SMel Gorman * If we already have plenty of memory free for 2399e0c23279SMel Gorman * compaction in this zone, don't free any more. 2400e0c23279SMel Gorman * Even though compaction is invoked for any 2401e0c23279SMel Gorman * non-zero order, only frequent costly order 2402e0c23279SMel Gorman * reclamation is disruptive enough to become a 2403c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2404c7cfa37bSCopot Alexandru * page allocations. 2405e0887c19SRik van Riel */ 24060b06496aSJohannes Weiner if (IS_ENABLED(CONFIG_COMPACTION) && 24070b06496aSJohannes Weiner sc->order > PAGE_ALLOC_COSTLY_ORDER && 24080b06496aSJohannes Weiner zonelist_zone_idx(z) <= requested_highidx && 24090b06496aSJohannes Weiner compaction_ready(zone, sc->order)) { 24100b06496aSJohannes Weiner sc->compaction_ready = true; 2411e0887c19SRik van Riel continue; 2412e0887c19SRik van Riel } 24130b06496aSJohannes Weiner 24140608f43dSAndrew Morton /* 24150608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 24160608f43dSAndrew Morton * and returns the number of reclaimed pages and 24170608f43dSAndrew Morton * scanned pages. This works for global memory pressure 24180608f43dSAndrew Morton * and balancing, not for a memcg's limit. 24190608f43dSAndrew Morton */ 24200608f43dSAndrew Morton nr_soft_scanned = 0; 24210608f43dSAndrew Morton nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 24220608f43dSAndrew Morton sc->order, sc->gfp_mask, 24230608f43dSAndrew Morton &nr_soft_scanned); 24240608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 24250608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 24262344d7e4SJohannes Weiner if (nr_soft_reclaimed) 24272344d7e4SJohannes Weiner reclaimable = true; 2428ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2429ac34a1a3SKAMEZAWA Hiroyuki } 2430d149e3b2SYing Han 24312344d7e4SJohannes Weiner if (shrink_zone(zone, sc)) 24322344d7e4SJohannes Weiner reclaimable = true; 24332344d7e4SJohannes Weiner 24342344d7e4SJohannes Weiner if (global_reclaim(sc) && 24352344d7e4SJohannes Weiner !reclaimable && zone_reclaimable(zone)) 24362344d7e4SJohannes Weiner reclaimable = true; 24371da177e4SLinus Torvalds } 2438e0c23279SMel Gorman 243965ec02cbSVladimir Davydov /* 244065ec02cbSVladimir Davydov * Don't shrink slabs when reclaiming memory from over limit cgroups 244165ec02cbSVladimir Davydov * but do shrink slab at least once when aborting reclaim for 244265ec02cbSVladimir Davydov * compaction to avoid unevenly scanning file/anon LRU pages over slab 244365ec02cbSVladimir Davydov * pages. 244465ec02cbSVladimir Davydov */ 244565ec02cbSVladimir Davydov if (global_reclaim(sc)) { 24463115cd91SVladimir Davydov shrink_slab(&shrink, sc->nr_scanned, lru_pages); 244765ec02cbSVladimir Davydov if (reclaim_state) { 244865ec02cbSVladimir Davydov sc->nr_reclaimed += reclaim_state->reclaimed_slab; 244965ec02cbSVladimir Davydov reclaim_state->reclaimed_slab = 0; 245065ec02cbSVladimir Davydov } 245165ec02cbSVladimir Davydov } 245265ec02cbSVladimir Davydov 2453619d0d76SWeijie Yang /* 2454619d0d76SWeijie Yang * Restore to original mask to avoid the impact on the caller if we 2455619d0d76SWeijie Yang * promoted it to __GFP_HIGHMEM. 2456619d0d76SWeijie Yang */ 2457619d0d76SWeijie Yang sc->gfp_mask = orig_mask; 2458d1908362SMinchan Kim 24592344d7e4SJohannes Weiner return reclaimable; 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds /* 24631da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 24641da177e4SLinus Torvalds * 24651da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 24661da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 24671da177e4SLinus Torvalds * 24681da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 24691da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 24705b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 24715b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 24725b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 24735b0830cbSJens Axboe * work, and the allocation attempt will fail. 2474a41f24eaSNishanth Aravamudan * 2475a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2476a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 24771da177e4SLinus Torvalds */ 2478dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 24793115cd91SVladimir Davydov struct scan_control *sc) 24801da177e4SLinus Torvalds { 248169e05944SAndrew Morton unsigned long total_scanned = 0; 248222fba335SKOSAKI Motohiro unsigned long writeback_threshold; 24832344d7e4SJohannes Weiner bool zones_reclaimable; 24841da177e4SLinus Torvalds 2485873b4771SKeika Kobayashi delayacct_freepages_start(); 2486873b4771SKeika Kobayashi 248789b5fae5SJohannes Weiner if (global_reclaim(sc)) 2488f8891e5eSChristoph Lameter count_vm_event(ALLOCSTALL); 24891da177e4SLinus Torvalds 24909e3b2f8cSKonstantin Khlebnikov do { 249170ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 249270ddf637SAnton Vorontsov sc->priority); 249366e1707bSBalbir Singh sc->nr_scanned = 0; 24942344d7e4SJohannes Weiner zones_reclaimable = shrink_zones(zonelist, sc); 2495e0c23279SMel Gorman 249666e1707bSBalbir Singh total_scanned += sc->nr_scanned; 2497bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 24980b06496aSJohannes Weiner break; 24990b06496aSJohannes Weiner 25000b06496aSJohannes Weiner if (sc->compaction_ready) 25010b06496aSJohannes Weiner break; 25021da177e4SLinus Torvalds 25031da177e4SLinus Torvalds /* 25040e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 25050e50ce3bSMinchan Kim * writepage even in laptop mode. 25060e50ce3bSMinchan Kim */ 25070e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 25080e50ce3bSMinchan Kim sc->may_writepage = 1; 25090e50ce3bSMinchan Kim 25100e50ce3bSMinchan Kim /* 25111da177e4SLinus Torvalds * Try to write back as many pages as we just scanned. This 25121da177e4SLinus Torvalds * tends to cause slow streaming writers to write data to the 25131da177e4SLinus Torvalds * disk smoothly, at the dirtying rate, which is nice. But 25141da177e4SLinus Torvalds * that's undesirable in laptop mode, where we *want* lumpy 25151da177e4SLinus Torvalds * writeout. So in laptop mode, write out the whole world. 25161da177e4SLinus Torvalds */ 251722fba335SKOSAKI Motohiro writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2; 251822fba335SKOSAKI Motohiro if (total_scanned > writeback_threshold) { 25190e175a18SCurt Wohlgemuth wakeup_flusher_threads(laptop_mode ? 0 : total_scanned, 25200e175a18SCurt Wohlgemuth WB_REASON_TRY_TO_FREE_PAGES); 252166e1707bSBalbir Singh sc->may_writepage = 1; 25221da177e4SLinus Torvalds } 25230b06496aSJohannes Weiner } while (--sc->priority >= 0); 2524bb21c7ceSKOSAKI Motohiro 2525873b4771SKeika Kobayashi delayacct_freepages_end(); 2526873b4771SKeika Kobayashi 2527bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 2528bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 2529bb21c7ceSKOSAKI Motohiro 25300cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 25310b06496aSJohannes Weiner if (sc->compaction_ready) 25327335084dSMel Gorman return 1; 25337335084dSMel Gorman 25342344d7e4SJohannes Weiner /* Any of the zones still reclaimable? Don't OOM. */ 25352344d7e4SJohannes Weiner if (zones_reclaimable) 2536bb21c7ceSKOSAKI Motohiro return 1; 2537bb21c7ceSKOSAKI Motohiro 2538bb21c7ceSKOSAKI Motohiro return 0; 25391da177e4SLinus Torvalds } 25401da177e4SLinus Torvalds 25415515061dSMel Gorman static bool pfmemalloc_watermark_ok(pg_data_t *pgdat) 25425515061dSMel Gorman { 25435515061dSMel Gorman struct zone *zone; 25445515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 25455515061dSMel Gorman unsigned long free_pages = 0; 25465515061dSMel Gorman int i; 25475515061dSMel Gorman bool wmark_ok; 25485515061dSMel Gorman 25495515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 25505515061dSMel Gorman zone = &pgdat->node_zones[i]; 2551675becceSMel Gorman if (!populated_zone(zone)) 2552675becceSMel Gorman continue; 2553675becceSMel Gorman 25545515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 25555515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 25565515061dSMel Gorman } 25575515061dSMel Gorman 2558675becceSMel Gorman /* If there are no reserves (unexpected config) then do not throttle */ 2559675becceSMel Gorman if (!pfmemalloc_reserve) 2560675becceSMel Gorman return true; 2561675becceSMel Gorman 25625515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 25635515061dSMel Gorman 25645515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 25655515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 25665515061dSMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, 25675515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 25685515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 25695515061dSMel Gorman } 25705515061dSMel Gorman 25715515061dSMel Gorman return wmark_ok; 25725515061dSMel Gorman } 25735515061dSMel Gorman 25745515061dSMel Gorman /* 25755515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 25765515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 25775515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 257850694c28SMel Gorman * when the low watermark is reached. 257950694c28SMel Gorman * 258050694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 258150694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 25825515061dSMel Gorman */ 258350694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 25845515061dSMel Gorman nodemask_t *nodemask) 25855515061dSMel Gorman { 2586675becceSMel Gorman struct zoneref *z; 25875515061dSMel Gorman struct zone *zone; 2588675becceSMel Gorman pg_data_t *pgdat = NULL; 25895515061dSMel Gorman 25905515061dSMel Gorman /* 25915515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 25925515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 25935515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 25945515061dSMel Gorman * committing a transaction where throttling it could forcing other 25955515061dSMel Gorman * processes to block on log_wait_commit(). 25965515061dSMel Gorman */ 25975515061dSMel Gorman if (current->flags & PF_KTHREAD) 259850694c28SMel Gorman goto out; 259950694c28SMel Gorman 260050694c28SMel Gorman /* 260150694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 260250694c28SMel Gorman * It should return quickly so it can exit and free its memory 260350694c28SMel Gorman */ 260450694c28SMel Gorman if (fatal_signal_pending(current)) 260550694c28SMel Gorman goto out; 26065515061dSMel Gorman 2607675becceSMel Gorman /* 2608675becceSMel Gorman * Check if the pfmemalloc reserves are ok by finding the first node 2609675becceSMel Gorman * with a usable ZONE_NORMAL or lower zone. The expectation is that 2610675becceSMel Gorman * GFP_KERNEL will be required for allocating network buffers when 2611675becceSMel Gorman * swapping over the network so ZONE_HIGHMEM is unusable. 2612675becceSMel Gorman * 2613675becceSMel Gorman * Throttling is based on the first usable node and throttled processes 2614675becceSMel Gorman * wait on a queue until kswapd makes progress and wakes them. There 2615675becceSMel Gorman * is an affinity then between processes waking up and where reclaim 2616675becceSMel Gorman * progress has been made assuming the process wakes on the same node. 2617675becceSMel Gorman * More importantly, processes running on remote nodes will not compete 2618675becceSMel Gorman * for remote pfmemalloc reserves and processes on different nodes 2619675becceSMel Gorman * should make reasonable progress. 2620675becceSMel Gorman */ 2621675becceSMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2622675becceSMel Gorman gfp_mask, nodemask) { 2623675becceSMel Gorman if (zone_idx(zone) > ZONE_NORMAL) 2624675becceSMel Gorman continue; 2625675becceSMel Gorman 2626675becceSMel Gorman /* Throttle based on the first usable node */ 26275515061dSMel Gorman pgdat = zone->zone_pgdat; 26285515061dSMel Gorman if (pfmemalloc_watermark_ok(pgdat)) 262950694c28SMel Gorman goto out; 2630675becceSMel Gorman break; 2631675becceSMel Gorman } 2632675becceSMel Gorman 2633675becceSMel Gorman /* If no zone was usable by the allocation flags then do not throttle */ 2634675becceSMel Gorman if (!pgdat) 2635675becceSMel Gorman goto out; 26365515061dSMel Gorman 263768243e76SMel Gorman /* Account for the throttling */ 263868243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 263968243e76SMel Gorman 26405515061dSMel Gorman /* 26415515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 26425515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 26435515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 26445515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 26455515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 26465515061dSMel Gorman * second before continuing. 26475515061dSMel Gorman */ 26485515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 26495515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 26505515061dSMel Gorman pfmemalloc_watermark_ok(pgdat), HZ); 265150694c28SMel Gorman 265250694c28SMel Gorman goto check_pending; 26535515061dSMel Gorman } 26545515061dSMel Gorman 26555515061dSMel Gorman /* Throttle until kswapd wakes the process */ 26565515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 26575515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)); 265850694c28SMel Gorman 265950694c28SMel Gorman check_pending: 266050694c28SMel Gorman if (fatal_signal_pending(current)) 266150694c28SMel Gorman return true; 266250694c28SMel Gorman 266350694c28SMel Gorman out: 266450694c28SMel Gorman return false; 26655515061dSMel Gorman } 26665515061dSMel Gorman 2667dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 2668327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 266966e1707bSBalbir Singh { 267033906bc5SMel Gorman unsigned long nr_reclaimed; 267166e1707bSBalbir Singh struct scan_control sc = { 267222fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2673*ee814fe2SJohannes Weiner .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), 2674*ee814fe2SJohannes Weiner .order = order, 2675*ee814fe2SJohannes Weiner .nodemask = nodemask, 2676*ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 2677*ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 2678a6dc60f8SJohannes Weiner .may_unmap = 1, 26792e2e4259SKOSAKI Motohiro .may_swap = 1, 268066e1707bSBalbir Singh }; 268166e1707bSBalbir Singh 26825515061dSMel Gorman /* 268350694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 268450694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 268550694c28SMel Gorman * point. 26865515061dSMel Gorman */ 268750694c28SMel Gorman if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask)) 26885515061dSMel Gorman return 1; 26895515061dSMel Gorman 269033906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 269133906bc5SMel Gorman sc.may_writepage, 269233906bc5SMel Gorman gfp_mask); 269333906bc5SMel Gorman 26943115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 269533906bc5SMel Gorman 269633906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 269733906bc5SMel Gorman 269833906bc5SMel Gorman return nr_reclaimed; 269966e1707bSBalbir Singh } 270066e1707bSBalbir Singh 2701c255a458SAndrew Morton #ifdef CONFIG_MEMCG 270266e1707bSBalbir Singh 270372835c86SJohannes Weiner unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg, 27044e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 27050ae5e89cSYing Han struct zone *zone, 27060ae5e89cSYing Han unsigned long *nr_scanned) 27074e416953SBalbir Singh { 27084e416953SBalbir Singh struct scan_control sc = { 2709b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2710*ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 27114e416953SBalbir Singh .may_writepage = !laptop_mode, 27124e416953SBalbir Singh .may_unmap = 1, 27134e416953SBalbir Singh .may_swap = !noswap, 27144e416953SBalbir Singh }; 2715f9be23d6SKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 271602695175SJohannes Weiner int swappiness = mem_cgroup_swappiness(memcg); 27170ae5e89cSYing Han 27184e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 27194e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 2720bdce6d9eSKOSAKI Motohiro 27219e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 2722bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2723bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2724bdce6d9eSKOSAKI Motohiro 27254e416953SBalbir Singh /* 27264e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 27274e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 27284e416953SBalbir Singh * if we don't reclaim here, the shrink_zone from balance_pgdat 27294e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 27304e416953SBalbir Singh * the priority and make it zero. 27314e416953SBalbir Singh */ 273202695175SJohannes Weiner shrink_lruvec(lruvec, swappiness, &sc); 2733bdce6d9eSKOSAKI Motohiro 2734bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 2735bdce6d9eSKOSAKI Motohiro 27360ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 27374e416953SBalbir Singh return sc.nr_reclaimed; 27384e416953SBalbir Singh } 27394e416953SBalbir Singh 274072835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 27418c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 2742185efc0fSJohannes Weiner bool noswap) 274366e1707bSBalbir Singh { 27444e416953SBalbir Singh struct zonelist *zonelist; 2745bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 2746889976dbSYing Han int nid; 274766e1707bSBalbir Singh struct scan_control sc = { 2748*ee814fe2SJohannes Weiner .nr_to_reclaim = SWAP_CLUSTER_MAX, 2749*ee814fe2SJohannes Weiner .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 2750*ee814fe2SJohannes Weiner (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 2751*ee814fe2SJohannes Weiner .target_mem_cgroup = memcg, 2752*ee814fe2SJohannes Weiner .priority = DEF_PRIORITY, 275366e1707bSBalbir Singh .may_writepage = !laptop_mode, 2754a6dc60f8SJohannes Weiner .may_unmap = 1, 27552e2e4259SKOSAKI Motohiro .may_swap = !noswap, 2756a09ed5e0SYing Han }; 275766e1707bSBalbir Singh 2758889976dbSYing Han /* 2759889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 2760889976dbSYing Han * take care of from where we get pages. So the node where we start the 2761889976dbSYing Han * scan does not need to be the current node. 2762889976dbSYing Han */ 276372835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 2764889976dbSYing Han 2765889976dbSYing Han zonelist = NODE_DATA(nid)->node_zonelists; 2766bdce6d9eSKOSAKI Motohiro 2767bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 2768bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2769bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2770bdce6d9eSKOSAKI Motohiro 27713115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 2772bdce6d9eSKOSAKI Motohiro 2773bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 2774bdce6d9eSKOSAKI Motohiro 2775bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 277666e1707bSBalbir Singh } 277766e1707bSBalbir Singh #endif 277866e1707bSBalbir Singh 27799e3b2f8cSKonstantin Khlebnikov static void age_active_anon(struct zone *zone, struct scan_control *sc) 2780f16015fbSJohannes Weiner { 2781b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 2782b95a2f2dSJohannes Weiner 2783b95a2f2dSJohannes Weiner if (!total_swap_pages) 2784b95a2f2dSJohannes Weiner return; 2785b95a2f2dSJohannes Weiner 2786b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 2787b95a2f2dSJohannes Weiner do { 2788c56d5c7dSKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 2789f16015fbSJohannes Weiner 2790c56d5c7dSKonstantin Khlebnikov if (inactive_anon_is_low(lruvec)) 27911a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 27929e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 2793b95a2f2dSJohannes Weiner 2794b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 2795b95a2f2dSJohannes Weiner } while (memcg); 2796f16015fbSJohannes Weiner } 2797f16015fbSJohannes Weiner 279860cefed4SJohannes Weiner static bool zone_balanced(struct zone *zone, int order, 279960cefed4SJohannes Weiner unsigned long balance_gap, int classzone_idx) 280060cefed4SJohannes Weiner { 280160cefed4SJohannes Weiner if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone) + 280260cefed4SJohannes Weiner balance_gap, classzone_idx, 0)) 280360cefed4SJohannes Weiner return false; 280460cefed4SJohannes Weiner 2805d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && order && 2806d84da3f9SKirill A. Shutemov !compaction_suitable(zone, order)) 280760cefed4SJohannes Weiner return false; 280860cefed4SJohannes Weiner 280960cefed4SJohannes Weiner return true; 281060cefed4SJohannes Weiner } 281160cefed4SJohannes Weiner 28121741c877SMel Gorman /* 28134ae0a48bSZlatko Calusic * pgdat_balanced() is used when checking if a node is balanced. 28144ae0a48bSZlatko Calusic * 28154ae0a48bSZlatko Calusic * For order-0, all zones must be balanced! 28164ae0a48bSZlatko Calusic * 28174ae0a48bSZlatko Calusic * For high-order allocations only zones that meet watermarks and are in a 28184ae0a48bSZlatko Calusic * zone allowed by the callers classzone_idx are added to balanced_pages. The 28194ae0a48bSZlatko Calusic * total of balanced pages must be at least 25% of the zones allowed by 28204ae0a48bSZlatko Calusic * classzone_idx for the node to be considered balanced. Forcing all zones to 28214ae0a48bSZlatko Calusic * be balanced for high orders can cause excessive reclaim when there are 28224ae0a48bSZlatko Calusic * imbalanced zones. 28231741c877SMel Gorman * The choice of 25% is due to 28241741c877SMel Gorman * o a 16M DMA zone that is balanced will not balance a zone on any 28251741c877SMel Gorman * reasonable sized machine 28261741c877SMel Gorman * o On all other machines, the top zone must be at least a reasonable 282725985edcSLucas De Marchi * percentage of the middle zones. For example, on 32-bit x86, highmem 28281741c877SMel Gorman * would need to be at least 256M for it to be balance a whole node. 28291741c877SMel Gorman * Similarly, on x86-64 the Normal zone would need to be at least 1G 28301741c877SMel Gorman * to balance a node on its own. These seemed like reasonable ratios. 28311741c877SMel Gorman */ 28324ae0a48bSZlatko Calusic static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx) 28331741c877SMel Gorman { 2834b40da049SJiang Liu unsigned long managed_pages = 0; 28354ae0a48bSZlatko Calusic unsigned long balanced_pages = 0; 28361741c877SMel Gorman int i; 28371741c877SMel Gorman 28384ae0a48bSZlatko Calusic /* Check the watermark levels */ 28394ae0a48bSZlatko Calusic for (i = 0; i <= classzone_idx; i++) { 28404ae0a48bSZlatko Calusic struct zone *zone = pgdat->node_zones + i; 28411741c877SMel Gorman 28424ae0a48bSZlatko Calusic if (!populated_zone(zone)) 28434ae0a48bSZlatko Calusic continue; 28444ae0a48bSZlatko Calusic 2845b40da049SJiang Liu managed_pages += zone->managed_pages; 28464ae0a48bSZlatko Calusic 28474ae0a48bSZlatko Calusic /* 28484ae0a48bSZlatko Calusic * A special case here: 28494ae0a48bSZlatko Calusic * 28504ae0a48bSZlatko Calusic * balance_pgdat() skips over all_unreclaimable after 28514ae0a48bSZlatko Calusic * DEF_PRIORITY. Effectively, it considers them balanced so 28524ae0a48bSZlatko Calusic * they must be considered balanced here as well! 28534ae0a48bSZlatko Calusic */ 28546e543d57SLisa Du if (!zone_reclaimable(zone)) { 2855b40da049SJiang Liu balanced_pages += zone->managed_pages; 28564ae0a48bSZlatko Calusic continue; 28574ae0a48bSZlatko Calusic } 28584ae0a48bSZlatko Calusic 28594ae0a48bSZlatko Calusic if (zone_balanced(zone, order, 0, i)) 2860b40da049SJiang Liu balanced_pages += zone->managed_pages; 28614ae0a48bSZlatko Calusic else if (!order) 28624ae0a48bSZlatko Calusic return false; 28634ae0a48bSZlatko Calusic } 28644ae0a48bSZlatko Calusic 28654ae0a48bSZlatko Calusic if (order) 2866b40da049SJiang Liu return balanced_pages >= (managed_pages >> 2); 28674ae0a48bSZlatko Calusic else 28684ae0a48bSZlatko Calusic return true; 28691741c877SMel Gorman } 28701741c877SMel Gorman 28715515061dSMel Gorman /* 28725515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 28735515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 28745515061dSMel Gorman * 28755515061dSMel Gorman * Returns true if kswapd is ready to sleep 28765515061dSMel Gorman */ 28775515061dSMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, long remaining, 2878dc83edd9SMel Gorman int classzone_idx) 2879f50de2d3SMel Gorman { 2880f50de2d3SMel Gorman /* If a direct reclaimer woke kswapd within HZ/10, it's premature */ 2881f50de2d3SMel Gorman if (remaining) 28825515061dSMel Gorman return false; 28835515061dSMel Gorman 28845515061dSMel Gorman /* 28855515061dSMel Gorman * There is a potential race between when kswapd checks its watermarks 28865515061dSMel Gorman * and a process gets throttled. There is also a potential race if 28875515061dSMel Gorman * processes get throttled, kswapd wakes, a large process exits therby 28885515061dSMel Gorman * balancing the zones that causes kswapd to miss a wakeup. If kswapd 28895515061dSMel Gorman * is going to sleep, no process should be sleeping on pfmemalloc_wait 28905515061dSMel Gorman * so wake them now if necessary. If necessary, processes will wake 28915515061dSMel Gorman * kswapd and get throttled again 28925515061dSMel Gorman */ 28935515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait)) { 28945515061dSMel Gorman wake_up(&pgdat->pfmemalloc_wait); 28955515061dSMel Gorman return false; 28965515061dSMel Gorman } 2897f50de2d3SMel Gorman 28984ae0a48bSZlatko Calusic return pgdat_balanced(pgdat, order, classzone_idx); 2899f50de2d3SMel Gorman } 2900f50de2d3SMel Gorman 29011da177e4SLinus Torvalds /* 290275485363SMel Gorman * kswapd shrinks the zone by the number of pages required to reach 290375485363SMel Gorman * the high watermark. 2904b8e83b94SMel Gorman * 2905b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 2906283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 2907283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 290875485363SMel Gorman */ 2909b8e83b94SMel Gorman static bool kswapd_shrink_zone(struct zone *zone, 29107c954f6dSMel Gorman int classzone_idx, 291175485363SMel Gorman struct scan_control *sc, 29122ab44f43SMel Gorman unsigned long lru_pages, 29132ab44f43SMel Gorman unsigned long *nr_attempted) 291475485363SMel Gorman { 29157c954f6dSMel Gorman int testorder = sc->order; 29167c954f6dSMel Gorman unsigned long balance_gap; 291775485363SMel Gorman struct reclaim_state *reclaim_state = current->reclaim_state; 291875485363SMel Gorman struct shrink_control shrink = { 291975485363SMel Gorman .gfp_mask = sc->gfp_mask, 292075485363SMel Gorman }; 29217c954f6dSMel Gorman bool lowmem_pressure; 292275485363SMel Gorman 292375485363SMel Gorman /* Reclaim above the high watermark. */ 292475485363SMel Gorman sc->nr_to_reclaim = max(SWAP_CLUSTER_MAX, high_wmark_pages(zone)); 29257c954f6dSMel Gorman 29267c954f6dSMel Gorman /* 29277c954f6dSMel Gorman * Kswapd reclaims only single pages with compaction enabled. Trying 29287c954f6dSMel Gorman * too hard to reclaim until contiguous free pages have become 29297c954f6dSMel Gorman * available can hurt performance by evicting too much useful data 29307c954f6dSMel Gorman * from memory. Do not reclaim more than needed for compaction. 29317c954f6dSMel Gorman */ 29327c954f6dSMel Gorman if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 29337c954f6dSMel Gorman compaction_suitable(zone, sc->order) != 29347c954f6dSMel Gorman COMPACT_SKIPPED) 29357c954f6dSMel Gorman testorder = 0; 29367c954f6dSMel Gorman 29377c954f6dSMel Gorman /* 29387c954f6dSMel Gorman * We put equal pressure on every zone, unless one zone has way too 29397c954f6dSMel Gorman * many pages free already. The "too many pages" is defined as the 29407c954f6dSMel Gorman * high wmark plus a "gap" where the gap is either the low 29417c954f6dSMel Gorman * watermark or 1% of the zone, whichever is smaller. 29427c954f6dSMel Gorman */ 29434be89a34SJianyu Zhan balance_gap = min(low_wmark_pages(zone), DIV_ROUND_UP( 29444be89a34SJianyu Zhan zone->managed_pages, KSWAPD_ZONE_BALANCE_GAP_RATIO)); 29457c954f6dSMel Gorman 29467c954f6dSMel Gorman /* 29477c954f6dSMel Gorman * If there is no low memory pressure or the zone is balanced then no 29487c954f6dSMel Gorman * reclaim is necessary 29497c954f6dSMel Gorman */ 29507c954f6dSMel Gorman lowmem_pressure = (buffer_heads_over_limit && is_highmem(zone)); 29517c954f6dSMel Gorman if (!lowmem_pressure && zone_balanced(zone, testorder, 29527c954f6dSMel Gorman balance_gap, classzone_idx)) 29537c954f6dSMel Gorman return true; 29547c954f6dSMel Gorman 295575485363SMel Gorman shrink_zone(zone, sc); 29560ce3d744SDave Chinner nodes_clear(shrink.nodes_to_scan); 29570ce3d744SDave Chinner node_set(zone_to_nid(zone), shrink.nodes_to_scan); 295875485363SMel Gorman 295975485363SMel Gorman reclaim_state->reclaimed_slab = 0; 29606e543d57SLisa Du shrink_slab(&shrink, sc->nr_scanned, lru_pages); 296175485363SMel Gorman sc->nr_reclaimed += reclaim_state->reclaimed_slab; 296275485363SMel Gorman 29632ab44f43SMel Gorman /* Account for the number of pages attempted to reclaim */ 29642ab44f43SMel Gorman *nr_attempted += sc->nr_to_reclaim; 29652ab44f43SMel Gorman 2966283aba9fSMel Gorman zone_clear_flag(zone, ZONE_WRITEBACK); 2967283aba9fSMel Gorman 29687c954f6dSMel Gorman /* 29697c954f6dSMel Gorman * If a zone reaches its high watermark, consider it to be no longer 29707c954f6dSMel Gorman * congested. It's possible there are dirty pages backed by congested 29717c954f6dSMel Gorman * BDIs but as pressure is relieved, speculatively avoid congestion 29727c954f6dSMel Gorman * waits. 29737c954f6dSMel Gorman */ 29746e543d57SLisa Du if (zone_reclaimable(zone) && 29757c954f6dSMel Gorman zone_balanced(zone, testorder, 0, classzone_idx)) { 29767c954f6dSMel Gorman zone_clear_flag(zone, ZONE_CONGESTED); 29777c954f6dSMel Gorman zone_clear_flag(zone, ZONE_TAIL_LRU_DIRTY); 29787c954f6dSMel Gorman } 29797c954f6dSMel Gorman 2980b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 298175485363SMel Gorman } 298275485363SMel Gorman 298375485363SMel Gorman /* 29841da177e4SLinus Torvalds * For kswapd, balance_pgdat() will work across all this node's zones until 298541858966SMel Gorman * they are all at high_wmark_pages(zone). 29861da177e4SLinus Torvalds * 29870abdee2bSMel Gorman * Returns the final order kswapd was reclaiming at 29881da177e4SLinus Torvalds * 29891da177e4SLinus Torvalds * There is special handling here for zones which are full of pinned pages. 29901da177e4SLinus Torvalds * This can happen if the pages are all mlocked, or if they are all used by 29911da177e4SLinus Torvalds * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. 29921da177e4SLinus Torvalds * What we do is to detect the case where all pages in the zone have been 29931da177e4SLinus Torvalds * scanned twice and there has been zero successful reclaim. Mark the zone as 29941da177e4SLinus Torvalds * dead and from now on, only perform a short scan. Basically we're polling 29951da177e4SLinus Torvalds * the zone for when the problem goes away. 29961da177e4SLinus Torvalds * 29971da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 299841858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 299941858966SMel Gorman * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the 300041858966SMel Gorman * lower zones regardless of the number of free pages in the lower zones. This 300141858966SMel Gorman * interoperates with the page allocator fallback scheme to ensure that aging 300241858966SMel Gorman * of pages is balanced across the zones. 30031da177e4SLinus Torvalds */ 300499504748SMel Gorman static unsigned long balance_pgdat(pg_data_t *pgdat, int order, 3005dc83edd9SMel Gorman int *classzone_idx) 30061da177e4SLinus Torvalds { 30071da177e4SLinus Torvalds int i; 300899504748SMel Gorman int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ 30090608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 30100608f43dSAndrew Morton unsigned long nr_soft_scanned; 3011179e9639SAndrew Morton struct scan_control sc = { 3012179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 3013*ee814fe2SJohannes Weiner .order = order, 3014b8e83b94SMel Gorman .priority = DEF_PRIORITY, 3015*ee814fe2SJohannes Weiner .may_writepage = !laptop_mode, 3016a6dc60f8SJohannes Weiner .may_unmap = 1, 30172e2e4259SKOSAKI Motohiro .may_swap = 1, 3018179e9639SAndrew Morton }; 3019f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 30201da177e4SLinus Torvalds 30219e3b2f8cSKonstantin Khlebnikov do { 30221da177e4SLinus Torvalds unsigned long lru_pages = 0; 30232ab44f43SMel Gorman unsigned long nr_attempted = 0; 3024b8e83b94SMel Gorman bool raise_priority = true; 30252ab44f43SMel Gorman bool pgdat_needs_compaction = (order > 0); 3026b8e83b94SMel Gorman 3027b8e83b94SMel Gorman sc.nr_reclaimed = 0; 30281da177e4SLinus Torvalds 30291da177e4SLinus Torvalds /* 30301da177e4SLinus Torvalds * Scan in the highmem->dma direction for the highest 30311da177e4SLinus Torvalds * zone which needs scanning 30321da177e4SLinus Torvalds */ 30331da177e4SLinus Torvalds for (i = pgdat->nr_zones - 1; i >= 0; i--) { 30341da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 30351da177e4SLinus Torvalds 3036f3fe6512SCon Kolivas if (!populated_zone(zone)) 30371da177e4SLinus Torvalds continue; 30381da177e4SLinus Torvalds 30396e543d57SLisa Du if (sc.priority != DEF_PRIORITY && 30406e543d57SLisa Du !zone_reclaimable(zone)) 30411da177e4SLinus Torvalds continue; 30421da177e4SLinus Torvalds 3043556adecbSRik van Riel /* 3044556adecbSRik van Riel * Do some background aging of the anon list, to give 3045556adecbSRik van Riel * pages a chance to be referenced before reclaiming. 3046556adecbSRik van Riel */ 30479e3b2f8cSKonstantin Khlebnikov age_active_anon(zone, &sc); 3048556adecbSRik van Riel 3049cc715d99SMel Gorman /* 3050cc715d99SMel Gorman * If the number of buffer_heads in the machine 3051cc715d99SMel Gorman * exceeds the maximum allowed level and this node 3052cc715d99SMel Gorman * has a highmem zone, force kswapd to reclaim from 3053cc715d99SMel Gorman * it to relieve lowmem pressure. 3054cc715d99SMel Gorman */ 3055cc715d99SMel Gorman if (buffer_heads_over_limit && is_highmem_idx(i)) { 3056cc715d99SMel Gorman end_zone = i; 3057cc715d99SMel Gorman break; 3058cc715d99SMel Gorman } 3059cc715d99SMel Gorman 306060cefed4SJohannes Weiner if (!zone_balanced(zone, order, 0, 0)) { 30611da177e4SLinus Torvalds end_zone = i; 3062e1dbeda6SAndrew Morton break; 3063439423f6SShaohua Li } else { 3064d43006d5SMel Gorman /* 3065d43006d5SMel Gorman * If balanced, clear the dirty and congested 3066d43006d5SMel Gorman * flags 3067d43006d5SMel Gorman */ 3068439423f6SShaohua Li zone_clear_flag(zone, ZONE_CONGESTED); 3069d43006d5SMel Gorman zone_clear_flag(zone, ZONE_TAIL_LRU_DIRTY); 30701da177e4SLinus Torvalds } 30711da177e4SLinus Torvalds } 3072dafcb73eSZlatko Calusic 3073b8e83b94SMel Gorman if (i < 0) 30741da177e4SLinus Torvalds goto out; 3075e1dbeda6SAndrew Morton 30761da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 30771da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 30781da177e4SLinus Torvalds 30792ab44f43SMel Gorman if (!populated_zone(zone)) 30802ab44f43SMel Gorman continue; 30812ab44f43SMel Gorman 3082adea02a1SWu Fengguang lru_pages += zone_reclaimable_pages(zone); 30832ab44f43SMel Gorman 30842ab44f43SMel Gorman /* 30852ab44f43SMel Gorman * If any zone is currently balanced then kswapd will 30862ab44f43SMel Gorman * not call compaction as it is expected that the 30872ab44f43SMel Gorman * necessary pages are already available. 30882ab44f43SMel Gorman */ 30892ab44f43SMel Gorman if (pgdat_needs_compaction && 30902ab44f43SMel Gorman zone_watermark_ok(zone, order, 30912ab44f43SMel Gorman low_wmark_pages(zone), 30922ab44f43SMel Gorman *classzone_idx, 0)) 30932ab44f43SMel Gorman pgdat_needs_compaction = false; 30941da177e4SLinus Torvalds } 30951da177e4SLinus Torvalds 30961da177e4SLinus Torvalds /* 3097b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 3098b7ea3c41SMel Gorman * even in laptop mode. 3099b7ea3c41SMel Gorman */ 3100b7ea3c41SMel Gorman if (sc.priority < DEF_PRIORITY - 2) 3101b7ea3c41SMel Gorman sc.may_writepage = 1; 3102b7ea3c41SMel Gorman 3103b7ea3c41SMel Gorman /* 31041da177e4SLinus Torvalds * Now scan the zone in the dma->highmem direction, stopping 31051da177e4SLinus Torvalds * at the last zone which needs scanning. 31061da177e4SLinus Torvalds * 31071da177e4SLinus Torvalds * We do this because the page allocator works in the opposite 31081da177e4SLinus Torvalds * direction. This prevents the page allocator from allocating 31091da177e4SLinus Torvalds * pages behind kswapd's direction of progress, which would 31101da177e4SLinus Torvalds * cause too much scanning of the lower zones. 31111da177e4SLinus Torvalds */ 31121da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 31131da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 31141da177e4SLinus Torvalds 3115f3fe6512SCon Kolivas if (!populated_zone(zone)) 31161da177e4SLinus Torvalds continue; 31171da177e4SLinus Torvalds 31186e543d57SLisa Du if (sc.priority != DEF_PRIORITY && 31196e543d57SLisa Du !zone_reclaimable(zone)) 31201da177e4SLinus Torvalds continue; 31211da177e4SLinus Torvalds 31221da177e4SLinus Torvalds sc.nr_scanned = 0; 31234e416953SBalbir Singh 31240608f43dSAndrew Morton nr_soft_scanned = 0; 31250608f43dSAndrew Morton /* 31260608f43dSAndrew Morton * Call soft limit reclaim before calling shrink_zone. 31270608f43dSAndrew Morton */ 31280608f43dSAndrew Morton nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 31290608f43dSAndrew Morton order, sc.gfp_mask, 31300608f43dSAndrew Morton &nr_soft_scanned); 31310608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 31320608f43dSAndrew Morton 313332a4330dSRik van Riel /* 31347c954f6dSMel Gorman * There should be no need to raise the scanning 31357c954f6dSMel Gorman * priority if enough pages are already being scanned 31367c954f6dSMel Gorman * that that high watermark would be met at 100% 31377c954f6dSMel Gorman * efficiency. 313832a4330dSRik van Riel */ 31397c954f6dSMel Gorman if (kswapd_shrink_zone(zone, end_zone, &sc, 31407c954f6dSMel Gorman lru_pages, &nr_attempted)) 3141b8e83b94SMel Gorman raise_priority = false; 3142b8e83b94SMel Gorman } 3143d7868daeSMel Gorman 31445515061dSMel Gorman /* 31455515061dSMel Gorman * If the low watermark is met there is no need for processes 31465515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 31475515061dSMel Gorman * able to safely make forward progress. Wake them 31485515061dSMel Gorman */ 31495515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 31505515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)) 31515515061dSMel Gorman wake_up(&pgdat->pfmemalloc_wait); 31525515061dSMel Gorman 31531da177e4SLinus Torvalds /* 3154b8e83b94SMel Gorman * Fragmentation may mean that the system cannot be rebalanced 3155b8e83b94SMel Gorman * for high-order allocations in all zones. If twice the 3156b8e83b94SMel Gorman * allocation size has been reclaimed and the zones are still 3157b8e83b94SMel Gorman * not balanced then recheck the watermarks at order-0 to 3158b8e83b94SMel Gorman * prevent kswapd reclaiming excessively. Assume that a 3159b8e83b94SMel Gorman * process requested a high-order can direct reclaim/compact. 31601da177e4SLinus Torvalds */ 3161b8e83b94SMel Gorman if (order && sc.nr_reclaimed >= 2UL << order) 316273ce02e9SKOSAKI Motohiro order = sc.order = 0; 316373ce02e9SKOSAKI Motohiro 3164b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 3165b8e83b94SMel Gorman if (try_to_freeze() || kthread_should_stop()) 3166b8e83b94SMel Gorman break; 3167b8e83b94SMel Gorman 3168b8e83b94SMel Gorman /* 31692ab44f43SMel Gorman * Compact if necessary and kswapd is reclaiming at least the 31702ab44f43SMel Gorman * high watermark number of pages as requsted 31712ab44f43SMel Gorman */ 31722ab44f43SMel Gorman if (pgdat_needs_compaction && sc.nr_reclaimed > nr_attempted) 31732ab44f43SMel Gorman compact_pgdat(pgdat, order); 31742ab44f43SMel Gorman 31752ab44f43SMel Gorman /* 3176b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3177b8e83b94SMel Gorman * progress in reclaiming pages 3178b8e83b94SMel Gorman */ 3179b8e83b94SMel Gorman if (raise_priority || !sc.nr_reclaimed) 3180b8e83b94SMel Gorman sc.priority--; 31819aa41348SMel Gorman } while (sc.priority >= 1 && 3182b8e83b94SMel Gorman !pgdat_balanced(pgdat, order, *classzone_idx)); 31831da177e4SLinus Torvalds 3184b8e83b94SMel Gorman out: 31850abdee2bSMel Gorman /* 31865515061dSMel Gorman * Return the order we were reclaiming at so prepare_kswapd_sleep() 31870abdee2bSMel Gorman * makes a decision on the order we were last reclaiming at. However, 31880abdee2bSMel Gorman * if another caller entered the allocator slow path while kswapd 31890abdee2bSMel Gorman * was awake, order will remain at the higher level 31900abdee2bSMel Gorman */ 3191dc83edd9SMel Gorman *classzone_idx = end_zone; 31920abdee2bSMel Gorman return order; 31931da177e4SLinus Torvalds } 31941da177e4SLinus Torvalds 3195dc83edd9SMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx) 3196f0bc0a60SKOSAKI Motohiro { 3197f0bc0a60SKOSAKI Motohiro long remaining = 0; 3198f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3199f0bc0a60SKOSAKI Motohiro 3200f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3201f0bc0a60SKOSAKI Motohiro return; 3202f0bc0a60SKOSAKI Motohiro 3203f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3204f0bc0a60SKOSAKI Motohiro 3205f0bc0a60SKOSAKI Motohiro /* Try to sleep for a short interval */ 32065515061dSMel Gorman if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) { 3207f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 3208f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3209f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3210f0bc0a60SKOSAKI Motohiro } 3211f0bc0a60SKOSAKI Motohiro 3212f0bc0a60SKOSAKI Motohiro /* 3213f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3214f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3215f0bc0a60SKOSAKI Motohiro */ 32165515061dSMel Gorman if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) { 3217f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3218f0bc0a60SKOSAKI Motohiro 3219f0bc0a60SKOSAKI Motohiro /* 3220f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3221f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3222f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3223f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3224f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3225f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3226f0bc0a60SKOSAKI Motohiro */ 3227f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 32281c7e7f6cSAaditya Kumar 322962997027SMel Gorman /* 323062997027SMel Gorman * Compaction records what page blocks it recently failed to 323162997027SMel Gorman * isolate pages from and skips them in the future scanning. 323262997027SMel Gorman * When kswapd is going to sleep, it is reasonable to assume 323362997027SMel Gorman * that pages and compaction may succeed so reset the cache. 323462997027SMel Gorman */ 323562997027SMel Gorman reset_isolation_suitable(pgdat); 323662997027SMel Gorman 32371c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3238f0bc0a60SKOSAKI Motohiro schedule(); 32391c7e7f6cSAaditya Kumar 3240f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3241f0bc0a60SKOSAKI Motohiro } else { 3242f0bc0a60SKOSAKI Motohiro if (remaining) 3243f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3244f0bc0a60SKOSAKI Motohiro else 3245f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3246f0bc0a60SKOSAKI Motohiro } 3247f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3248f0bc0a60SKOSAKI Motohiro } 3249f0bc0a60SKOSAKI Motohiro 32501da177e4SLinus Torvalds /* 32511da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 32521da177e4SLinus Torvalds * from the init process. 32531da177e4SLinus Torvalds * 32541da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 32551da177e4SLinus Torvalds * free memory available even if there is no other activity 32561da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 32571da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 32581da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 32591da177e4SLinus Torvalds * 32601da177e4SLinus Torvalds * If there are applications that are active memory-allocators 32611da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 32621da177e4SLinus Torvalds */ 32631da177e4SLinus Torvalds static int kswapd(void *p) 32641da177e4SLinus Torvalds { 3265215ddd66SMel Gorman unsigned long order, new_order; 3266d2ebd0f6SAlex,Shi unsigned balanced_order; 3267215ddd66SMel Gorman int classzone_idx, new_classzone_idx; 3268d2ebd0f6SAlex,Shi int balanced_classzone_idx; 32691da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 32701da177e4SLinus Torvalds struct task_struct *tsk = current; 3271f0bc0a60SKOSAKI Motohiro 32721da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 32731da177e4SLinus Torvalds .reclaimed_slab = 0, 32741da177e4SLinus Torvalds }; 3275a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 32761da177e4SLinus Torvalds 3277cf40bd16SNick Piggin lockdep_set_current_reclaim_state(GFP_KERNEL); 3278cf40bd16SNick Piggin 3279174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3280c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 32811da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 32821da177e4SLinus Torvalds 32831da177e4SLinus Torvalds /* 32841da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 32851da177e4SLinus Torvalds * and that if we need more memory we should get access to it 32861da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 32871da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 32881da177e4SLinus Torvalds * 32891da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 32901da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 32911da177e4SLinus Torvalds * page out something else, and this flag essentially protects 32921da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 32931da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 32941da177e4SLinus Torvalds */ 3295930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 329683144186SRafael J. Wysocki set_freezable(); 32971da177e4SLinus Torvalds 3298215ddd66SMel Gorman order = new_order = 0; 3299d2ebd0f6SAlex,Shi balanced_order = 0; 3300215ddd66SMel Gorman classzone_idx = new_classzone_idx = pgdat->nr_zones - 1; 3301d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 33021da177e4SLinus Torvalds for ( ; ; ) { 33036f6313d4SJeff Liu bool ret; 33043e1d1d28SChristoph Lameter 3305215ddd66SMel Gorman /* 3306215ddd66SMel Gorman * If the last balance_pgdat was unsuccessful it's unlikely a 3307215ddd66SMel Gorman * new request of a similar or harder type will succeed soon 3308215ddd66SMel Gorman * so consider going to sleep on the basis we reclaimed at 3309215ddd66SMel Gorman */ 3310d2ebd0f6SAlex,Shi if (balanced_classzone_idx >= new_classzone_idx && 3311d2ebd0f6SAlex,Shi balanced_order == new_order) { 33121da177e4SLinus Torvalds new_order = pgdat->kswapd_max_order; 331399504748SMel Gorman new_classzone_idx = pgdat->classzone_idx; 33141da177e4SLinus Torvalds pgdat->kswapd_max_order = 0; 3315215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 3316215ddd66SMel Gorman } 3317215ddd66SMel Gorman 331899504748SMel Gorman if (order < new_order || classzone_idx > new_classzone_idx) { 33191da177e4SLinus Torvalds /* 33201da177e4SLinus Torvalds * Don't sleep if someone wants a larger 'order' 332199504748SMel Gorman * allocation or has tigher zone constraints 33221da177e4SLinus Torvalds */ 33231da177e4SLinus Torvalds order = new_order; 332499504748SMel Gorman classzone_idx = new_classzone_idx; 33251da177e4SLinus Torvalds } else { 3326d2ebd0f6SAlex,Shi kswapd_try_to_sleep(pgdat, balanced_order, 3327d2ebd0f6SAlex,Shi balanced_classzone_idx); 33281da177e4SLinus Torvalds order = pgdat->kswapd_max_order; 332999504748SMel Gorman classzone_idx = pgdat->classzone_idx; 3330f0dfcde0SAlex,Shi new_order = order; 3331f0dfcde0SAlex,Shi new_classzone_idx = classzone_idx; 33324d40502eSMel Gorman pgdat->kswapd_max_order = 0; 3333215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 33341da177e4SLinus Torvalds } 33351da177e4SLinus Torvalds 33368fe23e05SDavid Rientjes ret = try_to_freeze(); 33378fe23e05SDavid Rientjes if (kthread_should_stop()) 33388fe23e05SDavid Rientjes break; 33398fe23e05SDavid Rientjes 33408fe23e05SDavid Rientjes /* 33418fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 33428fe23e05SDavid Rientjes * after returning from the refrigerator 3343b1296cc4SRafael J. Wysocki */ 334433906bc5SMel Gorman if (!ret) { 334533906bc5SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, order); 3346d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 3347d2ebd0f6SAlex,Shi balanced_order = balance_pgdat(pgdat, order, 3348d2ebd0f6SAlex,Shi &balanced_classzone_idx); 33491da177e4SLinus Torvalds } 335033906bc5SMel Gorman } 3351b0a8cc58STakamori Yamaguchi 335271abdc15SJohannes Weiner tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); 3353b0a8cc58STakamori Yamaguchi current->reclaim_state = NULL; 335471abdc15SJohannes Weiner lockdep_clear_current_reclaim_state(); 335571abdc15SJohannes Weiner 33561da177e4SLinus Torvalds return 0; 33571da177e4SLinus Torvalds } 33581da177e4SLinus Torvalds 33591da177e4SLinus Torvalds /* 33601da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 33611da177e4SLinus Torvalds */ 336299504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) 33631da177e4SLinus Torvalds { 33641da177e4SLinus Torvalds pg_data_t *pgdat; 33651da177e4SLinus Torvalds 3366f3fe6512SCon Kolivas if (!populated_zone(zone)) 33671da177e4SLinus Torvalds return; 33681da177e4SLinus Torvalds 336902a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 33701da177e4SLinus Torvalds return; 337188f5acf8SMel Gorman pgdat = zone->zone_pgdat; 337299504748SMel Gorman if (pgdat->kswapd_max_order < order) { 337388f5acf8SMel Gorman pgdat->kswapd_max_order = order; 337499504748SMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx); 337599504748SMel Gorman } 33768d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 33771da177e4SLinus Torvalds return; 3378892f795dSJohannes Weiner if (zone_balanced(zone, order, 0, 0)) 337988f5acf8SMel Gorman return; 338088f5acf8SMel Gorman 338188f5acf8SMel Gorman trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order); 33828d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 33831da177e4SLinus Torvalds } 33841da177e4SLinus Torvalds 3385c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 33861da177e4SLinus Torvalds /* 33877b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3388d6277db4SRafael J. Wysocki * freed pages. 3389d6277db4SRafael J. Wysocki * 3390d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3391d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3392d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 33931da177e4SLinus Torvalds */ 33947b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 33951da177e4SLinus Torvalds { 3396d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 3397d6277db4SRafael J. Wysocki struct scan_control sc = { 33987b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 3399*ee814fe2SJohannes Weiner .gfp_mask = GFP_HIGHUSER_MOVABLE, 34009e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 3401*ee814fe2SJohannes Weiner .may_writepage = 1, 3402*ee814fe2SJohannes Weiner .may_unmap = 1, 3403*ee814fe2SJohannes Weiner .may_swap = 1, 3404*ee814fe2SJohannes Weiner .hibernation_mode = 1, 34051da177e4SLinus Torvalds }; 34067b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 34077b51755cSKOSAKI Motohiro struct task_struct *p = current; 34087b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 34091da177e4SLinus Torvalds 34107b51755cSKOSAKI Motohiro p->flags |= PF_MEMALLOC; 34117b51755cSKOSAKI Motohiro lockdep_set_current_reclaim_state(sc.gfp_mask); 3412d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 34137b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 3414d6277db4SRafael J. Wysocki 34153115cd91SVladimir Davydov nr_reclaimed = do_try_to_free_pages(zonelist, &sc); 3416d6277db4SRafael J. Wysocki 34177b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 34187b51755cSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 34197b51755cSKOSAKI Motohiro p->flags &= ~PF_MEMALLOC; 3420d6277db4SRafael J. Wysocki 34217b51755cSKOSAKI Motohiro return nr_reclaimed; 34221da177e4SLinus Torvalds } 3423c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 34241da177e4SLinus Torvalds 34251da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 34261da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 34271da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 34281da177e4SLinus Torvalds restore their cpu bindings. */ 3429fcb35a9bSGreg Kroah-Hartman static int cpu_callback(struct notifier_block *nfb, unsigned long action, 3430fcb35a9bSGreg Kroah-Hartman void *hcpu) 34311da177e4SLinus Torvalds { 343258c0a4a7SYasunori Goto int nid; 34331da177e4SLinus Torvalds 34348bb78442SRafael J. Wysocki if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 343548fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 3436c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3437a70f7302SRusty Russell const struct cpumask *mask; 3438a70f7302SRusty Russell 3439a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3440c5f59f08SMike Travis 34413e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 34421da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3443c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 34441da177e4SLinus Torvalds } 34451da177e4SLinus Torvalds } 34461da177e4SLinus Torvalds return NOTIFY_OK; 34471da177e4SLinus Torvalds } 34481da177e4SLinus Torvalds 34493218ae14SYasunori Goto /* 34503218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 34513218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 34523218ae14SYasunori Goto */ 34533218ae14SYasunori Goto int kswapd_run(int nid) 34543218ae14SYasunori Goto { 34553218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 34563218ae14SYasunori Goto int ret = 0; 34573218ae14SYasunori Goto 34583218ae14SYasunori Goto if (pgdat->kswapd) 34593218ae14SYasunori Goto return 0; 34603218ae14SYasunori Goto 34613218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 34623218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 34633218ae14SYasunori Goto /* failure at boot is fatal */ 34643218ae14SYasunori Goto BUG_ON(system_state == SYSTEM_BOOTING); 3465d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 3466d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 3467d72515b8SXishi Qiu pgdat->kswapd = NULL; 34683218ae14SYasunori Goto } 34693218ae14SYasunori Goto return ret; 34703218ae14SYasunori Goto } 34713218ae14SYasunori Goto 34728fe23e05SDavid Rientjes /* 3473d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 3474bfc8c901SVladimir Davydov * hold mem_hotplug_begin/end(). 34758fe23e05SDavid Rientjes */ 34768fe23e05SDavid Rientjes void kswapd_stop(int nid) 34778fe23e05SDavid Rientjes { 34788fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 34798fe23e05SDavid Rientjes 3480d8adde17SJiang Liu if (kswapd) { 34818fe23e05SDavid Rientjes kthread_stop(kswapd); 3482d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 3483d8adde17SJiang Liu } 34848fe23e05SDavid Rientjes } 34858fe23e05SDavid Rientjes 34861da177e4SLinus Torvalds static int __init kswapd_init(void) 34871da177e4SLinus Torvalds { 34883218ae14SYasunori Goto int nid; 348969e05944SAndrew Morton 34901da177e4SLinus Torvalds swap_setup(); 349148fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 34923218ae14SYasunori Goto kswapd_run(nid); 34931da177e4SLinus Torvalds hotcpu_notifier(cpu_callback, 0); 34941da177e4SLinus Torvalds return 0; 34951da177e4SLinus Torvalds } 34961da177e4SLinus Torvalds 34971da177e4SLinus Torvalds module_init(kswapd_init) 34989eeff239SChristoph Lameter 34999eeff239SChristoph Lameter #ifdef CONFIG_NUMA 35009eeff239SChristoph Lameter /* 35019eeff239SChristoph Lameter * Zone reclaim mode 35029eeff239SChristoph Lameter * 35039eeff239SChristoph Lameter * If non-zero call zone_reclaim when the number of free pages falls below 35049eeff239SChristoph Lameter * the watermarks. 35059eeff239SChristoph Lameter */ 35069eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly; 35079eeff239SChristoph Lameter 35081b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 35097d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 35101b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 35111b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ 35121b2ffb78SChristoph Lameter 35139eeff239SChristoph Lameter /* 3514a92f7126SChristoph Lameter * Priority for ZONE_RECLAIM. This determines the fraction of pages 3515a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3516a92f7126SChristoph Lameter * a zone. 3517a92f7126SChristoph Lameter */ 3518a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4 3519a92f7126SChristoph Lameter 35209eeff239SChristoph Lameter /* 35219614634fSChristoph Lameter * Percentage of pages in a zone that must be unmapped for zone_reclaim to 35229614634fSChristoph Lameter * occur. 35239614634fSChristoph Lameter */ 35249614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 35259614634fSChristoph Lameter 35269614634fSChristoph Lameter /* 35270ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 35280ff38490SChristoph Lameter * slab reclaim needs to occur. 35290ff38490SChristoph Lameter */ 35300ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 35310ff38490SChristoph Lameter 353290afa5deSMel Gorman static inline unsigned long zone_unmapped_file_pages(struct zone *zone) 353390afa5deSMel Gorman { 353490afa5deSMel Gorman unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED); 353590afa5deSMel Gorman unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) + 353690afa5deSMel Gorman zone_page_state(zone, NR_ACTIVE_FILE); 353790afa5deSMel Gorman 353890afa5deSMel Gorman /* 353990afa5deSMel Gorman * It's possible for there to be more file mapped pages than 354090afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 354190afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 354290afa5deSMel Gorman */ 354390afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 354490afa5deSMel Gorman } 354590afa5deSMel Gorman 354690afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 354790afa5deSMel Gorman static long zone_pagecache_reclaimable(struct zone *zone) 354890afa5deSMel Gorman { 354990afa5deSMel Gorman long nr_pagecache_reclaimable; 355090afa5deSMel Gorman long delta = 0; 355190afa5deSMel Gorman 355290afa5deSMel Gorman /* 355390afa5deSMel Gorman * If RECLAIM_SWAP is set, then all file pages are considered 355490afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 355590afa5deSMel Gorman * pages like swapcache and zone_unmapped_file_pages() provides 355690afa5deSMel Gorman * a better estimate 355790afa5deSMel Gorman */ 355890afa5deSMel Gorman if (zone_reclaim_mode & RECLAIM_SWAP) 355990afa5deSMel Gorman nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES); 356090afa5deSMel Gorman else 356190afa5deSMel Gorman nr_pagecache_reclaimable = zone_unmapped_file_pages(zone); 356290afa5deSMel Gorman 356390afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 356490afa5deSMel Gorman if (!(zone_reclaim_mode & RECLAIM_WRITE)) 356590afa5deSMel Gorman delta += zone_page_state(zone, NR_FILE_DIRTY); 356690afa5deSMel Gorman 356790afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 356890afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 356990afa5deSMel Gorman delta = nr_pagecache_reclaimable; 357090afa5deSMel Gorman 357190afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 357290afa5deSMel Gorman } 357390afa5deSMel Gorman 35740ff38490SChristoph Lameter /* 35759eeff239SChristoph Lameter * Try to free up some pages from this zone through reclaim. 35769eeff239SChristoph Lameter */ 3577179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 35789eeff239SChristoph Lameter { 35797fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 358069e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 35819eeff239SChristoph Lameter struct task_struct *p = current; 35829eeff239SChristoph Lameter struct reclaim_state reclaim_state; 3583179e9639SAndrew Morton struct scan_control sc = { 358462b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 358521caf2fcSMing Lei .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), 3586bd2f6199SJohannes Weiner .order = order, 35879e3b2f8cSKonstantin Khlebnikov .priority = ZONE_RECLAIM_PRIORITY, 3588*ee814fe2SJohannes Weiner .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), 3589*ee814fe2SJohannes Weiner .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP), 3590*ee814fe2SJohannes Weiner .may_swap = 1, 3591179e9639SAndrew Morton }; 3592a09ed5e0SYing Han struct shrink_control shrink = { 3593a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 3594a09ed5e0SYing Han }; 359515748048SKOSAKI Motohiro unsigned long nr_slab_pages0, nr_slab_pages1; 35969eeff239SChristoph Lameter 35979eeff239SChristoph Lameter cond_resched(); 3598d4f7796eSChristoph Lameter /* 3599d4f7796eSChristoph Lameter * We need to be able to allocate from the reserves for RECLAIM_SWAP 3600d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 3601d4f7796eSChristoph Lameter * and RECLAIM_SWAP. 3602d4f7796eSChristoph Lameter */ 3603d4f7796eSChristoph Lameter p->flags |= PF_MEMALLOC | PF_SWAPWRITE; 360476ca542dSKOSAKI Motohiro lockdep_set_current_reclaim_state(gfp_mask); 36059eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 36069eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 3607c84db23cSChristoph Lameter 360890afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) { 3609a92f7126SChristoph Lameter /* 36100ff38490SChristoph Lameter * Free memory by calling shrink zone with increasing 36110ff38490SChristoph Lameter * priorities until we have enough memory freed. 3612a92f7126SChristoph Lameter */ 3613a92f7126SChristoph Lameter do { 36149e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, &sc); 36159e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 36160ff38490SChristoph Lameter } 3617a92f7126SChristoph Lameter 361815748048SKOSAKI Motohiro nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 361915748048SKOSAKI Motohiro if (nr_slab_pages0 > zone->min_slab_pages) { 36202a16e3f4SChristoph Lameter /* 36217fb2d46dSChristoph Lameter * shrink_slab() does not currently allow us to determine how 36220ff38490SChristoph Lameter * many pages were freed in this zone. So we take the current 36230ff38490SChristoph Lameter * number of slab pages and shake the slab until it is reduced 36240ff38490SChristoph Lameter * by the same nr_pages that we used for reclaiming unmapped 36250ff38490SChristoph Lameter * pages. 36262a16e3f4SChristoph Lameter */ 36270ce3d744SDave Chinner nodes_clear(shrink.nodes_to_scan); 36280ce3d744SDave Chinner node_set(zone_to_nid(zone), shrink.nodes_to_scan); 36294dc4b3d9SKOSAKI Motohiro for (;;) { 36304dc4b3d9SKOSAKI Motohiro unsigned long lru_pages = zone_reclaimable_pages(zone); 36314dc4b3d9SKOSAKI Motohiro 36324dc4b3d9SKOSAKI Motohiro /* No reclaimable slab or very low memory pressure */ 36331495f230SYing Han if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages)) 36344dc4b3d9SKOSAKI Motohiro break; 36354dc4b3d9SKOSAKI Motohiro 36364dc4b3d9SKOSAKI Motohiro /* Freed enough memory */ 36374dc4b3d9SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, 36384dc4b3d9SKOSAKI Motohiro NR_SLAB_RECLAIMABLE); 36394dc4b3d9SKOSAKI Motohiro if (nr_slab_pages1 + nr_pages <= nr_slab_pages0) 36404dc4b3d9SKOSAKI Motohiro break; 36414dc4b3d9SKOSAKI Motohiro } 364283e33a47SChristoph Lameter 364383e33a47SChristoph Lameter /* 364483e33a47SChristoph Lameter * Update nr_reclaimed by the number of slab pages we 364583e33a47SChristoph Lameter * reclaimed from this zone. 364683e33a47SChristoph Lameter */ 364715748048SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 364815748048SKOSAKI Motohiro if (nr_slab_pages1 < nr_slab_pages0) 364915748048SKOSAKI Motohiro sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1; 36502a16e3f4SChristoph Lameter } 36512a16e3f4SChristoph Lameter 36529eeff239SChristoph Lameter p->reclaim_state = NULL; 3653d4f7796eSChristoph Lameter current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); 365476ca542dSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 3655a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 36569eeff239SChristoph Lameter } 3657179e9639SAndrew Morton 3658179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 3659179e9639SAndrew Morton { 3660179e9639SAndrew Morton int node_id; 3661d773ed6bSDavid Rientjes int ret; 3662179e9639SAndrew Morton 3663179e9639SAndrew Morton /* 36640ff38490SChristoph Lameter * Zone reclaim reclaims unmapped file backed pages and 36650ff38490SChristoph Lameter * slab pages if we are over the defined limits. 366634aa1330SChristoph Lameter * 36679614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 36689614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 36699614634fSChristoph Lameter * thrown out if the zone is overallocated. So we do not reclaim 36709614634fSChristoph Lameter * if less than a specified percentage of the zone is used by 36719614634fSChristoph Lameter * unmapped file backed pages. 3672179e9639SAndrew Morton */ 367390afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages && 367490afa5deSMel Gorman zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages) 3675fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3676179e9639SAndrew Morton 36776e543d57SLisa Du if (!zone_reclaimable(zone)) 3678fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3679d773ed6bSDavid Rientjes 3680179e9639SAndrew Morton /* 3681d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 3682179e9639SAndrew Morton */ 3683d773ed6bSDavid Rientjes if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC)) 3684fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3685179e9639SAndrew Morton 3686179e9639SAndrew Morton /* 3687179e9639SAndrew Morton * Only run zone reclaim on the local zone or on zones that do not 3688179e9639SAndrew Morton * have associated processors. This will favor the local processor 3689179e9639SAndrew Morton * over remote processors and spread off node memory allocations 3690179e9639SAndrew Morton * as wide as possible. 3691179e9639SAndrew Morton */ 369289fa3024SChristoph Lameter node_id = zone_to_nid(zone); 369337c0708dSChristoph Lameter if (node_state(node_id, N_CPU) && node_id != numa_node_id()) 3694fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3695d773ed6bSDavid Rientjes 3696d773ed6bSDavid Rientjes if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED)) 3697fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3698fa5e084eSMel Gorman 3699d773ed6bSDavid Rientjes ret = __zone_reclaim(zone, gfp_mask, order); 3700d773ed6bSDavid Rientjes zone_clear_flag(zone, ZONE_RECLAIM_LOCKED); 3701d773ed6bSDavid Rientjes 370224cf7251SMel Gorman if (!ret) 370324cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 370424cf7251SMel Gorman 3705d773ed6bSDavid Rientjes return ret; 3706179e9639SAndrew Morton } 37079eeff239SChristoph Lameter #endif 3708894bc310SLee Schermerhorn 3709894bc310SLee Schermerhorn /* 3710894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 3711894bc310SLee Schermerhorn * @page: the page to test 3712894bc310SLee Schermerhorn * 3713894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 371439b5f29aSHugh Dickins * lists vs unevictable list. 3715894bc310SLee Schermerhorn * 3716894bc310SLee Schermerhorn * Reasons page might not be evictable: 3717ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 3718b291f000SNick Piggin * (2) page is part of an mlocked VMA 3719ba9ddf49SLee Schermerhorn * 3720894bc310SLee Schermerhorn */ 372139b5f29aSHugh Dickins int page_evictable(struct page *page) 3722894bc310SLee Schermerhorn { 372339b5f29aSHugh Dickins return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); 3724894bc310SLee Schermerhorn } 372589e004eaSLee Schermerhorn 372685046579SHugh Dickins #ifdef CONFIG_SHMEM 372789e004eaSLee Schermerhorn /** 372824513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 372924513264SHugh Dickins * @pages: array of pages to check 373024513264SHugh Dickins * @nr_pages: number of pages to check 373189e004eaSLee Schermerhorn * 373224513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 373385046579SHugh Dickins * 373485046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 373589e004eaSLee Schermerhorn */ 373624513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 373789e004eaSLee Schermerhorn { 3738925b7673SJohannes Weiner struct lruvec *lruvec; 373924513264SHugh Dickins struct zone *zone = NULL; 374024513264SHugh Dickins int pgscanned = 0; 374124513264SHugh Dickins int pgrescued = 0; 374289e004eaSLee Schermerhorn int i; 374389e004eaSLee Schermerhorn 374424513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 374524513264SHugh Dickins struct page *page = pages[i]; 374624513264SHugh Dickins struct zone *pagezone; 374789e004eaSLee Schermerhorn 374824513264SHugh Dickins pgscanned++; 374924513264SHugh Dickins pagezone = page_zone(page); 375089e004eaSLee Schermerhorn if (pagezone != zone) { 375189e004eaSLee Schermerhorn if (zone) 375289e004eaSLee Schermerhorn spin_unlock_irq(&zone->lru_lock); 375389e004eaSLee Schermerhorn zone = pagezone; 375489e004eaSLee Schermerhorn spin_lock_irq(&zone->lru_lock); 375589e004eaSLee Schermerhorn } 3756fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 375789e004eaSLee Schermerhorn 375824513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 375924513264SHugh Dickins continue; 376089e004eaSLee Schermerhorn 376139b5f29aSHugh Dickins if (page_evictable(page)) { 376224513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 376324513264SHugh Dickins 3764309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 376524513264SHugh Dickins ClearPageUnevictable(page); 3766fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 3767fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 376824513264SHugh Dickins pgrescued++; 376989e004eaSLee Schermerhorn } 377089e004eaSLee Schermerhorn } 377124513264SHugh Dickins 377224513264SHugh Dickins if (zone) { 377324513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 377424513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 377524513264SHugh Dickins spin_unlock_irq(&zone->lru_lock); 377624513264SHugh Dickins } 377785046579SHugh Dickins } 377885046579SHugh Dickins #endif /* CONFIG_SHMEM */ 3779af936a16SLee Schermerhorn 3780264e56d8SJohannes Weiner static void warn_scan_unevictable_pages(void) 3781af936a16SLee Schermerhorn { 3782264e56d8SJohannes Weiner printk_once(KERN_WARNING 378325bd91bdSKOSAKI Motohiro "%s: The scan_unevictable_pages sysctl/node-interface has been " 3784264e56d8SJohannes Weiner "disabled for lack of a legitimate use case. If you have " 378525bd91bdSKOSAKI Motohiro "one, please send an email to linux-mm@kvack.org.\n", 378625bd91bdSKOSAKI Motohiro current->comm); 3787af936a16SLee Schermerhorn } 3788af936a16SLee Schermerhorn 3789af936a16SLee Schermerhorn /* 3790af936a16SLee Schermerhorn * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of 3791af936a16SLee Schermerhorn * all nodes' unevictable lists for evictable pages 3792af936a16SLee Schermerhorn */ 3793af936a16SLee Schermerhorn unsigned long scan_unevictable_pages; 3794af936a16SLee Schermerhorn 3795af936a16SLee Schermerhorn int scan_unevictable_handler(struct ctl_table *table, int write, 37968d65af78SAlexey Dobriyan void __user *buffer, 3797af936a16SLee Schermerhorn size_t *length, loff_t *ppos) 3798af936a16SLee Schermerhorn { 3799264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 38008d65af78SAlexey Dobriyan proc_doulongvec_minmax(table, write, buffer, length, ppos); 3801af936a16SLee Schermerhorn scan_unevictable_pages = 0; 3802af936a16SLee Schermerhorn return 0; 3803af936a16SLee Schermerhorn } 3804af936a16SLee Schermerhorn 3805e4455abbSThadeu Lima de Souza Cascardo #ifdef CONFIG_NUMA 3806af936a16SLee Schermerhorn /* 3807af936a16SLee Schermerhorn * per node 'scan_unevictable_pages' attribute. On demand re-scan of 3808af936a16SLee Schermerhorn * a specified node's per zone unevictable lists for evictable pages. 3809af936a16SLee Schermerhorn */ 3810af936a16SLee Schermerhorn 381110fbcf4cSKay Sievers static ssize_t read_scan_unevictable_node(struct device *dev, 381210fbcf4cSKay Sievers struct device_attribute *attr, 3813af936a16SLee Schermerhorn char *buf) 3814af936a16SLee Schermerhorn { 3815264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3816af936a16SLee Schermerhorn return sprintf(buf, "0\n"); /* always zero; should fit... */ 3817af936a16SLee Schermerhorn } 3818af936a16SLee Schermerhorn 381910fbcf4cSKay Sievers static ssize_t write_scan_unevictable_node(struct device *dev, 382010fbcf4cSKay Sievers struct device_attribute *attr, 3821af936a16SLee Schermerhorn const char *buf, size_t count) 3822af936a16SLee Schermerhorn { 3823264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3824af936a16SLee Schermerhorn return 1; 3825af936a16SLee Schermerhorn } 3826af936a16SLee Schermerhorn 3827af936a16SLee Schermerhorn 382810fbcf4cSKay Sievers static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR, 3829af936a16SLee Schermerhorn read_scan_unevictable_node, 3830af936a16SLee Schermerhorn write_scan_unevictable_node); 3831af936a16SLee Schermerhorn 3832af936a16SLee Schermerhorn int scan_unevictable_register_node(struct node *node) 3833af936a16SLee Schermerhorn { 383410fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages); 3835af936a16SLee Schermerhorn } 3836af936a16SLee Schermerhorn 3837af936a16SLee Schermerhorn void scan_unevictable_unregister_node(struct node *node) 3838af936a16SLee Schermerhorn { 383910fbcf4cSKay Sievers device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages); 3840af936a16SLee Schermerhorn } 3841e4455abbSThadeu Lima de Souza Cascardo #endif 3842